I’m using textured quads as sprites in my OpenGL program. Everything works fine except transparency. I don’t know how to make certain parts of my sprite transperent. I know that this can done in DirectDraw very easily by telling it the keycolor which is in my example purple (RGB = (255, 0, 255).
However, there is no such command in OpenGL and therefore I somehow have to tell OpenGL, how to make certain parts look transparent. I’ve read that this works well with image files (I’m using bitmaps) which have an alpha value set… well, I don’t know how to actually set the alpha value of an image - at least I don’t have an app which can do that for me.
So how can I perform transperancy on my sprites? ( in a simple way )
P.S.
I don’t want to use sprite libraries, DirectDraw, or anything else… I want to do it on my own.
If your current bitmap is a 24 bit RGB i means bitmap-data i an array of unsigned char’s where every pixel is three bytes long. The first byte is the red value, the next is green and the last i blue. If you want to set the alpha-value you should convert the bitmap to an 32 bit RGBA bitmap in your code. The last byte in every pixel is the alpha value. Set to 0 for transparent and 255 for opaque.
Remember to enable blending:
glEnable (GL_BLEND);
And to set how OpenGL should blend:
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
It is relatively easy to hack up a function that converts a 24bit bitmap to a 32bit bitmap using say black as a transparent colour key. Just check whether the first 3 bytes are all 0, and then set the 4th byte to 0 for transparant, 1 if the first 3 bytes are anything else.