Skip to main content
GameDev.net gamedev.net
🔒 Locked

Transparency

Started by zelda6486 Aug 2, 2002 at 2:00 PM 3 replies 750+ views
Original Post
zelda6486
zelda6486
I am using sprites for my game and, since I''m mapping them on quads, I need the outer area to be transparent. Just one color, doesn''t matter which, needs to be transparent, and the rest opaque. I''m using bitmaps for the textures. I can''t use masking, so I decided I''d manually add an alpha channel to the 24 bit bitmap and make it 32. I set all white pixels to 0, all nonwhite to 1. Now I''m wondering what I need to do for OpenGL to take into account the new alpha channel. I''ve heard all kinds of stuff on the boards-that I should use the alpha test, blending, or both. I need to know what I need to do to get white pixels, or any color for that matter, tranparent, using bitmaps. I''ll post any code needed.
Xero-X2
Xero-X2
this is what I use for alpha channel images to create textures.

gluBuild2DMipmaps(GL_TEXTURE_2D, 4, pBitmap->sizeX, pBitmap->sizeY, GL_RGBA, GL_UNSIGNED_BYTE, data);

and now how to draw them..

glDepthMask(GL_FALSE); // turns off depth buffer writes
glDisable(GL_LIGHTING); // disables lighting so you can use color with out materials
glEnable(GL_BLEND); // enables blending
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // use alpha channel on image

//draw quad with texture

glDepthMask(GL_TRUE);
glEnable(GL_LIGHTING);with out materials
glDisable(GL_BLEND); // enables blending

good luck
"I seek knowledge and to help those who also seek it"
Bong
Bong
If you want your sprites to be transparent and use color keying, use normal blending like the above post. If you don''t need transparency on your sprites, you can use the alpha test. It''s simpler and doesn''t require your sprites to be drawn in order.
Bong, James Bong
Maximus
Maximus
Using blending also eats up around twice the fillrate, so its considerably slower than doing a simple alpha test.

-----------------------
"When I have a problem on an Nvidia, I assume that it is my fault. With anyone else''s drivers, I assume it is their fault" - John Carmack
-----------------------"When I have a problem on an Nvidia, I assume that it is my fault. With anyone else's drivers, I assume it is their fault" - John Carmack
moore52
moore52
I suppose you should use

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

the best way is to load 24bit_bmp_RGB and make 32bit_bmp_RGBA
I don''t have source in hand but you sholud create new buffer and rewrite loaded data of bmp adding alpha channel:


char *data=bmp->data;
char *new_buff=new char[4*bmp->width*bmp->height];

for (int i=0;iwidth*bmp->height;i++)
{

red=*data++;
green=*data++;
blue=*data++;
*new_buff++=red;
*new_buff++=green;
*new_buff++=blue;

if ((red==0)&&(blue==0)&&(green==0))
{
alpha=0;
}
else
{
alpha=255; //this is char format
}

*new_buff++=alpha;

}

P.S.
I write this at hand, so better don''t copy this.

P.S.2
Better don''t use mipmaps, it scales image, so some alpha data can be lost!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.