/* * Copyright (c) 1996-1999 Silicon Graphics, Inc. All rights reserved. * * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. * * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL, * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ /* cullface.c - draw a solid torus with front/back face culling. * * Key - toggle between front/back face culling * Key - rotate polygon along X axis * Escape Key - exit program */ #include /* includes gl.h, glu.h */ /* includes gl.h, glu.h */ #include #include "misc.h" /* Function Prototypes */ GLvoid initgfx( GLvoid ); GLvoid keyboard( GLubyte, GLint, GLint ); GLvoid drawScene( GLvoid ); GLvoid reshape( GLsizei, GLsizei ); void printHelp( char * ); /* Global Definitions */ #define KEY_ESC 27 /* ascii value for the escape key */ /* Global Variables */ static GLint cullface = GL_BACK; static GLboolean doRotate = GL_FALSE; void main( int argc, char *argv[] ) { GLsizei width, height; glutInit( &argc, argv ); width = glutGet( GLUT_SCREEN_WIDTH ); height = glutGet( GLUT_SCREEN_HEIGHT ); glutInitWindowPosition( (width / 2) + 4, height / 4 ); glutInitWindowSize( (width / 2) - 4, height / 2 ); glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH ); glutCreateWindow( argv[0] ); initgfx(); glutReshapeFunc( reshape ); glutKeyboardFunc( keyboard ); glutDisplayFunc( drawScene ); printHelp( argv[0] ); glutMainLoop(); } void printHelp( char *progname ) { fprintf(stdout, "\n%s - demonstrates front/back face culling\n\n" " Key - toggle between front/back face culling\n" " Key - rotate polygon around X axis\n" "Escape Key - exit the program\n\n", progname); fprintf(stdout, "culling %s faces\n", (cullface == GL_FRONT ? "FRONT" : "BACK" )); } GLvoid initgfx( GLvoid ) { glClearColor( 0.0, 0.0, 1.0, 1.0 ); glShadeModel( GL_FLAT ); glEnable( GL_DEPTH_TEST ); /* Enable face culling */ glEnable( GL_CULL_FACE ); } GLvoid reshape( GLsizei width, GLsizei height ) { GLdouble aspect; glViewport( 0, 0, width, height ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); aspect = (GLdouble) width / (GLdouble) height; gluPerspective( 45.0, aspect, 3.0, 7.0 ); glMatrixMode( GL_MODELVIEW ); } GLvoid keyboard( GLubyte key, GLint x, GLint y ) { switch (key) { case 'f': /* toggle between front/back face culling */ if (cullface == GL_FRONT) { cullface = GL_BACK; } else { cullface = GL_FRONT; } fprintf(stdout, "culling %s faces\n", (cullface == GL_FRONT ? "FRONT" : "BACK" )); glCullFace( cullface ); glutPostRedisplay(); break; case 'r': doRotate = !doRotate; glutPostRedisplay(); break; case KEY_ESC: /* Exit when the Escape key is pressed */ exit(0); } } GLvoid drawScene( GLvoid ) { static GLfloat whiteColor[] = { 1.0f, 1.0f, 1.0f }; static GLfloat purpleColor[] = { 0.8f, 0.0f, 1.0f }; glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glPushMatrix(); /* Move origin between the near and far clipping planes */ glTranslatef( 0.0f, 0.0f, -4.0f ); /* Draw a grid in the x-y plane centered at the origin */ glColor3fv( whiteColor ); WireGrid(); glColor3fv( purpleColor ); /* Draw a rectangle in front of the grid */ glTranslatef( 0.0f, 0.0f, 0.1f ); if (doRotate) glRotatef( -45.0f, 1.0f, 0.0f, 0.0f ); /* Draw a torus */ glutSolidTorus( 0.2, 0.7, 15, 31 ); glPopMatrix(); glFlush(); }