gpt4 book ai didi

webgl - WebGL 上的多纹理 GL_TEXTURE1 问题

转载 作者:行者123 更新时间:2023-11-30 23:54:55 30 4
gpt4 key购买 nike

我正在学习在 WebGL 上使用多纹理。我有以下着色器代码片段:

uniform int flag;

uniform sampler2D texture0;
uniform sampler2D texture1;

varying vec2 v_texCoord;

void main()
{
vec2 texCoord = vec2(v_texCoord.s, 1.0 - v_texCoord.t);

vec4 texel0 = texture2D(texture0, texCoord);
vec4 texel1 = texture2D(texture1, texCoord);

if (1 == flag)
{
gl_FragColor = texel1;

} else
{
gl_FragColor = texel0;
}
}

另外,我的 JavaScript 代码片段:
gl.uniform1i(gl.getUniformLocation(gl.program, "flag"), 1);
gl.uniform1i(gl.getUniformLocation(gl.program, "texture0"), 0); // Texture Unit 0
gl.uniform1i(gl.getUniformLocation(gl.program, "texture1"), 1); // Texture Unit 1
// Activate 2 textures
gl.enable(gl.TEXTURE_2D);

gl.activeTexture(gl.GL_TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, photoTexture0);

gl.activeTexture(gl.GL_TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, photoTexture1);

photoTexture1 (texture1) 仅在屏幕上显示黑色。我可以正确显示 photoTexture0 (texture0)。似乎 photoTexture1 (texture1) 在着色器中不可见。

由于上面的代码不起作用而且我是新手,我想知道我是否做错了什么和/或我误解了多纹理如何工作的概念。

任何帮助表示赞赏。在此先感谢您的帮助。

注意:在 http://www.khronos.org/message_boards/viewtopic.php?f=43&t=3357&p=8858#p8858 上交叉发布

最佳答案

我发现并解决了问题

gl.activeTexture(gl.TEXTURE0);   // remove the "GL_" on GL_TEXTURE0
gl.bindTexture(gl.TEXTURE_2D, photoTexture0);

gl.uniform1i(gl.getUniformLocation(gl.program, "texture1"), 0);
gl.activeTexture(gl.TEXTURE1); // remove the "GL_" on GL_TEXTURE1

我从用 C 语言编写的示例中剪切了 GL_TEXTURE0。

关于webgl - WebGL 上的多纹理 GL_TEXTURE1 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4476560/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com