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.91 by greg, Thu Jun 19 16:26:55 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 77 | Line 76 | static long  lastpos = -1;             /* last flush position */
76   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
77  
78   #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
80 #define  freeav(av)     free((void *)av);
79  
80   static void initambfile(int creat);
81   static void avsave(AMBVAL *av);
# Line 185 | Line 183 | setambient(void)                               /* initialize calculation */
183                                          (flen - lastpos)/AMBVALSIZ);
184                          error(WARNING, errmsg);
185                          fseek(ambfp, lastpos, SEEK_SET);
188 #ifndef _WIN32 /* XXX we need a replacement for that one */
186                          ftruncate(fileno(ambfp), (off_t)lastpos);
190 #endif
187                  }
188          } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
189                  initambfile(1);                 /* else create new file */
# Line 197 | Line 193 | setambient(void)                               /* initialize calculation */
193                  sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
194                  error(SYSTEM, errmsg);
195          }
200 #ifdef getc_unlocked
201        flockfile(ambfp);                       /* application-level lock */
202 #endif
196   #ifdef  F_SETLKW
197          aflock(F_UNLCK);                        /* release file */
198   #endif
# Line 220 | 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 271 | Line 264 | static int     plugaleak(RAY *r, AMBVAL *ap, FVECT anorm,
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 void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
267 > static int      extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
268                                  FVECT uvw[3]);
269  
270   void
# Line 282 | 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 305 | Line 310 | multambient(           /* compute ambient component & multiply
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 314 | 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);          /* 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 486 | Line 508 | sumambient(            /* get interpolated ambient value */
508                  /*
509                   *  Extrapolate value and compute final weight (hat function)
510                   */
511 <                extambient(ct, av, r->rop, rn, uvw);
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);
515                  scalecolor(ct, d);
# Line 537 | Line 560 | makeambient(           /* make a new ambient value for storage
560   }
561  
562  
563 < static void
563 > static int
564   extambient(             /* extrapolate value at pv, nv */
565          COLOR  cr,
566          AMBVAL   *ap,
# Line 546 | Line 569 | extambient(            /* extrapolate value at pv, nv */
569          FVECT  uvw[3]
570   )
571   {
572 +        const double    min_d = 0.05;
573          static FVECT    my_uvw[3];
574          FVECT           v1;
575          int             i;
# Line 565 | 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);
570 <                return;
571 <        }
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 633 | 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 653 | 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 663 | 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 1020 | 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 1046 | 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 1123 | 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 1167 | 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 1189 | 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 1211 | 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)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines