--- ray/src/rt/ambcomp.c 2014/05/03 05:46:19 2.47 +++ ray/src/rt/ambcomp.c 2014/05/07 20:20:24 2.51 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: ambcomp.c,v 2.47 2014/05/03 05:46:19 greg Exp $"; +static const char RCSid[] = "$Id: ambcomp.c,v 2.51 2014/05/07 20:20:24 greg Exp $"; #endif /* * Routines to compute "ambient" values using Monte Carlo @@ -23,8 +23,6 @@ static const char RCSid[] = "$Id: ambcomp.c,v 2.47 201 #ifdef NEWAMB -/* #define HEM_MULT 4.0 /* hem multiplier (bigger => sparser cache) */ - extern void SDsquare2disk(double ds[2], double seedx, double seedy); /* vertex direction bit positions */ @@ -274,10 +272,10 @@ static void ambsupersamp(double acol[3], AMBHEMI *hp, int cnt) { float *earr = getambdiffs(hp); - double e2sum = 0; + double e2sum = 0.0; AMBSAMP *ap; RAY ar; - COLOR asum; + double asum[3]; float *ep; int i, j, n; @@ -290,7 +288,7 @@ ambsupersamp(double acol[3], AMBHEMI *hp, int cnt) for (ap = hp->sa, i = 0; i < hp->ns; i++) for (j = 0; j < hp->ns; j++, ap++) { int nss = *ep/e2sum*cnt + frandom(); - setcolor(asum, 0., 0., 0.); + asum[0] = asum[1] = asum[2] = 0.0; for (n = 1; n <= nss; n++) { if (!getambsamp(&ar, hp, i, j, n)) { nss = n-1; @@ -301,7 +299,7 @@ ambsupersamp(double acol[3], AMBHEMI *hp, int cnt) if (nss) { /* update returned ambient value */ const double ssf = 1./(nss + 1); for (n = 3; n--; ) - acol[n] += ssf*colval(asum,n) + + acol[n] += ssf*asum[n] + (ssf - 1.)*colval(ap->v,n); } e2sum -= *ep++; /* update remainders */ @@ -720,38 +718,43 @@ ambdirgrad(AMBHEMI *hp, FVECT uv[2], float dg[2]) } -/* Make sure radii don't extend beyond what we see in our periphery */ -static void -hem_radii(AMBHEMI *hp, FVECT uv[2], float ra[2]) +/* Compute potential light leak direction flags for cache value */ +static uint32 +ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, const double r1) { -#ifdef HEM_MULT - double udsum = 0, vdsum = 0; - double uwsum = 0, vwsum = 0; + const double max_d = 1.0/(minarad*ambacc + 0.001); + const double ang_res = 0.5*PI/(hp->ns-1); + const double ang_step = ang_res/((int)(16/PI*ang_res) + (1+FTINY)); + double avg_d = 0; + uint32 flgs = 0; int i, j; - /* circle around perimeter */ + /* check distances above us */ + for (i = hp->ns*3/4; i-- > hp->ns>>2; ) + for (j = hp->ns*3/4; j-- > hp->ns>>2; ) + avg_d += ambsam(hp,i,j).d; + avg_d *= 4.0/(hp->ns*hp->ns); + if (avg_d >= max_d) /* too close to corral? */ + return(0); + /* else circle around perimeter */ for (i = 0; i < hp->ns; i++) for (j = 0; j < hp->ns; j += !i|(i==hp->ns-1) ? 1 : hp->ns-1) { AMBSAMP *ap = &ambsam(hp,i,j); FVECT vec; - double us2, vs2; + double u, v; + double ang, a1; + int abp; + if ((ap->d <= FTINY) | (ap->d >= max_d)) + continue; /* too far or too near */ VSUB(vec, ap->p, hp->rp->rop); - us2 = DOT(vec, uv[0]) * ap->d; - us2 *= us2; - vs2 = DOT(vec, uv[1]) * ap->d; - vs2 *= vs2; - udsum += us2 * ap->d; - uwsum += us2; - vdsum += vs2 * ap->d; - vwsum += vs2; + u = DOT(vec, uv[0]) * ap->d; + v = DOT(vec, uv[1]) * ap->d; + if ((r0*r0*u*u + r1*r1*v*v) * ap->d*ap->d <= 1.0) + continue; /* occluder outside ellipse */ + ang = atan2a(v, u); /* else set direction flags */ + for (a1 = ang-.5*ang_res; a1 <= ang+.5*ang_res; a1 += ang_step) + flgs |= 1L<<(int)(16/PI*(a1 + 2.*PI*(a1 < 0))); } - uwsum *= HEM_MULT; /* adjust effective hem size */ - vwsum *= HEM_MULT; - /* cap radii (recall d=1/rt) */ - if (ra[0]*udsum > uwsum) - ra[0] = uwsum/udsum; - if (ra[1]*vdsum > vwsum) - ra[1] = vwsum/vdsum; -#endif + return(flgs); } @@ -763,7 +766,8 @@ doambient( /* compute ambient component */ FVECT uv[2], /* returned (optional) */ float ra[2], /* returned (optional) */ float pg[2], /* returned (optional) */ - float dg[2] /* returned (optional) */ + float dg[2], /* returned (optional) */ + uint32 *crlp /* returned (optional) */ ) { AMBHEMI *hp = inithemi(rcol, r, wt); @@ -783,6 +787,8 @@ doambient( /* compute ambient component */ pg[0] = pg[1] = 0.0; if (dg != NULL) dg[0] = dg[1] = 0.0; + if (crlp != NULL) + *crlp = 0; /* sample the hemisphere */ acol[0] = acol[1] = acol[2] = 0.0; cnt = 0; @@ -803,7 +809,7 @@ doambient( /* compute ambient component */ return(-1); /* return value w/o Hessian */ } cnt = ambssamp*wt + 0.5; /* perform super-sampling? */ - if (cnt > 0) + if (cnt > 8) ambsupersamp(acol, hp, cnt); copycolor(rcol, acol); /* final indirect irradiance/PI */ if ((ra == NULL) & (pg == NULL) & (dg == NULL)) { @@ -836,10 +842,9 @@ doambient( /* compute ambient component */ ra[0] = 1.0/d; if (ra[1]*(d = fabs(pg[1])) > 1.0) ra[1] = 1.0/d; + if (ra[0] > ra[1]) + ra[0] = ra[1]; } - hem_radii(hp, uv, ra); - if (ra[0] > ra[1]) - ra[0] = ra[1]; if (ra[0] < minarad) { ra[0] = minarad; if (ra[1] < minarad) @@ -853,6 +858,8 @@ doambient( /* compute ambient component */ if (ra[0] > maxarad) ra[0] = maxarad; } + if (crlp != NULL) /* flag encroached directions */ + *crlp = ambcorral(hp, uv, ra[0]*ambacc, ra[1]*ambacc); if (pg != NULL) { /* cap gradient if necessary */ d = pg[0]*pg[0]*ra[0]*ra[0] + pg[1]*pg[1]*ra[1]*ra[1]; if (d > 1.0) {