/* ** File: timer01.c ** Description: Rotating Square ** Demonstrates use of glutTimerFunc ** Rev: 1.0 ** Created: 11 Aug 06 ** Last Update: 11 Aug 06 ** Author: Fran Soddell ** Email: F.Soddell@latrobe.edu.au */ #include #include #include /* ** manage GLUT window */ int width = 300; int height = 300; int xPosition = 100; int yPosition = 100; /* ** manage world coordinates */ GLdouble xLeft = -1.0; GLdouble xRight = 1.0; GLdouble yBottom = -1.0; GLdouble yTop = 1.0; /* ** manage rgb colours */ enum { black, pink, orange, white }; typedef GLfloat colourType[3]; colourType colour[]={ {0.0, 0.0, 0.0}, {1.0, 0.9, 0.9}, {1.000, 0.549, 0.000}, {1.0, 1.0, 1.0} }; /* ** manage model(s) */ typedef GLdouble vertexType [2]; # define NUM_VERTICES_FOR_SQUARE 4 vertexType square[]={ {0.5,0.5},{-0.5,0.5},{-0.5,-0.5},{0.5,-0.5} }; /* ** manage transforms */ GLdouble ra = 0; GLdouble angleIncrement = 0.5; /* ** manage animation */ int timer_milliseconds = 5000; /* ** *********************************************************** */ /* ** Function to render an OpenGL primitive. ** IN: an image array of vertexType, the number of vertices in the array, ** and the type of OpenGL primitive to be rendered. ** OUT: */ void render(vertexType * image, int numVertices, int primitive){ int i; glBegin(primitive); for(i=0;i= 360.0) ra = 0.0; } /* ** GLUT callback function ** Called whenever an ordinary key is pressed by the user. ** IN: char value of key pressed, x and y position of the mouse ** OUT: */ void keyboard(unsigned char key,int xMouse,int yMouse){ switch(key){ /* 27 is the integer value of the Esc key */ case 27: exit(EXIT_SUCCESS); case 'm': printf("%s%d%s%d\n","x=",xMouse," y=",yMouse); break; /* increase the rotation angle for the square */ case 'r': rotateSquare(); break; /* if the input key is not handled, display it */ default: printf("%s%c\n","key=",key); } glutPostRedisplay(); } void setProjection(void){ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(xLeft, xRight, yBottom, yTop); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /* ** GLUT callback function ** This is called to render the graphics display in the GLUT window. */ void display(void){ setProjection(); /* ** Clear the window with the background colour before ** (re)rendering. */ glClear(GL_COLOR_BUFFER_BIT); glColor3fv(colour[orange]); glPushMatrix(); glRotated(ra, 0, 0, 1); render(square,NUM_VERTICES_FOR_SQUARE,GL_POLYGON); glPopMatrix(); /* ** Swap the back buffer into the front. */ glutSwapBuffers(); } /* ** GLUT callback function ** called first when a GLUT window is created ** then called every time the window is resized by the user ** IN: w - the current width of the window ** h - the current height of the window */ void reshape(int w, int h){ width = w; height = h; } /* ** TODO: COMMENTS */ void timer(int id){ static count = 0; switch(id){ case 1: glClearColor(0.7, 0.9, 0.7, 0.0); rotateSquare(); glutTimerFunc(50, timer, 2); break; case 2: count++; rotateSquare(); if(count < 300) glutTimerFunc(50, timer, 2); else{ count = 0; angleIncrement = -0.5; glutTimerFunc(50, timer, 3); } break; case 3: glClearColor(0.9, 0.9, 0.7, 0.0); rotateSquare(); glutTimerFunc(50, timer, 3); break; } glutPostRedisplay(); } /* ** initialise GLUT ** IN: argc - number of command line arguments ** argv - array of arguments */ void setUpGLUT(int argc,char ** argv){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(width, height); glutInitWindowPosition(xPosition, yPosition); glutCreateWindow("Rotating Square"); /* ** Register callbacks. */ glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutReshapeFunc(reshape); glutTimerFunc(timer_milliseconds, timer, 1); } /* ** initialise OpenGL */ void initialiseGL(void){ /* ** Set the background colour ** (clear colour) to white. */ glClearColor(1,1,1,0.0); } int main(int argc,char ** argv){ setUpGLUT(argc,argv); initialiseGL(); /* ** Display window and enter the GLUT event ** processing loop. */ glutMainLoop(); /* ** Return success code to operating system. ** Required ANSI standard C code. ** EXIT_SUCCESS defined in stdlib.h */ return EXIT_SUCCESS; }