[英]Problem with reading multiple texture units from a Shader in OpenGL
I am trying to read two different textures in my shader, one for regular texturing, and one bumpmap. 我正在尝试在着色器中读取两种不同的纹理,一种用于常规纹理化,一种用于凹凸贴图。 However both Sampler2D's are reading from the same texture unit. 但是,两个Sampler2D都从同一纹理单元读取。 I am setting the uniforms to 0 and 1 however, and I have bound the textures to their respective units as follows: 我将统一设置为0和1,并将纹理绑定到其各自的单元,如下所示:
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texMgr->GetTexture("stone")->texture);
glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texMgr->GetTexture("bump")->texture);
In my render loop I set the uniforms as follows: 在渲染循环中,我将制服设置如下:
shaderMgr->activeProgram()->setUniform1f("tex", 0);
shaderMgr->activeProgram()->setUniform1f("norm", 1);
And finally my shader code: 最后是我的着色器代码:
varying vec4 colorVarying;
varying vec4 normalVarying;
varying vec3 lightDirVarying;
varying vec2 textureCoordinateVarying;
uniform sampler2D tex;
uniform sampler2D norm;
void main() {
vec4 texColor = texture2D(tex, textureCoordinateVarying);
vec4 normColor = texture2D(norm, textureCoordinateVarying);
vec3 newNormal = vec3(2.0 * normColor.x - 1.0, 2.0 * normColor.y - 1.0, 2.0 * normColor.z - 1.0);
vec3 normal = normalize(normalVarying.xyz + newNormal);
float diff = max(0.0, dot(normal, normalize(lightDirVarying)));
gl_FragColor = texColor * (diff + 0.3);
}
I only call glActiveTexture in my renderloop every time I draw an Object, and I only call glActiveTexture(GL_TEXTURE1) once (in my initialization). 每次绘制对象时,我只会在我的renderloop中调用glActiveTexture,并且只调用一次glActiveTexture(GL_TEXTURE1)(在初始化过程中)。
glActiveTexture(GL_TEXTURE0);
glBindTexture( GL_TEXTURE_2D, object->tex->texture);
Everything is working fine for the first texture, but the second texture (Bump) isn't showing up. 第一个纹理一切正常,但第二个纹理(凹凸)未显示。 I've tried: 我试过了:
All these things didn't help, the norm sampler is solely reading the GL_TEXTURE0 unit. 所有这些都无济于事,范数采样器仅读取GL_TEXTURE0单元。
I have been searching for hours now, and I still haven't found the answer. 我已经搜索了几个小时,但仍然没有找到答案。 I have no idea what I am doing wrong and why my norm sampler isn't reading from the proper GL_TEXTURE1 unit. 我不知道我在做什么错,为什么我的范数采样器无法从正确的GL_TEXTURE1单元读取。
shaderMgr->activeProgram()->setUniform1f("tex", 0);
如果这是调用glUniform1f,那是错误的,您必须使用glUniform1i设置纹理采样器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.