我正在绘制一个带有纹理的多边形,作为我的 OpenGL 程序中 HUD 的一部分。
//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();
问题是我无法让图像周围的“框”与背景融为一体。我在 Photoshop 中打开了图像 (.bmp) 并删除了我想要显示的图像周围的像素,但它仍然绘制了整个矩形图像。它用我在 glColor3f() 中使用的最后一种颜色为我删除的像素着色,我可以让整个图像变得透明,但我只希望我在 Photoshop 中删除的像素是透明的。有什么建议么?
我用于纹理的属性:
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());
这是我的程序的图像。我试图让白框消失,但是当我使用 glColor4f() 减小 alpha 时,整个图像会消失,而不仅仅是白框。
img607.imageshack.us/img607/51/ogly.png
从文件加载纹理的代码:
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;
}
}