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.60 by greg, Sat May 28 22:27:54 2005 UTC vs.
Revision 2.106 by greg, Thu Nov 8 00:54:07 2018 UTC

# Line 1 | Line 1
1 #ifndef lint
1   static const char       RCSid[] = "$Id$";
3 #endif
2   /*
3   *  ambient.c - routines dealing with ambient (inter-reflected) component.
4   *
# Line 14 | Line 12 | static const char      RCSid[] = "$Id$";
12   #include  "platform.h"
13   #include  "ray.h"
14   #include  "otypes.h"
15 + #include  "otspecial.h"
16   #include  "resolu.h"
17   #include  "ambient.h"
18   #include  "random.h"
19 + #include  "pmapamb.h"
20  
21   #ifndef  OCTSCALE
22   #define  OCTSCALE       1.0     /* ceil((valid rad.)/(cube size)) */
# Line 25 | Line 25 | static const char      RCSid[] = "$Id$";
25   extern char  *shm_boundary;     /* memory sharing boundary */
26  
27   #ifndef  MAXASET
28 < #define  MAXASET        2047    /* maximum number of elements in ambient set */
28 > #define  MAXASET        4095    /* maximum number of elements in ambient set */
29   #endif
30   OBJECT  ambset[MAXASET+1]={0};  /* ambient include/exclude set */
31  
# Line 39 | Line 39 | static int  nunflshed = 0;     /* number of unflushed ambi
39  
40   #ifndef SORT_THRESH
41   #ifdef SMLMEM
42 < #define SORT_THRESH     ((3L<<20)/sizeof(AMBVAL))
42 > #define SORT_THRESH     ((16L<<20)/sizeof(AMBVAL))
43   #else
44 < #define SORT_THRESH     ((9L<<20)/sizeof(AMBVAL))
44 > #define SORT_THRESH     ((64L<<20)/sizeof(AMBVAL))
45   #endif
46   #endif
47   #ifndef SORT_INTVL
# Line 51 | Line 51 | static int  nunflshed = 0;     /* number of unflushed ambi
51   #define MAX_SORT_INTVL  (SORT_INTVL<<6)
52   #endif
53  
54 +
55   static double  avsum = 0.;              /* computed ambient value sum (log) */
56   static unsigned int  navsum = 0;        /* number of values in avsum */
57   static unsigned int  nambvals = 0;      /* total number of indirect values */
# Line 76 | Line 77 | static long  lastpos = -1;             /* last flush position */
77   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
78  
79   #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
79 #define  freeav(av)     free((void *)av);
80  
81   static void initambfile(int creat);
82   static void avsave(AMBVAL *av);
# Line 84 | Line 84 | static AMBVAL *avstore(AMBVAL  *aval);
84   static AMBTREE *newambtree(void);
85   static void freeambtree(AMBTREE  *atp);
86  
87 < typedef void unloadtf_t(void *);
87 > typedef void unloadtf_t(AMBVAL *);
88   static unloadtf_t avinsert;
89   static unloadtf_t av2list;
90 + static unloadtf_t avfree;
91   static void unloadatree(AMBTREE  *at, unloadtf_t *f);
92  
93   static int aposcmp(const void *avp1, const void *avp2);
# Line 98 | Line 99 | static void aflock(int  typ);
99   #endif
100  
101  
102 < extern void
102 > void
103   setambres(                              /* set ambient resolution */
104          int  ar
105   )
# Line 107 | Line 108 | setambres(                             /* set ambient resolution */
108                                                  /* set min & max radii */
109          if (ar <= 0) {
110                  minarad = 0;
111 <                maxarad = thescene.cusize / 2.0;
111 >                maxarad = thescene.cusize*0.2;
112          } else {
113                  minarad = thescene.cusize / ar;
114 <                maxarad = 64 * minarad;                 /* heuristic */
115 <                if (maxarad > thescene.cusize / 2.0)
116 <                        maxarad = thescene.cusize / 2.0;
114 >                maxarad = 64.0 * minarad;               /* heuristic */
115 >                if (maxarad > thescene.cusize*0.2)
116 >                        maxarad = thescene.cusize*0.2;
117          }
118          if (minarad <= FTINY)
119 <                minarad = 10*FTINY;
119 >                minarad = 10.0*FTINY;
120          if (maxarad <= minarad)
121 <                maxarad = 64 * minarad;
121 >                maxarad = 64.0 * minarad;
122   }
123  
124  
125 < extern void
125 > void
126   setambacc(                              /* set ambient accuracy */
127          double  newa
128   )
129   {
130 <        double  ambdiff;
131 <
132 <        if (newa < 0.0)
133 <                newa = 0.0;
134 <        ambdiff = fabs(newa - ambacc);
135 <        if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0)
136 <                sortambvals(1);                 /* rebuild tree */
130 >        static double   olda;           /* remember previous setting here */
131 >        
132 >        newa *= (newa > 0);
133 >        if (fabs(newa - olda) >= .05*(newa + olda)) {
134 >                ambacc = newa;
135 >                if (nambvals > 0)
136 >                        sortambvals(1);         /* rebuild tree */
137 >        }
138   }
139  
140  
141 < extern void
141 > void
142   setambient(void)                                /* initialize calculation */
143   {
144          int     readonly = 0;
145 <        long  pos, flen;
145 >        long    flen;
146          AMBVAL  amb;
147                                                  /* make sure we're fresh */
148          ambdone();
# Line 160 | Line 162 | setambient(void)                               /* initialize calculation */
162                  readonly = (ambfp = fopen(ambfile, "r")) != NULL;
163          if (ambfp != NULL) {
164                  initambfile(0);                 /* file exists */
165 <                pos = ftell(ambfp);
165 >                lastpos = ftell(ambfp);
166                  while (readambval(&amb, ambfp))
167 <                        avinsert(avstore(&amb));
167 >                        avstore(&amb);
168                  nambshare = nambvals;           /* share loaded values */
169                  if (readonly) {
170                          sprintf(errmsg,
# Line 174 | Line 176 | setambient(void)                               /* initialize calculation */
176                          return;                 /* avoid ambsync() */
177                  }
178                                                  /* align file pointer */
179 <                pos += (long)nambvals*AMBVALSIZ;
179 >                lastpos += (long)nambvals*AMBVALSIZ;
180                  flen = lseek(fileno(ambfp), (off_t)0, SEEK_END);
181 <                if (flen != pos) {
181 >                if (flen != lastpos) {
182                          sprintf(errmsg,
183                          "ignoring last %ld values in ambient file (corrupted)",
184 <                                        (flen - pos)/AMBVALSIZ);
184 >                                        (flen - lastpos)/AMBVALSIZ);
185                          error(WARNING, errmsg);
186 <                        fseek(ambfp, pos, 0);
187 < #ifndef _WIN32 /* XXX we need a replacement for that one */
186 <                        ftruncate(fileno(ambfp), (off_t)pos);
187 < #endif
186 >                        fseek(ambfp, lastpos, SEEK_SET);
187 >                        ftruncate(fileno(ambfp), (off_t)lastpos);
188                  }
189          } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
190                  initambfile(1);                 /* else create new file */
191 +                fflush(ambfp);
192 +                lastpos = ftell(ambfp);
193          } else {
194                  sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
195                  error(SYSTEM, errmsg);
196          }
197 <        nunflshed++;    /* lie */
198 <        ambsync();
197 > #ifdef  F_SETLKW
198 >        aflock(F_UNLCK);                        /* release file */
199 > #endif
200   }
201  
202  
203 < extern void
203 > void
204   ambdone(void)                   /* close ambient file and free memory */
205   {
206          if (ambfp != NULL) {            /* close ambient file */
# Line 211 | Line 214 | ambdone(void)                  /* close ambient file and free memory
214                  lastpos = -1;
215          }
216                                          /* free ambient tree */
217 <        unloadatree(&atrunk, free);
217 >        unloadatree(&atrunk, avfree);
218                                          /* reset state variables */
219          avsum = 0.;
220          navsum = 0;
# Line 223 | Line 226 | ambdone(void)                  /* close ambient file and free memory
226   }
227  
228  
229 < extern void
229 > void
230   ambnotify(                      /* record new modifier */
231          OBJECT  obj
232   )
233   {
234          static int  hitlimit = 0;
235 <        register OBJREC  *o;
236 <        register char  **amblp;
235 >        OBJREC   *o;
236 >        char  **amblp;
237  
238          if (obj == OVOID) {             /* starting over */
239                  ambset[0] = 0;
# Line 252 | Line 255 | ambnotify(                     /* record new modifier */
255                  }
256   }
257  
258 + /************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/
259  
260 < extern void
260 > #ifndef OLDAMB
261 >
262 > #define tfunc(lwr, x, upr)      (((x)-(lwr))/((upr)-(lwr)))
263 >
264 > static int      plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang);
265 > static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
266 >                                AMBTREE *at, FVECT c0, double s);
267 > static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
268 > static int      extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
269 >                                FVECT uvw[3]);
270 >
271 > void
272   multambient(            /* compute ambient component & multiply by coef. */
273          COLOR  aval,
274 <        register RAY  *r,
274 >        RAY  *r,
275          FVECT  nrm
276   )
277   {
278          static int  rdepth = 0;                 /* ambient recursion */
279 <        COLOR   acol;
279 >        COLOR   acol, caustic;
280 >        int     i, ok;
281          double  d, l;
282  
283 +        /* PMAP: Factor in ambient from photon map, if enabled and ray is
284 +         * ambient. Return as all ambient components accounted for, else
285 +         * continue. */
286 +        if (ambPmap(aval, r, rdepth))
287 +                return;
288 +
289 +        /* PMAP: Factor in specular-diffuse ambient (caustics) from photon
290 +         * map, if enabled and ray is primary, else caustic is zero.  Continue
291 +         * with RADIANCE ambient calculation */
292 +        copycolor(caustic, aval);
293 +        ambPmapCaustic(caustic, r, rdepth);
294 +        
295          if (ambdiv <= 0)                        /* no ambient calculation */
296                  goto dumbamb;
297                                                  /* check number of bounces */
# Line 275 | Line 303 | multambient(           /* compute ambient component & multiply
303                  goto dumbamb;
304  
305          if (ambacc <= FTINY) {                  /* no ambient storage */
306 +                FVECT   uvd[2];
307 +                float   dgrad[2], *dgp = NULL;
308 +
309 +                if (nrm != r->ron && DOT(nrm,r->ron) < 0.9999)
310 +                        dgp = dgrad;            /* compute rotational grad. */
311 +                copycolor(acol, aval);
312                  rdepth++;
313 <                d = doambient(acol, r, aval, intens(aval)*r->rweight,
314 <                                                        NULL, NULL);
313 >                ok = doambient(acol, r, r->rweight,
314 >                                uvd, NULL, NULL, dgp, NULL);
315                  rdepth--;
316 <                if (d <= FTINY)
316 >                if (!ok)
317                          goto dumbamb;
318 +                if ((ok > 0) & (dgp != NULL)) { /* apply texture */
319 +                        FVECT   v1;
320 +                        VCROSS(v1, r->ron, nrm);
321 +                        d = 1.0;
322 +                        for (i = 3; i--; )
323 +                                d += v1[i] * (dgp[0]*uvd[0][i] + dgp[1]*uvd[1][i]);
324 +                        if (d >= 0.05)
325 +                                scalecolor(acol, d);
326 +                }
327 +                copycolor(aval, acol);
328 +
329 +                /* PMAP: add in caustic */
330 +                addcolor(aval, caustic);
331 +                return;
332 +        }
333 +
334 +        if (tracktime)                          /* sort to minimize thrashing */
335 +                sortambvals(0);
336 +                                                /* interpolate ambient value */
337 +        setcolor(acol, 0.0, 0.0, 0.0);
338 +        d = sumambient(acol, r, nrm, rdepth,
339 +                        &atrunk, thescene.cuorg, thescene.cusize);
340 +                        
341 +        if (d > FTINY) {
342 +                d = 1.0/d;
343 +                scalecolor(acol, d);
344                  multcolor(aval, acol);
345 +
346 +                /* PMAP: add in caustic */
347 +                addcolor(aval, caustic);
348                  return;
349          }
350 +        
351 +        rdepth++;                               /* need to cache new value */
352 +        ok = makeambient(acol, r, nrm, rdepth-1);
353 +        rdepth--;
354 +        
355 +        if (ok) {
356 +                multcolor(aval, acol);          /* computed new value */
357  
358 +                /* PMAP: add in caustic */
359 +                addcolor(aval, caustic);
360 +                return;
361 +        }
362 +        
363 + dumbamb:                                        /* return global value */
364 +        if ((ambvwt <= 0) | (navsum == 0)) {
365 +                multcolor(aval, ambval);
366 +                
367 +                /* PMAP: add in caustic */
368 +                addcolor(aval, caustic);
369 +                return;
370 +        }
371 +        
372 +        l = bright(ambval);                     /* average in computations */  
373 +        if (l > FTINY) {
374 +                d = (log(l)*(double)ambvwt + avsum) /
375 +                                (double)(ambvwt + navsum);
376 +                d = exp(d) / l;
377 +                scalecolor(aval, d);
378 +                multcolor(aval, ambval);        /* apply color of ambval */
379 +        } else {
380 +                d = exp( avsum / (double)navsum );
381 +                scalecolor(aval, d);            /* neutral color */
382 +        }
383 + }
384 +
385 +
386 + /* Plug a potential leak where ambient cache value is occluded */
387 + static int
388 + plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang)
389 + {
390 +        const double    cost70sq = 0.1169778;   /* cos(70deg)^2 */
391 +        RAY             rtst;
392 +        FVECT           vdif;
393 +        double          normdot, ndotd, nadotd;
394 +        double          a, b, c, t[2];
395 +
396 +        ang += 2.*PI*(ang < 0);                 /* check direction flags */
397 +        if ( !(ap->corral>>(int)(ang*(16./PI)) & 1) )
398 +                return(0);
399 +        /*
400 +         * Generate test ray, targeting 20 degrees above sample point plane
401 +         * along surface normal from cache position.  This should be high
402 +         * enough to miss local geometry we don't really care about.
403 +         */
404 +        VSUB(vdif, ap->pos, r->rop);
405 +        normdot = DOT(anorm, r->ron);
406 +        ndotd = DOT(vdif, r->ron);
407 +        nadotd = DOT(vdif, anorm);
408 +        a = normdot*normdot - cost70sq;
409 +        b = 2.0*(normdot*ndotd - nadotd*cost70sq);
410 +        c = ndotd*ndotd - DOT(vdif,vdif)*cost70sq;
411 +        if (quadratic(t, a, b, c) != 2)
412 +                return(1);                      /* should rarely happen */
413 +        if (t[1] <= FTINY)
414 +                return(0);                      /* should fail behind test */
415 +        rayorigin(&rtst, SHADOW, r, NULL);
416 +        VSUM(rtst.rdir, vdif, anorm, t[1]);     /* further dist. > plane */
417 +        rtst.rmax = normalize(rtst.rdir);       /* short ray test */
418 +        while (localhit(&rtst, &thescene)) {    /* check for occluder */
419 +                OBJREC  *m = findmaterial(rtst.ro);
420 +                if (m != NULL && !istransp(m->otype) && !isBSDFproxy(m) &&
421 +                                (rtst.clipset == NULL ||
422 +                                        !inset(rtst.clipset, rtst.ro->omod)))
423 +                        return(1);              /* plug light leak */
424 +                VCOPY(rtst.rorg, rtst.rop);     /* skip invisible surface */
425 +                rtst.rmax -= rtst.rot;
426 +                rayclear(&rtst);
427 +        }
428 +        return(0);                              /* seems we're OK */
429 + }
430 +
431 +
432 + static double
433 + sumambient(             /* get interpolated ambient value */
434 +        COLOR  acol,
435 +        RAY  *r,
436 +        FVECT  rn,
437 +        int  al,
438 +        AMBTREE  *at,
439 +        FVECT  c0,
440 +        double  s
441 + )
442 + {                       /* initial limit is 10 degrees plus ambacc radians */
443 +        const double    minangle = 10.0 * PI/180.;
444 +        double          maxangle = minangle + ambacc;
445 +        double          wsum = 0.0;
446 +        FVECT           ck0;
447 +        int             i, j;
448 +        AMBVAL          *av;
449 +
450 +        if (at->kid != NULL) {          /* sum children first */                                
451 +                s *= 0.5;
452 +                for (i = 0; i < 8; i++) {
453 +                        for (j = 0; j < 3; j++) {
454 +                                ck0[j] = c0[j];
455 +                                if (1<<j & i)
456 +                                        ck0[j] += s;
457 +                                if (r->rop[j] < ck0[j] - OCTSCALE*s)
458 +                                        break;
459 +                                if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
460 +                                        break;
461 +                        }
462 +                        if (j == 3)
463 +                                wsum += sumambient(acol, r, rn, al,
464 +                                                        at->kid+i, ck0, s);
465 +                }
466 +                                        /* good enough? */
467 +                if (wsum >= 0.05 && s > minarad*10.0)
468 +                        return(wsum);
469 +        }
470 +                                        /* adjust maximum angle */
471 +        if (at->alist != NULL && (at->alist->lvl <= al) & (r->rweight < 0.6))
472 +                maxangle = (maxangle - PI/2.)*pow(r->rweight,0.13) + PI/2.;
473 +                                        /* sum this node */
474 +        for (av = at->alist; av != NULL; av = av->next) {
475 +                double  u, v, d, delta_r2, delta_t2;
476 +                COLOR   ct;
477 +                FVECT   uvw[3];
478 +                                        /* record access */
479 +                if (tracktime)
480 +                        av->latick = ambclock;
481 +                /*
482 +                 *  Ambient level test
483 +                 */
484 +                if (av->lvl > al ||     /* list sorted, so this works */
485 +                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
486 +                        break;
487 +                /*
488 +                 *  Direction test using unperturbed normal
489 +                 */
490 +                decodedir(uvw[2], av->ndir);
491 +                d = DOT(uvw[2], r->ron);
492 +                if (d <= 0.0)           /* >= 90 degrees */
493 +                        continue;
494 +                delta_r2 = 2.0 - 2.0*d; /* approx. radians^2 */
495 +                if (delta_r2 >= maxangle*maxangle)
496 +                        continue;
497 +                /*
498 +                 *  Modified ray behind test
499 +                 */
500 +                VSUB(ck0, r->rop, av->pos);
501 +                d = DOT(ck0, uvw[2]);
502 +                if (d < -minarad*ambacc-.001)
503 +                        continue;
504 +                d /= av->rad[0];
505 +                delta_t2 = d*d;
506 +                if (delta_t2 >= ambacc*ambacc)
507 +                        continue;
508 +                /*
509 +                 *  Elliptical radii test based on Hessian
510 +                 */
511 +                decodedir(uvw[0], av->udir);
512 +                VCROSS(uvw[1], uvw[2], uvw[0]);
513 +                d = (u = DOT(ck0, uvw[0])) / av->rad[0];
514 +                delta_t2 += d*d;
515 +                d = (v = DOT(ck0, uvw[1])) / av->rad[1];
516 +                delta_t2 += d*d;
517 +                if (delta_t2 >= ambacc*ambacc)
518 +                        continue;
519 +                /*
520 +                 *  Test for potential light leak
521 +                 */
522 +                if (av->corral && plugaleak(r, av, uvw[2], atan2a(v,u)))
523 +                        continue;
524 +                /*
525 +                 *  Extrapolate value and compute final weight (hat function)
526 +                 */
527 +                if (!extambient(ct, av, r->rop, rn, uvw))
528 +                        continue;
529 +                d = tfunc(maxangle, sqrt(delta_r2), 0.0) *
530 +                        tfunc(ambacc, sqrt(delta_t2), 0.0);
531 +                scalecolor(ct, d);
532 +                addcolor(acol, ct);
533 +                wsum += d;
534 +        }
535 +        return(wsum);
536 + }
537 +
538 +
539 + static int
540 + makeambient(            /* make a new ambient value for storage */
541 +        COLOR  acol,
542 +        RAY  *r,
543 +        FVECT  rn,
544 +        int  al
545 + )
546 + {
547 +        AMBVAL  amb;
548 +        FVECT   uvw[3];
549 +        int     i;
550 +
551 +        amb.weight = 1.0;                       /* compute weight */
552 +        for (i = al; i-- > 0; )
553 +                amb.weight *= AVGREFL;
554 +        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
555 +                amb.weight = 1.25*r->rweight;
556 +        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
557 +                                                /* compute ambient */
558 +        i = doambient(acol, r, amb.weight,
559 +                        uvw, amb.rad, amb.gpos, amb.gdir, &amb.corral);
560 +        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
561 +        if (i <= 0 || amb.rad[0] <= FTINY)      /* no Hessian or zero radius */
562 +                return(i);
563 +                                                /* store value */
564 +        VCOPY(amb.pos, r->rop);
565 +        amb.ndir = encodedir(r->ron);
566 +        amb.udir = encodedir(uvw[0]);
567 +        amb.lvl = al;
568 +        copycolor(amb.val, acol);
569 +                                                /* insert into tree */
570 +        avsave(&amb);                           /* and save to file */
571 +        if (rn != r->ron) {                     /* texture */
572 +                VCOPY(uvw[2], r->ron);
573 +                extambient(acol, &amb, r->rop, rn, uvw);
574 +        }
575 +        return(1);
576 + }
577 +
578 +
579 + static int
580 + extambient(             /* extrapolate value at pv, nv */
581 +        COLOR  cr,
582 +        AMBVAL   *ap,
583 +        FVECT  pv,
584 +        FVECT  nv,
585 +        FVECT  uvw[3]
586 + )
587 + {
588 +        const double    min_d = 0.05;
589 +        static FVECT    my_uvw[3];
590 +        FVECT           v1;
591 +        int             i;
592 +        double          d = 1.0;        /* zeroeth order */
593 +
594 +        if (uvw == NULL) {              /* need local coordinates? */
595 +                decodedir(my_uvw[2], ap->ndir);
596 +                decodedir(my_uvw[0], ap->udir);
597 +                VCROSS(my_uvw[1], my_uvw[2], my_uvw[0]);
598 +                uvw = my_uvw;
599 +        }
600 +        for (i = 3; i--; )              /* gradient due to translation */
601 +                d += (pv[i] - ap->pos[i]) *
602 +                        (ap->gpos[0]*uvw[0][i] + ap->gpos[1]*uvw[1][i]);
603 +
604 +        VCROSS(v1, uvw[2], nv);         /* gradient due to rotation */
605 +        for (i = 3; i--; )
606 +                d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]);
607 +        
608 +        if (d < min_d)                  /* should not use if we can avoid it */
609 +                d = min_d;
610 +        copycolor(cr, ap->val);
611 +        scalecolor(cr, d);
612 +        return(d > min_d);
613 + }
614 +
615 +
616 + static void
617 + avinsert(                               /* insert ambient value in our tree */
618 +        AMBVAL *av
619 + )
620 + {
621 +        AMBTREE  *at;
622 +        AMBVAL  *ap;
623 +        AMBVAL  avh;
624 +        FVECT  ck0;
625 +        double  s;
626 +        int  branch;
627 +        int  i;
628 +
629 +        if (av->rad[0] <= FTINY)
630 +                error(CONSISTENCY, "zero ambient radius in avinsert");
631 +        at = &atrunk;
632 +        VCOPY(ck0, thescene.cuorg);
633 +        s = thescene.cusize;
634 +        while (s*(OCTSCALE/2) > av->rad[1]*ambacc) {
635 +                if (at->kid == NULL)
636 +                        if ((at->kid = newambtree()) == NULL)
637 +                                error(SYSTEM, "out of memory in avinsert");
638 +                s *= 0.5;
639 +                branch = 0;
640 +                for (i = 0; i < 3; i++)
641 +                        if (av->pos[i] > ck0[i] + s) {
642 +                                ck0[i] += s;
643 +                                branch |= 1 << i;
644 +                        }
645 +                at = at->kid + branch;
646 +        }
647 +        avh.next = at->alist;           /* order by increasing level */
648 +        for (ap = &avh; ap->next != NULL; ap = ap->next)
649 +                if ( ap->next->lvl > av->lvl ||
650 +                                (ap->next->lvl == av->lvl) &
651 +                                (ap->next->weight <= av->weight) )
652 +                        break;
653 +        av->next = ap->next;
654 +        ap->next = (AMBVAL*)av;
655 +        at->alist = avh.next;
656 + }
657 +
658 +
659 + #else /* ! NEWAMB */
660 +
661 + static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
662 +                                AMBTREE *at, FVECT c0, double s);
663 + static double   makeambient(COLOR acol, RAY *r, FVECT rn, int al);
664 + static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv);
665 +
666 +
667 + void
668 + multambient(            /* compute ambient component & multiply by coef. */
669 +        COLOR  aval,
670 +        RAY  *r,
671 +        FVECT  nrm
672 + )
673 + {
674 +        static int  rdepth = 0;                 /* ambient recursion */
675 +        COLOR   acol, caustic;
676 +        double  d, l;
677 +
678 +        /* PMAP: Factor in ambient from global photon map (if enabled) and return
679 +         * as all ambient components accounted for */
680 +        if (ambPmap(aval, r, rdepth))
681 +                return;
682 +
683 +        /* PMAP: Otherwise factor in ambient from caustic photon map
684 +         * (ambPmapCaustic() returns zero if caustic photons disabled) and
685 +         * continue with RADIANCE ambient calculation */
686 +        copycolor(caustic, aval);
687 +        ambPmapCaustic(caustic, r, rdepth);
688 +        
689 +        if (ambdiv <= 0)                        /* no ambient calculation */
690 +                goto dumbamb;
691 +                                                /* check number of bounces */
692 +        if (rdepth >= ambounce)
693 +                goto dumbamb;
694 +                                                /* check ambient list */
695 +        if (ambincl != -1 && r->ro != NULL &&
696 +                        ambincl != inset(ambset, r->ro->omod))
697 +                goto dumbamb;
698 +
699 +        if (ambacc <= FTINY) {                  /* no ambient storage */
700 +                copycolor(acol, aval);
701 +                rdepth++;
702 +                d = doambient(acol, r, r->rweight, NULL, NULL);
703 +                rdepth--;
704 +                if (d <= FTINY)
705 +                        goto dumbamb;
706 +                copycolor(aval, acol);          
707 +        
708 +           /* PMAP: add in caustic */
709 +                addcolor(aval, caustic);        
710 +                return;
711 +        }
712 +
713          if (tracktime)                          /* sort to minimize thrashing */
714                  sortambvals(0);
715 <                                                /* get ambient value */
715 >                                                /* interpolate ambient value */
716          setcolor(acol, 0.0, 0.0, 0.0);
717 <        d = sumambient(acol, r, intens(aval)*r->rweight, nrm, rdepth,
717 >        d = sumambient(acol, r, nrm, rdepth,
718                          &atrunk, thescene.cuorg, thescene.cusize);
719 +                        
720          if (d > FTINY) {
721 <                scalecolor(acol, 1.0/d);
721 >                d = 1.0/d;
722 >                scalecolor(acol, d);
723                  multcolor(aval, acol);
724 +                
725 +                /* PMAP: add in caustic */
726 +                addcolor(aval, caustic);        
727                  return;
728          }
729 +        
730          rdepth++;                               /* need to cache new value */
731 <        d = makeambient(acol, r, aval, nrm, rdepth-1);
731 >        d = makeambient(acol, r, nrm, rdepth-1);
732          rdepth--;
733 +        
734          if (d > FTINY) {
735                  multcolor(aval, acol);          /* got new value */
736 +
737 +                /* PMAP: add in caustic */
738 +                addcolor(aval, caustic);                        
739                  return;
740          }
741 +        
742   dumbamb:                                        /* return global value */
743          if ((ambvwt <= 0) | (navsum == 0)) {
744                  multcolor(aval, ambval);
745 +
746 +                /* PMAP: add in caustic */
747 +                addcolor(aval, caustic);        
748                  return;
749          }
750 +        
751          l = bright(ambval);                     /* average in computations */
752          if (l > FTINY) {
753                  d = (log(l)*(double)ambvwt + avsum) /
# Line 322 | Line 762 | dumbamb:                                       /* return global value */
762   }
763  
764  
765 < extern double
765 > static double
766   sumambient(     /* get interpolated ambient value */
767          COLOR  acol,
768 <        register RAY  *r,
329 <        double  aw,
768 >        RAY  *r,
769          FVECT  rn,
770          int  al,
771          AMBTREE  *at,
# Line 338 | Line 777 | sumambient(    /* get interpolated ambient value */
777          COLOR  ct;
778          FVECT  ck0;
779          int  i;
780 <        register int  j;
781 <        register AMBVAL  *av;
780 >        int  j;
781 >        AMBVAL   *av;
782  
783          wsum = 0.0;
784                                          /* do this node */
# Line 350 | Line 789 | sumambient(    /* get interpolated ambient value */
789                  /*
790                   *  Ambient level test.
791                   */
792 <                if (av->lvl > al)       /* list sorted, so this works */
792 >                if (av->lvl > al ||     /* list sorted, so this works */
793 >                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
794                          break;
355                if (av->weight < 0.85*aw)
356                        continue;
795                  /*
796                   *  Ambient radius test.
797                   */
798 <                d = av->pos[0] - r->rop[0];
799 <                e1 = d * d;
362 <                d = av->pos[1] - r->rop[1];
363 <                e1 += d * d;
364 <                d = av->pos[2] - r->rop[2];
365 <                e1 += d * d;
366 <                e1 /= av->rad * av->rad;
798 >                VSUB(ck0, av->pos, r->rop);
799 >                e1 = DOT(ck0, ck0) / (av->rad * av->rad);
800                  if (e1 > ambacc*ambacc*1.21)
801                          continue;
802                  /*
# Line 380 | Line 813 | sumambient(    /* get interpolated ambient value */
813                          }
814                  }
815                  e2 = (1.0 - d) * r->rweight;
816 <                if (e2 < 0.0) e2 = 0.0;
817 <                if (e1 + e2 > ambacc*ambacc*1.21)
816 >                if (e2 < 0.0)
817 >                        e2 = 0.0;
818 >                else if (e1 + e2 > ambacc*ambacc*1.21)
819                          continue;
820                  /*
821                   *  Ray behind test.
# Line 431 | Line 865 | sumambient(    /* get interpolated ambient value */
865                                  break;
866                  }
867                  if (j == 3)
868 <                        wsum += sumambient(acol, r, aw, rn, al,
868 >                        wsum += sumambient(acol, r, rn, al,
869                                                  at->kid+i, ck0, s);
870          }
871          return(wsum);
872   }
873  
874  
875 < extern double
876 < makeambient(            /* make a new ambient value */
875 > static double
876 > makeambient(            /* make a new ambient value for storage */
877          COLOR  acol,
878          RAY  *r,
445        COLOR  ac,
879          FVECT  rn,
880          int  al
881   )
882   {
883          AMBVAL  amb;
451        double  awt;
884          FVECT   gp, gd;
885          int     i;
886  
887 <        amb.weight = AVGREFL;                   /* compute weight */
888 <        for (i = al; i-- >= 0; )
887 >        amb.weight = 1.0;                       /* compute weight */
888 >        for (i = al; i-- > 0; )
889                  amb.weight *= AVGREFL;
890 <        awt = intens(ac)*r->rweight;
891 <        if (awt < 0.07*amb.weight)              /* heuristic override */
892 <                amb.weight = 1.2*awt;
890 >        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
891 >                amb.weight = 1.25*r->rweight;
892 >        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
893                                                  /* compute ambient */
894 <        amb.rad = doambient(acol, r, ac, amb.weight, gp, gd);
895 <        if (amb.rad <= FTINY)
894 >        amb.rad = doambient(acol, r, amb.weight, gp, gd);
895 >        if (amb.rad <= FTINY) {
896 >                setcolor(acol, 0.0, 0.0, 0.0);
897                  return(0.0);
898 <                                                /* store it */
898 >        }
899 >        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
900 >                                                /* store value */
901          VCOPY(amb.pos, r->rop);
902          VCOPY(amb.dir, r->ron);
903          amb.lvl = al;
# Line 477 | Line 912 | makeambient(           /* make a new ambient value */
912   }
913  
914  
915 < extern void
915 > static void
916   extambient(             /* extrapolate value at pv, nv */
917          COLOR  cr,
918 <        register AMBVAL  *ap,
918 >        AMBVAL   *ap,
919          FVECT  pv,
920          FVECT  nv
921   )
922   {
923          FVECT  v1;
924 <        register int  i;
924 >        int  i;
925          double  d;
926  
927          d = 1.0;                        /* zeroeth order */
# Line 506 | Line 941 | extambient(            /* extrapolate value at pv, nv */
941  
942  
943   static void
944 + avinsert(                               /* insert ambient value in our tree */
945 +        AMBVAL *av
946 + )
947 + {
948 +        AMBTREE  *at;
949 +        AMBVAL  *ap;
950 +        AMBVAL  avh;
951 +        FVECT  ck0;
952 +        double  s;
953 +        int  branch;
954 +        int  i;
955 +
956 +        if (av->rad <= FTINY)
957 +                error(CONSISTENCY, "zero ambient radius in avinsert");
958 +        at = &atrunk;
959 +        VCOPY(ck0, thescene.cuorg);
960 +        s = thescene.cusize;
961 +        while (s*(OCTSCALE/2) > av->rad*ambacc) {
962 +                if (at->kid == NULL)
963 +                        if ((at->kid = newambtree()) == NULL)
964 +                                error(SYSTEM, "out of memory in avinsert");
965 +                s *= 0.5;
966 +                branch = 0;
967 +                for (i = 0; i < 3; i++)
968 +                        if (av->pos[i] > ck0[i] + s) {
969 +                                ck0[i] += s;
970 +                                branch |= 1 << i;
971 +                        }
972 +                at = at->kid + branch;
973 +        }
974 +        avh.next = at->alist;           /* order by increasing level */
975 +        for (ap = &avh; ap->next != NULL; ap = ap->next)
976 +                if ( ap->next->lvl > av->lvl ||
977 +                                (ap->next->lvl == av->lvl) &
978 +                                (ap->next->weight <= av->weight) )
979 +                        break;
980 +        av->next = ap->next;
981 +        ap->next = (AMBVAL*)av;
982 +        at->alist = avh.next;
983 + }
984 +
985 + #endif  /* ! NEWAMB */
986 +
987 + /************* FOLLOWING ROUTINES SAME FOR NEW & OLD METHODS ***************/
988 +
989 + static void
990   initambfile(            /* initialize ambient file */
991 <        int  creat
991 >        int  cre8
992   )
993   {
994          extern char  *progname, *octname;
995          static char  *mybuf = NULL;
996  
997   #ifdef  F_SETLKW
998 <        aflock(creat ? F_WRLCK : F_RDLCK);
998 >        aflock(cre8 ? F_WRLCK : F_RDLCK);
999   #endif
1000          SET_FILE_BINARY(ambfp);
1001          if (mybuf == NULL)
1002                  mybuf = (char *)bmalloc(BUFSIZ+8);
1003          setbuf(ambfp, mybuf);
1004 <        if (creat) {                    /* new file */
1004 >        if (cre8) {                     /* new file */
1005                  newheader("RADIANCE", ambfp);
1006                  fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ",
1007                                  progname, colval(ambval,RED),
# Line 529 | Line 1010 | initambfile(           /* initialize ambient file */
1010                  fprintf(ambfp, "-ad %d -as %d -ar %d ",
1011                                  ambdiv, ambssamp, ambres);
1012                  if (octname != NULL)
1013 <                        printargs(1, &octname, ambfp);
1014 <                else
534 <                        fputc('\n', ambfp);
1013 >                        fputs(octname, ambfp);
1014 >                fputc('\n', ambfp);
1015                  fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
1016                  fputnow(ambfp);
1017                  fputformat(AMBFMT, ambfp);
1018 <                putc('\n', ambfp);
1018 >                fputc('\n', ambfp);
1019                  putambmagic(ambfp);
1020          } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
1021                  error(USER, "bad ambient file");
# Line 547 | Line 1027 | avsave(                                /* insert and save an ambient value */
1027          AMBVAL  *av
1028   )
1029   {
1030 <        avinsert(avstore(av));
1030 >        avstore(av);
1031          if (ambfp == NULL)
1032                  return;
1033          if (writambval(av, ambfp) < 0)
# Line 562 | Line 1042 | writerr:
1042  
1043  
1044   static AMBVAL *
1045 < avstore(                                /* allocate memory and store aval */
1046 <        register AMBVAL  *aval
1045 > avstore(                                /* allocate memory and save aval */
1046 >        AMBVAL  *aval
1047   )
1048   {
1049 <        register AMBVAL  *av;
1049 >        AMBVAL  *av;
1050          double  d;
1051  
1052          if ((av = newambval()) == NULL)
# Line 580 | Line 1060 | avstore(                               /* allocate memory and store aval */
1060                  avsum += log(d);
1061                  navsum++;
1062          }
1063 +        avinsert(av);                   /* insert in our cache tree */
1064          return(av);
1065   }
1066  
# Line 592 | Line 1073 | static AMBTREE  *atfreelist = NULL;    /* free ambient tr
1073   static AMBTREE *
1074   newambtree(void)                                /* allocate 8 ambient tree structs */
1075   {
1076 <        register AMBTREE  *atp, *upperlim;
1076 >        AMBTREE  *atp, *upperlim;
1077  
1078          if (atfreelist == NULL) {       /* get more nodes */
1079                  atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE));
# Line 606 | Line 1087 | newambtree(void)                               /* allocate 8 ambient tree structs
1087          }
1088          atp = atfreelist;
1089          atfreelist = atp->kid;
1090 <        memset((char *)atp, '\0', 8*sizeof(AMBTREE));
1090 >        memset(atp, 0, 8*sizeof(AMBTREE));
1091          return(atp);
1092   }
1093  
# Line 622 | Line 1103 | freeambtree(                   /* free 8 ambient tree structs */
1103  
1104  
1105   static void
625 avinsert(                               /* insert ambient value in our tree */
626        void *av
627 )
628 {
629        register AMBTREE  *at;
630        register AMBVAL  *ap;
631        AMBVAL  avh;
632        FVECT  ck0;
633        double  s;
634        int  branch;
635        register int  i;
636
637        if (((AMBVAL*)av)->rad <= FTINY)
638                error(CONSISTENCY, "zero ambient radius in avinsert");
639        at = &atrunk;
640        VCOPY(ck0, thescene.cuorg);
641        s = thescene.cusize;
642        while (s*(OCTSCALE/2) > ((AMBVAL*)av)->rad*ambacc) {
643                if (at->kid == NULL)
644                        if ((at->kid = newambtree()) == NULL)
645                                error(SYSTEM, "out of memory in avinsert");
646                s *= 0.5;
647                branch = 0;
648                for (i = 0; i < 3; i++)
649                        if (((AMBVAL*)av)->pos[i] > ck0[i] + s) {
650                                ck0[i] += s;
651                                branch |= 1 << i;
652                        }
653                at = at->kid + branch;
654        }
655        avh.next = at->alist;           /* order by increasing level */
656        for (ap = &avh; ap->next != NULL; ap = ap->next)
657                if (ap->next->lvl >= ((AMBVAL*)av)->lvl)
658                        break;
659        ((AMBVAL*)av)->next = ap->next;
660        ap->next = (AMBVAL*)av;
661        at->alist = avh.next;
662 }
663
664
665 static void
1106   unloadatree(                    /* unload an ambient value tree */
1107 <        register AMBTREE  *at,
1107 >        AMBTREE  *at,
1108          unloadtf_t *f
1109   )
1110   {
1111 <        register AMBVAL  *av;
1112 <        register int  i;
1111 >        AMBVAL  *av;
1112 >        int  i;
1113                                          /* transfer values at this node */
1114          for (av = at->alist; av != NULL; av = at->alist) {
1115                  at->alist = av->next;
1116 +                av->next = NULL;
1117                  (*f)(av);
1118          }
1119          if (at->kid == NULL)
# Line 694 | Line 1135 | static int     i_avlist;               /* index for lists */
1135   static int alatcmp(const void *av1, const void *av2);
1136  
1137   static void
1138 + avfree(AMBVAL *av)
1139 + {
1140 +        free(av);
1141 + }
1142 +
1143 + static void
1144   av2list(
1145 <        void *av
1145 >        AMBVAL *av
1146   )
1147   {
1148   #ifdef DEBUG
# Line 703 | Line 1150 | av2list(
1150                  error(CONSISTENCY, "too many ambient values in av2list1");
1151   #endif
1152          avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av;
1153 <        avlist1[i_avlist++].t = ((AMBVAL*)av)->latick;
1153 >        avlist1[i_avlist++].t = av->latick;
1154   }
1155  
1156  
# Line 713 | Line 1160 | alatcmp(                       /* compare ambient values for MRA */
1160          const void *av2
1161   )
1162   {
1163 <        register long  lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t;
1163 >        long  lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t;
1164          return(lc<0 ? -1 : lc>0 ? 1 : 0);
1165   }
1166  
# Line 730 | Line 1177 | aposcmp(                       /* compare ambient value positions */
1177          const void      *avp2
1178   )
1179   {
1180 <        register long   diff = *(char * const *)avp1 - *(char * const *)avp2;
1180 >        long    diff = *(char * const *)avp1 - *(char * const *)avp2;
1181          if (diff < 0)
1182                  return(-1);
1183          return(diff > 0);
1184   }
1185  
1186 < #if 1
1186 >
1187   static int
1188   avlmemi(                                /* find list position from address */
1189          AMBVAL  *avaddr
1190   )
1191   {
1192 <        register AMBVAL  **avlpp;
1192 >        AMBVAL  **avlpp;
1193  
1194 <        avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
1194 >        avlpp = (AMBVAL **)bsearch(&avaddr, avlist2,
1195                          nambvals, sizeof(AMBVAL *), aposcmp);
1196          if (avlpp == NULL)
1197                  error(CONSISTENCY, "address not found in avlmemi");
1198          return(avlpp - avlist2);
1199   }
753 #else
754 #define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \
755                                nambvals,sizeof(AMBVAL *),aposcmp) - avlist2)
756 #endif
1200  
1201  
1202   static void
# Line 763 | Line 1206 | sortambvals(                   /* resort ambient values */
1206   {
1207          AMBTREE  oldatrunk;
1208          AMBVAL  tav, *tap, *pnext;
1209 <        register int    i, j;
1209 >        int     i, j;
1210                                          /* see if it's time yet */
1211          if (!always && (ambclock++ < lastsort+sortintvl ||
1212                          nambvals < SORT_THRESH))
# Line 792 | Line 1235 | sortambvals(                   /* resort ambient values */
1235          }
1236          if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
1237                  if (avlist2 != NULL)
1238 <                        free((void *)avlist2);
1238 >                        free(avlist2);
1239                  if (always) {           /* rebuild without sorting */
1240                          oldatrunk = atrunk;
1241                          atrunk.alist = NULL;
# Line 819 | Line 1262 | sortambvals(                   /* resort ambient values */
1262                  if (i_avlist < nambvals)
1263                          error(CONSISTENCY, "missing ambient values in sortambvals");
1264   #endif
1265 <                qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp);
1266 <                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
1265 >                qsort(avlist1, nambvals, sizeof(struct avl), alatcmp);
1266 >                qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
1267                  for (i = 0; i < nambvals; i++) {
1268                          if (avlist1[i].p == NULL)
1269                                  continue;
# Line 836 | Line 1279 | sortambvals(                   /* resort ambient values */
1279                          avinsert(avlist2[j]);
1280                          avlist1[j].p = NULL;
1281                  }
1282 <                free((void *)avlist1);
1283 <                free((void *)avlist2);
1282 >                free(avlist1);
1283 >                free(avlist2);
1284                                                  /* compute new sort interval */
1285                  sortintvl = ambclock - lastsort;
1286                  if (sortintvl >= MAX_SORT_INTVL/2)
# Line 863 | Line 1306 | aflock(                        /* lock/unlock ambient file */
1306   {
1307          static struct flock  fls;       /* static so initialized to zeroes */
1308  
1309 +        if (typ == fls.l_type)          /* already called? */
1310 +                return;
1311          fls.l_type = typ;
1312          if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
1313                  error(SYSTEM, "cannot (un)lock ambient file");
1314   }
1315  
1316  
1317 < extern int
1317 > int
1318   ambsync(void)                   /* synchronize ambient file */
1319   {
1320          long  flen;
1321          AMBVAL  avs;
1322 <        register int  n;
1322 >        int  n;
1323  
1324 <        if (nunflshed == 0)
1324 >        if (ambfp == NULL)      /* no ambient file? */
1325                  return(0);
1326 <        if (lastpos < 0)        /* initializing (locked in initambfile) */
1327 <                goto syncend;
883 <                                /* gain exclusive access */
884 <        aflock(F_WRLCK);
1326 >                                /* gain appropriate access */
1327 >        aflock(nunflshed ? F_WRLCK : F_RDLCK);
1328                                  /* see if file has grown */
1329          if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
1330                  goto seekerr;
1331 <        if ( (n = flen - lastpos) ) {           /* file has grown */
1332 <                if (ambinp == NULL) {           /* use duplicate filedes */
1333 <                        ambinp = fdopen(dup(fileno(ambfp)), "r");
1331 >        if ((n = flen - lastpos) > 0) {         /* file has grown */
1332 >                if (ambinp == NULL) {           /* get new file pointer */
1333 >                        ambinp = fopen(ambfile, "rb");
1334                          if (ambinp == NULL)
1335 <                                error(SYSTEM, "fdopen failed in ambsync");
1335 >                                error(SYSTEM, "fopen failed in ambsync");
1336                  }
1337 <                if (fseek(ambinp, lastpos, 0) < 0)
1337 >                if (fseek(ambinp, lastpos, SEEK_SET) < 0)
1338                          goto seekerr;
1339                  while (n >= AMBVALSIZ) {        /* load contributed values */
1340                          if (!readambval(&avs, ambinp)) {
# Line 901 | Line 1344 | ambsync(void)                  /* synchronize ambient file */
1344                                  error(WARNING, errmsg);
1345                                  break;
1346                          }
1347 <                        avinsert(avstore(&avs));
1347 >                        avstore(&avs);
1348                          n -= AMBVALSIZ;
1349                  }
1350 <                /*** seek always as safety measure
1351 <                if (n) ***/                     /* alignment */
1352 <                        if (lseek(fileno(ambfp), (off_t)(flen-n), SEEK_SET) < 0)
910 <                                goto seekerr;
1350 >                lastpos = flen - n;             /* check alignment */
1351 >                if (n && lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1352 >                        goto seekerr;
1353          }
912 #ifdef  DEBUG
913        if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) {
914                sprintf(errmsg, "ambient file buffer at %d rather than %d",
915                                ambfp->_ptr - ambfp->_base,
916                                nunflshed*AMBVALSIZ);
917                error(CONSISTENCY, errmsg);
918        }
919 #endif
920 syncend:
1354          n = fflush(ambfp);                      /* calls write() at last */
1355 <        if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
923 <                goto seekerr;
1355 >        lastpos += (long)nunflshed*AMBVALSIZ;
1356          aflock(F_UNLCK);                        /* release file */
1357          nunflshed = 0;
1358          return(n);
1359   seekerr:
1360          error(SYSTEM, "seek failed in ambsync");
1361 <        return -1; /* pro forma return */
1361 >        return(EOF);    /* pro forma return */
1362   }
1363  
1364 < #else
1364 > #else   /* ! F_SETLKW */
1365  
1366 < extern int
1366 > int
1367   ambsync(void)                   /* flush ambient file */
1368   {
1369 <        if (nunflshed == 0)
1369 >        if (ambfp == NULL)
1370                  return(0);
1371          nunflshed = 0;
1372          return(fflush(ambfp));
1373   }
1374  
1375 < #endif
1375 > #endif  /* ! F_SETLKW */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines