Multitexture GL_TEXTURE1 issue on WebGL

2019-04-10 21:44发布

I am learning to use multitexture on WebGL. I have the following shader code snippets:

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

Also, my JavaScript code snippets:

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

The photoTexture1 (texture1) displays black only on the screen. I can display the photoTexture0 (texture0) correctly. It seems that photoTexture1 (texture1) is not visible in the shader.

Since the above code does not work and I am new on this, I am wondering if I did something wrong and/or I misunderstand the concept how multitexture works.

Any help is appreciated. Thanks in advance for your help.

NOTE: Cross post on http://www.khronos.org/message_boards/viewtopic.php?f=43&t=3357&p=8858#p8858

标签: webgl
1条回答
Melony?
2楼-- · 2019-04-10 22:05

I found and fixed the problem

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

I cut'n'paste the GL_TEXTURE0 from an example written in C language.

查看更多
登录 后发表回答