4 |
|
|
5 |
|
Roland Schregle (roland.schregle@{hslu.ch, gmail.com}) |
6 |
|
(c) Fraunhofer Institute for Solar Energy Systems, |
7 |
< |
Lucerne University of Applied Sciences & Arts |
7 |
> |
(c) Lucerne University of Applied Sciences and Arts, |
8 |
> |
supported by the Swiss National Science Foundation (SNSF, #147053) |
9 |
|
================================================================== |
10 |
|
|
11 |
|
$Id$ |
114 |
|
VCOPY(rayOut -> rdir, rayIn -> rdir); |
115 |
|
} |
116 |
|
else if (fluxAtten) { |
117 |
< |
/* Attenuate and normalised flux for scattered rays */ |
117 |
> |
/* Attenuate and normalise flux for scattered rays */ |
118 |
|
multcolor(rayOut -> rcol, fluxAtten); |
119 |
|
colorNorm(rayOut -> rcol); |
120 |
|
} |
1384 |
|
|
1385 |
|
|
1386 |
|
|
1387 |
+ |
#if 0 |
1388 |
+ |
static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn) |
1389 |
+ |
/* Generate new photon ray for BSDF modifier and recurse. */ |
1390 |
+ |
{ |
1391 |
+ |
int hitFront; |
1392 |
+ |
SDError err; |
1393 |
+ |
FVECT upvec; |
1394 |
+ |
MFUNC *mf; |
1395 |
+ |
BSDFDAT nd; |
1396 |
+ |
RAY rayOut; |
1397 |
+ |
|
1398 |
+ |
/* Following code adapted from m_bsdf() */ |
1399 |
+ |
/* Check arguments */ |
1400 |
+ |
if (mat -> oargs.nsargs < 6 || mat -> oargs.nfargs > 9 || |
1401 |
+ |
mat -> oargs.nfargs % 3) |
1402 |
+ |
objerror(mat, USER, "bad # arguments"); |
1403 |
+ |
|
1404 |
+ |
hitFront = (rayIn -> rod > 0); |
1405 |
+ |
|
1406 |
+ |
/* Load cal file */ |
1407 |
+ |
mf = getfunc(mat, 5, 0x1d, 1); |
1408 |
+ |
|
1409 |
+ |
/* Get thickness */ |
1410 |
+ |
nd.thick = evalue(mf -> ep [0]); |
1411 |
+ |
if ((-FTINY <= nd.thick) & (nd.thick <= FTINY)) |
1412 |
+ |
nd.thick = .0; |
1413 |
+ |
|
1414 |
+ |
if (nd.thick != .0 || (!hitFront && !backvis)) { |
1415 |
+ |
/* Proxy geometry present, so use it instead and transfer ray */ |
1416 |
+ |
photonRay(rayIn, &rayOut, PMAP_XFER, NULL); |
1417 |
+ |
tracePhoton(&rayOut); |
1418 |
+ |
|
1419 |
+ |
return 0; |
1420 |
+ |
} |
1421 |
+ |
|
1422 |
+ |
/* Get BSDF data */ |
1423 |
+ |
nd.sd = loadBSDF(mat -> oargs.sarg [1]); |
1424 |
+ |
|
1425 |
+ |
/* Diffuse reflectance */ |
1426 |
+ |
if (hitFront) { |
1427 |
+ |
if (mat -> oargs.nfargs < 3) |
1428 |
+ |
setcolor(nd.rdiff, .0, .0, .0); |
1429 |
+ |
else setcolor(nd.rdiff, mat -> oargs.farg [0], mat -> oargs.farg [1], |
1430 |
+ |
mat -> oargs.farg [2]); |
1431 |
+ |
} |
1432 |
+ |
else if (mat -> oargs.nfargs < 6) { |
1433 |
+ |
/* Check for absorbing backside */ |
1434 |
+ |
if (!backvis && !nd.sd -> rb && !nd.sd -> tf) { |
1435 |
+ |
SDfreeCache(nd.sd); |
1436 |
+ |
return 0; |
1437 |
+ |
} |
1438 |
+ |
|
1439 |
+ |
setcolor(nd.rdiff, .0, .0, .0); |
1440 |
+ |
} |
1441 |
+ |
else setcolor(nd.rdiff, mat -> oargs.farg [3], mat -> oargs.farg [4], |
1442 |
+ |
mat -> oargs.farg [5]); |
1443 |
+ |
|
1444 |
+ |
/* Diffuse transmittance */ |
1445 |
+ |
if (mat -> oargs.nfargs < 9) |
1446 |
+ |
setcolor(nd.tdiff, .0, .0, .0); |
1447 |
+ |
else setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7], |
1448 |
+ |
mat -> oargs.farg [8]); |
1449 |
+ |
|
1450 |
+ |
nd.mp = mat; |
1451 |
+ |
nd.pr = rayIn; |
1452 |
+ |
|
1453 |
+ |
/* Get modifiers */ |
1454 |
+ |
raytexture(rayIn, mat -> omod); |
1455 |
+ |
|
1456 |
+ |
/* Modify diffuse values */ |
1457 |
+ |
multcolor(nd.rdiff, rayIn -> pcol); |
1458 |
+ |
multcolor(nd.tdiff, rayIn -> pcol); |
1459 |
+ |
|
1460 |
+ |
/* Get up vector & xform to world coords */ |
1461 |
+ |
upvec [0] = evalue(mf -> ep [1]); |
1462 |
+ |
upvec [1] = evalue(mf -> ep [2]); |
1463 |
+ |
upvec [2] = evalue(mf -> ep [3]); |
1464 |
+ |
|
1465 |
+ |
if (mf -> fxp != &unitxf) { |
1466 |
+ |
multv3(upvec, upvec, mf -> fxp -> xfm); |
1467 |
+ |
nd.thick *= mf -> fxp -> sca; |
1468 |
+ |
} |
1469 |
+ |
|
1470 |
+ |
if (rayIn -> rox) { |
1471 |
+ |
multv3(upvec, upvec, rayIn -> rox -> f.xfm); |
1472 |
+ |
nd.thick *= rayIn -> rox -> f.sca; |
1473 |
+ |
} |
1474 |
+ |
|
1475 |
+ |
/* Perturb normal */ |
1476 |
+ |
raynormal(nd.pnorm, rayIn); |
1477 |
+ |
|
1478 |
+ |
/* Xform incident dir to local BSDF coords */ |
1479 |
+ |
err = SDcompXform(nd.toloc, nd.pnorm, upvec); |
1480 |
+ |
|
1481 |
+ |
if (!err) { |
1482 |
+ |
nd.vray [0] = -rayIn -> rdir [0]; |
1483 |
+ |
nd.vray [1] = -rayIn -> rdir [1]; |
1484 |
+ |
nd.vray [2] = -rayIn -> rdir [2]; |
1485 |
+ |
err = SDmapDir(nd.vray, nd.toloc, nd.vray); |
1486 |
+ |
} |
1487 |
+ |
|
1488 |
+ |
if (!err) |
1489 |
+ |
err = SDinvXform(nd.fromloc, nd.toloc); |
1490 |
+ |
|
1491 |
+ |
if (err) { |
1492 |
+ |
objerror(mat, WARNING, "Illegal orientation vector"); |
1493 |
+ |
return 0; |
1494 |
+ |
} |
1495 |
+ |
|
1496 |
+ |
/* Determine BSDF resolution */ |
1497 |
+ |
err = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin + SDqueryMax, nd.sd); |
1498 |
+ |
|
1499 |
+ |
if (err) |
1500 |
+ |
objerror(mat, USER, transSDError(err)); |
1501 |
+ |
|
1502 |
+ |
nd.sr_vpsa [0] = sqrt(nd.sr_vpsa [0]); |
1503 |
+ |
nd.sr_vpsa [1] = sqrt(nd.sr_vpsa [1]); |
1504 |
+ |
|
1505 |
+ |
/* Orient perturbed normal towards incident side */ |
1506 |
+ |
if (!hitFront) { |
1507 |
+ |
nd.pnorm [0] = -nd.pnorm [0]; |
1508 |
+ |
nd.pnorm [1] = -nd.pnorm [1]; |
1509 |
+ |
nd.pnorm [2] = -nd.pnorm [2]; |
1510 |
+ |
} |
1511 |
+ |
|
1512 |
+ |
/* Following code adapted from SDsampBSDF() */ |
1513 |
+ |
{ |
1514 |
+ |
SDSpectralDF *rdf, *tdf; |
1515 |
+ |
SDValue bsdfVal; |
1516 |
+ |
double xi, rhoDiff = 0; |
1517 |
+ |
float coef [SDmaxCh]; |
1518 |
+ |
int i, j, n, nr; |
1519 |
+ |
SDComponent *sdc; |
1520 |
+ |
const SDCDst **cdarr = NULL; |
1521 |
+ |
|
1522 |
+ |
/* Get diffuse albedo (?) */ |
1523 |
+ |
if (hitFront) { |
1524 |
+ |
bsdfVal = nd.sd -> rLambFront; |
1525 |
+ |
rdf = nd.sd -> rf; |
1526 |
+ |
tdf = nd.sd -> tf ? nd.sd -> tf : nd.sd -> tb; |
1527 |
+ |
} |
1528 |
+ |
else { |
1529 |
+ |
bsdfVal = nd.sd -> rLambBack; |
1530 |
+ |
rdf = nd.sd -> rb; |
1531 |
+ |
tdf = nd.sd -> tb ? nd.sd -> tb : nd.sd -> tf; |
1532 |
+ |
} |
1533 |
+ |
|
1534 |
+ |
rhoDiff = bsdfVal.cieY; |
1535 |
+ |
bsdfVal.cieY += nd.sd -> tLamb.cieY; |
1536 |
+ |
|
1537 |
+ |
/* Allocate non-diffuse sampling */ |
1538 |
+ |
i = nr = rdf ? rdf -> ncomp : 0; |
1539 |
+ |
j = tdf ? tdf -> ncomp : 0; |
1540 |
+ |
n = i + j; |
1541 |
+ |
|
1542 |
+ |
if (n > 0 && !(cdarr = (const SDCDst**)malloc(n * sizeof(SDCDst*)))) |
1543 |
+ |
objerror(mat, USER, transSDError(SDEmemory)); |
1544 |
+ |
|
1545 |
+ |
while (j-- > 0) { |
1546 |
+ |
/* Sum up non-diffuse transmittance */ |
1547 |
+ |
cdarr [i + j] = (*tdf -> comp [j].func -> getCDist)(nd.vray, &tdf -> comp [j]); |
1548 |
+ |
|
1549 |
+ |
if (!cdarr [i + j]) |
1550 |
+ |
cdarr [i + j] = &SDemptyCD; |
1551 |
+ |
else bsdfVal.cieY += cdarr [i + j] -> cTotal; |
1552 |
+ |
} |
1553 |
+ |
|
1554 |
+ |
while (i-- > 0) { |
1555 |
+ |
/* Sum up non-diffuse reflectance */ |
1556 |
+ |
cdarr [i] = (*rdf -> comp [i].func -> getCDist)(nd.vray, &rdf -> comp [i]); |
1557 |
+ |
|
1558 |
+ |
if (!cdarr [i]) |
1559 |
+ |
cdarr [i] = &SDemptyCD; |
1560 |
+ |
else bsdfVal.cieY += cdarr [i] -> cTotal; |
1561 |
+ |
} |
1562 |
+ |
|
1563 |
+ |
if (bsdfVal.cieY <= FTINY) { |
1564 |
+ |
/* Don't bother sampling, just absorb photon */ |
1565 |
+ |
if (cdarr) |
1566 |
+ |
free(cdarr); |
1567 |
+ |
return 0; |
1568 |
+ |
} |
1569 |
+ |
|
1570 |
+ |
/* Insert direct and indirect photon hits if diffuse component */ |
1571 |
+ |
if (rhoDiff > FTINY || nd.sd -> tLamb.cieY > FTINY) |
1572 |
+ |
addPhotons(rayIn); |
1573 |
+ |
|
1574 |
+ |
xi = pmapRandom(rouletteState); |
1575 |
+ |
|
1576 |
+ |
if ((xi -= rhoDiff) <= 0) { |
1577 |
+ |
/* Diffuse reflection */ |
1578 |
+ |
photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.rdiff); |
1579 |
+ |
diffPhotonScatter(nd.pnorm, &rayOut); |
1580 |
+ |
} |
1581 |
+ |
else if ((xi -= nd.sd -> tLamb.cieY) <= 0) { |
1582 |
+ |
/* Diffuse transmission */ |
1583 |
+ |
flipsurface(rayIn); |
1584 |
+ |
photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.tdiff); |
1585 |
+ |
bsdfVal.spec = nd.sd -> tLamb.spec; |
1586 |
+ |
diffPhotonScatter(nd.pnorm, &rayOut); |
1587 |
+ |
} |
1588 |
+ |
else { |
1589 |
+ |
int rayOutType; |
1590 |
+ |
COLOR bsdfRGB; |
1591 |
+ |
|
1592 |
+ |
/* Non-diffuse CDF inversion (?) */ |
1593 |
+ |
for (i = 0; i < n && (xi -= cdarr [i] -> cTotal) > 0; i++); |
1594 |
+ |
|
1595 |
+ |
if (i >= n) { |
1596 |
+ |
/* Absorbed -- photon went Deer Hunter */ |
1597 |
+ |
if (cdarr) |
1598 |
+ |
free(cdarr); |
1599 |
+ |
return 0; |
1600 |
+ |
} |
1601 |
+ |
|
1602 |
+ |
if (i < nr) { |
1603 |
+ |
/* Non-diffuse reflection */ |
1604 |
+ |
sdc = &rdf -> comp [i]; |
1605 |
+ |
rayOutType = PMAP_SPECREFL; |
1606 |
+ |
} |
1607 |
+ |
else { |
1608 |
+ |
/* Non-diffuse transmission */ |
1609 |
+ |
sdc = &tdf -> comp [i - nr]; |
1610 |
+ |
rayOutType = PMAP_SPECTRANS; |
1611 |
+ |
} |
1612 |
+ |
|
1613 |
+ |
/* Generate non-diff sample dir */ |
1614 |
+ |
VCOPY(rayOut.rdir, nd.vray); |
1615 |
+ |
err = (*sdc -> func -> sampCDist) |
1616 |
+ |
(rayOut.rdir, pmapRandom(scatterState), cdarr [i]); |
1617 |
+ |
if (err) |
1618 |
+ |
objerror(mat, USER, transSDError(SDEinternal)); |
1619 |
+ |
|
1620 |
+ |
/* Get colour */ |
1621 |
+ |
j = (*sdc -> func -> getBSDFs)(coef, rayOut.rdir, nd.vray, sdc); |
1622 |
+ |
|
1623 |
+ |
if (j <= 0) { |
1624 |
+ |
sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error", |
1625 |
+ |
nd.sd -> name); |
1626 |
+ |
objerror(mat, USER, transSDError(SDEinternal)); |
1627 |
+ |
} |
1628 |
+ |
|
1629 |
+ |
bsdfVal.spec = sdc -> cspec [0]; |
1630 |
+ |
rhoDiff = coef [0]; |
1631 |
+ |
|
1632 |
+ |
while (--j) { |
1633 |
+ |
c_cmix(&bsdfVal.spec, rhoDiff, &bsdfVal.spec, coef [j], |
1634 |
+ |
&sdc -> cspec [j]); |
1635 |
+ |
rhoDiff += coef [j]; |
1636 |
+ |
} |
1637 |
+ |
|
1638 |
+ |
/* ? */ |
1639 |
+ |
c_ccvt(&bsdfVal.spec, C_CSXY + C_CSSPEC); |
1640 |
+ |
ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB); |
1641 |
+ |
|
1642 |
+ |
/* Xform outgoing dir to world coords */ |
1643 |
+ |
if ((err = SDmapDir(rayOut.rdir, nd.fromloc, rayOut.rdir))) { |
1644 |
+ |
objerror(mat, USER, transSDError(err)); |
1645 |
+ |
return 0; |
1646 |
+ |
} |
1647 |
+ |
|
1648 |
+ |
photonRay(rayIn, &rayOut, rayOutType, bsdfRGB); |
1649 |
+ |
} |
1650 |
+ |
|
1651 |
+ |
if (cdarr) |
1652 |
+ |
free(cdarr); |
1653 |
+ |
} |
1654 |
+ |
|
1655 |
+ |
/* Clean up BSDF */ |
1656 |
+ |
SDfreeCache(nd.sd); |
1657 |
+ |
|
1658 |
+ |
tracePhoton(&rayOut); |
1659 |
+ |
return 0; |
1660 |
+ |
} |
1661 |
+ |
#else |
1662 |
+ |
|
1663 |
+ |
/* |
1664 |
+ |
The following code is |
1665 |
+ |
(c) Lucerne University of Applied Sciences and Arts, |
1666 |
+ |
supported by the Swiss National Science Foundation (SNSF, #147053) |
1667 |
+ |
*/ |
1668 |
+ |
|
1669 |
|
static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn) |
1670 |
|
/* Generate new photon ray for BSDF modifier and recurse. */ |
1671 |
|
{ |
1672 |
|
int hitFront; |
1673 |
|
SDError err; |
1674 |
+ |
SDValue bsdfVal; |
1675 |
|
FVECT upvec; |
1676 |
|
MFUNC *mf; |
1677 |
|
BSDFDAT nd; |
1678 |
|
RAY rayOut; |
1679 |
< |
|
1679 |
> |
COLOR bsdfRGB; |
1680 |
> |
double prDiff, ptDiff, prDiffSD, ptDiffSD, prSpecSD, ptSpecSD, |
1681 |
> |
albedo, xi, xi2; |
1682 |
> |
const double patAlb = colorAvg(rayIn -> pcol); |
1683 |
> |
|
1684 |
|
/* Following code adapted from m_bsdf() */ |
1685 |
|
/* Check arguments */ |
1686 |
|
if (mat -> oargs.nsargs < 6 || mat -> oargs.nfargs > 9 || |
1708 |
|
/* Get BSDF data */ |
1709 |
|
nd.sd = loadBSDF(mat -> oargs.sarg [1]); |
1710 |
|
|
1711 |
< |
/* Diffuse reflectance */ |
1711 |
> |
/* Extra diffuse reflectance from material def */ |
1712 |
|
if (hitFront) { |
1713 |
|
if (mat -> oargs.nfargs < 3) |
1714 |
|
setcolor(nd.rdiff, .0, .0, .0); |
1727 |
|
else setcolor(nd.rdiff, mat -> oargs.farg [3], mat -> oargs.farg [4], |
1728 |
|
mat -> oargs.farg [5]); |
1729 |
|
|
1730 |
< |
/* Diffuse transmittance */ |
1730 |
> |
/* Extra diffuse transmittance from material def */ |
1731 |
|
if (mat -> oargs.nfargs < 9) |
1732 |
|
setcolor(nd.tdiff, .0, .0, .0); |
1733 |
|
else setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7], |
1794 |
|
nd.pnorm [1] = -nd.pnorm [1]; |
1795 |
|
nd.pnorm [2] = -nd.pnorm [2]; |
1796 |
|
} |
1797 |
< |
|
1798 |
< |
/* Following code adapted from SDsampBSDF() */ |
1799 |
< |
{ |
1800 |
< |
SDSpectralDF *rdf, *tdf; |
1801 |
< |
SDValue bsdfVal; |
1802 |
< |
double xi, rhoDiff = 0; |
1803 |
< |
float coef [SDmaxCh]; |
1804 |
< |
int i, j, n, nr; |
1805 |
< |
SDComponent *sdc; |
1806 |
< |
const SDCDst **cdarr = NULL; |
1807 |
< |
|
1808 |
< |
/* Get diffuse albedo (?) */ |
1809 |
< |
if (hitFront) { |
1810 |
< |
bsdfVal = nd.sd -> rLambFront; |
1811 |
< |
rdf = nd.sd -> rf; |
1812 |
< |
tdf = nd.sd -> tf ? nd.sd -> tf : nd.sd -> tb; |
1813 |
< |
} |
1814 |
< |
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)); |
1797 |
> |
|
1798 |
> |
/* Get scatter probabilities (weighted by pattern except for spec refl) |
1799 |
> |
* prDiff, ptDiff: extra diffuse component in material def |
1800 |
> |
* prDiffSD, ptDiffSD: diffuse (constant) component in SDF |
1801 |
> |
* prSpecSD, ptSpecSD: non-diffuse ("specular") component in SDF |
1802 |
> |
* albedo: sum of above, inverse absorption probability */ |
1803 |
> |
prDiff = colorAvg(nd.rdiff); |
1804 |
> |
ptDiff = colorAvg(nd.tdiff); |
1805 |
> |
prDiffSD = patAlb * SDdirectHemi(nd.vray, SDsampDf | SDsampR, nd.sd); |
1806 |
> |
ptDiffSD = patAlb * SDdirectHemi(nd.vray, SDsampDf | SDsampT, nd.sd); |
1807 |
> |
prSpecSD = SDdirectHemi(nd.vray, SDsampSp | SDsampR, nd.sd); |
1808 |
> |
ptSpecSD = patAlb * SDdirectHemi(nd.vray, SDsampSp | SDsampT, nd.sd); |
1809 |
> |
albedo = prDiff + ptDiff + prDiffSD + ptDiffSD + prSpecSD + ptSpecSD; |
1810 |
> |
|
1811 |
> |
/* |
1812 |
> |
if (albedo > 1) |
1813 |
> |
objerror(mat, WARNING, "Invalid albedo"); |
1814 |
> |
*/ |
1815 |
|
|
1816 |
< |
while (j-- > 0) { |
1817 |
< |
/* Sum up non-diffuse transmittance */ |
1818 |
< |
cdarr [i + j] = (*tdf -> comp [j].func -> getCDist)(nd.vray, &tdf -> comp [j]); |
1819 |
< |
|
1820 |
< |
if (!cdarr [i + j]) |
1548 |
< |
cdarr [i + j] = &SDemptyCD; |
1549 |
< |
else bsdfVal.cieY += cdarr [i + j] -> cTotal; |
1550 |
< |
} |
1816 |
> |
/* Insert direct and indirect photon hits if diffuse component */ |
1817 |
> |
if (prDiff + ptDiff + prDiffSD + ptDiffSD > FTINY) |
1818 |
> |
addPhotons(rayIn); |
1819 |
> |
|
1820 |
> |
xi = xi2 = pmapRandom(rouletteState); |
1821 |
|
|
1822 |
< |
while (i-- > 0) { |
1823 |
< |
/* Sum up non-diffuse reflectance */ |
1824 |
< |
cdarr [i] = (*rdf -> comp [i].func -> getCDist)(nd.vray, &rdf -> comp [i]); |
1822 |
> |
if (xi > albedo) |
1823 |
> |
/* Absorbtion */ |
1824 |
> |
return 0; |
1825 |
> |
|
1826 |
> |
if ((xi -= prDiff) <= 0) { |
1827 |
> |
/* Diffuse reflection (extra component in material def) */ |
1828 |
> |
photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.rdiff); |
1829 |
> |
diffPhotonScatter(nd.pnorm, &rayOut); |
1830 |
> |
} |
1831 |
> |
|
1832 |
> |
else if ((xi -= ptDiff) <= 0) { |
1833 |
> |
/* Diffuse transmission (extra component in material def) */ |
1834 |
> |
flipsurface(rayIn); |
1835 |
> |
photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.tdiff); |
1836 |
> |
diffPhotonScatter(nd.pnorm, &rayOut); |
1837 |
> |
} |
1838 |
> |
|
1839 |
> |
else { /* Sample SDF */ |
1840 |
> |
if ((xi -= prDiffSD) <= 0) { |
1841 |
> |
/* Diffuse SDF reflection (constant component) */ |
1842 |
> |
if ((err = SDsampBSDF(&bsdfVal, nd.vray, xi2, |
1843 |
> |
SDsampDf | SDsampR, nd.sd))) |
1844 |
> |
objerror(mat, USER, transSDError(err)); |
1845 |
|
|
1846 |
< |
if (!cdarr [i]) |
1847 |
< |
cdarr [i] = &SDemptyCD; |
1848 |
< |
else bsdfVal.cieY += cdarr [i] -> cTotal; |
1846 |
> |
/* Apply pattern to spectral component */ |
1847 |
> |
ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB); |
1848 |
> |
multcolor(bsdfRGB, rayIn -> pcol); |
1849 |
> |
photonRay(rayIn, &rayOut, PMAP_DIFFREFL, bsdfRGB); |
1850 |
|
} |
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 |
– |
} |
1851 |
|
|
1852 |
< |
if (i < nr) { |
1853 |
< |
/* Non-diffuse reflection */ |
1854 |
< |
sdc = &rdf -> comp [i]; |
1855 |
< |
rayOutType = PMAP_SPECREFL; |
1856 |
< |
} |
1605 |
< |
else { |
1606 |
< |
/* Non-diffuse transmission */ |
1607 |
< |
sdc = &tdf -> comp [i - nr]; |
1608 |
< |
rayOutType = PMAP_SPECTRANS; |
1609 |
< |
} |
1852 |
> |
else if ((xi -= ptDiffSD) <= 0) { |
1853 |
> |
/* Diffuse SDF transmission (constant component) */ |
1854 |
> |
if ((err = SDsampBSDF(&bsdfVal, nd.vray, xi2, |
1855 |
> |
SDsampDf | SDsampT, nd.sd))) |
1856 |
> |
objerror(mat, USER, transSDError(err)); |
1857 |
|
|
1858 |
< |
/* 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); |
1858 |
> |
/* Apply pattern to spectral component */ |
1859 |
|
ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB); |
1860 |
< |
|
1861 |
< |
/* Xform outgoing dir to world coords */ |
1862 |
< |
if ((err = SDmapDir(rayOut.rdir, nd.fromloc, rayOut.rdir))) { |
1860 |
> |
multcolor(bsdfRGB, rayIn -> pcol); |
1861 |
> |
addcolor(bsdfRGB, nd.tdiff); |
1862 |
> |
flipsurface(rayIn); /* Necessary? */ |
1863 |
> |
photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, bsdfRGB); |
1864 |
> |
} |
1865 |
> |
|
1866 |
> |
else if ((xi -= prSpecSD) <= 0) { |
1867 |
> |
/* Non-diffuse ("specular") SDF reflection */ |
1868 |
> |
if ((err = SDsampBSDF(&bsdfVal, nd.vray, xi2, |
1869 |
> |
SDsampSp | SDsampR, nd.sd))) |
1870 |
|
objerror(mat, USER, transSDError(err)); |
1643 |
– |
return 0; |
1644 |
– |
} |
1871 |
|
|
1872 |
< |
photonRay(rayIn, &rayOut, rayOutType, bsdfRGB); |
1872 |
> |
ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB); |
1873 |
> |
photonRay(rayIn, &rayOut, PMAP_SPECREFL, bsdfRGB); |
1874 |
|
} |
1875 |
|
|
1876 |
< |
if (cdarr) |
1877 |
< |
free(cdarr); |
1876 |
> |
else { |
1877 |
> |
/* Non-diffuse ("specular") SDF transmission */ |
1878 |
> |
if ((err = SDsampBSDF(&bsdfVal, nd.vray, xi2, |
1879 |
> |
SDsampSp | SDsampT, nd.sd))) |
1880 |
> |
objerror(mat, USER, transSDError(err)); |
1881 |
> |
|
1882 |
> |
/* Apply pattern to spectral component */ |
1883 |
> |
ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB); |
1884 |
> |
multcolor(bsdfRGB, rayIn -> pcol); |
1885 |
> |
flipsurface(rayIn); /* Necessary? */ |
1886 |
> |
photonRay(rayIn, &rayOut, PMAP_SPECTRANS, bsdfRGB); |
1887 |
> |
} |
1888 |
> |
|
1889 |
> |
/* Xform outgoing dir to world coords */ |
1890 |
> |
if ((err = SDmapDir(rayOut.rdir, nd.fromloc, nd.vray))) { |
1891 |
> |
objerror(mat, USER, transSDError(err)); |
1892 |
> |
return 0; |
1893 |
> |
} |
1894 |
|
} |
1895 |
< |
|
1896 |
< |
/* Clean up BSDF */ |
1895 |
> |
|
1896 |
> |
/* Clean up */ |
1897 |
|
SDfreeCache(nd.sd); |
1898 |
|
|
1899 |
|
tracePhoton(&rayOut); |
1900 |
|
return 0; |
1901 |
|
} |
1902 |
+ |
#endif |
1903 |
|
|
1904 |
|
|
1905 |
|
|