--- ray/src/rt/ambient.c 2005/04/19 01:15:06 2.58 +++ ray/src/rt/ambient.c 2007/09/15 05:16:10 2.66 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: ambient.c,v 2.58 2005/04/19 01:15:06 greg Exp $"; +static const char RCSid[] = "$Id: ambient.c,v 2.66 2007/09/15 05:16:10 greg Exp $"; #endif /* * ambient.c - routines dealing with ambient (inter-reflected) component. @@ -140,7 +140,7 @@ extern void setambient(void) /* initialize calculation */ { int readonly = 0; - long pos, flen; + long flen; AMBVAL amb; /* make sure we're fresh */ ambdone(); @@ -160,7 +160,7 @@ setambient(void) /* initialize calculation */ readonly = (ambfp = fopen(ambfile, "r")) != NULL; if (ambfp != NULL) { initambfile(0); /* file exists */ - pos = ftell(ambfp); + lastpos = ftell(ambfp); while (readambval(&amb, ambfp)) avinsert(avstore(&amb)); nambshare = nambvals; /* share loaded values */ @@ -174,26 +174,26 @@ setambient(void) /* initialize calculation */ return; /* avoid ambsync() */ } /* align file pointer */ - pos += (long)nambvals*AMBVALSIZ; + lastpos += (long)nambvals*AMBVALSIZ; flen = lseek(fileno(ambfp), (off_t)0, SEEK_END); - if (flen != pos) { + if (flen != lastpos) { sprintf(errmsg, "ignoring last %ld values in ambient file (corrupted)", - (flen - pos)/AMBVALSIZ); + (flen - lastpos)/AMBVALSIZ); error(WARNING, errmsg); - fseek(ambfp, pos, 0); + fseek(ambfp, lastpos, SEEK_SET); #ifndef _WIN32 /* XXX we need a replacement for that one */ - ftruncate(fileno(ambfp), (off_t)pos); + ftruncate(fileno(ambfp), (off_t)lastpos); #endif } } else if ((ambfp = fopen(ambfile, "w+")) != NULL) { initambfile(1); /* else create new file */ + lastpos = ftell(ambfp); } else { sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); error(SYSTEM, errmsg); } - nunflshed++; /* lie */ - ambsync(); + ambsync(); /* load previous values */ } @@ -275,29 +275,30 @@ multambient( /* compute ambient component & multiply goto dumbamb; if (ambacc <= FTINY) { /* no ambient storage */ + copycolor(acol, aval); rdepth++; - d = doambient(acol, r, aval, intens(aval)*r->rweight, - NULL, NULL); + d = doambient(acol, r, r->rweight, NULL, NULL); rdepth--; if (d <= FTINY) goto dumbamb; - multcolor(aval, acol); + copycolor(aval, acol); return; } if (tracktime) /* sort to minimize thrashing */ sortambvals(0); - /* get ambient value */ + /* interpolate ambient value */ setcolor(acol, 0.0, 0.0, 0.0); d = sumambient(acol, r, nrm, rdepth, &atrunk, thescene.cuorg, thescene.cusize); if (d > FTINY) { - scalecolor(acol, 1.0/d); + d = 1.0/d; + scalecolor(acol, d); multcolor(aval, acol); return; } rdepth++; /* need to cache new value */ - d = makeambient(acol, r, aval, nrm, rdepth-1); + d = makeambient(acol, r, nrm, rdepth-1); rdepth--; if (d > FTINY) { multcolor(aval, acol); /* got new value */ @@ -351,7 +352,7 @@ sumambient( /* get interpolated ambient value */ */ if (av->lvl > al) /* list sorted, so this works */ break; - if (av->weight < r->rweight-FTINY) + if (av->weight < 0.9*r->rweight) continue; /* * Ambient radius test. @@ -430,34 +431,39 @@ sumambient( /* get interpolated ambient value */ break; } if (j == 3) - wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s); + wsum += sumambient(acol, r, rn, al, + at->kid+i, ck0, s); } return(wsum); } extern double -makeambient( /* make a new ambient value */ +makeambient( /* make a new ambient value for storage */ COLOR acol, RAY *r, - COLOR ac, FVECT rn, int al ) { AMBVAL amb; - double coef; FVECT gp, gd; - /* compute weight */ - amb.weight = pow(AVGREFL, (double)al); - coef = intens(ac)*r->rweight; - if (coef < 0.1*amb.weight) /* heuristic */ - amb.weight = coef; + int i; + + amb.weight = 1.0; /* compute weight */ + for (i = al; i-- > 0; ) + amb.weight *= AVGREFL; + if (r->rweight < 0.1*amb.weight) /* heuristic override */ + amb.weight = 1.25*r->rweight; + setcolor(acol, AVGREFL, AVGREFL, AVGREFL); /* compute ambient */ - amb.rad = doambient(acol, r, ac, amb.weight, gp, gd); - if (amb.rad <= FTINY) + amb.rad = doambient(acol, r, amb.weight, gp, gd); + if (amb.rad <= FTINY) { + setcolor(acol, 0.0, 0.0, 0.0); return(0.0); - /* store it */ + } + scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */ + /* store value */ VCOPY(amb.pos, r->rop); VCOPY(amb.dir, r->ron); amb.lvl = al; @@ -858,6 +864,8 @@ aflock( /* lock/unlock ambient file */ { static struct flock fls; /* static so initialized to zeroes */ + if (typ == fls.l_type) /* already called? */ + return; fls.l_type = typ; if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) error(SYSTEM, "cannot (un)lock ambient file"); @@ -871,12 +879,10 @@ ambsync(void) /* synchronize ambient file */ AMBVAL avs; register int n; - if (nunflshed == 0) + if (ambfp == NULL) /* no ambient file? */ return(0); - if (lastpos < 0) /* initializing (locked in initambfile) */ - goto syncend; - /* gain exclusive access */ - aflock(F_WRLCK); + /* gain appropriate access */ + aflock(nunflshed ? F_WRLCK : F_RDLCK); /* see if file has grown */ if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0) goto seekerr; @@ -886,7 +892,7 @@ ambsync(void) /* synchronize ambient file */ if (ambinp == NULL) error(SYSTEM, "fdopen failed in ambsync"); } - if (fseek(ambinp, lastpos, 0) < 0) + if (fseek(ambinp, lastpos, SEEK_SET) < 0) goto seekerr; while (n >= AMBVALSIZ) { /* load contributed values */ if (!readambval(&avs, ambinp)) { @@ -899,9 +905,10 @@ ambsync(void) /* synchronize ambient file */ avinsert(avstore(&avs)); n -= AMBVALSIZ; } + lastpos = flen - n; /*** seek always as safety measure if (n) ***/ /* alignment */ - if (lseek(fileno(ambfp), (off_t)(flen-n), SEEK_SET) < 0) + if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) goto seekerr; } #ifdef DEBUG @@ -912,10 +919,12 @@ ambsync(void) /* synchronize ambient file */ error(CONSISTENCY, errmsg); } #endif -syncend: n = fflush(ambfp); /* calls write() at last */ - if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) + if (n != EOF) + lastpos += (long)nunflshed*AMBVALSIZ; + else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) goto seekerr; + aflock(F_UNLCK); /* release file */ nunflshed = 0; return(n); @@ -929,7 +938,7 @@ seekerr: extern int ambsync(void) /* flush ambient file */ { - if (nunflshed == 0) + if (ambfp == NULL) return(0); nunflshed = 0; return(fflush(ambfp));