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

标签 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();

最佳答案

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

关于c - OpenGL如何从程序中设置着色器纹理(sampler2D),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11178196/

相关文章:

在 Linux 上将 CUDA 代码编译为静态库 (.a)

c - 从文件中读取选项

c++ - OpenGl 读取和写入相同的纹理

OpenGL透视投影像素完美绘图

c++ - 如何使用具有 3D 纹理的图像存储?

c - foo((&i)++) 在 C 中给出编译错误

c - 最后输入特定字符串-c程序

c++ - 着色器限制

android - OpenGL 二维水模拟

javascript - 如何在 THREE.js 中正确制作法线贴图?