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

Comparing ray/src/rt/m_bsdf.c (file contents):
Revision 2.44 by greg, Sat Dec 2 16:13:04 2017 UTC vs.
Revision 2.54 by greg, Wed Aug 8 04:15:18 2018 UTC

# Line 8 | Line 8 | static const char RCSid[] = "$Id$";
8   #include "copyright.h"
9  
10   #include  "ray.h"
11 + #include  "otypes.h"
12   #include  "ambient.h"
13   #include  "source.h"
14   #include  "func.h"
# Line 16 | Line 17 | static const char RCSid[] = "$Id$";
17   #include  "pmapmat.h"
18  
19   /*
20 < *      Arguments to this material include optional diffuse colors.
20 >  *     Arguments to this material include optional diffuse colors.
21   *  String arguments include the BSDF and function files.
22 < *      A non-zero thickness causes the strange but useful behavior
22 > *      For the MAT_BSDF type, a non-zero thickness causes the useful behavior
23   *  of translating transmitted rays this distance beneath the surface
24   *  (opposite the surface normal) to bypass any intervening geometry.
25   *  Translation only affects scattered, non-source-directed samples.
# Line 35 | Line 36 | static const char RCSid[] = "$Id$";
36   *  hides geometry in front of the surface when rays hit from behind,
37   *  and applies only the transmission and backside reflectance properties.
38   *  Reflection is ignored on the hidden side, as those rays pass through.
39 < *      When thickness is set to zero, shadow rays will be blocked unless
40 < *  a BTDF has a strong "through" component in the source direction.
39 > *      For the MAT_ABSDF type, we check for a strong "through" component.
40 > *  Such a component will cause direct rays to pass through unscattered.
41   *  A separate test prevents over-counting by dropping samples that are
42   *  too close to this "through" direction.  BSDFs with such a through direction
43   *  will also have a view component, meaning they are somewhat see-through.
44 + *  A MAT_BSDF type with zero thickness behaves the same as a MAT_ABSDF
45 + *  type with no strong through component.
46   *      The "up" vector for the BSDF is given by three variables, defined
47   *  (along with the thickness) by the named function file, or '.' if none.
48   *  Together with the surface normal, this defines the local coordinate
# Line 52 | Line 55 | static const char RCSid[] = "$Id$";
55   *  not multiplied.  However, patterns affect this material as a multiplier
56   *  on everything except non-diffuse reflection.
57   *
58 + *  Arguments for MAT_ABSDF are:
59 + *      5+      BSDFfile        ux uy uz        funcfile        transform
60 + *      0
61 + *      0|3|6|9 rdf     gdf     bdf
62 + *              rdb     gdb     bdb
63 + *              rdt     gdt     bdt
64 + *
65   *  Arguments for MAT_BSDF are:
66   *      6+      thick   BSDFfile        ux uy uz        funcfile        transform
67   *      0
# Line 76 | Line 86 | typedef struct {
86          RREAL   toloc[3][3];    /* world to local BSDF coords */
87          RREAL   fromloc[3][3];  /* local BSDF coords to world */
88          double  thick;          /* surface thickness */
89 <        COLOR   cthru;          /* "through" component multiplier */
89 >        COLOR   cthru;          /* "through" component for MAT_ABSDF */
90          SDData  *sd;            /* loaded BSDF data */
91          COLOR   rdiff;          /* diffuse reflection */
92          COLOR   runsamp;        /* BSDF hemispherical reflection */
# Line 86 | Line 96 | typedef struct {
96  
97   #define cvt_sdcolor(cv, svp)    ccy2rgb(&(svp)->spec, (svp)->cieY, cv)
98  
99 < /* Compute "through" component color */
99 > /* Compute "through" component color for MAT_ABSDF */
100   static void
101   compute_through(BSDFDAT *ndp)
102   {
# Line 106 | Line 116 | compute_through(BSDFDAT *ndp)
116                                          {0, -1.6},
117                                          {1.6, 0},
118                                  };
119 <        const double    peak_over = 2.0;
119 >        const double    peak_over = 1.5;
120          SDSpectralDF    *dfp;
121          FVECT           pdir;
122          double          tomega, srchrad;
# Line 114 | Line 124 | compute_through(BSDFDAT *ndp)
124          int             i;
125          SDError         ec;
126  
117        setcolor(ndp->cthru, 0, 0, 0);          /* starting assumption */
118
127          if (ndp->pr->rod > 0)
128                  dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
129          else
# Line 142 | Line 150 | compute_through(BSDFDAT *ndp)
150                          goto baderror;
151                  cvt_sdcolor(vcol, &sv);
152                  addcolor(vsum, vcol);
153 <                if (bright(vcol) > bright(vpeak)) {
153 >                if (sv.cieY > bright(vpeak)) {
154                          copycolor(vpeak, vcol);
155                          VCOPY(pdir, tdir);
156                  }
# Line 154 | Line 162 | compute_through(BSDFDAT *ndp)
162                  goto baderror;
163          if (tomega > 1.5*dfp->minProjSA)
164                  return;                         /* not really a peak? */
157        tomega /= fabs(pdir[2]);                /* remove cosine factor */
165          if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .001)
166                  return;                         /* < 0.1% transmission */
167          for (i = 3; i--; )                      /* remove peak from average */
# Line 228 | Line 235 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
235                  diffY = 0;
236                  setcolor(cdiff,  0, 0, 0);
237          }
238 <                                        /* need projected solid angles */
238 >                                        /* need projected solid angle */
239          omega *= fabs(vsrc[2]);
233        ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
234        if (ec)
235                goto baderror;
240                                          /* check indirect over-counting */
241          if ((vsrc[2] > 0) ^ (ndp->vray[2] > 0) && bright(ndp->cthru) > FTINY) {
242 <                double  dx = vsrc[0] + ndp->vray[0];
243 <                double  dy = vsrc[1] + ndp->vray[1];
244 <                if (dx*dx + dy*dy <= (4./PI)*(omega + tomega +
245 <                                                2.*sqrt(omega*tomega)))
242 >                double          dx = vsrc[0] + ndp->vray[0];
243 >                double          dy = vsrc[1] + ndp->vray[1];
244 >                SDSpectralDF    *dfp = (ndp->pr->rod > 0) ?
245 >                        ((ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb) :
246 >                        ((ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf) ;
247 >
248 >                if (dx*dx + dy*dy <= (2.5*4./PI)*(omega + dfp->minProjSA +
249 >                                                2.*sqrt(omega*dfp->minProjSA)))
250                          return(0);
251          }
252 +        ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
253 +        if (ec)
254 +                goto baderror;
255                                          /* assign number of samples */
256          sf = specjitter * ndp->pr->rweight;
257          if (tomega <= 0)
# Line 442 | Line 453 | static int
453   sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int xmit)
454   {
455          const int       hasthru = (xmit &&
456 <                                !(ndp->pr->crtype & (SPECULAR|AMBIENT)) &&
457 <                                bright(ndp->cthru) > FTINY);
456 >                                        !(ndp->pr->crtype & (SPECULAR|AMBIENT))
457 >                                        && bright(ndp->cthru) > FTINY);
458          int             nstarget = 1;
459          int             nsent = 0;
460          int             n;
# Line 490 | Line 501 | sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int xmit
501                  if (xmit)                       /* apply pattern on transmit */
502                          multcolor(sr.rcoef, ndp->pr->pcol);
503                  if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
504 <                        if (maxdepth > 0)
505 <                                break;
506 <                        continue;               /* Russian roulette victim */
504 >                        if (!n & (nstarget > 1)) {
505 >                                n = nstarget;   /* avoid infinitue loop */
506 >                                nstarget = nstarget*sr.rweight/minweight;
507 >                                if (n == nstarget) break;
508 >                                n = -1;         /* moved target */
509 >                        }
510 >                        continue;               /* try again */
511                  }
512                  if (xmit && ndp->thick != 0)    /* need to offset origin? */
513                          VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
# Line 508 | Line 523 | sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int xmit
523   static int
524   sample_sdf(BSDFDAT *ndp, int sflags)
525   {
526 <        int             hasthru = (sflags == SDsampSpT
527 <                                    && !(ndp->pr->crtype & (SPECULAR|AMBIENT))
528 <                                    && bright(ndp->cthru) > FTINY);
526 >        int             hasthru = (sflags == SDsampSpT &&
527 >                                        !(ndp->pr->crtype & (SPECULAR|AMBIENT))
528 >                                        && bright(ndp->cthru) > FTINY);
529          int             n, ntotal = 0;
530          double          b = 0;
531          SDSpectralDF    *dfp;
# Line 577 | Line 592 | sample_sdf(BSDFDAT *ndp, int sflags)
592   int
593   m_bsdf(OBJREC *m, RAY *r)
594   {
595 +        int     hasthick = (m->otype == MAT_BSDF);
596          int     hitfront;
597          COLOR   ctmp;
598          SDError ec;
# Line 584 | Line 600 | m_bsdf(OBJREC *m, RAY *r)
600          MFUNC   *mf;
601          BSDFDAT nd;
602                                                  /* check arguments */
603 <        if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) |
603 >        if ((m->oargs.nsargs < hasthick+5) | (m->oargs.nfargs > 9) |
604                                  (m->oargs.nfargs % 3))
605                  objerror(m, USER, "bad # arguments");
606                                                  /* record surface struck */
607          hitfront = (r->rod > 0);
608                                                  /* load cal file */
609 <        mf = getfunc(m, 5, 0x1d, 1);
609 >        mf = hasthick   ? getfunc(m, 5, 0x1d, 1)
610 >                        : getfunc(m, 4, 0xe, 1) ;
611          setfunc(m, r);
612 <                                                /* get thickness */
613 <        nd.thick = evalue(mf->ep[0]);
614 <        if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
615 <                nd.thick = 0;
612 >        nd.thick = 0;                           /* set thickness */
613 >        if (hasthick) {
614 >                nd.thick = evalue(mf->ep[0]);
615 >                if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
616 >                        nd.thick = 0;
617 >        }
618                                                  /* check backface visibility */
619          if (!hitfront & !backvis) {
620                  raytrans(r);
# Line 608 | Line 627 | m_bsdf(OBJREC *m, RAY *r)
627                  raytrans(r);                    /* hide our proxy */
628                  return(1);
629          }
630 +        if (hasthick && r->crtype & SHADOW)     /* early shadow check #1 */
631 +                return(1);
632          nd.mp = m;
633          nd.pr = r;
634                                                  /* get BSDF data */
635 <        nd.sd = loadBSDF(m->oargs.sarg[1]);
636 <                                                /* early shadow check */
635 >        nd.sd = loadBSDF(m->oargs.sarg[hasthick]);
636 >                                                /* early shadow check #2 */
637          if (r->crtype & SHADOW && (nd.sd->tf == NULL) & (nd.sd->tb == NULL))
638                  return(1);
639                                                  /* diffuse reflectance */
# Line 647 | Line 668 | m_bsdf(OBJREC *m, RAY *r)
668          multcolor(nd.rdiff, r->pcol);
669          multcolor(nd.tdiff, r->pcol);
670                                                  /* get up vector */
671 <        upvec[0] = evalue(mf->ep[1]);
672 <        upvec[1] = evalue(mf->ep[2]);
673 <        upvec[2] = evalue(mf->ep[3]);
671 >        upvec[0] = evalue(mf->ep[hasthick+0]);
672 >        upvec[1] = evalue(mf->ep[hasthick+1]);
673 >        upvec[2] = evalue(mf->ep[hasthick+2]);
674                                                  /* return to world coords */
675          if (mf->fxp != &unitxf) {
676                  multv3(upvec, upvec, mf->fxp->xfm);
# Line 672 | Line 693 | m_bsdf(OBJREC *m, RAY *r)
693                  objerror(m, WARNING, "Illegal orientation vector");
694                  return(1);
695          }
696 <        compute_through(&nd);                   /* compute through component */
697 <        if (r->crtype & SHADOW) {
698 <                RAY     tr;                     /* attempt to pass shadow ray */
699 <                if (rayorigin(&tr, TRANS, r, nd.cthru) < 0)
700 <                        return(1);              /* blocked */
701 <                VCOPY(tr.rdir, r->rdir);
702 <                rayvalue(&tr);                  /* transmit with scaling */
703 <                multcolor(tr.rcol, tr.rcoef);
704 <                copycolor(r->rcol, tr.rcol);
705 <                return(1);                      /* we're done */
696 >        setcolor(nd.cthru, 0, 0, 0);            /* consider through component */
697 >        if (m->otype == MAT_ABSDF) {
698 >                compute_through(&nd);
699 >                if (r->crtype & SHADOW) {
700 >                        RAY     tr;             /* attempt to pass shadow ray */
701 >                        if (rayorigin(&tr, TRANS, r, nd.cthru) < 0)
702 >                                return(1);      /* no through component */
703 >                        VCOPY(tr.rdir, r->rdir);
704 >                        rayvalue(&tr);          /* transmit with scaling */
705 >                        multcolor(tr.rcol, tr.rcoef);
706 >                        copycolor(r->rcol, tr.rcol);
707 >                        return(1);              /* we're done */
708 >                }
709          }
710          ec = SDinvXform(nd.fromloc, nd.toloc);
711          if (!ec)                                /* determine BSDF resolution */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines