ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/pmapmat.c
(Generate patch)

Comparing ray/src/rt/pmapmat.c (file contents):
Revision 2.1 by greg, Tue Feb 24 19:39:27 2015 UTC vs.
Revision 2.5 by greg, Thu May 21 05:54:54 2015 UTC

# Line 4 | Line 4
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$
# Line 113 | Line 114 | void photonRay (const RAY *rayIn, RAY *rayOut,
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     }
# Line 198 | Line 199 | static int isoSpecPhotonScatter (NORMDAT *nd, RAY *ray
199     int      niter, i = 0;
200    
201     /* Set up sample coordinates */  
202 <   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 <  
202 >   getperpendicular(u, nd -> pnorm, 1);  
203     fcross(v, nd -> pnorm, u);
204    
205     if (nd -> specfl & SP_REFL) {
# Line 267 | Line 263 | static void diffPhotonScatter (FVECT normal, RAY* rayO
263     int         i = 0;
264  
265     /* Set up sample coordinates */
266 <   do {
271 <      v [0] = v [1] = v [2] = 0;
272 <      v [i++] = 1;
273 <      fcross(u, v, normal);
274 <   } while (normalize(u) < FTINY);
275 <  
266 >   getperpendicular(u, normal, 1);
267     fcross(v, normal, u);
268    
269     /* Convert theta & phi to cartesian */
# Line 323 | Line 314 | static int normalPhotonScatter (OBJREC *mat, RAY *rayI
314        nd.specfl |= SP_FLAT;  
315        
316     /* Perturb normal */
317 <   if ((hastexture = DOT(rayIn -> pert, rayIn -> pert)) > sqr(FTINY))
317 >   if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY)) ))
318        nd.pdot = raynormal(nd.pnorm, rayIn);
319     else {
320        VCOPY(nd.pnorm, rayIn -> ron);
# Line 752 | Line 743 | static int dielectricPhotonScatter (OBJREC *mat, RAY *
743     /* get modifiers */
744     raytexture(rayIn, mat -> omod);                      
745    
746 <   if ((hastexture = DOT(rayIn -> pert, rayIn -> pert)) > FTINY * FTINY)
746 >   if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > FTINY * FTINY)))
747        /* Perturb normal */
748        cos1 = raynormal(dnorm, rayIn);
749     else {
# Line 899 | Line 890 | static int glassPhotonScatter (OBJREC *mat, RAY *rayIn
890     /* reorient if necessary */
891     if (rayIn -> rod < 0)
892        flipsurface(rayIn);
893 <   if ((hastexture = DOT(rayIn -> pert, rayIn -> pert)) > FTINY * FTINY)
893 >   if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > FTINY * FTINY) ))
894        pdot = raynormal(pnorm, rayIn);
895     else {
896        VCOPY(pnorm, rayIn -> ron);
# Line 1383 | Line 1374 | static int pattexPhotonScatter (OBJREC *mat, RAY *rayI
1374  
1375  
1376  
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 +
1653 + /*
1654 +   The following code is
1655 +   (c) Lucerne University of Applied Sciences and Arts,
1656 +   supported by the Swiss National Science Foundation (SNSF, #147053)
1657 + */  
1658 +
1659   static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1660   /* Generate new photon ray for BSDF modifier and recurse. */
1661   {
1662     int      hitFront;
1663     SDError  err;
1664 +   SDValue  bsdfVal;
1665     FVECT           upvec;
1666     MFUNC           *mf;
1667     BSDFDAT      nd;
1668     RAY      rayOut;
1669 <
1669 >   COLOR    bsdfRGB;
1670 >   double   prDiff, ptDiff, prDiffSD, ptDiffSD, prSpecSD, ptSpecSD,
1671 >            albedo, xi, xi2;
1672 >   const double patAlb = colorAvg(rayIn -> pcol);
1673 >  
1674     /* Following code adapted from m_bsdf() */
1675     /* Check arguments */
1676     if (mat -> oargs.nsargs < 6 || mat -> oargs.nfargs > 9 ||
# Line 1420 | Line 1698 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1698     /* Get BSDF data */
1699     nd.sd = loadBSDF(mat -> oargs.sarg [1]);
1700    
1701 <   /* Diffuse reflectance */
1701 >   /* Extra diffuse reflectance from material def */
1702     if (hitFront) {
1703        if (mat -> oargs.nfargs < 3)
1704           setcolor(nd.rdiff, .0, .0, .0);
# Line 1439 | Line 1717 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1717     else setcolor(nd.rdiff, mat -> oargs.farg [3], mat -> oargs.farg [4],
1718                   mat -> oargs.farg [5]);
1719  
1720 <        /* Diffuse transmittance */
1720 >        /* Extra diffuse transmittance from material def */
1721          if (mat -> oargs.nfargs < 9)
1722             setcolor(nd.tdiff, .0, .0, .0);
1723     else setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7],
# Line 1506 | Line 1784 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1784        nd.pnorm [1] = -nd.pnorm [1];
1785        nd.pnorm [2] = -nd.pnorm [2];
1786     }
1787 <  
1788 <   /* Following code adapted from SDsampBSDF() */
1789 <   {
1790 <      SDSpectralDF   *rdf, *tdf;
1791 <      SDValue        bsdfVal;
1792 <      double         xi, rhoDiff = 0;
1793 <      float          coef [SDmaxCh];
1794 <      int            i, j, n, nr;
1795 <      SDComponent          *sdc;
1796 <      const SDCDst   **cdarr = NULL;
1797 <      
1798 <      /* Get diffuse albedo (?) */
1799 <      if (hitFront) {
1800 <         bsdfVal = nd.sd -> rLambFront;
1801 <         rdf = nd.sd -> rf;
1802 <         tdf = nd.sd -> tf ? nd.sd -> tf : nd.sd -> tb;
1803 <      }
1804 <      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));
1787 >
1788 >   /* Get scatter probabilities (weighted by pattern except for spec refl)
1789 >    * prDiff, ptDiff:      extra diffuse component in material def
1790 >    * prDiffSD, ptDiffSD:  diffuse (constant) component in SDF
1791 >    * prSpecSD, ptSpecSD:  non-diffuse ("specular") component in SDF
1792 >    * albedo:              sum of above, inverse absorption probability */
1793 >   prDiff   = colorAvg(nd.rdiff);
1794 >   ptDiff   = colorAvg(nd.tdiff);
1795 >   prDiffSD = patAlb * SDdirectHemi(nd.vray, SDsampDf | SDsampR, nd.sd);
1796 >   ptDiffSD = patAlb * SDdirectHemi(nd.vray, SDsampDf | SDsampT, nd.sd);
1797 >   prSpecSD = SDdirectHemi(nd.vray, SDsampSp | SDsampR, nd.sd);
1798 >   ptSpecSD = patAlb * SDdirectHemi(nd.vray, SDsampSp | SDsampT, nd.sd);
1799 >   albedo   = prDiff + ptDiff + prDiffSD + ptDiffSD + prSpecSD + ptSpecSD;
1800 >
1801 >   /*    
1802 >   if (albedo > 1)
1803 >      objerror(mat, WARNING, "Invalid albedo");
1804 >   */
1805          
1806 <      while (j-- > 0) {
1807 <         /* Sum up non-diffuse transmittance */
1808 <         cdarr [i + j] = (*tdf -> comp [j].func -> getCDist)(nd.vray, &tdf -> comp [j]);
1809 <        
1810 <         if (!cdarr [i + j])
1548 <            cdarr [i + j] = &SDemptyCD;
1549 <         else bsdfVal.cieY += cdarr [i + j] -> cTotal;
1550 <      }
1806 >   /* Insert direct and indirect photon hits if diffuse component */
1807 >   if (prDiff + ptDiff + prDiffSD + ptDiffSD > FTINY)
1808 >      addPhotons(rayIn);        
1809 >
1810 >   xi = xi2 = pmapRandom(rouletteState);
1811        
1812 <      while (i-- > 0) {
1813 <         /* Sum up non-diffuse reflectance */
1814 <         cdarr [i] = (*rdf -> comp [i].func -> getCDist)(nd.vray, &rdf -> comp [i]);
1812 >   if (xi > albedo)
1813 >      /* Absorbtion */
1814 >      return 0;
1815 >  
1816 >   if ((xi -= prDiff) <= 0) {
1817 >      /* Diffuse reflection (extra component in material def) */
1818 >      photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.rdiff);
1819 >      diffPhotonScatter(nd.pnorm, &rayOut);
1820 >   }
1821 >  
1822 >   else if ((xi -= ptDiff) <= 0) {
1823 >      /* Diffuse transmission (extra component in material def) */
1824 >      flipsurface(rayIn);
1825 >      photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.tdiff);
1826 >      diffPhotonScatter(nd.pnorm, &rayOut);      
1827 >   }
1828 >  
1829 >   else {   /* Sample SDF */
1830 >      if ((xi -= prDiffSD) <= 0) {
1831 >         /* Diffuse SDF reflection (constant component) */
1832 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, xi2,
1833 >                               SDsampDf | SDsampR, nd.sd)))
1834 >            objerror(mat, USER, transSDError(err));
1835          
1836 <         if (!cdarr [i])
1837 <            cdarr [i] = &SDemptyCD;
1838 <         else bsdfVal.cieY += cdarr [i] -> cTotal;
1836 >         /* Apply pattern to spectral component */
1837 >         ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1838 >         multcolor(bsdfRGB, rayIn -> pcol);
1839 >         photonRay(rayIn, &rayOut, PMAP_DIFFREFL, bsdfRGB);
1840        }
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         }
1841  
1842 <         if (i < nr) {
1843 <            /* Non-diffuse reflection */
1844 <            sdc = &rdf -> comp [i];
1845 <            rayOutType = PMAP_SPECREFL;
1846 <         }
1605 <         else {
1606 <            /* Non-diffuse transmission */
1607 <            sdc = &tdf -> comp [i - nr];
1608 <            rayOutType = PMAP_SPECTRANS;
1609 <         }
1842 >      else if ((xi -= ptDiffSD) <= 0) {
1843 >         /* Diffuse SDF transmission (constant component) */
1844 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, xi2,
1845 >                               SDsampDf | SDsampT, nd.sd)))
1846 >            objerror(mat, USER, transSDError(err));
1847          
1848 <         /* 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);
1848 >         /* Apply pattern to spectral component */
1849           ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1850 <        
1851 <         /* Xform outgoing dir to world coords */
1852 <         if ((err = SDmapDir(rayOut.rdir, nd.fromloc, rayOut.rdir))) {
1850 >         multcolor(bsdfRGB, rayIn -> pcol);
1851 >         addcolor(bsdfRGB, nd.tdiff);      
1852 >         flipsurface(rayIn);  /* Necessary? */
1853 >         photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, bsdfRGB);
1854 >      }
1855 >
1856 >      else if ((xi -= prSpecSD) <= 0) {
1857 >         /* Non-diffuse ("specular") SDF reflection */
1858 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, xi2,
1859 >                               SDsampSp | SDsampR, nd.sd)))
1860              objerror(mat, USER, transSDError(err));
1643            return 0;
1644         }
1861          
1862 <         photonRay(rayIn, &rayOut, rayOutType, bsdfRGB);
1862 >         ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1863 >         photonRay(rayIn, &rayOut, PMAP_SPECREFL, bsdfRGB);
1864        }
1865        
1866 <      if (cdarr)
1867 <         free(cdarr);
1866 >      else {
1867 >         /* Non-diffuse ("specular") SDF transmission */
1868 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, xi2,
1869 >                               SDsampSp | SDsampT, nd.sd)))
1870 >            objerror(mat, USER, transSDError(err));
1871 >
1872 >         /* Apply pattern to spectral component */
1873 >         ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1874 >         multcolor(bsdfRGB, rayIn -> pcol);
1875 >         flipsurface(rayIn);  /* Necessary? */
1876 >         photonRay(rayIn, &rayOut, PMAP_SPECTRANS, bsdfRGB);
1877 >      }      
1878 >      
1879 >      /* Xform outgoing dir to world coords */
1880 >      if ((err = SDmapDir(rayOut.rdir, nd.fromloc, nd.vray))) {
1881 >         objerror(mat, USER, transSDError(err));
1882 >         return 0;
1883 >      }
1884     }
1885 <                        
1886 <   /* Clean up BSDF */
1885 >      
1886 >   /* Clean up */
1887     SDfreeCache(nd.sd);
1888  
1889     tracePhoton(&rayOut);
1890     return 0;
1891   }
1892 + #endif
1893  
1894  
1895  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines