Tuesday, 16 August 2016

Program 3: Write a program to implement 2D Geometric Transformations.

PROGRAM 3: 2D transformations
·         Header File
·         Render func
·         Handle resize
·         Display
·         Timer
·         Main func

1.      Header File.(gl/glut.h , math.h, windows.h)
2.      Initialise rendering function for the 2d object
void initRendering()
{         
glEnable(GL_DEPTH_TEST);
}
3.      Create Handle Resize
void handleResize(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(double)w/(double)h,1.0,200.0);
}
4.      Display Function
void drawScene()
{
    glClearColor(0.0f, 0.0f, 1.0f, 0.0f);//dark blue
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glRotatef(_angle,0.0f,0.0f,1.0f); //rotate object by 30 degree with respect to y-axis
    glTranslatef(0.0f, 0.0f, -10.0f);
    //************************
    glPushMatrix();
    glScalef(2,2,2);
    //OBJ
      glPopMatrix();
    glutSwapBuffers();
}
5.      Timer
void update(int value)
{
       _angle+=2.0f;
      if(_angle>360.f)
      {
                  _angle-=360;
      }
    glutPostRedisplay();
    glutTimerFunc(25,update,0);
}
6.      Main Function
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("rotate");
    initRendering();
    glutDisplayFunc(drawScene);
    glutReshapeFunc(handleResize);
    glutTimerFunc(25,update,0);
    glutMainLoop();
    return 0;
}
SAMPLE PROGRAM:PROGRAM 3
//header file and include math.h as well
#include<math.h>
float _angle=45.0;
void initRendering()
{
glEnable(GL_DEPTH_TEST);
}
void handleResize(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(double)w/(double)h,1.0,200.0);
}
void drawScene()
{
    glClearColor(0.0f, 0.0f, 1.0f, 0.0f);//dark blue
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glRotatef(_angle,0.0f,0.0f,1.0f); //rotate object by 30 degree with respect to y-axis
    glTranslatef(0.0f, 0.0f, -10.0f);
    //************************
    glPushMatrix();
    glScalef(2,2,2);
    //OBJ
    glPopMatrix();
    glutSwapBuffers();
}
void update(int value)
{
    _angle+=2.0f;
    if(_angle>360.f)
    {
    _angle-=360;
    }
    glutPostRedisplay();
    glutTimerFunc(25,update,0);
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("rotate");
    initRendering();
    glutDisplayFunc(drawScene);
    glutReshapeFunc(handleResize);
    glutTimerFunc(25,update,0);
    glutMainLoop();
    return 0;

}

No comments:

Post a Comment