home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Graphics / graphics-16000.iso / msdos / raytrace / rayshade / src / sky.c < prev    next >
C/C++ Source or Header  |  1992-05-05  |  2KB  |  77 lines

  1. /*
  2.  * sky.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: sky.c,v 4.0 91/07/17 14:43:43 kolb Exp Locker: kolb $
  17.  *
  18.  * $Log:    sky.c,v $
  19.  * Revision 4.0  91/07/17  14:43:43  kolb
  20.  * Initial version.
  21.  * 
  22.  */
  23. #include "texture.h"
  24. #include "sky.h"
  25.  
  26. Sky *
  27. SkyCreate(scale, h, lambda, octaves, cthresh, lthresh)
  28. Float h, lambda, scale, cthresh, lthresh;
  29. int octaves;
  30. {
  31.     Sky *sky;
  32.  
  33.     sky = (Sky *)RayMalloc(sizeof(Sky));
  34.  
  35.     sky->beta = 1. + 2 * h;
  36.     sky->lambda = lambda;
  37.     sky->scale = scale;
  38.     sky->cthresh = cthresh;
  39.     sky->lthresh = lthresh;
  40.     sky->octaves = octaves;
  41.     sky->omega = pow(lambda, -0.5 * sky->beta);
  42.  
  43.     return sky;
  44. }
  45.  
  46. void
  47. SkyApply(sky, prim, ray, pos, norm, gnorm, surf)
  48. Sky *sky;
  49. Geom *prim;
  50. Ray *ray;
  51. Vector *pos, *norm, *gnorm;
  52. Surface *surf;
  53. {
  54.     Float It, maxval;
  55.  
  56.     It = fBm(pos, sky->omega, sky->lambda, sky->octaves);
  57.     maxval = 1. / (1. - sky->omega);
  58.     /*
  59.      * Map from [-maxval,maxval] to [0,1]
  60.      */
  61.     It = (maxval +  It) * 0.5/maxval;
  62.  
  63.     It = (It - sky->lthresh) / (sky->cthresh - sky->lthresh);
  64.     if (It < 0.)
  65.         It = 0;
  66.     else if (It > 1.)
  67.         It = 1;
  68.  
  69.     if (sky->scale != 0.)
  70.         It = pow(It, sky->scale);
  71.  
  72.     surf->transp = 1. - It;
  73.  
  74.     ColorScale(It, surf->diff, &surf->diff);
  75.     ColorScale(It, surf->amb, &surf->amb);
  76. }
  77.