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_terran2.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  7.0 KB  |  222 lines

  1. /* Was terran.sl -- changed color spline to not use white 
  2.   -- tal@SpamSucks_cs.caltech.edu */
  3.   
  4. /*
  5.  * terran.sl - surface for an Earth-like planet.
  6.  *
  7.  *
  8.  * DESCRIPTION:
  9.  *      When put on a sphere, sets the color to look like relatively
  10.  *   Earth-like.  The shader works by using a variety of fractal 
  11.  *   turbulence and mottling techniques.
  12.  *      Note that there is a companion displacement shader "terranbump"
  13.  *   which is necessary to get the best effect.  If you do this, it is
  14.  *   important that the parameters common to "terran" and "terranbump"
  15.  *   both be set to the same values.  Otherwise you get bumpy mountains
  16.  *   in the middle of the ocean.
  17.  *
  18.  *
  19.  * PARAMETERS:
  20.  *    Ka, Kd - the usual meaning
  21.  *    spectral_exp, lacunarity, octaves - control the fractal characteristics
  22.  *                of the bump pattern.
  23.  *    bump_scale - scaling of the mountains
  24.  *    multifractal - zero uses fBm noise, nonzero uses multifractal
  25.  *    dist_scale - scaling for multifractal distortion
  26.  *    offset - elevation offset
  27.  *    sea_level - obvious
  28.  *    mtn_scale - scaling factor for mountains
  29.  *    lat_scale, nonlinear, purt_scale, map_exp - control scaling of 
  30.  *               terrain type by latitude
  31.  *    ice_caps - latitude at which ice caps tend to form on the oceans
  32.  *    depth_scale, depth_max - scaling factor and max depth of oceans
  33.  *    mottle_limit, mottle_scale, moddle_dim, mottle_mag - control the
  34.  *               mottling that adds detail to lower latitude regions.
  35.  *
  36.  *
  37.  * HINTS:
  38.  *       The default values for the shader assume that the planet is
  39.  *    represented by a unit sphere.  The texture space and/or parameters
  40.  *    to this shader will need to be altered if the size of your planet
  41.  *    is radically different.
  42.  *       For best results, use with the "terranbump" displacement shader,
  43.  *    and add a cloud layer using either "planetclouds" or "venusclouds".
  44.  *
  45.  *
  46.  * AUTHOR: Ken Musgrave.
  47.  *    Conversion to Shading Language and minor modifications by Larry Gritz.
  48.  *
  49.  *
  50.  * REFERENCES:
  51.  *
  52.  *
  53.  * HISTORY:
  54.  *    ???? - original texture developed by F. Ken Musgrave.
  55.  *    Feb 1994 - Conversion to Shading Language by L. Gritz
  56.  *    1 March 1994 by lg
  57.  *    May 28 1995 Didn't want polar caps so removed white from color spline --
  58.  *       tal@SpamSucks_cs.caltech.edu
  59.  * 
  60.  */
  61.  
  62. #include "k3d_noises.h"
  63. #include "k3d_constants.h"
  64.  
  65. #define N_OFFSET 0.7
  66.  
  67. surface
  68. k3d_terran2 (float Ka = .5, Kd = .7;
  69.       float spectral_exp = 0.5;
  70.       float lacunarity = 2, octaves = 7;
  71.       float bump_scale = 0.07;
  72.       float multifractal = 0;
  73.       float dist_scale = .2;
  74.       float offset = 0;
  75.       float sea_level = 0;
  76.       float mtn_scale = 1;
  77.       float lat_scale = 0.95;
  78.       float nonlinear = 0;
  79.       float purt_scale = .9;
  80.       float map_exp = 0;
  81.       float ice_caps = 0.9;
  82.       float depth_scale = 1;
  83.       float depth_max = .5;
  84.       float mottle_limit = 0.75;
  85.       float mottle_scale = 20;
  86.       float mottle_dim = .25;
  87.       float mottle_mag = .02;)
  88. {
  89.   point PP;
  90.   point PtN;
  91.   float chaos, latitude, purt;
  92.   color Ct;
  93.   point Ptexture, tp;
  94.   float l, o, a, i, weight;      /* Loop variables for fBm calc */
  95.   float bumpy;
  96.  
  97.   /* Do all shading in shader space */
  98.   Ptexture = transform ("shader", P);
  99.   PtN = normalize (Ptexture);      /* Version of Ptexture with radius 1 */
  100.  
  101.   /**********************************************************************
  102.    * First, figure out where we are in relation to the oceans/mountains.
  103.    * Note: this section of code must be identical to "terranbump" if you
  104.    *       expect these two shaders to work well together.
  105.    **********************************************************************/
  106.  
  107.   if (multifractal == 0) {    /* use a "standard" fBm bump function */
  108.       o = 1;  l = 1;  bumpy = 0;
  109.       for (i = 0;  i < octaves;  i += 1) {
  110.       bumpy += o * snoise2 (l * Ptexture);
  111.       l *= lacunarity;
  112.       o *= spectral_exp;
  113.         }
  114.     }
  115.   else {            /* use a "multifractal" fBm bump function */
  116.       /* get "distortion" vector, as used with clouds */
  117.       Ptexture += dist_scale * DNoise (Ptexture);
  118.       /* compute bump vector using MfBm with displaced point */
  119.       o = spectral_exp;  tp = Ptexture;
  120.       weight = abs (VLNoise (tp, 1.5));
  121.       bumpy = weight * snoise2 (tp);
  122.       for (i = 1;  i < octaves  &&  weight >= VERY_SMALL;  i += 1) {
  123.       tp *= lacunarity;
  124.       /* get subsequent values, weighted by previous value */
  125.       weight *= o * (N_OFFSET + snoise2(tp));
  126.       weight = clamp (abs(weight), 0, 1);
  127.       bumpy += snoise2(tp) * min (weight, spectral_exp);
  128.       o *= spectral_exp;
  129.     }
  130.     }
  131.  
  132.   /* get the "height" of the bump, displacing by offset */
  133.   chaos = bumpy + offset;
  134.   /* set bump for land masses (i.e., areas above "sea level") */
  135.   if (chaos > sea_level) {
  136.       chaos *= mtn_scale;
  137. /*      sea_level *= mtn_scale; */
  138.     }
  139.  
  140.  
  141.   /************************************************************************
  142.    * Step 2: Assign a climite type, roughly by latitude.
  143.    ************************************************************************/
  144.  
  145.   /* make climate symmetric about equator -- use the "v" parameter */
  146.   latitude = abs (zcomp (PtN));
  147.  
  148.   /* fractally purturb color map offset using "chaos" */
  149.   /*  "nonlinear" scales purturbation-by-z */
  150.   /*  "purt_scale" scales overall purturbation */
  151.   latitude += chaos*(nonlinear*(1-latitude) + purt_scale);
  152.   if (map_exp > 0)
  153.        latitude = lat_scale * pow(latitude,map_exp);
  154.   else latitude *= lat_scale;
  155.  
  156.  
  157.   if (chaos > sea_level) {
  158.       /* Choose color of land based on the following spline.
  159.        * Ken originally had a huge table.  I was too lazy to type it in,
  160.        * so I used a scanned photo of the real Earth to select some
  161.        * suitable colors.  -- lg
  162.        */
  163. /*
  164.       Ct = spline (latitude,
  165.            color (.529, .412, .2745),
  166.            color (.529, .412, .2745),
  167.            color (.529, .412, .2745),
  168.            color (.255, .341,  0),
  169.            color (.256, .341, .141),
  170.            color (.235, .392, .235),
  171.            color (.490, .494, .1176),
  172.            color (.655, .529, .392),
  173.            color (.769, .616, .314),
  174.            color (.976, .820, .471),
  175.            color (1,1,1),
  176.            color (1,1,1));
  177. */
  178.       Ct = spline (latitude,
  179.            color (.5, .39, .2),
  180.            color (.5, .39, .2),
  181.            color (.5, .39, .2),
  182.            color (.2, .3,  0),
  183.            color (.085, .2, .04),
  184.            color (.065, .22, .04),
  185.            color (.5, .42, .28),
  186.            color (.6, .5, .23),
  187.            color (.976, .820, .471), 
  188.         /*   color (.976, .820, .471), */ 
  189.            color (1,1,1) /* ,
  190.            color (1,1,1) */);
  191.  
  192.      /* mottle the color some */
  193.      if (latitude < mottle_limit) {
  194.          PP = mottle_scale * Ptexture;
  195.      l = 1;  o = 1;
  196.      for (i = 0;  i < 6;  i += 1) {
  197.          purt += o * snoise2 (l * PP);
  198.          l *= 2;
  199.          o *= mottle_dim;
  200.        }
  201.      Ct += (mottle_mag * purt) * (color (0.5,0.175,0.5));
  202.        }
  203.     }
  204.   else { 
  205.       /* Oceans */
  206.       Ct = color(.1,.2,.5);
  207.       if (ice_caps > 0  &&  latitude > ice_caps)
  208.       Ct = color(1,1,1);  /* Ice color */
  209.       else {
  210.       /* Adjust color of water to darken deeper seas */
  211.           chaos -= sea_level;
  212.       chaos *= depth_scale;
  213.       chaos = max (chaos, -depth_max);
  214.       Ct *= (1+chaos);
  215.         }
  216.     }
  217.  
  218.   /* Shade using matte model */
  219.   Oi = 1;
  220.   Ci = Os * Ct * (Ka * ambient() + Kd * diffuse(faceforward(normalize(N),I)));
  221. }
  222.