OpenGL Programs for Various Geometric Transformations

download OpenGL Programs for Various Geometric Transformations

of 11

Transcript of OpenGL Programs for Various Geometric Transformations

  • 8/10/2019 OpenGL Programs for Various Geometric Transformations

    1/11

    Basic Procedure to run Programs on openGL

    Step-I: Install Microsoft Visual C++ Express Edition 2008.

    Step-II: Copy the following GLUT Files form GLFiles folder and paste them to the

    following folders on your PC.

    1. glut.h and glut. export definition file C:program files\Microsoft

    SDKs\Windows\V6.0\include\gl

    2. glut32.lib C:program files\Microsoft SDKs\Windows\V6.0\Lib

    3. glut32.dll C:Windows\System32

    Step-III: To write the OpenGl Program with GLUT, Use following procedure.

    1. Start programs Microsoft Visual C++ Express Edition 2008 Microsoft Visual

    C++ Express Edition 2008 Click.

    2.

    Use following procedure to open page for writing an OpenGL program.

    File - New Project Win 32 Win 32 Console Applications Give File Name Give

    LocationOKFinish.

    3. Delete everything on the page.

    4. Then, Copy the OPENGL Template and paste it there to type your OpenGL

    program at respective locations.

    5. Save the program

    6.

    Build your solution by pressing F7 Key for successful compilation. If error occurs,remove it and again debug by pressing F7 key.

    7. After successful compilation, debug your program as

    Debug Menustart debugging

    8. Check your solution. OK

  • 8/10/2019 OpenGL Programs for Various Geometric Transformations

    2/11

    // OEPNGL TEMPLATE//

    #include"stdafx.h"

    #include

    voiddisplay()

    {glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();

    glLoadIdentity();

    // Use OPENGL Commands for Transformations

    ----------------------

    --------------------------------------------

    // USE OPENGL Commands to draw Transformed Geometry with Green colorglBegin(GL_COMMAND);

    ---------------------------------

    ----------------------------------

    ---------------------------------

    ---------------------------------

    glEnd();

    glPopMatrix();

    // To draw X and Y axesglBegin(GL_LINES);

    glColor3f(1.0f, 0.0f, 0.0f);glVertex2f(-1.0f, 0.0f);

    glVertex2f(1.0f, 0.0f);

    glVertex2f(0.0f, -1.0f);

    glVertex2f(0.0f, 1.0f);glEnd();

    // USE OPENGL Commands to draw Original Given Geometry with red colorglBegin(GL_COMMAND);

    ---------------------------------

    ----------------------------------

    ---------------------------------

    ---------------------------------

    glEnd();

    glFlush();

    }

    // To initilize GLUT and To create Window for display

    intmain(intargc, char** argv){

    glutInit(&argc, argv);

    glutCreateWindow("Give Name to Output Display File");glutDisplayFunc(display);

    glutMainLoop();

    return0;}

    int_tmain(intargc, _TCHAR* argv[])

    {return0;

    }

  • 8/10/2019 OpenGL Programs for Various Geometric Transformations

    3/11

    Q.1 -Draw red colored triangle with vertices (0.2, -0.3), (0.8, -0.3) and (0.5, 0.2). Also, draw

    X and Y axes. Rotate the triangle about its origin by 90 degrees (Z-axis) with green color.

    // Traingle1.cpp : Defines the entry point for the console application.

    #include "stdafx.h"

    #include

    void display()

    {

    glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();

    glLoadIdentity();

    // To Rotate traingle about origin by 90 degrees (Z-axis)

    glRotatef(90,0.0,0.0,1.0);

    // To draw Triangle using 03 vertices with green color

    glBegin(GL_TRIANGLES);

    glColor3f(0.0f, 1.0f, 0.0f);

    glVertex2f(0.2f, -0.3f);

    glVertex2f(0.8f, -0.3f);

    glVertex2f(0.5f, 0.2f);

    glEnd();

    glPopMatrix();

    // To draw X and Y axes

    glBegin(GL_LINES);

    glColor3f(1.0f, 0.0f, 0.0f);

    glVertex2f(-1.02f, 0.0f);

    glVertex2f(1.0f, 0.0f);

    glVertex2f(0.0f, -1.0f);

    glVertex2f(0.0f, 1.0f);

    glEnd();

    // To draw original Triangle using 03 vertices with red color

    glBegin(GL_TRIANGLES);

    glColor3f(1.0f, 0.0f, 0.0f);

    glVertex2f(0.2f, -0.3f);

    glVertex2f(0.8f, -0.3f);

    glVertex2f(0.5f, 0.2f);

    glEnd();

    glFlush();

    }

    // To initilize GLUT and To create Window for display

    int main(int argc, char** argv)

    {

    glutInit(&argc, argv);glutCreateWindow("Original and Rotated Triangle");

    glutDisplayFunc(display);

    glutMainLoop();

    return 0;

    }

    int _tmain(int argc, _TCHAR* argv[])

    {

    return 0;

    }

  • 8/10/2019 OpenGL Programs for Various Geometric Transformations

    4/11

    Q. 2- Draw a red colored triangle with vertices (0.2, -0.3), (0.8, -0.3) and (0.5, 0.2). Also,

    draw X and Y axes. Rotate the triangle about its vertex (0.2, -0.3) with green color.

    // trinagle.cpp : Defines the entry point for the console application.

    #include"stdafx.h"

    #include

    voiddisplay()

    {glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();

    glLoadIdentity();

    // To Rotate traingle about point by 180 degrees

    glTranslatef(0.2f,-0.3f,0.0f);glRotatef(180,0.0,0.0,1.0);

    glTranslatef(-0.2f,0.3f,0.0f);

    // To draw Triangle using 03 vertices with green colorglBegin(GL_TRIANGLES);

    glColor3f(0.0f, 1.0f, 0.0f);

    glVertex2f(0.2f, -0.3f);glVertex2f(0.8f, -0.3f);

    glVertex2f(0.5f, 0.2f);

    glEnd();

    glPopMatrix();

    // To draw X and Y axesglBegin(GL_LINES);

    glColor3f(1.0f, 0.0f, 0.0f);

    glVertex2f(-1.02f, 0.0f);glVertex2f(1.0f, 0.0f);

    glVertex2f(0.0f, -1.0f);

    glVertex2f(0.0f, 1.0f);

    glEnd();

    // To draw original Triangle using 03 vertices with red color

    glBegin(GL_TRIANGLES);

    glColor3f(1.0f, 0.0f, 0.0f);glVertex2f(0.2f, -0.3f);

    glVertex2f(0.8f, -0.3f);

    glVertex2f(0.5f, 0.2f);glEnd();

    glFlush();

    }

    // To initilize GLUT and To create Window for display

    intmain(intargc, char** argv)

    {glutInit(&argc, argv);

    glutCreateWindow("Original and Rotated Triangle");

    glutDisplayFunc(display);glutMainLoop();

    return0;

    }

    int_tmain(intargc, _TCHAR* argv[])

    {return0;

    }

  • 8/10/2019 OpenGL Programs for Various Geometric Transformations

    5/11

    Q.3 - Draw a square in the centre of the screen having unit screen with red color. Also,

    draw X and Y axes. Scale the square with scaling parameters Sx = 1.2 and Sy = 1.2 about

    the origin with green color.

    // square.cpp : Defines the entry point for the console application.

    #include"stdafx.h"

    #include

    voiddisplay()

    {

    glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();

    glLoadIdentity();

    // To perform Sacling about origin

    glScalef(1.2,1.2,1.0);

    // To draw square using 04 vertices with green color

    glBegin(GL_QUADS);

    glColor3f(0.0f, 1.0f, 0.0f);

    glVertex2f(-0.5f, -0.5f);glVertex2f(0.5f, -0.5f);

    glVertex2f(0.5f, 0.5f);

    glVertex2f(-0.5f, 0.5f);glEnd();

    glPopMatrix();

    // To draw X and Y Axes

    glBegin(GL_LINES);

    glColor3f(1.0f, 0.0f, 0.0f);glVertex2f(-1.02f, 0.0f);

    glVertex2f(1.0f, 0.0f);

    glVertex2f(0.0f, -1.0f);

    glVertex2f(0.0f, 1.0f);glEnd();

    // To Draw Original Square with red colorglBegin(GL_QUADS);

    glColor3f(1.0f, 0.0f, 0.0f);

    glVertex2f(-0.5f, -0.5f);glVertex2f(0.5f, -0.5f);

    glVertex2f(0.5f, 0.5f);

    glVertex2f(-0.5f, 0.5f);

    glEnd();glFlush();

    }

    // To initilize GLUT and To create Window for display

    intmain(intargc, char** argv)

    {glutInit(&argc, argv);glutCreateWindow(" Original and Scaled Square");

    glutDisplayFunc(display);

    glutMainLoop();

    return0;

    }

    int_tmain(intargc, _TCHAR* argv[])

    {return0;

    }

  • 8/10/2019 OpenGL Programs for Various Geometric Transformations

    6/11

    Q.4 - Draw a square in the centre of the screen having unit screen with red color. Also,

    draw X and Y axes. Scale the square with scaling parameters Sx = 1.2 and Sy = 1.2 about

    the point (-1,-1) with green color.

    // Square1.cpp : Defines the entry point for the console application.

    #include"stdafx.h"#include

    voiddisplay(){

    glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();

    glLoadIdentity();

    // To perform Sacling about pointglTranslatef(-0.5f,-0.5f,0.0f);

    glScalef(1.2,1.2,1.0);

    glTranslatef(0.5f,0.5f,0.0f);

    // To draw square using 04 vertices with green color

    glBegin(GL_QUADS);

    glColor3f(0.0f, 1.0f, 0.0f);glVertex2f(-0.5f, -0.5f);

    glVertex2f(0.5f, -0.5f);

    glVertex2f(0.5f, 0.5f);glVertex2f(-0.5f, 0.5f);

    glEnd();

    glPopMatrix();

    // To draw X and Y Axes

    glBegin(GL_LINES);glColor3f(1.0f, 0.0f, 0.0f);

    glVertex2f(-1.02f, 0.0f);

    glVertex2f(1.0f, 0.0f);

    glVertex2f(0.0f, -1.0f);glVertex2f(0.0f, 1.0f);

    glEnd();

    // To Draw Original Square with red color

    glBegin(GL_QUADS);

    glColor3f(1.0f, 0.0f, 0.0f);glVertex2f(-0.5f, -0.5f);

    glVertex2f(0.5f, -0.5f);

    glVertex2f(0.5f, 0.5f);

    glVertex2f(-0.5f, 0.5f);glEnd();

    glFlush();

    }

    // To initilize GLUT and To create Window for display

    intmain(intargc, char** argv){

    glutInit(&argc, argv);

    glutCreateWindow(" Original and Scaled Square");

    glutDisplayFunc(display);

    glutMainLoop();

    return0;

    }

    int_tmain(intargc, _TCHAR* argv[])

    {return0;

    }

  • 8/10/2019 OpenGL Programs for Various Geometric Transformations

    7/11

    Q.5 - Draw a rectangle having corners (0.1, 0.2) and (0.3, 0.5) with red color. Draw axes X

    and Y. Scale this rectangle by two times in X and Y direction about origin with green color.

    // rectangle.cpp : Defines the entry point for the console application.

    #include"stdafx.h"#include

    voiddisplay(){

    glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();glLoadIdentity();

    // To perform Scaling about about originglScalef(2.0, 2.0, 1.0);

    // To draw Rectangle using two corners with green color

    glColor3f(0.0f, 1.0f, 0.0f);

    GLfloat corner1[ ] = {0.1f, 0.2f};

    GLfloat corner2[ ] = {0.3f, 0.5f};glRectfv(corner1, corner2);

    glPopMatrix();

    // To draw X and Y Axes

    glBegin(GL_LINES);

    glColor3f(0.0f, 0.0f, 1.0f);

    glVertex2f(-1.0f, 0.0f);glVertex2f(1.0f, 0.0f);

    glVertex2f(0.0f, -1.0f);glVertex2f(0.0f, 1.0f);

    glEnd();

    // To draw original Rectangle using two corners with red colorglColor3f(1.0f, 0.0f, 0.0f);

    //GLfloat corner1[ ]={0.1f,0.2f};

    //GLfloat corner2[ ]={0.3f,0.5f};glRectfv(corner1, corner2);

    glFlush();}

    // To initilize GLUT and To create Window for display

    intmain(intargc, char** argv)

    {glutInit(&argc, argv);

    glutCreateWindow("Original and Scaled Rectangle");

    glutDisplayFunc(display);glutMainLoop();

    return0;

    }

    int_tmain(intargc, _TCHAR* argv[]){

    return0;

    }

  • 8/10/2019 OpenGL Programs for Various Geometric Transformations

    8/11

    Q.6 - Draw a rectangle having corners (0.1, 0.2) and (0.3, 0.5) with red color. Draw axes X

    and Y. Scale this rectangle by two times in X and Y direction about the vertex (0.1, 0.2)

    with green color.

    // rectangle.cpp : Defines the entry point for the console application.

    #include"stdafx.h"#include

    voiddisplay()

    {glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();

    glLoadIdentity();

    // To perform Scaling about about PointglTranslatef(0.1f, 0.2f, 0.0f);

    glScalef(2.0, 2.0, 1.0);glTranslatef(-0.1f, -0.2f, 0.0f);

    // To draw Rectangle using two corners with green colorglColor3f(0.0f, 1.0f, 0.0f);

    GLfloat corner1[ ] = {0.1f, 0.2f};

    GLfloat corner2[ ] = {0.3f, 0.5f};

    glRectfv(corner1, corner2);

    glPopMatrix();

    // To draw X and Y Axes

    glBegin(GL_LINES);

    glColor3f(0.0f, 0.0f, 1.0f);glVertex2f(-1.0f, 0.0f);

    glVertex2f(1.0f, 0.0f);

    glVertex2f(0.0f, -1.0f);

    glVertex2f(0.0f, 1.0f);glEnd();

    // To draw original Rectangle using two corners with red colorglColor3f(1.0f, 0.0f, 0.0f);

    GLfloat corner1[ ]={0.1f,0.2f};

    GLfloat corner2[ ]={0.3f,0.5f};

    glRectfv(corner1, corner2);glFlush();

    }

    // To initilize GLUT and To create Window for display

    intmain(intargc, char** argv)

    {

    glutInit(&argc, argv);

    glutCreateWindow("Original and Scaled Rectangle");glutDisplayFunc(display);

    glutMainLoop();

    return0;

    }

    int_tmain(intargc, _TCHAR* argv[])

    {return0;

    }

  • 8/10/2019 OpenGL Programs for Various Geometric Transformations

    9/11

    Q.7 - Draw a six sided polygon having vertices (0.2, 0.3), (0.4, 0.3), (0.5, 0.5), (0.4, 0.7) (0.2,

    0.7) and (0.1, 0.5) with red color. Draw axes X and Y. Rotate it by 90 degrees about Z- axis

    or origin with green color.

    // hexagon.cpp : Defines the entry point for the console application.

    #include"stdafx.h"

    #include

    voiddisplay(){

    glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();

    glLoadIdentity();

    // To rotate Hexagon about origin by 90 degrees(Z-Axis)glRotatef(90,0.0,0.0,1.0);

    // To Draw Rotated Hexagon with green color

    glBegin(GL_POLYGON);glColor3f(0.0f, 1.0f, 0.0f);

    glVertex2f(0.2f, 0.3f);

    glVertex2f(0.4f, 0.3f);glVertex2f(0.5f, 0.5f);

    glVertex2f(0.4f, 0.7f);

    glVertex2f(0.2f, 0.7f);

    glVertex2f(0.1f, 0.5f);glEnd();

    glPopMatrix();

    // To draw X and Y axes

    glBegin(GL_LINES);

    glColor3f(1.0f, 0.0f, 0.0f);glVertex2f(-1.02f, 0.0f);

    glVertex2f(1.0f, 0.0f);

    glVertex2f(0.0f, -1.0f);

    glVertex2f(0.0f, 1.0f);glEnd();

    // To Draw Original Hexagon with red colorglBegin(GL_POLYGON);

    glColor3f(1.0f, 0.0f, 0.0f);

    glVertex2f(0.2f, 0.3f);

    glVertex2f(0.4f, 0.3f);glVertex2f(0.5f, 0.5f);

    glVertex2f(0.4f, 0.7f);

    glVertex2f(0.2f, 0.7f);glVertex2f(0.1f, 0.5f);

    glEnd();

    glFlush();

    }

    // To initilize GLUT and To create Window for displayintmain(intargc, char** argv)

    {glutInit(&argc, argv);

    glutCreateWindow("Original and Rotated Hexagon");

    glutDisplayFunc(display);

    glutMainLoop();

    return0;

    }int_tmain(intargc, _TCHAR* argv[])

    {return0;

    }

  • 8/10/2019 OpenGL Programs for Various Geometric Transformations

    10/11

    Q.8 - Draw a six sided polygon having vertices (0.2, 0.3), (0.4, 0.3), (0.5, 0.5), (0.4, 0.7) (0.2,

    0.7) and (0.1, 0.5) with red color. Draw axes X and Y. Rotate it by 180 degrees about point

    (0.2, 0.3) with green color.

    // Hexagon1.cpp : Defines the entry point for the console application.#include"stdafx.h"

    #include

    voiddisplay(){

    glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();glLoadIdentity();

    // To rotate Hexagon about Point by 180 degrees

    glTranslatef(0.2f,0.3f,0.0f);glRotatef(180,0.0,0.0,1.0);

    glTranslatef(-0.2f,-0.3f,0.0f);

    // To Draw Rotated Hexagon with green colorglBegin(GL_POLYGON);

    glColor3f(0.0f, 1.0f, 0.0f);

    glVertex2f(0.2f, 0.3f);glVertex2f(0.4f, 0.3f);

    glVertex2f(0.5f, 0.5f);

    glVertex2f(0.4f, 0.7f);glVertex2f(0.2f, 0.7f);

    glVertex2f(0.1f, 0.5f);

    glEnd();

    glPopMatrix();

    // To draw X and Y axes

    glBegin(GL_LINES);glColor3f(1.0f, 0.0f, 0.0f);

    glVertex2f(-1.02f, 0.0f);

    glVertex2f(1.0f, 0.0f);

    glVertex2f(0.0f, -1.0f);glVertex2f(0.0f, 1.0f);

    glEnd();

    // To Draw Original Hexagon with red color

    glBegin(GL_POLYGON);

    glColor3f(1.0f, 0.0f, 0.0f);glVertex2f(0.2f, 0.3f);

    glVertex2f(0.4f, 0.3f);

    glVertex2f(0.5f, 0.5f);

    glVertex2f(0.4f, 0.7f);glVertex2f(0.2f, 0.7f);

    glVertex2f(0.1f, 0.5f);

    glEnd();glFlush();

    }

    // To initilize GLUT and To create Window for displayintmain(intargc, char** argv)

    {

    glutInit(&argc, argv);glutCreateWindow("Original and Rotated Hexagon");

    glutDisplayFunc(display);

    glutMainLoop();return0;

    }

    int_tmain(intargc, _TCHAR* argv[]){

    return0;

    }

  • 8/10/2019 OpenGL Programs for Various Geometric Transformations

    11/11

    // OEPNGL TEMPLATE//

    #include"stdafx.h"

    #include

    voiddisplay()

    {glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();

    glLoadIdentity();

    // Use OPENGL Commands for Transformations

    --------------------------------------------

    ----------------------

    // USE OPENGL Commands to draw Transformed Geometry with Green color

    glBegin(GL_COMMAND);

    ---------------------------------

    ----------------------------------

    ---------------------------------

    ---------------------------------

    glEnd();

    glPopMatrix();

    // To draw X and Y axes

    glBegin(GL_COMMAND);

    glColor3f(1.0f, 0.0f, 0.0f);glVertex2f(-1.02f, 0.0f);

    glVertex2f(1.0f, 0.0f);

    glVertex2f(0.0f, -1.0f);glVertex2f(0.0f, 1.0f);

    glEnd();

    // USE OPENGL Commands to draw Original Given Geometry with red colorglBegin(GL_COMMAND);

    ---------------------------------

    -------------------------------------------------------------------

    ---------------------------------

    glEnd();

    glFlush();

    }

    // To initilize GLUT and To create Window for display

    intmain(intargc, char** argv)

    {glutInit(&argc, argv);

    glutCreateWindow("Give Name to Output Display File");glutDisplayFunc(display);

    glutMainLoop();

    return0;}

    int_tmain(intargc, _TCHAR* argv[])

    {return0;

    }