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_redapple.sl < prev    next >
Encoding:
Text File  |  2008-01-23  |  7.8 KB  |  239 lines

  1. /*
  2.  * Renamed to JMredapple.sl for RMR -- talrmr@SpamSucks_pacbell.net  7/18/99
  3.  *
  4.  * redapple.sl - A nice shader for reddy-green apples.
  5.  *
  6.  * Copyright (C) Jonathan Merritt, 1999.
  7.  * Feel free to use this shader to create apples anywhere and
  8.  * everywhere.
  9.  *
  10.  * This shader creates a nice skin for red-green apples. It _is_ tuned
  11.  * for specific geometry, but can very easily be adjusted to fit any
  12.  * apple you care to lay it on. Apples are requred to have the "s"
  13.  * texture parameter running equatorialy around them (like latitude
  14.  * lines), and "t" running from pole to pole (like longditude). Apples
  15.  * look best from the side, but work ok from the top, so long as you
  16.  * don't mind the simple lerping I used to fade to green at the poles.
  17.  *
  18.  * Look out for the "txtscale" parameter: some noise calculations are
  19.  * done using shader space, instead of "s" and "t" parameters (I think
  20.  * my apple's parameters are stretched a bit?), so if you use
  21.  * different size apples, make sure to change txtscale appropriately.
  22.  *
  23.  * I think this shader is really cool. If you think so too, you can
  24.  * email me your praises, notification of my credits in big motion
  25.  * pictures, job offers at Pixar, etc... to:  jmerritt@SpamSucks_warpax.com   :-)
  26.  *
  27.  * Have FUN!!!   :-)
  28.  */
  29.  
  30. #include "k3d_noises.h"
  31.  
  32. /*#define USE_LG_RAYTRACE */
  33.  
  34. #ifdef USE_LG_RAYTRACE
  35. #include "rayserver.h"
  36. #include "raytrace.h"
  37. #endif
  38.  
  39.  
  40. #define TSCALE .05
  41.  
  42. surface
  43. k3d_redapple (
  44.     float Ks = .25;           /* Specular reflection coeff.    */
  45.     float Kd = .5;            /* Diffuse reflection coeff.     */
  46.     float Ka = 1;             /* Ambient light coeff.          */
  47.     float Kr = .05;           /* Mirror-like reflection coeff. */
  48.     float roughness = .1;     /* Specular roughness param.     */
  49.     float label = 0;          /* Non-uniformity param.         */
  50.     float redness = 1;        /* Amount of 'redness' - higher  */
  51.                               /*  values give redder apples.   */
  52.                               /*  2 = almost complete red.     */
  53.     float txtscale = .05;     /* Ugly kludge (see above...)    */
  54.     color specularcolor = 1;  /* Specular reflection color     */
  55. )
  56. {
  57.     /*
  58.      * Shader variables
  59.      */
  60.     vector Nf, V, Rdir;
  61.     point PP, newP;
  62.     float base_turb, blotch_turb, disp_turb;
  63.     float small_noise, blotch, speck, disp = 0, blackness;
  64.     color cs, small_speckle, base_color, reflect;
  65.  
  66.  
  67.     /*
  68.      * Some nice colors for our red apple
  69.      */
  70.     color red   = color(0.52,0.00,0.00);
  71.     color dred  = color(0.40,0.00,0.00);
  72.     color green = color(0.76,0.80,0.37);
  73.     color brown = color(0.70,0.78,0.34);
  74.     color black = color(0.00,0.00,0.00);
  75.  
  76.  
  77.     /*
  78.      * Standard settings
  79.      */
  80.     PP = transform("shader", P);
  81.     V = normalize(-I);
  82.  
  83.  
  84.     /*
  85.      * Pick the base color for the apple.
  86.      *
  87.      * The base color consists of patches of pure green,
  88.      * and patches of finely speckled red and green. These
  89.      * are set up so that the poles of the apple (as "t"
  90.      * goes to 1 or 0) are colored more and more green.
  91.      *
  92.      * The apple geometry is such that lines of constant
  93.      * "t" are lines of longditude (from pole to pole).
  94.      * The constants T1 - T4 specify locations of the
  95.      * start of 'greenness' toward the poles.
  96.      */
  97.     #define BASE_SF          1.2    /* s-factor for big noise          */
  98.     #define BASE_TF          3      /* t-factor for big noise          */
  99.     #define BASE_NF          100    /* scaling factor for small noise  */
  100.     #define BASE_NOISE_AMP   0.2    /* small noise color mix amplitude */
  101.     #define BASE_GRC         0.4    /* shift factor for more red       */
  102.     #define T1               0.0    /* t < T1 is pure green            */
  103.     #define T2               0.18   /* lerp s.t. T1->T2 => green->red  */
  104.     #define T3               0.8    /* T2->T3 => red can exist here    */
  105.     #define T4               1.0    /* lerp s.t. T3->T4 => red->green  */
  106.                                     /* t > T4 is pure green            */
  107.     base_turb = noise(BASE_SF*sin(2*PI*s) + PI + label,
  108.                       BASE_TF*t + label);
  109.     base_turb = pow(base_turb, (1/redness));
  110.     small_noise = snoise(BASE_NF*PP*txtscale/TSCALE);
  111.     small_speckle = mix(red, green, BASE_GRC+
  112.                         (small_noise*BASE_NOISE_AMP));
  113.     if (t < T1)
  114.         base_turb = 0;
  115.     else if (t >= T1 && t < T2)
  116.         base_turb *= (t-T1)/(T2-T1);
  117.     else if (t >= T3 && t < T4)
  118.         base_turb *= (T4-t)/(T4-T3);
  119.     else if (t >= T4)
  120.         base_turb = 0;
  121.     base_color = spline(base_turb, green, green,
  122.                         small_speckle, small_speckle,
  123.                         small_speckle);
  124.  
  125.  
  126.     /*
  127.      * Pick the blotch color.
  128.      *
  129.      * The blotch color is finally determined by mixing,
  130.      * using the blotching coefficient "blotch". This
  131.      * section sets that coefficient, using a funky yet
  132.      * really simple noise routine.
  133.      */
  134.     #define BLOTCH_SF          20   /* s-factor for blotches  */
  135.     #define BLOTCH_TF          15   /* t-factor for blotches  */
  136.     #define BLOTCH_TCF         2.5  /* scaling kludge         */
  137.     #define BLOTCH_DELTA       0.1  /* 'nother scaling kludge */
  138.     #define BLOTCH_SPECK_COEFF 0.3  /* blotch specking coeff  */
  139.     blotch_turb = noise(BLOTCH_SF*sin(2*PI*(s+.1234)) + PI + label,
  140.                         BLOTCH_TF*t + label) + BLOTCH_DELTA;
  141.     blotch = pow(blotch_turb, 1.3) * BLOTCH_TCF * 
  142.              pow(base_turb, 2);
  143.     blotch = blotch * (1+small_noise) +
  144.              small_noise * BLOTCH_SPECK_COEFF;
  145.     if (blotch > 1) blotch = 1;
  146.  
  147.  
  148.     /*
  149.      * Set the brown specking.
  150.      *
  151.      * The apple has brown speckles on it, set by mixing
  152.      * using the speckling coefficient "speck". These
  153.      * specks are very dot-like in nature, and this
  154.      * dottiness technique is stolen from LG's starfield 
  155.      * shader.
  156.      */
  157.     #define SPECK_NF        50    /* A noise scaling factor      */
  158.     #define SPECK_CUTOFF    .45   /* Cutoff for 'tops' of specks */
  159.     speck = pow(smoothstep(SPECK_CUTOFF, 1,
  160.                 noise(SPECK_NF*PP*txtscale/TSCALE)), 3);
  161.  
  162.     /*
  163.      * Determine where the apple goes black at the poles.
  164.      * This simulates the spots where the stalk would be
  165.      * attached, and where the wierd bit at the very bottom
  166.      * is.
  167.      */
  168.     #define BEDGE  .01
  169.     #define BWIDTH .01
  170.     blackness = 1-(smoothstep(BEDGE, BEDGE+BWIDTH, t)*
  171.                    smoothstep(BEDGE, BEDGE+BWIDTH, 1-t));
  172.  
  173.  
  174.     /*
  175.      * Combine what we have so far to set the surface
  176.      * color.
  177.      */
  178.     cs = mix(base_color, dred, blotch);
  179.     cs = mix(cs, brown, speck);
  180.     cs = mix(cs, black, blackness);
  181.  
  182.  
  183.     /*
  184.      * Set the shading surface normal.
  185.      *
  186.      * Here we set the surface normal to fix up the specular
  187.      * highlights. We'd like them perturbed by the small
  188.      * noise, affected a little by the brown specks, and
  189.      * also dented a bit (alas, no real apples are perfectly
  190.      * un-dented).
  191.      */
  192.     #define DISP_SF     10             /* s-factor for dent noise */
  193.     #define DISP_TF     30             /* t-factor for dent noise */
  194.     #define DISP_DENT_AMP     (1/15)   /* dent amplitude          */
  195.     #define DISP_SMNOISE_AMP  (1/1000) /* small noise amplitude   */
  196.     #define DISP_SPECK_AMP    (1/40)   /* speckle disp. amplitude */
  197.     disp += noise(DISP_SF*sin(2*PI*s) + PI + label,
  198.                   DISP_TF*t + label) * DISP_DENT_AMP * txtscale / TSCALE;
  199.     disp += small_noise * DISP_SMNOISE_AMP * txtscale / TSCALE;
  200.     disp -= speck * DISP_SPECK_AMP * txtscale / TSCALE;
  201.     newP = calculatenormal(P + disp * normalize(N));
  202.     Nf = faceforward(normalize(newP), I);
  203.  
  204.  
  205.     /*
  206.      * 'Mirror' reflections
  207.      *
  208.      * Here, we raytrace for the slight mirrored reflections
  209.      * in the surface of an apple. They don't add much, but
  210.      * may be needed for the 'perfect' apple :-).
  211.      *
  212.      * Note: You'll need Larry Gritz's raytrace helper files
  213.      * for this bit!
  214.      */
  215. #ifdef USE_LG_RAYTRACE
  216.     if (Kr > .01) {
  217.         Rdir = normalize(reflect(normalize(I), Nf));
  218.         reflect = RayTrace(P, Rdir, 0, 1, 1);
  219.     } else {
  220.         reflect = 0;
  221.     }
  222. #else
  223.     reflect = 0;
  224. #endif
  225.  
  226.  
  227.     /*
  228.      * Combine everything to get Ci, in the standard form.
  229.      */
  230.     Oi = Os;
  231.     Ci = Os * (cs * (Ka*ambient() + Kd*diffuse(Nf)) +
  232.                specularcolor * (Kr * reflect +
  233.                                 Ks * specular(Nf, V, roughness)));
  234.         
  235. }
  236.  
  237.  
  238.  
  239.