--- ray/src/rt/ambient.c 2014/05/11 19:03:37 2.89 +++ ray/src/rt/ambient.c 2016/03/03 22:47:21 2.100 @@ -1,6 +1,4 @@ -#ifndef lint -static const char RCSid[] = "$Id: ambient.c,v 2.89 2014/05/11 19:03:37 greg Exp $"; -#endif +static const char RCSid[] = "$Id: ambient.c,v 2.100 2016/03/03 22:47:21 greg Exp $"; /* * ambient.c - routines dealing with ambient (inter-reflected) component. * @@ -17,6 +15,7 @@ static const char RCSid[] = "$Id: ambient.c,v 2.89 201 #include "resolu.h" #include "ambient.h" #include "random.h" +#include "pmapamb.h" #ifndef OCTSCALE #define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ @@ -197,9 +196,6 @@ setambient(void) /* initialize calculation */ sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); error(SYSTEM, errmsg); } -#ifdef getc_unlocked - flockfile(ambfp); /* application-level lock */ -#endif #ifdef F_SETLKW aflock(F_UNLCK); /* release file */ #endif @@ -220,7 +216,7 @@ ambdone(void) /* close ambient file and free memory lastpos = -1; } /* free ambient tree */ - unloadatree(&atrunk, &avfree); + unloadatree(&atrunk, avfree); /* reset state variables */ avsum = 0.; navsum = 0; @@ -263,7 +259,7 @@ ambnotify( /* record new modifier */ /************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/ -#ifdef NEWAMB +#ifndef OLDAMB #define tfunc(lwr, x, upr) (((x)-(lwr))/((upr)-(lwr))) @@ -271,7 +267,7 @@ static int plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, 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, +static int extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, FVECT uvw[3]); void @@ -282,10 +278,22 @@ multambient( /* compute ambient component & multiply ) { static int rdepth = 0; /* ambient recursion */ - COLOR acol; + COLOR acol, caustic; int ok; double d, l; + /* PMAP: Factor in ambient from photon map, if enabled and ray is + * ambient. Return as all ambient components accounted for, else + * continue. */ + if (ambPmap(aval, r, rdepth)) + return; + + /* PMAP: Factor in specular-diffuse ambient (caustics) from photon + * map, if enabled and ray is primary, else caustic is zero. Continue + * with RADIANCE ambient calculation */ + copycolor(caustic, aval); + ambPmapCaustic(caustic, r, rdepth); + if (ambdiv <= 0) /* no ambient calculation */ goto dumbamb; /* check number of bounces */ @@ -305,6 +313,9 @@ multambient( /* compute ambient component & multiply if (!ok) goto dumbamb; copycolor(aval, acol); + + /* PMAP: add in caustic */ + addcolor(aval, caustic); return; } @@ -314,25 +325,39 @@ multambient( /* compute ambient component & multiply setcolor(acol, 0.0, 0.0, 0.0); d = sumambient(acol, r, nrm, rdepth, &atrunk, thescene.cuorg, thescene.cusize); + if (d > FTINY) { d = 1.0/d; scalecolor(acol, d); multcolor(aval, acol); + + /* PMAP: add in caustic */ + addcolor(aval, caustic); return; } + rdepth++; /* need to cache new value */ ok = makeambient(acol, r, nrm, rdepth-1); rdepth--; + if (ok) { multcolor(aval, acol); /* computed new value */ + + /* PMAP: add in caustic */ + addcolor(aval, caustic); return; } + dumbamb: /* return global value */ if ((ambvwt <= 0) | (navsum == 0)) { multcolor(aval, ambval); + + /* PMAP: add in caustic */ + addcolor(aval, caustic); return; } - l = bright(ambval); /* average in computations */ + + l = bright(ambval); /* average in computations */ if (l > FTINY) { d = (log(l)*(double)ambvwt + avsum) / (double)(ambvwt + navsum); @@ -486,7 +511,8 @@ sumambient( /* get interpolated ambient value */ /* * Extrapolate value and compute final weight (hat function) */ - extambient(ct, av, r->rop, rn, uvw); + if (!extambient(ct, av, r->rop, rn, uvw)) + continue; d = tfunc(maxangle, sqrt(delta_r2), 0.0) * tfunc(ambacc, sqrt(delta_t2), 0.0); scalecolor(ct, d); @@ -537,7 +563,7 @@ makeambient( /* make a new ambient value for storage } -static void +static int extambient( /* extrapolate value at pv, nv */ COLOR cr, AMBVAL *ap, @@ -546,6 +572,7 @@ extambient( /* extrapolate value at pv, nv */ FVECT uvw[3] ) { + const double min_d = 0.05; static FVECT my_uvw[3]; FVECT v1; int i; @@ -565,12 +592,11 @@ extambient( /* extrapolate value at pv, nv */ for (i = 3; i--; ) d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]); - if (d <= 0.0) { - setcolor(cr, 0.0, 0.0, 0.0); - return; - } + if (d < min_d) /* should not use if we can avoid it */ + d = min_d; copycolor(cr, ap->val); scalecolor(cr, d); + return(d > min_d); } @@ -633,9 +659,20 @@ multambient( /* compute ambient component & multiply ) { static int rdepth = 0; /* ambient recursion */ - COLOR acol; + COLOR acol, caustic; double d, l; + /* PMAP: Factor in ambient from global photon map (if enabled) and return + * as all ambient components accounted for */ + if (ambPmap(aval, r, rdepth)) + return; + + /* PMAP: Otherwise factor in ambient from caustic photon map + * (ambPmapCaustic() returns zero if caustic photons disabled) and + * continue with RADIANCE ambient calculation */ + copycolor(caustic, aval); + ambPmapCaustic(caustic, r, rdepth); + if (ambdiv <= 0) /* no ambient calculation */ goto dumbamb; /* check number of bounces */ @@ -653,7 +690,10 @@ multambient( /* compute ambient component & multiply rdepth--; if (d <= FTINY) goto dumbamb; - copycolor(aval, acol); + copycolor(aval, acol); + + /* PMAP: add in caustic */ + addcolor(aval, caustic); return; } @@ -663,24 +703,38 @@ multambient( /* compute ambient component & multiply setcolor(acol, 0.0, 0.0, 0.0); d = sumambient(acol, r, nrm, rdepth, &atrunk, thescene.cuorg, thescene.cusize); + if (d > FTINY) { d = 1.0/d; scalecolor(acol, d); multcolor(aval, acol); + + /* PMAP: add in caustic */ + addcolor(aval, caustic); return; } + rdepth++; /* need to cache new value */ d = makeambient(acol, r, nrm, rdepth-1); rdepth--; + if (d > FTINY) { multcolor(aval, acol); /* got new value */ + + /* PMAP: add in caustic */ + addcolor(aval, caustic); return; } + dumbamb: /* return global value */ if ((ambvwt <= 0) | (navsum == 0)) { multcolor(aval, ambval); + + /* PMAP: add in caustic */ + addcolor(aval, caustic); return; } + l = bright(ambval); /* average in computations */ if (l > FTINY) { d = (log(l)*(double)ambvwt + avsum) / @@ -1172,7 +1226,7 @@ sortambvals( /* resort ambient values */ oldatrunk = atrunk; atrunk.alist = NULL; atrunk.kid = NULL; - unloadatree(&oldatrunk, &avinsert); + unloadatree(&oldatrunk, avinsert); } } else { /* sort memory by last access time */ /* @@ -1189,13 +1243,13 @@ sortambvals( /* resort ambient values */ eputs(errmsg); #endif i_avlist = 0; - unloadatree(&atrunk, &av2list); /* empty current tree */ + unloadatree(&atrunk, av2list); /* empty current tree */ #ifdef DEBUG if (i_avlist < nambvals) error(CONSISTENCY, "missing ambient values in sortambvals"); #endif - qsort((char *)avlist1, nambvals, sizeof(struct avl), &alatcmp); - qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), &aposcmp); + qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp); + qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); for (i = 0; i < nambvals; i++) { if (avlist1[i].p == NULL) continue;