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

Comparing ray/src/rt/pmapmat.c (file contents):
Revision 2.2 by rschregle, Wed Apr 22 15:50:44 2015 UTC vs.
Revision 2.23 by rschregle, Wed Jan 20 19:44:15 2021 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 43 | Line 46 | typedef struct {
46     COLOR    mcolor, scolor;
47     FVECT    vrefl, prdir, pnorm;
48     double   alpha2, rdiff, rspec, trans, tdiff, tspec, pdot;
49 < }  NORMDAT;
49 > } NORMDAT;
50  
51   typedef struct {
52     OBJREC   *mp;
# Line 52 | Line 55 | typedef struct {
55     COLOR    mcolor, scolor;
56     FVECT    vrefl, prdir, u, v, pnorm;
57     double   u_alpha, v_alpha, rdiff, rspec, trans, tdiff, tspec, pdot;
58 < }  ANISODAT;
58 > } ANISODAT;
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 normalise 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  
126
148   static void addPhotons (const RAY *r)
149   /* Insert photon hits, where applicable */
150   {
151     if (!r -> rlvl)
152 <      /* Add direct photon map at primary hitpoint */
153 <      addPhoton(directPmap, r);
152 >      /* Add direct photon at primary hitpoint */
153 >      newPhoton(directPmap, r);
154     else {
155 <      /* Add global or precomputed photon map at indirect hitpoint */
156 <      addPhoton(preCompPmap ? preCompPmap : globalPmap, r);
155 >      /* Add global or precomputed photon at indirect hitpoint */
156 >      newPhoton(preCompPmap ? preCompPmap : globalPmap, r);
157  
158        /* Store caustic photon if specular flag set */
159        if (PMAP_CAUSTICRAY(r))
160 <         addPhoton(causticPmap, r);
160 >         newPhoton(causticPmap, r);
161          
162        /* Store in contribution photon map */
163 <      addPhoton(contribPmap, r);
163 >      newPhoton(contribPmap, r);
164     }
165   }
166  
# Line 198 | Line 219 | static int isoSpecPhotonScatter (NORMDAT *nd, RAY *ray
219     int      niter, i = 0;
220    
221     /* Set up sample coordinates */  
222 <   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 <  
222 >   getperpendicular(u, nd -> pnorm, 1);
223     fcross(v, nd -> pnorm, u);
224    
225     if (nd -> specfl & SP_REFL) {
# Line 237 | Line 253 | static int isoSpecPhotonScatter (NORMDAT *nd, RAY *ray
253           cosp = cos(d);
254           sinp = sin(d);
255           d2 = pmapRandom(scatterState);
256 <         d = d2 <= FTINY ? 1 : sqrt(-log(d2) * nd -> alpha2);        
256 >         d = d2 <= FTINY ? 1 : sqrt(-log(d2) * nd -> alpha2);
257          
258           for (i = 0; i < 3; i++)
259              rayOut -> rdir [i] = nd -> prdir [i] +
# Line 258 | Line 274 | static int isoSpecPhotonScatter (NORMDAT *nd, RAY *ray
274   static void diffPhotonScatter (FVECT normal, RAY* rayOut)
275   /* Generate cosine-weighted direction for diffuse ray */
276   {
277 <   const RREAL cosThetaSqr = pmapRandom(scatterState),
277 >   const RREAL cosThetaSqr = pmapRandom(scatterState),
278                 cosTheta = sqrt(cosThetaSqr),
279 <               sinTheta = sqrt(1 - cosThetaSqr),
280 <               phi = 2 * PI * pmapRandom(scatterState),
279 >               sinTheta = sqrt(1 - cosThetaSqr),
280 >               phi = 2 * PI * pmapRandom(scatterState),
281                 du = cos(phi) * sinTheta, dv = sin(phi) * sinTheta;
282     FVECT       u, v;
283     int         i = 0;
284  
285     /* Set up sample coordinates */
286 <   do {
271 <      v [0] = v [1] = v [2] = 0;
272 <      v [i++] = 1;
273 <      fcross(u, v, normal);
274 <   } while (normalize(u) < FTINY);
275 <  
286 >   getperpendicular(u, normal, 1);
287     fcross(v, normal, u);
288    
289     /* Convert theta & phi to cartesian */
# Line 307 | Line 318 | static int normalPhotonScatter (OBJREC *mat, RAY *rayI
318        }
319     else raytexture(rayIn, mat -> omod);
320    
321 +   nd.mp = mat;
322     nd.rp = rayIn;
323    
324     /* Get material color */
# 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 468 | Line 480 | static int normalPhotonScatter (OBJREC *mat, RAY *rayI
480  
481  
482  
483 < static void getacoords (ANISODAT *np)
483 > static void getacoords (ANISODAT *nd)
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]);
483 <          
484 <   if ((errno == EDOM) | (errno == ERANGE)) {
485 <      objerror(np->mp, WARNING, "compute error");
486 <      np->specfl |= SP_BADU;
487 <      return;
488 <   }
489 >   mf = getfunc(nd -> mp, 3, 0x7, 1);
490 >   setfunc(nd -> mp, nd -> rp);
491 >   errno = 0;
492 >
493 >   for (i = 0; i < 3; i++)
494 >      nd -> u [i] = evalue(mf -> ep [i]);
495    
496 <   if (mf->fxp != &unitxf)
497 <      multv3(np->u, np->u, mf->fxp->xfm);
496 >   if (errno == EDOM || errno == ERANGE)
497 >      nd -> u [0] = nd -> u [1] = nd -> u [2] = 0.0;
498        
499 <        fcross(np->v, np->pnorm, np->u);
500 <        
501 <        if (normalize(np->v) == 0.0) {
502 <           objerror(np->mp, WARNING, "illegal orientation vector");
497 <           np->specfl |= SP_BADU;
498 <           return;
499 <   }
499 >   if (mf -> fxp != &unitxf)
500 >      multv3(nd -> u, nd -> u, mf -> fxp -> xfm);
501 >
502 >   fcross(nd -> v, nd -> pnorm, nd -> u);
503    
504 <   fcross(np->u, np->v, np->pnorm);
504 >   if (normalize(nd -> v) == 0.0) {
505 >      if (fabs(nd -> u_alpha - nd -> v_alpha) > 0.001)
506 >         objerror(nd -> mp, WARNING, "illegal orientation vector");
507 >      getperpendicular(nd -> u, nd -> pnorm, 1);
508 >      fcross(nd -> v, nd -> pnorm, nd -> u);
509 >      nd -> u_alpha = nd -> v_alpha =
510 >         sqrt(0.5 * (sqr(nd -> u_alpha) + sqr(nd -> v_alpha)));
511 >   }
512 >   else fcross(nd -> u, nd -> v, nd -> pnorm);
513   }
514  
515  
# Line 521 | Line 532 | static int anisoSpecPhotonScatter (ANISODAT *nd, RAY *
532     if (rayOut -> rtype & TRANS) {
533        /* Specular transmission */
534  
535 <      if (DOT(rayIn -> pert, rayIn -> pert) <= FTINY * FTINY)
535 >      if (DOT(rayIn -> pert, rayIn -> pert) <= sqr(FTINY))
536           VCOPY(nd -> prdir, rayIn -> rdir);
537        else {
538           /* perturb */
# Line 557 | Line 568 | static int anisoSpecPhotonScatter (ANISODAT *nd, RAY *
568           }
569        }
570        
571 <      return 0;  
571 >      return 0;
572     }
573    
574     else {
# Line 575 | Line 586 | static int anisoSpecPhotonScatter (ANISODAT *nd, RAY *
586           d = d2 <= FTINY ? 1
587                           : sqrt(-log(d2) /
588                                  (sqr(cosp) / sqr(nd -> u_alpha) +
589 <                                 sqr(sinp) / (nd -> v_alpha * nd -> v_alpha)));
589 >                                 sqr(sinp) / (nd->v_alpha * nd->v_alpha)));
590                                  
591           for (i = 0; i < 3; i++)
592              h [i] = nd -> pnorm [i] +
# Line 604 | Line 615 | static int anisoPhotonScatter (OBJREC *mat, RAY *rayIn
615     if (mat -> oargs.nfargs != (mat -> otype == MAT_TRANS2 ? 8 : 6))
616        objerror(mat, USER, "bad number of real arguments");
617        
618 +   nd.mp = mat;
619     nd.rp = rayIn;
608   nd.mp = objptr(rayIn -> ro -> omod);
620    
621     /* get material color */
622     copycolor(nd.mcolor, mat -> oargs.farg);
# Line 649 | Line 660 | static int anisoPhotonScatter (OBJREC *mat, RAY *rayIn
660     if (nd.rspec > FTINY) {
661        nd.specfl |= SP_REFL;
662        
663 <      /* comput   e specular color */
663 >      /* compute specular color */
664        if (mat -> otype == MAT_METAL2)
665           copycolor(nd.scolor, nd.mcolor);
666        else setcolor(nd.scolor, 1, 1, 1);
# Line 752 | Line 763 | static int dielectricPhotonScatter (OBJREC *mat, RAY *
763     /* get modifiers */
764     raytexture(rayIn, mat -> omod);                      
765    
766 <   if ((hastexture = DOT(rayIn -> pert, rayIn -> pert)) > FTINY * FTINY)
766 >   if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY))))
767        /* Perturb normal */
768        cos1 = raynormal(dnorm, rayIn);
769     else {
# Line 762 | Line 773 | static int dielectricPhotonScatter (OBJREC *mat, RAY *
773    
774     /* index of refraction */
775     nratio = mat -> otype ==
776 <      MAT_DIELECTRIC ? mat -> oargs.farg [3] + mat -> oargs.farg [4] / MLAMBDA
777 <                     : mat -> oargs.farg [3] / mat -> oargs.farg [7];
776 >      MAT_DIELECTRIC ? mat->oargs.farg[3] + mat->oargs.farg[4] / MLAMBDA
777 >                     : mat->oargs.farg[3] / mat->oargs.farg[7];
778                      
779     if (cos1 < 0) {
780        /* inside */
# Line 840 | Line 851 | static int dielectricPhotonScatter (OBJREC *mat, RAY *
851        for (i = 0; i < 3; i++)
852           rayOut.rdir [i] = nratio * rayIn -> rdir [i] + d1 * dnorm [i];
853          
854 <      if (hastexture && DOT(rayOut.rdir, rayIn -> ron) * hastexture >= -FTINY) {
854 >      if (hastexture && DOT(rayOut.rdir, rayIn->ron)*hastexture >= -FTINY) {
855           d1 *= hastexture;
856          
857           for (i = 0; i < 3; i++)
# Line 859 | Line 870 | static int dielectricPhotonScatter (OBJREC *mat, RAY *
870        photonRay(rayIn, &rayOut, PMAP_SPECREFL, NULL);
871        VSUM(rayOut.rdir, rayIn -> rdir, dnorm, 2 * cos1);
872        
873 <      if (hastexture && DOT(rayOut.rdir, rayIn -> ron) * hastexture <= FTINY)
873 >      if (hastexture && DOT(rayOut.rdir, rayIn->ron) * hastexture <= FTINY)
874           for (i = 0; i < 3; i++)
875              rayOut.rdir [i] = rayIn -> rdir [i] +
876                                2 * rayIn -> rod * rayIn -> ron [i];
# Line 899 | Line 910 | static int glassPhotonScatter (OBJREC *mat, RAY *rayIn
910     /* reorient if necessary */
911     if (rayIn -> rod < 0)
912        flipsurface(rayIn);
913 <   if ((hastexture = DOT(rayIn -> pert, rayIn -> pert)) > FTINY * FTINY)
913 >   if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY))))
914        pdot = raynormal(pnorm, rayIn);
915     else {
916        VCOPY(pnorm, rayIn -> ron);
# Line 981 | Line 992 | static int aliasPhotonScatter (OBJREC *mat, RAY *rayIn
992   /* Transfer photon scattering to alias target */
993   {
994     OBJECT   aliasObj;
995 <   OBJREC   aliasRec;
995 >   OBJREC   aliasRec, *aliasPtr;
996    
997     /* Straight replacement? */
998     if (!mat -> oargs.nsargs) {
999 <      mat = objptr(mat -> omod);
1000 <      photonScatter [mat -> otype] (mat, rayIn);
999 >      /* Skip void modifier! */
1000 >      if (mat -> omod != OVOID) {  
1001 >         mat = objptr(mat -> omod);
1002 >         photonScatter [mat -> otype] (mat, rayIn);
1003 >      }
1004        
1005        return 0;
1006     }
# Line 995 | Line 1009 | static int aliasPhotonScatter (OBJREC *mat, RAY *rayIn
1009     if (mat -> oargs.nsargs != 1)
1010        objerror(mat, INTERNAL, "bad # string arguments");
1011        
1012 <   aliasObj = lastmod(objndx(mat), mat -> oargs.sarg [0]);
1013 <      
1000 <   if (aliasObj < 0)
1001 <      objerror(mat, USER, "bad reference");
1002 <      
1003 <   memcpy(&aliasRec, objptr(aliasObj), sizeof(OBJREC));
1012 >   aliasPtr = mat;
1013 >   aliasObj = objndx(aliasPtr);
1014    
1015 +   /* Follow alias trail */
1016 +   do {
1017 +      aliasObj = aliasPtr -> oargs.nsargs == 1
1018 +                     ? lastmod(aliasObj, aliasPtr -> oargs.sarg [0])
1019 +                     : aliasPtr -> omod;
1020 +      if (aliasObj < 0)
1021 +         objerror(aliasPtr, USER, "bad reference");
1022 +        
1023 +      aliasPtr = objptr(aliasObj);
1024 +   } while (aliasPtr -> otype == MOD_ALIAS);
1025 +
1026 +   /* Copy alias object */
1027 +   aliasRec = *aliasPtr;
1028 +  
1029     /* Substitute modifier */
1030     aliasRec.omod = mat -> omod;
1031    
1032     /* Replacement scattering routine */
1033     photonScatter [aliasRec.otype] (&aliasRec, rayIn);
1034 +
1035 +   /* Avoid potential memory leak? */
1036 +   if (aliasRec.os != aliasPtr -> os) {
1037 +      if (aliasPtr -> os)
1038 +         free_os(aliasPtr);
1039 +      aliasPtr -> os = aliasRec.os;
1040 +   }
1041 +
1042     return 0;
1043   }
1044  
# Line 1035 | Line 1067 | static int clipPhotonScatter (OBJREC *mat, RAY *rayIn)
1067              continue;
1068              
1069           if ((mod = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) {
1070 <            sprintf(errmsg, "unknown modifier \"%s\"", mat -> oargs.sarg [i]);
1070 >            sprintf(errmsg, "unknown modifier \"%s\"", mat->oargs.sarg[i]);
1071              objerror(mat, WARNING, errmsg);
1072              continue;
1073           }
# Line 1231 | Line 1263 | static int mx_dataPhotonScatter (OBJREC *mat, RAY *ray
1263        if (!strcmp(mat -> oargs.sarg [i], VOIDID))
1264           mod [i] = OVOID;
1265        else if ((mod [i] = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) {
1266 <         sprintf(errmsg, "undefined modifier \"%s\"", mat -> oargs.sarg [i]);
1266 >         sprintf(errmsg, "undefined modifier \"%s\"", mat->oargs.sarg[i]);
1267           objerror(mat, USER, errmsg);
1268        }
1269        
# Line 1257 | Line 1289 | static int mx_dataPhotonScatter (OBJREC *mat, RAY *ray
1289     if (errno)
1290        objerror(mat, WARNING, "compute error");
1291     else {
1292 <      mat = objptr(mod [pmapRandom(rouletteState) < coef ? 0 : 1]);
1293 <      photonScatter [mat -> otype] (mat, rayIn);
1292 >      OBJECT mxMod = mod [pmapRandom(rouletteState) < coef ? 0 : 1];
1293 >      
1294 >      if (mxMod != OVOID) {
1295 >         mat = objptr(mxMod);
1296 >         photonScatter [mat -> otype] (mat, rayIn);
1297 >      }
1298 >      else {
1299 >         /* Transfer ray if no modifier */
1300 >         RAY rayOut;
1301 >        
1302 >         photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1303 >         tracePhoton(&rayOut);      
1304 >      }            
1305     }
1306    
1307     return 0;
# Line 1285 | Line 1328 | static int mx_pdataPhotonScatter (OBJREC *mat, RAY *ra
1328        if (!strcmp(mat -> oargs.sarg [i], VOIDID))
1329           mod [i] = OVOID;
1330        else if ((mod [i] = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) {
1331 <         sprintf(errmsg, "undefined modifier \"%s\"", mat -> oargs.sarg [i]);
1331 >         sprintf(errmsg, "undefined modifier \"%s\"", mat->oargs.sarg[i]);
1332           objerror(mat, USER, errmsg);
1333        }
1334        
# Line 1310 | Line 1353 | static int mx_pdataPhotonScatter (OBJREC *mat, RAY *ra
1353     if (errno)
1354        objerror(mat, WARNING, "compute error");
1355     else {
1356 <      mat = objptr(mod [pmapRandom(rouletteState) < coef ? 0 : 1]);
1357 <      photonScatter [mat -> otype] (mat, rayIn);
1356 >      OBJECT mxMod = mod [pmapRandom(rouletteState) < coef ? 0 : 1];
1357 >      
1358 >      if (mxMod != OVOID) {
1359 >         mat = objptr(mxMod);
1360 >         photonScatter [mat -> otype] (mat, rayIn);
1361 >      }
1362 >      else {
1363 >         /* Transfer ray if no modifier */
1364 >         RAY rayOut;
1365 >        
1366 >         photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1367 >         tracePhoton(&rayOut);      
1368 >      }      
1369     }  
1370    
1371     return 0;
# Line 1336 | Line 1390 | static int mx_funcPhotonScatter (OBJREC *mat, RAY *ray
1390        if (!strcmp(mat -> oargs.sarg [i], VOIDID))
1391           mod [i] = OVOID;
1392        else if ((mod [i] = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) {
1393 <         sprintf(errmsg, "undefined modifier \"%s\"", mat -> oargs.sarg [i]);
1393 >         sprintf(errmsg, "undefined modifier \"%s\"", mat->oargs.sarg[i]);
1394           objerror(mat, USER, errmsg);
1395        }
1396        
# Line 1350 | Line 1404 | static int mx_funcPhotonScatter (OBJREC *mat, RAY *ray
1404     if (errno)
1405        objerror(mat, WARNING, "compute error");
1406     else {        
1407 <      mat = objptr(mod [pmapRandom(rouletteState) < coef ? 0 : 1]);
1408 <      photonScatter [mat -> otype] (mat, rayIn);
1407 >      OBJECT mxMod = mod [pmapRandom(rouletteState) < coef ? 0 : 1];
1408 >      
1409 >      if (mxMod != OVOID) {
1410 >         mat = objptr(mxMod);
1411 >         photonScatter [mat -> otype] (mat, rayIn);
1412 >      }
1413 >      else {
1414 >         /* Transfer ray if no modifier */
1415 >         RAY rayOut;
1416 >        
1417 >         photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1418 >         tracePhoton(&rayOut);      
1419 >      }
1420     }
1421    
1422     return 0;
# Line 1383 | Line 1448 | static int pattexPhotonScatter (OBJREC *mat, RAY *rayI
1448  
1449  
1450  
1451 < #if 0
1452 <   static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1453 <   /* Generate new photon ray for BSDF modifier and recurse. */
1454 <   {
1455 <      int      hitFront;
1456 <      SDError  err;
1457 <      FVECT        upvec;
1393 <      MFUNC        *mf;
1394 <      BSDFDAT   nd;
1395 <      RAY      rayOut;
1451 > static int setbrdfunc(BRDFDAT *bd)
1452 > /* Set up brdf function and variables; ripped off from m_brdf.c */
1453 > {
1454 >   FVECT v;
1455 >  
1456 >   if (setfunc(bd -> mp, bd -> pr) == 0)
1457 >      return 0;
1458  
1459 <      /* Following code adapted from m_bsdf() */
1460 <      /* Check arguments */
1461 <      if (mat -> oargs.nsargs < 6 || mat -> oargs.nfargs > 9 ||
1462 <          mat -> oargs.nfargs % 3)
1463 <         objerror(mat, USER, "bad # arguments");
1464 <        
1465 <      hitFront = (rayIn -> rod > 0);
1459 >   /* (Re)Assign func variables */
1460 >   multv3(v, bd -> pnorm, funcxf.xfm);
1461 >   varset("NxP", '=', v [0] / funcxf.sca);
1462 >   varset("NyP", '=', v [1] / funcxf.sca);
1463 >   varset("NzP", '=', v [2] / funcxf.sca);
1464 >   varset("RdotP", '=',
1465 >          bd -> pdot <= -1. ? -1. : bd -> pdot >= 1. ? 1. : bd -> pdot);
1466 >   varset("CrP", '=', colval(bd -> mcolor, RED));
1467 >   varset("CgP", '=', colval(bd -> mcolor, GRN));
1468 >   varset("CbP", '=', colval(bd -> mcolor, BLU));
1469 >  
1470 >   return 1;
1471 > }
1472  
1405      /* Load cal file */
1406      mf = getfunc(mat, 5, 0x1d, 1);
1407      
1408      /* Get thickness */
1409      nd.thick = evalue(mf -> ep [0]);
1410      if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
1411         nd.thick = .0;
1412        
1413      if (nd.thick != .0 || (!hitFront && !backvis)) {
1414         /* Proxy geometry present, so use it instead and transfer ray */
1415         photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1416         tracePhoton(&rayOut);
1417        
1418         return 0;
1419      }
1473  
1421      /* Get BSDF data */
1422      nd.sd = loadBSDF(mat -> oargs.sarg [1]);
1423      
1424      /* Diffuse reflectance */
1425      if (hitFront) {
1426         if (mat -> oargs.nfargs < 3)
1427            setcolor(nd.rdiff, .0, .0, .0);
1428         else setcolor(nd.rdiff, mat -> oargs.farg [0], mat -> oargs.farg [1],
1429                       mat -> oargs.farg [2]);
1430      }    
1431      else if (mat -> oargs.nfargs < 6) {
1432         /* Check for absorbing backside */
1433         if (!backvis && !nd.sd -> rb && !nd.sd -> tf) {
1434            SDfreeCache(nd.sd);                    
1435            return 0;
1436         }
1437        
1438         setcolor(nd.rdiff, .0, .0, .0);
1439      }
1440      else setcolor(nd.rdiff, mat -> oargs.farg [3], mat -> oargs.farg [4],
1441                    mat -> oargs.farg [5]);
1474  
1475 <      /* Diffuse transmittance */
1476 <      if (mat -> oargs.nfargs < 9)
1477 <         setcolor(nd.tdiff, .0, .0, .0);
1478 <      else setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7],
1479 <                    mat -> oargs.farg [8]);
1480 <                  
1481 <      nd.mp = mat;
1482 <      nd.pr = rayIn;
1475 > static int brdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1476 > /* Generate new photon ray for BRTDfunc material and recurse. Only ideal
1477 >   reflection and transmission are sampled for the specular componentent. */
1478 > {
1479 >   int      hitfront = 1, hastexture, i;
1480 >   BRDFDAT  nd;
1481 >   RAY      rayOut;
1482 >   COLOR    rspecCol, tspecCol;
1483 >   double   prDiff, ptDiff, prSpec, ptSpec, albedo, xi;
1484 >   MFUNC    *mf;
1485 >   FVECT    bnorm;
1486 >
1487 >   /* Check argz */
1488 >   if (mat -> oargs.nsargs < 10 || mat -> oargs.nfargs < 9)
1489 >      objerror(mat, USER, "bad # arguments");
1490        
1491 <      /* Get modifiers */
1492 <      raytexture(rayIn, mat -> omod);
1493 <      
1494 <      /* Modify diffuse values */
1495 <      multcolor(nd.rdiff, rayIn -> pcol);
1496 <      multcolor(nd.tdiff, rayIn -> pcol);
1497 <        
1498 <      /* Get up vector & xform to world coords */
1499 <      upvec [0] = evalue(mf -> ep [1]);
1500 <      upvec [1] = evalue(mf -> ep [2]);
1501 <      upvec [2] = evalue(mf -> ep [3]);
1502 <      
1503 <      if (mf -> fxp != &unitxf) {
1504 <         multv3(upvec, upvec, mf -> fxp -> xfm);
1505 <         nd.thick *= mf -> fxp -> sca;
1491 >   nd.mp = mat;
1492 >   nd.pr = rayIn;
1493 >   /* Dummiez */
1494 >   nd.rspec = nd.tspec = 1.0;
1495 >   nd.trans = 0.5;
1496 >
1497 >   /* Diffuz reflektanz */
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 >   /* Diffuz tranzmittanz */
1505 >   setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7],
1506 >            mat -> oargs.farg [8]);
1507 >
1508 >   /* Get modz */
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 valuz */
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 <      if (rayIn -> rox) {
1529 <         multv3(upvec, upvec, rayIn -> rox -> f.xfm);
1530 <         nd.thick *= rayIn -> rox -> f.sca;
1528 >      hitfront = 0;
1529 >   }
1530 >  
1531 >   /* Get pattern kolour, modify diffuz valuz */
1532 >   copycolor(nd.mcolor, rayIn -> pcol);
1533 >   multcolor(nd.rdiff, nd.mcolor);
1534 >   multcolor(nd.tdiff, nd.mcolor);
1535 >
1536 >   /* Load cal file, evaluate spekula refl/tranz varz */
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 probz */
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 hitz if diffuz komponent */
1557 >   if (prDiff > FTINY || ptDiff > FTINY)
1558 >      addPhotons(rayIn);
1559 >
1560 >   /* Stochastically sample absorption or scattering evenz */
1561 >   if ((xi = pmapRandom(rouletteState)) > albedo)
1562 >      /* Absorbed */
1563 >      return 0;
1564 >
1565 >   if (xi > (albedo -= prSpec)) {
1566 >      /* Ideal spekula reflekzion */
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 >      /* Ideal spekula tranzmission */
1573 >      photonRay(rayIn, &rayOut, PMAP_SPECTRANS, tspecCol);
1574 >      if (hastexture) {
1575 >         /* Perturb direkzion */
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        }
1582 +      else VCOPY(rayOut.rdir, rayIn -> rdir);
1583 +   }
1584 +   else if (xi > (albedo -= prDiff)) {
1585 +      /* Diffuz reflekzion */
1586 +      if (!hitfront)
1587 +         flipsurface(rayIn);
1588 +      photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.mcolor);
1589 +      diffPhotonScatter(nd.pnorm, &rayOut);
1590 +   }
1591 +   else {
1592 +      /* Diffuz tranzmission */
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 +   tracePhoton(&rayOut);
1603 +   return 0;
1604 + }
1605 +
1606 +
1607 +
1608 + int brdf2PhotonScatter (OBJREC *mat, RAY *rayIn)
1609 + /* Generate new photon ray for procedural or data driven BRDF material and
1610 +   recurse. Only diffuse reflection and transmission are sampled. */
1611 + {
1612 +   BRDFDAT  nd;
1613 +   RAY      rayOut;
1614 +   double   dtmp, prDiff, ptDiff, albedo, xi;
1615 +   MFUNC    *mf;
1616 +   FVECT    bnorm;
1617 +
1618 +   /* Check argz */
1619 +   if (mat -> oargs.nsargs < (hasdata(mat -> otype) ? 4 : 2) ||
1620 +       mat -> oargs.nfargs < (mat -> otype == MAT_TFUNC ||
1621 +                              mat -> otype == MAT_TDATA ? 6 : 4))
1622 +      objerror(mat, USER, "bad # arguments");
1623        
1624 <      /* Perturb normal */
1625 <      raynormal(nd.pnorm, rayIn);
1626 <      
1627 <      /* Xform incident dir to local BSDF coords */
1628 <      err = SDcompXform(nd.toloc, nd.pnorm, upvec);
1479 <      
1480 <      if (!err) {
1481 <         nd.vray [0] = -rayIn -> rdir [0];
1482 <         nd.vray [1] = -rayIn -> rdir [1];
1483 <         nd.vray [2] = -rayIn -> rdir [2];
1484 <         err = SDmapDir(nd.vray, nd.toloc, nd.vray);
1485 <      }
1486 <      
1487 <      if (!err)
1488 <         err = SDinvXform(nd.fromloc, nd.toloc);
1489 <        
1490 <      if (err) {
1491 <         objerror(mat, WARNING, "Illegal orientation vector");
1624 >   if (rayIn -> rod < 0.0) {
1625 >      /* Hit backside; reorient if visible, else transfer photon */
1626 >      if (!backvis) {
1627 >         photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1628 >         tracePhoton(&rayOut);
1629           return 0;
1630        }
1631        
1632 <      /* Determine BSDF resolution */
1633 <      err = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin + SDqueryMax, nd.sd);
1634 <      
1635 <      if (err)
1499 <         objerror(mat, USER, transSDError(err));
1500 <        
1501 <      nd.sr_vpsa [0] = sqrt(nd.sr_vpsa [0]);
1502 <      nd.sr_vpsa [1] = sqrt(nd.sr_vpsa [1]);
1632 >      raytexture(rayIn, mat -> omod);
1633 >      flipsurface(rayIn);
1634 >   }
1635 >   else raytexture(rayIn, mat -> omod);
1636  
1637 <      /* Orient perturbed normal towards incident side */
1638 <      if (!hitFront) {                  
1639 <         nd.pnorm [0] = -nd.pnorm [0];
1640 <         nd.pnorm [1] = -nd.pnorm [1];
1641 <         nd.pnorm [2] = -nd.pnorm [2];
1642 <      }
1643 <      
1644 <      /* Following code adapted from SDsampBSDF() */
1645 <      {
1646 <         SDSpectralDF   *rdf, *tdf;
1647 <         SDValue        bsdfVal;
1648 <         double         xi, rhoDiff = 0;
1649 <         float          coef [SDmaxCh];
1650 <         int            i, j, n, nr;
1651 <         SDComponent       *sdc;
1652 <         const SDCDst   **cdarr = NULL;
1653 <        
1654 <         /* Get diffuse albedo (?) */
1655 <         if (hitFront) {
1656 <            bsdfVal = nd.sd -> rLambFront;
1657 <            rdf = nd.sd -> rf;
1658 <            tdf = nd.sd -> tf ? nd.sd -> tf : nd.sd -> tb;
1659 <         }
1660 <         else {
1661 <            bsdfVal = nd.sd -> rLambBack;
1662 <            rdf = nd.sd -> rb;
1663 <            tdf = nd.sd -> tb ? nd.sd -> tb : nd.sd -> tf;
1664 <         }
1665 <        
1666 <         rhoDiff = bsdfVal.cieY;
1667 <         bsdfVal.cieY += nd.sd -> tLamb.cieY;
1668 <        
1669 <         /* Allocate non-diffuse sampling */
1670 <         i = nr = rdf ? rdf -> ncomp : 0;
1671 <         j = tdf ? tdf -> ncomp : 0;
1672 <         n = i + j;
1673 <        
1674 <         if (n > 0 && !(cdarr = (const SDCDst**)malloc(n * sizeof(SDCDst*))))
1675 <            objerror(mat, USER, transSDError(SDEmemory));
1676 <            
1544 <         while (j-- > 0) {
1545 <            /* Sum up non-diffuse transmittance */
1546 <            cdarr [i + j] = (*tdf -> comp [j].func -> getCDist)(nd.vray, &tdf -> comp [j]);
1547 <            
1548 <            if (!cdarr [i + j])
1549 <               cdarr [i + j] = &SDemptyCD;
1550 <            else bsdfVal.cieY += cdarr [i + j] -> cTotal;
1551 <         }
1552 <        
1553 <         while (i-- > 0) {
1554 <            /* Sum up non-diffuse reflectance */
1555 <            cdarr [i] = (*rdf -> comp [i].func -> getCDist)(nd.vray, &rdf -> comp [i]);
1556 <            
1557 <            if (!cdarr [i])
1558 <               cdarr [i] = &SDemptyCD;
1559 <            else bsdfVal.cieY += cdarr [i] -> cTotal;
1560 <         }
1561 <        
1562 <         if (bsdfVal.cieY <= FTINY) {
1563 <            /* Don't bother sampling, just absorb photon */
1564 <            if (cdarr)
1565 <               free(cdarr);
1566 <            return 0;
1567 <         }      
1568 <        
1569 <         /* Insert direct and indirect photon hits if diffuse component */
1570 <         if (rhoDiff > FTINY || nd.sd -> tLamb.cieY > FTINY)
1571 <            addPhotons(rayIn);  
1572 <            
1573 <         xi = pmapRandom(rouletteState);
1574 <        
1575 <         if ((xi -= rhoDiff) <= 0) {
1576 <            /* Diffuse reflection */
1577 <            photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.rdiff);
1578 <            diffPhotonScatter(nd.pnorm, &rayOut);
1579 <         }
1580 <         else if ((xi -= nd.sd -> tLamb.cieY) <= 0) {
1581 <            /* Diffuse transmission */
1582 <            flipsurface(rayIn);
1583 <            photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.tdiff);
1584 <            bsdfVal.spec = nd.sd -> tLamb.spec;
1585 <            diffPhotonScatter(nd.pnorm, &rayOut);
1586 <         }
1587 <         else {
1588 <            int rayOutType;
1589 <            COLOR bsdfRGB;
1590 <              
1591 <            /* Non-diffuse CDF inversion (?) */
1592 <            for (i = 0; i < n && (xi -= cdarr [i] -> cTotal) > 0; i++);
1593 <            
1594 <            if (i >= n) {
1595 <               /* Absorbed -- photon went Deer Hunter */
1596 <               if (cdarr)
1597 <                  free(cdarr);
1598 <               return 0;
1599 <            }
1637 >   nd.mp = mat;
1638 >   nd.pr = rayIn;
1639 >  
1640 >   /* Material kolour */
1641 >   setcolor(nd.mcolor, mat -> oargs.farg [0], mat -> oargs.farg [1],
1642 >            mat -> oargs.farg [2]);
1643 >   /* Spekula komponent */
1644 >   nd.rspec = mat -> oargs.farg [3];
1645 >  
1646 >   /* Tranzmittanz */
1647 >   if (mat -> otype == MAT_TFUNC || mat -> otype == MAT_TDATA) {
1648 >      nd.trans = mat -> oargs.farg [4] * (1.0 - nd.rspec);
1649 >      nd.tspec = nd.trans * mat -> oargs.farg [5];
1650 >      dtmp = nd.trans - nd.tspec;
1651 >      setcolor(nd.tdiff, dtmp, dtmp, dtmp);
1652 >   }
1653 >   else {
1654 >      nd.tspec = nd.trans = 0.0;
1655 >      setcolor(nd.tdiff, 0.0, 0.0, 0.0);
1656 >   }
1657 >  
1658 >   /* Reflektanz */
1659 >   dtmp = 1.0 - nd.trans - nd.rspec;
1660 >   setcolor(nd.rdiff, dtmp, dtmp, dtmp);
1661 >   /* Perturb normal */
1662 >   nd.pdot = raynormal(nd.pnorm, rayIn);
1663 >   /* Modify material kolour */
1664 >   multcolor(nd.mcolor, rayIn -> pcol);
1665 >   multcolor(nd.rdiff, nd.mcolor);
1666 >   multcolor(nd.tdiff, nd.mcolor);
1667 >  
1668 >   /* Load auxiliary filez */
1669 >   if (hasdata(mat -> otype)) {
1670 >      nd.dp = getdata(mat -> oargs.sarg [1]);
1671 >      getfunc(mat, 2, 0, 0);
1672 >   }
1673 >   else {
1674 >      nd.dp = NULL;
1675 >      getfunc(mat, 1, 0, 0);
1676 >   }
1677  
1678 <            if (i < nr) {
1679 <               /* Non-diffuse reflection */
1680 <               sdc = &rdf -> comp [i];
1681 <               rayOutType = PMAP_SPECREFL;
1605 <            }
1606 <            else {
1607 <               /* Non-diffuse transmission */
1608 <               sdc = &tdf -> comp [i - nr];
1609 <               rayOutType = PMAP_SPECTRANS;
1610 <            }
1611 <            
1612 <            /* Generate non-diff sample dir */
1613 <            VCOPY(rayOut.rdir, nd.vray);
1614 <            err = (*sdc -> func -> sampCDist)
1615 <                  (rayOut.rdir, pmapRandom(scatterState), cdarr [i]);              
1616 <            if (err)
1617 <               objerror(mat, USER, transSDError(SDEinternal));
1678 >   /* Set up probz */
1679 >   prDiff = colorAvg(nd.rdiff);
1680 >   ptDiff = colorAvg(nd.tdiff);
1681 >   albedo = prDiff + ptDiff;
1682  
1683 <            /* Get colour */
1684 <            j = (*sdc -> func -> getBSDFs)(coef, rayOut.rdir, nd.vray, sdc);
1685 <            
1622 <            if (j <= 0) {
1623 <               sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error",
1624 <                       nd.sd -> name);
1625 <               objerror(mat, USER, transSDError(SDEinternal));
1626 <            }
1627 <            
1628 <            bsdfVal.spec = sdc -> cspec [0];
1629 <            rhoDiff = coef [0];
1630 <            
1631 <            while (--j) {
1632 <               c_cmix(&bsdfVal.spec, rhoDiff, &bsdfVal.spec, coef [j],
1633 <                      &sdc -> cspec [j]);
1634 <               rhoDiff += coef [j];
1635 <            }
1636 <            
1637 <            /* ? */
1638 <            c_ccvt(&bsdfVal.spec, C_CSXY + C_CSSPEC);
1639 <            ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1640 <            
1641 <            /* Xform outgoing dir to world coords */
1642 <            if ((err = SDmapDir(rayOut.rdir, nd.fromloc, rayOut.rdir))) {
1643 <               objerror(mat, USER, transSDError(err));
1644 <               return 0;
1645 <            }
1646 <            
1647 <            photonRay(rayIn, &rayOut, rayOutType, bsdfRGB);
1648 <         }
1649 <        
1650 <         if (cdarr)
1651 <            free(cdarr);
1652 <      }
1653 <                          
1654 <      /* Clean up BSDF */
1655 <      SDfreeCache(nd.sd);
1683 >   /* Insert direct and indirect photon hitz if diffuz komponent */
1684 >   if (prDiff > FTINY || ptDiff > FTINY)
1685 >      addPhotons(rayIn);
1686  
1687 <      tracePhoton(&rayOut);
1687 >   /* Stochastically sample absorption or scattering evenz */
1688 >   if ((xi = pmapRandom(rouletteState)) > albedo)
1689 >      /* Absorbed */
1690        return 0;
1691 +
1692 +   if (xi > (albedo -= prDiff)) {
1693 +      /* Diffuz reflekzion */
1694 +      photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.rdiff);
1695 +      diffPhotonScatter(nd.pnorm, &rayOut);
1696     }
1697 < #else
1697 >   else {
1698 >      /* Diffuz tranzmission */
1699 >      flipsurface(rayIn);
1700 >      photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.tdiff);
1701 >      bnorm [0] = -nd.pnorm [0];
1702 >      bnorm [1] = -nd.pnorm [1];
1703 >      bnorm [2] = -nd.pnorm [2];
1704 >      diffPhotonScatter(bnorm, &rayOut);
1705 >   }
1706  
1707 +   tracePhoton(&rayOut);
1708 +   return 0;
1709 + }
1710 +
1711 +
1712 +
1713 + /*
1714 +   ==================================================================
1715 +   The following code is
1716 +   (c) Lucerne University of Applied Sciences and Arts,
1717 +   supported by the Swiss National Science Foundation (SNSF, #147053)
1718 +   ==================================================================
1719 + */
1720 +
1721   static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1722   /* Generate new photon ray for BSDF modifier and recurse. */
1723   {
1724 +   int      hasthick = (mat->otype == MAT_BSDF);
1725     int      hitFront;
1726     SDError  err;
1727     SDValue  bsdfVal;
# Line 1670 | Line 1730 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1730     BSDFDAT      nd;
1731     RAY      rayOut;
1732     COLOR    bsdfRGB;
1733 +   int      transmitted;
1734     double   prDiff, ptDiff, prDiffSD, ptDiffSD, prSpecSD, ptSpecSD,
1735 <            albedo, xi, xi2;
1736 <   const double patAlb = colorAvg(rayIn -> pcol);
1735 >            albedo, xi;
1736 >   const double patAlb = bright(rayIn -> pcol);
1737    
1738     /* Following code adapted from m_bsdf() */
1739     /* Check arguments */
1740 <   if (mat -> oargs.nsargs < 6 || mat -> oargs.nfargs > 9 ||
1740 >   if (mat -> oargs.nsargs < hasthick+5 || mat -> oargs.nfargs > 9 ||
1741         mat -> oargs.nfargs % 3)
1742        objerror(mat, USER, "bad # arguments");
1743        
1744 <        hitFront = (rayIn -> rod > 0);
1744 >   hitFront = (rayIn -> rod > 0);
1745  
1746 <        /* Load cal file */
1747 <        mf = getfunc(mat, 5, 0x1d, 1);
1748 <        
1749 <        /* Get thickness */
1750 <        nd.thick = evalue(mf -> ep [0]);
1751 <        if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
1752 <                nd.thick = .0;
1753 <                
1754 <   if (nd.thick != .0 || (!hitFront && !backvis)) {
1694 <      /* Proxy geometry present, so use it instead and transfer ray */
1695 <      photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1696 <      tracePhoton(&rayOut);
1697 <      
1698 <      return 0;
1746 >   /* Load cal file */
1747 >   mf = hasthick ? getfunc(mat, 5, 0x1d, 1) : getfunc(mat, 4, 0xe, 1);
1748 >
1749 >   /* Get thickness */
1750 >   nd.thick = 0;
1751 >   if (hasthick) {
1752 >   nd.thick = evalue(mf -> ep [0]);
1753 >   if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
1754 >      nd.thick = .0;
1755     }
1756  
1757     /* Get BSDF data */
1758 <   nd.sd = loadBSDF(mat -> oargs.sarg [1]);
1758 >   nd.sd = loadBSDF(mat -> oargs.sarg [hasthick]);
1759    
1760     /* Extra diffuse reflectance from material def */
1761     if (hitFront) {
# Line 1709 | Line 1765 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1765                      mat -> oargs.farg [2]);
1766     }    
1767     else if (mat -> oargs.nfargs < 6) {
1768 <        /* Check for absorbing backside */
1769 <        if (!backvis && !nd.sd -> rb && !nd.sd -> tf) {
1770 <           SDfreeCache(nd.sd);                    
1771 <           return 0;
1768 >      /* Check for absorbing backside */
1769 >      if (!backvis && !nd.sd -> rb && !nd.sd -> tf) {
1770 >         SDfreeCache(nd.sd);
1771 >         return 0;
1772        }
1773        
1774        setcolor(nd.rdiff, .0, .0, .0);
# Line 1720 | Line 1776 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1776     else setcolor(nd.rdiff, mat -> oargs.farg [3], mat -> oargs.farg [4],
1777                   mat -> oargs.farg [5]);
1778  
1779 <        /* Extra diffuse transmittance from material def */
1780 <        if (mat -> oargs.nfargs < 9)
1781 <           setcolor(nd.tdiff, .0, .0, .0);
1779 >   /* Extra diffuse transmittance from material def */
1780 >   if (mat -> oargs.nfargs < 9)
1781 >      setcolor(nd.tdiff, .0, .0, .0);
1782     else setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7],
1783                   mat -> oargs.farg [8]);
1784                
1785     nd.mp = mat;
1786     nd.pr = rayIn;
1787 <        
1787 >
1788     /* Get modifiers */
1789     raytexture(rayIn, mat -> omod);
1790    
1791     /* Modify diffuse values */
1792     multcolor(nd.rdiff, rayIn -> pcol);
1793     multcolor(nd.tdiff, rayIn -> pcol);
1794 <                
1794 >
1795     /* Get up vector & xform to world coords */
1796 <   upvec [0] = evalue(mf -> ep [1]);
1797 <   upvec [1] = evalue(mf -> ep [2]);
1798 <   upvec [2] = evalue(mf -> ep [3]);
1796 >   upvec [0] = evalue(mf -> ep [hasthick+0]);
1797 >   upvec [1] = evalue(mf -> ep [hasthick+1]);
1798 >   upvec [2] = evalue(mf -> ep [hasthick+2]);
1799    
1800     if (mf -> fxp != &unitxf) {
1801        multv3(upvec, upvec, mf -> fxp -> xfm);
# Line 1773 | Line 1829 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1829     }
1830    
1831     /* Determine BSDF resolution */
1832 <   err = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin + SDqueryMax, nd.sd);
1832 >   err = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL,
1833 >                    SDqueryMin + SDqueryMax, nd.sd);
1834    
1835     if (err)
1836        objerror(mat, USER, transSDError(err));
# Line 1782 | Line 1839 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1839     nd.sr_vpsa [1] = sqrt(nd.sr_vpsa [1]);
1840  
1841     /* Orient perturbed normal towards incident side */
1842 <   if (!hitFront) {                    
1842 >   if (!hitFront) {
1843        nd.pnorm [0] = -nd.pnorm [0];
1844        nd.pnorm [1] = -nd.pnorm [1];
1845        nd.pnorm [2] = -nd.pnorm [2];
# Line 1810 | Line 1867 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1867     if (prDiff + ptDiff + prDiffSD + ptDiffSD > FTINY)
1868        addPhotons(rayIn);        
1869  
1870 <   xi = xi2 = pmapRandom(rouletteState);
1870 >   xi = pmapRandom(rouletteState);
1871        
1872     if (xi > albedo)
1873        /* Absorbtion */
1874        return 0;
1875    
1876 +   transmitted = 0;
1877 +
1878     if ((xi -= prDiff) <= 0) {
1879        /* Diffuse reflection (extra component in material def) */
1880        photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.rdiff);
# Line 1824 | Line 1883 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1883    
1884     else if ((xi -= ptDiff) <= 0) {
1885        /* Diffuse transmission (extra component in material def) */
1827      flipsurface(rayIn);
1886        photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.tdiff);
1887 <      diffPhotonScatter(nd.pnorm, &rayOut);      
1887 >      diffPhotonScatter(nd.pnorm, &rayOut);
1888 >      transmitted = 1;
1889     }
1890 <  
1890 >  
1891     else {   /* Sample SDF */
1892        if ((xi -= prDiffSD) <= 0) {
1893           /* Diffuse SDF reflection (constant component) */
1894 <         if ((err = SDsampBSDF(&bsdfVal, nd.vray, xi2,
1894 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1895                                 SDsampDf | SDsampR, nd.sd)))
1896              objerror(mat, USER, transSDError(err));
1897          
# Line 1844 | Line 1903 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1903  
1904        else if ((xi -= ptDiffSD) <= 0) {
1905           /* Diffuse SDF transmission (constant component) */
1906 <         if ((err = SDsampBSDF(&bsdfVal, nd.vray, xi2,
1906 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1907                                 SDsampDf | SDsampT, nd.sd)))
1908              objerror(mat, USER, transSDError(err));
1909          
# Line 1852 | Line 1911 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1911           ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1912           multcolor(bsdfRGB, rayIn -> pcol);
1913           addcolor(bsdfRGB, nd.tdiff);      
1855         flipsurface(rayIn);  /* Necessary? */
1914           photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, bsdfRGB);
1915 +         transmitted = 1;
1916        }
1917  
1918        else if ((xi -= prSpecSD) <= 0) {
1919           /* Non-diffuse ("specular") SDF reflection */
1920 <         if ((err = SDsampBSDF(&bsdfVal, nd.vray, xi2,
1920 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1921                                 SDsampSp | SDsampR, nd.sd)))
1922              objerror(mat, USER, transSDError(err));
1923          
# Line 1868 | Line 1927 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1927        
1928        else {
1929           /* Non-diffuse ("specular") SDF transmission */
1930 <         if ((err = SDsampBSDF(&bsdfVal, nd.vray, xi2,
1930 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1931                                 SDsampSp | SDsampT, nd.sd)))
1932              objerror(mat, USER, transSDError(err));
1933  
1934           /* Apply pattern to spectral component */
1935           ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1936           multcolor(bsdfRGB, rayIn -> pcol);
1878         flipsurface(rayIn);  /* Necessary? */
1937           photonRay(rayIn, &rayOut, PMAP_SPECTRANS, bsdfRGB);
1938 +         transmitted = 1;
1939        }      
1940        
1941        /* Xform outgoing dir to world coords */
# Line 1889 | Line 1948 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1948     /* Clean up */
1949     SDfreeCache(nd.sd);
1950  
1951 +   /* Offset outgoing photon origin by thickness to bypass proxy geometry */
1952 +   if (transmitted && nd.thick != 0)
1953 +      VSUM(rayOut.rorg, rayOut.rorg, rayIn -> ron, -nd.thick);
1954 +
1955     tracePhoton(&rayOut);
1956     return 0;
1957   }
1895 #endif
1958  
1959  
1960  
1961   static int lightPhotonScatter (OBJREC* mat, RAY* ray)
1962 < /* Light sources doan' reflect */
1962 > /* Light sources doan' reflect, mang */
1963   {
1964     return 0;
1965   }
# Line 1908 | Line 1970 | void initPhotonScatterFuncs ()
1970   /* Init photonScatter[] dispatch table */
1971   {
1972     int i;
1973 <  
1973 >
1974 >   /* Catch-all for inconsistencies */
1975     for (i = 0; i < NUMOTYPE; i++)
1976        photonScatter [i] = o_default;
1977 <      
1977 >
1978     photonScatter [MAT_LIGHT] = photonScatter [MAT_ILLUM] =
1979        photonScatter [MAT_GLOW] = photonScatter [MAT_SPOT] =
1980           lightPhotonScatter;
1981 <        
1981 >
1982     photonScatter [MAT_PLASTIC] = photonScatter [MAT_METAL] =
1983        photonScatter [MAT_TRANS] = normalPhotonScatter;
1984        
# Line 1924 | Line 1987 | void initPhotonScatterFuncs ()
1987        
1988     photonScatter [MAT_DIELECTRIC] = photonScatter [MAT_INTERFACE] =
1989        dielectricPhotonScatter;
1990 <      
1990 >
1991     photonScatter [MAT_MIST] = mistPhotonScatter;
1992     photonScatter [MAT_GLASS] = glassPhotonScatter;
1993     photonScatter [MAT_CLIP] = clipPhotonScatter;
# Line 1932 | Line 1995 | void initPhotonScatterFuncs ()
1995     photonScatter [MIX_FUNC] = mx_funcPhotonScatter;
1996     photonScatter [MIX_DATA] = mx_dataPhotonScatter;
1997     photonScatter [MIX_PICT]= mx_pdataPhotonScatter;
1998 <  
1998 >
1999     photonScatter [PAT_BDATA] = photonScatter [PAT_CDATA] =
2000        photonScatter [PAT_BFUNC] = photonScatter [PAT_CFUNC] =
2001           photonScatter [PAT_CPICT] = photonScatter [TEX_FUNC] =
2002              photonScatter [TEX_DATA] = pattexPhotonScatter;
2003 <            
2003 >
2004     photonScatter [MOD_ALIAS] = aliasPhotonScatter;
2005 <   photonScatter [MAT_BSDF] = bsdfPhotonScatter;
2005 >   photonScatter [MAT_BRTDF] = brdfPhotonScatter;
2006 >  
2007 >   photonScatter [MAT_PFUNC] = photonScatter [MAT_MFUNC] =
2008 >      photonScatter [MAT_PDATA] = photonScatter [MAT_MDATA] =
2009 >         photonScatter [MAT_TFUNC] = photonScatter [MAT_TDATA] =
2010 >            brdf2PhotonScatter;
2011 >
2012 >   photonScatter [MAT_BSDF] = photonScatter [MAT_ABSDF] =
2013 >      bsdfPhotonScatter;
2014   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines