Transparent Texture Map question

Hi, I’m desperately trying to get my texture maps to be transparent. How do I load in images to be transparent. I know I have to set the alpha value for the transparent color to 0, but I don’t know how to load in a picture to and set it.

Any help is greatly appreciated

OK so you’ve already got a picture that’s 24bpp RGB and 8bpp alpha = 32bpp total.

OpenGL doesn’t support any particular file format like TIF or TGA or whatever. Whatever file format you choose you’ll have to use your own code to load it in. To start with try using a simpler file format like RAW. After you’ve loaded the file keep the width and height info tucked away, discard the header info and keep the image bits.

Enable transparency testing with glEnable(GL_ALPHA_TEST) and setup the transparency test with glAlphaFunc(GL_GREATER,0). This test will draw any pixel that is not black. There are other tests too. Use glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, pImageBits ) to draw the image.

Remember to disable GL_ALPHA_TEST when done.
If your image shows up essentually correct but looking real ugly try replacing the 7th parameter from glTexImage2D with GL_BGRA_EXT.
If your picture looks like static you may need to change your row alignment with glPixelStorei(GL_UNPACK_ALIGNMENT,???).
After you get it going look into using glBindTexture with glTexImage2D.

Yes, I’ve got a 32bit tga file and a function that loads it in. Right now though, I have it displaying the picture with no transparency.

could you expand on glAlphaFunc. My picture is on a white background, so should I use glAlphaFunc(GL_GREATER, 1)??? WHat if I need to use a color other than black/white, like green.


The color (RGB) of the transparent areas can be any color. glAlphaFunc only tests the alpha values of your texture not the RGBs.

So before drawing enable alpha testing with glEnable(GL_ALPHA_TEST) and use glAlphaFunc(GL_GREATER,0.5). This test evaluates the alphas of your incoming texture. Pixels with alphas greater than 0.5 (or 129-255) will get drawn. Pixels with alphas less than or equal to 0.5 (or 0-128) are discarded. This is assuming your alpha layer is 8bpp (or 256 possible values) essentially a greyscale image by itself.

Note that the second parameter of glAlphaFunc is clamped from 0.0 to 1.0
Using glAlphaFunc(GL_GREATER,1) would produce nothing because there is no possible alpha value that would make this expression true
(alpha)1 > 1(refValue)