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 / text / text3D.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  9.5 KB  |  408 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. /* text3D.c - show how to render moving and stationary text in 
  19.  *        3D scenes
  20.  *
  21.  *  Escape key              - exit the program 
  22.  *  Left Mouse Button        - change incidence and azimuth angles
  23.  *  Middle Mousebutton        - change the twist angle based on
  24.  *                  horizontal mouse movement
  25.  *  Right Mousebutton        - zoom in and out based on vertical
  26.  *                  mouse movement
  27.  *  R Key            - reset viewpoint
  28.  */
  29. #include <GL/gl.h>
  30. #include <GL/glu.h>
  31. #include <GL/glut.h>
  32.  
  33. #include <math.h>    /* for fmodf */
  34. #include <stdio.h>    /* for printf */
  35.  
  36. /*  Function Prototypes  */
  37.  
  38. GLvoid  initgfx( GLvoid );
  39. GLvoid  animate( GLvoid );
  40. GLvoid  visibility( GLint );
  41. GLvoid  drawScene( GLvoid );
  42. GLvoid  reshape( GLsizei, GLsizei );
  43. GLvoid  keyboard( GLubyte, GLint, GLint );
  44. GLvoid  mouse( GLint, GLint, GLint, GLint );
  45. GLvoid  motion( GLint, GLint );
  46.  
  47. void resetView( GLvoid );
  48. void polarView( GLfloat, GLfloat, GLfloat, GLfloat);
  49. void printHelp( char * );
  50.  
  51. /* Global Definitions */
  52.  
  53. #define KEY_ESC    27    /* ascii value for the escape key */
  54.  
  55. /* Global Variables */
  56.  
  57. static GLfloat        shoulderAngle = 0.0;    /* controls shoulder rotation */
  58.  
  59. static enum        actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
  60. static GLint        action;
  61.  
  62. static GLdouble        xStart = 0.0, yStart = 0.0;
  63.  
  64. static GLfloat        beamWidth = 2.0, beamHeight = 0.4, beamDepth = 1.0;
  65. static GLfloat         near, far, distance, twistAngle, incAngle, azimAngle;
  66.  
  67. static void           *fixedFont, *strokeFont;
  68. static char            *labelStr = "Robot Arm";
  69.  
  70. void
  71. main( int argc, char *argv[] )
  72. {
  73.     GLsizei width, height;
  74.  
  75.     glutInit( &argc, argv );
  76.  
  77.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  78.     height = glutGet( GLUT_SCREEN_HEIGHT );
  79.     glutInitWindowPosition( (width / 2) + 4, height / 4 );
  80.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  81.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  82.     glutCreateWindow( argv[0] );
  83.  
  84.     initgfx();
  85.  
  86.     glutMouseFunc( mouse );
  87.     glutMotionFunc( motion );
  88.     glutKeyboardFunc( keyboard );
  89.     glutIdleFunc( animate );
  90.     glutVisibilityFunc( visibility );
  91.     glutReshapeFunc( reshape );
  92.     glutDisplayFunc( drawScene ); 
  93.  
  94.     printHelp( argv[0] );
  95.  
  96.     glutMainLoop();
  97. }
  98.  
  99. void
  100. printHelp( char *progname )
  101. {
  102.     fprintf(stdout, "\n%s - render a scene with both stationary "
  103.         "and moving text\n\n"
  104.         "Axes: X - red, Y - green, Z - blue\n\n"
  105.         "Left Mousebutton    - move eye position\n"
  106.         "Middle Mousebutton    - change twist angle\n"
  107.         "Right Mousebutton    - move up / down to zoom in / out\n"
  108.         "Escape Key        - exit the program\n"
  109.         "<R> Key        - reset viewpoint\n",
  110.         progname);
  111. }
  112.  
  113. GLvoid
  114. initgfx( GLvoid )
  115. {
  116.     GLfloat maxObjectSize;
  117.  
  118.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  119.     glShadeModel( GL_FLAT );
  120.     glEnable( GL_DEPTH_TEST );
  121.  
  122.     /* Maximum size of all the objects in your scene */
  123.     maxObjectSize = fsqrt( ((2*beamWidth) * (2*beamWidth)) +
  124.                 ((2*beamHeight) * (2*beamHeight)) +
  125.                 (beamDepth * beamDepth) );
  126.  
  127.     /* Set up near and far so that ( far - near ) > maxObjectSize, */
  128.     /* and determine the viewing distance (adjust for zooming) */
  129.     near = 1.0;
  130.     far = near + 8*maxObjectSize; 
  131.  
  132.     resetView();
  133.  
  134.     /* Set up two fonts to use for rendering */
  135.     fixedFont = GLUT_BITMAP_TIMES_ROMAN_24;
  136.     strokeFont = GLUT_STROKE_ROMAN;
  137. }
  138.  
  139. GLvoid
  140. reshape( GLsizei width, GLsizei height )
  141. {
  142.     GLdouble            aspect;
  143.  
  144.     glViewport( 0, 0, width, height );
  145.  
  146.     aspect = (GLdouble) width / (GLdouble) height;
  147.  
  148.     glMatrixMode( GL_PROJECTION );
  149.     glLoadIdentity();
  150.     gluPerspective( 45.0, aspect, near, far );
  151.     glMatrixMode( GL_MODELVIEW );
  152. }
  153.  
  154. GLvoid 
  155. animate( GLvoid )
  156. {
  157.     /* update the rotation of the shoulder for each scene */
  158.     shoulderAngle = fmodf( (shoulderAngle + 1.0), 360.0 );
  159.  
  160.     /* Tell GLUT to redraw the scene */
  161.     glutPostRedisplay();
  162. }
  163.  
  164. GLvoid
  165. visibility( int state ) 
  166. {
  167.     if (state == GLUT_VISIBLE) {
  168.         glutIdleFunc( animate );
  169.     } else {
  170.         glutIdleFunc( NULL );
  171.     }
  172. }
  173.  
  174. GLvoid 
  175. keyboard( GLubyte key, GLint x, GLint y )
  176. {
  177.     switch (key) {
  178.     case 'R':
  179.         resetView();
  180.         glutPostRedisplay();
  181.         break;
  182.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  183.         exit(0);
  184.     }
  185. }
  186.  
  187. GLvoid 
  188. mouse( GLint button, GLint state, GLint x, GLint y )
  189. {
  190.     static GLint buttons_down = 0;
  191.  
  192.     if (state == GLUT_DOWN) {
  193.         switch (button) {
  194.         case GLUT_LEFT_BUTTON:
  195.             action = MOVE_EYE;
  196.             break;
  197.         case GLUT_MIDDLE_BUTTON:
  198.             action = TWIST_EYE;
  199.             break;
  200.         case GLUT_RIGHT_BUTTON:
  201.             action = ZOOM;
  202.             break;
  203.         }
  204.  
  205.         /* Update the saved mouse position */
  206.         xStart = x;
  207.         yStart = y;
  208.     } else {
  209.         if (--buttons_down == 0) 
  210.             action = MOVE_NONE;
  211.     }
  212.  
  213. }
  214.  
  215. GLvoid
  216. motion( GLint x, GLint y )
  217. {
  218.     switch (action) {
  219.     case MOVE_EYE:
  220.         /* Adjust the eye position based on the mouse position */
  221.         azimAngle += (GLdouble) (x - xStart);
  222.         incAngle -= (GLdouble) (y - yStart);
  223.         break;
  224.     case TWIST_EYE:
  225.         /* Adjust the eye twist based on the mouse position */
  226.         twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
  227.         break;
  228.     case ZOOM:
  229.         /* Adjust the eye distance based on the mouse position */
  230.         distance -= (GLdouble) (y - yStart)/10.0;
  231.         break;
  232.     default:
  233.         printf("unknown action %d\n", action);
  234.     }
  235.     
  236.     /* Update the stored mouse position for later use */
  237.     xStart = x;
  238.     yStart = y;
  239.  
  240.     glutPostRedisplay();
  241. }
  242.  
  243. void
  244. resetView( GLvoid )
  245. {
  246.     distance = near + (far - near) / 2.0;
  247.     twistAngle = 0.0;    /* rotation of viewing volume (camera) */
  248.     incAngle = 0.0;
  249.     azimAngle = 0.0;
  250. }
  251.  
  252. void
  253. polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
  254.             GLfloat twist)
  255. {
  256.     glTranslatef( 0.0, 0.0, -distance);
  257.     glRotatef( -twist, 0.0, 0.0, 1.0);
  258.     glRotatef( -incidence, 1.0, 0.0, 0.0);
  259.     glRotatef( -azimuth, 0.0, 0.0, 1.0);
  260. }
  261.  
  262. GLvoid
  263. renderBitmapString( void *font, char *string )
  264. {
  265.         int i;
  266.         int len = (int) strlen(string);
  267.         for (i = 0; i < len; i++) {
  268.                 glutBitmapCharacter(font, string[i]);
  269.         }
  270. }
  271.  
  272. GLvoid
  273. renderStrokeString( void *font, char *string )
  274. {
  275.         int i;
  276.         int len = (int) strlen(string);
  277.         for (i = 0; i < len; i++) {
  278.                 glutStrokeCharacter(font, string[i]);
  279.         }
  280. }
  281.  
  282. /* print mouse and keyboard input commands */
  283. GLvoid
  284. printInputCommands( void *font )
  285. {
  286.     static char *lmouse = "left mouse - change incidence & azimuth";
  287.     static char *mmouse = "middle mouse - change twist";
  288.     static char *rmouse = "right mouse - zoom in and out";
  289.     static char *Rkey = "R key - reset view";
  290.  
  291.     GLfloat yoffset;
  292.     GLsizei winHeight = glutGet(GLUT_WINDOW_HEIGHT);
  293.  
  294.     yoffset = winHeight-15.0;
  295.     glRasterPos3f( 10.0, yoffset, 0.0 );
  296.     renderBitmapString( font, lmouse );
  297.  
  298.     yoffset -= 20.0;
  299.     glRasterPos3f( 10.0, yoffset, 0.0 );
  300.     renderBitmapString( font, mmouse );
  301.  
  302.     yoffset -= 20.0;
  303.     glRasterPos3f( 10.0, yoffset, 0.0 );
  304.     renderBitmapString( font, rmouse );
  305.  
  306.     yoffset -= 20.0;
  307.     glRasterPos3f( 10.0, yoffset, 0.0 );
  308.     renderBitmapString( font, Rkey );
  309. }
  310.  
  311. /* update status of polarView parameters */
  312. GLvoid
  313. printStatus( void *font ) 
  314. {
  315.     static char s[50];
  316.     GLfloat yoffset = 65.0;
  317.  
  318.     /* update polarview information */
  319.     glRasterPos3f( 10.0, yoffset, 0.0 ); 
  320.     sprintf (s, "incidence is %4d", (GLint) incAngle);
  321.     renderBitmapString( font, s );
  322.  
  323.     yoffset -= 20.0;
  324.     glRasterPos3f( 10.0, yoffset, 0.0 );
  325.     sprintf (s, "azimuth is %4d", (GLint) azimAngle);
  326.     renderBitmapString( font, s );
  327.  
  328.     yoffset -= 20.0;
  329.     glRasterPos3f( 10.0, yoffset, 0.0 );
  330.     sprintf (s, "Twist is %4d", (GLint) twistAngle);
  331.     renderBitmapString( font, s );
  332. }
  333.  
  334. GLvoid
  335. renderStationaryText( void *font )
  336. {
  337.     GLsizei winWidth, winHeight;
  338.  
  339.     winWidth = glutGet(GLUT_WINDOW_WIDTH); 
  340.     winHeight = glutGet(GLUT_WINDOW_HEIGHT);
  341.  
  342.     /* Turn off the depth buffer */
  343.     glDisable( GL_DEPTH_TEST );
  344.  
  345.     /* save and then clear modelview matrix */
  346.     glPushMatrix(); 
  347.     glLoadIdentity(); 
  348.  
  349.     /* save Projection matrix */
  350.     glMatrixMode( GL_PROJECTION );
  351.     glPushMatrix();
  352.         /* reset world space to display status */
  353.         glLoadIdentity();
  354.         glOrtho( 0.0, winWidth, 0.0, winHeight, -1.0, 1.0 );
  355.  
  356.         printInputCommands( font );
  357.         printStatus( font );
  358.  
  359.     /* restore projection matrix */
  360.     glPopMatrix();
  361.  
  362.     /* restore previous modelview transformations */
  363.     glMatrixMode( GL_MODELVIEW );
  364.     glPopMatrix();
  365.  
  366.     /* Turn on the depth buffer */
  367.     glEnable( GL_DEPTH_TEST );
  368. }
  369.  
  370. GLvoid
  371. drawScene( GLvoid )
  372. {
  373.     static GLfloat        upperArmColor[] = { 1.0, 0.0, 0.0 };
  374.     static GLfloat        lowerArmColor[] = { 0.8, 0.5, 0.5 };
  375.     
  376.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  377.  
  378.     glPushMatrix();
  379.         polarView( distance, azimAngle, incAngle, twistAngle );
  380.         XYZaxes();
  381.  
  382.         /* Draw the shoulder centered at the new origin */
  383.         glRotatef( shoulderAngle, 0.0, 0.0, 1.0 );
  384.         glTranslatef( 1.0, 0.0, 0.0 ); 
  385.             glColor3fv( upperArmColor );
  386.         WireBox( beamWidth, beamHeight, beamDepth );
  387.  
  388.         /* Draw the lower arm */
  389.         glTranslatef( 1.0, 0.0, 0.0 );
  390.         glRotatef( 45.0, 0.0, 0.0, 1.0 );
  391.         glTranslatef( 1.0, 0.0, 0.0 );
  392.             glColor3fv( lowerArmColor );
  393.         WireBox( beamWidth, beamHeight, beamDepth );
  394.  
  395.         glColor3f( 1.0, 1.0, 1.0 );
  396.  
  397.         glTranslatef( 1.5, 1.8, 0.0 );
  398.         glRotatef( -90.0, 0.0, 0.0, 1.0 );
  399.         glScalef( 0.005, 0.005, 0.005 );
  400.         renderStrokeString( strokeFont, labelStr );
  401.     glPopMatrix();
  402.  
  403.     glColor3f( 1.0, 1.0, 0.0 );
  404.     renderStationaryText( fixedFont );
  405.  
  406.     glutSwapBuffers();
  407. }
  408.