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.31 by greg, Fri Feb 17 23:24:56 2017 UTC vs.
Revision 2.39 by greg, Fri Jun 2 18:10:11 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   runsamp;        /* BSDF hemispherical reflection */
83          COLOR   tdiff;          /* diffuse transmission */
84 +        COLOR   tunsamp;        /* BSDF hemispherical transmission */
85   }  BSDFDAT;             /* BSDF material data */
86  
87   #define cvt_sdcolor(cv, svp)    ccy2rgb(&(svp)->spec, (svp)->cieY, cv)
88  
89 + /* Compute "through" component color */
90 + static void
91 + compute_through(BSDFDAT *ndp)
92 + {
93 + #define NDIR2CHECK      13
94 +        static const float      dir2check[NDIR2CHECK][2] = {
95 +                                        {0, 0},
96 +                                        {-0.8, 0},
97 +                                        {0, 0.8},
98 +                                        {0, -0.8},
99 +                                        {0.8, 0},
100 +                                        {-0.8, 0.8},
101 +                                        {-0.8, -0.8},
102 +                                        {0.8, 0.8},
103 +                                        {0.8, -0.8},
104 +                                        {-1.6, 0},
105 +                                        {0, 1.6},
106 +                                        {0, -1.6},
107 +                                        {1.6, 0},
108 +                                };
109 +        const double    peak_over = 2.0;
110 +        SDSpectralDF    *dfp;
111 +        FVECT           pdir;
112 +        double          tomega, srchrad;
113 +        COLOR           vpeak, vsum;
114 +        int             nsum, i;
115 +        SDError         ec;
116 +
117 +        setcolor(ndp->cthru, .0, .0, .0);       /* starting assumption */
118 +
119 +        if (!(ndp->pr->crtype & (SPECULAR|AMBIENT|SHADOW)))
120 +                return;                         /* simply don't need to know */
121 +
122 +        if (ndp->pr->rod > 0)
123 +                dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
124 +        else
125 +                dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
126 +
127 +        if (dfp == NULL)
128 +                return;                         /* no specular transmission */
129 +        if (bright(ndp->pr->pcol) <= FTINY)
130 +                return;                         /* pattern is black, here */
131 +        srchrad = sqrt(dfp->minProjSA);         /* else search for peak */
132 +        setcolor(vpeak, .0, .0, .0);
133 +        setcolor(vsum, .0, .0, .0);
134 +        nsum = 0;
135 +        for (i = 0; i < NDIR2CHECK; i++) {
136 +                FVECT   tdir;
137 +                SDValue sv;
138 +                COLOR   vcol;
139 +                tdir[0] = -ndp->vray[0] + dir2check[i][0]*srchrad;
140 +                tdir[1] = -ndp->vray[1] + dir2check[i][1]*srchrad;
141 +                tdir[2] = -ndp->vray[2];
142 +                normalize(tdir);
143 +                ec = SDevalBSDF(&sv, tdir, ndp->vray, ndp->sd);
144 +                if (ec)
145 +                        goto baderror;
146 +                cvt_sdcolor(vcol, &sv);
147 +                addcolor(vsum, vcol);
148 +                ++nsum;
149 +                if (bright(vcol) > bright(vpeak)) {
150 +                        copycolor(vpeak, vcol);
151 +                        VCOPY(pdir, tdir);
152 +                }
153 +        }
154 +        ec = SDsizeBSDF(&tomega, pdir, ndp->vray, SDqueryMin, ndp->sd);
155 +        if (ec)
156 +                goto baderror;
157 +        if (tomega > 1.5*dfp->minProjSA)
158 +                return;                         /* not really a peak? */
159 +        if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .007)
160 +                return;                         /* < 0.7% transmission */
161 +        for (i = 3; i--; )                      /* remove peak from average */
162 +                colval(vsum,i) -= colval(vpeak,i);
163 +        --nsum;
164 +        if (peak_over*bright(vsum) >= nsum*bright(vpeak))
165 +                return;                         /* not peaky enough */
166 +        copycolor(ndp->cthru, vpeak);           /* else use it */
167 +        scalecolor(ndp->cthru, tomega);
168 +        multcolor(ndp->cthru, ndp->pr->pcol);   /* modify by pattern */
169 +        return;
170 + baderror:
171 +        objerror(ndp->mp, USER, transSDError(ec));
172 + #undef NDIR2CHECK
173 + }
174 +
175   /* Jitter ray sample according to projected solid angle and specjitter */
176   static void
177   bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
# Line 93 | Line 186 | bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
186          normalize(vres);
187   }
188  
189 < /* Evaluate BSDF for direct component, returning true if OK to proceed */
189 > /* Get BSDF specular for direct component, returning true if OK to proceed */
190   static int
191 < direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
191 > direct_specular_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
192   {
193          int     nsamp, ok = 0;
194          FVECT   vsrc, vsmp, vjit;
195 <        double  tomega;
195 >        double  tomega, tomega2;
196          double  sf, tsr, sd[2];
197 <        COLOR   csmp;
197 >        COLOR   csmp, cdiff;
198 >        double  diffY;
199          SDValue sv;
200          SDError ec;
201          int     i;
202 +                                        /* in case we fail */
203 +        setcolor(cval, .0, .0, .0);
204                                          /* transform source direction */
205          if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
206                  return(0);
207 <                                        /* assign number of samples */
207 >                                        /* will discount diffuse portion */
208 >        switch ((vsrc[2] > 0)<<1 | (ndp->vray[2] > 0)) {
209 >        case 3:
210 >                if (ndp->sd->rf == NULL)
211 >                        return(0);      /* all diffuse */
212 >                sv = ndp->sd->rLambFront;
213 >                break;
214 >        case 0:
215 >                if (ndp->sd->rb == NULL)
216 >                        return(0);      /* all diffuse */
217 >                sv = ndp->sd->rLambBack;
218 >                break;
219 >        default:
220 >                if ((ndp->sd->tf == NULL) & (ndp->sd->tb == NULL))
221 >                        return(0);      /* all diffuse */
222 >                sv = ndp->sd->tLamb;
223 >                break;
224 >        }
225 >        if (sv.cieY > FTINY) {
226 >                diffY = sv.cieY *= 1./PI;
227 >                cvt_sdcolor(cdiff, &sv);
228 >        } else {
229 >                diffY = .0;
230 >                setcolor(cdiff, .0, .0, .0);
231 >        }
232 >                                        /* need projected solid angles */
233 >        omega *= fabs(vsrc[2]);
234          ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
235          if (ec)
236                  goto baderror;
237                                          /* check indirect over-counting */
238 <        if (ndp->thick != 0 && ndp->pr->crtype & (SPECULAR|AMBIENT)
239 <                                && vsrc[2] > 0 ^ ndp->vray[2] > 0) {
238 >        if (ndp->pr->crtype & (SPECULAR|AMBIENT)
239 >                                && (vsrc[2] > 0) ^ (ndp->vray[2] > 0)
240 >                                && bright(ndp->cthru) > FTINY) {
241                  double  dx = vsrc[0] + ndp->vray[0];
242                  double  dy = vsrc[1] + ndp->vray[1];
243 <                if (dx*dx + dy*dy <= omega+tomega)
243 >                if (dx*dx + dy*dy <= (4./PI)*(omega + tomega +
244 >                                                2.*sqrt(omega*tomega)))
245                          return(0);
246          }
247 +                                        /* assign number of samples */
248          sf = specjitter * ndp->pr->rweight;
249          if (tomega <= .0)
250                  nsamp = 1;
# Line 128 | Line 253 | direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, B
253          else
254                  nsamp = 4.*sf*omega/tomega + .5;
255          nsamp += !nsamp;
256 <        setcolor(cval, .0, .0, .0);     /* sample our source area */
132 <        sf = sqrt(omega);
256 >        sf = sqrt(omega);               /* sample our source area */
257          tsr = sqrt(tomega);
258          for (i = nsamp; i--; ) {
259                  VCOPY(vsmp, vsrc);      /* jitter query directions */
# Line 137 | Line 261 | direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, B
261                          multisamp(sd, 2, (i + frandom())/(double)nsamp);
262                          vsmp[0] += (sd[0] - .5)*sf;
263                          vsmp[1] += (sd[1] - .5)*sf;
264 <                        if (normalize(vsmp) == 0) {
141 <                                --nsamp;
142 <                                continue;
143 <                        }
264 >                        normalize(vsmp);
265                  }
266                  bsdf_jitter(vjit, ndp, tsr);
267                                          /* compute BSDF */
268                  ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
269                  if (ec)
270                          goto baderror;
271 <                if (sv.cieY <= FTINY)   /* worth using? */
272 <                        continue;
271 >                if (sv.cieY - diffY <= FTINY)
272 >                        continue;       /* no specular part */
273 >                                        /* check for variable resolution */
274 >                ec = SDsizeBSDF(&tomega2, vjit, vsmp, SDqueryMin, ndp->sd);
275 >                if (ec)
276 >                        goto baderror;
277 >                if (tomega2 < .12*tomega)
278 >                        continue;       /* not safe to include */
279                  cvt_sdcolor(csmp, &sv);
280 <                addcolor(cval, csmp);   /* average it in */
280 >                addcolor(cval, csmp);   /* else average it in */
281                  ++ok;
282          }
283 <        sf = 1./(double)nsamp;
283 >        if (!ok)                        /* no valid specular samples? */
284 >                return(0);
285 >
286 >        sf = 1./(double)ok;             /* compute average BSDF */
287          scalecolor(cval, sf);
288 <        return(ok);
288 >                                        /* subtract diffuse contribution */
289 >        for (i = 3*(diffY > FTINY); i--; )
290 >                if ((colval(cval,i) -= colval(cdiff,i)) < .0)
291 >                        colval(cval,i) = .0;
292 >        return(1);
293   baderror:
294          objerror(ndp->mp, USER, transSDError(ec));
295          return(0);                      /* gratis return */
# Line 183 | Line 317 | dir_bsdf(
317  
318          if (ldot > 0 && bright(np->rdiff) > FTINY) {
319                  /*
320 <                 *  Compute added diffuse reflected component.
320 >                 *  Compute diffuse reflected component
321                   */
322                  copycolor(ctmp, np->rdiff);
323                  dtmp = ldot * omega * (1./PI);
# Line 192 | Line 326 | dir_bsdf(
326          }
327          if (ldot < 0 && bright(np->tdiff) > FTINY) {
328                  /*
329 <                 *  Compute added diffuse transmission.
329 >                 *  Compute diffuse transmission
330                   */
331                  copycolor(ctmp, np->tdiff);
332                  dtmp = -ldot * omega * (1.0/PI);
# Line 202 | Line 336 | dir_bsdf(
336          if (ambRayInPmap(np->pr))
337                  return;         /* specular already in photon map */
338          /*
339 <         *  Compute scattering coefficient using BSDF.
339 >         *  Compute specular scattering coefficient using BSDF
340           */
341 <        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
341 >        if (!direct_specular_OK(ctmp, ldir, omega, np))
342                  return;
343          if (ldot < 0) {         /* pattern for specular transmission */
344                  multcolor(ctmp, np->pr->pcol);
# Line 238 | Line 372 | dir_brdf(
372  
373          if (bright(np->rdiff) > FTINY) {
374                  /*
375 <                 *  Compute added diffuse reflected component.
375 >                 *  Compute diffuse reflected component
376                   */
377                  copycolor(ctmp, np->rdiff);
378                  dtmp = ldot * omega * (1./PI);
# Line 248 | Line 382 | dir_brdf(
382          if (ambRayInPmap(np->pr))
383                  return;         /* specular already in photon map */
384          /*
385 <         *  Compute reflection coefficient using BSDF.
385 >         *  Compute specular reflection coefficient using BSDF
386           */
387 <        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
387 >        if (!direct_specular_OK(ctmp, ldir, omega, np))
388                  return;
389          dtmp = ldot * omega;
390          scalecolor(ctmp, dtmp);
# Line 280 | Line 414 | dir_btdf(
414  
415          if (bright(np->tdiff) > FTINY) {
416                  /*
417 <                 *  Compute added diffuse transmission.
417 >                 *  Compute diffuse transmission
418                   */
419                  copycolor(ctmp, np->tdiff);
420                  dtmp = -ldot * omega * (1.0/PI);
# Line 290 | Line 424 | dir_btdf(
424          if (ambRayInPmap(np->pr))
425                  return;         /* specular already in photon map */
426          /*
427 <         *  Compute scattering coefficient using BSDF.
427 >         *  Compute specular scattering coefficient using BSDF
428           */
429 <        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
429 >        if (!direct_specular_OK(ctmp, ldir, omega, np))
430                  return;
431                                          /* full pattern on transmission */
432          multcolor(ctmp, np->pr->pcol);
# Line 348 | Line 482 | sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usep
482                          continue;               /* Russian roulette victim */
483                  }
484                                                  /* need to offset origin? */
485 <                if (ndp->thick != 0 && ndp->pr->rod > 0 ^ vsmp[2] > 0)
485 >                if (ndp->thick != 0 && (ndp->pr->rod > 0) ^ (vsmp[2] > 0))
486                          VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
487                  rayvalue(&sr);                  /* send & evaluate sample */
488                  multcolor(sr.rcol, sr.rcoef);
# Line 366 | Line 500 | sample_sdf(BSDFDAT *ndp, int sflags)
500          COLORV          *unsc;
501  
502          if (sflags == SDsampSpT) {
503 <                unsc = ndp->tdiff;
503 >                unsc = ndp->tunsamp;
504                  if (ndp->pr->rod > 0)
505                          dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
506                  else
507                          dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
508          } else /* sflags == SDsampSpR */ {
509 <                unsc = ndp->rdiff;
509 >                unsc = ndp->runsamp;
510                  if (ndp->pr->rod > 0)
511                          dfp = ndp->sd->rf;
512                  else
513                          dfp = ndp->sd->rb;
514          }
515 +        setcolor(unsc, 0., 0., 0.);
516          if (dfp == NULL)                        /* no specular component? */
517                  return(0);
518                                                  /* below sampling threshold? */
# Line 385 | Line 520 | sample_sdf(BSDFDAT *ndp, int sflags)
520                  if (dfp->maxHemi > FTINY) {     /* XXX no color from BSDF */
521                          FVECT   vjit;
522                          double  d;
388                        COLOR   ctmp;
523                          bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
524                          d = SDdirectHemi(vjit, sflags, ndp->sd);
525                          if (sflags == SDsampSpT) {
526 <                                copycolor(ctmp, ndp->pr->pcol);
527 <                                scalecolor(ctmp, d);
526 >                                copycolor(unsc, ndp->pr->pcol);
527 >                                scalecolor(unsc, d);
528                          } else                  /* no pattern on reflection */
529 <                                setcolor(ctmp, d, d, d);
396 <                        addcolor(unsc, ctmp);
529 >                                setcolor(unsc, d, d, d);
530                  }
531                  return(0);
532          }
# Line 431 | Line 564 | m_bsdf(OBJREC *m, RAY *r)
564          nd.thick = evalue(mf->ep[0]);
565          if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
566                  nd.thick = .0;
434                                                /* check shadow */
435        if (r->crtype & SHADOW) {
436                if (nd.thick != 0)
437                        raytrans(r);            /* pass-through */
438                return(1);                      /* or shadow */
439        }
567                                                  /* check backface visibility */
568          if (!hitfront & !backvis) {
569                  raytrans(r);
570                  return(1);
571          }
572                                                  /* check other rays to pass */
573 <        if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) ||
573 >        if (nd.thick != 0 && (r->crtype & SHADOW ||
574 >                                !(r->crtype & (SPECULAR|AMBIENT)) ||
575                                  (nd.thick > 0) ^ hitfront)) {
576                  raytrans(r);                    /* hide our proxy */
577                  return(1);
# Line 452 | Line 580 | m_bsdf(OBJREC *m, RAY *r)
580          nd.pr = r;
581                                                  /* get BSDF data */
582          nd.sd = loadBSDF(m->oargs.sarg[1]);
583 +                                                /* early shadow check */
584 +        if (r->crtype & SHADOW && (nd.sd->tf == NULL) & (nd.sd->tb == NULL))
585 +                return(1);
586                                                  /* diffuse reflectance */
587          if (hitfront) {
588                  cvt_sdcolor(nd.rdiff, &nd.sd->rLambFront);
# Line 505 | Line 636 | m_bsdf(OBJREC *m, RAY *r)
636                  nd.vray[2] = -r->rdir[2];
637                  ec = SDmapDir(nd.vray, nd.toloc, nd.vray);
638          }
508        if (!ec)
509                ec = SDinvXform(nd.fromloc, nd.toloc);
639          if (ec) {
640                  objerror(m, WARNING, "Illegal orientation vector");
641                  return(1);
642          }
643 <                                                /* determine BSDF resolution */
644 <        ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin+SDqueryMax, nd.sd);
643 >        compute_through(&nd);                   /* compute through component */
644 >        if (r->crtype & SHADOW) {
645 >                RAY     tr;                     /* attempt to pass shadow ray */
646 >                if (rayorigin(&tr, TRANS, r, nd.cthru) < 0)
647 >                        return(1);              /* blocked */
648 >                VCOPY(tr.rdir, r->rdir);
649 >                rayvalue(&tr);                  /* transmit with scaling */
650 >                multcolor(tr.rcol, tr.rcoef);
651 >                copycolor(r->rcol, tr.rcol);
652 >                return(1);                      /* we're done */
653 >        }
654 >        ec = SDinvXform(nd.fromloc, nd.toloc);
655 >        if (!ec)                                /* determine BSDF resolution */
656 >                ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL,
657 >                                        SDqueryMin+SDqueryMax, nd.sd);
658          if (ec)
659                  objerror(m, USER, transSDError(ec));
660  
# Line 528 | Line 670 | m_bsdf(OBJREC *m, RAY *r)
670                                                  /* sample transmission */
671          sample_sdf(&nd, SDsampSpT);
672                                                  /* compute indirect diffuse */
673 <        if (bright(nd.rdiff) > FTINY) {         /* ambient from reflection */
673 >        copycolor(ctmp, nd.rdiff);
674 >        addcolor(ctmp, nd.runsamp);
675 >        if (bright(ctmp) > FTINY) {             /* ambient from reflection */
676                  if (!hitfront)
677                          flipsurface(r);
534                copycolor(ctmp, nd.rdiff);
678                  multambient(ctmp, r, nd.pnorm);
679                  addcolor(r->rcol, ctmp);
680                  if (!hitfront)
681                          flipsurface(r);
682          }
683 <        if (bright(nd.tdiff) > FTINY) {         /* ambient from other side */
683 >        copycolor(ctmp, nd.tdiff);
684 >        addcolor(ctmp, nd.tunsamp);
685 >        if (bright(ctmp) > FTINY) {             /* ambient from other side */
686                  FVECT  bnorm;
687                  if (hitfront)
688                          flipsurface(r);
689                  bnorm[0] = -nd.pnorm[0];
690                  bnorm[1] = -nd.pnorm[1];
691                  bnorm[2] = -nd.pnorm[2];
547                copycolor(ctmp, nd.tdiff);
692                  if (nd.thick != 0) {            /* proxy with offset? */
693                          VCOPY(vtmp, r->rop);
694                          VSUM(r->rop, vtmp, r->ron, nd.thick);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines