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)) */ |
76 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
77 |
|
|
78 |
|
#define newambval() (AMBVAL *)malloc(sizeof(AMBVAL)) |
80 |
– |
#define freeav(av) free((void *)av); |
79 |
|
|
80 |
|
static void initambfile(int creat); |
81 |
|
static void avsave(AMBVAL *av); |
183 |
|
(flen - lastpos)/AMBVALSIZ); |
184 |
|
error(WARNING, errmsg); |
185 |
|
fseek(ambfp, lastpos, SEEK_SET); |
188 |
– |
#ifndef _WIN32 /* XXX we need a replacement for that one */ |
186 |
|
ftruncate(fileno(ambfp), (off_t)lastpos); |
190 |
– |
#endif |
187 |
|
} |
188 |
|
} else if ((ambfp = fopen(ambfile, "w+")) != NULL) { |
189 |
|
initambfile(1); /* else create new file */ |
193 |
|
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
194 |
|
error(SYSTEM, errmsg); |
195 |
|
} |
200 |
– |
#ifdef getc_unlocked |
201 |
– |
flockfile(ambfp); /* application-level lock */ |
202 |
– |
#endif |
196 |
|
#ifdef F_SETLKW |
197 |
|
aflock(F_UNLCK); /* release file */ |
198 |
|
#endif |
213 |
|
lastpos = -1; |
214 |
|
} |
215 |
|
/* free ambient tree */ |
216 |
< |
unloadatree(&atrunk, &avfree); |
216 |
> |
unloadatree(&atrunk, avfree); |
217 |
|
/* reset state variables */ |
218 |
|
avsum = 0.; |
219 |
|
navsum = 0; |
275 |
|
) |
276 |
|
{ |
277 |
|
static int rdepth = 0; /* ambient recursion */ |
278 |
< |
COLOR acol; |
279 |
< |
int ok; |
278 |
> |
COLOR acol, caustic; |
279 |
> |
int i, ok; |
280 |
|
double d, l; |
281 |
|
|
282 |
+ |
/* PMAP: Factor in ambient from photon map, if enabled and ray is |
283 |
+ |
* ambient. Return as all ambient components accounted for, else |
284 |
+ |
* continue. */ |
285 |
+ |
if (ambPmap(aval, r, rdepth)) |
286 |
+ |
return; |
287 |
+ |
|
288 |
+ |
/* PMAP: Factor in specular-diffuse ambient (caustics) from photon |
289 |
+ |
* map, if enabled and ray is primary, else caustic is zero. Continue |
290 |
+ |
* with RADIANCE ambient calculation */ |
291 |
+ |
copycolor(caustic, aval); |
292 |
+ |
ambPmapCaustic(caustic, r, rdepth); |
293 |
+ |
|
294 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
295 |
|
goto dumbamb; |
296 |
|
/* check number of bounces */ |
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 */ |
329 |
+ |
addcolor(aval, caustic); |
330 |
|
return; |
331 |
|
} |
332 |
|
|
336 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
337 |
|
d = sumambient(acol, r, nrm, rdepth, |
338 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
339 |
+ |
|
340 |
|
if (d > FTINY) { |
341 |
|
d = 1.0/d; |
342 |
|
scalecolor(acol, d); |
343 |
|
multcolor(aval, acol); |
344 |
+ |
|
345 |
+ |
/* PMAP: add in caustic */ |
346 |
+ |
addcolor(aval, caustic); |
347 |
|
return; |
348 |
|
} |
349 |
+ |
|
350 |
|
rdepth++; /* need to cache new value */ |
351 |
|
ok = makeambient(acol, r, nrm, rdepth-1); |
352 |
|
rdepth--; |
353 |
+ |
|
354 |
|
if (ok) { |
355 |
|
multcolor(aval, acol); /* computed new value */ |
356 |
+ |
|
357 |
+ |
/* PMAP: add in caustic */ |
358 |
+ |
addcolor(aval, caustic); |
359 |
|
return; |
360 |
|
} |
361 |
+ |
|
362 |
|
dumbamb: /* return global value */ |
363 |
|
if ((ambvwt <= 0) | (navsum == 0)) { |
364 |
|
multcolor(aval, ambval); |
365 |
+ |
|
366 |
+ |
/* PMAP: add in caustic */ |
367 |
+ |
addcolor(aval, caustic); |
368 |
|
return; |
369 |
|
} |
370 |
< |
l = bright(ambval); /* average in computations */ |
370 |
> |
|
371 |
> |
l = bright(ambval); /* average in computations */ |
372 |
|
if (l > FTINY) { |
373 |
|
d = (log(l)*(double)ambvwt + avsum) / |
374 |
|
(double)(ambvwt + navsum); |
670 |
|
) |
671 |
|
{ |
672 |
|
static int rdepth = 0; /* ambient recursion */ |
673 |
< |
COLOR acol; |
673 |
> |
COLOR acol, caustic; |
674 |
|
double d, l; |
675 |
|
|
676 |
+ |
/* PMAP: Factor in ambient from global photon map (if enabled) and return |
677 |
+ |
* as all ambient components accounted for */ |
678 |
+ |
if (ambPmap(aval, r, rdepth)) |
679 |
+ |
return; |
680 |
+ |
|
681 |
+ |
/* PMAP: Otherwise factor in ambient from caustic photon map |
682 |
+ |
* (ambPmapCaustic() returns zero if caustic photons disabled) and |
683 |
+ |
* continue with RADIANCE ambient calculation */ |
684 |
+ |
copycolor(caustic, aval); |
685 |
+ |
ambPmapCaustic(caustic, r, rdepth); |
686 |
+ |
|
687 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
688 |
|
goto dumbamb; |
689 |
|
/* check number of bounces */ |
701 |
|
rdepth--; |
702 |
|
if (d <= FTINY) |
703 |
|
goto dumbamb; |
704 |
< |
copycolor(aval, acol); |
704 |
> |
copycolor(aval, acol); |
705 |
> |
|
706 |
> |
/* PMAP: add in caustic */ |
707 |
> |
addcolor(aval, caustic); |
708 |
|
return; |
709 |
|
} |
710 |
|
|
714 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
715 |
|
d = sumambient(acol, r, nrm, rdepth, |
716 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
717 |
+ |
|
718 |
|
if (d > FTINY) { |
719 |
|
d = 1.0/d; |
720 |
|
scalecolor(acol, d); |
721 |
|
multcolor(aval, acol); |
722 |
+ |
|
723 |
+ |
/* PMAP: add in caustic */ |
724 |
+ |
addcolor(aval, caustic); |
725 |
|
return; |
726 |
|
} |
727 |
+ |
|
728 |
|
rdepth++; /* need to cache new value */ |
729 |
|
d = makeambient(acol, r, nrm, rdepth-1); |
730 |
|
rdepth--; |
731 |
+ |
|
732 |
|
if (d > FTINY) { |
733 |
|
multcolor(aval, acol); /* got new value */ |
734 |
+ |
|
735 |
+ |
/* PMAP: add in caustic */ |
736 |
+ |
addcolor(aval, caustic); |
737 |
|
return; |
738 |
|
} |
739 |
+ |
|
740 |
|
dumbamb: /* return global value */ |
741 |
|
if ((ambvwt <= 0) | (navsum == 0)) { |
742 |
|
multcolor(aval, ambval); |
743 |
+ |
|
744 |
+ |
/* PMAP: add in caustic */ |
745 |
+ |
addcolor(aval, caustic); |
746 |
|
return; |
747 |
|
} |
748 |
+ |
|
749 |
|
l = bright(ambval); /* average in computations */ |
750 |
|
if (l > FTINY) { |
751 |
|
d = (log(l)*(double)ambvwt + avsum) / |
1085 |
|
} |
1086 |
|
atp = atfreelist; |
1087 |
|
atfreelist = atp->kid; |
1088 |
< |
memset((char *)atp, '\0', 8*sizeof(AMBTREE)); |
1088 |
> |
memset(atp, 0, 8*sizeof(AMBTREE)); |
1089 |
|
return(atp); |
1090 |
|
} |
1091 |
|
|
1111 |
|
/* transfer values at this node */ |
1112 |
|
for (av = at->alist; av != NULL; av = at->alist) { |
1113 |
|
at->alist = av->next; |
1114 |
+ |
av->next = NULL; |
1115 |
|
(*f)(av); |
1116 |
|
} |
1117 |
|
if (at->kid == NULL) |
1189 |
|
{ |
1190 |
|
AMBVAL **avlpp; |
1191 |
|
|
1192 |
< |
avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2, |
1193 |
< |
nambvals, sizeof(AMBVAL *), &aposcmp); |
1192 |
> |
avlpp = (AMBVAL **)bsearch(&avaddr, avlist2, |
1193 |
> |
nambvals, sizeof(AMBVAL *), aposcmp); |
1194 |
|
if (avlpp == NULL) |
1195 |
|
error(CONSISTENCY, "address not found in avlmemi"); |
1196 |
|
return(avlpp - avlist2); |
1233 |
|
} |
1234 |
|
if (avlist1 == NULL) { /* no time tracking -- rebuild tree? */ |
1235 |
|
if (avlist2 != NULL) |
1236 |
< |
free((void *)avlist2); |
1236 |
> |
free(avlist2); |
1237 |
|
if (always) { /* rebuild without sorting */ |
1238 |
|
oldatrunk = atrunk; |
1239 |
|
atrunk.alist = NULL; |
1240 |
|
atrunk.kid = NULL; |
1241 |
< |
unloadatree(&oldatrunk, &avinsert); |
1241 |
> |
unloadatree(&oldatrunk, avinsert); |
1242 |
|
} |
1243 |
|
} else { /* sort memory by last access time */ |
1244 |
|
/* |
1255 |
|
eputs(errmsg); |
1256 |
|
#endif |
1257 |
|
i_avlist = 0; |
1258 |
< |
unloadatree(&atrunk, &av2list); /* empty current tree */ |
1258 |
> |
unloadatree(&atrunk, av2list); /* empty current tree */ |
1259 |
|
#ifdef DEBUG |
1260 |
|
if (i_avlist < nambvals) |
1261 |
|
error(CONSISTENCY, "missing ambient values in sortambvals"); |
1262 |
|
#endif |
1263 |
< |
qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp); |
1264 |
< |
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
1263 |
> |
qsort(avlist1, nambvals, sizeof(struct avl), alatcmp); |
1264 |
> |
qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
1265 |
|
for (i = 0; i < nambvals; i++) { |
1266 |
|
if (avlist1[i].p == NULL) |
1267 |
|
continue; |
1277 |
|
avinsert(avlist2[j]); |
1278 |
|
avlist1[j].p = NULL; |
1279 |
|
} |
1280 |
< |
free((void *)avlist1); |
1281 |
< |
free((void *)avlist2); |
1280 |
> |
free(avlist1); |
1281 |
> |
free(avlist2); |
1282 |
|
/* compute new sort interval */ |
1283 |
|
sortintvl = ambclock - lastsort; |
1284 |
|
if (sortintvl >= MAX_SORT_INTVL/2) |