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.27 by greg, Sat Apr 29 10:51:28 1995 UTC vs.
Revision 2.36 by greg, Wed Feb 14 15:43:22 1996 UTC

# Line 18 | Line 18 | static char SCCSid[] = "$SunId$ LBL";
18  
19   #include  "random.h"
20  
21 < #define  OCTSCALE       0.5     /* ceil((valid rad.)/(cube size)) */
21 > #ifndef  OCTSCALE
22 > #define  OCTSCALE       1.0     /* ceil((valid rad.)/(cube size)) */
23 > #endif
24  
25   typedef struct ambtree {
26          AMBVAL  *alist;         /* ambient value list */
# Line 48 | Line 50 | static int  nunflshed = 0;     /* number of unflushed ambi
50   #endif
51   #endif
52   #ifndef SORT_INTVL
53 < #define SORT_INTVL      (SORT_THRESH*8)
53 > #define SORT_INTVL      (SORT_THRESH*256)
54   #endif
55 + #ifndef MAX_SORT_INTVL
56 + #define MAX_SORT_INTVL  (SORT_INTVL<<4)
57 + #endif
58  
59 + static COLOR  avsum = BLKCOLOR;         /* computed ambient value sum */
60 + static unsigned int  nambvals = 0;      /* total number of indirect values */
61 + static unsigned int  nambshare = 0;     /* number of values from file */
62   static unsigned long  ambclock = 0;     /* ambient access clock */
55 static unsigned int  nambvals = 0;      /* number of stored ambient values */
63   static unsigned long  lastsort = 0;     /* time of last value sort */
64   static long  sortintvl = SORT_INTVL;    /* time until next sort */
65  
# Line 60 | Line 67 | static long  sortintvl = SORT_INTVL;   /* time until nex
67          /*
68           * Track access times unless we are sharing ambient values
69           * through memory on a multiprocessor, when we want to avoid
70 <         * claiming our own memory (copy on write).
70 >         * claiming our own memory (copy on write).  Go ahead anyway
71 >         * if more than two thirds of our values are unshared.
72           */
73 < #define tracktime       (shm_boundary == NULL || ambfp == NULL)
66 < #define checksort()     if (ambclock > lastsort+sortintvl && \
67 <                        nambvals > SORT_THRESH) sortambvals(0)
73 > #define tracktime       (shm_boundary == NULL || nambvals > 3*nambshare)
74  
75   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
76  
# Line 88 | Line 94 | int  ar;
94                  maxarad = thescene.cusize / 2.0;
95          } else {
96                  minarad = thescene.cusize / ar;
97 <                maxarad = 16 * minarad;                 /* heuristic */
97 >                maxarad = 64 * minarad;                 /* heuristic */
98                  if (maxarad > thescene.cusize / 2.0)
99                          maxarad = thescene.cusize / 2.0;
100          }
# Line 102 | Line 108 | int  ar;
108   setambacc(newa)                         /* set ambient accuracy */
109   double  newa;
110   {
111 <        static double  oldambacc = -1.0;
111 >        double  ambdiff;
112  
113 <        ambacc = newa < 0.0 ? 0.0 : newa;       /* may be done already */
114 <        if (oldambacc < -FTINY)
115 <                oldambacc = ambacc;     /* do nothing first call */
116 <        if (fabs(newa - oldambacc) < 0.01)
117 <                return;                 /* insignificant -- don't bother */
112 <        if (ambacc <= FTINY)
113 <                return;                 /* cannot build new tree */
114 <                                        /* else need to rebuild tree */
115 <        sortambvals(1);
116 <        oldambacc = ambacc;             /* remeber setting for next call */
113 >        if (newa < 0.0)
114 >                newa = 0.0;
115 >        ambdiff = fabs(newa - ambacc);
116 >        if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0)
117 >                sortambvals(1);                 /* rebuild tree */
118   }
119  
120  
# Line 141 | Line 142 | char  *afile;
142                          avinsert(avstore(&amb));
143                                                  /* align */
144                  fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
145 +                nambshare = nambvals;
146          } else if ((ambfp = fopen(afile, "w+")) != NULL)
147                  initambfile(1);
148          else {
# Line 174 | Line 176 | OBJECT obj;
176   }
177  
178  
179 < ambient(acol, r)                /* compute ambient component for ray */
179 > ambient(acol, r, nrm)           /* compute ambient component for ray */
180   COLOR  acol;
181   register RAY  *r;
182 + FVECT  nrm;
183   {
184          static int  rdepth = 0;                 /* ambient recursion */
185          double  d;
# Line 195 | Line 198 | register RAY  *r;
198                  rdepth++;
199                  d = doambient(acol, r, r->rweight, NULL, NULL);
200                  rdepth--;
201 <                if (d == 0.0)
201 >                if (d <= FTINY)
202                          goto dumbamb;
203                  return;
204          }
205                                                  /* resort memory? */
206 <        checksort();
206 >        sortambvals(0);
207                                                  /* get ambient value */
208          setcolor(acol, 0.0, 0.0, 0.0);
209 <        d = sumambient(acol, r, rdepth,
209 >        d = sumambient(acol, r, nrm, rdepth,
210                          &atrunk, thescene.cuorg, thescene.cusize);
211 <        if (d > FTINY)
211 >        if (d > FTINY) {
212                  scalecolor(acol, 1.0/d);
213 <        else {
211 <                d = makeambient(acol, r, rdepth++);
212 <                rdepth--;
213 >                return;
214          }
215 +        rdepth++;                               /* need to cache new value */
216 +        d = makeambient(acol, r, nrm, rdepth-1);
217 +        rdepth--;
218          if (d > FTINY)
219                  return;
220   dumbamb:                                        /* return global value */
221          copycolor(acol, ambval);
222 +        if (ambvwt <= 0 | nambvals == 0)
223 +                return;
224 +        scalecolor(acol, (double)ambvwt);
225 +        addcolor(acol, avsum);                  /* average in computations */
226 +        d = 1.0/(ambvwt+nambvals);
227 +        scalecolor(acol, d);
228   }
229  
230  
231   double
232 < sumambient(acol, r, al, at, c0, s)      /* get interpolated ambient value */
232 > sumambient(acol, r, rn, al, at, c0, s)  /* get interpolated ambient value */
233   COLOR  acol;
234   register RAY  *r;
235 + FVECT  rn;
236   int  al;
237   AMBTREE  *at;
238   FVECT  c0;
# Line 233 | Line 244 | double s;
244          int  i;
245          register int  j;
246          register AMBVAL  *av;
247 <                                        /* do this node */
247 >
248          wsum = 0.0;
249 +                                        /* do this node */
250          for (av = at->alist; av != NULL; av = av->next) {
251                  if (tracktime)
252                          av->latick = ambclock++;
# Line 248 | Line 260 | double s;
260                  /*
261                   *  Ambient radius test.
262                   */
263 <                e1 = 0.0;
264 <                for (j = 0; j < 3; j++) {
265 <                        d = av->pos[j] - r->rop[j];
266 <                        e1 += d * d;
267 <                }
263 >                d = av->pos[0] - r->rop[0];
264 >                e1 = d * d;
265 >                d = av->pos[1] - r->rop[1];
266 >                e1 += d * d;
267 >                d = av->pos[2] - r->rop[2];
268 >                e1 += d * d;
269                  e1 /= av->rad * av->rad;
270                  if (e1 > ambacc*ambacc*1.21)
271                          continue;
# Line 276 | Line 289 | double s;
289                   *  Jittering final test reduces image artifacts.
290                   */
291                  wt = sqrt(e1) + sqrt(e2);
292 <                wt *= .9 + .2*urand(9015+samplendx);
280 <                if (wt > ambacc)
292 >                if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
293                          continue;
294                  if (wt <= 1e-3)
295                          wt = 1e3;
296                  else
297                          wt = 1.0 / wt;
298                  wsum += wt;
299 <                extambient(ct, av, r->rop, r->ron);
299 >                extambient(ct, av, r->rop, rn);
300                  scalecolor(ct, wt);
301                  addcolor(acol, ct);
302          }
# Line 303 | Line 315 | double s;
315                                  break;
316                  }
317                  if (j == 3)
318 <                        wsum += sumambient(acol, r, al, at->kid+i, ck0, s);
318 >                        wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s);
319          }
320          return(wsum);
321   }
322  
323  
324   double
325 < makeambient(acol, r, al)        /* make a new ambient value */
325 > makeambient(acol, r, rn, al)    /* make a new ambient value */
326   COLOR  acol;
327   register RAY  *r;
328 + FVECT  rn;
329   int  al;
330   {
331          AMBVAL  amb;
332          FVECT   gp, gd;
333                                                  /* compute weight */
334          amb.weight = pow(AVGREFL, (double)al);
335 <        if (r->rweight < 0.2*amb.weight)        /* heuristic */
335 >        if (r->rweight < 0.1*amb.weight)        /* heuristic */
336                  amb.weight = r->rweight;
337                                                  /* compute ambient */
338          amb.rad = doambient(acol, r, amb.weight, gp, gd);
339 <        if (amb.rad == 0.0)
339 >        if (amb.rad <= FTINY)
340                  return(0.0);
341                                                  /* store it */
342          VCOPY(amb.pos, r->rop);
# Line 334 | Line 347 | int  al;
347          VCOPY(amb.gdir, gd);
348                                                  /* insert into tree */
349          avsave(&amb);                           /* and save to file */
350 +        if (rn != r->ron)
351 +                extambient(acol, &amb, r->rop, rn);     /* texture */
352          return(amb.rad);
353   }
354  
# Line 424 | Line 439 | register AMBVAL  *aval;
439          copystruct(av, aval);
440          av->latick = ambclock;
441          av->next = NULL;
442 +        addcolor(avsum, av->val);       /* add to sum for averaging */
443          nambvals++;
444          return(av);
445   }
# Line 586 | Line 602 | int    always;
602          AMBTREE  oldatrunk;
603          AMBVAL  tav, *tap, *pnext;
604          register int    i, j;
605 +                                        /* see if it's time yet */
606 +        if (!always && (ambclock < lastsort+sortintvl ||
607 +                        nambvals < SORT_THRESH))
608 +                return;
609          /*
610           * The idea here is to minimize memory thrashing
611           * in VM systems by improving reference locality.
# Line 625 | Line 645 | int    always;
645                   * when we're thrashing, which is when we need to do it.
646                   */
647   #ifdef DEBUG
648 <                sprintf(errmsg, "sorting %u ambient values...", nambvals);
648 >                sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...",
649 >                                nambvals, ambclock);
650                  eputs(errmsg);
651   #endif
652                  i_avlist = 0;
# Line 653 | Line 674 | int    always;
674                  }
675                  free((char *)avlist1);
676                  free((char *)avlist2);
677 <                if (sortintvl < SORT_INTVL<<6)
677 >                                                /* compute new sort interval */
678 >                sortintvl = ambclock - lastsort;
679 >                if (sortintvl >= MAX_SORT_INTVL/2)
680 >                        sortintvl = MAX_SORT_INTVL;
681 >                else
682                          sortintvl <<= 1;        /* wait twice as long next */
683   #ifdef DEBUG
684                  eputs("done\n");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines