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_ruledpaper.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  4.2 KB  |  145 lines

  1. /*
  2.  * EM_paper.sl -- loose leaf paper
  3.  *
  4.  * DESCRIPTION:
  5.  *   Makes a patch look like a piece of loose-leaf paper with lines,
  6.  *   the margin, binder holes and writing (as an image texture).
  7.  *   Works in s/t space.
  8.  *
  9.  * PARAMETERS:
  10.  *   Ka, Kd, Ks, specular, roughness - work just like the plastic shader
  11.  *   linestr - how strongly do the lines show up (0.0 to 1.0)
  12.  *   texturename - name of image to map onto paper (optional)
  13.  *
  14.  * ANTIALIASING: no antialiasing. This can be a real problem because of the
  15.  *               thin lines on the paper.
  16.  *
  17.  * AUTHOR: written by Emil Mikulic
  18.  *         email: darkmoon@SpamSucks_connexus.apana.org.au
  19.  *
  20.  * HISTORY:
  21.  *    8 Nov 1997 - started writing shader for RDC under MS VC++
  22.  *    9 Nov 1997 - added binder holes and finished shader
  23.  *   15 Nov 1997 - translated to Renderman SL in Linux
  24.  *
  25.  * last modified 15 Nov 1997 by Emil Mikulic
  26.  *
  27.  * COMMENTS:
  28.  *   Don't worry about the paper anatomy settings. I moved them to
  29.  *   the paper(...) block so that they get calculated on initialisation
  30.  *   (initcode) instead of being re-calced for every sample.
  31.  *
  32.  *   I don't know how many holes there were in paper when _YOU_ were a kid
  33.  *   but in my day there's 7. I actually got a piece of loose-leaf paper
  34.  *   and got all the measurements with a ruler. This is the real thing!!
  35.  *
  36.  *   The paper shader works for A4 pieces of paper, so remeber to make your
  37.  *   patch 21x29.7 units or at least with an approximate aspect ratio. 
  38.  *
  39.  */
  40.  
  41. surface
  42. k3d_ruledpaper(float Ka = 1, Kd = 0.5, Ks = 0.5;
  43.     float roughness = 0.1;
  44.     color specularcolor = 1;
  45.     float linestr = 1;
  46.     string texturename = "";
  47.     
  48.     /* Paper anatomy settings */
  49.     // Paper width
  50.     float     pw=21,
  51.     // Paper height 
  52.         ph=29.7,
  53.     // Width of plastic strip
  54.          shiny=1.25/pw,
  55.     // Spacing between lines
  56.         lines = 0.7 / ph,
  57.     // Thickness of lines
  58.         linet = 0.05 / ph,
  59.     // How far into the page do the lines start
  60.         linef = 0.5 / pw,
  61.     // Top and bottom margins for lines
  62.         margt = 2 / ph,
  63.         margb = 1 - (1.35 / ph),
  64.     // Where does the vertical line (margin) start and end?
  65.         verts = 2.75 / pw,
  66.         verte = 2.85 / pw,
  67.     // Settings for binder holes (circles)
  68.     // Circle center along page (ss coordinate system)
  69.         circms = 1.25,
  70.     // Circle radius in ss/tt coord. system
  71.         circr = 0.3,
  72.     // Bounding box for circles
  73.         boundl = (circms-circr)/pw,
  74.         boundr = (circms+circr)/pw;    )
  75. {
  76.  
  77. // Fancy macro checks ss/tt coords against bounding box and [if inside]
  78. // calculates if it's inside the circle. If it is, the colour
  79. // becomes black and the opacity becomes 0.
  80.  
  81. #define circle(sm,tm) { \
  82.     if ((tt>(tm-circr)/ph) && (tt>(tm+circr)/ph))    \
  83.     {                        \
  84.         if (distance( point(ss,tt,0), point(sm,tm,0) ) <= circr)    \
  85.             Oi = color(0,0,0);        \
  86.     }                        \
  87. }
  88.  
  89.     point Nf, V;
  90.     color tex;
  91.     float ss,tt;
  92.  
  93.     // For plastic and diffuse shading
  94.     Nf = faceforward(normalize(N), I);
  95.     V = normalize(-I);
  96.     
  97.     // Check if there's a texture given.
  98.     // Use a nice handwrititng font and you can make it look like
  99.     // there's writing on the paper.
  100.     if (texturename != "")
  101.         tex = color texture(texturename);
  102.     else
  103.         tex = Cs; // Colour of paper
  104.     
  105.     // If we're inside the borders/margins
  106.     if ( (s>linef) && (t>margt) && (t<margb) )
  107.     // Figure out where in the lining we are and draw accordingly
  108.     if ( (mod(t, lines)<=linet) || ((s>=verts) && (s<=verte)) )
  109.         tex = (tex*(1-linestr)) + (color(0.2,0.8,0.8) * linestr);
  110.     
  111.     Oi = Os;
  112.  
  113.     // Bounding box for circle calcs
  114.     if ((s>boundl) && (s<boundr)) 
  115.     {
  116.         // Figure out where we are on the paper so that we can get
  117.         // accurate, aspecty circles.
  118.         ss = s * pw;
  119.         tt = t * ph;
  120.  
  121.         // Find bounding boxes first to speed up calcs
  122.         // I wrote this handy macro to automate it!
  123.         circle(circms,2.7);
  124.         circle(circms,4.05);
  125.         circle(circms,10.7);
  126.         circle(circms,14.75);
  127.         circle(circms,18.75);
  128.         circle(circms,25.45);
  129.         circle(circms,26.75);
  130.     }
  131.  
  132.     // Check if it actually needs shading
  133.     if (Oi == color (0, 0, 0)) { Ci = 0; } else {
  134.         if (s<=shiny) {
  135.             // If inside plasticky reinforcement strip then
  136.             // use plastic shading model.
  137.             Ci = Oi * (tex * (Ka * ambient() + Kd * diffuse(Nf)) + 
  138.             specularcolor * Ks * specular(Nf, V, roughness));
  139.         } else {
  140.             // Else shade diffuse paper
  141.             Ci = Oi * tex * (Ka * ambient() + Kd * diffuse(Nf));
  142.         };
  143.     }
  144. }
  145.