home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / OpenGL / stonehenge / atmosphere.c++ next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.6 KB  |  168 lines

  1. /*
  2.  * (c) Copyright 1993, 1994, 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. #include <GL/glu.h>
  38. #include <GL/glx.h>
  39.  
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43.  
  44. #define ATMOSPHERE_EXTERN
  45. #include "atmosphere.h"
  46.  
  47. const Weather clear("Clear", 0.);
  48. const Weather foggy("Foggy", .04, .5, white, Color(.6, .6, 1), white);
  49. const Weather very_foggy("Very Foggy", .25, .5, white, white, white);
  50. const Weather rainy("Rainy", .01, 1., black, Color(.35, .35, .35), black);
  51.  
  52. Weather weathers[nweathers] = {clear, foggy, very_foggy, rainy};
  53.  
  54. const float root3_2 = sqrt(3.) / 2.;
  55.  
  56. inline float clamp(float a, float min = 0, float max = 1)
  57. {
  58.   if (a < min) return min;
  59.   else if (a > max) return max;
  60.   else return a;
  61. }
  62.  
  63. Weather::Weather()
  64. {
  65.   strcpy(name, "No name");
  66.   fog_density = 0;
  67.   fog_color = white;
  68.   fog_spread = 1;
  69.   sun_brightness = 1;
  70.   light_sun = GL_LIGHT0;
  71.   light_ambient = GL_LIGHT1;
  72.   sky_top = blue;
  73.   sky_bottom = white;
  74. }
  75.  
  76. Weather::Weather(const char *n, GLfloat fd, GLfloat fs, Color fc, 
  77.          Color s1, Color s2, GLfloat sb,
  78.          GLenum ls, GLenum la)
  79. {
  80.   strcpy(name, n);
  81.   fog_density = fd;
  82.   fog_color = fc;
  83.   fog_spread = fs;
  84.   sun_brightness = sb;
  85.   light_sun = ls;
  86.   light_ambient = la;
  87.   sky_top = s1;
  88.   sky_bottom = s2;
  89. }
  90.  
  91. Weather::~Weather()
  92. {
  93. }
  94.  
  95. Weather Weather::operator=(Weather a)
  96. {
  97.   strcpy(name, a.name);
  98.  
  99.   fog_density = a.fog_density;
  100.   fog_color = a.fog_color;
  101.   fog_spread = a.fog_spread;
  102.  
  103.   sun_brightness = a.sun_brightness;
  104.  
  105.   light_sun = a.light_sun;
  106.   light_ambient = a.light_ambient;
  107.  
  108.   sky_top = a.sky_top;
  109.   sky_bottom = a.sky_bottom;
  110.  
  111.   return *this;
  112. }
  113.  
  114. void Weather::apply(Point sun)
  115. {
  116.   Color c;
  117.  
  118.   if (fog_density != 0) {
  119.     glFogf(GL_FOG_DENSITY, fog_density);
  120.     glFogfv(GL_FOG_COLOR, fog_color.c);
  121.     glFogi(GL_FOG_MODE, GL_EXP2);
  122.   }
  123.   glLightfv(light_sun, GL_AMBIENT, black.c);
  124.   c = white;
  125.   c *= sun_brightness;
  126.   glLightfv(light_sun, GL_DIFFUSE, c.c);
  127.   glLightfv(light_sun, GL_SPECULAR, white.c);
  128.   if (sun.pt[2] >= 0.0) glEnable(light_sun);
  129.   else glDisable(light_sun);
  130.  
  131.   c = white;
  132.   c *= .25;
  133.   glLightfv(light_ambient, GL_AMBIENT, c.c);
  134.   glLightfv(light_ambient, GL_DIFFUSE, black.c);
  135.   glLightfv(light_ambient, GL_SPECULAR, black.c);
  136.   glEnable(light_ambient);
  137. }
  138.  
  139. void Weather::draw_sky(Point sun)
  140. {
  141.   Point p;
  142.   float c;
  143.   Color bottom, top;
  144.  
  145.   if (sun.pt[2] >= .5) c = 1.;
  146.   else if (sun.pt[2] >= -.5) 
  147.     c = sqrt(1. - sun.pt[2]*sun.pt[2])*.5 + sun.pt[2]*root3_2;
  148.   else c = 0;
  149.  
  150.   bottom = sky_bottom * c;
  151.   top = sky_top * c;
  152.  
  153.   /* This is drawn as a far-away quad */
  154.   glBegin(GL_QUADS);
  155.   glColor3fv(bottom.c);
  156.   glVertex3f(-1, -1, -1);
  157.   glVertex3f(1, -1, -1);
  158.   glColor3fv(top.c);
  159.   glVertex3f(1, 1, -1);
  160.   glVertex3f(-1, 1, -1);
  161.   glEnd();
  162. }
  163.  
  164. GLfloat Weather::shadow_blur()
  165. {
  166.   return clamp(fog_density * 10.);
  167. }
  168.