Remembering the past to build the future

" Remembering the past to build the future "

Friday 4 April 2014

Texture mapping on Torus

Hi, let's see a more complex texture mapping. In this demo I have written in the Geometry class a torus rendering method :

void torus(float out_radius, float int_radius, int textureId);

The method takes as input the tube radius ,the big radius from the center of the torus and a texture id. For a good quality rendering is important to set the normal on each vertex of the torus mesh, so for this reason we have the following private method used by torus:

Vector3 Geometry::getPointNormal(float out_angle,float int_angle, 
float out_radius,float int_radius){
 
 Vector3 tangentOut( -sin(out_angle), 
               cos(out_angle), 
      0);

 Vector3 tangentInt( cos(out_angle)*(-sin(int_angle)) ,
              sin(out_angle)*(-sin(int_angle)) ,
         cos(int_angle) );
 
 Vector3 normal = tangentOut^tangentInt;

 return normal.Normalize();

 }

getPointNormal() returns the normal vector (angles in radiants) for a vertex identified by polar coordinates. Another method used by the torus is:

Vector3 Geometry::getPoint(float out_angle,float int_angle, float out_radius,
float int_radius){

 return Vector3( ( out_radius+int_radius*cos(int_angle) )*cos(out_angle),
                 ( out_radius+int_radius*cos(int_angle) )*sin(out_angle),
                                         int_radius*sin(int_angle));
  
}

That returns cartesian coordinates from polar coordinates. For apply the texture we have the method:

void Geometry::glTorusTexCoord(float out_angle,float int_angle,int tx){

 glTexCoord2f( out_angle/PI , (int_angle)/PI );
 
}
The texture file must reside in your project directory. In my case I made a directory called texture and I put the file here.

Download the sources here


Here it is the video demo:



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