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.2 by rschregle, Wed Apr 22 15:50:44 2015 UTC vs.
Revision 2.16 by rschregle, Mon Jun 11 12:46:51 2018 UTC

# Line 1 | Line 1
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  
# Line 103 | Line 106 | void photonRay (const RAY *rayIn, RAY *rayOut,
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 normalise 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  
# Line 129 | Line 137 | static void addPhotons (const RAY *r)
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  
# Line 198 | Line 206 | static int isoSpecPhotonScatter (NORMDAT *nd, RAY *ray
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) {
# Line 267 | Line 270 | static void diffPhotonScatter (FVECT normal, RAY* rayO
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 */
# Line 323 | Line 321 | static int normalPhotonScatter (OBJREC *mat, RAY *rayI
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);
# Line 752 | Line 750 | static int dielectricPhotonScatter (OBJREC *mat, RAY *
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 {
# Line 899 | Line 897 | static int glassPhotonScatter (OBJREC *mat, RAY *rayIn
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);
# Line 985 | Line 983 | static int aliasPhotonScatter (OBJREC *mat, RAY *rayIn
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     }
# Line 1257 | Line 1258 | static int mx_dataPhotonScatter (OBJREC *mat, RAY *ray
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;
# Line 1310 | Line 1322 | static int mx_pdataPhotonScatter (OBJREC *mat, RAY *ra
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;
# Line 1350 | Line 1373 | static int mx_funcPhotonScatter (OBJREC *mat, RAY *ray
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;
# Line 1383 | Line 1417 | static int pattexPhotonScatter (OBJREC *mat, RAY *rayI
1417  
1418  
1419  
1420 < #if 0
1421 <   static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1422 <   /* Generate new photon ray for BSDF modifier and recurse. */
1423 <   {
1424 <      int      hitFront;
1425 <      SDError  err;
1426 <      FVECT        upvec;
1393 <      MFUNC        *mf;
1394 <      BSDFDAT   nd;
1395 <      RAY      rayOut;
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  
1397      /* Following code adapted from m_bsdf() */
1398      /* Check arguments */
1399      if (mat -> oargs.nsargs < 6 || mat -> oargs.nfargs > 9 ||
1400          mat -> oargs.nfargs % 3)
1401         objerror(mat, USER, "bad # arguments");
1402        
1403      hitFront = (rayIn -> rod > 0);
1404
1405      /* Load cal file */
1406      mf = getfunc(mat, 5, 0x1d, 1);
1407      
1408      /* Get thickness */
1409      nd.thick = evalue(mf -> ep [0]);
1410      if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
1411         nd.thick = .0;
1412        
1413      if (nd.thick != .0 || (!hitFront && !backvis)) {
1414         /* Proxy geometry present, so use it instead and transfer ray */
1415         photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1416         tracePhoton(&rayOut);
1417        
1418         return 0;
1419      }
1420
1421      /* Get BSDF data */
1422      nd.sd = loadBSDF(mat -> oargs.sarg [1]);
1423      
1424      /* Diffuse reflectance */
1425      if (hitFront) {
1426         if (mat -> oargs.nfargs < 3)
1427            setcolor(nd.rdiff, .0, .0, .0);
1428         else setcolor(nd.rdiff, mat -> oargs.farg [0], mat -> oargs.farg [1],
1429                       mat -> oargs.farg [2]);
1430      }    
1431      else if (mat -> oargs.nfargs < 6) {
1432         /* Check for absorbing backside */
1433         if (!backvis && !nd.sd -> rb && !nd.sd -> tf) {
1434            SDfreeCache(nd.sd);                    
1435            return 0;
1436         }
1437        
1438         setcolor(nd.rdiff, .0, .0, .0);
1439      }
1440      else setcolor(nd.rdiff, mat -> oargs.farg [3], mat -> oargs.farg [4],
1441                    mat -> oargs.farg [5]);
1442
1443      /* Diffuse transmittance */
1444      if (mat -> oargs.nfargs < 9)
1445         setcolor(nd.tdiff, .0, .0, .0);
1446      else setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7],
1447                    mat -> oargs.farg [8]);
1448                  
1449      nd.mp = mat;
1450      nd.pr = rayIn;
1451      
1452      /* Get modifiers */
1453      raytexture(rayIn, mat -> omod);
1454      
1455      /* Modify diffuse values */
1456      multcolor(nd.rdiff, rayIn -> pcol);
1457      multcolor(nd.tdiff, rayIn -> pcol);
1458        
1459      /* Get up vector & xform to world coords */
1460      upvec [0] = evalue(mf -> ep [1]);
1461      upvec [1] = evalue(mf -> ep [2]);
1462      upvec [2] = evalue(mf -> ep [3]);
1463      
1464      if (mf -> fxp != &unitxf) {
1465         multv3(upvec, upvec, mf -> fxp -> xfm);
1466         nd.thick *= mf -> fxp -> sca;
1467      }
1468      
1469      if (rayIn -> rox) {
1470         multv3(upvec, upvec, rayIn -> rox -> f.xfm);
1471         nd.thick *= rayIn -> rox -> f.sca;
1472      }
1473      
1474      /* Perturb normal */
1475      raynormal(nd.pnorm, rayIn);
1476      
1477      /* Xform incident dir to local BSDF coords */
1478      err = SDcompXform(nd.toloc, nd.pnorm, upvec);
1479      
1480      if (!err) {
1481         nd.vray [0] = -rayIn -> rdir [0];
1482         nd.vray [1] = -rayIn -> rdir [1];
1483         nd.vray [2] = -rayIn -> rdir [2];
1484         err = SDmapDir(nd.vray, nd.toloc, nd.vray);
1485      }
1486      
1487      if (!err)
1488         err = SDinvXform(nd.fromloc, nd.toloc);
1489        
1490      if (err) {
1491         objerror(mat, WARNING, "Illegal orientation vector");
1492         return 0;
1493      }
1494      
1495      /* Determine BSDF resolution */
1496      err = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin + SDqueryMax, nd.sd);
1497      
1498      if (err)
1499         objerror(mat, USER, transSDError(err));
1500        
1501      nd.sr_vpsa [0] = sqrt(nd.sr_vpsa [0]);
1502      nd.sr_vpsa [1] = sqrt(nd.sr_vpsa [1]);
1503
1504      /* Orient perturbed normal towards incident side */
1505      if (!hitFront) {                  
1506         nd.pnorm [0] = -nd.pnorm [0];
1507         nd.pnorm [1] = -nd.pnorm [1];
1508         nd.pnorm [2] = -nd.pnorm [2];
1509      }
1510      
1511      /* Following code adapted from SDsampBSDF() */
1512      {
1513         SDSpectralDF   *rdf, *tdf;
1514         SDValue        bsdfVal;
1515         double         xi, rhoDiff = 0;
1516         float          coef [SDmaxCh];
1517         int            i, j, n, nr;
1518         SDComponent       *sdc;
1519         const SDCDst   **cdarr = NULL;
1520        
1521         /* Get diffuse albedo (?) */
1522         if (hitFront) {
1523            bsdfVal = nd.sd -> rLambFront;
1524            rdf = nd.sd -> rf;
1525            tdf = nd.sd -> tf ? nd.sd -> tf : nd.sd -> tb;
1526         }
1527         else {
1528            bsdfVal = nd.sd -> rLambBack;
1529            rdf = nd.sd -> rb;
1530            tdf = nd.sd -> tb ? nd.sd -> tb : nd.sd -> tf;
1531         }
1532        
1533         rhoDiff = bsdfVal.cieY;
1534         bsdfVal.cieY += nd.sd -> tLamb.cieY;
1535        
1536         /* Allocate non-diffuse sampling */
1537         i = nr = rdf ? rdf -> ncomp : 0;
1538         j = tdf ? tdf -> ncomp : 0;
1539         n = i + j;
1540        
1541         if (n > 0 && !(cdarr = (const SDCDst**)malloc(n * sizeof(SDCDst*))))
1542            objerror(mat, USER, transSDError(SDEmemory));
1543            
1544         while (j-- > 0) {
1545            /* Sum up non-diffuse transmittance */
1546            cdarr [i + j] = (*tdf -> comp [j].func -> getCDist)(nd.vray, &tdf -> comp [j]);
1547            
1548            if (!cdarr [i + j])
1549               cdarr [i + j] = &SDemptyCD;
1550            else bsdfVal.cieY += cdarr [i + j] -> cTotal;
1551         }
1552        
1553         while (i-- > 0) {
1554            /* Sum up non-diffuse reflectance */
1555            cdarr [i] = (*rdf -> comp [i].func -> getCDist)(nd.vray, &rdf -> comp [i]);
1556            
1557            if (!cdarr [i])
1558               cdarr [i] = &SDemptyCD;
1559            else bsdfVal.cieY += cdarr [i] -> cTotal;
1560         }
1561        
1562         if (bsdfVal.cieY <= FTINY) {
1563            /* Don't bother sampling, just absorb photon */
1564            if (cdarr)
1565               free(cdarr);
1566            return 0;
1567         }      
1568        
1569         /* Insert direct and indirect photon hits if diffuse component */
1570         if (rhoDiff > FTINY || nd.sd -> tLamb.cieY > FTINY)
1571            addPhotons(rayIn);  
1572            
1573         xi = pmapRandom(rouletteState);
1574        
1575         if ((xi -= rhoDiff) <= 0) {
1576            /* Diffuse reflection */
1577            photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.rdiff);
1578            diffPhotonScatter(nd.pnorm, &rayOut);
1579         }
1580         else if ((xi -= nd.sd -> tLamb.cieY) <= 0) {
1581            /* Diffuse transmission */
1582            flipsurface(rayIn);
1583            photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.tdiff);
1584            bsdfVal.spec = nd.sd -> tLamb.spec;
1585            diffPhotonScatter(nd.pnorm, &rayOut);
1586         }
1587         else {
1588            int rayOutType;
1589            COLOR bsdfRGB;
1590              
1591            /* Non-diffuse CDF inversion (?) */
1592            for (i = 0; i < n && (xi -= cdarr [i] -> cTotal) > 0; i++);
1593            
1594            if (i >= n) {
1595               /* Absorbed -- photon went Deer Hunter */
1596               if (cdarr)
1597                  free(cdarr);
1598               return 0;
1599            }
1600
1601            if (i < nr) {
1602               /* Non-diffuse reflection */
1603               sdc = &rdf -> comp [i];
1604               rayOutType = PMAP_SPECREFL;
1605            }
1606            else {
1607               /* Non-diffuse transmission */
1608               sdc = &tdf -> comp [i - nr];
1609               rayOutType = PMAP_SPECTRANS;
1610            }
1611            
1612            /* Generate non-diff sample dir */
1613            VCOPY(rayOut.rdir, nd.vray);
1614            err = (*sdc -> func -> sampCDist)
1615                  (rayOut.rdir, pmapRandom(scatterState), cdarr [i]);              
1616            if (err)
1617               objerror(mat, USER, transSDError(SDEinternal));
1618
1619            /* Get colour */
1620            j = (*sdc -> func -> getBSDFs)(coef, rayOut.rdir, nd.vray, sdc);
1621            
1622            if (j <= 0) {
1623               sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error",
1624                       nd.sd -> name);
1625               objerror(mat, USER, transSDError(SDEinternal));
1626            }
1627            
1628            bsdfVal.spec = sdc -> cspec [0];
1629            rhoDiff = coef [0];
1630            
1631            while (--j) {
1632               c_cmix(&bsdfVal.spec, rhoDiff, &bsdfVal.spec, coef [j],
1633                      &sdc -> cspec [j]);
1634               rhoDiff += coef [j];
1635            }
1636            
1637            /* ? */
1638            c_ccvt(&bsdfVal.spec, C_CSXY + C_CSSPEC);
1639            ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1640            
1641            /* Xform outgoing dir to world coords */
1642            if ((err = SDmapDir(rayOut.rdir, nd.fromloc, rayOut.rdir))) {
1643               objerror(mat, USER, transSDError(err));
1644               return 0;
1645            }
1646            
1647            photonRay(rayIn, &rayOut, rayOutType, bsdfRGB);
1648         }
1649        
1650         if (cdarr)
1651            free(cdarr);
1652      }
1653                          
1654      /* Clean up BSDF */
1655      SDfreeCache(nd.sd);
1656
1657      tracePhoton(&rayOut);
1658      return 0;
1659   }
1660 #else
1661
1428   static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1429   /* Generate new photon ray for BSDF modifier and recurse. */
1430   {
# Line 1670 | Line 1436 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1436     BSDFDAT      nd;
1437     RAY      rayOut;
1438     COLOR    bsdfRGB;
1439 +   int      transmitted;
1440     double   prDiff, ptDiff, prDiffSD, ptDiffSD, prSpecSD, ptSpecSD,
1441 <            albedo, xi, xi2;
1442 <   const double patAlb = colorAvg(rayIn -> pcol);
1441 >            albedo, xi;
1442 >   const double patAlb = bright(rayIn -> pcol);
1443    
1444     /* Following code adapted from m_bsdf() */
1445     /* Check arguments */
# Line 1680 | Line 1447 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1447         mat -> oargs.nfargs % 3)
1448        objerror(mat, USER, "bad # arguments");
1449        
1450 <        hitFront = (rayIn -> rod > 0);
1450 >   hitFront = (rayIn -> rod > 0);
1451  
1452 <        /* Load cal file */
1453 <        mf = getfunc(mat, 5, 0x1d, 1);
1687 <        
1688 <        /* Get thickness */
1689 <        nd.thick = evalue(mf -> ep [0]);
1690 <        if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
1691 <                nd.thick = .0;
1692 <                
1693 <   if (nd.thick != .0 || (!hitFront && !backvis)) {
1694 <      /* Proxy geometry present, so use it instead and transfer ray */
1695 <      photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1696 <      tracePhoton(&rayOut);
1697 <      
1698 <      return 0;
1699 <   }
1452 >   /* Load cal file */
1453 >   mf = getfunc(mat, 5, 0x1d, 1);
1454  
1455 +   /* Get thickness */
1456 +   nd.thick = evalue(mf -> ep [0]);
1457 +   if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
1458 +      nd.thick = .0;
1459 +
1460     /* Get BSDF data */
1461     nd.sd = loadBSDF(mat -> oargs.sarg [1]);
1462    
# Line 1720 | Line 1479 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1479     else setcolor(nd.rdiff, mat -> oargs.farg [3], mat -> oargs.farg [4],
1480                   mat -> oargs.farg [5]);
1481  
1482 <        /* Extra diffuse transmittance from material def */
1483 <        if (mat -> oargs.nfargs < 9)
1484 <           setcolor(nd.tdiff, .0, .0, .0);
1482 >   /* Extra diffuse transmittance from material def */
1483 >   if (mat -> oargs.nfargs < 9)
1484 >      setcolor(nd.tdiff, .0, .0, .0);
1485     else setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7],
1486                   mat -> oargs.farg [8]);
1487                
1488     nd.mp = mat;
1489     nd.pr = rayIn;
1490 <        
1490 >
1491     /* Get modifiers */
1492     raytexture(rayIn, mat -> omod);
1493    
1494     /* Modify diffuse values */
1495     multcolor(nd.rdiff, rayIn -> pcol);
1496     multcolor(nd.tdiff, rayIn -> pcol);
1497 <                
1497 >
1498     /* Get up vector & xform to world coords */
1499     upvec [0] = evalue(mf -> ep [1]);
1500     upvec [1] = evalue(mf -> ep [2]);
# Line 1782 | Line 1541 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1541     nd.sr_vpsa [1] = sqrt(nd.sr_vpsa [1]);
1542  
1543     /* Orient perturbed normal towards incident side */
1544 <   if (!hitFront) {                    
1544 >   if (!hitFront) {
1545        nd.pnorm [0] = -nd.pnorm [0];
1546        nd.pnorm [1] = -nd.pnorm [1];
1547        nd.pnorm [2] = -nd.pnorm [2];
# Line 1810 | Line 1569 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1569     if (prDiff + ptDiff + prDiffSD + ptDiffSD > FTINY)
1570        addPhotons(rayIn);        
1571  
1572 <   xi = xi2 = pmapRandom(rouletteState);
1572 >   xi = pmapRandom(rouletteState);
1573        
1574     if (xi > albedo)
1575        /* Absorbtion */
1576        return 0;
1577    
1578 +   transmitted = 0;
1579 +
1580     if ((xi -= prDiff) <= 0) {
1581        /* Diffuse reflection (extra component in material def) */
1582        photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.rdiff);
# Line 1824 | Line 1585 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1585    
1586     else if ((xi -= ptDiff) <= 0) {
1587        /* Diffuse transmission (extra component in material def) */
1827      flipsurface(rayIn);
1588        photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.tdiff);
1589 <      diffPhotonScatter(nd.pnorm, &rayOut);      
1589 >      diffPhotonScatter(nd.pnorm, &rayOut);
1590 >      transmitted = 1;
1591     }
1592 <  
1592 >  
1593     else {   /* Sample SDF */
1594        if ((xi -= prDiffSD) <= 0) {
1595           /* Diffuse SDF reflection (constant component) */
1596 <         if ((err = SDsampBSDF(&bsdfVal, nd.vray, xi2,
1596 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1597                                 SDsampDf | SDsampR, nd.sd)))
1598              objerror(mat, USER, transSDError(err));
1599          
# Line 1844 | Line 1605 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1605  
1606        else if ((xi -= ptDiffSD) <= 0) {
1607           /* Diffuse SDF transmission (constant component) */
1608 <         if ((err = SDsampBSDF(&bsdfVal, nd.vray, xi2,
1608 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1609                                 SDsampDf | SDsampT, nd.sd)))
1610              objerror(mat, USER, transSDError(err));
1611          
# Line 1852 | Line 1613 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1613           ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1614           multcolor(bsdfRGB, rayIn -> pcol);
1615           addcolor(bsdfRGB, nd.tdiff);      
1855         flipsurface(rayIn);  /* Necessary? */
1616           photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, bsdfRGB);
1617 +         transmitted = 1;
1618        }
1619  
1620        else if ((xi -= prSpecSD) <= 0) {
1621           /* Non-diffuse ("specular") SDF reflection */
1622 <         if ((err = SDsampBSDF(&bsdfVal, nd.vray, xi2,
1622 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1623                                 SDsampSp | SDsampR, nd.sd)))
1624              objerror(mat, USER, transSDError(err));
1625          
# Line 1868 | Line 1629 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1629        
1630        else {
1631           /* Non-diffuse ("specular") SDF transmission */
1632 <         if ((err = SDsampBSDF(&bsdfVal, nd.vray, xi2,
1632 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1633                                 SDsampSp | SDsampT, nd.sd)))
1634              objerror(mat, USER, transSDError(err));
1635  
1636           /* Apply pattern to spectral component */
1637           ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1638           multcolor(bsdfRGB, rayIn -> pcol);
1878         flipsurface(rayIn);  /* Necessary? */
1639           photonRay(rayIn, &rayOut, PMAP_SPECTRANS, bsdfRGB);
1640 +         transmitted = 1;
1641        }      
1642        
1643        /* Xform outgoing dir to world coords */
# Line 1889 | Line 1650 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1650     /* Clean up */
1651     SDfreeCache(nd.sd);
1652  
1653 +   /* Offset outgoing photon origin by thickness to bypass proxy geometry */
1654 +   if (transmitted && nd.thick != 0)
1655 +      VSUM(rayOut.rorg, rayOut.rorg, rayIn -> ron, -nd.thick);
1656 +
1657     tracePhoton(&rayOut);
1658     return 0;
1659   }
1895 #endif
1660  
1661  
1662  
1663   static int lightPhotonScatter (OBJREC* mat, RAY* ray)
1664 < /* Light sources doan' reflect */
1664 > /* Light sources doan' reflect, mang */
1665   {
1666     return 0;
1667   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines