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.15 by rschregle, Tue Mar 20 19:55:33 2018 UTC vs.
Revision 2.22 by rschregle, Mon Jan 13 17:12:19 2020 UTC

# Line 34 | Line 34 | static const char RCSid[] = "$Id$";
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 46 | 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 55 | 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 131 | Line 145 | void photonRay (const RAY *rayIn, RAY *rayOut,
145   }
146  
147  
134
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 */
152 >      /* Add direct photon at primary hitpoint */
153        newPhoton(directPmap, r);
154     else {
155 <      /* Add global or precomputed photon map at indirect hitpoint */
155 >      /* Add global or precomputed photon at indirect hitpoint */
156        newPhoton(preCompPmap ? preCompPmap : globalPmap, r);
157  
158        /* Store caustic photon if specular flag set */
# Line 206 | Line 219 | static int isoSpecPhotonScatter (NORMDAT *nd, RAY *ray
219     int      niter, i = 0;
220    
221     /* Set up sample coordinates */  
222 <   getperpendicular(u, nd -> pnorm, 1);  
222 >   getperpendicular(u, nd -> pnorm, 1);
223     fcross(v, nd -> pnorm, u);
224    
225     if (nd -> specfl & SP_REFL) {
# Line 240 | 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 261 | 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;
# Line 305 | 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 421 | 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 466 | 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]);
481 <          
482 <   if ((errno == EDOM) | (errno == ERANGE)) {
483 <      objerror(np->mp, WARNING, "compute error");
484 <      np->specfl |= SP_BADU;
485 <      return;
486 <   }
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");
495 <           np->specfl |= SP_BADU;
496 <           return;
497 <   }
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 519 | 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 555 | Line 568 | static int anisoSpecPhotonScatter (ANISODAT *nd, RAY *
568           }
569        }
570        
571 <      return 0;  
571 >      return 0;
572     }
573    
574     else {
# Line 573 | 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 602 | 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;
606   nd.mp = objptr(rayIn -> ro -> omod);
620    
621     /* get material color */
622     copycolor(nd.mcolor, mat -> oargs.farg);
# Line 647 | 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 750 | 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 760 | 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 838 | 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 857 | 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 897 | 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 979 | 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) {
# Line 996 | 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 <      
1001 <   if (aliasObj < 0)
1002 <      objerror(mat, USER, "bad reference");
1003 <      
1004 <   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 1036 | 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 1232 | 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 1297 | 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 1359 | 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 1417 | Line 1448 | static int pattexPhotonScatter (OBJREC *mat, RAY *rayI
1448  
1449  
1450  
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 +   /* (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 +
1473 +
1474 +
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 +   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 +      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 +         else VCOPY(rayOut.rdir, rayIn -> rdir);
1582 +      }
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 +   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 +      raytexture(rayIn, mat -> omod);
1633 +      flipsurface(rayIn);
1634 +   }
1635 +   else raytexture(rayIn, mat -> omod);
1636 +
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 +   /* Set up probz */
1679 +   prDiff = colorAvg(nd.rdiff);
1680 +   ptDiff = colorAvg(nd.tdiff);
1681 +   albedo = prDiff + ptDiff;
1682 +
1683 +   /* Insert direct and indirect photon hitz if diffuz komponent */
1684 +   if (prDiff > FTINY || ptDiff > FTINY)
1685 +      addPhotons(rayIn);
1686 +
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 {
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 < */  
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 1443 | Line 1737 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
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);
1454 <        
1455 <        /* Get thickness */
1456 <        nd.thick = evalue(mf -> ep [0]);
1457 <        if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
1458 <                nd.thick = .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 1468 | 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 1479 | 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 1532 | 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 1541 | 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 1585 | Line 1883 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1883    
1884     else if ((xi -= ptDiff) <= 0) {
1885        /* Diffuse transmission (extra component in material def) */
1588      flipsurface(rayIn);
1589      nd.thick = -nd.thick;
1886        photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.tdiff);
1887        diffPhotonScatter(nd.pnorm, &rayOut);
1888        transmitted = 1;
# Line 1615 | 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);      
1618         flipsurface(rayIn);  /* Necessary? */
1619         nd.thick = -nd.thick;
1914           photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, bsdfRGB);
1915           transmitted = 1;
1916        }
# Line 1640 | Line 1934 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1934           /* Apply pattern to spectral component */
1935           ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1936           multcolor(bsdfRGB, rayIn -> pcol);
1643         flipsurface(rayIn);  /* Necessary? */
1644         nd.thick = -nd.thick;
1937           photonRay(rayIn, &rayOut, PMAP_SPECTRANS, bsdfRGB);
1938           transmitted = 1;
1939        }      
# Line 1656 | Line 1948 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1948     /* Clean up */
1949     SDfreeCache(nd.sd);
1950  
1951 <   /* Need to offset ray origin to get past detail geometry? */
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  
# Line 1678 | 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 1694 | 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 1702 | 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