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.85 by greg, Tue May 6 17:15:11 2014 UTC vs.
Revision 2.96 by rschregle, Wed Jul 29 18:57:03 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 109 | Line 108 | setambres(                             /* set ambient resolution */
108                                                  /* set min & max radii */
109          if (ar <= 0) {
110                  minarad = 0;
111 <                maxarad = thescene.cusize*0.5;
111 >                maxarad = thescene.cusize*0.2;
112          } else {
113                  minarad = thescene.cusize / ar;
114                  maxarad = 64.0 * minarad;               /* heuristic */
115 <                if (maxarad > thescene.cusize*0.5)
116 <                        maxarad = thescene.cusize*0.5;
115 >                if (maxarad > thescene.cusize*0.2)
116 >                        maxarad = thescene.cusize*0.2;
117          }
118          if (minarad <= FTINY)
119                  minarad = 10.0*FTINY;
# Line 165 | Line 164 | setambient(void)                               /* initialize calculation */
164                  initambfile(0);                 /* file exists */
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 263 | Line 262 | ambnotify(                     /* record new modifier */
262  
263   /************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/
264  
265 < #ifdef NEWAMB
265 > #ifndef OLDAMB
266  
267   #define tfunc(lwr, x, upr)      (((x)-(lwr))/((upr)-(lwr)))
268  
269 + static int      plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang);
270   static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
271                                  AMBTREE *at, FVECT c0, double s);
272   static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
273 < static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
273 > static int      extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
274                                  FVECT uvw[3]);
275  
276   void
# Line 281 | Line 281 | multambient(           /* compute ambient component & multiply
281   )
282   {
283          static int  rdepth = 0;                 /* ambient recursion */
284 <        COLOR   acol;
284 >        COLOR   acol, caustic;
285          int     ok;
286          double  d, l;
287  
288 +        /* PMAP: Factor in ambient from photon map, if enabled and ray is
289 +         * ambient. Return as all ambient components accounted for, else
290 +         * continue. */
291 +        if (ambPmap(aval, r, rdepth))
292 +                return;
293 +
294 +        /* PMAP: Factor in specular-diffuse ambient (caustics) from photon
295 +         * map, if enabled and ray is primary, else caustic is zero.  Continue
296 +         * with RADIANCE ambient calculation */
297 +        copycolor(caustic, aval);
298 +        ambPmapCaustic(caustic, r, rdepth);
299 +        
300          if (ambdiv <= 0)                        /* no ambient calculation */
301                  goto dumbamb;
302                                                  /* check number of bounces */
# Line 298 | Line 310 | multambient(           /* compute ambient component & multiply
310          if (ambacc <= FTINY) {                  /* no ambient storage */
311                  copycolor(acol, aval);
312                  rdepth++;
313 <                ok = doambient(acol, r, r->rweight, NULL, NULL, NULL, NULL);
313 >                ok = doambient(acol, r, r->rweight,
314 >                                NULL, NULL, NULL, NULL, NULL);
315                  rdepth--;
316                  if (!ok)
317                          goto dumbamb;
318                  copycolor(aval, acol);
319 +
320 +                /* PMAP: add in caustic */
321 +                addcolor(aval, caustic);
322                  return;
323          }
324  
# Line 312 | Line 328 | multambient(           /* compute ambient component & multiply
328          setcolor(acol, 0.0, 0.0, 0.0);
329          d = sumambient(acol, r, nrm, rdepth,
330                          &atrunk, thescene.cuorg, thescene.cusize);
331 +                        
332          if (d > FTINY) {
333                  d = 1.0/d;
334                  scalecolor(acol, d);
335                  multcolor(aval, acol);
336 +
337 +                /* PMAP: add in caustic */
338 +                addcolor(aval, caustic);
339                  return;
340          }
341 +        
342          rdepth++;                               /* need to cache new value */
343          ok = makeambient(acol, r, nrm, rdepth-1);
344          rdepth--;
345 +        
346          if (ok) {
347                  multcolor(aval, acol);          /* computed new value */
348 +
349 +                /* PMAP: add in caustic */
350 +                addcolor(aval, caustic);
351                  return;
352          }
353 +        
354   dumbamb:                                        /* return global value */
355          if ((ambvwt <= 0) | (navsum == 0)) {
356                  multcolor(aval, ambval);
357 +                
358 +                /* PMAP: add in caustic */
359 +                addcolor(aval, caustic);
360                  return;
361          }
362 <        l = bright(ambval);                     /* average in computations */
362 >        
363 >        l = bright(ambval);                     /* average in computations */  
364          if (l > FTINY) {
365                  d = (log(l)*(double)ambvwt + avsum) /
366                                  (double)(ambvwt + navsum);
# Line 344 | Line 374 | dumbamb:                                       /* return global value */
374   }
375  
376  
377 < double
377 > /* Plug a potential leak where ambient cache value is occluded */
378 > static int
379 > plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang)
380 > {
381 >        const double    cost70sq = 0.1169778;   /* cos(70deg)^2 */
382 >        RAY             rtst;
383 >        FVECT           vdif;
384 >        double          normdot, ndotd, nadotd;
385 >        double          a, b, c, t[2];
386 >
387 >        ang += 2.*PI*(ang < 0);                 /* check direction flags */
388 >        if ( !(ap->corral>>(int)(ang*(16./PI)) & 1) )
389 >                return(0);
390 >        /*
391 >         * Generate test ray, targeting 20 degrees above sample point plane
392 >         * along surface normal from cache position.  This should be high
393 >         * enough to miss local geometry we don't really care about.
394 >         */
395 >        VSUB(vdif, ap->pos, r->rop);
396 >        normdot = DOT(anorm, r->ron);
397 >        ndotd = DOT(vdif, r->ron);
398 >        nadotd = DOT(vdif, anorm);
399 >        a = normdot*normdot - cost70sq;
400 >        b = 2.0*(normdot*ndotd - nadotd*cost70sq);
401 >        c = ndotd*ndotd - DOT(vdif,vdif)*cost70sq;
402 >        if (quadratic(t, a, b, c) != 2)
403 >                return(1);                      /* should rarely happen */
404 >        if (t[1] <= FTINY)
405 >                return(0);                      /* should fail behind test */
406 >        rayorigin(&rtst, SHADOW, r, NULL);
407 >        VSUM(rtst.rdir, vdif, anorm, t[1]);     /* further dist. > plane */
408 >        rtst.rmax = normalize(rtst.rdir);       /* short ray test */
409 >        while (localhit(&rtst, &thescene)) {    /* check for occluder */
410 >                if (rtst.ro->omod != OVOID &&
411 >                                (rtst.clipset == NULL ||
412 >                                        !inset(rtst.clipset, rtst.ro->omod)))
413 >                        return(1);              /* plug light leak */
414 >                VCOPY(rtst.rorg, rtst.rop);     /* skip invisible surface */
415 >                rtst.rmax -= rtst.rot;
416 >                rayclear(&rtst);
417 >        }
418 >        return(0);                              /* seems we're OK */
419 > }
420 >
421 >
422 > static double
423   sumambient(             /* get interpolated ambient value */
424          COLOR  acol,
425          RAY  *r,
# Line 387 | Line 462 | sumambient(            /* get interpolated ambient value */
462                  maxangle = (maxangle - PI/2.)*pow(r->rweight,0.13) + PI/2.;
463                                          /* sum this node */
464          for (av = at->alist; av != NULL; av = av->next) {
465 <                double  d, delta_r2, delta_t2;
465 >                double  u, v, d, delta_r2, delta_t2;
466                  COLOR   ct;
467                  FVECT   uvw[3];
468                                          /* record access */
# Line 396 | Line 471 | sumambient(            /* get interpolated ambient value */
471                  /*
472                   *  Ambient level test
473                   */
474 <                if (av->lvl > al)       /* list sorted, so this works */
474 >                if (av->lvl > al ||     /* list sorted, so this works */
475 >                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
476                          break;
401                if (av->weight < 0.9*r->rweight)
402                        continue;
477                  /*
478                   *  Direction test using unperturbed normal
479                   */
# Line 426 | Line 500 | sumambient(            /* get interpolated ambient value */
500                   */
501                  decodedir(uvw[0], av->udir);
502                  VCROSS(uvw[1], uvw[2], uvw[0]);
503 <                d = DOT(ck0, uvw[0]) / av->rad[0];
503 >                d = (u = DOT(ck0, uvw[0])) / av->rad[0];
504                  delta_t2 += d*d;
505 <                d = DOT(ck0, uvw[1]) / av->rad[1];
505 >                d = (v = DOT(ck0, uvw[1])) / av->rad[1];
506                  delta_t2 += d*d;
507                  if (delta_t2 >= ambacc*ambacc)
508                          continue;
509                  /*
510 +                 *  Test for potential light leak
511 +                 */
512 +                if (av->corral && plugaleak(r, av, uvw[2], atan2a(v,u)))
513 +                        continue;
514 +                /*
515                   *  Extrapolate value and compute final weight (hat function)
516                   */
517 <                extambient(ct, av, r->rop, rn, uvw);
517 >                if (!extambient(ct, av, r->rop, rn, uvw))
518 >                        continue;
519                  d = tfunc(maxangle, sqrt(delta_r2), 0.0) *
520                          tfunc(ambacc, sqrt(delta_t2), 0.0);
521                  scalecolor(ct, d);
# Line 446 | Line 526 | sumambient(            /* get interpolated ambient value */
526   }
527  
528  
529 < int
529 > static int
530   makeambient(            /* make a new ambient value for storage */
531          COLOR  acol,
532          RAY  *r,
# Line 465 | Line 545 | makeambient(           /* make a new ambient value for storage
545                  amb.weight = 1.25*r->rweight;
546          setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
547                                                  /* compute ambient */
548 <        i = doambient(acol, r, amb.weight, uvw, amb.rad, amb.gpos, amb.gdir);
548 >        i = doambient(acol, r, amb.weight,
549 >                        uvw, amb.rad, amb.gpos, amb.gdir, &amb.corral);
550          scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
551          if (i <= 0 || amb.rad[0] <= FTINY)      /* no Hessian or zero radius */
552                  return(i);
# Line 485 | Line 566 | makeambient(           /* make a new ambient value for storage
566   }
567  
568  
569 < void
569 > static int
570   extambient(             /* extrapolate value at pv, nv */
571          COLOR  cr,
572          AMBVAL   *ap,
# Line 494 | Line 575 | extambient(            /* extrapolate value at pv, nv */
575          FVECT  uvw[3]
576   )
577   {
578 +        const double    min_d = 0.05;
579          static FVECT    my_uvw[3];
580          FVECT           v1;
581          int             i;
# Line 513 | Line 595 | extambient(            /* extrapolate value at pv, nv */
595          for (i = 3; i--; )
596                  d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]);
597          
598 <        if (d <= 0.0) {
599 <                setcolor(cr, 0.0, 0.0, 0.0);
518 <                return;
519 <        }
598 >        if (d < min_d)                  /* should not use if we can avoid it */
599 >                d = min_d;
600          copycolor(cr, ap->val);
601          scalecolor(cr, d);
602 +        return(d > min_d);
603   }
604  
605  
# Line 555 | Line 636 | avinsert(                              /* insert ambient value in our tree */
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 >= av->lvl)
639 >                if ( ap->next->lvl > av->lvl ||
640 >                                (ap->next->lvl == av->lvl) &
641 >                                (ap->next->weight <= av->weight) )
642                          break;
643          av->next = ap->next;
644          ap->next = (AMBVAL*)av;
# Line 579 | Line 662 | multambient(           /* compute ambient component & multiply
662   )
663   {
664          static int  rdepth = 0;                 /* ambient recursion */
665 <        COLOR   acol;
665 >        COLOR   acol, caustic;
666          double  d, l;
667  
668 +        /* PMAP: Factor in ambient from global photon map (if enabled) and return
669 +         * as all ambient components accounted for */
670 +        if (ambPmap(aval, r, rdepth))
671 +                return;
672 +
673 +        /* PMAP: Otherwise factor in ambient from caustic photon map
674 +         * (ambPmapCaustic() returns zero if caustic photons disabled) and
675 +         * continue with RADIANCE ambient calculation */
676 +        copycolor(caustic, aval);
677 +        ambPmapCaustic(caustic, r, rdepth);
678 +        
679          if (ambdiv <= 0)                        /* no ambient calculation */
680                  goto dumbamb;
681                                                  /* check number of bounces */
# Line 599 | Line 693 | multambient(           /* compute ambient component & multiply
693                  rdepth--;
694                  if (d <= FTINY)
695                          goto dumbamb;
696 <                copycolor(aval, acol);
696 >                copycolor(aval, acol);          
697 >        
698 >           /* PMAP: add in caustic */
699 >                addcolor(aval, caustic);        
700                  return;
701          }
702  
# Line 609 | Line 706 | multambient(           /* compute ambient component & multiply
706          setcolor(acol, 0.0, 0.0, 0.0);
707          d = sumambient(acol, r, nrm, rdepth,
708                          &atrunk, thescene.cuorg, thescene.cusize);
709 +                        
710          if (d > FTINY) {
711                  d = 1.0/d;
712                  scalecolor(acol, d);
713                  multcolor(aval, acol);
714 +                
715 +                /* PMAP: add in caustic */
716 +                addcolor(aval, caustic);        
717                  return;
718          }
719 +        
720          rdepth++;                               /* need to cache new value */
721          d = makeambient(acol, r, nrm, rdepth-1);
722          rdepth--;
723 +        
724          if (d > FTINY) {
725                  multcolor(aval, acol);          /* got new value */
726 +
727 +                /* PMAP: add in caustic */
728 +                addcolor(aval, caustic);                        
729                  return;
730          }
731 +        
732   dumbamb:                                        /* return global value */
733          if ((ambvwt <= 0) | (navsum == 0)) {
734                  multcolor(aval, ambval);
735 +
736 +                /* PMAP: add in caustic */
737 +                addcolor(aval, caustic);        
738                  return;
739          }
740 +        
741          l = bright(ambval);                     /* average in computations */
742          if (l > FTINY) {
743                  d = (log(l)*(double)ambvwt + avsum) /
# Line 668 | Line 779 | sumambient(    /* get interpolated ambient value */
779                  /*
780                   *  Ambient level test.
781                   */
782 <                if (av->lvl > al)       /* list sorted, so this works */
782 >                if (av->lvl > al ||     /* list sorted, so this works */
783 >                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
784                          break;
673                if (av->weight < 0.9*r->rweight)
674                        continue;
785                  /*
786                   *  Ambient radius test.
787                   */
# Line 853 | Line 963 | avinsert(                              /* insert ambient value in our tree */
963          }
964          avh.next = at->alist;           /* order by increasing level */
965          for (ap = &avh; ap->next != NULL; ap = ap->next)
966 <                if (ap->next->lvl >= av->lvl)
966 >                if ( ap->next->lvl > av->lvl ||
967 >                                (ap->next->lvl == av->lvl) &
968 >                                (ap->next->weight <= av->weight) )
969                          break;
970          av->next = ap->next;
971          ap->next = (AMBVAL*)av;
# Line 905 | Line 1017 | avsave(                                /* insert and save an ambient value */
1017          AMBVAL  *av
1018   )
1019   {
1020 <        avinsert(avstore(av));
1020 >        avstore(av);
1021          if (ambfp == NULL)
1022                  return;
1023          if (writambval(av, ambfp) < 0)
# Line 920 | Line 1032 | writerr:
1032  
1033  
1034   static AMBVAL *
1035 < avstore(                                /* allocate memory and store aval */
1035 > avstore(                                /* allocate memory and save aval */
1036          AMBVAL  *aval
1037   )
1038   {
# Line 938 | Line 1050 | avstore(                               /* allocate memory and store aval */
1050                  avsum += log(d);
1051                  navsum++;
1052          }
1053 +        avinsert(av);                   /* insert in our cache tree */
1054          return(av);
1055   }
1056  
# Line 1138 | Line 1251 | sortambvals(                   /* resort ambient values */
1251                  if (i_avlist < nambvals)
1252                          error(CONSISTENCY, "missing ambient values in sortambvals");
1253   #endif
1254 <                qsort((char *)avlist1, nambvals, sizeof(struct avl), &alatcmp);
1255 <                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), &aposcmp);
1254 >                qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp);
1255 >                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
1256                  for (i = 0; i < nambvals; i++) {
1257                          if (avlist1[i].p == NULL)
1258                                  continue;
# Line 1220 | Line 1333 | ambsync(void)                  /* synchronize ambient file */
1333                                  error(WARNING, errmsg);
1334                                  break;
1335                          }
1336 <                        avinsert(avstore(&avs));
1336 >                        avstore(&avs);
1337                          n -= AMBVALSIZ;
1338                  }
1339                  lastpos = flen - n;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines