--- ray/src/rt/ambient.c 2014/04/19 19:20:47 2.75 +++ ray/src/rt/ambient.c 2014/05/06 17:15:11 2.85 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: ambient.c,v 2.75 2014/04/19 19:20:47 greg Exp $"; +static const char RCSid[] = "$Id: ambient.c,v 2.85 2014/05/06 17:15:11 greg Exp $"; #endif /* * ambient.c - routines dealing with ambient (inter-reflected) component. @@ -52,7 +52,6 @@ static int nunflshed = 0; /* number of unflushed ambi #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 */ @@ -110,17 +109,17 @@ setambres( /* set ambient resolution */ /* set min & max radii */ if (ar <= 0) { minarad = 0; - maxarad = thescene.cusize / 2.0; + maxarad = thescene.cusize*0.5; } else { minarad = thescene.cusize / ar; - maxarad = 64 * minarad; /* heuristic */ - if (maxarad > thescene.cusize / 2.0) - maxarad = thescene.cusize / 2.0; + maxarad = 64.0 * minarad; /* heuristic */ + if (maxarad > thescene.cusize*0.5) + maxarad = thescene.cusize*0.5; } if (minarad <= FTINY) - minarad = 10*FTINY; + minarad = 10.0*FTINY; if (maxarad <= minarad) - maxarad = 64 * minarad; + maxarad = 64.0 * minarad; } @@ -129,13 +128,11 @@ setambacc( /* set ambient accuracy */ double newa ) { - double ambdiff; - - if (newa < 0.0) - newa = 0.0; - ambdiff = fabs(newa - ambacc); - if (ambdiff >= .01 && (ambacc = newa) > FTINY) { - qambacc = sqrt(sqrt(ambacc)); + static double olda; /* remember previous setting here */ + + newa *= (newa > 0); + if (fabs(newa - olda) >= .05*(newa + olda)) { + ambacc = newa; if (nambvals > 0) sortambvals(1); /* rebuild tree */ } @@ -357,12 +354,37 @@ 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.; + double maxangle = minangle + ambacc; double wsum = 0.0; FVECT ck0; int i, j; AMBVAL *av; + + if (at->kid != NULL) { /* sum children first */ + s *= 0.5; + for (i = 0; i < 8; i++) { + for (j = 0; j < 3; j++) { + ck0[j] = c0[j]; + if (1<rop[j] < ck0[j] - OCTSCALE*s) + break; + if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s) + break; + } + if (j == 3) + wsum += sumambient(acol, r, rn, al, + at->kid+i, ck0, s); + } + /* good enough? */ + if (wsum >= 0.05 && s > minarad*10.0) + return(wsum); + } + /* adjust maximum angle */ + if (at->alist != NULL && (at->alist->lvl <= al) & (r->rweight < 0.6)) + maxangle = (maxangle - PI/2.)*pow(r->rweight,0.13) + PI/2.; /* sum this node */ for (av = at->alist; av != NULL; av = av->next) { double d, delta_r2, delta_t2; @@ -389,53 +411,37 @@ sumambient( /* get interpolated ambient value */ if (delta_r2 >= maxangle*maxangle) continue; /* + * Modified ray behind test + */ + VSUB(ck0, r->rop, av->pos); + d = DOT(ck0, uvw[2]); + if (d < -minarad*ambacc-.001) + continue; + d /= av->rad[0]; + delta_t2 = d*d; + if (delta_t2 >= ambacc*ambacc) + 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 >= qambacc*qambacc) + if (delta_t2 >= ambacc*ambacc) 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(qambacc, sqrt(delta_t2), 0.0); + tfunc(ambacc, sqrt(delta_t2), 0.0); scalecolor(ct, d); addcolor(acol, ct); wsum += d; } - if (at->kid == NULL) - return(wsum); - /* sum children */ - s *= 0.5; - for (i = 0; i < 8; i++) { - for (j = 0; j < 3; j++) { - ck0[j] = c0[j]; - if (1<rop[j] < ck0[j] - OCTSCALE*s) - break; - if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s) - break; - } - if (j == 3) - wsum += sumambient(acol, r, rn, al, - at->kid+i, ck0, s); - } return(wsum); } @@ -461,7 +467,7 @@ makeambient( /* make a new ambient value for storage /* compute ambient */ i = doambient(acol, r, amb.weight, uvw, amb.rad, amb.gpos, amb.gdir); scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */ - if (i <= 0) /* no Hessian => no storage */ + if (i <= 0 || amb.rad[0] <= FTINY) /* no Hessian or zero radius */ return(i); /* store value */ VCOPY(amb.pos, r->rop); @@ -534,7 +540,7 @@ avinsert( /* insert ambient value in our tree */ at = &atrunk; VCOPY(ck0, thescene.cuorg); s = thescene.cusize; - while (s*(OCTSCALE/2) > av->rad[1]*qambacc) { + while (s*(OCTSCALE/2) > av->rad[1]*ambacc) { if (at->kid == NULL) if ((at->kid = newambtree()) == NULL) error(SYSTEM, "out of memory in avinsert");