I'm trying to load an RGBA image into OpenGL using the stb image library. The bottom half of the image's transparent section is showing up with partial transparency, and not complete transparency. I think this may have something to do with the image format, but I'm not sure.
Here is the source image taken from Gimp:
Here is the output: (Note that I am currently having the shader output the alpha channel into the red channel, so red means it's opaque and black is transparent)
Here is the code that loads the image data and creates the texture:
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
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_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
int width, height, numChannels;
char *filename = "path/to/image.png";
unsigned char *data = stbi_load(filename, &width, &height, &numChannels, 0);
if (data)
{
GLint format = 0;
if (numChannels == 3) {
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
format = GL_RGB;
}
else if (numChannels == 4) {
format = GL_RGBA;
}
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
I am currently exporting the image as RGBA with 8 bits per color. I have tried different formats, like 16 bits per color, floating point colors instead of integer colors, but those made it worse, so I think that RGBA with 8bpc is correct.
This code also works with other images, as seen below, so I'm not exactly sure why this isn't working.