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"; |
21 |
|
#ifndef OCTSCALE |
22 |
|
#define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ |
23 |
|
#endif |
24 |
– |
#ifndef AMBVWT |
25 |
– |
#define AMBVWT 250 /* relative ambient value weight (# calcs) */ |
26 |
– |
#endif |
24 |
|
|
25 |
|
typedef struct ambtree { |
26 |
|
AMBVAL *alist; /* ambient value list */ |
57 |
|
#endif |
58 |
|
|
59 |
|
static COLOR avsum = BLKCOLOR; /* computed ambient value sum */ |
60 |
< |
static unsigned int nambvals = 0; /* number of computed ambient values */ |
60 |
> |
static unsigned int nambvals = 0; /* total number of indirect values */ |
61 |
> |
static unsigned int nambshare = 0; /* number of values from file */ |
62 |
|
static unsigned long ambclock = 0; /* ambient access clock */ |
63 |
|
static unsigned long lastsort = 0; /* time of last value sort */ |
64 |
|
static long sortintvl = SORT_INTVL; /* time until next sort */ |
67 |
|
/* |
68 |
|
* Track access times unless we are sharing ambient values |
69 |
|
* through memory on a multiprocessor, when we want to avoid |
70 |
< |
* claiming our own memory (copy on write). |
70 |
> |
* claiming our own memory (copy on write). Go ahead anyway |
71 |
> |
* if more than two thirds of our values are unshared. |
72 |
|
*/ |
73 |
< |
#define tracktime (shm_boundary == NULL || ambfp == NULL) |
73 |
> |
#define tracktime (shm_boundary == NULL || nambvals > 3*nambshare) |
74 |
|
|
75 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
76 |
|
|
121 |
|
setambient(afile) /* initialize calculation */ |
122 |
|
char *afile; |
123 |
|
{ |
124 |
< |
long headlen; |
124 |
> |
long pos, flen; |
125 |
|
AMBVAL amb; |
126 |
|
/* init ambient limits */ |
127 |
|
setambres(ambres); |
137 |
|
/* open ambient file */ |
138 |
|
if ((ambfp = fopen(afile, "r+")) != NULL) { |
139 |
|
initambfile(0); |
140 |
< |
headlen = ftell(ambfp); |
140 |
> |
pos = ftell(ambfp); |
141 |
|
while (readambval(&amb, ambfp)) |
142 |
|
avinsert(avstore(&amb)); |
143 |
|
/* align */ |
144 |
< |
fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); |
144 |
> |
pos += (long)nambvals*AMBVALSIZ; |
145 |
> |
flen = lseek(fileno(ambfp), 0L, 2); |
146 |
> |
if (flen != pos) { |
147 |
> |
error(WARNING, |
148 |
> |
"ignoring last %ld values in ambient file (corrupted)", |
149 |
> |
(flen - pos)/AMBVALSIZ); |
150 |
> |
fseek(ambfp, pos, 0); |
151 |
> |
ftruncate(fileno(ambfp), pos); |
152 |
> |
} |
153 |
> |
nambshare = nambvals; |
154 |
|
} else if ((ambfp = fopen(afile, "w+")) != NULL) |
155 |
|
initambfile(1); |
156 |
|
else { |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
< |
ambient(acol, r) /* compute ambient component for ray */ |
187 |
> |
ambient(acol, r, nrm) /* compute ambient component for ray */ |
188 |
|
COLOR acol; |
189 |
|
register RAY *r; |
190 |
+ |
FVECT nrm; |
191 |
|
{ |
192 |
|
static int rdepth = 0; /* ambient recursion */ |
193 |
|
double d; |
214 |
|
sortambvals(0); |
215 |
|
/* get ambient value */ |
216 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
217 |
< |
d = sumambient(acol, r, rdepth, |
217 |
> |
d = sumambient(acol, r, nrm, rdepth, |
218 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
219 |
|
if (d > FTINY) { |
220 |
|
scalecolor(acol, 1.0/d); |
221 |
|
return; |
222 |
|
} |
223 |
|
rdepth++; /* need to cache new value */ |
224 |
< |
d = makeambient(acol, r, rdepth-1); |
224 |
> |
d = makeambient(acol, r, nrm, rdepth-1); |
225 |
|
rdepth--; |
226 |
|
if (d > FTINY) |
227 |
|
return; |
228 |
|
dumbamb: /* return global value */ |
229 |
|
copycolor(acol, ambval); |
230 |
< |
#if AMBVWT |
222 |
< |
if (nambvals == 0) |
230 |
> |
if (ambvwt <= 0 | nambvals == 0) |
231 |
|
return; |
232 |
< |
scalecolor(acol, (double)AMBVWT); |
232 |
> |
scalecolor(acol, (double)ambvwt); |
233 |
|
addcolor(acol, avsum); /* average in computations */ |
234 |
< |
d = 1.0/(AMBVWT+nambvals); |
234 |
> |
d = 1.0/(ambvwt+nambvals); |
235 |
|
scalecolor(acol, d); |
228 |
– |
#endif |
236 |
|
} |
237 |
|
|
238 |
|
|
239 |
|
double |
240 |
< |
sumambient(acol, r, al, at, c0, s) /* get interpolated ambient value */ |
240 |
> |
sumambient(acol, r, rn, al, at, c0, s) /* get interpolated ambient value */ |
241 |
|
COLOR acol; |
242 |
|
register RAY *r; |
243 |
+ |
FVECT rn; |
244 |
|
int al; |
245 |
|
AMBTREE *at; |
246 |
|
FVECT c0; |
304 |
|
else |
305 |
|
wt = 1.0 / wt; |
306 |
|
wsum += wt; |
307 |
< |
extambient(ct, av, r->rop, r->ron); |
307 |
> |
extambient(ct, av, r->rop, rn); |
308 |
|
scalecolor(ct, wt); |
309 |
|
addcolor(acol, ct); |
310 |
|
} |
323 |
|
break; |
324 |
|
} |
325 |
|
if (j == 3) |
326 |
< |
wsum += sumambient(acol, r, al, at->kid+i, ck0, s); |
326 |
> |
wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s); |
327 |
|
} |
328 |
|
return(wsum); |
329 |
|
} |
330 |
|
|
331 |
|
|
332 |
|
double |
333 |
< |
makeambient(acol, r, al) /* make a new ambient value */ |
333 |
> |
makeambient(acol, r, rn, al) /* make a new ambient value */ |
334 |
|
COLOR acol; |
335 |
|
register RAY *r; |
336 |
+ |
FVECT rn; |
337 |
|
int al; |
338 |
|
{ |
339 |
|
AMBVAL amb; |
355 |
|
VCOPY(amb.gdir, gd); |
356 |
|
/* insert into tree */ |
357 |
|
avsave(&amb); /* and save to file */ |
358 |
+ |
if (rn != r->ron) |
359 |
+ |
extambient(acol, &amb, r->rop, rn); /* texture */ |
360 |
|
return(amb.rad); |
361 |
|
} |
362 |
|
|
572 |
|
alatcmp(avp1, avp2) /* compare ambient values for MRA */ |
573 |
|
AMBVAL **avp1, **avp2; |
574 |
|
{ |
575 |
< |
return((**avp2).latick - (**avp1).latick); |
575 |
> |
register long lc = (**avp2).latick - (**avp1).latick; |
576 |
> |
return(lc<0 ? -1 : lc>0 ? 1 : 0); |
577 |
|
} |
578 |
|
|
579 |
|
|
585 |
|
} |
586 |
|
|
587 |
|
|
588 |
< |
#ifdef DEBUG |
588 |
> |
#if 1 |
589 |
|
static int |
590 |
|
avlmemi(avaddr) /* find list position from address */ |
591 |
|
AMBVAL *avaddr; |
740 |
|
if (fseek(ambinp, lastpos, 0) < 0) |
741 |
|
goto seekerr; |
742 |
|
while (n >= AMBVALSIZ) { /* load contributed values */ |
743 |
< |
readambval(&avs, ambinp); |
743 |
> |
if (!readambval(&avs, ambinp)) { |
744 |
> |
sprintf(errmsg, |
745 |
> |
"ambient file corrupted near character %ld", |
746 |
> |
flen - n); |
747 |
> |
error(WARNING, errmsg); |
748 |
> |
break; |
749 |
> |
} |
750 |
|
avinsert(avstore(&avs)); |
751 |
|
n -= AMBVALSIZ; |
752 |
|
} |