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 / examples / adv_lighting / movingLight.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  8.3 KB  |  337 lines

  1. /* Copyright 1996, Silicon Graphics, Inc.
  2.  * All Rights Reserved.
  3.  *
  4.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  5.  * the contents of this file may not be disclosed to third parties, copied or
  6.  * duplicated in any form, in whole or in part, without the prior written
  7.  * permission of Silicon Graphics, Inc.
  8.  *
  9.  * RESTRICTED RIGHTS LEGEND:
  10.  * Use, duplication or disclosure by the Government is subject to restrictions
  11.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  12.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  13.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  14.  * rights reserved under the Copyright Laws of the United States.
  15.  */
  16. /* movingLight.c - Set up a light that moves independent 
  17.  *        of the objects in the scene.
  18.  *
  19.  *  Left Mouse Button        - change incidence and azimuth angles
  20.  *  Middle Mousebutton        - change the twist angle based on
  21.  *                  horizontal mouse movement
  22.  *  Right Mousebutton        - zoom in and out based on vertical
  23.  *                  mouse movement
  24.  *  <a> key            - toggle light animation
  25.  *  Escape key            - exit the program
  26.  */
  27.  
  28. #include <GL/gl.h>
  29. #include <GL/glu.h>
  30. #include <GL/glut.h>
  31.  
  32. #include <math.h>
  33. #include <stdio.h>
  34.  
  35. /*  Function Prototypes  */
  36.  
  37. GLvoid  initgfx( GLvoid );
  38. GLvoid  animate( GLvoid );
  39. GLvoid  visibility( GLint );
  40. GLvoid  drawScene( GLvoid );
  41. GLvoid  reshape( GLsizei, GLsizei );
  42. GLvoid  keyboard( GLubyte, GLint, GLint );
  43. GLvoid  mouse( GLint, GLint, GLint, GLint );
  44. GLvoid  motion( GLint, GLint );
  45.  
  46. void resetView( GLvoid );
  47. void polarView( GLfloat, GLfloat, GLfloat, GLfloat );
  48. void printHelp( char * );
  49.  
  50. /* Global Definitions */
  51.  
  52. #define KEY_ESC    27    /* ascii value for the escape key */
  53.  
  54. /* Global Variables */
  55.  
  56. static GLfloat        lightAngle = 0.0;    /* controls light rotation */
  57.  
  58. static GLboolean     animateLight = GL_TRUE;
  59.  
  60. static enum        actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
  61. static GLint        action;
  62.  
  63. static GLdouble        xStart = 0.0, yStart = 0.0;
  64.  
  65. static GLfloat         fovy, near, far, distance, twistAngle, incAngle, azimAngle;
  66.  
  67. void
  68. main( int argc, char *argv[] )
  69. {
  70.     GLsizei width, height;
  71.  
  72.     glutInit( &argc, argv );
  73.  
  74.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  75.     height = glutGet( GLUT_SCREEN_HEIGHT );
  76.     glutInitWindowPosition( width / 4, height / 4 );
  77.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  78.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  79.     glutCreateWindow( argv[0] );
  80.  
  81.     initgfx();
  82.  
  83.     glutIdleFunc( animate );
  84.     glutVisibilityFunc( visibility );
  85.     glutMouseFunc( mouse );
  86.     glutMotionFunc( motion );
  87.     glutKeyboardFunc( keyboard );
  88.     glutReshapeFunc( reshape );
  89.     glutDisplayFunc( drawScene ); 
  90.  
  91.     printHelp( argv[0] );
  92.  
  93.     glutMainLoop();
  94. }
  95.  
  96. void
  97. printHelp( char *progname )
  98. {
  99.     fprintf(stdout, "\n%s - demonstrate how to add a moving light\n\n" 
  100.         "Left Mousebutton    - move eye position\n"
  101.         "Middle Mousebutton    - change twist angle\n"
  102.         "Right Mousebutton    - move up / down to zoom in / out\n"
  103.         "<a> Key            - toggle light animation\n"
  104.         "Escape Key        - exit the program\n\n",
  105.         progname);
  106. }
  107.  
  108. GLvoid
  109. initgfx( GLvoid )
  110. {
  111.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  112.     glEnable( GL_DEPTH_TEST );
  113.  
  114.     near = 3.0;    /* Near clipping plane location */
  115.     far  = 12.0;    /* Far clipping plane location */
  116.  
  117.     resetView();
  118.  
  119.     /* Turn on a default light */
  120.     glEnable( GL_LIGHT0 );
  121. }
  122.  
  123. GLvoid 
  124. keyboard( GLubyte key, GLint x, GLint y )
  125. {
  126.     switch (key) {
  127.     case 'a':    /* toggle light animation */
  128.         animateLight = !animateLight;
  129.         if ( animateLight )
  130.             glutIdleFunc(animate);
  131.         else 
  132.             glutIdleFunc(NULL);
  133.         glutPostRedisplay();
  134.         break;
  135.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  136.         exit(0);
  137.     }
  138. }
  139.  
  140. GLvoid 
  141. mouse( GLint button, GLint state, GLint x, GLint y )
  142. {
  143.     static GLint buttons_down = 0;
  144.  
  145.     if (state == GLUT_DOWN) {
  146.         switch (button) {
  147.         case GLUT_LEFT_BUTTON:
  148.             action = MOVE_EYE;
  149.             break;
  150.         case GLUT_MIDDLE_BUTTON:
  151.             action = TWIST_EYE;
  152.             break;
  153.         case GLUT_RIGHT_BUTTON:
  154.             action = ZOOM;
  155.             break;
  156.         }
  157.  
  158.         /* Update the saved mouse position */
  159.         xStart = x;
  160.         yStart = y;
  161.     } else {
  162.         if (--buttons_down == 0) 
  163.             action = MOVE_NONE;
  164.     }
  165. }
  166.  
  167. GLvoid
  168. motion( GLint x, GLint y )
  169. {
  170.     switch (action) {
  171.     case MOVE_EYE:
  172.         /* Adjust the eye position based on the mouse position */
  173.         azimAngle += (GLdouble) (x - xStart);
  174.         incAngle -= (GLdouble) (y - yStart);
  175.         break;
  176.     case TWIST_EYE:
  177.         /* Adjust the eye twist based on the mouse position */
  178.         twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
  179.         break;
  180.     case ZOOM:
  181.         /* Adjust the eye distance based on the mouse position */
  182.         distance -= (GLdouble) (y - yStart)/10.0;
  183.         break;
  184.     default:
  185.         printf("unknown action %d\n", action);
  186.     }
  187.     
  188.     /* Update the stored mouse position for later use */
  189.     xStart = x;
  190.     yStart = y;
  191.  
  192.     glutPostRedisplay();
  193. }
  194.  
  195. void
  196. resetView( GLvoid )
  197. {
  198.     distance = near + (far - near) / 2.0;
  199.     twistAngle = 0.0;    /* rotation of viewing volume (camera) */
  200.     incAngle = 60.0;
  201.     azimAngle = 0.0;
  202.     fovy = 60.0;    /* Field of view in Y angle */
  203. }
  204.  
  205. GLvoid
  206. reshape( GLsizei width, GLsizei height )
  207. {
  208.     GLdouble    aspect;
  209.  
  210.     glViewport( 0, 0, width, height );
  211.  
  212.     aspect = (GLdouble) width / (GLdouble) height;
  213.  
  214.     glMatrixMode( GL_PROJECTION );
  215.     glLoadIdentity();
  216.     gluPerspective( fovy, aspect, near, far );
  217.     glMatrixMode( GL_MODELVIEW );
  218. }
  219.  
  220. GLvoid 
  221. animate( GLvoid )
  222. {
  223.     /* update the rotation of the light for each scene */
  224.     lightAngle = fmodf( (lightAngle + 2.0), 360.0 );
  225.  
  226.     /* Tell GLUT to redraw the scene */
  227.     glutPostRedisplay();
  228. }
  229.  
  230. GLvoid
  231. visibility( int state ) 
  232. {
  233.     if (state == GLUT_VISIBLE && animateLight) {
  234.         glutIdleFunc( animate );
  235.     } else {
  236.         glutIdleFunc( NULL );
  237.     }
  238. }
  239.  
  240. void
  241. polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
  242.             GLfloat twist)
  243. {
  244.     glTranslatef( 0.0, 0.0, -distance);
  245.     glRotatef( -twist, 0.0, 0.0, 1.0);
  246.     glRotatef( -incidence, 1.0, 0.0, 0.0);
  247.     glRotatef( -azimuth, 0.0, 0.0, 1.0);
  248. }
  249.  
  250. GLvoid
  251. drawScene( GLvoid )
  252. {
  253.     /* Define a few materials properties */
  254.     GLfloat   redAmbient[] = { 0.3, 0.1, 0.1, 1.0 };
  255.     GLfloat   redDiffuse[] = { 1.0, 0.0, 0.0, 1.0 };
  256.     GLfloat   blueAmbient[] = { 0.1, 0.1, 0.3, 1.0 };
  257.     GLfloat   blueDiffuse[] = { 0.0, 0.0, 1.0, 1.0 };
  258.     GLfloat   yellowDiffuse[] = { 1.0, 1.0, 0.0, 1.0 };
  259.     GLfloat   yellowEmission[] = { 0.6, 0.6, 0.0, 1.0 };
  260.     GLfloat   defaultEmission[] = { 0.0, 0.0, 0.0, 1.0 };
  261.     GLfloat   whiteSpecular[] = { 1.0, 1.0, 1.0, 1.0 };
  262.     GLfloat   greenSpecular[] = { 0.0, 1.0, 0.0, 1.0 };
  263.     GLfloat   defaultSpecular[] = { 0.0, 0.0, 0.0, 1.0 };
  264.  
  265.     /* local light */
  266.     GLfloat   lightPosition[] = { 0.0, 0.0, 0.0, 1.0 };
  267.  
  268.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  269.  
  270.     glPushMatrix();
  271.  
  272.         polarView( distance, azimAngle, incAngle, twistAngle );
  273.  
  274.         XYZaxes();
  275.  
  276.         /* Animate the light with its own set of transformations */
  277.         glPushMatrix();
  278.             glRotatef( lightAngle, 0.0, 1.0, 0.0 );
  279.             glTranslatef( 2.7, 0.0, 0.0 );
  280.  
  281.             /* By giving the light position its own modeling
  282.              * transformations (due to the glPushMatrix() 
  283.               * and glPopMatrix() calls), the light moves
  284.              * independently of the objects in the scene.  
  285.              */
  286.             glLightfv( GL_LIGHT0, GL_POSITION, lightPosition );
  287.  
  288.             /* draw a small (unlit) white sphere to
  289.              * represent the light in our scene. 
  290.              */
  291.             glColor3f( 1.0, 1.0, 1.0 );
  292.             glutSolidSphere( 0.07, 4, 7);
  293.         glPopMatrix();
  294.  
  295.         glEnable( GL_LIGHTING );
  296.  
  297.         glMaterialfv( GL_FRONT, GL_EMISSION, defaultEmission );
  298.  
  299.         /* Set properties for a shiny red material,
  300.          * with a green highlight */
  301.         glMaterialfv( GL_FRONT, GL_AMBIENT, redAmbient );
  302.         glMaterialfv( GL_FRONT, GL_DIFFUSE, redDiffuse );
  303.         glMaterialfv( GL_FRONT, GL_SPECULAR, greenSpecular );
  304.         glMaterialf( GL_FRONT, GL_SHININESS, 128.0 );
  305.         glPushMatrix();
  306.             glTranslatef( -2.0, 1.5, 0.0 );
  307.             glutSolidSphere( 0.7, 31, 31 );
  308.         glPopMatrix();
  309.  
  310.         /* Set properties for a dull blue material with
  311.          *   a small white highlight */
  312.         glMaterialfv( GL_FRONT, GL_AMBIENT, blueAmbient );
  313.         glMaterialfv( GL_FRONT, GL_DIFFUSE, blueDiffuse );
  314.         glMaterialfv( GL_FRONT, GL_SPECULAR, whiteSpecular );
  315.         glMaterialf( GL_FRONT, GL_SHININESS, 20.0 );
  316.         glPushMatrix();
  317.             glTranslatef( 2.5, 0.0, 0.0 );
  318.             glutSolidTorus( 0.25, 0.75, 16, 31 );
  319.         glPopMatrix();
  320.  
  321.         /* Set properties for a yellow glowing material */
  322.         glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, yellowDiffuse);
  323.         glMaterialfv( GL_FRONT, GL_EMISSION, yellowEmission );
  324.         glMaterialfv( GL_FRONT, GL_SPECULAR, defaultSpecular );
  325.  
  326.         glPushMatrix();
  327.             glTranslatef( 0.0, 2.0, 2.0 );
  328.             glutSolidCube( 0.5 );
  329.         glPopMatrix();
  330.  
  331.         glDisable( GL_LIGHTING );
  332.  
  333.     glPopMatrix();
  334.  
  335.     glutSwapBuffers();
  336. }
  337.