47 |
|
#define newambtree() (AMBTREE *)calloc(8, sizeof(AMBTREE)) |
48 |
|
|
49 |
|
|
50 |
+ |
setambres(ar) /* set ambient resolution */ |
51 |
+ |
int ar; |
52 |
+ |
{ |
53 |
+ |
/* set min & max radii */ |
54 |
+ |
if (ar <= 0) { |
55 |
+ |
minarad = 0.0; |
56 |
+ |
maxarad = thescene.cusize / 2.0; |
57 |
+ |
} else { |
58 |
+ |
minarad = thescene.cusize / ar; |
59 |
+ |
maxarad = 16.0 * minarad; /* heuristic */ |
60 |
+ |
if (maxarad > thescene.cusize / 2.0) |
61 |
+ |
maxarad = thescene.cusize / 2.0; |
62 |
+ |
} |
63 |
+ |
} |
64 |
+ |
|
65 |
+ |
|
66 |
|
setambient(afile) /* initialize calculation */ |
67 |
|
char *afile; |
68 |
|
{ |
69 |
|
long ftell(); |
70 |
|
AMBVAL amb; |
71 |
< |
|
72 |
< |
maxarad = thescene.cusize / 2.0; /* maximum radius */ |
73 |
< |
/* minimum radius */ |
58 |
< |
minarad = ambres > 0 ? thescene.cusize/ambres : 0.0; |
59 |
< |
|
60 |
< |
/* open ambient file */ |
71 |
> |
/* init ambient limits */ |
72 |
> |
setambres(ambres); |
73 |
> |
/* open ambient file */ |
74 |
|
if (afile != NULL) |
75 |
|
if ((ambfp = fopen(afile, "r+")) != NULL) { |
76 |
|
while (fread((char *)&amb,sizeof(AMBVAL),1,ambfp) == 1) |
113 |
|
register RAY *r; |
114 |
|
{ |
115 |
|
static int rdepth = 0; /* ambient recursion */ |
116 |
< |
double wsum; |
116 |
> |
double d; |
117 |
|
|
105 |
– |
rdepth++; /* increment level */ |
106 |
– |
|
118 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
119 |
|
goto dumbamb; |
120 |
|
/* check number of bounces */ |
121 |
< |
if (rdepth > ambounce) |
121 |
> |
if (rdepth >= ambounce) |
122 |
|
goto dumbamb; |
123 |
|
/* check ambient list */ |
124 |
|
if (ambincl != -1 && r->ro != NULL && |
126 |
|
goto dumbamb; |
127 |
|
|
128 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
129 |
< |
if (doambient(acol, r, NULL, NULL) == 0.0) |
129 |
> |
rdepth++; |
130 |
> |
d = doambient(acol, r, r->rweight, NULL, NULL); |
131 |
> |
rdepth--; |
132 |
> |
if (d == 0.0) |
133 |
|
goto dumbamb; |
134 |
< |
goto done; |
134 |
> |
return; |
135 |
|
} |
136 |
|
/* get ambient value */ |
137 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
138 |
< |
wsum = sumambient(acol, r, &atrunk, thescene.cuorg, thescene.cusize); |
139 |
< |
if (wsum > FTINY) |
140 |
< |
scalecolor(acol, 1.0/wsum); |
141 |
< |
else if (makeambient(acol, r) == 0.0) |
142 |
< |
goto dumbamb; |
143 |
< |
goto done; |
144 |
< |
|
138 |
> |
d = sumambient(acol, r, rdepth, |
139 |
> |
&atrunk, thescene.cuorg, thescene.cusize); |
140 |
> |
if (d > FTINY) |
141 |
> |
scalecolor(acol, 1.0/d); |
142 |
> |
else { |
143 |
> |
d = makeambient(acol, r, rdepth++); |
144 |
> |
rdepth--; |
145 |
> |
} |
146 |
> |
if (d > FTINY) |
147 |
> |
return; |
148 |
|
dumbamb: /* return global value */ |
149 |
|
copycolor(acol, ambval); |
133 |
– |
done: /* must finish here! */ |
134 |
– |
rdepth--; |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
|
double |
154 |
< |
sumambient(acol, r, at, c0, s) /* get interpolated ambient value */ |
154 |
> |
sumambient(acol, r, al, at, c0, s) /* get interpolated ambient value */ |
155 |
|
COLOR acol; |
156 |
|
register RAY *r; |
157 |
+ |
int al; |
158 |
|
AMBTREE *at; |
159 |
|
FVECT c0; |
160 |
|
double s; |
170 |
|
wsum = 0.0; |
171 |
|
for (av = at->alist; av != NULL; av = av->next) { |
172 |
|
/* |
173 |
< |
* Ray strength test. |
173 |
> |
* Ambient level test. |
174 |
|
*/ |
175 |
< |
if (av->lvl > r->rlvl || av->weight < r->rweight-FTINY) |
175 |
> |
if (av->lvl > al || av->weight < r->rweight-FTINY) |
176 |
|
continue; |
177 |
|
/* |
178 |
|
* Ambient radius test. |
199 |
|
for (j = 0; j < 3; j++) |
200 |
|
d += (r->rop[j] - av->pos[j]) * |
201 |
|
(av->dir[j] + r->ron[j]); |
202 |
< |
if (d*0.5 < -minarad*ambacc) |
202 |
> |
if (d*0.5 < -minarad*ambacc-.001) |
203 |
|
continue; |
204 |
|
/* |
205 |
|
* Jittering final test reduces image artifacts. |
206 |
|
*/ |
207 |
|
wt = sqrt(e1) + sqrt(e2); |
208 |
< |
wt *= .9 + .2*frandom(); |
208 |
> |
wt *= .9 + .2*urand(9015+samplendx); |
209 |
|
if (wt > ambacc) |
210 |
|
continue; |
211 |
|
if (wt <= 1e-3) |
213 |
|
else |
214 |
|
wt = 1.0 / wt; |
215 |
|
wsum += wt; |
216 |
< |
copycolor(ct, av->val); |
216 |
> |
extambient(ct, av, r->rop, r->ron); |
217 |
|
scalecolor(ct, wt); |
218 |
|
addcolor(acol, ct); |
219 |
|
} |
232 |
|
break; |
233 |
|
} |
234 |
|
if (j == 3) |
235 |
< |
wsum += sumambient(acol, r, at->kid+i, ck0, s); |
235 |
> |
wsum += sumambient(acol, r, al, at->kid+i, ck0, s); |
236 |
|
} |
237 |
|
return(wsum); |
238 |
|
} |
239 |
|
|
240 |
|
|
241 |
|
double |
242 |
< |
makeambient(acol, r) /* make a new ambient value */ |
242 |
> |
makeambient(acol, r, al) /* make a new ambient value */ |
243 |
|
COLOR acol; |
244 |
|
register RAY *r; |
245 |
+ |
int al; |
246 |
|
{ |
247 |
|
AMBVAL amb; |
248 |
|
FVECT gp, gd; |
249 |
< |
|
250 |
< |
amb.rad = doambient(acol, r, gp, gd); /* compute ambient */ |
249 |
> |
/* compute weight */ |
250 |
> |
amb.weight = pow(AVGREFL, (double)al); |
251 |
> |
if (r->rweight < 0.2*amb.weight) /* heuristic */ |
252 |
> |
amb.weight = r->rweight; |
253 |
> |
/* compute ambient */ |
254 |
> |
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
255 |
|
if (amb.rad == 0.0) |
256 |
|
return(0.0); |
257 |
|
/* store it */ |
258 |
|
VCOPY(amb.pos, r->rop); |
259 |
|
VCOPY(amb.dir, r->ron); |
260 |
< |
amb.lvl = r->rlvl; |
240 |
< |
amb.weight = r->rweight; |
260 |
> |
amb.lvl = al; |
261 |
|
copycolor(amb.val, acol); |
262 |
|
VCOPY(amb.gpos, gp); |
263 |
|
VCOPY(amb.gdir, gd); |
265 |
|
avinsert(&amb, &atrunk, thescene.cuorg, thescene.cusize); |
266 |
|
avsave(&amb); /* write to file */ |
267 |
|
return(amb.rad); |
268 |
+ |
} |
269 |
+ |
|
270 |
+ |
|
271 |
+ |
extambient(cr, ap, pv, nv) /* extrapolate value at pv, nv */ |
272 |
+ |
COLOR cr; |
273 |
+ |
register AMBVAL *ap; |
274 |
+ |
FVECT pv, nv; |
275 |
+ |
{ |
276 |
+ |
FVECT v1, v2; |
277 |
+ |
register int i; |
278 |
+ |
double d; |
279 |
+ |
|
280 |
+ |
d = 1.0; /* zeroeth order */ |
281 |
+ |
/* gradient due to translation */ |
282 |
+ |
for (i = 0; i < 3; i++) |
283 |
+ |
d += ap->gpos[i]*(pv[i]-ap->pos[i]); |
284 |
+ |
/* gradient due to rotation */ |
285 |
+ |
VCOPY(v1, ap->dir); |
286 |
+ |
fcross(v2, v1, nv); |
287 |
+ |
d += DOT(ap->gdir, v2); |
288 |
+ |
if (d <= 0.0) { |
289 |
+ |
setcolor(cr, 0.0, 0.0, 0.0); |
290 |
+ |
return; |
291 |
+ |
} |
292 |
+ |
copycolor(cr, ap->val); |
293 |
+ |
scalecolor(cr, d); |
294 |
|
} |
295 |
|
|
296 |
|
|