1 |
/* Copyright (c) 1986 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* ambient.c - routines dealing with ambient (inter-reflected) component. |
9 |
* |
10 |
* The macro AMBFLUSH (if defined) is the number of ambient values |
11 |
* to wait before flushing to the ambient file. |
12 |
* |
13 |
* 5/9/86 |
14 |
*/ |
15 |
|
16 |
#include "ray.h" |
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 |
|
26 |
extern CUBE thescene; /* contains space boundaries */ |
27 |
|
28 |
extern COLOR ambval; /* global ambient component */ |
29 |
extern double ambacc; /* ambient accuracy */ |
30 |
extern int ambres; /* ambient resolution */ |
31 |
extern int ambdiv; /* number of divisions for calculation */ |
32 |
extern int ambssamp; /* number of super-samples */ |
33 |
extern int ambounce; /* number of ambient bounces */ |
34 |
extern char *amblist[]; /* ambient include/exclude list */ |
35 |
extern int ambincl; /* include == 1, exclude == 0 */ |
36 |
|
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 */ |
42 |
|
43 |
typedef struct ambval { |
44 |
FVECT pos; /* position in space */ |
45 |
FVECT dir; /* normal direction */ |
46 |
int lvl; /* recursion level of parent ray */ |
47 |
float weight; /* weight of parent ray */ |
48 |
COLOR val; /* computed ambient value */ |
49 |
float rad; /* validity radius */ |
50 |
struct ambval *next; /* next in list */ |
51 |
} AMBVAL; /* ambient value */ |
52 |
|
53 |
typedef struct ambtree { |
54 |
AMBVAL *alist; /* ambient value list */ |
55 |
struct ambtree *kid; /* 8 child nodes */ |
56 |
} AMBTREE; /* ambient octree */ |
57 |
|
58 |
typedef struct { |
59 |
float k; /* error contribution per sample */ |
60 |
COLOR v; /* ray sum */ |
61 |
int n; /* number of samples */ |
62 |
short t, p; /* theta, phi indices */ |
63 |
} AMBSAMP; /* ambient sample */ |
64 |
|
65 |
static AMBTREE atrunk; /* our ambient trunk node */ |
66 |
|
67 |
static FILE *ambfp = NULL; /* ambient file pointer */ |
68 |
|
69 |
#define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL)) |
70 |
|
71 |
#define newambtree() (AMBTREE *)calloc(8, sizeof(AMBTREE)) |
72 |
|
73 |
double sumambient(), doambient(), makeambient(); |
74 |
|
75 |
|
76 |
setambient(afile) /* initialize calculation */ |
77 |
char *afile; |
78 |
{ |
79 |
long ftell(); |
80 |
AMBVAL amb; |
81 |
|
82 |
maxarad = thescene.cusize / 2.0; /* maximum radius */ |
83 |
/* minimum radius */ |
84 |
minarad = ambres > 0 ? thescene.cusize/ambres : 0.0; |
85 |
|
86 |
/* open ambient file */ |
87 |
if (afile != NULL) |
88 |
if ((ambfp = fopen(afile, "r+")) != NULL) { |
89 |
while (fread((char *)&amb,sizeof(AMBVAL),1,ambfp) == 1) |
90 |
avinsert(&amb, &atrunk, thescene.cuorg, |
91 |
thescene.cusize); |
92 |
/* align */ |
93 |
fseek(ambfp, -(ftell(ambfp)%sizeof(AMBVAL)), 1); |
94 |
} else if ((ambfp = fopen(afile, "w")) == NULL) { |
95 |
sprintf(errmsg, "cannot open ambient file \"%s\"", |
96 |
afile); |
97 |
error(SYSTEM, errmsg); |
98 |
} |
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; |
127 |
{ |
128 |
static int rdepth = 0; /* ambient recursion */ |
129 |
double wsum; |
130 |
|
131 |
rdepth++; /* increment level */ |
132 |
|
133 |
if (ambdiv <= 0) /* no ambient calculation */ |
134 |
goto dumbamb; |
135 |
/* check number of bounces */ |
136 |
if (rdepth > ambounce) |
137 |
goto dumbamb; |
138 |
/* check ambient list */ |
139 |
if (ambincl != -1 && r->ro != NULL && |
140 |
ambincl != inset(ambset, r->ro->omod)) |
141 |
goto dumbamb; |
142 |
|
143 |
if (ambacc <= FTINY) { /* no ambient storage */ |
144 |
if (doambient(acol, r) == 0.0) |
145 |
goto dumbamb; |
146 |
goto done; |
147 |
} |
148 |
/* get ambient value */ |
149 |
setcolor(acol, 0.0, 0.0, 0.0); |
150 |
wsum = sumambient(acol, r, &atrunk, thescene.cuorg, thescene.cusize); |
151 |
if (wsum > FTINY) |
152 |
scalecolor(acol, 1.0/wsum); |
153 |
else if (makeambient(acol, r) == 0.0) |
154 |
goto dumbamb; |
155 |
goto done; |
156 |
|
157 |
dumbamb: /* return global value */ |
158 |
copycolor(acol, ambval); |
159 |
done: /* must finish here! */ |
160 |
rdepth--; |
161 |
} |
162 |
|
163 |
|
164 |
double |
165 |
sumambient(acol, r, at, c0, s) /* get interpolated ambient value */ |
166 |
COLOR acol; |
167 |
register RAY *r; |
168 |
AMBTREE *at; |
169 |
FVECT c0; |
170 |
double s; |
171 |
{ |
172 |
extern double sqrt(); |
173 |
double d, e1, e2, wt, wsum; |
174 |
COLOR ct; |
175 |
FVECT ck0; |
176 |
int i; |
177 |
register int j; |
178 |
register AMBVAL *av; |
179 |
/* do this node */ |
180 |
wsum = 0.0; |
181 |
for (av = at->alist; av != NULL; av = av->next) { |
182 |
/* |
183 |
* Ray strength test. |
184 |
*/ |
185 |
if (av->lvl > r->rlvl || av->weight < r->rweight-FTINY) |
186 |
continue; |
187 |
/* |
188 |
* Ambient radius test. |
189 |
*/ |
190 |
e1 = 0.0; |
191 |
for (j = 0; j < 3; j++) { |
192 |
d = av->pos[j] - r->rop[j]; |
193 |
e1 += d * d; |
194 |
} |
195 |
e1 /= av->rad * av->rad; |
196 |
if (e1 > ambacc*ambacc*1.21) |
197 |
continue; |
198 |
/* |
199 |
* Normal direction test. |
200 |
*/ |
201 |
e2 = (1.0 - DOT(av->dir, r->ron)) * r->rweight; |
202 |
if (e2 < 0.0) e2 = 0.0; |
203 |
if (e1 + e2 > ambacc*ambacc*1.21) |
204 |
continue; |
205 |
/* |
206 |
* Ray behind test. |
207 |
*/ |
208 |
d = 0.0; |
209 |
for (j = 0; j < 3; j++) |
210 |
d += (r->rop[j] - av->pos[j]) * |
211 |
(av->dir[j] + r->ron[j]); |
212 |
if (d < -minarad) |
213 |
continue; |
214 |
/* |
215 |
* Jittering final test reduces image artifacts. |
216 |
*/ |
217 |
wt = sqrt(e1) + sqrt(e2); |
218 |
wt *= .9 + .2*frandom(); |
219 |
if (wt > ambacc) |
220 |
continue; |
221 |
if (wt <= 1e-3) |
222 |
wt = 1e3; |
223 |
else |
224 |
wt = 1.0 / wt; |
225 |
wsum += wt; |
226 |
copycolor(ct, av->val); |
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 |
|
250 |
|
251 |
double |
252 |
makeambient(acol, r) /* make a new ambient value */ |
253 |
COLOR acol; |
254 |
register RAY *r; |
255 |
{ |
256 |
AMBVAL amb; |
257 |
|
258 |
amb.rad = doambient(acol, r); /* compute ambient */ |
259 |
if (amb.rad == 0.0) |
260 |
return(0.0); |
261 |
/* store it */ |
262 |
VCOPY(amb.pos, r->rop); |
263 |
VCOPY(amb.dir, r->ron); |
264 |
amb.lvl = r->rlvl; |
265 |
amb.weight = r->rweight; |
266 |
copycolor(amb.val, acol); |
267 |
/* insert into tree */ |
268 |
avinsert(&amb, &atrunk, thescene.cuorg, thescene.cusize); |
269 |
avsave(&amb); /* write to file */ |
270 |
return(amb.rad); |
271 |
} |
272 |
|
273 |
|
274 |
double |
275 |
doambient(acol, r) /* compute ambient component */ |
276 |
COLOR acol; |
277 |
register RAY *r; |
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; |
285 |
AMBSAMP dnew; |
286 |
RAY ar; |
287 |
FVECT ux, uy; |
288 |
double arad; |
289 |
int ndivs, nt, np, ns, ne, i, j; |
290 |
register int k; |
291 |
|
292 |
setcolor(acol, 0.0, 0.0, 0.0); |
293 |
/* set number of divisions */ |
294 |
nt = sqrt(ambdiv * r->rweight * 0.5) + 0.5; |
295 |
np = 2 * nt; |
296 |
ndivs = nt * np; |
297 |
/* check first */ |
298 |
if (ndivs == 0 || rayorigin(&ar, r, AMBIENT, 0.5) < 0) |
299 |
return(0.0); |
300 |
/* set number of super-samples */ |
301 |
ns = ambssamp * r->rweight + 0.5; |
302 |
if (ns > 0) { |
303 |
div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP)); |
304 |
if (div == NULL) |
305 |
error(SYSTEM, "out of memory in doambient"); |
306 |
} |
307 |
/* make axes */ |
308 |
uy[0] = uy[1] = uy[2] = 0.0; |
309 |
for (k = 0; k < 3; k++) |
310 |
if (r->ron[k] < 0.6 && r->ron[k] > -0.6) |
311 |
break; |
312 |
uy[k] = 1.0; |
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++) { |
322 |
hlist[1] = i; |
323 |
for (j = 0; j < np; j++) { |
324 |
rayorigin(&ar, r, AMBIENT, 0.5); /* pretested */ |
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 */ |
341 |
div[ne].k = 0.0; |
342 |
copycolor(div[ne].v, ar.rcol); |
343 |
div[ne].n = 0; |
344 |
div[ne].t = i; div[ne].p = j; |
345 |
/* sum errors */ |
346 |
b = bright(ar.rcol); |
347 |
if (i > 0) { /* from above */ |
348 |
b2 = bright(div[ne-np].v) - b; |
349 |
b2 *= b2 * 0.25; |
350 |
div[ne].k += b2; |
351 |
div[ne].n++; |
352 |
div[ne-np].k += b2; |
353 |
div[ne-np].n++; |
354 |
} |
355 |
if (j > 0) { /* from behind */ |
356 |
b2 = bright(div[ne-1].v) - b; |
357 |
b2 *= b2 * 0.25; |
358 |
div[ne].k += b2; |
359 |
div[ne].n++; |
360 |
div[ne-1].k += b2; |
361 |
div[ne-1].n++; |
362 |
} |
363 |
if (j == np-1) { /* around */ |
364 |
b2 = bright(div[ne-(np-1)].v) - b; |
365 |
b2 *= b2 * 0.25; |
366 |
div[ne].k += b2; |
367 |
div[ne].n++; |
368 |
div[ne-(np-1)].k += b2; |
369 |
div[ne-(np-1)].n++; |
370 |
} |
371 |
ne++; |
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; |
379 |
div[k].n = 1; |
380 |
} |
381 |
/* sort the divisions */ |
382 |
qsort(div, ne, sizeof(AMBSAMP), ambcmp); |
383 |
/* skim excess */ |
384 |
while (ne > ns) { |
385 |
ne--; |
386 |
addcolor(acol, div[ne].v); |
387 |
} |
388 |
/* super-sample */ |
389 |
for (i = ns; i > 0; i--) { |
390 |
rayorigin(&ar, r, AMBIENT, 0.5); /* pretested */ |
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 */ |
409 |
copycolor(dnew.v, div[0].v); |
410 |
addcolor(dnew.v, ar.rcol); |
411 |
dnew.n = div[0].n + 1; |
412 |
dnew.t = div[0].t; dnew.p = div[0].p; |
413 |
b2 = bright(dnew.v)/dnew.n - bright(ar.rcol); |
414 |
b2 = b2*b2 + div[0].k*(div[0].n*div[0].n); |
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 |
copystruct(&div[k], &div[k+1]); |
419 |
copystruct(&div[k], &dnew); |
420 |
|
421 |
if (ne >= i) { /* extract darkest division */ |
422 |
ne--; |
423 |
if (div[ne].n > 1) |
424 |
scalecolor(div[ne].v, 1.0/div[ne].n); |
425 |
addcolor(acol, div[ne].v); |
426 |
} |
427 |
} |
428 |
scalecolor(acol, 1.0/ndivs); |
429 |
if (arad <= FTINY) |
430 |
arad = FHUGE; |
431 |
else |
432 |
arad = (ndivs+ns) / arad / sqrt(r->rweight); |
433 |
if (arad > maxarad) |
434 |
arad = maxarad; |
435 |
else if (arad < minarad) |
436 |
arad = minarad; |
437 |
if (ns > 0) |
438 |
free((char *)div); |
439 |
return(arad); |
440 |
} |
441 |
|
442 |
|
443 |
static int |
444 |
ambcmp(d1, d2) /* decreasing order */ |
445 |
AMBSAMP *d1, *d2; |
446 |
{ |
447 |
if (d1->k < d2->k) |
448 |
return(1); |
449 |
if (d1->k > d2->k) |
450 |
return(-1); |
451 |
return(0); |
452 |
} |
453 |
|
454 |
|
455 |
static |
456 |
avsave(av) /* save an ambient value */ |
457 |
AMBVAL *av; |
458 |
{ |
459 |
#ifdef AMBFLUSH |
460 |
static int nunflshed = 0; |
461 |
#endif |
462 |
if (ambfp == NULL) |
463 |
return; |
464 |
if (fwrite((char *)av, sizeof(AMBVAL), 1, ambfp) != 1) |
465 |
goto writerr; |
466 |
#ifdef AMBFLUSH |
467 |
if (++nunflshed >= AMBFLUSH) { |
468 |
if (fflush(ambfp) == EOF) |
469 |
goto writerr; |
470 |
nunflshed = 0; |
471 |
} |
472 |
#endif |
473 |
return; |
474 |
writerr: |
475 |
error(SYSTEM, "error writing ambient file"); |
476 |
} |
477 |
|
478 |
|
479 |
static |
480 |
avinsert(aval, at, c0, s) /* insert ambient value in a tree */ |
481 |
AMBVAL *aval; |
482 |
register AMBTREE *at; |
483 |
FVECT c0; |
484 |
double s; |
485 |
{ |
486 |
FVECT ck0; |
487 |
int branch; |
488 |
register AMBVAL *av; |
489 |
register int i; |
490 |
|
491 |
if ((av = newambval()) == NULL) |
492 |
goto memerr; |
493 |
copystruct(av, aval); |
494 |
VCOPY(ck0, c0); |
495 |
while (s*(OCTSCALE/2) > av->rad*ambacc) { |
496 |
if (at->kid == NULL) |
497 |
if ((at->kid = newambtree()) == NULL) |
498 |
goto memerr; |
499 |
s *= 0.5; |
500 |
branch = 0; |
501 |
for (i = 0; i < 3; i++) |
502 |
if (av->pos[i] > ck0[i] + s) { |
503 |
ck0[i] += s; |
504 |
branch |= 1 << i; |
505 |
} |
506 |
at = at->kid + branch; |
507 |
} |
508 |
av->next = at->alist; |
509 |
at->alist = av; |
510 |
return; |
511 |
memerr: |
512 |
error(SYSTEM, "out of memory in avinsert"); |
513 |
} |