-1

So I have this problem: I am loading image with stbi_load() and then creating texture with glTexImage2D(). When I load .jpg image, then everything works normaly, but when I load .png, then the drawn image is black, where it should be transparent. Here is picture: pictureOfWronglyDrawnPng

Here is code I have used:

Frist I set texture parametri:

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_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

Then I load the picture:

m_data = stbi_load(pathToTexture, &m_height, &m_width, &m_nrChannels, RGBA ? STBI_rgb : STBI_rgb_alpha);

And lastly I create texture and genarte mipMap:

        glTexImage2D(GL_TEXTURE_2D, 0, RGBA ? GL_RGB : GL_RGBA, m_height, m_width, 0, RGBA ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, m_data);
    glGenerateMipmap(GL_TEXTURE_2D);

RGBA is bool that i set when i create Texture class

Here is also my fragment shader:

#version 330 core

out vec4 OutFrag;

uniform vec3 color;
uniform sampler2D tex;

in vec2 TexPos;

void main()
{
    OutFrag = vec4(color, 1.0)  * texture(tex, TexPos);
}

Does anyone know, why is the texture drawing incorrectly?

1 Answers1

0

As Alan has mentioned in a comment RGBA ? GL_RGB : GL_RGBA doesn't quiet look right. If RGBA bool is set, don't you want to enable RGBA? if so this should be RGBA ? GL_RGBA : GL_RGB instead of RGBA ? GL_RGB : GL_RGBA.

This will evaluate to GL_RGBA if the RGBA flag is set and would evaluate to GL_RGB otherwise. Your current code does the opposite. Of course it could be the case that this inverse behavior was intentional though seems unlikely judging by the flag name RGBA.

Also you might want to enable blending as well.

Try adding these two lines

glEnable(GL_BLEND);

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

I-A-S
  • 1