| 19 |
|
#include "otypes.h" |
| 20 |
|
#include "rtotypes.h" |
| 21 |
|
#include "random.h" |
| 22 |
+ |
#include "pmapmat.h" |
| 23 |
|
|
| 24 |
|
#ifndef MAXITER |
| 25 |
|
#define MAXITER 10 /* maximum # specular ray attempts */ |
| 66 |
|
double pdot; /* perturbed dot product */ |
| 67 |
|
} NORMDAT; /* normal material data */ |
| 68 |
|
|
| 69 |
< |
static srcdirf_t dirnorm; |
| 69 |
< |
static void gaussamp(RAY *r, NORMDAT *np); |
| 69 |
> |
static void gaussamp(NORMDAT *np); |
| 70 |
|
|
| 71 |
|
|
| 72 |
|
static void |
| 73 |
|
dirnorm( /* compute source contribution */ |
| 74 |
|
COLOR cval, /* returned coefficient */ |
| 75 |
< |
void *nnp, /* material data */ |
| 75 |
> |
void *nnp, /* material data */ |
| 76 |
|
FVECT ldir, /* light source direction */ |
| 77 |
|
double omega /* light source size */ |
| 78 |
|
) |
| 79 |
|
{ |
| 80 |
< |
register NORMDAT *np = nnp; |
| 80 |
> |
NORMDAT *np = nnp; |
| 81 |
|
double ldot; |
| 82 |
|
double lrdiff, ltdiff; |
| 83 |
|
double dtmp, d2, d3, d4; |
| 112 |
|
scalecolor(ctmp, dtmp); |
| 113 |
|
addcolor(cval, ctmp); |
| 114 |
|
} |
| 115 |
+ |
|
| 116 |
+ |
if (ldot < -FTINY && ltdiff > FTINY) { |
| 117 |
+ |
/* |
| 118 |
+ |
* Compute diffuse transmission. |
| 119 |
+ |
*/ |
| 120 |
+ |
copycolor(ctmp, np->mcolor); |
| 121 |
+ |
dtmp = -ldot * omega * ltdiff * (1.0/PI); |
| 122 |
+ |
scalecolor(ctmp, dtmp); |
| 123 |
+ |
addcolor(cval, ctmp); |
| 124 |
+ |
} |
| 125 |
+ |
|
| 126 |
+ |
if (ambRayInPmap(np->rp)) |
| 127 |
+ |
return; /* specular already in photon map */ |
| 128 |
+ |
|
| 129 |
|
if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) { |
| 130 |
|
/* |
| 131 |
|
* Compute specular reflection coefficient using |
| 137 |
|
if (np->specfl & SP_FLAT) |
| 138 |
|
dtmp += omega * (0.25/PI); |
| 139 |
|
/* half vector */ |
| 140 |
< |
vtmp[0] = ldir[0] - np->rp->rdir[0]; |
| 127 |
< |
vtmp[1] = ldir[1] - np->rp->rdir[1]; |
| 128 |
< |
vtmp[2] = ldir[2] - np->rp->rdir[2]; |
| 140 |
> |
VSUB(vtmp, ldir, np->rp->rdir); |
| 141 |
|
d2 = DOT(vtmp, np->pnorm); |
| 142 |
|
d2 *= d2; |
| 143 |
|
d3 = DOT(vtmp,vtmp); |
| 152 |
|
addcolor(cval, ctmp); |
| 153 |
|
} |
| 154 |
|
} |
| 155 |
< |
if (ldot < -FTINY && ltdiff > FTINY) { |
| 156 |
< |
/* |
| 145 |
< |
* Compute diffuse transmission. |
| 146 |
< |
*/ |
| 147 |
< |
copycolor(ctmp, np->mcolor); |
| 148 |
< |
dtmp = -ldot * omega * ltdiff * (1.0/PI); |
| 149 |
< |
scalecolor(ctmp, dtmp); |
| 150 |
< |
addcolor(cval, ctmp); |
| 151 |
< |
} |
| 155 |
> |
|
| 156 |
> |
|
| 157 |
|
if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) { |
| 158 |
|
/* |
| 159 |
|
* Compute specular transmission. Specular transmission |
| 174 |
|
} |
| 175 |
|
|
| 176 |
|
|
| 177 |
< |
extern int |
| 177 |
> |
int |
| 178 |
|
m_normal( /* color a ray that hit something normal */ |
| 179 |
< |
register OBJREC *m, |
| 180 |
< |
register RAY *r |
| 179 |
> |
OBJREC *m, |
| 180 |
> |
RAY *r |
| 181 |
|
) |
| 182 |
|
{ |
| 183 |
|
NORMDAT nd; |
| 187 |
|
int hastexture; |
| 188 |
|
double d; |
| 189 |
|
COLOR ctmp; |
| 190 |
< |
register int i; |
| 190 |
> |
int i; |
| 191 |
> |
|
| 192 |
> |
/* PMAP: skip transmitted shadow ray if accounted for in photon map */ |
| 193 |
> |
/* No longer needed? |
| 194 |
> |
if (shadowRayInPmap(r) || ambRayInPmap(r)) |
| 195 |
> |
return(1); */ |
| 196 |
> |
|
| 197 |
|
/* easy shadow test */ |
| 198 |
|
if (r->crtype & SHADOW && m->otype != MAT_TRANS) |
| 199 |
|
return(1); |
| 202 |
|
objerror(m, USER, "bad number of arguments"); |
| 203 |
|
/* check for back side */ |
| 204 |
|
if (r->rod < 0.0) { |
| 205 |
< |
if (!backvis && m->otype != MAT_TRANS) { |
| 205 |
> |
if (!backvis) { |
| 206 |
|
raytrans(r); |
| 207 |
|
return(1); |
| 208 |
|
} |
| 238 |
|
nd.rspec = m->oargs.farg[3]; |
| 239 |
|
/* compute Fresnel approx. */ |
| 240 |
|
if (nd.specfl & SP_PURE && nd.rspec >= FRESTHRESH) { |
| 241 |
< |
fest = FRESNE(r->rod); |
| 241 |
> |
fest = FRESNE(nd.pdot); |
| 242 |
|
nd.rspec += fest*(1. - nd.rspec); |
| 243 |
|
} else |
| 244 |
|
fest = 0.; |
| 253 |
|
if (!(nd.specfl & SP_PURE) && |
| 254 |
|
specthresh >= nd.tspec-FTINY) |
| 255 |
|
nd.specfl |= SP_TBLT; |
| 256 |
< |
if (!hastexture || r->crtype & SHADOW) { |
| 256 |
> |
if (!hastexture || r->crtype & (SHADOW|AMBIENT)) { |
| 257 |
|
VCOPY(nd.prdir, r->rdir); |
| 258 |
|
transtest = 2; |
| 259 |
|
} else { |
| 268 |
|
} else |
| 269 |
|
nd.tdiff = nd.tspec = nd.trans = 0.0; |
| 270 |
|
/* transmitted ray */ |
| 271 |
+ |
|
| 272 |
|
if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) { |
| 273 |
|
RAY lr; |
| 274 |
|
copycolor(lr.rcoef, nd.mcolor); /* modified by color */ |
| 295 |
|
if (m->otype != MAT_METAL) { |
| 296 |
|
setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec); |
| 297 |
|
} else if (fest > FTINY) { |
| 298 |
< |
d = nd.rspec*(1. - fest); |
| 298 |
> |
d = m->oargs.farg[3]*(1. - fest); |
| 299 |
|
for (i = 0; i < 3; i++) |
| 300 |
< |
nd.scolor[i] = fest + nd.mcolor[i]*d; |
| 300 |
> |
colval(nd.scolor,i) = fest + |
| 301 |
> |
colval(nd.mcolor,i)*d; |
| 302 |
|
} else { |
| 303 |
|
copycolor(nd.scolor, nd.mcolor); |
| 304 |
|
scalecolor(nd.scolor, nd.rspec); |
| 321 |
|
rayvalue(&lr); |
| 322 |
|
multcolor(lr.rcol, lr.rcoef); |
| 323 |
|
addcolor(r->rcol, lr.rcol); |
| 324 |
< |
if (!hastexture && nd.specfl & SP_FLAT) { |
| 324 |
> |
if (nd.specfl & SP_FLAT && |
| 325 |
> |
!hastexture | (r->crtype & AMBIENT)) { |
| 326 |
|
mirtest = 2.*bright(lr.rcol); |
| 327 |
|
mirdist = r->rot + lr.rt; |
| 328 |
|
} |
| 335 |
|
return(1); /* 100% pure specular */ |
| 336 |
|
|
| 337 |
|
if (!(nd.specfl & SP_PURE)) |
| 338 |
< |
gaussamp(r, &nd); /* checks *BLT flags */ |
| 338 |
> |
gaussamp(&nd); /* checks *BLT flags */ |
| 339 |
|
|
| 340 |
|
if (nd.rdiff > FTINY) { /* ambient from this side */ |
| 341 |
|
copycolor(ctmp, nd.mcolor); /* modified by material color */ |
| 342 |
< |
if (nd.specfl & SP_RBLT) |
| 343 |
< |
scalecolor(ctmp, 1.0-nd.trans); |
| 344 |
< |
else |
| 331 |
< |
scalecolor(ctmp, nd.rdiff); |
| 342 |
> |
scalecolor(ctmp, nd.rdiff); |
| 343 |
> |
if (nd.specfl & SP_RBLT) /* add in specular as well? */ |
| 344 |
> |
addcolor(ctmp, nd.scolor); |
| 345 |
|
multambient(ctmp, r, hastexture ? nd.pnorm : r->ron); |
| 346 |
|
addcolor(r->rcol, ctmp); /* add to returned color */ |
| 347 |
|
} |
| 378 |
|
|
| 379 |
|
static void |
| 380 |
|
gaussamp( /* sample Gaussian specular */ |
| 381 |
< |
RAY *r, |
| 369 |
< |
register NORMDAT *np |
| 381 |
> |
NORMDAT *np |
| 382 |
|
) |
| 383 |
|
{ |
| 384 |
|
RAY sr; |
| 385 |
|
FVECT u, v, h; |
| 386 |
|
double rv[2]; |
| 387 |
|
double d, sinp, cosp; |
| 388 |
< |
COLOR scol; |
| 389 |
< |
int niter, ns2go; |
| 390 |
< |
register int i; |
| 388 |
> |
COLOR scol; |
| 389 |
> |
int maxiter, ntrials, nstarget, nstaken; |
| 390 |
> |
int i; |
| 391 |
|
/* quick test */ |
| 392 |
|
if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL && |
| 393 |
|
(np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN) |
| 394 |
|
return; |
| 395 |
|
/* set up sample coordinates */ |
| 396 |
< |
v[0] = v[1] = v[2] = 0.0; |
| 385 |
< |
for (i = 0; i < 3; i++) |
| 386 |
< |
if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6) |
| 387 |
< |
break; |
| 388 |
< |
v[i] = 1.0; |
| 389 |
< |
fcross(u, v, np->pnorm); |
| 390 |
< |
normalize(u); |
| 396 |
> |
getperpendicular(u, np->pnorm, rand_samp); |
| 397 |
|
fcross(v, np->pnorm, u); |
| 398 |
|
/* compute reflection */ |
| 399 |
|
if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && |
| 400 |
< |
rayorigin(&sr, SPECULAR, r, np->scolor) == 0) { |
| 401 |
< |
copycolor(scol, np->scolor); |
| 396 |
< |
ns2go = 1; |
| 400 |
> |
rayorigin(&sr, SPECULAR, np->rp, np->scolor) == 0) { |
| 401 |
> |
nstarget = 1; |
| 402 |
|
if (specjitter > 1.5) { /* multiple samples? */ |
| 403 |
< |
ns2go = specjitter*r->rweight + .5; |
| 404 |
< |
if ((d = bright(scol)) <= minweight*ns2go) |
| 405 |
< |
ns2go = d/minweight; |
| 406 |
< |
if (ns2go > 1) { |
| 407 |
< |
d = 1./ns2go; |
| 408 |
< |
scalecolor(scol, d); |
| 403 |
> |
nstarget = specjitter*np->rp->rweight + .5; |
| 404 |
> |
if (sr.rweight <= minweight*nstarget) |
| 405 |
> |
nstarget = sr.rweight/minweight; |
| 406 |
> |
if (nstarget > 1) { |
| 407 |
> |
d = 1./nstarget; |
| 408 |
> |
scalecolor(sr.rcoef, d); |
| 409 |
> |
sr.rweight *= d; |
| 410 |
|
} else |
| 411 |
< |
ns2go = 1; |
| 411 |
> |
nstarget = 1; |
| 412 |
|
} |
| 413 |
< |
dimlist[ndims++] = (int)np->mp; |
| 414 |
< |
for (niter = ns2go*MAXITER; (ns2go > 0) & (niter > 0); niter--) { |
| 415 |
< |
if (specjitter > 1.5) |
| 413 |
> |
setcolor(scol, 0., 0., 0.); |
| 414 |
> |
dimlist[ndims++] = (int)(size_t)np->mp; |
| 415 |
> |
maxiter = MAXITER*nstarget; |
| 416 |
> |
for (nstaken = ntrials = 0; nstaken < nstarget && |
| 417 |
> |
ntrials < maxiter; ntrials++) { |
| 418 |
> |
if (ntrials) |
| 419 |
|
d = frandom(); |
| 420 |
|
else |
| 421 |
|
d = urand(ilhash(dimlist,ndims)+samplendx); |
| 431 |
|
d = sqrt( np->alpha2 * -log(rv[1]) ); |
| 432 |
|
for (i = 0; i < 3; i++) |
| 433 |
|
h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]); |
| 434 |
< |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
| 435 |
< |
if (d <= np->pdot + FTINY) |
| 434 |
> |
d = -2.0 * DOT(h, np->rp->rdir) / (1.0 + d*d); |
| 435 |
> |
VSUM(sr.rdir, np->rp->rdir, h, d); |
| 436 |
> |
/* sample rejection test */ |
| 437 |
> |
if ((d = DOT(sr.rdir, np->rp->ron)) <= FTINY) |
| 438 |
|
continue; |
| 428 |
– |
VSUM(sr.rdir, r->rdir, h, d); |
| 429 |
– |
if (DOT(sr.rdir, r->ron) <= FTINY) |
| 430 |
– |
continue; |
| 439 |
|
checknorm(sr.rdir); |
| 440 |
< |
if (specjitter > 1.5) { /* adjusted W-G-M-D weight */ |
| 441 |
< |
copycolor(sr.rcoef, scol); |
| 442 |
< |
d = 2.*(1. - np->pdot/d); |
| 443 |
< |
scalecolor(sr.rcoef, d); |
| 444 |
< |
rayclear(&sr); |
| 440 |
> |
if (nstarget > 1) { /* W-G-M-D adjustment */ |
| 441 |
> |
if (nstaken) rayclear(&sr); |
| 442 |
> |
rayvalue(&sr); |
| 443 |
> |
d = 2./(1. + np->rp->rod/d); |
| 444 |
> |
scalecolor(sr.rcol, d); |
| 445 |
> |
addcolor(scol, sr.rcol); |
| 446 |
> |
} else { |
| 447 |
> |
rayvalue(&sr); |
| 448 |
> |
multcolor(sr.rcol, sr.rcoef); |
| 449 |
> |
addcolor(np->rp->rcol, sr.rcol); |
| 450 |
|
} |
| 451 |
< |
rayvalue(&sr); |
| 439 |
< |
multcolor(sr.rcol, sr.rcoef); |
| 440 |
< |
addcolor(r->rcol, sr.rcol); |
| 441 |
< |
--ns2go; |
| 451 |
> |
++nstaken; |
| 452 |
|
} |
| 453 |
+ |
if (nstarget > 1) { /* final W-G-M-D weighting */ |
| 454 |
+ |
multcolor(scol, sr.rcoef); |
| 455 |
+ |
d = (double)nstarget/ntrials; |
| 456 |
+ |
scalecolor(scol, d); |
| 457 |
+ |
addcolor(np->rp->rcol, scol); |
| 458 |
+ |
} |
| 459 |
|
ndims--; |
| 460 |
|
} |
| 461 |
|
/* compute transmission */ |
| 462 |
|
copycolor(sr.rcoef, np->mcolor); /* modified by color */ |
| 463 |
|
scalecolor(sr.rcoef, np->tspec); |
| 464 |
|
if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && |
| 465 |
< |
rayorigin(&sr, SPECULAR, r, sr.rcoef) == 0) { |
| 466 |
< |
copycolor(scol, sr.rcoef); |
| 451 |
< |
ns2go = 1; |
| 465 |
> |
rayorigin(&sr, SPECULAR, np->rp, sr.rcoef) == 0) { |
| 466 |
> |
nstarget = 1; |
| 467 |
|
if (specjitter > 1.5) { /* multiple samples? */ |
| 468 |
< |
ns2go = specjitter*r->rweight + .5; |
| 469 |
< |
if ((d = bright(scol)) <= minweight*ns2go) |
| 470 |
< |
ns2go = d/minweight; |
| 471 |
< |
if (ns2go > 1) { |
| 472 |
< |
d = 1./ns2go; |
| 473 |
< |
scalecolor(scol, d); |
| 468 |
> |
nstarget = specjitter*np->rp->rweight + .5; |
| 469 |
> |
if (sr.rweight <= minweight*nstarget) |
| 470 |
> |
nstarget = sr.rweight/minweight; |
| 471 |
> |
if (nstarget > 1) { |
| 472 |
> |
d = 1./nstarget; |
| 473 |
> |
scalecolor(sr.rcoef, d); |
| 474 |
> |
sr.rweight *= d; |
| 475 |
|
} else |
| 476 |
< |
ns2go = 1; |
| 476 |
> |
nstarget = 1; |
| 477 |
|
} |
| 478 |
< |
dimlist[ndims++] = (int)np->mp; |
| 479 |
< |
for (niter = ns2go*MAXITER; (ns2go > 0) & (niter > 0); niter--) { |
| 480 |
< |
if (specjitter > 1.5) |
| 478 |
> |
dimlist[ndims++] = (int)(size_t)np->mp; |
| 479 |
> |
maxiter = MAXITER*nstarget; |
| 480 |
> |
for (nstaken = ntrials = 0; nstaken < nstarget && |
| 481 |
> |
ntrials < maxiter; ntrials++) { |
| 482 |
> |
if (ntrials) |
| 483 |
|
d = frandom(); |
| 484 |
|
else |
| 485 |
< |
d = urand(ilhash(dimlist,ndims)+1823+samplendx); |
| 485 |
> |
d = urand(ilhash(dimlist,ndims)+samplendx); |
| 486 |
|
multisamp(rv, 2, d); |
| 487 |
|
d = 2.0*PI * rv[0]; |
| 488 |
|
cosp = tcos(d); |
| 495 |
|
d = sqrt( np->alpha2 * -log(rv[1]) ); |
| 496 |
|
for (i = 0; i < 3; i++) |
| 497 |
|
sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]); |
| 498 |
< |
if (DOT(sr.rdir, r->ron) >= -FTINY) |
| 498 |
> |
/* sample rejection test */ |
| 499 |
> |
if (DOT(sr.rdir, np->rp->ron) >= -FTINY) |
| 500 |
|
continue; |
| 501 |
|
normalize(sr.rdir); /* OK, normalize */ |
| 502 |
< |
if (specjitter > 1.5) { /* multi-sampling */ |
| 484 |
< |
copycolor(sr.rcoef, scol); |
| 502 |
> |
if (nstaken) /* multi-sampling */ |
| 503 |
|
rayclear(&sr); |
| 486 |
– |
} |
| 504 |
|
rayvalue(&sr); |
| 505 |
|
multcolor(sr.rcol, sr.rcoef); |
| 506 |
< |
addcolor(r->rcol, sr.rcol); |
| 507 |
< |
--ns2go; |
| 506 |
> |
addcolor(np->rp->rcol, sr.rcol); |
| 507 |
> |
++nstaken; |
| 508 |
|
} |
| 509 |
|
ndims--; |
| 510 |
|
} |