--- ray/src/rt/ambcomp.c 2014/05/07 01:16:02 2.49 +++ ray/src/rt/ambcomp.c 2014/05/08 04:02:40 2.53 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: ambcomp.c,v 2.49 2014/05/07 01:16:02 greg Exp $"; +static const char RCSid[] = "$Id: ambcomp.c,v 2.53 2014/05/08 04:02:40 greg Exp $"; #endif /* * Routines to compute "ambient" values using Monte Carlo @@ -105,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); } @@ -272,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; @@ -288,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; @@ -299,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 */ @@ -537,7 +537,7 @@ add2gradient(FVECT grad, FVECT egrad1, FVECT egrad2, F /* Compute anisotropic radii and eigenvector directions */ -static int +static void eigenvectors(FVECT uv[2], float ra[2], FVECT hessian[3]) { double hess2[2][2]; @@ -559,9 +559,10 @@ eigenvectors(FVECT uv[2], float ra[2], FVECT hessian[3 if (i == 1) /* double-root (circle) */ evalue[1] = evalue[0]; if (!i || ((evalue[0] = fabs(evalue[0])) <= FTINY*FTINY) | - ((evalue[1] = fabs(evalue[1])) <= FTINY*FTINY) ) - error(INTERNAL, "bad eigenvalue calculation"); - + ((evalue[1] = fabs(evalue[1])) <= FTINY*FTINY) ) { + ra[0] = ra[1] = maxarad; + return; + } if (evalue[0] > evalue[1]) { ra[0] = sqrt(sqrt(4.0/evalue[0])); ra[1] = sqrt(sqrt(4.0/evalue[1])); @@ -722,30 +723,42 @@ ambdirgrad(AMBHEMI *hp, FVECT uv[2], float dg[2]) static uint32 ambcorral(AMBHEMI *hp, FVECT uv[2], const double r0, const double r1) { - uint32 flgs = 0; - int i, j; - /* circle around perimeter */ + 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; + double ang, a1; int abp; - if (ap->d <= FTINY) - continue; + 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 */ - ang += 2.0*PI*(ang < 0); - ang *= 16./PI; - if ((ang < .5) | (ang >= 31.5)) - flgs |= 0x80000001; - else - flgs |= 3L<<(int)(ang-.5); + 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); } @@ -802,7 +815,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)) { @@ -816,6 +829,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++) @@ -851,7 +865,8 @@ doambient( /* compute ambient component */ if (ra[0] > maxarad) ra[0] = maxarad; } - if (crlp != NULL) /* flag encroached directions */ + /* 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];