--- ray/src/rt/ambient.c 1996/07/10 13:33:56 2.41 +++ ray/src/rt/ambient.c 1999/01/10 12:45:19 2.46 @@ -56,7 +56,8 @@ static int nunflshed = 0; /* number of unflushed ambi #define MAX_SORT_INTVL (SORT_INTVL<<6) #endif -static COLOR avsum = BLKCOLOR; /* computed ambient value sum */ +static double avsum = 0.; /* computed ambient value sum (log) */ +static unsigned int navsum = 0; /* number of values in avsum */ static unsigned int nambvals = 0; /* total number of indirect values */ static unsigned int nambshare = 0; /* number of values from file */ static unsigned long ambclock = 0; /* ambient access clock */ @@ -69,14 +70,16 @@ static long sortintvl = SORT_INTVL; /* time until nex * through memory on a multiprocessor, when we want to avoid * claiming our own memory (copy on write). Go ahead anyway * if more than two thirds of our values are unshared. + * Compile with -Dtracktime=0 to turn this code off. */ +#ifndef tracktime #define tracktime (shm_boundary == NULL || nambvals > 3*nambshare) +#endif #define AMBFLUSH (BUFSIZ/AMBVALSIZ) #define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL)) -extern long ftell(), lseek(); static int initambfile(), avsave(), avinsert(), sortambvals(), avlmemi(); static AMBVAL *avstore(); #ifdef F_SETLKW @@ -121,6 +124,7 @@ double newa; setambient(afile) /* initialize calculation */ char *afile; { + int readonly = 0; long pos, flen; AMBVAL amb; /* init ambient limits */ @@ -135,25 +139,37 @@ char *afile; return; } /* open ambient file */ - if ((ambfp = fopen(afile, "r+")) != NULL) { - initambfile(0); + if ((ambfp = fopen(afile, "r+")) == NULL) + readonly = (ambfp = fopen(afile, "r")) != NULL; + if (ambfp != NULL) { + initambfile(0); /* file exists */ pos = ftell(ambfp); while (readambval(&amb, ambfp)) avinsert(avstore(&amb)); - /* align */ + nambshare = nambvals; /* share loaded values */ + if (readonly) { + sprintf(errmsg, + "loaded %u values from read-only ambient file", + nambvals); + error(WARNING, errmsg); + fclose(ambfp); /* close file so no writes */ + ambfp = NULL; + return; /* avoid ambsync() */ + } + /* align file pointer */ pos += (long)nambvals*AMBVALSIZ; flen = lseek(fileno(ambfp), 0L, 2); if (flen != pos) { - error(WARNING, + sprintf(errmsg, "ignoring last %ld values in ambient file (corrupted)", (flen - pos)/AMBVALSIZ); + error(WARNING, errmsg); fseek(ambfp, pos, 0); ftruncate(fileno(ambfp), pos); } - nambshare = nambvals; - } else if ((ambfp = fopen(afile, "w+")) != NULL) - initambfile(1); - else { + } else if ((ambfp = fopen(afile, "w+")) != NULL) { + initambfile(1); /* else create new file */ + } else { sprintf(errmsg, "cannot open ambient file \"%s\"", afile); error(SYSTEM, errmsg); } @@ -190,7 +206,7 @@ register RAY *r; FVECT nrm; { static int rdepth = 0; /* ambient recursion */ - double d; + double d, l; if (ambdiv <= 0) /* no ambient calculation */ goto dumbamb; @@ -228,12 +244,18 @@ FVECT nrm; return; dumbamb: /* return global value */ copycolor(acol, ambval); - if (ambvwt <= 0 | nambvals == 0) + if (ambvwt <= 0 | navsum == 0) return; - scalecolor(acol, (double)ambvwt); - addcolor(acol, avsum); /* average in computations */ - d = 1.0/(ambvwt+nambvals); - scalecolor(acol, d); + l = bright(ambval); /* average in computations */ + if (l > FTINY) { + d = (log(l)*(double)ambvwt + avsum) / + (double)(ambvwt + navsum); + d = exp(d) / l; + scalecolor(acol, d); /* apply color of ambval */ + } else { + d = exp( avsum / (double)navsum ); + setcolor(acol, d, d, d); /* neutral color */ + } } @@ -367,7 +389,7 @@ COLOR cr; register AMBVAL *ap; FVECT pv, nv; { - FVECT v1, v2; + FVECT v1; register int i; double d; @@ -376,9 +398,8 @@ FVECT pv, nv; for (i = 0; i < 3; i++) d += ap->gpos[i]*(pv[i]-ap->pos[i]); /* gradient due to rotation */ - VCOPY(v1, ap->dir); - fcross(v2, v1, nv); - d += DOT(ap->gdir, v2); + VCROSS(v1, ap->dir, nv); + d += DOT(ap->gdir, v1); if (d <= 0.0) { setcolor(cr, 0.0, 0.0, 0.0); return; @@ -442,14 +463,19 @@ avstore(aval) /* allocate memory and store aval */ register AMBVAL *aval; { register AMBVAL *av; + double d; if ((av = newambval()) == NULL) error(SYSTEM, "out of memory in avstore"); copystruct(av, aval); av->latick = ambclock; av->next = NULL; - addcolor(avsum, av->val); /* add to sum for averaging */ nambvals++; + d = bright(av->val); + if (d > FTINY) { /* add to log sum for averaging */ + avsum += log(d); + navsum++; + } return(av); }