/****************************************************** ** Demo Program for Timing ** Object rotates once every 4 seconds - exactly. ** Uses a signal sent at regular intervals to call PostRedisplay. ** ** R.S. Dannelly *****************************************************/ #include #include #include #include #include #define TRANS_PER_SEC 25 GLfloat spin = 0.0; /*****************************************************/ void display(void) { static int i=0; // send an alarm in N microseconds struct itimerval old, new; new.it_interval.tv_usec = 0; new.it_interval.tv_sec = 0; new.it_value.tv_usec = (long int) (4000000 / TRANS_PER_SEC); new.it_value.tv_sec = 0; if (setitimer (ITIMER_REAL, &new, &old) < 0) printf("timer not set\n"); // clear the screen glClearColor (0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); // draw the spun teapot glLoadIdentity(); glRotatef (spin, 0,1,0); if (i++ % TRANS_PER_SEC == 0) // draw red when spin == 0 glColor3f (1.0, 0.0, 0.0); else glColor3f (0.5, 0.5, 0.5); glutSolidTeapot(15); glutSwapBuffers(); glFlush(); } /*****************************************************/ void reshape(int w, int h) { glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-50.0, 50.0, -50.0, 50.0, -50.0, 50.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /****** use the mouse to exit *************************/ void mouse(int button, int state, int x, int y) { if (state == GLUT_DOWN) exit(1); } /********************************************************/ void signal_handler() { // be sure to re-register this handler signal (SIGALRM, signal_handler); // move a little bit spin += (360.0 / TRANS_PER_SEC); glutPostRedisplay(); } /********************************************************/ int main(int argc, char** argv) { signal (SIGALRM, signal_handler); // handle the alarm signal glutInit(&argc, argv); // standard startup stuff glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE); glutInitWindowSize (450, 450); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc(mouse); // exit on mouse click glutMainLoop(); return 0; }