Archived

This topic is now archived and is closed to further replies.

DJohnson

Textures and Transparancy

Recommended Posts

Hey, I was hoping someone could help me out with a bit of a problem. I''m trying to make part of a texture transparent. This function: glTexImage2D(GL_TEXTURE_2D, 0, 4, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, my_texels); seems to get the texture properly. And I''m setting the alpha channel to 255 where I want it transparent, 0 where I want it opaque. I wonder if I''m using the glBlendFunc properly? glBlendFunc(GL_SRC_ALPHA, GL_ZERO); I''ve tried all sorts of combinations with the paramenters, but nothing the transparancy I get seems to be either the entire image or none of it. -Doug

Share this post


Link to post
Share on other sites
GL_SRC_ALPHA,GL_ONE_MINUS_SOURCEALPHA


btw i have a BLACK AND WHITE font TGA and i wonder how can i blend it so that white is obaque and black is transparent?

creating an alpha channel would be a pain in the ass

Share this post


Link to post
Share on other sites
Basiror: You could convert the loaded TGA manually (eg set a while pixel to (0, 0, 0, 1) and a black pixel to (0, 0, 0, 0) before you send the data off to the texture generator.

Or pick up a package such as Paint Shop Pro. Export your TGA as a 2 bit RAW and load that RAW into the alpha channel (you might need to invert the colors). I''m not sure if the latter method will work but the first should be easy.

Sander Maréchal
[Lone Wolves Game Development][RoboBlast][Articles][GD Emporium][Webdesign][E-mail]

Share this post


Link to post
Share on other sites
well i need to try it
i could add a alpha channel and set the alpha values when i load the TGA
i hoped there would be a little blending trick

thx

Share this post


Link to post
Share on other sites
If you want to, you can add a mask in the alpha channel, but if you want it to create an alpha channel from the image at runtime, there's a pretty simple way.

You can put this with your loading code, or in a separate funtion, as long as it has access to the texture data. Do this BEFORE you create the texture with OpenGL.

unsigned char *data;
// Texture data

Load data....
data should now be allocated and contain pixel data, three bytes (rgb) per pixel

unsigned char *newData = new unsigned char[width*height*4];
// Create a new set of data the size of the image times 4 (rgba)

for (int i=0; i < width*height; i++)
{
newData[4*i] = data[3*i];
newData[4*i+1] = data[3*i+1];
newData[4*i+2] = data[3*i+2];
if (data[3*i] == 0 && data[3*i+1] == 0 && data[3*i+2] == 0)
newData[4*i+3] = 0; // minimum alpha value (transparent)
else
newData[4*i+3] = 0xFF // Maximum alpha value (opaque)
}

now use newData to generate your texture. Be sure to pass glTexImage GL_RGBA.

EDIT: Almost forgot. When you use the texture (font, sprite, whatever it's for), you can enable GL_ALPHA_TEST and call glAlphaFunc(GL_GREATER, 0), which tells OpenGL only to draw pixels with an alpha value greater than zero, thus masking out all of the black.

[edited by - pi_equals_3 on May 8, 2003 8:59:31 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by pi_equals_3
If you want to, you can add a mask in the alpha channel, but if you want it to create an alpha channel from the image at runtime, there''s a pretty simple way.

You can put this with your loading code, or in a separate funtion, as long as it has access to the texture data. Do this BEFORE you create the texture with OpenGL.

unsigned char *data;
// Texture data

Load data....
data should now be allocated and contain pixel data, three bytes (rgb) per pixel

unsigned char *newData = new unsigned char[width*height*4];
// Create a new set of data the size of the image times 4 (rgba)

for (int i=0; i < width*height; i++)
{
newData[4*i] = data[3*i];
newData[4*i+1] = data[3*i+1];
newData[4*i+2] = data[3*i+2];
if (data[3*i] == 0 && data[3*i+1] == 0 && data[3*i+2] == 0)
newData[4*i+3] = 0; // minimum alpha value (transparent)
else
newData[4*i+3] = 0xFF // Maximum alpha value (opaque)
}

now use newData to generate your texture. Be sure to pass glTexImage GL_RGBA.

EDIT: Almost forgot. When you use the texture (font, sprite, whatever it''s for), you can enable GL_ALPHA_TEST and call glAlphaFunc(GL_GREATER, 0), which tells OpenGL only to draw pixels with an alpha value greater than zero, thus masking out all of the black.

[edited by - pi_equals_3 on May 8, 2003 8:59:31 AM]


this certainly works but i rather prefer to write a little convertor so i don t have to mess around with it everytime i use the textures for another project

Share this post


Link to post
Share on other sites