Texture Mapping in OpenGL



Written by Mark Relunia
Wednesday, 10 June 2009 13:51
I'm going to teach you how to use texture mapping in OpenGL.
Before we start, be sure you know how to create OpenGL Window because I'm not going to teach you how to do it here.
Now, let's begin on how to create a texture. I'll use this image because she's cute. lol

//First we need a storage for our texture.
GLuint texture;
//Here's the pointer to the image data in memory.
unsigned char *texels = 0;
//This one holds the width and height of the image.
int width, height;
//BITMAP LOADER
//LoadFileBMP will return false if not loaded properly.
if( LoadFileBMP( filename, &texels, &width, &height, true ) )
{
// Now, we generate only one texture. Just increase the number if you're going to load more than one texture.
glGenTextures( 1, &texture );
// glBindTexture tells OpenGL to bind the named texture to a texture target. In this case, we are telling OpenGL that everything that will happen to the texture will be stored here.
glBindTexture( GL_TEXTURE_2D, texture );
//The next two lines will tell OpenGL what type of filtering to use.
//GL_TEXTURE_MAG_FILTER is used when the pixel being textured maps to an area less than or equal to one texture element.
//GL_TEXTURE_MIN_FILTER is used whenever the pixel being textured maps to an area greater than one texture element.
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//GL_TEXTURE_2D tells OpenGL that the texture is 2D
//0 represents the images level of detail
//GL_RGBA because the image is made up of red, green and blue.
//width,height is of course the width and height of the texture
//0 is the border
//GL_RGB specifies the format of the pixel data
//GL_UNSIGNED_BYTE specifies the data type of the pixel data
//image specifies a pointer to the image data in memory.
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,texels);
}
We can use the data on how to create a cube from my last tutorial.
float vertices[8][3] = { { 1.0f, 1.0f, 0.0f }, //Vertex 0
{ 0.0f, 1.0f, 0.0f }, //Vertex 1
{ 0.0f, 0.0f, 0.0f }, //Vertex 2
{ 1.0f, 0.0f, 0.0f }, //Vertex 3
{ 1.0f, 0.0f, 1.0f }, //Vertex 4
{ 1.0f, 1.0f, 1.0f }, //Vertex 5
{ 0.0f, 1.0f, 1.0f }, //Vertex 6
{ 0.0f, 0.0f, 1.0f }}; //Vertex 7
Now we can now bind the texture onto our pretty cube.
//This will enable Texture Mapping
glEnable(GL_TEXTURE_2D);
//Selects the texture to use.
glBindTexture(GL_TEXTURE_2D, texture);
//We tell OpenGL that we want to make quads.
glBegin(GL_QUADS);
//glTexCoord2f(GLfloat s, GLfloat t) is used to specify the texture coordinates
//s is the x coordinate
//t is the y coordinate
//You can set it from 0 to 1. 0 = left side of the texture, 0.5 = middle of the texture, and 1 = right side of the texture. Take a look at the square above.
//Just to make it look nice I'm going to define it. lol.
// X, Y
#define TopRight 1, 1
#define TopLeft 0, 1
#define BottomRight 1, 0
#define BottomLeft 0, 0
//Front Face
glTexCoord2f(TopRight); glVertex3fv(vertices[0]); //Top Right
glTexCoord2f(TopLeft); glVertex3fv(vertices[1]); //Top Left
glTexCoord2f(BottomLeft); glVertex3fv(vertices[2]); //Bottom Left
glTexCoord2f(BottomRight); glVertex3fv(vertices[3]); //Bottom Right
//Back Face
glTexCoord2f(TopRight); glVertex3fv(vertices[4]); //Top Right
glTexCoord2f(TopLeft); glVertex3fv(vertices[7]); //Top Left
glTexCoord2f(BottomLeft); glVertex3fv(vertices[6]); //Bottom Left
glTexCoord2f(BottomRight); glVertex3fv(vertices[5]); //Bottom Right
//Right Face
glTexCoord2f(TopLeft); glVertex3fv(vertices[0]); //Top Left
glTexCoord2f(BottomLeft); glVertex3fv(vertices[3]); //Bottom Left
glTexCoord2f(BottomRight); glVertex3fv(vertices[4]); //Bottom Right
glTexCoord2f(TopRight); glVertex3fv(vertices[5]); //Top Right
//Left Face
glTexCoord2f(TopRight); glVertex3fv(vertices[1]); //Top Right
glTexCoord2f(TopLeft); glVertex3fv(vertices[6]); //Top Left
glTexCoord2f(BottomLeft); glVertex3fv(vertices[7]); //Bottom Left
glTexCoord2f(BottomRight); glVertex3fv(vertices[2]); //Bottom Right
//Top Face
glTexCoord2f(BottomRight); glVertex3fv(vertices[0]); //Bottom Right
glTexCoord2f(TopRight); glVertex3fv(vertices[5]); //Top Right
glTexCoord2f(TopLeft); glVertex3fv(vertices[6]); //Top Left
glTexCoord2f(BottomLeft); glVertex3fv(vertices[1]); //Bottom Left
//Bottom Face
glTexCoord2f(TopLeft); glVertex3fv(vertices[7]); //Top Left
glTexCoord2f(TopRight); glVertex3fv(vertices[4]); //Top Right
glTexCoord2f(BottomRight); glVertex3fv(vertices[3]); //Bottom Right
glTexCoord2f(BottomLeft); glVertex3fv(vertices[2]); //Bottom Left
//Tells OpenGL that we are done.
glEnd();
//This will disable Texture Mapping
glDisable(GL_TEXTURE_2D);
Bitmap Loader:
DownloadVideo:
WatchNow we're done. If you have any comments or suggestions, just post a comment here or try to email me.
other websites who have been discussing the same topic, and I was wondering when
it comes to texture mapping do you have to have an image loader header file? I
believe you do, but if so how do I start one or could I just find one? Thank
you.