-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?

4

1 回答 1

0

正如艾伦在评论中提到的那样,RGBA ? GL_RGB : GL_RGBA看起来并不安静。如果RGBA设置了 bool,你不想启用 RGBA 吗?如果是这样,这应该RGBA ? GL_RGBA : GL_RGB代替RGBA ? GL_RGB : GL_RGBA.

这将评估GL_RGBA是否设置了 RGBA 标志,GL_RGB否则将评估为。您当前的代码正好相反。当然,这种反向行为可能是故意的,尽管从标志名称来看似乎不太可能RGBA

此外,您可能还想启用混合。

尝试添加这两行

glEnable(GL_BLEND);

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

于 2022-01-29T14:54:34.840 回答