首页 > 解决方案 > OpenGL/GLSL 多纹理绑定不起作用

问题描述

周围有很多类似问题的答案,但我找不到适合我的解决方案。

我有一个包含多个网格的场景,每个都有以下绘制调用:

shader.use();

for (unsigned int i = 0; i < textures.size(); i++)
{
    glActiveTexture(GL_TEXTURE0 + i);
    glBindTexture(GL_TEXTURE_2D, textures[i].id);
}

for (unsigned int i = 0; i < textures.size(); i++)
{
    glUniform1f(glGetUniformLocation(shader.ID, material_uniforms[i].c_str()), i);
}

glActiveTexture(GL_TEXTURE0);

// draw mesh
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);

我的碎片着色器制服(除其他外)是:

uniform sampler2D texture_ambient1;
uniform sampler2D texture_diffuse1;
uniform sampler2D texture_specular1;
uniform sampler2D texture_bump1;
uniform sampler2D texture_dissolve1;

在 RenderDoc 中,虽然在资源检查器中似乎列出了所有纹理,但纹理查看器中的“输入”(FS 0[0] texture_ambient1、FS 1[0] texture_diffuse1、FS 2[0] texture_dissolve1 和 FS 3[0] texture_specular1(我不知道我的另一个“texture_bump1”去了哪里?))每个绘制调用似乎都是完全相同的纹理。

同样在“Pipeline State”和FS Stage下的RenderDoc中,Textures和Samplers每个只有一个绑定项目?似乎与 RenderDoc 看到上述所有输入的事实相矛盾?

我在我的代码中做错了什么吗?

标签: opengl

解决方案


看起来您正在尝试使用绑定纹理单元,glUniform1f但纹理单元索引是整数(不是浮点数),因此您应该使用glUniform1i来设置该统一。

glUniform1i(glGetUniformLocation(shader.ID, material_uniforms[i].c_str()), i);

推荐阅读