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_brickanti.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  2.2 KB  |  83 lines

  1. /* I took wave's lead and renamed brickant to DPBrickAnti.sl -- tal@SpamSucks_cs.caltech.edu */
  2.  
  3. /* 
  4.  * brickant.sl
  5.  *
  6.  * AUTHOR: Darwyn Peachy
  7.  *
  8.  * REFERENCES:
  9.  *    _Texturing and Modeling: A Procedural Approach_, by David S. Ebert, ed.,
  10.  *    F. Kenton Musgrave, Darwyn Peachey, Ken Perlin, and Steven Worley.
  11.  *    Academic Press, 1994.  ISBN 0-12-228760-6.
  12.  */
  13.  
  14.  
  15. #include "k3d_proctext.h"
  16. #include "k3d_functions.h"
  17.  
  18. #define BRICKWIDTH      0.25
  19. #define BRICKHEIGHT     0.08
  20. #define MORTARTHICKNESS 0.02
  21.  
  22. #define BMWIDTH         (BRICKWIDTH+MORTARTHICKNESS)
  23. #define BMHEIGHT        (BRICKHEIGHT+MORTARTHICKNESS)
  24. #define MWF             (MORTARTHICKNESS*0.5/BMWIDTH)
  25. #define MHF             (MORTARTHICKNESS*0.5/BMHEIGHT)
  26.  
  27. surface
  28. k3d_brickanti(
  29.     uniform float Ka = 1;
  30.     uniform float Kd = 1;
  31.     uniform color Cbrick = color (0.5, 0.15, 0.14);
  32.     uniform color Cmortar = color (0.5, 0.5, 0.5);
  33.      )
  34. {
  35.     color Ct;
  36.     point Nf;
  37.     float ss, tt, sbrick, tbrick, w, h;
  38.     float scoord = s;
  39.     float tcoord = t;
  40.     float swidth, twidth;
  41.  
  42.     Nf = normalize(faceforward(N, I));
  43.  
  44.     ss = scoord / BMWIDTH;
  45.     tt = tcoord / BMHEIGHT;
  46.  
  47.     if (mod(tt*0.5,1) > 0.5)
  48.         ss += 0.5;  /* shift alternate rows */
  49.  
  50.     swidth = abs(Du(ss)*du) + abs(Dv(ss)*dv);
  51.     twidth = abs(Du(tt)*du) + abs(Dv(tt)*dv);
  52.     tbrick = floor(tt); /* which brick? */
  53.     sbrick = floor(ss); /* which brick? */
  54.  
  55. #if 0
  56.     /* This is the simple antialiasing with "boxstep" */
  57.     ss -= sbrick;
  58.     tt -= tbrick;
  59.  
  60.     w = boxstep(MWF-swidth,MWF,ss)
  61.       - boxstep(1-MWF-swidth,1-MWF,ss);
  62.     h = boxstep(MHF-twidth,MHF,tt)
  63.       - boxstep(1-MHF-twidth,1-MHF,tt);
  64.  
  65. #else
  66.     /* This is the preferred antialiasing using integrals. */
  67. #define frac(x)        mod((x),1)
  68. #define sintegral(ss)  (floor(ss)*(1-2*MWF) + \
  69.                         max(0,frac(ss)-MWF))
  70. #define tintegral(tt)  (floor(tt)*(1-2*MHF) + \
  71.                         max(0,frac(tt)-MHF))
  72.  
  73.     w = (sintegral(ss+swidth) - sintegral(ss))/swidth;
  74.     h = (tintegral(tt+twidth) - tintegral(tt))/twidth;
  75. #endif
  76.  
  77.     Ct = mix(Cmortar, Cbrick, w*h);
  78.  
  79.     /* diffuse reflection model */
  80.     Oi = Os;
  81.     Ci = Os * Ct * (Ka * ambient() + Kd * diffuse(Nf));
  82. }
  83.