1 |
– |
#ifndef lint |
1 |
|
static const char RCSid[] = "$Id$"; |
3 |
– |
#endif |
2 |
|
/* |
3 |
|
* ambient.c - routines dealing with ambient (inter-reflected) component. |
4 |
|
* |
12 |
|
#include "platform.h" |
13 |
|
#include "ray.h" |
14 |
|
#include "otypes.h" |
15 |
+ |
#include "otspecial.h" |
16 |
|
#include "resolu.h" |
17 |
|
#include "ambient.h" |
18 |
+ |
#include "source.h" |
19 |
|
#include "random.h" |
20 |
+ |
#include "pmapamb.h" |
21 |
|
|
22 |
|
#ifndef OCTSCALE |
23 |
|
#define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ |
78 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
79 |
|
|
80 |
|
#define newambval() (AMBVAL *)malloc(sizeof(AMBVAL)) |
80 |
– |
#define freeav(av) free((void *)av); |
81 |
|
|
82 |
|
static void initambfile(int creat); |
83 |
|
static void avsave(AMBVAL *av); |
185 |
|
(flen - lastpos)/AMBVALSIZ); |
186 |
|
error(WARNING, errmsg); |
187 |
|
fseek(ambfp, lastpos, SEEK_SET); |
188 |
– |
#ifndef _WIN32 /* XXX we need a replacement for that one */ |
188 |
|
ftruncate(fileno(ambfp), (off_t)lastpos); |
190 |
– |
#endif |
189 |
|
} |
190 |
|
} else if ((ambfp = fopen(ambfile, "w+")) != NULL) { |
191 |
|
initambfile(1); /* else create new file */ |
195 |
|
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
196 |
|
error(SYSTEM, errmsg); |
197 |
|
} |
200 |
– |
#ifdef getc_unlocked |
201 |
– |
flockfile(ambfp); /* application-level lock */ |
202 |
– |
#endif |
198 |
|
#ifdef F_SETLKW |
199 |
|
aflock(F_UNLCK); /* release file */ |
200 |
|
#endif |
215 |
|
lastpos = -1; |
216 |
|
} |
217 |
|
/* free ambient tree */ |
218 |
< |
unloadatree(&atrunk, &avfree); |
218 |
> |
unloadatree(&atrunk, avfree); |
219 |
|
/* reset state variables */ |
220 |
|
avsum = 0.; |
221 |
|
navsum = 0; |
266 |
|
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
267 |
|
AMBTREE *at, FVECT c0, double s); |
268 |
|
static int makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
269 |
< |
static void extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
269 |
> |
static int extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
270 |
|
FVECT uvw[3]); |
271 |
|
|
272 |
|
void |
277 |
|
) |
278 |
|
{ |
279 |
|
static int rdepth = 0; /* ambient recursion */ |
280 |
< |
COLOR acol; |
281 |
< |
int ok; |
280 |
> |
COLOR acol, caustic; |
281 |
> |
int i, ok; |
282 |
|
double d, l; |
283 |
|
|
284 |
+ |
/* PMAP: Factor in ambient from photon map, if enabled and ray is |
285 |
+ |
* ambient. Return as all ambient components accounted for, else |
286 |
+ |
* continue. */ |
287 |
+ |
if (ambPmap(aval, r, rdepth)) |
288 |
+ |
return; |
289 |
+ |
|
290 |
+ |
/* PMAP: Factor in specular-diffuse ambient (caustics) from photon |
291 |
+ |
* map, if enabled and ray is primary, else caustic is zero. Continue |
292 |
+ |
* with RADIANCE ambient calculation */ |
293 |
+ |
copycolor(caustic, aval); |
294 |
+ |
ambPmapCaustic(caustic, r, rdepth); |
295 |
+ |
|
296 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
297 |
|
goto dumbamb; |
298 |
|
/* check number of bounces */ |
304 |
|
goto dumbamb; |
305 |
|
|
306 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
307 |
+ |
FVECT uvd[2]; |
308 |
+ |
float dgrad[2], *dgp = NULL; |
309 |
+ |
|
310 |
+ |
if (nrm != r->ron && DOT(nrm,r->ron) < 0.9999) |
311 |
+ |
dgp = dgrad; /* compute rotational grad. */ |
312 |
|
copycolor(acol, aval); |
313 |
|
rdepth++; |
314 |
|
ok = doambient(acol, r, r->rweight, |
315 |
< |
NULL, NULL, NULL, NULL, NULL); |
315 |
> |
uvd, NULL, NULL, dgp, NULL); |
316 |
|
rdepth--; |
317 |
|
if (!ok) |
318 |
|
goto dumbamb; |
319 |
+ |
if ((ok > 0) & (dgp != NULL)) { /* apply texture */ |
320 |
+ |
FVECT v1; |
321 |
+ |
VCROSS(v1, r->ron, nrm); |
322 |
+ |
d = 1.0; |
323 |
+ |
for (i = 3; i--; ) |
324 |
+ |
d += v1[i] * (dgp[0]*uvd[0][i] + dgp[1]*uvd[1][i]); |
325 |
+ |
if (d >= 0.05) |
326 |
+ |
scalecolor(acol, d); |
327 |
+ |
} |
328 |
|
copycolor(aval, acol); |
329 |
+ |
|
330 |
+ |
/* PMAP: add in caustic */ |
331 |
+ |
addcolor(aval, caustic); |
332 |
|
return; |
333 |
|
} |
334 |
|
|
338 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
339 |
|
d = sumambient(acol, r, nrm, rdepth, |
340 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
341 |
+ |
|
342 |
|
if (d > FTINY) { |
343 |
|
d = 1.0/d; |
344 |
|
scalecolor(acol, d); |
345 |
|
multcolor(aval, acol); |
346 |
+ |
|
347 |
+ |
/* PMAP: add in caustic */ |
348 |
+ |
addcolor(aval, caustic); |
349 |
|
return; |
350 |
|
} |
351 |
+ |
|
352 |
|
rdepth++; /* need to cache new value */ |
353 |
|
ok = makeambient(acol, r, nrm, rdepth-1); |
354 |
|
rdepth--; |
355 |
+ |
|
356 |
|
if (ok) { |
357 |
|
multcolor(aval, acol); /* computed new value */ |
358 |
+ |
|
359 |
+ |
/* PMAP: add in caustic */ |
360 |
+ |
addcolor(aval, caustic); |
361 |
|
return; |
362 |
|
} |
363 |
+ |
|
364 |
|
dumbamb: /* return global value */ |
365 |
|
if ((ambvwt <= 0) | (navsum == 0)) { |
366 |
|
multcolor(aval, ambval); |
367 |
+ |
|
368 |
+ |
/* PMAP: add in caustic */ |
369 |
+ |
addcolor(aval, caustic); |
370 |
|
return; |
371 |
|
} |
372 |
< |
l = bright(ambval); /* average in computations */ |
372 |
> |
|
373 |
> |
l = bright(ambval); /* average in computations */ |
374 |
|
if (l > FTINY) { |
375 |
|
d = (log(l)*(double)ambvwt + avsum) / |
376 |
|
(double)(ambvwt + navsum); |
417 |
|
VSUM(rtst.rdir, vdif, anorm, t[1]); /* further dist. > plane */ |
418 |
|
rtst.rmax = normalize(rtst.rdir); /* short ray test */ |
419 |
|
while (localhit(&rtst, &thescene)) { /* check for occluder */ |
420 |
< |
if (rtst.ro->omod != OVOID && |
420 |
> |
OBJREC *m = findmaterial(rtst.ro); |
421 |
> |
if (m != NULL && !istransp(m->otype) && !isBSDFproxy(m) && |
422 |
|
(rtst.clipset == NULL || |
423 |
|
!inset(rtst.clipset, rtst.ro->omod))) |
424 |
|
return(1); /* plug light leak */ |
525 |
|
/* |
526 |
|
* Extrapolate value and compute final weight (hat function) |
527 |
|
*/ |
528 |
< |
extambient(ct, av, r->rop, rn, uvw); |
528 |
> |
if (!extambient(ct, av, r->rop, rn, uvw)) |
529 |
> |
continue; |
530 |
|
d = tfunc(maxangle, sqrt(delta_r2), 0.0) * |
531 |
|
tfunc(ambacc, sqrt(delta_t2), 0.0); |
532 |
|
scalecolor(ct, d); |
577 |
|
} |
578 |
|
|
579 |
|
|
580 |
< |
static void |
580 |
> |
static int |
581 |
|
extambient( /* extrapolate value at pv, nv */ |
582 |
|
COLOR cr, |
583 |
|
AMBVAL *ap, |
586 |
|
FVECT uvw[3] |
587 |
|
) |
588 |
|
{ |
589 |
+ |
const double min_d = 0.05; |
590 |
|
static FVECT my_uvw[3]; |
591 |
|
FVECT v1; |
592 |
|
int i; |
606 |
|
for (i = 3; i--; ) |
607 |
|
d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]); |
608 |
|
|
609 |
< |
if (d <= 0.0) { |
610 |
< |
setcolor(cr, 0.0, 0.0, 0.0); |
570 |
< |
return; |
571 |
< |
} |
609 |
> |
if (d < min_d) /* should not use if we can avoid it */ |
610 |
> |
d = min_d; |
611 |
|
copycolor(cr, ap->val); |
612 |
|
scalecolor(cr, d); |
613 |
+ |
return(d > min_d); |
614 |
|
} |
615 |
|
|
616 |
|
|
673 |
|
) |
674 |
|
{ |
675 |
|
static int rdepth = 0; /* ambient recursion */ |
676 |
< |
COLOR acol; |
676 |
> |
COLOR acol, caustic; |
677 |
|
double d, l; |
678 |
|
|
679 |
+ |
/* PMAP: Factor in ambient from global photon map (if enabled) and return |
680 |
+ |
* as all ambient components accounted for */ |
681 |
+ |
if (ambPmap(aval, r, rdepth)) |
682 |
+ |
return; |
683 |
+ |
|
684 |
+ |
/* PMAP: Otherwise factor in ambient from caustic photon map |
685 |
+ |
* (ambPmapCaustic() returns zero if caustic photons disabled) and |
686 |
+ |
* continue with RADIANCE ambient calculation */ |
687 |
+ |
copycolor(caustic, aval); |
688 |
+ |
ambPmapCaustic(caustic, r, rdepth); |
689 |
+ |
|
690 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
691 |
|
goto dumbamb; |
692 |
|
/* check number of bounces */ |
704 |
|
rdepth--; |
705 |
|
if (d <= FTINY) |
706 |
|
goto dumbamb; |
707 |
< |
copycolor(aval, acol); |
707 |
> |
copycolor(aval, acol); |
708 |
> |
|
709 |
> |
/* PMAP: add in caustic */ |
710 |
> |
addcolor(aval, caustic); |
711 |
|
return; |
712 |
|
} |
713 |
|
|
717 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
718 |
|
d = sumambient(acol, r, nrm, rdepth, |
719 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
720 |
+ |
|
721 |
|
if (d > FTINY) { |
722 |
|
d = 1.0/d; |
723 |
|
scalecolor(acol, d); |
724 |
|
multcolor(aval, acol); |
725 |
+ |
|
726 |
+ |
/* PMAP: add in caustic */ |
727 |
+ |
addcolor(aval, caustic); |
728 |
|
return; |
729 |
|
} |
730 |
+ |
|
731 |
|
rdepth++; /* need to cache new value */ |
732 |
|
d = makeambient(acol, r, nrm, rdepth-1); |
733 |
|
rdepth--; |
734 |
+ |
|
735 |
|
if (d > FTINY) { |
736 |
|
multcolor(aval, acol); /* got new value */ |
737 |
+ |
|
738 |
+ |
/* PMAP: add in caustic */ |
739 |
+ |
addcolor(aval, caustic); |
740 |
|
return; |
741 |
|
} |
742 |
+ |
|
743 |
|
dumbamb: /* return global value */ |
744 |
|
if ((ambvwt <= 0) | (navsum == 0)) { |
745 |
|
multcolor(aval, ambval); |
746 |
+ |
|
747 |
+ |
/* PMAP: add in caustic */ |
748 |
+ |
addcolor(aval, caustic); |
749 |
|
return; |
750 |
|
} |
751 |
+ |
|
752 |
|
l = bright(ambval); /* average in computations */ |
753 |
|
if (l > FTINY) { |
754 |
|
d = (log(l)*(double)ambvwt + avsum) / |
1088 |
|
} |
1089 |
|
atp = atfreelist; |
1090 |
|
atfreelist = atp->kid; |
1091 |
< |
memset((char *)atp, '\0', 8*sizeof(AMBTREE)); |
1091 |
> |
memset(atp, 0, 8*sizeof(AMBTREE)); |
1092 |
|
return(atp); |
1093 |
|
} |
1094 |
|
|
1114 |
|
/* transfer values at this node */ |
1115 |
|
for (av = at->alist; av != NULL; av = at->alist) { |
1116 |
|
at->alist = av->next; |
1117 |
+ |
av->next = NULL; |
1118 |
|
(*f)(av); |
1119 |
|
} |
1120 |
|
if (at->kid == NULL) |
1192 |
|
{ |
1193 |
|
AMBVAL **avlpp; |
1194 |
|
|
1195 |
< |
avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2, |
1196 |
< |
nambvals, sizeof(AMBVAL *), &aposcmp); |
1195 |
> |
avlpp = (AMBVAL **)bsearch(&avaddr, avlist2, |
1196 |
> |
nambvals, sizeof(AMBVAL *), aposcmp); |
1197 |
|
if (avlpp == NULL) |
1198 |
|
error(CONSISTENCY, "address not found in avlmemi"); |
1199 |
|
return(avlpp - avlist2); |
1236 |
|
} |
1237 |
|
if (avlist1 == NULL) { /* no time tracking -- rebuild tree? */ |
1238 |
|
if (avlist2 != NULL) |
1239 |
< |
free((void *)avlist2); |
1239 |
> |
free(avlist2); |
1240 |
|
if (always) { /* rebuild without sorting */ |
1241 |
|
oldatrunk = atrunk; |
1242 |
|
atrunk.alist = NULL; |
1243 |
|
atrunk.kid = NULL; |
1244 |
< |
unloadatree(&oldatrunk, &avinsert); |
1244 |
> |
unloadatree(&oldatrunk, avinsert); |
1245 |
|
} |
1246 |
|
} else { /* sort memory by last access time */ |
1247 |
|
/* |
1258 |
|
eputs(errmsg); |
1259 |
|
#endif |
1260 |
|
i_avlist = 0; |
1261 |
< |
unloadatree(&atrunk, &av2list); /* empty current tree */ |
1261 |
> |
unloadatree(&atrunk, av2list); /* empty current tree */ |
1262 |
|
#ifdef DEBUG |
1263 |
|
if (i_avlist < nambvals) |
1264 |
|
error(CONSISTENCY, "missing ambient values in sortambvals"); |
1265 |
|
#endif |
1266 |
< |
qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp); |
1267 |
< |
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
1266 |
> |
qsort(avlist1, nambvals, sizeof(struct avl), alatcmp); |
1267 |
> |
qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
1268 |
|
for (i = 0; i < nambvals; i++) { |
1269 |
|
if (avlist1[i].p == NULL) |
1270 |
|
continue; |
1280 |
|
avinsert(avlist2[j]); |
1281 |
|
avlist1[j].p = NULL; |
1282 |
|
} |
1283 |
< |
free((void *)avlist1); |
1284 |
< |
free((void *)avlist2); |
1283 |
> |
free(avlist1); |
1284 |
> |
free(avlist2); |
1285 |
|
/* compute new sort interval */ |
1286 |
|
sortintvl = ambclock - lastsort; |
1287 |
|
if (sortintvl >= MAX_SORT_INTVL/2) |
1330 |
|
if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0) |
1331 |
|
goto seekerr; |
1332 |
|
if ((n = flen - lastpos) > 0) { /* file has grown */ |
1333 |
< |
if (ambinp == NULL) { /* use duplicate filedes */ |
1334 |
< |
ambinp = fdopen(dup(fileno(ambfp)), "r"); |
1333 |
> |
if (ambinp == NULL) { /* get new file pointer */ |
1334 |
> |
ambinp = fopen(ambfile, "rb"); |
1335 |
|
if (ambinp == NULL) |
1336 |
< |
error(SYSTEM, "fdopen failed in ambsync"); |
1336 |
> |
error(SYSTEM, "fopen failed in ambsync"); |
1337 |
|
} |
1338 |
|
if (fseek(ambinp, lastpos, SEEK_SET) < 0) |
1339 |
|
goto seekerr; |
1348 |
|
avstore(&avs); |
1349 |
|
n -= AMBVALSIZ; |
1350 |
|
} |
1351 |
< |
lastpos = flen - n; |
1352 |
< |
/*** seek always as safety measure |
1353 |
< |
if (n) ***/ /* alignment */ |
1285 |
< |
if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) |
1286 |
< |
goto seekerr; |
1351 |
> |
lastpos = flen - n; /* check alignment */ |
1352 |
> |
if (n && lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) |
1353 |
> |
goto seekerr; |
1354 |
|
} |
1355 |
|
n = fflush(ambfp); /* calls write() at last */ |
1356 |
< |
if (n != EOF) |
1290 |
< |
lastpos += (long)nunflshed*AMBVALSIZ; |
1291 |
< |
else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) |
1292 |
< |
goto seekerr; |
1293 |
< |
|
1356 |
> |
lastpos += (long)nunflshed*AMBVALSIZ; |
1357 |
|
aflock(F_UNLCK); /* release file */ |
1358 |
|
nunflshed = 0; |
1359 |
|
return(n); |
1360 |
|
seekerr: |
1361 |
|
error(SYSTEM, "seek failed in ambsync"); |
1362 |
< |
return -1; /* pro forma return */ |
1362 |
> |
return(EOF); /* pro forma return */ |
1363 |
|
} |
1364 |
|
|
1365 |
|
#else /* ! F_SETLKW */ |