opengl - 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 看到上述所有输入的事实相矛盾?
我在我的代码中做错了什么吗?
解决方案
看起来您正在尝试使用绑定纹理单元,glUniform1f
但纹理单元索引是整数(不是浮点数),因此您应该使用glUniform1i
来设置该统一。
glUniform1i(glGetUniformLocation(shader.ID, material_uniforms[i].c_str()), i);
推荐阅读
- domain-driven-design - 领域驱动设计 - 从 UI 增量保存
- php - 无法从 PHP 函数回显或打印
- c++ - 添加了 Info.plist 但 Xcode 仍然崩溃
- reactjs - 如何通过reactjs在点击事件上批准待处理按钮显示
- scala - Scalatra SwaggerSupport NPEs on simple case class?
- php - 我们可以在客户端或服务器端保护 JSON 文件数据吗
- python - 模块“tensorflow._api.v2.train”没有属性“GradientDescentOptimizer”
- android - 如何将布局(由java制作)附加到片段?
- javascript - 如何在 div 中显示 jquery 警报消息?
- azure - 部署到 Azure 应用服务时,Webpack 应用(办公插件)无法启动