Color key in opengl

I’m using GLUT and glpng to put together a simple 2D graphics API.

I’m having trouble finding information on how to achieve the “colorkey” effect on a texture.

I want to make the color ‘black’ transparent, for sprites. I don’t want to use a mask that requires a separate black and white image file.

I’ve been playing with GL_BLEND and I can’t achieve this colorkey effect. The only time I do manage it, involves making the entire texture 50% blend transparent, which is cool, but not what I want.

How do I achieve the color key effect?



There is no “color key” effect in OpenGL. You can either do it manually by adding an alpha channel to the texture with alpha = 0 on every black pixel, or you can do it in a shader.

But why not simply use the alpha channel in the png image?

Yes glpng provides access to the alpha channel, I just don’t know how to use it and the documentation is cursory.

Try to google then. This is, unfortunately for you, not the place for that point. You can anyway use another format: RGB, TGA are well known and well documented and support the alpha channel. I guess there aren’t any other solution, but the point Overmind stippled: use shaders.

Thanks anyway.

You’ll find what you want :wink:

There’s not much to do to use the alpha channel. Just make a PNG with transparency, load the texture as GL_RGBA (I think glpng will do this for you automatically).

Then set your blend mode to GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA and enable blending :wink:

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.