Transparency in Polygons
Here''s a question that I thought would be easy, but has turned out to be much more difficult than I thought it would be.
I have 3 small icons of a plane, and 3 images of a tank. (Currently I have them in *.jpg and *.tga formats, both of which I can read in.) The first plane and tank are red, the 2nd yellow, and the 3rd blue. Each has a white background.
I want to display these planes and tanks on a textured polygon with a map as it''s texture (which I already have loaded in). This is so that, over time, I can show where the plane is on the map from a top-down perspective to the viewer.
However, the problem I''m facing is that the white background is showing up along with the icons themselves, which looks rather tacky. I want to clear out just the white part of the image via an alpha colour, but leave the coloured vehicle alone.
Can I do this with TGA''s or JPEG''s, or will I have to use another picture format to do this? If I can use one of those (JPEG''s are preferable to TGA''s), how do I go about doing this?
Thanks in advance,
Dragonus
Hi,
On my site (Download section), I''ll have a little example who display a tree.
I use the alpha chanel and the black on the TGA is not displayed.
========================
Leyder Dylan
http://ibelgique.ifrance.com/Slug-Production/
On my site (Download section), I''ll have a little example who display a tree.
I use the alpha chanel and the black on the TGA is not displayed.
========================
Leyder Dylan
http://ibelgique.ifrance.com/Slug-Production/
I don''t think JPGs have an alpha, so you should use TGAs. You''ll need to make your texture of type RGBA not RGB to make sure it uses the alpha plane. Of course, you''ll need to have 4 bytes per pixel instead of 3, etc...
I would use black for your background color instead of white or you''ll get a white halo around your icons where there mixing takes place...
I would use black for your background color instead of white or you''ll get a white halo around your icons where there mixing takes place...
TGA''s it is then.
Leyder Dylan, I looked at your code, and then wrote what appeared to be the differences in code in my code. (I hope I get them all -- sadly, my knowledge of French is solely what the Babel Fish tells me.)
Namely, those differences were the glAlphaFunc(GL_GREATER, 0.3f) and the glEnable(GL_ALPHA_TEST) and glDisable(GL_ALPHA_TEST) pairing. However, what I expected to work... well... doesn''t. In fact, it''s weird. I know for a fact that all of the alpha values aren''t 0 (I stepped through the BGRA->RGBA loop to verify that), but it will only display when I had the alpha function to be <= 0.05f. Then I put my enable/disable commands in a different function, and now it treats the whole image as if every alpha value is >= 0.95f.
Did I miss something in there (cause I probably did)? Is there a reasonable explanation for why it''s doing that?
~ Dragonus
Leyder Dylan, I looked at your code, and then wrote what appeared to be the differences in code in my code. (I hope I get them all -- sadly, my knowledge of French is solely what the Babel Fish tells me.)
Namely, those differences were the glAlphaFunc(GL_GREATER, 0.3f) and the glEnable(GL_ALPHA_TEST) and glDisable(GL_ALPHA_TEST) pairing. However, what I expected to work... well... doesn''t. In fact, it''s weird. I know for a fact that all of the alpha values aren''t 0 (I stepped through the BGRA->RGBA loop to verify that), but it will only display when I had the alpha function to be <= 0.05f. Then I put my enable/disable commands in a different function, and now it treats the whole image as if every alpha value is >= 0.95f.
Did I miss something in there (cause I probably did)? Is there a reasonable explanation for why it''s doing that?
~ Dragonus
You can use jpg if you want, or even bmp for that matter. Just do the following. Either sacrafice all color for the easy way and do the following:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_AlPHA, GL_ONE_MINUS_SRC_COLOR);
then all BLACK is eliminated, and the brighter the color, the more solid it is. Or do it the right way and keep the color.
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0);
This also eliminates all black, but keeps color. Hope this helps.
Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_AlPHA, GL_ONE_MINUS_SRC_COLOR);
then all BLACK is eliminated, and the brighter the color, the more solid it is. Or do it the right way and keep the color.
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0);
This also eliminates all black, but keeps color. Hope this helps.
Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"
Hello there,
just a little tip.
you can get away with one texture and just change the
color of the textured primitive ie
glColor3f(1,0,0) for red and use GL_MODULATE for the
texture thingy (cant remeber call of the top of my
head).
also enable GL_ALPHA_TEST and
set glAlphaFunc(GL_GREATER,0) which is quicker.
You may not need to enable blending if you do this??
Cheers
Mark.
just a little tip.
you can get away with one texture and just change the
color of the textured primitive ie
glColor3f(1,0,0) for red and use GL_MODULATE for the
texture thingy (cant remeber call of the top of my
head).
also enable GL_ALPHA_TEST and
set glAlphaFunc(GL_GREATER,0) which is quicker.
You may not need to enable blending if you do this??
Cheers
Mark.
D''oh! I''m such a complete idiot for not realizing it sooner. :o
It always helps if you specify a number of colour components to match the format of the image, neh?
glTexImage2D(GL_TEXTURE_2D, 0, 4, textureImage->sizeX, textureImage->sizeY, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureImage->data);
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, textureImage->sizeX, textureImage->sizeY, GL_RGBA, GL_UNSIGNED_BYTE, textureImage->data);
~ Dragonus
It always helps if you specify a number of colour components to match the format of the image, neh?
glTexImage2D(GL_TEXTURE_2D, 0, 4, textureImage->sizeX, textureImage->sizeY, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureImage->data);
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, textureImage->sizeX, textureImage->sizeY, GL_RGBA, GL_UNSIGNED_BYTE, textureImage->data);
~ Dragonus
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement