home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / macraysh.sit / Code / Source / fog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-24  |  1.6 KB  |  72 lines

  1. /*
  2.  * fog.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.  * $Id: fog.c,v 4.0 91/07/17 14:40:14 kolb Exp Locker: kolb $
  17.  *
  18.  * $Log:    fog.c,v $
  19.  * Revision 4.0  91/07/17  14:40:14  kolb
  20.  * Initial version.
  21.  * 
  22.  */
  23. #include "atmosphere.h"
  24. #include "fog.h"
  25.  
  26. Fog *
  27. FogCreate(color, trans)
  28. Color *color, *trans;
  29. {
  30.     Fog *fog;
  31.  
  32.     fog = (Fog *)Malloc(sizeof(Fog));
  33.  
  34.     if (color == (Color *)NULL)
  35.         fog->color.r = fog->color.g = fog->color.b = 0.;
  36.     else
  37.         fog->color = *color;
  38.     if (trans == (Color *)NULL)
  39.         fog->trans.r = fog->trans.g = fog->trans.b = FAR_AWAY;
  40.     else {
  41.         fog->trans = *trans;
  42.     }
  43.     return fog;
  44. }
  45.  
  46. /*
  47.  * Add fog to the given color.
  48.  */
  49. void
  50. FogApply(fog, ray, pos, dist, color)
  51. Fog *fog;
  52. Ray *ray;
  53. Vector *pos;
  54. Float dist;
  55. Color *color;
  56. {
  57.     Float atten;
  58.     extern Float ExpAtten();
  59.  
  60.     atten = ExpAtten(dist, fog->trans.r);
  61.     if (fog->trans.r == fog->trans.g && fog->trans.r == fog->trans.b) {
  62.         ColorBlend(color, &fog->color, atten, 1. - atten);
  63.         return;
  64.     }
  65.     color->r = atten*color->r + (1. - atten) * fog->color.r;
  66.  
  67.     atten = ExpAtten(dist, fog->trans.g);
  68.     color->g = atten*color->g + (1. - atten) * fog->color.g;
  69.     atten = ExpAtten(dist, fog->trans.b);
  70.     color->b = atten*color->b + (1. - atten) * fog->color.b;
  71. }
  72.