17 |
|
|
18 |
|
#include "octree.h" |
19 |
|
|
20 |
+ |
#include "otypes.h" |
21 |
+ |
|
22 |
|
#include "random.h" |
23 |
|
|
24 |
|
#define OCTSCALE 0.5 /* ceil((valid rad.)/(cube size)) */ |
25 |
|
|
24 |
– |
#define WDONE 4 /* stop if wsum/wmin is at or above */ |
25 |
– |
|
26 |
|
extern CUBE thescene; /* contains space boundaries */ |
27 |
|
|
28 |
|
extern COLOR ambval; /* global ambient component */ |
34 |
|
extern char *amblist[]; /* ambient include/exclude list */ |
35 |
|
extern int ambincl; /* include == 1, exclude == 0 */ |
36 |
|
|
37 |
< |
OBJECT ambset[128]; /* ambient include/exclude set */ |
37 |
> |
#define MAXASET 511 /* maximum number of elements in ambient set */ |
38 |
> |
OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */ |
39 |
|
|
40 |
|
double maxarad; /* maximum ambient radius */ |
41 |
|
double minarad; /* minimum ambient radius */ |
77 |
|
char *afile; |
78 |
|
{ |
79 |
|
long ftell(); |
79 |
– |
char **amblp; |
80 |
– |
OBJECT obj; |
80 |
|
AMBVAL amb; |
81 |
< |
/* set up ambient set */ |
83 |
< |
ambset[0] = 0; |
84 |
< |
for (amblp = amblist; *amblp != NULL; amblp++) { |
85 |
< |
if ((obj = modifier(*amblp)) == OVOID) { |
86 |
< |
sprintf(errmsg, "unknown %s modifier \"%s\"", |
87 |
< |
ambincl ? "include" : "exclude", *amblp); |
88 |
< |
error(WARNING, errmsg); |
89 |
< |
continue; |
90 |
< |
} |
91 |
< |
if (!inset(ambset, obj)) |
92 |
< |
insertelem(ambset, obj); |
93 |
< |
} |
81 |
> |
|
82 |
|
maxarad = thescene.cusize / 2.0; /* maximum radius */ |
83 |
|
/* minimum radius */ |
84 |
|
minarad = ambres > 0 ? thescene.cusize/ambres : 0.0; |
86 |
|
/* open ambient file */ |
87 |
|
if (afile != NULL) |
88 |
|
if ((ambfp = fopen(afile, "r+")) != NULL) { |
89 |
< |
while (fread(&amb, sizeof(AMBVAL), 1, ambfp) == 1) |
89 |
> |
while (fread((char *)&amb,sizeof(AMBVAL),1,ambfp) == 1) |
90 |
|
avinsert(&amb, &atrunk, thescene.cuorg, |
91 |
|
thescene.cusize); |
92 |
|
/* align */ |
99 |
|
} |
100 |
|
|
101 |
|
|
102 |
+ |
ambnotify(obj) /* record new modifier */ |
103 |
+ |
OBJECT obj; |
104 |
+ |
{ |
105 |
+ |
static int hitlimit = 0; |
106 |
+ |
register OBJREC *o = objptr(obj); |
107 |
+ |
register char **amblp; |
108 |
+ |
|
109 |
+ |
if (hitlimit || !ismodifier(o->otype)) |
110 |
+ |
return; |
111 |
+ |
for (amblp = amblist; *amblp != NULL; amblp++) |
112 |
+ |
if (!strcmp(o->oname, *amblp)) { |
113 |
+ |
if (ambset[0] >= MAXASET) { |
114 |
+ |
error(WARNING, "too many modifiers in ambient list"); |
115 |
+ |
hitlimit++; |
116 |
+ |
return; /* should this be fatal? */ |
117 |
+ |
} |
118 |
+ |
insertelem(ambset, obj); |
119 |
+ |
return; |
120 |
+ |
} |
121 |
+ |
} |
122 |
+ |
|
123 |
+ |
|
124 |
|
ambient(acol, r) /* compute ambient component for ray */ |
125 |
|
COLOR acol; |
126 |
|
register RAY *r; |
176 |
|
int i; |
177 |
|
register int j; |
178 |
|
register AMBVAL *av; |
179 |
< |
|
179 |
> |
/* do this node */ |
180 |
|
wsum = 0.0; |
171 |
– |
/* check children first */ |
172 |
– |
if (at->kid != NULL) { |
173 |
– |
s *= 0.5; |
174 |
– |
for (i = 0; i < 8; i++) { |
175 |
– |
for (j = 0; j < 3; j++) { |
176 |
– |
ck0[j] = c0[j]; |
177 |
– |
if (1<<j & i) |
178 |
– |
ck0[j] += s; |
179 |
– |
if (r->rop[j] < ck0[j] - OCTSCALE*s) |
180 |
– |
break; |
181 |
– |
if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s) |
182 |
– |
break; |
183 |
– |
} |
184 |
– |
if (j == 3) |
185 |
– |
wsum += sumambient(acol, r, at->kid+i, ck0, s); |
186 |
– |
} |
187 |
– |
if (wsum*ambacc >= WDONE) |
188 |
– |
return(wsum); /* close enough */ |
189 |
– |
} |
190 |
– |
/* check this node */ |
181 |
|
for (av = at->alist; av != NULL; av = av->next) { |
182 |
|
/* |
183 |
|
* Ray strength test. |
215 |
|
* Jittering final test reduces image artifacts. |
216 |
|
*/ |
217 |
|
wt = sqrt(e1) + sqrt(e2); |
218 |
< |
wt *= 0.9 + 0.2*frandom(); |
218 |
> |
wt *= .9 + .2*frandom(); |
219 |
|
if (wt > ambacc) |
220 |
|
continue; |
221 |
|
if (wt <= 1e-3) |
227 |
|
scalecolor(ct, wt); |
228 |
|
addcolor(acol, ct); |
229 |
|
} |
230 |
+ |
if (at->kid == NULL) |
231 |
+ |
return(wsum); |
232 |
+ |
/* do children */ |
233 |
+ |
s *= 0.5; |
234 |
+ |
for (i = 0; i < 8; i++) { |
235 |
+ |
for (j = 0; j < 3; j++) { |
236 |
+ |
ck0[j] = c0[j]; |
237 |
+ |
if (1<<j & i) |
238 |
+ |
ck0[j] += s; |
239 |
+ |
if (r->rop[j] < ck0[j] - OCTSCALE*s) |
240 |
+ |
break; |
241 |
+ |
if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s) |
242 |
+ |
break; |
243 |
+ |
} |
244 |
+ |
if (j == 3) |
245 |
+ |
wsum += sumambient(acol, r, at->kid+i, ck0, s); |
246 |
+ |
} |
247 |
|
return(wsum); |
248 |
|
} |
249 |
|
|
278 |
|
{ |
279 |
|
extern int ambcmp(); |
280 |
|
extern double sin(), cos(), sqrt(); |
281 |
+ |
int hlist[4]; |
282 |
|
double phi, xd, yd, zd; |
283 |
|
double b, b2; |
284 |
|
register AMBSAMP *div; |
313 |
|
fcross(ux, r->ron, uy); |
314 |
|
normalize(ux); |
315 |
|
fcross(uy, ux, r->ron); |
316 |
+ |
/* set up urand */ |
317 |
+ |
hlist[0] = r->rno; |
318 |
|
/* sample divisions */ |
319 |
|
arad = 0.0; |
320 |
|
ne = 0; |
321 |
< |
for (i = 0; i < nt; i++) |
321 |
> |
for (i = 0; i < nt; i++) { |
322 |
> |
hlist[1] = i; |
323 |
|
for (j = 0; j < np; j++) { |
324 |
|
rayorigin(&ar, r, AMBIENT, 0.5); /* pretested */ |
325 |
< |
zd = sqrt((i+frandom())/nt); |
326 |
< |
phi = 2.0*PI * (j+frandom())/np; |
325 |
> |
hlist[2] = j; |
326 |
> |
hlist[3] = 0; |
327 |
> |
zd = sqrt((i+urand(ilhash(hlist,4)))/nt); |
328 |
> |
hlist[3] = 1; |
329 |
> |
phi = 2.0*PI * (j+urand(ilhash(hlist,4)))/np; |
330 |
|
xd = cos(phi) * zd; |
331 |
|
yd = sin(phi) * zd; |
332 |
|
zd = sqrt(1.0 - zd*zd); |
333 |
|
for (k = 0; k < 3; k++) |
334 |
|
ar.rdir[k] = xd*ux[k]+yd*uy[k]+zd*r->ron[k]; |
335 |
+ |
dimlist[ndims++] = i*np + j + 38813; |
336 |
|
rayvalue(&ar); |
337 |
+ |
ndims--; |
338 |
|
if (ar.rot < FHUGE) |
339 |
|
arad += 1.0 / ar.rot; |
340 |
|
if (ns > 0) { /* save division */ |
372 |
|
} else |
373 |
|
addcolor(acol, ar.rcol); |
374 |
|
} |
375 |
+ |
} |
376 |
|
for (k = 0; k < ne; k++) { /* compute errors */ |
377 |
|
if (div[k].n > 1) |
378 |
|
div[k].k /= div[k].n; |
388 |
|
/* super-sample */ |
389 |
|
for (i = ns; i > 0; i--) { |
390 |
|
rayorigin(&ar, r, AMBIENT, 0.5); /* pretested */ |
391 |
< |
zd = sqrt((div[0].t+frandom())/nt); |
392 |
< |
phi = 2.0*PI * (div[0].p+frandom())/np; |
391 |
> |
hlist[1] = div[0].t; |
392 |
> |
hlist[2] = div[0].p; |
393 |
> |
hlist[3] = 0; |
394 |
> |
zd = sqrt((div[0].t+urand(ilhash(hlist,4)+div[0].n))/nt); |
395 |
> |
hlist[3] = 1; |
396 |
> |
phi = 2.0*PI * (div[0].p+urand(ilhash(hlist,4)+div[0].n))/np; |
397 |
|
xd = cos(phi) * zd; |
398 |
|
yd = sin(phi) * zd; |
399 |
|
zd = sqrt(1.0 - zd*zd); |
400 |
|
for (k = 0; k < 3; k++) |
401 |
|
ar.rdir[k] = xd*ux[k]+yd*uy[k]+zd*r->ron[k]; |
402 |
+ |
dimlist[ndims++] = div[0].t*np + div[0].p + 38813; |
403 |
|
rayvalue(&ar); |
404 |
+ |
ndims--; |
405 |
+ |
rayvalue(&ar); |
406 |
|
if (ar.rot < FHUGE) |
407 |
|
arad += 1.0 / ar.rot; |
408 |
|
/* recompute error */ |
415 |
|
dnew.k = b2/(dnew.n*dnew.n); |
416 |
|
/* reinsert */ |
417 |
|
for (k = 0; k < ne-1 && dnew.k < div[k+1].k; k++) |
418 |
< |
bcopy(&div[k+1], &div[k], sizeof(AMBSAMP)); |
419 |
< |
bcopy(&dnew, &div[k], sizeof(AMBSAMP)); |
418 |
> |
copystruct(&div[k], &div[k+1]); |
419 |
> |
copystruct(&div[k], &dnew); |
420 |
|
|
421 |
|
if (ne >= i) { /* extract darkest division */ |
422 |
|
ne--; |
461 |
|
#endif |
462 |
|
if (ambfp == NULL) |
463 |
|
return; |
464 |
< |
if (fwrite(av, sizeof(AMBVAL), 1, ambfp) != 1) |
464 |
> |
if (fwrite((char *)av, sizeof(AMBVAL), 1, ambfp) != 1) |
465 |
|
goto writerr; |
466 |
|
#ifdef AMBFLUSH |
467 |
|
if (++nunflshed >= AMBFLUSH) { |
490 |
|
|
491 |
|
if ((av = newambval()) == NULL) |
492 |
|
goto memerr; |
493 |
< |
bcopy(aval, av, sizeof(AMBVAL)); |
493 |
> |
copystruct(av, aval); |
494 |
|
VCOPY(ck0, c0); |
495 |
|
while (s*(OCTSCALE/2) > av->rad*ambacc) { |
496 |
|
if (at->kid == NULL) |