PDA

View Full Version : Questions about textures



K A Z E
12-20-2005, 03:11 PM
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.

Omaha
12-20-2005, 07:10 PM
Did you enable blending and set appropriate blending modes, for example:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Before drawing your texture on the screen?

Do you have alpha planes?

Jan
12-21-2005, 01:26 AM
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).

Jan.

Ehsan Kamrani
12-21-2005, 02:09 AM
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*:
http://glbook.gamedev.net/source.asp

Regards
-Ehsan-

K A Z E
12-21-2005, 11:51 AM
Hrm. I added this to my code:

glEnable(GL_ALPHA_TEST);
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 &amp;&amp; Image[i+1] == 254 &amp;&amp; Image[i+2] == 254)
Image[i+3] = 0;
else
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?

K A Z E
12-21-2005, 11:55 AM
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.

12-21-2005, 01:00 PM
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:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Forget about it.

Ehsan Kamrani
12-22-2005, 12:27 AM
Did you download that chapter?

Jan
12-22-2005, 12:28 AM
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.

Jan.