276 |
|
{ |
277 |
|
static int rdepth = 0; /* ambient recursion */ |
278 |
|
COLOR acol, caustic; |
279 |
< |
int ok; |
279 |
> |
int i, ok; |
280 |
|
double d, l; |
281 |
|
|
282 |
|
/* PMAP: Factor in ambient from photon map, if enabled and ray is |
302 |
|
goto dumbamb; |
303 |
|
|
304 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
305 |
+ |
FVECT uvd[2]; |
306 |
+ |
float dgrad[2], *dgp = NULL; |
307 |
+ |
|
308 |
+ |
if (nrm != r->ron && DOT(nrm,r->ron) < 0.9999) |
309 |
+ |
dgp = dgrad; /* compute rotational grad. */ |
310 |
|
copycolor(acol, aval); |
311 |
|
rdepth++; |
312 |
|
ok = doambient(acol, r, r->rweight, |
313 |
< |
NULL, NULL, NULL, NULL, NULL); |
313 |
> |
uvd, NULL, NULL, dgp, NULL); |
314 |
|
rdepth--; |
315 |
|
if (!ok) |
316 |
|
goto dumbamb; |
317 |
+ |
if ((ok > 0) & (dgp != NULL)) { /* apply texture */ |
318 |
+ |
FVECT v1; |
319 |
+ |
VCROSS(v1, r->ron, nrm); |
320 |
+ |
d = 1.0; |
321 |
+ |
for (i = 3; i--; ) |
322 |
+ |
d += v1[i] * (dgp[0]*uvd[0][i] + dgp[1]*uvd[1][i]); |
323 |
+ |
if (d >= 0.05) |
324 |
+ |
scalecolor(acol, d); |
325 |
+ |
} |
326 |
|
copycolor(aval, acol); |
327 |
|
|
328 |
|
/* PMAP: add in caustic */ |
1327 |
|
if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0) |
1328 |
|
goto seekerr; |
1329 |
|
if ((n = flen - lastpos) > 0) { /* file has grown */ |
1330 |
< |
if (ambinp == NULL) { /* use duplicate filedes */ |
1331 |
< |
ambinp = fdopen(dup(fileno(ambfp)), "r"); |
1330 |
> |
if (ambinp == NULL) { /* get new file pointer */ |
1331 |
> |
ambinp = fopen(ambfile, "rb"); |
1332 |
|
if (ambinp == NULL) |
1333 |
< |
error(SYSTEM, "fdopen failed in ambsync"); |
1333 |
> |
error(SYSTEM, "fopen failed in ambsync"); |
1334 |
|
} |
1335 |
|
if (fseek(ambinp, lastpos, SEEK_SET) < 0) |
1336 |
|
goto seekerr; |
1345 |
|
avstore(&avs); |
1346 |
|
n -= AMBVALSIZ; |
1347 |
|
} |
1348 |
< |
lastpos = flen - n; |
1349 |
< |
/*** seek always as safety measure |
1350 |
< |
if (n) ***/ /* alignment */ |
1337 |
< |
if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) |
1338 |
< |
goto seekerr; |
1348 |
> |
lastpos = flen - n; /* check alignment */ |
1349 |
> |
if (n && lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) |
1350 |
> |
goto seekerr; |
1351 |
|
} |
1352 |
|
n = fflush(ambfp); /* calls write() at last */ |
1353 |
< |
if (n != EOF) |
1342 |
< |
lastpos += (long)nunflshed*AMBVALSIZ; |
1343 |
< |
else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) |
1344 |
< |
goto seekerr; |
1345 |
< |
|
1353 |
> |
lastpos += (long)nunflshed*AMBVALSIZ; |
1354 |
|
aflock(F_UNLCK); /* release file */ |
1355 |
|
nunflshed = 0; |
1356 |
|
return(n); |
1357 |
|
seekerr: |
1358 |
|
error(SYSTEM, "seek failed in ambsync"); |
1359 |
< |
return -1; /* pro forma return */ |
1359 |
> |
return(EOF); /* pro forma return */ |
1360 |
|
} |
1361 |
|
|
1362 |
|
#else /* ! F_SETLKW */ |