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_terran.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  4.8 KB  |  168 lines

  1. /*
  2. mottle_limit, mottle_scale, moddle_dim, mottle_mag - control the
  3.            mottling that adds detail to lower latitude regions.
  4. */
  5.  
  6. #include "k3d_noises.h"
  7. #include "k3d_constants.h"
  8.  
  9. #define N_OFFSET 0.7
  10.  
  11. surface k3d_terran(float Ka = .5, Kd = .7;
  12.            float spectral_exp = 0.5;
  13.            float lacunarity = 2, octaves = 7;
  14.            float multifractal = 0; float dist_scale = .2;
  15.            float offset = 0;
  16.            float sea_level = 0;
  17.            float mtn_scale = 1;
  18.            float lat_scale = 0.95;
  19.            float nonlinear = 0;
  20.            float purt_scale = .9; float map_exp = 0;
  21.            float ice_caps = 0.9; float depth_scale = 1;
  22.            float depth_max = .5; float mottle_limit = 0.75;
  23.            float mottle_scale = 20; float mottle_dim = .25;
  24.            float mottle_mag = .02;)
  25. {
  26.   point PP;
  27.   point PtN;
  28.   float chaos, latitude, purt;
  29.   color Ct;
  30.   point Ptexture, tp;
  31.   float l, o, a, i, weight;    /* Loop variables for fBm calc */
  32.   float bumpy;
  33.  
  34.   /* Do all shading in shader space */
  35.   Ptexture = transform("shader", P);
  36.   PtN = normalize(Ptexture);    /* Version of Ptexture with radius 1 */
  37.  
  38.     /**********************************************************************
  39.    * First, figure out where we are in relation to the oceans/mountains.
  40.    * Note: this section of code must be identical to "terranbump" if you
  41.    *       expect these two shaders to work well together.
  42.    **********************************************************************/
  43.  
  44.   if(multifractal == 0)
  45.     {                /* use a "standard" fBm bump function */
  46.       o = 1;
  47.       l = 1;
  48.       bumpy = 0;
  49.       for(i = 0; i < octaves; i += 1)
  50.     {
  51.       bumpy += o * snoise2(l * Ptexture);
  52.       l *= lacunarity;
  53.       o *= spectral_exp;
  54.     }
  55.     }
  56.   else
  57.     {                /* use a "multifractal" fBm bump function */
  58.       /* get "distortion" vector, as used with clouds */
  59.       Ptexture += dist_scale * DNoise(Ptexture);
  60.       /* compute bump vector using MfBm with displaced point */
  61.       o = spectral_exp;
  62.       tp = Ptexture;
  63.       weight = abs(VLNoise(tp, 1.5));
  64.       bumpy = weight * snoise2(tp);
  65.       for(i = 1; i < octaves && weight >= VERY_SMALL; i += 1)
  66.     {
  67.       tp *= lacunarity;
  68.       /* get subsequent values, weighted by previous value */
  69.       weight *= o * (N_OFFSET + snoise2(tp));
  70.       weight = clamp(abs(weight), 0, 1);
  71.       bumpy += snoise2(tp) * min(weight, spectral_exp);
  72.       o *= spectral_exp;
  73.     }
  74.     }
  75.  
  76.   /* get the "height" of the bump, displacing by offset */
  77.   chaos = bumpy + offset;
  78.   /* set bump for land masses (i.e., areas above "sea level") */
  79.   if(chaos > sea_level)
  80.     {
  81.       chaos *= mtn_scale;
  82. /*      sea_level *= mtn_scale; */
  83.     }
  84.  
  85.  
  86.     /************************************************************************
  87.    * Step 2: Assign a climite type, roughly by latitude.
  88.    ************************************************************************/
  89.  
  90.   /* make climate symmetric about equator -- use the "v" parameter */
  91.   latitude = abs(ycomp(PtN));
  92.  
  93.   /* fractally purturb color map offset using "chaos" */
  94.   /*  "nonlinear" scales purturbation-by-z */
  95.   /*  "purt_scale" scales overall purturbation */
  96.   latitude += chaos * (nonlinear * (1 - latitude) + purt_scale);
  97.   if(map_exp > 0)
  98.     latitude = lat_scale * pow(latitude, map_exp);
  99.   else
  100.     latitude *= lat_scale;
  101.  
  102.  
  103.   if(chaos > sea_level)
  104.     {
  105.       /* Choose color of land based on the following spline.
  106.        * Ken originally had a huge table.  I was too lazy to type it in,
  107.        * so I used a scanned photo of the real Earth to select some
  108.        * suitable colors.  -- lg
  109.        */
  110. /*
  111.       Ct = spline (latitude,
  112.            color (.529, .412, .2745),
  113.            color (.529, .412, .2745),
  114.            color (.529, .412, .2745),
  115.            color (.255, .341,  0),
  116.            color (.256, .341, .141),
  117.            color (.235, .392, .235),
  118.            color (.490, .494, .1176),
  119.            color (.655, .529, .392),
  120.            color (.769, .616, .314),
  121.            color (.976, .820, .471),
  122.            color (1,1,1),
  123.            color (1,1,1));
  124. */
  125.       Ct =
  126.     spline(latitude, color(.5, .39, .2), color(.5, .39, .2),
  127.            color(.5, .39, .2), color(.2, .3, 0), color(.085, .2, .04),
  128.            color(.065, .22, .04), color(.5, .42, .28), color(.6, .5, .23),
  129. /*           color (.976, .820, .471), */
  130.            color(1, 1, 1), color(1, 1, 1));
  131.  
  132.       /* mottle the color some */
  133.       if(latitude < mottle_limit)
  134.     {
  135.       PP = mottle_scale * Ptexture;
  136.       l = 1;
  137.       o = 1;
  138.       for(i = 0; i < 6; i += 1)
  139.         {
  140.           purt += o * snoise2(l * PP);
  141.           l *= 2;
  142.           o *= mottle_dim;
  143.         }
  144.       Ct += (mottle_mag * purt) * (color(0.5, 0.175, 0.5));
  145.     }
  146.     }
  147.   else
  148.     {
  149.       /* Oceans */
  150.       Ct = color(.1, .2, .5);
  151.       if(ice_caps > 0 && latitude > ice_caps)
  152.     Ct = color(1, 1, 1);    /* Ice color */
  153.       else
  154.     {
  155.       /* Adjust color of water to darken deeper seas */
  156.       chaos -= sea_level;
  157.       chaos *= depth_scale;
  158.       chaos = max(chaos, -depth_max);
  159.       Ct *= (1 + chaos);
  160.     }
  161.     }
  162.  
  163.   /* Shade using matte model */
  164.   Oi = Os;
  165.   Ci =
  166.     Os * Ct * (Ka * ambient() + Kd * diffuse(faceforward(normalize(N), I)));
  167. }
  168.