Color keying in OpenGL

Started by
5 comments, last by kburkhart84 18 years, 5 months ago
What would be the simplest way to color key in OpenGL? I want it so all pixels in the texture that are of color R 00, G FF, B FF to be invisible.

Learn to make games with my SDL 2 Tutorials

Advertisement
Do it where you load the texture. Check what pixels are your "color key" and make the alpha 0 and the rest one. Then when you draw use the alpha test to make the texture have transparent the parts you checked for when you loaded your texture.


Quote:Original post by kburkhart84
Do it where you load the texture. Check what pixels are your "color key" and make the alpha 0 and the rest one. Then when you draw use the alpha test to make the texture have transparent the parts you checked for when you loaded your texture.


Yup. You don't color key in the same since as it is traditionaly done. You use an alpha channel saved with the image file. That also gives you 256 levels of transpancy VS 2.

I gather one could write something that loads a 24-bit RGB image and creates an alpha channel on the fly from a specific color. To save a wee bit of disk space. I doubt it would be worth it.
------------------------------------------------------------- neglected projects Lore and The KeepersRandom artwork
I just thought I would make additional commenents if you use gluBuildMipmaps2d (if not this will not likly apply) while useing a color key, it is likly that during its resizeing of your texture the color 'under' your invisible sections will bleed slightly into the surrounding visible sections, if you ever come accross this problem

1. generate mipmaps yourself =D takeing glubuildmipmaps2d out of the equation
2. find a way to make the color colors fit the texture.

may or may not apply but I thought I'd mention it, as it can sometimes cause a problem.
"I seek knowledge and to help those who also seek it"
Lazy Foo,

If you have your own bitmap (or other image file format) loading routine, it should be pretty easy to use the method kburkhart84 suggested. But in case you don't and maybe use an SDL function to load images, it's not going to be that easy.

The simplest thing to do then would be to add an alpha channel to the image parts which you want to colorkey, save as tga, and use SDL_Image to load it all (that's how I did it [smile]).

edit: but don't forget to set the glAlphaFunc to GL_GREATER and enable GL_ALPHA_TEST.
Quote:Original post by Xero-X2
I just thought I would make additional commenents if you use gluBuildMipmaps2d (if not this will not likly apply) while useing a color key, it is likly that during its resizeing of your texture the color 'under' your invisible sections will bleed slightly into the surrounding visible sections, if you ever come accross this problem

1. generate mipmaps yourself =D takeing glubuildmipmaps2d out of the equation
2. find a way to make the color colors fit the texture.

may or may not apply but I thought I'd mention it, as it can sometimes cause a problem.
This is also a problem caused by just using linear filtering on the texture. On the transparent/opaque edge it will interpolate between the transparent color and the opaque color to become a semi-transparent mix of the opaque and transparent colors. The best way to avoid this is to loop through your image and on the transparent/opaque borders set the transparent color to that of the opaque color (or a mix of the opaque colors that border the transparent texel). It may also be a good idea to do this "color extrusion" a few pixels into the transparent part of the image instead of just one.
For images like this, I just use GL_NEAREST for filtering so I avoid that problem altogether. The easiest way probably is to use the TGA format, as stated above.


This topic is closed to new replies.

Advertisement