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.4 by greg, Wed May 20 15:59:44 2015 UTC vs.
Revision 2.21 by rschregle, Thu Dec 6 20:00:35 2018 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines