home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 1995, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* stencil.c
- * This program demonstrates a simple application of the stencil
- * planes to mask an arbitrary screen area.
- *
- * Escape key - exit the program
- * <r> key - toggle rotation
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdio.h>
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid animate( GLvoid );
- GLvoid visibility( GLint );
- GLvoid keyboard( GLubyte, GLint, GLint );
-
- GLvoid setupStencil( GLvoid );
-
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static GLfloat angle = 0.0;
-
- static GLboolean rotating = GL_TRUE;
-
- void
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( width/4, height/4 );
- glutInitWindowSize( width/2, height/2 );
- glutInitDisplayMode( GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE|GLUT_STENCIL );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutIdleFunc( animate );
- glutVisibilityFunc( visibility );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout, "%s -- demonstrates how to use the stencil planes "
- "to create a mask\n\n"
- "Escape key - exit the program\n"
- "<r> key - toggle rotation\n\n", progname);
- }
-
- /* Initialize material properties, light source, lighting model, etc. */
- GLvoid
- initgfx( GLvoid )
- {
- GLfloat mat_specular[] = { 0.8, 0.8, 0.8, 1.0 };
- GLfloat mat_shininess[] = { 10.0 };
- GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
-
- glMaterialfv( GL_FRONT, GL_SPECULAR, mat_specular );
- glMaterialfv( GL_FRONT, GL_SHININESS, mat_shininess );
- glLightfv( GL_LIGHT0, GL_POSITION, light_position );
-
- glEnable( GL_LIGHTING );
- glEnable( GL_LIGHT0 );
-
- glEnable( GL_DEPTH_TEST );
-
- glClearStencil( 0 );
- glStencilMask( 0x1 );
- glEnable( GL_STENCIL_TEST );
- setupStencil();
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case 'r': /* toggle rotation */
- rotating = !rotating;
- if (rotating)
- glutIdleFunc( animate );
- else
- glutIdleFunc( NULL );
- break;
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- glViewport( 0, 0, width, height );
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 45.0, (GLdouble) width / (GLdouble) height, 3.0, 13.0 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glTranslatef( 0.0, 0.0, -5.0 );
-
- /* reinitialize stencil plane to fit new window size */
- setupStencil();
- }
-
- GLvoid
- animate( GLvoid )
- {
- angle = fmodf( angle + 1.0, 360.0 ); /* update angle */
- glutPostRedisplay(); /* Tell GLUT to redraw */
- }
-
- GLvoid
- visibility( int state )
- {
- if (state == GLUT_VISIBLE && rotating) {
- glutIdleFunc( animate );
- } else {
- glutIdleFunc( NULL );
- }
- }
-
- /* setupStencil() creates a simple diamond-shaped stencil
- * pattern stored in the stencil planes. This sets up the
- * stencil for use in drawScene().
- */
- GLvoid
- setupStencil( GLvoid )
- {
- glClear( GL_STENCIL_BUFFER_BIT );
- glStencilFunc( GL_ALWAYS, 1, 0x1 );
- glStencilOp( GL_REPLACE, GL_REPLACE, GL_REPLACE );
-
- glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
- glDepthMask( GL_FALSE );
- glBegin( GL_QUADS );
- glVertex3i( 1, 0, 0 );
- glVertex3i( 0, 1, 0 );
- glVertex3i( -1, 0, 0 );
- glVertex3i( 0, -1, 0 );
- glEnd();
- glDepthMask( GL_TRUE );
- glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
-
- glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };
- GLfloat magenta[] = { 1.0, 0.0, 1.0, 1.0 };
-
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
-
- /* hack - normally, the stencil should only have to
- * be re-initialized when window changes size, or is moved,
- * but GLUT doesn't provide a callback for when the window moves,
- * so we must do it every time in case the window moves
- */
- setupStencil();
-
- glPushMatrix();
- glRotatef( angle, 0.0, 1.0, 0.0 );
-
- /* only draw where the stencil bits == 1 */
- glStencilFunc( GL_EQUAL, 1, 1 );
-
- /* draw yellow torus */
- glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, yellow );
- glutSolidTorus( 0.25, 0.75, 15, 31 );
-
- /* only draw where the stencil bits == 0 */
- glStencilFunc( GL_EQUAL, 0, 1 );
-
- /* draw magenta torus */
- glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, magenta );
- glutSolidTorus( 0.25, 0.75, 15, 31 );
- glPopMatrix();
-
- glutSwapBuffers();
- }
-