1
votes

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();

The problem is that I can't get the "box" around the image to blend with the background. I opened the image (.bmp) in Photoshop and deleted the pixels around the image that I want displayed, but it still draws the whole rectangular image. It colors the pixels that I deleted with the last color that I used with glColor3f(), and I can get the whole image to become transparent, but I only want the pixels that I deleted in Photoshop to be transparent. Any suggestions?

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());

Here's an image of my program. I'm trying to get the white box to disappear, but as I decrease the alpha with glColor4f(), the whole image fades instead of just the white box.
img607.imageshack.us/img607/51/ogly.png

Code that loads a texture from file:

texture::texture(string filename)
{
// Routine to read a bitmap file.
// Works only for uncompressed bmp files of 24-bit color.
// Both width and height must be powers of 2.
unsigned int size, offset, headerSize;

// Read input file name.
ifstream infile(filename.c_str(), ios::binary);

// Get the starting point of the image data.
infile.seekg(10);
infile.read((char *) &offset, 4);

// Get the header size of the bitmap.
infile.read((char *) &headerSize,4);

// Get width and height values in the bitmap header.
infile.seekg(18);
infile.read( (char *) &sizeX, 4);
infile.read( (char *) &sizeY, 4);

// Allocate buffer for the image.
size = sizeX * sizeY * 24;
data = new unsigned char[size];

// Read bitmap data.
infile.seekg(offset);
infile.read((char *) data , size);

// Reverse color from bgr to rgb.
int temp;
for (unsigned int i = 0; i < size; i += 3)
{ 
temp = data[i];
data[i] = data[i+2];
data[i+2] = temp;
}
}
3
What do you have GL_TEXTURE_WRAP_S and GL_TEXTURE_WRAP_T set to? What about GL_TEXTURE_ENV_MODE?genpfault
I added the properties that I am using for the textures.Drake

3 Answers

2
votes

I don't see a glEnable(GL_BLEND) or a glBlendFunc in your code. Are you doing this?

Also, do you have an alpha channel in your image?

EDIT: You're loading the texture with the format GL_RGB, you're telling OpenGL there is no alpha on this texture.

  1. You need to make an image with alpha transparency. Look here for a GIMP tutorial or here for a Photoshop tutorial.
  2. Now load the texture indicating the correct image formats. There are MANY tutorials for "opengl alpha blending" on the internet - here is a C++ and SDL video tutorial.

Hope this helps!

1
votes

If I understand correctly, you want transparency to work with your textures, yes? If so, change

gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, TextureList[i]->getSizeX(), TextureList[i]->getSizeY(), GL_RGB, GL_UNSIGNED_BYTE, TextureList[i]->getData());

to

gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, TextureList[i]->getSizeX(), TextureList[i]->getSizeY(), GL_RGBA, GL_UNSIGNED_BYTE, TextureList[i]->getData());

To allow for an alpha channel, and turn on blending with:

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

UPDATE
As far as I know, BMPs don't support transparency (they can, ananthonline corrected me in the comments, but your application must support this) you should try one of the following formats if your imag editor does not support BMPs with alpha:

  • PNG
  • TIFF (recent variations)
  • TARGA
0
votes

To use the transparency channel (alpha channel) you need both to generate the BMP file with this channel (Photoshop does this if you ask him to), and specify the correct format when generating the mipmaps and sending the image to the video card.

This way, the image will have the needed transparency info and the OpenGl driver will know that the image has this info.