OpenGL我的纹理显示为单色。尝试不同的纹理只能从纹理中单独出来。如果纹理多数为绿色,则输出为绿色。
我的纹理类
Texture::Texture(const std::string& FilePath)
{
this->RenderID = 0;
this->FilePath = FilePath;
this->localBuffer = nullptr;
this->width = 0;
this->height = 0;
this->BPP = 0;
stbi_set_flip_vertically_on_load(1);
localBuffer = stbi_load(this->FilePath.c_str(), &width, &height, &BPP, 4);
glGenTextures(1, &RenderID);
glBindTexture(GL_TEXTURE_2D, RenderID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if (localBuffer)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, localBuffer);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "error damn it";
}
unbind();
stbi_image_free(localBuffer);
}
我的主循环
while (!window.isClosed())
{
window.clear();
render.Draw(vao, ibo, shade);
texture.bind();
shade.setUniform1i("u_texture", 0);
window.update();
}
片段着色器:
#version 330 core
layout(location = 0) out vec4 FragColor;
in vec2 tex_out;
uniform sampler2D u_texture;
void main()
{
vec4 tex = texture(u_texture , tex_out);
FragColor = tex;
}
使用的纹理:
输出: