View Full Version : color key in opengl

05-05-2006, 08:50 AM
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?



05-05-2006, 11:04 AM
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?

05-05-2006, 04:02 PM
Yes glpng provides access to the alpha channel, I just don't know how to use it and the documentation is cursory.

05-05-2006, 05:33 PM
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.

05-05-2006, 06:16 PM
Thanks anyway.

05-05-2006, 06:52 PM
You'll find what you want ;)

05-06-2006, 01:26 PM
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 ;)