home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / answers / solar7.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.2 KB  |  246 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* solar7.c - a simple solar system.  
  19.  *    Added a reshape handler to reset the viewport and viewing volume.
  20.  *    Add depth buffering and back face culling.
  21.  *
  22.  *    F1 key            - print help information
  23.  *    SPACE Key        - toggle between solid/wireframe models
  24.  *    Escape Key        - exit program
  25.  */
  26. #include <GL/gl.h>
  27. #include <GL/glu.h>
  28. #include <GL/glut.h>
  29.  
  30. #include <stdio.h>
  31. #include <math.h>
  32.  
  33. /* Function Prototypes */
  34.  
  35. GLvoid initgfx( GLvoid );
  36. GLvoid keyboard( GLubyte, GLint, GLint );
  37. GLvoid specialkeys( GLint, GLint, GLint );
  38. GLvoid reshape( GLsizei, GLsizei );
  39. GLvoid drawScene( GLvoid );
  40.  
  41. void checkError( char * );
  42. void printHelp( char * );
  43.  
  44. /* Global Variables */
  45.  
  46. static char *progname; 
  47.  
  48. static GLboolean filledFlag = GL_TRUE;
  49.  
  50. /* Global Definitions */
  51.  
  52. #define KEY_ESC    27    /* ascii value for the escape key */
  53.  
  54. void
  55. main( int argc, char *argv[] )
  56. {
  57.     GLsizei width, height;
  58.  
  59.     glutInit( &argc, argv );
  60.  
  61.     /* create a window that is 1/4 the size of the screen,
  62.      * and position it in the middle of the screen.
  63.      */
  64.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  65.     height = glutGet( GLUT_SCREEN_HEIGHT );
  66.     glutInitWindowPosition( width / 4, height / 4 );
  67.     glutInitWindowSize( width / 2, height / 2 );
  68.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH );
  69.     glutCreateWindow( argv[0] );
  70.  
  71.     initgfx();
  72.  
  73.     glutReshapeFunc( reshape );
  74.     glutKeyboardFunc( keyboard );
  75.     glutSpecialFunc( specialkeys );
  76.     glutDisplayFunc( drawScene ); 
  77.  
  78.     progname = argv[0];
  79.  
  80.     printHelp( progname );
  81.  
  82.     glutMainLoop();
  83. }
  84.  
  85. void
  86. printHelp( char *progname )
  87. {
  88.     fprintf(stdout, 
  89.         "\n%s - model some objects\n\n"
  90.         "F1 key        - print help information\n"
  91.         "SPACE key    - toggle between solid/wireframe mode\n"
  92.         "Escape Key    - exit the program\n\n",
  93.         progname);
  94. }
  95.  
  96. GLvoid
  97. initgfx( GLvoid )
  98. {
  99.     /* set clear color to black */
  100.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  101.  
  102.     /* enable the depth buffer */
  103.     glEnable( GL_DEPTH_TEST );
  104.  
  105.     /* enable the face culling */
  106.     glEnable( GL_CULL_FACE );
  107. }
  108.  
  109. GLvoid 
  110. reshape( GLsizei width, GLsizei height )
  111. {
  112.     GLdouble    aspect;
  113.  
  114.     glViewport( 0, 0, width, height );
  115.  
  116.     /* compute aspect ratio */
  117.     aspect = (GLdouble) width / (GLdouble) height;
  118.  
  119.     glMatrixMode( GL_PROJECTION );
  120.  
  121.     /* Reset world coordinates first ... */
  122.     glLoadIdentity();
  123.  
  124.     /* Reset the viewing volume based on the new aspect ratio */
  125.     gluPerspective( 45.0, aspect, 1.0, 20.0 );
  126.  
  127.     glMatrixMode( GL_MODELVIEW );
  128. }
  129.  
  130. void 
  131. checkError( char *label )
  132. {
  133.     GLenum error;
  134.     while ( (error = glGetError()) != GL_NO_ERROR )
  135.         printf( "%s: %s\n", label, gluErrorString(error) );
  136. }
  137.  
  138. GLvoid 
  139. keyboard( GLubyte key, GLint x, GLint y )
  140. {
  141.     switch (key) {
  142.     case ' ':    /* toggle fill mode */
  143.         filledFlag = !filledFlag;
  144.         glutPostRedisplay();
  145.         break;
  146.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  147.         exit(0);
  148.     }
  149. }
  150.  
  151. GLvoid 
  152. specialkeys( GLint key, GLint x, GLint y )
  153. {
  154.     switch (key) {
  155.     case GLUT_KEY_F1:    /* Function key #1 */
  156.         /* print help information */
  157.         printHelp( progname );
  158.         break;
  159.     }
  160. }
  161.  
  162. GLvoid
  163. drawScene( GLvoid )
  164. {
  165.     static GLfloat    year = 45.0, day = -90.0;
  166.  
  167.     static GLfloat  yellow[] = { 1.0, 1.0, 0.0, 1.0 };
  168.     static GLfloat  blue[] = { 0.0, 0.0, 1.0, 1.0 };
  169.     static GLfloat  gray[] = { 0.2, 0.2, 0.2, 1.0 };
  170.  
  171.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  172.  
  173.     glPushMatrix();
  174.  
  175.         /* move into the viewing volume */
  176.         glTranslatef( 0.0, 0.0, -10.0 );
  177.  
  178.         /* draw sun */
  179.  
  180.         glPushMatrix();
  181.  
  182.             /* Give the sun a yellow glow */
  183.             glColor4fv( yellow );
  184.           
  185.             /* rotate on our own axis */
  186.             glRotatef( 90.0, 1.0, 0.0, 0.0 );
  187.             if (filledFlag)
  188.                 glutSolidSphere( 0.7, 15, 15 );
  189.             else
  190.                 glutWireSphere( 0.7, 15, 15 );
  191.  
  192.         glPopMatrix();
  193.  
  194.         glPushMatrix();
  195.  
  196.             /* draw earth */
  197.  
  198.             /* rotate to the right time of year */
  199.             glRotatef( year, 0.0, 1.0, 0.0 );
  200.  
  201.             /* translate out to our orbit about the sun */
  202.             glTranslatef( 3.5, 0.0, 0.0 );
  203.             glPushMatrix();
  204.  
  205.                 /* set color for the earth */
  206.                 glColor4fv( blue );
  207.  
  208.                 /* rotate on our own axis */
  209.                 glRotatef( 90.0, 1.0, 0.0, 0.0 );
  210.                 if (filledFlag)
  211.                     glutSolidSphere( 0.4, 15, 15 );
  212.                 else
  213.                     glutWireSphere( 0.4, 15, 15 );
  214.                 
  215.             glPopMatrix();
  216.  
  217.             /* draw moon */
  218.             glPushMatrix();
  219.  
  220.                 /* set color for the moon */
  221.                 glColor4fv( gray );
  222.  
  223.                 /* rotate to the right time of day */
  224.                 glRotatef( day, 0.0, 1.0, 0.0 );
  225.  
  226.                 /* translate out to our orbit about the earth */
  227.                 glTranslatef( 1.0, 0.0, 0.0 );
  228.  
  229.                 /* rotate on our axis */
  230.                 glRotatef( 90.0, 1.0, 0.0, 0.0 );
  231.                 if (filledFlag)
  232.                     glutSolidSphere( 0.2, 15, 15 );
  233.                 else
  234.                     glutWireSphere( 0.2, 15, 15 );
  235.  
  236.             glPopMatrix();
  237.  
  238.         glPopMatrix();
  239.  
  240.     glPopMatrix();
  241.  
  242.     checkError( "drawScene" );
  243.  
  244.     glFlush();
  245. }
  246.