Messed up Textures :p

Started by
3 comments, last by jm_hewitt 19 years, 9 months ago
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/
Advertisement
Quote:Original post by 31337
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.


This is your problem. You need to render the transparent stuff *after* everything else.

Why does the texture have a different background color depending on the format? I assume its my loading code, or an issue with FreeImage.
OpenGL Revolutions http://students.hightechhigh.org/~jjensen/
Your jpg doesn't have an alpha channel (I've never heard of a jpg that supported alpha channels anyway) so the white background you're getting with the jpg is just part of the image.

The tga does have an alpha channel, and your alpha blending code is probably working fine, but because you're rendering it first, it's getting blended with a clear buffer, and nothing draws over the top of it thanks to the z buffer.

Change your clear colour and the background of the crosshair will change to match. Render the scene before the crosshair, and your crosshair will get blended over the scene as it's supposed to, rather than over the empty buffer.
I go with PNG files for transparency. In Photoshop, you "Save as web" and can save an alpha channel. DevIL image library can load it up and return you a GLuint if desired with the texture loaded.

This topic is closed to new replies.

Advertisement