OpenGL Texturing
When we apply image data to a geometric primitive, 
we call this a texture or texture map. 
shows the dramatic difference that can be achieved 
by texture mapping geometry. 
The cube on the left is a lit and shaded featureless 
surface, whereas the cube on the right shows a 
richness in detail that can be reasonably achieved 
only with texture mapping.
Loading Textures 
Using GLuint we create a variable for the texture, called 
‘texture’, but you can call it. 
In the ‘display’ function we first set the variable ‘texture’ 
to the actual loaded image using: 
texture = LoadTexture( “texture.raw”, 256, 256 ); 
where 256, 256 is the width and the height of the file 
respectively. 
Then we enable the 2D Texturing, this is done with: 
glEnable( GL_TEXTURE_2D ); 
and bind the texture with: 
glBindTexture( GL_TEXTURE_2D, texture ); 
then after drawing everything we need with the texture, 
we clear it to save system resources.
Section 1: The loading of the texture: 
GLuint LoadTexture( const char * filename, int width, int 
height ) 
{ 
GLuint texture; 
unsigned char * data; 
FILE * file; 
The following code will read in our RAW file 
file = fopen( filename, “rb” ); We need to open our file 
if ( file == NULL ) return 0; If our file is empty, set our 
texture to empty 
data = (unsigned char *)malloc( width * height * 3 ); 
assign the nessecary memory for the texture 
fread( data, width * height * 3, 1, file ); read in our file 
fclose( file ); close our file, no point leaving it open
glGenTextures( 1, &texture ); then we need to tell 
OpenGL that we are generating a texture 
glBindTexture( GL_TEXTURE_2D, texture ); now 
we bind the texture that we are working with 
glTexEnvf( GL_TEXTURE_ENV, 
GL_TEXTURE_ENV_MODE, GL_MODULATE ); set 
texture environment parameters 
The parameter GL_MODULATE will blend the 
texture with whatever is underneath, setting it 
to GL_DECAL 
will tell the texture to replace whatever is on the 
object.
here we are setting what textures to use and when. The 
MIN filter is which quality to show when the texture is 
near the view, 
the MAG filter is which quality to show when the texture 
is far from the view. 
The qualities are (in order from worst to best) 
GL_NEAREST 
GL_LINEAR 
GL_LINEAR_MIPMAP_NEAREST 
GL_LINEAR_MIPMAP_LINEAR 
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
GL_LINEAR ); 
glTexParameterf( GL_TEXTURE_2D, 
GL_TEXTURE_MAG_FILTER, GL_LINEAR );
Here we are setting the parameter to repeat the texture 
instead of clamping the texture 
to the edge of our shape. 
glTexParameterf( GL_TEXTURE_2D, 
GL_TEXTURE_WRAP_S, GL_REPEAT ); 
glTexParameterf( GL_TEXTURE_2D, 
GL_TEXTURE_WRAP_T, GL_REPEAT ); 
Generate the texture 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 
0, GL_RGB, GL_UNSIGNED_BYTE, data); 
free( data ); free the texture 
return texture; return the texture data 
}
Section 2: Cleaning Up: 
If we decide not to clean up after our work, you will 
find that the program will inevitably 
slow down your computer, taking more and more 
memory. 
void FreeTexture( GLuint texture ) 
{ 
glDeleteTextures( 1, &texture ); Delete our 
texture, simple enough. 
}
Section 3: Mapping Textures to Geometry 
You provide OpenGL with information about how to map 
the texture to the geometry. 
You do this by specifying a texture coordinate for each 
vertex. 
Texels in a texture map are addressed not as a memory 
location (as you would for pixmaps), but as a more 
abstract (usually floating-point values) texture coordinate. 
Typically, texture coordinates are specified as floating-point 
values that are clamped in the range 0.0 to 1.0. 
Texture coordinates are named s, t, r, and q (similar to 
vertex coordinates x, y, z, and w) supporting from one- to 
three-dimensional texture coordinates, and optionally a 
way to scale the coordinates.
void glTexCoord1f(GLfloat s); 
void glTexCoord2f(Glfloat s, GLfloat t); 
void glTexCoord3f(GLfloat s, GLfloat t, GLfloat r);
OpenGL then stretches or shrinks the texture as 
necessary to apply the texture to the geometry as 
mapped 
An example of a two-dimensional texture being 
mapped to a GL_QUAD. Note the corners of the 
texture correspond to the corners of the quad. As 
you do with other vertex properties (materials, 
normals, and so on), you must specify the texture 
coordinate before the vertex!
This figure also shows a square texture map, but the 
geometry is a triangle. Superimposed on the texture 
map are the texture coordinates of the locations in 
the map being extended to the vertices of the 
triangle.
glBegin(GL_TRIANGLES); 
glTexCoord2f(1.0f, 1.0f); 
glVertex3fv(1.0,1.0,1.0); 
glBegin (GL_QUADS); 
glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0); 
glTexCoord2d(1.0,0.0); glVertex2d(1.0,-1.0); 
glTexCoord2d(1.0,1.0); glVertex2d(1.0,1.0); 
glTexCoord2d(0.0,1.0); glVertex2d(-1.0,1.0); 
glEnd();
the windows header file, then we also need to add the 
Stdio header file. 
in the ’display’ function we first set the variable ’texture’ 
to the actual loaded image using: 
texture = LoadTexture( ”texture.raw”, 256, 256 ); 
where 256, 256 is the width and the height of the file 
respectively. 
then we enable the 2D Texturing, this is done with: 
glEnable( GL_TEXTURE_2D ); 
and bind the texture with: 
glBindTexture( GL_TEXTURE_2D, texture ); 
then after drawing everything we need with the texture,w 
e clear it to save system resources.

Opengl texturing

  • 1.
  • 2.
    When we applyimage data to a geometric primitive, we call this a texture or texture map. shows the dramatic difference that can be achieved by texture mapping geometry. The cube on the left is a lit and shaded featureless surface, whereas the cube on the right shows a richness in detail that can be reasonably achieved only with texture mapping.
  • 3.
    Loading Textures UsingGLuint we create a variable for the texture, called ‘texture’, but you can call it. In the ‘display’ function we first set the variable ‘texture’ to the actual loaded image using: texture = LoadTexture( “texture.raw”, 256, 256 ); where 256, 256 is the width and the height of the file respectively. Then we enable the 2D Texturing, this is done with: glEnable( GL_TEXTURE_2D ); and bind the texture with: glBindTexture( GL_TEXTURE_2D, texture ); then after drawing everything we need with the texture, we clear it to save system resources.
  • 4.
    Section 1: Theloading of the texture: GLuint LoadTexture( const char * filename, int width, int height ) { GLuint texture; unsigned char * data; FILE * file; The following code will read in our RAW file file = fopen( filename, “rb” ); We need to open our file if ( file == NULL ) return 0; If our file is empty, set our texture to empty data = (unsigned char *)malloc( width * height * 3 ); assign the nessecary memory for the texture fread( data, width * height * 3, 1, file ); read in our file fclose( file ); close our file, no point leaving it open
  • 5.
    glGenTextures( 1, &texture); then we need to tell OpenGL that we are generating a texture glBindTexture( GL_TEXTURE_2D, texture ); now we bind the texture that we are working with glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); set texture environment parameters The parameter GL_MODULATE will blend the texture with whatever is underneath, setting it to GL_DECAL will tell the texture to replace whatever is on the object.
  • 6.
    here we aresetting what textures to use and when. The MIN filter is which quality to show when the texture is near the view, the MAG filter is which quality to show when the texture is far from the view. The qualities are (in order from worst to best) GL_NEAREST GL_LINEAR GL_LINEAR_MIPMAP_NEAREST GL_LINEAR_MIPMAP_LINEAR glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  • 7.
    Here we aresetting the parameter to repeat the texture instead of clamping the texture to the edge of our shape. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); Generate the texture glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); free( data ); free the texture return texture; return the texture data }
  • 8.
    Section 2: CleaningUp: If we decide not to clean up after our work, you will find that the program will inevitably slow down your computer, taking more and more memory. void FreeTexture( GLuint texture ) { glDeleteTextures( 1, &texture ); Delete our texture, simple enough. }
  • 9.
    Section 3: MappingTextures to Geometry You provide OpenGL with information about how to map the texture to the geometry. You do this by specifying a texture coordinate for each vertex. Texels in a texture map are addressed not as a memory location (as you would for pixmaps), but as a more abstract (usually floating-point values) texture coordinate. Typically, texture coordinates are specified as floating-point values that are clamped in the range 0.0 to 1.0. Texture coordinates are named s, t, r, and q (similar to vertex coordinates x, y, z, and w) supporting from one- to three-dimensional texture coordinates, and optionally a way to scale the coordinates.
  • 10.
    void glTexCoord1f(GLfloat s); void glTexCoord2f(Glfloat s, GLfloat t); void glTexCoord3f(GLfloat s, GLfloat t, GLfloat r);
  • 11.
    OpenGL then stretchesor shrinks the texture as necessary to apply the texture to the geometry as mapped An example of a two-dimensional texture being mapped to a GL_QUAD. Note the corners of the texture correspond to the corners of the quad. As you do with other vertex properties (materials, normals, and so on), you must specify the texture coordinate before the vertex!
  • 12.
    This figure alsoshows a square texture map, but the geometry is a triangle. Superimposed on the texture map are the texture coordinates of the locations in the map being extended to the vertices of the triangle.
  • 13.
    glBegin(GL_TRIANGLES); glTexCoord2f(1.0f, 1.0f); glVertex3fv(1.0,1.0,1.0); glBegin (GL_QUADS); glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0); glTexCoord2d(1.0,0.0); glVertex2d(1.0,-1.0); glTexCoord2d(1.0,1.0); glVertex2d(1.0,1.0); glTexCoord2d(0.0,1.0); glVertex2d(-1.0,1.0); glEnd();
  • 14.
    the windows headerfile, then we also need to add the Stdio header file. in the ’display’ function we first set the variable ’texture’ to the actual loaded image using: texture = LoadTexture( ”texture.raw”, 256, 256 ); where 256, 256 is the width and the height of the file respectively. then we enable the 2D Texturing, this is done with: glEnable( GL_TEXTURE_2D ); and bind the texture with: glBindTexture( GL_TEXTURE_2D, texture ); then after drawing everything we need with the texture,w e clear it to save system resources.