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 |
+ |
#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*256) |
53 |
> |
#define SORT_INTVL (SORT_THRESH<<1) |
54 |
|
#endif |
55 |
|
#ifndef MAX_SORT_INTVL |
56 |
< |
#define MAX_SORT_INTVL (SORT_INTVL<<4) |
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(); |
75 |
< |
static int initambfile(), avsave(), avinsert(), sortambvals(); |
83 |
> |
static int initambfile(), avsave(), avinsert(), sortambvals(), avlmemi(); |
84 |
|
static AMBVAL *avstore(); |
85 |
|
#ifdef F_SETLKW |
86 |
|
static aflock(); |
111 |
|
setambacc(newa) /* set ambient accuracy */ |
112 |
|
double newa; |
113 |
|
{ |
114 |
< |
static double oldambacc = -1.0; |
114 |
> |
double ambdiff; |
115 |
|
|
116 |
< |
ambacc = newa < 0.0 ? 0.0 : newa; /* may be done already */ |
117 |
< |
if (oldambacc < -FTINY) |
118 |
< |
oldambacc = ambacc; /* do nothing first call */ |
119 |
< |
if (fabs(newa - oldambacc) < 0.01) |
120 |
< |
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 */ |
116 |
> |
if (newa < 0.0) |
117 |
> |
newa = 0.0; |
118 |
> |
ambdiff = fabs(newa - ambacc); |
119 |
> |
if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0) |
120 |
> |
sortambvals(1); /* rebuild tree */ |
121 |
|
} |
122 |
|
|
123 |
|
|
124 |
|
setambient(afile) /* initialize calculation */ |
125 |
|
char *afile; |
126 |
|
{ |
127 |
< |
long headlen; |
127 |
> |
long pos, flen; |
128 |
|
AMBVAL amb; |
129 |
|
/* init ambient limits */ |
130 |
|
setambres(ambres); |
140 |
|
/* open ambient file */ |
141 |
|
if ((ambfp = fopen(afile, "r+")) != NULL) { |
142 |
|
initambfile(0); |
143 |
< |
headlen = ftell(ambfp); |
143 |
> |
pos = ftell(ambfp); |
144 |
|
while (readambval(&amb, ambfp)) |
145 |
|
avinsert(avstore(&amb)); |
146 |
|
/* align */ |
147 |
< |
fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); |
147 |
> |
pos += (long)nambvals*AMBVALSIZ; |
148 |
> |
flen = lseek(fileno(ambfp), 0L, 2); |
149 |
> |
if (flen != pos) { |
150 |
> |
sprintf(errmsg, |
151 |
> |
"ignoring last %ld values in ambient file (corrupted)", |
152 |
> |
(flen - pos)/AMBVALSIZ); |
153 |
> |
error(WARNING, errmsg); |
154 |
> |
fseek(ambfp, pos, 0); |
155 |
> |
ftruncate(fileno(ambfp), pos); |
156 |
> |
} |
157 |
> |
nambshare = nambvals; |
158 |
|
} else if ((ambfp = fopen(afile, "w+")) != NULL) |
159 |
|
initambfile(1); |
160 |
|
else { |
188 |
|
} |
189 |
|
|
190 |
|
|
191 |
< |
ambient(acol, r) /* compute ambient component for ray */ |
191 |
> |
ambient(acol, r, nrm) /* compute ambient component for ray */ |
192 |
|
COLOR acol; |
193 |
|
register RAY *r; |
194 |
+ |
FVECT nrm; |
195 |
|
{ |
196 |
|
static int rdepth = 0; /* ambient recursion */ |
197 |
< |
double d; |
197 |
> |
double d, l; |
198 |
|
|
199 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
200 |
|
goto dumbamb; |
210 |
|
rdepth++; |
211 |
|
d = doambient(acol, r, r->rweight, NULL, NULL); |
212 |
|
rdepth--; |
213 |
< |
if (d == 0.0) |
213 |
> |
if (d <= FTINY) |
214 |
|
goto dumbamb; |
215 |
|
return; |
216 |
|
} |
217 |
< |
/* resort memory? */ |
218 |
< |
sortambvals(0); |
217 |
> |
|
218 |
> |
if (tracktime) /* sort to minimize thrashing */ |
219 |
> |
sortambvals(0); |
220 |
|
/* get ambient value */ |
221 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
222 |
< |
d = sumambient(acol, r, rdepth, |
222 |
> |
d = sumambient(acol, r, nrm, rdepth, |
223 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
224 |
< |
if (d > FTINY) |
224 |
> |
if (d > FTINY) { |
225 |
|
scalecolor(acol, 1.0/d); |
226 |
< |
else { |
212 |
< |
d = makeambient(acol, r, rdepth++); |
213 |
< |
rdepth--; |
226 |
> |
return; |
227 |
|
} |
228 |
+ |
rdepth++; /* need to cache new value */ |
229 |
+ |
d = makeambient(acol, r, nrm, rdepth-1); |
230 |
+ |
rdepth--; |
231 |
|
if (d > FTINY) |
232 |
|
return; |
233 |
|
dumbamb: /* return global value */ |
234 |
|
copycolor(acol, ambval); |
235 |
+ |
if (ambvwt <= 0 | navsum == 0) |
236 |
+ |
return; |
237 |
+ |
l = bright(ambval); /* average in computations */ |
238 |
+ |
if (l > FTINY) { |
239 |
+ |
d = (log(l)*(double)ambvwt + avsum) / |
240 |
+ |
(double)(ambvwt + navsum); |
241 |
+ |
d = exp(d) / l; |
242 |
+ |
scalecolor(acol, d); /* apply color of ambval */ |
243 |
+ |
} else { |
244 |
+ |
d = exp( avsum / (double)navsum ); |
245 |
+ |
setcolor(acol, d, d, d); /* neutral color */ |
246 |
+ |
} |
247 |
|
} |
248 |
|
|
249 |
|
|
250 |
|
double |
251 |
< |
sumambient(acol, r, al, at, c0, s) /* get interpolated ambient value */ |
251 |
> |
sumambient(acol, r, rn, al, at, c0, s) /* get interpolated ambient value */ |
252 |
|
COLOR acol; |
253 |
|
register RAY *r; |
254 |
+ |
FVECT rn; |
255 |
|
int al; |
256 |
|
AMBTREE *at; |
257 |
|
FVECT c0; |
268 |
|
/* do this node */ |
269 |
|
for (av = at->alist; av != NULL; av = av->next) { |
270 |
|
if (tracktime) |
271 |
< |
av->latick = ambclock++; |
271 |
> |
av->latick = ambclock; |
272 |
|
/* |
273 |
|
* Ambient level test. |
274 |
|
*/ |
315 |
|
else |
316 |
|
wt = 1.0 / wt; |
317 |
|
wsum += wt; |
318 |
< |
extambient(ct, av, r->rop, r->ron); |
318 |
> |
extambient(ct, av, r->rop, rn); |
319 |
|
scalecolor(ct, wt); |
320 |
|
addcolor(acol, ct); |
321 |
|
} |
334 |
|
break; |
335 |
|
} |
336 |
|
if (j == 3) |
337 |
< |
wsum += sumambient(acol, r, al, at->kid+i, ck0, s); |
337 |
> |
wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s); |
338 |
|
} |
339 |
|
return(wsum); |
340 |
|
} |
341 |
|
|
342 |
|
|
343 |
|
double |
344 |
< |
makeambient(acol, r, al) /* make a new ambient value */ |
344 |
> |
makeambient(acol, r, rn, al) /* make a new ambient value */ |
345 |
|
COLOR acol; |
346 |
|
register RAY *r; |
347 |
+ |
FVECT rn; |
348 |
|
int al; |
349 |
|
{ |
350 |
|
AMBVAL amb; |
351 |
|
FVECT gp, gd; |
352 |
|
/* compute weight */ |
353 |
|
amb.weight = pow(AVGREFL, (double)al); |
354 |
< |
if (r->rweight < 0.2*amb.weight) /* heuristic */ |
354 |
> |
if (r->rweight < 0.1*amb.weight) /* heuristic */ |
355 |
|
amb.weight = r->rweight; |
356 |
|
/* compute ambient */ |
357 |
|
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
358 |
< |
if (amb.rad == 0.0) |
358 |
> |
if (amb.rad <= FTINY) |
359 |
|
return(0.0); |
360 |
|
/* store it */ |
361 |
|
VCOPY(amb.pos, r->rop); |
366 |
|
VCOPY(amb.gdir, gd); |
367 |
|
/* insert into tree */ |
368 |
|
avsave(&amb); /* and save to file */ |
369 |
+ |
if (rn != r->ron) |
370 |
+ |
extambient(acol, &amb, r->rop, rn); /* texture */ |
371 |
|
return(amb.rad); |
372 |
|
} |
373 |
|
|
377 |
|
register AMBVAL *ap; |
378 |
|
FVECT pv, nv; |
379 |
|
{ |
380 |
< |
FVECT v1, v2; |
380 |
> |
FVECT v1; |
381 |
|
register int i; |
382 |
|
double d; |
383 |
|
|
386 |
|
for (i = 0; i < 3; i++) |
387 |
|
d += ap->gpos[i]*(pv[i]-ap->pos[i]); |
388 |
|
/* gradient due to rotation */ |
389 |
< |
VCOPY(v1, ap->dir); |
390 |
< |
fcross(v2, v1, nv); |
359 |
< |
d += DOT(ap->gdir, v2); |
389 |
> |
VCROSS(v1, ap->dir, nv); |
390 |
> |
d += DOT(ap->gdir, v1); |
391 |
|
if (d <= 0.0) { |
392 |
|
setcolor(cr, 0.0, 0.0, 0.0); |
393 |
|
return; |
412 |
|
setbuf(ambfp, bmalloc(BUFSIZ+8)); |
413 |
|
if (creat) { /* new file */ |
414 |
|
newheader("RADIANCE", ambfp); |
415 |
< |
fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ", |
415 |
> |
fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ", |
416 |
|
progname, colval(ambval,RED), |
417 |
|
colval(ambval,GRN), colval(ambval,BLU), |
418 |
< |
ambounce, ambacc); |
418 |
> |
ambvwt, ambounce, ambacc); |
419 |
|
fprintf(ambfp, "-ad %d -as %d -ar %d %s\n", |
420 |
|
ambdiv, ambssamp, ambres, |
421 |
|
octname==NULL ? "" : octname); |
451 |
|
register AMBVAL *aval; |
452 |
|
{ |
453 |
|
register AMBVAL *av; |
454 |
+ |
double d; |
455 |
|
|
456 |
|
if ((av = newambval()) == NULL) |
457 |
|
error(SYSTEM, "out of memory in avstore"); |
459 |
|
av->latick = ambclock; |
460 |
|
av->next = NULL; |
461 |
|
nambvals++; |
462 |
+ |
d = bright(av->val); |
463 |
+ |
if (d > FTINY) { /* add to log sum for averaging */ |
464 |
+ |
avsum += log(d); |
465 |
+ |
navsum++; |
466 |
+ |
} |
467 |
|
return(av); |
468 |
|
} |
469 |
|
|
566 |
|
} |
567 |
|
|
568 |
|
|
569 |
< |
static AMBVAL **avlist1, **avlist2; /* ambient value lists for sorting */ |
569 |
> |
static struct avl { |
570 |
> |
AMBVAL *p; |
571 |
> |
unsigned long t; |
572 |
> |
} *avlist1; /* ambient value list with ticks */ |
573 |
> |
static AMBVAL **avlist2; /* memory positions for sorting */ |
574 |
|
static int i_avlist; /* index for lists */ |
575 |
|
|
576 |
|
|
577 |
|
static |
578 |
|
av2list(av) |
579 |
< |
AMBVAL *av; |
579 |
> |
register AMBVAL *av; |
580 |
|
{ |
581 |
|
#ifdef DEBUG |
582 |
|
if (i_avlist >= nambvals) |
583 |
|
error(CONSISTENCY, "too many ambient values in av2list1"); |
584 |
|
#endif |
585 |
< |
avlist1[i_avlist] = avlist2[i_avlist] = av; |
586 |
< |
i_avlist++; |
585 |
> |
avlist1[i_avlist].p = avlist2[i_avlist] = av; |
586 |
> |
avlist1[i_avlist++].t = av->latick; |
587 |
|
} |
588 |
|
|
589 |
|
|
590 |
|
static int |
591 |
< |
alatcmp(avp1, avp2) /* compare ambient values for MRA */ |
592 |
< |
AMBVAL **avp1, **avp2; |
591 |
> |
alatcmp(av1, av2) /* compare ambient values for MRA */ |
592 |
> |
struct avl *av1, *av2; |
593 |
|
{ |
594 |
< |
return((**avp2).latick - (**avp1).latick); |
594 |
> |
register long lc = av2->t - av1->t; |
595 |
> |
return(lc<0 ? -1 : lc>0 ? 1 : 0); |
596 |
|
} |
597 |
|
|
598 |
|
|
604 |
|
} |
605 |
|
|
606 |
|
|
607 |
< |
#ifdef DEBUG |
607 |
> |
#if 1 |
608 |
|
static int |
609 |
|
avlmemi(avaddr) /* find list position from address */ |
610 |
|
AMBVAL *avaddr; |
631 |
|
AMBVAL tav, *tap, *pnext; |
632 |
|
register int i, j; |
633 |
|
/* see if it's time yet */ |
634 |
< |
if (!always && (ambclock < lastsort+sortintvl || |
634 |
> |
if (!always && (ambclock++ < lastsort+sortintvl || |
635 |
|
nambvals < SORT_THRESH)) |
636 |
|
return; |
637 |
|
/* |
650 |
|
* tree will be rebuilt with the new accuracy parameter. |
651 |
|
*/ |
652 |
|
if (tracktime) { /* allocate pointer arrays to sort */ |
611 |
– |
avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
653 |
|
avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
654 |
< |
} else |
655 |
< |
avlist1 = avlist2 = NULL; |
656 |
< |
if (avlist2 == NULL) { /* no time tracking -- rebuild tree? */ |
657 |
< |
if (avlist1 != NULL) |
658 |
< |
free((char *)avlist1); |
654 |
> |
avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl)); |
655 |
> |
} else { |
656 |
> |
avlist2 = NULL; |
657 |
> |
avlist1 = NULL; |
658 |
> |
} |
659 |
> |
if (avlist1 == NULL) { /* no time tracking -- rebuild tree? */ |
660 |
> |
if (avlist2 != NULL) |
661 |
> |
free((char *)avlist2); |
662 |
|
if (always) { /* rebuild without sorting */ |
663 |
|
copystruct(&oldatrunk, &atrunk); |
664 |
|
atrunk.alist = NULL; |
685 |
|
if (i_avlist < nambvals) |
686 |
|
error(CONSISTENCY, "missing ambient values in sortambvals"); |
687 |
|
#endif |
688 |
< |
qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp); |
688 |
> |
qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp); |
689 |
|
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
690 |
|
for (i = 0; i < nambvals; i++) { |
691 |
< |
if (avlist1[i] == NULL) |
691 |
> |
if (avlist1[i].p == NULL) |
692 |
|
continue; |
693 |
|
tap = avlist2[i]; |
694 |
|
copystruct(&tav, tap); |
695 |
< |
for (j = i; (pnext = avlist1[j]) != tap; |
695 |
> |
for (j = i; (pnext = avlist1[j].p) != tap; |
696 |
|
j = avlmemi(pnext)) { |
697 |
|
copystruct(avlist2[j], pnext); |
698 |
|
avinsert(avlist2[j]); |
699 |
< |
avlist1[j] = NULL; |
699 |
> |
avlist1[j].p = NULL; |
700 |
|
} |
701 |
|
copystruct(avlist2[j], &tav); |
702 |
|
avinsert(avlist2[j]); |
703 |
< |
avlist1[j] = NULL; |
703 |
> |
avlist1[j].p = NULL; |
704 |
|
} |
705 |
|
free((char *)avlist1); |
706 |
|
free((char *)avlist2); |
707 |
|
/* compute new sort interval */ |
708 |
|
sortintvl = ambclock - lastsort; |
709 |
< |
if (sortintvl > MAX_SORT_INTVL) |
709 |
> |
if (sortintvl >= MAX_SORT_INTVL/2) |
710 |
|
sortintvl = MAX_SORT_INTVL; |
711 |
|
else |
712 |
|
sortintvl <<= 1; /* wait twice as long next */ |
761 |
|
if (fseek(ambinp, lastpos, 0) < 0) |
762 |
|
goto seekerr; |
763 |
|
while (n >= AMBVALSIZ) { /* load contributed values */ |
764 |
< |
readambval(&avs, ambinp); |
764 |
> |
if (!readambval(&avs, ambinp)) { |
765 |
> |
sprintf(errmsg, |
766 |
> |
"ambient file corrupted near character %ld", |
767 |
> |
flen - n); |
768 |
> |
error(WARNING, errmsg); |
769 |
> |
break; |
770 |
> |
} |
771 |
|
avinsert(avstore(&avs)); |
772 |
|
n -= AMBVALSIZ; |
773 |
|
} |