Upload
sardar-alam
View
112
Download
5
Embed Size (px)
DESCRIPTION
texturing on objects using opengl library
Citation preview
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 textureglBindTexture( GL_TEXTURE_2D, texture ); now we bind the texture that we are working withglTexEnvf( 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_NEARESTGL_LINEARGL_LINEAR_MIPMAP_NEARESTGL_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 textureto 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 inevitablyslow 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 ’textur
e’ 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.