/* * Copyright (c) 1996-1999 Silicon Graphics, Inc. All rights reserved. * * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. * * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL, * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ /* solar8.c - a simple solar system. * Added a reshape handler to reset the viewport and viewing volume. * Add depth buffering and back face culling. * Add continuous animation and an option to toggle it on and off. * * F1 key - print help information * Key - toggle rotation on / off * SPACE Key - toggle between solid/wireframe models * Escape Key - exit program */ #include /* includes gl.h, glu.h */ #include #include #include "checkerror.h" /* Function Prototypes */ GLvoid initgfx( GLvoid ); GLvoid keyboard( GLubyte, GLint, GLint ); GLvoid specialkeys( GLint, GLint, GLint ); GLvoid animate( GLvoid ); GLvoid visibility( GLint ); GLvoid reshape( GLsizei, GLsizei ); GLvoid drawScene( GLvoid ); void printHelp( char * ); /* Global Variables */ static char *progname; static GLboolean filledFlag = GL_TRUE; static GLfloat sunRadius = 0.7f; static GLfloat earthRadius = 0.4f, earthOrbit = 3.5f; static GLfloat moonRadius = 0.2f, moonOrbit = 1.0f; static GLfloat nearClip, farClip, distance, twistAngle, incAngle, azimAngle; static GLfloat year = 45.0f, day = -90.0f; /* Global Definitions */ #define KEY_ESC 27 /* ascii value for the escape key */ void main( int argc, char *argv[] ) { GLsizei width, height; glutInit( &argc, argv ); /* create a window that is 1/4 the size of the screen, * and position it in the middle of the screen. */ width = glutGet( GLUT_SCREEN_WIDTH ); height = glutGet( GLUT_SCREEN_HEIGHT ); glutInitWindowPosition( width / 4, height / 4 ); glutInitWindowSize( width / 2, height / 2 ); glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE ); glutCreateWindow( argv[0] ); initgfx(); glutIdleFunc( animate ); glutVisibilityFunc( visibility ); glutReshapeFunc( reshape ); glutKeyboardFunc( keyboard ); glutSpecialFunc( specialkeys ); glutDisplayFunc( drawScene ); progname = argv[0]; printHelp( progname ); glutMainLoop(); } void printHelp( char *progname ) { fprintf(stdout, "\n%s - model the solar system\n\n" "F1 key - print help information\n" " Key - toggle rotation on / off\n" "SPACE key - toggle between solid/wireframe mode\n" "Escape Key - exit the program\n\n", progname); } GLvoid initgfx( GLvoid ) { /* set clear color to black */ glClearColor( 0.0f, 0.0f, 0.0f, 1.0f ); /* enable the depth buffer */ glEnable( GL_DEPTH_TEST ); /* enable the face culling */ glEnable( GL_CULL_FACE ); } GLvoid reshape( GLsizei width, GLsizei height ) { GLdouble aspect; glViewport( 0, 0, width, height ); /* compute aspect ratio */ aspect = (GLdouble) width / (GLdouble) height; glMatrixMode( GL_PROJECTION ); /* Reset world coordinates first ... */ glLoadIdentity(); /* Reset the viewing volume based on the new aspect ratio */ gluPerspective( 45.0f, aspect, 1.0f, 20.0f ); glMatrixMode( GL_MODELVIEW ); } GLvoid keyboard( GLubyte key, GLint x, GLint y ) { static GLboolean rotateFlag = GL_TRUE; switch (key) { case 'r': rotateFlag = !rotateFlag; if (rotateFlag) { glutIdleFunc( animate ); } else { glutIdleFunc( NULL ); } glutPostRedisplay(); break; case ' ': /* toggle fill mode */ filledFlag = !filledFlag; glutPostRedisplay(); break; case KEY_ESC: /* Exit when the Escape key is pressed */ exit(0); } } GLvoid specialkeys( GLint key, GLint x, GLint y ) { switch (key) { case GLUT_KEY_F1: /* Function key #1 */ /* print help information */ printHelp( progname ); break; } } GLvoid animate( GLvoid ) { /* update the rotation of the earth and moon in their orbits */ year = fmod( (year + 0.5), 360.0f ); day = fmod( (day + 5.0), 360.0f ); /* Tell GLUT to redraw the scene */ glutPostRedisplay(); } GLvoid visibility( int state ) { if (state == GLUT_VISIBLE) { glutIdleFunc( animate ); } else { glutIdleFunc( NULL ); } } GLvoid drawScene( GLvoid ) { static GLfloat yellow[] = { 1.0f, 1.0f, 0.0f, 1.0f }; static GLfloat blue[] = { 0.0f, 0.0f, 1.0f, 1.0f }; static GLfloat gray[] = { 0.2f, 0.2f, 0.2f, 1.0f }; glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glPushMatrix(); /* move into the viewing volume */ glTranslatef( 0.0f, 0.0f, -10.0f ); /* draw sun */ glPushMatrix(); /* Give the sun a yellow glow */ glColor4fv( yellow ); /* rotate on our own axis */ glRotatef( 90.0f, 1.0f, 0.0f, 0.0f ); if (filledFlag) glutSolidSphere( sunRadius, 15, 15 ); else glutWireSphere( sunRadius, 15, 15 ); glPopMatrix(); glPushMatrix(); /* draw earth */ /* rotate to the right time of year */ glRotatef( year, 0.0f, 1.0f, 0.0f ); /* translate out to our orbit about the sun */ glTranslatef( earthOrbit, 0.0f, 0.0f ); glPushMatrix(); /* set color for the earth */ glColor4fv( blue ); /* rotate on our own axis */ glRotatef( 90.0f, 1.0f, 0.0f, 0.0f ); if (filledFlag) glutSolidSphere( earthRadius, 15, 15 ); else glutWireSphere( earthRadius, 15, 15 ); glPopMatrix(); /* draw moon */ glPushMatrix(); /* set color for the moon */ glColor4fv( gray ); /* rotate to the right time of day */ glRotatef( day, 0.0f, 1.0f, 0.0f ); /* translate out to our orbit about the earth */ glTranslatef( moonOrbit, 0.0f, 0.0f ); /* rotate on our axis */ glRotatef( 90.0f, 1.0f, 0.0f, 0.0f ); if (filledFlag) glutSolidSphere( moonRadius, 15, 15 ); else glutWireSphere( moonRadius, 15, 15 ); glPopMatrix(); glPopMatrix(); glPopMatrix(); checkError( "drawScene" ); glutSwapBuffers(); }