--- ray/src/rt/ambient.c 1995/04/29 10:51:28 2.27 +++ ray/src/rt/ambient.c 1995/10/17 18:22:47 2.32 @@ -18,7 +18,12 @@ static char SCCSid[] = "$SunId$ LBL"; #include "random.h" -#define OCTSCALE 0.5 /* ceil((valid rad.)/(cube size)) */ +#ifndef OCTSCALE +#define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ +#endif +#ifndef AMBVWT +#define AMBVWT 250 /* relative ambient value weight (# calcs) */ +#endif typedef struct ambtree { AMBVAL *alist; /* ambient value list */ @@ -48,11 +53,15 @@ static int nunflshed = 0; /* number of unflushed ambi #endif #endif #ifndef SORT_INTVL -#define SORT_INTVL (SORT_THRESH*8) +#define SORT_INTVL (SORT_THRESH*256) #endif +#ifndef MAX_SORT_INTVL +#define MAX_SORT_INTVL (SORT_INTVL<<4) +#endif +static COLOR avsum = BLKCOLOR; /* computed ambient value sum */ +static unsigned int nambvals = 0; /* number of computed ambient values */ static unsigned long ambclock = 0; /* ambient access clock */ -static unsigned int nambvals = 0; /* number of stored ambient values */ static unsigned long lastsort = 0; /* time of last value sort */ static long sortintvl = SORT_INTVL; /* time until next sort */ @@ -63,8 +72,6 @@ static long sortintvl = SORT_INTVL; /* time until nex * claiming our own memory (copy on write). */ #define tracktime (shm_boundary == NULL || ambfp == NULL) -#define checksort() if (ambclock > lastsort+sortintvl && \ - nambvals > SORT_THRESH) sortambvals(0) #define AMBFLUSH (BUFSIZ/AMBVALSIZ) @@ -88,7 +95,7 @@ int ar; maxarad = thescene.cusize / 2.0; } else { minarad = thescene.cusize / ar; - maxarad = 16 * minarad; /* heuristic */ + maxarad = 64 * minarad; /* heuristic */ if (maxarad > thescene.cusize / 2.0) maxarad = thescene.cusize / 2.0; } @@ -195,26 +202,35 @@ register RAY *r; rdepth++; d = doambient(acol, r, r->rweight, NULL, NULL); rdepth--; - if (d == 0.0) + if (d <= FTINY) goto dumbamb; return; } /* resort memory? */ - checksort(); + sortambvals(0); /* get ambient value */ setcolor(acol, 0.0, 0.0, 0.0); d = sumambient(acol, r, rdepth, &atrunk, thescene.cuorg, thescene.cusize); - if (d > FTINY) + if (d > FTINY) { scalecolor(acol, 1.0/d); - else { - d = makeambient(acol, r, rdepth++); - rdepth--; + return; } + rdepth++; + d = makeambient(acol, r, rdepth-1); + rdepth--; if (d > FTINY) return; dumbamb: /* return global value */ copycolor(acol, ambval); +#if AMBVWT + if (nambvals == 0) + return; + scalecolor(acol, (double)AMBVWT); + addcolor(acol, avsum); /* average in computations */ + d = 1.0/(AMBVWT+nambvals); + scalecolor(acol, d); +#endif } @@ -233,8 +249,9 @@ double s; int i; register int j; register AMBVAL *av; - /* do this node */ + wsum = 0.0; + /* do this node */ for (av = at->alist; av != NULL; av = av->next) { if (tracktime) av->latick = ambclock++; @@ -248,11 +265,12 @@ double s; /* * Ambient radius test. */ - e1 = 0.0; - for (j = 0; j < 3; j++) { - d = av->pos[j] - r->rop[j]; - e1 += d * d; - } + d = av->pos[0] - r->rop[0]; + e1 = d * d; + d = av->pos[1] - r->rop[1]; + e1 += d * d; + d = av->pos[2] - r->rop[2]; + e1 += d * d; e1 /= av->rad * av->rad; if (e1 > ambacc*ambacc*1.21) continue; @@ -276,8 +294,7 @@ double s; * Jittering final test reduces image artifacts. */ wt = sqrt(e1) + sqrt(e2); - wt *= .9 + .2*urand(9015+samplendx); - if (wt > ambacc) + if (wt > ambacc*(.9+.2*urand(9015+samplendx))) continue; if (wt <= 1e-3) wt = 1e3; @@ -424,6 +441,7 @@ register AMBVAL *aval; copystruct(av, aval); av->latick = ambclock; av->next = NULL; + addcolor(avsum, av->val); /* add to sum for averaging */ nambvals++; return(av); } @@ -586,6 +604,10 @@ int always; AMBTREE oldatrunk; AMBVAL tav, *tap, *pnext; register int i, j; + /* see if it's time yet */ + if (!always && (ambclock < lastsort+sortintvl || + nambvals < SORT_THRESH)) + return; /* * The idea here is to minimize memory thrashing * in VM systems by improving reference locality. @@ -625,7 +647,8 @@ int always; * when we're thrashing, which is when we need to do it. */ #ifdef DEBUG - sprintf(errmsg, "sorting %u ambient values...", nambvals); + sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...", + nambvals, ambclock); eputs(errmsg); #endif i_avlist = 0; @@ -653,7 +676,11 @@ int always; } free((char *)avlist1); free((char *)avlist2); - if (sortintvl < SORT_INTVL<<6) + /* compute new sort interval */ + sortintvl = ambclock - lastsort; + if (sortintvl >= MAX_SORT_INTVL/2) + sortintvl = MAX_SORT_INTVL; + else sortintvl <<= 1; /* wait twice as long next */ #ifdef DEBUG eputs("done\n");