home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / share / k3d / shaders / displacemnt / k3d_dturb.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  749 b   |  37 lines

  1. /* dturb.sl
  2.  *
  3.  * turbulence displacement
  4.  */
  5.  
  6. #include "k3d_rmannotes.h"
  7. #include "k3d_filterwidth.h"
  8.  
  9. displacement k3d_dturb(float Km = 0.1, freq = 10, flatness = 1)
  10. {
  11.   float magnitude, layer_mag;
  12.   point PP;
  13.   float width, cutoff, fade, f, turb, maxfreq = 16;
  14.  
  15.   /* compute turbulence */
  16.  
  17.   PP = transform("shader", P) * freq;
  18.  
  19.   width = filterwidthp(PP);
  20.   cutoff = clamp(0.5 / width, 0, maxfreq);
  21.  
  22.   turb = 0;
  23.   for (f = 1; f < 0.5 * cutoff; f *= 2) 
  24.     turb += abs(snoise(PP * f)) / f;
  25.   fade = clamp(2 * (cutoff - f) / cutoff, 0, 1);
  26.   turb += fade * abs(snoise(PP * f)) / f;
  27.  
  28.   /* raise to power to create flat areas */
  29.  
  30.   magnitude = pow(turb, flatness);
  31.  
  32.   /* output */
  33.  
  34.   P += Km * magnitude * normalize(N);
  35.   N = calculatenormal(P);
  36. }
  37.