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 |
|
/* reset state variables */ |
221 |
|
avsum = 0.; |
222 |
|
navsum = 0; |
259 |
|
|
260 |
|
/************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/ |
261 |
|
|
262 |
< |
#ifdef NEWAMB |
262 |
> |
#ifndef OLDAMB |
263 |
|
|
264 |
|
#define tfunc(lwr, x, upr) (((x)-(lwr))/((upr)-(lwr))) |
265 |
|
|
267 |
|
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
268 |
|
AMBTREE *at, FVECT c0, double s); |
269 |
|
static int makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
270 |
< |
static void extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
270 |
> |
static int extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
271 |
|
FVECT uvw[3]); |
272 |
|
|
273 |
|
void |
278 |
|
) |
279 |
|
{ |
280 |
|
static int rdepth = 0; /* ambient recursion */ |
281 |
< |
COLOR acol; |
281 |
> |
COLOR acol, caustic; |
282 |
|
int ok; |
283 |
|
double d, l; |
284 |
|
|
285 |
+ |
/* PMAP: Factor in ambient from photon map, if enabled and ray is |
286 |
+ |
* ambient. Return as all ambient components accounted for, else |
287 |
+ |
* continue. */ |
288 |
+ |
if (ambPmap(aval, r, rdepth)) |
289 |
+ |
return; |
290 |
+ |
|
291 |
+ |
/* PMAP: Factor in specular-diffuse ambient (caustics) from photon |
292 |
+ |
* map, if enabled and ray is primary, else caustic is zero. Continue |
293 |
+ |
* with RADIANCE ambient calculation */ |
294 |
+ |
copycolor(caustic, aval); |
295 |
+ |
ambPmapCaustic(caustic, r, rdepth); |
296 |
+ |
|
297 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
298 |
|
goto dumbamb; |
299 |
|
/* check number of bounces */ |
313 |
|
if (!ok) |
314 |
|
goto dumbamb; |
315 |
|
copycolor(aval, acol); |
316 |
+ |
|
317 |
+ |
/* PMAP: add in caustic */ |
318 |
+ |
addcolor(aval, caustic); |
319 |
|
return; |
320 |
|
} |
321 |
|
|
325 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
326 |
|
d = sumambient(acol, r, nrm, rdepth, |
327 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
328 |
+ |
|
329 |
|
if (d > FTINY) { |
330 |
|
d = 1.0/d; |
331 |
|
scalecolor(acol, d); |
332 |
|
multcolor(aval, acol); |
333 |
+ |
|
334 |
+ |
/* PMAP: add in caustic */ |
335 |
+ |
addcolor(aval, caustic); |
336 |
|
return; |
337 |
|
} |
338 |
+ |
|
339 |
|
rdepth++; /* need to cache new value */ |
340 |
|
ok = makeambient(acol, r, nrm, rdepth-1); |
341 |
|
rdepth--; |
342 |
+ |
|
343 |
|
if (ok) { |
344 |
|
multcolor(aval, acol); /* computed new value */ |
345 |
+ |
|
346 |
+ |
/* PMAP: add in caustic */ |
347 |
+ |
addcolor(aval, caustic); |
348 |
|
return; |
349 |
|
} |
350 |
+ |
|
351 |
|
dumbamb: /* return global value */ |
352 |
|
if ((ambvwt <= 0) | (navsum == 0)) { |
353 |
|
multcolor(aval, ambval); |
354 |
+ |
|
355 |
+ |
/* PMAP: add in caustic */ |
356 |
+ |
addcolor(aval, caustic); |
357 |
|
return; |
358 |
|
} |
359 |
< |
l = bright(ambval); /* average in computations */ |
359 |
> |
|
360 |
> |
l = bright(ambval); /* average in computations */ |
361 |
|
if (l > FTINY) { |
362 |
|
d = (log(l)*(double)ambvwt + avsum) / |
363 |
|
(double)(ambvwt + navsum); |
511 |
|
/* |
512 |
|
* Extrapolate value and compute final weight (hat function) |
513 |
|
*/ |
514 |
< |
extambient(ct, av, r->rop, rn, uvw); |
514 |
> |
if (!extambient(ct, av, r->rop, rn, uvw)) |
515 |
> |
continue; |
516 |
|
d = tfunc(maxangle, sqrt(delta_r2), 0.0) * |
517 |
|
tfunc(ambacc, sqrt(delta_t2), 0.0); |
518 |
|
scalecolor(ct, d); |
563 |
|
} |
564 |
|
|
565 |
|
|
566 |
< |
static void |
566 |
> |
static int |
567 |
|
extambient( /* extrapolate value at pv, nv */ |
568 |
|
COLOR cr, |
569 |
|
AMBVAL *ap, |
572 |
|
FVECT uvw[3] |
573 |
|
) |
574 |
|
{ |
575 |
+ |
const double min_d = 0.05; |
576 |
|
static FVECT my_uvw[3]; |
577 |
|
FVECT v1; |
578 |
|
int i; |
592 |
|
for (i = 3; i--; ) |
593 |
|
d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]); |
594 |
|
|
595 |
< |
if (d <= 0.0) { |
596 |
< |
setcolor(cr, 0.0, 0.0, 0.0); |
570 |
< |
return; |
571 |
< |
} |
595 |
> |
if (d < min_d) /* should not use if we can avoid it */ |
596 |
> |
d = min_d; |
597 |
|
copycolor(cr, ap->val); |
598 |
|
scalecolor(cr, d); |
599 |
+ |
return(d > min_d); |
600 |
|
} |
601 |
|
|
602 |
|
|
659 |
|
) |
660 |
|
{ |
661 |
|
static int rdepth = 0; /* ambient recursion */ |
662 |
< |
COLOR acol; |
662 |
> |
COLOR acol, caustic; |
663 |
|
double d, l; |
664 |
|
|
665 |
+ |
/* PMAP: Factor in ambient from global photon map (if enabled) and return |
666 |
+ |
* as all ambient components accounted for */ |
667 |
+ |
if (ambPmap(aval, r, rdepth)) |
668 |
+ |
return; |
669 |
+ |
|
670 |
+ |
/* PMAP: Otherwise factor in ambient from caustic photon map |
671 |
+ |
* (ambPmapCaustic() returns zero if caustic photons disabled) and |
672 |
+ |
* continue with RADIANCE ambient calculation */ |
673 |
+ |
copycolor(caustic, aval); |
674 |
+ |
ambPmapCaustic(caustic, r, rdepth); |
675 |
+ |
|
676 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
677 |
|
goto dumbamb; |
678 |
|
/* check number of bounces */ |
690 |
|
rdepth--; |
691 |
|
if (d <= FTINY) |
692 |
|
goto dumbamb; |
693 |
< |
copycolor(aval, acol); |
693 |
> |
copycolor(aval, acol); |
694 |
> |
|
695 |
> |
/* PMAP: add in caustic */ |
696 |
> |
addcolor(aval, caustic); |
697 |
|
return; |
698 |
|
} |
699 |
|
|
703 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
704 |
|
d = sumambient(acol, r, nrm, rdepth, |
705 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
706 |
+ |
|
707 |
|
if (d > FTINY) { |
708 |
|
d = 1.0/d; |
709 |
|
scalecolor(acol, d); |
710 |
|
multcolor(aval, acol); |
711 |
+ |
|
712 |
+ |
/* PMAP: add in caustic */ |
713 |
+ |
addcolor(aval, caustic); |
714 |
|
return; |
715 |
|
} |
716 |
+ |
|
717 |
|
rdepth++; /* need to cache new value */ |
718 |
|
d = makeambient(acol, r, nrm, rdepth-1); |
719 |
|
rdepth--; |
720 |
+ |
|
721 |
|
if (d > FTINY) { |
722 |
|
multcolor(aval, acol); /* got new value */ |
723 |
+ |
|
724 |
+ |
/* PMAP: add in caustic */ |
725 |
+ |
addcolor(aval, caustic); |
726 |
|
return; |
727 |
|
} |
728 |
+ |
|
729 |
|
dumbamb: /* return global value */ |
730 |
|
if ((ambvwt <= 0) | (navsum == 0)) { |
731 |
|
multcolor(aval, ambval); |
732 |
+ |
|
733 |
+ |
/* PMAP: add in caustic */ |
734 |
+ |
addcolor(aval, caustic); |
735 |
|
return; |
736 |
|
} |
737 |
+ |
|
738 |
|
l = bright(ambval); /* average in computations */ |
739 |
|
if (l > FTINY) { |
740 |
|
d = (log(l)*(double)ambvwt + avsum) / |
1226 |
|
oldatrunk = atrunk; |
1227 |
|
atrunk.alist = NULL; |
1228 |
|
atrunk.kid = NULL; |
1229 |
< |
unloadatree(&oldatrunk, &avinsert); |
1229 |
> |
unloadatree(&oldatrunk, avinsert); |
1230 |
|
} |
1231 |
|
} else { /* sort memory by last access time */ |
1232 |
|
/* |
1243 |
|
eputs(errmsg); |
1244 |
|
#endif |
1245 |
|
i_avlist = 0; |
1246 |
< |
unloadatree(&atrunk, &av2list); /* empty current tree */ |
1246 |
> |
unloadatree(&atrunk, av2list); /* empty current tree */ |
1247 |
|
#ifdef DEBUG |
1248 |
|
if (i_avlist < nambvals) |
1249 |
|
error(CONSISTENCY, "missing ambient values in sortambvals"); |