home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / opengl / auxdemo / fog.c < prev    next >
C/C++ Source or Header  |  1999-05-11  |  6KB  |  165 lines

  1. /*
  2.  * (c) Copyright 1993, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. /*
  38.  *  fog.c
  39.  *  This program draws 5 red teapots, each at a different 
  40.  *  z distance from the eye, in different types of fog.  
  41.  *  Pressing the left mouse button chooses between 3 types of 
  42.  *  fog:  exponential, exponential squared, and linear.  
  43.  *  In this program, there is a fixed density value, as well 
  44.  *  as fixed start and end values for the linear fog.
  45.  */
  46. #include <GL/gl.h>
  47. #include <GL/glu.h>
  48. #include <math.h>
  49. #include "aux.h"
  50.  
  51. GLint fogMode;
  52.  
  53. void cycleFog (AUX_EVENTREC *event)
  54. {
  55.     if (fogMode == GL_EXP) {
  56.     fogMode = GL_EXP2;
  57.     printf ("Fog mode is GL_EXP2\n");
  58.     }
  59.     else if (fogMode == GL_EXP2) {
  60.     fogMode = GL_LINEAR;
  61.     printf ("Fog mode is GL_LINEAR\n");
  62.     glFogf (GL_FOG_START, 1.0);
  63.     glFogf (GL_FOG_END, 5.0);
  64.     }
  65.     else if (fogMode == GL_LINEAR) {
  66.     fogMode = GL_EXP;
  67.     printf ("Fog mode is GL_EXP\n");
  68.     }
  69.     glFogi (GL_FOG_MODE, fogMode);
  70. }
  71.  
  72. /*  Initialize z-buffer, projection matrix, light source, 
  73.  *  and lighting model.  Do not specify a material property here.
  74.  */
  75. void myinit(void)
  76. {
  77.     GLfloat position[] = { 0.0, 3.0, 3.0, 0.0 };
  78.     GLfloat local_view[] = { 0.0 };
  79.  
  80.     glEnable(GL_DEPTH_TEST);
  81.     glDepthFunc(GL_LESS);
  82.  
  83.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  84.     glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
  85.  
  86.     glFrontFace (GL_CW);
  87.     glEnable(GL_LIGHTING);
  88.     glEnable(GL_LIGHT0);
  89.     glEnable(GL_AUTO_NORMAL);
  90.     glEnable(GL_NORMALIZE);
  91.     glEnable(GL_FOG);
  92.     {
  93.     GLfloat fogColor[4] = {0.5, 0.5, 0.5, 1.0};
  94.  
  95.     fogMode = GL_EXP;
  96.     glFogi (GL_FOG_MODE, fogMode);
  97.     glFogfv (GL_FOG_COLOR, fogColor);
  98.     glFogf (GL_FOG_DENSITY, 0.35);
  99.     glHint (GL_FOG_HINT, GL_DONT_CARE);
  100.     glClearColor(0.5, 0.5, 0.5, 1.0);
  101.     }
  102. }
  103.  
  104. void renderRedTeapot (GLfloat x, GLfloat y, GLfloat z)
  105. {
  106.     float mat[4];
  107.  
  108.     glPushMatrix();
  109.     glTranslatef (x, y, z);
  110.     mat[0] = 0.1745; mat[1] = 0.01175; mat[2] = 0.01175; mat[3] = 1.0;    
  111.     glMaterialfv (GL_FRONT, GL_AMBIENT, mat);
  112.     mat[0] = 0.61424; mat[1] = 0.04136; mat[2] = 0.04136;    
  113.     glMaterialfv (GL_FRONT, GL_DIFFUSE, mat);
  114.     mat[0] = 0.727811; mat[1] = 0.626959; mat[2] = 0.626959;
  115.     glMaterialfv (GL_FRONT, GL_SPECULAR, mat);
  116.     glMaterialf (GL_FRONT, GL_SHININESS, 0.6*128.0);
  117.     glColor3f(1, 1, 1);
  118.     auxSolidTeapot(1.0);
  119.     glPopMatrix();
  120. }
  121.  
  122. /*  display() draws 5 teapots at different z positions.
  123.  */
  124. void display(void)
  125. {
  126.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  127.     renderRedTeapot (-4.0, -0.5, -1.0);
  128.     renderRedTeapot (-2.0, -0.5, -2.0);
  129.     renderRedTeapot (0.0, -0.5, -3.0);
  130.     renderRedTeapot (2.0, -0.5, -4.0);
  131.     renderRedTeapot (4.0, -0.5, -5.0);
  132.     glFlush();
  133. }
  134.  
  135. void myReshape(int w, int h)
  136. {
  137.     glViewport(0, 0, w, h);
  138.     glMatrixMode(GL_PROJECTION);
  139.     glLoadIdentity();
  140.     if (w <= (h*3))
  141.     glOrtho (-6.0, 6.0, -2.0*((GLfloat) h*3)/(GLfloat) w, 
  142.         2.0*((GLfloat) h*3)/(GLfloat) w, 0.0, 10.0);
  143.     else
  144.     glOrtho (-6.0*(GLfloat) w/((GLfloat) h*3), 
  145.         6.0*(GLfloat) w/((GLfloat) h*3), -2.0, 2.0, 0.0, 10.0);
  146.     glMatrixMode(GL_MODELVIEW);
  147.     glLoadIdentity ();
  148. }
  149.  
  150. /*  Main Loop
  151.  *  Open window with initial window size, title bar, 
  152.  *  RGBA display mode, depth buffer, and handle input events.
  153.  */
  154. int main(int argc, char** argv)
  155. {
  156.     auxInitDisplayMode (AUX_SINGLE | AUX_RGB | AUX_DEPTH);
  157.     auxInitPosition (0, 0, 450, 150);
  158.     auxInitWindow (argv[0]);
  159.     auxMouseFunc (AUX_LEFTBUTTON, AUX_MOUSEDOWN, cycleFog);
  160.     myinit();
  161.     auxReshapeFunc (myReshape);
  162.     auxMainLoop(display);
  163. }
  164.  
  165.