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_gloop.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  1.8 KB  |  76 lines

  1. /*  IDGloop.sl written 8/99 by Ivan DeWolf
  2.  *  courtesy of Rhythm & Hues  (thanks!)
  3.  *  ivan@SpamSucks_martian-labs.com
  4.  *
  5.  * an example of nonlinear displacements 
  6.  * generated from the gradient of a noise function.
  7.  * Written as an explanation to the talk I gave at
  8.  * the "stupid renderman tricks" section of the 1998
  9.  * Renderman Users Group meeting at siggraph.
  10.  *
  11.  * freq - the frequency of the noise. Lower values
  12.  *        will cause a larger displacement so you will 
  13.  *        have to change your displacement bounds to fit
  14.  *        rhymes with freak.
  15.  *
  16.  * magnitude - how far to displace. probably fine set where it is.
  17.  *
  18.  * displacement bounds can be computed with the following 
  19.  * wildly complex formula:
  20.  *
  21.  * 10*magnitude/freq
  22.  * 
  23.  */
  24.  
  25. displacement
  26. k3d_gloop(float freq = 5, magnitude = .2){
  27.  
  28.   vector overdist = .1;
  29.   vector stepsize = magnitude/freq;
  30.   float numsteps = 20;
  31.   point  Psh = transform("object",P)*freq;
  32.   float  i;
  33.  
  34.   vector dPduN = normalize(vtransform("object",dPdu));
  35.   vector dPdvN = normalize(vtransform("object",dPdv));
  36.  
  37.   point  Pou = Psh + (dPduN*overdist);/*P Over a distance in U*/
  38.   point  Pov = Psh + (dPdvN*overdist);
  39.  
  40.   /*noise function at the point, over in u, and over in v*/
  41.   float  nz = noise(Psh)-.5;
  42.   float  nzou = noise(Pou)-.5;
  43.   float  nzov = noise(Pov)-.5;
  44.  
  45.   float  chu = (nz - nzou);/*change in noise value in u*/
  46.   float  chv = (nz - nzov); 
  47.  
  48.   /*init deflected derivatives*/
  49.   vector DdPdu = dPduN;
  50.   vector DdPdv = dPdvN;
  51.  
  52.   vector step = DdPdu^DdPdv;
  53.  
  54.   /*where it all happens*/
  55.   for(i=1;i<numsteps;i=i+1){
  56.     P -= vtransform("object","current",step)*nz*stepsize;
  57.     DdPdu = normalize(DdPdu+(step*chu));
  58.     DdPdv = normalize(DdPdv+(step*chv));
  59.     step = DdPdu^DdPdv;
  60.   }
  61.  
  62.   /* wash your hands after displacing P*/
  63.   N = normalize(calculatenormal(P));
  64. }
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.