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 / fog / fog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  6.7 KB  |  293 lines

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