I've been playing around with the png format with FreeImage as of late. I've managed to load in PNG images and get them onto a quad as a texture, however the alpha channel shows up as black. I've enabled GL_BLEND and looked around quite a bit on google, but I've turned up nothing to help me. Here's my code that I'm using..
void HVSTGFX::initGL()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
}
This is called on WM_CREATE. As you can see, I have GL_BLEND in there.
unsigned char * HVSTGFX::loadPNGFile(char *fileName, PNGFILE *pngfile)
{
FREE_IMAGE_FORMAT fif = FIF_PNG;
//pointer to the image, once loaded
FIBITMAP *dib(0);
//pointer to the image data
unsigned char* bits(0);
unsigned char tempRGB;
if(FreeImage_FIFSupportsReading(fif))
dib = FreeImage_Load(fif, fileName);
if(!dib)
return NULL;
bits = FreeImage_GetBits(dib);
pngfile->width = FreeImage_GetWidth(dib); pngfile->height = FreeImage_GetHeight(dib);
int size = pngfile->width*pngfile->height;//(FreeImage_GetWidth(dib) * FreeImage_GetHeight(dib));
for (int imageIDx = 0; imageIDx < size*(pngfile->width/16); imageIDx += 4)
{
tempRGB = bits[imageIDx];
bits[imageIDx] = bits[imageIDx + 2];
bits[imageIDx + 2] = tempRGB;
}
//FreeImage_Unload(dib);
return bits;
}
This is my PNG loader.
void HVSTGFX::createSpritePNG(float width, float height, float x, float y, CSprite sprite)
{
if (!sprite.loaded)
{
//glGenTextures(texCount++, &sprite.texture);
//glBindTexture(GL_TEXTURE_2D, sprite.texture);
sprite.loaded = true;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}
else
glColor4f(1.0f,1.0f,1.0f, .0f );
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width, y, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x + width, y + height, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y + height, 0.0f);
glEnd();
}
This actually draws the given image.
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pngTest.pngFile.width, pngTest.pngFile.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pngTest.sprite);
HVSTGFX::createSpritePNG(.4f, .54f, -.5f, -.5f, pngTest);
And this is in my main loop. As I said, the image gets textured and draws without a problem, but the alpha channel is just black. Anyone able to help?