home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 August / chip_08_2000.iso / aktualnosci / shareware / Rhinoceros / rh11eval_20000320.exe / %MAINDIR% / RIB / raytrace.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-08  |  3.0 KB  |  92 lines

  1. /*
  2.  * raytrace.h - functions which make ray tracing easier.
  3.  *
  4.  * Author: written by Larry Gritz (gritzl@acm.org)
  5.  *
  6.  * $Revision: 1.1 $      $Date: 1998/09/18 02:33:55 $
  7.  */
  8.  
  9.  
  10. #ifndef RAYTRACE_H
  11. #define RAYTRACE_H 1
  12.  
  13.  
  14. #include "patterns.h" /* needed for definition of filterwidthp */
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21. /* RayTrace() - A fancy ray trace routine, particularly suitable for
  22.  * use with the "ray server."  Tries to sample over the surface
  23.  * element and over the varying ray spread due to surface curvature.
  24.  * An ordinary call to trace would point sample the environment in a
  25.  * very simplistic way.  This function takes the size of the surface
  26.  * facet and curvature of the surface into account, and lets you
  27.  * sample the space with multiple rays.
  28.  * 
  29.  * Inputs:
  30.  *    P - surface position
  31.  *    Rdir - the reflection direction.
  32.  *    blur - reflection blurriness; 0 = sharp reflection
  33.  *    jitter - when 1, fully jitter the stochastic ray directions.  Lower
  34.  *          numbers jitter less, 0 doesn't jitter.  Lowering jitter may help
  35.  *          alleviate "sparkling" due to animation with low nrays.
  36.  *    nsamples - number of rays with which to sample.  Larger numbers will
  37.  *          yield better-sampled reflections, but will be more expensive.
  38.  *          Note that the function reduces this number for secondary rays,
  39.  *          assuming that the distribution from primary rays will be
  40.  *          sufficient!
  41.  * Return value: the average of the trace calls.
  42.  * 
  43.  */
  44.  
  45. color
  46. RayTrace (point P;                     /* position */
  47.       vector Rdir;                 /* direction to trace */
  48.           float blur;                  /* extra blur to add */
  49.       float jitter;                /* how much to jitter sample rays */
  50.       uniform float nsamples;      /* how many rays to cast */
  51.          )
  52. {
  53.     float rand () {
  54.     extern float jitter;
  55.     return (raylevel()==0) ? (0.5 + jitter * (float random() - 0.5)) : 0.5;
  56.     }
  57.     extern float du, dv;
  58.     color C = 0;
  59.     float bluramt = blur + filterwidthp(Rdir);
  60.     uniform float nrays = (raylevel() == 0 ? max(1,ceil(sqrt(nsamples))) : 1);
  61.     if (bluramt > 0 || nrays > 1) {
  62.     /* Construct orthogonal components to Rdir */
  63.     vector uoffset = blur * normalize (vector (zcomp(Rdir) - ycomp(Rdir),
  64.                            xcomp(Rdir) - zcomp(Rdir),
  65.                            ycomp(Rdir) - xcomp(Rdir)));
  66.     vector voffset = Rdir ^ uoffset;
  67.     vector Tu = Du(P) * (1.5 * du); /* overblur just a tad... */
  68.     vector Tv = Dv(P) * (1.5 * dv);
  69.         uniform float i, j;
  70.     for (i = 0;  i < nrays;  i += 1) {
  71.         for (j = 0;  j < nrays;  j += 1) {
  72.         /* Add a random offset to the smooth reflection vector */
  73.         vector R = Rdir +
  74.             ((i + rand())/nrays - 0.5) * uoffset +
  75.             ((j + rand())/nrays - 0.5) * voffset;
  76.         point Pray = P +
  77.             ((j + rand())/nrays - 0.5) * Tu +
  78.             ((i + rand())/nrays - 0.5) * Tv;
  79.         C += trace (P, normalize(R));
  80.         }
  81.     }
  82.     C /= (nrays*nrays);
  83.     } else {
  84.     /* No blur or curvature, just do a simple trace */
  85.     C = trace (P, Rdir);
  86.     }
  87.     return C;
  88. }
  89.  
  90.  
  91. #endif /* defined(RAYTRACE_H) */
  92.