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 1.14 by greg, Fri Jun 7 10:04:29 1991 UTC vs.
Revision 2.8 by greg, Thu Jul 16 14:03:27 1992 UTC

# Line 41 | Line 41 | double  minarad;               /* minimum ambient radius */
41   static AMBTREE  atrunk;         /* our ambient trunk node */
42  
43   static FILE  *ambfp = NULL;     /* ambient file pointer */
44 + static char  *afname;           /* ambient file name */
45 + static long  ambheadlen;        /* length of ambient file header */
46  
47 + #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
48 +
49   #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
50  
51   #define  newambtree()   (AMBTREE *)calloc(8, sizeof(AMBTREE))
52  
53 + extern long  ftell(), lseek();
54  
55 +
56 + setambres(ar)                           /* set ambient resolution */
57 + int  ar;
58 + {
59 +                                                /* set min & max radii */
60 +        if (ar <= 0) {
61 +                minarad = 0.0;
62 +                maxarad = thescene.cusize / 2.0;
63 +        } else {
64 +                minarad = thescene.cusize / ar;
65 +                maxarad = 16.0 * minarad;               /* heuristic */
66 +                if (maxarad > thescene.cusize / 2.0)
67 +                        maxarad = thescene.cusize / 2.0;
68 +        }
69 +        if (maxarad <= FTINY)
70 +                maxarad = .001;
71 + }
72 +
73 +
74   setambient(afile)                       /* initialize calculation */
75   char  *afile;
76   {
53        long  ftell();
77          AMBVAL  amb;
78 <
79 <        maxarad = thescene.cusize / 2.0;                /* maximum radius */
80 <                                                        /* minimum radius */
81 <        minarad = ambres > 0 ? thescene.cusize/ambres : 0.0;
59 <
60 <                                        /* open ambient file */
61 <        if (afile != NULL)
78 >                                                /* init ambient limits */
79 >        setambres(ambres);
80 >                                                /* open ambient file */
81 >        if ((afname = afile) != NULL)
82                  if ((ambfp = fopen(afile, "r+")) != NULL) {
83 <                        while (fread((char *)&amb,sizeof(AMBVAL),1,ambfp) == 1)
83 >                        initambfile(0);
84 >                        while (readambval(&amb, ambfp))
85                                  avinsert(&amb, &atrunk, thescene.cuorg,
86                                                  thescene.cusize);
87                                                          /* align */
88 <                        fseek(ambfp, -(ftell(ambfp)%sizeof(AMBVAL)), 1);
89 <                } else if ((ambfp = fopen(afile, "w")) == NULL) {
88 >                        fseek(ambfp, -((ftell(ambfp)-ambheadlen)%AMBVALSIZ), 1);
89 >                } else if ((ambfp = fopen(afile, "w")) != NULL)
90 >                        initambfile(1);
91 >                else {
92                          sprintf(errmsg, "cannot open ambient file \"%s\"",
93                                          afile);
94                          error(SYSTEM, errmsg);
# Line 73 | Line 96 | char  *afile;
96   }
97  
98  
99 + initambfile(creat)              /* initialize ambient file */
100 + int  creat;
101 + {
102 +        extern char  *progname, *octname, VersionID[];
103 +
104 +        setbuf(ambfp, bmalloc(BUFSIZ));
105 +        if (creat) {                    /* new file */
106 +                fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
107 +                                progname, colval(ambval,RED),
108 +                                colval(ambval,GRN), colval(ambval,BLU),
109 +                                ambounce, ambacc);
110 +                fprintf(ambfp, "-ad %d -as %d -ar %d %s\n",
111 +                                ambdiv, ambssamp, ambres,
112 +                                octname==NULL ? "" : octname);
113 +                fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
114 +                fputformat(AMBFMT, ambfp);
115 +                putc('\n', ambfp);
116 +                putambmagic(ambfp);
117 +                fflush(ambfp);
118 +        } else if (checkheader(ambfp, AMBFMT, NULL) < 0
119 +                        || !hasambmagic(ambfp)) {
120 +                sprintf(errmsg, "\"%s\" is not an ambient file", afname);
121 +                error(USER, errmsg);
122 +        }
123 +        ambheadlen = ftell(ambfp);
124 + }
125 +
126 +
127   ambnotify(obj)                  /* record new modifier */
128   OBJECT  obj;
129   {
# Line 100 | Line 151 | COLOR  acol;
151   register RAY  *r;
152   {
153          static int  rdepth = 0;                 /* ambient recursion */
154 <        double  wsum;
154 >        double  d;
155  
105        rdepth++;                               /* increment level */
106
156          if (ambdiv <= 0)                        /* no ambient calculation */
157                  goto dumbamb;
158                                                  /* check number of bounces */
159 <        if (rdepth > ambounce)
159 >        if (rdepth >= ambounce)
160                  goto dumbamb;
161                                                  /* check ambient list */
162          if (ambincl != -1 && r->ro != NULL &&
# Line 115 | Line 164 | register RAY  *r;
164                  goto dumbamb;
165  
166          if (ambacc <= FTINY) {                  /* no ambient storage */
167 <                if (doambient(acol, r, NULL, NULL) == 0.0)
167 >                rdepth++;
168 >                d = doambient(acol, r, r->rweight, NULL, NULL);
169 >                rdepth--;
170 >                if (d == 0.0)
171                          goto dumbamb;
172 <                goto done;
172 >                return;
173          }
174                                                  /* get ambient value */
175          setcolor(acol, 0.0, 0.0, 0.0);
176 <        wsum = sumambient(acol, r, &atrunk, thescene.cuorg, thescene.cusize);
177 <        if (wsum > FTINY)
178 <                scalecolor(acol, 1.0/wsum);
179 <        else if (makeambient(acol, r) == 0.0)
180 <                goto dumbamb;
181 <        goto done;
182 <
176 >        d = sumambient(acol, r, rdepth,
177 >                        &atrunk, thescene.cuorg, thescene.cusize);
178 >        if (d > FTINY)
179 >                scalecolor(acol, 1.0/d);
180 >        else {
181 >                d = makeambient(acol, r, rdepth++);
182 >                rdepth--;
183 >        }
184 >        if (d > FTINY)
185 >                return;
186   dumbamb:                                        /* return global value */
187          copycolor(acol, ambval);
133 done:                                           /* must finish here! */
134        rdepth--;
188   }
189  
190  
191   double
192 < sumambient(acol, r, at, c0, s)          /* get interpolated ambient value */
192 > sumambient(acol, r, al, at, c0, s)      /* get interpolated ambient value */
193   COLOR  acol;
194   register RAY  *r;
195 + int  al;
196   AMBTREE  *at;
197   FVECT  c0;
198   double  s;
# Line 154 | Line 208 | double  s;
208          wsum = 0.0;
209          for (av = at->alist; av != NULL; av = av->next) {
210                  /*
211 <                 *  Ray strength test.
211 >                 *  Ambient level test.
212                   */
213 <                if (av->lvl > r->rlvl || av->weight < r->rweight-FTINY)
213 >                if (av->lvl > al || av->weight < r->rweight-FTINY)
214                          continue;
215                  /*
216                   *  Ambient radius test.
# Line 183 | Line 237 | double  s;
237                  for (j = 0; j < 3; j++)
238                          d += (r->rop[j] - av->pos[j]) *
239                                          (av->dir[j] + r->ron[j]);
240 <                if (d*0.5 < -minarad*ambacc)
240 >                if (d*0.5 < -minarad*ambacc-.001)
241                          continue;
242                  /*
243                   *  Jittering final test reduces image artifacts.
244                   */
245                  wt = sqrt(e1) + sqrt(e2);
246 <                wt *= .9 + .2*frandom();
246 >                wt *= .9 + .2*urand(9015+samplendx);
247                  if (wt > ambacc)
248                          continue;
249                  if (wt <= 1e-3)
# Line 197 | Line 251 | double  s;
251                  else
252                          wt = 1.0 / wt;
253                  wsum += wt;
254 <                copycolor(ct, av->val);
254 >                extambient(ct, av, r->rop, r->ron);
255                  scalecolor(ct, wt);
256                  addcolor(acol, ct);
257          }
# Line 216 | Line 270 | double  s;
270                                  break;
271                  }
272                  if (j == 3)
273 <                        wsum += sumambient(acol, r, at->kid+i, ck0, s);
273 >                        wsum += sumambient(acol, r, al, at->kid+i, ck0, s);
274          }
275          return(wsum);
276   }
277  
278  
279   double
280 < makeambient(acol, r)            /* make a new ambient value */
280 > makeambient(acol, r, al)        /* make a new ambient value */
281   COLOR  acol;
282   register RAY  *r;
283 + int  al;
284   {
285          AMBVAL  amb;
286          FVECT   gp, gd;
287 <
288 <        amb.rad = doambient(acol, r, gp, gd);   /* compute ambient */
287 >                                                /* compute weight */
288 >        amb.weight = pow(AVGREFL, (double)al);
289 >        if (r->rweight < 0.2*amb.weight)        /* heuristic */
290 >                amb.weight = r->rweight;
291 >                                                /* compute ambient */
292 >        amb.rad = doambient(acol, r, amb.weight, gp, gd);
293          if (amb.rad == 0.0)
294                  return(0.0);
295                                                  /* store it */
296          VCOPY(amb.pos, r->rop);
297          VCOPY(amb.dir, r->ron);
298 <        amb.lvl = r->rlvl;
240 <        amb.weight = r->rweight;
298 >        amb.lvl = al;
299          copycolor(amb.val, acol);
300          VCOPY(amb.gpos, gp);
301          VCOPY(amb.gdir, gd);
302                                                  /* insert into tree */
303 <        avinsert(&amb, &atrunk, thescene.cuorg, thescene.cusize);
246 <        avsave(&amb);                           /* write to file */
303 >        avsave(&amb);                           /* and save to file */
304          return(amb.rad);
305   }
306  
307  
308 + extambient(cr, ap, pv, nv)              /* extrapolate value at pv, nv */
309 + COLOR  cr;
310 + register AMBVAL  *ap;
311 + FVECT  pv, nv;
312 + {
313 +        FVECT  v1, v2;
314 +        register int  i;
315 +        double  d;
316 +
317 +        d = 1.0;                        /* zeroeth order */
318 +                                        /* gradient due to translation */
319 +        for (i = 0; i < 3; i++)
320 +                d += ap->gpos[i]*(pv[i]-ap->pos[i]);
321 +                                        /* gradient due to rotation */
322 +        VCOPY(v1, ap->dir);
323 +        fcross(v2, v1, nv);
324 +        d += DOT(ap->gdir, v2);
325 +        if (d <= 0.0) {
326 +                setcolor(cr, 0.0, 0.0, 0.0);
327 +                return;
328 +        }
329 +        copycolor(cr, ap->val);
330 +        scalecolor(cr, d);
331 + }
332 +
333 +
334   static
335 < avsave(av)                              /* save an ambient value */
335 > avsave(av)                              /* insert and save an ambient value */
336   AMBVAL  *av;
337   {
255 #ifdef  AMBFLUSH
338          static int  nunflshed = 0;
339 < #endif
339 >
340 >        avinsert(av, &atrunk, thescene.cuorg, thescene.cusize);
341          if (ambfp == NULL)
342                  return;
343 <        if (fwrite((char *)av, sizeof(AMBVAL), 1, ambfp) != 1)
343 >        if (writambval(av, ambfp) < 0)
344                  goto writerr;
262 #ifdef  AMBFLUSH
345          if (++nunflshed >= AMBFLUSH) {
346 <                if (fflush(ambfp) == EOF)
346 >                if (ambsync() == EOF)
347                          goto writerr;
348                  nunflshed = 0;
349          }
268 #endif
350          return;
351   writerr:
352          error(SYSTEM, "error writing ambient file");
# Line 306 | Line 387 | double  s;
387          return;
388   memerr:
389          error(SYSTEM, "out of memory in avinsert");
390 + }
391 +
392 +
393 + #include  <fcntl.h>
394 +
395 +
396 + static
397 + ambsync()                       /* synchronize ambient file */
398 + {
399 +        static FILE  *ambinp = NULL;
400 +        struct flock  fls;
401 +        AMBVAL  avs;
402 +        long  lastpos, flen;
403 +        register int  n;
404 +                                /* gain exclusive access */
405 +        fls.l_type = F_WRLCK;
406 +        fls.l_whence = 0;
407 +        fls.l_start = 0L;
408 +        fls.l_len = 0L;
409 +        if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
410 +                error(SYSTEM, "cannot lock ambient file");
411 +                                /* see if file has grown */
412 +        lastpos = lseek(fileno(ambfp), 0L, 2);  /* may move pointer */
413 +        flen = lseek(fileno(ambfp), 0L, 1);     /* new(?) file length */
414 +        if (n = (flen - lastpos)%AMBVALSIZ)     /* assure alignment */
415 +                lseek(fileno(ambfp), flen -= n, 0);
416 +        if (n = (flen - lastpos)/AMBVALSIZ) {   /* file has grown */
417 +                if (ambinp == NULL && (ambinp = fopen(afname, "r")) == NULL)
418 +                        error(SYSTEM, "cannot reopen ambient file");
419 +                fseek(ambinp, lastpos, 0);      /* go to previous position */
420 +                while (n--) {                   /* load contributed values */
421 +                        readambval(&avs, ambinp);
422 +                        avinsert(&avs,&atrunk,thescene.cuorg,thescene.cusize);
423 +                }
424 +        }
425 +        n = fflush(ambfp);                      /* calls write() at last */
426 +        fls.l_type = F_UNLCK;                   /* release file */
427 +        fcntl(fileno(ambfp), F_SETLKW, &fls);
428 +        return(n);
429   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines