/* * Copyright (c) 1996-1999 Silicon Graphics, Inc. All rights reserved. * * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. * * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL, * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ /* mouse.c - use the mouse to control the location of the viewpoint * * Escape key - exit the program * Left Mouse Button - change incidence and azimuth angles * Middle Mousebutton - change the twist angle based on * horizontal mouse movement * Right Mousebutton - zoom in and out based on vertical * mouse movement * R Key - reset viewpoint */ #include /* includes gl.h, glu.h */ #include #include /* for printf */ #include "axes.h" #include "shapes.h" /* Function Prototypes */ GLvoid initgfx( GLvoid ); GLvoid drawScene( GLvoid ); GLvoid reshape( GLsizei, GLsizei ); GLvoid keyboard( GLubyte, GLint, GLint ); GLvoid mouse( GLint, GLint, GLint, GLint ); GLvoid motion( GLint, GLint ); void resetView( GLvoid ); void polarView( GLfloat, GLfloat, GLfloat, GLfloat); void printHelp( char * ); /* Global Definitions */ #define KEY_ESC 27 /* ascii value for the escape key */ /* Global Variables */ enum actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE }; static GLint action; static GLdouble xStart = 0.0, yStart = 0.0; static GLfloat beamWidth = 2.0f, beamHeight = 0.4f, beamDepth = 1.0f; static GLfloat nearClip, farClip, distance, twistAngle, incAngle, azimAngle; void main( int argc, char *argv[] ) { GLsizei width, height; glutInit( &argc, argv ); width = glutGet( GLUT_SCREEN_WIDTH ); height = glutGet( GLUT_SCREEN_HEIGHT ); glutInitWindowPosition( (width / 2) + 4, height / 4 ); glutInitWindowSize( (width / 2) - 4, height / 2 ); glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE ); glutCreateWindow( argv[0] ); initgfx(); glutMouseFunc( mouse ); glutMotionFunc( motion ); glutKeyboardFunc( keyboard ); glutReshapeFunc( reshape ); glutDisplayFunc( drawScene ); printHelp( argv[0] ); glutMainLoop(); } void printHelp( char *progname ) { fprintf(stdout, "\n%s - use the mouse to control the viewpoint\n\n" "Axes: X - red, Y - green, Z - blue\n\n" "Left Mousebutton - move eye position\n" "Middle Mousebutton - change twist angle\n" "Right Mousebutton - move up / down to zoom in / out\n" " Key - reset viewpoint\n" "Escape Key - exit the program\n", progname); } GLvoid initgfx( GLvoid ) { GLfloat maxObjectSize; glClearColor( 0.0, 0.0, 0.0, 1.0 ); glShadeModel( GL_FLAT ); glEnable( GL_DEPTH_TEST ); /* Maximum size of all the objects in your scene */ maxObjectSize = sqrt( ((2*beamWidth) * (2*beamWidth)) + ((2*beamHeight) * (2*beamHeight)) + (beamDepth * beamDepth) ); /* Set up nearClip and farClip so that */ /* ( farClip - nearClip ) > maxObjectSize, */ /* and determine the viewing distance (adjust for zooming) */ nearClip = 1.0; farClip = nearClip + 8*maxObjectSize; resetView(); } GLvoid reshape( GLsizei width, GLsizei height ) { GLdouble aspect; glViewport( 0, 0, width, height ); aspect = (GLdouble) width / (GLdouble) height; glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 45.0, aspect, nearClip, farClip ); glMatrixMode( GL_MODELVIEW ); } GLvoid keyboard( GLubyte key, GLint x, GLint y ) { switch (key) { case 'R': resetView(); glutPostRedisplay(); break; case KEY_ESC: /* Exit whenever the Escape key is pressed */ exit(0); } } GLvoid mouse( GLint button, GLint state, GLint x, GLint y ) { static GLint buttons_down = 0; if (state == GLUT_DOWN) { /* most recent button down wins */ buttons_down++; switch (button) { case GLUT_LEFT_BUTTON: /* in case there are only two mouse * buttons, shift-left is equivalent to the * middle mouse button */ if (glutGetModifiers() == GLUT_ACTIVE_SHIFT) action = TWIST_EYE; else action = MOVE_EYE; break; case GLUT_MIDDLE_BUTTON: action = TWIST_EYE; break; case GLUT_RIGHT_BUTTON: action = ZOOM; break; } /* Update the saved mouse position */ xStart = x; yStart = y; } else { if (--buttons_down == 0) action = MOVE_NONE; /* no more buttons down */ } } GLvoid motion( GLint x, GLint y ) { switch (action) { case MOVE_EYE: /* Adjust the eye position based on the mouse position */ azimAngle += (GLdouble) (x - xStart); incAngle -= (GLdouble) (y - yStart); break; case TWIST_EYE: /* Adjust the eye twist based on the mouse position */ twistAngle = fmod(twistAngle+(x - xStart), 360.0); break; case ZOOM: /* Adjust the eye distance based on the mouse position */ distance -= (GLdouble) (y - yStart)/10.0; break; default: printf("unknown action %d\n", action); } /* Update the stored mouse position for later use */ xStart = x; yStart = y; glutPostRedisplay(); } void resetView( GLvoid ) { distance = nearClip + (farClip - nearClip) / 2.0; twistAngle = 0.0; /* rotation of viewing volume (camera) */ incAngle = 0.0; azimAngle = 0.0; } void polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence, GLfloat twist) { glTranslatef( 0.0f, 0.0f, -distance); glRotatef( -twist, 0.0f, 0.0f, 1.0f); glRotatef( -incidence, 1.0f, 0.0f, 0.0f); glRotatef( -azimuth, 0.0f, 0.0f, 1.0f); } GLvoid drawScene( GLvoid ) { static GLfloat upperArmColor[] = { 1.0f, 0.0f, 0.0f }; static GLfloat lowerArmColor[] = { 0.8f, 0.5f, 0.5f }; glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glPushMatrix(); polarView( distance, azimAngle, incAngle, twistAngle ); XYZaxes(); /* Draw the shoulder centered at the new origin */ glTranslatef( 1.0f, 0.0f, 0.0f ); glColor3fv( upperArmColor ); WireBox( beamWidth, beamHeight, beamDepth ); /* Draw the lower arm at the end of the upper arm and * rotate it 45 degrees */ glTranslatef( 1.0f, 0.0f, 0.0f ); glRotatef( 45.0f, 0.0f, 0.0f, 1.0f ); glTranslatef( 1.0f, 0.0f, 0.0f ); glColor3fv( lowerArmColor ); WireBox( beamWidth, beamHeight, beamDepth ); glPopMatrix(); glutSwapBuffers(); }