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.6 by greg, Fri May 22 10:27:40 2015 UTC vs.
Revision 2.24 by rschregle, Mon Feb 22 13:27:49 2021 UTC

# Line 1 | Line 1
1 + #ifndef lint
2 + static const char RCSid[] = "$Id$";
3 + #endif
4   /*
5 <   ==================================================================
5 >
6 >   ======================================================================
7     Photon map support routines for scattering by materials.
8  
9     Roland Schregle (roland.schregle@{hslu.ch, gmail.com})
10     (c) Fraunhofer Institute for Solar Energy Systems,
11 +       supported by the German Research Foundation
12 +       (DFG LU-204/10-2, "Fassadenintegrierte Regelsysteme FARESYS")
13     (c) Lucerne University of Applied Sciences and Arts,
14 <   supported by the Swiss National Science Foundation (SNSF, #147053)
15 <   ==================================================================
14 >       supported by the Swiss National Science Foundation
15 >       (SNSF #147053, "Daylight Redirecting Components")
16 >   ======================================================================
17    
11   $Id$
18   */
19  
20  
# Line 32 | Line 38
38   #define  SP_FLAT     010
39   #define  SP_BADU     040
40   #define  MLAMBDA     500
41 < #define  RINDEX    1.52
41 > #define  RINDEX      1.52
42   #define  FRESNE(ci)  (exp(-5.85*(ci)) - 0.00287989916)
43  
44  
# Line 44 | Line 50 | typedef struct {
50     COLOR    mcolor, scolor;
51     FVECT    vrefl, prdir, pnorm;
52     double   alpha2, rdiff, rspec, trans, tdiff, tspec, pdot;
53 < }  NORMDAT;
53 > } NORMDAT;
54  
55   typedef struct {
56     OBJREC   *mp;
# Line 53 | Line 59 | typedef struct {
59     COLOR    mcolor, scolor;
60     FVECT    vrefl, prdir, u, v, pnorm;
61     double   u_alpha, v_alpha, rdiff, rspec, trans, tdiff, tspec, pdot;
62 < }  ANISODAT;
62 > } ANISODAT;
63  
64   typedef struct {
65     OBJREC   *mp;
66 <   RAY      *pr;                
67 <   FVECT    pnorm;              
68 <   FVECT    vray;              
69 <   double   sr_vpsa [2];        
70 <   RREAL        toloc [3][3];  
71 <   RREAL        fromloc [3][3];
72 <   double   thick;              
66 >   RAY      *pr;
67 >   DATARRAY *dp;
68 >   COLOR    mcolor;
69 >   COLOR    rdiff;
70 >   COLOR    tdiff;
71 >   double   rspec;
72 >   double   trans;
73 >   double   tspec;
74 >   FVECT    pnorm;
75 >   double   pdot;
76 > } BRDFDAT;
77 >
78 > typedef struct {
79 >   OBJREC   *mp;
80 >   RAY      *pr;
81 >   FVECT    pnorm;
82 >   FVECT    vray;
83 >   double   sr_vpsa [2];
84 >   RREAL    toloc [3][3];
85 >   RREAL    fromloc [3][3];
86 >   double   thick;
87     SDData   *sd;
88     COLOR           runsamp;
89     COLOR           rdiff;
# Line 104 | Line 124 | void photonRay (const RAY *rayIn, RAY *rayOut,
124   {
125     rayorigin(rayOut, rayOutType, rayIn, NULL);
126    
127 <   /* Transfer flux */
128 <   copycolor(rayOut -> rcol, rayIn -> rcol);
129 <  
130 <   /* Copy caustic flag & direction for transferred rays */
131 <   if (rayOutType == PMAP_XFER) {
132 <      /* rayOut -> rtype |= rayIn -> rtype & SPECULAR; */
133 <      rayOut -> rtype |= rayIn -> rtype;
134 <      VCOPY(rayOut -> rdir, rayIn -> rdir);
135 <   }
136 <   else if (fluxAtten) {
137 <      /* Attenuate and normalise flux for scattered rays */
138 <      multcolor(rayOut -> rcol, fluxAtten);
139 <      colorNorm(rayOut -> rcol);
140 <   }
127 >   if (rayIn) {
128 >      /* Transfer flux */
129 >      copycolor(rayOut -> rcol, rayIn -> rcol);
130 >      
131 >      /* Copy caustic flag & direction for transferred rays */
132 >      if (rayOutType == PMAP_XFER) {
133 >         /* rayOut -> rtype |= rayIn -> rtype & SPECULAR; */
134 >         rayOut -> rtype |= rayIn -> rtype;
135 >         VCOPY(rayOut -> rdir, rayIn -> rdir);
136 >      }
137 >      else if (fluxAtten) {
138 >         /* Attenuate and normalise flux for scattered rays */
139 >         multcolor(rayOut -> rcol, fluxAtten);
140 >         colorNorm(rayOut -> rcol);
141 >      }
142  
143 <   /* Propagate index of emitting light source */
144 <   rayOut -> rsrc = rayIn -> rsrc;
143 >      /* Propagate index of emitting light source */
144 >      rayOut -> rsrc = rayIn -> rsrc;
145 >      
146 >      /* Update maximum photon path distance */
147 >      rayOut -> rmax = rayIn -> rmax - rayIn -> rot;
148 >   }
149   }
150  
151  
127
152   static void addPhotons (const RAY *r)
153   /* Insert photon hits, where applicable */
154   {
155     if (!r -> rlvl)
156 <      /* Add direct photon map at primary hitpoint */
157 <      addPhoton(directPmap, r);
156 >      /* Add direct photon at primary hitpoint */
157 >      newPhoton(directPmap, r);
158     else {
159 <      /* Add global or precomputed photon map at indirect hitpoint */
160 <      addPhoton(preCompPmap ? preCompPmap : globalPmap, r);
159 >      /* Add global or precomputed photon at indirect hitpoint */
160 >      newPhoton(preCompPmap ? preCompPmap : globalPmap, r);
161  
162        /* Store caustic photon if specular flag set */
163        if (PMAP_CAUSTICRAY(r))
164 <         addPhoton(causticPmap, r);
164 >         newPhoton(causticPmap, r);
165          
166        /* Store in contribution photon map */
167 <      addPhoton(contribPmap, r);
167 >      newPhoton(contribPmap, r);
168     }
169   }
170  
# Line 199 | Line 223 | static int isoSpecPhotonScatter (NORMDAT *nd, RAY *ray
223     int      niter, i = 0;
224    
225     /* Set up sample coordinates */  
226 <   getperpendicular(u, nd -> pnorm, 1);  
226 >   getperpendicular(u, nd -> pnorm, 1);
227     fcross(v, nd -> pnorm, u);
228    
229     if (nd -> specfl & SP_REFL) {
# Line 233 | Line 257 | static int isoSpecPhotonScatter (NORMDAT *nd, RAY *ray
257           cosp = cos(d);
258           sinp = sin(d);
259           d2 = pmapRandom(scatterState);
260 <         d = d2 <= FTINY ? 1 : sqrt(-log(d2) * nd -> alpha2);        
260 >         d = d2 <= FTINY ? 1 : sqrt(-log(d2) * nd -> alpha2);
261          
262           for (i = 0; i < 3; i++)
263              rayOut -> rdir [i] = nd -> prdir [i] +
# Line 254 | Line 278 | static int isoSpecPhotonScatter (NORMDAT *nd, RAY *ray
278   static void diffPhotonScatter (FVECT normal, RAY* rayOut)
279   /* Generate cosine-weighted direction for diffuse ray */
280   {
281 <   const RREAL cosThetaSqr = pmapRandom(scatterState),
281 >   const RREAL cosThetaSqr = pmapRandom(scatterState),
282                 cosTheta = sqrt(cosThetaSqr),
283 <               sinTheta = sqrt(1 - cosThetaSqr),
284 <               phi = 2 * PI * pmapRandom(scatterState),
283 >               sinTheta = sqrt(1 - cosThetaSqr),
284 >               phi = 2 * PI * pmapRandom(scatterState),
285                 du = cos(phi) * sinTheta, dv = sin(phi) * sinTheta;
286     FVECT       u, v;
287     int         i = 0;
# Line 298 | Line 322 | static int normalPhotonScatter (OBJREC *mat, RAY *rayI
322        }
323     else raytexture(rayIn, mat -> omod);
324    
325 +   nd.mp = mat;
326     nd.rp = rayIn;
327    
328     /* Get material color */
# Line 414 | Line 439 | static int normalPhotonScatter (OBJREC *mat, RAY *rayI
439        
440        if (hastexture) {
441           /* Perturb */
442 <         for (i = 0; i < 3; i++)
442 >         for (i = 0; i < 3; i++)
443              nd.prdir [i] = rayIn -> rdir [i] - rayIn -> pert [i];
444              
445 <         if (DOT(nd.prdir, rayIn -> ron) < -FTINY)
445 >         if (DOT(nd.prdir, rayIn -> ron) < -FTINY)
446              normalize(nd.prdir);
447           else VCOPY(nd.prdir, rayIn -> rdir);
448        }
449        else VCOPY(nd.prdir, rayIn -> rdir);
450        
451        if ((nd.specfl & (SP_TRAN | SP_PURE)) == (SP_TRAN | SP_PURE))
452 <         /* Perfect specular transmission */  
452 >         /* Perfect specular transmission */
453           VCOPY(rayOut.rdir, nd.prdir);
454 <      else if (!isoSpecPhotonScatter(&nd, &rayOut))
454 >      else if (!isoSpecPhotonScatter(&nd, &rayOut))
455           return 0;
456          
457 <      photonRay(rayIn, &rayOut, PMAP_SPECTRANS, nd.mcolor);  
457 >      photonRay(rayIn, &rayOut, PMAP_SPECTRANS, nd.mcolor);
458     }
459    
460     else if (xi > (albedo -= prdiff)) {
# Line 459 | Line 484 | static int normalPhotonScatter (OBJREC *mat, RAY *rayI
484  
485  
486  
487 < static void getacoords (ANISODAT *np)
487 > static void getacoords (ANISODAT *nd)
488   /* Set up coordinate system for anisotropic sampling; cloned from aniso.c */
489   {
490 <        MFUNC  *mf;
491 <        int  i;
490 >   MFUNC *mf;
491 >   int   i;
492  
493 <        mf = getfunc(np->mp, 3, 0x7, 1);
494 <        setfunc(np->mp, np->rp);
495 <        errno = 0;
496 <        
497 <        for (i = 0; i < 3; i++)
498 <           np->u[i] = evalue(mf->ep[i]);
474 <          
475 <   if ((errno == EDOM) | (errno == ERANGE)) {
476 <      objerror(np->mp, WARNING, "compute error");
477 <      np->specfl |= SP_BADU;
478 <      return;
479 <   }
493 >   mf = getfunc(nd -> mp, 3, 0x7, 1);
494 >   setfunc(nd -> mp, nd -> rp);
495 >   errno = 0;
496 >
497 >   for (i = 0; i < 3; i++)
498 >      nd -> u [i] = evalue(mf -> ep [i]);
499    
500 <   if (mf->fxp != &unitxf)
501 <      multv3(np->u, np->u, mf->fxp->xfm);
500 >   if (errno == EDOM || errno == ERANGE)
501 >      nd -> u [0] = nd -> u [1] = nd -> u [2] = 0.0;
502        
503 <        fcross(np->v, np->pnorm, np->u);
504 <        
505 <        if (normalize(np->v) == 0.0) {
506 <           objerror(np->mp, WARNING, "illegal orientation vector");
488 <           np->specfl |= SP_BADU;
489 <           return;
490 <   }
503 >   if (mf -> fxp != &unitxf)
504 >      multv3(nd -> u, nd -> u, mf -> fxp -> xfm);
505 >
506 >   fcross(nd -> v, nd -> pnorm, nd -> u);
507    
508 <   fcross(np->u, np->v, np->pnorm);
508 >   if (normalize(nd -> v) == 0.0) {
509 >      if (fabs(nd -> u_alpha - nd -> v_alpha) > 0.001)
510 >         objerror(nd -> mp, WARNING, "illegal orientation vector");
511 >      getperpendicular(nd -> u, nd -> pnorm, 1);
512 >      fcross(nd -> v, nd -> pnorm, nd -> u);
513 >      nd -> u_alpha = nd -> v_alpha =
514 >         sqrt(0.5 * (sqr(nd -> u_alpha) + sqr(nd -> v_alpha)));
515 >   }
516 >   else fcross(nd -> u, nd -> v, nd -> pnorm);
517   }
518  
519  
# Line 512 | Line 536 | static int anisoSpecPhotonScatter (ANISODAT *nd, RAY *
536     if (rayOut -> rtype & TRANS) {
537        /* Specular transmission */
538  
539 <      if (DOT(rayIn -> pert, rayIn -> pert) <= FTINY * FTINY)
539 >      if (DOT(rayIn -> pert, rayIn -> pert) <= sqr(FTINY))
540           VCOPY(nd -> prdir, rayIn -> rdir);
541        else {
542           /* perturb */
# Line 548 | Line 572 | static int anisoSpecPhotonScatter (ANISODAT *nd, RAY *
572           }
573        }
574        
575 <      return 0;  
575 >      return 0;
576     }
577    
578     else {
# Line 566 | Line 590 | static int anisoSpecPhotonScatter (ANISODAT *nd, RAY *
590           d = d2 <= FTINY ? 1
591                           : sqrt(-log(d2) /
592                                  (sqr(cosp) / sqr(nd -> u_alpha) +
593 <                                 sqr(sinp) / (nd -> v_alpha * nd -> v_alpha)));
593 >                                 sqr(sinp) / (nd->v_alpha * nd->v_alpha)));
594                                  
595           for (i = 0; i < 3; i++)
596              h [i] = nd -> pnorm [i] +
# Line 595 | Line 619 | static int anisoPhotonScatter (OBJREC *mat, RAY *rayIn
619     if (mat -> oargs.nfargs != (mat -> otype == MAT_TRANS2 ? 8 : 6))
620        objerror(mat, USER, "bad number of real arguments");
621        
622 +   nd.mp = mat;
623     nd.rp = rayIn;
599   nd.mp = objptr(rayIn -> ro -> omod);
624    
625     /* get material color */
626     copycolor(nd.mcolor, mat -> oargs.farg);
# Line 640 | Line 664 | static int anisoPhotonScatter (OBJREC *mat, RAY *rayIn
664     if (nd.rspec > FTINY) {
665        nd.specfl |= SP_REFL;
666        
667 <      /* comput   e specular color */
667 >      /* compute specular color */
668        if (mat -> otype == MAT_METAL2)
669           copycolor(nd.scolor, nd.mcolor);
670        else setcolor(nd.scolor, 1, 1, 1);
# Line 743 | Line 767 | static int dielectricPhotonScatter (OBJREC *mat, RAY *
767     /* get modifiers */
768     raytexture(rayIn, mat -> omod);                      
769    
770 <   if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > FTINY * FTINY)))
770 >   if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY))))
771        /* Perturb normal */
772        cos1 = raynormal(dnorm, rayIn);
773     else {
# Line 753 | Line 777 | static int dielectricPhotonScatter (OBJREC *mat, RAY *
777    
778     /* index of refraction */
779     nratio = mat -> otype ==
780 <      MAT_DIELECTRIC ? mat -> oargs.farg [3] + mat -> oargs.farg [4] / MLAMBDA
781 <                     : mat -> oargs.farg [3] / mat -> oargs.farg [7];
780 >      MAT_DIELECTRIC ? mat->oargs.farg[3] + mat->oargs.farg[4] / MLAMBDA
781 >                     : mat->oargs.farg[3] / mat->oargs.farg[7];
782                      
783     if (cos1 < 0) {
784        /* inside */
# Line 831 | Line 855 | static int dielectricPhotonScatter (OBJREC *mat, RAY *
855        for (i = 0; i < 3; i++)
856           rayOut.rdir [i] = nratio * rayIn -> rdir [i] + d1 * dnorm [i];
857          
858 <      if (hastexture && DOT(rayOut.rdir, rayIn -> ron) * hastexture >= -FTINY) {
858 >      if (hastexture && DOT(rayOut.rdir, rayIn->ron)*hastexture >= -FTINY) {
859           d1 *= hastexture;
860          
861           for (i = 0; i < 3; i++)
# Line 850 | Line 874 | static int dielectricPhotonScatter (OBJREC *mat, RAY *
874        photonRay(rayIn, &rayOut, PMAP_SPECREFL, NULL);
875        VSUM(rayOut.rdir, rayIn -> rdir, dnorm, 2 * cos1);
876        
877 <      if (hastexture && DOT(rayOut.rdir, rayIn -> ron) * hastexture <= FTINY)
877 >      if (hastexture && DOT(rayOut.rdir, rayIn->ron) * hastexture <= FTINY)
878           for (i = 0; i < 3; i++)
879              rayOut.rdir [i] = rayIn -> rdir [i] +
880                                2 * rayIn -> rod * rayIn -> ron [i];
# Line 890 | Line 914 | static int glassPhotonScatter (OBJREC *mat, RAY *rayIn
914     /* reorient if necessary */
915     if (rayIn -> rod < 0)
916        flipsurface(rayIn);
917 <   if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > FTINY * FTINY) ))
917 >   if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY))))
918        pdot = raynormal(pnorm, rayIn);
919     else {
920        VCOPY(pnorm, rayIn -> ron);
# Line 972 | Line 996 | static int aliasPhotonScatter (OBJREC *mat, RAY *rayIn
996   /* Transfer photon scattering to alias target */
997   {
998     OBJECT   aliasObj;
999 <   OBJREC   aliasRec;
999 >   OBJREC   aliasRec, *aliasPtr;
1000    
1001     /* Straight replacement? */
1002     if (!mat -> oargs.nsargs) {
1003 <      mat = objptr(mat -> omod);
1004 <      photonScatter [mat -> otype] (mat, rayIn);
1003 >      /* Skip void modifier! */
1004 >      if (mat -> omod != OVOID) {  
1005 >         mat = objptr(mat -> omod);
1006 >         photonScatter [mat -> otype] (mat, rayIn);
1007 >      }
1008        
1009        return 0;
1010     }
# Line 986 | Line 1013 | static int aliasPhotonScatter (OBJREC *mat, RAY *rayIn
1013     if (mat -> oargs.nsargs != 1)
1014        objerror(mat, INTERNAL, "bad # string arguments");
1015        
1016 <   aliasObj = lastmod(objndx(mat), mat -> oargs.sarg [0]);
1017 <      
991 <   if (aliasObj < 0)
992 <      objerror(mat, USER, "bad reference");
993 <      
994 <   memcpy(&aliasRec, objptr(aliasObj), sizeof(OBJREC));
1016 >   aliasPtr = mat;
1017 >   aliasObj = objndx(aliasPtr);
1018    
1019 +   /* Follow alias trail */
1020 +   do {
1021 +      aliasObj = aliasPtr -> oargs.nsargs == 1
1022 +                     ? lastmod(aliasObj, aliasPtr -> oargs.sarg [0])
1023 +                     : aliasPtr -> omod;
1024 +      if (aliasObj < 0)
1025 +         objerror(aliasPtr, USER, "bad reference");
1026 +        
1027 +      aliasPtr = objptr(aliasObj);
1028 +   } while (aliasPtr -> otype == MOD_ALIAS);
1029 +
1030 +   /* Copy alias object */
1031 +   aliasRec = *aliasPtr;
1032 +  
1033     /* Substitute modifier */
1034     aliasRec.omod = mat -> omod;
1035    
1036     /* Replacement scattering routine */
1037     photonScatter [aliasRec.otype] (&aliasRec, rayIn);
1038 +
1039 +   /* Avoid potential memory leak? */
1040 +   if (aliasRec.os != aliasPtr -> os) {
1041 +      if (aliasPtr -> os)
1042 +         free_os(aliasPtr);
1043 +      aliasPtr -> os = aliasRec.os;
1044 +   }
1045 +
1046     return 0;
1047   }
1048  
# Line 1026 | Line 1071 | static int clipPhotonScatter (OBJREC *mat, RAY *rayIn)
1071              continue;
1072              
1073           if ((mod = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) {
1074 <            sprintf(errmsg, "unknown modifier \"%s\"", mat -> oargs.sarg [i]);
1074 >            sprintf(errmsg, "unknown modifier \"%s\"", mat->oargs.sarg[i]);
1075              objerror(mat, WARNING, errmsg);
1076              continue;
1077           }
# Line 1222 | Line 1267 | static int mx_dataPhotonScatter (OBJREC *mat, RAY *ray
1267        if (!strcmp(mat -> oargs.sarg [i], VOIDID))
1268           mod [i] = OVOID;
1269        else if ((mod [i] = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) {
1270 <         sprintf(errmsg, "undefined modifier \"%s\"", mat -> oargs.sarg [i]);
1270 >         sprintf(errmsg, "undefined modifier \"%s\"", mat->oargs.sarg[i]);
1271           objerror(mat, USER, errmsg);
1272        }
1273        
# Line 1248 | Line 1293 | static int mx_dataPhotonScatter (OBJREC *mat, RAY *ray
1293     if (errno)
1294        objerror(mat, WARNING, "compute error");
1295     else {
1296 <      mat = objptr(mod [pmapRandom(rouletteState) < coef ? 0 : 1]);
1297 <      photonScatter [mat -> otype] (mat, rayIn);
1296 >      OBJECT mxMod = mod [pmapRandom(rouletteState) < coef ? 0 : 1];
1297 >      
1298 >      if (mxMod != OVOID) {
1299 >         mat = objptr(mxMod);
1300 >         photonScatter [mat -> otype] (mat, rayIn);
1301 >      }
1302 >      else {
1303 >         /* Transfer ray if no modifier */
1304 >         RAY rayOut;
1305 >        
1306 >         photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1307 >         tracePhoton(&rayOut);      
1308 >      }            
1309     }
1310    
1311     return 0;
# Line 1276 | Line 1332 | static int mx_pdataPhotonScatter (OBJREC *mat, RAY *ra
1332        if (!strcmp(mat -> oargs.sarg [i], VOIDID))
1333           mod [i] = OVOID;
1334        else if ((mod [i] = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) {
1335 <         sprintf(errmsg, "undefined modifier \"%s\"", mat -> oargs.sarg [i]);
1335 >         sprintf(errmsg, "undefined modifier \"%s\"", mat->oargs.sarg[i]);
1336           objerror(mat, USER, errmsg);
1337        }
1338        
# Line 1301 | Line 1357 | static int mx_pdataPhotonScatter (OBJREC *mat, RAY *ra
1357     if (errno)
1358        objerror(mat, WARNING, "compute error");
1359     else {
1360 <      mat = objptr(mod [pmapRandom(rouletteState) < coef ? 0 : 1]);
1361 <      photonScatter [mat -> otype] (mat, rayIn);
1360 >      OBJECT mxMod = mod [pmapRandom(rouletteState) < coef ? 0 : 1];
1361 >      
1362 >      if (mxMod != OVOID) {
1363 >         mat = objptr(mxMod);
1364 >         photonScatter [mat -> otype] (mat, rayIn);
1365 >      }
1366 >      else {
1367 >         /* Transfer ray if no modifier */
1368 >         RAY rayOut;
1369 >        
1370 >         photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1371 >         tracePhoton(&rayOut);      
1372 >      }      
1373     }  
1374    
1375     return 0;
# Line 1327 | Line 1394 | static int mx_funcPhotonScatter (OBJREC *mat, RAY *ray
1394        if (!strcmp(mat -> oargs.sarg [i], VOIDID))
1395           mod [i] = OVOID;
1396        else if ((mod [i] = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) {
1397 <         sprintf(errmsg, "undefined modifier \"%s\"", mat -> oargs.sarg [i]);
1397 >         sprintf(errmsg, "undefined modifier \"%s\"", mat->oargs.sarg[i]);
1398           objerror(mat, USER, errmsg);
1399        }
1400        
# Line 1341 | Line 1408 | static int mx_funcPhotonScatter (OBJREC *mat, RAY *ray
1408     if (errno)
1409        objerror(mat, WARNING, "compute error");
1410     else {        
1411 <      mat = objptr(mod [pmapRandom(rouletteState) < coef ? 0 : 1]);
1412 <      photonScatter [mat -> otype] (mat, rayIn);
1411 >      OBJECT mxMod = mod [pmapRandom(rouletteState) < coef ? 0 : 1];
1412 >      
1413 >      if (mxMod != OVOID) {
1414 >         mat = objptr(mxMod);
1415 >         photonScatter [mat -> otype] (mat, rayIn);
1416 >      }
1417 >      else {
1418 >         /* Transfer ray if no modifier */
1419 >         RAY rayOut;
1420 >        
1421 >         photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1422 >         tracePhoton(&rayOut);      
1423 >      }
1424     }
1425    
1426     return 0;
# Line 1374 | Line 1452 | static int pattexPhotonScatter (OBJREC *mat, RAY *rayI
1452  
1453  
1454  
1455 < #if 0
1456 <   static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1457 <   /* Generate new photon ray for BSDF modifier and recurse. */
1458 <   {
1459 <      int      hitFront;
1460 <      SDError  err;
1461 <      FVECT        upvec;
1384 <      MFUNC        *mf;
1385 <      BSDFDAT   nd;
1386 <      RAY      rayOut;
1455 > static int setbrdfunc(BRDFDAT *bd)
1456 > /* Set up brdf function and variables; ripped off from m_brdf.c */
1457 > {
1458 >   FVECT v;
1459 >  
1460 >   if (setfunc(bd -> mp, bd -> pr) == 0)
1461 >      return 0;
1462  
1463 <      /* Following code adapted from m_bsdf() */
1464 <      /* Check arguments */
1465 <      if (mat -> oargs.nsargs < 6 || mat -> oargs.nfargs > 9 ||
1466 <          mat -> oargs.nfargs % 3)
1467 <         objerror(mat, USER, "bad # arguments");
1468 <        
1469 <      hitFront = (rayIn -> rod > 0);
1463 >   /* (Re)Assign func variables */
1464 >   multv3(v, bd -> pnorm, funcxf.xfm);
1465 >   varset("NxP", '=', v [0] / funcxf.sca);
1466 >   varset("NyP", '=', v [1] / funcxf.sca);
1467 >   varset("NzP", '=', v [2] / funcxf.sca);
1468 >   varset("RdotP", '=',
1469 >          bd -> pdot <= -1. ? -1. : bd -> pdot >= 1. ? 1. : bd -> pdot);
1470 >   varset("CrP", '=', colval(bd -> mcolor, RED));
1471 >   varset("CgP", '=', colval(bd -> mcolor, GRN));
1472 >   varset("CbP", '=', colval(bd -> mcolor, BLU));
1473 >  
1474 >   return 1;
1475 > }
1476  
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      }
1477  
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]);
1478  
1479 <      /* Diffuse transmittance */
1480 <      if (mat -> oargs.nfargs < 9)
1481 <         setcolor(nd.tdiff, .0, .0, .0);
1482 <      else setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7],
1483 <                    mat -> oargs.farg [8]);
1484 <                  
1485 <      nd.mp = mat;
1486 <      nd.pr = rayIn;
1479 > static int brdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1480 > /* Generate new photon ray for BRTDfunc material and recurse. Only ideal
1481 >   reflection and transmission are sampled for the specular componentent. */
1482 > {
1483 >   int      hitfront = 1, hastexture, i;
1484 >   BRDFDAT  nd;
1485 >   RAY      rayOut;
1486 >   COLOR    rspecCol, tspecCol;
1487 >   double   prDiff, ptDiff, prSpec, ptSpec, albedo, xi;
1488 >   MFUNC    *mf;
1489 >   FVECT    bnorm;
1490 >
1491 >   /* Check argz */
1492 >   if (mat -> oargs.nsargs < 10 || mat -> oargs.nfargs < 9)
1493 >      objerror(mat, USER, "bad # arguments");
1494        
1495 <      /* Get modifiers */
1496 <      raytexture(rayIn, mat -> omod);
1497 <      
1498 <      /* Modify diffuse values */
1499 <      multcolor(nd.rdiff, rayIn -> pcol);
1500 <      multcolor(nd.tdiff, rayIn -> pcol);
1501 <        
1502 <      /* Get up vector & xform to world coords */
1503 <      upvec [0] = evalue(mf -> ep [1]);
1504 <      upvec [1] = evalue(mf -> ep [2]);
1505 <      upvec [2] = evalue(mf -> ep [3]);
1506 <      
1507 <      if (mf -> fxp != &unitxf) {
1508 <         multv3(upvec, upvec, mf -> fxp -> xfm);
1509 <         nd.thick *= mf -> fxp -> sca;
1495 >   nd.mp = mat;
1496 >   nd.pr = rayIn;
1497 >   /* Dummiez */
1498 >   nd.rspec = nd.tspec = 1.0;
1499 >   nd.trans = 0.5;
1500 >
1501 >   /* Diffuz reflektanz */
1502 >   if (rayIn -> rod > 0.0)
1503 >      setcolor(nd.rdiff, mat -> oargs.farg[0], mat -> oargs.farg [1],
1504 >               mat -> oargs.farg [2]);
1505 >   else
1506 >      setcolor(nd.rdiff, mat-> oargs.farg [3], mat -> oargs.farg [4],
1507 >               mat -> oargs.farg [5]);
1508 >   /* Diffuz tranzmittanz */
1509 >   setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7],
1510 >            mat -> oargs.farg [8]);
1511 >
1512 >   /* Get modz */
1513 >   raytexture(rayIn, mat -> omod);
1514 >   hastexture = (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY));
1515 >   if (hastexture) {
1516 >      /* Perturb normal */
1517 >      nd.pdot = raynormal(nd.pnorm, rayIn);
1518 >   }
1519 >   else {
1520 >      VCOPY(nd.pnorm, rayIn -> ron);
1521 >      nd.pdot = rayIn -> rod;
1522 >   }
1523 >
1524 >   if (rayIn -> rod < 0.0) {
1525 >      /* Orient perturbed valuz */
1526 >      nd.pdot = -nd.pdot;
1527 >      for (i = 0; i < 3; i++) {
1528 >         nd.pnorm [i] = -nd.pnorm [i];
1529 >         rayIn -> pert [i] = -rayIn -> pert [i];
1530        }
1531        
1532 <      if (rayIn -> rox) {
1533 <         multv3(upvec, upvec, rayIn -> rox -> f.xfm);
1534 <         nd.thick *= rayIn -> rox -> f.sca;
1532 >      hitfront = 0;
1533 >   }
1534 >  
1535 >   /* Get pattern kolour, modify diffuz valuz */
1536 >   copycolor(nd.mcolor, rayIn -> pcol);
1537 >   multcolor(nd.rdiff, nd.mcolor);
1538 >   multcolor(nd.tdiff, nd.mcolor);
1539 >
1540 >   /* Load cal file, evaluate spekula refl/tranz varz */
1541 >   nd.dp = NULL;
1542 >   mf = getfunc(mat, 9, 0x3f, 0);
1543 >   setbrdfunc(&nd);
1544 >   errno = 0;
1545 >   setcolor(rspecCol,
1546 >            evalue(mf->ep[0]), evalue(mf->ep[1]), evalue(mf->ep[2]));
1547 >   setcolor(tspecCol,
1548 >            evalue(mf->ep[3]), evalue(mf->ep[4]), evalue(mf->ep[5]));
1549 >   if (errno == EDOM || errno == ERANGE)
1550 >      objerror(mat, WARNING, "compute error");
1551 >   else {
1552 >      /* Set up probz */
1553 >      prDiff = colorAvg(nd.rdiff);
1554 >      ptDiff = colorAvg(nd.tdiff);
1555 >      prSpec = colorAvg(rspecCol);
1556 >      ptSpec = colorAvg(tspecCol);
1557 >      albedo = prDiff + ptDiff + prSpec + ptSpec;
1558 >   }
1559 >
1560 >   /* Insert direct and indirect photon hitz if diffuz komponent */
1561 >   if (prDiff > FTINY || ptDiff > FTINY)
1562 >      addPhotons(rayIn);
1563 >
1564 >   /* Stochastically sample absorption or scattering evenz */
1565 >   if ((xi = pmapRandom(rouletteState)) > albedo)
1566 >      /* Absorbed */
1567 >      return 0;
1568 >
1569 >   if (xi > (albedo -= prSpec)) {
1570 >      /* Ideal spekula reflekzion */
1571 >      photonRay(rayIn, &rayOut, PMAP_SPECREFL, rspecCol);
1572 >      VSUM(rayOut.rdir, rayIn -> rdir, nd.pnorm, 2 * nd.pdot);
1573 >      checknorm(rayOut.rdir);
1574 >   }
1575 >   else if (xi > (albedo -= ptSpec)) {
1576 >      /* Ideal spekula tranzmission */
1577 >      photonRay(rayIn, &rayOut, PMAP_SPECTRANS, tspecCol);
1578 >      if (hastexture) {
1579 >         /* Perturb direkzion */
1580 >         VSUB(rayOut.rdir, rayIn -> rdir, rayIn -> pert);
1581 >         if (normalize(rayOut.rdir) == 0.0) {
1582 >            objerror(mat, WARNING, "illegal perturbation");
1583 >            VCOPY(rayOut.rdir, rayIn -> rdir);
1584 >         }
1585        }
1586 +      else VCOPY(rayOut.rdir, rayIn -> rdir);
1587 +   }
1588 +   else if (xi > (albedo -= prDiff)) {
1589 +      /* Diffuz reflekzion */
1590 +      if (!hitfront)
1591 +         flipsurface(rayIn);
1592 +      photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.mcolor);
1593 +      diffPhotonScatter(nd.pnorm, &rayOut);
1594 +   }
1595 +   else {
1596 +      /* Diffuz tranzmission */
1597 +      if (hitfront)
1598 +         flipsurface(rayIn);
1599 +      photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.mcolor);
1600 +      bnorm [0] = -nd.pnorm [0];
1601 +      bnorm [1] = -nd.pnorm [1];
1602 +      bnorm [2] = -nd.pnorm [2];
1603 +      diffPhotonScatter(bnorm, &rayOut);
1604 +   }
1605 +
1606 +   tracePhoton(&rayOut);
1607 +   return 0;
1608 + }
1609 +
1610 +
1611 +
1612 + int brdf2PhotonScatter (OBJREC *mat, RAY *rayIn)
1613 + /* Generate new photon ray for procedural or data driven BRDF material and
1614 +   recurse. Only diffuse reflection and transmission are sampled. */
1615 + {
1616 +   BRDFDAT  nd;
1617 +   RAY      rayOut;
1618 +   double   dtmp, prDiff, ptDiff, albedo, xi;
1619 +   MFUNC    *mf;
1620 +   FVECT    bnorm;
1621 +
1622 +   /* Check argz */
1623 +   if (mat -> oargs.nsargs < (hasdata(mat -> otype) ? 4 : 2) ||
1624 +       mat -> oargs.nfargs < (mat -> otype == MAT_TFUNC ||
1625 +                              mat -> otype == MAT_TDATA ? 6 : 4))
1626 +      objerror(mat, USER, "bad # arguments");
1627        
1628 <      /* Perturb normal */
1629 <      raynormal(nd.pnorm, rayIn);
1630 <      
1631 <      /* Xform incident dir to local BSDF coords */
1632 <      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");
1628 >   if (rayIn -> rod < 0.0) {
1629 >      /* Hit backside; reorient if visible, else transfer photon */
1630 >      if (!backvis) {
1631 >         photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1632 >         tracePhoton(&rayOut);
1633           return 0;
1634        }
1635        
1636 <      /* Determine BSDF resolution */
1637 <      err = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin + SDqueryMax, nd.sd);
1638 <      
1639 <      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]);
1636 >      raytexture(rayIn, mat -> omod);
1637 >      flipsurface(rayIn);
1638 >   }
1639 >   else raytexture(rayIn, mat -> omod);
1640  
1641 <      /* Orient perturbed normal towards incident side */
1642 <      if (!hitFront) {                  
1643 <         nd.pnorm [0] = -nd.pnorm [0];
1644 <         nd.pnorm [1] = -nd.pnorm [1];
1645 <         nd.pnorm [2] = -nd.pnorm [2];
1646 <      }
1647 <      
1648 <      /* Following code adapted from SDsampBSDF() */
1649 <      {
1650 <         SDSpectralDF   *rdf, *tdf;
1651 <         SDValue        bsdfVal;
1652 <         double         xi, rhoDiff = 0;
1653 <         float          coef [SDmaxCh];
1654 <         int            i, j, n, nr;
1655 <         SDComponent       *sdc;
1656 <         const SDCDst   **cdarr = NULL;
1657 <        
1658 <         /* Get diffuse albedo (?) */
1659 <         if (hitFront) {
1660 <            bsdfVal = nd.sd -> rLambFront;
1661 <            rdf = nd.sd -> rf;
1662 <            tdf = nd.sd -> tf ? nd.sd -> tf : nd.sd -> tb;
1663 <         }
1664 <         else {
1665 <            bsdfVal = nd.sd -> rLambBack;
1666 <            rdf = nd.sd -> rb;
1667 <            tdf = nd.sd -> tb ? nd.sd -> tb : nd.sd -> tf;
1668 <         }
1669 <        
1670 <         rhoDiff = bsdfVal.cieY;
1671 <         bsdfVal.cieY += nd.sd -> tLamb.cieY;
1672 <        
1673 <         /* Allocate non-diffuse sampling */
1674 <         i = nr = rdf ? rdf -> ncomp : 0;
1675 <         j = tdf ? tdf -> ncomp : 0;
1676 <         n = i + j;
1677 <        
1678 <         if (n > 0 && !(cdarr = (const SDCDst**)malloc(n * sizeof(SDCDst*))))
1679 <            objerror(mat, USER, transSDError(SDEmemory));
1680 <            
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 <            }
1641 >   nd.mp = mat;
1642 >   nd.pr = rayIn;
1643 >  
1644 >   /* Material kolour */
1645 >   setcolor(nd.mcolor, mat -> oargs.farg [0], mat -> oargs.farg [1],
1646 >            mat -> oargs.farg [2]);
1647 >   /* Spekula komponent */
1648 >   nd.rspec = mat -> oargs.farg [3];
1649 >  
1650 >   /* Tranzmittanz */
1651 >   if (mat -> otype == MAT_TFUNC || mat -> otype == MAT_TDATA) {
1652 >      nd.trans = mat -> oargs.farg [4] * (1.0 - nd.rspec);
1653 >      nd.tspec = nd.trans * mat -> oargs.farg [5];
1654 >      dtmp = nd.trans - nd.tspec;
1655 >      setcolor(nd.tdiff, dtmp, dtmp, dtmp);
1656 >   }
1657 >   else {
1658 >      nd.tspec = nd.trans = 0.0;
1659 >      setcolor(nd.tdiff, 0.0, 0.0, 0.0);
1660 >   }
1661 >  
1662 >   /* Reflektanz */
1663 >   dtmp = 1.0 - nd.trans - nd.rspec;
1664 >   setcolor(nd.rdiff, dtmp, dtmp, dtmp);
1665 >   /* Perturb normal */
1666 >   nd.pdot = raynormal(nd.pnorm, rayIn);
1667 >   /* Modify material kolour */
1668 >   multcolor(nd.mcolor, rayIn -> pcol);
1669 >   multcolor(nd.rdiff, nd.mcolor);
1670 >   multcolor(nd.tdiff, nd.mcolor);
1671 >  
1672 >   /* Load auxiliary filez */
1673 >   if (hasdata(mat -> otype)) {
1674 >      nd.dp = getdata(mat -> oargs.sarg [1]);
1675 >      getfunc(mat, 2, 0, 0);
1676 >   }
1677 >   else {
1678 >      nd.dp = NULL;
1679 >      getfunc(mat, 1, 0, 0);
1680 >   }
1681  
1682 <            if (i < nr) {
1683 <               /* Non-diffuse reflection */
1684 <               sdc = &rdf -> comp [i];
1685 <               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));
1682 >   /* Set up probz */
1683 >   prDiff = colorAvg(nd.rdiff);
1684 >   ptDiff = colorAvg(nd.tdiff);
1685 >   albedo = prDiff + ptDiff;
1686  
1687 <            /* Get colour */
1688 <            j = (*sdc -> func -> getBSDFs)(coef, rayOut.rdir, nd.vray, sdc);
1689 <            
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);
1687 >   /* Insert direct and indirect photon hitz if diffuz komponent */
1688 >   if (prDiff > FTINY || ptDiff > FTINY)
1689 >      addPhotons(rayIn);
1690  
1691 <      tracePhoton(&rayOut);
1691 >   /* Stochastically sample absorption or scattering evenz */
1692 >   if ((xi = pmapRandom(rouletteState)) > albedo)
1693 >      /* Absorbed */
1694        return 0;
1695 +
1696 +   if (xi > (albedo -= prDiff)) {
1697 +      /* Diffuz reflekzion */
1698 +      photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.rdiff);
1699 +      diffPhotonScatter(nd.pnorm, &rayOut);
1700     }
1701 < #else
1701 >   else {
1702 >      /* Diffuz tranzmission */
1703 >      flipsurface(rayIn);
1704 >      photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.tdiff);
1705 >      bnorm [0] = -nd.pnorm [0];
1706 >      bnorm [1] = -nd.pnorm [1];
1707 >      bnorm [2] = -nd.pnorm [2];
1708 >      diffPhotonScatter(bnorm, &rayOut);
1709 >   }
1710  
1711 +   tracePhoton(&rayOut);
1712 +   return 0;
1713 + }
1714 +
1715 +
1716 +
1717   /*
1718 +   ======================================================================
1719     The following code is
1720     (c) Lucerne University of Applied Sciences and Arts,
1721 <   supported by the Swiss National Science Foundation (SNSF, #147053)
1722 < */  
1721 >       supported by the Swiss National Science Foundation
1722 >       (SNSF #147053, "Daylight Redirecting Components")
1723 >   ======================================================================
1724 > */
1725  
1726   static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1727   /* Generate new photon ray for BSDF modifier and recurse. */
1728   {
1729 <   int      hitFront;
1730 <   SDError  err;
1731 <   SDValue  bsdfVal;
1732 <   FVECT           upvec;
1733 <   MFUNC           *mf;
1734 <   BSDFDAT      nd;
1735 <   RAY      rayOut;
1736 <   COLOR    bsdfRGB;
1737 <   int      transmitted;
1738 <   double   prDiff, ptDiff, prDiffSD, ptDiffSD, prSpecSD, ptSpecSD,
1739 <            albedo, xi;
1740 <   const double patAlb = bright(rayIn -> pcol);
1729 >   int            hasthick = (mat->otype == MAT_BSDF);
1730 >   int            hitFront;
1731 >   SDError        err;
1732 >   SDValue        bsdfVal;
1733 >   FVECT          upvec;
1734 >   MFUNC          *mf;
1735 >   BSDFDAT        nd;
1736 >   RAY            rayOut;
1737 >   COLOR          bsdfRGB;
1738 >   int            transmitted;
1739 >   double         prDiff, ptDiff, prDiffSD, ptDiffSD, prSpecSD, ptSpecSD,
1740 >                  albedo, xi;
1741 >   const double   patAlb = bright(rayIn -> pcol);
1742    
1743     /* Following code adapted from m_bsdf() */
1744     /* Check arguments */
1745 <   if (mat -> oargs.nsargs < 6 || mat -> oargs.nfargs > 9 ||
1746 <       mat -> oargs.nfargs % 3)
1745 >   if (
1746 >      mat -> oargs.nsargs < hasthick+5 ||
1747 >      mat -> oargs.nfargs > 9 || mat -> oargs.nfargs % 3
1748 >   )
1749        objerror(mat, USER, "bad # arguments");
1750        
1751 <        hitFront = (rayIn -> rod > 0);
1751 >   hitFront = (rayIn -> rod > 0);
1752  
1753 <        /* Load cal file */
1754 <        mf = getfunc(mat, 5, 0x1d, 1);
1755 <        
1756 <        /* Get thickness */
1757 <        nd.thick = evalue(mf -> ep [0]);
1758 <        if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
1759 <                nd.thick = .0;
1760 < #if 0  
1761 <   if (nd.thick != .0 || (!hitFront && !backvis)) {
1692 <      /* Proxy geometry present, so use it instead and transfer ray */
1693 <      photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1694 <      tracePhoton(&rayOut);
1695 <      
1696 <      return 0;
1753 >   /* Load cal file */
1754 >   mf = hasthick ? getfunc(mat, 5, 0x1d, 1) : getfunc(mat, 4, 0xe, 1);
1755 >
1756 >   /* Get thickness */
1757 >   nd.thick = 0;
1758 >   if (hasthick) {
1759 >      nd.thick = evalue(mf -> ep [0]);
1760 >      if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
1761 >         nd.thick = .0;
1762     }
1763 < #endif
1763 >
1764     /* Get BSDF data */
1765 <   nd.sd = loadBSDF(mat -> oargs.sarg [1]);
1765 >   nd.sd = loadBSDF(mat -> oargs.sarg [hasthick]);
1766    
1767     /* Extra diffuse reflectance from material def */
1768     if (hitFront) {
1769        if (mat -> oargs.nfargs < 3)
1770           setcolor(nd.rdiff, .0, .0, .0);
1771 <      else setcolor(nd.rdiff, mat -> oargs.farg [0], mat -> oargs.farg [1],
1772 <                    mat -> oargs.farg [2]);
1771 >      else setcolor(
1772 >         nd.rdiff,
1773 >         mat -> oargs.farg [0], mat -> oargs.farg [1], mat -> oargs.farg [2]
1774 >      );
1775     }    
1776     else if (mat -> oargs.nfargs < 6) {
1777 <        /* Check for absorbing backside */
1778 <        if (!backvis && !nd.sd -> rb && !nd.sd -> tf) {
1779 <           SDfreeCache(nd.sd);                    
1780 <           return 0;
1777 >      /* Check for absorbing backside */
1778 >      if (!backvis && !nd.sd -> rb && !nd.sd -> tf) {
1779 >         SDfreeCache(nd.sd);
1780 >         return 0;
1781        }
1782        
1783        setcolor(nd.rdiff, .0, .0, .0);
1784     }
1785 <   else setcolor(nd.rdiff, mat -> oargs.farg [3], mat -> oargs.farg [4],
1786 <                 mat -> oargs.farg [5]);
1785 >   else setcolor(
1786 >      nd.rdiff,
1787 >      mat -> oargs.farg [3], mat -> oargs.farg [4], mat -> oargs.farg [5]
1788 >   );
1789  
1790 <        /* Extra diffuse transmittance from material def */
1791 <        if (mat -> oargs.nfargs < 9)
1792 <           setcolor(nd.tdiff, .0, .0, .0);
1793 <   else setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7],
1794 <                 mat -> oargs.farg [8]);
1790 >   /* Extra diffuse transmittance from material def */
1791 >   if (mat -> oargs.nfargs < 9)
1792 >      setcolor(nd.tdiff, .0, .0, .0);
1793 >   else setcolor(
1794 >      nd.tdiff,
1795 >      mat -> oargs.farg [6], mat -> oargs.farg [7], mat -> oargs.farg [8]
1796 >   );
1797                
1798     nd.mp = mat;
1799     nd.pr = rayIn;
1800 <        
1800 >
1801     /* Get modifiers */
1802     raytexture(rayIn, mat -> omod);
1803    
1804     /* Modify diffuse values */
1805     multcolor(nd.rdiff, rayIn -> pcol);
1806     multcolor(nd.tdiff, rayIn -> pcol);
1807 <                
1807 >
1808     /* Get up vector & xform to world coords */
1809 <   upvec [0] = evalue(mf -> ep [1]);
1810 <   upvec [1] = evalue(mf -> ep [2]);
1811 <   upvec [2] = evalue(mf -> ep [3]);
1809 >   upvec [0] = evalue(mf -> ep [hasthick+0]);
1810 >   upvec [1] = evalue(mf -> ep [hasthick+1]);
1811 >   upvec [2] = evalue(mf -> ep [hasthick+2]);
1812    
1813     if (mf -> fxp != &unitxf) {
1814        multv3(upvec, upvec, mf -> fxp -> xfm);
# Line 1771 | Line 1842 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1842     }
1843    
1844     /* Determine BSDF resolution */
1845 <   err = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin + SDqueryMax, nd.sd);
1845 >   err = SDsizeBSDF(
1846 >      nd.sr_vpsa, nd.vray, NULL, SDqueryMin + SDqueryMax, nd.sd
1847 >   );
1848    
1849     if (err)
1850        objerror(mat, USER, transSDError(err));
# Line 1780 | Line 1853 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1853     nd.sr_vpsa [1] = sqrt(nd.sr_vpsa [1]);
1854  
1855     /* Orient perturbed normal towards incident side */
1856 <   if (!hitFront) {                    
1856 >   if (!hitFront) {
1857        nd.pnorm [0] = -nd.pnorm [0];
1858        nd.pnorm [1] = -nd.pnorm [1];
1859        nd.pnorm [2] = -nd.pnorm [2];
# Line 1824 | Line 1897 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1897    
1898     else if ((xi -= ptDiff) <= 0) {
1899        /* Diffuse transmission (extra component in material def) */
1827      flipsurface(rayIn);
1828      nd.thick = -nd.thick;
1900        photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.tdiff);
1901        diffPhotonScatter(nd.pnorm, &rayOut);
1902        transmitted = 1;
# Line 1834 | Line 1905 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1905     else {   /* Sample SDF */
1906        if ((xi -= prDiffSD) <= 0) {
1907           /* Diffuse SDF reflection (constant component) */
1908 <         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1909 <                               SDsampDf | SDsampR, nd.sd)))
1908 >         if ((err = SDsampBSDF(
1909 >            &bsdfVal, nd.vray, pmapRandom(scatterState),
1910 >            SDsampDf | SDsampR, nd.sd
1911 >         )))
1912              objerror(mat, USER, transSDError(err));
1913          
1914           /* Apply pattern to spectral component */
# Line 1846 | Line 1919 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1919  
1920        else if ((xi -= ptDiffSD) <= 0) {
1921           /* Diffuse SDF transmission (constant component) */
1922 <         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1923 <                               SDsampDf | SDsampT, nd.sd)))
1922 >         if ((err = SDsampBSDF(
1923 >            &bsdfVal, nd.vray, pmapRandom(scatterState),
1924 >            SDsampDf | SDsampT, nd.sd
1925 >         )))
1926              objerror(mat, USER, transSDError(err));
1927          
1928           /* Apply pattern to spectral component */
1929           ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1930           multcolor(bsdfRGB, rayIn -> pcol);
1931           addcolor(bsdfRGB, nd.tdiff);      
1932 <         flipsurface(rayIn);  /* Necessary? */
1858 <         nd.thick = -nd.thick;
1859 <         photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, bsdfRGB);
1932 >         photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, bsdfRGB);
1933           transmitted = 1;
1934        }
1935  
1936        else if ((xi -= prSpecSD) <= 0) {
1937           /* Non-diffuse ("specular") SDF reflection */
1938 <         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1939 <                               SDsampSp | SDsampR, nd.sd)))
1938 >         if ((err = SDsampBSDF(
1939 >            &bsdfVal, nd.vray, pmapRandom(scatterState),
1940 >            SDsampSp | SDsampR, nd.sd
1941 >         )))
1942              objerror(mat, USER, transSDError(err));
1943          
1944           ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
# Line 1872 | Line 1947 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1947        
1948        else {
1949           /* Non-diffuse ("specular") SDF transmission */
1950 <         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1951 <                               SDsampSp | SDsampT, nd.sd)))
1950 >         if ((err = SDsampBSDF(
1951 >            &bsdfVal, nd.vray, pmapRandom(scatterState),
1952 >            SDsampSp | SDsampT, nd.sd
1953 >         )))
1954              objerror(mat, USER, transSDError(err));
1955  
1956           /* Apply pattern to spectral component */
1957           ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1958           multcolor(bsdfRGB, rayIn -> pcol);
1882         flipsurface(rayIn);  /* Necessary? */
1883         nd.thick = -nd.thick;
1959           photonRay(rayIn, &rayOut, PMAP_SPECTRANS, bsdfRGB);
1960           transmitted = 1;
1961        }      
# Line 1895 | Line 1970 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1970     /* Clean up */
1971     SDfreeCache(nd.sd);
1972  
1973 <   /* Need to offset ray origin to get past detail geometry? */
1973 >   /* Offset outgoing photon origin by thickness to bypass proxy geometry */
1974     if (transmitted && nd.thick != 0)
1975 <        VSUM(rayOut.rorg, rayOut.rorg, rayIn -> ron, -nd.thick);
1975 >      VSUM(rayOut.rorg, rayOut.rorg, rayIn -> ron, -nd.thick);
1976  
1977     tracePhoton(&rayOut);
1978     return 0;
1979   }
1905 #endif
1980  
1981  
1982  
1983   static int lightPhotonScatter (OBJREC* mat, RAY* ray)
1984 < /* Light sources doan' reflect */
1984 > /* Light sources doan' reflect, mang */
1985   {
1986     return 0;
1987   }
# Line 1918 | Line 1992 | void initPhotonScatterFuncs ()
1992   /* Init photonScatter[] dispatch table */
1993   {
1994     int i;
1995 <  
1995 >
1996 >   /* Catch-all for inconsistencies */
1997     for (i = 0; i < NUMOTYPE; i++)
1998        photonScatter [i] = o_default;
1999 <      
1999 >
2000     photonScatter [MAT_LIGHT] = photonScatter [MAT_ILLUM] =
2001        photonScatter [MAT_GLOW] = photonScatter [MAT_SPOT] =
2002           lightPhotonScatter;
2003 <        
2003 >
2004     photonScatter [MAT_PLASTIC] = photonScatter [MAT_METAL] =
2005        photonScatter [MAT_TRANS] = normalPhotonScatter;
2006        
# Line 1934 | Line 2009 | void initPhotonScatterFuncs ()
2009        
2010     photonScatter [MAT_DIELECTRIC] = photonScatter [MAT_INTERFACE] =
2011        dielectricPhotonScatter;
2012 <      
2012 >
2013     photonScatter [MAT_MIST] = mistPhotonScatter;
2014     photonScatter [MAT_GLASS] = glassPhotonScatter;
2015     photonScatter [MAT_CLIP] = clipPhotonScatter;
# Line 1942 | Line 2017 | void initPhotonScatterFuncs ()
2017     photonScatter [MIX_FUNC] = mx_funcPhotonScatter;
2018     photonScatter [MIX_DATA] = mx_dataPhotonScatter;
2019     photonScatter [MIX_PICT]= mx_pdataPhotonScatter;
2020 <  
2020 >
2021     photonScatter [PAT_BDATA] = photonScatter [PAT_CDATA] =
2022        photonScatter [PAT_BFUNC] = photonScatter [PAT_CFUNC] =
2023           photonScatter [PAT_CPICT] = photonScatter [TEX_FUNC] =
2024              photonScatter [TEX_DATA] = pattexPhotonScatter;
2025 <            
2025 >
2026     photonScatter [MOD_ALIAS] = aliasPhotonScatter;
2027 <   photonScatter [MAT_BSDF] = bsdfPhotonScatter;
2027 >   photonScatter [MAT_BRTDF] = brdfPhotonScatter;
2028 >  
2029 >   photonScatter [MAT_PFUNC] = photonScatter [MAT_MFUNC] =
2030 >      photonScatter [MAT_PDATA] = photonScatter [MAT_MDATA] =
2031 >         photonScatter [MAT_TFUNC] = photonScatter [MAT_TDATA] =
2032 >            brdf2PhotonScatter;
2033 >
2034 >   photonScatter [MAT_BSDF] = photonScatter [MAT_ABSDF] =
2035 >      bsdfPhotonScatter;
2036   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines