I have a texture I want to draw onto two polygons that is partially transparent, which can be found here:
http://students.hightechhigh.org/~jjensen/Pictures/crosshair.jpg
The PSD can also be found here, along with the picture in TGA format:
http://students.hightechhigh.org/~jjensen/Pictures/crosshair.psd
http://students.hightechhigh.org/~jjensen/Pictures/crosshair.tga
The problem is that when I load the jpg, the background shows up as white where it should be transparent. When I load the tga the background is black where it should be transparent. And sadly, my texture class doesn't support psd, shucks.
The picture is a crosshair I'm trying to draw over a 3D scene. I go into gluOrtho2D mode and draw it first, then switch back into perspective and draw everything else. My texture loading code is based off of the FreeImage library. Below I will put my texturing code and my rendering code, just incase anybody cares to see.
Below is my crosshair rendering code snippet.
[SOURCE]
glEnable(GL_BLEND);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 1.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
g_cCross.BindTexture();
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2d(0.0, 0.0); glVertex2d(0.5-CROSS_SIZE, 0.5-CROSS_SIZE);
glTexCoord2d(1.0, 0.0); glVertex2d(0.5+CROSS_SIZE, 0.5-CROSS_SIZE);
glTexCoord2d(0.0, 1.0); glVertex2d(0.5-CROSS_SIZE, 0.5+CROSS_SIZE);
glTexCoord2d(1.0, 1.0); glVertex2d(0.5+CROSS_SIZE, 0.5+CROSS_SIZE);
glEnd();
[/SOURCE]
And here is my texture loading code snippet.
[SOURCE]
/* Loads a 2D texture into an OpenGL texture. */
bool CTexture::LoadTexture(const char *szFilename, int iFormat) {
FIBITMAP *pImage;
FIBITMAP *p32Bit;
if(iFormat==TGA) {
pImage = FreeImage_Load(FIF_TARGA, szFilename, 0);
}
else if(iFormat==JPG) {
pImage = FreeImage_Load(FIF_JPEG, szFilename, JPEG_DEFAULT);
}
else if(iFormat==JPG_QUALITY) {
pImage = FreeImage_Load(FIF_JPEG, szFilename, JPEG_ACCURATE);
}
else if(iFormat==PNG) {
pImage = FreeImage_Load(FIF_PNG, szFilename, 0);
}
else if(iFormat==BMP) {
pImage = FreeImage_Load(FIF_BMP, szFilename, 0);
}
if(pImage==NULL) {
return false;
}
unsigned int uiWidth = FreeImage_GetWidth(pImage);
unsigned int uiHeight = FreeImage_GetHeight(pImage);
if((int)sqrt((double)uiWidth) % 2!=0 || (int)sqrt((double)uiHeight) % 2!=0) {
FreeImage_Unload(pImage);
return false;
}
p32Bit = FreeImage_ConvertTo32Bits(pImage);
FreeImage_Unload(pImage);
if(p32Bit==NULL) {
return false;
}
unsigned char *pData = (unsigned char *)malloc(uiWidth*uiHeight*4);
for(unsigned int i=0; i<uiHeight; i++) {
for(unsigned int j=0; j<uiWidth; j++) {
RGBQUAD rgb;
FreeImage_GetPixelColor(p32Bit, j, i, &rgb);
pData[(i*uiWidth+j)*4+0] = rgb.rgbRed;
pData[(i*uiWidth+j)*4+1] = rgb.rgbGreen;
pData[(i*uiWidth+j)*4+2] = rgb.rgbBlue;
pData[(i*uiWidth+j)*4+3] = rgb.rgbReserved;
}
}
glGenTextures(1, &m_uiTexture);
glBindTexture(GL_TEXTURE_2D, m_uiTexture);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexImage2D(GL_TEXTURE_2D,0,4,uiWidth,uiHeight,0,GL_RGBA,GL_UNSIGNED_BYTE,pData);
free(pData);
FreeImage_Unload(p32Bit);
return true;
}
[/SOURCE]
This is really frustrating, I hate messing with transparency. Thanks for your help,
OpenGL Revolutions http://students.hightechhigh.org/~jjensen/