192 |
|
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
193 |
|
error(SYSTEM, errmsg); |
194 |
|
} |
195 |
< |
nunflshed++; /* lie */ |
196 |
< |
ambsync(); |
195 |
> |
ambsync(); /* load previous values */ |
196 |
|
} |
197 |
|
|
198 |
|
|
274 |
|
goto dumbamb; |
275 |
|
|
276 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
277 |
+ |
copycolor(acol, aval); |
278 |
|
rdepth++; |
279 |
< |
d = doambient(acol, r, aval, intens(aval)*r->rweight, |
280 |
< |
NULL, NULL); |
279 |
> |
d = doambient(acol, r, r->rweight, NULL, NULL); |
280 |
|
rdepth--; |
281 |
|
if (d <= FTINY) |
282 |
|
goto dumbamb; |
283 |
< |
multcolor(aval, acol); |
283 |
> |
copycolor(aval, acol); |
284 |
|
return; |
285 |
|
} |
286 |
|
|
287 |
|
if (tracktime) /* sort to minimize thrashing */ |
288 |
|
sortambvals(0); |
289 |
< |
/* get ambient value */ |
289 |
> |
/* interpolate ambient value */ |
290 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
291 |
< |
d = sumambient(acol, r, intens(aval)*r->rweight, nrm, rdepth, |
291 |
> |
d = sumambient(acol, r, nrm, rdepth, |
292 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
293 |
|
if (d > FTINY) { |
294 |
< |
scalecolor(acol, 1.0/d); |
294 |
> |
d = 1.0/d; |
295 |
> |
scalecolor(acol, d); |
296 |
|
multcolor(aval, acol); |
297 |
|
return; |
298 |
|
} |
299 |
|
rdepth++; /* need to cache new value */ |
300 |
< |
d = makeambient(acol, r, aval, nrm, rdepth-1); |
300 |
> |
d = makeambient(acol, r, nrm, rdepth-1); |
301 |
|
rdepth--; |
302 |
|
if (d > FTINY) { |
303 |
|
multcolor(aval, acol); /* got new value */ |
326 |
|
sumambient( /* get interpolated ambient value */ |
327 |
|
COLOR acol, |
328 |
|
register RAY *r, |
329 |
– |
double rw, |
329 |
|
FVECT rn, |
330 |
|
int al, |
331 |
|
AMBTREE *at, |
351 |
|
*/ |
352 |
|
if (av->lvl > al) /* list sorted, so this works */ |
353 |
|
break; |
354 |
< |
if (av->weight < 0.9*rw) |
354 |
> |
if (av->weight < 0.9*r->rweight) |
355 |
|
continue; |
356 |
|
/* |
357 |
|
* Ambient radius test. |
430 |
|
break; |
431 |
|
} |
432 |
|
if (j == 3) |
433 |
< |
wsum += sumambient(acol, r, rw, rn, al, |
433 |
> |
wsum += sumambient(acol, r, rn, al, |
434 |
|
at->kid+i, ck0, s); |
435 |
|
} |
436 |
|
return(wsum); |
438 |
|
|
439 |
|
|
440 |
|
extern double |
441 |
< |
makeambient( /* make a new ambient value */ |
441 |
> |
makeambient( /* make a new ambient value for storage */ |
442 |
|
COLOR acol, |
443 |
|
RAY *r, |
445 |
– |
COLOR ac, |
444 |
|
FVECT rn, |
445 |
|
int al |
446 |
|
) |
447 |
|
{ |
448 |
|
AMBVAL amb; |
451 |
– |
double coef; |
449 |
|
FVECT gp, gd; |
450 |
< |
/* compute weight */ |
451 |
< |
amb.weight = pow(AVGREFL, (double)al); |
452 |
< |
coef = intens(ac)*r->rweight; |
453 |
< |
if (coef < 0.1*amb.weight) /* heuristic */ |
454 |
< |
amb.weight = coef; |
450 |
> |
int i; |
451 |
> |
|
452 |
> |
amb.weight = 1.0; /* compute weight */ |
453 |
> |
for (i = al; i-- > 0; ) |
454 |
> |
amb.weight *= AVGREFL; |
455 |
> |
if (r->rweight < 0.1*amb.weight) /* heuristic override */ |
456 |
> |
amb.weight = 1.25*r->rweight; |
457 |
> |
setcolor(acol, AVGREFL, AVGREFL, AVGREFL); |
458 |
|
/* compute ambient */ |
459 |
< |
amb.rad = doambient(acol, r, ac, amb.weight, gp, gd); |
460 |
< |
if (amb.rad <= FTINY) |
459 |
> |
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
460 |
> |
if (amb.rad <= FTINY) { |
461 |
> |
setcolor(acol, 0.0, 0.0, 0.0); |
462 |
|
return(0.0); |
463 |
< |
/* store it */ |
463 |
> |
} |
464 |
> |
scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */ |
465 |
> |
/* store value */ |
466 |
|
VCOPY(amb.pos, r->rop); |
467 |
|
VCOPY(amb.dir, r->ron); |
468 |
|
amb.lvl = al; |
876 |
|
AMBVAL avs; |
877 |
|
register int n; |
878 |
|
|
876 |
– |
if (nunflshed == 0) |
877 |
– |
return(0); |
879 |
|
if (lastpos < 0) /* initializing (locked in initambfile) */ |
880 |
|
goto syncend; |
881 |
< |
/* gain exclusive access */ |
882 |
< |
aflock(F_WRLCK); |
881 |
> |
/* gain appropriate access */ |
882 |
> |
aflock(nunflshed ? F_WRLCK : F_RDLCK); |
883 |
|
/* see if file has grown */ |
884 |
|
if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0) |
885 |
|
goto seekerr; |
902 |
|
avinsert(avstore(&avs)); |
903 |
|
n -= AMBVALSIZ; |
904 |
|
} |
905 |
+ |
lastpos = flen - n; |
906 |
|
/*** seek always as safety measure |
907 |
|
if (n) ***/ /* alignment */ |
908 |
< |
if (lseek(fileno(ambfp), (off_t)(flen-n), SEEK_SET) < 0) |
908 |
> |
if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) |
909 |
|
goto seekerr; |
910 |
|
} |
911 |
|
#ifdef DEBUG |
918 |
|
#endif |
919 |
|
syncend: |
920 |
|
n = fflush(ambfp); /* calls write() at last */ |
921 |
< |
if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) |
922 |
< |
goto seekerr; |
921 |
> |
if (n == EOF) { |
922 |
> |
if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) |
923 |
> |
goto seekerr; |
924 |
> |
} else |
925 |
> |
lastpos += (long)nunflshed*AMBVALSIZ; |
926 |
|
aflock(F_UNLCK); /* release file */ |
927 |
|
nunflshed = 0; |
928 |
|
return(n); |
936 |
|
extern int |
937 |
|
ambsync(void) /* flush ambient file */ |
938 |
|
{ |
934 |
– |
if (nunflshed == 0) |
935 |
– |
return(0); |
939 |
|
nunflshed = 0; |
940 |
|
return(fflush(ambfp)); |
941 |
|
} |