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