Jump to content
  • Advertisement
Sign in to follow this  
Lazy Foo

OpenGL Color keying in OpenGL

This topic is 4625 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!