OpenGL如何从程序中设置着色器纹理(sampler2D)

时间:2012-06-24 14:18:18

标签: c opengl shader d

我想将GLSL着色器中的2个纹理组合为1。

问题是我无法在我的程序中设置着色器中的sampler2D。 (着色器/程序正确编译,纹理加载正确,顶点着色器也正确)我尝试使用教程中的以下代码:

glUniform1i(program.getUniformLocation("Texture0"), 0);
glUniform1i(program.getUniformLocation("Texture1"), 1);

//texture 0, first texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, colorTexture.handle);

//texture 1, other texture
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, normalTexture.handle);

片段着色器代码如下所示

uniform sampler2D Texture0;
uniform sampler2D Texture1;

void main(void)
{
    vec4 color = texture2D(Texture0, vec2(gl_TexCoord[0]));
    vec4 normal = texture2D(Texture1, vec2(gl_TexCoord[0]));
    gl_FragColor = normal + color;
}

绘制
glUseProgram(program.handle);
glColor3f(1, 1, 1);
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0);
    glVertex3f(0, 0, 0);
    glTexCoord2f(1, 0);
    glVertex3f(500, 0, 0);
    glTexCoord2f(1, 1);
    glVertex3f(500, 500, 0);
    glTexCoord2f(0, 1);
    glVertex3f(0, 500, 0);
    glEnd();

1 个答案:

答案 0 :(得分:2)

尝试更改第二个采样器以使用gl_TexCoord [1]

uniform sampler2D Texture0;
uniform sampler2D Texture1;

void main(void)
{
    vec4 color = texture2D(Texture0, vec2(gl_TexCoord[0]));
    vec4 normal = texture2D(Texture1, vec2(gl_TexCoord[1]));
    gl_FragColor = normal + color;
}