HOME
OPENGL
CONTACT
 If you would like to be
 informed if there is
 new tutorial, please
 submit your e-mail to
 us. Thank you.
 
 
6. TRANSFORMATION IN OPENGL

1.

From the previous tutorial, if you run the program using GL_POLYGON in step number 8, you will get this result:

2.

Now, replace the program with this program. When you run it, it will be like this:

Now we will go through the program explanation.
TRANSLATION

3.

At line 18 in the program, glTranslatef is used to move the object along the X, Y and Z axis. The parameters involved are:
    void glTranslatef (float X, float Y,  float Z);

In this example, the function glTranslatef() is used to move the polygon by 1 unit along the X-axis in the right (positive) direction. 
    glTranslatef(1.0, 0.0, 0.0);
See this diagram for clearer view:

Simply said, the vertices at coordinate X, will be added with 1.0. For vertices at coordinate Y and Z, they will be added with 0.0.
   V0 (   0 + 1.0,    0 + 0.0,  0 + 0.0)
   
V1 (   1 + 1.0,    0 + 0.0,  0 + 0.0)
   
V2 (   1 + 1.0,    1 + 0.0,  0 + 0.0)
   
V3 (0.5 + 1.0, 1.5 + 0.0,  0 + 0.0)
   
V4 (   0 + 1.0,    1 + 0.0,  0 + 0.0)
Now, the latest vertices coordinates will be like this:
   V0 (   0,    0, 0)
   
V1 (   2,    0, 0)
   
V2 (   2,    1, 0)
  
V3 (1.5, 1.5, 0)
   
V4 (   1,    1, 0)
SCALING
4. Now, replace line 18 (glTranslatef) with this:
    glScalef(2.0, 1.0, 1.0);

5.

This time, the output will be like this:

6.

glScalef is used to resize the object. The parameters involved are:
    void glScalef (float X, float Y,  float Z);
In the example above, all the vertices at coordinate X, will be multiplied by 2.0. For vertices at coordinate Y and Z, they will be multiplied by 1.0.
   V0 (   0 x 2.0,    0 x 1.0, 0 x 1.0)
 
  V1 (   1 x 2.0,    0 x 1.0, 0 x 1.0)
  
V2 (   1 x 2.0,    1 x 1.0, 0 x 1.0)
 
  V3 (0.5 x 2.0, 1.5 x 1.0, 0 x 1.0)
  
V4 (   0 x 2.0,    1 x 1.0, 0 x 1.0) 
Now, the latest vertices coordinates will be like this:
   V0 ( 0, 0, 0)
   
V1 ( 2, 0, 0)
   
V2 ( 2, 1, 0)
   
V3 (1.0, 1.5, 0)
   
V4 ( 0, 1, 0)
ROTATION
7. Now, replace line 18 (glScalef) with this:
    glRotatef(-90.0, 0.0, 0.0, 1.0);

8.

This time, the output will be like this:

9.

glRotatef is used to rotate the object around the axis. The parameters involved are:
    void glRotatef (float Angle, float X, float Y,  float Z);
In the example, the object is rotated around Z-axis, with -90 degrees rotation.
10. Now, you can try the combination of the transformations in your program such as this, and see the result: 
    glPushMatrix();
        glTranslatef(1.0, 2.0, 0.0);

        glRotatef(180.0, 0.0, 0.0, 1.0);
        glScalef(1.0, 2.0, 1.0);
        glBegin(GL_POLYGON);           
            glVertex3f( 0.0, 0.0, 0.0);          
// V0 ( 0, 0, 0)
            glVertex3f( 1.0f, 0.0, 0.0);         
// V1 ( 1, 0, 0)
            glVertex3f( 1.0f, 1.0f, 0.0);        
// V2 ( 1, 1, 0)
            glVertex3f( 0.5f, 1.5f, 0.0);        
// V3 (0.5, 1.5, 0)
            glVertex3f( 0.0, 1.0f, 0.0);          
// V4 ( 0, 1, 0)
        glEnd();
    glPopMatrix();

PREVIOUS   INDEX   NEXT
Copyright © 2005 VREMPIRE LAIR - All rights reserved.