Tuesday, April 17, 2012

GLFW(OpenGL FrameWork) tutorial 3:Transformation:Translation,Scaling and Rotation.

If you are beginner to OpenGL FrameWork (GLFW) visit my previous tutorial lesson Here so that you can understand in details.Translation ,Scaling and Rotation of object is so easy because OpenGL provide these command to us we don’t have to write our own  Transformation matrix.

Translation command:

Translation is movement along any of the three axis in a 3D scene, for example, moving something the left is a translation on the X axis if you are looking straight on.
Command:
glTranslatef( xValue, yValue, zValue );
e.g:
glTranslate(5.0, 0.0, 0.0);
This translate the scene five unit along positive x-axis. One things you must remember ,all the scene drawn below this command will be translated.
So,if you want to translate only one object then you have to restore the transformation. Restore can be done by putting drawing scene and translation command between glPushMatrix() and glPopMatrix() .

Rotation command:

Turning  the object with respect to any axis is called rotation, and is measured by the number of degrees (or radians) .For example if you have turned from your starting position along any three axis in a 3D scene. With the three main axis being the X, Y and Z axis. You can also rotate the object with respect to any arbitary axis.
Command:
glRotatef(angleRotatation, xValue, yValue, zValue);
angleRotation can be +ve for anticlockwise rotation and -ve for clockwise rotation.
x,y,z vaue are the axis of rotation.

Scaling Command:

Scaling means increasing/decreasing size of object by a given factor. If scaling factor is more than one then size increases and for factor less than more decreases the size.
Command:
glScalef(xValue,yValue,zValue);
Now,lets take a look to the code below:
 
//include header file for glfw library so that we can use OpenGL
#include <GL/glfw.h>
#include <stdlib.h>  //needed for exit function
#include <iostream>
using namespace std;
 
//Initializes 3D rendering
void initializeRendering()
{
    glfwInit();
    //Makes 3D drawing work when something is in front of something else
    glEnable(GL_DEPTH_TEST);
}
 
//Called when a key is pressed
void GLFWCALL handleKeypress(int key,int press) //The key that was pressed
{
    switch (key) {
        case GLFW_KEY_ESC: //Escape key
            exit(0); //Exit the program
    }
}
 
//Called when the window is resized
void GLFWCALL handleResize(int width,int height)
{
    //Tell OpenGL how to convert from coordinates to pixel values
    glViewport( 0, 0, width, height );
 
    glMatrixMode( GL_PROJECTION ); //Switch to setting the camera perspective
    //Set the camera perspective
    glLoadIdentity(); //reset the camera
    gluPerspective( 45.0f,                      //camera angle
                (GLfloat)width/(GLfloat)height, //The width to height ratio
                 1.0f,                          //The near z clipping coordinate
                100.0f );                       //The far z clipping coordinate
}
void display()
{
    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); //clear background screen to black
 
    //Clear information from last draw
    glClear( GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
 
    glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
    glLoadIdentity(); //Reset the drawing perspective
 
    glTranslatef(0.0f,0.0f,-35.0f); //Translate whole scene to -ve z-axis by -35 unit
 
    glPushMatrix();
    glTranslatef(-2.0f,0.0f,0.0f); //Translate only Rectangle
    glBegin(GL_POLYGON); //Begin quadrilateral coordinates
    //Trapezoid
    glVertex3f(-2.0f,0.0f,5.0f);
    glVertex3f(-5.0f,0.0f,5.0f);
    glVertex3f(-5.0f,5.0f,5.0f);
    glVertex3f(-2.0f,5.0f,5.0f);
 
    glEnd(); //End quadrilateral coordinates
    glPopMatrix();
 
    glPushMatrix();
    glTranslatef(2.0f,0.0f,0.0f); //Translate it
    glScalef(1.5f,1.5f,1.5f); //scale the triangle all side by factor 1.5 unit
    glBegin(GL_TRIANGLES); //Begin triangle coordinates
    glVertex3f(3.0f,0.0f,0.0f);
    glVertex3f(0.0f,-3.0f,0.0f);
    glVertex3f(6.0f,-3.0f,0.0f);
    glEnd(); //End triangle coordinates
    glPopMatrix();
 
    glPushMatrix();
    glTranslatef(0.0f,3.0f,0.0f); //translate it
    glRotatef(30,0.0f,0.0f,1.0f); //rotate the pentagon by 30 degree anti-clockwise
    glBegin(GL_POLYGON);
    glVertex3f(1.0f,0.0f,0.0f);
    glVertex3f(5.0f,0.0f,0.0f);
    glVertex3f(5.0f,4.0f,0.0f);
    glVertex3f(3.0f,6.0f,0.0f);
    glVertex3f(1.0f,4.0f,0.0f);
    glEnd();
    glPopMatrix();
    glfwSwapBuffers();
 
}
int main()
{
   // int     width, height;
    //int     frame = 0;
    bool    running = true;
 
    initializeRendering();
 
 
 
    if( !glfwOpenWindow( 800, // width of window
                         800, //height of window
                          0,  //redbits
                          0,  //greenbits
                          0,  //bluebits
                          0,  //alphabits
                          0,  //depthbits
                          0, //stencilbits
                          GLFW_WINDOW ) //mode
        ) //return false if window is not created
    {
        glfwTerminate(); //terminating glfw window
        return 0;
    }
 
    glfwSetWindowTitle("codeincodeblock.blogspot.com - Transformation");
    glfwSetWindowSizeCallback(handleResize); //callback function of GLFW to handle window resize
    glfwSetKeyCallback(handleKeypress); //callback function to handle keypress
    while(running) // infinite loop to draw object again and again
    {              // because once object is draw then window is terminated
        display();
        running = glfwGetWindowParam( GLFW_OPENED ); //when glfw window is opened then it return true
                                                     //if closed then return false
    }
    return 0;
}


You can also download the project fiel form here Download

No comments:

Post a Comment