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 |
+ |
if (maxarad <= FTINY) |
64 |
+ |
maxarad = .001; |
65 |
+ |
} |
66 |
+ |
|
67 |
+ |
|
68 |
|
setambient(afile) /* initialize calculation */ |
69 |
|
char *afile; |
70 |
|
{ |
71 |
|
long ftell(); |
72 |
|
AMBVAL amb; |
73 |
< |
|
74 |
< |
maxarad = thescene.cusize / 2.0; /* maximum radius */ |
75 |
< |
/* minimum radius */ |
58 |
< |
minarad = ambres > 0 ? thescene.cusize/ambres : 0.0; |
59 |
< |
|
60 |
< |
/* open ambient file */ |
73 |
> |
/* init ambient limits */ |
74 |
> |
setambres(ambres); |
75 |
> |
/* open ambient file */ |
76 |
|
if (afile != NULL) |
77 |
|
if ((ambfp = fopen(afile, "r+")) != NULL) { |
78 |
|
while (fread((char *)&amb,sizeof(AMBVAL),1,ambfp) == 1) |
115 |
|
register RAY *r; |
116 |
|
{ |
117 |
|
static int rdepth = 0; /* ambient recursion */ |
118 |
< |
double wsum; |
118 |
> |
double d; |
119 |
|
|
105 |
– |
rdepth++; /* increment level */ |
106 |
– |
|
120 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
121 |
|
goto dumbamb; |
122 |
|
/* check number of bounces */ |
123 |
< |
if (rdepth > ambounce) |
123 |
> |
if (rdepth >= ambounce) |
124 |
|
goto dumbamb; |
125 |
|
/* check ambient list */ |
126 |
|
if (ambincl != -1 && r->ro != NULL && |
128 |
|
goto dumbamb; |
129 |
|
|
130 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
131 |
< |
if (doambient(acol, r, NULL, NULL) == 0.0) |
131 |
> |
rdepth++; |
132 |
> |
d = doambient(acol, r, r->rweight, NULL, NULL); |
133 |
> |
rdepth--; |
134 |
> |
if (d == 0.0) |
135 |
|
goto dumbamb; |
136 |
< |
goto done; |
136 |
> |
return; |
137 |
|
} |
138 |
|
/* get ambient value */ |
139 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
140 |
< |
wsum = sumambient(acol, r, &atrunk, thescene.cuorg, thescene.cusize); |
141 |
< |
if (wsum > FTINY) |
142 |
< |
scalecolor(acol, 1.0/wsum); |
143 |
< |
else if (makeambient(acol, r) == 0.0) |
144 |
< |
goto dumbamb; |
145 |
< |
goto done; |
146 |
< |
|
140 |
> |
d = sumambient(acol, r, rdepth, |
141 |
> |
&atrunk, thescene.cuorg, thescene.cusize); |
142 |
> |
if (d > FTINY) |
143 |
> |
scalecolor(acol, 1.0/d); |
144 |
> |
else { |
145 |
> |
d = makeambient(acol, r, rdepth++); |
146 |
> |
rdepth--; |
147 |
> |
} |
148 |
> |
if (d > FTINY) |
149 |
> |
return; |
150 |
|
dumbamb: /* return global value */ |
151 |
|
copycolor(acol, ambval); |
133 |
– |
done: /* must finish here! */ |
134 |
– |
rdepth--; |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
|
double |
156 |
< |
sumambient(acol, r, at, c0, s) /* get interpolated ambient value */ |
156 |
> |
sumambient(acol, r, al, at, c0, s) /* get interpolated ambient value */ |
157 |
|
COLOR acol; |
158 |
|
register RAY *r; |
159 |
+ |
int al; |
160 |
|
AMBTREE *at; |
161 |
|
FVECT c0; |
162 |
|
double s; |
172 |
|
wsum = 0.0; |
173 |
|
for (av = at->alist; av != NULL; av = av->next) { |
174 |
|
/* |
175 |
< |
* Ray strength test. |
175 |
> |
* Ambient level test. |
176 |
|
*/ |
177 |
< |
if (av->lvl > r->rlvl || av->weight < r->rweight-FTINY) |
177 |
> |
if (av->lvl > al || av->weight < r->rweight-FTINY) |
178 |
|
continue; |
179 |
|
/* |
180 |
|
* Ambient radius test. |
201 |
|
for (j = 0; j < 3; j++) |
202 |
|
d += (r->rop[j] - av->pos[j]) * |
203 |
|
(av->dir[j] + r->ron[j]); |
204 |
< |
if (d*0.5 < -minarad*ambacc) |
204 |
> |
if (d*0.5 < -minarad*ambacc-.001) |
205 |
|
continue; |
206 |
|
/* |
207 |
|
* Jittering final test reduces image artifacts. |
208 |
|
*/ |
209 |
|
wt = sqrt(e1) + sqrt(e2); |
210 |
< |
wt *= .9 + .2*frandom(); |
210 |
> |
wt *= .9 + .2*urand(9015+samplendx); |
211 |
|
if (wt > ambacc) |
212 |
|
continue; |
213 |
|
if (wt <= 1e-3) |
234 |
|
break; |
235 |
|
} |
236 |
|
if (j == 3) |
237 |
< |
wsum += sumambient(acol, r, at->kid+i, ck0, s); |
237 |
> |
wsum += sumambient(acol, r, al, at->kid+i, ck0, s); |
238 |
|
} |
239 |
|
return(wsum); |
240 |
|
} |
241 |
|
|
242 |
|
|
243 |
|
double |
244 |
< |
makeambient(acol, r) /* make a new ambient value */ |
244 |
> |
makeambient(acol, r, al) /* make a new ambient value */ |
245 |
|
COLOR acol; |
246 |
|
register RAY *r; |
247 |
+ |
int al; |
248 |
|
{ |
249 |
|
AMBVAL amb; |
250 |
|
FVECT gp, gd; |
251 |
< |
|
252 |
< |
amb.rad = doambient(acol, r, gp, gd); /* compute ambient */ |
251 |
> |
/* compute weight */ |
252 |
> |
amb.weight = pow(AVGREFL, (double)al); |
253 |
> |
if (r->rweight < 0.2*amb.weight) /* heuristic */ |
254 |
> |
amb.weight = r->rweight; |
255 |
> |
/* compute ambient */ |
256 |
> |
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
257 |
|
if (amb.rad == 0.0) |
258 |
|
return(0.0); |
259 |
|
/* store it */ |
260 |
|
VCOPY(amb.pos, r->rop); |
261 |
|
VCOPY(amb.dir, r->ron); |
262 |
< |
amb.lvl = r->rlvl; |
240 |
< |
amb.weight = r->rweight; |
262 |
> |
amb.lvl = al; |
263 |
|
copycolor(amb.val, acol); |
264 |
|
VCOPY(amb.gpos, gp); |
265 |
|
VCOPY(amb.gdir, gd); |