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.58 by greg, Tue Apr 19 01:15:06 2005 UTC vs.
Revision 2.65 by greg, Sat Sep 15 02:47:30 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 275 | Line 274 | multambient(           /* compute ambient component & multiply
274                  goto dumbamb;
275  
276          if (ambacc <= FTINY) {                  /* no ambient storage */
277 +                copycolor(acol, aval);
278                  rdepth++;
279 <                d = doambient(acol, r, aval, intens(aval)*r->rweight,
280 <                                                        NULL, NULL);
279 >                d = doambient(acol, r, r->rweight, NULL, NULL);
280                  rdepth--;
281                  if (d <= FTINY)
282                          goto dumbamb;
283 <                multcolor(aval, acol);
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, aval, nrm, rdepth-1);
300 >        d = makeambient(acol, r, nrm, rdepth-1);
301          rdepth--;
302          if (d > FTINY) {
303                  multcolor(aval, acol);          /* got new value */
# Line 351 | 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 430 | 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          RAY  *r,
443        COLOR  ac,
444          FVECT  rn,
445          int  al
446   )
447   {
448          AMBVAL  amb;
449        double  coef;
449          FVECT   gp, gd;
450 <                                                /* compute weight */
451 <        amb.weight = pow(AVGREFL, (double)al);
452 <        coef = intens(ac)*r->rweight;
453 <        if (coef < 0.1*amb.weight)              /* heuristic */
454 <                amb.weight = coef;
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, ac, amb.weight, gp, gd);
460 <        if (amb.rad <= FTINY)
459 >        amb.rad = doambient(acol, r, amb.weight, gp, gd);
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 871 | Line 876 | ambsync(void)                  /* synchronize ambient file */
876          AMBVAL  avs;
877          register int  n;
878  
879 <        if (nunflshed == 0)
879 >        if (ambfp == NULL)      /* no ambient file? */
880                  return(0);
881          if (lastpos < 0)        /* initializing (locked in initambfile) */
882                  goto syncend;
883 <                                /* gain exclusive access */
884 <        aflock(F_WRLCK);
883 >                                /* gain appropriate access */
884 >        aflock(nunflshed ? F_WRLCK : F_RDLCK);
885                                  /* see if file has grown */
886          if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
887                  goto seekerr;
# Line 899 | Line 904 | ambsync(void)                  /* synchronize ambient file */
904                          avinsert(avstore(&avs));
905                          n -= AMBVALSIZ;
906                  }
907 +                lastpos = flen - n;
908                  /*** seek always as safety measure
909                  if (n) ***/                     /* alignment */
910 <                        if (lseek(fileno(ambfp), (off_t)(flen-n), SEEK_SET) < 0)
910 >                        if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
911                                  goto seekerr;
912          }
913   #ifdef  DEBUG
# Line 914 | Line 920 | ambsync(void)                  /* synchronize ambient file */
920   #endif
921   syncend:
922          n = fflush(ambfp);                      /* calls write() at last */
923 <        if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
924 <                goto seekerr;
923 >        if ((n == EOF) | (lastpos < 0)) {
924 >                if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
925 >                        goto seekerr;
926 >        } else
927 >                lastpos += (long)nunflshed*AMBVALSIZ;
928          aflock(F_UNLCK);                        /* release file */
929          nunflshed = 0;
930          return(n);
# Line 929 | Line 938 | seekerr:
938   extern int
939   ambsync(void)                   /* flush ambient file */
940   {
941 <        if (nunflshed == 0)
941 >        if (ambfp == NULL)
942                  return(0);
943          nunflshed = 0;
944          return(fflush(ambfp));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines