--- ray/src/rt/ambient.c 2005/05/31 18:01:09 2.61 +++ ray/src/rt/ambient.c 2007/09/14 00:50:31 2.64 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: ambient.c,v 2.61 2005/05/31 18:01:09 greg Exp $"; +static const char RCSid[] = "$Id: ambient.c,v 2.64 2007/09/14 00:50:31 greg Exp $"; #endif /* * ambient.c - routines dealing with ambient (inter-reflected) component. @@ -192,8 +192,7 @@ setambient(void) /* initialize calculation */ sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); error(SYSTEM, errmsg); } - nunflshed++; /* lie */ - ambsync(); + ambsync(); /* load previous values */ } @@ -454,7 +453,7 @@ makeambient( /* make a new ambient value for storage for (i = al; i-- > 0; ) amb.weight *= AVGREFL; if (r->rweight < 0.1*amb.weight) /* heuristic override */ - amb.weight = r->rweight; + amb.weight = 1.25*r->rweight; setcolor(acol, AVGREFL, AVGREFL, AVGREFL); /* compute ambient */ amb.rad = doambient(acol, r, amb.weight, gp, gd); @@ -877,12 +876,10 @@ ambsync(void) /* synchronize ambient file */ AMBVAL avs; register int n; - if (nunflshed == 0) - 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; @@ -905,9 +902,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 @@ -920,8 +918,11 @@ ambsync(void) /* synchronize ambient file */ #endif syncend: n = fflush(ambfp); /* calls write() at last */ - if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) - goto seekerr; + if (n == EOF) { + if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) + goto seekerr; + } else + lastpos += (long)nunflshed*AMBVALSIZ; aflock(F_UNLCK); /* release file */ nunflshed = 0; return(n); @@ -935,8 +936,6 @@ seekerr: extern int ambsync(void) /* flush ambient file */ { - if (nunflshed == 0) - return(0); nunflshed = 0; return(fflush(ambfp)); }