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 / texture / swim.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  7.3 KB  |  311 lines

  1. /*
  2.  * Copyright 1996, 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. /* swim.c 
  19.  * This program demonstrates how a texture with alpha values
  20.  * can be used with blending to create transparent textures.
  21.  *
  22.  * The texture environment mode can be changed interactively.
  23.  *
  24.  *    <a> Key        - toggle alpha test on/off
  25.  *    <b> Key        - toggle blending on/off
  26.  *    <e> Key        - cycle through environment modes
  27.  *    <s> Key        - toggle swimming on/off
  28.  *    Escape Key    - exit program
  29.  */
  30. #include <GL/gl.h>
  31. #include <GL/glu.h>
  32. #include <GL/glut.h>
  33.  
  34. #include <math.h>
  35. #include <stdio.h>
  36.  
  37. #include "rgbImageFile.h"    /* should be in ../../include */
  38.  
  39. /*  Function Prototypes  */
  40.  
  41. GLvoid  initgfx( GLvoid );
  42. GLvoid  animate( GLvoid );
  43. GLvoid  visibility( GLint );
  44. GLvoid  drawScene( GLvoid );
  45. GLvoid  reshape( GLsizei, GLsizei );
  46. GLvoid  keyboard( GLubyte, GLint, GLint );
  47.  
  48. GLvoid  initTexture( unsigned int *, GLsizei, GLsizei );
  49.  
  50. void resetView( GLvoid );
  51. void printHelp( char * );
  52.  
  53. /* Global Definitions */
  54.  
  55. #define KEY_ESC    27    /* ascii value for the escape key */
  56.  
  57. /* Global Variables */
  58.  
  59. static GLfloat         swim = 0.0;
  60. static GLboolean     swimFlag = GL_TRUE;
  61.  
  62. static GLuint         envmode = 0;
  63.  
  64. static GLfloat water[] = { 0, 0.3, 0.5, 1 };
  65.  
  66. typedef struct {
  67.     GLint    type;
  68.     char    *name;
  69. } EnvModeInfo;
  70.  
  71. static EnvModeInfo modes[4] = {
  72.     { GL_DECAL, "GL_DECAL" },
  73.     { GL_MODULATE, "GL_MODULATE" },
  74.     { GL_BLEND, "GL_BLEND" },
  75.     { GL_REPLACE_EXT, "GL_REPLACE_EXT" },
  76. };
  77.  
  78. void
  79. main ( int argc, char *argv[])
  80. {
  81.     GLsizei     width, height;
  82.     char        *imageFileName = "fish.rgba";
  83.     unsigned int     *image;
  84.     GLsizei        imageWidth, imageHeight;
  85.     
  86.     glutInit( &argc, argv );
  87.  
  88.     if (argc < 2) {
  89.         fprintf (stderr, "usage: %s <imageFileName>\n", argv[0] );
  90.     } else
  91.         imageFileName = argv[1];
  92.  
  93.     fprintf(stdout, "using image %s\n\n", imageFileName );
  94.  
  95.     image = rgbReadImageFile(imageFileName, &imageWidth,
  96.              &imageHeight);
  97.  
  98.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  99.     height = glutGet( GLUT_SCREEN_HEIGHT );
  100.     glutInitWindowPosition( width / 4, height / 4 );
  101.     glutInitWindowSize( width / 2, height / 2 );
  102.     glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
  103.     glutCreateWindow( argv[0] );
  104.  
  105.     initTexture( image, imageWidth, imageHeight );
  106.     initgfx();
  107.  
  108.     glutKeyboardFunc( keyboard );
  109.     glutIdleFunc( animate );
  110.     glutVisibilityFunc( visibility );
  111.     glutReshapeFunc( reshape );
  112.     glutDisplayFunc( drawScene ); 
  113.  
  114.     printHelp( argv[0] );
  115.  
  116.     glutMainLoop();
  117. }
  118.  
  119. void
  120. printHelp( char *progname )
  121. {
  122.     fprintf(stdout, "\n%s - demonstrates texture environment modes\n\n" 
  123.         "<a> Key        - toggle alpha test on/off\n"
  124.         "<b> Key        - toggle blending on/off\n"
  125.         "<e> Key    - cycle through texture environment modes\n"
  126.         "<s> Key        - toggle swimming on/off\n"
  127.         "Escape Key    - exit the program\n\n",
  128.         progname);
  129.     printf("\nTexture Environment Mode is %s\n", modes[envmode].name);
  130. }
  131.  
  132. GLvoid 
  133. initgfx()
  134. {
  135.     glClearColor( water[0], water[1], water[2], water[3] );
  136.  
  137.     /* Don't draw nearly transparent pixels */
  138.     glAlphaFunc( GL_GREATER, 0.1 );
  139.     glEnable( GL_ALPHA_TEST );
  140.  
  141.     /* Set up blending so that the rectangle around the
  142.      * fish will be    transparent with smooth edges 
  143.      */
  144.     glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  145.     glEnable( GL_BLEND );
  146. }
  147.  
  148. GLvoid 
  149. initTexture( unsigned int *image, 
  150.     GLsizei imageWidth, GLsizei imageHeight )
  151. {
  152.     /* use gluBuild2DMipmaps to create and load mipmaps (it will 
  153.      * also scale he original image to be a power of two, if 
  154.      * necessary)
  155.      *
  156.      *      gluBuild2DMipmaps( target, components, width, height,
  157.      *              format,  type, imageArray ) 
  158.      */
  159.  
  160.     gluBuild2DMipmaps( GL_TEXTURE_2D, 4, imageWidth, imageHeight,
  161.              GL_RGBA, GL_UNSIGNED_BYTE, image );
  162.  
  163.     /* Setting the magnification filter to nearest instead of linear 
  164.      * may run faster on some platforms, with possibly lower quality 
  165.      */
  166.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  167.  
  168.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, modes[envmode].type);
  169.  
  170.     /* Used when tex env mode is GL_BLEND */
  171.     glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, water );
  172.  
  173.     /* enable 2D texture mapping */
  174.     glEnable( GL_TEXTURE_2D );
  175. }
  176.  
  177. GLvoid
  178. reshape( GLsizei width, GLsizei height )
  179. {
  180.     GLdouble      aspect;
  181.  
  182.     glViewport( 0, 0, width, height );
  183.  
  184.     aspect = (GLdouble) width / (GLdouble) height;
  185.  
  186.     glMatrixMode( GL_PROJECTION );
  187.     glLoadIdentity();
  188.     gluPerspective( 45.0, aspect, 1.0, 50.0 );
  189.     glMatrixMode( GL_MODELVIEW );
  190.     glLoadIdentity();
  191.     glTranslatef( 0.0, 0.0, -12.0 ); 
  192. }
  193.  
  194. GLvoid 
  195. animate( GLvoid )
  196. {
  197.     swim = fmodf( swim + 0.1, 35.0 );
  198.  
  199.     /* Tell GLUT to redraw the scene */
  200.     glutPostRedisplay();
  201. }
  202.  
  203. GLvoid
  204. visibility( int state ) 
  205. {
  206.     if (state == GLUT_VISIBLE && swimFlag) {
  207.         glutIdleFunc( animate );
  208.     } else {
  209.         glutIdleFunc( NULL );
  210.     }
  211. }
  212.  
  213. GLvoid
  214. cycleTexEnvMode( GLvoid )
  215. {
  216.     envmode = (envmode + 1) % 4;
  217.     
  218.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, modes[envmode].type);
  219.     printf("Texture Environment Mode is %s\n", modes[envmode].name );
  220. }
  221.  
  222. GLvoid
  223. toggleBlending( GLvoid )
  224. {
  225.     static GLboolean blending = GL_TRUE;
  226.  
  227.     blending = !blending;
  228.     if (blending) 
  229.         glEnable( GL_BLEND );
  230.     else
  231.         glDisable( GL_BLEND );
  232.  
  233.     printf( "blending %s\n", (blending? "enabled":"disabled"));
  234. }
  235.  
  236. GLvoid
  237. toggleAlphaTest( GLvoid )
  238. {
  239.     static GLboolean alphatest = GL_TRUE;
  240.  
  241.     alphatest = !alphatest;
  242.     if (alphatest) 
  243.         glEnable( GL_ALPHA_TEST );
  244.     else
  245.         glDisable( GL_ALPHA_TEST );
  246.  
  247.     printf( "alphatest %s\n", (alphatest? "enabled":"disabled"));
  248. }
  249.  
  250. GLvoid 
  251. keyboard( GLubyte key, GLint x, GLint y )
  252. {
  253.     switch (key) {
  254.     case 'a':    /* toggle alpha test */
  255.         toggleAlphaTest();
  256.         glutPostRedisplay();
  257.         break;
  258.     case 'b':    /* toggle blending */
  259.         toggleBlending();
  260.         glutPostRedisplay();
  261.         break;
  262.     case 'e':    /* cycle through texture environment modes */
  263.         cycleTexEnvMode();
  264.         glutPostRedisplay();
  265.         break;
  266.     case 's':    /* toggle swimming */
  267.         swimFlag = !swimFlag;
  268.         if (swimFlag)
  269.             glutIdleFunc( animate );
  270.         else
  271.             glutIdleFunc( NULL);
  272.         break;
  273.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  274.         exit(0);
  275.     }
  276. }
  277.  
  278. GLvoid
  279. drawScene(void)
  280. {
  281.     static float v0[3] = { -1.5, -1.0, 0.0 };
  282.     static float v1[3] = {  1.5, -1.0, 0.0 };
  283.     static float v2[3] = {  1.5,  1.0, 0.0 };
  284.     static float v3[3] = { -1.5,  1.0, 0.0 };
  285.  
  286.     static float t0[2] = { 0.0, 0.0 };
  287.     static float t1[2] = { 1.0, 0.0 };
  288.     static float t2[2] = { 1.0, 1.0 };
  289.     static float t3[2] = { 0.0, 1.0 };
  290.  
  291.     glClear( GL_COLOR_BUFFER_BIT );
  292.  
  293.     /* Use a white polygon so that the colors are as
  294.      * bright as possible when using modulate mode.
  295.      */
  296.     glColor4f( 1.0, 1.0, 1.0, 1.0 );
  297.     glPushMatrix ();
  298.         glTranslatef( -6.0 + swim, 0.2*sinf(swim * 3.0), -swim );
  299.     
  300.         glBegin( GL_QUADS );
  301.             glTexCoord2fv( t0 ); glVertex3fv( v0 );
  302.             glTexCoord2fv( t1 ); glVertex3fv( v1 );
  303.             glTexCoord2fv( t2 ); glVertex3fv( v2 );
  304.             glTexCoord2fv( t3 ); glVertex3fv( v3 );
  305.         glEnd();
  306.  
  307.     glPopMatrix ();
  308.  
  309.     glutSwapBuffers();
  310. }
  311.