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