Hi all,
I have the following code example from the book “Interactive Computer Graphics A Top-Down Approach 6th Edition” that exemplifies the use of textures.
So far I was ok with the book, but I started having problems understanding how texture are sent to the fragment shader.
-
According to OpenGL reference page: “You must use glGenTextures to generate a set of new texture names.”, and this is what the first glBindTexture( GL_TEXTURE_2D, textures[0] ) is doing.
Why is glBindTexture( GL_TEXTURE_2D, textures[0] ) being called a second time if the name of the texture has already been generated? -
According to the reference: “glActiveTexture selects which texture unit subsequent texture state calls will affect. The number of texture units an implementation supports is implementation dependent, but must be at least 80.”
What does this really means? -
What command should I execute and where if I want to send to the fragment shader textures[1] rather than texture[0] ?
Any help would be much appreciated!!
// Texture objects and storage for texture image
GLuint textures[2];
void init() {
// Initialize texture objects
glGenTextures( 2, textures );
[b] glBindTexture( GL_TEXTURE_2D, textures[0] );[/u]
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, TextureSize, TextureSize, 0, GL_RGB, GL_UNSIGNED_BYTE, image );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glBindTexture( GL_TEXTURE_2D, textures[1] );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, TextureSize, TextureSize, 0, GL_RGB, GL_UNSIGNED_BYTE, image2 );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
[b] glActiveTexture( GL_TEXTURE0 );[/b]
[b] glBindTexture( GL_TEXTURE_2D, textures[0] );[/b]
// Create a vertex array object
...
// Create and initialize a buffer object
...
// Load shaders and use the resulting shader program
GLuint program = InitShader( "vshader71.glsl", "fshader71.glsl" );
glUseProgram( program );
// set up vertex arrays
...
// Set the value of the fragment shader texture sampler variable
// ("texture") to the the appropriate texture unit. In this case,
// zero, for GL_TEXTURE0 which was previously set by calling
// glActiveTexture().
glUniform1i( glGetUniformLocation(program, "texture"), 0 );
...
}
Glut display callback function
void display( void ) {
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
...
glDrawArrays( GL_TRIANGLES, 0, NumVertices );
glutSwapBuffers();
}
Fragment shader
#version 150
in vec4 color;
in vec2 texCoord;
out vec3 fColor;
uniform sampler2D texture10;
void main() {
fColor = texture2D( texture0, texCoord ).rgb;
}