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()
?
A sampler2D is used to do lookup in a standard texture image; a samplerCube is used to do lookup in a cubemap texture (Subsection 5.3. 4). The value of a sampler variable is a reference to a texture unit. The value tells which texture unit is invoked when the sampler variable is used to do texture lookup.
sampler2D is just a way to get data from an array. Don't think of it as a texture. Think of it has a 2D array with special hardware to do interpolation between values in the array (LINEAR filtering) and values between mips. Set the sampling to NEAREST and they just become 2D arrays.
gl_Position is the predefined variable which is available only in the vertex shader program. It contains the vertex position. In the above code, the coordinates attribute is passed in the form of a vector. As vertex shader is a per-vertex operation, the gl_position value is calculated for each vertex.
Description. glTexCoord specifies texture coordinates in one, two, three, or four dimensions. glTexCoord1 sets the current texture coordinates to s 0 0 1 ; a call to glTexCoord2 sets them to s t 0 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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With