Questions about textures

I read this tutorial on masking at NeHe. At the bottom there was a note from someone else about how you can set pixel’s alpha valus to 0 to make them transparent. That seems like a better way to do it to me. So say I have a bitmap loaded into an array of unsigned characters. How would I specify alpha values for each pixel? I tried making a test array with only 1 pixel who I gave the values {255, 0, 0, 0} and used glTexImage2D and told it the format was GL_RGBA, but it’s not transparent… Any help would be appreciated. Thanks in advance.

Did you enable blending and set appropriate blending modes, for example:


Before drawing your texture on the screen?

Do you have alpha planes?

You can also achieve alpha-masking using the Alpha Test:

glEnable (GL_ALPHA_TEST);
glAlphaFunc (GL_GREATER, 0.1f);

This means, that only pixels get rendered, that have an alpha-value greater than 0.1.

With the Alpha Test you get hard edges, as you have certainly seen in many games. With blending you can get soft, blended edges, which looks much nicer, but has a big impact on performance (compared to the Alpha Test).


As you have said, bitmap textures don’t support the alpha channel. So you should write some extra codes to add an Alpha channel to your picture.
Before you write your code, you need a bitmap texture with a BLACK background. Make sure that the color of your picture is not black. So only the background color of your picture is black. Now you can write a code and say that the alpha value of the black points is equal to 0–So the alpha value of the background is 0.
Then you can call the function glAlphaFunc():

glEnable( GL_ALPHA_TEST );
glAlphaFunc( GL_GREATER, 0 );

Also you can add the blending to improve the appearence of your textures.
For more information about this topic, i suggest you to download the source code of chapter 15 of the book OpenGL Game Programming. Take a look at the function LoadBitmapWithAlpha:


Hrm. I added this to my code:

glAlphaFunc(GL_GREATER, 0);

And I used this method:

void AddAlpha()
		unsigned char* temp = new unsigned char[(Width * Height * 3)];
		for(int i=0;i < (Width * Height * 3);i++)
			temp[i] = Image[i];
		delete[] Image;
		Image = new unsigned char[(Width * Height * 4)];
		int n=0;
		for(i=0;i < (Width*Height*4);i+=4)
			Image[i] = temp[n];
			Image[i+1] = temp[n+1];
			Image[i+2] = temp[n+2];
			if(Image[i] == 254 && Image[i+1] == 254 && Image[i+2] == 254)
				Image[i+3] = 0;
				Image[i+3] = 255;

			n += 3;
		delete[] temp;

To loop through my array that held the bitmap data and add a fourth element for each pixel. Also, the parts of my bitmap I wanted to be transparent, I colored (254, 254, 254). But it still wouldn’t work. What am I doing wrong?

Also I tried it with a black background and changed the AddAlpha method to set alpha to 0 if the background is black, but it still doesn’t work.

If you’re going to use an alpha channel for a texture, then I see no reason why you should
choose alpha-testing over alpha blending. You’re just complicating things. Really, it is
as easy as:


Forget about it.

Did you download that chapter?

When you upload your texture to OpenGL, you need to store your texture in RGBA format (internal format), so that your alpha channel doesn’t get lost.
Use GL_RGBA8 (32 Bit) for the internal format parameter.

However, i wouldn’t use a color key of 254. Use simply black. Because then both, alpha-masking and alpha-blending work as expected and you can “on the fly” decide, which you want to use without having to recode a lot of stuff.