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.1 by greg, Tue Nov 12 17:09:42 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 192 | Line 243 | double  s;
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 249 | Line 300 | int  al;
300          VCOPY(amb.gpos, gp);
301          VCOPY(amb.gdir, gd);
302                                                  /* insert into tree */
303 <        avinsert(&amb, &atrunk, thescene.cuorg, thescene.cusize);
253 <        avsave(&amb);                           /* write to file */
303 >        avsave(&amb);                           /* and save to file */
304          return(amb.rad);
305   }
306  
# Line 282 | Line 332 | FVECT  pv, nv;
332  
333  
334   static
335 < avsave(av)                              /* save an ambient value */
335 > avsave(av)                              /* insert and save an ambient value */
336   AMBVAL  *av;
337   {
288 #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;
295 #ifdef  AMBFLUSH
345          if (++nunflshed >= AMBFLUSH) {
346 <                if (fflush(ambfp) == EOF)
346 >                if (ambsync() == EOF)
347                          goto writerr;
348                  nunflshed = 0;
349          }
301 #endif
350          return;
351   writerr:
352          error(SYSTEM, "error writing ambient file");
# Line 339 | 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