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.19 by rschregle, Tue Dec 4 21:58:46 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 31 | Line 34
34   #define  SP_FLAT     010
35   #define  SP_BADU     040
36   #define  MLAMBDA     500
37 < #define  RINDEX    1.52
37 > #define  RINDEX      1.52
38   #define  FRESNE(ci)  (exp(-5.85*(ci)) - 0.00287989916)
39  
40  
# Line 56 | Line 59 | typedef struct {
59  
60   typedef struct {
61     OBJREC   *mp;
62 <   RAY      *pr;                
63 <   FVECT    pnorm;              
64 <   FVECT    vray;              
65 <   double   sr_vpsa [2];        
66 <   RREAL        toloc [3][3];  
67 <   RREAL        fromloc [3][3];
68 <   double   thick;              
62 >   RAY      *pr;
63 >   DATARRAY *dp;
64 >   COLOR    mcolor;
65 >   COLOR    rdiff;
66 >   COLOR    tdiff;
67 >   double   rspec;
68 >   double   trans;
69 >   double   tspec;
70 >   FVECT    pnorm;
71 >   double   pdot;
72 > }  BRDFDAT;
73 >
74 > typedef struct {
75 >   OBJREC   *mp;
76 >   RAY      *pr;
77 >   FVECT    pnorm;
78 >   FVECT    vray;
79 >   double   sr_vpsa [2];
80 >   RREAL    toloc [3][3];
81 >   RREAL    fromloc [3][3];
82 >   double   thick;
83     SDData   *sd;
84     COLOR           runsamp;
85     COLOR           rdiff;
# Line 103 | Line 120 | void photonRay (const RAY *rayIn, RAY *rayOut,
120   {
121     rayorigin(rayOut, rayOutType, rayIn, NULL);
122    
123 <   /* Transfer flux */
124 <   copycolor(rayOut -> rcol, rayIn -> rcol);
125 <  
126 <   /* Copy caustic flag & direction for transferred rays */
127 <   if (rayOutType == PMAP_XFER) {
128 <      /* rayOut -> rtype |= rayIn -> rtype & SPECULAR; */
129 <      rayOut -> rtype |= rayIn -> rtype;
130 <      VCOPY(rayOut -> rdir, rayIn -> rdir);
131 <   }
132 <   else if (fluxAtten) {
133 <      /* Attenuate and normalised flux for scattered rays */
134 <      multcolor(rayOut -> rcol, fluxAtten);
135 <      colorNorm(rayOut -> rcol);
136 <   }
123 >   if (rayIn) {
124 >      /* Transfer flux */
125 >      copycolor(rayOut -> rcol, rayIn -> rcol);
126 >      
127 >      /* Copy caustic flag & direction for transferred rays */
128 >      if (rayOutType == PMAP_XFER) {
129 >         /* rayOut -> rtype |= rayIn -> rtype & SPECULAR; */
130 >         rayOut -> rtype |= rayIn -> rtype;
131 >         VCOPY(rayOut -> rdir, rayIn -> rdir);
132 >      }
133 >      else if (fluxAtten) {
134 >         /* Attenuate and normalise flux for scattered rays */
135 >         multcolor(rayOut -> rcol, fluxAtten);
136 >         colorNorm(rayOut -> rcol);
137 >      }
138  
139 <   /* Propagate index of emitting light source */
140 <   rayOut -> rsrc = rayIn -> rsrc;
139 >      /* Propagate index of emitting light source */
140 >      rayOut -> rsrc = rayIn -> rsrc;
141 >      
142 >      /* Update maximum photon path distance */
143 >      rayOut -> rmax = rayIn -> rmax - rayIn -> rot;
144 >   }
145   }
146  
147  
# Line 129 | Line 151 | static void addPhotons (const RAY *r)
151   {
152     if (!r -> rlvl)
153        /* Add direct photon map at primary hitpoint */
154 <      addPhoton(directPmap, r);
154 >      newPhoton(directPmap, r);
155     else {
156        /* Add global or precomputed photon map at indirect hitpoint */
157 <      addPhoton(preCompPmap ? preCompPmap : globalPmap, r);
157 >      newPhoton(preCompPmap ? preCompPmap : globalPmap, r);
158  
159        /* Store caustic photon if specular flag set */
160        if (PMAP_CAUSTICRAY(r))
161 <         addPhoton(causticPmap, r);
161 >         newPhoton(causticPmap, r);
162          
163        /* Store in contribution photon map */
164 <      addPhoton(contribPmap, r);
164 >      newPhoton(contribPmap, r);
165     }
166   }
167  
# Line 198 | Line 220 | static int isoSpecPhotonScatter (NORMDAT *nd, RAY *ray
220     int      niter, i = 0;
221    
222     /* Set up sample coordinates */  
223 <   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 <  
223 >   getperpendicular(u, nd -> pnorm, 1);
224     fcross(v, nd -> pnorm, u);
225    
226     if (nd -> specfl & SP_REFL) {
# Line 237 | Line 254 | static int isoSpecPhotonScatter (NORMDAT *nd, RAY *ray
254           cosp = cos(d);
255           sinp = sin(d);
256           d2 = pmapRandom(scatterState);
257 <         d = d2 <= FTINY ? 1 : sqrt(-log(d2) * nd -> alpha2);        
257 >         d = d2 <= FTINY ? 1 : sqrt(-log(d2) * nd -> alpha2);
258          
259           for (i = 0; i < 3; i++)
260              rayOut -> rdir [i] = nd -> prdir [i] +
# Line 258 | Line 275 | static int isoSpecPhotonScatter (NORMDAT *nd, RAY *ray
275   static void diffPhotonScatter (FVECT normal, RAY* rayOut)
276   /* Generate cosine-weighted direction for diffuse ray */
277   {
278 <   const RREAL cosThetaSqr = pmapRandom(scatterState),
278 >   const RREAL cosThetaSqr = pmapRandom(scatterState),
279                 cosTheta = sqrt(cosThetaSqr),
280 <               sinTheta = sqrt(1 - cosThetaSqr),
281 <               phi = 2 * PI * pmapRandom(scatterState),
280 >               sinTheta = sqrt(1 - cosThetaSqr),
281 >               phi = 2 * PI * pmapRandom(scatterState),
282                 du = cos(phi) * sinTheta, dv = sin(phi) * sinTheta;
283     FVECT       u, v;
284     int         i = 0;
285  
286     /* Set up sample coordinates */
287 <   do {
271 <      v [0] = v [1] = v [2] = 0;
272 <      v [i++] = 1;
273 <      fcross(u, v, normal);
274 <   } while (normalize(u) < FTINY);
275 <  
287 >   getperpendicular(u, normal, 1);
288     fcross(v, normal, u);
289    
290     /* Convert theta & phi to cartesian */
# Line 323 | Line 335 | static int normalPhotonScatter (OBJREC *mat, RAY *rayI
335        nd.specfl |= SP_FLAT;  
336        
337     /* Perturb normal */
338 <   if ((hastexture = DOT(rayIn -> pert, rayIn -> pert)) > sqr(FTINY))
338 >   if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY)) ))
339        nd.pdot = raynormal(nd.pnorm, rayIn);
340     else {
341        VCOPY(nd.pnorm, rayIn -> ron);
# Line 423 | Line 435 | static int normalPhotonScatter (OBJREC *mat, RAY *rayI
435        
436        if (hastexture) {
437           /* Perturb */
438 <         for (i = 0; i < 3; i++)
438 >         for (i = 0; i < 3; i++)
439              nd.prdir [i] = rayIn -> rdir [i] - rayIn -> pert [i];
440              
441 <         if (DOT(nd.prdir, rayIn -> ron) < -FTINY)
441 >         if (DOT(nd.prdir, rayIn -> ron) < -FTINY)
442              normalize(nd.prdir);
443           else VCOPY(nd.prdir, rayIn -> rdir);
444        }
445        else VCOPY(nd.prdir, rayIn -> rdir);
446        
447        if ((nd.specfl & (SP_TRAN | SP_PURE)) == (SP_TRAN | SP_PURE))
448 <         /* Perfect specular transmission */  
448 >         /* Perfect specular transmission */
449           VCOPY(rayOut.rdir, nd.prdir);
450 <      else if (!isoSpecPhotonScatter(&nd, &rayOut))
450 >      else if (!isoSpecPhotonScatter(&nd, &rayOut))
451           return 0;
452          
453 <      photonRay(rayIn, &rayOut, PMAP_SPECTRANS, nd.mcolor);  
453 >      photonRay(rayIn, &rayOut, PMAP_SPECTRANS, nd.mcolor);
454     }
455    
456     else if (xi > (albedo -= prdiff)) {
# Line 471 | Line 483 | static int normalPhotonScatter (OBJREC *mat, RAY *rayI
483   static void getacoords (ANISODAT *np)
484   /* Set up coordinate system for anisotropic sampling; cloned from aniso.c */
485   {
486 <        MFUNC  *mf;
487 <        int  i;
486 >   MFUNC  *mf;
487 >   int  i;
488  
489 <        mf = getfunc(np->mp, 3, 0x7, 1);
490 <        setfunc(np->mp, np->rp);
491 <        errno = 0;
492 <        
493 <        for (i = 0; i < 3; i++)
494 <           np->u[i] = evalue(mf->ep[i]);
495 <          
489 >   mf = getfunc(np->mp, 3, 0x7, 1);
490 >   setfunc(np->mp, np->rp);
491 >   errno = 0;
492 >
493 >   for (i = 0; i < 3; i++)
494 >      np->u[i] = evalue(mf->ep[i]);
495 >  
496     if ((errno == EDOM) | (errno == ERANGE)) {
497        objerror(np->mp, WARNING, "compute error");
498        np->specfl |= SP_BADU;
# Line 490 | Line 502 | static void getacoords (ANISODAT *np)
502     if (mf->fxp != &unitxf)
503        multv3(np->u, np->u, mf->fxp->xfm);
504        
505 <        fcross(np->v, np->pnorm, np->u);
506 <        
507 <        if (normalize(np->v) == 0.0) {
508 <           objerror(np->mp, WARNING, "illegal orientation vector");
509 <           np->specfl |= SP_BADU;
510 <           return;
505 >   fcross(np->v, np->pnorm, np->u);
506 >
507 >   if (normalize(np->v) == 0.0) {
508 >      objerror(np->mp, WARNING, "illegal orientation vector");
509 >      np->specfl |= SP_BADU;
510 >      return;
511     }
512    
513     fcross(np->u, np->v, np->pnorm);
# Line 521 | Line 533 | static int anisoSpecPhotonScatter (ANISODAT *nd, RAY *
533     if (rayOut -> rtype & TRANS) {
534        /* Specular transmission */
535  
536 <      if (DOT(rayIn -> pert, rayIn -> pert) <= FTINY * FTINY)
536 >      if (DOT(rayIn -> pert, rayIn -> pert) <= sqr(FTINY))
537           VCOPY(nd -> prdir, rayIn -> rdir);
538        else {
539           /* perturb */
# Line 557 | Line 569 | static int anisoSpecPhotonScatter (ANISODAT *nd, RAY *
569           }
570        }
571        
572 <      return 0;  
572 >      return 0;
573     }
574    
575     else {
# Line 575 | Line 587 | static int anisoSpecPhotonScatter (ANISODAT *nd, RAY *
587           d = d2 <= FTINY ? 1
588                           : sqrt(-log(d2) /
589                                  (sqr(cosp) / sqr(nd -> u_alpha) +
590 <                                 sqr(sinp) / (nd -> v_alpha * nd -> v_alpha)));
590 >                                 sqr(sinp) / (nd->v_alpha * nd->v_alpha)));
591                                  
592           for (i = 0; i < 3; i++)
593              h [i] = nd -> pnorm [i] +
# Line 752 | Line 764 | static int dielectricPhotonScatter (OBJREC *mat, RAY *
764     /* get modifiers */
765     raytexture(rayIn, mat -> omod);                      
766    
767 <   if ((hastexture = DOT(rayIn -> pert, rayIn -> pert)) > FTINY * FTINY)
767 >   if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY))))
768        /* Perturb normal */
769        cos1 = raynormal(dnorm, rayIn);
770     else {
# Line 762 | Line 774 | static int dielectricPhotonScatter (OBJREC *mat, RAY *
774    
775     /* index of refraction */
776     nratio = mat -> otype ==
777 <      MAT_DIELECTRIC ? mat -> oargs.farg [3] + mat -> oargs.farg [4] / MLAMBDA
778 <                     : mat -> oargs.farg [3] / mat -> oargs.farg [7];
777 >      MAT_DIELECTRIC ? mat->oargs.farg[3] + mat->oargs.farg[4] / MLAMBDA
778 >                     : mat->oargs.farg[3] / mat->oargs.farg[7];
779                      
780     if (cos1 < 0) {
781        /* inside */
# Line 840 | Line 852 | static int dielectricPhotonScatter (OBJREC *mat, RAY *
852        for (i = 0; i < 3; i++)
853           rayOut.rdir [i] = nratio * rayIn -> rdir [i] + d1 * dnorm [i];
854          
855 <      if (hastexture && DOT(rayOut.rdir, rayIn -> ron) * hastexture >= -FTINY) {
855 >      if (hastexture && DOT(rayOut.rdir, rayIn->ron)*hastexture >= -FTINY) {
856           d1 *= hastexture;
857          
858           for (i = 0; i < 3; i++)
# Line 859 | Line 871 | static int dielectricPhotonScatter (OBJREC *mat, RAY *
871        photonRay(rayIn, &rayOut, PMAP_SPECREFL, NULL);
872        VSUM(rayOut.rdir, rayIn -> rdir, dnorm, 2 * cos1);
873        
874 <      if (hastexture && DOT(rayOut.rdir, rayIn -> ron) * hastexture <= FTINY)
874 >      if (hastexture && DOT(rayOut.rdir, rayIn->ron) * hastexture <= FTINY)
875           for (i = 0; i < 3; i++)
876              rayOut.rdir [i] = rayIn -> rdir [i] +
877                                2 * rayIn -> rod * rayIn -> ron [i];
# Line 899 | Line 911 | static int glassPhotonScatter (OBJREC *mat, RAY *rayIn
911     /* reorient if necessary */
912     if (rayIn -> rod < 0)
913        flipsurface(rayIn);
914 <   if ((hastexture = DOT(rayIn -> pert, rayIn -> pert)) > FTINY * FTINY)
914 >   if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY))))
915        pdot = raynormal(pnorm, rayIn);
916     else {
917        VCOPY(pnorm, rayIn -> ron);
# Line 981 | Line 993 | static int aliasPhotonScatter (OBJREC *mat, RAY *rayIn
993   /* Transfer photon scattering to alias target */
994   {
995     OBJECT   aliasObj;
996 <   OBJREC   aliasRec;
996 >   OBJREC   aliasRec, *aliasPtr;
997    
998     /* Straight replacement? */
999     if (!mat -> oargs.nsargs) {
1000 <      mat = objptr(mat -> omod);
1001 <      photonScatter [mat -> otype] (mat, rayIn);
1000 >      /* Skip void modifier! */
1001 >      if (mat -> omod != OVOID) {  
1002 >         mat = objptr(mat -> omod);
1003 >         photonScatter [mat -> otype] (mat, rayIn);
1004 >      }
1005        
1006        return 0;
1007     }
# Line 995 | Line 1010 | static int aliasPhotonScatter (OBJREC *mat, RAY *rayIn
1010     if (mat -> oargs.nsargs != 1)
1011        objerror(mat, INTERNAL, "bad # string arguments");
1012        
1013 <   aliasObj = lastmod(objndx(mat), mat -> oargs.sarg [0]);
1014 <      
1000 <   if (aliasObj < 0)
1001 <      objerror(mat, USER, "bad reference");
1002 <      
1003 <   memcpy(&aliasRec, objptr(aliasObj), sizeof(OBJREC));
1013 >   aliasPtr = mat;
1014 >   aliasObj = objndx(aliasPtr);
1015    
1016 +   /* Follow alias trail */
1017 +   do {
1018 +      aliasObj = aliasPtr -> oargs.nsargs == 1
1019 +                     ? lastmod(aliasObj, aliasPtr -> oargs.sarg [0])
1020 +                     : aliasPtr -> omod;
1021 +      if (aliasObj < 0)
1022 +         objerror(aliasPtr, USER, "bad reference");
1023 +        
1024 +      aliasPtr = objptr(aliasObj);
1025 +   } while (aliasPtr -> otype == MOD_ALIAS);
1026 +
1027 +   /* Copy alias object */
1028 +   aliasRec = *aliasPtr;
1029 +  
1030     /* Substitute modifier */
1031     aliasRec.omod = mat -> omod;
1032    
1033     /* Replacement scattering routine */
1034     photonScatter [aliasRec.otype] (&aliasRec, rayIn);
1035 +
1036 + #if 0
1037 +   /* Avoid potential memory leak? */
1038 +   if (aliasRec.os != aliasPtr -> os) {
1039 +      if (aliasObj -> os)
1040 +         free_os(aliasObj);
1041 +      aliasPtr -> os = aliasRec.os;
1042 +   }
1043 + #endif
1044 +
1045     return 0;
1046   }
1047  
# Line 1035 | Line 1070 | static int clipPhotonScatter (OBJREC *mat, RAY *rayIn)
1070              continue;
1071              
1072           if ((mod = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) {
1073 <            sprintf(errmsg, "unknown modifier \"%s\"", mat -> oargs.sarg [i]);
1073 >            sprintf(errmsg, "unknown modifier \"%s\"", mat->oargs.sarg[i]);
1074              objerror(mat, WARNING, errmsg);
1075              continue;
1076           }
# Line 1231 | Line 1266 | static int mx_dataPhotonScatter (OBJREC *mat, RAY *ray
1266        if (!strcmp(mat -> oargs.sarg [i], VOIDID))
1267           mod [i] = OVOID;
1268        else if ((mod [i] = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) {
1269 <         sprintf(errmsg, "undefined modifier \"%s\"", mat -> oargs.sarg [i]);
1269 >         sprintf(errmsg, "undefined modifier \"%s\"", mat->oargs.sarg[i]);
1270           objerror(mat, USER, errmsg);
1271        }
1272        
# Line 1257 | Line 1292 | static int mx_dataPhotonScatter (OBJREC *mat, RAY *ray
1292     if (errno)
1293        objerror(mat, WARNING, "compute error");
1294     else {
1295 <      mat = objptr(mod [pmapRandom(rouletteState) < coef ? 0 : 1]);
1296 <      photonScatter [mat -> otype] (mat, rayIn);
1295 >      OBJECT mxMod = mod [pmapRandom(rouletteState) < coef ? 0 : 1];
1296 >      
1297 >      if (mxMod != OVOID) {
1298 >         mat = objptr(mxMod);
1299 >         photonScatter [mat -> otype] (mat, rayIn);
1300 >      }
1301 >      else {
1302 >         /* Transfer ray if no modifier */
1303 >         RAY rayOut;
1304 >        
1305 >         photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1306 >         tracePhoton(&rayOut);      
1307 >      }            
1308     }
1309    
1310     return 0;
# Line 1285 | Line 1331 | static int mx_pdataPhotonScatter (OBJREC *mat, RAY *ra
1331        if (!strcmp(mat -> oargs.sarg [i], VOIDID))
1332           mod [i] = OVOID;
1333        else if ((mod [i] = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) {
1334 <         sprintf(errmsg, "undefined modifier \"%s\"", mat -> oargs.sarg [i]);
1334 >         sprintf(errmsg, "undefined modifier \"%s\"", mat->oargs.sarg[i]);
1335           objerror(mat, USER, errmsg);
1336        }
1337        
# Line 1310 | Line 1356 | static int mx_pdataPhotonScatter (OBJREC *mat, RAY *ra
1356     if (errno)
1357        objerror(mat, WARNING, "compute error");
1358     else {
1359 <      mat = objptr(mod [pmapRandom(rouletteState) < coef ? 0 : 1]);
1360 <      photonScatter [mat -> otype] (mat, rayIn);
1359 >      OBJECT mxMod = mod [pmapRandom(rouletteState) < coef ? 0 : 1];
1360 >      
1361 >      if (mxMod != OVOID) {
1362 >         mat = objptr(mxMod);
1363 >         photonScatter [mat -> otype] (mat, rayIn);
1364 >      }
1365 >      else {
1366 >         /* Transfer ray if no modifier */
1367 >         RAY rayOut;
1368 >        
1369 >         photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1370 >         tracePhoton(&rayOut);      
1371 >      }      
1372     }  
1373    
1374     return 0;
# Line 1336 | Line 1393 | static int mx_funcPhotonScatter (OBJREC *mat, RAY *ray
1393        if (!strcmp(mat -> oargs.sarg [i], VOIDID))
1394           mod [i] = OVOID;
1395        else if ((mod [i] = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) {
1396 <         sprintf(errmsg, "undefined modifier \"%s\"", mat -> oargs.sarg [i]);
1396 >         sprintf(errmsg, "undefined modifier \"%s\"", mat->oargs.sarg[i]);
1397           objerror(mat, USER, errmsg);
1398        }
1399        
# Line 1350 | Line 1407 | static int mx_funcPhotonScatter (OBJREC *mat, RAY *ray
1407     if (errno)
1408        objerror(mat, WARNING, "compute error");
1409     else {        
1410 <      mat = objptr(mod [pmapRandom(rouletteState) < coef ? 0 : 1]);
1411 <      photonScatter [mat -> otype] (mat, rayIn);
1410 >      OBJECT mxMod = mod [pmapRandom(rouletteState) < coef ? 0 : 1];
1411 >      
1412 >      if (mxMod != OVOID) {
1413 >         mat = objptr(mxMod);
1414 >         photonScatter [mat -> otype] (mat, rayIn);
1415 >      }
1416 >      else {
1417 >         /* Transfer ray if no modifier */
1418 >         RAY rayOut;
1419 >        
1420 >         photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1421 >         tracePhoton(&rayOut);      
1422 >      }
1423     }
1424    
1425     return 0;
# Line 1383 | Line 1451 | static int pattexPhotonScatter (OBJREC *mat, RAY *rayI
1451  
1452  
1453  
1454 + static int setbrdfunc(BRDFDAT *bd)
1455 + /* Set up brdf function and variables; ripped off from m_brdf.c */
1456 + {
1457 +   FVECT v;
1458 +  
1459 +   if (setfunc(bd -> mp, bd -> pr) == 0)
1460 +      return 0;
1461 +
1462 +   /* (Re)Assign func variables */
1463 +   multv3(v, bd -> pnorm, funcxf.xfm);
1464 +   varset("NxP", '=', v [0] / funcxf.sca);
1465 +   varset("NyP", '=', v [1] / funcxf.sca);
1466 +   varset("NzP", '=', v [2] / funcxf.sca);
1467 +   varset("RdotP", '=',
1468 +          bd -> pdot <= -1. ? -1. : bd -> pdot >= 1. ? 1. : bd -> pdot);
1469 +   varset("CrP", '=', colval(bd -> mcolor, RED));
1470 +   varset("CgP", '=', colval(bd -> mcolor, GRN));
1471 +   varset("CbP", '=', colval(bd -> mcolor, BLU));
1472 +  
1473 +   return 1;
1474 + }
1475 +
1476 +
1477 +
1478 + static int brtdFuncPhotonScatter (OBJREC *mat, RAY *rayIn)
1479 + /* Generate new photon ray for BRTDfunc material and recurse */
1480 + {
1481 +   int      hitfront = 1, hastexture, i;
1482 +   BRDFDAT  nd;
1483 +   RAY      rayOut;
1484 +   COLOR    rspecCol, tspecCol;
1485 +   double   prDiff, ptDiff, prSpec, ptSpec, albedo, xi;
1486 +   MFUNC    *mf;
1487 +   FVECT    bnorm;
1488 +
1489 +   if (mat -> oargs.nsargs < 10 || mat -> oargs.nfargs < 9)
1490 +      objerror(mat, USER, "bad # arguments");
1491 +   nd.mp = mat;
1492 +   nd.pr = rayIn;
1493 +   /* Dummies */
1494 +   nd.rspec = nd.tspec = 1.0;
1495 +   nd.trans = 0.5;
1496 +
1497 +   /* Diffuse reflectance */
1498 +   if (rayIn -> rod > 0.0)
1499 +      setcolor(nd.rdiff, mat -> oargs.farg[0], mat -> oargs.farg [1],
1500 +               mat -> oargs.farg [2]);
1501 +   else
1502 +      setcolor(nd.rdiff, mat-> oargs.farg [3], mat -> oargs.farg [4],
1503 +               mat -> oargs.farg [5]);
1504 +   /* Diffuse transmittance */
1505 +   setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7],
1506 +            mat -> oargs.farg [8]);
1507 +
1508 +   /* Get modifiers */
1509 +   raytexture(rayIn, mat -> omod);
1510 +   hastexture = (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY));
1511 +   if (hastexture) {
1512 +      /* Perturb normal */
1513 +      nd.pdot = raynormal(nd.pnorm, rayIn);
1514 +   }
1515 +   else {
1516 +      VCOPY(nd.pnorm, rayIn -> ron);
1517 +      nd.pdot = rayIn -> rod;
1518 +   }
1519 +
1520 +   if (rayIn -> rod < 0.0) {
1521 +      /* Orient perturbed values */
1522 +      nd.pdot = -nd.pdot;
1523 +      for (i = 0; i < 3; i++) {
1524 +         nd.pnorm [i] = -nd.pnorm [i];
1525 +         rayIn -> pert [i] = -rayIn -> pert [i];
1526 +      }
1527 +      
1528 +      hitfront = 0;
1529 +   }
1530 +  
1531 +   /* Get pattern color, modify diffuse values */
1532 +   copycolor(nd.mcolor, rayIn -> pcol);
1533 +   multcolor(nd.rdiff, nd.mcolor);
1534 +   multcolor(nd.tdiff, nd.mcolor);
1535 +
1536 +   /* Load cal file, evaluate spec refl/trans vars */
1537 +   nd.dp = NULL;
1538 +   mf = getfunc(mat, 9, 0x3f, 0);
1539 +   setbrdfunc(&nd);
1540 +   errno = 0;
1541 +   setcolor(rspecCol,
1542 +            evalue(mf->ep[0]), evalue(mf->ep[1]), evalue(mf->ep[2]));
1543 +   setcolor(tspecCol,
1544 +            evalue(mf->ep[3]), evalue(mf->ep[4]), evalue(mf->ep[5]));
1545 +   if (errno == EDOM || errno == ERANGE)
1546 +      objerror(mat, WARNING, "compute error");
1547 +   else {
1548 +      /* Set up probabilities */
1549 +      prDiff = colorAvg(nd.rdiff);
1550 +      ptDiff = colorAvg(nd.tdiff);
1551 +      prSpec = colorAvg(rspecCol);
1552 +      ptSpec = colorAvg(tspecCol);
1553 +      albedo = prDiff + ptDiff + prSpec + ptSpec;
1554 +   }
1555 +
1556 +   /* Insert direct and indirect photon hits if diffuse component */
1557 +   if (prDiff > FTINY || ptDiff > FTINY)
1558 +      addPhotons(rayIn);
1559 +
1560 +   /* Stochastically sample absorption or scattering events */
1561 +   if ((xi = pmapRandom(rouletteState)) > albedo)
1562 +      /* Absorbed */
1563 +      return 0;
1564 +
1565 +   if (xi > (albedo -= prSpec)) {
1566 +      /* Specular reflection */
1567 +      photonRay(rayIn, &rayOut, PMAP_SPECREFL, rspecCol);
1568 +      VSUM(rayOut.rdir, rayIn -> rdir, nd.pnorm, 2 * nd.pdot);
1569 +      checknorm(rayOut.rdir);
1570 +   }
1571 +   else if (xi > (albedo -= ptSpec)) {
1572 +      /* Specular transmission */
1573 +      photonRay(rayIn, &rayOut, PMAP_SPECTRANS, tspecCol);
1574 +      if (hastexture) {
1575 +         /* Perturb direction */
1576 +         VSUB(rayOut.rdir, rayIn -> rdir, rayIn -> pert);
1577 +         if (normalize(rayOut.rdir) == 0.0) {
1578 +            objerror(mat, WARNING, "illegal perturbation");
1579 +            VCOPY(rayOut.rdir, rayIn -> rdir);
1580 +         }
1581 +         else VCOPY(rayOut.rdir, rayIn -> rdir);
1582 +      }
1583 +   }
1584 +   else if (xi > (albedo -= prDiff)) {
1585 +      /* Diffuse reflection */
1586 +      if (!hitfront)
1587 +         flipsurface(rayIn);
1588 +      photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.mcolor);
1589 +      diffPhotonScatter(nd.pnorm, &rayOut);
1590 +   }
1591 +   else {
1592 +      /* Diffuse transmission */
1593 +      if (hitfront)
1594 +         flipsurface(rayIn);
1595 +      photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.mcolor);
1596 +      bnorm [0] = -nd.pnorm [0];
1597 +      bnorm [1] = -nd.pnorm [1];
1598 +      bnorm [2] = -nd.pnorm [2];
1599 +      diffPhotonScatter(bnorm, &rayOut);
1600 +   }
1601 +
1602 +   return 0;
1603 + }
1604 +
1605 +
1606 +
1607 + #if 0
1608 + int
1609 + m_brdf2(                        /* color a ray that hit a BRDF material */
1610 +        OBJREC  *m,
1611 +        RAY  *r
1612 + )
1613 + {
1614 +        BRDFDAT  nd;
1615 +        COLOR  ctmp;
1616 +        FVECT  vtmp;
1617 +        double  dtmp;
1618 +                                                /* always a shadow */
1619 +        if (r->crtype & SHADOW)
1620 +                return(1);
1621 +                                                /* check arguments */
1622 +        if ((m->oargs.nsargs < (hasdata(m->otype)?4:2)) | (m->oargs.nfargs <
1623 +                        ((m->otype==MAT_TFUNC)|(m->otype==MAT_TDATA)?6:4)))
1624 +                objerror(m, USER, "bad # arguments");
1625 +                                                /* check for back side */
1626 +        if (r->rod < 0.0) {
1627 +                if (!backvis) {
1628 +                        raytrans(r);
1629 +                        return(1);
1630 +                }
1631 +                raytexture(r, m->omod);
1632 +                flipsurface(r);                 /* reorient if backvis */
1633 +        } else
1634 +                raytexture(r, m->omod);
1635 +
1636 +        nd.mp = m;
1637 +        nd.pr = r;
1638 +                                                /* get material color */
1639 +        setcolor(nd.mcolor, m->oargs.farg[0],
1640 +                        m->oargs.farg[1],
1641 +                        m->oargs.farg[2]);
1642 +                                                /* get specular component */
1643 +        nd.rspec = m->oargs.farg[3];
1644 +                                                /* compute transmittance */
1645 +        if ((m->otype == MAT_TFUNC) | (m->otype == MAT_TDATA)) {
1646 +                nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec);
1647 +                nd.tspec = nd.trans * m->oargs.farg[5];
1648 +                dtmp = nd.trans - nd.tspec;
1649 +                setcolor(nd.tdiff, dtmp, dtmp, dtmp);
1650 +        } else {
1651 +                nd.tspec = nd.trans = 0.0;
1652 +                setcolor(nd.tdiff, 0.0, 0.0, 0.0);
1653 +        }
1654 +                                                /* compute reflectance */
1655 +        dtmp = 1.0 - nd.trans - nd.rspec;
1656 +        setcolor(nd.rdiff, dtmp, dtmp, dtmp);
1657 +        nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
1658 +        multcolor(nd.mcolor, r->pcol);          /* modify material color */
1659 +        multcolor(nd.rdiff, nd.mcolor);
1660 +        multcolor(nd.tdiff, nd.mcolor);
1661 +                                                /* load auxiliary files */
1662 +        if (hasdata(m->otype)) {
1663 +                nd.dp = getdata(m->oargs.sarg[1]);
1664 +                getfunc(m, 2, 0, 0);
1665 +        } else {
1666 +                nd.dp = NULL;
1667 +                getfunc(m, 1, 0, 0);
1668 +        }
1669 +                                                /* compute ambient */
1670 +        if (nd.trans < 1.0-FTINY) {
1671 +                copycolor(ctmp, nd.mcolor);     /* modified by material color */
1672 +                scalecolor(ctmp, 1.0-nd.trans);
1673 +                multambient(ctmp, r, nd.pnorm);
1674 +                addcolor(r->rcol, ctmp);        /* add to returned color */
1675 +        }
1676 +        if (nd.trans > FTINY) {         /* from other side */
1677 +                flipsurface(r);
1678 +                vtmp[0] = -nd.pnorm[0];
1679 +                vtmp[1] = -nd.pnorm[1];
1680 +                vtmp[2] = -nd.pnorm[2];
1681 +                copycolor(ctmp, nd.mcolor);
1682 +                scalecolor(ctmp, nd.trans);
1683 +                multambient(ctmp, r, vtmp);
1684 +                addcolor(r->rcol, ctmp);
1685 +                flipsurface(r);
1686 +        }
1687 +                                                /* add direct component */
1688 +        direct(r, dirbrdf, &nd);
1689 +
1690 +        return(1);
1691 + }
1692 + #endif
1693 +
1694 +
1695 +
1696 + /*
1697 +   ==================================================================
1698 +   The following code is
1699 +   (c) Lucerne University of Applied Sciences and Arts,
1700 +   supported by the Swiss National Science Foundation (SNSF, #147053)
1701 +   ==================================================================
1702 + */
1703 +
1704   static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1705   /* Generate new photon ray for BSDF modifier and recurse. */
1706   {
1707 +   int      hasthick = (mat->otype == MAT_BSDF);
1708     int      hitFront;
1709     SDError  err;
1710 +   SDValue  bsdfVal;
1711     FVECT           upvec;
1712     MFUNC           *mf;
1713     BSDFDAT      nd;
1714     RAY      rayOut;
1715 <
1715 >   COLOR    bsdfRGB;
1716 >   int      transmitted;
1717 >   double   prDiff, ptDiff, prDiffSD, ptDiffSD, prSpecSD, ptSpecSD,
1718 >            albedo, xi;
1719 >   const double patAlb = bright(rayIn -> pcol);
1720 >  
1721     /* Following code adapted from m_bsdf() */
1722     /* Check arguments */
1723 <   if (mat -> oargs.nsargs < 6 || mat -> oargs.nfargs > 9 ||
1723 >   if (mat -> oargs.nsargs < hasthick+5 || mat -> oargs.nfargs > 9 ||
1724         mat -> oargs.nfargs % 3)
1725        objerror(mat, USER, "bad # arguments");
1726        
1727 <        hitFront = (rayIn -> rod > 0);
1727 >   hitFront = (rayIn -> rod > 0);
1728  
1729 <        /* Load cal file */
1730 <        mf = getfunc(mat, 5, 0x1d, 1);
1731 <        
1732 <        /* Get thickness */
1733 <        nd.thick = evalue(mf -> ep [0]);
1734 <        if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
1735 <                nd.thick = .0;
1736 <                
1737 <   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;
1729 >   /* Load cal file */
1730 >   mf = hasthick ? getfunc(mat, 5, 0x1d, 1) : getfunc(mat, 4, 0xe, 1);
1731 >
1732 >   /* Get thickness */
1733 >   nd.thick = 0;
1734 >   if (hasthick) {
1735 >   nd.thick = evalue(mf -> ep [0]);
1736 >   if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
1737 >      nd.thick = .0;
1738     }
1739  
1740     /* Get BSDF data */
1741 <   nd.sd = loadBSDF(mat -> oargs.sarg [1]);
1741 >   nd.sd = loadBSDF(mat -> oargs.sarg [hasthick]);
1742    
1743 <   /* Diffuse reflectance */
1743 >   /* Extra diffuse reflectance from material def */
1744     if (hitFront) {
1745        if (mat -> oargs.nfargs < 3)
1746           setcolor(nd.rdiff, .0, .0, .0);
# Line 1428 | Line 1748 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1748                      mat -> oargs.farg [2]);
1749     }    
1750     else if (mat -> oargs.nfargs < 6) {
1751 <        /* Check for absorbing backside */
1752 <        if (!backvis && !nd.sd -> rb && !nd.sd -> tf) {
1753 <           SDfreeCache(nd.sd);                    
1754 <           return 0;
1751 >      /* Check for absorbing backside */
1752 >      if (!backvis && !nd.sd -> rb && !nd.sd -> tf) {
1753 >         SDfreeCache(nd.sd);
1754 >         return 0;
1755        }
1756        
1757        setcolor(nd.rdiff, .0, .0, .0);
# Line 1439 | Line 1759 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1759     else setcolor(nd.rdiff, mat -> oargs.farg [3], mat -> oargs.farg [4],
1760                   mat -> oargs.farg [5]);
1761  
1762 <        /* Diffuse transmittance */
1763 <        if (mat -> oargs.nfargs < 9)
1764 <           setcolor(nd.tdiff, .0, .0, .0);
1762 >   /* Extra diffuse transmittance from material def */
1763 >   if (mat -> oargs.nfargs < 9)
1764 >      setcolor(nd.tdiff, .0, .0, .0);
1765     else setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7],
1766                   mat -> oargs.farg [8]);
1767                
1768     nd.mp = mat;
1769     nd.pr = rayIn;
1770 <        
1770 >
1771     /* Get modifiers */
1772     raytexture(rayIn, mat -> omod);
1773    
1774     /* Modify diffuse values */
1775     multcolor(nd.rdiff, rayIn -> pcol);
1776     multcolor(nd.tdiff, rayIn -> pcol);
1777 <                
1777 >
1778     /* Get up vector & xform to world coords */
1779 <   upvec [0] = evalue(mf -> ep [1]);
1780 <   upvec [1] = evalue(mf -> ep [2]);
1781 <   upvec [2] = evalue(mf -> ep [3]);
1779 >   upvec [0] = evalue(mf -> ep [hasthick+0]);
1780 >   upvec [1] = evalue(mf -> ep [hasthick+1]);
1781 >   upvec [2] = evalue(mf -> ep [hasthick+2]);
1782    
1783     if (mf -> fxp != &unitxf) {
1784        multv3(upvec, upvec, mf -> fxp -> xfm);
# Line 1492 | Line 1812 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1812     }
1813    
1814     /* Determine BSDF resolution */
1815 <   err = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin + SDqueryMax, nd.sd);
1815 >   err = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL,
1816 >                    SDqueryMin + SDqueryMax, nd.sd);
1817    
1818     if (err)
1819        objerror(mat, USER, transSDError(err));
# Line 1501 | Line 1822 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1822     nd.sr_vpsa [1] = sqrt(nd.sr_vpsa [1]);
1823  
1824     /* Orient perturbed normal towards incident side */
1825 <   if (!hitFront) {                    
1825 >   if (!hitFront) {
1826        nd.pnorm [0] = -nd.pnorm [0];
1827        nd.pnorm [1] = -nd.pnorm [1];
1828        nd.pnorm [2] = -nd.pnorm [2];
1829     }
1830 <  
1831 <   /* Following code adapted from SDsampBSDF() */
1832 <   {
1833 <      SDSpectralDF   *rdf, *tdf;
1834 <      SDValue        bsdfVal;
1835 <      double         xi, rhoDiff = 0;
1836 <      float          coef [SDmaxCh];
1837 <      int            i, j, n, nr;
1838 <      SDComponent          *sdc;
1839 <      const SDCDst   **cdarr = NULL;
1840 <      
1841 <      /* Get diffuse albedo (?) */
1842 <      if (hitFront) {
1843 <         bsdfVal = nd.sd -> rLambFront;
1844 <         rdf = nd.sd -> rf;
1845 <         tdf = nd.sd -> tf ? nd.sd -> tf : nd.sd -> tb;
1846 <      }
1847 <      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));
1830 >
1831 >   /* Get scatter probabilities (weighted by pattern except for spec refl)
1832 >    * prDiff, ptDiff:      extra diffuse component in material def
1833 >    * prDiffSD, ptDiffSD:  diffuse (constant) component in SDF
1834 >    * prSpecSD, ptSpecSD:  non-diffuse ("specular") component in SDF
1835 >    * albedo:              sum of above, inverse absorption probability */
1836 >   prDiff   = colorAvg(nd.rdiff);
1837 >   ptDiff   = colorAvg(nd.tdiff);
1838 >   prDiffSD = patAlb * SDdirectHemi(nd.vray, SDsampDf | SDsampR, nd.sd);
1839 >   ptDiffSD = patAlb * SDdirectHemi(nd.vray, SDsampDf | SDsampT, nd.sd);
1840 >   prSpecSD = SDdirectHemi(nd.vray, SDsampSp | SDsampR, nd.sd);
1841 >   ptSpecSD = patAlb * SDdirectHemi(nd.vray, SDsampSp | SDsampT, nd.sd);
1842 >   albedo   = prDiff + ptDiff + prDiffSD + ptDiffSD + prSpecSD + ptSpecSD;
1843 >
1844 >   /*    
1845 >   if (albedo > 1)
1846 >      objerror(mat, WARNING, "Invalid albedo");
1847 >   */
1848          
1849 <      while (j-- > 0) {
1850 <         /* Sum up non-diffuse transmittance */
1851 <         cdarr [i + j] = (*tdf -> comp [j].func -> getCDist)(nd.vray, &tdf -> comp [j]);
1852 <        
1853 <         if (!cdarr [i + j])
1548 <            cdarr [i + j] = &SDemptyCD;
1549 <         else bsdfVal.cieY += cdarr [i + j] -> cTotal;
1550 <      }
1849 >   /* Insert direct and indirect photon hits if diffuse component */
1850 >   if (prDiff + ptDiff + prDiffSD + ptDiffSD > FTINY)
1851 >      addPhotons(rayIn);        
1852 >
1853 >   xi = pmapRandom(rouletteState);
1854        
1855 <      while (i-- > 0) {
1856 <         /* Sum up non-diffuse reflectance */
1857 <         cdarr [i] = (*rdf -> comp [i].func -> getCDist)(nd.vray, &rdf -> comp [i]);
1855 >   if (xi > albedo)
1856 >      /* Absorbtion */
1857 >      return 0;
1858 >  
1859 >   transmitted = 0;
1860 >
1861 >   if ((xi -= prDiff) <= 0) {
1862 >      /* Diffuse reflection (extra component in material def) */
1863 >      photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.rdiff);
1864 >      diffPhotonScatter(nd.pnorm, &rayOut);
1865 >   }
1866 >  
1867 >   else if ((xi -= ptDiff) <= 0) {
1868 >      /* Diffuse transmission (extra component in material def) */
1869 >      photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.tdiff);
1870 >      diffPhotonScatter(nd.pnorm, &rayOut);
1871 >      transmitted = 1;
1872 >   }
1873 >  
1874 >   else {   /* Sample SDF */
1875 >      if ((xi -= prDiffSD) <= 0) {
1876 >         /* Diffuse SDF reflection (constant component) */
1877 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1878 >                               SDsampDf | SDsampR, nd.sd)))
1879 >            objerror(mat, USER, transSDError(err));
1880          
1881 <         if (!cdarr [i])
1882 <            cdarr [i] = &SDemptyCD;
1883 <         else bsdfVal.cieY += cdarr [i] -> cTotal;
1881 >         /* Apply pattern to spectral component */
1882 >         ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1883 >         multcolor(bsdfRGB, rayIn -> pcol);
1884 >         photonRay(rayIn, &rayOut, PMAP_DIFFREFL, bsdfRGB);
1885        }
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         }
1886  
1887 <         if (i < nr) {
1888 <            /* Non-diffuse reflection */
1889 <            sdc = &rdf -> comp [i];
1890 <            rayOutType = PMAP_SPECREFL;
1891 <         }
1605 <         else {
1606 <            /* Non-diffuse transmission */
1607 <            sdc = &tdf -> comp [i - nr];
1608 <            rayOutType = PMAP_SPECTRANS;
1609 <         }
1887 >      else if ((xi -= ptDiffSD) <= 0) {
1888 >         /* Diffuse SDF transmission (constant component) */
1889 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1890 >                               SDsampDf | SDsampT, nd.sd)))
1891 >            objerror(mat, USER, transSDError(err));
1892          
1893 <         /* 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);
1893 >         /* Apply pattern to spectral component */
1894           ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1895 <        
1896 <         /* Xform outgoing dir to world coords */
1897 <         if ((err = SDmapDir(rayOut.rdir, nd.fromloc, rayOut.rdir))) {
1895 >         multcolor(bsdfRGB, rayIn -> pcol);
1896 >         addcolor(bsdfRGB, nd.tdiff);      
1897 >         photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, bsdfRGB);
1898 >         transmitted = 1;
1899 >      }
1900 >
1901 >      else if ((xi -= prSpecSD) <= 0) {
1902 >         /* Non-diffuse ("specular") SDF reflection */
1903 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1904 >                               SDsampSp | SDsampR, nd.sd)))
1905              objerror(mat, USER, transSDError(err));
1643            return 0;
1644         }
1906          
1907 <         photonRay(rayIn, &rayOut, rayOutType, bsdfRGB);
1907 >         ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1908 >         photonRay(rayIn, &rayOut, PMAP_SPECREFL, bsdfRGB);
1909        }
1910        
1911 <      if (cdarr)
1912 <         free(cdarr);
1911 >      else {
1912 >         /* Non-diffuse ("specular") SDF transmission */
1913 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1914 >                               SDsampSp | SDsampT, nd.sd)))
1915 >            objerror(mat, USER, transSDError(err));
1916 >
1917 >         /* Apply pattern to spectral component */
1918 >         ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1919 >         multcolor(bsdfRGB, rayIn -> pcol);
1920 >         photonRay(rayIn, &rayOut, PMAP_SPECTRANS, bsdfRGB);
1921 >         transmitted = 1;
1922 >      }      
1923 >      
1924 >      /* Xform outgoing dir to world coords */
1925 >      if ((err = SDmapDir(rayOut.rdir, nd.fromloc, nd.vray))) {
1926 >         objerror(mat, USER, transSDError(err));
1927 >         return 0;
1928 >      }
1929     }
1930 <                        
1931 <   /* Clean up BSDF */
1930 >      
1931 >   /* Clean up */
1932     SDfreeCache(nd.sd);
1933  
1934 +   /* Offset outgoing photon origin by thickness to bypass proxy geometry */
1935 +   if (transmitted && nd.thick != 0)
1936 +      VSUM(rayOut.rorg, rayOut.rorg, rayIn -> ron, -nd.thick);
1937 +
1938     tracePhoton(&rayOut);
1939     return 0;
1940   }
# Line 1660 | Line 1942 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1942  
1943  
1944   static int lightPhotonScatter (OBJREC* mat, RAY* ray)
1945 < /* Light sources doan' reflect */
1945 > /* Light sources doan' reflect, mang */
1946   {
1947     return 0;
1948   }
# Line 1672 | Line 1954 | void initPhotonScatterFuncs ()
1954   {
1955     int i;
1956    
1957 +   /* Catch-all for inconsistencies */
1958     for (i = 0; i < NUMOTYPE; i++)
1959        photonScatter [i] = o_default;
1960        
# Line 1702 | Line 1985 | void initPhotonScatterFuncs ()
1985              photonScatter [TEX_DATA] = pattexPhotonScatter;
1986              
1987     photonScatter [MOD_ALIAS] = aliasPhotonScatter;
1988 <   photonScatter [MAT_BSDF] = bsdfPhotonScatter;
1988 >   photonScatter [MAT_BRTDF] = brtdFuncPhotonScatter;
1989 >   photonScatter [MAT_BSDF] =
1990 >      photonScatter [MAT_ABSDF] = bsdfPhotonScatter;
1991   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines