Programming - Games - OpenGL and C++

This toturial was written for me by Imran Khan (imranahmedkhan82@hotmail.com, iak1982@yahoo.com).
Copyright (c) 2004 is owned by Martin Baker

Texture Mapping:

This is one of the most important section of this Tutorial.In this section we will learn how to map a bitmap on a 3D object.Here we will Map a bitmap to a cube, we already draw in our previous section.

First of all we will define a variable on the top.

GLuint texture[1];

This Variable will hold the texture u want to map. If u want to map one texture then the array size will be one.If u want different texture, array size can be increase. Like if you want to use 3 texture on a single object.Then the declaration will be like

" GLunit texture[3]; "

Now we will write a function which will load a BMP.

AUX_RGBImageRec *LoadBMP(char *Filename)
{
FILE *File=NULL;

if (!Filename)
{
return NULL;
}

File=fopen(Filename,"r");

if (File)
{
fclose(File);
return auxDIBImageLoad(Filename);
}

return NULL;
}

This Method takes filesname as argument and return a pointer. First we check whether the file name specified or not. If not then return NULL. Next we open the Bitmap file for reading ("r").Then we check whether the file exist or not. If yes then close the file and return the data from the Bitmap.

Now we will write a method which will call the above method to load the BMP and convert it into Texture.

int LoadGLTextures()
{
int Status=FALSE;

AUX_RGBImageRec *TextureImage[1];

memset(TextureImage,0,sizeof(void *)*1);
if (TextureImage[0]=LoadBMP("Data/image1.bmp"))
{
Status=TRUE;

glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}

if (TextureImage[0])
{
if (TextureImage[0]->data)
{
free(TextureImage[0]->data);
}

free(TextureImage[0]);
}

return Status;
}

First we Define a simple variable which we will use for True/False. Next we create an image record that we can store our bitmap in. The record will hold the bitmap width, height, and data. Then we Clear the image record.Then we write a condition in which it will load a BMP and return its width,height,data (record) and assign this record to a variable (image record) above.If all goes fine we will set the status to TRUE.Then we call a method " glGenTextures(1,&texture[0]) " which is use to tells OpenGL that we want to generate one texture name.If you want more texture then u can increase the number.Next line "glBindTexture(GL_TEXTURE_2D, texture[0]) " tells OpenGL to bind the named texture texture[0] to a texture
target. Next we create the actual texture. The " glTexImage2D(...) " line tells OpenGL the texture will be a 2D texture.

glTexImage2D got 9 arguments which are as follows :

  • 1st - the texture will be a 2D texture (GL_TEXTURE_2D) or 3D.
  • 2nd - level of detail.
  • 3rd - number of data components.( here it is 3 , coz we image is compose of (R,G,B).
  • 4th - width of the texture.
  • 5th - height of the texture.
  • 6th - border.
  • 7th - tells the data component. (here it is R,G,B).
  • 8th - the data that makes up the image is made up of unsigned bytes.
  • 9th - data of the texture.

The next two lines tell OpenGL what type of filtering to use when the image is larger (GL_TEXTURE_MAG_FILTER) or stretched on the screen than the original texture, or when it's smaller (GL_TEXTURE_MIN_FILTER) on the screen than the actual texture. GL_LINEAR makes the texture look smooth.

Next lines use to free up the Memory.

Now we will change the "InitGL" method. i.e. It will load texture in the beginning.

int InitGL(GLvoid)
{
if (!LoadGLTextures())
{
return FALSE;
}

glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glEnable(GL_DEPTH_TEST);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
return TRUE;
}

First we try to load the texture " LoadGLTexture " . If it fails to load the texture it will return False. Then we enable 2D texture mapping.Next 2 lines we already described in " My First OpenGL Program ". Next line "glEnable(GL_DEPTH_TEST) " enables the depth test..Then we set the prespective view and return TRUE.

Now the final method "DrawGLScene" ...

int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-5.0f);
glRotatef(30.0f,0.0f,1.0f,0.0f);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left
// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();

return TRUE;
}

We are familiar with first 4 lines. In line " glBindTexture(GL_TEXTURE_2D, texture[0]) " , We bind the texture. we use index ZERO, coz we load the texture at index ZERO in "LoadGLTextures" method. Then we begin drawing the cube. Then we use a function "glTexCoord2f" as the texture is 2D, it got 2 arguments. 1st is x-axis ,(0.0f is the left side of the texture. 0.5f is the middle of the texture, and 1.0f is the right side of the texture. 2nd argument is y-axis (0.0f is the bottom of the texture. 0.5f is the middle of the texture, and 1.0f is the top of the texture.)

Now first we describe " Front Face ". we define 4 vertex along with there texture co-ordinates. As we know coordinate for bottom left will be (0,0) [ x-axis = (0.0f is the left side of the texture) , y-axis = (0.0f is the bottom of the texture) ] .

In the same way we describe vertices for each Face.

Now when we run the program, u will get this output.

Download the Source Code.

next: camera


metadata block
see also:
Correspondence about this page

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2023 Martin John Baker - All rights reserved - privacy policy.