--- ray/src/rt/ambcomp.c 2014/05/02 21:58:50 2.46 +++ ray/src/rt/ambcomp.c 2014/05/09 16:05:09 2.55 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: ambcomp.c,v 2.46 2014/05/02 21:58:50 greg Exp $"; +static const char RCSid[] = "$Id: ambcomp.c,v 2.55 2014/05/09 16:05:09 greg Exp $"; #endif /* * Routines to compute "ambient" values using Monte Carlo @@ -50,6 +50,7 @@ static const int adjacent_trifl[8] = { typedef struct { COLOR v; /* hemisphere sample value */ + float d; /* reciprocal distance (1/rt) */ FVECT p; /* intersection point */ } AMBSAMP; /* sample value */ @@ -104,7 +105,7 @@ vdb_edge(int db1, int db2) case VDB_xY: return(db2==VDB_x ? VDB_y : VDB_X); case VDB_Xy: return(db2==VDB_y ? VDB_x : VDB_Y); } - error(INTERNAL, "forbidden diagonal in vdb_edge()"); + error(CONSISTENCY, "forbidden diagonal in vdb_edge()"); return(-1); } @@ -207,20 +208,16 @@ ambsample( /* initial ambient division sample */ AMBSAMP *ap = &ambsam(hp,i,j); RAY ar; /* generate hemispherical sample */ - if (!getambsamp(&ar, hp, i, j, 0)) - goto badsample; - /* limit vertex distance */ + if (!getambsamp(&ar, hp, i, j, 0) || ar.rt <= FTINY) { + memset(ap, 0, sizeof(AMBSAMP)); + return(NULL); + } + ap->d = 1.0/ar.rt; /* limit vertex distance */ if (ar.rt > 10.0*thescene.cusize) ar.rt = 10.0*thescene.cusize; - else if (ar.rt <= FTINY) /* should never happen! */ - goto badsample; VSUM(ap->p, ar.rorg, ar.rdir, ar.rt); copycolor(ap->v, ar.rcol); return(ap); -badsample: - setcolor(ap->v, 0., 0., 0.); - VCOPY(ap->p, hp->rp->rop); - return(NULL); } @@ -246,25 +243,31 @@ getambdiffs(AMBHEMI *hp) ep[0] += d2; ep[-hp->ns] += d2; } - if (j) { /* from behind */ - d2 = b - bright(ap[-1].v); - d2 *= d2; - ep[0] += d2; - ep[-1] += d2; - } + if (!j) continue; + /* from behind */ + d2 = b - bright(ap[-1].v); + d2 *= d2; + ep[0] += d2; + ep[-1] += d2; + if (!i) continue; + /* diagonal */ + d2 = b - bright(ap[-hp->ns-1].v); + d2 *= d2; + ep[0] += d2; + ep[-hp->ns-1] += d2; } /* correct for number of neighbors */ - earr[0] *= 2.f; - earr[hp->ns-1] *= 2.f; - earr[(hp->ns-1)*hp->ns] *= 2.f; - earr[(hp->ns-1)*hp->ns + hp->ns-1] *= 2.f; + earr[0] *= 8./3.; + earr[hp->ns-1] *= 8./3.; + earr[(hp->ns-1)*hp->ns] *= 8./3.; + earr[(hp->ns-1)*hp->ns + hp->ns-1] *= 8./3.; for (i = 1; i < hp->ns-1; i++) { - earr[i*hp->ns] *= 4./3.; - earr[i*hp->ns + hp->ns-1] *= 4./3.; + earr[i*hp->ns] *= 8./5.; + earr[i*hp->ns + hp->ns-1] *= 8./5.; } for (j = 1; j < hp->ns-1; j++) { - earr[j] *= 4./3.; - earr[(hp->ns-1)*hp->ns + j] *= 4./3.; + earr[j] *= 8./5.; + earr[(hp->ns-1)*hp->ns + j] *= 8./5.; } return(earr); } @@ -275,23 +278,25 @@ static void ambsupersamp(double acol[3], AMBHEMI *hp, int cnt) { float *earr = getambdiffs(hp); - double e2sum = 0; + double e2rem = 0; AMBSAMP *ap; RAY ar; - COLOR asum; + double asum[3]; float *ep; - int i, j, n; + int i, j, n, nss; if (earr == NULL) /* just skip calc. if no memory */ return; - /* add up estimated variances */ - for (ep = earr + hp->ns*hp->ns; ep-- > earr; ) - e2sum += *ep; + /* accumulate estimated variances */ + for (ep = earr + hp->ns*hp->ns; ep > earr; ) + e2rem += *--ep; ep = earr; /* perform super-sampling */ 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.); + if (e2rem <= FTINY) + goto done; /* nothing left to do */ + nss = *ep/e2rem*cnt + frandom(); + asum[0] = asum[1] = asum[2] = 0.0; for (n = 1; n <= nss; n++) { if (!getambsamp(&ar, hp, i, j, n)) { nss = n-1; @@ -300,14 +305,15 @@ ambsupersamp(double acol[3], AMBHEMI *hp, int cnt) addcolor(asum, ar.rcol); } if (nss) { /* update returned ambient value */ - const double ssf = 1./(nss + 1); + 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 */ + e2rem -= *ep++; /* update remainders */ cnt -= nss; } +done: free(earr); } @@ -317,51 +323,42 @@ static uby8 * vertex_flags(AMBHEMI *hp) { uby8 *vflags = (uby8 *)calloc(hp->ns*hp->ns, sizeof(uby8)); - double *dist2a = (double *)malloc(sizeof(double)*hp->ns); uby8 *vf; + AMBSAMP *ap; int i, j; - if ((vflags == NULL) | (dist2a == NULL)) + if (vflags == NULL) error(SYSTEM, "out of memory in vertex_flags()"); - vf = vflags; /* compute distances along first row */ - for (j = 0; j < hp->ns; j++) { - dist2a[j] = dist2(ambsam(hp,0,j).p, hp->rp->rop); - ++vf; - if (!j) continue; - if (dist2a[j] >= dist2a[j-1]) - vf[0] |= 1<sa; /* compute farthest along first row */ + for (j = 0; j < hp->ns-1; j++, vf++, ap++) + if (ap[0].d <= ap[1].d) + vf[0] |= 1<ns; i++) { - double d2n = dist2(ambsam(hp,i,0).p, hp->rp->rop); - for (j = 0; j < hp->ns-1; j++) { - double d2 = d2n; - if (d2 >= dist2a[j]) /* row before */ + for (j = 0; j < hp->ns-1; j++, vf++, ap++) { + if (ap[0].d <= ap[-hp->ns].d) /* row before */ vf[0] |= 1<ns] |= 1<= dist2a[j+1]) /* diagonal we care about */ + if (ap[0].d <= ap[1-hp->ns].d) /* diagonal we care about */ vf[0] |= 1<ns] |= 1<rp->rop); - if (d2 >= d2n) /* column after */ + if (ap[0].d <= ap[1].d) /* column after */ vf[0] |= 1<= dist2a[j]) /* final column edge */ + if (ap[0].d <= ap[-hp->ns].d) /* final column edge */ vf[0] |= 1<ns] |= 1< evalue[1]) { ra[0] = sqrt(sqrt(4.0/evalue[0])); ra[1] = sqrt(sqrt(4.0/evalue[1])); @@ -730,6 +728,51 @@ ambdirgrad(AMBHEMI *hp, FVECT uv[2], float dg[2]) } +/* Compute potential light leak direction flags for cache value */ +static uint32 +ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, const double r1) +{ + 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; + /* don't bother for a few samples */ + if (hp->ns < 12) + return(0); + /* check distances overhead */ + 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*r0 >= 1.0) /* ceiling too low for corral? */ + return(0); + if (avg_d >= max_d) /* insurance */ + 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 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); + 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))); + } + return(flgs); +} + + int doambient( /* compute ambient component */ COLOR rcol, /* input/output color */ @@ -738,7 +781,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); @@ -758,6 +802,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; @@ -778,7 +824,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)) { @@ -792,6 +838,7 @@ doambient( /* compute ambient component */ K = 1.0; pg = NULL; dg = NULL; + crlp = NULL; } ap = hp->sa; /* relative Y channel from here on... */ for (i = hp->ns*hp->ns; i--; ap++) @@ -827,6 +874,9 @@ doambient( /* compute ambient component */ if (ra[0] > maxarad) ra[0] = maxarad; } + /* flag encroached directions */ + if ((wt >= 0.5-FTINY) & (crlp != NULL)) + *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) {