WebGL上的多纹理GL_纹理1问题
我正在学习在WebGL上使用multitexture。我有以下着色器代码片段:WebGL上的多纹理GL_纹理1问题,webgl,Webgl,我正在学习在WebGL上使用multitexture。我有以下着色器代码片段: 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(textu
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;
}
}
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);
另外,我的JavaScript代码片段:
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;
}
}
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)在着色器中不可见
由于上面的代码不起作用,而且我是新手,我想知道我是否做错了什么和/或我误解了multitexture如何工作的概念
感谢您的帮助。提前感谢你的帮助
注意:我在上发现并修复了该问题
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语言编写的示例中剪切“n”粘贴GL\u Texture E0。WebGL在Texture E0、Texture E1等上不使用前缀“GL\uU”。请参阅WebGL规范