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.8 by greg, Fri Jan 12 11:30:59 1990 UTC vs.
Revision 2.18 by greg, Fri Jan 29 12:02:23 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1993 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 6 | Line 6 | static char SCCSid[] = "$SunId$ LBL";
6  
7   /*
8   *  ambient.c - routines dealing with ambient (inter-reflected) component.
9 *
10 *  The macro AMBFLUSH (if defined) is the number of ambient values
11 *      to wait before flushing to the ambient file.
12 *
13 *     5/9/86
9   */
10  
11   #include  "ray.h"
# Line 19 | Line 14 | static char SCCSid[] = "$SunId$ LBL";
14  
15   #include  "otypes.h"
16  
17 + #include  "ambient.h"
18 +
19   #include  "random.h"
20  
21 < #define  OCTSCALE       0.5     /* ceil((valid rad.)/(cube size)) */
21 > #define  OCTSCALE       0.5     /* ceil((valid rad.)/(cube size)) */
22  
23 + typedef struct ambtree {
24 +        AMBVAL  *alist;         /* ambient value list */
25 +        struct ambtree  *kid;   /* 8 child nodes */
26 + }  AMBTREE;                     /* ambient octree */
27 +
28   extern CUBE  thescene;          /* contains space boundaries */
29  
30 < extern COLOR  ambval;           /* global ambient component */
31 < 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 */
30 > #define  MAXASET        511     /* maximum number of elements in ambient set */
31 > OBJECT  ambset[MAXASET+1]={0};  /* ambient include/exclude set */
32  
33 < OBJECT  ambset[256]={0};        /* ambient include/exclude set */
33 > double  maxarad;                /* maximum ambient radius */
34 > double  minarad;                /* minimum ambient radius */
35  
36 < double  maxarad;                /* maximum ambient radius */
40 < double  minarad;                /* minimum ambient radius */
36 > static AMBTREE  atrunk;         /* our ambient trunk node */
37  
38 < typedef struct ambval {
39 <        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 */
38 > static FILE  *ambfp = NULL;     /* ambient file pointer */
39 > static int  nunflshed = 0;      /* number of unflushed ambient values */
40  
41 < typedef struct ambtree {
53 <        AMBVAL  *alist;         /* ambient value list */
54 <        struct ambtree  *kid;   /* 8 child nodes */
55 < }  AMBTREE;                     /* ambient octree */
41 > #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
42  
43 < 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 */
43 > #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
44  
45 < static AMBTREE  atrunk;         /* our ambient trunk node */
45 > #define  newambtree()   (AMBTREE *)calloc(8, sizeof(AMBTREE))
46  
47 < static FILE  *ambfp = NULL;     /* ambient file pointer */
47 > extern long  ftell(), lseek();
48 > static int  initambfile(), avsave(), avinsert();
49  
68 #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
50  
51 < #define  newambtree()   (AMBTREE *)calloc(8, sizeof(AMBTREE))
51 > setambres(ar)                           /* set ambient resolution */
52 > int  ar;
53 > {
54 >                                                /* set min & max radii */
55 >        if (ar <= 0) {
56 >                minarad = 0.0;
57 >                maxarad = thescene.cusize / 2.0;
58 >        } else {
59 >                minarad = thescene.cusize / ar;
60 >                maxarad = 16.0 * minarad;               /* heuristic */
61 >                if (maxarad > thescene.cusize / 2.0)
62 >                        maxarad = thescene.cusize / 2.0;
63 >        }
64 >        if (maxarad <= FTINY)
65 >                maxarad = .001;
66 > }
67  
72 double  sumambient(), doambient(), makeambient();
68  
74
69   setambient(afile)                       /* initialize calculation */
70   char  *afile;
71   {
72 <        long  ftell();
73 <        OBJECT  obj;
74 <        AMBVAL  amb;
75 <
76 <        maxarad = thescene.cusize / 2.0;                /* maximum radius */
77 <                                                        /* minimum radius */
84 <        minarad = ambres > 0 ? thescene.cusize/ambres : 0.0;
85 <
86 <                                        /* open ambient file */
87 <        if (afile != NULL)
72 >        long  headlen;
73 >        AMBVAL  amb;
74 >                                                /* init ambient limits */
75 >        setambres(ambres);
76 >                                                /* open ambient file */
77 >        if (afile != NULL) {
78                  if ((ambfp = fopen(afile, "r+")) != NULL) {
79 <                        while (fread(&amb, sizeof(AMBVAL), 1, ambfp) == 1)
79 >                        initambfile(0);
80 >                        headlen = ftell(ambfp);
81 >                        while (readambval(&amb, ambfp))
82                                  avinsert(&amb, &atrunk, thescene.cuorg,
83                                                  thescene.cusize);
84                                                          /* align */
85 <                        fseek(ambfp, -(ftell(ambfp)%sizeof(AMBVAL)), 1);
86 <                } else if ((ambfp = fopen(afile, "w")) == NULL) {
85 >                        fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
86 >                } else if ((ambfp = fopen(afile, "w+")) != NULL)
87 >                        initambfile(1);
88 >                else {
89                          sprintf(errmsg, "cannot open ambient file \"%s\"",
90                                          afile);
91                          error(SYSTEM, errmsg);
92                  }
93 +                nunflshed++;    /* lie */
94 +                ambsync();
95 +        }
96   }
97  
98  
99   ambnotify(obj)                  /* record new modifier */
100 < OBJECT  obj;
100 > OBJECT  obj;
101   {
102 <        register OBJREC  *o = objptr(obj);
102 >        static int  hitlimit = 0;
103 >        register OBJREC  *o = objptr(obj);
104          register char  **amblp;
105  
106 <        if (!ismodifier(o->otype))
106 >        if (hitlimit || !ismodifier(o->otype))
107                  return;
108          for (amblp = amblist; *amblp != NULL; amblp++)
109                  if (!strcmp(o->oname, *amblp)) {
110 +                        if (ambset[0] >= MAXASET) {
111 +                                error(WARNING, "too many modifiers in ambient list");
112 +                                hitlimit++;
113 +                                return;         /* should this be fatal? */
114 +                        }
115                          insertelem(ambset, obj);
116                          return;
117                  }
# Line 120 | Line 123 | COLOR  acol;
123   register RAY  *r;
124   {
125          static int  rdepth = 0;                 /* ambient recursion */
126 <        double  wsum;
126 >        double  d;
127  
125        rdepth++;                               /* increment level */
126
128          if (ambdiv <= 0)                        /* no ambient calculation */
129                  goto dumbamb;
130                                                  /* check number of bounces */
131 <        if (rdepth > ambounce)
131 >        if (rdepth >= ambounce)
132                  goto dumbamb;
133                                                  /* check ambient list */
134          if (ambincl != -1 && r->ro != NULL &&
# Line 135 | Line 136 | register RAY  *r;
136                  goto dumbamb;
137  
138          if (ambacc <= FTINY) {                  /* no ambient storage */
139 <                if (doambient(acol, r) == 0.0)
139 >                rdepth++;
140 >                d = doambient(acol, r, r->rweight, NULL, NULL);
141 >                rdepth--;
142 >                if (d == 0.0)
143                          goto dumbamb;
144 <                goto done;
144 >                return;
145          }
146                                                  /* get ambient value */
147          setcolor(acol, 0.0, 0.0, 0.0);
148 <        wsum = sumambient(acol, r, &atrunk, thescene.cuorg, thescene.cusize);
149 <        if (wsum > FTINY)
150 <                scalecolor(acol, 1.0/wsum);
151 <        else if (makeambient(acol, r) == 0.0)
152 <                goto dumbamb;
153 <        goto done;
154 <
148 >        d = sumambient(acol, r, rdepth,
149 >                        &atrunk, thescene.cuorg, thescene.cusize);
150 >        if (d > FTINY)
151 >                scalecolor(acol, 1.0/d);
152 >        else {
153 >                d = makeambient(acol, r, rdepth++);
154 >                rdepth--;
155 >        }
156 >        if (d > FTINY)
157 >                return;
158   dumbamb:                                        /* return global value */
159          copycolor(acol, ambval);
153 done:                                           /* must finish here! */
154        rdepth--;
160   }
161  
162  
163   double
164 < sumambient(acol, r, at, c0, s)          /* get interpolated ambient value */
164 > sumambient(acol, r, al, at, c0, s)      /* get interpolated ambient value */
165   COLOR  acol;
166   register RAY  *r;
167 < AMBTREE  *at;
167 > int  al;
168 > AMBTREE  *at;
169   FVECT  c0;
170 < double  s;
170 > double  s;
171   {
172 <        extern double  sqrt();
167 <        double  d, e1, e2, wt, wsum;
172 >        double  d, e1, e2, wt, wsum;
173          COLOR  ct;
174          FVECT  ck0;
175          int  i;
176          register int  j;
177 <        register AMBVAL  *av;
177 >        register AMBVAL  *av;
178                                          /* do this node */
179          wsum = 0.0;
180          for (av = at->alist; av != NULL; av = av->next) {
181                  /*
182 <                 *  Ray strength test.
182 >                 *  Ambient level test.
183                   */
184 <                if (av->lvl > r->rlvl || av->weight < r->rweight-FTINY)
184 >                if (av->lvl > al || av->weight < r->rweight-FTINY)
185                          continue;
186                  /*
187                   *  Ambient radius test.
# Line 203 | Line 208 | double  s;
208                  for (j = 0; j < 3; j++)
209                          d += (r->rop[j] - av->pos[j]) *
210                                          (av->dir[j] + r->ron[j]);
211 <                if (d < -minarad)
211 >                if (d*0.5 < -minarad*ambacc-.001)
212                          continue;
213                  /*
214                   *  Jittering final test reduces image artifacts.
215                   */
216                  wt = sqrt(e1) + sqrt(e2);
217 <                wt *= .9 + .2*frandom();
217 >                wt *= .9 + .2*urand(9015+samplendx);
218                  if (wt > ambacc)
219                          continue;
220                  if (wt <= 1e-3)
# Line 217 | Line 222 | double  s;
222                  else
223                          wt = 1.0 / wt;
224                  wsum += wt;
225 <                copycolor(ct, av->val);
225 >                extambient(ct, av, r->rop, r->ron);
226                  scalecolor(ct, wt);
227                  addcolor(acol, ct);
228          }
# Line 236 | Line 241 | double  s;
241                                  break;
242                  }
243                  if (j == 3)
244 <                        wsum += sumambient(acol, r, at->kid+i, ck0, s);
244 >                        wsum += sumambient(acol, r, al, at->kid+i, ck0, s);
245          }
246          return(wsum);
247   }
248  
249  
250   double
251 < makeambient(acol, r)            /* make a new ambient value */
251 > makeambient(acol, r, al)        /* make a new ambient value */
252   COLOR  acol;
253   register RAY  *r;
254 + int  al;
255   {
256 <        AMBVAL  amb;
257 <
258 <        amb.rad = doambient(acol, r);           /* compute ambient */
256 >        AMBVAL  amb;
257 >        FVECT   gp, gd;
258 >                                                /* compute weight */
259 >        amb.weight = pow(AVGREFL, (double)al);
260 >        if (r->rweight < 0.2*amb.weight)        /* heuristic */
261 >                amb.weight = r->rweight;
262 >                                                /* compute ambient */
263 >        amb.rad = doambient(acol, r, amb.weight, gp, gd);
264          if (amb.rad == 0.0)
265                  return(0.0);
266                                                  /* store it */
267          VCOPY(amb.pos, r->rop);
268          VCOPY(amb.dir, r->ron);
269 <        amb.lvl = r->rlvl;
259 <        amb.weight = r->rweight;
269 >        amb.lvl = al;
270          copycolor(amb.val, acol);
271 +        VCOPY(amb.gpos, gp);
272 +        VCOPY(amb.gdir, gd);
273                                                  /* insert into tree */
274 <        avinsert(&amb, &atrunk, thescene.cuorg, thescene.cusize);
263 <        avsave(&amb);                           /* write to file */
274 >        avsave(&amb);                           /* and save to file */
275          return(amb.rad);
276   }
277  
278  
279 < double
280 < doambient(acol, r)                      /* compute ambient component */
281 < COLOR  acol;
282 < register RAY  *r;
279 > extambient(cr, ap, pv, nv)              /* extrapolate value at pv, nv */
280 > COLOR  cr;
281 > register AMBVAL  *ap;
282 > FVECT  pv, nv;
283   {
284 <        extern int  ambcmp();
285 <        extern double  sin(), cos(), sqrt();
286 <        double  phi, xd, yd, zd;
276 <        double  b, b2;
277 <        register AMBSAMP  *div;
278 <        AMBSAMP  dnew;
279 <        RAY  ar;
280 <        FVECT  ux, uy;
281 <        double  arad;
282 <        int  ndivs, nt, np, ns, ne, i, j;
283 <        register int  k;
284 >        FVECT  v1, v2;
285 >        register int  i;
286 >        double  d;
287  
288 <        setcolor(acol, 0.0, 0.0, 0.0);
289 <                                        /* set number of divisions */
290 <        nt = sqrt(ambdiv * r->rweight * 0.5) + 0.5;
291 <        np = 2 * nt;
292 <        ndivs = nt * np;
293 <                                        /* check first */
294 <        if (ndivs == 0 || rayorigin(&ar, r, AMBIENT, 0.5) < 0)
295 <                return(0.0);
296 <                                        /* set number of super-samples */
297 <        ns = ambssamp * r->rweight + 0.5;
298 <        if (ns > 0) {
296 <                div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP));
297 <                if (div == NULL)
298 <                        error(SYSTEM, "out of memory in doambient");
288 >        d = 1.0;                        /* zeroeth order */
289 >                                        /* gradient due to translation */
290 >        for (i = 0; i < 3; i++)
291 >                d += ap->gpos[i]*(pv[i]-ap->pos[i]);
292 >                                        /* gradient due to rotation */
293 >        VCOPY(v1, ap->dir);
294 >        fcross(v2, v1, nv);
295 >        d += DOT(ap->gdir, v2);
296 >        if (d <= 0.0) {
297 >                setcolor(cr, 0.0, 0.0, 0.0);
298 >                return;
299          }
300 <                                        /* make axes */
301 <        uy[0] = uy[1] = uy[2] = 0.0;
302 <        for (k = 0; k < 3; k++)
303 <                if (r->ron[k] < 0.6 && r->ron[k] > -0.6)
304 <                        break;
305 <        uy[k] = 1.0;
306 <        fcross(ux, r->ron, uy);
307 <        normalize(ux);
308 <        fcross(uy, ux, r->ron);
309 <                                                /* sample divisions */
310 <        arad = 0.0;
311 <        ne = 0;
312 <        for (i = 0; i < nt; i++)
313 <                for (j = 0; j < np; j++) {
314 <                        rayorigin(&ar, r, AMBIENT, 0.5);        /* pretested */
315 <                        zd = sqrt((i+frandom())/nt);
316 <                        phi = 2.0*PI * (j+frandom())/np;
317 <                        xd = cos(phi) * zd;
318 <                        yd = sin(phi) * zd;
319 <                        zd = sqrt(1.0 - zd*zd);
320 <                        for (k = 0; k < 3; k++)
321 <                                ar.rdir[k] = xd*ux[k]+yd*uy[k]+zd*r->ron[k];
322 <                        rayvalue(&ar);
323 <                        if (ar.rot < FHUGE)
324 <                                arad += 1.0 / ar.rot;
325 <                        if (ns > 0) {                   /* save division */
326 <                                div[ne].k = 0.0;
327 <                                copycolor(div[ne].v, ar.rcol);
328 <                                div[ne].n = 0;
329 <                                div[ne].t = i; div[ne].p = j;
330 <                                                        /* sum errors */
331 <                                b = bright(ar.rcol);
332 <                                if (i > 0) {            /* from above */
333 <                                        b2 = bright(div[ne-np].v) - b;
334 <                                        b2 *= b2 * 0.25;
335 <                                        div[ne].k += b2;
336 <                                        div[ne].n++;
337 <                                        div[ne-np].k += b2;
338 <                                        div[ne-np].n++;
339 <                                }
340 <                                if (j > 0) {            /* from behind */
341 <                                        b2 = bright(div[ne-1].v) - b;
342 <                                        b2 *= b2 * 0.25;
343 <                                        div[ne].k += b2;
344 <                                        div[ne].n++;
345 <                                        div[ne-1].k += b2;
346 <                                        div[ne-1].n++;
347 <                                }
348 <                                if (j == np-1) {        /* around */
349 <                                        b2 = bright(div[ne-(np-1)].v) - b;
350 <                                        b2 *= b2 * 0.25;
351 <                                        div[ne].k += b2;
352 <                                        div[ne].n++;
353 <                                        div[ne-(np-1)].k += b2;
354 <                                        div[ne-(np-1)].n++;
355 <                                }
356 <                                ne++;
357 <                        } else
358 <                                addcolor(acol, ar.rcol);
359 <                }
360 <        for (k = 0; k < ne; k++) {              /* compute errors */
361 <                if (div[k].n > 1)
362 <                        div[k].k /= div[k].n;
363 <                div[k].n = 1;
364 <        }
365 <                                                /* sort the divisions */
366 <        qsort(div, ne, sizeof(AMBSAMP), ambcmp);
367 <                                                /* skim excess */
368 <        while (ne > ns) {
369 <                ne--;
370 <                addcolor(acol, div[ne].v);
371 <        }
372 <                                                /* super-sample */
373 <        for (i = ns; i > 0; i--) {
374 <                rayorigin(&ar, r, AMBIENT, 0.5);        /* pretested */
375 <                zd = sqrt((div[0].t+frandom())/nt);
376 <                phi = 2.0*PI * (div[0].p+frandom())/np;
377 <                xd = cos(phi) * zd;
378 <                yd = sin(phi) * zd;
379 <                zd = sqrt(1.0 - zd*zd);
380 <                for (k = 0; k < 3; k++)
381 <                        ar.rdir[k] = xd*ux[k]+yd*uy[k]+zd*r->ron[k];
382 <                rayvalue(&ar);
383 <                if (ar.rot < FHUGE)
384 <                        arad += 1.0 / ar.rot;
385 <                                                /* recompute error */
386 <                copycolor(dnew.v, div[0].v);
387 <                addcolor(dnew.v, ar.rcol);
388 <                dnew.n = div[0].n + 1;
389 <                dnew.t = div[0].t; dnew.p = div[0].p;
390 <                b2 = bright(dnew.v)/dnew.n - bright(ar.rcol);
391 <                b2 = b2*b2 + div[0].k*(div[0].n*div[0].n);
392 <                dnew.k = b2/(dnew.n*dnew.n);
393 <                                                /* reinsert */
394 <                for (k = 0; k < ne-1 && dnew.k < div[k+1].k; k++)
395 <                        bcopy(&div[k+1], &div[k], sizeof(AMBSAMP));
396 <                bcopy(&dnew, &div[k], sizeof(AMBSAMP));
397 <
398 <                if (ne >= i) {          /* extract darkest division */
399 <                        ne--;
400 <                        if (div[ne].n > 1)
401 <                                scalecolor(div[ne].v, 1.0/div[ne].n);
402 <                        addcolor(acol, div[ne].v);
403 <                }
404 <        }
405 <        scalecolor(acol, 1.0/ndivs);
406 <        if (arad <= FTINY)
407 <                arad = FHUGE;
408 <        else
409 <                arad = (ndivs+ns) / arad / sqrt(r->rweight);
410 <        if (arad > maxarad)
411 <                arad = maxarad;
412 <        else if (arad < minarad)
413 <                arad = minarad;
414 <        if (ns > 0)
415 <                free((char *)div);
416 <        return(arad);
300 >        copycolor(cr, ap->val);
301 >        scalecolor(cr, d);
302   }
303  
304  
305 < static int
306 < ambcmp(d1, d2)                          /* decreasing order */
307 < AMBSAMP  *d1, *d2;
305 > static
306 > initambfile(creat)              /* initialize ambient file */
307 > int  creat;
308   {
309 <        if (d1->k < d2->k)
310 <                return(1);
311 <        if (d1->k > d2->k)
312 <                return(-1);
313 <        return(0);
309 >        extern char  *progname, *octname, VersionID[];
310 >
311 > #ifdef MSDOS
312 >        setmode(fileno(ambfp), O_BINARY);
313 > #endif
314 >        setbuf(ambfp, bmalloc(BUFSIZ));
315 >        if (creat) {                    /* new file */
316 >                fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
317 >                                progname, colval(ambval,RED),
318 >                                colval(ambval,GRN), colval(ambval,BLU),
319 >                                ambounce, ambacc);
320 >                fprintf(ambfp, "-ad %d -as %d -ar %d %s\n",
321 >                                ambdiv, ambssamp, ambres,
322 >                                octname==NULL ? "" : octname);
323 >                fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
324 >                fputformat(AMBFMT, ambfp);
325 >                putc('\n', ambfp);
326 >                putambmagic(ambfp);
327 >        } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
328 >                error(USER, "bad ambient file");
329   }
330  
331  
332   static
333 < avsave(av)                              /* save an ambient value */
334 < AMBVAL  *av;
333 > avsave(av)                              /* insert and save an ambient value */
334 > AMBVAL  *av;
335   {
336 < #ifdef  AMBFLUSH
437 <        static int  nunflshed = 0;
438 < #endif
336 >        avinsert(av, &atrunk, thescene.cuorg, thescene.cusize);
337          if (ambfp == NULL)
338                  return;
339 <        if (fwrite(av, sizeof(AMBVAL), 1, ambfp) != 1)
339 >        if (writambval(av, ambfp) < 0)
340                  goto writerr;
341 < #ifdef  AMBFLUSH
342 <        if (++nunflshed >= AMBFLUSH) {
445 <                if (fflush(ambfp) == EOF)
341 >        if (++nunflshed >= AMBFLUSH)
342 >                if (ambsync() == EOF)
343                          goto writerr;
447                nunflshed = 0;
448        }
449 #endif
344          return;
345   writerr:
346          error(SYSTEM, "error writing ambient file");
# Line 455 | Line 349 | writerr:
349  
350   static
351   avinsert(aval, at, c0, s)               /* insert ambient value in a tree */
352 < AMBVAL  *aval;
352 > AMBVAL  *aval;
353   register AMBTREE  *at;
354   FVECT  c0;
355 < double  s;
355 > double  s;
356   {
357          FVECT  ck0;
358          int  branch;
359 <        register AMBVAL  *av;
359 >        register AMBVAL  *av;
360          register int  i;
361  
362          if ((av = newambval()) == NULL)
363                  goto memerr;
364 <        bcopy(aval, av, sizeof(AMBVAL));
364 >        copystruct(av, aval);
365          VCOPY(ck0, c0);
366          while (s*(OCTSCALE/2) > av->rad*ambacc) {
367                  if (at->kid == NULL)
# Line 488 | Line 382 | double  s;
382   memerr:
383          error(SYSTEM, "out of memory in avinsert");
384   }
385 +
386 +
387 + #ifdef  F_SETLKW
388 +
389 + int
390 + ambsync()                       /* synchronize ambient file */
391 + {
392 +        static FILE  *ambinp = NULL;
393 +        static long  lastpos = -1;
394 +        struct flock  fls;
395 +        long  flen;
396 +        AMBVAL  avs;
397 +        register int  n;
398 +
399 +        if (nunflshed == 0)
400 +                return(0);
401 +                                /* gain exclusive access */
402 +        fls.l_type = F_WRLCK;
403 +        fls.l_whence = 0;
404 +        fls.l_start = 0L;
405 +        fls.l_len = 0L;
406 +        if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
407 +                error(SYSTEM, "cannot lock ambient file");
408 +        if (lastpos < 0)        /* initializing */
409 +                goto syncend;
410 +                                /* see if file has grown */
411 +        if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
412 +                error(SYSTEM, "cannot seek on ambient file");
413 +        if (n = flen - lastpos) {               /* file has grown */
414 +                if (ambinp == NULL) {           /* use duplicate filedes */
415 +                        ambinp = fdopen(dup(fileno(ambfp)), "r");
416 +                        if (ambinp == NULL)
417 +                                error(SYSTEM, "fdopen failed in ambsync");
418 +                }
419 +                if (fseek(ambinp, lastpos, 0) < 0)
420 +                        error(SYSTEM, "fseek failed in ambsync");
421 +                while (n >= AMBVALSIZ) {        /* load contributed values */
422 +                        readambval(&avs, ambinp);
423 +                        avinsert(&avs,&atrunk,thescene.cuorg,thescene.cusize);
424 +                        n -= AMBVALSIZ;
425 +                }
426 +                if (n)                          /* alignment */
427 +                        lseek(fileno(ambfp), flen-n, 0);
428 +        }
429 + syncend:
430 +        n = fflush(ambfp);                      /* calls write() at last */
431 +        lastpos = lseek(fileno(ambfp), 0L, 1);
432 +        fls.l_type = F_UNLCK;                   /* release file */
433 +        fcntl(fileno(ambfp), F_SETLKW, &fls);
434 +        nunflshed = 0;
435 +        return(n);
436 + }
437 +
438 + #else
439 +
440 + int
441 + ambsync()                       /* flush ambient file */
442 + {
443 +        if (nunflshed == 0)
444 +                return(0);
445 +        nunflshed = 0;
446 +        return(fflush(ambfp));
447 + }
448 +
449 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines