How to render Sprites in OpenGl (transparency question)

Started by
5 comments, last by wizardpc 11 years, 4 months ago
So my question is more about rendering a certain color transparent in an image texture. The images are RGB, and I wish to render everything except pink (255,0,255).

Is it easy to do something like this with OpenGL?
Advertisement
This form of transparency is known as colour key. You could find some answers perhaps by googling "opengl colour key".

As an approach, it's very much out of favour and is a bit awkward on modern graphics pipelines. Basically, your options are to hack something into your pixel shader to detect and reject pixels with the magenta colour, or to preprocess your texture (either offline, or as you load it) to add an alpha channel to your texture (an alpha channel is a fourth colour channel which is often used for transparency). I'd recommend adding the alpha channel.
I agree with C0lumbo, except that I would generate the alpha-channel when loading the texture image.. Load the image so you have raw RGB values from it, then set up to create a 4-channel RGBA texture, and then loop through all the pixels in the source image RGB, checking for RGB 255,0,255.. If you encounter pink, set the RGBA to 0,0,0,0, black with zero opacity, this will produce black edges instead of pink ones where your texture should be transparent (if you're using any filtering other than GL_NEAREST when rendering your texture).. Then just make sure you enable GL_BLEND, and your glBlendFunc is set to GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA.
Thanks for the replies, So I guess there is no way when rendering it with an OpenGL call. I will do the work when I load the texture. I'm using GLFW's Texture loading, so I'm guessing this will be a bit of work.
Thanks for the correct term to google "color key". In my research I found an easy way to do it with a shader. Is it better for me to manipulate the tex data, or is it better to just send the texture to the shader and let it handle the transparency. This would only be for my 2D sprites. And I'm using shaders for all my other rendering in my game.
Use a shader if you want to keep things simple.

Use a shader if you want to keep things simple.


I did, and with your advice of using GL_NEAREST, it works perfectly and was very simple like you said. +1

This topic is closed to new replies.

Advertisement