WebGL 上的多纹理 GL_TEXTURE1 问题 [英] Multitexture GL_TEXTURE1 issue on WebGL

查看:28
本文介绍了WebGL 上的多纹理 GL_TEXTURE1 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

另外,我的 JavaScript 代码片段:

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

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

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.

注意:在 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.

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

这篇关于WebGL 上的多纹理 GL_TEXTURE1 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆