--- ray/src/rt/ambient.c 2014/04/11 20:31:37 2.71 +++ ray/src/rt/ambient.c 2014/04/26 15:54:43 2.84 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: ambient.c,v 2.71 2014/04/11 20:31:37 greg Exp $"; +static const char RCSid[] = "$Id: ambient.c,v 2.84 2014/04/26 15:54:43 greg Exp $"; #endif /* * ambient.c - routines dealing with ambient (inter-reflected) component. @@ -51,6 +51,7 @@ static int nunflshed = 0; /* number of unflushed ambi #define MAX_SORT_INTVL (SORT_INTVL<<6) #endif + 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 */ @@ -108,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; } @@ -127,13 +128,14 @@ 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 && nambvals > 0) - sortambvals(1); /* rebuild tree */ + 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 */ + } } @@ -263,8 +265,13 @@ ambnotify( /* record new modifier */ #ifdef NEWAMB -#define tfunc(lwr, x, upr) (((x)-(lwr)/((upr)-(lwr))) +#define tfunc(lwr, x, upr) (((x)-(lwr))/((upr)-(lwr))) +static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, + AMBTREE *at, FVECT c0, double s); +static int makeambient(COLOR acol, RAY *r, FVECT rn, int al); +static void extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, + FVECT uvw[3]); void multambient( /* compute ambient component & multiply by coef. */ @@ -315,7 +322,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 */ @@ -338,7 +345,7 @@ dumbamb: /* return global value */ double -sumambient( /* get interpolated ambient value */ +sumambient( /* get interpolated ambient value */ COLOR acol, RAY *r, FVECT rn, @@ -347,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; @@ -362,14 +394,14 @@ sumambient( /* get interpolated ambient value */ if (tracktime) av->latick = ambclock; /* - * Ambient level test. + * Ambient level test */ if (av->lvl > al) /* list sorted, so this works */ break; if (av->weight < 0.9*r->rweight) continue; /* - * Direction test using unperturbed normal. + * Direction test using unperturbed normal */ decodedir(uvw[2], av->ndir); d = DOT(uvw[2], r->ron); @@ -379,53 +411,37 @@ sumambient( /* get interpolated ambient value */ if (delta_r2 >= maxangle*maxangle) continue; /* - * Ambient radius test. + * Modified ray behind test */ + VSUB(ck0, av->pos, r->rop); + 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 >= ambacc*ambacc) continue; /* - * Ray behind test. + * Extrapolate value and compute final weight (hat function) */ - 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; - /* - * Convert to 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); - wsum += d; - extambient(ct, av, uvw, r->rop, rn); 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); } @@ -439,7 +455,7 @@ makeambient( /* make a new ambient value for storage ) { AMBVAL amb; - FVECT uv[2]; + FVECT uvw[3]; int i; amb.weight = 1.0; /* compute weight */ @@ -449,21 +465,22 @@ 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, uv, 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); - amb.udir = encodedir(uv[0]); + amb.udir = encodedir(uvw[0]); amb.lvl = al; copycolor(amb.val, acol); /* insert into tree */ avsave(&amb); /* and save to file */ - if (rn != r->ron) - extambient(acol, &amb, r->rop, rn); /* texture */ + if (rn != r->ron) { /* texture */ + VCOPY(uvw[2], r->ron); + extambient(acol, &amb, r->rop, rn, uvw); + } return(1); } @@ -472,15 +489,22 @@ void extambient( /* extrapolate value at pv, nv */ COLOR cr, AMBVAL *ap, - FVECT uvw[3], FVECT pv, - FVECT nv + FVECT nv, + FVECT uvw[3] ) { - FVECT v1; - int i; - double d = 1.0; /* zeroeth order */ + static FVECT my_uvw[3]; + FVECT v1; + int i; + double d = 1.0; /* zeroeth order */ + if (uvw == NULL) { /* need local coordinates? */ + decodedir(my_uvw[2], ap->ndir); + decodedir(my_uvw[0], ap->udir); + VCROSS(my_uvw[1], my_uvw[2], my_uvw[0]); + uvw = my_uvw; + } for (i = 3; i--; ) /* gradient due to translation */ d += (pv[i] - ap->pos[i]) * (ap->gpos[0]*uvw[0][i] + ap->gpos[1]*uvw[1][i]); @@ -541,7 +565,12 @@ avinsert( /* insert ambient value in our tree */ #else /* ! NEWAMB */ +static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, + AMBTREE *at, FVECT c0, double s); +static double makeambient(COLOR acol, RAY *r, FVECT rn, int al); +static void extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv); + void multambient( /* compute ambient component & multiply by coef. */ COLOR aval, @@ -612,7 +641,7 @@ dumbamb: /* return global value */ } -double +static double sumambient( /* get interpolated ambient value */ COLOR acol, RAY *r, @@ -723,7 +752,7 @@ sumambient( /* get interpolated ambient value */ } -double +static double makeambient( /* make a new ambient value for storage */ COLOR acol, RAY *r, @@ -763,7 +792,7 @@ makeambient( /* make a new ambient value for storage } -void +static void extambient( /* extrapolate value at pv, nv */ COLOR cr, AMBVAL *ap,