/******************************************************* Skeleton for a simple OpenGL program Clicking the mouse starts and stops an object rotating by R.S. Dannelly *******************************************************/ #include #include /***** Global Variables *****/ static GLfloat spin = 0.0; // cumulative rotation /******************************************************** THIS IS THE PRIMARY DRAWING FUNCTION ********************************************************/ void display(void) { glClearColor (0.0,0.0,0.0,0.0); // clear to black glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); // save transformation matrix glRotatef(spin, 1.0, 1.0, 0.0); /* place code for your lines here */ glPopMatrix(); // restore transformation matrix glFlush(); } /******************************************************** The Reshape function is called when the window changes size or is initially created. ********************************************************/ void reshape(int w, int h) { /* 1 : use the entire window */ glViewport (0, 0, (GLsizei) w, (GLsizei) h); /* 2 : clear the Projection Matrix */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); /* 3 : define the size of the universe */ glOrtho(-50.0, 50.0, -50.0, 50.0, -50.0, 50.0); /* 4 : clear the Transformation Matrix */ glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /*********************************************************** Code for rotating called when the CPU is idle and the mouse has been pressed ***********************************************************/ void spinDisplay (void) { spin += 2.0; // add to the spin global variable if (spin > 360.0) spin -= 360.0; glutPostRedisplay(); // update the display } /************************************************** Mouse event handling routine **************************************************/ void mouse(int button, int state, int x, int y) { static int spinning = 0; // only process mouse clicks, not releases if (state == GLUT_DOWN) if (!spinning) { spinning = 1; glutIdleFunc(spinDisplay); // use available CPU time to spin } else { spinning = 0; glutIdleFunc(NULL); // do nothing while idle } } /********************************************************/ /***** M A I N **************************************/ /********************************************************/ int main(int argc, char** argv) { /*** connect this client to the hardware ***/ glutInit(&argc, argv); /*** create a window to draw into ***/ glutInitDisplayMode (GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); /*** do some work ***/ glutDisplayFunc(display); // how to draw glutReshapeFunc(reshape); // configure the world glutMouseFunc(mouse); // mouse handler glutMainLoop(); // infinite loop return 0; }