使用OpenGL绘制到帧缓冲区的alpha通道而忽略RGB值?

时间:2012-08-20 06:14:30

标签: iphone ios opengl-es textures

我的OpenGL视图的CAEAGLLayer上的opaque属性设置为NO。 OpenGL视图(检查板)后面有一个UIImageView。最初在OpenGL视图(Emu鸟照片)上绘制纹理。现在我想在fame缓冲区的中间绘制另一个纹理。新纹理的颜色为全黑,alpha从上到下从0变为255.

这是我的第二个纹理......

Checkboard indicates transparency

这就是我想要的......

What I want

这就是我得到的......

What I get

checkboard纹理是EAGLView后面的UIImageView中的UIImage。

我不想打扰帧缓冲区中的RGB值,我只想写入Alpha通道。我试过......

  1. glDisable(GL_BLEND)和glColorMask(0,0,0,1)
  2. glEnable(GL_BLEND)和glBlendFuncSeparate(GL_ZERO,GL_ONE,GL_ONE,GL_ZERO)
  3. 似乎没什么用。 RGB值始终被修改,像素变得更亮。

    如果源RGBA是(r2,g2,b2,a2)并且目的地是(r1,g1,b1,a1)。我希望最终值为(r1,g1,b1,a2)或最好是(r1,g1,b1,some_function_of(a1,a2))。我如何实现这一目标?

    这是我的代码......

    用于绘制第二个纹理的目标C代码...

    - (void) drawTexture {
        GLfloat textureCoordinates[] = {
            0.0f, 1.0f,
            1.0f, 1.0f,
            0.0f,  0.0f,
            1.0f,  0.0f,
        };
    
        static const GLfloat imageVertices[] = {
            -0.5f, -0.5f,
            0.5f, -0.5f,
            -0.5f,  0.5f,
            0.5f,  0.5f,
        };
    
        [EAGLContext setCurrentContext:context];
    
        glViewport(0, 0, backingWidth, backingHeight);
    
        glUseProgram(program);
    
        glActiveTexture(GL_TEXTURE2);
        glBindTexture(GL_TEXTURE_2D, texture);
    
        glEnable (GL_BLEND);
        glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_ONE, GL_ZERO);
    
        glUniform1i(inputImageTexture, 2);
    
        glVertexAttribPointer(position, 2, GL_FLOAT, 0, 0, imageVertices);
        glVertexAttribPointer(inputTextureCoordinate, 2, GL_FLOAT, 0, 0, textureCoordinates);
    
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    
        glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
        [context presentRenderbuffer:GL_RENDERBUFFER];
    }
    

    顶点着色器......

     attribute vec4 position;
     attribute vec4 inputTextureCoordinate;
    
     varying vec2 textureCoordinate;
    
     void main()
     {
        gl_Position = position;
        textureCoordinate = inputTextureCoordinate.xy;
     }
    

    片段着色器......

     varying highp vec2 textureCoordinate;
    
     uniform sampler2D inputImageTexture;
    
     void main()
     {
         gl_FragColor = texture2D(inputImageTexture, textureCoordinate);
     }
    

1 个答案:

答案 0 :(得分:0)

glColorMask必须有效 - 就我而言,它对我有用。另外,据我所知glBlendFuncSeparate在iOS上运行错误。

(理论上)你可以做到以下几点:

  • 将混合设置为乘法模式(GL_DST_COLOR,GL_ONE_MINUS_SRC_ALPHA)
  • 使纹理变白
  • glColorMask设置为已启用的所有频道

如果您拥有iOS 5+的iDevice,您可以在XCode的OpenGL Capture窗口中查看framebuffer alpha。

相关问题