| 54 |
|
} |
| 55 |
|
|
| 56 |
|
|
| 57 |
– |
double |
| 57 |
|
divsample(dp, h, r) /* sample a division */ |
| 58 |
|
register AMBSAMP *dp; |
| 59 |
|
AMBHEMI *h; |
| 60 |
|
RAY *r; |
| 61 |
|
{ |
| 62 |
|
RAY ar; |
| 63 |
< |
int hlist[4]; |
| 63 |
> |
int hlist[3]; |
| 64 |
> |
double spt[2]; |
| 65 |
|
double xd, yd, zd; |
| 66 |
|
double b2; |
| 67 |
|
double phi; |
| 68 |
|
register int i; |
| 69 |
|
|
| 70 |
< |
if (rayorigin(&ar, r, AMBIENT, 0.5) < 0) |
| 71 |
< |
return(0.0); |
| 70 |
> |
if (rayorigin(&ar, r, AMBIENT, AVGREFL) < 0) |
| 71 |
> |
return(-1); |
| 72 |
|
hlist[0] = r->rno; |
| 73 |
|
hlist[1] = dp->t; |
| 74 |
|
hlist[2] = dp->p; |
| 75 |
< |
hlist[3] = 0; |
| 76 |
< |
zd = sqrt((dp->t+urand(ilhash(hlist,4)+dp->n))/h->nt); |
| 77 |
< |
hlist[3] = 1; |
| 78 |
< |
phi = 2.0*PI * (dp->p+urand(ilhash(hlist,4)+dp->n))/h->np; |
| 75 |
> |
multisamp(spt, 2, urand(ilhash(hlist,3)+dp->n)); |
| 76 |
> |
zd = sqrt((dp->t + spt[0])/h->nt); |
| 77 |
> |
phi = 2.0*PI * (dp->p + spt[1])/h->np; |
| 78 |
|
xd = cos(phi) * zd; |
| 79 |
|
yd = sin(phi) * zd; |
| 80 |
|
zd = sqrt(1.0 - zd*zd); |
| 86 |
|
rayvalue(&ar); |
| 87 |
|
ndims--; |
| 88 |
|
addcolor(dp->v, ar.rcol); |
| 89 |
< |
if (ar.rot < FHUGE) |
| 90 |
< |
dp->r += 1.0/ar.rot; |
| 89 |
> |
if (ar.rt > FTINY && ar.rt < FHUGE) |
| 90 |
> |
dp->r += 1.0/ar.rt; |
| 91 |
|
/* (re)initialize error */ |
| 92 |
|
if (dp->n++) { |
| 93 |
|
b2 = bright(dp->v)/dp->n - bright(ar.rcol); |
| 95 |
|
dp->k = b2/(dp->n*dp->n); |
| 96 |
|
} else |
| 97 |
|
dp->k = 0.0; |
| 98 |
< |
return(ar.rot); |
| 98 |
> |
return(0); |
| 99 |
|
} |
| 100 |
|
|
| 101 |
|
|
| 102 |
|
double |
| 103 |
< |
doambient(acol, r, pg, dg) /* compute ambient component */ |
| 103 |
> |
doambient(acol, r, wt, pg, dg) /* compute ambient component */ |
| 104 |
|
COLOR acol; |
| 105 |
|
RAY *r; |
| 106 |
+ |
double wt; |
| 107 |
|
FVECT pg, dg; |
| 108 |
|
{ |
| 109 |
|
double b, d; |
| 117 |
|
/* initialize color */ |
| 118 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
| 119 |
|
/* initialize hemisphere */ |
| 120 |
< |
inithemi(&hemi, r); |
| 120 |
> |
inithemi(&hemi, r, wt); |
| 121 |
|
ndivs = hemi.nt * hemi.np; |
| 122 |
|
if (ndivs == 0) |
| 123 |
|
return(0.0); |
| 124 |
|
/* set number of super-samples */ |
| 125 |
< |
ns = ambssamp * r->rweight + 0.5; |
| 125 |
> |
ns = ambssamp * wt + 0.5; |
| 126 |
|
if (ns > 0 || pg != NULL || dg != NULL) { |
| 127 |
|
div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP)); |
| 128 |
|
if (div == NULL) |
| 139 |
|
setcolor(dp->v, 0.0, 0.0, 0.0); |
| 140 |
|
dp->r = 0.0; |
| 141 |
|
dp->n = 0; |
| 142 |
< |
if ((d = divsample(dp, &hemi, r)) == 0.0) |
| 142 |
> |
if (divsample(dp, &hemi, r) < 0) |
| 143 |
|
goto oopsy; |
| 144 |
|
if (div != NULL) |
| 145 |
|
dp++; |
| 149 |
|
} |
| 150 |
|
} |
| 151 |
|
if (ns > 0) { /* perform super-sampling */ |
| 152 |
< |
comperrs(div, hemi); /* compute errors */ |
| 152 |
> |
comperrs(div, &hemi); /* compute errors */ |
| 153 |
|
qsort(div, ndivs, sizeof(AMBSAMP), ambcmp); /* sort divs */ |
| 154 |
– |
dp = div + ndivs; /* skim excess */ |
| 155 |
– |
for (i = ndivs; i > ns; i--) { |
| 156 |
– |
dp--; |
| 157 |
– |
addcolor(acol, dp->v); |
| 158 |
– |
arad += dp->r; |
| 159 |
– |
} |
| 154 |
|
/* super-sample */ |
| 155 |
|
for (i = ns; i > 0; i--) { |
| 156 |
|
copystruct(&dnew, div); |
| 157 |
< |
if ((d = divsample(&dnew, &hemi)) == 0.0) |
| 157 |
> |
if (divsample(&dnew, &hemi, r) < 0) |
| 158 |
|
goto oopsy; |
| 165 |
– |
if (d < FHUGE) |
| 166 |
– |
arad += 1.0 / d; |
| 159 |
|
/* reinsert */ |
| 160 |
|
dp = div; |
| 161 |
|
j = ndivs < i ? ndivs : i; |
| 164 |
|
dp++; |
| 165 |
|
} |
| 166 |
|
copystruct(dp, &dnew); |
| 175 |
– |
/* extract darkest */ |
| 176 |
– |
if (i <= ndivs) { |
| 177 |
– |
dp = div + i-1; |
| 178 |
– |
arad += dp->r; |
| 179 |
– |
if (dp->n > 1) { |
| 180 |
– |
b = 1.0/dp->n; |
| 181 |
– |
scalecolor(dp->v, b); |
| 182 |
– |
dp->r *= b; |
| 183 |
– |
dp->n = 1; |
| 184 |
– |
} |
| 185 |
– |
addcolor(acol, dp->v); |
| 186 |
– |
} |
| 167 |
|
} |
| 168 |
|
if (pg != NULL || dg != NULL) /* restore order */ |
| 169 |
|
qsort(div, ndivs, sizeof(AMBSAMP), ambnorm); |
| 170 |
|
} |
| 171 |
|
/* compute returned values */ |
| 172 |
< |
if (pg != NULL) |
| 173 |
< |
posgradient(pg, div, &hemi); |
| 174 |
< |
if (dg != NULL) |
| 175 |
< |
dirgradient(dg, div, &hemi); |
| 176 |
< |
if (div != NULL) |
| 172 |
> |
if (div != NULL) { |
| 173 |
> |
for (i = ndivs, dp = div; i-- > 0; dp++) { |
| 174 |
> |
arad += dp->r; |
| 175 |
> |
if (dp->n > 1) { |
| 176 |
> |
b = 1.0/dp->n; |
| 177 |
> |
scalecolor(dp->v, b); |
| 178 |
> |
dp->r *= b; |
| 179 |
> |
dp->n = 1; |
| 180 |
> |
} |
| 181 |
> |
addcolor(acol, dp->v); |
| 182 |
> |
} |
| 183 |
> |
b = bright(acol); |
| 184 |
> |
if (b > FTINY) { |
| 185 |
> |
b = ndivs/b; |
| 186 |
> |
if (pg != NULL) { |
| 187 |
> |
posgradient(pg, div, &hemi); |
| 188 |
> |
for (i = 0; i < 3; i++) |
| 189 |
> |
pg[i] *= b; |
| 190 |
> |
} |
| 191 |
> |
if (dg != NULL) { |
| 192 |
> |
dirgradient(dg, div, &hemi); |
| 193 |
> |
for (i = 0; i < 3; i++) |
| 194 |
> |
dg[i] *= b; |
| 195 |
> |
} |
| 196 |
> |
} else { |
| 197 |
> |
if (pg != NULL) |
| 198 |
> |
for (i = 0; i < 3; i++) |
| 199 |
> |
pg[i] = 0.0; |
| 200 |
> |
if (dg != NULL) |
| 201 |
> |
for (i = 0; i < 3; i++) |
| 202 |
> |
dg[i] = 0.0; |
| 203 |
> |
} |
| 204 |
|
free((char *)div); |
| 205 |
+ |
} |
| 206 |
|
b = 1.0/ndivs; |
| 207 |
|
scalecolor(acol, b); |
| 208 |
|
if (arad <= FTINY) |
| 213 |
|
arad = maxarad; |
| 214 |
|
else if (arad < minarad) |
| 215 |
|
arad = minarad; |
| 216 |
< |
arad /= sqrt(r->rweight); |
| 216 |
> |
arad /= sqrt(wt); |
| 217 |
> |
if (pg != NULL) { /* clip pos. gradient if too large */ |
| 218 |
> |
d = 4.0*DOT(pg,pg)*arad*arad; |
| 219 |
> |
if (d > 1.0) { |
| 220 |
> |
d = 1.0/sqrt(d); |
| 221 |
> |
for (i = 0; i < 3; i++) |
| 222 |
> |
pg[i] *= d; |
| 223 |
> |
} |
| 224 |
> |
} |
| 225 |
|
return(arad); |
| 226 |
|
oopsy: |
| 227 |
|
if (div != NULL) |
| 230 |
|
} |
| 231 |
|
|
| 232 |
|
|
| 233 |
< |
inithemi(hp, r) /* initialize sampling hemisphere */ |
| 233 |
> |
inithemi(hp, r, wt) /* initialize sampling hemisphere */ |
| 234 |
|
register AMBHEMI *hp; |
| 235 |
|
RAY *r; |
| 236 |
+ |
double wt; |
| 237 |
|
{ |
| 238 |
|
register int i; |
| 239 |
|
/* set number of divisions */ |
| 240 |
< |
hp->nt = sqrt(ambdiv * r->rweight * 0.5) + 0.5; |
| 241 |
< |
hp->np = 2 * hp->nt; |
| 240 |
> |
if (wt < (.25*PI)/ambdiv+FTINY) { |
| 241 |
> |
hp->nt = hp->np = 0; |
| 242 |
> |
return; /* zero samples */ |
| 243 |
> |
} |
| 244 |
> |
hp->nt = sqrt(ambdiv * wt / PI) + 0.5; |
| 245 |
> |
hp->np = PI * hp->nt + 0.5; |
| 246 |
|
/* make axes */ |
| 247 |
|
VCOPY(hp->uz, r->ron); |
| 248 |
|
hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0; |
| 252 |
|
if (i >= 3) |
| 253 |
|
error(CONSISTENCY, "bad ray direction in inithemi"); |
| 254 |
|
hp->uy[i] = 1.0; |
| 255 |
< |
fcross(hp->ux, hp->uz, hp->uy); |
| 255 |
> |
fcross(hp->ux, hp->uy, hp->uz); |
| 256 |
|
normalize(hp->ux); |
| 257 |
< |
fcross(hp->uy, hp->ux, hp->uz); |
| 257 |
> |
fcross(hp->uy, hp->uz, hp->ux); |
| 258 |
|
} |
| 259 |
|
|
| 260 |
|
|
| 269 |
|
dp = da; |
| 270 |
|
for (i = 0; i < hp->nt; i++) |
| 271 |
|
for (j = 0; j < hp->np; j++) { |
| 272 |
+ |
#ifdef DEBUG |
| 273 |
+ |
if (dp->t != i || dp->p != j) |
| 274 |
+ |
error(CONSISTENCY, |
| 275 |
+ |
"division order in comperrs"); |
| 276 |
+ |
#endif |
| 277 |
|
b = bright(dp[0].v); |
| 278 |
|
if (i > 0) { /* from above */ |
| 279 |
|
b2 = bright(dp[-hp->np].v) - b; |
| 286 |
|
b2 *= b2 * 0.25; |
| 287 |
|
dp[0].k += b2; |
| 288 |
|
dp[-1].k += b2; |
| 289 |
< |
} |
| 290 |
< |
if (j == hp->np-1) { /* around */ |
| 265 |
< |
b2 = bright(dp[-(hp->np-1)].v) - b; |
| 289 |
> |
} else { /* around */ |
| 290 |
> |
b2 = bright(dp[hp->np-1].v) - b; |
| 291 |
|
b2 *= b2 * 0.25; |
| 292 |
|
dp[0].k += b2; |
| 293 |
< |
dp[-(hp->np-1)].k += b2; |
| 293 |
> |
dp[hp->np-1].k += b2; |
| 294 |
|
} |
| 295 |
|
dp++; |
| 296 |
|
} |
| 348 |
|
dp += hp->np; |
| 349 |
|
} |
| 350 |
|
if (hp->nt > 1) { |
| 351 |
< |
mag0 /= (double)(hp->nt-1); |
| 351 |
> |
mag0 /= (double)hp->np; |
| 352 |
|
mag1 /= (double)hp->nt; |
| 353 |
|
} |
| 354 |
|
phi = 2.0*PI * (double)j/hp->np; |
| 357 |
|
yd += mag0*sinp + mag1*cosp; |
| 358 |
|
} |
| 359 |
|
for (i = 0; i < 3; i++) |
| 360 |
< |
gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/hp->np; |
| 360 |
> |
gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/PI; |
| 361 |
|
} |
| 362 |
|
|
| 363 |
|
|
| 389 |
|
yd += mag * sin(phi); |
| 390 |
|
} |
| 391 |
|
for (i = 0; i < 3; i++) |
| 392 |
< |
gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/(hp->nt*hp->np); |
| 392 |
> |
gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])*PI/(hp->nt*hp->np); |
| 393 |
|
} |