| 11 |
|
#include "face.h" |
| 12 |
|
#include "cone.h" |
| 13 |
|
#include "source.h" |
| 14 |
+ |
#include "paths.h" |
| 15 |
|
|
| 16 |
+ |
#ifndef NBSDFSAMPS |
| 17 |
+ |
#define NBSDFSAMPS 256 /* BSDF resampling count */ |
| 18 |
+ |
#endif |
| 19 |
|
|
| 20 |
|
COLORV * distarr = NULL; /* distribution array */ |
| 21 |
|
int distsiz = 0; |
| 28 |
|
{ |
| 29 |
|
if (siz <= 0) { |
| 30 |
|
if (distsiz > 0) |
| 31 |
< |
free((void *)distarr); |
| 31 |
> |
free(distarr); |
| 32 |
|
distarr = NULL; |
| 33 |
|
distsiz = 0; |
| 34 |
|
return; |
| 35 |
|
} |
| 36 |
|
if (distsiz < siz) { |
| 37 |
|
if (distsiz > 0) |
| 38 |
< |
free((void *)distarr); |
| 38 |
> |
free(distarr); |
| 39 |
|
distarr = (COLORV *)malloc(sizeof(COLOR)*siz); |
| 40 |
|
if (distarr == NULL) |
| 41 |
|
error(SYSTEM, "out of memory in newdist"); |
| 81 |
|
VCOPY(myRay.rorg, org); |
| 82 |
|
VCOPY(myRay.rdir, dir); |
| 83 |
|
myRay.rmax = .0; |
| 84 |
< |
rayorigin(&myRay, PRIMARY, NULL, NULL); |
| 84 |
> |
rayorigin(&myRay, PRIMARY|SPECULAR, NULL, NULL); |
| 85 |
|
myRay.rno = ndx; |
| 86 |
|
/* queue ray, check result */ |
| 87 |
|
process_ray(&myRay, ray_pqueue(&myRay)); |
| 92 |
|
srcsamps( /* sample sources from this surface position */ |
| 93 |
|
struct illum_args *il, |
| 94 |
|
FVECT org, |
| 95 |
< |
FVECT nrm, |
| 95 |
> |
double eps, |
| 96 |
|
MAT4 ixfm |
| 97 |
|
) |
| 98 |
|
{ |
| 99 |
< |
int nalt, nazi; |
| 99 |
> |
int nalt=1, nazi=1; |
| 100 |
|
SRCINDEX si; |
| 101 |
|
RAY sr; |
| 102 |
|
FVECT v; |
| 103 |
|
double d; |
| 104 |
|
int i, j; |
| 105 |
|
/* get sampling density */ |
| 106 |
< |
if (il->sampdens <= 0) { |
| 103 |
< |
nalt = nazi = 1; |
| 104 |
< |
} else { |
| 106 |
> |
if (il->sampdens > 0) { |
| 107 |
|
i = PI * il->sampdens; |
| 108 |
|
nalt = sqrt(i/PI) + .5; |
| 109 |
|
nazi = PI*nalt + .5; |
| 111 |
|
initsrcindex(&si); /* loop over (sub)sources */ |
| 112 |
|
for ( ; ; ) { |
| 113 |
|
VCOPY(sr.rorg, org); /* pick side to shoot from */ |
| 114 |
< |
if (il->sd != NULL) { |
| 113 |
< |
int sn = si.sn; |
| 114 |
< |
if (si.sp+1 >= si.np) ++sn; |
| 115 |
< |
if (sn >= nsources) break; |
| 116 |
< |
if (source[sn].sflags & SDISTANT) |
| 117 |
< |
d = DOT(source[sn].sloc, nrm); |
| 118 |
< |
else { |
| 119 |
< |
VSUB(v, source[sn].sloc, org); |
| 120 |
< |
d = DOT(v, nrm); |
| 121 |
< |
} |
| 122 |
< |
} else |
| 123 |
< |
d = 1.0; /* only transmission */ |
| 124 |
< |
if (d < 0.0) |
| 125 |
< |
d = -1.0001*il->thick - 5.*FTINY; |
| 126 |
< |
else |
| 127 |
< |
d = 5.*FTINY; |
| 128 |
< |
for (i = 3; i--; ) |
| 129 |
< |
sr.rorg[i] += d*nrm[i]; |
| 114 |
> |
samplendx++; /* increment sample counter */ |
| 115 |
|
if (!srcray(&sr, NULL, &si)) |
| 116 |
|
break; /* end of sources */ |
| 117 |
|
/* index direction */ |
| 119 |
|
multv3(v, sr.rdir, ixfm); |
| 120 |
|
else |
| 121 |
|
VCOPY(v, sr.rdir); |
| 122 |
< |
if (il->sd != NULL) { |
| 123 |
< |
i = getBSDF_incndx(il->sd, v); |
| 124 |
< |
if (i < 0) |
| 125 |
< |
continue; /* must not be important */ |
| 126 |
< |
sr.rno = i; |
| 142 |
< |
d = 1.0/getBSDF_incohm(il->sd, i); |
| 143 |
< |
} else { |
| 144 |
< |
if (v[2] >= -FTINY) |
| 145 |
< |
continue; /* only sample transmission */ |
| 146 |
< |
d = 1.0 - v[2]*v[2]; |
| 147 |
< |
i = d*nalt; |
| 148 |
< |
d = atan2(-v[1], -v[0])/(2.*PI); |
| 149 |
< |
if (d < 0.0) d += 1.0; |
| 150 |
< |
j = d*nazi + 0.5; |
| 151 |
< |
if (j >= nazi) j = 0; |
| 152 |
< |
sr.rno = i*nazi + j; |
| 153 |
< |
d = nalt*nazi/PI * -v[2]; |
| 154 |
< |
} |
| 122 |
> |
if (v[2] >= -FTINY) |
| 123 |
> |
continue; /* only sample transmission */ |
| 124 |
> |
v[0] = -v[0]; v[1] = -v[1]; v[2] = -v[2]; |
| 125 |
> |
sr.rno = flatindex(v, nalt, nazi); |
| 126 |
> |
d = nalt*nazi*(1./PI) * v[2]; |
| 127 |
|
d *= si.dom; /* solid angle correction */ |
| 128 |
|
scalecolor(sr.rcoef, d); |
| 129 |
+ |
VSUM(sr.rorg, sr.rorg, sr.rdir, -eps); |
| 130 |
|
process_ray(&sr, ray_pqueue(&sr)); |
| 131 |
|
} |
| 132 |
|
} |
| 149 |
|
FVECT n |
| 150 |
|
) |
| 151 |
|
{ |
| 152 |
< |
register int i; |
| 180 |
< |
|
| 181 |
< |
v[0] = v[1] = v[2] = 0.0; |
| 182 |
< |
for (i = 0; i < 3; i++) |
| 183 |
< |
if (n[i] < 0.6 && n[i] > -0.6) |
| 184 |
< |
break; |
| 185 |
< |
v[i] = 1.0; |
| 186 |
< |
fcross(u, v, n); |
| 187 |
< |
normalize(u); |
| 152 |
> |
getperpendicular(u, n, 1); |
| 153 |
|
fcross(v, n, u); |
| 154 |
|
} |
| 155 |
|
|
| 156 |
|
|
| 157 |
|
static void |
| 158 |
|
rounddir( /* compute uniform spherical direction */ |
| 159 |
< |
register FVECT dv, |
| 159 |
> |
FVECT dv, |
| 160 |
|
double alt, |
| 161 |
|
double azi |
| 162 |
|
) |
| 189 |
|
|
| 190 |
|
|
| 191 |
|
int |
| 192 |
+ |
flatindex( /* compute index for hemispherical direction */ |
| 193 |
+ |
FVECT dv, |
| 194 |
+ |
int nalt, |
| 195 |
+ |
int nazi |
| 196 |
+ |
) |
| 197 |
+ |
{ |
| 198 |
+ |
double d; |
| 199 |
+ |
int i, j; |
| 200 |
+ |
|
| 201 |
+ |
d = 1.0 - dv[2]*dv[2]; |
| 202 |
+ |
i = d*nalt; |
| 203 |
+ |
d = atan2(dv[1], dv[0]) * (0.5/PI); |
| 204 |
+ |
if (d < 0.0) d += 1.0; |
| 205 |
+ |
j = d*nazi + 0.5; |
| 206 |
+ |
if (j >= nazi) j = 0; |
| 207 |
+ |
return(i*nazi + j); |
| 208 |
+ |
} |
| 209 |
+ |
|
| 210 |
+ |
|
| 211 |
+ |
int |
| 212 |
|
my_default( /* default illum action */ |
| 213 |
|
OBJREC *ob, |
| 214 |
|
struct illum_args *il, |
| 237 |
|
FVECT dn, org, dir; |
| 238 |
|
FVECT u, v; |
| 239 |
|
double ur[2], vr[2]; |
| 240 |
+ |
double epsilon; |
| 241 |
|
MAT4 xfm; |
| 242 |
+ |
char xfrot[64]; |
| 243 |
|
int nallow; |
| 244 |
|
FACE *fa; |
| 245 |
|
int i, j; |
| 250 |
|
return(my_default(ob, il, nm)); |
| 251 |
|
} |
| 252 |
|
/* set up sampling */ |
| 253 |
< |
if (il->sd != NULL) { |
| 254 |
< |
if (!getBSDF_xfm(xfm, fa->norm, il->udir)) { |
| 268 |
< |
objerror(ob, WARNING, "illegal up direction"); |
| 269 |
< |
freeface(ob); |
| 270 |
< |
return(my_default(ob, il, nm)); |
| 271 |
< |
} |
| 272 |
< |
n = il->sd->ninc; |
| 253 |
> |
if (il->sampdens <= 0) { |
| 254 |
> |
nalt = nazi = 1; /* diffuse assumption */ |
| 255 |
|
} else { |
| 256 |
< |
if (il->sampdens <= 0) { |
| 257 |
< |
nalt = nazi = 1; /* diffuse assumption */ |
| 258 |
< |
} else { |
| 277 |
< |
n = PI * il->sampdens; |
| 278 |
< |
nalt = sqrt(n/PI) + .5; |
| 279 |
< |
nazi = PI*nalt + .5; |
| 280 |
< |
} |
| 281 |
< |
n = nazi*nalt; |
| 256 |
> |
n = PI * il->sampdens; |
| 257 |
> |
nalt = sqrt(n/PI) + .5; |
| 258 |
> |
nazi = PI*nalt + .5; |
| 259 |
|
} |
| 260 |
+ |
n = nazi*nalt; |
| 261 |
|
newdist(n); |
| 262 |
|
/* take first edge >= sqrt(area) */ |
| 263 |
|
for (j = fa->nv-1, i = 0; i < fa->nv; j = i++) { |
| 287 |
|
dim[0] = random(); |
| 288 |
|
/* sample polygon */ |
| 289 |
|
nallow = 5*n*il->nsamps; |
| 290 |
+ |
epsilon = .005*sqrt(fa->area); |
| 291 |
|
for (dim[1] = 0; dim[1] < n; dim[1]++) |
| 292 |
|
for (i = 0; i < il->nsamps; i++) { |
| 293 |
|
/* randomize direction */ |
| 294 |
|
h = ilhash(dim, 2) + i; |
| 295 |
< |
if (il->sd != NULL) { |
| 296 |
< |
r_BSDF_incvec(dir, il->sd, dim[1], urand(h), xfm); |
| 297 |
< |
} else { |
| 298 |
< |
multisamp(sp, 2, urand(h)); |
| 299 |
< |
alti = dim[1]/nazi; |
| 300 |
< |
r1 = (alti + sp[0])/nalt; |
| 322 |
< |
r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi; |
| 323 |
< |
flatdir(dn, r1, r2); |
| 324 |
< |
for (j = 0; j < 3; j++) |
| 295 |
> |
multisamp(sp, 2, urand(h)); |
| 296 |
> |
alti = dim[1]/nazi; |
| 297 |
> |
r1 = (alti + sp[0])/nalt; |
| 298 |
> |
r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi; |
| 299 |
> |
flatdir(dn, r1, r2); |
| 300 |
> |
for (j = 0; j < 3; j++) |
| 301 |
|
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - |
| 302 |
|
dn[2]*fa->norm[j]; |
| 327 |
– |
} |
| 303 |
|
/* randomize location */ |
| 304 |
|
do { |
| 305 |
|
multisamp(sp, 2, urand(h+4862+nallow)); |
| 315 |
|
freeface(ob); |
| 316 |
|
return(my_default(ob, il, nm)); |
| 317 |
|
} |
| 318 |
< |
if (il->sd != NULL && DOT(dir, fa->norm) < -FTINY) |
| 344 |
< |
r1 = -1.0001*il->thick - 5.*FTINY; |
| 345 |
< |
else |
| 346 |
< |
r1 = 5.*FTINY; |
| 347 |
< |
for (j = 0; j < 3; j++) |
| 348 |
< |
org[j] += r1*fa->norm[j]; |
| 318 |
> |
VSUM(org, org, dir, -epsilon); |
| 319 |
|
/* send sample */ |
| 320 |
|
raysamp(dim[1], org, dir); |
| 321 |
|
} |
| 322 |
|
/* add in direct component? */ |
| 323 |
< |
if (!directvis && il->flags & IL_LIGHT) { |
| 323 |
> |
if (il->flags & IL_LIGHT) { |
| 324 |
|
MAT4 ixfm; |
| 325 |
< |
if (il->sd == NULL) { |
| 326 |
< |
for (i = 3; i--; ) { |
| 327 |
< |
ixfm[i][0] = u[i]; |
| 328 |
< |
ixfm[i][1] = v[i]; |
| 329 |
< |
ixfm[i][2] = fa->norm[i]; |
| 330 |
< |
ixfm[i][3] = 0.; |
| 331 |
< |
} |
| 332 |
< |
ixfm[3][0] = ixfm[3][1] = ixfm[3][2] = 0.; |
| 363 |
< |
ixfm[3][3] = 1.; |
| 364 |
< |
} else if (!invmat4(ixfm, xfm)) |
| 365 |
< |
objerror(ob, INTERNAL, "cannot invert BSDF transform"); |
| 325 |
> |
for (i = 3; i--; ) { |
| 326 |
> |
ixfm[i][0] = u[i]; |
| 327 |
> |
ixfm[i][1] = v[i]; |
| 328 |
> |
ixfm[i][2] = fa->norm[i]; |
| 329 |
> |
ixfm[i][3] = 0.; |
| 330 |
> |
} |
| 331 |
> |
ixfm[3][0] = ixfm[3][1] = ixfm[3][2] = 0.; |
| 332 |
> |
ixfm[3][3] = 1.; |
| 333 |
|
dim[0] = random(); |
| 334 |
|
nallow = 10*il->nsamps; |
| 335 |
|
for (i = 0; i < il->nsamps; i++) { |
| 350 |
|
return(my_default(ob, il, nm)); |
| 351 |
|
} |
| 352 |
|
/* sample source rays */ |
| 353 |
< |
srcsamps(il, org, fa->norm, ixfm); |
| 353 |
> |
srcsamps(il, org, epsilon, ixfm); |
| 354 |
|
} |
| 355 |
|
} |
| 356 |
|
/* wait for all rays to finish */ |
| 357 |
|
rayclean(); |
| 391 |
– |
if (il->sd != NULL) { /* run distribution through BSDF */ |
| 392 |
– |
nalt = sqrt(il->sd->nout/PI) + .5; |
| 393 |
– |
nazi = PI*nalt + .5; |
| 394 |
– |
redistribute(il->sd, nalt, nazi, u, v, fa->norm, xfm); |
| 395 |
– |
if (!il->sampdens) |
| 396 |
– |
il->sampdens = nalt*nazi/PI + .999; |
| 397 |
– |
} |
| 358 |
|
/* write out the face and its distribution */ |
| 359 |
|
if (average(il, distarr, n)) { |
| 360 |
|
if (il->sampdens > 0) |
| 370 |
|
|
| 371 |
|
int |
| 372 |
|
my_sphere( /* make an illum sphere */ |
| 373 |
< |
register OBJREC *ob, |
| 373 |
> |
OBJREC *ob, |
| 374 |
|
struct illum_args *il, |
| 375 |
|
char *nm |
| 376 |
|
) |
| 380 |
|
double sp[4], r1, r2, r3; |
| 381 |
|
FVECT org, dir; |
| 382 |
|
FVECT u, v; |
| 383 |
< |
register int i, j; |
| 383 |
> |
int i, j; |
| 384 |
|
/* check arguments */ |
| 385 |
|
if (ob->oargs.nfargs != 4) |
| 386 |
|
objerror(ob, USER, "bad # of arguments"); |
| 392 |
|
nalt = sqrt(2./PI*n) + .5; |
| 393 |
|
nazi = PI/2.*nalt + .5; |
| 394 |
|
} |
| 435 |
– |
if (il->sd != NULL) |
| 436 |
– |
objerror(ob, WARNING, "BSDF ignored"); |
| 395 |
|
n = nalt*nazi; |
| 396 |
|
newdist(n); |
| 397 |
|
dim[0] = random(); |
| 455 |
|
/* get/check arguments */ |
| 456 |
|
co = getcone(ob, 0); |
| 457 |
|
/* set up sampling */ |
| 458 |
< |
if (il->sd != NULL) { |
| 459 |
< |
if (!getBSDF_xfm(xfm, co->ad, il->udir)) { |
| 502 |
< |
objerror(ob, WARNING, "illegal up direction"); |
| 503 |
< |
freecone(ob); |
| 504 |
< |
return(my_default(ob, il, nm)); |
| 505 |
< |
} |
| 506 |
< |
n = il->sd->ninc; |
| 458 |
> |
if (il->sampdens <= 0) { |
| 459 |
> |
nalt = nazi = 1; /* diffuse assumption */ |
| 460 |
|
} else { |
| 461 |
< |
if (il->sampdens <= 0) { |
| 462 |
< |
nalt = nazi = 1; /* diffuse assumption */ |
| 463 |
< |
} else { |
| 511 |
< |
n = PI * il->sampdens; |
| 512 |
< |
nalt = sqrt(n/PI) + .5; |
| 513 |
< |
nazi = PI*nalt + .5; |
| 514 |
< |
} |
| 515 |
< |
n = nazi*nalt; |
| 461 |
> |
n = PI * il->sampdens; |
| 462 |
> |
nalt = sqrt(n/PI) + .5; |
| 463 |
> |
nazi = PI*nalt + .5; |
| 464 |
|
} |
| 465 |
+ |
n = nazi*nalt; |
| 466 |
|
newdist(n); |
| 467 |
|
mkaxes(u, v, co->ad); |
| 468 |
|
dim[0] = random(); |
| 472 |
|
/* next sample point */ |
| 473 |
|
h = ilhash(dim,2) + i; |
| 474 |
|
/* randomize direction */ |
| 475 |
< |
if (il->sd != NULL) { |
| 476 |
< |
r_BSDF_incvec(dir, il->sd, dim[1], urand(h), xfm); |
| 477 |
< |
} else { |
| 478 |
< |
multisamp(sp, 2, urand(h)); |
| 479 |
< |
alti = dim[1]/nazi; |
| 480 |
< |
r1 = (alti + sp[0])/nalt; |
| 481 |
< |
r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi; |
| 533 |
< |
flatdir(dn, r1, r2); |
| 534 |
< |
for (j = 0; j < 3; j++) |
| 535 |
< |
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*co->ad[j]; |
| 536 |
< |
} |
| 475 |
> |
multisamp(sp, 2, urand(h)); |
| 476 |
> |
alti = dim[1]/nazi; |
| 477 |
> |
r1 = (alti + sp[0])/nalt; |
| 478 |
> |
r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi; |
| 479 |
> |
flatdir(dn, r1, r2); |
| 480 |
> |
for (j = 0; j < 3; j++) |
| 481 |
> |
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*co->ad[j]; |
| 482 |
|
/* randomize location */ |
| 483 |
|
multisamp(sp, 2, urand(h+8371)); |
| 484 |
|
r3 = sqrt(CO_R0(co)*CO_R0(co) + |
| 486 |
|
r2 = 2.*PI*sp[1]; |
| 487 |
|
r1 = r3*cos(r2); |
| 488 |
|
r2 = r3*sin(r2); |
| 489 |
< |
if (il->sd != NULL && DOT(dir, co->ad) < -FTINY) |
| 545 |
< |
r3 = -1.0001*il->thick - 5.*FTINY; |
| 546 |
< |
else |
| 547 |
< |
r3 = 5.*FTINY; |
| 489 |
> |
r3 = 5.*FTINY; |
| 490 |
|
for (j = 0; j < 3; j++) |
| 491 |
|
org[j] = CO_P0(co)[j] + r1*u[j] + r2*v[j] + |
| 492 |
|
r3*co->ad[j]; |
| 494 |
|
raysamp(dim[1], org, dir); |
| 495 |
|
} |
| 496 |
|
/* add in direct component? */ |
| 497 |
< |
if (!directvis && il->flags & IL_LIGHT) { |
| 497 |
> |
if (il->flags & IL_LIGHT) { |
| 498 |
|
MAT4 ixfm; |
| 499 |
< |
if (il->sd == NULL) { |
| 500 |
< |
for (i = 3; i--; ) { |
| 501 |
< |
ixfm[i][0] = u[i]; |
| 502 |
< |
ixfm[i][1] = v[i]; |
| 503 |
< |
ixfm[i][2] = co->ad[i]; |
| 504 |
< |
ixfm[i][3] = 0.; |
| 505 |
< |
} |
| 506 |
< |
ixfm[3][0] = ixfm[3][1] = ixfm[3][2] = 0.; |
| 565 |
< |
ixfm[3][3] = 1.; |
| 566 |
< |
} else if (!invmat4(ixfm, xfm)) |
| 567 |
< |
objerror(ob, INTERNAL, "cannot invert BSDF transform"); |
| 499 |
> |
for (i = 3; i--; ) { |
| 500 |
> |
ixfm[i][0] = u[i]; |
| 501 |
> |
ixfm[i][1] = v[i]; |
| 502 |
> |
ixfm[i][2] = co->ad[i]; |
| 503 |
> |
ixfm[i][3] = 0.; |
| 504 |
> |
} |
| 505 |
> |
ixfm[3][0] = ixfm[3][1] = ixfm[3][2] = 0.; |
| 506 |
> |
ixfm[3][3] = 1.; |
| 507 |
|
dim[0] = random(); |
| 508 |
|
for (i = 0; i < il->nsamps; i++) { |
| 509 |
|
/* randomize location */ |
| 517 |
|
for (j = 0; j < 3; j++) |
| 518 |
|
org[j] = CO_P0(co)[j] + r1*u[j] + r2*v[j]; |
| 519 |
|
/* sample source rays */ |
| 520 |
< |
srcsamps(il, org, co->ad, ixfm); |
| 520 |
> |
srcsamps(il, org, 5.*FTINY, ixfm); |
| 521 |
|
} |
| 522 |
|
} |
| 523 |
|
/* wait for all rays to finish */ |
| 524 |
|
rayclean(); |
| 586 |
– |
if (il->sd != NULL) { /* run distribution through BSDF */ |
| 587 |
– |
nalt = sqrt(il->sd->nout/PI) + .5; |
| 588 |
– |
nazi = PI*nalt + .5; |
| 589 |
– |
redistribute(il->sd, nalt, nazi, u, v, co->ad, xfm); |
| 590 |
– |
if (!il->sampdens) |
| 591 |
– |
il->sampdens = nalt*nazi/PI + .999; |
| 592 |
– |
} |
| 525 |
|
/* write out the ring and its distribution */ |
| 526 |
|
if (average(il, distarr, n)) { |
| 527 |
|
if (il->sampdens > 0) |