OpenGL ES - 无论是否有FBO

时间:2017-08-22 09:31:07

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

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

有两种模式:

A)没有FBO:

  1. 绘制对象
  2. 渲染到屏幕
  3. B)使用FBO

    1. 绑定FBO
    2. 绘制对象
    3. 将FBO渲染到屏幕
    4. 场景绘制顺序是:

      1. 使用glClearColor绘制带红色(或黑色)颜色(1,0,0,1)的背景
      2. 使用透明度颜色(1,1,1,0.5)绘制纹理
      3. 以下是结果(左边没有FBO,右边是FBO):

        1)没有透明度的图像:两者都相同

        image 1 image 1

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

        image 2 image 3

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

        image 4 image 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));
        

1 个答案:

答案 0 :(得分:1)

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

glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

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