home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / share / k3d / shaders / surface / k3d_planetclouds.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  3.4 KB  |  106 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. #include "k3d_noises.h"
  57. #include "k3d_constants.h"
  58.  
  59.  
  60.  
  61. surface k3d_planetclouds(float Ka = 0.5, Kd = 0.75;
  62.              float distortionscale = 1; float omega = 0.7;
  63.              float lambda = 2; float octaves = 9;
  64.              float offset = 0;)
  65. {
  66.   point Pdistortion;        /* "distortion" vector */
  67.   point PP;            /* Point after distortion */
  68.   float l, o, a, i;        /* Loop control for fractal sum */
  69.   float result;            /* Fractal sum is stored here */
  70.  
  71.   /* Transform to texture coordinates */
  72.   PP = transform("shader", P);
  73.  
  74.   /* Add in "distortion" vector */
  75.   Pdistortion = distortionscale * DNoise(PP);
  76.   /* Second cirrus: replace DNoise with vector fBm */
  77.   PP = PP + Pdistortion;
  78.  
  79.   /* Compute VLfBm */
  80.   l = 1;
  81.   o = 1;
  82.   a = 0;
  83.   for(i = 0; i < octaves && o >= VERY_SMALL; i += 1)
  84.     {
  85.       a += o * VLNoise(PP * l, 1);
  86.       l *= lambda;
  87.       o *= omega;
  88.     }
  89.   result = a;
  90.  
  91.   /* Adjust zero crossing (where the clouds disappear) */
  92.   result += offset;
  93.  
  94.   if(result < 0)
  95.     result = 0;
  96.  
  97.   /* Scale density */
  98.   result /= (1 + offset);
  99.  
  100.   /* Modulate surface opacity by the cloud value */
  101.   Oi = result * Os;
  102.  
  103.   /* Shade like matte, but with color scaled by cloud opacity */
  104.   Ci = Oi * (Ka * ambient() + Kd * diffuse(faceforward(normalize(N), I)));
  105. }
  106.