--- ray/src/rt/ambcomp.c 2017/01/26 16:46:58 2.76 +++ ray/src/rt/ambcomp.c 2017/04/21 16:07:29 2.77 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: ambcomp.c,v 2.76 2017/01/26 16:46:58 greg Exp $"; +static const char RCSid[] = "$Id: ambcomp.c,v 2.77 2017/04/21 16:07:29 greg Exp $"; #endif /* * Routines to compute "ambient" values using Monte Carlo @@ -160,6 +160,7 @@ resample: static float * getambdiffs(AMBHEMI *hp) { + const double normf = 1./bright(hp->acoef); float *earr = (float *)calloc(hp->ns*hp->ns, sizeof(float)); float *ep; AMBSAMP *ap; @@ -173,20 +174,20 @@ getambdiffs(AMBHEMI *hp) for (j = 0; j < hp->ns; j++, ap++, ep++) { b = bright(ap[0].v); if (i) { /* from above */ - d2 = b - bright(ap[-hp->ns].v); + d2 = normf*(b - bright(ap[-hp->ns].v)); d2 *= d2; ep[0] += d2; ep[-hp->ns] += d2; } if (!j) continue; /* from behind */ - d2 = b - bright(ap[-1].v); + d2 = normf*(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 = normf*(b - bright(ap[-hp->ns-1].v)); d2 *= d2; ep[0] += d2; ep[-hp->ns-1] += d2; @@ -230,7 +231,7 @@ ambsupersamp(AMBHEMI *hp, int cnt) goto done; /* nothing left to do */ nss = *ep/e2rem*cnt + frandom(); for (n = 1; n <= nss && ambsample(hp,i,j,n); n++) - --cnt; + if (!--cnt) goto done; e2rem -= *ep++; /* update remainder */ } done: @@ -248,6 +249,9 @@ samp_hemi( /* sample indirect hemisphere */ AMBHEMI *hp; double d; int n, i, j; + /* insignificance check */ + if (bright(rcol) <= FTINY) + return(NULL); /* set number of divisions */ if (ambacc <= FTINY && wt > (d = 0.8*intens(rcol)*r->rweight/(ambdiv*minweight)))