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.56 by schorsch, Tue Mar 30 16:13:00 2004 UTC vs.
Revision 2.98 by greg, Sun Aug 23 00:17:12 2015 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 24 | Line 23 | static const char      RCSid[] = "$Id$";
23  
24   extern char  *shm_boundary;     /* memory sharing boundary */
25  
26 < #define  MAXASET        511     /* maximum number of elements in ambient set */
26 > #ifndef  MAXASET
27 > #define  MAXASET        4095    /* maximum number of elements in ambient set */
28 > #endif
29   OBJECT  ambset[MAXASET+1]={0};  /* ambient include/exclude set */
30  
31   double  maxarad;                /* maximum ambient radius */
# Line 37 | Line 38 | static int  nunflshed = 0;     /* number of unflushed ambi
38  
39   #ifndef SORT_THRESH
40   #ifdef SMLMEM
41 < #define SORT_THRESH     ((3L<<20)/sizeof(AMBVAL))
41 > #define SORT_THRESH     ((16L<<20)/sizeof(AMBVAL))
42   #else
43 < #define SORT_THRESH     ((9L<<20)/sizeof(AMBVAL))
43 > #define SORT_THRESH     ((64L<<20)/sizeof(AMBVAL))
44   #endif
45   #endif
46   #ifndef SORT_INTVL
# Line 49 | 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 82 | Line 84 | static AMBVAL *avstore(AMBVAL  *aval);
84   static AMBTREE *newambtree(void);
85   static void freeambtree(AMBTREE  *atp);
86  
87 < typedef void unloadtf_t(void *);
87 > typedef void unloadtf_t(AMBVAL *);
88   static unloadtf_t avinsert;
89   static unloadtf_t av2list;
90 + static unloadtf_t avfree;
91   static void unloadatree(AMBTREE  *at, unloadtf_t *f);
92  
93   static int aposcmp(const void *avp1, const void *avp2);
# Line 96 | Line 99 | static void aflock(int  typ);
99   #endif
100  
101  
102 < extern void
102 > void
103   setambres(                              /* set ambient resolution */
104          int  ar
105   )
# Line 105 | Line 108 | setambres(                             /* set ambient resolution */
108                                                  /* set min & max radii */
109          if (ar <= 0) {
110                  minarad = 0;
111 <                maxarad = thescene.cusize / 2.0;
111 >                maxarad = thescene.cusize*0.2;
112          } else {
113                  minarad = thescene.cusize / ar;
114 <                maxarad = 64 * minarad;                 /* heuristic */
115 <                if (maxarad > thescene.cusize / 2.0)
116 <                        maxarad = thescene.cusize / 2.0;
114 >                maxarad = 64.0 * minarad;               /* heuristic */
115 >                if (maxarad > thescene.cusize*0.2)
116 >                        maxarad = thescene.cusize*0.2;
117          }
118          if (minarad <= FTINY)
119 <                minarad = 10*FTINY;
119 >                minarad = 10.0*FTINY;
120          if (maxarad <= minarad)
121 <                maxarad = 64 * minarad;
121 >                maxarad = 64.0 * minarad;
122   }
123  
124  
125 < extern void
125 > void
126   setambacc(                              /* set ambient accuracy */
127          double  newa
128   )
129   {
130 <        double  ambdiff;
131 <
132 <        if (newa < 0.0)
133 <                newa = 0.0;
134 <        ambdiff = fabs(newa - ambacc);
135 <        if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0)
136 <                sortambvals(1);                 /* rebuild tree */
130 >        static double   olda;           /* remember previous setting here */
131 >        
132 >        newa *= (newa > 0);
133 >        if (fabs(newa - olda) >= .05*(newa + olda)) {
134 >                ambacc = newa;
135 >                if (nambvals > 0)
136 >                        sortambvals(1);         /* rebuild tree */
137 >        }
138   }
139  
140  
141 < extern void
141 > void
142   setambient(void)                                /* initialize calculation */
143   {
144          int     readonly = 0;
145 <        long  pos, flen;
145 >        long    flen;
146          AMBVAL  amb;
147                                                  /* make sure we're fresh */
148          ambdone();
# Line 158 | Line 162 | setambient(void)                               /* initialize calculation */
162                  readonly = (ambfp = fopen(ambfile, "r")) != NULL;
163          if (ambfp != NULL) {
164                  initambfile(0);                 /* file exists */
165 <                pos = ftell(ambfp);
165 >                lastpos = ftell(ambfp);
166                  while (readambval(&amb, ambfp))
167 <                        avinsert(avstore(&amb));
167 >                        avstore(&amb);
168                  nambshare = nambvals;           /* share loaded values */
169                  if (readonly) {
170                          sprintf(errmsg,
# Line 172 | Line 176 | setambient(void)                               /* initialize calculation */
176                          return;                 /* avoid ambsync() */
177                  }
178                                                  /* align file pointer */
179 <                pos += (long)nambvals*AMBVALSIZ;
179 >                lastpos += (long)nambvals*AMBVALSIZ;
180                  flen = lseek(fileno(ambfp), (off_t)0, SEEK_END);
181 <                if (flen != pos) {
181 >                if (flen != lastpos) {
182                          sprintf(errmsg,
183                          "ignoring last %ld values in ambient file (corrupted)",
184 <                                        (flen - pos)/AMBVALSIZ);
184 >                                        (flen - lastpos)/AMBVALSIZ);
185                          error(WARNING, errmsg);
186 <                        fseek(ambfp, pos, 0);
186 >                        fseek(ambfp, lastpos, SEEK_SET);
187   #ifndef _WIN32 /* XXX we need a replacement for that one */
188 <                        ftruncate(fileno(ambfp), (off_t)pos);
188 >                        ftruncate(fileno(ambfp), (off_t)lastpos);
189   #endif
190                  }
191          } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
192                  initambfile(1);                 /* else create new file */
193 +                fflush(ambfp);
194 +                lastpos = ftell(ambfp);
195          } else {
196                  sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
197                  error(SYSTEM, errmsg);
198          }
199 <        nunflshed++;    /* lie */
200 <        ambsync();
199 > #ifdef  F_SETLKW
200 >        aflock(F_UNLCK);                        /* release file */
201 > #endif
202   }
203  
204  
205 < extern void
205 > void
206   ambdone(void)                   /* close ambient file and free memory */
207   {
208          if (ambfp != NULL) {            /* close ambient file */
# Line 209 | Line 216 | ambdone(void)                  /* close ambient file and free memory
216                  lastpos = -1;
217          }
218                                          /* free ambient tree */
219 <        unloadatree(&atrunk, free);
219 >        unloadatree(&atrunk, &avfree);
220                                          /* reset state variables */
221          avsum = 0.;
222          navsum = 0;
# Line 221 | Line 228 | ambdone(void)                  /* close ambient file and free memory
228   }
229  
230  
231 < extern void
231 > void
232   ambnotify(                      /* record new modifier */
233          OBJECT  obj
234   )
235   {
236          static int  hitlimit = 0;
237 <        register OBJREC  *o;
238 <        register char  **amblp;
237 >        OBJREC   *o;
238 >        char  **amblp;
239  
240          if (obj == OVOID) {             /* starting over */
241                  ambset[0] = 0;
# Line 250 | Line 257 | ambnotify(                     /* record new modifier */
257                  }
258   }
259  
260 + /************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/
261  
262 < extern void
263 < ambient(                /* compute ambient component for ray */
262 > #ifndef OLDAMB
263 >
264 > #define tfunc(lwr, x, upr)      (((x)-(lwr))/((upr)-(lwr)))
265 >
266 > static int      plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang);
267 > static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
268 >                                AMBTREE *at, FVECT c0, double s);
269 > static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
270 > static int      extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
271 >                                FVECT uvw[3]);
272 >
273 > void
274 > multambient(            /* compute ambient component & multiply by coef. */
275 >        COLOR  aval,
276 >        RAY  *r,
277 >        FVECT  nrm
278 > )
279 > {
280 >        static int  rdepth = 0;                 /* ambient recursion */
281 >        COLOR   acol, caustic;
282 >        int     ok;
283 >        double  d, l;
284 >
285 >        /* PMAP: Factor in ambient from photon map, if enabled and ray is
286 >         * ambient. Return as all ambient components accounted for, else
287 >         * continue. */
288 >        if (ambPmap(aval, r, rdepth))
289 >                return;
290 >
291 >        /* PMAP: Factor in specular-diffuse ambient (caustics) from photon
292 >         * map, if enabled and ray is primary, else caustic is zero.  Continue
293 >         * with RADIANCE ambient calculation */
294 >        copycolor(caustic, aval);
295 >        ambPmapCaustic(caustic, r, rdepth);
296 >        
297 >        if (ambdiv <= 0)                        /* no ambient calculation */
298 >                goto dumbamb;
299 >                                                /* check number of bounces */
300 >        if (rdepth >= ambounce)
301 >                goto dumbamb;
302 >                                                /* check ambient list */
303 >        if (ambincl != -1 && r->ro != NULL &&
304 >                        ambincl != inset(ambset, r->ro->omod))
305 >                goto dumbamb;
306 >
307 >        if (ambacc <= FTINY) {                  /* no ambient storage */
308 >                copycolor(acol, aval);
309 >                rdepth++;
310 >                ok = doambient(acol, r, r->rweight,
311 >                                NULL, NULL, NULL, NULL, NULL);
312 >                rdepth--;
313 >                if (!ok)
314 >                        goto dumbamb;
315 >                copycolor(aval, acol);
316 >
317 >                /* PMAP: add in caustic */
318 >                addcolor(aval, caustic);
319 >                return;
320 >        }
321 >
322 >        if (tracktime)                          /* sort to minimize thrashing */
323 >                sortambvals(0);
324 >                                                /* interpolate ambient value */
325 >        setcolor(acol, 0.0, 0.0, 0.0);
326 >        d = sumambient(acol, r, nrm, rdepth,
327 >                        &atrunk, thescene.cuorg, thescene.cusize);
328 >                        
329 >        if (d > FTINY) {
330 >                d = 1.0/d;
331 >                scalecolor(acol, d);
332 >                multcolor(aval, acol);
333 >
334 >                /* PMAP: add in caustic */
335 >                addcolor(aval, caustic);
336 >                return;
337 >        }
338 >        
339 >        rdepth++;                               /* need to cache new value */
340 >        ok = makeambient(acol, r, nrm, rdepth-1);
341 >        rdepth--;
342 >        
343 >        if (ok) {
344 >                multcolor(aval, acol);          /* computed new value */
345 >
346 >                /* PMAP: add in caustic */
347 >                addcolor(aval, caustic);
348 >                return;
349 >        }
350 >        
351 > dumbamb:                                        /* return global value */
352 >        if ((ambvwt <= 0) | (navsum == 0)) {
353 >                multcolor(aval, ambval);
354 >                
355 >                /* PMAP: add in caustic */
356 >                addcolor(aval, caustic);
357 >                return;
358 >        }
359 >        
360 >        l = bright(ambval);                     /* average in computations */  
361 >        if (l > FTINY) {
362 >                d = (log(l)*(double)ambvwt + avsum) /
363 >                                (double)(ambvwt + navsum);
364 >                d = exp(d) / l;
365 >                scalecolor(aval, d);
366 >                multcolor(aval, ambval);        /* apply color of ambval */
367 >        } else {
368 >                d = exp( avsum / (double)navsum );
369 >                scalecolor(aval, d);            /* neutral color */
370 >        }
371 > }
372 >
373 >
374 > /* Plug a potential leak where ambient cache value is occluded */
375 > static int
376 > plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang)
377 > {
378 >        const double    cost70sq = 0.1169778;   /* cos(70deg)^2 */
379 >        RAY             rtst;
380 >        FVECT           vdif;
381 >        double          normdot, ndotd, nadotd;
382 >        double          a, b, c, t[2];
383 >
384 >        ang += 2.*PI*(ang < 0);                 /* check direction flags */
385 >        if ( !(ap->corral>>(int)(ang*(16./PI)) & 1) )
386 >                return(0);
387 >        /*
388 >         * Generate test ray, targeting 20 degrees above sample point plane
389 >         * along surface normal from cache position.  This should be high
390 >         * enough to miss local geometry we don't really care about.
391 >         */
392 >        VSUB(vdif, ap->pos, r->rop);
393 >        normdot = DOT(anorm, r->ron);
394 >        ndotd = DOT(vdif, r->ron);
395 >        nadotd = DOT(vdif, anorm);
396 >        a = normdot*normdot - cost70sq;
397 >        b = 2.0*(normdot*ndotd - nadotd*cost70sq);
398 >        c = ndotd*ndotd - DOT(vdif,vdif)*cost70sq;
399 >        if (quadratic(t, a, b, c) != 2)
400 >                return(1);                      /* should rarely happen */
401 >        if (t[1] <= FTINY)
402 >                return(0);                      /* should fail behind test */
403 >        rayorigin(&rtst, SHADOW, r, NULL);
404 >        VSUM(rtst.rdir, vdif, anorm, t[1]);     /* further dist. > plane */
405 >        rtst.rmax = normalize(rtst.rdir);       /* short ray test */
406 >        while (localhit(&rtst, &thescene)) {    /* check for occluder */
407 >                if (rtst.ro->omod != OVOID &&
408 >                                (rtst.clipset == NULL ||
409 >                                        !inset(rtst.clipset, rtst.ro->omod)))
410 >                        return(1);              /* plug light leak */
411 >                VCOPY(rtst.rorg, rtst.rop);     /* skip invisible surface */
412 >                rtst.rmax -= rtst.rot;
413 >                rayclear(&rtst);
414 >        }
415 >        return(0);                              /* seems we're OK */
416 > }
417 >
418 >
419 > static double
420 > sumambient(             /* get interpolated ambient value */
421          COLOR  acol,
422 <        register RAY  *r,
422 >        RAY  *r,
423 >        FVECT  rn,
424 >        int  al,
425 >        AMBTREE  *at,
426 >        FVECT  c0,
427 >        double  s
428 > )
429 > {                       /* initial limit is 10 degrees plus ambacc radians */
430 >        const double    minangle = 10.0 * PI/180.;
431 >        double          maxangle = minangle + ambacc;
432 >        double          wsum = 0.0;
433 >        FVECT           ck0;
434 >        int             i, j;
435 >        AMBVAL          *av;
436 >
437 >        if (at->kid != NULL) {          /* sum children first */                                
438 >                s *= 0.5;
439 >                for (i = 0; i < 8; i++) {
440 >                        for (j = 0; j < 3; j++) {
441 >                                ck0[j] = c0[j];
442 >                                if (1<<j & i)
443 >                                        ck0[j] += s;
444 >                                if (r->rop[j] < ck0[j] - OCTSCALE*s)
445 >                                        break;
446 >                                if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
447 >                                        break;
448 >                        }
449 >                        if (j == 3)
450 >                                wsum += sumambient(acol, r, rn, al,
451 >                                                        at->kid+i, ck0, s);
452 >                }
453 >                                        /* good enough? */
454 >                if (wsum >= 0.05 && s > minarad*10.0)
455 >                        return(wsum);
456 >        }
457 >                                        /* adjust maximum angle */
458 >        if (at->alist != NULL && (at->alist->lvl <= al) & (r->rweight < 0.6))
459 >                maxangle = (maxangle - PI/2.)*pow(r->rweight,0.13) + PI/2.;
460 >                                        /* sum this node */
461 >        for (av = at->alist; av != NULL; av = av->next) {
462 >                double  u, v, d, delta_r2, delta_t2;
463 >                COLOR   ct;
464 >                FVECT   uvw[3];
465 >                                        /* record access */
466 >                if (tracktime)
467 >                        av->latick = ambclock;
468 >                /*
469 >                 *  Ambient level test
470 >                 */
471 >                if (av->lvl > al ||     /* list sorted, so this works */
472 >                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
473 >                        break;
474 >                /*
475 >                 *  Direction test using unperturbed normal
476 >                 */
477 >                decodedir(uvw[2], av->ndir);
478 >                d = DOT(uvw[2], r->ron);
479 >                if (d <= 0.0)           /* >= 90 degrees */
480 >                        continue;
481 >                delta_r2 = 2.0 - 2.0*d; /* approx. radians^2 */
482 >                if (delta_r2 >= maxangle*maxangle)
483 >                        continue;
484 >                /*
485 >                 *  Modified ray behind test
486 >                 */
487 >                VSUB(ck0, r->rop, av->pos);
488 >                d = DOT(ck0, uvw[2]);
489 >                if (d < -minarad*ambacc-.001)
490 >                        continue;
491 >                d /= av->rad[0];
492 >                delta_t2 = d*d;
493 >                if (delta_t2 >= ambacc*ambacc)
494 >                        continue;
495 >                /*
496 >                 *  Elliptical radii test based on Hessian
497 >                 */
498 >                decodedir(uvw[0], av->udir);
499 >                VCROSS(uvw[1], uvw[2], uvw[0]);
500 >                d = (u = DOT(ck0, uvw[0])) / av->rad[0];
501 >                delta_t2 += d*d;
502 >                d = (v = DOT(ck0, uvw[1])) / av->rad[1];
503 >                delta_t2 += d*d;
504 >                if (delta_t2 >= ambacc*ambacc)
505 >                        continue;
506 >                /*
507 >                 *  Test for potential light leak
508 >                 */
509 >                if (av->corral && plugaleak(r, av, uvw[2], atan2a(v,u)))
510 >                        continue;
511 >                /*
512 >                 *  Extrapolate value and compute final weight (hat function)
513 >                 */
514 >                if (!extambient(ct, av, r->rop, rn, uvw))
515 >                        continue;
516 >                d = tfunc(maxangle, sqrt(delta_r2), 0.0) *
517 >                        tfunc(ambacc, sqrt(delta_t2), 0.0);
518 >                scalecolor(ct, d);
519 >                addcolor(acol, ct);
520 >                wsum += d;
521 >        }
522 >        return(wsum);
523 > }
524 >
525 >
526 > static int
527 > makeambient(            /* make a new ambient value for storage */
528 >        COLOR  acol,
529 >        RAY  *r,
530 >        FVECT  rn,
531 >        int  al
532 > )
533 > {
534 >        AMBVAL  amb;
535 >        FVECT   uvw[3];
536 >        int     i;
537 >
538 >        amb.weight = 1.0;                       /* compute weight */
539 >        for (i = al; i-- > 0; )
540 >                amb.weight *= AVGREFL;
541 >        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
542 >                amb.weight = 1.25*r->rweight;
543 >        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
544 >                                                /* compute ambient */
545 >        i = doambient(acol, r, amb.weight,
546 >                        uvw, amb.rad, amb.gpos, amb.gdir, &amb.corral);
547 >        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
548 >        if (i <= 0 || amb.rad[0] <= FTINY)      /* no Hessian or zero radius */
549 >                return(i);
550 >                                                /* store value */
551 >        VCOPY(amb.pos, r->rop);
552 >        amb.ndir = encodedir(r->ron);
553 >        amb.udir = encodedir(uvw[0]);
554 >        amb.lvl = al;
555 >        copycolor(amb.val, acol);
556 >                                                /* insert into tree */
557 >        avsave(&amb);                           /* and save to file */
558 >        if (rn != r->ron) {                     /* texture */
559 >                VCOPY(uvw[2], r->ron);
560 >                extambient(acol, &amb, r->rop, rn, uvw);
561 >        }
562 >        return(1);
563 > }
564 >
565 >
566 > static int
567 > extambient(             /* extrapolate value at pv, nv */
568 >        COLOR  cr,
569 >        AMBVAL   *ap,
570 >        FVECT  pv,
571 >        FVECT  nv,
572 >        FVECT  uvw[3]
573 > )
574 > {
575 >        const double    min_d = 0.05;
576 >        static FVECT    my_uvw[3];
577 >        FVECT           v1;
578 >        int             i;
579 >        double          d = 1.0;        /* zeroeth order */
580 >
581 >        if (uvw == NULL) {              /* need local coordinates? */
582 >                decodedir(my_uvw[2], ap->ndir);
583 >                decodedir(my_uvw[0], ap->udir);
584 >                VCROSS(my_uvw[1], my_uvw[2], my_uvw[0]);
585 >                uvw = my_uvw;
586 >        }
587 >        for (i = 3; i--; )              /* gradient due to translation */
588 >                d += (pv[i] - ap->pos[i]) *
589 >                        (ap->gpos[0]*uvw[0][i] + ap->gpos[1]*uvw[1][i]);
590 >
591 >        VCROSS(v1, uvw[2], nv);         /* gradient due to rotation */
592 >        for (i = 3; i--; )
593 >                d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]);
594 >        
595 >        if (d < min_d)                  /* should not use if we can avoid it */
596 >                d = min_d;
597 >        copycolor(cr, ap->val);
598 >        scalecolor(cr, d);
599 >        return(d > min_d);
600 > }
601 >
602 >
603 > static void
604 > avinsert(                               /* insert ambient value in our tree */
605 >        AMBVAL *av
606 > )
607 > {
608 >        AMBTREE  *at;
609 >        AMBVAL  *ap;
610 >        AMBVAL  avh;
611 >        FVECT  ck0;
612 >        double  s;
613 >        int  branch;
614 >        int  i;
615 >
616 >        if (av->rad[0] <= FTINY)
617 >                error(CONSISTENCY, "zero ambient radius in avinsert");
618 >        at = &atrunk;
619 >        VCOPY(ck0, thescene.cuorg);
620 >        s = thescene.cusize;
621 >        while (s*(OCTSCALE/2) > av->rad[1]*ambacc) {
622 >                if (at->kid == NULL)
623 >                        if ((at->kid = newambtree()) == NULL)
624 >                                error(SYSTEM, "out of memory in avinsert");
625 >                s *= 0.5;
626 >                branch = 0;
627 >                for (i = 0; i < 3; i++)
628 >                        if (av->pos[i] > ck0[i] + s) {
629 >                                ck0[i] += s;
630 >                                branch |= 1 << i;
631 >                        }
632 >                at = at->kid + branch;
633 >        }
634 >        avh.next = at->alist;           /* order by increasing level */
635 >        for (ap = &avh; ap->next != NULL; ap = ap->next)
636 >                if ( ap->next->lvl > av->lvl ||
637 >                                (ap->next->lvl == av->lvl) &
638 >                                (ap->next->weight <= av->weight) )
639 >                        break;
640 >        av->next = ap->next;
641 >        ap->next = (AMBVAL*)av;
642 >        at->alist = avh.next;
643 > }
644 >
645 >
646 > #else /* ! NEWAMB */
647 >
648 > static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
649 >                                AMBTREE *at, FVECT c0, double s);
650 > static double   makeambient(COLOR acol, RAY *r, FVECT rn, int al);
651 > static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv);
652 >
653 >
654 > void
655 > multambient(            /* compute ambient component & multiply by coef. */
656 >        COLOR  aval,
657 >        RAY  *r,
658          FVECT  nrm
659   )
660   {
661          static int  rdepth = 0;                 /* ambient recursion */
662 +        COLOR   acol, caustic;
663          double  d, l;
664  
665 +        /* PMAP: Factor in ambient from global photon map (if enabled) and return
666 +         * as all ambient components accounted for */
667 +        if (ambPmap(aval, r, rdepth))
668 +                return;
669 +
670 +        /* PMAP: Otherwise factor in ambient from caustic photon map
671 +         * (ambPmapCaustic() returns zero if caustic photons disabled) and
672 +         * continue with RADIANCE ambient calculation */
673 +        copycolor(caustic, aval);
674 +        ambPmapCaustic(caustic, r, rdepth);
675 +        
676          if (ambdiv <= 0)                        /* no ambient calculation */
677                  goto dumbamb;
678                                                  /* check number of bounces */
# Line 272 | Line 684 | ambient(               /* compute ambient component for ray */
684                  goto dumbamb;
685  
686          if (ambacc <= FTINY) {                  /* no ambient storage */
687 +                copycolor(acol, aval);
688                  rdepth++;
689                  d = doambient(acol, r, r->rweight, NULL, NULL);
690                  rdepth--;
691                  if (d <= FTINY)
692                          goto dumbamb;
693 +                copycolor(aval, acol);          
694 +        
695 +           /* PMAP: add in caustic */
696 +                addcolor(aval, caustic);        
697                  return;
698          }
699  
700          if (tracktime)                          /* sort to minimize thrashing */
701                  sortambvals(0);
702 <                                                /* get ambient value */
702 >                                                /* interpolate ambient value */
703          setcolor(acol, 0.0, 0.0, 0.0);
704          d = sumambient(acol, r, nrm, rdepth,
705                          &atrunk, thescene.cuorg, thescene.cusize);
706 +                        
707          if (d > FTINY) {
708 <                scalecolor(acol, 1.0/d);
708 >                d = 1.0/d;
709 >                scalecolor(acol, d);
710 >                multcolor(aval, acol);
711 >                
712 >                /* PMAP: add in caustic */
713 >                addcolor(aval, caustic);        
714                  return;
715          }
716 +        
717          rdepth++;                               /* need to cache new value */
718          d = makeambient(acol, r, nrm, rdepth-1);
719          rdepth--;
720 <        if (d > FTINY)
720 >        
721 >        if (d > FTINY) {
722 >                multcolor(aval, acol);          /* got new value */
723 >
724 >                /* PMAP: add in caustic */
725 >                addcolor(aval, caustic);                        
726                  return;
727 +        }
728 +        
729   dumbamb:                                        /* return global value */
730 <        copycolor(acol, ambval);
731 <        if ((ambvwt <= 0) | (navsum == 0))
730 >        if ((ambvwt <= 0) | (navsum == 0)) {
731 >                multcolor(aval, ambval);
732 >
733 >                /* PMAP: add in caustic */
734 >                addcolor(aval, caustic);        
735                  return;
736 +        }
737 +        
738          l = bright(ambval);                     /* average in computations */
739          if (l > FTINY) {
740                  d = (log(l)*(double)ambvwt + avsum) /
741                                  (double)(ambvwt + navsum);
742                  d = exp(d) / l;
743 <                scalecolor(acol, d);            /* apply color of ambval */
743 >                scalecolor(aval, d);
744 >                multcolor(aval, ambval);        /* apply color of ambval */
745          } else {
746                  d = exp( avsum / (double)navsum );
747 <                setcolor(acol, d, d, d);        /* neutral color */
747 >                scalecolor(aval, d);            /* neutral color */
748          }
749   }
750  
751  
752 < extern double
752 > static double
753   sumambient(     /* get interpolated ambient value */
754          COLOR  acol,
755 <        register RAY  *r,
755 >        RAY  *r,
756          FVECT  rn,
757          int  al,
758          AMBTREE  *at,
# Line 327 | Line 764 | sumambient(    /* get interpolated ambient value */
764          COLOR  ct;
765          FVECT  ck0;
766          int  i;
767 <        register int  j;
768 <        register AMBVAL  *av;
767 >        int  j;
768 >        AMBVAL   *av;
769  
770          wsum = 0.0;
771                                          /* do this node */
# Line 339 | Line 776 | sumambient(    /* get interpolated ambient value */
776                  /*
777                   *  Ambient level test.
778                   */
779 <                if (av->lvl > al)       /* list sorted, so this works */
779 >                if (av->lvl > al ||     /* list sorted, so this works */
780 >                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
781                          break;
344                if (av->weight < r->rweight-FTINY)
345                        continue;
782                  /*
783                   *  Ambient radius test.
784                   */
785 <                d = av->pos[0] - r->rop[0];
786 <                e1 = d * d;
351 <                d = av->pos[1] - r->rop[1];
352 <                e1 += d * d;
353 <                d = av->pos[2] - r->rop[2];
354 <                e1 += d * d;
355 <                e1 /= av->rad * av->rad;
785 >                VSUB(ck0, av->pos, r->rop);
786 >                e1 = DOT(ck0, ck0) / (av->rad * av->rad);
787                  if (e1 > ambacc*ambacc*1.21)
788                          continue;
789                  /*
# Line 369 | Line 800 | sumambient(    /* get interpolated ambient value */
800                          }
801                  }
802                  e2 = (1.0 - d) * r->rweight;
803 <                if (e2 < 0.0) e2 = 0.0;
804 <                if (e1 + e2 > ambacc*ambacc*1.21)
803 >                if (e2 < 0.0)
804 >                        e2 = 0.0;
805 >                else if (e1 + e2 > ambacc*ambacc*1.21)
806                          continue;
807                  /*
808                   *  Ray behind test.
# Line 420 | Line 852 | sumambient(    /* get interpolated ambient value */
852                                  break;
853                  }
854                  if (j == 3)
855 <                        wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s);
855 >                        wsum += sumambient(acol, r, rn, al,
856 >                                                at->kid+i, ck0, s);
857          }
858          return(wsum);
859   }
860  
861  
862 < extern double
863 < makeambient(    /* make a new ambient value */
862 > static double
863 > makeambient(            /* make a new ambient value for storage */
864          COLOR  acol,
865 <        register RAY  *r,
865 >        RAY  *r,
866          FVECT  rn,
867          int  al
868   )
869   {
870          AMBVAL  amb;
871          FVECT   gp, gd;
872 <                                                /* compute weight */
873 <        amb.weight = pow(AVGREFL, (double)al);
874 <        if (r->rweight < 0.1*amb.weight)        /* heuristic */
875 <                amb.weight = r->rweight;
872 >        int     i;
873 >
874 >        amb.weight = 1.0;                       /* compute weight */
875 >        for (i = al; i-- > 0; )
876 >                amb.weight *= AVGREFL;
877 >        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
878 >                amb.weight = 1.25*r->rweight;
879 >        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
880                                                  /* compute ambient */
881          amb.rad = doambient(acol, r, amb.weight, gp, gd);
882 <        if (amb.rad <= FTINY)
882 >        if (amb.rad <= FTINY) {
883 >                setcolor(acol, 0.0, 0.0, 0.0);
884                  return(0.0);
885 <                                                /* store it */
885 >        }
886 >        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
887 >                                                /* store value */
888          VCOPY(amb.pos, r->rop);
889          VCOPY(amb.dir, r->ron);
890          amb.lvl = al;
# Line 459 | Line 899 | makeambient(   /* make a new ambient value */
899   }
900  
901  
902 < extern void
902 > static void
903   extambient(             /* extrapolate value at pv, nv */
904          COLOR  cr,
905 <        register AMBVAL  *ap,
905 >        AMBVAL   *ap,
906          FVECT  pv,
907          FVECT  nv
908   )
909   {
910          FVECT  v1;
911 <        register int  i;
911 >        int  i;
912          double  d;
913  
914          d = 1.0;                        /* zeroeth order */
# Line 488 | Line 928 | extambient(            /* extrapolate value at pv, nv */
928  
929  
930   static void
931 + avinsert(                               /* insert ambient value in our tree */
932 +        AMBVAL *av
933 + )
934 + {
935 +        AMBTREE  *at;
936 +        AMBVAL  *ap;
937 +        AMBVAL  avh;
938 +        FVECT  ck0;
939 +        double  s;
940 +        int  branch;
941 +        int  i;
942 +
943 +        if (av->rad <= FTINY)
944 +                error(CONSISTENCY, "zero ambient radius in avinsert");
945 +        at = &atrunk;
946 +        VCOPY(ck0, thescene.cuorg);
947 +        s = thescene.cusize;
948 +        while (s*(OCTSCALE/2) > av->rad*ambacc) {
949 +                if (at->kid == NULL)
950 +                        if ((at->kid = newambtree()) == NULL)
951 +                                error(SYSTEM, "out of memory in avinsert");
952 +                s *= 0.5;
953 +                branch = 0;
954 +                for (i = 0; i < 3; i++)
955 +                        if (av->pos[i] > ck0[i] + s) {
956 +                                ck0[i] += s;
957 +                                branch |= 1 << i;
958 +                        }
959 +                at = at->kid + branch;
960 +        }
961 +        avh.next = at->alist;           /* order by increasing level */
962 +        for (ap = &avh; ap->next != NULL; ap = ap->next)
963 +                if ( ap->next->lvl > av->lvl ||
964 +                                (ap->next->lvl == av->lvl) &
965 +                                (ap->next->weight <= av->weight) )
966 +                        break;
967 +        av->next = ap->next;
968 +        ap->next = (AMBVAL*)av;
969 +        at->alist = avh.next;
970 + }
971 +
972 + #endif  /* ! NEWAMB */
973 +
974 + /************* FOLLOWING ROUTINES SAME FOR NEW & OLD METHODS ***************/
975 +
976 + static void
977   initambfile(            /* initialize ambient file */
978 <        int  creat
978 >        int  cre8
979   )
980   {
981          extern char  *progname, *octname;
982          static char  *mybuf = NULL;
983  
984   #ifdef  F_SETLKW
985 <        aflock(creat ? F_WRLCK : F_RDLCK);
985 >        aflock(cre8 ? F_WRLCK : F_RDLCK);
986   #endif
987          SET_FILE_BINARY(ambfp);
988          if (mybuf == NULL)
989                  mybuf = (char *)bmalloc(BUFSIZ+8);
990          setbuf(ambfp, mybuf);
991 <        if (creat) {                    /* new file */
991 >        if (cre8) {                     /* new file */
992                  newheader("RADIANCE", ambfp);
993                  fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ",
994                                  progname, colval(ambval,RED),
# Line 511 | Line 997 | initambfile(           /* initialize ambient file */
997                  fprintf(ambfp, "-ad %d -as %d -ar %d ",
998                                  ambdiv, ambssamp, ambres);
999                  if (octname != NULL)
1000 <                        printargs(1, &octname, ambfp);
1001 <                else
516 <                        fputc('\n', ambfp);
1000 >                        fputs(octname, ambfp);
1001 >                fputc('\n', ambfp);
1002                  fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
1003                  fputnow(ambfp);
1004                  fputformat(AMBFMT, ambfp);
1005 <                putc('\n', ambfp);
1005 >                fputc('\n', ambfp);
1006                  putambmagic(ambfp);
1007          } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
1008                  error(USER, "bad ambient file");
# Line 529 | Line 1014 | avsave(                                /* insert and save an ambient value */
1014          AMBVAL  *av
1015   )
1016   {
1017 <        avinsert(avstore(av));
1017 >        avstore(av);
1018          if (ambfp == NULL)
1019                  return;
1020          if (writambval(av, ambfp) < 0)
# Line 544 | Line 1029 | writerr:
1029  
1030  
1031   static AMBVAL *
1032 < avstore(                                /* allocate memory and store aval */
1033 <        register AMBVAL  *aval
1032 > avstore(                                /* allocate memory and save aval */
1033 >        AMBVAL  *aval
1034   )
1035   {
1036 <        register AMBVAL  *av;
1036 >        AMBVAL  *av;
1037          double  d;
1038  
1039          if ((av = newambval()) == NULL)
# Line 562 | Line 1047 | avstore(                               /* allocate memory and store aval */
1047                  avsum += log(d);
1048                  navsum++;
1049          }
1050 +        avinsert(av);                   /* insert in our cache tree */
1051          return(av);
1052   }
1053  
# Line 574 | Line 1060 | static AMBTREE  *atfreelist = NULL;    /* free ambient tr
1060   static AMBTREE *
1061   newambtree(void)                                /* allocate 8 ambient tree structs */
1062   {
1063 <        register AMBTREE  *atp, *upperlim;
1063 >        AMBTREE  *atp, *upperlim;
1064  
1065          if (atfreelist == NULL) {       /* get more nodes */
1066                  atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE));
# Line 604 | Line 1090 | freeambtree(                   /* free 8 ambient tree structs */
1090  
1091  
1092   static void
607 avinsert(                               /* insert ambient value in our tree */
608        void *av
609 )
610 {
611        register AMBTREE  *at;
612        register AMBVAL  *ap;
613        AMBVAL  avh;
614        FVECT  ck0;
615        double  s;
616        int  branch;
617        register int  i;
618
619        if (((AMBVAL*)av)->rad <= FTINY)
620                error(CONSISTENCY, "zero ambient radius in avinsert");
621        at = &atrunk;
622        VCOPY(ck0, thescene.cuorg);
623        s = thescene.cusize;
624        while (s*(OCTSCALE/2) > ((AMBVAL*)av)->rad*ambacc) {
625                if (at->kid == NULL)
626                        if ((at->kid = newambtree()) == NULL)
627                                error(SYSTEM, "out of memory in avinsert");
628                s *= 0.5;
629                branch = 0;
630                for (i = 0; i < 3; i++)
631                        if (((AMBVAL*)av)->pos[i] > ck0[i] + s) {
632                                ck0[i] += s;
633                                branch |= 1 << i;
634                        }
635                at = at->kid + branch;
636        }
637        avh.next = at->alist;           /* order by increasing level */
638        for (ap = &avh; ap->next != NULL; ap = ap->next)
639                if (ap->next->lvl >= ((AMBVAL*)av)->lvl)
640                        break;
641        ((AMBVAL*)av)->next = ap->next;
642        ap->next = (AMBVAL*)av;
643        at->alist = avh.next;
644 }
645
646
647 static void
1093   unloadatree(                    /* unload an ambient value tree */
1094 <        register AMBTREE  *at,
1094 >        AMBTREE  *at,
1095          unloadtf_t *f
1096   )
1097   {
1098 <        register AMBVAL  *av;
1099 <        register int  i;
1098 >        AMBVAL  *av;
1099 >        int  i;
1100                                          /* transfer values at this node */
1101          for (av = at->alist; av != NULL; av = at->alist) {
1102                  at->alist = av->next;
# Line 676 | Line 1121 | static int     i_avlist;               /* index for lists */
1121   static int alatcmp(const void *av1, const void *av2);
1122  
1123   static void
1124 + avfree(AMBVAL *av)
1125 + {
1126 +        free(av);
1127 + }
1128 +
1129 + static void
1130   av2list(
1131 <        void *av
1131 >        AMBVAL *av
1132   )
1133   {
1134   #ifdef DEBUG
# Line 685 | Line 1136 | av2list(
1136                  error(CONSISTENCY, "too many ambient values in av2list1");
1137   #endif
1138          avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av;
1139 <        avlist1[i_avlist++].t = ((AMBVAL*)av)->latick;
1139 >        avlist1[i_avlist++].t = av->latick;
1140   }
1141  
1142  
# Line 695 | Line 1146 | alatcmp(                       /* compare ambient values for MRA */
1146          const void *av2
1147   )
1148   {
1149 <        register long  lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t;
1149 >        long  lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t;
1150          return(lc<0 ? -1 : lc>0 ? 1 : 0);
1151   }
1152  
# Line 712 | Line 1163 | aposcmp(                       /* compare ambient value positions */
1163          const void      *avp2
1164   )
1165   {
1166 <        register long   diff = *(char * const *)avp1 - *(char * const *)avp2;
1166 >        long    diff = *(char * const *)avp1 - *(char * const *)avp2;
1167          if (diff < 0)
1168                  return(-1);
1169          return(diff > 0);
1170   }
1171  
1172 < #if 1
1172 >
1173   static int
1174   avlmemi(                                /* find list position from address */
1175          AMBVAL  *avaddr
1176   )
1177   {
1178 <        register AMBVAL  **avlpp;
1178 >        AMBVAL  **avlpp;
1179  
1180          avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
1181 <                        nambvals, sizeof(AMBVAL *), aposcmp);
1181 >                        nambvals, sizeof(AMBVAL *), &aposcmp);
1182          if (avlpp == NULL)
1183                  error(CONSISTENCY, "address not found in avlmemi");
1184          return(avlpp - avlist2);
1185   }
735 #else
736 #define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \
737                                nambvals,sizeof(AMBVAL *),aposcmp) - avlist2)
738 #endif
1186  
1187  
1188   static void
# Line 745 | Line 1192 | sortambvals(                   /* resort ambient values */
1192   {
1193          AMBTREE  oldatrunk;
1194          AMBVAL  tav, *tap, *pnext;
1195 <        register int    i, j;
1195 >        int     i, j;
1196                                          /* see if it's time yet */
1197          if (!always && (ambclock++ < lastsort+sortintvl ||
1198                          nambvals < SORT_THRESH))
# Line 779 | Line 1226 | sortambvals(                   /* resort ambient values */
1226                          oldatrunk = atrunk;
1227                          atrunk.alist = NULL;
1228                          atrunk.kid = NULL;
1229 <                        unloadatree(&oldatrunk, avinsert);
1229 >                        unloadatree(&oldatrunk, &avinsert);
1230                  }
1231          } else {                        /* sort memory by last access time */
1232                  /*
# Line 796 | Line 1243 | sortambvals(                   /* resort ambient values */
1243                  eputs(errmsg);
1244   #endif
1245                  i_avlist = 0;
1246 <                unloadatree(&atrunk, av2list);  /* empty current tree */
1246 >                unloadatree(&atrunk, &av2list); /* empty current tree */
1247   #ifdef DEBUG
1248                  if (i_avlist < nambvals)
1249                          error(CONSISTENCY, "missing ambient values in sortambvals");
# Line 845 | Line 1292 | aflock(                        /* lock/unlock ambient file */
1292   {
1293          static struct flock  fls;       /* static so initialized to zeroes */
1294  
1295 +        if (typ == fls.l_type)          /* already called? */
1296 +                return;
1297          fls.l_type = typ;
1298          if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
1299                  error(SYSTEM, "cannot (un)lock ambient file");
1300   }
1301  
1302  
1303 < extern int
1303 > int
1304   ambsync(void)                   /* synchronize ambient file */
1305   {
1306          long  flen;
1307          AMBVAL  avs;
1308 <        register int  n;
1308 >        int  n;
1309  
1310 <        if (nunflshed == 0)
1310 >        if (ambfp == NULL)      /* no ambient file? */
1311                  return(0);
1312 <        if (lastpos < 0)        /* initializing (locked in initambfile) */
1313 <                goto syncend;
865 <                                /* gain exclusive access */
866 <        aflock(F_WRLCK);
1312 >                                /* gain appropriate access */
1313 >        aflock(nunflshed ? F_WRLCK : F_RDLCK);
1314                                  /* see if file has grown */
1315          if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
1316                  goto seekerr;
1317 <        if ( (n = flen - lastpos) ) {           /* file has grown */
1317 >        if ((n = flen - lastpos) > 0) {         /* file has grown */
1318                  if (ambinp == NULL) {           /* use duplicate filedes */
1319                          ambinp = fdopen(dup(fileno(ambfp)), "r");
1320                          if (ambinp == NULL)
1321                                  error(SYSTEM, "fdopen failed in ambsync");
1322                  }
1323 <                if (fseek(ambinp, lastpos, 0) < 0)
1323 >                if (fseek(ambinp, lastpos, SEEK_SET) < 0)
1324                          goto seekerr;
1325                  while (n >= AMBVALSIZ) {        /* load contributed values */
1326                          if (!readambval(&avs, ambinp)) {
# Line 883 | Line 1330 | ambsync(void)                  /* synchronize ambient file */
1330                                  error(WARNING, errmsg);
1331                                  break;
1332                          }
1333 <                        avinsert(avstore(&avs));
1333 >                        avstore(&avs);
1334                          n -= AMBVALSIZ;
1335                  }
1336 +                lastpos = flen - n;
1337                  /*** seek always as safety measure
1338                  if (n) ***/                     /* alignment */
1339 <                        if (lseek(fileno(ambfp), (off_t)(flen-n), SEEK_SET) < 0)
1339 >                        if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1340                                  goto seekerr;
1341          }
894 #ifdef  DEBUG
895        if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) {
896                sprintf(errmsg, "ambient file buffer at %d rather than %d",
897                                ambfp->_ptr - ambfp->_base,
898                                nunflshed*AMBVALSIZ);
899                error(CONSISTENCY, errmsg);
900        }
901 #endif
902 syncend:
1342          n = fflush(ambfp);                      /* calls write() at last */
1343 <        if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
1343 >        if (n != EOF)
1344 >                lastpos += (long)nunflshed*AMBVALSIZ;
1345 >        else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
1346                  goto seekerr;
1347 +                
1348          aflock(F_UNLCK);                        /* release file */
1349          nunflshed = 0;
1350          return(n);
# Line 911 | Line 1353 | seekerr:
1353          return -1; /* pro forma return */
1354   }
1355  
1356 < #else
1356 > #else   /* ! F_SETLKW */
1357  
1358 < extern int
1358 > int
1359   ambsync(void)                   /* flush ambient file */
1360   {
1361 <        if (nunflshed == 0)
1361 >        if (ambfp == NULL)
1362                  return(0);
1363          nunflshed = 0;
1364          return(fflush(ambfp));
1365   }
1366  
1367 < #endif
1367 > #endif  /* ! F_SETLKW */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines