1 |
+ |
#ifndef lint |
2 |
+ |
static const char RCSid[] = "$Id$"; |
3 |
+ |
#endif |
4 |
|
/* |
5 |
|
================================================================== |
6 |
|
Photon map support routines for scattering by materials. |
7 |
|
|
8 |
|
Roland Schregle (roland.schregle@{hslu.ch, gmail.com}) |
9 |
|
(c) Fraunhofer Institute for Solar Energy Systems, |
10 |
< |
Lucerne University of Applied Sciences & Arts |
10 |
> |
(c) Lucerne University of Applied Sciences and Arts, |
11 |
> |
supported by the Swiss National Science Foundation (SNSF, #147053) |
12 |
|
================================================================== |
13 |
|
|
10 |
– |
$Id$ |
14 |
|
*/ |
15 |
|
|
16 |
|
|
116 |
|
VCOPY(rayOut -> rdir, rayIn -> rdir); |
117 |
|
} |
118 |
|
else if (fluxAtten) { |
119 |
< |
/* Attenuate and normalised flux for scattered rays */ |
119 |
> |
/* Attenuate and normalise flux for scattered rays */ |
120 |
|
multcolor(rayOut -> rcol, fluxAtten); |
121 |
|
colorNorm(rayOut -> rcol); |
122 |
|
} |
201 |
|
int niter, i = 0; |
202 |
|
|
203 |
|
/* Set up sample coordinates */ |
204 |
< |
do { |
202 |
< |
v [0] = v [1] = v [2] = 0; |
203 |
< |
v [i++] = 1; |
204 |
< |
fcross(u, v, nd -> pnorm); |
205 |
< |
} while (normalize(u) < FTINY); |
206 |
< |
|
204 |
> |
getperpendicular(u, nd -> pnorm, 1); |
205 |
|
fcross(v, nd -> pnorm, u); |
206 |
|
|
207 |
|
if (nd -> specfl & SP_REFL) { |
265 |
|
int i = 0; |
266 |
|
|
267 |
|
/* Set up sample coordinates */ |
268 |
< |
do { |
271 |
< |
v [0] = v [1] = v [2] = 0; |
272 |
< |
v [i++] = 1; |
273 |
< |
fcross(u, v, normal); |
274 |
< |
} while (normalize(u) < FTINY); |
275 |
< |
|
268 |
> |
getperpendicular(u, normal, 1); |
269 |
|
fcross(v, normal, u); |
270 |
|
|
271 |
|
/* Convert theta & phi to cartesian */ |
316 |
|
nd.specfl |= SP_FLAT; |
317 |
|
|
318 |
|
/* Perturb normal */ |
319 |
< |
if ((hastexture = DOT(rayIn -> pert, rayIn -> pert)) > sqr(FTINY)) |
319 |
> |
if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY)) )) |
320 |
|
nd.pdot = raynormal(nd.pnorm, rayIn); |
321 |
|
else { |
322 |
|
VCOPY(nd.pnorm, rayIn -> ron); |
745 |
|
/* get modifiers */ |
746 |
|
raytexture(rayIn, mat -> omod); |
747 |
|
|
748 |
< |
if ((hastexture = DOT(rayIn -> pert, rayIn -> pert)) > FTINY * FTINY) |
748 |
> |
if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > FTINY * FTINY))) |
749 |
|
/* Perturb normal */ |
750 |
|
cos1 = raynormal(dnorm, rayIn); |
751 |
|
else { |
892 |
|
/* reorient if necessary */ |
893 |
|
if (rayIn -> rod < 0) |
894 |
|
flipsurface(rayIn); |
895 |
< |
if ((hastexture = DOT(rayIn -> pert, rayIn -> pert)) > FTINY * FTINY) |
895 |
> |
if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > FTINY * FTINY) )) |
896 |
|
pdot = raynormal(pnorm, rayIn); |
897 |
|
else { |
898 |
|
VCOPY(pnorm, rayIn -> ron); |
1376 |
|
|
1377 |
|
|
1378 |
|
|
1379 |
+ |
/* |
1380 |
+ |
================================================================== |
1381 |
+ |
The following code is |
1382 |
+ |
(c) Lucerne University of Applied Sciences and Arts, |
1383 |
+ |
supported by the Swiss National Science Foundation (SNSF, #147053) |
1384 |
+ |
================================================================== |
1385 |
+ |
*/ |
1386 |
+ |
|
1387 |
|
static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn) |
1388 |
|
/* Generate new photon ray for BSDF modifier and recurse. */ |
1389 |
|
{ |
1390 |
|
int hitFront; |
1391 |
|
SDError err; |
1392 |
+ |
SDValue bsdfVal; |
1393 |
|
FVECT upvec; |
1394 |
|
MFUNC *mf; |
1395 |
|
BSDFDAT nd; |
1396 |
|
RAY rayOut; |
1397 |
< |
|
1397 |
> |
COLOR bsdfRGB; |
1398 |
> |
int transmitted; |
1399 |
> |
double prDiff, ptDiff, prDiffSD, ptDiffSD, prSpecSD, ptSpecSD, |
1400 |
> |
albedo, xi; |
1401 |
> |
const double patAlb = bright(rayIn -> pcol); |
1402 |
> |
|
1403 |
|
/* Following code adapted from m_bsdf() */ |
1404 |
|
/* Check arguments */ |
1405 |
|
if (mat -> oargs.nsargs < 6 || mat -> oargs.nfargs > 9 || |
1415 |
|
nd.thick = evalue(mf -> ep [0]); |
1416 |
|
if ((-FTINY <= nd.thick) & (nd.thick <= FTINY)) |
1417 |
|
nd.thick = .0; |
1411 |
– |
|
1412 |
– |
if (nd.thick != .0 || (!hitFront && !backvis)) { |
1413 |
– |
/* Proxy geometry present, so use it instead and transfer ray */ |
1414 |
– |
photonRay(rayIn, &rayOut, PMAP_XFER, NULL); |
1415 |
– |
tracePhoton(&rayOut); |
1416 |
– |
|
1417 |
– |
return 0; |
1418 |
– |
} |
1418 |
|
|
1419 |
|
/* Get BSDF data */ |
1420 |
|
nd.sd = loadBSDF(mat -> oargs.sarg [1]); |
1421 |
|
|
1422 |
< |
/* Diffuse reflectance */ |
1422 |
> |
/* Extra diffuse reflectance from material def */ |
1423 |
|
if (hitFront) { |
1424 |
|
if (mat -> oargs.nfargs < 3) |
1425 |
|
setcolor(nd.rdiff, .0, .0, .0); |
1438 |
|
else setcolor(nd.rdiff, mat -> oargs.farg [3], mat -> oargs.farg [4], |
1439 |
|
mat -> oargs.farg [5]); |
1440 |
|
|
1441 |
< |
/* Diffuse transmittance */ |
1441 |
> |
/* Extra diffuse transmittance from material def */ |
1442 |
|
if (mat -> oargs.nfargs < 9) |
1443 |
|
setcolor(nd.tdiff, .0, .0, .0); |
1444 |
|
else setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7], |
1505 |
|
nd.pnorm [1] = -nd.pnorm [1]; |
1506 |
|
nd.pnorm [2] = -nd.pnorm [2]; |
1507 |
|
} |
1508 |
< |
|
1509 |
< |
/* Following code adapted from SDsampBSDF() */ |
1510 |
< |
{ |
1511 |
< |
SDSpectralDF *rdf, *tdf; |
1512 |
< |
SDValue bsdfVal; |
1513 |
< |
double xi, rhoDiff = 0; |
1514 |
< |
float coef [SDmaxCh]; |
1515 |
< |
int i, j, n, nr; |
1516 |
< |
SDComponent *sdc; |
1517 |
< |
const SDCDst **cdarr = NULL; |
1518 |
< |
|
1519 |
< |
/* Get diffuse albedo (?) */ |
1520 |
< |
if (hitFront) { |
1521 |
< |
bsdfVal = nd.sd -> rLambFront; |
1522 |
< |
rdf = nd.sd -> rf; |
1523 |
< |
tdf = nd.sd -> tf ? nd.sd -> tf : nd.sd -> tb; |
1524 |
< |
} |
1525 |
< |
else { |
1527 |
< |
bsdfVal = nd.sd -> rLambBack; |
1528 |
< |
rdf = nd.sd -> rb; |
1529 |
< |
tdf = nd.sd -> tb ? nd.sd -> tb : nd.sd -> tf; |
1530 |
< |
} |
1531 |
< |
|
1532 |
< |
rhoDiff = bsdfVal.cieY; |
1533 |
< |
bsdfVal.cieY += nd.sd -> tLamb.cieY; |
1534 |
< |
|
1535 |
< |
/* Allocate non-diffuse sampling */ |
1536 |
< |
i = nr = rdf ? rdf -> ncomp : 0; |
1537 |
< |
j = tdf ? tdf -> ncomp : 0; |
1538 |
< |
n = i + j; |
1539 |
< |
|
1540 |
< |
if (n > 0 && !(cdarr = (const SDCDst**)malloc(n * sizeof(SDCDst*)))) |
1541 |
< |
objerror(mat, USER, transSDError(SDEmemory)); |
1508 |
> |
|
1509 |
> |
/* Get scatter probabilities (weighted by pattern except for spec refl) |
1510 |
> |
* prDiff, ptDiff: extra diffuse component in material def |
1511 |
> |
* prDiffSD, ptDiffSD: diffuse (constant) component in SDF |
1512 |
> |
* prSpecSD, ptSpecSD: non-diffuse ("specular") component in SDF |
1513 |
> |
* albedo: sum of above, inverse absorption probability */ |
1514 |
> |
prDiff = colorAvg(nd.rdiff); |
1515 |
> |
ptDiff = colorAvg(nd.tdiff); |
1516 |
> |
prDiffSD = patAlb * SDdirectHemi(nd.vray, SDsampDf | SDsampR, nd.sd); |
1517 |
> |
ptDiffSD = patAlb * SDdirectHemi(nd.vray, SDsampDf | SDsampT, nd.sd); |
1518 |
> |
prSpecSD = SDdirectHemi(nd.vray, SDsampSp | SDsampR, nd.sd); |
1519 |
> |
ptSpecSD = patAlb * SDdirectHemi(nd.vray, SDsampSp | SDsampT, nd.sd); |
1520 |
> |
albedo = prDiff + ptDiff + prDiffSD + ptDiffSD + prSpecSD + ptSpecSD; |
1521 |
> |
|
1522 |
> |
/* |
1523 |
> |
if (albedo > 1) |
1524 |
> |
objerror(mat, WARNING, "Invalid albedo"); |
1525 |
> |
*/ |
1526 |
|
|
1527 |
< |
while (j-- > 0) { |
1528 |
< |
/* Sum up non-diffuse transmittance */ |
1529 |
< |
cdarr [i + j] = (*tdf -> comp [j].func -> getCDist)(nd.vray, &tdf -> comp [j]); |
1530 |
< |
|
1531 |
< |
if (!cdarr [i + j]) |
1548 |
< |
cdarr [i + j] = &SDemptyCD; |
1549 |
< |
else bsdfVal.cieY += cdarr [i + j] -> cTotal; |
1550 |
< |
} |
1527 |
> |
/* Insert direct and indirect photon hits if diffuse component */ |
1528 |
> |
if (prDiff + ptDiff + prDiffSD + ptDiffSD > FTINY) |
1529 |
> |
addPhotons(rayIn); |
1530 |
> |
|
1531 |
> |
xi = pmapRandom(rouletteState); |
1532 |
|
|
1533 |
< |
while (i-- > 0) { |
1534 |
< |
/* Sum up non-diffuse reflectance */ |
1535 |
< |
cdarr [i] = (*rdf -> comp [i].func -> getCDist)(nd.vray, &rdf -> comp [i]); |
1533 |
> |
if (xi > albedo) |
1534 |
> |
/* Absorbtion */ |
1535 |
> |
return 0; |
1536 |
> |
|
1537 |
> |
transmitted = 0; |
1538 |
> |
|
1539 |
> |
if ((xi -= prDiff) <= 0) { |
1540 |
> |
/* Diffuse reflection (extra component in material def) */ |
1541 |
> |
photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.rdiff); |
1542 |
> |
diffPhotonScatter(nd.pnorm, &rayOut); |
1543 |
> |
} |
1544 |
> |
|
1545 |
> |
else if ((xi -= ptDiff) <= 0) { |
1546 |
> |
/* Diffuse transmission (extra component in material def) */ |
1547 |
> |
flipsurface(rayIn); |
1548 |
> |
nd.thick = -nd.thick; |
1549 |
> |
photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.tdiff); |
1550 |
> |
diffPhotonScatter(nd.pnorm, &rayOut); |
1551 |
> |
transmitted = 1; |
1552 |
> |
} |
1553 |
> |
|
1554 |
> |
else { /* Sample SDF */ |
1555 |
> |
if ((xi -= prDiffSD) <= 0) { |
1556 |
> |
/* Diffuse SDF reflection (constant component) */ |
1557 |
> |
if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState), |
1558 |
> |
SDsampDf | SDsampR, nd.sd))) |
1559 |
> |
objerror(mat, USER, transSDError(err)); |
1560 |
|
|
1561 |
< |
if (!cdarr [i]) |
1562 |
< |
cdarr [i] = &SDemptyCD; |
1563 |
< |
else bsdfVal.cieY += cdarr [i] -> cTotal; |
1561 |
> |
/* Apply pattern to spectral component */ |
1562 |
> |
ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB); |
1563 |
> |
multcolor(bsdfRGB, rayIn -> pcol); |
1564 |
> |
photonRay(rayIn, &rayOut, PMAP_DIFFREFL, bsdfRGB); |
1565 |
|
} |
1560 |
– |
|
1561 |
– |
if (bsdfVal.cieY <= FTINY) { |
1562 |
– |
/* Don't bother sampling, just absorb photon */ |
1563 |
– |
if (cdarr) |
1564 |
– |
free(cdarr); |
1565 |
– |
return 0; |
1566 |
– |
} |
1567 |
– |
|
1568 |
– |
/* Insert direct and indirect photon hits if diffuse component */ |
1569 |
– |
if (rhoDiff > FTINY || nd.sd -> tLamb.cieY > FTINY) |
1570 |
– |
addPhotons(rayIn); |
1571 |
– |
|
1572 |
– |
xi = pmapRandom(rouletteState); |
1573 |
– |
|
1574 |
– |
if ((xi -= rhoDiff) <= 0) { |
1575 |
– |
/* Diffuse reflection */ |
1576 |
– |
photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.rdiff); |
1577 |
– |
diffPhotonScatter(nd.pnorm, &rayOut); |
1578 |
– |
} |
1579 |
– |
else if ((xi -= nd.sd -> tLamb.cieY) <= 0) { |
1580 |
– |
/* Diffuse transmission */ |
1581 |
– |
flipsurface(rayIn); |
1582 |
– |
photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.tdiff); |
1583 |
– |
bsdfVal.spec = nd.sd -> tLamb.spec; |
1584 |
– |
diffPhotonScatter(nd.pnorm, &rayOut); |
1585 |
– |
} |
1586 |
– |
else { |
1587 |
– |
int rayOutType; |
1588 |
– |
COLOR bsdfRGB; |
1589 |
– |
|
1590 |
– |
/* Non-diffuse CDF inversion (?) */ |
1591 |
– |
for (i = 0; i < n && (xi -= cdarr [i] -> cTotal) > 0; i++); |
1592 |
– |
|
1593 |
– |
if (i >= n) { |
1594 |
– |
/* Absorbed -- photon went Deer Hunter */ |
1595 |
– |
if (cdarr) |
1596 |
– |
free(cdarr); |
1597 |
– |
return 0; |
1598 |
– |
} |
1566 |
|
|
1567 |
< |
if (i < nr) { |
1568 |
< |
/* Non-diffuse reflection */ |
1569 |
< |
sdc = &rdf -> comp [i]; |
1570 |
< |
rayOutType = PMAP_SPECREFL; |
1571 |
< |
} |
1605 |
< |
else { |
1606 |
< |
/* Non-diffuse transmission */ |
1607 |
< |
sdc = &tdf -> comp [i - nr]; |
1608 |
< |
rayOutType = PMAP_SPECTRANS; |
1609 |
< |
} |
1567 |
> |
else if ((xi -= ptDiffSD) <= 0) { |
1568 |
> |
/* Diffuse SDF transmission (constant component) */ |
1569 |
> |
if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState), |
1570 |
> |
SDsampDf | SDsampT, nd.sd))) |
1571 |
> |
objerror(mat, USER, transSDError(err)); |
1572 |
|
|
1573 |
< |
/* Generate non-diff sample dir */ |
1612 |
< |
VCOPY(rayOut.rdir, nd.vray); |
1613 |
< |
err = (*sdc -> func -> sampCDist) |
1614 |
< |
(rayOut.rdir, pmapRandom(scatterState), cdarr [i]); |
1615 |
< |
if (err) |
1616 |
< |
objerror(mat, USER, transSDError(SDEinternal)); |
1617 |
< |
|
1618 |
< |
/* Get colour */ |
1619 |
< |
j = (*sdc -> func -> getBSDFs)(coef, rayOut.rdir, nd.vray, sdc); |
1620 |
< |
|
1621 |
< |
if (j <= 0) { |
1622 |
< |
sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error", |
1623 |
< |
nd.sd -> name); |
1624 |
< |
objerror(mat, USER, transSDError(SDEinternal)); |
1625 |
< |
} |
1626 |
< |
|
1627 |
< |
bsdfVal.spec = sdc -> cspec [0]; |
1628 |
< |
rhoDiff = coef [0]; |
1629 |
< |
|
1630 |
< |
while (--j) { |
1631 |
< |
c_cmix(&bsdfVal.spec, rhoDiff, &bsdfVal.spec, coef [j], |
1632 |
< |
&sdc -> cspec [j]); |
1633 |
< |
rhoDiff += coef [j]; |
1634 |
< |
} |
1635 |
< |
|
1636 |
< |
/* ? */ |
1637 |
< |
c_ccvt(&bsdfVal.spec, C_CSXY + C_CSSPEC); |
1573 |
> |
/* Apply pattern to spectral component */ |
1574 |
|
ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB); |
1575 |
< |
|
1576 |
< |
/* Xform outgoing dir to world coords */ |
1577 |
< |
if ((err = SDmapDir(rayOut.rdir, nd.fromloc, rayOut.rdir))) { |
1575 |
> |
multcolor(bsdfRGB, rayIn -> pcol); |
1576 |
> |
addcolor(bsdfRGB, nd.tdiff); |
1577 |
> |
flipsurface(rayIn); /* Necessary? */ |
1578 |
> |
nd.thick = -nd.thick; |
1579 |
> |
photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, bsdfRGB); |
1580 |
> |
transmitted = 1; |
1581 |
> |
} |
1582 |
> |
|
1583 |
> |
else if ((xi -= prSpecSD) <= 0) { |
1584 |
> |
/* Non-diffuse ("specular") SDF reflection */ |
1585 |
> |
if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState), |
1586 |
> |
SDsampSp | SDsampR, nd.sd))) |
1587 |
|
objerror(mat, USER, transSDError(err)); |
1643 |
– |
return 0; |
1644 |
– |
} |
1588 |
|
|
1589 |
< |
photonRay(rayIn, &rayOut, rayOutType, bsdfRGB); |
1589 |
> |
ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB); |
1590 |
> |
photonRay(rayIn, &rayOut, PMAP_SPECREFL, bsdfRGB); |
1591 |
|
} |
1592 |
|
|
1593 |
< |
if (cdarr) |
1594 |
< |
free(cdarr); |
1593 |
> |
else { |
1594 |
> |
/* Non-diffuse ("specular") SDF transmission */ |
1595 |
> |
if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState), |
1596 |
> |
SDsampSp | SDsampT, nd.sd))) |
1597 |
> |
objerror(mat, USER, transSDError(err)); |
1598 |
> |
|
1599 |
> |
/* Apply pattern to spectral component */ |
1600 |
> |
ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB); |
1601 |
> |
multcolor(bsdfRGB, rayIn -> pcol); |
1602 |
> |
flipsurface(rayIn); /* Necessary? */ |
1603 |
> |
nd.thick = -nd.thick; |
1604 |
> |
photonRay(rayIn, &rayOut, PMAP_SPECTRANS, bsdfRGB); |
1605 |
> |
transmitted = 1; |
1606 |
> |
} |
1607 |
> |
|
1608 |
> |
/* Xform outgoing dir to world coords */ |
1609 |
> |
if ((err = SDmapDir(rayOut.rdir, nd.fromloc, nd.vray))) { |
1610 |
> |
objerror(mat, USER, transSDError(err)); |
1611 |
> |
return 0; |
1612 |
> |
} |
1613 |
|
} |
1614 |
< |
|
1615 |
< |
/* Clean up BSDF */ |
1614 |
> |
|
1615 |
> |
/* Clean up */ |
1616 |
|
SDfreeCache(nd.sd); |
1617 |
+ |
|
1618 |
+ |
/* Need to offset ray origin to get past detail geometry? */ |
1619 |
+ |
if (transmitted && nd.thick != 0) |
1620 |
+ |
VSUM(rayOut.rorg, rayOut.rorg, rayIn -> ron, -nd.thick); |
1621 |
|
|
1622 |
|
tracePhoton(&rayOut); |
1623 |
|
return 0; |