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 / solar10.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  9.7 KB  |  420 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. /* solar10.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.  *    Add a viewing transformation and control it using mouse input.
  22.  *    Enable lighting and give the sun, earth and moon material properties
  23.  *     Add continuous animation and a menu to control it.
  24.  *
  25.  *    F1 key            - print help information
  26.  *    Left Mouse Button    - change incidence and azimuth angles
  27.  *    Middle Mouse Button    - change the twist angle based on
  28.  *                  horizontal mouse movement
  29.  *    Right Mouse Button    - zoom in and out based on vertical
  30.  *                  mouse movement
  31.  *    <r> Key            - toggle rotation on / off
  32.  *    <R> Key            - reset viewpoint
  33.  *    SPACE Key        - toggle between solid/wireframe models
  34.  *    Escape Key        - exit program
  35.  */
  36. #include <GL/gl.h>
  37. #include <GL/glu.h>
  38. #include <GL/glut.h>
  39.  
  40. #include <stdio.h>
  41. #include <math.h>
  42.  
  43. /* Function Prototypes */
  44.  
  45. GLvoid initgfx( GLvoid );
  46. GLvoid keyboard( GLubyte, GLint, GLint );
  47. GLvoid specialkeys( GLint, GLint, GLint );
  48. GLvoid mouse( GLint, GLint, GLint, GLint );
  49. GLvoid motion( GLint, GLint );
  50. GLvoid animate( GLvoid );
  51. GLvoid visibility( GLint );
  52. GLvoid reshape( GLsizei, GLsizei );
  53. GLvoid drawScene( GLvoid );
  54.  
  55. void checkError( char * );
  56. void printHelp( char * );
  57.  
  58. void resetView( GLvoid );
  59. void polarView( GLfloat, GLfloat, GLfloat, GLfloat);
  60.  
  61. /* Global Variables */
  62.  
  63. static char *progname; 
  64.  
  65. static GLboolean filledFlag = GL_TRUE;
  66.  
  67. static enum        actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
  68. static GLint        action;
  69.  
  70. static GLdouble        xStart = 0.0, yStart = 0.0;
  71.  
  72. static GLfloat        sunRadius = 0.7;
  73. static GLfloat        earthRadius = 0.4, earthOrbit = 3.5; 
  74. static GLfloat        moonRadius = 0.2, moonOrbit = 1.0;
  75. static GLfloat         near, far, distance, twistAngle, incAngle, azimAngle;
  76.  
  77. static GLfloat        year = 45.0, day = -90.0;
  78.  
  79. /* Global Definitions */
  80.  
  81. #define KEY_ESC    27    /* ascii value for the escape key */
  82.  
  83. void
  84. main( int argc, char *argv[] )
  85. {
  86.     GLsizei width, height;
  87.  
  88.     glutInit( &argc, argv );
  89.  
  90.     /* create a window that is 1/4 the size of the screen,
  91.      * and position it in the middle of the screen.
  92.      */
  93.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  94.     height = glutGet( GLUT_SCREEN_HEIGHT );
  95.     glutInitWindowPosition( width / 4, height / 4 );
  96.     glutInitWindowSize( width / 2, height / 2 );
  97.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  98.     glutCreateWindow( argv[0] );
  99.  
  100.     initgfx();
  101.  
  102.     glutIdleFunc( animate );
  103.     glutVisibilityFunc( visibility );
  104.     glutReshapeFunc( reshape );
  105.     glutKeyboardFunc( keyboard );
  106.     glutSpecialFunc( specialkeys );
  107.     glutMouseFunc( mouse );
  108.     glutMotionFunc( motion );
  109.     glutDisplayFunc( drawScene ); 
  110.  
  111.     progname = argv[0];
  112.  
  113.     printHelp( progname );
  114.  
  115.     glutMainLoop();
  116. }
  117.  
  118. void
  119. printHelp( char *progname )
  120. {
  121.     fprintf(stdout, 
  122.         "\n%s - model some objects\n\n"
  123.         "F1 key        - print help information\n"
  124.         "Left Mousebutton    - move eye position\n"
  125.         "Middle Mousebutton    - change twist angle\n"
  126.         "Right Mousebutton    - move up / down to zoom in / out\n"
  127.         "<r> Key        - toggle rotation on / off\n"
  128.         "<R> Key        - reset viewpoint\n"
  129.         "SPACE key    - toggle between solid/wireframe mode\n"
  130.         "Escape Key    - exit the program\n\n",
  131.         progname);
  132. }
  133.  
  134. GLvoid
  135. initgfx( GLvoid )
  136. {
  137.     GLfloat maxOrbit;
  138.  
  139.     /* Set up properties for the light */
  140.     GLfloat    sunSpecular[] = { 0.0, 0.0, 0.0, 1.0 };
  141.  
  142.     /* set clear color to black */
  143.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  144.  
  145.     /* enable the depth buffer */
  146.     glEnable( GL_DEPTH_TEST );
  147.  
  148.     /* enable the face culling */
  149.     glEnable( GL_CULL_FACE );
  150.  
  151.     /* Maximum size of all the objects in your scene */
  152.     maxOrbit = earthOrbit + moonOrbit + moonRadius;
  153.  
  154.     /* Set up near and far so that ( far - near ) > maxObjectSize, */
  155.     /* and determine the viewing distance (adjust for zooming) */
  156.     near = 1.0;
  157.     far = near + 8*maxOrbit; 
  158.  
  159.     resetView();
  160.  
  161.     /* Set the light properties */
  162.     glLightfv( GL_LIGHT0, GL_SPECULAR, sunSpecular );
  163.  
  164.     /* Turn on the default light */
  165.     glEnable( GL_LIGHT0 );
  166.  
  167.     /* Enable fast material changes for diffuse material */
  168.     glColorMaterial( GL_FRONT, GL_DIFFUSE );
  169. }
  170.  
  171. GLvoid 
  172. reshape( GLsizei width, GLsizei height )
  173. {
  174.     GLdouble    aspect;
  175.  
  176.     glViewport( 0, 0, width, height );
  177.  
  178.     /* compute aspect ratio */
  179.     aspect = (GLdouble) width / (GLdouble) height;
  180.  
  181.     glMatrixMode( GL_PROJECTION );
  182.  
  183.     /* Reset world coordinates first ... */
  184.     glLoadIdentity();
  185.  
  186.     /* Reset the viewing volume based on the new aspect ratio */
  187.     gluPerspective( 45.0, aspect, near, far );
  188.  
  189.     glMatrixMode( GL_MODELVIEW );
  190. }
  191.  
  192. void 
  193. checkError( char *label )
  194. {
  195.     GLenum error;
  196.     while ( (error = glGetError()) != GL_NO_ERROR )
  197.         printf( "%s: %s\n", label, gluErrorString(error) );
  198. }
  199.  
  200. GLvoid 
  201. keyboard( GLubyte key, GLint x, GLint y )
  202. {
  203.     static GLboolean    rotateFlag = GL_TRUE;
  204.  
  205.     switch (key) {
  206.     case 'r':
  207.         rotateFlag = !rotateFlag;
  208.         if (rotateFlag) {
  209.             glutIdleFunc( animate );
  210.         } else {
  211.             glutIdleFunc( NULL );
  212.         }
  213.         glutPostRedisplay();
  214.         break;
  215.     case ' ':    /* toggle fill mode */
  216.         filledFlag = !filledFlag;
  217.         glutPostRedisplay();
  218.         break;
  219.     case 'R':
  220.         resetView();
  221.         glutPostRedisplay();
  222.         break;
  223.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  224.         exit(0);
  225.     }
  226. }
  227.  
  228. GLvoid 
  229. specialkeys( GLint key, GLint x, GLint y )
  230. {
  231.     switch (key) {
  232.     case GLUT_KEY_F1:    /* Function key #1 */
  233.         /* print help information */
  234.         printHelp( progname );
  235.         break;
  236.     }
  237. }
  238.  
  239. GLvoid 
  240. mouse( GLint button, GLint state, GLint x, GLint y )
  241. {
  242.     if (state == GLUT_DOWN) {
  243.         switch (button) {
  244.         case GLUT_LEFT_BUTTON:
  245.             action = MOVE_EYE;
  246.             break;
  247.         case GLUT_MIDDLE_BUTTON:
  248.             action = TWIST_EYE;
  249.             break;
  250.         case GLUT_RIGHT_BUTTON:
  251.             action = ZOOM;
  252.             break;
  253.         }
  254.  
  255.         /* Update the saved mouse position */
  256.         xStart = x;
  257.         yStart = y;
  258.     } else {
  259.         action = MOVE_NONE;
  260.     }
  261.  
  262. }
  263.  
  264. GLvoid
  265. motion( GLint x, GLint y )
  266. {
  267.     switch (action) {
  268.     case MOVE_EYE:
  269.         /* Adjust the eye position based on the mouse position */
  270.         azimAngle += (GLdouble) (x - xStart);
  271.         incAngle -= (GLdouble) (y - yStart);
  272.         break;
  273.     case TWIST_EYE:
  274.         /* Adjust the eye twist based on the mouse position */
  275.         twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
  276.         break;
  277.     case ZOOM:
  278.         /* Adjust the eye distance based on the mouse position */
  279.         distance -= (GLdouble) (y - yStart)/10.0;
  280.         break;
  281.     default:
  282.         printf("unknown action %d\n", action);
  283.     }
  284.     
  285.     /* Update the stored mouse position for later use */
  286.     xStart = x;
  287.     yStart = y;
  288.  
  289.     glutPostRedisplay();
  290. }
  291.  
  292. GLvoid 
  293. animate( GLvoid )
  294. {
  295.     /* update the rotation of the earth and moon in their orbits */
  296.     year = fmodf( (year + 0.5), 360.0 );
  297.     day = fmodf( (day + 5.0), 360.0 );
  298.  
  299.     /* Tell GLUT to redraw the scene */
  300.     glutPostRedisplay();
  301. }
  302.  
  303. GLvoid
  304. visibility( int state ) 
  305. {
  306.     if (state == GLUT_VISIBLE) {
  307.         glutIdleFunc( animate );
  308.     } else {
  309.         glutIdleFunc( NULL );
  310.     }
  311. }
  312.  
  313. void
  314. resetView( GLvoid )
  315. {
  316.     distance = near + (far - near) / 2.0;
  317.     twistAngle = 0.0;    /* rotation of viewing volume (camera) */
  318.     incAngle = 0.0;
  319.     azimAngle = 0.0;
  320. }
  321.  
  322. void
  323. polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
  324.             GLfloat twist)
  325. {
  326.     glTranslatef( 0.0, 0.0, -distance);
  327.     glRotatef( -twist, 0.0, 0.0, 1.0);
  328.     glRotatef( -incidence, 1.0, 0.0, 0.0);
  329.     glRotatef( -azimuth, 0.0, 0.0, 1.0);
  330. }
  331.  
  332. GLvoid
  333. drawScene( GLvoid )
  334. {
  335.     static GLfloat  yellow[] = { 1.0, 1.0, 0.0, 1.0 };
  336.     static GLfloat  blue[] = { 0.0, 0.0, 1.0, 1.0 };
  337.     static GLfloat  gray[] = { 0.4, 0.4, 0.4, 1.0 };
  338.  
  339.     static GLfloat  noEmission[] = { 0.0, 0.0, 0.0, 1.0 };
  340.     static GLfloat  defaultDiffuse[] = { 0.8, 0.8, 0.8, 1.0 };
  341.  
  342.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  343.  
  344.     glPushMatrix();
  345.  
  346.         /* set up viewing transformation */
  347.         polarView( distance, azimAngle, incAngle, twistAngle );
  348.  
  349.         glEnable( GL_LIGHTING );
  350.         glEnable( GL_COLOR_MATERIAL );
  351.  
  352.         /* draw sun */
  353.  
  354.         /* Give the sun a yellow glow */
  355.         glMaterialfv( GL_FRONT, GL_EMISSION, yellow );
  356.       
  357.         glPushMatrix();
  358.             /* rotate on our own axis */
  359.             glRotatef( 90.0, 1.0, 0.0, 0.0 );
  360.             if (filledFlag)
  361.                 glutSolidSphere( sunRadius, 15, 15 );
  362.             else
  363.                 glutWireSphere( sunRadius, 15, 15 );
  364.         glPopMatrix();
  365.  
  366.         /* turn emmission off while drawing planets */
  367.         glMaterialfv( GL_FRONT, GL_EMISSION, noEmission );
  368.  
  369.         /* set material for the earth */
  370.         glColor4fv( blue );
  371.  
  372.         glPushMatrix();
  373.  
  374.             /* draw earth */
  375.  
  376.             /* rotate to the right time of year */
  377.             glRotatef( year, 0.0, 1.0, 0.0 );
  378.  
  379.             /* translate out to our orbit about the sun */
  380.             glTranslatef( earthOrbit, 0.0, 0.0 );
  381.             glPushMatrix();
  382.                 /* rotate on our own axis */
  383.                 glRotatef( 90.0, 1.0, 0.0, 0.0 );
  384.                 if (filledFlag)
  385.                     glutSolidSphere( earthRadius, 15, 15 );
  386.                 else
  387.                     glutWireSphere( earthRadius, 15, 15 );
  388.             glPopMatrix();
  389.  
  390.             /* set material for the moon */
  391.             glColor4fv( gray );
  392.  
  393.             /* draw moon */
  394.             glPushMatrix();
  395.                 /* rotate to the right time of day */
  396.                 glRotatef( day, 0.0, 1.0, 0.0 );
  397.  
  398.                 /* translate out to our orbit about the earth */
  399.                 glTranslatef( moonOrbit, 0.0, 0.0 );
  400.  
  401.                 /* rotate on our axis */
  402.                 glRotatef( 90.0, 1.0, 0.0, 0.0 );
  403.                 if (filledFlag)
  404.                     glutSolidSphere( moonRadius, 15, 15 );
  405.                 else
  406.                     glutWireSphere( moonRadius, 15, 15 );
  407.             glPopMatrix();
  408.  
  409.         glPopMatrix();
  410.  
  411.         glDisable( GL_COLOR_MATERIAL );
  412.         glDisable( GL_LIGHTING );
  413.  
  414.     glPopMatrix();
  415.  
  416.     checkError( "drawScene" );
  417.  
  418.     glutSwapBuffers();
  419. }
  420.