--- ray/src/rt/raytrace.c 2005/05/31 18:01:09 2.51 +++ ray/src/rt/raytrace.c 2009/12/12 00:03:42 2.60 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: raytrace.c,v 2.51 2005/05/31 18:01:09 greg Exp $"; +static const char RCSid[] = "$Id: raytrace.c,v 2.60 2009/12/12 00:03:42 greg Exp $"; #endif /* * raytrace.c - routines for tracing and shading rays. @@ -17,13 +17,13 @@ static const char RCSid[] = "$Id: raytrace.c,v 2.51 20 #define MAXCSET ((MAXSET+1)*2-1) /* maximum check set size */ -unsigned long raynum = 0; /* next unique ray number */ -unsigned long nrays = 0; /* number of calls to localhit */ +RNUMBER raynum = 0; /* next unique ray number */ +RNUMBER nrays = 0; /* number of calls to localhit */ static RREAL Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; OBJREC Lamb = { OVOID, MAT_PLASTIC, "Lambertian", - {0, 5, NULL, Lambfa}, NULL, + {NULL, Lambfa, 0, 5}, NULL }; /* a Lambertian surface */ OBJREC Aftplane; /* aft clipping plane object */ @@ -93,10 +93,19 @@ 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); + 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"); @@ -104,14 +113,14 @@ rayorigin( /* start new ray from old one */ return(-1); /* upper reflection limit */ if (r->rweight >= minweight) return(0); - if (frandom() < r->rweight/minweight) + 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 <= maxdepth && r->rweight >= minweight ? 0 : -1); + return(r->rlvl <= abs(maxdepth) && r->rweight >= minweight ? 0 : -1); } @@ -294,12 +303,18 @@ raymixture( /* mix modifiers */ foremat = backmat = 0; /* foreground */ fr = *r; - if (coef > FTINY) + if (coef > FTINY) { + fr.rweight *= coef; + scalecolor(fr.rcoef, coef); foremat = rayshade(&fr, fore); + } /* background */ br = *r; - if (coef < 1.0-FTINY) + if (coef < 1.0-FTINY) { + br.rweight *= 1.0-coef; + scalecolor(br.rcoef, 1.0-coef); backmat = rayshade(&br, back); + } /* check for transparency */ if (backmat ^ foremat) { if (backmat && coef > FTINY) @@ -346,29 +361,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]); }