简体   繁体   English

在OpenGL中从着色器读取多个纹理单位的问题

[英]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: 我试过了:

  • Adding ARB to everything, I'm not sure if this matters. 将ARB添加到所有内容中,我不确定是否很重要。 (I'm on OSX 10.6 if that is of any use) (如果有什么用,我在OSX 10.6上)
  • Adding glEnable(GL_TEXTURE_2D) every time I switch texture units. 每次切换纹理单位时添加glEnable(GL_TEXTURE_2D)。
  • 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 不切换TextureUnits,仅在开始时对其进行初始化
  • 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. 所有这些都无济于事,范数采样器仅读取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.

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