ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambient.c
(Generate patch)

Comparing ray/src/rt/ambient.c (file contents):
Revision 2.57 by greg, Fri Nov 5 17:36:55 2004 UTC vs.
Revision 2.63 by greg, Thu Sep 13 20:13:16 2007 UTC

# Line 192 | Line 192 | setambient(void)                               /* initialize calculation */
192                  sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
193                  error(SYSTEM, errmsg);
194          }
195 <        nunflshed++;    /* lie */
196 <        ambsync();
195 >        ambsync();                      /* load previous values */
196   }
197  
198  
# Line 254 | Line 253 | ambnotify(                     /* record new modifier */
253  
254  
255   extern void
256 < ambient(                /* compute ambient component for ray */
257 <        COLOR  acol,
256 > multambient(            /* compute ambient component & multiply by coef. */
257 >        COLOR  aval,
258          register RAY  *r,
259          FVECT  nrm
260   )
261   {
262          static int  rdepth = 0;                 /* ambient recursion */
263 +        COLOR   acol;
264          double  d, l;
265  
266          if (ambdiv <= 0)                        /* no ambient calculation */
# Line 274 | Line 274 | ambient(               /* compute ambient component for ray */
274                  goto dumbamb;
275  
276          if (ambacc <= FTINY) {                  /* no ambient storage */
277 +                copycolor(acol, aval);
278                  rdepth++;
279                  d = doambient(acol, r, r->rweight, NULL, NULL);
280                  rdepth--;
281                  if (d <= FTINY)
282                          goto dumbamb;
283 +                copycolor(aval, acol);
284                  return;
285          }
286  
287          if (tracktime)                          /* sort to minimize thrashing */
288                  sortambvals(0);
289 <                                                /* get ambient value */
289 >                                                /* interpolate ambient value */
290          setcolor(acol, 0.0, 0.0, 0.0);
291          d = sumambient(acol, r, nrm, rdepth,
292                          &atrunk, thescene.cuorg, thescene.cusize);
293          if (d > FTINY) {
294 <                scalecolor(acol, 1.0/d);
294 >                d = 1.0/d;
295 >                scalecolor(acol, d);
296 >                multcolor(aval, acol);
297                  return;
298          }
299          rdepth++;                               /* need to cache new value */
300          d = makeambient(acol, r, nrm, rdepth-1);
301          rdepth--;
302 <        if (d > FTINY)
302 >        if (d > FTINY) {
303 >                multcolor(aval, acol);          /* got new value */
304                  return;
305 +        }
306   dumbamb:                                        /* return global value */
307 <        copycolor(acol, ambval);
308 <        if ((ambvwt <= 0) | (navsum == 0))
307 >        if ((ambvwt <= 0) | (navsum == 0)) {
308 >                multcolor(aval, ambval);
309                  return;
310 +        }
311          l = bright(ambval);                     /* average in computations */
312          if (l > FTINY) {
313                  d = (log(l)*(double)ambvwt + avsum) /
314                                  (double)(ambvwt + navsum);
315                  d = exp(d) / l;
316 <                scalecolor(acol, d);            /* apply color of ambval */
316 >                scalecolor(aval, d);
317 >                multcolor(aval, ambval);        /* apply color of ambval */
318          } else {
319                  d = exp( avsum / (double)navsum );
320 <                setcolor(acol, d, d, d);        /* neutral color */
320 >                scalecolor(aval, d);            /* neutral color */
321          }
322   }
323  
# Line 343 | Line 351 | sumambient(    /* get interpolated ambient value */
351                   */
352                  if (av->lvl > al)       /* list sorted, so this works */
353                          break;
354 <                if (av->weight < r->rweight-FTINY)
354 >                if (av->weight < 0.9*r->rweight)
355                          continue;
356                  /*
357                   *  Ambient radius test.
# Line 422 | Line 430 | sumambient(    /* get interpolated ambient value */
430                                  break;
431                  }
432                  if (j == 3)
433 <                        wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s);
433 >                        wsum += sumambient(acol, r, rn, al,
434 >                                                at->kid+i, ck0, s);
435          }
436          return(wsum);
437   }
438  
439  
440   extern double
441 < makeambient(    /* make a new ambient value */
441 > makeambient(            /* make a new ambient value for storage */
442          COLOR  acol,
443 <        register RAY  *r,
443 >        RAY  *r,
444          FVECT  rn,
445          int  al
446   )
447   {
448          AMBVAL  amb;
449          FVECT   gp, gd;
450 <                                                /* compute weight */
451 <        amb.weight = pow(AVGREFL, (double)al);
452 <        if (r->rweight < 0.1*amb.weight)        /* heuristic */
453 <                amb.weight = r->rweight;
450 >        int     i;
451 >
452 >        amb.weight = 1.0;                       /* compute weight */
453 >        for (i = al; i-- > 0; )
454 >                amb.weight *= AVGREFL;
455 >        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
456 >                amb.weight = 1.25*r->rweight;
457 >        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
458                                                  /* compute ambient */
459          amb.rad = doambient(acol, r, amb.weight, gp, gd);
460 <        if (amb.rad <= FTINY)
460 >        if (amb.rad <= FTINY) {
461 >                setcolor(acol, 0.0, 0.0, 0.0);
462                  return(0.0);
463 <                                                /* store it */
463 >        }
464 >        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
465 >                                                /* store value */
466          VCOPY(amb.pos, r->rop);
467          VCOPY(amb.dir, r->ron);
468          amb.lvl = al;
# Line 860 | Line 876 | ambsync(void)                  /* synchronize ambient file */
876          AMBVAL  avs;
877          register int  n;
878  
863        if (nunflshed == 0)
864                return(0);
879          if (lastpos < 0)        /* initializing (locked in initambfile) */
880                  goto syncend;
881 <                                /* gain exclusive access */
882 <        aflock(F_WRLCK);
881 >                                /* gain appropriate access */
882 >        aflock(nunflshed ? F_WRLCK : F_RDLCK);
883                                  /* see if file has grown */
884          if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
885                  goto seekerr;
# Line 918 | Line 932 | seekerr:
932   extern int
933   ambsync(void)                   /* flush ambient file */
934   {
921        if (nunflshed == 0)
922                return(0);
935          nunflshed = 0;
936          return(fflush(ambfp));
937   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines