home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 1996, 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.
- */
-
- /* alphaTest.c - shows how alpha test can be used to implement a
- * transparency algorithm
- *
- *
- * Escape key - exit the program
- * Left Mouse Button - change incidence and azimuth angles
- * Middle Mousebutton - change the twist angle based on
- * horizontal mouse movement
- * Right Mousebutton - zoom in and out based on vertical
- * mouse movement
- * <b> key - cycle through different blend functions
- * <d> Key - toggle depth mask on / off
- * <R> key - reset view
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
- #include <math.h>
- #include <stdio.h> /* for printf */
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid drawObjects( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
- GLvoid mouse( GLint, GLint, GLint, GLint );
- GLvoid motion( GLint, GLint );
- GLvoid blendFuncCycle( GLvoid );
-
- void resetView( GLvoid );
- void polarView( GLfloat, GLfloat, GLfloat, GLfloat);
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static GLboolean depthMaskFlag = GL_FALSE;
-
- static enum actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
- static GLint action;
-
- static GLint xStart = 0, yStart = 0;
-
- static GLfloat near, far, distance, twistAngle, incAngle, azimAngle;
-
- GLvoid
- 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_DOUBLE | GLUT_DEPTH );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutMouseFunc( mouse );
- glutMotionFunc( motion );
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - demonstrates how to use alpha test with\n"
- "the depth buffer to implement a transparency algorithm\n\n"
- "Axes: X - red, Y - green, Z - blue\n\n"
- "Left Mousebutton - move eye position\n"
- "Middle Mousebutton - change twist angle\n"
- "Right Mousebutton - move up / down to zoom in / out\n"
- "<b> Key - cycle through 3 blend functions\n"
- "<d> Key - toggle depth mask on / off\n"
- "<R> Key - reset view\n"
- "Escape Key - exit the program\n\n",
- progname);
- }
-
- GLvoid
- initgfx( GLvoid )
- {
- GLfloat maxObjectSize = 3.0;
-
- glClearColor( 0.0, 0.0, 0.0, 0.0 );
- glShadeModel( GL_FLAT );
-
- /* Set up near and far so that ( far - near ) > maxObjectSize, */
- /* and determine the viewing distance (adjust for zooming) */
- near = 1.0;
- far = near + 8*maxObjectSize;
-
- resetView();
-
- /* set the initial blend function */
- glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
- fprintf(stdout,
- "glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA )\n" );
-
- glEnable( GL_DEPTH_TEST );
- printf("depth buffer is enabled\n");
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- aspect = (GLdouble) width / (GLdouble) height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 45.0, aspect, near, far );
- glMatrixMode( GL_MODELVIEW );
- }
-
- GLvoid
- blendFuncCycle( GLvoid )
- {
- static int whichBlendFunc = 0;
-
- whichBlendFunc = (whichBlendFunc + 1) % 3;
-
- switch (whichBlendFunc)
- {
- case 0:
- glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
- fprintf(stdout,
- "glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA )\n" );
- break;
- case 1:
- glBlendFunc( GL_SRC_ALPHA, GL_ONE );
- fprintf(stdout,
- "glBlendFunc( GL_SRC_ALPHA, GL_ONE )\n" );
- break;
- case 2:
- glBlendFunc( GL_ONE, GL_ZERO );
- fprintf(stdout,
- "glBlendFunc( GL_ONE, GL_ZERO )\n" );
- break;
- default:
- break;
- }
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case 'b': /* cycle through several blend funcs */
- blendFuncCycle();
- glutPostRedisplay();
- break;
- case 'd':
- depthMaskFlag = !depthMaskFlag;
- printf("writing into the depth buffer is %s\n",
- (depthMaskFlag ? "disabled" : "enabled"));
- glutPostRedisplay();
- break;
- case 'R':
- resetView();
- glutPostRedisplay();
- break;
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- mouse( GLint button, GLint state, GLint x, GLint y )
- {
- if (state == GLUT_DOWN) {
- switch (button) {
- case GLUT_LEFT_BUTTON:
- action = MOVE_EYE;
- break;
- case GLUT_MIDDLE_BUTTON:
- action = TWIST_EYE;
- break;
- case GLUT_RIGHT_BUTTON:
- action = ZOOM;
- break;
- }
-
- /* Update the saved mouse position */
- xStart = x;
- yStart = y;
- } else {
- action = MOVE_NONE;
- }
-
- }
-
- GLvoid
- motion( GLint x, GLint y )
- {
- switch (action) {
- case MOVE_EYE:
- /* Adjust the eye position based on the mouse position */
- azimAngle += (GLdouble) (x - xStart);
- incAngle -= (GLdouble) (y - yStart);
- break;
- case TWIST_EYE:
- /* Adjust the eye twist based on the mouse position */
- twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
- break;
- case ZOOM:
- /* Adjust the eye distance based on the mouse position */
- distance -= (GLdouble) (y - yStart)/10.0;
- break;
- default:
- printf("unknown action %d\n", action);
- }
-
- /* Update the stored mouse position for later use */
- xStart = x;
- yStart = y;
-
- glutPostRedisplay();
- }
-
- void
- resetView( GLvoid )
- {
- distance = near + (far - near) / 2.0;
- twistAngle = 0.0; /* rotation of viewing volume (camera) */
- incAngle = 0.0;
- azimAngle = 0.0;
- }
-
- void
- polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
- GLfloat twist)
- {
- glTranslatef( 0.0, 0.0, -distance);
- glRotatef( -twist, 0.0, 0.0, 1.0);
- glRotatef( -incidence, 1.0, 0.0, 0.0);
- glRotatef( -azimuth, 0.0, 0.0, 1.0);
- }
-
- GLvoid
- drawObjects( GLvoid )
- {
- static GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };
- static GLfloat green[] = { 0.0, 1.0, 0.0, 0.25 };
- static GLfloat red[] = { 0.7, 0.0, 0.0, 0.75 };
-
- /* draw torus closest to the eye */
- glPushMatrix ();
- glTranslatef (0.0, 0.0, 1.0);
- glColor4fv( yellow );
- glutSolidTorus(0.275, 0.85, 15, 15);
- glPopMatrix ();
-
- /* draw cylinder further from the eye */
- glPushMatrix ();
- glTranslatef (0.0, 0.0, -1.0);
- glColor4fv( red );
- SolidCylinder(1.0, 2.0);
- glPopMatrix ();
-
- /* draw sphere still further from the eye */
- glPushMatrix ();
- glTranslatef (0.0, 0.0, -2.0);
- glColor4fv( green );
- glutSolidSphere(0.5, 15, 15);
- glPopMatrix ();
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
-
- glPushMatrix();
- polarView( distance, azimAngle, incAngle, twistAngle );
- XYZaxes();
-
- /* enable alpha testing */
- glEnable( GL_ALPHA_TEST );
-
- /* test for alpha == 1.0, so that
- * only opaque objects are drawn */
- glAlphaFunc( GL_EQUAL, 1.0 );
-
- drawObjects();
-
- if ( depthMaskFlag == GL_TRUE )
- glDepthMask( GL_FALSE );
-
-
- /* test for alpha != 1.0, so that
- * only transparent objects are drawn */
- glAlphaFunc( GL_NOTEQUAL, 1.0 );
-
- glEnable( GL_BLEND );
- drawObjects();
- glDisable( GL_BLEND );
-
- if ( depthMaskFlag == GL_TRUE )
- glDepthMask( GL_TRUE );
-
- /* disable alpha testing */
- glDisable( GL_ALPHA_TEST );
-
- glPopMatrix();
- glutSwapBuffers();
- }
-