33 |
|
static AMBTREE atrunk; /* our ambient trunk node */ |
34 |
|
|
35 |
|
static FILE *ambfp = NULL; /* ambient file pointer */ |
36 |
< |
static int nunflshed = 0; /* number of unflushed ambient values */ |
36 |
> |
static int nunflshed; /* number of unflushed ambient values */ |
37 |
|
|
38 |
|
static double avsum = 0.; /* computed ambient value sum (log) */ |
39 |
|
static unsigned int navsum = 0; /* number of values in avsum */ |
40 |
|
static unsigned int nambvals = 0; /* total number of indirect values */ |
41 |
– |
static unsigned int nambshare = 0; /* number of values from file */ |
41 |
|
static FILE *ambinp = NULL; /* auxiliary file for input */ |
42 |
< |
static long lastpos = -1; /* last flush position */ |
42 |
> |
static off_t lastpos = -1; /* last flush position */ |
43 |
|
|
44 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
45 |
|
|
116 |
|
setambient(void) /* initialize calculation */ |
117 |
|
{ |
118 |
|
int readonly = 0; |
119 |
< |
long flen; |
119 |
> |
off_t flen; |
120 |
|
AMBVAL amb; |
121 |
|
/* make sure we're fresh */ |
122 |
|
ambdone(); |
136 |
|
readonly = (ambfp = fopen(ambfile, "r")) != NULL; |
137 |
|
if (ambfp != NULL) { |
138 |
|
initambfile(0); /* file exists */ |
139 |
< |
lastpos = ftell(ambfp); |
139 |
> |
lastpos = ftello(ambfp); |
140 |
|
while (readambval(&amb, ambfp)) |
141 |
|
avstore(&amb); |
143 |
– |
nambshare = nambvals; /* share loaded values */ |
142 |
|
if (readonly) { |
143 |
|
sprintf(errmsg, |
144 |
|
"loaded %u values from read-only ambient file", |
149 |
|
return; /* avoid ambsync() */ |
150 |
|
} |
151 |
|
/* align file pointer */ |
152 |
< |
lastpos += (long)nambvals*AMBVALSIZ; |
153 |
< |
flen = lseek(fileno(ambfp), (off_t)0, SEEK_END); |
152 |
> |
lastpos += (off_t)nambvals*AMBVALSIZ; |
153 |
> |
flen = lseek(fileno(ambfp), 0, SEEK_END); |
154 |
|
if (flen != lastpos) { |
155 |
|
sprintf(errmsg, |
156 |
|
"ignoring last %ld values in ambient file (corrupted)", |
157 |
|
(flen - lastpos)/AMBVALSIZ); |
158 |
|
error(WARNING, errmsg); |
159 |
< |
fseek(ambfp, lastpos, SEEK_SET); |
160 |
< |
ftruncate(fileno(ambfp), (off_t)lastpos); |
159 |
> |
fseeko(ambfp, lastpos, SEEK_SET); |
160 |
> |
ftruncate(fileno(ambfp), lastpos); |
161 |
|
} |
162 |
|
} else if ((ambfp = fopen(ambfile, "w+")) != NULL) { |
163 |
|
initambfile(1); /* else create new file */ |
164 |
|
fflush(ambfp); |
165 |
< |
lastpos = ftell(ambfp); |
165 |
> |
lastpos = ftello(ambfp); |
166 |
|
} else { |
167 |
|
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
168 |
|
error(SYSTEM, errmsg); |
192 |
|
avsum = 0.; |
193 |
|
navsum = 0; |
194 |
|
nambvals = 0; |
197 |
– |
nambshare = 0; |
195 |
|
} |
196 |
|
|
197 |
|
|
379 |
|
rtst.rmax = normalize(rtst.rdir); /* short ray test */ |
380 |
|
while (localhit(&rtst, &thescene)) { /* check for occluder */ |
381 |
|
OBJREC *m = findmaterial(rtst.ro); |
382 |
< |
if (m != NULL && !istransp(m->otype) && !isBSDFproxy(m) && |
382 |
> |
if (m != NULL && !istransp(m) && !isBSDFproxy(m) && |
383 |
|
(rtst.clipset == NULL || |
384 |
|
!inset(rtst.clipset, rtst.ro->omod))) |
385 |
|
return(1); /* plug light leak */ |
627 |
|
{ |
628 |
|
extern char *progname, *octname; |
629 |
|
static char *mybuf = NULL; |
630 |
+ |
int ntries = 3; |
631 |
|
|
632 |
+ |
if (!AMBFLUSH) |
633 |
+ |
error(INTERNAL, "BUFSIZ too small in initambfile"); |
634 |
|
#ifdef F_SETLKW |
635 |
|
aflock(cre8 ? F_WRLCK : F_RDLCK); |
636 |
|
#endif |
638 |
|
if (mybuf == NULL) |
639 |
|
mybuf = (char *)bmalloc(BUFSIZ+8); |
640 |
|
setbuf(ambfp, mybuf); |
641 |
+ |
nunflshed = 0; |
642 |
|
retry: |
643 |
|
if (cre8) { /* new file */ |
644 |
|
newheader("RADIANCE", ambfp); |
666 |
|
fputc('\n', ambfp); |
667 |
|
putambmagic(ambfp); |
668 |
|
} else if (getheader(ambfp, amb_headline, NULL) < 0 || !hasambmagic(ambfp)) { |
668 |
– |
#ifndef F_SETLKW |
669 |
– |
static int ntries = 3; |
669 |
|
if (--ntries > 0 && ftell(ambfp) == 0) { |
670 |
+ |
#ifdef F_SETLKW |
671 |
+ |
aflock(F_UNLCK); |
672 |
|
clearerr(ambfp); |
673 |
|
sleep(2); |
674 |
+ |
aflock(F_RDLCK); |
675 |
+ |
#else |
676 |
+ |
clearerr(ambfp); |
677 |
+ |
sleep(2); |
678 |
+ |
#endif |
679 |
|
goto retry; |
680 |
|
} |
675 |
– |
#endif |
681 |
|
error(USER, "bad/incompatible ambient file"); |
682 |
|
} |
683 |
|
if ((AMB_CNDX != CNDX) | (AMB_WLPART != WLPART)) { |
838 |
|
int |
839 |
|
ambsync(void) /* synchronize ambient file */ |
840 |
|
{ |
841 |
< |
long flen; |
841 |
> |
off_t flen; |
842 |
|
AMBVAL avs; |
843 |
|
int n; |
844 |
|
|
847 |
|
/* gain appropriate access */ |
848 |
|
aflock(nunflshed ? F_WRLCK : F_RDLCK); |
849 |
|
/* see if file has grown */ |
850 |
< |
if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0) |
850 |
> |
if ((flen = lseek(fileno(ambfp), 0, SEEK_END)) < 0) |
851 |
|
goto seekerr; |
852 |
|
if ((n = flen - lastpos) > 0) { /* file has grown */ |
853 |
|
if (ambinp == NULL) { /* get new file pointer */ |
855 |
|
if (ambinp == NULL) |
856 |
|
error(SYSTEM, "fopen failed in ambsync"); |
857 |
|
} |
858 |
< |
if (fseek(ambinp, lastpos, SEEK_SET) < 0) |
858 |
> |
if (fseeko(ambinp, lastpos, SEEK_SET) < 0) |
859 |
|
goto seekerr; |
860 |
|
while (n >= AMBVALSIZ) { /* load contributed values */ |
861 |
|
if (!readambval(&avs, ambinp)) { |
869 |
|
n -= AMBVALSIZ; |
870 |
|
} |
871 |
|
lastpos = flen - n; /* check alignment */ |
872 |
< |
if (n && lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) |
872 |
> |
if (n && lseek(fileno(ambfp), lastpos, SEEK_SET) < 0) |
873 |
|
goto seekerr; |
874 |
|
} |
875 |
|
n = fflush(ambfp); /* calls write() at last */ |
876 |
< |
lastpos += (long)nunflshed*AMBVALSIZ; |
876 |
> |
lastpos += (off_t)nunflshed*AMBVALSIZ; |
877 |
|
aflock(F_UNLCK); /* release file */ |
878 |
|
nunflshed = 0; |
879 |
|
return(n); |