1 |
+ |
#ifndef lint |
2 |
+ |
static const char RCSid[] = "$Id$"; |
3 |
+ |
#endif |
4 |
|
/* |
5 |
|
================================================================== |
6 |
|
Photon map support routines for scattering by materials. |
11 |
|
supported by the Swiss National Science Foundation (SNSF, #147053) |
12 |
|
================================================================== |
13 |
|
|
11 |
– |
$Id$ |
14 |
|
*/ |
15 |
|
|
16 |
|
|
1376 |
|
|
1377 |
|
|
1378 |
|
|
1377 |
– |
#if 0 |
1378 |
– |
static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn) |
1379 |
– |
/* Generate new photon ray for BSDF modifier and recurse. */ |
1380 |
– |
{ |
1381 |
– |
int hitFront; |
1382 |
– |
SDError err; |
1383 |
– |
FVECT upvec; |
1384 |
– |
MFUNC *mf; |
1385 |
– |
BSDFDAT nd; |
1386 |
– |
RAY rayOut; |
1387 |
– |
|
1388 |
– |
/* Following code adapted from m_bsdf() */ |
1389 |
– |
/* Check arguments */ |
1390 |
– |
if (mat -> oargs.nsargs < 6 || mat -> oargs.nfargs > 9 || |
1391 |
– |
mat -> oargs.nfargs % 3) |
1392 |
– |
objerror(mat, USER, "bad # arguments"); |
1393 |
– |
|
1394 |
– |
hitFront = (rayIn -> rod > 0); |
1395 |
– |
|
1396 |
– |
/* Load cal file */ |
1397 |
– |
mf = getfunc(mat, 5, 0x1d, 1); |
1398 |
– |
|
1399 |
– |
/* Get thickness */ |
1400 |
– |
nd.thick = evalue(mf -> ep [0]); |
1401 |
– |
if ((-FTINY <= nd.thick) & (nd.thick <= FTINY)) |
1402 |
– |
nd.thick = .0; |
1403 |
– |
|
1404 |
– |
if (nd.thick != .0 || (!hitFront && !backvis)) { |
1405 |
– |
/* Proxy geometry present, so use it instead and transfer ray */ |
1406 |
– |
photonRay(rayIn, &rayOut, PMAP_XFER, NULL); |
1407 |
– |
tracePhoton(&rayOut); |
1408 |
– |
|
1409 |
– |
return 0; |
1410 |
– |
} |
1411 |
– |
|
1412 |
– |
/* Get BSDF data */ |
1413 |
– |
nd.sd = loadBSDF(mat -> oargs.sarg [1]); |
1414 |
– |
|
1415 |
– |
/* Diffuse reflectance */ |
1416 |
– |
if (hitFront) { |
1417 |
– |
if (mat -> oargs.nfargs < 3) |
1418 |
– |
setcolor(nd.rdiff, .0, .0, .0); |
1419 |
– |
else setcolor(nd.rdiff, mat -> oargs.farg [0], mat -> oargs.farg [1], |
1420 |
– |
mat -> oargs.farg [2]); |
1421 |
– |
} |
1422 |
– |
else if (mat -> oargs.nfargs < 6) { |
1423 |
– |
/* Check for absorbing backside */ |
1424 |
– |
if (!backvis && !nd.sd -> rb && !nd.sd -> tf) { |
1425 |
– |
SDfreeCache(nd.sd); |
1426 |
– |
return 0; |
1427 |
– |
} |
1428 |
– |
|
1429 |
– |
setcolor(nd.rdiff, .0, .0, .0); |
1430 |
– |
} |
1431 |
– |
else setcolor(nd.rdiff, mat -> oargs.farg [3], mat -> oargs.farg [4], |
1432 |
– |
mat -> oargs.farg [5]); |
1433 |
– |
|
1434 |
– |
/* Diffuse transmittance */ |
1435 |
– |
if (mat -> oargs.nfargs < 9) |
1436 |
– |
setcolor(nd.tdiff, .0, .0, .0); |
1437 |
– |
else setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7], |
1438 |
– |
mat -> oargs.farg [8]); |
1439 |
– |
|
1440 |
– |
nd.mp = mat; |
1441 |
– |
nd.pr = rayIn; |
1442 |
– |
|
1443 |
– |
/* Get modifiers */ |
1444 |
– |
raytexture(rayIn, mat -> omod); |
1445 |
– |
|
1446 |
– |
/* Modify diffuse values */ |
1447 |
– |
multcolor(nd.rdiff, rayIn -> pcol); |
1448 |
– |
multcolor(nd.tdiff, rayIn -> pcol); |
1449 |
– |
|
1450 |
– |
/* Get up vector & xform to world coords */ |
1451 |
– |
upvec [0] = evalue(mf -> ep [1]); |
1452 |
– |
upvec [1] = evalue(mf -> ep [2]); |
1453 |
– |
upvec [2] = evalue(mf -> ep [3]); |
1454 |
– |
|
1455 |
– |
if (mf -> fxp != &unitxf) { |
1456 |
– |
multv3(upvec, upvec, mf -> fxp -> xfm); |
1457 |
– |
nd.thick *= mf -> fxp -> sca; |
1458 |
– |
} |
1459 |
– |
|
1460 |
– |
if (rayIn -> rox) { |
1461 |
– |
multv3(upvec, upvec, rayIn -> rox -> f.xfm); |
1462 |
– |
nd.thick *= rayIn -> rox -> f.sca; |
1463 |
– |
} |
1464 |
– |
|
1465 |
– |
/* Perturb normal */ |
1466 |
– |
raynormal(nd.pnorm, rayIn); |
1467 |
– |
|
1468 |
– |
/* Xform incident dir to local BSDF coords */ |
1469 |
– |
err = SDcompXform(nd.toloc, nd.pnorm, upvec); |
1470 |
– |
|
1471 |
– |
if (!err) { |
1472 |
– |
nd.vray [0] = -rayIn -> rdir [0]; |
1473 |
– |
nd.vray [1] = -rayIn -> rdir [1]; |
1474 |
– |
nd.vray [2] = -rayIn -> rdir [2]; |
1475 |
– |
err = SDmapDir(nd.vray, nd.toloc, nd.vray); |
1476 |
– |
} |
1477 |
– |
|
1478 |
– |
if (!err) |
1479 |
– |
err = SDinvXform(nd.fromloc, nd.toloc); |
1480 |
– |
|
1481 |
– |
if (err) { |
1482 |
– |
objerror(mat, WARNING, "Illegal orientation vector"); |
1483 |
– |
return 0; |
1484 |
– |
} |
1485 |
– |
|
1486 |
– |
/* Determine BSDF resolution */ |
1487 |
– |
err = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin + SDqueryMax, nd.sd); |
1488 |
– |
|
1489 |
– |
if (err) |
1490 |
– |
objerror(mat, USER, transSDError(err)); |
1491 |
– |
|
1492 |
– |
nd.sr_vpsa [0] = sqrt(nd.sr_vpsa [0]); |
1493 |
– |
nd.sr_vpsa [1] = sqrt(nd.sr_vpsa [1]); |
1494 |
– |
|
1495 |
– |
/* Orient perturbed normal towards incident side */ |
1496 |
– |
if (!hitFront) { |
1497 |
– |
nd.pnorm [0] = -nd.pnorm [0]; |
1498 |
– |
nd.pnorm [1] = -nd.pnorm [1]; |
1499 |
– |
nd.pnorm [2] = -nd.pnorm [2]; |
1500 |
– |
} |
1501 |
– |
|
1502 |
– |
/* Following code adapted from SDsampBSDF() */ |
1503 |
– |
{ |
1504 |
– |
SDSpectralDF *rdf, *tdf; |
1505 |
– |
SDValue bsdfVal; |
1506 |
– |
double xi, rhoDiff = 0; |
1507 |
– |
float coef [SDmaxCh]; |
1508 |
– |
int i, j, n, nr; |
1509 |
– |
SDComponent *sdc; |
1510 |
– |
const SDCDst **cdarr = NULL; |
1511 |
– |
|
1512 |
– |
/* Get diffuse albedo (?) */ |
1513 |
– |
if (hitFront) { |
1514 |
– |
bsdfVal = nd.sd -> rLambFront; |
1515 |
– |
rdf = nd.sd -> rf; |
1516 |
– |
tdf = nd.sd -> tf ? nd.sd -> tf : nd.sd -> tb; |
1517 |
– |
} |
1518 |
– |
else { |
1519 |
– |
bsdfVal = nd.sd -> rLambBack; |
1520 |
– |
rdf = nd.sd -> rb; |
1521 |
– |
tdf = nd.sd -> tb ? nd.sd -> tb : nd.sd -> tf; |
1522 |
– |
} |
1523 |
– |
|
1524 |
– |
rhoDiff = bsdfVal.cieY; |
1525 |
– |
bsdfVal.cieY += nd.sd -> tLamb.cieY; |
1526 |
– |
|
1527 |
– |
/* Allocate non-diffuse sampling */ |
1528 |
– |
i = nr = rdf ? rdf -> ncomp : 0; |
1529 |
– |
j = tdf ? tdf -> ncomp : 0; |
1530 |
– |
n = i + j; |
1531 |
– |
|
1532 |
– |
if (n > 0 && !(cdarr = (const SDCDst**)malloc(n * sizeof(SDCDst*)))) |
1533 |
– |
objerror(mat, USER, transSDError(SDEmemory)); |
1534 |
– |
|
1535 |
– |
while (j-- > 0) { |
1536 |
– |
/* Sum up non-diffuse transmittance */ |
1537 |
– |
cdarr [i + j] = (*tdf -> comp [j].func -> getCDist)(nd.vray, &tdf -> comp [j]); |
1538 |
– |
|
1539 |
– |
if (!cdarr [i + j]) |
1540 |
– |
cdarr [i + j] = &SDemptyCD; |
1541 |
– |
else bsdfVal.cieY += cdarr [i + j] -> cTotal; |
1542 |
– |
} |
1543 |
– |
|
1544 |
– |
while (i-- > 0) { |
1545 |
– |
/* Sum up non-diffuse reflectance */ |
1546 |
– |
cdarr [i] = (*rdf -> comp [i].func -> getCDist)(nd.vray, &rdf -> comp [i]); |
1547 |
– |
|
1548 |
– |
if (!cdarr [i]) |
1549 |
– |
cdarr [i] = &SDemptyCD; |
1550 |
– |
else bsdfVal.cieY += cdarr [i] -> cTotal; |
1551 |
– |
} |
1552 |
– |
|
1553 |
– |
if (bsdfVal.cieY <= FTINY) { |
1554 |
– |
/* Don't bother sampling, just absorb photon */ |
1555 |
– |
if (cdarr) |
1556 |
– |
free(cdarr); |
1557 |
– |
return 0; |
1558 |
– |
} |
1559 |
– |
|
1560 |
– |
/* Insert direct and indirect photon hits if diffuse component */ |
1561 |
– |
if (rhoDiff > FTINY || nd.sd -> tLamb.cieY > FTINY) |
1562 |
– |
addPhotons(rayIn); |
1563 |
– |
|
1564 |
– |
xi = pmapRandom(rouletteState); |
1565 |
– |
|
1566 |
– |
if ((xi -= rhoDiff) <= 0) { |
1567 |
– |
/* Diffuse reflection */ |
1568 |
– |
photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.rdiff); |
1569 |
– |
diffPhotonScatter(nd.pnorm, &rayOut); |
1570 |
– |
} |
1571 |
– |
else if ((xi -= nd.sd -> tLamb.cieY) <= 0) { |
1572 |
– |
/* Diffuse transmission */ |
1573 |
– |
flipsurface(rayIn); |
1574 |
– |
photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.tdiff); |
1575 |
– |
bsdfVal.spec = nd.sd -> tLamb.spec; |
1576 |
– |
diffPhotonScatter(nd.pnorm, &rayOut); |
1577 |
– |
} |
1578 |
– |
else { |
1579 |
– |
int rayOutType; |
1580 |
– |
COLOR bsdfRGB; |
1581 |
– |
|
1582 |
– |
/* Non-diffuse CDF inversion (?) */ |
1583 |
– |
for (i = 0; i < n && (xi -= cdarr [i] -> cTotal) > 0; i++); |
1584 |
– |
|
1585 |
– |
if (i >= n) { |
1586 |
– |
/* Absorbed -- photon went Deer Hunter */ |
1587 |
– |
if (cdarr) |
1588 |
– |
free(cdarr); |
1589 |
– |
return 0; |
1590 |
– |
} |
1591 |
– |
|
1592 |
– |
if (i < nr) { |
1593 |
– |
/* Non-diffuse reflection */ |
1594 |
– |
sdc = &rdf -> comp [i]; |
1595 |
– |
rayOutType = PMAP_SPECREFL; |
1596 |
– |
} |
1597 |
– |
else { |
1598 |
– |
/* Non-diffuse transmission */ |
1599 |
– |
sdc = &tdf -> comp [i - nr]; |
1600 |
– |
rayOutType = PMAP_SPECTRANS; |
1601 |
– |
} |
1602 |
– |
|
1603 |
– |
/* Generate non-diff sample dir */ |
1604 |
– |
VCOPY(rayOut.rdir, nd.vray); |
1605 |
– |
err = (*sdc -> func -> sampCDist) |
1606 |
– |
(rayOut.rdir, pmapRandom(scatterState), cdarr [i]); |
1607 |
– |
if (err) |
1608 |
– |
objerror(mat, USER, transSDError(SDEinternal)); |
1609 |
– |
|
1610 |
– |
/* Get colour */ |
1611 |
– |
j = (*sdc -> func -> getBSDFs)(coef, rayOut.rdir, nd.vray, sdc); |
1612 |
– |
|
1613 |
– |
if (j <= 0) { |
1614 |
– |
sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error", |
1615 |
– |
nd.sd -> name); |
1616 |
– |
objerror(mat, USER, transSDError(SDEinternal)); |
1617 |
– |
} |
1618 |
– |
|
1619 |
– |
bsdfVal.spec = sdc -> cspec [0]; |
1620 |
– |
rhoDiff = coef [0]; |
1621 |
– |
|
1622 |
– |
while (--j) { |
1623 |
– |
c_cmix(&bsdfVal.spec, rhoDiff, &bsdfVal.spec, coef [j], |
1624 |
– |
&sdc -> cspec [j]); |
1625 |
– |
rhoDiff += coef [j]; |
1626 |
– |
} |
1627 |
– |
|
1628 |
– |
/* ? */ |
1629 |
– |
c_ccvt(&bsdfVal.spec, C_CSXY + C_CSSPEC); |
1630 |
– |
ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB); |
1631 |
– |
|
1632 |
– |
/* Xform outgoing dir to world coords */ |
1633 |
– |
if ((err = SDmapDir(rayOut.rdir, nd.fromloc, rayOut.rdir))) { |
1634 |
– |
objerror(mat, USER, transSDError(err)); |
1635 |
– |
return 0; |
1636 |
– |
} |
1637 |
– |
|
1638 |
– |
photonRay(rayIn, &rayOut, rayOutType, bsdfRGB); |
1639 |
– |
} |
1640 |
– |
|
1641 |
– |
if (cdarr) |
1642 |
– |
free(cdarr); |
1643 |
– |
} |
1644 |
– |
|
1645 |
– |
/* Clean up BSDF */ |
1646 |
– |
SDfreeCache(nd.sd); |
1647 |
– |
|
1648 |
– |
tracePhoton(&rayOut); |
1649 |
– |
return 0; |
1650 |
– |
} |
1651 |
– |
#else |
1652 |
– |
|
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) |
1415 |
|
nd.thick = evalue(mf -> ep [0]); |
1416 |
|
if ((-FTINY <= nd.thick) & (nd.thick <= FTINY)) |
1417 |
|
nd.thick = .0; |
1418 |
< |
#if 0 |
1691 |
< |
if (nd.thick != .0 || (!hitFront && !backvis)) { |
1692 |
< |
/* Proxy geometry present, so use it instead and transfer ray */ |
1693 |
< |
photonRay(rayIn, &rayOut, PMAP_XFER, NULL); |
1694 |
< |
tracePhoton(&rayOut); |
1695 |
< |
|
1696 |
< |
return 0; |
1697 |
< |
} |
1698 |
< |
#endif |
1418 |
> |
|
1419 |
|
/* Get BSDF data */ |
1420 |
|
nd.sd = loadBSDF(mat -> oargs.sarg [1]); |
1421 |
|
|
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); |
1578 |
> |
nd.thick = -nd.thick; |
1579 |
> |
photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, bsdfRGB); |
1580 |
|
transmitted = 1; |
1581 |
|
} |
1582 |
|
|
1600 |
|
ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB); |
1601 |
|
multcolor(bsdfRGB, rayIn -> pcol); |
1602 |
|
flipsurface(rayIn); /* Necessary? */ |
1603 |
< |
nd.thick = -nd.thick; |
1603 |
> |
nd.thick = -nd.thick; |
1604 |
|
photonRay(rayIn, &rayOut, PMAP_SPECTRANS, bsdfRGB); |
1605 |
|
transmitted = 1; |
1606 |
|
} |
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); |
1620 |
> |
VSUM(rayOut.rorg, rayOut.rorg, rayIn -> ron, -nd.thick); |
1621 |
|
|
1622 |
|
tracePhoton(&rayOut); |
1623 |
|
return 0; |
1624 |
|
} |
1905 |
– |
#endif |
1625 |
|
|
1626 |
|
|
1627 |
|
|