问题
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()?
回答1:
The sampler2D is bound to a texture unit. The glUniform call binds it to texture unit zero. The glActiveTexture() call is only needed if you are going to use multiple texture units (because GL_TEXTURE0 is the default anyway).
来源:https://stackoverflow.com/questions/10868958/what-does-sampler2d-store