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 / depth_buffer / painter.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  3.2 KB  |  143 lines

  1. /* Copyright 1993, 1994, 1996, Silicon Graphics, Inc.
  2.  * All Rights Reserved.
  3.  *
  4.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  5.  * the contents of this file may not be disclosed to third parties, copied or
  6.  * duplicated in any form, in whole or in part, without the prior written
  7.  * permission of Silicon Graphics, Inc.
  8.  *
  9.  * RESTRICTED RIGHTS LEGEND:
  10.  * Use, duplication or disclosure by the Government is subject to restrictions
  11.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  12.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  13.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  14.  * rights reserved under the Copyright Laws of the United States.
  15.  */
  16.  
  17. /* painter.c - use the painter's algorithm to draw a solid polygon 
  18.  *    in front of a line grid
  19.  *
  20.  *    <r> Key        - rotate polygon along X axis
  21.  *    Escape Key    - exit program
  22.  */
  23. #include <GL/gl.h>
  24. #include <GL/glu.h>
  25. #include <GL/glut.h>
  26.  
  27. #include <stdio.h>
  28.  
  29. /*  Function Prototypes  */
  30.  
  31. GLvoid  initgfx( GLvoid );
  32. GLvoid  keyboard( GLubyte, GLint, GLint );
  33. GLvoid  drawScene( GLvoid );
  34. GLvoid  reshape( GLsizei, GLsizei );
  35.  
  36. void printHelp( char * );
  37.  
  38. /* Global Definitions */
  39.  
  40. #define KEY_ESC    27    /* ascii value for the escape key */
  41.  
  42. /* Global Variables */
  43.  
  44. static GLboolean doRotate = GL_FALSE;
  45.  
  46. void
  47. main( int argc, char *argv[] )
  48. {
  49.     GLsizei width, height;
  50.  
  51.     glutInit( &argc, argv );
  52.  
  53.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  54.     height = glutGet( GLUT_SCREEN_HEIGHT );
  55.     glutInitWindowPosition( 0, height / 4 );
  56.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  57.     glutInitDisplayMode( GLUT_RGBA );
  58.     glutCreateWindow( argv[0] );
  59.  
  60.     initgfx();
  61.  
  62.     glutReshapeFunc( reshape );
  63.     glutKeyboardFunc( keyboard );
  64.     glutDisplayFunc( drawScene ); 
  65.  
  66.     printHelp( argv[0] );
  67.  
  68.     glutMainLoop();
  69. }
  70.  
  71. void
  72. printHelp( char *progname )
  73. {
  74.     fprintf(stdout, "\n%s - demonstrates painter's algorithm\n\n"
  75.         "<r> Key        - rotate polygon around X axis\n"
  76.         "Escape Key        - exit the program\n\n",
  77.         progname);
  78. }
  79.  
  80. GLvoid
  81. initgfx( GLvoid )
  82. {
  83.     glClearColor( 0.0, 0.0, 1.0, 1.0 );
  84.     glShadeModel( GL_FLAT );
  85. }
  86.  
  87. GLvoid
  88. reshape( GLsizei width, GLsizei height )
  89. {
  90.     GLdouble    aspect;
  91.  
  92.     glViewport( 0, 0, width, height );
  93.  
  94.     glMatrixMode( GL_PROJECTION );
  95.     glLoadIdentity();
  96.     aspect = (GLdouble) width / (GLdouble) height;
  97.     gluPerspective( 45.0, aspect, 3.0, 7.0 );
  98.     glMatrixMode( GL_MODELVIEW );
  99. }
  100.  
  101. GLvoid 
  102. keyboard( GLubyte key, GLint x, GLint y )
  103. {
  104.     switch (key) {
  105.     case 'r':
  106.         doRotate = !doRotate;
  107.         glutPostRedisplay();
  108.         break;
  109.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  110.         exit(0);
  111.     }
  112. }
  113.  
  114. GLvoid
  115. drawScene( GLvoid )
  116. {
  117.     static GLfloat    whiteColor[] = { 1.0, 1.0, 1.0 };
  118.     static GLfloat    purpleColor[] = { 0.8, 0.0, 1.0 };
  119.  
  120.     glClear( GL_COLOR_BUFFER_BIT );
  121.  
  122.     glPushMatrix();
  123.         /* Move origin between the near and far clipping planes */
  124.         glTranslatef( 0.0, 0.0, -4.0 );
  125.  
  126.         /* Draw a grid in the x-y plane centered at the origin */
  127.         glColor3fv( whiteColor );
  128.         WireGrid();
  129.  
  130.         glColor3fv( purpleColor );
  131.  
  132.         /* Draw a rectangle in front of the grid */
  133.         glTranslatef( 0.0, 0.0, 0.1 );
  134.  
  135.         if (doRotate)
  136.             glRotatef( -45.0, 1.0, 0.0, 0.0 );
  137.  
  138.         glRectf( -0.5, -0.5, 0.5, 0.5 );
  139.     glPopMatrix();
  140.  
  141.     glFlush();
  142. }
  143.