home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / book / fog.c < prev    next >
C/C++ Source or Header  |  1996-05-27  |  5KB  |  166 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 <stdio.h>
  50. #include "glaux.h"
  51.  
  52. GLint fogMode;
  53.  
  54. void cycleFog (AUX_EVENTREC *event)
  55. {
  56.     if (fogMode == GL_EXP) {
  57.     fogMode = GL_EXP2;
  58.     printf ("Fog mode is GL_EXP2\n");
  59.     }
  60.     else if (fogMode == GL_EXP2) {
  61.     fogMode = GL_LINEAR;
  62.     printf ("Fog mode is GL_LINEAR\n");
  63.     glFogf (GL_FOG_START, 1.0);
  64.     glFogf (GL_FOG_END, 5.0);
  65.     }
  66.     else if (fogMode == GL_LINEAR) {
  67.     fogMode = GL_EXP;
  68.     printf ("Fog mode is GL_EXP\n");
  69.     }
  70.     glFogi (GL_FOG_MODE, fogMode);
  71. }
  72.  
  73. /*  Initialize z-buffer, projection matrix, light source, 
  74.  *  and lighting model.  Do not specify a material property here.
  75.  */
  76. void myinit(void)
  77. {
  78.     GLfloat position[] = { 0.0, 3.0, 3.0, 0.0 };
  79.     GLfloat local_view[] = { 0.0 };
  80.  
  81.     glEnable(GL_DEPTH_TEST);
  82.     glDepthFunc(GL_LESS);
  83.  
  84.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  85.     glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
  86.  
  87.     glFrontFace (GL_CW);
  88.     glEnable(GL_LIGHTING);
  89.     glEnable(GL_LIGHT0);
  90.     glEnable(GL_AUTO_NORMAL);
  91.     glEnable(GL_NORMALIZE);
  92.     glEnable(GL_FOG);
  93.     {
  94.     GLfloat fogColor[4] = {0.5, 0.5, 0.5, 1.0};
  95.  
  96.     fogMode = GL_EXP;
  97.     glFogi (GL_FOG_MODE, fogMode);
  98.     glFogfv (GL_FOG_COLOR, fogColor);
  99.     glFogf (GL_FOG_DENSITY, 0.35);
  100.     glHint (GL_FOG_HINT, GL_DONT_CARE);
  101.     glClearColor(0.5, 0.5, 0.5, 1.0);
  102.     }
  103. }
  104.  
  105. void renderRedTeapot (GLfloat x, GLfloat y, GLfloat z)
  106. {
  107.     float mat[4];
  108.  
  109.     glPushMatrix();
  110.     glTranslatef (x, y, z);
  111.     mat[0] = 0.1745; mat[1] = 0.01175; mat[2] = 0.01175; mat[3] = 1.0;    
  112.     glMaterialfv (GL_FRONT, GL_AMBIENT, mat);
  113.     mat[0] = 0.61424; mat[1] = 0.04136; mat[2] = 0.04136;    
  114.     glMaterialfv (GL_FRONT, GL_DIFFUSE, mat);
  115.     mat[0] = 0.727811; mat[1] = 0.626959; mat[2] = 0.626959;
  116.     glMaterialfv (GL_FRONT, GL_SPECULAR, mat);
  117.     glMaterialf (GL_FRONT, GL_SHININESS, 0.6*128.0);
  118.     glColor3f(1, 1, 1);
  119.     auxSolidTeapot(1.0);
  120.     glPopMatrix();
  121. }
  122.  
  123. /*  display() draws 5 teapots at different z positions.
  124.  */
  125. void display(void)
  126. {
  127.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  128.     renderRedTeapot (-4.0, -0.5, -1.0);
  129.     renderRedTeapot (-2.0, -0.5, -2.0);
  130.     renderRedTeapot (0.0, -0.5, -3.0);
  131.     renderRedTeapot (2.0, -0.5, -4.0);
  132.     renderRedTeapot (4.0, -0.5, -5.0);
  133.     glFlush();
  134. }
  135.  
  136. void myReshape(int w, int h)
  137. {
  138.     glViewport(0, 0, w, h);
  139.     glMatrixMode(GL_PROJECTION);
  140.     glLoadIdentity();
  141.     if (w <= (h*3))
  142.     glOrtho (-6.0, 6.0, -2.0*((GLfloat) h*3)/(GLfloat) w, 
  143.         2.0*((GLfloat) h*3)/(GLfloat) w, 0.0, 10.0);
  144.     else
  145.     glOrtho (-6.0*(GLfloat) w/((GLfloat) h*3), 
  146.         6.0*(GLfloat) w/((GLfloat) h*3), -2.0, 2.0, 0.0, 10.0);
  147.     glMatrixMode(GL_MODELVIEW);
  148.     glLoadIdentity ();
  149. }
  150.  
  151. /*  Main Loop
  152.  *  Open window with initial window size, title bar, 
  153.  *  RGBA display mode, depth buffer, and handle input events.
  154.  */
  155. int main(int argc, char** argv)
  156. {
  157.     auxInitDisplayMode (AUX_SINGLE | AUX_RGB | AUX_DEPTH);
  158.     auxInitPosition (0, 0, 450, 150);
  159.     auxInitWindow (argv[0]);
  160.     auxMouseFunc (AUX_LEFTBUTTON, AUX_MOUSEDOWN, cycleFog);
  161.     myinit();
  162.     auxReshapeFunc (myReshape);
  163.     auxMainLoop(display);
  164. }
  165.  
  166.