Loading PNG with stb_image for OpenGL texture gives wrong colors
Asked Answered
T

2

30

I am using stb_image to load a 32-bit PNG file (RGBA) and I am creating an OpenGL texture with it.

It works fine for 24-bit PNG files (with no alpha channel), but when I use a 32-bit PNG file, something goes wrong.

This is what the texture should look like:

Texture

And this is what it looks like when rendered with OpenGL (the black parts are meant to be transparent, and are when I enable blending):

Wrong Texture

This is how I load the texture:

int w;
int h;
int comp;
unsigned char* image = stbi_load(filename.c_str(), &w, &h, &comp, STBI_rgb);

if(image == nullptr)
    throw(std::string("Failed to load texture"));

glGenTextures(1, &m_texture);

glBindTexture(GL_TEXTURE_2D, m_texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

if(comp == 3)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
else if(comp == 4)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);

glBindTexture(GL_TEXTURE_2D, 0);

stbi_image_free(image);

And these are the window parameters (using SDL)

SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);

What is happening?

Tinytinya answered 18/4, 2014 at 8:13 Comment(1)
++ convert tensorFixer
T
26

Changing the STBI_rgb to STBI_rgb_alpha in the stbi_load function call fixed it.

Probably best not to specify RGB when its RGBA :D

Tinytinya answered 18/4, 2014 at 11:9 Comment(0)
R
50

Your actual bug is that you use comp to determine your format (GL_RBA/GL_RGBA) parameter to glTexImage2D. This happens because when you load an image using stbi_load, the value returned in comp will always match the source image, not the image data returned.

More specifically, your bug is that you use STBI_rgb, causing stbi_load to return 3 byte pixels, but then you load it with glTexImage2D as 4 byte pixels with GL_RGBA because comp is 4 when you load a 32 bit image.

You must set the format in your call to glTexImage2D to GL_RGB if you use STBI_rgb and to GL_RGBA if you use STBI_rgb_alpha.

Bonus to other readers

Are you having a similar problem and the above still doesn't help? Then your image data might not have rows on the alignment OpenGL expects. Try glPixelStorei(GL_UNPACK_ALIGNMENT, 1); before you call glTexImage2D. See https://www.khronos.org/opengl/wiki/Pixel_Transfer#Pixel_layout for more information.

Recoup answered 6/3, 2018 at 8:29 Comment(1)
I've encountered the need for GL_UNPACK_ALIGNMENT with stb_image a few times. stb_image documentation indicates that it does not try to perform any kind of alignment - and most certainly that becomes clear when handing OpengGL an NPOT (non-power-of-two) texture without said alignment. POT textures will be naturally aligned, while NPOT textures will not be.Eubanks
T
26

Changing the STBI_rgb to STBI_rgb_alpha in the stbi_load function call fixed it.

Probably best not to specify RGB when its RGBA :D

Tinytinya answered 18/4, 2014 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.