/* demoScene.c */ /* Purpose: create a scene using push/pop and GLUT3D models */ #ifdef __APPLE__ #include #else #include #endif #include #include #include #define KEY_ESC 27 /* GLUT doesn't supply this */ /* Global Definitions */ static const GLfloat green[] = {0.0, 1.0, 0.0}; static const GLfloat red[] = {1.0, 0.0, 0.0}; static const GLfloat blue[] = {0.0, 0.0, 1.0}; static const GLdouble cubeSize = 10.0; static const GLdouble coneBase = 3.0; static const GLdouble coneHeight = 10.0; static const GLint coneSlices = 8; static const GLint coneStacks = 15; static const GLdouble sphereRadius = 4.0; static const GLint sphereSlices = 8; static const GLint sphereStacks = 15; void printHelp( void ) { fprintf(stdout, "\ndemoScene - using push and pop\n\n" "Escape key - exit the program\n\n"); } GLvoid keyboard( GLubyte key, GLint x, GLint y ) { switch ( key ) { case KEY_ESC: exit(0); } } GLvoid checkError( const char* const label ) { GLenum error; error = glGetError(); while (GL_NO_ERROR != error ) { fprintf(stderr,"%s: %s\n", label, gluErrorString(error) ); error = glGetError(); } } GLvoid reshape( GLint width, GLint height ) { GLdouble aspect; aspect = (GLdouble) width / (GLdouble) height; /* We still want the output to cover the whole window */ glViewport(0, 0, width, height); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); /* Get the viewing volume to have the correct aspect ratio */ gluPerspective(45.0, aspect, 1.0, 300.0); glMatrixMode(GL_MODELVIEW); } GLvoid drawConeAndSphere( GLvoid ) { glPushMatrix(); /* Move to the side of the cube */ glTranslatef(10.0, 0.0, 0.0); /* Now rotate the cone */ glPushMatrix(); glRotatef(-90.0, 1.0, 0.0, 0.0); glRotatef(90.0, 0.0, 1.0, 0.0); /* Draw the first cone */ glColor3fv(red); glutWireCone(coneBase,coneHeight,coneSlices,coneStacks); glPopMatrix(); /* Move to the side of the cone and draw the sphere */ glPushMatrix(); glTranslatef(14.0, 0.0, 0.0); glColor3fv(blue); glutWireSphere(sphereRadius, sphereSlices, sphereStacks); glPopMatrix(); glPopMatrix(); } GLvoid drawScene( GLvoid ) { glClear(GL_COLOR_BUFFER_BIT); glColor3fv(blue); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* Translate into the viewing volume */ glTranslatef(0.0f, 0.0f, -75.0f); glPushMatrix(); /* Draw the cube */ glRotatef(30, 0.0, 1.0, 0.0); glutWireCube(cubeSize); glPopMatrix(); { int angle; for( angle = 0; angle < 360; angle += 90 ) { glPushMatrix(); glRotatef( angle, 0.0, 0.0, 1.0 ); drawConeAndSphere(); glPopMatrix(); } } glFlush(); } GLvoid init( GLvoid ) { glClearColor( 1.0, 1.0, 1.0, 1.0 ); } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutCreateWindow(argv[0]); init(); glutKeyboardFunc( keyboard ); glutReshapeFunc( reshape ); glutDisplayFunc( drawScene ); glutMainLoop(); return 0; }