PDA

View Full Version : Texture Transparency .BMP



Darius
12-10-2012, 08:22 PM
I am drawing a polygon with texture on it as part of the HUD in my OpenGL program:



//Change the projection so that it is suitable for drawing HUD
glMatrixMode(GL_PROJECTION); //Change the projection
glLoadIdentity();
glOrtho(0, 800, 800, 0, -1, 1); //2D Mode
glMatrixMode(GL_MODELVIEW); //Back to modeling
glLoadIdentity();

//Draw the polygon with the texture on it
glBegin(GL_POLYGON);
glTexCoord2f(0.0, 1.0); glVertex3f(250.0, 680, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(570.0, 680, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(570.0, 800, 0.0);
glTexCoord2f(0.0, 0.0); glVertex3f(250.0, 800, 0.0);
glEnd();

//Change the projection back to how it was before
glMatrixMode(GL_PROJECTION); //Change the projection
glLoadIdentity();
gluPerspective(45.0, ((GLfloat)800) / GLfloat(800), 1.0, 200.0); //3D Mode
glMatrixMode(GL_MODELVIEW); //Back to modeling
glLoadIdentity();


Properties that I am using for the textures:



glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, TextureList[i]->getSizeX(), TextureList[i]->getSizeY(), GL_RGB, GL_UNSIGNED_BYTE, TextureList[i]->getData());


The problem is that I can't get the white "box" around the gun to blend with the tiles in the background. Here is a photo:

img607.imageshack.us/img607/51/ogly.png

I opened the image (.bmp) in Photoshop and deleted the pixels around the gun, but it still draws the whole rectangular image. It colors the pixels that I deleted with the last color that I used in glColor3f(), and I can get the whole image to become transparent, but I only want the pixels that I deleted in Photoshop to be fully transparent. I'm using glBlend too. From what I researched, it has to do with alpha channeling with the image, but I have no idea how. Can anyone help?

tonyo_au
12-11-2012, 01:03 AM
Your image doesn't have an alpha component so it will default to 1 (opaque). You will need to use something like a .png which has an alpha component or write a shader to drop pixels with a particular colour or have a black and white mask image that
you draw to the stencil buffer and then render the texture with stenciling enabled.

thokra
12-11-2012, 02:29 AM
Or you can use shaders and assign the alpha yourself. It's almost 2013. Time to use GLSL...

Darius
12-11-2012, 03:56 AM
It's for a school project so GLSL will be on the list of things to learn right after this semester!
My OpenGL program can only read .BMP images, so I think the most practical solution at the moment is to do as tonyo_au said and have a black and white mask image that I draw to the stencil buffer and then render the texture with stenciling enabled.
Except I don't know how to do that...I imagine I would make another image of my texture with the gun being all black, and the background white, and send that to the stencil buffer before rendering the original image. Sounds like just a few lines of code...do you know of any tutorial for this by any chance?

tonyo_au
12-11-2012, 10:48 PM
Always remember google is your friend
http://en.wikibooks.org/wiki/OpenGL_Programming/Stencil_buffer

this is worth looking at as well

http://ogldev.atspace.co.uk/index.html

Darius
12-12-2012, 07:18 PM
Thank you!