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.6 by greg, Tue Aug 8 17:31:23 1989 UTC vs.
Revision 2.8 by greg, Thu Jul 16 14:03:27 1992 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1991 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 17 | Line 17 | static char SCCSid[] = "$SunId$ LBL";
17  
18   #include  "octree.h"
19  
20 + #include  "otypes.h"
21 +
22 + #include  "ambient.h"
23 +
24   #include  "random.h"
25  
26   #define  OCTSCALE       0.5     /* ceil((valid rad.)/(cube size)) */
27  
28 < #define  WDONE          4       /* stop if wsum/wmin is at or above */
28 > typedef struct ambtree {
29 >        AMBVAL  *alist;         /* ambient value list */
30 >        struct ambtree  *kid;   /* 8 child nodes */
31 > }  AMBTREE;                     /* ambient octree */
32  
33   extern CUBE  thescene;          /* contains space boundaries */
34  
35 < extern COLOR  ambval;           /* global ambient component */
36 < extern double  ambacc;          /* ambient accuracy */
30 < extern int  ambres;             /* ambient resolution */
31 < extern int  ambdiv;             /* number of divisions for calculation */
32 < extern int  ambssamp;           /* number of super-samples */
33 < extern int  ambounce;           /* number of ambient bounces */
34 < extern char  *amblist[];        /* ambient include/exclude list */
35 < extern int  ambincl;            /* include == 1, exclude == 0 */
35 > #define  MAXASET        511     /* maximum number of elements in ambient set */
36 > OBJECT  ambset[MAXASET+1]={0};  /* ambient include/exclude set */
37  
37 OBJECT  ambset[128];            /* ambient include/exclude set */
38
38   double  maxarad;                /* maximum ambient radius */
39   double  minarad;                /* minimum ambient radius */
40  
42 typedef struct ambval {
43        FVECT  pos;             /* position in space */
44        FVECT  dir;             /* normal direction */
45        int  lvl;               /* recursion level of parent ray */
46        float  weight;          /* weight of parent ray */
47        COLOR  val;             /* computed ambient value */
48        float  rad;             /* validity radius */
49        struct ambval  *next;   /* next in list */
50 }  AMBVAL;                      /* ambient value */
51
52 typedef struct ambtree {
53        AMBVAL  *alist;         /* ambient value list */
54        struct ambtree  *kid;   /* 8 child nodes */
55 }  AMBTREE;                     /* ambient octree */
56
57 typedef struct {
58        float  k;               /* error contribution per sample */
59        COLOR  v;               /* ray sum */
60        int  n;                 /* number of samples */
61        short  t, p;            /* theta, phi indices */
62 }  AMBSAMP;                     /* ambient sample */
63
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 < double  sumambient(), doambient(), makeambient();
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   {
78        long  ftell();
79        char  **amblp;
80        OBJECT  obj;
77          AMBVAL  amb;
78 <                                        /* set up ambient set */
79 <        ambset[0] = 0;
80 <        for (amblp = amblist; *amblp != NULL; amblp++) {
81 <                if ((obj = modifier(*amblp)) == OVOID) {
86 <                        sprintf(errmsg, "unknown %s modifier \"%s\"",
87 <                                ambincl ? "include" : "exclude", *amblp);
88 <                        error(WARNING, errmsg);
89 <                        continue;
90 <                }
91 <                if (!inset(ambset, obj))
92 <                        insertelem(ambset, obj);
93 <        }
94 <        maxarad = thescene.cusize / 2.0;                /* maximum radius */
95 <                                                        /* minimum radius */
96 <        minarad = ambres > 0 ? thescene.cusize/ambres : 0.0;
97 <
98 <                                        /* open ambient file */
99 <        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(&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 111 | 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 + {
130 +        static int  hitlimit = 0;
131 +        register OBJREC  *o = objptr(obj);
132 +        register char  **amblp;
133 +
134 +        if (hitlimit || !ismodifier(o->otype))
135 +                return;
136 +        for (amblp = amblist; *amblp != NULL; amblp++)
137 +                if (!strcmp(o->oname, *amblp)) {
138 +                        if (ambset[0] >= MAXASET) {
139 +                                error(WARNING, "too many modifiers in ambient list");
140 +                                hitlimit++;
141 +                                return;         /* should this be fatal? */
142 +                        }
143 +                        insertelem(ambset, obj);
144 +                        return;
145 +                }
146 + }
147 +
148 +
149   ambient(acol, r)                /* compute ambient component for ray */
150   COLOR  acol;
151   register RAY  *r;
152   {
153          static int  rdepth = 0;                 /* ambient recursion */
154 <        double  wsum;
154 >        double  d;
155  
121        rdepth++;                               /* increment level */
122
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 131 | Line 164 | register RAY  *r;
164                  goto dumbamb;
165  
166          if (ambacc <= FTINY) {                  /* no ambient storage */
167 <                if (doambient(acol, r) == 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);
149 done:                                           /* must finish here! */
150        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 166 | Line 204 | double  s;
204          int  i;
205          register int  j;
206          register AMBVAL  *av;
207 <
207 >                                        /* do this node */
208          wsum = 0.0;
171                                        /* check children first */
172        if (at->kid != NULL) {
173                s *= 0.5;
174                for (i = 0; i < 8; i++) {
175                        for (j = 0; j < 3; j++) {
176                                ck0[j] = c0[j];
177                                if (1<<j & i)
178                                        ck0[j] += s;
179                                if (r->rop[j] < ck0[j] - OCTSCALE*s)
180                                        break;
181                                if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
182                                        break;
183                        }
184                        if (j == 3)
185                                wsum += sumambient(acol, r, at->kid+i, ck0, s);
186                }
187                if (wsum*ambacc >= WDONE)
188                        return(wsum);           /* close enough */
189        }
190                                        /* check this node */
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 219 | 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 < -minarad)
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 *= 0.9 + 0.2*frandom();
246 >                wt *= .9 + .2*urand(9015+samplendx);
247                  if (wt > ambacc)
248                          continue;
249                  if (wt <= 1e-3)
# Line 233 | 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          }
258 +        if (at->kid == NULL)
259 +                return(wsum);
260 +                                        /* do children */
261 +        s *= 0.5;
262 +        for (i = 0; i < 8; i++) {
263 +                for (j = 0; j < 3; j++) {
264 +                        ck0[j] = c0[j];
265 +                        if (1<<j & i)
266 +                                ck0[j] += s;
267 +                        if (r->rop[j] < ck0[j] - OCTSCALE*s)
268 +                                break;
269 +                        if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
270 +                                break;
271 +                }
272 +                if (j == 3)
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 <
287 <        amb.rad = doambient(acol, r);           /* compute ambient */
286 >        FVECT   gp, gd;
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;
258 <        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);
262 <        avsave(&amb);                           /* write to file */
303 >        avsave(&amb);                           /* and save to file */
304          return(amb.rad);
305   }
306  
307  
308 < double
309 < doambient(acol, r)                      /* compute ambient component */
310 < COLOR  acol;
311 < register RAY  *r;
308 > extambient(cr, ap, pv, nv)              /* extrapolate value at pv, nv */
309 > COLOR  cr;
310 > register AMBVAL  *ap;
311 > FVECT  pv, nv;
312   {
313 <        extern int  ambcmp();
314 <        extern double  sin(), cos(), sqrt();
315 <        double  phi, xd, yd, zd;
275 <        double  b, b2;
276 <        register AMBSAMP  *div;
277 <        AMBSAMP  dnew;
278 <        RAY  ar;
279 <        FVECT  ux, uy;
280 <        double  arad;
281 <        int  ndivs, nt, np, ns, ne, i, j;
282 <        register int  k;
313 >        FVECT  v1, v2;
314 >        register int  i;
315 >        double  d;
316  
317 <        setcolor(acol, 0.0, 0.0, 0.0);
318 <                                        /* set number of divisions */
319 <        nt = sqrt(ambdiv * r->rweight * 0.5) + 0.5;
320 <        np = 2 * nt;
321 <        ndivs = nt * np;
322 <                                        /* check first */
323 <        if (ndivs == 0 || rayorigin(&ar, r, AMBIENT, 0.5) < 0)
324 <                return(0.0);
325 <                                        /* set number of super-samples */
326 <        ns = ambssamp * r->rweight + 0.5;
327 <        if (ns > 0) {
295 <                div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP));
296 <                if (div == NULL)
297 <                        error(SYSTEM, "out of memory in doambient");
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 <                                        /* make axes */
330 <        uy[0] = uy[1] = uy[2] = 0.0;
301 <        for (k = 0; k < 3; k++)
302 <                if (r->ron[k] < 0.6 && r->ron[k] > -0.6)
303 <                        break;
304 <        uy[k] = 1.0;
305 <        fcross(ux, r->ron, uy);
306 <        normalize(ux);
307 <        fcross(uy, ux, r->ron);
308 <                                                /* sample divisions */
309 <        arad = 0.0;
310 <        ne = 0;
311 <        for (i = 0; i < nt; i++)
312 <                for (j = 0; j < np; j++) {
313 <                        rayorigin(&ar, r, AMBIENT, 0.5);        /* pretested */
314 <                        zd = sqrt((i+frandom())/nt);
315 <                        phi = 2.0*PI * (j+frandom())/np;
316 <                        xd = cos(phi) * zd;
317 <                        yd = sin(phi) * zd;
318 <                        zd = sqrt(1.0 - zd*zd);
319 <                        for (k = 0; k < 3; k++)
320 <                                ar.rdir[k] = xd*ux[k]+yd*uy[k]+zd*r->ron[k];
321 <                        rayvalue(&ar);
322 <                        if (ar.rot < FHUGE)
323 <                                arad += 1.0 / ar.rot;
324 <                        if (ns > 0) {                   /* save division */
325 <                                div[ne].k = 0.0;
326 <                                copycolor(div[ne].v, ar.rcol);
327 <                                div[ne].n = 0;
328 <                                div[ne].t = i; div[ne].p = j;
329 <                                                        /* sum errors */
330 <                                b = bright(ar.rcol);
331 <                                if (i > 0) {            /* from above */
332 <                                        b2 = bright(div[ne-np].v) - b;
333 <                                        b2 *= b2 * 0.25;
334 <                                        div[ne].k += b2;
335 <                                        div[ne].n++;
336 <                                        div[ne-np].k += b2;
337 <                                        div[ne-np].n++;
338 <                                }
339 <                                if (j > 0) {            /* from behind */
340 <                                        b2 = bright(div[ne-1].v) - b;
341 <                                        b2 *= b2 * 0.25;
342 <                                        div[ne].k += b2;
343 <                                        div[ne].n++;
344 <                                        div[ne-1].k += b2;
345 <                                        div[ne-1].n++;
346 <                                }
347 <                                if (j == np-1) {        /* around */
348 <                                        b2 = bright(div[ne-(np-1)].v) - b;
349 <                                        b2 *= b2 * 0.25;
350 <                                        div[ne].k += b2;
351 <                                        div[ne].n++;
352 <                                        div[ne-(np-1)].k += b2;
353 <                                        div[ne-(np-1)].n++;
354 <                                }
355 <                                ne++;
356 <                        } else
357 <                                addcolor(acol, ar.rcol);
358 <                }
359 <        for (k = 0; k < ne; k++) {              /* compute errors */
360 <                if (div[k].n > 1)
361 <                        div[k].k /= div[k].n;
362 <                div[k].n = 1;
363 <        }
364 <                                                /* sort the divisions */
365 <        qsort(div, ne, sizeof(AMBSAMP), ambcmp);
366 <                                                /* skim excess */
367 <        while (ne > ns) {
368 <                ne--;
369 <                addcolor(acol, div[ne].v);
370 <        }
371 <                                                /* super-sample */
372 <        for (i = ns; i > 0; i--) {
373 <                rayorigin(&ar, r, AMBIENT, 0.5);        /* pretested */
374 <                zd = sqrt((div[0].t+frandom())/nt);
375 <                phi = 2.0*PI * (div[0].p+frandom())/np;
376 <                xd = cos(phi) * zd;
377 <                yd = sin(phi) * zd;
378 <                zd = sqrt(1.0 - zd*zd);
379 <                for (k = 0; k < 3; k++)
380 <                        ar.rdir[k] = xd*ux[k]+yd*uy[k]+zd*r->ron[k];
381 <                rayvalue(&ar);
382 <                if (ar.rot < FHUGE)
383 <                        arad += 1.0 / ar.rot;
384 <                                                /* recompute error */
385 <                copycolor(dnew.v, div[0].v);
386 <                addcolor(dnew.v, ar.rcol);
387 <                dnew.n = div[0].n + 1;
388 <                dnew.t = div[0].t; dnew.p = div[0].p;
389 <                b2 = bright(dnew.v)/dnew.n - bright(ar.rcol);
390 <                b2 = b2*b2 + div[0].k*(div[0].n*div[0].n);
391 <                dnew.k = b2/(dnew.n*dnew.n);
392 <                                                /* reinsert */
393 <                for (k = 0; k < ne-1 && dnew.k < div[k+1].k; k++)
394 <                        bcopy(&div[k+1], &div[k], sizeof(AMBSAMP));
395 <                bcopy(&dnew, &div[k], sizeof(AMBSAMP));
396 <
397 <                if (ne >= i) {          /* extract darkest division */
398 <                        ne--;
399 <                        if (div[ne].n > 1)
400 <                                scalecolor(div[ne].v, 1.0/div[ne].n);
401 <                        addcolor(acol, div[ne].v);
402 <                }
403 <        }
404 <        scalecolor(acol, 1.0/ndivs);
405 <        if (arad <= FTINY)
406 <                arad = FHUGE;
407 <        else
408 <                arad = (ndivs+ns) / arad / sqrt(r->rweight);
409 <        if (arad > maxarad)
410 <                arad = maxarad;
411 <        else if (arad < minarad)
412 <                arad = minarad;
413 <        if (ns > 0)
414 <                free((char *)div);
415 <        return(arad);
329 >        copycolor(cr, ap->val);
330 >        scalecolor(cr, d);
331   }
332  
333  
419 static int
420 ambcmp(d1, d2)                          /* decreasing order */
421 AMBSAMP  *d1, *d2;
422 {
423        if (d1->k < d2->k)
424                return(1);
425        if (d1->k > d2->k)
426                return(-1);
427        return(0);
428 }
429
430
334   static
335 < avsave(av)                              /* save an ambient value */
335 > avsave(av)                              /* insert and save an ambient value */
336   AMBVAL  *av;
337   {
435 #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(av, sizeof(AMBVAL), 1, ambfp) != 1)
343 >        if (writambval(av, ambfp) < 0)
344                  goto writerr;
442 #ifdef  AMBFLUSH
345          if (++nunflshed >= AMBFLUSH) {
346 <                if (fflush(ambfp) == EOF)
346 >                if (ambsync() == EOF)
347                          goto writerr;
348                  nunflshed = 0;
349          }
448 #endif
350          return;
351   writerr:
352          error(SYSTEM, "error writing ambient file");
# Line 466 | Line 367 | double  s;
367  
368          if ((av = newambval()) == NULL)
369                  goto memerr;
370 <        bcopy(aval, av, sizeof(AMBVAL));
370 >        copystruct(av, aval);
371          VCOPY(ck0, c0);
372          while (s*(OCTSCALE/2) > av->rad*ambacc) {
373                  if (at->kid == NULL)
# Line 486 | 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