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.35 by greg, Tue May 16 02:52:15 2017 UTC vs.
Revision 2.51 by greg, Mon Jun 25 23:04:06 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.
41 < *  A separate test prevents over-counting by dropping specular & ambient
42 < *  samples that are too close to this "through" direction.  The same
43 < *  restriction applies for the proxy case (thickness != 0).
39 > *      For the MAT_SBSDF 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_SBSDF
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_SBSDF 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_SBSDF */
90          SDData  *sd;            /* loaded BSDF data */
91          COLOR   rdiff;          /* diffuse reflection */
92 +        COLOR   runsamp;        /* BSDF hemispherical reflection */
93          COLOR   tdiff;          /* diffuse transmission */
94 +        COLOR   tunsamp;        /* BSDF hemispherical transmission */
95   }  BSDFDAT;             /* BSDF material data */
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_SBSDF */
100   static void
101   compute_through(BSDFDAT *ndp)
102   {
# Line 104 | 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;
123          COLOR           vpeak, vsum;
124 <        int             nsum, i;
124 >        int             i;
125          SDError         ec;
126  
115        setcolor(ndp->cthru, .0, .0, .0);       /* starting assumption */
116
127          if (ndp->pr->rod > 0)
128                  dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
129          else
# Line 124 | Line 134 | compute_through(BSDFDAT *ndp)
134          if (bright(ndp->pr->pcol) <= FTINY)
135                  return;                         /* pattern is black, here */
136          srchrad = sqrt(dfp->minProjSA);         /* else search for peak */
137 <        setcolor(vpeak, .0, .0, .0);
138 <        setcolor(vsum, .0, .0, .0);
139 <        nsum = 0;
137 >        setcolor(vpeak, 0, 0, 0);
138 >        setcolor(vsum, 0, 0, 0);
139 >        pdir[2] = 0.0;
140          for (i = 0; i < NDIR2CHECK; i++) {
141                  FVECT   tdir;
142                  SDValue sv;
# Line 134 | Line 144 | compute_through(BSDFDAT *ndp)
144                  tdir[0] = -ndp->vray[0] + dir2check[i][0]*srchrad;
145                  tdir[1] = -ndp->vray[1] + dir2check[i][1]*srchrad;
146                  tdir[2] = -ndp->vray[2];
147 <                if (normalize(tdir) == 0)
138 <                        continue;
147 >                normalize(tdir);
148                  ec = SDevalBSDF(&sv, tdir, ndp->vray, ndp->sd);
149                  if (ec)
150                          goto baderror;
151                  cvt_sdcolor(vcol, &sv);
152                  addcolor(vsum, vcol);
153 <                ++nsum;
145 <                if (bright(vcol) > bright(vpeak)) {
153 >                if (sv.cieY > bright(vpeak)) {
154                          copycolor(vpeak, vcol);
155                          VCOPY(pdir, tdir);
156                  }
157          }
158 +        if (pdir[2] == 0.0)
159 +                return;                         /* zero neighborhood */
160          ec = SDsizeBSDF(&tomega, pdir, ndp->vray, SDqueryMin, ndp->sd);
161          if (ec)
162                  goto baderror;
163          if (tomega > 1.5*dfp->minProjSA)
164                  return;                         /* not really a peak? */
165 <        if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .007)
166 <                return;                         /* < 0.7% transmission */
165 >        tomega /= fabs(pdir[2]);                /* remove cosine factor */
166 >        if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .001)
167 >                return;                         /* < 0.1% transmission */
168          for (i = 3; i--; )                      /* remove peak from average */
169                  colval(vsum,i) -= colval(vpeak,i);
170 <        --nsum;
160 <        if (peak_over*bright(vsum) >= nsum*bright(vpeak))
170 >        if (peak_over*bright(vsum) >= (NDIR2CHECK-1)*bright(vpeak))
171                  return;                         /* not peaky enough */
172          copycolor(ndp->cthru, vpeak);           /* else use it */
173          scalecolor(ndp->cthru, tomega);
# Line 186 | Line 196 | bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
196   static int
197   direct_specular_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
198   {
199 <        int     nsamp, ok = 0;
199 >        int     nsamp;
200 >        double  wtot = 0;
201          FVECT   vsrc, vsmp, vjit;
202 <        double  tomega;
202 >        double  tomega, tomega2;
203          double  sf, tsr, sd[2];
204          COLOR   csmp, cdiff;
205          double  diffY;
206          SDValue sv;
207          SDError ec;
208          int     i;
209 +                                        /* in case we fail */
210 +        setcolor(cval,  0, 0, 0);
211                                          /* transform source direction */
212          if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
213                  return(0);
# Line 220 | Line 233 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
233                  diffY = sv.cieY *= 1./PI;
234                  cvt_sdcolor(cdiff, &sv);
235          } else {
236 <                diffY = .0;
237 <                setcolor(cdiff, .0, .0, .0);
236 >                diffY = 0;
237 >                setcolor(cdiff,  0, 0, 0);
238          }
239 <                                        /* assign number of samples */
239 >                                        /* need projected solid angles */
240 >        omega *= fabs(vsrc[2]);
241          ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
242          if (ec)
243                  goto baderror;
244                                          /* check indirect over-counting */
245 <        if ((ndp->thick != 0 || bright(ndp->cthru) > FTINY)
232 <                                && ndp->pr->crtype & (SPECULAR|AMBIENT)
233 <                                && (vsrc[2] > 0) ^ (ndp->vray[2] > 0)) {
245 >        if ((vsrc[2] > 0) ^ (ndp->vray[2] > 0) && bright(ndp->cthru) > FTINY) {
246                  double  dx = vsrc[0] + ndp->vray[0];
247                  double  dy = vsrc[1] + ndp->vray[1];
248 <                if (dx*dx + dy*dy <= omega+tomega)
248 >                if (dx*dx + dy*dy <= (2.5*4./PI)*(omega + tomega +
249 >                                                2.*sqrt(omega*tomega)))
250                          return(0);
251          }
252 +                                        /* assign number of samples */
253          sf = specjitter * ndp->pr->rweight;
254 <        if (tomega <= .0)
254 >        if (tomega <= 0)
255                  nsamp = 1;
256          else if (25.*tomega <= omega)
257                  nsamp = 100.*sf + .5;
258          else
259                  nsamp = 4.*sf*omega/tomega + .5;
260          nsamp += !nsamp;
261 <        setcolor(cval, .0, .0, .0);     /* sample our source area */
248 <        sf = sqrt(omega);
261 >        sf = sqrt(omega);               /* sample our source area */
262          tsr = sqrt(tomega);
263          for (i = nsamp; i--; ) {
264                  VCOPY(vsmp, vsrc);      /* jitter query directions */
# Line 253 | Line 266 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
266                          multisamp(sd, 2, (i + frandom())/(double)nsamp);
267                          vsmp[0] += (sd[0] - .5)*sf;
268                          vsmp[1] += (sd[1] - .5)*sf;
269 <                        if (normalize(vsmp) == 0) {
257 <                                --nsamp;
258 <                                continue;
259 <                        }
269 >                        normalize(vsmp);
270                  }
271                  bsdf_jitter(vjit, ndp, tsr);
272                                          /* compute BSDF */
273                  ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
274                  if (ec)
275                          goto baderror;
276 <                if (sv.cieY - diffY <= FTINY) {
267 <                        addcolor(cval, cdiff);
276 >                if (sv.cieY - diffY <= FTINY)
277                          continue;       /* no specular part */
278 <                }
278 >                                        /* check for variable resolution */
279 >                ec = SDsizeBSDF(&tomega2, vjit, vsmp, SDqueryMin, ndp->sd);
280 >                if (ec)
281 >                        goto baderror;
282 >                if (tomega2 < .12*tomega)
283 >                        continue;       /* not safe to include */
284                  cvt_sdcolor(csmp, &sv);
285 <                addcolor(cval, csmp);   /* else average it in */
286 <                ++ok;
285 >
286 >                if (sf < 2.5*tsr) {     /* weight by Y for small sources */
287 >                        scalecolor(csmp, sv.cieY);
288 >                        wtot += sv.cieY;
289 >                } else
290 >                        wtot += 1.;
291 >                addcolor(cval, csmp);
292          }
293 <        if (!ok) {
294 <                setcolor(cval, .0, .0, .0);
295 <                return(0);              /* no valid specular samples */
296 <        }
278 <        sf = 1./(double)nsamp;
293 >        if (wtot <= FTINY)              /* no valid specular samples? */
294 >                return(0);
295 >
296 >        sf = 1./wtot;                   /* weighted average BSDF */
297          scalecolor(cval, sf);
298                                          /* subtract diffuse contribution */
299          for (i = 3*(diffY > FTINY); i--; )
300 <                if ((colval(cval,i) -= colval(cdiff,i)) < .0)
301 <                        colval(cval,i) = .0;
300 >                if ((colval(cval,i) -= colval(cdiff,i)) < 0)
301 >                        colval(cval,i) = 0;
302          return(1);
303   baderror:
304          objerror(ndp->mp, USER, transSDError(ec));
# Line 301 | Line 319 | dir_bsdf(
319          double          dtmp;
320          COLOR           ctmp;
321  
322 <        setcolor(cval, .0, .0, .0);
322 >        setcolor(cval,  0, 0, 0);
323  
324          ldot = DOT(np->pnorm, ldir);
325          if ((-FTINY <= ldot) & (ldot <= FTINY))
# Line 309 | Line 327 | dir_bsdf(
327  
328          if (ldot > 0 && bright(np->rdiff) > FTINY) {
329                  /*
330 <                 *  Compute added diffuse reflected component.
330 >                 *  Compute diffuse reflected component
331                   */
332                  copycolor(ctmp, np->rdiff);
333                  dtmp = ldot * omega * (1./PI);
# Line 318 | Line 336 | dir_bsdf(
336          }
337          if (ldot < 0 && bright(np->tdiff) > FTINY) {
338                  /*
339 <                 *  Compute added diffuse transmission.
339 >                 *  Compute diffuse transmission
340                   */
341                  copycolor(ctmp, np->tdiff);
342                  dtmp = -ldot * omega * (1.0/PI);
# Line 328 | Line 346 | dir_bsdf(
346          if (ambRayInPmap(np->pr))
347                  return;         /* specular already in photon map */
348          /*
349 <         *  Compute specular scattering coefficient using BSDF.
349 >         *  Compute specular scattering coefficient using BSDF
350           */
351          if (!direct_specular_OK(ctmp, ldir, omega, np))
352                  return;
# Line 355 | Line 373 | dir_brdf(
373          double          dtmp;
374          COLOR           ctmp, ctmp1, ctmp2;
375  
376 <        setcolor(cval, .0, .0, .0);
376 >        setcolor(cval,  0, 0, 0);
377  
378          ldot = DOT(np->pnorm, ldir);
379          
# Line 364 | Line 382 | dir_brdf(
382  
383          if (bright(np->rdiff) > FTINY) {
384                  /*
385 <                 *  Compute added diffuse reflected component.
385 >                 *  Compute diffuse reflected component
386                   */
387                  copycolor(ctmp, np->rdiff);
388                  dtmp = ldot * omega * (1./PI);
# Line 374 | Line 392 | dir_brdf(
392          if (ambRayInPmap(np->pr))
393                  return;         /* specular already in photon map */
394          /*
395 <         *  Compute specular reflection coefficient using BSDF.
395 >         *  Compute specular reflection coefficient using BSDF
396           */
397          if (!direct_specular_OK(ctmp, ldir, omega, np))
398                  return;
# Line 397 | Line 415 | dir_btdf(
415          double          dtmp;
416          COLOR           ctmp;
417  
418 <        setcolor(cval, .0, .0, .0);
418 >        setcolor(cval,  0, 0, 0);
419  
420          ldot = DOT(np->pnorm, ldir);
421  
# Line 406 | Line 424 | dir_btdf(
424  
425          if (bright(np->tdiff) > FTINY) {
426                  /*
427 <                 *  Compute added diffuse transmission.
427 >                 *  Compute diffuse transmission
428                   */
429                  copycolor(ctmp, np->tdiff);
430                  dtmp = -ldot * omega * (1.0/PI);
# Line 416 | Line 434 | dir_btdf(
434          if (ambRayInPmap(np->pr))
435                  return;         /* specular already in photon map */
436          /*
437 <         *  Compute specular scattering coefficient using BSDF.
437 >         *  Compute specular scattering coefficient using BSDF
438           */
439          if (!direct_specular_OK(ctmp, ldir, omega, np))
440                  return;
# Line 429 | Line 447 | dir_btdf(
447  
448   /* Sample separate BSDF component */
449   static int
450 < sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
450 > sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int xmit)
451   {
452 <        int     nstarget = 1;
453 <        int     nsent;
454 <        SDError ec;
455 <        SDValue bsv;
456 <        double  xrand;
457 <        FVECT   vsmp;
458 <        RAY     sr;
452 >        const int       hasthru = (xmit &&
453 >                                        !(ndp->pr->crtype & (SPECULAR|AMBIENT))
454 >                                        && bright(ndp->cthru) > FTINY);
455 >        int             nstarget = 1;
456 >        int             nsent = 0;
457 >        int             n;
458 >        SDError         ec;
459 >        SDValue         bsv;
460 >        double          xrand;
461 >        FVECT           vsmp, vinc;
462 >        RAY             sr;
463                                                  /* multiple samples? */
464          if (specjitter > 1.5) {
465                  nstarget = specjitter*ndp->pr->rweight + .5;
466                  nstarget += !nstarget;
467          }
468                                                  /* run through our samples */
469 <        for (nsent = 0; nsent < nstarget; nsent++) {
469 >        for (n = 0; n < nstarget; n++) {
470                  if (nstarget == 1) {            /* stratify random variable */
471                          xrand = urand(ilhash(dimlist,ndims)+samplendx);
472                          if (specjitter < 1.)
473                                  xrand = .5 + specjitter*(xrand-.5);
474                  } else {
475 <                        xrand = (nsent + frandom())/(double)nstarget;
475 >                        xrand = (n + frandom())/(double)nstarget;
476                  }
477                  SDerrorDetail[0] = '\0';        /* sample direction & coef. */
478                  bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]);
479 +                VCOPY(vinc, vsmp);              /* to compare after */
480                  ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
481                  if (ec)
482                          objerror(ndp->mp, USER, transSDError(ec));
483                  if (bsv.cieY <= FTINY)          /* zero component? */
484                          break;
485 <                                                /* map vector to world */
485 >                if (hasthru) {                  /* check for view ray */
486 >                        double  dx = vinc[0] + vsmp[0];
487 >                        double  dy = vinc[1] + vsmp[1];
488 >                        if (dx*dx + dy*dy <= ndp->sr_vpsa[0]*ndp->sr_vpsa[0])
489 >                                continue;       /* exclude view sample */
490 >                }
491 >                                                /* map non-view sample->world */
492                  if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
493                          break;
494                                                  /* spawn a specular ray */
495                  if (nstarget > 1)
496                          bsv.cieY /= (double)nstarget;
497                  cvt_sdcolor(sr.rcoef, &bsv);    /* use sample color */
498 <                if (usepat)                     /* apply pattern? */
498 >                if (xmit)                       /* apply pattern on transmit */
499                          multcolor(sr.rcoef, ndp->pr->pcol);
500                  if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
501 <                        if (maxdepth > 0)
502 <                                break;
503 <                        continue;               /* Russian roulette victim */
501 >                        if (!n & (nstarget > 1)) {
502 >                                n = nstarget;   /* avoid infinitue loop */
503 >                                nstarget = nstarget*sr.rweight/minweight;
504 >                                if (n == nstarget) break;
505 >                                n = -1;         /* moved target */
506 >                        }
507 >                        continue;               /* try again */
508                  }
509 <                                                /* need to offset origin? */
477 <                if (ndp->thick != 0 && (ndp->pr->rod > 0) ^ (vsmp[2] > 0))
509 >                if (xmit && ndp->thick != 0)    /* need to offset origin? */
510                          VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
511                  rayvalue(&sr);                  /* send & evaluate sample */
512                  multcolor(sr.rcol, sr.rcoef);
513                  addcolor(ndp->pr->rcol, sr.rcol);
514 +                ++nsent;
515          }
516          return(nsent);
517   }
# Line 487 | Line 520 | sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usep
520   static int
521   sample_sdf(BSDFDAT *ndp, int sflags)
522   {
523 +        int             hasthru = (sflags == SDsampSpT &&
524 +                                        !(ndp->pr->crtype & (SPECULAR|AMBIENT))
525 +                                        && bright(ndp->cthru) > FTINY);
526          int             n, ntotal = 0;
527 +        double          b = 0;
528          SDSpectralDF    *dfp;
529          COLORV          *unsc;
530  
531          if (sflags == SDsampSpT) {
532 <                unsc = ndp->tdiff;
532 >                unsc = ndp->tunsamp;
533                  if (ndp->pr->rod > 0)
534                          dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
535                  else
536                          dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
537          } else /* sflags == SDsampSpR */ {
538 <                unsc = ndp->rdiff;
538 >                unsc = ndp->runsamp;
539                  if (ndp->pr->rod > 0)
540                          dfp = ndp->sd->rf;
541                  else
542                          dfp = ndp->sd->rb;
543          }
544 +        setcolor(unsc,  0, 0, 0);
545          if (dfp == NULL)                        /* no specular component? */
546                  return(0);
547 <                                                /* below sampling threshold? */
548 <        if (dfp->maxHemi <= specthresh+FTINY) {
549 <                if (dfp->maxHemi > FTINY) {     /* XXX no color from BSDF */
550 <                        FVECT   vjit;
551 <                        double  d;
552 <                        COLOR   ctmp;
553 <                        bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
554 <                        d = SDdirectHemi(vjit, sflags, ndp->sd);
547 >
548 >        if (hasthru) {                          /* separate view sample? */
549 >                RAY     tr;
550 >                if (rayorigin(&tr, TRANS, ndp->pr, ndp->cthru) == 0) {
551 >                        VCOPY(tr.rdir, ndp->pr->rdir);
552 >                        rayvalue(&tr);
553 >                        multcolor(tr.rcol, tr.rcoef);
554 >                        addcolor(ndp->pr->rcol, tr.rcol);
555 >                        ++ntotal;
556 >                        b = bright(ndp->cthru);
557 >                } else
558 >                        hasthru = 0;
559 >        }
560 >        if (dfp->maxHemi - b <= FTINY) {        /* have specular to sample? */
561 >                b = 0;
562 >        } else {
563 >                FVECT   vjit;
564 >                bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
565 >                b = SDdirectHemi(vjit, sflags, ndp->sd) - b;
566 >                if (b < 0) b = 0;
567 >        }
568 >        if (b <= specthresh+FTINY) {            /* below sampling threshold? */
569 >                if (b > FTINY) {                /* XXX no color from BSDF */
570                          if (sflags == SDsampSpT) {
571 <                                copycolor(ctmp, ndp->pr->pcol);
572 <                                scalecolor(ctmp, d);
571 >                                copycolor(unsc, ndp->pr->pcol);
572 >                                scalecolor(unsc, b);
573                          } else                  /* no pattern on reflection */
574 <                                setcolor(ctmp, d, d, d);
522 <                        addcolor(unsc, ctmp);
574 >                                setcolor(unsc, b, b, b);
575                  }
576 <                return(0);
576 >                return(ntotal);
577          }
578 <                                                /* else need to sample */
579 <        dimlist[ndims++] = (int)(size_t)ndp->mp;
528 <        ndims++;
578 >        dimlist[ndims] = (int)(size_t)ndp->mp;  /* else sample specular */
579 >        ndims += 2;
580          for (n = dfp->ncomp; n--; ) {           /* loop over components */
581                  dimlist[ndims-1] = n + 9438;
582                  ntotal += sample_sdcomp(ndp, &dfp->comp[n], sflags==SDsampSpT);
# Line 538 | Line 589 | sample_sdf(BSDFDAT *ndp, int sflags)
589   int
590   m_bsdf(OBJREC *m, RAY *r)
591   {
592 +        int     hasthick = (m->otype == MAT_BSDF);
593          int     hitfront;
594          COLOR   ctmp;
595          SDError ec;
# Line 545 | Line 597 | m_bsdf(OBJREC *m, RAY *r)
597          MFUNC   *mf;
598          BSDFDAT nd;
599                                                  /* check arguments */
600 <        if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) |
600 >        if ((m->oargs.nsargs < hasthick+5) | (m->oargs.nfargs > 9) |
601                                  (m->oargs.nfargs % 3))
602                  objerror(m, USER, "bad # arguments");
603                                                  /* record surface struck */
604          hitfront = (r->rod > 0);
605                                                  /* load cal file */
606 <        mf = getfunc(m, 5, 0x1d, 1);
606 >        mf = hasthick   ? getfunc(m, 5, 0x1d, 1)
607 >                        : getfunc(m, 4, 0xe, 1) ;
608          setfunc(m, r);
609 <                                                /* get thickness */
610 <        nd.thick = evalue(mf->ep[0]);
611 <        if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
612 <                nd.thick = .0;
609 >        nd.thick = 0;                           /* set thickness */
610 >        if (hasthick) {
611 >                nd.thick = evalue(mf->ep[0]);
612 >                if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
613 >                        nd.thick = 0;
614 >        }
615                                                  /* check backface visibility */
616          if (!hitfront & !backvis) {
617                  raytrans(r);
# Line 569 | Line 624 | m_bsdf(OBJREC *m, RAY *r)
624                  raytrans(r);                    /* hide our proxy */
625                  return(1);
626          }
627 +        if (hasthick && r->crtype & SHADOW)     /* early shadow check #1 */
628 +                return(1);
629          nd.mp = m;
630          nd.pr = r;
631                                                  /* get BSDF data */
632 <        nd.sd = loadBSDF(m->oargs.sarg[1]);
633 <                                                /* early shadow check */
632 >        nd.sd = loadBSDF(m->oargs.sarg[hasthick]);
633 >                                                /* early shadow check #2 */
634          if (r->crtype & SHADOW && (nd.sd->tf == NULL) & (nd.sd->tb == NULL))
635                  return(1);
636                                                  /* diffuse reflectance */
# Line 608 | Line 665 | m_bsdf(OBJREC *m, RAY *r)
665          multcolor(nd.rdiff, r->pcol);
666          multcolor(nd.tdiff, r->pcol);
667                                                  /* get up vector */
668 <        upvec[0] = evalue(mf->ep[1]);
669 <        upvec[1] = evalue(mf->ep[2]);
670 <        upvec[2] = evalue(mf->ep[3]);
668 >        upvec[0] = evalue(mf->ep[hasthick+0]);
669 >        upvec[1] = evalue(mf->ep[hasthick+1]);
670 >        upvec[2] = evalue(mf->ep[hasthick+2]);
671                                                  /* return to world coords */
672          if (mf->fxp != &unitxf) {
673                  multv3(upvec, upvec, mf->fxp->xfm);
# Line 633 | Line 690 | m_bsdf(OBJREC *m, RAY *r)
690                  objerror(m, WARNING, "Illegal orientation vector");
691                  return(1);
692          }
693 <        compute_through(&nd);                   /* compute through component */
694 <        if (r->crtype & SHADOW) {
695 <                RAY     tr;                     /* attempt to pass shadow ray */
696 <                if (rayorigin(&tr, TRANS, r, nd.cthru) < 0)
697 <                        return(1);              /* blocked */
698 <                VCOPY(tr.rdir, r->rdir);
699 <                rayvalue(&tr);                  /* transmit with scaling */
700 <                multcolor(tr.rcol, tr.rcoef);
701 <                copycolor(r->rcol, tr.rcol);
702 <                return(1);                      /* we're done */
693 >        setcolor(nd.cthru, 0, 0, 0);            /* consider through component */
694 >        if (m->otype == MAT_SBSDF) {
695 >                compute_through(&nd);
696 >                if (r->crtype & SHADOW) {
697 >                        RAY     tr;             /* attempt to pass shadow ray */
698 >                        if (rayorigin(&tr, TRANS, r, nd.cthru) < 0)
699 >                                return(1);      /* no through component */
700 >                        VCOPY(tr.rdir, r->rdir);
701 >                        rayvalue(&tr);          /* transmit with scaling */
702 >                        multcolor(tr.rcol, tr.rcoef);
703 >                        copycolor(r->rcol, tr.rcol);
704 >                        return(1);              /* we're done */
705 >                }
706          }
707          ec = SDinvXform(nd.fromloc, nd.toloc);
708          if (!ec)                                /* determine BSDF resolution */
# Line 663 | Line 723 | m_bsdf(OBJREC *m, RAY *r)
723                                                  /* sample transmission */
724          sample_sdf(&nd, SDsampSpT);
725                                                  /* compute indirect diffuse */
726 <        if (bright(nd.rdiff) > FTINY) {         /* ambient from reflection */
726 >        copycolor(ctmp, nd.rdiff);
727 >        addcolor(ctmp, nd.runsamp);
728 >        if (bright(ctmp) > FTINY) {             /* ambient from reflection */
729                  if (!hitfront)
730                          flipsurface(r);
669                copycolor(ctmp, nd.rdiff);
731                  multambient(ctmp, r, nd.pnorm);
732                  addcolor(r->rcol, ctmp);
733                  if (!hitfront)
734                          flipsurface(r);
735          }
736 <        if (bright(nd.tdiff) > FTINY) {         /* ambient from other side */
736 >        copycolor(ctmp, nd.tdiff);
737 >        addcolor(ctmp, nd.tunsamp);
738 >        if (bright(ctmp) > FTINY) {             /* ambient from other side */
739                  FVECT  bnorm;
740                  if (hitfront)
741                          flipsurface(r);
742                  bnorm[0] = -nd.pnorm[0];
743                  bnorm[1] = -nd.pnorm[1];
744                  bnorm[2] = -nd.pnorm[2];
682                copycolor(ctmp, nd.tdiff);
745                  if (nd.thick != 0) {            /* proxy with offset? */
746                          VCOPY(vtmp, r->rop);
747                          VSUM(r->rop, vtmp, r->ron, nd.thick);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines