--- ray/src/rt/raytrace.c 2005/04/19 01:15:06 2.49 +++ ray/src/rt/raytrace.c 2005/08/22 21:54:41 2.56 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: raytrace.c,v 2.49 2005/04/19 01:15:06 greg Exp $"; +static const char RCSid[] = "$Id: raytrace.c,v 2.56 2005/08/22 21:54:41 greg Exp $"; #endif /* * raytrace.c - routines for tracing and shading rays. @@ -13,6 +13,7 @@ static const char RCSid[] = "$Id: raytrace.c,v 2.49 20 #include "source.h" #include "otypes.h" #include "otspecial.h" +#include "random.h" #define MAXCSET ((MAXSET+1)*2-1) /* maximum check set size */ @@ -58,6 +59,7 @@ rayorigin( /* start new ray from old one */ r->crtype = r->rtype = rt; r->rsrc = -1; r->clipset = NULL; + r->revf = raytrace; copycolor(r->cext, cextinction); copycolor(r->albedo, salbedo); r->gecc = seccg; @@ -78,6 +80,7 @@ rayorigin( /* start new ray from old one */ r->clipset = ro->newcset; r->rmax = ro->rmax <= FTINY ? 0.0 : ro->rmax - ro->rot; } + r->revf = ro->revf; copycolor(r->cext, ro->cext); copycolor(r->albedo, ro->albedo); r->gecc = ro->gecc; @@ -90,11 +93,32 @@ rayorigin( /* start new ray from old one */ colval(ro->cext,RED) : colval(ro->cext,GRN); if (colval(ro->cext,BLU) < re) re = colval(ro->cext,BLU); re *= ro->rot; - if (re > .1) - r->rweight *= exp(-re); + if (re > 0.1) + if (re > 92.) + r->rweight = 0.0; + else + r->rweight *= exp(-re); } rayclear(r); - return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); + if (r->rweight <= 0.0) /* check for expiration */ + return(-1); + if (r->crtype & SHADOW) /* shadow commitment */ + return(0); + if (maxdepth <= 0 && rc != NULL) { /* Russian roulette */ + if (minweight <= 0.0) + error(USER, "zero ray weight in Russian roulette"); + if (maxdepth < 0 && r->rlvl > -maxdepth) + return(-1); /* upper reflection limit */ + if (r->rweight >= minweight) + return(0); + if (frandom() > r->rweight/minweight) + return(-1); + rw = minweight/r->rweight; /* promote survivor */ + scalecolor(r->rcoef, rw); + r->rweight = minweight; + return(0); + } + return(r->rlvl <= abs(maxdepth) && r->rweight >= minweight ? 0 : -1); } @@ -118,7 +142,7 @@ rayclear( /* clear a ray for (re)evaluation */ extern void -rayvalue( /* trace a ray and compute its value */ +raytrace( /* trace a ray and compute its value */ RAY *r ) { @@ -277,12 +301,16 @@ raymixture( /* mix modifiers */ foremat = backmat = 0; /* foreground */ fr = *r; - if (coef > FTINY) + if (coef > FTINY) { + scalecolor(fr.rcoef, coef); foremat = rayshade(&fr, fore); + } /* background */ br = *r; - if (coef < 1.0-FTINY) + if (coef < 1.0-FTINY) { + scalecolor(br.rcoef, 1.0-coef); backmat = rayshade(&br, back); + } /* check for transparency */ if (backmat ^ foremat) { if (backmat && coef > FTINY) @@ -329,29 +357,27 @@ raydist( /* compute (cumulative) ray distance */ extern void raycontrib( /* compute (cumulative) ray contribution */ - COLOR rc, + double rc[3], const RAY *r, int flags ) { - COLOR eext, ext1; - - setcolor(eext, 0., 0., 0.); - setcolor(rc, 1., 1., 1.); + double eext[3]; + int i; + eext[0] = eext[1] = eext[2] = 0.; + rc[0] = rc[1] = rc[2] = 1.; + while (r != NULL && r->crtype&flags) { - multcolor(rc, r->rcoef); - copycolor(ext1, r->cext); - scalecolor(ext1, r->rot); - addcolor(eext, ext1); + for (i = 3; i--; ) { + rc[i] *= colval(r->rcoef,i); + eext[i] += r->rot * colval(r->cext,i); + } r = r->parent; } - if (intens(eext) > FTINY) { - setcolor(ext1, exp(-colval(eext,RED)), - exp(-colval(eext,GRN)), - exp(-colval(eext,BLU))); - multcolor(rc, ext1); - } + for (i = 3; i--; ) + rc[i] *= (eext[i] <= FTINY) ? 1. : + (eext[i] > 92.) ? 0. : exp(-eext[i]); }