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

  1. /* Copyright 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. /* depth_buffer.c - draw a solid polygon that intersects the line grid
  18.  *    using the depth buffer to remove hidden surfaces.
  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( (width / 2) + 4, height / 4 );
  56.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  57.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH );
  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 depth buffering\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.     glEnable( GL_DEPTH_TEST );
  87. }
  88.  
  89. GLvoid
  90. reshape( GLsizei width, GLsizei height )
  91. {
  92.     GLdouble    aspect;
  93.  
  94.     glViewport( 0, 0, width, height );
  95.  
  96.     glMatrixMode( GL_PROJECTION );
  97.     glLoadIdentity();
  98.     aspect = (GLdouble) width / (GLdouble) height;
  99.     gluPerspective( 45.0, aspect, 3.0, 7.0 );
  100.     glMatrixMode( GL_MODELVIEW );
  101. }
  102.  
  103. GLvoid 
  104. keyboard( GLubyte key, GLint x, GLint y )
  105. {
  106.     switch (key) {
  107.     case 'r':
  108.         doRotate = !doRotate;
  109.         glutPostRedisplay();
  110.         break;
  111.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  112.         exit(0);
  113.     }
  114. }
  115.  
  116. GLvoid
  117. drawScene( GLvoid )
  118. {
  119.     static GLfloat    whiteColor[] = { 1.0, 1.0, 1.0 };
  120.     static GLfloat    purpleColor[] = { 0.8, 0.0, 1.0 };
  121.  
  122.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  123.  
  124.     glPushMatrix();
  125.         /* Move origin between the near and far clipping planes */
  126.         glTranslatef( 0.0, 0.0, -4.0 );
  127.  
  128.         /* Draw a grid in the x-y plane centered at the origin */
  129.         glColor3fv( whiteColor );
  130.         WireGrid();
  131.  
  132.         glColor3fv( purpleColor );
  133.  
  134.         /* Draw a rectangle in front of the grid */
  135.         glTranslatef( 0.0, 0.0, 0.1 );
  136.  
  137.         if (doRotate)
  138.             glRotatef( -45.0, 1.0, 0.0, 0.0 );
  139.  
  140.         glRectf( -0.5, -0.5, 0.5, 0.5 );
  141.     glPopMatrix();
  142.  
  143.     glFlush();
  144. }
  145.