在 OpenGL 中从着色器读取多个纹理单元时出现问题

发布于 2024-10-17 17:16:33 字数 1840 浏览 3 评论 0原文

我正在尝试在着色器中读取两种不同的纹理,一种用于常规纹理,一种用于凹凸贴图。然而,两个 Sampler2D 都从同一纹理单元读取。然而,我将制服设置为 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);

在我的渲染循环中,我将制服设置如下:

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

最后是我的着色器代码:

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);
}

我每次只在渲染循环中调用 glActiveTexture我绘制一个对象,并且只调用一次 glActiveTexture(GL_TEXTURE1) 一次(在我的初始化中)。

glActiveTexture(GL_TEXTURE0);
glBindTexture( GL_TEXTURE_2D, object->tex->texture);

第一个纹理一切正常,但第二个纹理(凹凸)没有显示。我尝试过:

  • 将 ARB 添加到所有内容中,我不确定这是否重要。 (我使用的是 OSX 10.6,如​​果有任何用处的话)
  • 每次切换纹理单元时添加 glEnable(GL_TEXTURE_2D) 。
  • 的初始化中设置制服
  • 不在程序之间切换,仅使用一个着色器运行,并在每次切换纹理单元时重置纹理参数
  • 。不在TextureUnits之间切换,只在开始时初始化它们
  • 开始一个新项目,只复制相关代码并运行它,使其尽可能小

所有这些都没有帮助,规范采样器仅读取GL_TEXTURE0单元。

我已经搜索了几个小时了,但仍然没有找到答案。我不知道我做错了什么,也不知道为什么我的标准采样器没有从正确的 GL_TEXTURE1 单元读取数据。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

菩提树下叶撕阳。 2024-10-24 17:16:33
shaderMgr->activeProgram()->setUniform1f("tex", 0);

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

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

If this is calling glUniform1f, that's wrong, you must use glUniform1i to set texture samplers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文