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)) */ |
108 |
|
/* set min & max radii */ |
109 |
|
if (ar <= 0) { |
110 |
|
minarad = 0; |
111 |
< |
maxarad = thescene.cusize*0.5; |
111 |
> |
maxarad = thescene.cusize*0.2; |
112 |
|
} else { |
113 |
|
minarad = thescene.cusize / ar; |
114 |
|
maxarad = 64.0 * minarad; /* heuristic */ |
115 |
< |
if (maxarad > thescene.cusize*0.5) |
116 |
< |
maxarad = thescene.cusize*0.5; |
115 |
> |
if (maxarad > thescene.cusize*0.2) |
116 |
> |
maxarad = thescene.cusize*0.2; |
117 |
|
} |
118 |
|
if (minarad <= FTINY) |
119 |
|
minarad = 10.0*FTINY; |
184 |
|
(flen - lastpos)/AMBVALSIZ); |
185 |
|
error(WARNING, errmsg); |
186 |
|
fseek(ambfp, lastpos, SEEK_SET); |
188 |
– |
#ifndef _WIN32 /* XXX we need a replacement for that one */ |
187 |
|
ftruncate(fileno(ambfp), (off_t)lastpos); |
190 |
– |
#endif |
188 |
|
} |
189 |
|
} else if ((ambfp = fopen(ambfile, "w+")) != NULL) { |
190 |
|
initambfile(1); /* else create new file */ |
194 |
|
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
195 |
|
error(SYSTEM, errmsg); |
196 |
|
} |
200 |
– |
#ifdef getc_unlocked |
201 |
– |
flockfile(ambfp); /* application-level lock */ |
202 |
– |
#endif |
197 |
|
#ifdef F_SETLKW |
198 |
|
aflock(F_UNLCK); /* release file */ |
199 |
|
#endif |
214 |
|
lastpos = -1; |
215 |
|
} |
216 |
|
/* free ambient tree */ |
217 |
< |
unloadatree(&atrunk, &avfree); |
217 |
> |
unloadatree(&atrunk, avfree); |
218 |
|
/* reset state variables */ |
219 |
|
avsum = 0.; |
220 |
|
navsum = 0; |
257 |
|
|
258 |
|
/************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/ |
259 |
|
|
260 |
< |
#ifdef NEWAMB |
260 |
> |
#ifndef OLDAMB |
261 |
|
|
262 |
|
#define tfunc(lwr, x, upr) (((x)-(lwr))/((upr)-(lwr))) |
263 |
|
|
265 |
|
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
266 |
|
AMBTREE *at, FVECT c0, double s); |
267 |
|
static int makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
268 |
< |
static void extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
268 |
> |
static int extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
269 |
|
FVECT uvw[3]); |
270 |
|
|
271 |
|
void |
276 |
|
) |
277 |
|
{ |
278 |
|
static int rdepth = 0; /* ambient recursion */ |
279 |
< |
COLOR acol; |
279 |
> |
COLOR acol, caustic; |
280 |
|
int ok; |
281 |
|
double d, l; |
282 |
|
|
283 |
+ |
/* PMAP: Factor in ambient from photon map, if enabled and ray is |
284 |
+ |
* ambient. Return as all ambient components accounted for, else |
285 |
+ |
* continue. */ |
286 |
+ |
if (ambPmap(aval, r, rdepth)) |
287 |
+ |
return; |
288 |
+ |
|
289 |
+ |
/* PMAP: Factor in specular-diffuse ambient (caustics) from photon |
290 |
+ |
* map, if enabled and ray is primary, else caustic is zero. Continue |
291 |
+ |
* with RADIANCE ambient calculation */ |
292 |
+ |
copycolor(caustic, aval); |
293 |
+ |
ambPmapCaustic(caustic, r, rdepth); |
294 |
+ |
|
295 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
296 |
|
goto dumbamb; |
297 |
|
/* check number of bounces */ |
311 |
|
if (!ok) |
312 |
|
goto dumbamb; |
313 |
|
copycolor(aval, acol); |
314 |
+ |
|
315 |
+ |
/* PMAP: add in caustic */ |
316 |
+ |
addcolor(aval, caustic); |
317 |
|
return; |
318 |
|
} |
319 |
|
|
323 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
324 |
|
d = sumambient(acol, r, nrm, rdepth, |
325 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
326 |
+ |
|
327 |
|
if (d > FTINY) { |
328 |
|
d = 1.0/d; |
329 |
|
scalecolor(acol, d); |
330 |
|
multcolor(aval, acol); |
331 |
+ |
|
332 |
+ |
/* PMAP: add in caustic */ |
333 |
+ |
addcolor(aval, caustic); |
334 |
|
return; |
335 |
|
} |
336 |
+ |
|
337 |
|
rdepth++; /* need to cache new value */ |
338 |
|
ok = makeambient(acol, r, nrm, rdepth-1); |
339 |
|
rdepth--; |
340 |
+ |
|
341 |
|
if (ok) { |
342 |
|
multcolor(aval, acol); /* computed new value */ |
343 |
+ |
|
344 |
+ |
/* PMAP: add in caustic */ |
345 |
+ |
addcolor(aval, caustic); |
346 |
|
return; |
347 |
|
} |
348 |
+ |
|
349 |
|
dumbamb: /* return global value */ |
350 |
|
if ((ambvwt <= 0) | (navsum == 0)) { |
351 |
|
multcolor(aval, ambval); |
352 |
+ |
|
353 |
+ |
/* PMAP: add in caustic */ |
354 |
+ |
addcolor(aval, caustic); |
355 |
|
return; |
356 |
|
} |
357 |
< |
l = bright(ambval); /* average in computations */ |
357 |
> |
|
358 |
> |
l = bright(ambval); /* average in computations */ |
359 |
|
if (l > FTINY) { |
360 |
|
d = (log(l)*(double)ambvwt + avsum) / |
361 |
|
(double)(ambvwt + navsum); |
509 |
|
/* |
510 |
|
* Extrapolate value and compute final weight (hat function) |
511 |
|
*/ |
512 |
< |
extambient(ct, av, r->rop, rn, uvw); |
512 |
> |
if (!extambient(ct, av, r->rop, rn, uvw)) |
513 |
> |
continue; |
514 |
|
d = tfunc(maxangle, sqrt(delta_r2), 0.0) * |
515 |
|
tfunc(ambacc, sqrt(delta_t2), 0.0); |
516 |
|
scalecolor(ct, d); |
561 |
|
} |
562 |
|
|
563 |
|
|
564 |
< |
static void |
564 |
> |
static int |
565 |
|
extambient( /* extrapolate value at pv, nv */ |
566 |
|
COLOR cr, |
567 |
|
AMBVAL *ap, |
570 |
|
FVECT uvw[3] |
571 |
|
) |
572 |
|
{ |
573 |
+ |
const double min_d = 0.05; |
574 |
|
static FVECT my_uvw[3]; |
575 |
|
FVECT v1; |
576 |
|
int i; |
590 |
|
for (i = 3; i--; ) |
591 |
|
d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]); |
592 |
|
|
593 |
< |
if (d <= 0.0) { |
594 |
< |
setcolor(cr, 0.0, 0.0, 0.0); |
570 |
< |
return; |
571 |
< |
} |
593 |
> |
if (d < min_d) /* should not use if we can avoid it */ |
594 |
> |
d = min_d; |
595 |
|
copycolor(cr, ap->val); |
596 |
|
scalecolor(cr, d); |
597 |
+ |
return(d > min_d); |
598 |
|
} |
599 |
|
|
600 |
|
|
657 |
|
) |
658 |
|
{ |
659 |
|
static int rdepth = 0; /* ambient recursion */ |
660 |
< |
COLOR acol; |
660 |
> |
COLOR acol, caustic; |
661 |
|
double d, l; |
662 |
|
|
663 |
+ |
/* PMAP: Factor in ambient from global photon map (if enabled) and return |
664 |
+ |
* as all ambient components accounted for */ |
665 |
+ |
if (ambPmap(aval, r, rdepth)) |
666 |
+ |
return; |
667 |
+ |
|
668 |
+ |
/* PMAP: Otherwise factor in ambient from caustic photon map |
669 |
+ |
* (ambPmapCaustic() returns zero if caustic photons disabled) and |
670 |
+ |
* continue with RADIANCE ambient calculation */ |
671 |
+ |
copycolor(caustic, aval); |
672 |
+ |
ambPmapCaustic(caustic, r, rdepth); |
673 |
+ |
|
674 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
675 |
|
goto dumbamb; |
676 |
|
/* check number of bounces */ |
688 |
|
rdepth--; |
689 |
|
if (d <= FTINY) |
690 |
|
goto dumbamb; |
691 |
< |
copycolor(aval, acol); |
691 |
> |
copycolor(aval, acol); |
692 |
> |
|
693 |
> |
/* PMAP: add in caustic */ |
694 |
> |
addcolor(aval, caustic); |
695 |
|
return; |
696 |
|
} |
697 |
|
|
701 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
702 |
|
d = sumambient(acol, r, nrm, rdepth, |
703 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
704 |
+ |
|
705 |
|
if (d > FTINY) { |
706 |
|
d = 1.0/d; |
707 |
|
scalecolor(acol, d); |
708 |
|
multcolor(aval, acol); |
709 |
+ |
|
710 |
+ |
/* PMAP: add in caustic */ |
711 |
+ |
addcolor(aval, caustic); |
712 |
|
return; |
713 |
|
} |
714 |
+ |
|
715 |
|
rdepth++; /* need to cache new value */ |
716 |
|
d = makeambient(acol, r, nrm, rdepth-1); |
717 |
|
rdepth--; |
718 |
+ |
|
719 |
|
if (d > FTINY) { |
720 |
|
multcolor(aval, acol); /* got new value */ |
721 |
+ |
|
722 |
+ |
/* PMAP: add in caustic */ |
723 |
+ |
addcolor(aval, caustic); |
724 |
|
return; |
725 |
|
} |
726 |
+ |
|
727 |
|
dumbamb: /* return global value */ |
728 |
|
if ((ambvwt <= 0) | (navsum == 0)) { |
729 |
|
multcolor(aval, ambval); |
730 |
+ |
|
731 |
+ |
/* PMAP: add in caustic */ |
732 |
+ |
addcolor(aval, caustic); |
733 |
|
return; |
734 |
|
} |
735 |
+ |
|
736 |
|
l = bright(ambval); /* average in computations */ |
737 |
|
if (l > FTINY) { |
738 |
|
d = (log(l)*(double)ambvwt + avsum) / |
1224 |
|
oldatrunk = atrunk; |
1225 |
|
atrunk.alist = NULL; |
1226 |
|
atrunk.kid = NULL; |
1227 |
< |
unloadatree(&oldatrunk, &avinsert); |
1227 |
> |
unloadatree(&oldatrunk, avinsert); |
1228 |
|
} |
1229 |
|
} else { /* sort memory by last access time */ |
1230 |
|
/* |
1241 |
|
eputs(errmsg); |
1242 |
|
#endif |
1243 |
|
i_avlist = 0; |
1244 |
< |
unloadatree(&atrunk, &av2list); /* empty current tree */ |
1244 |
> |
unloadatree(&atrunk, av2list); /* empty current tree */ |
1245 |
|
#ifdef DEBUG |
1246 |
|
if (i_avlist < nambvals) |
1247 |
|
error(CONSISTENCY, "missing ambient values in sortambvals"); |
1248 |
|
#endif |
1249 |
< |
qsort((char *)avlist1, nambvals, sizeof(struct avl), &alatcmp); |
1250 |
< |
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), &aposcmp); |
1249 |
> |
qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp); |
1250 |
> |
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
1251 |
|
for (i = 0; i < nambvals; i++) { |
1252 |
|
if (avlist1[i].p == NULL) |
1253 |
|
continue; |