// Array of texels, dynamically populated fom an image file:
GLubyte* image_data; // Raw color information from a file
/* Load image data... (not shown) */
glEnable( GL_TEXTURE_2D ); // Turn on texturing
// Create a single texture object:
glGenTextures( 1, &tex_buffer_ID ); // Create an ID for a texture buffer
glBindTexture( GL_TEXTURE_2D, tex_buffer_ID ); // Bind texture buffer to GL_TEXTURE_2D target
glTexImage2D( // allocate storage for the texture and pass data to the texture buffer
GL_TEXTURE_2D, 0, GL_RGB, bmpWidth, bmpHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, image_data );
/* | | | | | | |
| | | | | | Raw data
| | | | | C++ client pixel data type
| | | | C++ client pixel data format
| | | must be 0, no longer used
| | internal format, the number of color components in the texture
| mipmapping level: 0 is the base image level
target texture type */
tex_coord_ID = glGetAttribLocation( shader_program_ID, "s_vTexCoord" );
glEnableVertexAttribArray( tex_coord_ID );
int texture_coord_offset = 6*NUM_VERTICES*sizeof( GLfloat ); // see example/Lecture17_Texture for details
glVertexAttribPointer(
tex_coord_ID, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET( texture_coord_offset ) );
// Set the preferences:
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
texture_ID = glGetUniformLocation( shader_program_ID, "texture" );
const int TEXTURE_UNIT_ZERO = 0;
// First, activate texture unit (relative to GL_TEXTURE0)
glActiveTexture( GL_TEXTURE0 + TEXTURE_UNIT_ZERO );
// Second, tell "texture" sampler to use the 0th texture unit:
glUniform1i( texture_ID, TEXTURE_UNIT_ZERO );