Remembering the past to build the future

" Remembering the past to build the future "

Thursday, 3 April 2014

OpenGL Texture Mapping

In this post I decided to get some Quake texture and making a simple but interesting 3D scene. As the other OpenGL post, we have a project with some C++ files and classes like Geometry,Scene,Main but in this demo I have used an external class for texture loading, TextureLoader inside the project. The first thing you have to control before to worry about compilation error is the following setting in your Visual C++ project:




Check Character Set = Not Set. Let's see the video demo before to start:




You can download the code here:

Texture Demo

For texture loader you can find all documentation here:

http://members.iinet.net.au/~cleathley/openGL/TextureLoader.htm

If you open the Scene class you can find the following fragment code on the top:

TextureLoader* Scene::pTextureLoader=new TextureLoader();
glTexture    Scene::floor;
glTexture    Scene::box;
glTexture    Scene::box2; 
TextureLoader is the class that loads the textures. We have three different textures, one for floor and two for boxes. The initGL() method inside the Scene class has now the code to initialize the textures:
 pTextureLoader->LoadTextureFromDisk("\\texture\\floor.jpg", &Scene::floor);
 pTextureLoader->LoadTextureFromDisk("\\texture\\box.jpg", &Scene::box); 
 pTextureLoader->LoadTextureFromDisk("\\texture\\box2.jpg", &Scene::box2); 
The texture files must reside in your project directory. In my case I made a directory called texture and I put all files here. With Geometry class in the draw() method of the Scene class, we find the code to draw all boxes using textures:
Geometry geo;

   glPushMatrix();
 glTranslatef(-3,0.75,5);
 geo.box(1.5f,1.5f,1.5f,Scene::box.TextureID);
   glPopMatrix();

   glPushMatrix();
 glTranslatef(-3,0.75,2);
 geo.box(1.5f,1.5f,1.5f,Scene::box.TextureID);
   glPopMatrix();
 
   glPushMatrix();
 glTranslatef(-3,0.75,-1);
 geo.box(1.5f,1.5f,1.5f,Scene::box.TextureID);
    glPopMatrix();

    glPushMatrix();
 glTranslatef(-3,0.75,-4);
 geo.box(1.5f,1.5f,1.5f,Scene::box.TextureID);
    glPopMatrix();

 
    glPushMatrix();
 glTranslatef(3,0.75,5);
 geo.box(1.5f,1.5f,1.5f,Scene::box2.TextureID);
    glPopMatrix();

    glPushMatrix();
 glTranslatef(3,0.75,2);
 geo.box(1.5f,1.5f,1.5f,Scene::box2.TextureID);
    glPopMatrix();
 
    glPushMatrix();
 glTranslatef(3,0.75,-1);
 geo.box(1.5f,1.5f,1.5f,Scene::box2.TextureID);
    glPopMatrix();

    glPushMatrix();
 glTranslatef(3,0.75,-4);
 geo.box(1.5f,1.5f,1.5f,Scene::box2.TextureID);
    glPopMatrix();

If you want to see how textures have been applied to boxes see the box method of the class Geometry:
void Geometry::box(GLdouble width, GLdouble height, GLdouble lenght,int textureId){
  
    glEnable(GL_TEXTURE_2D); 
    glShadeModel(GL_FLAT);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE, GL_MODULATE);

    glBindTexture(GL_TEXTURE_2D, textureId);

    // Front
   glBegin(GL_QUADS);
      glNormal3d(0, 0, lenght/2);
      glTexCoord2f(0, 0);
      glVertex3d(-width/2, height/2, lenght/2);
      glTexCoord2f(1, 0);
      glVertex3d(width/2, height/2, lenght/2);
      glTexCoord2f(1, 1);
      glVertex3d(width/2, -height/2, lenght/2); 
      glTexCoord2f(0, 1);
      glVertex3d(-width/2, -height/2, lenght/2);
   glEnd();

    // Right
   glBegin(GL_QUADS);
      glNormal3d(-width/2, 0, 0);
      glTexCoord2f(0, 0);
      glVertex3d(-width/2, +height/2, -lenght/2);
      glTexCoord2f(1, 0);
      glVertex3d(-width/2, +height/2, lenght/2);
      glTexCoord2f(1, 1);
      glVertex3d(-width/2, -height/2, lenght/2);
      glTexCoord2f(0, 1);
      glVertex3d(-width/2, -height/2, -lenght/2);
    glEnd();

    // Back
    glBegin(GL_QUADS);
       glNormal3d(0, 0, -lenght/2);
       glTexCoord2f(0, 0); 
       glVertex3d(width/2, height/2, -lenght/2);
       glTexCoord2f(1, 0);
       glVertex3d(-width/2, height/2, -lenght/2);
       glTexCoord2f(1, 1);
       glVertex3d(-width/2, -height/2, -lenght/2);
       glTexCoord2f(0, 1);
       glVertex3d(width/2, -height/2, -lenght/2);
    glEnd();

     // Left
    glBegin(GL_QUADS);
       glNormal3d(width/2, 0, 0);
       glTexCoord2f(0, 0);
       glVertex3d(width/2, height/2, lenght/2);
       glTexCoord2f(1, 0);
       glVertex3d(width/2, height/2, -lenght/2);
       glTexCoord2f(1, 1);
       glVertex3d(width/2, -height/2, -lenght/2);
       glTexCoord2f(0, 1);
       glVertex3d(width/2, -height/2, lenght/2);
     glEnd();

     // Top
     glBegin(GL_QUADS);
       glNormal3d(0, height/2, 0);
       glTexCoord2f(0, 0);
       glVertex3d(-width/2, height/2, -lenght/2);
       glTexCoord2f(1, 0);
       glVertex3d(width/2, height/2, -lenght/2);
       glTexCoord2f(1, 1);
       glVertex3d(width/2, height/2, lenght/2);
       glTexCoord2f(0, 1);
       glVertex3d(-width/2, height/2, lenght/2);
     glEnd();

     // Bottom
     glBegin(GL_QUADS);
        glNormal3d(0, -height/2, 0);
        glTexCoord2f(0, 0);
        glVertex3d(-width/2, -height/2, lenght/2);
        glTexCoord2f(1, 0);
        glVertex3d(width/2, -height/2, lenght/2);
        glTexCoord2f(1, 1);
        glVertex3d(width/2, -height/2, -lenght/2);
        glTexCoord2f(0, 1);
        glVertex3d(-width/2, -height/2, -lenght/2);
     glEnd();

     glDisable(GL_TEXTURE_2D);
}

My Android Games, much are free