简体   繁体   中英

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. I am setting the uniforms to 0 and 1 however, and I have bound the textures to their respective units as follows:

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).

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:

  • Adding ARB to everything, I'm not sure if this matters. (I'm on OSX 10.6 if that is of any use)
  • Adding glEnable(GL_TEXTURE_2D) every time I switch texture units.
  • Not switching between programs, and just run with one shaders, and set the uniforms in my initialization
  • Resetting the texture params every time I switch texture units.
  • Not switching between TextureUnits, and only initialize them at the start
  • Started a new project, only copied the relevant code and ran it, kept it as small as possible

All these things didn't help, the norm sampler is solely reading the GL_TEXTURE0 unit.

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.

shaderMgr->activeProgram()->setUniform1f("tex", 0);

如果这是调用glUniform1f,那是错误的,您必须使用glUniform1i设置纹理采样器。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM