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:



My Android Games, much are free