【问题标题】:OpenGL how to set a shader texture from the program (sampler2D)OpenGL如何从程序中设置着色器纹理(sampler2D)
【发布时间】:2012-06-24 14:18:18
【问题描述】:

我想将 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();

【问题讨论】:

  • 你的顶点着色器是什么样的?更重要的是:该程序实际所做的与您预期的有何不同?
  • 你在那里的东西对我来说看起来是正确的。你试过 glGetError 吗?

标签: c opengl shader d


【解决方案1】:

尝试将第二个采样器更改为使用 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 2017-08-25
    • 2023-03-09
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多