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

  1. /*
  2.  * cloud.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: cloud.c,v 4.0 91/07/17 14:41:57 kolb Exp Locker: kolb $
  17.  *
  18.  * $Log:    cloud.c,v $
  19.  * Revision 4.0  91/07/17  14:41:57  kolb
  20.  * Initial version.
  21.  * 
  22.  */
  23. #include "texture.h"
  24. #include "cloud.h"
  25.  
  26. /*
  27.  * Gardner-style textured ellipsoid.  Designed to be used on unit spheres
  28.  * centered at the origin.  (Of course, the spheres may be transformed
  29.  * into ellipsoids, translated, etc.)
  30.  */
  31. CloudText *
  32. CloudTextCreate(scale, h, lambda, octaves, cthresh, lthresh, transcale)
  33. Float scale, h, lambda, cthresh, lthresh, transcale;
  34. int octaves;
  35. {
  36.     CloudText *cloud;
  37.     
  38.     cloud = (CloudText *)RayMalloc(sizeof(CloudText));
  39.  
  40.     cloud->beta = 1. + 2 * h;
  41.     cloud->lambda = lambda;
  42.     cloud->scale = scale;
  43.     cloud->cthresh = cthresh;
  44.     cloud->range = lthresh - cthresh;
  45.     cloud->omega = pow(lambda, -0.5 * cloud->beta);
  46.     cloud->transcale = transcale;
  47.     cloud->maxval = 1. / (1. - cloud->beta);
  48.     cloud->octaves = octaves;
  49.  
  50.     return cloud;
  51. }
  52.  
  53. void
  54. CloudTextApply(cloud, prim, ray, pos, norm, gnorm, surf)
  55. CloudText *cloud;
  56. Geom *prim;
  57. Ray *ray;
  58. Vector *pos, *norm, *gnorm;
  59. Surface *surf;
  60. {
  61.     Ray pray;
  62.     Float alpha, beta, It, dsquared, d, limb;
  63.  
  64.     /*
  65.      * Transform ray to prim. space.
  66.      */
  67.     pray = *ray;
  68.     (void)TextRayToPrim(&pray);
  69.     dsquared = dotp(&pray.pos, &pray.pos);
  70.     if (fabs(dsquared) < 1. + EPSILON) {
  71.         surf->transp = 1.;
  72.         surf->amb.r = surf->amb.g = surf->amb.b = 0.;
  73.         surf->diff.r = surf->diff.g = surf->diff.b = 0.;
  74.         return;
  75.     }
  76.     It = fBm(pos,cloud->omega,cloud->lambda,cloud->octaves);
  77.     It = (cloud->maxval + It) * 0.5/cloud->maxval;
  78.     if (It < 0.)
  79.         It = 0;
  80.     else if (It > 1.)
  81.         It = 1;
  82.     d = sqrt(dsquared);
  83.     beta = sqrt(dsquared - 1) / d;
  84.     alpha = -dotp(&pray.pos, &pray.dir) / d;
  85.     limb = (alpha - beta) / (1 - beta);
  86.     /*
  87.      * limb is 0 on the limb, 1 at the center, < 1 outside.
  88.      */
  89.     surf->transp = 1. - (It-cloud->cthresh-cloud->range*(1.-limb))/
  90.             cloud->transcale;
  91.  
  92.     if (surf->transp > 1)
  93.         surf->transp = 1.;
  94.     if (surf->transp < 0)
  95.         surf->transp = 0.;
  96.  
  97.     ColorScale((1. - surf->transp) *
  98.            (1. - cloud->scale + cloud->scale*It),
  99.             surf->diff, &surf->diff);
  100.     ColorScale(1. - surf->transp, surf->amb, &surf->amb);
  101. }
  102.