--- ray/src/rt/ambient.c 2005/05/25 18:06:22 2.59 +++ ray/src/rt/ambient.c 2007/09/13 20:13:16 2.63 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: ambient.c,v 2.59 2005/05/25 18:06:22 greg Exp $"; +static const char RCSid[] = "$Id: ambient.c,v 2.63 2007/09/13 20:13:16 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 */ } @@ -275,29 +274,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, intens(aval)*r->rweight, nrm, rdepth, + 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 */ @@ -326,7 +326,6 @@ extern double sumambient( /* get interpolated ambient value */ COLOR acol, register RAY *r, - double rw, FVECT rn, int al, AMBTREE *at, @@ -352,7 +351,7 @@ sumambient( /* get interpolated ambient value */ */ if (av->lvl > al) /* list sorted, so this works */ break; - if (av->weight < 0.9*rw) + if (av->weight < 0.9*r->rweight) continue; /* * Ambient radius test. @@ -431,7 +430,7 @@ sumambient( /* get interpolated ambient value */ break; } if (j == 3) - wsum += sumambient(acol, r, rw, rn, al, + wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s); } return(wsum); @@ -439,27 +438,31 @@ sumambient( /* get interpolated ambient value */ 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; @@ -873,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; @@ -931,8 +932,6 @@ seekerr: extern int ambsync(void) /* flush ambient file */ { - if (nunflshed == 0) - return(0); nunflshed = 0; return(fflush(ambfp)); }