Tuesday, December 17, 2013

To draw a rotating square

/*
 * square.c
 * The square should stop rotating when middle button is pressed and

start
 * rotating again
 *  when left button is pressed
 *  Created on: Dec 16, 2013
 *      Author: kamath
 */
#include<GL/glut.h>
#include<math.h>
GLfloat theta,thetar;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POLYGON);
    thetar=theta/(3.14159/180.0);         //convert theta in degrees to

radians
    glVertex2f(cos(thetar),sin(thetar));
    glVertex2f(-sin(thetar),cos(thetar));
    glVertex2f(-cos(thetar),-sin(thetar));
    glVertex2f(sin(thetar),-cos(thetar));
    glEnd();
    glFlush();
    glutSwapBuffers();
}
void idle()
{
    theta+=2;
    if(theta>=360.0) theta-=360.0;
    glutPostRedisplay();
}

void mouse(int button,int state,int x,int y)      //  change idle

function based on
         //  mouse button pressed
{
    if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
    glutIdleFunc(idle);
    if(button==GLUT_MIDDLE_BUTTON&&state==GLUT_DOWN)
        glutIdleFunc(NULL);
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
    glutCreateWindow("Rotating Square");
    glutIdleFunc(idle);
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMainLoop();
    return 0;
}

Snapshot:


No comments:

Post a Comment