home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / stencil / stencil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.3 KB  |  223 lines

  1. /*
  2.  * Copyright 1993, 1995, 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. /*  stencil.c 
  19.  *  This program demonstrates a simple application of the stencil
  20.  *  planes to mask an arbitrary screen area.
  21.  *
  22.  *    Escape key    - exit the program
  23.  *    <r> key        - toggle rotation
  24.  */
  25. #include <GL/gl.h>
  26. #include <GL/glu.h>
  27. #include <GL/glut.h>
  28.  
  29. #include <math.h>
  30. #include <stdio.h>
  31.  
  32. /*  Function Prototypes  */
  33.  
  34. GLvoid  initgfx( GLvoid );
  35. GLvoid  drawScene( GLvoid );
  36. GLvoid  reshape( GLsizei, GLsizei );
  37. GLvoid  animate( GLvoid );
  38. GLvoid  visibility( GLint );
  39. GLvoid  keyboard( GLubyte, GLint, GLint );
  40.  
  41. GLvoid     setupStencil( 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 GLfloat        angle = 0.0;
  52.  
  53. static GLboolean     rotating = GL_TRUE;
  54.  
  55. void
  56. main( int argc, char *argv[] )
  57. {
  58.     GLsizei     width, height;
  59.  
  60.     glutInit( &argc, argv );
  61.  
  62.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  63.     height = glutGet( GLUT_SCREEN_HEIGHT );
  64.     glutInitWindowPosition( width/4, height/4 ); 
  65.     glutInitWindowSize( width/2, height/2 );
  66.     glutInitDisplayMode( GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE|GLUT_STENCIL );
  67.     glutCreateWindow( argv[0] );
  68.     
  69.     initgfx();
  70.  
  71.     glutKeyboardFunc( keyboard );
  72.     glutReshapeFunc( reshape );
  73.     glutIdleFunc( animate ); 
  74.     glutVisibilityFunc( visibility ); 
  75.     glutDisplayFunc( drawScene ); 
  76.  
  77.     printHelp( argv[0] );
  78.  
  79.     glutMainLoop();
  80. }
  81.  
  82. void
  83. printHelp( char *progname )
  84. {
  85.     fprintf(stdout, "%s -- demonstrates how to use the stencil planes "
  86.         "to create a mask\n\n"
  87.         "Escape key    - exit the program\n"
  88.         "<r> key        - toggle rotation\n\n", progname);
  89. }
  90.  
  91. /*  Initialize material properties, light source, lighting model, etc. */
  92. GLvoid
  93. initgfx( GLvoid )
  94. {
  95.     GLfloat mat_specular[] = { 0.8, 0.8, 0.8, 1.0 };
  96.     GLfloat mat_shininess[] = { 10.0 };
  97.     GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  98.  
  99.     glMaterialfv( GL_FRONT, GL_SPECULAR, mat_specular );
  100.     glMaterialfv( GL_FRONT, GL_SHININESS, mat_shininess );
  101.     glLightfv( GL_LIGHT0, GL_POSITION, light_position );
  102.  
  103.     glEnable( GL_LIGHTING );
  104.     glEnable( GL_LIGHT0 );
  105.  
  106.     glEnable( GL_DEPTH_TEST );
  107.  
  108.     glClearStencil( 0 );
  109.     glStencilMask( 0x1 );
  110.     glEnable( GL_STENCIL_TEST );
  111.     setupStencil();
  112. }
  113.  
  114. GLvoid 
  115. keyboard( GLubyte key, GLint x, GLint y )
  116. {
  117.     switch (key) {
  118.     case 'r':    /* toggle rotation */
  119.         rotating = !rotating;
  120.         if (rotating)
  121.             glutIdleFunc( animate );
  122.         else 
  123.             glutIdleFunc( NULL );
  124.         break;
  125.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  126.         exit(0);
  127.     }
  128. }
  129.  
  130. GLvoid
  131. reshape( GLsizei width, GLsizei height )
  132. {
  133.     glViewport( 0, 0, width, height );
  134.  
  135.     glMatrixMode( GL_PROJECTION );
  136.     glLoadIdentity();
  137.     gluPerspective( 45.0, (GLdouble) width / (GLdouble) height, 3.0, 13.0 );
  138.     glMatrixMode( GL_MODELVIEW );
  139.     glLoadIdentity();
  140.     glTranslatef( 0.0, 0.0, -5.0 ); 
  141.  
  142.     /* reinitialize stencil plane to fit new window size */
  143.     setupStencil();
  144. }
  145.  
  146. GLvoid
  147. animate( GLvoid )
  148. {
  149.     angle = fmodf( angle + 1.0, 360.0 );    /* update angle */
  150.     glutPostRedisplay();            /* Tell GLUT to redraw */
  151. }
  152.         
  153. GLvoid
  154. visibility( int state )
  155. {
  156.     if (state == GLUT_VISIBLE && rotating) {
  157.         glutIdleFunc( animate );
  158.     } else {
  159.         glutIdleFunc( NULL );
  160.     }
  161. }
  162.  
  163. /* setupStencil() creates a simple diamond-shaped stencil
  164.  * pattern stored in the stencil planes.  This sets up the
  165.  * stencil for use in drawScene().
  166.  */
  167. GLvoid
  168. setupStencil( GLvoid )
  169. {
  170.     glClear( GL_STENCIL_BUFFER_BIT );
  171.     glStencilFunc( GL_ALWAYS, 1, 0x1 );
  172.     glStencilOp( GL_REPLACE, GL_REPLACE, GL_REPLACE );
  173.  
  174.     glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
  175.     glDepthMask( GL_FALSE );
  176.     glBegin( GL_QUADS );        
  177.         glVertex3i( 1, 0, 0 );
  178.         glVertex3i( 0, 1, 0 );
  179.         glVertex3i( -1, 0, 0 );
  180.         glVertex3i( 0, -1, 0 );
  181.     glEnd();
  182.     glDepthMask( GL_TRUE );
  183.     glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
  184.  
  185.     glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
  186. }
  187.     
  188. GLvoid
  189. drawScene( GLvoid )
  190. {
  191.     GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };
  192.     GLfloat magenta[] = { 1.0, 0.0, 1.0, 1.0 };
  193.  
  194.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 
  195.  
  196.     /* hack - normally, the stencil should only have to
  197.      * be re-initialized when window changes size, or is moved, 
  198.      * but GLUT doesn't provide a callback for when the window moves,
  199.      * so we must do it every time in case the window moves
  200.      */
  201.     setupStencil();
  202.  
  203.     glPushMatrix(); 
  204.         glRotatef( angle, 0.0, 1.0, 0.0 );
  205.  
  206.         /* only draw where the stencil bits == 1 */
  207.         glStencilFunc( GL_EQUAL, 1, 1 );
  208.  
  209.         /* draw yellow torus */
  210.         glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, yellow );
  211.         glutSolidTorus( 0.25, 0.75, 15, 31 );
  212.  
  213.         /* only draw where the stencil bits == 0 */
  214.         glStencilFunc( GL_EQUAL, 0, 1 );
  215.         
  216.         /* draw magenta torus */
  217.         glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, magenta );
  218.         glutSolidTorus( 0.25, 0.75, 15, 31 );
  219.     glPopMatrix(); 
  220.  
  221.     glutSwapBuffers();
  222. }
  223.