/* * 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. */ /* lookat.c - use input from the keyboard to control the viewpoint * * F1 key - print help information * Left Arrow Key - move the reference point to the left * Right Arrow Key - move the reference point to the right * Up Arrow Key - move the reference point up * Down Arrow Key - move the reference point down * Escape key - exit the program */ #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 specialkeys( GLint, GLint, GLint ); void printHelp( char * ); /* Global Definitions */ #define KEY_ESC 27 /* ascii value for the escape key */ /* Global Variables */ static GLdouble xRef = 0.0, yRef = 0.0; static char *progname; GLvoid main( int argc, char *argv[] ) { GLsizei width, height; glutInit( &argc, argv ); width = glutGet( GLUT_SCREEN_WIDTH ); height = glutGet( GLUT_SCREEN_HEIGHT ); glutInitWindowPosition( 4, height / 4 ); glutInitWindowSize( (width / 2) - 4, height / 2 ); glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE ); glutCreateWindow( argv[0] ); initgfx(); glutKeyboardFunc( keyboard ); glutSpecialFunc( specialkeys ); glutReshapeFunc( reshape ); glutDisplayFunc( drawScene ); progname = argv[0]; printHelp( progname ); glutMainLoop(); } void printHelp( char *progname ) { fprintf(stdout, "\n%s - uses keyboard input to control the " "location of the reference point point\n\n" "F1 Key - print Help information\n" "Left Arrow Key - move reference point to the left\n" "Right Arrow Key - move reference point to the right\n" "Up Arrow Key - move reference point up\n" "Down Arrow Key - move reference point down\n" "Escape Key - exit the program\n\n", progname); } GLvoid initgfx( GLvoid ) { glClearColor( 0.0, 0.0, 0.0, 1.0 ); glShadeModel( GL_FLAT ); glEnable( GL_DEPTH_TEST ); } 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, 1.0, 20.0 ); glMatrixMode( GL_MODELVIEW ); } GLvoid keyboard( GLubyte key, GLint x, GLint y ) { switch (key) { case KEY_ESC: /* Exit when the Escape key is pressed */ exit(0); } } GLvoid specialkeys( GLint key, GLint x, GLint y ) { switch (key) { case GLUT_KEY_F1: /* print Help */ printHelp( progname ); break; case GLUT_KEY_LEFT: /* move reference point to the left */ xRef -= 0.5; if (xRef < -4.0) xRef = -4.0; glutPostRedisplay(); break; case GLUT_KEY_RIGHT: /* move reference point to the right */ xRef += 0.5; if (xRef > 4.0) xRef = 4.0; glutPostRedisplay(); break; case GLUT_KEY_UP: /* move reference point up */ yRef += 0.5; if (yRef > 3.0) yRef = 3.0; glutPostRedisplay(); break; case GLUT_KEY_DOWN: /* move reference point down */ yRef -= 0.5; if (yRef < -3.0) yRef = -3.0; glutPostRedisplay(); break; } } 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(); /* Move the reference point */ gluLookAt( 0.0, 0.0, 8.0, xRef, yRef, 0.0, 0.0, 1.0, 0.0 ); XYaxes(); /* Draw the shoulder at the new origin */ glTranslatef( 1.0f, 0.0f, 0.0f ); glColor3fv( upperArmColor ); WireBox( 2.0, 0.4, 1.0 ); /* 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( 2.0, 0.4, 1.0 ); glPopMatrix(); glutSwapBuffers(); }