1 |
< |
/* Copyright (c) 1995 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1996 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
18 |
|
|
19 |
|
#include "random.h" |
20 |
|
|
21 |
< |
#define OCTSCALE 0.5 /* ceil((valid rad.)/(cube size)) */ |
21 |
> |
#ifndef OCTSCALE |
22 |
> |
#define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ |
23 |
> |
#endif |
24 |
|
|
25 |
|
typedef struct ambtree { |
26 |
|
AMBVAL *alist; /* ambient value list */ |
50 |
|
#endif |
51 |
|
#endif |
52 |
|
#ifndef SORT_INTVL |
53 |
< |
#define SORT_INTVL (SORT_THRESH*32) |
53 |
> |
#define SORT_INTVL (SORT_THRESH<<1) |
54 |
|
#endif |
55 |
|
#ifndef MAX_SORT_INTVL |
56 |
< |
#define MAX_SORT_INTVL (SORT_INTVL<<8) |
56 |
> |
#define MAX_SORT_INTVL (SORT_INTVL<<6) |
57 |
|
#endif |
58 |
|
|
59 |
+ |
static double avsum = 0.; /* computed ambient value sum (log) */ |
60 |
+ |
static unsigned int navsum = 0; /* number of values in avsum */ |
61 |
+ |
static unsigned int nambvals = 0; /* total number of indirect values */ |
62 |
+ |
static unsigned int nambshare = 0; /* number of values from file */ |
63 |
|
static unsigned long ambclock = 0; /* ambient access clock */ |
58 |
– |
static unsigned int nambvals = 0; /* number of stored ambient values */ |
64 |
|
static unsigned long lastsort = 0; /* time of last value sort */ |
65 |
|
static long sortintvl = SORT_INTVL; /* time until next sort */ |
66 |
|
|
68 |
|
/* |
69 |
|
* Track access times unless we are sharing ambient values |
70 |
|
* through memory on a multiprocessor, when we want to avoid |
71 |
< |
* claiming our own memory (copy on write). |
71 |
> |
* claiming our own memory (copy on write). Go ahead anyway |
72 |
> |
* if more than two thirds of our values are unshared. |
73 |
> |
* Compile with -Dtracktime=0 to turn this code off. |
74 |
|
*/ |
75 |
< |
#define tracktime (shm_boundary == NULL || ambfp == NULL) |
75 |
> |
#ifndef tracktime |
76 |
> |
#define tracktime (shm_boundary == NULL || nambvals > 3*nambshare) |
77 |
> |
#endif |
78 |
|
|
79 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
80 |
|
|
81 |
|
#define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL)) |
82 |
|
|
83 |
|
extern long ftell(), lseek(); |
84 |
< |
static int initambfile(), avsave(), avinsert(), sortambvals(); |
84 |
> |
static int initambfile(), avsave(), avinsert(), sortambvals(), avlmemi(); |
85 |
|
static AMBVAL *avstore(); |
86 |
|
#ifdef F_SETLKW |
87 |
|
static aflock(); |
98 |
|
maxarad = thescene.cusize / 2.0; |
99 |
|
} else { |
100 |
|
minarad = thescene.cusize / ar; |
101 |
< |
maxarad = 16 * minarad; /* heuristic */ |
101 |
> |
maxarad = 64 * minarad; /* heuristic */ |
102 |
|
if (maxarad > thescene.cusize / 2.0) |
103 |
|
maxarad = thescene.cusize / 2.0; |
104 |
|
} |
112 |
|
setambacc(newa) /* set ambient accuracy */ |
113 |
|
double newa; |
114 |
|
{ |
115 |
< |
static double oldambacc = -1.0; |
115 |
> |
double ambdiff; |
116 |
|
|
117 |
< |
ambacc = newa < 0.0 ? 0.0 : newa; /* may be done already */ |
118 |
< |
if (oldambacc < -FTINY) |
119 |
< |
oldambacc = ambacc; /* do nothing first call */ |
120 |
< |
if (fabs(newa - oldambacc) < 0.01) |
121 |
< |
return; /* insignificant -- don't bother */ |
113 |
< |
if (ambacc <= FTINY) |
114 |
< |
return; /* cannot build new tree */ |
115 |
< |
/* else need to rebuild tree */ |
116 |
< |
sortambvals(1); |
117 |
< |
oldambacc = ambacc; /* remeber setting for next call */ |
117 |
> |
if (newa < 0.0) |
118 |
> |
newa = 0.0; |
119 |
> |
ambdiff = fabs(newa - ambacc); |
120 |
> |
if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0) |
121 |
> |
sortambvals(1); /* rebuild tree */ |
122 |
|
} |
123 |
|
|
124 |
|
|
125 |
|
setambient(afile) /* initialize calculation */ |
126 |
|
char *afile; |
127 |
|
{ |
128 |
< |
long headlen; |
128 |
> |
long pos, flen; |
129 |
|
AMBVAL amb; |
130 |
|
/* init ambient limits */ |
131 |
|
setambres(ambres); |
141 |
|
/* open ambient file */ |
142 |
|
if ((ambfp = fopen(afile, "r+")) != NULL) { |
143 |
|
initambfile(0); |
144 |
< |
headlen = ftell(ambfp); |
144 |
> |
pos = ftell(ambfp); |
145 |
|
while (readambval(&amb, ambfp)) |
146 |
|
avinsert(avstore(&amb)); |
147 |
|
/* align */ |
148 |
< |
fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); |
148 |
> |
pos += (long)nambvals*AMBVALSIZ; |
149 |
> |
flen = lseek(fileno(ambfp), 0L, 2); |
150 |
> |
if (flen != pos) { |
151 |
> |
sprintf(errmsg, |
152 |
> |
"ignoring last %ld values in ambient file (corrupted)", |
153 |
> |
(flen - pos)/AMBVALSIZ); |
154 |
> |
error(WARNING, errmsg); |
155 |
> |
fseek(ambfp, pos, 0); |
156 |
> |
ftruncate(fileno(ambfp), pos); |
157 |
> |
} |
158 |
> |
nambshare = nambvals; |
159 |
|
} else if ((ambfp = fopen(afile, "w+")) != NULL) |
160 |
|
initambfile(1); |
161 |
|
else { |
189 |
|
} |
190 |
|
|
191 |
|
|
192 |
< |
ambient(acol, r) /* compute ambient component for ray */ |
192 |
> |
ambient(acol, r, nrm) /* compute ambient component for ray */ |
193 |
|
COLOR acol; |
194 |
|
register RAY *r; |
195 |
+ |
FVECT nrm; |
196 |
|
{ |
197 |
|
static int rdepth = 0; /* ambient recursion */ |
198 |
< |
double d; |
198 |
> |
double d, l; |
199 |
|
|
200 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
201 |
|
goto dumbamb; |
211 |
|
rdepth++; |
212 |
|
d = doambient(acol, r, r->rweight, NULL, NULL); |
213 |
|
rdepth--; |
214 |
< |
if (d == 0.0) |
214 |
> |
if (d <= FTINY) |
215 |
|
goto dumbamb; |
216 |
|
return; |
217 |
|
} |
218 |
< |
/* resort memory? */ |
219 |
< |
sortambvals(0); |
218 |
> |
|
219 |
> |
if (tracktime) /* sort to minimize thrashing */ |
220 |
> |
sortambvals(0); |
221 |
|
/* get ambient value */ |
222 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
223 |
< |
d = sumambient(acol, r, rdepth, |
223 |
> |
d = sumambient(acol, r, nrm, rdepth, |
224 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
225 |
< |
if (d > FTINY) |
225 |
> |
if (d > FTINY) { |
226 |
|
scalecolor(acol, 1.0/d); |
227 |
< |
else { |
212 |
< |
d = makeambient(acol, r, rdepth++); |
213 |
< |
rdepth--; |
227 |
> |
return; |
228 |
|
} |
229 |
+ |
rdepth++; /* need to cache new value */ |
230 |
+ |
d = makeambient(acol, r, nrm, rdepth-1); |
231 |
+ |
rdepth--; |
232 |
|
if (d > FTINY) |
233 |
|
return; |
234 |
|
dumbamb: /* return global value */ |
235 |
|
copycolor(acol, ambval); |
236 |
+ |
if (ambvwt <= 0 | navsum == 0) |
237 |
+ |
return; |
238 |
+ |
l = bright(ambval); /* average in computations */ |
239 |
+ |
if (l > FTINY) { |
240 |
+ |
d = (log(l)*(double)ambvwt + avsum) / |
241 |
+ |
(double)(ambvwt + navsum); |
242 |
+ |
d = exp(d) / l; |
243 |
+ |
scalecolor(acol, d); /* apply color of ambval */ |
244 |
+ |
} else { |
245 |
+ |
d = exp( avsum / (double)navsum ); |
246 |
+ |
setcolor(acol, d, d, d); /* neutral color */ |
247 |
+ |
} |
248 |
|
} |
249 |
|
|
250 |
|
|
251 |
|
double |
252 |
< |
sumambient(acol, r, al, at, c0, s) /* get interpolated ambient value */ |
252 |
> |
sumambient(acol, r, rn, al, at, c0, s) /* get interpolated ambient value */ |
253 |
|
COLOR acol; |
254 |
|
register RAY *r; |
255 |
+ |
FVECT rn; |
256 |
|
int al; |
257 |
|
AMBTREE *at; |
258 |
|
FVECT c0; |
264 |
|
int i; |
265 |
|
register int j; |
266 |
|
register AMBVAL *av; |
267 |
< |
/* do this node */ |
267 |
> |
|
268 |
|
wsum = 0.0; |
269 |
+ |
/* do this node */ |
270 |
|
for (av = at->alist; av != NULL; av = av->next) { |
271 |
|
if (tracktime) |
272 |
< |
av->latick = ambclock++; |
272 |
> |
av->latick = ambclock; |
273 |
|
/* |
274 |
|
* Ambient level test. |
275 |
|
*/ |
280 |
|
/* |
281 |
|
* Ambient radius test. |
282 |
|
*/ |
283 |
< |
e1 = 0.0; |
284 |
< |
for (j = 0; j < 3; j++) { |
285 |
< |
d = av->pos[j] - r->rop[j]; |
286 |
< |
e1 += d * d; |
287 |
< |
} |
283 |
> |
d = av->pos[0] - r->rop[0]; |
284 |
> |
e1 = d * d; |
285 |
> |
d = av->pos[1] - r->rop[1]; |
286 |
> |
e1 += d * d; |
287 |
> |
d = av->pos[2] - r->rop[2]; |
288 |
> |
e1 += d * d; |
289 |
|
e1 /= av->rad * av->rad; |
290 |
|
if (e1 > ambacc*ambacc*1.21) |
291 |
|
continue; |
309 |
|
* Jittering final test reduces image artifacts. |
310 |
|
*/ |
311 |
|
wt = sqrt(e1) + sqrt(e2); |
312 |
< |
wt *= .9 + .2*urand(9015+samplendx); |
281 |
< |
if (wt > ambacc) |
312 |
> |
if (wt > ambacc*(.9+.2*urand(9015+samplendx))) |
313 |
|
continue; |
314 |
|
if (wt <= 1e-3) |
315 |
|
wt = 1e3; |
316 |
|
else |
317 |
|
wt = 1.0 / wt; |
318 |
|
wsum += wt; |
319 |
< |
extambient(ct, av, r->rop, r->ron); |
319 |
> |
extambient(ct, av, r->rop, rn); |
320 |
|
scalecolor(ct, wt); |
321 |
|
addcolor(acol, ct); |
322 |
|
} |
335 |
|
break; |
336 |
|
} |
337 |
|
if (j == 3) |
338 |
< |
wsum += sumambient(acol, r, al, at->kid+i, ck0, s); |
338 |
> |
wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s); |
339 |
|
} |
340 |
|
return(wsum); |
341 |
|
} |
342 |
|
|
343 |
|
|
344 |
|
double |
345 |
< |
makeambient(acol, r, al) /* make a new ambient value */ |
345 |
> |
makeambient(acol, r, rn, al) /* make a new ambient value */ |
346 |
|
COLOR acol; |
347 |
|
register RAY *r; |
348 |
+ |
FVECT rn; |
349 |
|
int al; |
350 |
|
{ |
351 |
|
AMBVAL amb; |
352 |
|
FVECT gp, gd; |
353 |
|
/* compute weight */ |
354 |
|
amb.weight = pow(AVGREFL, (double)al); |
355 |
< |
if (r->rweight < 0.2*amb.weight) /* heuristic */ |
355 |
> |
if (r->rweight < 0.1*amb.weight) /* heuristic */ |
356 |
|
amb.weight = r->rweight; |
357 |
|
/* compute ambient */ |
358 |
|
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
359 |
< |
if (amb.rad == 0.0) |
359 |
> |
if (amb.rad <= FTINY) |
360 |
|
return(0.0); |
361 |
|
/* store it */ |
362 |
|
VCOPY(amb.pos, r->rop); |
367 |
|
VCOPY(amb.gdir, gd); |
368 |
|
/* insert into tree */ |
369 |
|
avsave(&amb); /* and save to file */ |
370 |
+ |
if (rn != r->ron) |
371 |
+ |
extambient(acol, &amb, r->rop, rn); /* texture */ |
372 |
|
return(amb.rad); |
373 |
|
} |
374 |
|
|
414 |
|
setbuf(ambfp, bmalloc(BUFSIZ+8)); |
415 |
|
if (creat) { /* new file */ |
416 |
|
newheader("RADIANCE", ambfp); |
417 |
< |
fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ", |
417 |
> |
fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ", |
418 |
|
progname, colval(ambval,RED), |
419 |
|
colval(ambval,GRN), colval(ambval,BLU), |
420 |
< |
ambounce, ambacc); |
420 |
> |
ambvwt, ambounce, ambacc); |
421 |
|
fprintf(ambfp, "-ad %d -as %d -ar %d %s\n", |
422 |
|
ambdiv, ambssamp, ambres, |
423 |
|
octname==NULL ? "" : octname); |
453 |
|
register AMBVAL *aval; |
454 |
|
{ |
455 |
|
register AMBVAL *av; |
456 |
+ |
double d; |
457 |
|
|
458 |
|
if ((av = newambval()) == NULL) |
459 |
|
error(SYSTEM, "out of memory in avstore"); |
461 |
|
av->latick = ambclock; |
462 |
|
av->next = NULL; |
463 |
|
nambvals++; |
464 |
+ |
d = bright(av->val); |
465 |
+ |
if (d > FTINY) { /* add to log sum for averaging */ |
466 |
+ |
avsum += log(d); |
467 |
+ |
navsum++; |
468 |
+ |
} |
469 |
|
return(av); |
470 |
|
} |
471 |
|
|
568 |
|
} |
569 |
|
|
570 |
|
|
571 |
< |
static AMBVAL **avlist1, **avlist2; /* ambient value lists for sorting */ |
571 |
> |
static struct avl { |
572 |
> |
AMBVAL *p; |
573 |
> |
unsigned long t; |
574 |
> |
} *avlist1; /* ambient value list with ticks */ |
575 |
> |
static AMBVAL **avlist2; /* memory positions for sorting */ |
576 |
|
static int i_avlist; /* index for lists */ |
577 |
|
|
578 |
|
|
579 |
|
static |
580 |
|
av2list(av) |
581 |
< |
AMBVAL *av; |
581 |
> |
register AMBVAL *av; |
582 |
|
{ |
583 |
|
#ifdef DEBUG |
584 |
|
if (i_avlist >= nambvals) |
585 |
|
error(CONSISTENCY, "too many ambient values in av2list1"); |
586 |
|
#endif |
587 |
< |
avlist1[i_avlist] = avlist2[i_avlist] = av; |
588 |
< |
i_avlist++; |
587 |
> |
avlist1[i_avlist].p = avlist2[i_avlist] = av; |
588 |
> |
avlist1[i_avlist++].t = av->latick; |
589 |
|
} |
590 |
|
|
591 |
|
|
592 |
|
static int |
593 |
< |
alatcmp(avp1, avp2) /* compare ambient values for MRA */ |
594 |
< |
AMBVAL **avp1, **avp2; |
593 |
> |
alatcmp(av1, av2) /* compare ambient values for MRA */ |
594 |
> |
struct avl *av1, *av2; |
595 |
|
{ |
596 |
< |
return((**avp2).latick - (**avp1).latick); |
596 |
> |
register long lc = av2->t - av1->t; |
597 |
> |
return(lc<0 ? -1 : lc>0 ? 1 : 0); |
598 |
|
} |
599 |
|
|
600 |
|
|
606 |
|
} |
607 |
|
|
608 |
|
|
609 |
< |
#ifdef DEBUG |
609 |
> |
#if 1 |
610 |
|
static int |
611 |
|
avlmemi(avaddr) /* find list position from address */ |
612 |
|
AMBVAL *avaddr; |
633 |
|
AMBVAL tav, *tap, *pnext; |
634 |
|
register int i, j; |
635 |
|
/* see if it's time yet */ |
636 |
< |
if (!always && (ambclock < lastsort+sortintvl || |
636 |
> |
if (!always && (ambclock++ < lastsort+sortintvl || |
637 |
|
nambvals < SORT_THRESH)) |
638 |
|
return; |
639 |
|
/* |
652 |
|
* tree will be rebuilt with the new accuracy parameter. |
653 |
|
*/ |
654 |
|
if (tracktime) { /* allocate pointer arrays to sort */ |
610 |
– |
avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
655 |
|
avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
656 |
< |
} else |
657 |
< |
avlist1 = avlist2 = NULL; |
658 |
< |
if (avlist2 == NULL) { /* no time tracking -- rebuild tree? */ |
659 |
< |
if (avlist1 != NULL) |
660 |
< |
free((char *)avlist1); |
656 |
> |
avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl)); |
657 |
> |
} else { |
658 |
> |
avlist2 = NULL; |
659 |
> |
avlist1 = NULL; |
660 |
> |
} |
661 |
> |
if (avlist1 == NULL) { /* no time tracking -- rebuild tree? */ |
662 |
> |
if (avlist2 != NULL) |
663 |
> |
free((char *)avlist2); |
664 |
|
if (always) { /* rebuild without sorting */ |
665 |
|
copystruct(&oldatrunk, &atrunk); |
666 |
|
atrunk.alist = NULL; |
677 |
|
* when we're thrashing, which is when we need to do it. |
678 |
|
*/ |
679 |
|
#ifdef DEBUG |
680 |
< |
sprintf(errmsg, "sorting %u ambient values...", nambvals); |
680 |
> |
sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...", |
681 |
> |
nambvals, ambclock); |
682 |
|
eputs(errmsg); |
683 |
|
#endif |
684 |
|
i_avlist = 0; |
687 |
|
if (i_avlist < nambvals) |
688 |
|
error(CONSISTENCY, "missing ambient values in sortambvals"); |
689 |
|
#endif |
690 |
< |
qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp); |
690 |
> |
qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp); |
691 |
|
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
692 |
|
for (i = 0; i < nambvals; i++) { |
693 |
< |
if (avlist1[i] == NULL) |
693 |
> |
if (avlist1[i].p == NULL) |
694 |
|
continue; |
695 |
|
tap = avlist2[i]; |
696 |
|
copystruct(&tav, tap); |
697 |
< |
for (j = i; (pnext = avlist1[j]) != tap; |
697 |
> |
for (j = i; (pnext = avlist1[j].p) != tap; |
698 |
|
j = avlmemi(pnext)) { |
699 |
|
copystruct(avlist2[j], pnext); |
700 |
|
avinsert(avlist2[j]); |
701 |
< |
avlist1[j] = NULL; |
701 |
> |
avlist1[j].p = NULL; |
702 |
|
} |
703 |
|
copystruct(avlist2[j], &tav); |
704 |
|
avinsert(avlist2[j]); |
705 |
< |
avlist1[j] = NULL; |
705 |
> |
avlist1[j].p = NULL; |
706 |
|
} |
707 |
|
free((char *)avlist1); |
708 |
|
free((char *)avlist2); |
709 |
|
/* compute new sort interval */ |
710 |
|
sortintvl = ambclock - lastsort; |
711 |
< |
if (sortintvl > MAX_SORT_INTVL) |
711 |
> |
if (sortintvl >= MAX_SORT_INTVL/2) |
712 |
|
sortintvl = MAX_SORT_INTVL; |
713 |
|
else |
714 |
|
sortintvl <<= 1; /* wait twice as long next */ |
763 |
|
if (fseek(ambinp, lastpos, 0) < 0) |
764 |
|
goto seekerr; |
765 |
|
while (n >= AMBVALSIZ) { /* load contributed values */ |
766 |
< |
readambval(&avs, ambinp); |
766 |
> |
if (!readambval(&avs, ambinp)) { |
767 |
> |
sprintf(errmsg, |
768 |
> |
"ambient file corrupted near character %ld", |
769 |
> |
flen - n); |
770 |
> |
error(WARNING, errmsg); |
771 |
> |
break; |
772 |
> |
} |
773 |
|
avinsert(avstore(&avs)); |
774 |
|
n -= AMBVALSIZ; |
775 |
|
} |