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 / transformations / matrixmodes.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  3.4 KB  |  148 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. /* matrixmodes.c - draw a triangle under the effect of a rotation, 
  19.  *        a translation and a scale; use the matrix stack 
  20.  *        to ensure that the scene is properly redrawn.
  21.  *
  22.  *    Escape Key    - exit program
  23.  */
  24. #include <GL/gl.h>
  25. #include <GL/glu.h>
  26. #include <GL/glut.h>
  27.  
  28. #include <stdio.h>
  29.  
  30. /*  Function Prototypes  */
  31.  
  32. GLvoid  initgfx( GLvoid );
  33. GLvoid  keyboard( GLubyte, GLint, GLint );
  34. GLvoid  drawScene( GLvoid );
  35.  
  36. void drawTriangle( GLfloat red, GLfloat green, GLfloat blue );
  37.  
  38. void printHelp( char * );
  39.  
  40. /* Global Definitions */
  41.  
  42. #define KEY_ESC    27    /* ascii value for the escape key */
  43.  
  44. void
  45. main( int argc, char *argv[] )
  46. {
  47.     GLsizei width, height;
  48.  
  49.     glutInit( &argc, argv );
  50.  
  51.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  52.     height = glutGet( GLUT_SCREEN_HEIGHT );
  53.     glutInitWindowPosition( (width / 2) + 2, height / 4 );
  54.     glutInitWindowSize( width / 2, height / 2 );
  55.     glutInitDisplayMode( GLUT_RGBA );
  56.     glutCreateWindow( argv[0] );
  57.  
  58.     initgfx();
  59.  
  60.     glutKeyboardFunc( keyboard );
  61.     glutDisplayFunc( drawScene ); 
  62.  
  63.     printHelp( argv[0] );
  64.  
  65.     glMatrixMode( GL_PROJECTION );
  66.     gluPerspective( 45.0, (GLdouble) width/ (GLdouble)height, 3.0, 7.0 );
  67.     glMatrixMode( GL_MODELVIEW );
  68.  
  69.     glutMainLoop();
  70. }
  71.  
  72. void
  73. printHelp( char *progname )
  74. {
  75.     fprintf(stdout, 
  76.         "\n%s - demonstrates use of matrix modes and stacks\n\n"
  77.         "Escape Key        - exit the program\n\n",
  78.         progname);
  79. }
  80.  
  81. GLvoid
  82. initgfx( GLvoid )
  83. {
  84.     glClearColor( 0.0, 0.0, 1.0, 1.0 );
  85.     glShadeModel( GL_FLAT );
  86. }
  87.  
  88. GLvoid 
  89. keyboard( GLubyte key, GLint x, GLint y )
  90. {
  91.     switch (key) {
  92.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  93.         exit(0);
  94.     }
  95. }
  96.  
  97. GLvoid
  98. drawTriangle( GLfloat red, GLfloat green, GLfloat blue )
  99. {
  100.     glColor3f( red, green, blue );
  101.     glBegin( GL_TRIANGLES );
  102.         glVertex2f( -0.5, -0.5 );
  103.         glVertex2f( 0.5, -0.5 );
  104.         glVertex2f( 0.0, 0.5 );
  105.     glEnd();
  106. }
  107.  
  108. GLvoid
  109. drawScene( GLvoid )
  110. {
  111.     glClear( GL_COLOR_BUFFER_BIT );
  112.  
  113.     glPushMatrix();
  114.         /* Translate the origin so that it is between the near and far
  115.          *    clipping planes.
  116.          */
  117.         glTranslatef( 0.0, 0.0, -5.0 );
  118.  
  119.         /* draw a black triangle centered at the origin */
  120.         drawTriangle( 0.0, 0.0, 0.0 );
  121.  
  122.         /* Draw x and y axes to show the origin */
  123.         XYaxes();
  124.  
  125.         /* move over a bit and draw another triangle */
  126.         glTranslatef( 0.5, 0.0, 0.0 );
  127.         drawTriangle( 0.3, 0.3, 0.3 ); 
  128.  
  129.         /* move over a bit and draw a rotated triangle */
  130.         glTranslatef( 0.5, 0.0, 0.0 );
  131.  
  132.         /* Rotate 60 degrees clockwise around the positive Z axis */
  133.         glRotatef( -15.0, 0.0, 0.0, 1.0 );
  134.         drawTriangle( 0.7, 0.7, 0.7 );
  135.  
  136.         /* move over a bit and draw a scaled triangle */
  137.         glTranslatef( 0.5, 0.0, 0.0 );
  138.  
  139.         /* Scale the triangle by one-half in all dimensions */
  140.         glScalef( -0.5, 0.5, 0.5 );
  141.         drawTriangle( 1.0, 1.0, 1.0 );     /* draw green triangle */
  142.         XYaxes();
  143.  
  144.     glPopMatrix();
  145.  
  146.     glFlush();
  147. }
  148.