140 |
|
setambient(void) /* initialize calculation */ |
141 |
|
{ |
142 |
|
int readonly = 0; |
143 |
< |
long pos, flen; |
143 |
> |
long flen; |
144 |
|
AMBVAL amb; |
145 |
|
/* make sure we're fresh */ |
146 |
|
ambdone(); |
160 |
|
readonly = (ambfp = fopen(ambfile, "r")) != NULL; |
161 |
|
if (ambfp != NULL) { |
162 |
|
initambfile(0); /* file exists */ |
163 |
< |
pos = ftell(ambfp); |
163 |
> |
lastpos = ftell(ambfp); |
164 |
|
while (readambval(&amb, ambfp)) |
165 |
|
avinsert(avstore(&amb)); |
166 |
|
nambshare = nambvals; /* share loaded values */ |
174 |
|
return; /* avoid ambsync() */ |
175 |
|
} |
176 |
|
/* align file pointer */ |
177 |
< |
pos += (long)nambvals*AMBVALSIZ; |
177 |
> |
lastpos += (long)nambvals*AMBVALSIZ; |
178 |
|
flen = lseek(fileno(ambfp), (off_t)0, SEEK_END); |
179 |
< |
if (flen != pos) { |
179 |
> |
if (flen != lastpos) { |
180 |
|
sprintf(errmsg, |
181 |
|
"ignoring last %ld values in ambient file (corrupted)", |
182 |
< |
(flen - pos)/AMBVALSIZ); |
182 |
> |
(flen - lastpos)/AMBVALSIZ); |
183 |
|
error(WARNING, errmsg); |
184 |
< |
fseek(ambfp, pos, 0); |
184 |
> |
fseek(ambfp, lastpos, SEEK_SET); |
185 |
|
#ifndef _WIN32 /* XXX we need a replacement for that one */ |
186 |
< |
ftruncate(fileno(ambfp), (off_t)pos); |
186 |
> |
ftruncate(fileno(ambfp), (off_t)lastpos); |
187 |
|
#endif |
188 |
|
} |
189 |
|
} else if ((ambfp = fopen(ambfile, "w+")) != NULL) { |
190 |
|
initambfile(1); /* else create new file */ |
191 |
+ |
lastpos = ftell(ambfp); |
192 |
|
} else { |
193 |
|
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
194 |
|
error(SYSTEM, errmsg); |
195 |
|
} |
196 |
< |
nunflshed++; /* lie */ |
196 |
< |
ambsync(); |
196 |
> |
ambsync(); /* load previous values */ |
197 |
|
} |
198 |
|
|
199 |
|
|
275 |
|
goto dumbamb; |
276 |
|
|
277 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
278 |
+ |
copycolor(acol, aval); |
279 |
|
rdepth++; |
280 |
< |
d = doambient(acol, r, aval, intens(aval)*r->rweight, |
280 |
< |
NULL, NULL); |
280 |
> |
d = doambient(acol, r, r->rweight, NULL, NULL); |
281 |
|
rdepth--; |
282 |
|
if (d <= FTINY) |
283 |
|
goto dumbamb; |
284 |
< |
multcolor(aval, acol); |
284 |
> |
copycolor(aval, acol); |
285 |
|
return; |
286 |
|
} |
287 |
|
|
288 |
|
if (tracktime) /* sort to minimize thrashing */ |
289 |
|
sortambvals(0); |
290 |
< |
/* get ambient value */ |
290 |
> |
/* interpolate ambient value */ |
291 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
292 |
|
d = sumambient(acol, r, nrm, rdepth, |
293 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
294 |
|
if (d > FTINY) { |
295 |
< |
scalecolor(acol, 1.0/d); |
295 |
> |
d = 1.0/d; |
296 |
> |
scalecolor(acol, d); |
297 |
|
multcolor(aval, acol); |
298 |
|
return; |
299 |
|
} |
300 |
|
rdepth++; /* need to cache new value */ |
301 |
< |
d = makeambient(acol, r, aval, nrm, rdepth-1); |
301 |
> |
d = makeambient(acol, r, nrm, rdepth-1); |
302 |
|
rdepth--; |
303 |
|
if (d > FTINY) { |
304 |
|
multcolor(aval, acol); /* got new value */ |
352 |
|
*/ |
353 |
|
if (av->lvl > al) /* list sorted, so this works */ |
354 |
|
break; |
355 |
< |
if (av->weight < r->rweight-FTINY) |
355 |
> |
if (av->weight < 0.9*r->rweight) |
356 |
|
continue; |
357 |
|
/* |
358 |
|
* Ambient radius test. |
431 |
|
break; |
432 |
|
} |
433 |
|
if (j == 3) |
434 |
< |
wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s); |
434 |
> |
wsum += sumambient(acol, r, rn, al, |
435 |
> |
at->kid+i, ck0, s); |
436 |
|
} |
437 |
|
return(wsum); |
438 |
|
} |
439 |
|
|
440 |
|
|
441 |
|
extern double |
442 |
< |
makeambient( /* make a new ambient value */ |
442 |
> |
makeambient( /* make a new ambient value for storage */ |
443 |
|
COLOR acol, |
444 |
|
RAY *r, |
443 |
– |
COLOR ac, |
445 |
|
FVECT rn, |
446 |
|
int al |
447 |
|
) |
448 |
|
{ |
449 |
|
AMBVAL amb; |
449 |
– |
double coef; |
450 |
|
FVECT gp, gd; |
451 |
< |
/* compute weight */ |
452 |
< |
amb.weight = pow(AVGREFL, (double)al); |
453 |
< |
coef = intens(ac)*r->rweight; |
454 |
< |
if (coef < 0.1*amb.weight) /* heuristic */ |
455 |
< |
amb.weight = coef; |
451 |
> |
int i; |
452 |
> |
|
453 |
> |
amb.weight = 1.0; /* compute weight */ |
454 |
> |
for (i = al; i-- > 0; ) |
455 |
> |
amb.weight *= AVGREFL; |
456 |
> |
if (r->rweight < 0.1*amb.weight) /* heuristic override */ |
457 |
> |
amb.weight = 1.25*r->rweight; |
458 |
> |
setcolor(acol, AVGREFL, AVGREFL, AVGREFL); |
459 |
|
/* compute ambient */ |
460 |
< |
amb.rad = doambient(acol, r, ac, amb.weight, gp, gd); |
461 |
< |
if (amb.rad <= FTINY) |
460 |
> |
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
461 |
> |
if (amb.rad <= FTINY) { |
462 |
> |
setcolor(acol, 0.0, 0.0, 0.0); |
463 |
|
return(0.0); |
464 |
< |
/* store it */ |
464 |
> |
} |
465 |
> |
scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */ |
466 |
> |
/* store value */ |
467 |
|
VCOPY(amb.pos, r->rop); |
468 |
|
VCOPY(amb.dir, r->ron); |
469 |
|
amb.lvl = al; |
864 |
|
{ |
865 |
|
static struct flock fls; /* static so initialized to zeroes */ |
866 |
|
|
867 |
+ |
if (typ == fls.l_type) /* already called? */ |
868 |
+ |
return; |
869 |
|
fls.l_type = typ; |
870 |
|
if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) |
871 |
|
error(SYSTEM, "cannot (un)lock ambient file"); |
879 |
|
AMBVAL avs; |
880 |
|
register int n; |
881 |
|
|
882 |
< |
if (nunflshed == 0) |
882 |
> |
if (ambfp == NULL) /* no ambient file? */ |
883 |
|
return(0); |
884 |
< |
if (lastpos < 0) /* initializing (locked in initambfile) */ |
885 |
< |
goto syncend; |
878 |
< |
/* gain exclusive access */ |
879 |
< |
aflock(F_WRLCK); |
884 |
> |
/* gain appropriate access */ |
885 |
> |
aflock(nunflshed ? F_WRLCK : F_RDLCK); |
886 |
|
/* see if file has grown */ |
887 |
|
if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0) |
888 |
|
goto seekerr; |
892 |
|
if (ambinp == NULL) |
893 |
|
error(SYSTEM, "fdopen failed in ambsync"); |
894 |
|
} |
895 |
< |
if (fseek(ambinp, lastpos, 0) < 0) |
895 |
> |
if (fseek(ambinp, lastpos, SEEK_SET) < 0) |
896 |
|
goto seekerr; |
897 |
|
while (n >= AMBVALSIZ) { /* load contributed values */ |
898 |
|
if (!readambval(&avs, ambinp)) { |
905 |
|
avinsert(avstore(&avs)); |
906 |
|
n -= AMBVALSIZ; |
907 |
|
} |
908 |
+ |
lastpos = flen - n; |
909 |
|
/*** seek always as safety measure |
910 |
|
if (n) ***/ /* alignment */ |
911 |
< |
if (lseek(fileno(ambfp), (off_t)(flen-n), SEEK_SET) < 0) |
911 |
> |
if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) |
912 |
|
goto seekerr; |
913 |
|
} |
914 |
|
#ifdef DEBUG |
919 |
|
error(CONSISTENCY, errmsg); |
920 |
|
} |
921 |
|
#endif |
915 |
– |
syncend: |
922 |
|
n = fflush(ambfp); /* calls write() at last */ |
923 |
< |
if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) |
923 |
> |
if (n != EOF) |
924 |
> |
lastpos += (long)nunflshed*AMBVALSIZ; |
925 |
> |
else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) |
926 |
|
goto seekerr; |
927 |
+ |
|
928 |
|
aflock(F_UNLCK); /* release file */ |
929 |
|
nunflshed = 0; |
930 |
|
return(n); |
938 |
|
extern int |
939 |
|
ambsync(void) /* flush ambient file */ |
940 |
|
{ |
941 |
< |
if (nunflshed == 0) |
941 |
> |
if (ambfp == NULL) |
942 |
|
return(0); |
943 |
|
nunflshed = 0; |
944 |
|
return(fflush(ambfp)); |