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 |
|
|
253 |
|
|
254 |
|
|
255 |
|
extern void |
256 |
< |
ambient( /* compute ambient component for ray */ |
257 |
< |
COLOR acol, |
256 |
> |
multambient( /* compute ambient component & multiply by coef. */ |
257 |
> |
COLOR aval, |
258 |
|
register RAY *r, |
259 |
|
FVECT nrm |
260 |
|
) |
261 |
|
{ |
262 |
|
static int rdepth = 0; /* ambient recursion */ |
263 |
+ |
COLOR acol; |
264 |
|
double d, l; |
265 |
|
|
266 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
274 |
|
goto dumbamb; |
275 |
|
|
276 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
277 |
+ |
copycolor(acol, aval); |
278 |
|
rdepth++; |
279 |
|
d = doambient(acol, r, r->rweight, NULL, NULL); |
280 |
|
rdepth--; |
281 |
|
if (d <= FTINY) |
282 |
|
goto dumbamb; |
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, 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, nrm, rdepth-1); |
301 |
|
rdepth--; |
302 |
< |
if (d > FTINY) |
302 |
> |
if (d > FTINY) { |
303 |
> |
multcolor(aval, acol); /* got new value */ |
304 |
|
return; |
305 |
+ |
} |
306 |
|
dumbamb: /* return global value */ |
307 |
< |
copycolor(acol, ambval); |
308 |
< |
if ((ambvwt <= 0) | (navsum == 0)) |
307 |
> |
if ((ambvwt <= 0) | (navsum == 0)) { |
308 |
> |
multcolor(aval, ambval); |
309 |
|
return; |
310 |
+ |
} |
311 |
|
l = bright(ambval); /* average in computations */ |
312 |
|
if (l > FTINY) { |
313 |
|
d = (log(l)*(double)ambvwt + avsum) / |
314 |
|
(double)(ambvwt + navsum); |
315 |
|
d = exp(d) / l; |
316 |
< |
scalecolor(acol, d); /* apply color of ambval */ |
316 |
> |
scalecolor(aval, d); |
317 |
> |
multcolor(aval, ambval); /* apply color of ambval */ |
318 |
|
} else { |
319 |
|
d = exp( avsum / (double)navsum ); |
320 |
< |
setcolor(acol, d, d, d); /* neutral color */ |
320 |
> |
scalecolor(aval, d); /* neutral color */ |
321 |
|
} |
322 |
|
} |
323 |
|
|
351 |
|
*/ |
352 |
|
if (av->lvl > al) /* list sorted, so this works */ |
353 |
|
break; |
354 |
< |
if (av->weight < r->rweight-FTINY) |
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, rn, al, at->kid+i, ck0, s); |
433 |
> |
wsum += sumambient(acol, r, rn, al, |
434 |
> |
at->kid+i, ck0, s); |
435 |
|
} |
436 |
|
return(wsum); |
437 |
|
} |
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 |
< |
register RAY *r, |
443 |
> |
RAY *r, |
444 |
|
FVECT rn, |
445 |
|
int al |
446 |
|
) |
447 |
|
{ |
448 |
|
AMBVAL amb; |
449 |
|
FVECT gp, gd; |
450 |
< |
/* compute weight */ |
451 |
< |
amb.weight = pow(AVGREFL, (double)al); |
452 |
< |
if (r->rweight < 0.1*amb.weight) /* heuristic */ |
453 |
< |
amb.weight = r->rweight; |
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, amb.weight, gp, gd); |
460 |
< |
if (amb.rad <= FTINY) |
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 |
|
|
879 |
< |
if (nunflshed == 0) |
879 |
> |
if (ambfp == NULL) /* no ambient file? */ |
880 |
|
return(0); |
881 |
|
if (lastpos < 0) /* initializing (locked in initambfile) */ |
882 |
|
goto syncend; |
883 |
< |
/* gain exclusive access */ |
884 |
< |
aflock(F_WRLCK); |
883 |
> |
/* gain appropriate access */ |
884 |
> |
aflock(nunflshed ? F_WRLCK : F_RDLCK); |
885 |
|
/* see if file has grown */ |
886 |
|
if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0) |
887 |
|
goto seekerr; |
904 |
|
avinsert(avstore(&avs)); |
905 |
|
n -= AMBVALSIZ; |
906 |
|
} |
907 |
+ |
lastpos = flen - n; |
908 |
|
/*** seek always as safety measure |
909 |
|
if (n) ***/ /* alignment */ |
910 |
< |
if (lseek(fileno(ambfp), (off_t)(flen-n), SEEK_SET) < 0) |
910 |
> |
if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) |
911 |
|
goto seekerr; |
912 |
|
} |
913 |
|
#ifdef DEBUG |
920 |
|
#endif |
921 |
|
syncend: |
922 |
|
n = fflush(ambfp); /* calls write() at last */ |
923 |
< |
if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) |
924 |
< |
goto seekerr; |
923 |
> |
if ((n == EOF) | (lastpos < 0)) { |
924 |
> |
if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) |
925 |
> |
goto seekerr; |
926 |
> |
} else |
927 |
> |
lastpos += (long)nunflshed*AMBVALSIZ; |
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)); |