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_srfdeformation.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  2.4 KB  |  106 lines

  1. /* renamed shader to SIG2k_srf_deformation to be consistent with RMR 
  2.  *    -- tal@SpamSucks_renderman.org
  3.  */
  4.  
  5.  
  6. /* 
  7.    deformation surface shader
  8.    
  9.    projects a texture through the camera onto the Pref
  10.    object and deforms it to the P position
  11.           
  12.    additionally, calculates the changes to shading on the surface 
  13.    measured by the change in diffuse lighting from the Pref to P.
  14.    
  15.    additional contrast and color controls are left as an exersize
  16.    for the user.
  17.    
  18.    by Rob Bredow and Scott Stokdyk 
  19. */
  20.  
  21. color
  22. fnc_mydiffuse (color Cl; 
  23.            vector L; 
  24.            normal N) 
  25. {
  26.     normal Nn; 
  27.     vector Ln;
  28.  
  29.     Nn = normalize(N);
  30.     Ln = normalize(L);
  31.     return Cl * max(Ln.Nn,0);   
  32. }
  33.  
  34.  
  35. void 
  36. fnc_projectCurrentCamera(point P;
  37.              output float X, Y;)
  38. {
  39.     point Pndc = transform("NDC", P);
  40.  
  41.     X = xcomp(Pndc);
  42.     Y = ycomp(Pndc);
  43. }
  44.  
  45.  
  46. surface 
  47. k3d_srfdeformation(
  48.     string texname = "";        /* Texture to project */
  49.     float debug = 0;            /* 0 = deformed lit image
  50.                    1 = texture deformed with no lighting
  51.                    2 = output lighting of the P object
  52.                    3 = output lighting of the Pref object
  53.                 */
  54.     float Kd=1;                 /* Surface Kd for lighting calculations */
  55.     
  56.     varying point Pref = point "shader" (0,0,0);
  57.     )
  58. {
  59.     float x, y;
  60.     color Ci0, Ci1, Ci2;
  61.     normal N1, N2;
  62.     float illum_width = 180;
  63.  
  64.     point Porig = Pref;
  65.  
  66.     fnc_projectCurrentCamera(Pref, x, y);
  67.  
  68.     if (texname != "") {
  69.     Ci0 = texture(texname, x, y);
  70.     }
  71.     else Ci0 = 0.5;
  72.     
  73.     /* Calculate shading difference between P and Porig*/
  74.  
  75.     N = normalize(calculatenormal(P));
  76.     N1 = faceforward(normalize(N),I);
  77.     N = normalize(calculatenormal(Porig));
  78.     N2 = faceforward(normalize(N),I);
  79.  
  80.     Ci1 = 0.0; Ci2 = 0.0;
  81.  
  82.     /* These lighting loops can be enhanced to calculate
  83.        specular or reflection maps if needed */
  84.     
  85.     illuminance(P, N1, radians(illum_width)) {
  86.     Ci1 += Kd * fnc_mydiffuse(Cl,L,N1);
  87.     }
  88.  
  89.     illuminance(Porig, N2, radians(illum_width)) {
  90.     Ci2 += Kd * fnc_mydiffuse(Cl,L,N2);
  91.     }
  92.  
  93.     /* Difference in lighting acts as brightness control*/
  94.  
  95.     Ci = Ci0 * (1+(Ci1-Ci2));
  96.     Oi = 1.0;
  97.  
  98.     if (debug == 1) {  /* output the texture - no lighting */
  99.     Ci = Ci0;
  100.     } else if (debug == 2) { /* output texture with P's lighting */
  101.     Ci = Ci1;
  102.     } else if (debug == 3) { /* output texture with Pref's lighting */
  103.     Ci = Ci2;
  104.     }
  105. }
  106.