0

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:

Source Image

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)

Output

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.

Correct Image

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • It seems that the output image points to the wrong target (or at least there's nothing red in it). – BDL Aug 24 '21 at 22:24
  • Yes you're right! It's fixed now. Sorry, I tried to embed the images, but then when I converted them to links, I accidentally put the wrong image for the second one. – labmonkey398 Aug 24 '21 at 22:32

0 Answers0