I’ve read a texture example in OpenGL 2.1. The fragment shader looks like this:
#version 120
uniform sampler2D texture;
varying vec2 texcoord;
void main(void)
{
gl_FragColor = texture2D(texture, texcoord);
}
The texcoord is passed from vertex shader.
The following C++ rendering code is used:
void render()
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture_id);
glUniform1i(unf_texture, 0);
}
I am confused about some things. I have some question:
-
In the fragment shader, the texture is passed a zero value (by
glUniform1i()). Is the value really zero? Is the value something else? -
Is
glActiveTexture()call really need? -
Why do we pass a zero value in
glUniform1i()?
The
sampler2Dis bound to a texture unit. TheglUniformcall binds it to texture unit zero. TheglActiveTexture()call is only needed if you are going to use multiple texture units (becauseGL_TEXTURE0is the default anyway).