/* * double.c * This program demonstrates double buffering for * flicker-free animation. The left and right mouse * buttons start and stop the spinning motion of the square. */ #include #include static GLfloat spin = 0.0; int singleb, doubleb; void displays(void) { glClear (GL_COLOR_BUFFER_BIT); glRectf (-25.0, -25.0, 25.0, 25.0); glFlush(); //glutSwapBuffers (); } void spinDisplay (void) { spin = spin + 2.0; if (spin > 360.0) spin = spin - 360.0; glutSetWindow(singleb); glLoadIdentity(); glRotatef (spin, 0.0, 0.0, 1.0); glutPostRedisplay(); } void myinit (void) { glClearColor (0.0, 0.0, 0.0, 1.0); glColor3f (1.0, 1.0, 1.0); glShadeModel (GL_FLAT); } void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) glOrtho (-50.0, 50.0, -50.0*(GLfloat)h/(GLfloat)w, 50.0*(GLfloat)h/(GLfloat)w, -1.0, 1.0); else glOrtho (-50.0*(GLfloat)w/(GLfloat)h, 50.0*(GLfloat)w/(GLfloat)h, -50.0, 50.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity (); } void myKey(unsigned char key, int x, int y) { switch (key) { case 27: /* escape key */ exit(0); break; } } void specialKey(int key, int x, int y) { if(key == GLUT_KEY_UP) glutIdleFunc(spinDisplay); if(key==GLUT_KEY_DOWN) glutIdleFunc(NULL); } /* Main Loop * Open window with initial window size, title bar, * RGBA display mode, and handle input events. */ int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); singleb=glutCreateWindow("single buffered"); myinit (); glutDisplayFunc(displays); glutReshapeFunc (myReshape); glutIdleFunc (spinDisplay); glutKeyboardFunc(myKey); glutSpecialFunc(specialKey); glutMainLoop(); }