home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 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.
- */
-
- /* swim.c
- * This program demonstrates how a texture with alpha values
- * can be used with blending to create transparent textures.
- *
- * The texture environment mode can be changed interactively.
- *
- * <a> Key - toggle alpha test on/off
- * <b> Key - toggle blending on/off
- * <e> Key - cycle through environment modes
- * <s> Key - toggle swimming on/off
- * Escape Key - exit program
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdio.h>
-
- #include "rgbImageFile.h" /* should be in ../../include */
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid animate( GLvoid );
- GLvoid visibility( GLint );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
-
- GLvoid initTexture( unsigned int *, GLsizei, GLsizei );
-
- void resetView( GLvoid );
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static GLfloat swim = 0.0;
- static GLboolean swimFlag = GL_TRUE;
-
- static GLuint envmode = 0;
-
- static GLfloat water[] = { 0, 0.3, 0.5, 1 };
-
- typedef struct {
- GLint type;
- char *name;
- } EnvModeInfo;
-
- static EnvModeInfo modes[4] = {
- { GL_DECAL, "GL_DECAL" },
- { GL_MODULATE, "GL_MODULATE" },
- { GL_BLEND, "GL_BLEND" },
- { GL_REPLACE_EXT, "GL_REPLACE_EXT" },
- };
-
- void
- main ( int argc, char *argv[])
- {
- GLsizei width, height;
- char *imageFileName = "fish.rgba";
- unsigned int *image;
- GLsizei imageWidth, imageHeight;
-
- glutInit( &argc, argv );
-
- if (argc < 2) {
- fprintf (stderr, "usage: %s <imageFileName>\n", argv[0] );
- } else
- imageFileName = argv[1];
-
- fprintf(stdout, "using image %s\n\n", imageFileName );
-
- image = rgbReadImageFile(imageFileName, &imageWidth,
- &imageHeight);
-
- 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 );
- glutCreateWindow( argv[0] );
-
- initTexture( image, imageWidth, imageHeight );
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutIdleFunc( animate );
- glutVisibilityFunc( visibility );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - demonstrates texture environment modes\n\n"
- "<a> Key - toggle alpha test on/off\n"
- "<b> Key - toggle blending on/off\n"
- "<e> Key - cycle through texture environment modes\n"
- "<s> Key - toggle swimming on/off\n"
- "Escape Key - exit the program\n\n",
- progname);
- printf("\nTexture Environment Mode is %s\n", modes[envmode].name);
- }
-
- GLvoid
- initgfx()
- {
- glClearColor( water[0], water[1], water[2], water[3] );
-
- /* Don't draw nearly transparent pixels */
- glAlphaFunc( GL_GREATER, 0.1 );
- glEnable( GL_ALPHA_TEST );
-
- /* Set up blending so that the rectangle around the
- * fish will be transparent with smooth edges
- */
- glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
- glEnable( GL_BLEND );
- }
-
- GLvoid
- initTexture( unsigned int *image,
- GLsizei imageWidth, GLsizei imageHeight )
- {
- /* use gluBuild2DMipmaps to create and load mipmaps (it will
- * also scale he original image to be a power of two, if
- * necessary)
- *
- * gluBuild2DMipmaps( target, components, width, height,
- * format, type, imageArray )
- */
-
- gluBuild2DMipmaps( GL_TEXTURE_2D, 4, imageWidth, imageHeight,
- GL_RGBA, GL_UNSIGNED_BYTE, image );
-
- /* Setting the magnification filter to nearest instead of linear
- * may run faster on some platforms, with possibly lower quality
- */
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, modes[envmode].type);
-
- /* Used when tex env mode is GL_BLEND */
- glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, water );
-
- /* enable 2D texture mapping */
- glEnable( GL_TEXTURE_2D );
- }
-
- 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, 1.0, 50.0 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glTranslatef( 0.0, 0.0, -12.0 );
- }
-
- GLvoid
- animate( GLvoid )
- {
- swim = fmodf( swim + 0.1, 35.0 );
-
- /* Tell GLUT to redraw the scene */
- glutPostRedisplay();
- }
-
- GLvoid
- visibility( int state )
- {
- if (state == GLUT_VISIBLE && swimFlag) {
- glutIdleFunc( animate );
- } else {
- glutIdleFunc( NULL );
- }
- }
-
- GLvoid
- cycleTexEnvMode( GLvoid )
- {
- envmode = (envmode + 1) % 4;
-
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, modes[envmode].type);
- printf("Texture Environment Mode is %s\n", modes[envmode].name );
- }
-
- GLvoid
- toggleBlending( GLvoid )
- {
- static GLboolean blending = GL_TRUE;
-
- blending = !blending;
- if (blending)
- glEnable( GL_BLEND );
- else
- glDisable( GL_BLEND );
-
- printf( "blending %s\n", (blending? "enabled":"disabled"));
- }
-
- GLvoid
- toggleAlphaTest( GLvoid )
- {
- static GLboolean alphatest = GL_TRUE;
-
- alphatest = !alphatest;
- if (alphatest)
- glEnable( GL_ALPHA_TEST );
- else
- glDisable( GL_ALPHA_TEST );
-
- printf( "alphatest %s\n", (alphatest? "enabled":"disabled"));
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case 'a': /* toggle alpha test */
- toggleAlphaTest();
- glutPostRedisplay();
- break;
- case 'b': /* toggle blending */
- toggleBlending();
- glutPostRedisplay();
- break;
- case 'e': /* cycle through texture environment modes */
- cycleTexEnvMode();
- glutPostRedisplay();
- break;
- case 's': /* toggle swimming */
- swimFlag = !swimFlag;
- if (swimFlag)
- glutIdleFunc( animate );
- else
- glutIdleFunc( NULL);
- break;
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- drawScene(void)
- {
- static float v0[3] = { -1.5, -1.0, 0.0 };
- static float v1[3] = { 1.5, -1.0, 0.0 };
- static float v2[3] = { 1.5, 1.0, 0.0 };
- static float v3[3] = { -1.5, 1.0, 0.0 };
-
- static float t0[2] = { 0.0, 0.0 };
- static float t1[2] = { 1.0, 0.0 };
- static float t2[2] = { 1.0, 1.0 };
- static float t3[2] = { 0.0, 1.0 };
-
- glClear( GL_COLOR_BUFFER_BIT );
-
- /* Use a white polygon so that the colors are as
- * bright as possible when using modulate mode.
- */
- glColor4f( 1.0, 1.0, 1.0, 1.0 );
- glPushMatrix ();
- glTranslatef( -6.0 + swim, 0.2*sinf(swim * 3.0), -swim );
-
- glBegin( GL_QUADS );
- glTexCoord2fv( t0 ); glVertex3fv( v0 );
- glTexCoord2fv( t1 ); glVertex3fv( v1 );
- glTexCoord2fv( t2 ); glVertex3fv( v2 );
- glTexCoord2fv( t3 ); glVertex3fv( v3 );
- glEnd();
-
- glPopMatrix ();
-
- glutSwapBuffers();
- }
-