| 16 |
|
#include "random.h" |
| 17 |
|
|
| 18 |
|
|
| 19 |
< |
int |
| 19 |
> |
void |
| 20 |
|
inithemi( /* initialize sampling hemisphere */ |
| 21 |
|
register AMBHEMI *hp, |
| 22 |
+ |
COLOR ac, |
| 23 |
|
RAY *r, |
| 23 |
– |
COLOR ac, |
| 24 |
|
double wt |
| 25 |
|
) |
| 26 |
|
{ |
| 27 |
– |
int ns; |
| 27 |
|
double d; |
| 28 |
|
register int i; |
| 29 |
|
/* set number of divisions */ |
| 30 |
+ |
if (ambacc <= FTINY && |
| 31 |
+ |
wt > (d = 0.8*bright(ac)*r->rweight/(ambdiv*minweight))) |
| 32 |
+ |
wt = d; /* avoid ray termination */ |
| 33 |
|
hp->nt = sqrt(ambdiv * wt / PI) + 0.5; |
| 34 |
|
i = ambacc > FTINY ? 3 : 1; /* minimum number of samples */ |
| 35 |
|
if (hp->nt < i) |
| 36 |
|
hp->nt = i; |
| 37 |
|
hp->np = PI * hp->nt + 0.5; |
| 38 |
|
/* set number of super-samples */ |
| 39 |
< |
ns = ambssamp * wt + 0.5; |
| 39 |
> |
hp->ns = ambssamp * wt + 0.5; |
| 40 |
|
/* assign coefficient */ |
| 39 |
– |
d = 1.0/(hp->nt*hp->np + ns); /* XXX weight not uniform if ns > 0 */ |
| 41 |
|
copycolor(hp->acoef, ac); |
| 42 |
+ |
d = 1.0/(hp->nt*hp->np); |
| 43 |
|
scalecolor(hp->acoef, d); |
| 44 |
|
/* make axes */ |
| 45 |
|
VCOPY(hp->uz, r->ron); |
| 53 |
|
fcross(hp->ux, hp->uy, hp->uz); |
| 54 |
|
normalize(hp->ux); |
| 55 |
|
fcross(hp->uy, hp->uz, hp->ux); |
| 54 |
– |
return(ns); |
| 56 |
|
} |
| 57 |
|
|
| 58 |
|
|
| 70 |
|
double b2; |
| 71 |
|
double phi; |
| 72 |
|
register int i; |
| 73 |
< |
/* assign coefficient */ |
| 74 |
< |
if (ambacc <= FTINY) /* no storage, so report accurately */ |
| 74 |
< |
copycolor(ar.rcoef, h->acoef); |
| 75 |
< |
else /* else lie for sake of cache */ |
| 73 |
> |
/* ambient coefficient for weight */ |
| 74 |
> |
if (ambacc > FTINY) |
| 75 |
|
setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL); |
| 76 |
+ |
else |
| 77 |
+ |
copycolor(ar.rcoef, h->acoef); |
| 78 |
|
if (rayorigin(&ar, AMBIENT, r, ar.rcoef) < 0) |
| 79 |
|
return(-1); |
| 80 |
< |
copycolor(ar.rcoef, h->acoef); /* correct coefficient rtrace output */ |
| 80 |
> |
if (ambacc > FTINY) { |
| 81 |
> |
multcolor(ar.rcoef, h->acoef); |
| 82 |
> |
scalecolor(ar.rcoef, 1./AVGREFL); |
| 83 |
> |
} |
| 84 |
|
hlist[0] = r->rno; |
| 85 |
|
hlist[1] = dp->t; |
| 86 |
|
hlist[2] = dp->p; |
| 97 |
|
dimlist[ndims++] = dp->t*h->np + dp->p + 90171; |
| 98 |
|
rayvalue(&ar); |
| 99 |
|
ndims--; |
| 100 |
+ |
multcolor(ar.rcol, ar.rcoef); /* apply coefficient */ |
| 101 |
|
addcolor(dp->v, ar.rcol); |
| 102 |
|
/* use rt to improve gradient calc */ |
| 103 |
|
if (ar.rt > FTINY && ar.rt < FHUGE) |
| 150 |
|
doambient( /* compute ambient component */ |
| 151 |
|
COLOR acol, |
| 152 |
|
RAY *r, |
| 148 |
– |
COLOR ac, |
| 153 |
|
double wt, |
| 154 |
|
FVECT pg, |
| 155 |
|
FVECT dg |
| 161 |
|
AMBSAMP dnew; |
| 162 |
|
register AMBSAMP *dp; |
| 163 |
|
double arad; |
| 164 |
< |
int ndivs, ns; |
| 164 |
> |
int ndivs; |
| 165 |
|
register int i, j; |
| 162 |
– |
/* initialize color */ |
| 163 |
– |
setcolor(acol, 0.0, 0.0, 0.0); |
| 166 |
|
/* initialize hemisphere */ |
| 167 |
< |
ns = inithemi(&hemi, r, ac, wt); |
| 167 |
> |
inithemi(&hemi, acol, r, wt); |
| 168 |
|
ndivs = hemi.nt * hemi.np; |
| 169 |
+ |
/* initialize */ |
| 170 |
+ |
if (pg != NULL) |
| 171 |
+ |
pg[0] = pg[1] = pg[2] = 0.0; |
| 172 |
+ |
if (dg != NULL) |
| 173 |
+ |
dg[0] = dg[1] = dg[2] = 0.0; |
| 174 |
+ |
setcolor(acol, 0.0, 0.0, 0.0); |
| 175 |
|
if (ndivs == 0) |
| 176 |
|
return(0.0); |
| 177 |
|
/* allocate super-samples */ |
| 178 |
< |
if (ns > 0 || pg != NULL || dg != NULL) { |
| 178 |
> |
if (hemi.ns > 0 || pg != NULL || dg != NULL) { |
| 179 |
|
div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP)); |
| 180 |
|
if (div == NULL) |
| 181 |
|
error(SYSTEM, "out of memory in doambient"); |
| 191 |
|
setcolor(dp->v, 0.0, 0.0, 0.0); |
| 192 |
|
dp->r = 0.0; |
| 193 |
|
dp->n = 0; |
| 194 |
< |
if (divsample(dp, &hemi, r) < 0) |
| 195 |
< |
goto oopsy; |
| 194 |
> |
if (divsample(dp, &hemi, r) < 0) { |
| 195 |
> |
if (div == NULL) continue; |
| 196 |
> |
dp++; |
| 197 |
> |
hemi.ns = 0; /* incomplete sampling */ |
| 198 |
> |
pg = dg = NULL; |
| 199 |
> |
continue; |
| 200 |
> |
} |
| 201 |
|
arad += dp->r; |
| 202 |
|
if (div != NULL) |
| 203 |
|
dp++; |
| 204 |
|
else |
| 205 |
|
addcolor(acol, dp->v); |
| 206 |
|
} |
| 207 |
< |
if (ns > 0 && arad > FTINY && ndivs/arad < minarad) |
| 208 |
< |
ns = 0; /* close enough */ |
| 209 |
< |
else if (ns > 0) { /* else perform super-sampling */ |
| 207 |
> |
if (hemi.ns > 0 && arad > FTINY && ndivs/arad < minarad) |
| 208 |
> |
hemi.ns = 0; /* close enough */ |
| 209 |
> |
else if (hemi.ns > 0) { /* else perform super-sampling */ |
| 210 |
|
comperrs(div, &hemi); /* compute errors */ |
| 211 |
|
qsort(div, ndivs, sizeof(AMBSAMP), ambcmp); /* sort divs */ |
| 212 |
|
/* super-sample */ |
| 213 |
< |
for (i = ns; i > 0; i--) { |
| 213 |
> |
for (i = hemi.ns; i > 0; i--) { |
| 214 |
|
dnew = *div; |
| 215 |
< |
if (divsample(&dnew, &hemi, r) < 0) |
| 216 |
< |
goto oopsy; |
| 217 |
< |
/* reinsert */ |
| 218 |
< |
dp = div; |
| 215 |
> |
if (divsample(&dnew, &hemi, r) < 0) { |
| 216 |
> |
dp++; |
| 217 |
> |
continue; |
| 218 |
> |
} |
| 219 |
> |
dp = div; /* reinsert */ |
| 220 |
|
j = ndivs < i ? ndivs : i; |
| 221 |
|
while (--j > 0 && dnew.k < dp[1].k) { |
| 222 |
|
*dp = *(dp+1); |
| 242 |
|
} |
| 243 |
|
b = bright(acol); |
| 244 |
|
if (b > FTINY) { |
| 245 |
< |
b = ndivs/b; |
| 245 |
> |
b = 1.0/b; /* compute & normalize gradient(s) */ |
| 246 |
|
if (pg != NULL) { |
| 247 |
|
posgradient(pg, div, &hemi); |
| 248 |
|
for (i = 0; i < 3; i++) |
| 253 |
|
for (i = 0; i < 3; i++) |
| 254 |
|
dg[i] *= b; |
| 255 |
|
} |
| 242 |
– |
} else { |
| 243 |
– |
if (pg != NULL) |
| 244 |
– |
for (i = 0; i < 3; i++) |
| 245 |
– |
pg[i] = 0.0; |
| 246 |
– |
if (dg != NULL) |
| 247 |
– |
for (i = 0; i < 3; i++) |
| 248 |
– |
dg[i] = 0.0; |
| 256 |
|
} |
| 257 |
|
free((void *)div); |
| 258 |
|
} |
| 252 |
– |
b = 1.0/ndivs; |
| 253 |
– |
scalecolor(acol, b); |
| 259 |
|
if (arad <= FTINY) |
| 260 |
|
arad = maxarad; |
| 261 |
|
else |
| 262 |
< |
arad = (ndivs+ns)/arad; |
| 262 |
> |
arad = (ndivs+hemi.ns)/arad; |
| 263 |
|
if (pg != NULL) { /* reduce radius if gradient large */ |
| 264 |
|
d = DOT(pg,pg); |
| 265 |
|
if (d*arad*arad > 1.0) |
| 276 |
|
if ((arad /= sqrt(wt)) > maxarad) |
| 277 |
|
arad = maxarad; |
| 278 |
|
return(arad); |
| 274 |
– |
oopsy: |
| 275 |
– |
if (div != NULL) |
| 276 |
– |
free((void *)div); |
| 277 |
– |
return(0.0); |
| 279 |
|
} |
| 280 |
|
|
| 281 |
|
|
| 385 |
|
yd += mag0*sinp + mag1*cosp; |
| 386 |
|
} |
| 387 |
|
for (i = 0; i < 3; i++) |
| 388 |
< |
gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/PI; |
| 388 |
> |
gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])*(hp->nt*hp->np)/PI; |
| 389 |
|
} |
| 390 |
|
|
| 391 |
|
|
| 420 |
|
yd += mag * tsin(phi); |
| 421 |
|
} |
| 422 |
|
for (i = 0; i < 3; i++) |
| 423 |
< |
gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/(hp->nt*hp->np); |
| 423 |
> |
gv[i] = xd*hp->ux[i] + yd*hp->uy[i]; |
| 424 |
|
} |