OpenGL how to set a shader texture from the program (sampler2D)
12,742
try changing the second sampler to use 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;
}
Author by
Moritz Schöfl
Updated on June 04, 2022Comments
-
Moritz Schöfl almost 2 years
I want to combine 2 textures in a GLSL shader to 1.
the problem is that I fail to set the sampler2D in the shader from my program. (the shader/program compiles correctly, the textures load correctly, the vertex shader is correct too) I tried it with the following code from a tutorial:
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);
the fragment shader code looks like this
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; }
it gets drawn with
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();