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 / lookat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.5 KB  |  192 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. /* lookat.c - use input from the keyboard to control the viewpoint
  19.  *
  20.  *    F1 key        - print help information
  21.  *    Left Arrow Key    - move the reference point to the left
  22.  *    Right Arrow Key    - move the reference point to the right
  23.  *    Up Arrow Key    - move the reference point up
  24.  *    Down Arrow Key    - move the reference point down
  25.  *    Escape key     - exit the program
  26.  */
  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  specialkeys( GLint, GLint, GLint );
  41.  
  42. void printHelp( char * );
  43.  
  44. /* Global Definitions */
  45.  
  46. #define KEY_ESC    27    /* ascii value for the escape key */
  47.  
  48. /* Global Variables */
  49.  
  50. static GLdouble        xRef = 0.0, yRef = 0.0;
  51.  
  52. static char *progname;
  53.  
  54. GLvoid
  55. main( int argc, char *argv[] )
  56. {
  57.     GLsizei width, height;
  58.  
  59.     glutInit( &argc, argv );
  60.  
  61.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  62.     height = glutGet( GLUT_SCREEN_HEIGHT );
  63.     glutInitWindowPosition( 4, height / 4 );
  64.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  65.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  66.     glutCreateWindow( argv[0] );
  67.  
  68.     initgfx();
  69.  
  70.     glutKeyboardFunc( keyboard );
  71.     glutSpecialFunc( specialkeys );
  72.     glutReshapeFunc( reshape );
  73.     glutDisplayFunc( drawScene ); 
  74.  
  75.     progname = argv[0];
  76.     printHelp( progname );
  77.  
  78.     glutMainLoop();
  79. }
  80.  
  81. void
  82. printHelp( char *progname )
  83. {
  84.     fprintf(stdout, "\n%s - uses keyboard input to control the "
  85.         "location of the reference point point\n\n"
  86.         "F1 Key            - print Help information\n"
  87.         "Left Arrow Key        - move reference point to the left\n"
  88.         "Right Arrow Key    - move reference point to the right\n"
  89.         "Up Arrow Key        - move reference point up\n"
  90.         "Down Arrow Key        - move reference point down\n"
  91.         "Escape Key        - exit the program\n\n",
  92.         progname);
  93. }
  94.  
  95. GLvoid
  96. initgfx( GLvoid )
  97. {
  98.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  99.     glShadeModel( GL_FLAT );
  100.     glEnable( GL_DEPTH_TEST );
  101. }
  102.  
  103. GLvoid
  104. reshape( GLsizei width, GLsizei height )
  105. {
  106.     GLdouble    aspect;
  107.  
  108.     glViewport( 0, 0, width, height );
  109.  
  110.     aspect = (GLdouble) width / (GLdouble) height;
  111.  
  112.     glMatrixMode( GL_PROJECTION );
  113.     glLoadIdentity();
  114.     gluPerspective( 45.0, aspect, 1.0, 20.0 );
  115.     glMatrixMode( GL_MODELVIEW );
  116. }
  117.  
  118. GLvoid 
  119. keyboard( GLubyte key, GLint x, GLint y )
  120. {
  121.     switch (key) {
  122.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  123.         exit(0);
  124.     }
  125. }
  126.  
  127. GLvoid 
  128. specialkeys( GLint key, GLint x, GLint y )
  129. {
  130.     switch (key) {
  131.     case GLUT_KEY_F1:    /* print Help */
  132.         printHelp( progname );
  133.         break;
  134.  
  135.     case GLUT_KEY_LEFT:    /* move reference point to the left */
  136.         xRef -= 0.5;
  137.         if (xRef < -4.0) xRef = -4.0;
  138.         glutPostRedisplay();
  139.          break;
  140.  
  141.     case GLUT_KEY_RIGHT:    /* move reference point to the right */
  142.         xRef += 0.5;
  143.         if (xRef > 4.0) xRef = 4.0;
  144.         glutPostRedisplay();
  145.         break;
  146.  
  147.     case GLUT_KEY_UP:    /* move reference point up */
  148.         yRef += 0.5;
  149.         if (yRef > 3.0) yRef = 3.0;
  150.         glutPostRedisplay();
  151.         break;
  152.  
  153.     case GLUT_KEY_DOWN:    /* move reference point down */
  154.         yRef -= 0.5;
  155.         if (yRef < -3.0) yRef = -3.0;
  156.         glutPostRedisplay();
  157.         break;
  158.     }
  159. }
  160.  
  161. GLvoid
  162. drawScene( GLvoid )
  163. {
  164.     static GLfloat    upperArmColor[] = { 1.0, 0.0, 0.0 };
  165.     static GLfloat    lowerArmColor[] = { 0.8, 0.5, 0.5 };
  166.  
  167.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  168.  
  169.     glPushMatrix();
  170.  
  171.         /* Move the reference point */
  172.         gluLookAt( 0.0, 0.0, 8.0, xRef, yRef, 0.0, 0.0, 1.0, 0.0 );
  173.  
  174.         XYaxes();
  175.  
  176.         /* Draw the shoulder at the new origin */
  177.         glTranslatef( 1.0, 0.0, 0.0 ); 
  178.             glColor3fv( upperArmColor );
  179.         WireBox( 2.0, 0.4, 1.0 );
  180.  
  181.         /* Draw the lower arm at the end of the upper arm and 
  182.           * rotate it 45 degrees */
  183.         glTranslatef( 1.0, 0.0, 0.0 );
  184.         glRotatef( 45.0, 0.0, 0.0, 1.0 );
  185.         glTranslatef( 1.0, 0.0, 0.0 );
  186.             glColor3fv( lowerArmColor );
  187.         WireBox( 2.0, 0.4, 1.0 );
  188.     glPopMatrix();
  189.  
  190.     glutSwapBuffers();
  191. }
  192.