Search code examples
opengl-esfadealphavboblend

Fade Out OpenGL VBO with Colors but without Texture


I would like to fade out a VBO object in OpenGL ES. The VBO is drawn using RGBA format GL_UNSIGNED_BYTE like this:

glEnableClientState(GL_COLOR_ARRAY);
glColorPointer( 4, GL_UNSIGNED_BYTE, ....

I am fairly certain I should set up blending like this:

glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glColor4f( 1, 1, 1, alpha ); // I will be changing alpha from 1 to 0

But it doesn't fade, which makes sense, since I think the src alpha is coming from my VBO which is fixed.

So I thought maybe I should just pass 3 bytes to the driver keeping my VBO colors and then alpha will come from the glColor4f command.

 glColorPointer( 3, GL_UNSIGNED_BYTE, ....

But this crashes for unclear reasons (iPad development with Xcode) which I am still trying to decipher. I would think all of my glColorPointer offsets would still be fine - I still have all 4 bytes (RGBA) in MyVertexObject so I don't think it's a padding issue - I do not change any offset values in the glColorPointer command, just changed the 4 to a 3.

If I disable GL_COLOR_ARRAY the fade works perfectly but now I've lost the colors and it's using the white color I set above.

So I'm stuck as it seems I can't control the alpha channel separately from the RGB colors. Is there another way to fade a VBO with colors? Thanks.


Solution

  • You can specify blend factors independently of the colors in your VBOs by using GL_CONSTANT_ALPHA as the blend function:

    glBlendColor(0.0f, 0.0f, 0.0f, alpha);
    glBlendFunc(GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA);
    

    This will use the alpha component specified in glBlendColor() for the blend function.