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 |
|
|
106 |
|
{ |
107 |
|
rayorigin(rayOut, rayOutType, rayIn, NULL); |
108 |
|
|
109 |
< |
/* Transfer flux */ |
110 |
< |
copycolor(rayOut -> rcol, rayIn -> rcol); |
111 |
< |
|
112 |
< |
/* Copy caustic flag & direction for transferred rays */ |
113 |
< |
if (rayOutType == PMAP_XFER) { |
114 |
< |
/* rayOut -> rtype |= rayIn -> rtype & SPECULAR; */ |
115 |
< |
rayOut -> rtype |= rayIn -> rtype; |
116 |
< |
VCOPY(rayOut -> rdir, rayIn -> rdir); |
117 |
< |
} |
118 |
< |
else if (fluxAtten) { |
119 |
< |
/* Attenuate and normalised flux for scattered rays */ |
120 |
< |
multcolor(rayOut -> rcol, fluxAtten); |
121 |
< |
colorNorm(rayOut -> rcol); |
122 |
< |
} |
109 |
> |
if (rayIn) { |
110 |
> |
/* Transfer flux */ |
111 |
> |
copycolor(rayOut -> rcol, rayIn -> rcol); |
112 |
> |
|
113 |
> |
/* Copy caustic flag & direction for transferred rays */ |
114 |
> |
if (rayOutType == PMAP_XFER) { |
115 |
> |
/* rayOut -> rtype |= rayIn -> rtype & SPECULAR; */ |
116 |
> |
rayOut -> rtype |= rayIn -> rtype; |
117 |
> |
VCOPY(rayOut -> rdir, rayIn -> rdir); |
118 |
> |
} |
119 |
> |
else if (fluxAtten) { |
120 |
> |
/* Attenuate and normalise flux for scattered rays */ |
121 |
> |
multcolor(rayOut -> rcol, fluxAtten); |
122 |
> |
colorNorm(rayOut -> rcol); |
123 |
> |
} |
124 |
|
|
125 |
< |
/* Propagate index of emitting light source */ |
126 |
< |
rayOut -> rsrc = rayIn -> rsrc; |
125 |
> |
/* Propagate index of emitting light source */ |
126 |
> |
rayOut -> rsrc = rayIn -> rsrc; |
127 |
> |
|
128 |
> |
/* Update maximum photon path distance */ |
129 |
> |
rayOut -> rmax = rayIn -> rmax - rayIn -> rot; |
130 |
> |
} |
131 |
|
} |
132 |
|
|
133 |
|
|
137 |
|
{ |
138 |
|
if (!r -> rlvl) |
139 |
|
/* Add direct photon map at primary hitpoint */ |
140 |
< |
addPhoton(directPmap, r); |
140 |
> |
newPhoton(directPmap, r); |
141 |
|
else { |
142 |
|
/* Add global or precomputed photon map at indirect hitpoint */ |
143 |
< |
addPhoton(preCompPmap ? preCompPmap : globalPmap, r); |
143 |
> |
newPhoton(preCompPmap ? preCompPmap : globalPmap, r); |
144 |
|
|
145 |
|
/* Store caustic photon if specular flag set */ |
146 |
|
if (PMAP_CAUSTICRAY(r)) |
147 |
< |
addPhoton(causticPmap, r); |
147 |
> |
newPhoton(causticPmap, r); |
148 |
|
|
149 |
|
/* Store in contribution photon map */ |
150 |
< |
addPhoton(contribPmap, r); |
150 |
> |
newPhoton(contribPmap, r); |
151 |
|
} |
152 |
|
} |
153 |
|
|
206 |
|
int niter, i = 0; |
207 |
|
|
208 |
|
/* Set up sample coordinates */ |
209 |
< |
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 |
< |
|
209 |
> |
getperpendicular(u, nd -> pnorm, 1); |
210 |
|
fcross(v, nd -> pnorm, u); |
211 |
|
|
212 |
|
if (nd -> specfl & SP_REFL) { |
270 |
|
int i = 0; |
271 |
|
|
272 |
|
/* Set up sample coordinates */ |
273 |
< |
do { |
271 |
< |
v [0] = v [1] = v [2] = 0; |
272 |
< |
v [i++] = 1; |
273 |
< |
fcross(u, v, normal); |
274 |
< |
} while (normalize(u) < FTINY); |
275 |
< |
|
273 |
> |
getperpendicular(u, normal, 1); |
274 |
|
fcross(v, normal, u); |
275 |
|
|
276 |
|
/* Convert theta & phi to cartesian */ |
321 |
|
nd.specfl |= SP_FLAT; |
322 |
|
|
323 |
|
/* Perturb normal */ |
324 |
< |
if ((hastexture = DOT(rayIn -> pert, rayIn -> pert)) > sqr(FTINY)) |
324 |
> |
if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY)) )) |
325 |
|
nd.pdot = raynormal(nd.pnorm, rayIn); |
326 |
|
else { |
327 |
|
VCOPY(nd.pnorm, rayIn -> ron); |
750 |
|
/* get modifiers */ |
751 |
|
raytexture(rayIn, mat -> omod); |
752 |
|
|
753 |
< |
if ((hastexture = DOT(rayIn -> pert, rayIn -> pert)) > FTINY * FTINY) |
753 |
> |
if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > FTINY * FTINY))) |
754 |
|
/* Perturb normal */ |
755 |
|
cos1 = raynormal(dnorm, rayIn); |
756 |
|
else { |
897 |
|
/* reorient if necessary */ |
898 |
|
if (rayIn -> rod < 0) |
899 |
|
flipsurface(rayIn); |
900 |
< |
if ((hastexture = DOT(rayIn -> pert, rayIn -> pert)) > FTINY * FTINY) |
900 |
> |
if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > FTINY * FTINY) )) |
901 |
|
pdot = raynormal(pnorm, rayIn); |
902 |
|
else { |
903 |
|
VCOPY(pnorm, rayIn -> ron); |
983 |
|
|
984 |
|
/* Straight replacement? */ |
985 |
|
if (!mat -> oargs.nsargs) { |
986 |
< |
mat = objptr(mat -> omod); |
987 |
< |
photonScatter [mat -> otype] (mat, rayIn); |
986 |
> |
/* Skip void modifier! */ |
987 |
> |
if (mat -> omod != OVOID) { |
988 |
> |
mat = objptr(mat -> omod); |
989 |
> |
photonScatter [mat -> otype] (mat, rayIn); |
990 |
> |
} |
991 |
|
|
992 |
|
return 0; |
993 |
|
} |
1258 |
|
if (errno) |
1259 |
|
objerror(mat, WARNING, "compute error"); |
1260 |
|
else { |
1261 |
< |
mat = objptr(mod [pmapRandom(rouletteState) < coef ? 0 : 1]); |
1262 |
< |
photonScatter [mat -> otype] (mat, rayIn); |
1261 |
> |
OBJECT mxMod = mod [pmapRandom(rouletteState) < coef ? 0 : 1]; |
1262 |
> |
|
1263 |
> |
if (mxMod != OVOID) { |
1264 |
> |
mat = objptr(mxMod); |
1265 |
> |
photonScatter [mat -> otype] (mat, rayIn); |
1266 |
> |
} |
1267 |
> |
else { |
1268 |
> |
/* Transfer ray if no modifier */ |
1269 |
> |
RAY rayOut; |
1270 |
> |
|
1271 |
> |
photonRay(rayIn, &rayOut, PMAP_XFER, NULL); |
1272 |
> |
tracePhoton(&rayOut); |
1273 |
> |
} |
1274 |
|
} |
1275 |
|
|
1276 |
|
return 0; |
1322 |
|
if (errno) |
1323 |
|
objerror(mat, WARNING, "compute error"); |
1324 |
|
else { |
1325 |
< |
mat = objptr(mod [pmapRandom(rouletteState) < coef ? 0 : 1]); |
1326 |
< |
photonScatter [mat -> otype] (mat, rayIn); |
1325 |
> |
OBJECT mxMod = mod [pmapRandom(rouletteState) < coef ? 0 : 1]; |
1326 |
> |
|
1327 |
> |
if (mxMod != OVOID) { |
1328 |
> |
mat = objptr(mxMod); |
1329 |
> |
photonScatter [mat -> otype] (mat, rayIn); |
1330 |
> |
} |
1331 |
> |
else { |
1332 |
> |
/* Transfer ray if no modifier */ |
1333 |
> |
RAY rayOut; |
1334 |
> |
|
1335 |
> |
photonRay(rayIn, &rayOut, PMAP_XFER, NULL); |
1336 |
> |
tracePhoton(&rayOut); |
1337 |
> |
} |
1338 |
|
} |
1339 |
|
|
1340 |
|
return 0; |
1373 |
|
if (errno) |
1374 |
|
objerror(mat, WARNING, "compute error"); |
1375 |
|
else { |
1376 |
< |
mat = objptr(mod [pmapRandom(rouletteState) < coef ? 0 : 1]); |
1377 |
< |
photonScatter [mat -> otype] (mat, rayIn); |
1376 |
> |
OBJECT mxMod = mod [pmapRandom(rouletteState) < coef ? 0 : 1]; |
1377 |
> |
|
1378 |
> |
if (mxMod != OVOID) { |
1379 |
> |
mat = objptr(mxMod); |
1380 |
> |
photonScatter [mat -> otype] (mat, rayIn); |
1381 |
> |
} |
1382 |
> |
else { |
1383 |
> |
/* Transfer ray if no modifier */ |
1384 |
> |
RAY rayOut; |
1385 |
> |
|
1386 |
> |
photonRay(rayIn, &rayOut, PMAP_XFER, NULL); |
1387 |
> |
tracePhoton(&rayOut); |
1388 |
> |
} |
1389 |
|
} |
1390 |
|
|
1391 |
|
return 0; |
1417 |
|
|
1418 |
|
|
1419 |
|
|
1420 |
+ |
/* |
1421 |
+ |
================================================================== |
1422 |
+ |
The following code is |
1423 |
+ |
(c) Lucerne University of Applied Sciences and Arts, |
1424 |
+ |
supported by the Swiss National Science Foundation (SNSF, #147053) |
1425 |
+ |
================================================================== |
1426 |
+ |
*/ |
1427 |
+ |
|
1428 |
|
static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn) |
1429 |
|
/* Generate new photon ray for BSDF modifier and recurse. */ |
1430 |
|
{ |
1431 |
+ |
int hasthick = (mat->otype == MAT_BSDF); |
1432 |
|
int hitFront; |
1433 |
|
SDError err; |
1434 |
+ |
SDValue bsdfVal; |
1435 |
|
FVECT upvec; |
1436 |
|
MFUNC *mf; |
1437 |
|
BSDFDAT nd; |
1438 |
|
RAY rayOut; |
1439 |
< |
|
1439 |
> |
COLOR bsdfRGB; |
1440 |
> |
int transmitted; |
1441 |
> |
double prDiff, ptDiff, prDiffSD, ptDiffSD, prSpecSD, ptSpecSD, |
1442 |
> |
albedo, xi; |
1443 |
> |
const double patAlb = bright(rayIn -> pcol); |
1444 |
> |
|
1445 |
|
/* Following code adapted from m_bsdf() */ |
1446 |
|
/* Check arguments */ |
1447 |
< |
if (mat -> oargs.nsargs < 6 || mat -> oargs.nfargs > 9 || |
1447 |
> |
if (mat -> oargs.nsargs < hasthick+5 || mat -> oargs.nfargs > 9 || |
1448 |
|
mat -> oargs.nfargs % 3) |
1449 |
|
objerror(mat, USER, "bad # arguments"); |
1450 |
|
|
1451 |
< |
hitFront = (rayIn -> rod > 0); |
1451 |
> |
hitFront = (rayIn -> rod > 0); |
1452 |
|
|
1453 |
< |
/* Load cal file */ |
1454 |
< |
mf = getfunc(mat, 5, 0x1d, 1); |
1455 |
< |
|
1456 |
< |
/* Get thickness */ |
1453 |
> |
/* Load cal file */ |
1454 |
> |
mf = hasthick ? getfunc(mat, 5, 0x1d, 1) : getfunc(mat, 4, 0xe, 1); |
1455 |
> |
|
1456 |
> |
/* Get thickness */ |
1457 |
> |
nd.thick = 0; |
1458 |
> |
if (hasthick) { |
1459 |
|
nd.thick = evalue(mf -> ep [0]); |
1460 |
|
if ((-FTINY <= nd.thick) & (nd.thick <= FTINY)) |
1461 |
|
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; |
1462 |
|
} |
1463 |
|
|
1464 |
|
/* Get BSDF data */ |
1465 |
< |
nd.sd = loadBSDF(mat -> oargs.sarg [1]); |
1465 |
> |
nd.sd = loadBSDF(mat -> oargs.sarg [hasthick]); |
1466 |
|
|
1467 |
< |
/* Diffuse reflectance */ |
1467 |
> |
/* Extra diffuse reflectance from material def */ |
1468 |
|
if (hitFront) { |
1469 |
|
if (mat -> oargs.nfargs < 3) |
1470 |
|
setcolor(nd.rdiff, .0, .0, .0); |
1483 |
|
else setcolor(nd.rdiff, mat -> oargs.farg [3], mat -> oargs.farg [4], |
1484 |
|
mat -> oargs.farg [5]); |
1485 |
|
|
1486 |
< |
/* Diffuse transmittance */ |
1487 |
< |
if (mat -> oargs.nfargs < 9) |
1488 |
< |
setcolor(nd.tdiff, .0, .0, .0); |
1486 |
> |
/* Extra diffuse transmittance from material def */ |
1487 |
> |
if (mat -> oargs.nfargs < 9) |
1488 |
> |
setcolor(nd.tdiff, .0, .0, .0); |
1489 |
|
else setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7], |
1490 |
|
mat -> oargs.farg [8]); |
1491 |
|
|
1492 |
|
nd.mp = mat; |
1493 |
|
nd.pr = rayIn; |
1494 |
< |
|
1494 |
> |
|
1495 |
|
/* Get modifiers */ |
1496 |
|
raytexture(rayIn, mat -> omod); |
1497 |
|
|
1498 |
|
/* Modify diffuse values */ |
1499 |
|
multcolor(nd.rdiff, rayIn -> pcol); |
1500 |
|
multcolor(nd.tdiff, rayIn -> pcol); |
1501 |
< |
|
1501 |
> |
|
1502 |
|
/* Get up vector & xform to world coords */ |
1503 |
< |
upvec [0] = evalue(mf -> ep [1]); |
1504 |
< |
upvec [1] = evalue(mf -> ep [2]); |
1505 |
< |
upvec [2] = evalue(mf -> ep [3]); |
1503 |
> |
upvec [0] = evalue(mf -> ep [hasthick+0]); |
1504 |
> |
upvec [1] = evalue(mf -> ep [hasthick+1]); |
1505 |
> |
upvec [2] = evalue(mf -> ep [hasthick+2]); |
1506 |
|
|
1507 |
|
if (mf -> fxp != &unitxf) { |
1508 |
|
multv3(upvec, upvec, mf -> fxp -> xfm); |
1545 |
|
nd.sr_vpsa [1] = sqrt(nd.sr_vpsa [1]); |
1546 |
|
|
1547 |
|
/* Orient perturbed normal towards incident side */ |
1548 |
< |
if (!hitFront) { |
1548 |
> |
if (!hitFront) { |
1549 |
|
nd.pnorm [0] = -nd.pnorm [0]; |
1550 |
|
nd.pnorm [1] = -nd.pnorm [1]; |
1551 |
|
nd.pnorm [2] = -nd.pnorm [2]; |
1552 |
|
} |
1553 |
< |
|
1554 |
< |
/* Following code adapted from SDsampBSDF() */ |
1555 |
< |
{ |
1556 |
< |
SDSpectralDF *rdf, *tdf; |
1557 |
< |
SDValue bsdfVal; |
1558 |
< |
double xi, rhoDiff = 0; |
1559 |
< |
float coef [SDmaxCh]; |
1560 |
< |
int i, j, n, nr; |
1561 |
< |
SDComponent *sdc; |
1562 |
< |
const SDCDst **cdarr = NULL; |
1563 |
< |
|
1564 |
< |
/* Get diffuse albedo (?) */ |
1565 |
< |
if (hitFront) { |
1566 |
< |
bsdfVal = nd.sd -> rLambFront; |
1567 |
< |
rdf = nd.sd -> rf; |
1568 |
< |
tdf = nd.sd -> tf ? nd.sd -> tf : nd.sd -> tb; |
1569 |
< |
} |
1570 |
< |
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)); |
1553 |
> |
|
1554 |
> |
/* Get scatter probabilities (weighted by pattern except for spec refl) |
1555 |
> |
* prDiff, ptDiff: extra diffuse component in material def |
1556 |
> |
* prDiffSD, ptDiffSD: diffuse (constant) component in SDF |
1557 |
> |
* prSpecSD, ptSpecSD: non-diffuse ("specular") component in SDF |
1558 |
> |
* albedo: sum of above, inverse absorption probability */ |
1559 |
> |
prDiff = colorAvg(nd.rdiff); |
1560 |
> |
ptDiff = colorAvg(nd.tdiff); |
1561 |
> |
prDiffSD = patAlb * SDdirectHemi(nd.vray, SDsampDf | SDsampR, nd.sd); |
1562 |
> |
ptDiffSD = patAlb * SDdirectHemi(nd.vray, SDsampDf | SDsampT, nd.sd); |
1563 |
> |
prSpecSD = SDdirectHemi(nd.vray, SDsampSp | SDsampR, nd.sd); |
1564 |
> |
ptSpecSD = patAlb * SDdirectHemi(nd.vray, SDsampSp | SDsampT, nd.sd); |
1565 |
> |
albedo = prDiff + ptDiff + prDiffSD + ptDiffSD + prSpecSD + ptSpecSD; |
1566 |
> |
|
1567 |
> |
/* |
1568 |
> |
if (albedo > 1) |
1569 |
> |
objerror(mat, WARNING, "Invalid albedo"); |
1570 |
> |
*/ |
1571 |
|
|
1572 |
< |
while (j-- > 0) { |
1573 |
< |
/* Sum up non-diffuse transmittance */ |
1574 |
< |
cdarr [i + j] = (*tdf -> comp [j].func -> getCDist)(nd.vray, &tdf -> comp [j]); |
1575 |
< |
|
1576 |
< |
if (!cdarr [i + j]) |
1548 |
< |
cdarr [i + j] = &SDemptyCD; |
1549 |
< |
else bsdfVal.cieY += cdarr [i + j] -> cTotal; |
1550 |
< |
} |
1572 |
> |
/* Insert direct and indirect photon hits if diffuse component */ |
1573 |
> |
if (prDiff + ptDiff + prDiffSD + ptDiffSD > FTINY) |
1574 |
> |
addPhotons(rayIn); |
1575 |
> |
|
1576 |
> |
xi = pmapRandom(rouletteState); |
1577 |
|
|
1578 |
< |
while (i-- > 0) { |
1579 |
< |
/* Sum up non-diffuse reflectance */ |
1580 |
< |
cdarr [i] = (*rdf -> comp [i].func -> getCDist)(nd.vray, &rdf -> comp [i]); |
1578 |
> |
if (xi > albedo) |
1579 |
> |
/* Absorbtion */ |
1580 |
> |
return 0; |
1581 |
> |
|
1582 |
> |
transmitted = 0; |
1583 |
> |
|
1584 |
> |
if ((xi -= prDiff) <= 0) { |
1585 |
> |
/* Diffuse reflection (extra component in material def) */ |
1586 |
> |
photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.rdiff); |
1587 |
> |
diffPhotonScatter(nd.pnorm, &rayOut); |
1588 |
> |
} |
1589 |
> |
|
1590 |
> |
else if ((xi -= ptDiff) <= 0) { |
1591 |
> |
/* Diffuse transmission (extra component in material def) */ |
1592 |
> |
photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.tdiff); |
1593 |
> |
diffPhotonScatter(nd.pnorm, &rayOut); |
1594 |
> |
transmitted = 1; |
1595 |
> |
} |
1596 |
> |
|
1597 |
> |
else { /* Sample SDF */ |
1598 |
> |
if ((xi -= prDiffSD) <= 0) { |
1599 |
> |
/* Diffuse SDF reflection (constant component) */ |
1600 |
> |
if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState), |
1601 |
> |
SDsampDf | SDsampR, nd.sd))) |
1602 |
> |
objerror(mat, USER, transSDError(err)); |
1603 |
|
|
1604 |
< |
if (!cdarr [i]) |
1605 |
< |
cdarr [i] = &SDemptyCD; |
1606 |
< |
else bsdfVal.cieY += cdarr [i] -> cTotal; |
1604 |
> |
/* Apply pattern to spectral component */ |
1605 |
> |
ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB); |
1606 |
> |
multcolor(bsdfRGB, rayIn -> pcol); |
1607 |
> |
photonRay(rayIn, &rayOut, PMAP_DIFFREFL, bsdfRGB); |
1608 |
|
} |
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 |
– |
} |
1609 |
|
|
1610 |
< |
if (i < nr) { |
1611 |
< |
/* Non-diffuse reflection */ |
1612 |
< |
sdc = &rdf -> comp [i]; |
1613 |
< |
rayOutType = PMAP_SPECREFL; |
1614 |
< |
} |
1605 |
< |
else { |
1606 |
< |
/* Non-diffuse transmission */ |
1607 |
< |
sdc = &tdf -> comp [i - nr]; |
1608 |
< |
rayOutType = PMAP_SPECTRANS; |
1609 |
< |
} |
1610 |
> |
else if ((xi -= ptDiffSD) <= 0) { |
1611 |
> |
/* Diffuse SDF transmission (constant component) */ |
1612 |
> |
if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState), |
1613 |
> |
SDsampDf | SDsampT, nd.sd))) |
1614 |
> |
objerror(mat, USER, transSDError(err)); |
1615 |
|
|
1616 |
< |
/* 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); |
1616 |
> |
/* Apply pattern to spectral component */ |
1617 |
|
ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB); |
1618 |
< |
|
1619 |
< |
/* Xform outgoing dir to world coords */ |
1620 |
< |
if ((err = SDmapDir(rayOut.rdir, nd.fromloc, rayOut.rdir))) { |
1618 |
> |
multcolor(bsdfRGB, rayIn -> pcol); |
1619 |
> |
addcolor(bsdfRGB, nd.tdiff); |
1620 |
> |
photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, bsdfRGB); |
1621 |
> |
transmitted = 1; |
1622 |
> |
} |
1623 |
> |
|
1624 |
> |
else if ((xi -= prSpecSD) <= 0) { |
1625 |
> |
/* Non-diffuse ("specular") SDF reflection */ |
1626 |
> |
if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState), |
1627 |
> |
SDsampSp | SDsampR, nd.sd))) |
1628 |
|
objerror(mat, USER, transSDError(err)); |
1643 |
– |
return 0; |
1644 |
– |
} |
1629 |
|
|
1630 |
< |
photonRay(rayIn, &rayOut, rayOutType, bsdfRGB); |
1630 |
> |
ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB); |
1631 |
> |
photonRay(rayIn, &rayOut, PMAP_SPECREFL, bsdfRGB); |
1632 |
|
} |
1633 |
|
|
1634 |
< |
if (cdarr) |
1635 |
< |
free(cdarr); |
1634 |
> |
else { |
1635 |
> |
/* Non-diffuse ("specular") SDF transmission */ |
1636 |
> |
if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState), |
1637 |
> |
SDsampSp | SDsampT, nd.sd))) |
1638 |
> |
objerror(mat, USER, transSDError(err)); |
1639 |
> |
|
1640 |
> |
/* Apply pattern to spectral component */ |
1641 |
> |
ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB); |
1642 |
> |
multcolor(bsdfRGB, rayIn -> pcol); |
1643 |
> |
photonRay(rayIn, &rayOut, PMAP_SPECTRANS, bsdfRGB); |
1644 |
> |
transmitted = 1; |
1645 |
> |
} |
1646 |
> |
|
1647 |
> |
/* Xform outgoing dir to world coords */ |
1648 |
> |
if ((err = SDmapDir(rayOut.rdir, nd.fromloc, nd.vray))) { |
1649 |
> |
objerror(mat, USER, transSDError(err)); |
1650 |
> |
return 0; |
1651 |
> |
} |
1652 |
|
} |
1653 |
< |
|
1654 |
< |
/* Clean up BSDF */ |
1653 |
> |
|
1654 |
> |
/* Clean up */ |
1655 |
|
SDfreeCache(nd.sd); |
1656 |
|
|
1657 |
+ |
/* Offset outgoing photon origin by thickness to bypass proxy geometry */ |
1658 |
+ |
if (transmitted && nd.thick != 0) |
1659 |
+ |
VSUM(rayOut.rorg, rayOut.rorg, rayIn -> ron, -nd.thick); |
1660 |
+ |
|
1661 |
|
tracePhoton(&rayOut); |
1662 |
|
return 0; |
1663 |
|
} |
1665 |
|
|
1666 |
|
|
1667 |
|
static int lightPhotonScatter (OBJREC* mat, RAY* ray) |
1668 |
< |
/* Light sources doan' reflect */ |
1668 |
> |
/* Light sources doan' reflect, mang */ |
1669 |
|
{ |
1670 |
|
return 0; |
1671 |
|
} |
1707 |
|
photonScatter [TEX_DATA] = pattexPhotonScatter; |
1708 |
|
|
1709 |
|
photonScatter [MOD_ALIAS] = aliasPhotonScatter; |
1710 |
< |
photonScatter [MAT_BSDF] = bsdfPhotonScatter; |
1710 |
> |
photonScatter [MAT_BSDF] = |
1711 |
> |
photonScatter [MAT_SBSDF] = bsdfPhotonScatter; |
1712 |
|
} |