100 |
|
register RAY *r; |
101 |
|
{ |
102 |
|
static int rdepth = 0; /* ambient recursion */ |
103 |
< |
double wsum; |
103 |
> |
double d; |
104 |
|
|
105 |
– |
rdepth++; /* increment level */ |
106 |
– |
|
105 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
106 |
|
goto dumbamb; |
107 |
|
/* check number of bounces */ |
108 |
< |
if (rdepth > ambounce) |
108 |
> |
if (rdepth >= ambounce) |
109 |
|
goto dumbamb; |
110 |
|
/* check ambient list */ |
111 |
|
if (ambincl != -1 && r->ro != NULL && |
113 |
|
goto dumbamb; |
114 |
|
|
115 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
116 |
< |
if (doambient(acol, r, NULL, NULL) == 0.0) |
116 |
> |
rdepth++; |
117 |
> |
d = doambient(acol, r, r->rweight, NULL, NULL); |
118 |
> |
rdepth--; |
119 |
> |
if (d == 0.0) |
120 |
|
goto dumbamb; |
121 |
< |
goto done; |
121 |
> |
return; |
122 |
|
} |
123 |
|
/* get ambient value */ |
124 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
125 |
< |
wsum = sumambient(acol, r, &atrunk, thescene.cuorg, thescene.cusize); |
126 |
< |
if (wsum > FTINY) |
127 |
< |
scalecolor(acol, 1.0/wsum); |
128 |
< |
else if (makeambient(acol, r) == 0.0) |
129 |
< |
goto dumbamb; |
130 |
< |
goto done; |
131 |
< |
|
125 |
> |
d = sumambient(acol, r, rdepth, |
126 |
> |
&atrunk, thescene.cuorg, thescene.cusize); |
127 |
> |
if (d > FTINY) |
128 |
> |
scalecolor(acol, 1.0/d); |
129 |
> |
else { |
130 |
> |
d = makeambient(acol, r, rdepth++); |
131 |
> |
rdepth--; |
132 |
> |
} |
133 |
> |
if (d > FTINY) |
134 |
> |
return; |
135 |
|
dumbamb: /* return global value */ |
136 |
|
copycolor(acol, ambval); |
133 |
– |
done: /* must finish here! */ |
134 |
– |
rdepth--; |
137 |
|
} |
138 |
|
|
139 |
|
|
140 |
|
double |
141 |
< |
sumambient(acol, r, at, c0, s) /* get interpolated ambient value */ |
141 |
> |
sumambient(acol, r, al, at, c0, s) /* get interpolated ambient value */ |
142 |
|
COLOR acol; |
143 |
|
register RAY *r; |
144 |
+ |
int al; |
145 |
|
AMBTREE *at; |
146 |
|
FVECT c0; |
147 |
|
double s; |
157 |
|
wsum = 0.0; |
158 |
|
for (av = at->alist; av != NULL; av = av->next) { |
159 |
|
/* |
160 |
< |
* Ray strength test. |
160 |
> |
* Ambient level test. |
161 |
|
*/ |
162 |
< |
if (av->lvl > r->rlvl || av->weight < r->rweight-FTINY) |
162 |
> |
if (av->lvl > al || av->weight < r->rweight-FTINY) |
163 |
|
continue; |
164 |
|
/* |
165 |
|
* Ambient radius test. |
186 |
|
for (j = 0; j < 3; j++) |
187 |
|
d += (r->rop[j] - av->pos[j]) * |
188 |
|
(av->dir[j] + r->ron[j]); |
189 |
< |
if (d*0.5 < -minarad*ambacc) |
189 |
> |
if (d*0.5 < -minarad*ambacc-.001) |
190 |
|
continue; |
191 |
|
/* |
192 |
|
* Jittering final test reduces image artifacts. |
200 |
|
else |
201 |
|
wt = 1.0 / wt; |
202 |
|
wsum += wt; |
203 |
< |
copycolor(ct, av->val); |
203 |
> |
extambient(ct, av, r->rop, r->ron); |
204 |
|
scalecolor(ct, wt); |
205 |
|
addcolor(acol, ct); |
206 |
|
} |
219 |
|
break; |
220 |
|
} |
221 |
|
if (j == 3) |
222 |
< |
wsum += sumambient(acol, r, at->kid+i, ck0, s); |
222 |
> |
wsum += sumambient(acol, r, al, at->kid+i, ck0, s); |
223 |
|
} |
224 |
|
return(wsum); |
225 |
|
} |
226 |
|
|
227 |
|
|
228 |
|
double |
229 |
< |
makeambient(acol, r) /* make a new ambient value */ |
229 |
> |
makeambient(acol, r, al) /* make a new ambient value */ |
230 |
|
COLOR acol; |
231 |
|
register RAY *r; |
232 |
+ |
int al; |
233 |
|
{ |
234 |
|
AMBVAL amb; |
235 |
|
FVECT gp, gd; |
236 |
< |
|
237 |
< |
amb.rad = doambient(acol, r, gp, gd); /* compute ambient */ |
236 |
> |
/* compute weight */ |
237 |
> |
amb.weight = pow(AVGREFL, (double)al); |
238 |
> |
if (r->rweight < 0.2*amb.weight) /* heuristic */ |
239 |
> |
amb.weight = r->rweight; |
240 |
> |
/* compute ambient */ |
241 |
> |
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
242 |
|
if (amb.rad == 0.0) |
243 |
|
return(0.0); |
244 |
|
/* store it */ |
245 |
|
VCOPY(amb.pos, r->rop); |
246 |
|
VCOPY(amb.dir, r->ron); |
247 |
< |
amb.lvl = r->rlvl; |
240 |
< |
amb.weight = r->rweight; |
247 |
> |
amb.lvl = al; |
248 |
|
copycolor(amb.val, acol); |
249 |
|
VCOPY(amb.gpos, gp); |
250 |
|
VCOPY(amb.gdir, gd); |
252 |
|
avinsert(&amb, &atrunk, thescene.cuorg, thescene.cusize); |
253 |
|
avsave(&amb); /* write to file */ |
254 |
|
return(amb.rad); |
255 |
+ |
} |
256 |
+ |
|
257 |
+ |
|
258 |
+ |
extambient(cr, ap, pv, nv) /* extrapolate value at pv, nv */ |
259 |
+ |
COLOR cr; |
260 |
+ |
register AMBVAL *ap; |
261 |
+ |
FVECT pv, nv; |
262 |
+ |
{ |
263 |
+ |
FVECT v1, v2; |
264 |
+ |
register int i; |
265 |
+ |
double d; |
266 |
+ |
|
267 |
+ |
d = 1.0; /* zeroeth order */ |
268 |
+ |
/* gradient due to translation */ |
269 |
+ |
for (i = 0; i < 3; i++) |
270 |
+ |
d += ap->gpos[i]*(pv[i]-ap->pos[i]); |
271 |
+ |
/* gradient due to rotation */ |
272 |
+ |
VCOPY(v1, ap->dir); |
273 |
+ |
fcross(v2, v1, nv); |
274 |
+ |
d += DOT(ap->gdir, v2); |
275 |
+ |
if (d <= 0.0) { |
276 |
+ |
setcolor(cr, 0.0, 0.0, 0.0); |
277 |
+ |
return; |
278 |
+ |
} |
279 |
+ |
copycolor(cr, ap->val); |
280 |
+ |
scalecolor(cr, d); |
281 |
|
} |
282 |
|
|
283 |
|
|