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.19 by rschregle, Tue Dec 4 21:58:46 2018 UTC vs.
Revision 2.21 by rschregle, Thu Dec 6 20:00:35 2018 UTC

# 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;
# Line 69 | Line 69 | typedef struct {
69     double   tspec;
70     FVECT    pnorm;
71     double   pdot;
72 < }  BRDFDAT;
72 > } BRDFDAT;
73  
74   typedef struct {
75     OBJREC   *mp;
# Line 319 | 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 480 | 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);
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 <      np->u[i] = evalue(mf->ep[i]);
495 >      nd -> u [i] = evalue(mf -> ep [i]);
496    
497 <   if ((errno == EDOM) | (errno == ERANGE)) {
498 <      objerror(np->mp, WARNING, "compute error");
498 <      np->specfl |= SP_BADU;
499 <      return;
500 <   }
501 <  
502 <   if (mf->fxp != &unitxf)
503 <      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);
500 >   if (mf -> fxp != &unitxf)
501 >      multv3(nd -> u, nd -> u, mf -> fxp -> xfm);
502  
503 <   if (normalize(np->v) == 0.0) {
508 <      objerror(np->mp, WARNING, "illegal orientation vector");
509 <      np->specfl |= SP_BADU;
510 <      return;
511 <   }
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 616 | 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;
620   nd.mp = objptr(rayIn -> ro -> omod);
621    
622     /* get material color */
623     copycolor(nd.mcolor, mat -> oargs.farg);
# Line 661 | 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 1033 | Line 1033 | static int aliasPhotonScatter (OBJREC *mat, RAY *rayIn
1033     /* Replacement scattering routine */
1034     photonScatter [aliasRec.otype] (&aliasRec, rayIn);
1035  
1036 #if 0
1036     /* Avoid potential memory leak? */
1037     if (aliasRec.os != aliasPtr -> os) {
1038 <      if (aliasObj -> os)
1039 <         free_os(aliasObj);
1038 >      if (aliasPtr -> os)
1039 >         free_os(aliasPtr);
1040        aliasPtr -> os = aliasRec.os;
1041     }
1043 #endif
1042  
1043     return 0;
1044   }
# Line 1475 | Line 1473 | static int setbrdfunc(BRDFDAT *bd)
1473  
1474  
1475  
1476 < static int brtdFuncPhotonScatter (OBJREC *mat, RAY *rayIn)
1477 < /* Generate new photon ray for BRTDfunc material and recurse */
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;
# Line 1486 | Line 1485 | static int brtdFuncPhotonScatter (OBJREC *mat, RAY *ra
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     nd.mp = mat;
1493     nd.pr = rayIn;
1494 <   /* Dummies */
1494 >   /* Dummiez */
1495     nd.rspec = nd.tspec = 1.0;
1496     nd.trans = 0.5;
1497  
1498 <   /* Diffuse reflectance */
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 <   /* Diffuse transmittance */
1505 >   /* Diffuz tranzmittanz */
1506     setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7],
1507              mat -> oargs.farg [8]);
1508  
1509 <   /* Get modifiers */
1509 >   /* Get modz */
1510     raytexture(rayIn, mat -> omod);
1511     hastexture = (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY));
1512     if (hastexture) {
# Line 1518 | Line 1519 | static int brtdFuncPhotonScatter (OBJREC *mat, RAY *ra
1519     }
1520  
1521     if (rayIn -> rod < 0.0) {
1522 <      /* Orient perturbed values */
1522 >      /* Orient perturbed valuz */
1523        nd.pdot = -nd.pdot;
1524        for (i = 0; i < 3; i++) {
1525           nd.pnorm [i] = -nd.pnorm [i];
# Line 1528 | Line 1529 | static int brtdFuncPhotonScatter (OBJREC *mat, RAY *ra
1529        hitfront = 0;
1530     }
1531    
1532 <   /* Get pattern color, modify diffuse values */
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 spec refl/trans vars */
1537 >   /* Load cal file, evaluate spekula refl/tranz varz */
1538     nd.dp = NULL;
1539     mf = getfunc(mat, 9, 0x3f, 0);
1540     setbrdfunc(&nd);
# Line 1545 | Line 1546 | static int brtdFuncPhotonScatter (OBJREC *mat, RAY *ra
1546     if (errno == EDOM || errno == ERANGE)
1547        objerror(mat, WARNING, "compute error");
1548     else {
1549 <      /* Set up probabilities */
1549 >      /* Set up probz */
1550        prDiff = colorAvg(nd.rdiff);
1551        ptDiff = colorAvg(nd.tdiff);
1552        prSpec = colorAvg(rspecCol);
# Line 1553 | Line 1554 | static int brtdFuncPhotonScatter (OBJREC *mat, RAY *ra
1554        albedo = prDiff + ptDiff + prSpec + ptSpec;
1555     }
1556  
1557 <   /* Insert direct and indirect photon hits if diffuse component */
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 events */
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 <      /* Specular reflection */
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 <      /* Specular transmission */
1573 >      /* Ideal spekula tranzmission */
1574        photonRay(rayIn, &rayOut, PMAP_SPECTRANS, tspecCol);
1575        if (hastexture) {
1576 <         /* Perturb direction */
1576 >         /* Perturb direkzion */
1577           VSUB(rayOut.rdir, rayIn -> rdir, rayIn -> pert);
1578           if (normalize(rayOut.rdir) == 0.0) {
1579              objerror(mat, WARNING, "illegal perturbation");
# Line 1582 | Line 1583 | static int brtdFuncPhotonScatter (OBJREC *mat, RAY *ra
1583        }
1584     }
1585     else if (xi > (albedo -= prDiff)) {
1586 <      /* Diffuse reflection */
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 <      /* Diffuse transmission */
1593 >      /* Diffuz tranzmission */
1594        if (hitfront)
1595           flipsurface(rayIn);
1596        photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.mcolor);
# Line 1599 | Line 1600 | static int brtdFuncPhotonScatter (OBJREC *mat, RAY *ra
1600        diffPhotonScatter(bnorm, &rayOut);
1601     }
1602  
1603 +   tracePhoton(&rayOut);
1604     return 0;
1605   }
1606  
1607  
1608  
1609 < #if 0
1610 < int
1611 < m_brdf2(                        /* color a ray that hit a BRDF material */
1610 <        OBJREC  *m,
1611 <        RAY  *r
1612 < )
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 <        COLOR  ctmp;
1615 <        FVECT  vtmp;
1616 <        double  dtmp;
1617 <                                                /* always a shadow */
1619 <        if (r->crtype & SHADOW)
1620 <                return(1);
1621 <                                                /* check arguments */
1622 <        if ((m->oargs.nsargs < (hasdata(m->otype)?4:2)) | (m->oargs.nfargs <
1623 <                        ((m->otype==MAT_TFUNC)|(m->otype==MAT_TDATA)?6:4)))
1624 <                objerror(m, USER, "bad # arguments");
1625 <                                                /* check for back side */
1626 <        if (r->rod < 0.0) {
1627 <                if (!backvis) {
1628 <                        raytrans(r);
1629 <                        return(1);
1630 <                }
1631 <                raytexture(r, m->omod);
1632 <                flipsurface(r);                 /* reorient if backvis */
1633 <        } else
1634 <                raytexture(r, m->omod);
1613 >   BRDFDAT  nd;
1614 >   RAY      rayOut;
1615 >   double   dtmp, prDiff, ptDiff, albedo, xi;
1616 >   MFUNC    *mf;
1617 >   FVECT    bnorm;
1618  
1619 <        nd.mp = m;
1620 <        nd.pr = r;
1621 <                                                /* get material color */
1622 <        setcolor(nd.mcolor, m->oargs.farg[0],
1623 <                        m->oargs.farg[1],
1624 <                        m->oargs.farg[2]);
1625 <                                                /* get specular component */
1626 <        nd.rspec = m->oargs.farg[3];
1627 <                                                /* compute transmittance */
1628 <        if ((m->otype == MAT_TFUNC) | (m->otype == MAT_TDATA)) {
1629 <                nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec);
1630 <                nd.tspec = nd.trans * m->oargs.farg[5];
1631 <                dtmp = nd.trans - nd.tspec;
1632 <                setcolor(nd.tdiff, dtmp, dtmp, dtmp);
1633 <        } else {
1634 <                nd.tspec = nd.trans = 0.0;
1635 <                setcolor(nd.tdiff, 0.0, 0.0, 0.0);
1636 <        }
1654 <                                                /* compute reflectance */
1655 <        dtmp = 1.0 - nd.trans - nd.rspec;
1656 <        setcolor(nd.rdiff, dtmp, dtmp, dtmp);
1657 <        nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
1658 <        multcolor(nd.mcolor, r->pcol);          /* modify material color */
1659 <        multcolor(nd.rdiff, nd.mcolor);
1660 <        multcolor(nd.tdiff, nd.mcolor);
1661 <                                                /* load auxiliary files */
1662 <        if (hasdata(m->otype)) {
1663 <                nd.dp = getdata(m->oargs.sarg[1]);
1664 <                getfunc(m, 2, 0, 0);
1665 <        } else {
1666 <                nd.dp = NULL;
1667 <                getfunc(m, 1, 0, 0);
1668 <        }
1669 <                                                /* compute ambient */
1670 <        if (nd.trans < 1.0-FTINY) {
1671 <                copycolor(ctmp, nd.mcolor);     /* modified by material color */
1672 <                scalecolor(ctmp, 1.0-nd.trans);
1673 <                multambient(ctmp, r, nd.pnorm);
1674 <                addcolor(r->rcol, ctmp);        /* add to returned color */
1675 <        }
1676 <        if (nd.trans > FTINY) {         /* from other side */
1677 <                flipsurface(r);
1678 <                vtmp[0] = -nd.pnorm[0];
1679 <                vtmp[1] = -nd.pnorm[1];
1680 <                vtmp[2] = -nd.pnorm[2];
1681 <                copycolor(ctmp, nd.mcolor);
1682 <                scalecolor(ctmp, nd.trans);
1683 <                multambient(ctmp, r, vtmp);
1684 <                addcolor(r->rcol, ctmp);
1685 <                flipsurface(r);
1686 <        }
1687 <                                                /* add direct component */
1688 <        direct(r, dirbrdf, &nd);
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 >   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 >      raytexture(rayIn, mat -> omod);
1634 >      flipsurface(rayIn);
1635 >   }
1636 >   else raytexture(rayIn, mat -> omod);
1637  
1638 <        return(1);
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 >   /* Set up probz */
1680 >   prDiff = colorAvg(nd.rdiff);
1681 >   ptDiff = colorAvg(nd.tdiff);
1682 >   albedo = prDiff + ptDiff;
1683 >
1684 >   /* Insert direct and indirect photon hitz if diffuz komponent */
1685 >   if (prDiff > FTINY || ptDiff > FTINY)
1686 >      addPhotons(rayIn);
1687 >
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 {
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   }
1692 #endif
1711  
1712  
1713  
# Line 1953 | 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 1970 | 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 1978 | 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_BRTDF] = brtdFuncPhotonScatter;
2007 <   photonScatter [MAT_BSDF] =
2008 <      photonScatter [MAT_ABSDF] = 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