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 / modeling / order.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  3.1 KB  |  140 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. /* order.c - illustrate what happens when the order
  19.  *      of the modeling transformations is
  20.  *
  21.  *            glTranslate( ... );
  22.  *            glRotate( ... );
  23.  *     versus 
  24.  *            glRotate( ... );
  25.  *            glTranslate( ... );
  26.  *
  27.  *
  28.  *    <o> key        - toggle order of transformations
  29.  *    Escape key    - exit program
  30.  */
  31. #include <GL/gl.h>
  32. #include <GL/glu.h>
  33. #include <GL/glut.h>
  34.  
  35. #include <stdio.h>
  36.  
  37. /*  Function Prototypes  */
  38.  
  39. GLvoid  initgfx( GLvoid );
  40. GLvoid  keyboard( GLubyte, GLint, GLint );
  41. GLvoid  drawScene( GLvoid );
  42.  
  43. void printHelp( char * );
  44.  
  45. /* Global Definitions */
  46.  
  47. #define KEY_ESC    27    /* ascii value for the escape key */
  48.  
  49. /* Global Variables */
  50.  
  51. static GLboolean rotateFirst = GL_TRUE;
  52.  
  53. void
  54. main( int argc, char *argv[] )
  55. {
  56.     GLsizei width, height;
  57.  
  58.     glutInit( &argc, argv );
  59.  
  60.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  61.     height = glutGet( GLUT_SCREEN_HEIGHT );
  62.     glutInitWindowPosition( width / 4, height / 4 );
  63.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  64.     glutInitDisplayMode( GLUT_RGBA );
  65.     glutCreateWindow( argv[0] );
  66.  
  67.     initgfx();
  68.  
  69.     glutKeyboardFunc( keyboard );
  70.     glutDisplayFunc( drawScene ); 
  71.  
  72.     printHelp( argv[0] );
  73.  
  74.     glMatrixMode( GL_PROJECTION );
  75.     glOrtho( -2.0, 2.0, -2.0, 2.0, -1.0, 1.0 );
  76.     glMatrixMode( GL_MODELVIEW );
  77.  
  78.     glutMainLoop();
  79. }
  80.  
  81. void
  82. printHelp( char *progname )
  83. {
  84.     fprintf(stdout, 
  85.         "\n%s - demonstrates the effect of transformation order\n\n"
  86.         "<o> key        - toggle order of transformations\n"
  87.         "Escape Key        - exit the program\n\n",
  88.         progname);
  89.  
  90.     fprintf(stdout, "order is Rotate-Translate\n");
  91. }
  92.  
  93. GLvoid
  94. initgfx( GLvoid )
  95. {
  96.     glClearColor( 0.0, 0.0, 1.0, 1.0 );
  97.     glShadeModel( GL_FLAT );
  98. }
  99.  
  100. GLvoid 
  101. keyboard( GLubyte key, GLint x, GLint y )
  102. {
  103.     switch (key) {
  104.     case 'o':    /* toggle transformation order */
  105.         rotateFirst = !rotateFirst;
  106.         fprintf(stdout, "order is %s\n",
  107.             (rotateFirst?"Rotate-Translate":"Translate-Rotate"));
  108.         glutPostRedisplay();
  109.         break;
  110.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  111.         exit(0);
  112.     }
  113. }
  114.  
  115. GLvoid
  116. drawScene( GLvoid )
  117. {
  118.     glClear( GL_COLOR_BUFFER_BIT );
  119.  
  120.     glPushMatrix();
  121.         if ( rotateFirst ) {
  122.             /* Do a rotation first, then a translation */
  123.             glRotatef( 60.0, 0.0, 0.0, 1.0 );
  124.             glTranslatef( 1.0, 0.0, 0.0 );
  125.         } else {
  126.             /* Do a translation first, then a rotation */
  127.             glTranslatef( 1.0, 0.0, 0.0 );
  128.             glRotatef( 60.0, 0.0, 0.0, 1.0 );
  129.         }
  130.  
  131.         Checkerboard();
  132.  
  133.         /* Draw X-Y axis -- X is red, Y is green */
  134.         XYaxes();
  135.  
  136.     glPopMatrix();
  137.  
  138.     glFlush();
  139. }
  140.