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 / sgi_extensions / fog_func.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  7.8 KB  |  336 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. /* fog_func.c
  19.  *    This program demonstrates using the fog_func extension to
  20.  *    define a fog blending function.
  21.  *
  22.  *    If the fog mode is GL_EXP or GL_EXP2, pressing the up and down 
  23.  *    arrow keys adjusts the density value.
  24.  *
  25.  *    If the fog mode is GL_LINEAR, pressing the up and down 
  26.  *    arrow keys adjusts the end value for linear fog. The start value 
  27.  *    for the linear fog is fixed.
  28.  *
  29.  *    If the fog mode is GL_FOG_FUNC_SGI, an application defined
  30.  *    fog function is used.
  31.  *
  32.  *    Escape key    - exit the program
  33.  *    <f> key        - change the fog blend function
  34.  *    UP Arrow Key    - increase the fog density or end value
  35.  *    DOWN Arrow Key    - decrease the fog density or end value
  36.  */
  37. #include <GL/gl.h>
  38. #include <GL/glu.h>
  39. #include <GL/glut.h>
  40.  
  41. #include <math.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44.  
  45. /* Function Prototypes */
  46.  
  47. GLvoid  initgfx( GLvoid );
  48. GLvoid  drawScene( GLvoid );
  49. GLvoid  reshape( GLsizei, GLsizei );
  50. GLvoid  animate( GLvoid );
  51. GLvoid  visibility( GLint );
  52. GLvoid  keyboard( GLubyte, GLint, GLint );
  53. GLvoid  specialkeys( GLint, GLint, GLint );
  54.  
  55. GLvoid cycleFog( GLvoid );
  56. GLvoid inc( GLvoid );
  57. GLvoid dec( GLvoid );
  58.  
  59. void printHelp( char * );
  60.  
  61. /* Global Definitions */
  62.  
  63. #define KEY_ESC    27    /* ascii value for the escape key */
  64.  
  65. /* Global Variables */
  66.  
  67. static GLint    fogMode;
  68. static GLfloat    fogDensity;
  69. static GLfloat    fogStart = 5.0;
  70. static GLfloat    fogEnd = 11.0;
  71.  
  72. static GLboolean fogFuncSupported = GL_TRUE;
  73.  
  74. static GLfloat angle = 80.0;
  75.  
  76. void
  77. main( int argc, char *argv[] )
  78. {
  79.     GLsizei     width, height;
  80.  
  81.     glutInit( &argc, argv );
  82.  
  83.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  84.     height = glutGet( GLUT_SCREEN_HEIGHT );
  85.     glutInitWindowPosition( width/4, height/4 ); 
  86.     glutInitWindowSize( width/2, height/2 );
  87.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  88.     glutCreateWindow( argv[0] );
  89.     
  90.     initgfx();
  91.  
  92.     glutKeyboardFunc( keyboard );
  93.     glutSpecialFunc( specialkeys );
  94.     glutReshapeFunc( reshape );
  95.     glutIdleFunc( animate ); 
  96.     glutVisibilityFunc( visibility ); 
  97.     glutDisplayFunc( drawScene ); 
  98.  
  99.     printHelp( argv[0] );
  100.     printf("\nFog mode is GL_EXP, density = %4.2f\n", fogDensity);
  101.  
  102.     glutMainLoop();
  103. }
  104.  
  105. GLvoid
  106. printHelp( char *progname )
  107. {
  108.     fprintf(stdout,"\n%s - demonstrates fog\n\n"\
  109.         "Escape key        - exit the program\n"
  110.         "<f> key            - change fog blend function\n"
  111.         "UP Arrow Key        - increase fog density or end value\n"
  112.         "DOWN Arrow Key        - decrease fog density or end value\n",
  113.         progname
  114.     );
  115. }
  116.  
  117. /* Initialize lighting and materials, enable depth buffer, and set
  118.  * initial fog parameters.
  119.  */
  120. GLvoid
  121. initgfx( GLvoid )
  122. {
  123.     GLfloat     fogColor[4] = { 0.5, 0.5, 0.5, 1.0 };
  124.     GLfloat     fogSpline[64];
  125.  
  126.     /* mat_specular and mat_shininess are NOT default values */
  127.     GLfloat mat_specular[] = { 0.8, 0.8, 0.8, 1.0 };
  128.     GLfloat mat_shininess[] = { 10.0 };
  129.  
  130.     /* light_position is NOT default value */
  131.     GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  132.  
  133.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  134.  
  135.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  136.     glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  137.  
  138.     glEnable(GL_LIGHTING);
  139.     glEnable(GL_LIGHT0);
  140.  
  141.     glEnable(GL_COLOR_MATERIAL);
  142.     glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
  143.  
  144.     glEnable( GL_DEPTH_TEST );
  145.  
  146.     /* set the fog function and density */
  147.     fogMode = GL_EXP;
  148.     fogDensity = 0.25;
  149.  
  150.     /* setup and enable fog */
  151.     glFogi( GL_FOG_MODE, fogMode );
  152.     glFogfv( GL_FOG_COLOR, fogColor );
  153.     glFogf( GL_FOG_DENSITY, fogDensity );
  154.     glFogf( GL_FOG_START, fogStart );
  155.     glFogf( GL_FOG_END, fogEnd );
  156.     glHint( GL_FOG_HINT, GL_DONT_CARE );
  157.     glEnable( GL_FOG );
  158.  
  159.     if (!glutExtensionSupported("GL_SGIS_fog_func")) {
  160.         fogFuncSupported = GL_FALSE;
  161.         fprintf( stderr,
  162.            "GL_SGIS_fog_func not supported on this machine\n");
  163.     }
  164.  
  165.     if (fogFuncSupported) {
  166.         /* define our own fog function */
  167.         fogSpline[0] = 0.2;    fogSpline[1] = 1.0;
  168.         fogSpline[2] = 0.8;    fogSpline[3] = 0.8;
  169.         fogSpline[4] = 1.0;    fogSpline[5] = 0.6;
  170.         fogSpline[6] = 3.0;    fogSpline[7] = 0.4;
  171.         fogSpline[8] = 10.0;    fogSpline[9] = 0.35;
  172.         fogSpline[10] = 20.0;    fogSpline[11] = 0.3;
  173.         fogSpline[12] = 50.0;    fogSpline[13] = 0.27;
  174.         fogSpline[14] = 70.0;    fogSpline[15] = 0.2;
  175.         fogSpline[16] = 90.0;    fogSpline[17] = 0.05;
  176.         fogSpline[18] = 100.0;    fogSpline[19] = 0.0;
  177. #ifdef    GL_SGIS_fog_func
  178.         glFogFuncSGIS(10, fogSpline);
  179. #endif
  180.     }
  181.  
  182.     /* clear background to the same color as the fog */
  183.     glClearColor( fogColor[0], fogColor[1], fogColor[2], fogColor[3] );
  184. }
  185.  
  186. GLvoid
  187. cycleFog( GLvoid )
  188. {
  189.     if (fogMode == GL_EXP) {
  190.         fogMode = GL_EXP2;
  191.         printf( "Fog mode is GL_EXP2\n" );
  192.     } else if (fogMode == GL_EXP2) {
  193.         fogMode = GL_LINEAR;
  194.         printf( "Fog mode is GL_LINEAR\n" );
  195.     } else if (fogMode == GL_LINEAR) {
  196. #ifdef    GL_SGIS_fog_func
  197.         if (fogFuncSupported) {
  198.             fogMode = GL_FOG_FUNC_SGIS;
  199.             printf( "Fog mode is GL_FOG_FUNC_SGIS\n" );
  200.         } else {
  201.             fogMode = GL_EXP;
  202.             printf( "Fog mode is GL_EXP\n" );
  203.         }
  204.     } else if (fogMode == GL_FOG_FUNC_SGIS) {
  205. #endif
  206.         fogMode = GL_EXP;
  207.         printf( "Fog mode is GL_EXP\n" );
  208.     }
  209.     glFogi( GL_FOG_MODE, fogMode );
  210. }
  211.  
  212. GLvoid
  213. dec( GLvoid )
  214. {
  215.     if (fogMode == GL_LINEAR) {
  216.         fogEnd -= 0.5;
  217.         if (fogEnd < fogStart) fogEnd = fogStart;
  218.         glFogf( GL_FOG_END, fogEnd );
  219.         printf( "Fog end = %4.2f\n", fogEnd );
  220.     } else {
  221.         fogDensity -= 0.05;
  222.         if (fogDensity < 0.0) fogDensity = 0.0;
  223.         glFogf( GL_FOG_DENSITY, fogDensity );
  224.         printf( "Fog density = %4.2f\n", fogDensity );
  225.     }
  226. }
  227.  
  228. GLvoid
  229. inc( GLvoid )
  230. {
  231.     if (fogMode == GL_LINEAR) {
  232.         fogEnd += 0.5;
  233.         if (fogEnd > 12.0) fogEnd = 12.0;
  234.         glFogf( GL_FOG_END, fogEnd );
  235.         printf( "Fog end = %4.2f\n", fogEnd );
  236.     } else {
  237.         fogDensity += 0.05;
  238.         if (fogDensity > 1.0) fogDensity = 1.0;
  239.         glFogf (GL_FOG_DENSITY, fogDensity);
  240.         printf ("Fog density = %4.2f\n", fogDensity);
  241.     }
  242. }
  243.  
  244. GLvoid 
  245. specialkeys( GLint key, GLint x, GLint y )
  246. {
  247.     switch (key) {
  248.     case GLUT_KEY_UP:    /* increase fog density or end */
  249.         inc();
  250.         break;
  251.     case GLUT_KEY_DOWN:    /* decrease fog density or end */
  252.         dec();
  253.         break;
  254.     }
  255.     glutPostRedisplay();
  256. }
  257.  
  258. GLvoid 
  259. keyboard( GLubyte key, GLint x, GLint y )
  260. {
  261.     switch (key) {
  262.     case 'f':    /* cycle fog mode */
  263.         cycleFog();
  264.         glutPostRedisplay();
  265.         break;
  266.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  267.         exit(0);
  268.     }
  269. }
  270.  
  271. GLvoid
  272. reshape( GLsizei width, GLsizei height )
  273. {
  274.     GLdouble    aspect;
  275.  
  276.     glViewport( 0, 0, width, height );
  277.  
  278.     aspect = (GLdouble) width / (GLdouble) height;
  279.  
  280.     glMatrixMode( GL_PROJECTION );
  281.     glLoadIdentity();
  282.     gluPerspective( 45.0, aspect, 3.0, 13.0 );
  283.     glMatrixMode( GL_MODELVIEW );
  284.     glLoadIdentity();
  285.     glTranslatef( 0.0, 0.0, -8.0 ); 
  286. }
  287.  
  288. GLvoid
  289. animate( GLvoid )
  290. {
  291.     /* update the current angle */
  292.     angle = fmodf( (angle + 0.5), 360.0 );
  293.  
  294.     /* Tell GLUT to redraw the scene */
  295.     glutPostRedisplay();
  296. }
  297.         
  298. GLvoid
  299. visibility( int state )
  300. {
  301.     if (state == GLUT_VISIBLE) {
  302.         glutIdleFunc( animate );
  303.     } else {
  304.         glutIdleFunc( NULL );
  305.     }
  306. }
  307.  
  308. GLvoid
  309. drawScene( GLvoid )
  310. {
  311.     int i,  slices = 8;
  312.  
  313.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  314.  
  315.     glPushMatrix(); 
  316.  
  317.     glRotatef( angle, 0.0, 1.0, 0.0 );
  318.  
  319.     for ( i = 0; i < slices; i++ )
  320.     {
  321.         glColor3f( i/10.0, i/10.0, 1.0 - i/10.0 ); 
  322.         glPushMatrix(); 
  323.         glRotatef( i * 360.0/slices, 0, 0, 1 );
  324.         glTranslatef( 1.5, 0.0, 0.0 );
  325.         glRotatef( i * 360.0/slices, 0, 1, 0 );
  326.         glutSolidTorus( 0.25, 0.75, 15, 31 );
  327.         glPopMatrix();
  328.     }
  329.  
  330.     glPopMatrix();
  331.  
  332.     glutSwapBuffers();
  333.  
  334.     checkError("drawScene");
  335. }
  336.