/*************************************************** Movement Example Four Program that demonstrates using PushMatrix and PopMatrix for VERY complex movements. A moving grey cone is orbited by a red donut. Green and blue balls spin in and out of the donut. **************************************************/ #include #include // X and Y location, donut spin, sphere one and two spin float X=-50.0,Y=-20.0,dspin=0.0,s1spin=0.0, s2spin; /*************************************************************************/ void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glPushMatrix(); // save identity state glTranslatef(X,Y,0.0); // move the whole scene to X,Y glPushMatrix(); // save this movement glRotatef(-90, 1, 0, 0); // stand up the cone glColor3f(0.7,0.7,0.7); glutSolidCone (3,15,20,10); // draw the gray cone glPopMatrix(); // go back to movement without rotation glRotatef(dspin, 0, 1, 0); // spin the donut about Y axis glTranslatef(20, 5, 0.0); // move the donut away from cone glPushMatrix(); // save this transformation glTranslatef( 0, 7, 0); // move the red ball up to top of ring glRotatef(s1spin,1,0,0); // spin the red sphere about X glTranslatef( 0, 5, 0); // radius of ball's orbit is 5 glColor3f(0.2,0.8,0.5); glutSolidSphere(4,10,10); // draw the red ball glPopMatrix(); // go back to previous transformation state glPushMatrix(); // save this transformation glTranslatef( 7, 0, 0); // move the blue ball up to side of ring glRotatef(s2spin,0,1,0); // spin the blue sphere about Y glTranslatef( 5, 0, 0); // radius of ball's orbit is 5 glColor3f(0.2,0.3,0.9); glutSolidSphere(4,10,10); // draw the blue ball glPopMatrix(); // go back to previous transformation state glColor3f(0.9,0.3,0.2); glutSolidTorus(1,6,20,30); // draw the donut glPopMatrix(); // go back to identity matrix glutSwapBuffers(); glFinish(); } /*************************************************************************/ 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(); } /*************************************************************************/ void animate() { X += 0.2; Y += 0.1; dspin += 3; s1spin += 11; s2spin += 23; if (X > 60) X = -60.0; if (Y > 60) Y = -60.0; glutPostRedisplay(); } /*************************************************************************/ /*************************************************************************/ int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize (450, 450); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); glutDisplayFunc(display); glutReshapeFunc(reshape); glutIdleFunc(animate); glutMainLoop(); return 0; }