在 OpenGL 中从着色器读取多个纹理单元时出现问题
我正在尝试在着色器中读取两种不同的纹理,一种用于常规纹理,一种用于凹凸贴图。然而,两个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果这是调用glUniform1f,那是错误的,你必须使用glUniform1i来设置纹理采样器。
If this is calling glUniform1f, that's wrong, you must use glUniform1i to set texture samplers.