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_cyclone.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  1.9 KB  |  70 lines

  1. #include "k3d_noises.h"
  2. #include "k3d_constants.h"
  3.  
  4.  
  5. surface k3d_cyclone(float Ka = 0.5, Kd = 0.75; float max_radius = 1;
  6.             float twist = 0.5; float scale = .7, offset = .5;
  7.             float omega = 0.675; float octaves = 4;
  8.   )
  9. {
  10.   float radius, dist, angle, sine, cosine, eye_weight, value;
  11.   point Pt;            /* Point in texture space */
  12.   point PN;            /* Normalized vector in texture space */
  13.   point PP;            /* Point after distortion */
  14.   float l, o, a, i;        /* Loop control for fractal sum */
  15.  
  16.   /* Transform to texture coordinates */
  17.   Pt = transform("shader", P);
  18.  
  19.   /* Rotate hit point to "cyclone space" */
  20.   PN = normalize(Pt);
  21.   radius = sqrt(xcomp(PN) * xcomp(PN) + ycomp(PN) * ycomp(PN));
  22.  
  23.   if(radius < max_radius)
  24.     {                /* inside of cyclone */
  25.       /* invert distance from center */
  26.       dist = pow(max_radius - radius, 3);
  27.       angle = PI + twist * TWOPI * (max_radius - dist) / max_radius;
  28.       sine = sin(angle);
  29.       cosine = cos(angle);
  30.       PP =
  31.     point(xcomp(Pt) * cosine - ycomp(Pt) * sine,
  32.           xcomp(Pt) * sine + ycomp(Pt) * cosine, zcomp(Pt));
  33.       /* Subtract out "eye" of storm */
  34.       if(radius < 0.05 * max_radius)
  35.     {            /* if in "eye" */
  36.       eye_weight = (.1 * max_radius - radius) * 10;    /* normalize */
  37.       /* invert and make nonlinear */
  38.       eye_weight = pow(1 - eye_weight, 4);
  39.     }
  40.       else
  41.     eye_weight = 1;
  42.     }
  43.   else
  44.     PP = Pt;
  45.  
  46.   if(eye_weight > 0)
  47.     {                /* if in "storm" area */
  48.       /* Compute VLfBm */
  49.       l = 1;
  50.       o = 1;
  51.       a = 0;
  52.       for(i = 0; i < octaves && o >= VERY_SMALL; i += 1)
  53.     {
  54.       a += o * VLNoise(PP * l, 1);
  55.       l *= 2;
  56.       o *= omega;
  57.     }
  58.       value = abs(eye_weight * (offset + scale * a));
  59.     }
  60.   else
  61.     value = 0;
  62.  
  63.   /* Thin the density of the clouds */
  64.   Oi = value * Os;
  65.  
  66.   /* Shade like matte, but with color scaled by cloud opacity */
  67.   Ci =
  68.     Oi * Cs * (Ka * ambient() + Kd * diffuse(faceforward(normalize(N), I)));
  69. }
  70.