1 |
– |
#ifndef lint |
1 |
|
static const char RCSid[] = "$Id$"; |
3 |
– |
#endif |
2 |
|
/* |
3 |
|
* ambient.c - routines dealing with ambient (inter-reflected) component. |
4 |
|
* |
15 |
|
#include "resolu.h" |
16 |
|
#include "ambient.h" |
17 |
|
#include "random.h" |
18 |
+ |
#include "pmapamb.h" |
19 |
|
|
20 |
|
#ifndef OCTSCALE |
21 |
|
#define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ |
196 |
|
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
197 |
|
error(SYSTEM, errmsg); |
198 |
|
} |
200 |
– |
#ifdef getc_unlocked |
201 |
– |
flockfile(ambfp); /* application-level lock */ |
202 |
– |
#endif |
199 |
|
#ifdef F_SETLKW |
200 |
|
aflock(F_UNLCK); /* release file */ |
201 |
|
#endif |
216 |
|
lastpos = -1; |
217 |
|
} |
218 |
|
/* free ambient tree */ |
219 |
< |
unloadatree(&atrunk, &avfree); |
219 |
> |
unloadatree(&atrunk, avfree); |
220 |
> |
freeambtree(NULL); |
221 |
|
/* reset state variables */ |
222 |
|
avsum = 0.; |
223 |
|
navsum = 0; |
260 |
|
|
261 |
|
/************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/ |
262 |
|
|
263 |
< |
#ifdef NEWAMB |
263 |
> |
#ifndef OLDAMB |
264 |
|
|
265 |
|
#define tfunc(lwr, x, upr) (((x)-(lwr))/((upr)-(lwr))) |
266 |
|
|
268 |
|
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
269 |
|
AMBTREE *at, FVECT c0, double s); |
270 |
|
static int makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
271 |
< |
static void extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
271 |
> |
static int extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
272 |
|
FVECT uvw[3]); |
273 |
|
|
274 |
|
void |
279 |
|
) |
280 |
|
{ |
281 |
|
static int rdepth = 0; /* ambient recursion */ |
282 |
< |
COLOR acol; |
282 |
> |
COLOR acol, caustic; |
283 |
|
int ok; |
284 |
|
double d, l; |
285 |
|
|
286 |
+ |
/* PMAP: Factor in ambient from photon map, if enabled and ray is |
287 |
+ |
* ambient. Return as all ambient components accounted for, else |
288 |
+ |
* continue. */ |
289 |
+ |
if (ambPmap(aval, r, rdepth)) |
290 |
+ |
return; |
291 |
+ |
|
292 |
+ |
/* PMAP: Factor in specular-diffuse ambient (caustics) from photon |
293 |
+ |
* map, if enabled and ray is primary, else caustic is zero. Continue |
294 |
+ |
* with RADIANCE ambient calculation */ |
295 |
+ |
copycolor(caustic, aval); |
296 |
+ |
ambPmapCaustic(caustic, r, rdepth); |
297 |
+ |
|
298 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
299 |
|
goto dumbamb; |
300 |
|
/* check number of bounces */ |
314 |
|
if (!ok) |
315 |
|
goto dumbamb; |
316 |
|
copycolor(aval, acol); |
317 |
+ |
|
318 |
+ |
/* PMAP: add in caustic */ |
319 |
+ |
addcolor(aval, caustic); |
320 |
|
return; |
321 |
|
} |
322 |
|
|
326 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
327 |
|
d = sumambient(acol, r, nrm, rdepth, |
328 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
329 |
+ |
|
330 |
|
if (d > FTINY) { |
331 |
|
d = 1.0/d; |
332 |
|
scalecolor(acol, d); |
333 |
|
multcolor(aval, acol); |
334 |
+ |
|
335 |
+ |
/* PMAP: add in caustic */ |
336 |
+ |
addcolor(aval, caustic); |
337 |
|
return; |
338 |
|
} |
339 |
+ |
|
340 |
|
rdepth++; /* need to cache new value */ |
341 |
|
ok = makeambient(acol, r, nrm, rdepth-1); |
342 |
|
rdepth--; |
343 |
+ |
|
344 |
|
if (ok) { |
345 |
|
multcolor(aval, acol); /* computed new value */ |
346 |
+ |
|
347 |
+ |
/* PMAP: add in caustic */ |
348 |
+ |
addcolor(aval, caustic); |
349 |
|
return; |
350 |
|
} |
351 |
+ |
|
352 |
|
dumbamb: /* return global value */ |
353 |
|
if ((ambvwt <= 0) | (navsum == 0)) { |
354 |
|
multcolor(aval, ambval); |
355 |
+ |
|
356 |
+ |
/* PMAP: add in caustic */ |
357 |
+ |
addcolor(aval, caustic); |
358 |
|
return; |
359 |
|
} |
360 |
< |
l = bright(ambval); /* average in computations */ |
360 |
> |
|
361 |
> |
l = bright(ambval); /* average in computations */ |
362 |
|
if (l > FTINY) { |
363 |
|
d = (log(l)*(double)ambvwt + avsum) / |
364 |
|
(double)(ambvwt + navsum); |
512 |
|
/* |
513 |
|
* Extrapolate value and compute final weight (hat function) |
514 |
|
*/ |
515 |
< |
extambient(ct, av, r->rop, rn, uvw); |
515 |
> |
if (!extambient(ct, av, r->rop, rn, uvw)) |
516 |
> |
continue; |
517 |
|
d = tfunc(maxangle, sqrt(delta_r2), 0.0) * |
518 |
|
tfunc(ambacc, sqrt(delta_t2), 0.0); |
519 |
|
scalecolor(ct, d); |
564 |
|
} |
565 |
|
|
566 |
|
|
567 |
< |
static void |
567 |
> |
static int |
568 |
|
extambient( /* extrapolate value at pv, nv */ |
569 |
|
COLOR cr, |
570 |
|
AMBVAL *ap, |
573 |
|
FVECT uvw[3] |
574 |
|
) |
575 |
|
{ |
576 |
+ |
const double min_d = 0.05; |
577 |
|
static FVECT my_uvw[3]; |
578 |
|
FVECT v1; |
579 |
|
int i; |
593 |
|
for (i = 3; i--; ) |
594 |
|
d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]); |
595 |
|
|
596 |
< |
if (d <= 0.0) { |
597 |
< |
setcolor(cr, 0.0, 0.0, 0.0); |
570 |
< |
return; |
571 |
< |
} |
596 |
> |
if (d < min_d) /* should not use if we can avoid it */ |
597 |
> |
d = min_d; |
598 |
|
copycolor(cr, ap->val); |
599 |
|
scalecolor(cr, d); |
600 |
+ |
return(d > min_d); |
601 |
|
} |
602 |
|
|
603 |
|
|
660 |
|
) |
661 |
|
{ |
662 |
|
static int rdepth = 0; /* ambient recursion */ |
663 |
< |
COLOR acol; |
663 |
> |
COLOR acol, caustic; |
664 |
|
double d, l; |
665 |
|
|
666 |
+ |
/* PMAP: Factor in ambient from global photon map (if enabled) and return |
667 |
+ |
* as all ambient components accounted for */ |
668 |
+ |
if (ambPmap(aval, r, rdepth)) |
669 |
+ |
return; |
670 |
+ |
|
671 |
+ |
/* PMAP: Otherwise factor in ambient from caustic photon map |
672 |
+ |
* (ambPmapCaustic() returns zero if caustic photons disabled) and |
673 |
+ |
* continue with RADIANCE ambient calculation */ |
674 |
+ |
copycolor(caustic, aval); |
675 |
+ |
ambPmapCaustic(caustic, r, rdepth); |
676 |
+ |
|
677 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
678 |
|
goto dumbamb; |
679 |
|
/* check number of bounces */ |
691 |
|
rdepth--; |
692 |
|
if (d <= FTINY) |
693 |
|
goto dumbamb; |
694 |
< |
copycolor(aval, acol); |
694 |
> |
copycolor(aval, acol); |
695 |
> |
|
696 |
> |
/* PMAP: add in caustic */ |
697 |
> |
addcolor(aval, caustic); |
698 |
|
return; |
699 |
|
} |
700 |
|
|
704 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
705 |
|
d = sumambient(acol, r, nrm, rdepth, |
706 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
707 |
+ |
|
708 |
|
if (d > FTINY) { |
709 |
|
d = 1.0/d; |
710 |
|
scalecolor(acol, d); |
711 |
|
multcolor(aval, acol); |
712 |
+ |
|
713 |
+ |
/* PMAP: add in caustic */ |
714 |
+ |
addcolor(aval, caustic); |
715 |
|
return; |
716 |
|
} |
717 |
+ |
|
718 |
|
rdepth++; /* need to cache new value */ |
719 |
|
d = makeambient(acol, r, nrm, rdepth-1); |
720 |
|
rdepth--; |
721 |
+ |
|
722 |
|
if (d > FTINY) { |
723 |
|
multcolor(aval, acol); /* got new value */ |
724 |
+ |
|
725 |
+ |
/* PMAP: add in caustic */ |
726 |
+ |
addcolor(aval, caustic); |
727 |
|
return; |
728 |
|
} |
729 |
+ |
|
730 |
|
dumbamb: /* return global value */ |
731 |
|
if ((ambvwt <= 0) | (navsum == 0)) { |
732 |
|
multcolor(aval, ambval); |
733 |
+ |
|
734 |
+ |
/* PMAP: add in caustic */ |
735 |
+ |
addcolor(aval, caustic); |
736 |
|
return; |
737 |
|
} |
738 |
+ |
|
739 |
|
l = bright(ambval); /* average in computations */ |
740 |
|
if (l > FTINY) { |
741 |
|
d = (log(l)*(double)ambvwt + avsum) / |
1085 |
|
AMBTREE *atp |
1086 |
|
) |
1087 |
|
{ |
1088 |
< |
atp->kid = atfreelist; |
1088 |
> |
if (atp == NULL) { /* freeing free list? */ |
1089 |
> |
while ((atp = atfreelist) != NULL) { |
1090 |
> |
atfreelist = atp->kid; |
1091 |
> |
free(atp); |
1092 |
> |
} |
1093 |
> |
return; |
1094 |
> |
} |
1095 |
> |
atp->kid = atfreelist; /* else push node onto free list */ |
1096 |
|
atfreelist = atp; |
1097 |
|
} |
1098 |
|
|
1234 |
|
oldatrunk = atrunk; |
1235 |
|
atrunk.alist = NULL; |
1236 |
|
atrunk.kid = NULL; |
1237 |
< |
unloadatree(&oldatrunk, &avinsert); |
1237 |
> |
unloadatree(&oldatrunk, avinsert); |
1238 |
|
} |
1239 |
|
} else { /* sort memory by last access time */ |
1240 |
|
/* |
1251 |
|
eputs(errmsg); |
1252 |
|
#endif |
1253 |
|
i_avlist = 0; |
1254 |
< |
unloadatree(&atrunk, &av2list); /* empty current tree */ |
1254 |
> |
unloadatree(&atrunk, av2list); /* empty current tree */ |
1255 |
|
#ifdef DEBUG |
1256 |
|
if (i_avlist < nambvals) |
1257 |
|
error(CONSISTENCY, "missing ambient values in sortambvals"); |
1258 |
|
#endif |
1259 |
< |
qsort((char *)avlist1, nambvals, sizeof(struct avl), &alatcmp); |
1260 |
< |
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), &aposcmp); |
1259 |
> |
qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp); |
1260 |
> |
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
1261 |
|
for (i = 0; i < nambvals; i++) { |
1262 |
|
if (avlist1[i].p == NULL) |
1263 |
|
continue; |