#include #include #include /* In this lab, we will modify an OpenGL program to study Double-Buffering #1 Note again that the 400 x 400 window is mapped to a frame where the origin, (0,0), is in the middle. Compile and run the code as-is. What happens when you click on the left and right mouse buttons? #2 Now try it with the screen maximized. What changed? #3 Turn on double buffering. Look for the comments in the code below. */ #define WINDOW 400 #define LENGTH WINDOW/4.0 #define PI 4.0*atan(1.0) GLfloat theta=45.0; void myInit () { glClearColor( 1.0 , 1.0 , 1.0 , 0.0 ); glColor3f( 0.0 , 0.0 , 0.0 ); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-WINDOW/2.0 , WINDOW/2.0 , -WINDOW/2.0 , WINDOW/2.0); glMatrixMode(GL_MODELVIEW); } void display() { glClear(GL_COLOR_BUFFER_BIT); GLfloat thetar; GLfloat a,b; thetar = 2.0*PI*theta/360.0; a = LENGTH*cos(thetar); b = LENGTH*sin(thetar); glBegin(GL_POLYGON); glColor3f(1.0,0.0,0.0); glVertex2f(a,b); glColor3f(0.0,1.0,0.0); glVertex2f(-b,a); glColor3f(0.0,0.0,1.0); glVertex2f(-a,-b); glColor3f(0.5,0.5,0.5); glVertex2f(b,-a); glEnd(); // switch the following lines glFlush(); //glutSwapBuffers(); } void spinDisplay() { theta+=0.5; glutPostRedisplay(); } void mouse (GLint btn, GLint state, GLint x, GLint y) { if (btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN) { glutIdleFunc(spinDisplay); } if (btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) { glutIdleFunc(NULL); } } int main (int argc, char ** argv) { glutInit(&argc,argv); // switch the following lines glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB ); //glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB ); glutInitWindowSize(WINDOW,WINDOW); glutInitWindowPosition(0,0); glutCreateWindow("rotatin"); glutDisplayFunc(display); glutMouseFunc(mouse); myInit(); glutMainLoop(); return 0; }