home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 3 / Amiga Tools 3.iso / grafik / raytracing / rayshade-4.0.6.3 / libray / libsurf / mist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-09  |  1.8 KB  |  79 lines

  1. /*
  2.  * mist.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * mist.c,v 4.1 1994/08/09 08:01:35 explorer Exp
  17.  *
  18.  * mist.c,v
  19.  * Revision 4.1  1994/08/09  08:01:35  explorer
  20.  * Bump version to 4.1
  21.  *
  22.  * Revision 1.1.1.1  1994/08/08  04:52:13  explorer
  23.  * Initial import.  This is a prerelease of 4.0.6enh3, or 4.1 possibly.
  24.  *
  25.  * Revision 4.0  91/07/17  14:40:42  kolb
  26.  * Initial version.
  27.  * 
  28.  */
  29. #include "atmosphere.h"
  30. #include "mist.h"
  31.  
  32. Mist *
  33. MistCreate(color, trans, zero, scale)
  34. Color *color, *trans;
  35. Float zero, scale;
  36. {
  37.     Mist *mist;
  38.  
  39.     mist = (Mist *)Malloc(sizeof(Mist));
  40.     mist->color = *color;
  41.     mist->trans = *trans;
  42.     mist->zero = zero;
  43.     mist->scale = 1. / scale;
  44.     return mist;
  45. }
  46.  
  47. /*
  48.  * Add low-altitude mist to the given color.
  49.  */
  50. void
  51. MistApply(mist, ray, pos, dist, color)
  52. Mist *mist;
  53. Ray *ray;
  54. Vector *pos;
  55. Float dist;
  56. Color *color;
  57. {
  58.     Float deltaZ, d, atten;
  59.     extern Float ExpAtten();
  60.  
  61.     deltaZ = mist->scale * (pos->z - ray->pos.z);
  62.     if (fabs(deltaZ) > EPSILON)
  63.         d = (exp(-ray->pos.z*mist->scale + mist->zero) -
  64.                 exp(-pos->z*mist->scale + mist->zero)) / deltaZ;
  65.     else
  66.         d = exp(-pos->z*mist->scale + mist->zero);
  67.  
  68.     dist *= d;
  69.  
  70.     atten = ExpAtten(dist, mist->trans.r);
  71.     color->r = atten*color->r + (1. - atten)*mist->color.r;
  72.  
  73.     atten = ExpAtten(dist, mist->trans.g);
  74.     color->g = atten*color->g + (1. - atten)*mist->color.g;
  75.  
  76.     atten = ExpAtten(dist, mist->trans.b);
  77.     color->b = atten*color->b + (1. - atten)*mist->color.b;
  78. }
  79.