## 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;`
`}`
{"ft_lang_label":"__label__en","ft_lang_prob":0.74262065,"math_prob":0.96629554,"size":5806,"snap":"2023-40-2023-50","text_gpt3_token_len":1629,"char_repetition_ratio":0.13288522,"word_repetition_ratio":0.25032595,"special_character_ratio":0.26438168,"punctuation_ratio":0.22468086,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9886573,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-09T11:49:31Z\",\"WARC-Record-ID\":\"<urn:uuid:4ffb519b-4fae-4ff9-8ffe-0158c8ab06e8>\",\"Content-Length\":\"120703\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:95e15f2e-6f7b-40ca-a9cb-3e94dabf5a8b>\",\"WARC-Concurrent-To\":\"<urn:uuid:c3e0c234-85c1-4948-8d08-9be55517ddbe>\",\"WARC-IP-Address\":\"142.251.167.121\",\"WARC-Target-URI\":\"https://www.codeincodeblock.com/2012/04/glfwopengl-framework-tutorial.html\",\"WARC-Payload-Digest\":\"sha1:DAEYJWBZMNBRBB65OJ6YDR75JU3UEGAZ\",\"WARC-Block-Digest\":\"sha1:RK3DKVJLMFUTSVRESOQXJWRE2HPELKVO\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100909.82_warc_CC-MAIN-20231209103523-20231209133523-00492.warc.gz\"}"}