home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / 3D_Rendering / Musgrave_Shaders / Source / planetclouds.sl < prev    next >
Encoding:
Text File  |  1994-12-08  |  3.6 KB  |  113 lines

  1. /*
  2.  * planetclouds.sl - surface for a semi-opaque cloud layer to be put on
  3.  *                   an earth-like planetary model.
  4.  *
  5.  * DESCRIPTION:
  6.  *      When put on a sphere, sets the color & opacity of the sphere to
  7.  *   make it look like the clouds surrounding an Earth-like planet.
  8.  *      The shader works by creating a fractal turbulence function over
  9.  *   the surface, then modulating the opacity based on this function in
  10.  *   a way that looks like clouds on a planetary scale.
  11.  *
  12.  *
  13.  * PARAMETERS:
  14.  *    Ka, Kd - the usual meaning
  15.  *    distortionscale - controls the amount of texture distortion
  16.  *    omega,lambda,octaves - the fractal characteristics of the clouds
  17.  *    p4 - beats me
  18.  *    offset - controls the zero crossings (where the clouds disappear)
  19.  *
  20.  *
  21.  * HINTS:
  22.  *   1. The way this shader is typically used is to have two concentric
  23.  *      spheres represent a planet.  The inner one is colored like the
  24.  *      surface of a planet (perhaps using the "terran" shader), and the
  25.  *      outer one uses the "planetclouds" shader.
  26.  *   2. The best effects are achieved when the clouds not only occlude
  27.  *      the view of the planet, but also shadow it.  The way to do this
  28.  *      with the Blue Moon Renderer is to let the light cast shadows,
  29.  *      then declare the cloud sphere as follows:
  30.  *           AttributeBegin
  31.  *             Attribute "render" "casts_shadows" "shade"
  32.  *             Surface "planetclouds"
  33.  *             Sphere 1 -1 1 360
  34.  *           AttributeEnd
  35.  *   3. The default values for the shader assume that the planet is
  36.  *      represented by a unit sphere.  The texture space and/or parameters
  37.  *      to this shader will need to be altered if the size of your planet
  38.  *      is radically different.
  39.  *
  40.  *
  41.  * AUTHOR: Ken Musgrave
  42.  *    Conversion to Shading Language and other minor changes by Larry Gritz.
  43.  *
  44.  * REFERENCES:
  45.  *    _Texturing and Modeling: A Procedural Approach_, by David S. Ebert, ed.,
  46.  *    F. Kenton Musgrave, Darwyn Peachey, Ken Perlin, and Steven Worley.
  47.  *    Academic Press, 1994.  ISBN 0-12-228760-6.
  48.  *
  49.  * HISTORY:
  50.  *    ???? - original texture developed by Ken Musgrave.
  51.  *    Feb 1994 - Conversion to Shading Language by L. Gritz
  52.  *
  53.  * last modified 1 March 1994 by lg
  54.  */
  55.  
  56.  
  57.  
  58. #define TWOPI (2*PI)
  59.  
  60. /* Use signed Perlin noise */
  61. #define snoise(x) ((2*noise(x))-1)
  62. #define DNoise(p) (2*(point noise(p)) - point(1,1,1))
  63. #define VLNoise(Pt,scale) (snoise(DNoise(Pt)+(scale*Pt)))
  64. #define VERY_SMALL 0.001
  65.  
  66.  
  67.  
  68. surface
  69. planetclouds (float Ka = 0.5, Kd = 0.75;
  70.           float distortionscale = 1;
  71.           float omega = 0.7;
  72.           float lambda = 2;
  73.           float octaves = 9;
  74.           float offset = 0;)
  75. {
  76.   point Pdistortion;        /* "distortion" vector */
  77.   point PP;                 /* Point after distortion */
  78.   float l, o, a, i;         /* Loop control for fractal sum */
  79.   float result;             /* Fractal sum is stored here */
  80.  
  81.   /* Transform to texture coordinates */
  82.   PP = transform ("shader", P);
  83.  
  84.   /* Add in "distortion" vector */
  85.   Pdistortion = distortionscale * DNoise (PP);
  86.         /* Second cirrus: replace DNoise with vector fBm */
  87.   PP = PP + Pdistortion;
  88.  
  89.   /* Compute VLfBm */
  90.   l = 1;  o = 1;  a = 0;
  91.   for (i = 0;  i < octaves  &&  o >= VERY_SMALL;  i += 1) {
  92.       a += o * VLNoise (PP * l, 1);
  93.       l *= lambda;
  94.       o *= omega;
  95.     }
  96.   result = a;
  97.  
  98.   /* Adjust zero crossing (where the clouds disappear) */
  99.   result += offset;
  100.  
  101.   if (result < 0)
  102.       result = 0;
  103.  
  104.   /* Scale density */
  105.   result /= (1 + offset);
  106.  
  107.   /* Modulate surface opacity by the cloud value */
  108.   Oi = result * Os;
  109.  
  110.   /* Shade like matte, but with color scaled by cloud opacity */
  111.   Ci = Oi * (Ka * ambient() + Kd * diffuse(faceforward(normalize(N),I)));
  112. }
  113.