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.71 by greg, Fri Apr 11 20:31:37 2014 UTC vs.
Revision 2.102 by greg, Sun Apr 24 16:21:32 2016 UTC

# Line 1 | Line 1
1 #ifndef lint
1   static const char       RCSid[] = "$Id$";
3 #endif
2   /*
3   *  ambient.c - routines dealing with ambient (inter-reflected) component.
4   *
# Line 17 | Line 15 | static const char      RCSid[] = "$Id$";
15   #include  "resolu.h"
16   #include  "ambient.h"
17   #include  "random.h"
18 + #include  "pmapamb.h"
19  
20   #ifndef  OCTSCALE
21   #define  OCTSCALE       1.0     /* ceil((valid rad.)/(cube size)) */
# Line 51 | Line 50 | static int  nunflshed = 0;     /* number of unflushed ambi
50   #define MAX_SORT_INTVL  (SORT_INTVL<<6)
51   #endif
52  
53 +
54   static double  avsum = 0.;              /* computed ambient value sum (log) */
55   static unsigned int  navsum = 0;        /* number of values in avsum */
56   static unsigned int  nambvals = 0;      /* total number of indirect values */
# Line 76 | Line 76 | static long  lastpos = -1;             /* last flush position */
76   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
77  
78   #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
79 #define  freeav(av)     free((void *)av);
79  
80   static void initambfile(int creat);
81   static void avsave(AMBVAL *av);
# Line 108 | Line 107 | setambres(                             /* set ambient resolution */
107                                                  /* set min & max radii */
108          if (ar <= 0) {
109                  minarad = 0;
110 <                maxarad = thescene.cusize / 2.0;
110 >                maxarad = thescene.cusize*0.2;
111          } else {
112                  minarad = thescene.cusize / ar;
113 <                maxarad = 64 * minarad;                 /* heuristic */
114 <                if (maxarad > thescene.cusize / 2.0)
115 <                        maxarad = thescene.cusize / 2.0;
113 >                maxarad = 64.0 * minarad;               /* heuristic */
114 >                if (maxarad > thescene.cusize*0.2)
115 >                        maxarad = thescene.cusize*0.2;
116          }
117          if (minarad <= FTINY)
118 <                minarad = 10*FTINY;
118 >                minarad = 10.0*FTINY;
119          if (maxarad <= minarad)
120 <                maxarad = 64 * minarad;
120 >                maxarad = 64.0 * minarad;
121   }
122  
123  
# Line 127 | Line 126 | setambacc(                             /* set ambient accuracy */
126          double  newa
127   )
128   {
129 <        double  ambdiff;
130 <
131 <        if (newa < 0.0)
132 <                newa = 0.0;
133 <        ambdiff = fabs(newa - ambacc);
134 <        if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0)
135 <                sortambvals(1);                 /* rebuild tree */
129 >        static double   olda;           /* remember previous setting here */
130 >        
131 >        newa *= (newa > 0);
132 >        if (fabs(newa - olda) >= .05*(newa + olda)) {
133 >                ambacc = newa;
134 >                if (nambvals > 0)
135 >                        sortambvals(1);         /* rebuild tree */
136 >        }
137   }
138  
139  
# Line 163 | Line 163 | setambient(void)                               /* initialize calculation */
163                  initambfile(0);                 /* file exists */
164                  lastpos = ftell(ambfp);
165                  while (readambval(&amb, ambfp))
166 <                        avinsert(avstore(&amb));
166 >                        avstore(&amb);
167                  nambshare = nambvals;           /* share loaded values */
168                  if (readonly) {
169                          sprintf(errmsg,
# Line 183 | Line 183 | setambient(void)                               /* initialize calculation */
183                                          (flen - lastpos)/AMBVALSIZ);
184                          error(WARNING, errmsg);
185                          fseek(ambfp, lastpos, SEEK_SET);
186 #ifndef _WIN32 /* XXX we need a replacement for that one */
186                          ftruncate(fileno(ambfp), (off_t)lastpos);
188 #endif
187                  }
188          } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
189                  initambfile(1);                 /* else create new file */
# Line 195 | Line 193 | setambient(void)                               /* initialize calculation */
193                  sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
194                  error(SYSTEM, errmsg);
195          }
198 #ifdef getc_unlocked
199        flockfile(ambfp);                       /* application-level lock */
200 #endif
196   #ifdef  F_SETLKW
197          aflock(F_UNLCK);                        /* release file */
198   #endif
# Line 218 | Line 213 | ambdone(void)                  /* close ambient file and free memory
213                  lastpos = -1;
214          }
215                                          /* free ambient tree */
216 <        unloadatree(&atrunk, &avfree);
216 >        unloadatree(&atrunk, avfree);
217                                          /* reset state variables */
218          avsum = 0.;
219          navsum = 0;
# Line 261 | Line 256 | ambnotify(                     /* record new modifier */
256  
257   /************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/
258  
259 < #ifdef NEWAMB
259 > #ifndef OLDAMB
260  
261 < #define tfunc(lwr, x, upr)      (((x)-(lwr)/((upr)-(lwr)))
261 > #define tfunc(lwr, x, upr)      (((x)-(lwr))/((upr)-(lwr)))
262  
263 + static int      plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang);
264 + static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
265 +                                AMBTREE *at, FVECT c0, double s);
266 + static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
267 + static int      extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
268 +                                FVECT uvw[3]);
269  
270   void
271   multambient(            /* compute ambient component & multiply by coef. */
# Line 274 | Line 275 | multambient(           /* compute ambient component & multiply
275   )
276   {
277          static int  rdepth = 0;                 /* ambient recursion */
278 <        COLOR   acol;
278 >        COLOR   acol, caustic;
279          int     ok;
280          double  d, l;
281  
282 +        /* PMAP: Factor in ambient from photon map, if enabled and ray is
283 +         * ambient. Return as all ambient components accounted for, else
284 +         * continue. */
285 +        if (ambPmap(aval, r, rdepth))
286 +                return;
287 +
288 +        /* PMAP: Factor in specular-diffuse ambient (caustics) from photon
289 +         * map, if enabled and ray is primary, else caustic is zero.  Continue
290 +         * with RADIANCE ambient calculation */
291 +        copycolor(caustic, aval);
292 +        ambPmapCaustic(caustic, r, rdepth);
293 +        
294          if (ambdiv <= 0)                        /* no ambient calculation */
295                  goto dumbamb;
296                                                  /* check number of bounces */
# Line 291 | Line 304 | multambient(           /* compute ambient component & multiply
304          if (ambacc <= FTINY) {                  /* no ambient storage */
305                  copycolor(acol, aval);
306                  rdepth++;
307 <                ok = doambient(acol, r, r->rweight, NULL, NULL, NULL, NULL);
307 >                ok = doambient(acol, r, r->rweight,
308 >                                NULL, NULL, NULL, NULL, NULL);
309                  rdepth--;
310                  if (!ok)
311                          goto dumbamb;
312                  copycolor(aval, acol);
313 +
314 +                /* PMAP: add in caustic */
315 +                addcolor(aval, caustic);
316                  return;
317          }
318  
# Line 305 | Line 322 | multambient(           /* compute ambient component & multiply
322          setcolor(acol, 0.0, 0.0, 0.0);
323          d = sumambient(acol, r, nrm, rdepth,
324                          &atrunk, thescene.cuorg, thescene.cusize);
325 +                        
326          if (d > FTINY) {
327                  d = 1.0/d;
328                  scalecolor(acol, d);
329                  multcolor(aval, acol);
330 +
331 +                /* PMAP: add in caustic */
332 +                addcolor(aval, caustic);
333                  return;
334          }
335 +        
336          rdepth++;                               /* need to cache new value */
337          ok = makeambient(acol, r, nrm, rdepth-1);
338          rdepth--;
339 +        
340          if (ok) {
341 <                multcolor(aval, acol);          /* got new value */
341 >                multcolor(aval, acol);          /* computed new value */
342 >
343 >                /* PMAP: add in caustic */
344 >                addcolor(aval, caustic);
345                  return;
346          }
347 +        
348   dumbamb:                                        /* return global value */
349          if ((ambvwt <= 0) | (navsum == 0)) {
350                  multcolor(aval, ambval);
351 +                
352 +                /* PMAP: add in caustic */
353 +                addcolor(aval, caustic);
354                  return;
355          }
356 <        l = bright(ambval);                     /* average in computations */
356 >        
357 >        l = bright(ambval);                     /* average in computations */  
358          if (l > FTINY) {
359                  d = (log(l)*(double)ambvwt + avsum) /
360                                  (double)(ambvwt + navsum);
# Line 337 | Line 368 | dumbamb:                                       /* return global value */
368   }
369  
370  
371 < double
372 < sumambient(     /* get interpolated ambient value */
371 > /* Plug a potential leak where ambient cache value is occluded */
372 > static int
373 > plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang)
374 > {
375 >        const double    cost70sq = 0.1169778;   /* cos(70deg)^2 */
376 >        RAY             rtst;
377 >        FVECT           vdif;
378 >        double          normdot, ndotd, nadotd;
379 >        double          a, b, c, t[2];
380 >
381 >        ang += 2.*PI*(ang < 0);                 /* check direction flags */
382 >        if ( !(ap->corral>>(int)(ang*(16./PI)) & 1) )
383 >                return(0);
384 >        /*
385 >         * Generate test ray, targeting 20 degrees above sample point plane
386 >         * along surface normal from cache position.  This should be high
387 >         * enough to miss local geometry we don't really care about.
388 >         */
389 >        VSUB(vdif, ap->pos, r->rop);
390 >        normdot = DOT(anorm, r->ron);
391 >        ndotd = DOT(vdif, r->ron);
392 >        nadotd = DOT(vdif, anorm);
393 >        a = normdot*normdot - cost70sq;
394 >        b = 2.0*(normdot*ndotd - nadotd*cost70sq);
395 >        c = ndotd*ndotd - DOT(vdif,vdif)*cost70sq;
396 >        if (quadratic(t, a, b, c) != 2)
397 >                return(1);                      /* should rarely happen */
398 >        if (t[1] <= FTINY)
399 >                return(0);                      /* should fail behind test */
400 >        rayorigin(&rtst, SHADOW, r, NULL);
401 >        VSUM(rtst.rdir, vdif, anorm, t[1]);     /* further dist. > plane */
402 >        rtst.rmax = normalize(rtst.rdir);       /* short ray test */
403 >        while (localhit(&rtst, &thescene)) {    /* check for occluder */
404 >                if (rtst.ro->omod != OVOID &&
405 >                                (rtst.clipset == NULL ||
406 >                                        !inset(rtst.clipset, rtst.ro->omod)))
407 >                        return(1);              /* plug light leak */
408 >                VCOPY(rtst.rorg, rtst.rop);     /* skip invisible surface */
409 >                rtst.rmax -= rtst.rot;
410 >                rayclear(&rtst);
411 >        }
412 >        return(0);                              /* seems we're OK */
413 > }
414 >
415 >
416 > static double
417 > sumambient(             /* get interpolated ambient value */
418          COLOR  acol,
419          RAY  *r,
420          FVECT  rn,
# Line 347 | Line 423 | sumambient(    /* get interpolated ambient value */
423          FVECT  c0,
424          double  s
425   )
426 < {                                       /* initial limit is ambacc radians */
427 <        const double    maxangle = (ambacc-PI/2.)*pow(r->rweight,0.13) + PI/2.;
426 > {                       /* initial limit is 10 degrees plus ambacc radians */
427 >        const double    minangle = 10.0 * PI/180.;
428 >        double          maxangle = minangle + ambacc;
429          double          wsum = 0.0;
430          FVECT           ck0;
431          int             i, j;
432          AMBVAL          *av;
433 +
434 +        if (at->kid != NULL) {          /* sum children first */                                
435 +                s *= 0.5;
436 +                for (i = 0; i < 8; i++) {
437 +                        for (j = 0; j < 3; j++) {
438 +                                ck0[j] = c0[j];
439 +                                if (1<<j & i)
440 +                                        ck0[j] += s;
441 +                                if (r->rop[j] < ck0[j] - OCTSCALE*s)
442 +                                        break;
443 +                                if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
444 +                                        break;
445 +                        }
446 +                        if (j == 3)
447 +                                wsum += sumambient(acol, r, rn, al,
448 +                                                        at->kid+i, ck0, s);
449 +                }
450 +                                        /* good enough? */
451 +                if (wsum >= 0.05 && s > minarad*10.0)
452 +                        return(wsum);
453 +        }
454 +                                        /* adjust maximum angle */
455 +        if (at->alist != NULL && (at->alist->lvl <= al) & (r->rweight < 0.6))
456 +                maxangle = (maxangle - PI/2.)*pow(r->rweight,0.13) + PI/2.;
457                                          /* sum this node */
458          for (av = at->alist; av != NULL; av = av->next) {
459 <                double  d, delta_r2, delta_t2;
459 >                double  u, v, d, delta_r2, delta_t2;
460                  COLOR   ct;
461                  FVECT   uvw[3];
462                                          /* record access */
463                  if (tracktime)
464                          av->latick = ambclock;
465                  /*
466 <                 *  Ambient level test.
466 >                 *  Ambient level test
467                   */
468 <                if (av->lvl > al)       /* list sorted, so this works */
468 >                if (av->lvl > al ||     /* list sorted, so this works */
469 >                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
470                          break;
369                if (av->weight < 0.9*r->rweight)
370                        continue;
471                  /*
472 <                 *  Direction test using unperturbed normal.
472 >                 *  Direction test using unperturbed normal
473                   */
474                  decodedir(uvw[2], av->ndir);
475                  d = DOT(uvw[2], r->ron);
# Line 379 | Line 479 | sumambient(    /* get interpolated ambient value */
479                  if (delta_r2 >= maxangle*maxangle)
480                          continue;
481                  /*
482 <                 *  Ambient radius test.
482 >                 *  Modified ray behind test
483                   */
484 +                VSUB(ck0, r->rop, av->pos);
485 +                d = DOT(ck0, uvw[2]);
486 +                if (d < -minarad*ambacc-.001)
487 +                        continue;
488 +                d /= av->rad[0];
489 +                delta_t2 = d*d;
490 +                if (delta_t2 >= ambacc*ambacc)
491 +                        continue;
492 +                /*
493 +                 *  Elliptical radii test based on Hessian
494 +                 */
495                  decodedir(uvw[0], av->udir);
496                  VCROSS(uvw[1], uvw[2], uvw[0]);
497 <                VSUB(ck0, av->pos, r->rop);
387 <                d = DOT(ck0, uvw[0]) / av->rad[0];
388 <                delta_t2 = d*d;
389 <                d = DOT(ck0, uvw[1]) / av->rad[1];
497 >                d = (u = DOT(ck0, uvw[0])) / av->rad[0];
498                  delta_t2 += d*d;
499 +                d = (v = DOT(ck0, uvw[1])) / av->rad[1];
500 +                delta_t2 += d*d;
501                  if (delta_t2 >= ambacc*ambacc)
502                          continue;
503                  /*
504 <                 *  Ray behind test.
504 >                 *  Test for potential light leak
505                   */
506 <                d = 0.0;
397 <                for (j = 0; j < 3; j++)
398 <                        d += (r->rop[j] - av->pos[j])*(uvw[2][j] + r->ron[j]);
399 <                if (d*0.5 < -minarad*ambacc-.001)
506 >                if (av->corral && plugaleak(r, av, uvw[2], atan2a(v,u)))
507                          continue;
508                  /*
509 <                 *  Convert to final weight (hat function)
509 >                 *  Extrapolate value and compute final weight (hat function)
510                   */
511 +                if (!extambient(ct, av, r->rop, rn, uvw))
512 +                        continue;
513                  d = tfunc(maxangle, sqrt(delta_r2), 0.0) *
514                          tfunc(ambacc, sqrt(delta_t2), 0.0);
406                wsum += d;
407                extambient(ct, av, uvw, r->rop, rn);
515                  scalecolor(ct, d);
516                  addcolor(acol, ct);
517 +                wsum += d;
518          }
411        if (at->kid == NULL)
412                return(wsum);
413                                        /* sum children */
414        s *= 0.5;
415        for (i = 0; i < 8; i++) {
416                for (j = 0; j < 3; j++) {
417                        ck0[j] = c0[j];
418                        if (1<<j & i)
419                                ck0[j] += s;
420                        if (r->rop[j] < ck0[j] - OCTSCALE*s)
421                                break;
422                        if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
423                                break;
424                }
425                if (j == 3)
426                        wsum += sumambient(acol, r, rn, al,
427                                                at->kid+i, ck0, s);
428        }
519          return(wsum);
520   }
521  
522  
523 < int
523 > static int
524   makeambient(            /* make a new ambient value for storage */
525          COLOR  acol,
526          RAY  *r,
# Line 439 | Line 529 | makeambient(           /* make a new ambient value for storage
529   )
530   {
531          AMBVAL  amb;
532 <        FVECT   uv[2];
532 >        FVECT   uvw[3];
533          int     i;
534  
535          amb.weight = 1.0;                       /* compute weight */
# Line 449 | Line 539 | makeambient(           /* make a new ambient value for storage
539                  amb.weight = 1.25*r->rweight;
540          setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
541                                                  /* compute ambient */
542 <        if (!doambient(acol, r, amb.weight, uv, amb.rad, amb.gpos, amb.gdir)) {
543 <                setcolor(acol, 0.0, 0.0, 0.0);
454 <                return(0);
455 <        }
542 >        i = doambient(acol, r, amb.weight,
543 >                        uvw, amb.rad, amb.gpos, amb.gdir, &amb.corral);
544          scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
545 +        if (i <= 0 || amb.rad[0] <= FTINY)      /* no Hessian or zero radius */
546 +                return(i);
547                                                  /* store value */
548          VCOPY(amb.pos, r->rop);
549          amb.ndir = encodedir(r->ron);
550 <        amb.udir = encodedir(uv[0]);
550 >        amb.udir = encodedir(uvw[0]);
551          amb.lvl = al;
552          copycolor(amb.val, acol);
553                                                  /* insert into tree */
554          avsave(&amb);                           /* and save to file */
555 <        if (rn != r->ron)
556 <                extambient(acol, &amb, r->rop, rn);     /* texture */
555 >        if (rn != r->ron) {                     /* texture */
556 >                VCOPY(uvw[2], r->ron);
557 >                extambient(acol, &amb, r->rop, rn, uvw);
558 >        }
559          return(1);
560   }
561  
562  
563 < void
563 > static int
564   extambient(             /* extrapolate value at pv, nv */
565          COLOR  cr,
566          AMBVAL   *ap,
475        FVECT  uvw[3],
567          FVECT  pv,
568 <        FVECT  nv
568 >        FVECT  nv,
569 >        FVECT  uvw[3]
570   )
571   {
572 <        FVECT  v1;
573 <        int  i;
574 <        double  d = 1.0;                /* zeroeth order */
572 >        const double    min_d = 0.05;
573 >        static FVECT    my_uvw[3];
574 >        FVECT           v1;
575 >        int             i;
576 >        double          d = 1.0;        /* zeroeth order */
577  
578 +        if (uvw == NULL) {              /* need local coordinates? */
579 +                decodedir(my_uvw[2], ap->ndir);
580 +                decodedir(my_uvw[0], ap->udir);
581 +                VCROSS(my_uvw[1], my_uvw[2], my_uvw[0]);
582 +                uvw = my_uvw;
583 +        }
584          for (i = 3; i--; )              /* gradient due to translation */
585                  d += (pv[i] - ap->pos[i]) *
586                          (ap->gpos[0]*uvw[0][i] + ap->gpos[1]*uvw[1][i]);
# Line 489 | Line 589 | extambient(            /* extrapolate value at pv, nv */
589          for (i = 3; i--; )
590                  d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]);
591          
592 <        if (d <= 0.0) {
593 <                setcolor(cr, 0.0, 0.0, 0.0);
494 <                return;
495 <        }
592 >        if (d < min_d)                  /* should not use if we can avoid it */
593 >                d = min_d;
594          copycolor(cr, ap->val);
595          scalecolor(cr, d);
596 +        return(d > min_d);
597   }
598  
599  
# Line 531 | Line 630 | avinsert(                              /* insert ambient value in our tree */
630          }
631          avh.next = at->alist;           /* order by increasing level */
632          for (ap = &avh; ap->next != NULL; ap = ap->next)
633 <                if (ap->next->lvl >= av->lvl)
633 >                if ( ap->next->lvl > av->lvl ||
634 >                                (ap->next->lvl == av->lvl) &
635 >                                (ap->next->weight <= av->weight) )
636                          break;
637          av->next = ap->next;
638          ap->next = (AMBVAL*)av;
# Line 541 | Line 642 | avinsert(                              /* insert ambient value in our tree */
642  
643   #else /* ! NEWAMB */
644  
645 + static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
646 +                                AMBTREE *at, FVECT c0, double s);
647 + static double   makeambient(COLOR acol, RAY *r, FVECT rn, int al);
648 + static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv);
649  
650 +
651   void
652   multambient(            /* compute ambient component & multiply by coef. */
653          COLOR  aval,
# Line 550 | Line 656 | multambient(           /* compute ambient component & multiply
656   )
657   {
658          static int  rdepth = 0;                 /* ambient recursion */
659 <        COLOR   acol;
659 >        COLOR   acol, caustic;
660          double  d, l;
661  
662 +        /* PMAP: Factor in ambient from global photon map (if enabled) and return
663 +         * as all ambient components accounted for */
664 +        if (ambPmap(aval, r, rdepth))
665 +                return;
666 +
667 +        /* PMAP: Otherwise factor in ambient from caustic photon map
668 +         * (ambPmapCaustic() returns zero if caustic photons disabled) and
669 +         * continue with RADIANCE ambient calculation */
670 +        copycolor(caustic, aval);
671 +        ambPmapCaustic(caustic, r, rdepth);
672 +        
673          if (ambdiv <= 0)                        /* no ambient calculation */
674                  goto dumbamb;
675                                                  /* check number of bounces */
# Line 570 | Line 687 | multambient(           /* compute ambient component & multiply
687                  rdepth--;
688                  if (d <= FTINY)
689                          goto dumbamb;
690 <                copycolor(aval, acol);
690 >                copycolor(aval, acol);          
691 >        
692 >           /* PMAP: add in caustic */
693 >                addcolor(aval, caustic);        
694                  return;
695          }
696  
# Line 580 | Line 700 | multambient(           /* compute ambient component & multiply
700          setcolor(acol, 0.0, 0.0, 0.0);
701          d = sumambient(acol, r, nrm, rdepth,
702                          &atrunk, thescene.cuorg, thescene.cusize);
703 +                        
704          if (d > FTINY) {
705                  d = 1.0/d;
706                  scalecolor(acol, d);
707                  multcolor(aval, acol);
708 +                
709 +                /* PMAP: add in caustic */
710 +                addcolor(aval, caustic);        
711                  return;
712          }
713 +        
714          rdepth++;                               /* need to cache new value */
715          d = makeambient(acol, r, nrm, rdepth-1);
716          rdepth--;
717 +        
718          if (d > FTINY) {
719                  multcolor(aval, acol);          /* got new value */
720 +
721 +                /* PMAP: add in caustic */
722 +                addcolor(aval, caustic);                        
723                  return;
724          }
725 +        
726   dumbamb:                                        /* return global value */
727          if ((ambvwt <= 0) | (navsum == 0)) {
728                  multcolor(aval, ambval);
729 +
730 +                /* PMAP: add in caustic */
731 +                addcolor(aval, caustic);        
732                  return;
733          }
734 +        
735          l = bright(ambval);                     /* average in computations */
736          if (l > FTINY) {
737                  d = (log(l)*(double)ambvwt + avsum) /
# Line 612 | Line 746 | dumbamb:                                       /* return global value */
746   }
747  
748  
749 < double
749 > static double
750   sumambient(     /* get interpolated ambient value */
751          COLOR  acol,
752          RAY  *r,
# Line 639 | Line 773 | sumambient(    /* get interpolated ambient value */
773                  /*
774                   *  Ambient level test.
775                   */
776 <                if (av->lvl > al)       /* list sorted, so this works */
776 >                if (av->lvl > al ||     /* list sorted, so this works */
777 >                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
778                          break;
644                if (av->weight < 0.9*r->rweight)
645                        continue;
779                  /*
780                   *  Ambient radius test.
781                   */
# Line 723 | Line 856 | sumambient(    /* get interpolated ambient value */
856   }
857  
858  
859 < double
859 > static double
860   makeambient(            /* make a new ambient value for storage */
861          COLOR  acol,
862          RAY  *r,
# Line 763 | Line 896 | makeambient(           /* make a new ambient value for storage
896   }
897  
898  
899 < void
899 > static void
900   extambient(             /* extrapolate value at pv, nv */
901          COLOR  cr,
902          AMBVAL   *ap,
# Line 824 | Line 957 | avinsert(                              /* insert ambient value in our tree */
957          }
958          avh.next = at->alist;           /* order by increasing level */
959          for (ap = &avh; ap->next != NULL; ap = ap->next)
960 <                if (ap->next->lvl >= av->lvl)
960 >                if ( ap->next->lvl > av->lvl ||
961 >                                (ap->next->lvl == av->lvl) &
962 >                                (ap->next->weight <= av->weight) )
963                          break;
964          av->next = ap->next;
965          ap->next = (AMBVAL*)av;
# Line 876 | Line 1011 | avsave(                                /* insert and save an ambient value */
1011          AMBVAL  *av
1012   )
1013   {
1014 <        avinsert(avstore(av));
1014 >        avstore(av);
1015          if (ambfp == NULL)
1016                  return;
1017          if (writambval(av, ambfp) < 0)
# Line 891 | Line 1026 | writerr:
1026  
1027  
1028   static AMBVAL *
1029 < avstore(                                /* allocate memory and store aval */
1029 > avstore(                                /* allocate memory and save aval */
1030          AMBVAL  *aval
1031   )
1032   {
# Line 909 | Line 1044 | avstore(                               /* allocate memory and store aval */
1044                  avsum += log(d);
1045                  navsum++;
1046          }
1047 +        avinsert(av);                   /* insert in our cache tree */
1048          return(av);
1049   }
1050  
# Line 935 | Line 1071 | newambtree(void)                               /* allocate 8 ambient tree structs
1071          }
1072          atp = atfreelist;
1073          atfreelist = atp->kid;
1074 <        memset((char *)atp, '\0', 8*sizeof(AMBTREE));
1074 >        memset(atp, 0, 8*sizeof(AMBTREE));
1075          return(atp);
1076   }
1077  
# Line 961 | Line 1097 | unloadatree(                   /* unload an ambient value tree */
1097                                          /* transfer values at this node */
1098          for (av = at->alist; av != NULL; av = at->alist) {
1099                  at->alist = av->next;
1100 +                av->next = NULL;
1101                  (*f)(av);
1102          }
1103          if (at->kid == NULL)
# Line 1038 | Line 1175 | avlmemi(                               /* find list position from address */
1175   {
1176          AMBVAL  **avlpp;
1177  
1178 <        avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
1179 <                        nambvals, sizeof(AMBVAL *), &aposcmp);
1178 >        avlpp = (AMBVAL **)bsearch(&avaddr, avlist2,
1179 >                        nambvals, sizeof(AMBVAL *), aposcmp);
1180          if (avlpp == NULL)
1181                  error(CONSISTENCY, "address not found in avlmemi");
1182          return(avlpp - avlist2);
# Line 1082 | Line 1219 | sortambvals(                   /* resort ambient values */
1219          }
1220          if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
1221                  if (avlist2 != NULL)
1222 <                        free((void *)avlist2);
1222 >                        free(avlist2);
1223                  if (always) {           /* rebuild without sorting */
1224                          oldatrunk = atrunk;
1225                          atrunk.alist = NULL;
1226                          atrunk.kid = NULL;
1227 <                        unloadatree(&oldatrunk, &avinsert);
1227 >                        unloadatree(&oldatrunk, avinsert);
1228                  }
1229          } else {                        /* sort memory by last access time */
1230                  /*
# Line 1104 | Line 1241 | sortambvals(                   /* resort ambient values */
1241                  eputs(errmsg);
1242   #endif
1243                  i_avlist = 0;
1244 <                unloadatree(&atrunk, &av2list); /* empty current tree */
1244 >                unloadatree(&atrunk, av2list);  /* empty current tree */
1245   #ifdef DEBUG
1246                  if (i_avlist < nambvals)
1247                          error(CONSISTENCY, "missing ambient values in sortambvals");
1248   #endif
1249 <                qsort((char *)avlist1, nambvals, sizeof(struct avl), &alatcmp);
1250 <                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), &aposcmp);
1249 >                qsort(avlist1, nambvals, sizeof(struct avl), alatcmp);
1250 >                qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
1251                  for (i = 0; i < nambvals; i++) {
1252                          if (avlist1[i].p == NULL)
1253                                  continue;
# Line 1126 | Line 1263 | sortambvals(                   /* resort ambient values */
1263                          avinsert(avlist2[j]);
1264                          avlist1[j].p = NULL;
1265                  }
1266 <                free((void *)avlist1);
1267 <                free((void *)avlist2);
1266 >                free(avlist1);
1267 >                free(avlist2);
1268                                                  /* compute new sort interval */
1269                  sortintvl = ambclock - lastsort;
1270                  if (sortintvl >= MAX_SORT_INTVL/2)
# Line 1191 | Line 1328 | ambsync(void)                  /* synchronize ambient file */
1328                                  error(WARNING, errmsg);
1329                                  break;
1330                          }
1331 <                        avinsert(avstore(&avs));
1331 >                        avstore(&avs);
1332                          n -= AMBVALSIZ;
1333                  }
1334                  lastpos = flen - n;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines