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 / texenv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  6.8 KB  |  262 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. /* texenv.c 
  19.  * This program reads in a texture from a .rgb file, 
  20.  * and maps it onto a polygon using explicit coordinates.
  21.  *
  22.  * Decal modes uses only the texture color, not the original polygon 
  23.  * color, unless there are alpha values in the texture, in 
  24.  * which case alpha determines the percentage of texture 
  25.  * blended with the polygon color.  Decal may be faster than the 
  26.  * default modulate mode.
  27.  *
  28.  * Modulate mode modulates the color of the polygon by the color
  29.  * of the texture, so a white polygon should be used to get the
  30.  * full texture color.  If alpha is present, it is modulated by
  31.  * the texture alpha.  
  32.  *
  33.  * Blend mode uses the texture color as though it were an alpha
  34.  * value.  It uses the texture color to blend the fragment color
  35.  * with a constant blending color.  If alpha is present, it is 
  36.  * modulated by the texture alpha.
  37.  *
  38.  * Replace mode replaces both the fragments color and alpha values
  39.  * with the corresponding texture values.
  40.  *
  41.  * The texture environment mode and the polygon color can be 
  42.  * changed interactively.
  43.  *
  44.  *    <c> Key        - toggle between white and red polygon
  45.  *    <e> Key        - cycle through environment modes
  46.  *    Escape Key    - exit program
  47.  */
  48. #include <GL/gl.h>
  49. #include <GL/glu.h>
  50. #include <GL/glut.h>
  51.  
  52. #include <math.h>
  53. #include <stdio.h>
  54.  
  55. #include "rgbImageFile.h"    /* should be in ../../include */
  56.  
  57. /*  Function Prototypes  */
  58.  
  59. GLvoid  initgfx( GLvoid );
  60. GLvoid  drawScene( GLvoid );
  61. GLvoid  reshape( GLsizei, GLsizei );
  62. GLvoid  keyboard( GLubyte, GLint, GLint );
  63.  
  64. GLvoid  initTexture( unsigned int *, GLsizei, GLsizei );
  65.  
  66. void resetView( GLvoid );
  67. void printHelp( char * );
  68.  
  69. /* Global Definitions */
  70.  
  71. #define KEY_ESC    27    /* ascii value for the escape key */
  72.  
  73. /* Global Variables */
  74.  
  75. static GLuint         envmode = 0;
  76.  
  77. static GLfloat        white[] = { 1.0, 1.0, 1.0, 1.0 };
  78. static GLfloat        red[] = { 1.0, 0.0, 0.0, 1.0 };
  79. static GLfloat        water[] = { 0.0, 0.3, 0.5, 1.0 };
  80.  
  81. static GLboolean     useWhite = GL_FALSE;
  82.  
  83. typedef struct {
  84.     GLint    type;
  85.     char    *name;
  86. } EnvModeInfo;
  87.  
  88. static EnvModeInfo modes[4] = {
  89.     { GL_DECAL, "GL_DECAL" },
  90.     { GL_MODULATE, "GL_MODULATE" },
  91.     { GL_BLEND, "GL_BLEND" },
  92.     { GL_REPLACE_EXT, "GL_REPLACE_EXT" },
  93. };
  94.  
  95. void
  96. main ( int argc, char *argv[])
  97. {
  98.     GLsizei     width, height;
  99.     char        *imageFileName = "fish.rgba";
  100.     unsigned int     *image;
  101.     GLsizei        imageWidth, imageHeight;
  102.     
  103.     glutInit( &argc, argv );
  104.  
  105.     if (argc < 2) {
  106.         fprintf (stderr, "usage: %s <imageFileName>\n", argv[0] );
  107.     } else
  108.         imageFileName = argv[1];
  109.  
  110.     fprintf(stdout, "using image %s\n\n", imageFileName );
  111.  
  112.     image = rgbReadImageFile(imageFileName, &imageWidth,
  113.              &imageHeight);
  114.  
  115.     /* create a window that is 1/4 the size of the screen */
  116.  
  117.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  118.     height = glutGet( GLUT_SCREEN_HEIGHT );
  119.     glutInitWindowPosition( (width / 2) + 4, height / 4 );
  120.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  121.     glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
  122.     glutCreateWindow( argv[0] );
  123.  
  124.     initTexture( image, imageWidth, imageHeight );
  125.     initgfx();
  126.  
  127.     glutKeyboardFunc( keyboard );
  128.     glutReshapeFunc( reshape );
  129.     glutDisplayFunc( drawScene ); 
  130.  
  131.     printHelp( argv[0] );
  132.  
  133.     glutMainLoop();
  134. }
  135.  
  136. void
  137. printHelp( char *progname )
  138. {
  139.     fprintf(stdout, "\n%s - demonstrates texture environment modes\n\n" 
  140.         "<c> Key    - toggle polygon color between white/red \n"
  141.         "<e> Key    - cycle through texture environment modes\n"
  142.         "Escape Key    - exit the program\n\n",
  143.         progname);
  144.     printf("\nTexture Environment Mode is %s\n", modes[envmode].name);
  145. }
  146.  
  147. GLvoid 
  148. initgfx()
  149. {
  150.     glClearColor( water[0], water[1], water[2], water[3] );
  151. }
  152.  
  153. GLvoid 
  154. initTexture( unsigned int *image, 
  155.     GLsizei imageWidth, GLsizei imageHeight )
  156. {
  157.     /* use gluBuild2DMipmaps to create and load mipmaps (it will 
  158.      * also scale he original image to be a power of two, if 
  159.      * necessary)
  160.      *
  161.      *      gluBuild2DMipmaps( target, components, width, height,
  162.      *              format,  type, imageArray ) 
  163.      */
  164.  
  165.     gluBuild2DMipmaps( GL_TEXTURE_2D, 4, imageWidth, imageHeight,
  166.              GL_RGBA, GL_UNSIGNED_BYTE, image );
  167.  
  168.     /* Setting the magnification filter to nearest instead of linear 
  169.      * may run faster on some platforms, with possibly lower quality 
  170.      */
  171.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  172.  
  173.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, modes[envmode].type);
  174.  
  175.     /* Used when tex env mode is GL_BLEND */
  176.     glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, water );
  177.  
  178.     /* enable 2D texture mapping */
  179.     glEnable( GL_TEXTURE_2D );
  180. }
  181.  
  182. GLvoid
  183. reshape( GLsizei width, GLsizei height )
  184. {
  185.     GLdouble      aspect;
  186.  
  187.     glViewport( 0, 0, width, height );
  188.  
  189.     aspect = (GLdouble) width / (GLdouble) height;
  190.  
  191.     glMatrixMode( GL_PROJECTION );
  192.     glLoadIdentity();
  193.     gluPerspective( 45.0, aspect, 1.0, 50.0 );
  194.     glMatrixMode( GL_MODELVIEW );
  195.     glLoadIdentity();
  196.     glTranslatef( 0.0, 0.0, -12.0 ); 
  197. }
  198.  
  199. GLvoid
  200. cycleTexEnvMode( GLvoid )
  201. {
  202.     envmode = (envmode + 1) % 4;
  203.     
  204.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, modes[envmode].type);
  205.     printf("Texture Environment Mode is %s\n", modes[envmode].name );
  206. }
  207.  
  208. GLvoid 
  209. keyboard( GLubyte key, GLint x, GLint y )
  210. {
  211.     switch (key) {
  212.     case 'c':    /* toggle polygon color */
  213.         useWhite = !useWhite;
  214.         glutPostRedisplay();
  215.         break;
  216.     case 'e':    /* cycle through texture environment modes */
  217.         cycleTexEnvMode();
  218.         glutPostRedisplay();
  219.         break;
  220.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  221.         exit(0);
  222.     }
  223. }
  224.  
  225. GLvoid
  226. drawScene(void)
  227. {
  228.     static float v0[3] = { -1.5, -1.0, 0.0 };
  229.     static float v1[3] = {  1.5, -1.0, 0.0 };
  230.     static float v2[3] = {  1.5,  1.0, 0.0 };
  231.     static float v3[3] = { -1.5,  1.0, 0.0 };
  232.  
  233.     static float t0[2] = { 0.0, 0.0 };
  234.     static float t1[2] = { 1.0, 0.0 };
  235.     static float t2[2] = { 1.0, 1.0 };
  236.     static float t3[2] = { 0.0, 1.0 };
  237.  
  238.     glClear( GL_COLOR_BUFFER_BIT );
  239.  
  240.     if (useWhite) {
  241.         glColor4fv( white );
  242.     } else {
  243.         /* Make the polygon RED so that we can see what happens
  244.          * in modulate and blend mode; usually, you would make
  245.          * the polygon white.
  246.          */ 
  247.         glColor4fv( red );
  248.     } 
  249.     glPushMatrix ();
  250.  
  251.         glBegin( GL_QUADS );
  252.             glTexCoord2fv( t0 ); glVertex3fv( v0 );
  253.             glTexCoord2fv( t1 ); glVertex3fv( v1 );
  254.             glTexCoord2fv( t2 ); glVertex3fv( v2 );
  255.             glTexCoord2fv( t3 ); glVertex3fv( v3 );
  256.         glEnd();
  257.  
  258.     glPopMatrix ();
  259.  
  260.     glutSwapBuffers();
  261. }
  262.