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 / viewing / mouse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  6.1 KB  |  255 lines

  1. /*
  2.  * Copyright 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. /* mouse.c - use the mouse to control the location of the viewpoint
  19.  *
  20.  *  Escape key              - exit the program 
  21.  *  Left Mouse Button        - change incidence and azimuth angles
  22.  *  Middle Mousebutton        - change the twist angle based on
  23.  *                  horizontal mouse movement
  24.  *  Right Mousebutton        - zoom in and out based on vertical
  25.  *                  mouse movement
  26.  *  R Key            - reset viewpoint
  27.  */
  28. #include <GL/gl.h>
  29. #include <GL/glu.h>
  30. #include <GL/glut.h>
  31. #include <math.h>
  32. #include <stdio.h>    /* for printf */
  33.  
  34. /*  Function Prototypes  */
  35.  
  36. GLvoid  initgfx( GLvoid );
  37. GLvoid  drawScene( GLvoid );
  38. GLvoid  reshape( GLsizei, GLsizei );
  39. GLvoid  keyboard( GLubyte, GLint, GLint );
  40. GLvoid  mouse( GLint, GLint, GLint, GLint );
  41. GLvoid  motion( GLint, GLint );
  42.  
  43. void resetView( GLvoid );
  44. void polarView( GLfloat, GLfloat, GLfloat, GLfloat);
  45.  
  46. void printHelp( char * );
  47.  
  48. /* Global Definitions */
  49.  
  50. #define KEY_ESC    27    /* ascii value for the escape key */
  51.  
  52. /* Global Variables */
  53.  
  54. static enum        actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
  55. static GLint        action;
  56.  
  57. static GLdouble        xStart = 0.0, yStart = 0.0;
  58.  
  59. static GLfloat        beamWidth = 2.0, beamHeight = 0.4, beamDepth = 1.0;
  60. static GLfloat         near, far, distance, twistAngle, incAngle, azimAngle;
  61.  
  62. void
  63. main( int argc, char *argv[] )
  64. {
  65.     GLsizei width, height;
  66.  
  67.     glutInit( &argc, argv );
  68.  
  69.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  70.     height = glutGet( GLUT_SCREEN_HEIGHT );
  71.     glutInitWindowPosition( (width / 2) + 4, height / 4 );
  72.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  73.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  74.     glutCreateWindow( argv[0] );
  75.  
  76.     initgfx();
  77.  
  78.     glutMouseFunc( mouse );
  79.     glutMotionFunc( motion );
  80.     glutKeyboardFunc( keyboard );
  81.     glutReshapeFunc( reshape );
  82.     glutDisplayFunc( drawScene ); 
  83.  
  84.     printHelp( argv[0] );
  85.  
  86.     glutMainLoop();
  87. }
  88.  
  89. void
  90. printHelp( char *progname )
  91. {
  92.     fprintf(stdout, "\n%s - use the mouse to control the viewpoint\n\n"
  93.         "Axes: X - red, Y - green, Z - blue\n\n"
  94.         "Left Mousebutton    - move eye position\n"
  95.         "Middle Mousebutton    - change twist angle\n"
  96.         "Right Mousebutton    - move up / down to zoom in / out\n"
  97.         "<R> Key        - reset viewpoint\n"
  98.         "Escape Key        - exit the program\n",
  99.         progname);
  100. }
  101.  
  102. GLvoid
  103. initgfx( GLvoid )
  104. {
  105.     GLfloat maxObjectSize;
  106.  
  107.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  108.     glShadeModel( GL_FLAT );
  109.     glEnable( GL_DEPTH_TEST );
  110.  
  111.     /* Maximum size of all the objects in your scene */
  112.     maxObjectSize = fsqrt( ((2*beamWidth) * (2*beamWidth)) +
  113.                 ((2*beamHeight) * (2*beamHeight)) +
  114.                 (beamDepth * beamDepth) );
  115.  
  116.     /* Set up near and far so that ( far - near ) > maxObjectSize, */
  117.     /* and determine the viewing distance (adjust for zooming) */
  118.     near = 1.0;
  119.     far = near + 8*maxObjectSize; 
  120.  
  121.     resetView();
  122. }
  123.  
  124. GLvoid
  125. reshape( GLsizei width, GLsizei height )
  126. {
  127.     GLdouble            aspect;
  128.  
  129.     glViewport( 0, 0, width, height );
  130.  
  131.     aspect = (GLdouble) width / (GLdouble) height;
  132.  
  133.     glMatrixMode( GL_PROJECTION );
  134.     glLoadIdentity();
  135.     gluPerspective( 45.0, aspect, near, far );
  136.     glMatrixMode( GL_MODELVIEW );
  137. }
  138.  
  139. GLvoid 
  140. keyboard( GLubyte key, GLint x, GLint y )
  141. {
  142.     switch (key) {
  143.     case 'R':
  144.         resetView();
  145.         glutPostRedisplay();
  146.         break;
  147.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  148.         exit(0);
  149.     }
  150. }
  151.  
  152. GLvoid 
  153. mouse( GLint button, GLint state, GLint x, GLint y )
  154. {
  155.     static GLint buttons_down = 0;
  156.  
  157.     if (state == GLUT_DOWN) {    /* most recent button down wins */
  158.         buttons_down++;
  159.         switch (button) {
  160.         case GLUT_LEFT_BUTTON:
  161.             action = MOVE_EYE;
  162.             break;
  163.         case GLUT_MIDDLE_BUTTON:
  164.             action = TWIST_EYE;
  165.             break;
  166.         case GLUT_RIGHT_BUTTON:
  167.             action = ZOOM;
  168.             break;
  169.         }
  170.  
  171.         /* Update the saved mouse position */
  172.         xStart = x;
  173.         yStart = y;
  174.     } else {
  175.         if (--buttons_down == 0)
  176.             action = MOVE_NONE;    /* no more buttons down */
  177.     }
  178. }
  179.  
  180. GLvoid
  181. motion( GLint x, GLint y )
  182. {
  183.     switch (action) {
  184.     case MOVE_EYE:
  185.         /* Adjust the eye position based on the mouse position */
  186.         azimAngle += (GLdouble) (x - xStart);
  187.         incAngle -= (GLdouble) (y - yStart);
  188.         break;
  189.     case TWIST_EYE:
  190.         /* Adjust the eye twist based on the mouse position */
  191.         twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
  192.         break;
  193.     case ZOOM:
  194.         /* Adjust the eye distance based on the mouse position */
  195.         distance -= (GLdouble) (y - yStart)/10.0;
  196.         break;
  197.     default:
  198.         printf("unknown action %d\n", action);
  199.     }
  200.     
  201.     /* Update the stored mouse position for later use */
  202.     xStart = x;
  203.     yStart = y;
  204.  
  205.     glutPostRedisplay();
  206. }
  207.  
  208. void
  209. resetView( GLvoid )
  210. {
  211.     distance = near + (far - near) / 2.0;
  212.     twistAngle = 0.0;    /* rotation of viewing volume (camera) */
  213.     incAngle = 0.0;
  214.     azimAngle = 0.0;
  215. }
  216.  
  217. void
  218. polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
  219.             GLfloat twist)
  220. {
  221.     glTranslatef( 0.0, 0.0, -distance);
  222.     glRotatef( -twist, 0.0, 0.0, 1.0);
  223.     glRotatef( -incidence, 1.0, 0.0, 0.0);
  224.     glRotatef( -azimuth, 0.0, 0.0, 1.0);
  225. }
  226.  
  227. GLvoid
  228. drawScene( GLvoid )
  229. {
  230.     static GLfloat        upperArmColor[] = { 1.0, 0.0, 0.0 };
  231.     static GLfloat        lowerArmColor[] = { 0.8, 0.5, 0.5 };
  232.     
  233.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  234.  
  235.     glPushMatrix();
  236.         polarView( distance, azimAngle, incAngle, twistAngle );
  237.         XYZaxes();
  238.  
  239.         /* Draw the shoulder centered at the new origin */
  240.         glTranslatef( 1.0, 0.0, 0.0 ); 
  241.             glColor3fv( upperArmColor );
  242.         WireBox( beamWidth, beamHeight, beamDepth );
  243.  
  244.         /* Draw the lower arm at the end of the upper arm and 
  245.           * rotate it 45 degrees */
  246.         glTranslatef( 1.0, 0.0, 0.0 );
  247.         glRotatef( 45.0, 0.0, 0.0, 1.0 );
  248.         glTranslatef( 1.0, 0.0, 0.0 );
  249.             glColor3fv( lowerArmColor );
  250.         WireBox( beamWidth, beamHeight, beamDepth );
  251.     glPopMatrix();
  252.  
  253.     glutSwapBuffers();
  254. }
  255.