/* * This program demonstrates double buffering for flicker-free animation. * Originally written by Edward Angel, and only slightly adapted by Scott * D. Anderson. */ #include // This program doesn't actually use TW, but includes glut.h and // other header files. #include const int WinSize = 400; static GLfloat spinAngle = 0.0; int SingleBufferWindow; int DoubleBufferWindow; void displayDoubleBuffering(void) { glClear (GL_COLOR_BUFFER_BIT); glRectf (-25.0, -25.0, 25.0, 25.0); glFlush(); glutSwapBuffers (); } void displaySingleBuffering(void) { glClear(GL_COLOR_BUFFER_BIT); glRectf(-25.0, -25.0, 25.0, 25.0); glFlush(); } void spinDisplay (void) { spinAngle = spinAngle + 1.0; if (spinAngle > 360.0) spinAngle = spinAngle - 360.0; glutSetWindow(SingleBufferWindow); glLoadIdentity(); glRotatef (spinAngle, 0, 0, 1); glutPostRedisplay(); glutSetWindow(DoubleBufferWindow); glLoadIdentity(); glRotatef (spinAngle, 0, 0, 1); glutPostRedisplay(); } void myinit (void) { glClearColor(0, 0, 0, 1); glColor3f(1, 1, 1); glShadeModel(GL_FLAT); } bool Animate = false; void keys(unsigned char key, int x, int y) { switch(key) { case ' ': Animate = !Animate; if(Animate) glutIdleFunc(spinDisplay); else glutIdleFunc(NULL); break; default: exit(0); } } 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 (); } int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(WinSize,WinSize); glutInitWindowPosition(0,0); SingleBufferWindow=glutCreateWindow("single buffered"); myinit (); glutDisplayFunc(displaySingleBuffering); glutReshapeFunc (myReshape); glutIdleFunc (NULL); glutKeyboardFunc(keys); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(WinSize,WinSize); glutInitWindowPosition(WinSize+50,0); DoubleBufferWindow=glutCreateWindow("double buffered"); myinit (); glutDisplayFunc(displayDoubleBuffering); glutReshapeFunc (myReshape); glutIdleFunc (NULL); glutKeyboardFunc(keys); glutMainLoop(); }