0
votes

The opaque property is set to NO on my OpenGL view's CAEAGLLayer. There is a UIImageView behind the OpenGL view (the checkboard). A texture is drawn initially on the OpenGL view (the Emu bird photo). Now I want to draw another texture in the middle of the fame buffer. The new texture is completely black in color and the alpha changes from 0 to 255, from top to bottom.

This is my 2nd texture...

Checkboard indicates transparency

This is what I want...

What I want

This is what I get...

What I get

The checkboard texture is an UIImage in a UIImageView behind the EAGLView.

I do not want to disturb the RGB values in the frame buffer, I only want to write into the Alpha channel. I tried...

  1. glDisable(GL_BLEND) and glColorMask(0, 0, 0, 1)
  2. glEnable(GL_BLEND) and glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_ONE, GL_ZERO)

Nothing seems to work. The RGB values are always modified, the pixels become brighter.

If source RGBA is (r2, g2, b2, a2) and destination is (r1, g1, b1, a1). I want the final value to be (r1, g1, b1, a2) or preferably (r1, g1, b1, some_function_of(a1, a2)). How do I achieve this?

Here is my code...

Objective C code for drawing the second texture...

- (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];
}

Vertex shader...

 attribute vec4 position;
 attribute vec4 inputTextureCoordinate;

 varying vec2 textureCoordinate;

 void main()
 {
    gl_Position = position;
    textureCoordinate = inputTextureCoordinate.xy;
 }

Fragment shader...

 varying highp vec2 textureCoordinate;

 uniform sampler2D inputImageTexture;

 void main()
 {
     gl_FragColor = texture2D(inputImageTexture, textureCoordinate);
 }
1
I think you have the right idea, but you should try opengl.org/sdk/docs/man/xhtml/glBlendFuncSeparate.xml using the other parameters GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA also for the rgb channels.Pochi

1 Answers

0
votes

glColorMask must work - it worked for me in my case. Also, as far as I know glBlendFuncSeparate works buggy on iOS.

(In theory) you can do the following:

  • set blending to multiply mode (GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA)
  • make your texture white
  • set glColorMask to all channels enabled

If you have iDevice with iOS 5+ you can look at framebuffer alpha in OpenGL Capture window in XCode.