【问题标题】:OpenGL ES - Colors are different with or without FBOOpenGL ES - 使用或不使用 FBO 颜色不同
【发布时间】:2017-08-22 17:52:50
【问题描述】:

我目前正在使用 OpenGL ES 2.0 在 iPhone 上绘制对象(图像、矩形)。

有两种模式:

A) 没有 FBO:

  1. 绘制对象
  2. 渲染到屏幕

B) 使用 FBO

  1. 绑定FBO
  2. 绘制对象
  3. 将 FBO 渲染到屏幕

场景绘制顺序为:

  1. 使用 glClearColor 绘制红色(或黑色)颜色(1、0、0、1)的背景
  2. 使用透明颜色 (1, 1, 1, 0.5) 绘制纹理

这是结果(左边没有 FBO,右边有 FBO):

1) 没有透明度的图像:两者都是一样的

2) 透明度设置为 0.5,红色背景:两者不同

3) 透明度设置为 0.5,黑色背景:与 1) 无透明度相同

这是我创建 FBO 的方法:

GLint maxRenderBufferSize;
glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE, &maxRenderBufferSize);

GLuint textureWidth = (GLuint)self.size.width;
GLuint textureHeight = (GLuint)self.size.height;

if(maxRenderBufferSize <= (GLint)textureWidth || maxRenderBufferSize <= (GLint)textureHeight)
    @throw [NSException exceptionWithName:TAG
                                   reason:@"FBO cannot allocate that much space"
                                 userInfo:nil];

glGenFramebuffers(1, &fbo);
glGenRenderbuffers(1, &fboBuffer);

glBindFramebuffer(GL_FRAMEBUFFER, fbo);

glGenTextures(1, &fboTexture);
glBindTexture(GL_TEXTURE_2D, fboTexture);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fboTexture, 0);

glBindRenderbuffer(GL_RENDERBUFFER, fboBuffer);

GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(status != GL_FRAMEBUFFER_COMPLETE)
    @throw [NSException exceptionWithName:TAG
                                   reason:@"Failed to initialize fbo"
                                 userInfo:nil];

这是我的片段着色器:

gl_FragColor = (v_Color * texture2D(u_Texture, v_TexCoordinate));

【问题讨论】:

    标签: ios opengl-es opengl-es-2.0


    【解决方案1】:

    发现了问题,这行是我的渲染-FBO-to-window函数中的问题:

    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    

    我刚刚删除了它,因为我不需要在此步骤中进行 alpha 混合。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-10
      • 2012-02-17
      • 1970-01-01
      相关资源
      最近更新 更多