I'm using the STB library to load a PNG image that looks like this (yes it's small). It's a sprite sheet that I made in 5 minutes with transparency behind the character.
Eggy Sheet:
I made a class that parses the sprite sheet and feeds the appropriate Texture Coordinates to the rendering loop. And it works flawlessly, but I there's no more transparency, just black. I checked using the debugger, and image.components() == 4
, so there should be an alpha channel if I'm not misunderstanding.
Eggy Error:
I'm fairly certain that it has something to do with the way I'm parsing the image with STB, but I'm not sure. there could also be something else I'm missing. Here's the code:
public void renderBatch(){
DrawableAsset image;
Coordinates coord;
while(!images.isEmpty()){
image = images.dequeue();
coord = coords.dequeue();
int texID = glGenTextures();
glBindTexture(GL_TEXTURE_2D, texID);
if (image.components() == 3) {
if ((image.absWidth() & 3) != 0) {
glPixelStorei(GL_UNPACK_ALIGNMENT, 2 - (image.absWidth() & 1));
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.absWidth(), image.absHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, image.imageData());
} else {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.absWidth(), image.absHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image.imageData());
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glEnable(GL_TEXTURE_2D);
glPushMatrix();
glTranslatef(coord.x() + image.halfwidth(), coord.y() + image.halfHeight(), 0);
glRotatef(image.getAngle(), 0, 0, 1);
glScalef(image.getScale(), image.getScale(), 1f);
glTranslatef(-coord.x() - image.halfwidth(), -coord.y() - image.halfHeight(), 0);
renderImage(image, coord);
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glDeleteTextures(texID);
}
Some notes:
image.absWidth()
and image.absHeight()
return the dimensions of the actual texture sheet, while image.width()
and image.height()
return the dimensions of the cell.
renderImage()
is just the lines of code where the textured quad are rendered, and should be unrelated, but here it is just in case:
private void renderImage(DrawableAsset image, Coordinates coord){
float[] texCoords = image.regionCoordinates();
glBegin(GL_QUADS);
{
glTexCoord2f(texCoords[0], texCoords[1]);
glVertex2f(coord.x(), coord.y());
glTexCoord2f(texCoords[2], texCoords[3]);
glVertex2f(coord.x() + image.width(), coord.y());
glTexCoord2f(texCoords[4], texCoords[5]);
glVertex2f(coord.x() + image.width(), coord.y() + image.height());
glTexCoord2f(texCoords[6], texCoords[7]);
glVertex2f(coord.x(), coord.y() + image.height());
}
glEnd();
}
P.S. Also while I'm here, I've been trying to figure out how to eliminate the blending when I scale up images. I tried glDisable(GL_BLEND)
, but that doesn't do it. Any help and background info there?
The issue was caused because I had not set up glBlendFunc
correctly. Entering this line of code prior to any drawing did the trick!
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
More on the topic: Blending