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_water.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  3.3 KB  |  109 lines

  1. /*
  2.  * water.sl -- water surface, using ray tracing.
  3.  * This was originally shiny.sl, but I change it to make it transparant.  The interesting part colors that
  4.  * are not blue will pass thru the water.
  5.  *
  6.  * note : This is very similar to shiny.sl with a slight modification to give it a
  7.  *        water like look.
  8.  *
  9.  * modified by Lawrence D. Chin, cs184-bo
  10.  */
  11.  
  12. /* DESCRIPTION:
  13.  *   Makes a smoothly polished metal, using ray tracing to calculate
  14.  *   reflections of the environment.
  15.  * 
  16.  * PARAMETERS:
  17.  *    Ka, Kd, Ks, roughness, specularcolor - The usual meaning
  18.  *    Kr - coefficient for mirror-like reflections of environment
  19.  *    blur - how blurry are the reflections? (0 = perfectly sharp)
  20.  *    samples - set to higher than 1 for oversampling of blur
  21.  *
  22.  *
  23.  * HISTORY:
  24.  *      Aug 1991 -- written by lg in C
  25.  *      25 Jan 1994 -- recoded by lg in correct shading language.
  26.  *
  27.  * last modified 25 Jan 1994 by Larry Gritz
  28.  */
  29.  
  30. #include "k3d_rmannotes.h"
  31.  
  32. surface
  33. k3d_water ( float Ka = 0, Kd = 0, Ks = 1;
  34.     float Kr = 1, roughness = 0, blur = 0;
  35.     color specularcolor = 1;
  36.     float samples = 0,
  37.         radius = 8,           /* radius of ring */
  38.         half_width = 0.1;      /* half width of ring */
  39.       )
  40. {
  41.     /* FIRST LAYER */
  42.     
  43.     normal Nf;               /* Forward facing normal vector */
  44.     vector IN;               /* normalized incident vector */
  45.     vector uoffset, voffset; /* Offsets for blur */
  46.     color surface_color,     /* Resulting color */
  47.           ev;                /* Color of the reflections */
  48.     vector R, Rdir;          /* Direction to cast the ray */
  49.     uniform float i, j;
  50.     
  51.     /* Construct a forward facing surface normal */
  52.     Nf = faceforward (normalize(N), I);
  53.     IN = normalize (I);
  54.     ev = 0;
  55.  
  56.     /* Calculate the reflection color */
  57.     if (Kr > 0.001) {
  58.     /* Rdir gets the perfect reflection direction */
  59.     Rdir = normalize (reflect (IN, Nf));
  60.     if (blur > 0) {
  61.         /* Construct orthogonal components to Rdir */
  62.         uoffset = blur * normalize (vector (zcomp(Rdir) - ycomp(Rdir),
  63.                             xcomp(Rdir) - zcomp(Rdir),
  64.                             ycomp(Rdir) - xcomp(Rdir)));
  65.         voffset = Rdir ^ uoffset;
  66.         for (i = 0;  i < samples;  i += 1) {
  67.         for (j = 0;  j < samples;  j += 1) {
  68.             /* Add a random offset to the smooth reflection vector */
  69.             R = Rdir +
  70.             ((i + float random())/samples - 0.5) * uoffset +
  71.             ((j + float random())/samples - 0.5) * voffset;
  72.             ev += trace (P, normalize(R));
  73.         }
  74.         }
  75.         ev *= Kr / (samples*samples);
  76.     } else {
  77.         /* No blur, just do a simple trace */
  78.         ev = Kr * trace (P, Rdir);
  79.     }
  80.     }
  81.  
  82.     surface_color = Os * ( Cs * (Ka*ambient() + Kd*diffuse(Nf)) +
  83.         specularcolor * (ev + Ks*specular(Nf,-IN,roughness)));
  84.  
  85.     /* I added some current.  Note, it doesn't apppear horizontally. */
  86.     color current = (1,1,1);  
  87.    
  88.     color layer_color = (1,1,1);
  89.     color layer_opac = 1;
  90.     float fuzz = 0.025;
  91.     point center;
  92.     float d;
  93.  
  94.     center = (0.5, 0.5, 0);  /* position of ring */
  95.     d = distance(center, (s, t, 0));
  96.     layer_opac = pulse(radius - half_width, radius + half_width, fuzz, d);
  97.     surface_color = blend(surface_color, layer_color, layer_opac);
  98.  
  99.     Ci = surface_color;
  100.      
  101.     /* This gives the water a somewhat transparant look. 
  102.      *   Note: This does works with radiosity.
  103.      */
  104.  
  105.     Oi = (0, 0, 0.0000001);
  106.     
  107. }
  108.  
  109.