--- ray/src/rt/ambient.c 2014/04/11 22:54:34 2.72 +++ ray/src/rt/ambient.c 2014/04/25 23:04:16 2.81 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: ambient.c,v 2.72 2014/04/11 22:54:34 greg Exp $"; +static const char RCSid[] = "$Id: ambient.c,v 2.81 2014/04/25 23:04:16 greg Exp $"; #endif /* * ambient.c - routines dealing with ambient (inter-reflected) component. @@ -51,6 +51,8 @@ static int nunflshed = 0; /* number of unflushed ambi #define MAX_SORT_INTVL (SORT_INTVL<<6) #endif + +static double qambacc = 0.; /* ambient accuracy to the 1/4 power */ static double avsum = 0.; /* computed ambient value sum (log) */ static unsigned int navsum = 0; /* number of values in avsum */ static unsigned int nambvals = 0; /* total number of indirect values */ @@ -132,8 +134,11 @@ setambacc( /* set ambient accuracy */ if (newa < 0.0) newa = 0.0; ambdiff = fabs(newa - ambacc); - if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0) - sortambvals(1); /* rebuild tree */ + if (ambdiff >= .01 && (ambacc = newa) > FTINY) { + qambacc = sqrt(sqrt(ambacc)); + if (nambvals > 0) + sortambvals(1); /* rebuild tree */ + } } @@ -147,7 +152,7 @@ setambient(void) /* initialize calculation */ ambdone(); /* init ambient limits */ setambres(ambres); - setambacc(ambacc); + qambacc = sqrt(sqrt(ambacc *= (ambacc > FTINY))); if (ambfile == NULL || !ambfile[0]) return; if (ambacc <= FTINY) { @@ -320,7 +325,7 @@ multambient( /* compute ambient component & multiply ok = makeambient(acol, r, nrm, rdepth-1); rdepth--; if (ok) { - multcolor(aval, acol); /* got new value */ + multcolor(aval, acol); /* computed new value */ return; } dumbamb: /* return global value */ @@ -352,8 +357,10 @@ sumambient( /* get interpolated ambient value */ FVECT c0, double s ) -{ /* initial limit is ambacc radians */ - const double maxangle = (ambacc-PI/2.)*pow(r->rweight,0.13) + PI/2.; +{ /* initial limit is 10 degrees plus ambacc radians */ + const double minangle = 10.0 * PI/180.; + const double maxangle = (minangle+ambacc-PI/2.)*pow(r->rweight,0.13) + + PI/2.; double wsum = 0.0; FVECT ck0; int i, j; @@ -384,31 +391,33 @@ sumambient( /* get interpolated ambient value */ if (delta_r2 >= maxangle*maxangle) continue; /* + * Modified ray behind test + */ + VSUB(ck0, av->pos, r->rop); + d = DOT(ck0, uvw[2]); + if (d < -minarad*qambacc-.001) + continue; + d /= av->rad[0]; + delta_t2 = d*d; + if (delta_t2 >= qambacc*qambacc) + continue; + /* * Elliptical radii test based on Hessian */ decodedir(uvw[0], av->udir); VCROSS(uvw[1], uvw[2], uvw[0]); - VSUB(ck0, av->pos, r->rop); d = DOT(ck0, uvw[0]) / av->rad[0]; - delta_t2 = d*d; + delta_t2 += d*d; d = DOT(ck0, uvw[1]) / av->rad[1]; delta_t2 += d*d; - if (delta_t2 >= ambacc*ambacc) + if (delta_t2 >= qambacc*qambacc) continue; /* - * Intersection behind test - */ - d = 0.0; - for (j = 0; j < 3; j++) - d += (r->rop[j] - av->pos[j])*(uvw[2][j] + r->ron[j]); - if (d*0.5 < -minarad*ambacc-.001) - continue; - /* * Extrapolate value and compute final weight (hat function) */ extambient(ct, av, r->rop, rn, uvw); d = tfunc(maxangle, sqrt(delta_r2), 0.0) * - tfunc(ambacc, sqrt(delta_t2), 0.0); + tfunc(qambacc, sqrt(delta_t2), 0.0); scalecolor(ct, d); addcolor(acol, ct); wsum += d; @@ -454,11 +463,10 @@ makeambient( /* make a new ambient value for storage amb.weight = 1.25*r->rweight; setcolor(acol, AVGREFL, AVGREFL, AVGREFL); /* compute ambient */ - if (!doambient(acol, r, amb.weight, uvw, amb.rad, amb.gpos, amb.gdir)) { - setcolor(acol, 0.0, 0.0, 0.0); - return(0); - } + i = doambient(acol, r, amb.weight, uvw, amb.rad, amb.gpos, amb.gdir); scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */ + if (i <= 0 || amb.rad[0] <= FTINY) /* no Hessian or zero radius */ + return(i); /* store value */ VCOPY(amb.pos, r->rop); amb.ndir = encodedir(r->ron); @@ -530,7 +538,7 @@ avinsert( /* insert ambient value in our tree */ at = &atrunk; VCOPY(ck0, thescene.cuorg); s = thescene.cusize; - while (s*(OCTSCALE/2) > av->rad[1]*ambacc) { + while (s*(OCTSCALE/2) > av->rad[1]*qambacc) { if (at->kid == NULL) if ((at->kid = newambtree()) == NULL) error(SYSTEM, "out of memory in avinsert");