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.33 by greg, Sun Feb 19 17:22:18 2017 UTC vs.
Revision 2.36 by greg, Tue May 16 20:06:40 2017 UTC

# Line 23 | Line 23 | static const char RCSid[] = "$Id$";
23   *  (opposite the surface normal) to bypass any intervening geometry.
24   *  Translation only affects scattered, non-source-directed samples.
25   *  A non-zero thickness has the further side-effect that an unscattered
26 < *  (view) ray will pass right through our material if it has any
27 < *  non-diffuse transmission, making the BSDF surface invisible.  This
28 < *  shows the proxied geometry instead. Thickness has the further
29 < *  effect of turning off reflection on the hidden side so that rays
30 < *  heading in the opposite direction pass unimpeded through the BSDF
26 > *  (view) ray will pass right through our material, making the BSDF
27 > *  surface invisible and showing the proxied geometry instead. Thickness
28 > *  has the further effect of turning off reflection on the reverse side so
29 > *  rays heading in the opposite direction pass unimpeded through the BSDF
30   *  surface.  A paired surface may be placed on the opposide side of
31   *  the detail geometry, less than this thickness away, if a two-way
32   *  proxy is desired.  Note that the sign of the thickness is important.
# Line 36 | Line 35 | static const char RCSid[] = "$Id$";
35   *  hides geometry in front of the surface when rays hit from behind,
36   *  and applies only the transmission and backside reflectance properties.
37   *  Reflection is ignored on the hidden side, as those rays pass through.
38 + *      When thickness is set to zero, shadow rays will be blocked unless
39 + *  a BTDF has a strong "through" component in the source direction.
40 + *  A separate test prevents over-counting by dropping specular & ambient
41 + *  samples that are too close to this "through" direction.  The same
42 + *  restriction applies for the proxy case (thickness != 0).
43   *      The "up" vector for the BSDF is given by three variables, defined
44   *  (along with the thickness) by the named function file, or '.' if none.
45   *  Together with the surface normal, this defines the local coordinate
# Line 43 | Line 47 | static const char RCSid[] = "$Id$";
47   *      We do not reorient the surface, so if the BSDF has no back-side
48   *  reflectance and none is given in the real arguments, a BSDF surface
49   *  with zero thickness will appear black when viewed from behind
50 < *  unless backface visibility is off.
50 > *  unless backface visibility is on, when it becomes invisible.
51   *      The diffuse arguments are added to components in the BSDF file,
52   *  not multiplied.  However, patterns affect this material as a multiplier
53   *  on everything except non-diffuse reflection.
# Line 59 | Line 63 | static const char RCSid[] = "$Id$";
63   /*
64   * Note that our reverse ray-tracing process means that the positions
65   * of incoming and outgoing vectors may be reversed in our calls
66 < * to the BSDF library.  This is fine, since the bidirectional nature
66 > * to the BSDF library.  This is usually fine, since the bidirectional nature
67   * of the BSDF (that's what the 'B' stands for) means it all works out.
68   */
69  
# Line 72 | Line 76 | typedef struct {
76          RREAL   toloc[3][3];    /* world to local BSDF coords */
77          RREAL   fromloc[3][3];  /* local BSDF coords to world */
78          double  thick;          /* surface thickness */
79 +        COLOR   cthru;          /* "through" component multiplier */
80          SDData  *sd;            /* loaded BSDF data */
81          COLOR   rdiff;          /* diffuse reflection */
82          COLOR   tdiff;          /* diffuse transmission */
# Line 79 | Line 84 | typedef struct {
84  
85   #define cvt_sdcolor(cv, svp)    ccy2rgb(&(svp)->spec, (svp)->cieY, cv)
86  
87 + /* Compute "through" component color */
88 + static void
89 + compute_through(BSDFDAT *ndp)
90 + {
91 + #define NDIR2CHECK      13
92 +        static const float      dir2check[NDIR2CHECK][2] = {
93 +                                        {0, 0},
94 +                                        {-0.8, 0},
95 +                                        {0, 0.8},
96 +                                        {0, -0.8},
97 +                                        {0.8, 0},
98 +                                        {-0.8, 0.8},
99 +                                        {-0.8, -0.8},
100 +                                        {0.8, 0.8},
101 +                                        {0.8, -0.8},
102 +                                        {-1.6, 0},
103 +                                        {0, 1.6},
104 +                                        {0, -1.6},
105 +                                        {1.6, 0},
106 +                                };
107 +        const double    peak_over = 2.0;
108 +        SDSpectralDF    *dfp;
109 +        FVECT           pdir;
110 +        double          tomega, srchrad;
111 +        COLOR           vpeak, vsum;
112 +        int             nsum, i;
113 +        SDError         ec;
114 +
115 +        setcolor(ndp->cthru, .0, .0, .0);       /* starting assumption */
116 +
117 +        if (ndp->pr->rod > 0)
118 +                dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
119 +        else
120 +                dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
121 +
122 +        if (dfp == NULL)
123 +                return;                         /* no specular transmission */
124 +        if (bright(ndp->pr->pcol) <= FTINY)
125 +                return;                         /* pattern is black, here */
126 +        srchrad = sqrt(dfp->minProjSA);         /* else search for peak */
127 +        setcolor(vpeak, .0, .0, .0);
128 +        setcolor(vsum, .0, .0, .0);
129 +        nsum = 0;
130 +        for (i = 0; i < NDIR2CHECK; i++) {
131 +                FVECT   tdir;
132 +                SDValue sv;
133 +                COLOR   vcol;
134 +                tdir[0] = -ndp->vray[0] + dir2check[i][0]*srchrad;
135 +                tdir[1] = -ndp->vray[1] + dir2check[i][1]*srchrad;
136 +                tdir[2] = -ndp->vray[2];
137 +                normalize(tdir);
138 +                ec = SDevalBSDF(&sv, tdir, ndp->vray, ndp->sd);
139 +                if (ec)
140 +                        goto baderror;
141 +                cvt_sdcolor(vcol, &sv);
142 +                addcolor(vsum, vcol);
143 +                ++nsum;
144 +                if (bright(vcol) > bright(vpeak)) {
145 +                        copycolor(vpeak, vcol);
146 +                        VCOPY(pdir, tdir);
147 +                }
148 +        }
149 +        ec = SDsizeBSDF(&tomega, pdir, ndp->vray, SDqueryMin, ndp->sd);
150 +        if (ec)
151 +                goto baderror;
152 +        if (tomega > 1.5*dfp->minProjSA)
153 +                return;                         /* not really a peak? */
154 +        if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .007)
155 +                return;                         /* < 0.7% transmission */
156 +        for (i = 3; i--; )                      /* remove peak from average */
157 +                colval(vsum,i) -= colval(vpeak,i);
158 +        --nsum;
159 +        if (peak_over*bright(vsum) >= nsum*bright(vpeak))
160 +                return;                         /* not peaky enough */
161 +        copycolor(ndp->cthru, vpeak);           /* else use it */
162 +        scalecolor(ndp->cthru, tomega);
163 +        multcolor(ndp->cthru, ndp->pr->pcol);   /* modify by pattern */
164 +        return;
165 + baderror:
166 +        objerror(ndp->mp, USER, transSDError(ec));
167 + #undef NDIR2CHECK
168 + }
169 +
170   /* Jitter ray sample according to projected solid angle and specjitter */
171   static void
172   bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
# Line 99 | Line 187 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
187   {
188          int     nsamp, ok = 0;
189          FVECT   vsrc, vsmp, vjit;
190 <        double  tomega;
190 >        double  tomega, tomega2;
191          double  sf, tsr, sd[2];
192          COLOR   csmp, cdiff;
193          double  diffY;
# Line 139 | Line 227 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
227          if (ec)
228                  goto baderror;
229                                          /* check indirect over-counting */
230 <        if (ndp->thick != 0 && ndp->pr->crtype & (SPECULAR|AMBIENT)
230 >        if ((ndp->thick != 0 || bright(ndp->cthru) > FTINY)
231 >                                && ndp->pr->crtype & (SPECULAR|AMBIENT)
232                                  && (vsrc[2] > 0) ^ (ndp->vray[2] > 0)) {
233                  double  dx = vsrc[0] + ndp->vray[0];
234                  double  dy = vsrc[1] + ndp->vray[1];
# Line 163 | Line 252 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
252                          multisamp(sd, 2, (i + frandom())/(double)nsamp);
253                          vsmp[0] += (sd[0] - .5)*sf;
254                          vsmp[1] += (sd[1] - .5)*sf;
255 <                        if (normalize(vsmp) == 0) {
167 <                                --nsamp;
168 <                                continue;
169 <                        }
255 >                        normalize(vsmp);
256                  }
257                  bsdf_jitter(vjit, ndp, tsr);
258 <                                        /* compute BSDF */
258 >                                        /* check for variable resolution */
259 >                ec = SDsizeBSDF(&tomega2, vjit, vsmp, SDqueryMin, ndp->sd);
260 >                if (ec)
261 >                        goto baderror;
262 >                if (tomega2 < .12*tomega)
263 >                        continue;       /* not safe to include */
264 >                                        /* else compute BSDF */
265                  ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
266                  if (ec)
267                          goto baderror;
268 <                if (sv.cieY - diffY <= FTINY) {
177 <                        addcolor(cval, cdiff);
268 >                if (sv.cieY - diffY <= FTINY)
269                          continue;       /* no specular part */
179                }
270                  cvt_sdcolor(csmp, &sv);
271                  addcolor(cval, csmp);   /* else average it in */
272                  ++ok;
# Line 185 | Line 275 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
275                  setcolor(cval, .0, .0, .0);
276                  return(0);              /* no valid specular samples */
277          }
278 <        sf = 1./(double)nsamp;
278 >        sf = 1./(double)ok;
279          scalecolor(cval, sf);
280                                          /* subtract diffuse contribution */
281          for (i = 3*(diffY > FTINY); i--; )
# Line 467 | Line 557 | m_bsdf(OBJREC *m, RAY *r)
557          nd.thick = evalue(mf->ep[0]);
558          if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
559                  nd.thick = .0;
470                                                /* check shadow */
471        if (r->crtype & SHADOW) {
472                if (nd.thick != 0)
473                        raytrans(r);            /* pass-through */
474                return(1);                      /* or shadow */
475        }
560                                                  /* check backface visibility */
561          if (!hitfront & !backvis) {
562                  raytrans(r);
563                  return(1);
564          }
565                                                  /* check other rays to pass */
566 <        if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) ||
566 >        if (nd.thick != 0 && (r->crtype & SHADOW ||
567 >                                !(r->crtype & (SPECULAR|AMBIENT)) ||
568                                  (nd.thick > 0) ^ hitfront)) {
569                  raytrans(r);                    /* hide our proxy */
570                  return(1);
# Line 488 | Line 573 | m_bsdf(OBJREC *m, RAY *r)
573          nd.pr = r;
574                                                  /* get BSDF data */
575          nd.sd = loadBSDF(m->oargs.sarg[1]);
576 +                                                /* early shadow check */
577 +        if (r->crtype & SHADOW && (nd.sd->tf == NULL) & (nd.sd->tb == NULL))
578 +                return(1);
579                                                  /* diffuse reflectance */
580          if (hitfront) {
581                  cvt_sdcolor(nd.rdiff, &nd.sd->rLambFront);
# Line 541 | Line 629 | m_bsdf(OBJREC *m, RAY *r)
629                  nd.vray[2] = -r->rdir[2];
630                  ec = SDmapDir(nd.vray, nd.toloc, nd.vray);
631          }
544        if (!ec)
545                ec = SDinvXform(nd.fromloc, nd.toloc);
632          if (ec) {
633                  objerror(m, WARNING, "Illegal orientation vector");
634                  return(1);
635          }
636 <                                                /* determine BSDF resolution */
637 <        ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin+SDqueryMax, nd.sd);
636 >        compute_through(&nd);                   /* compute through component */
637 >        if (r->crtype & SHADOW) {
638 >                RAY     tr;                     /* attempt to pass shadow ray */
639 >                if (rayorigin(&tr, TRANS, r, nd.cthru) < 0)
640 >                        return(1);              /* blocked */
641 >                VCOPY(tr.rdir, r->rdir);
642 >                rayvalue(&tr);                  /* transmit with scaling */
643 >                multcolor(tr.rcol, tr.rcoef);
644 >                copycolor(r->rcol, tr.rcol);
645 >                return(1);                      /* we're done */
646 >        }
647 >        ec = SDinvXform(nd.fromloc, nd.toloc);
648 >        if (!ec)                                /* determine BSDF resolution */
649 >                ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL,
650 >                                        SDqueryMin+SDqueryMax, nd.sd);
651          if (ec)
652                  objerror(m, USER, transSDError(ec));
653  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines