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.15 by greg, Mon Aug 22 16:00:47 2011 UTC vs.
Revision 2.45 by greg, Fri Jan 5 02:47:46 2018 UTC

# Line 13 | Line 13 | static const char RCSid[] = "$Id$";
13   #include  "func.h"
14   #include  "bsdf.h"
15   #include  "random.h"
16 + #include  "pmapmat.h"
17  
18   /*
19   *      Arguments to this material include optional diffuse colors.
# Line 22 | 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
29 < *  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 35 | 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 samples that are
41 + *  too close to this "through" direction.  BSDFs with such a through direction
42 + *  will also have a view component, meaning they are somewhat see-through.
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 42 | 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 58 | 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 71 | 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   rdiff;          /* added diffuse reflection */
83 >        COLOR   tdiff;          /* diffuse transmission */
84          COLOR   tunsamp;        /* BSDF hemispherical transmission */
78        COLOR   tdiff;          /* added diffuse 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 = 1.5;
110 +        SDSpectralDF    *dfp;
111 +        FVECT           pdir;
112 +        double          tomega, srchrad;
113 +        COLOR           vpeak, vsum;
114 +        int             i;
115 +        SDError         ec;
116 +
117 +        setcolor(ndp->cthru, 0, 0, 0);          /* starting assumption */
118 +
119 +        if (ndp->pr->rod > 0)
120 +                dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
121 +        else
122 +                dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
123 +
124 +        if (dfp == NULL)
125 +                return;                         /* no specular transmission */
126 +        if (bright(ndp->pr->pcol) <= FTINY)
127 +                return;                         /* pattern is black, here */
128 +        srchrad = sqrt(dfp->minProjSA);         /* else search for peak */
129 +        setcolor(vpeak, 0, 0, 0);
130 +        setcolor(vsum, 0, 0, 0);
131 +        pdir[2] = 0.0;
132 +        for (i = 0; i < NDIR2CHECK; i++) {
133 +                FVECT   tdir;
134 +                SDValue sv;
135 +                COLOR   vcol;
136 +                tdir[0] = -ndp->vray[0] + dir2check[i][0]*srchrad;
137 +                tdir[1] = -ndp->vray[1] + dir2check[i][1]*srchrad;
138 +                tdir[2] = -ndp->vray[2];
139 +                normalize(tdir);
140 +                ec = SDevalBSDF(&sv, tdir, ndp->vray, ndp->sd);
141 +                if (ec)
142 +                        goto baderror;
143 +                cvt_sdcolor(vcol, &sv);
144 +                addcolor(vsum, vcol);
145 +                if (sv.cieY > bright(vpeak)) {
146 +                        copycolor(vpeak, vcol);
147 +                        VCOPY(pdir, tdir);
148 +                }
149 +        }
150 +        if (pdir[2] == 0.0)
151 +                return;                         /* zero neighborhood */
152 +        ec = SDsizeBSDF(&tomega, pdir, ndp->vray, SDqueryMin, ndp->sd);
153 +        if (ec)
154 +                goto baderror;
155 +        if (tomega > 1.5*dfp->minProjSA)
156 +                return;                         /* not really a peak? */
157 +        tomega /= fabs(pdir[2]);                /* remove cosine factor */
158 +        if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .001)
159 +                return;                         /* < 0.1% transmission */
160 +        for (i = 3; i--; )                      /* remove peak from average */
161 +                colval(vsum,i) -= colval(vpeak,i);
162 +        if (peak_over*bright(vsum) >= (NDIR2CHECK-1)*bright(vpeak))
163 +                return;                         /* not peaky enough */
164 +        copycolor(ndp->cthru, vpeak);           /* else use it */
165 +        scalecolor(ndp->cthru, tomega);
166 +        multcolor(ndp->cthru, ndp->pr->pcol);   /* modify by pattern */
167 +        return;
168 + baderror:
169 +        objerror(ndp->mp, USER, transSDError(ec));
170 + #undef NDIR2CHECK
171 + }
172 +
173   /* Jitter ray sample according to projected solid angle and specjitter */
174   static void
175   bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
# Line 94 | Line 184 | bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
184          normalize(vres);
185   }
186  
187 < /* Evaluate BSDF for direct component, returning true if OK to proceed */
187 > /* Get BSDF specular for direct component, returning true if OK to proceed */
188   static int
189 < direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
189 > direct_specular_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
190   {
191 <        int     nsamp, ok = 0;
191 >        int     nsamp;
192 >        double  wtot = 0;
193          FVECT   vsrc, vsmp, vjit;
194 <        double  tomega;
194 >        double  tomega, tomega2;
195          double  sf, tsr, sd[2];
196 <        COLOR   csmp;
196 >        COLOR   csmp, cdiff;
197 >        double  diffY;
198          SDValue sv;
199          SDError ec;
200          int     i;
201 +                                        /* in case we fail */
202 +        setcolor(cval,  0, 0, 0);
203                                          /* transform source direction */
204          if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
205                  return(0);
206 +                                        /* will discount diffuse portion */
207 +        switch ((vsrc[2] > 0)<<1 | (ndp->vray[2] > 0)) {
208 +        case 3:
209 +                if (ndp->sd->rf == NULL)
210 +                        return(0);      /* all diffuse */
211 +                sv = ndp->sd->rLambFront;
212 +                break;
213 +        case 0:
214 +                if (ndp->sd->rb == NULL)
215 +                        return(0);      /* all diffuse */
216 +                sv = ndp->sd->rLambBack;
217 +                break;
218 +        default:
219 +                if ((ndp->sd->tf == NULL) & (ndp->sd->tb == NULL))
220 +                        return(0);      /* all diffuse */
221 +                sv = ndp->sd->tLamb;
222 +                break;
223 +        }
224 +        if (sv.cieY > FTINY) {
225 +                diffY = sv.cieY *= 1./PI;
226 +                cvt_sdcolor(cdiff, &sv);
227 +        } else {
228 +                diffY = 0;
229 +                setcolor(cdiff,  0, 0, 0);
230 +        }
231 +                                        /* need projected solid angles */
232 +        omega *= fabs(vsrc[2]);
233 +        ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
234 +        if (ec)
235 +                goto baderror;
236                                          /* check indirect over-counting */
237 <        if (ndp->thick != 0 && ndp->pr->crtype & (SPECULAR|AMBIENT)
114 <                                && vsrc[2] > 0 ^ ndp->vray[2] > 0) {
237 >        if ((vsrc[2] > 0) ^ (ndp->vray[2] > 0) && bright(ndp->cthru) > FTINY) {
238                  double  dx = vsrc[0] + ndp->vray[0];
239                  double  dy = vsrc[1] + ndp->vray[1];
240 <                if (dx*dx + dy*dy <= omega*(1./PI))
240 >                if (dx*dx + dy*dy <= (4./PI)*(omega + tomega +
241 >                                                2.*sqrt(omega*tomega)))
242                          return(0);
243          }
244                                          /* assign number of samples */
121        ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
122        if (ec)
123                goto baderror;
245          sf = specjitter * ndp->pr->rweight;
246 <        if (25.*tomega <= omega)
246 >        if (tomega <= 0)
247 >                nsamp = 1;
248 >        else if (25.*tomega <= omega)
249                  nsamp = 100.*sf + .5;
250          else
251                  nsamp = 4.*sf*omega/tomega + .5;
252          nsamp += !nsamp;
253 <        setcolor(cval, .0, .0, .0);     /* sample our source area */
131 <        sf = sqrt(omega);
253 >        sf = sqrt(omega);               /* sample our source area */
254          tsr = sqrt(tomega);
255          for (i = nsamp; i--; ) {
256                  VCOPY(vsmp, vsrc);      /* jitter query directions */
# Line 136 | Line 258 | direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, B
258                          multisamp(sd, 2, (i + frandom())/(double)nsamp);
259                          vsmp[0] += (sd[0] - .5)*sf;
260                          vsmp[1] += (sd[1] - .5)*sf;
261 <                        if (normalize(vsmp) == 0) {
140 <                                --nsamp;
141 <                                continue;
142 <                        }
261 >                        normalize(vsmp);
262                  }
263                  bsdf_jitter(vjit, ndp, tsr);
264                                          /* compute BSDF */
265                  ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
266                  if (ec)
267                          goto baderror;
268 <                if (sv.cieY <= FTINY)   /* worth using? */
269 <                        continue;
268 >                if (sv.cieY - diffY <= FTINY)
269 >                        continue;       /* no specular part */
270 >                                        /* check for variable resolution */
271 >                ec = SDsizeBSDF(&tomega2, vjit, vsmp, SDqueryMin, ndp->sd);
272 >                if (ec)
273 >                        goto baderror;
274 >                if (tomega2 < .12*tomega)
275 >                        continue;       /* not safe to include */
276                  cvt_sdcolor(csmp, &sv);
277 <                addcolor(cval, csmp);   /* average it in */
278 <                ++ok;
277 >
278 >                if (sf < 2.5*tsr) {     /* weight by Y for small sources */
279 >                        scalecolor(csmp, sv.cieY);
280 >                        wtot += sv.cieY;
281 >                } else
282 >                        wtot += 1.;
283 >                addcolor(cval, csmp);
284          }
285 <        sf = 1./(double)nsamp;
285 >        if (wtot <= FTINY)              /* no valid specular samples? */
286 >                return(0);
287 >
288 >        sf = 1./wtot;                   /* weighted average BSDF */
289          scalecolor(cval, sf);
290 <        return(ok);
290 >                                        /* subtract diffuse contribution */
291 >        for (i = 3*(diffY > FTINY); i--; )
292 >                if ((colval(cval,i) -= colval(cdiff,i)) < 0)
293 >                        colval(cval,i) = 0;
294 >        return(1);
295   baderror:
296          objerror(ndp->mp, USER, transSDError(ec));
297 +        return(0);                      /* gratis return */
298   }
299  
300   /* Compute source contribution for BSDF (reflected & transmitted) */
# Line 173 | Line 311 | dir_bsdf(
311          double          dtmp;
312          COLOR           ctmp;
313  
314 <        setcolor(cval, .0, .0, .0);
314 >        setcolor(cval,  0, 0, 0);
315  
316          ldot = DOT(np->pnorm, ldir);
317          if ((-FTINY <= ldot) & (ldot <= FTINY))
# Line 181 | Line 319 | dir_bsdf(
319  
320          if (ldot > 0 && bright(np->rdiff) > FTINY) {
321                  /*
322 <                 *  Compute added diffuse reflected component.
322 >                 *  Compute diffuse reflected component
323                   */
324                  copycolor(ctmp, np->rdiff);
325                  dtmp = ldot * omega * (1./PI);
# Line 190 | Line 328 | dir_bsdf(
328          }
329          if (ldot < 0 && bright(np->tdiff) > FTINY) {
330                  /*
331 <                 *  Compute added diffuse transmission.
331 >                 *  Compute diffuse transmission
332                   */
333                  copycolor(ctmp, np->tdiff);
334                  dtmp = -ldot * omega * (1.0/PI);
335                  scalecolor(ctmp, dtmp);
336                  addcolor(cval, ctmp);
337          }
338 +        if (ambRayInPmap(np->pr))
339 +                return;         /* specular already in photon map */
340          /*
341 <         *  Compute scattering coefficient using BSDF.
341 >         *  Compute specular scattering coefficient using BSDF
342           */
343 <        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
343 >        if (!direct_specular_OK(ctmp, ldir, omega, np))
344                  return;
345 <        if (ldot > 0) {         /* pattern only diffuse reflection */
206 <                COLOR   ctmp1, ctmp2;
207 <                dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
208 <                                        : np->sd->rLambBack.cieY;
209 <                                        /* diffuse fraction */
210 <                dtmp /= PI * bright(ctmp);
211 <                copycolor(ctmp2, np->pr->pcol);
212 <                scalecolor(ctmp2, dtmp);
213 <                setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
214 <                addcolor(ctmp1, ctmp2);
215 <                multcolor(ctmp, ctmp1); /* apply derated pattern */
216 <                dtmp = ldot * omega;
217 <        } else {                        /* full pattern on transmission */
345 >        if (ldot < 0) {         /* pattern for specular transmission */
346                  multcolor(ctmp, np->pr->pcol);
347                  dtmp = -ldot * omega;
348 <        }
348 >        } else
349 >                dtmp = ldot * omega;
350          scalecolor(ctmp, dtmp);
351          addcolor(cval, ctmp);
352   }
# Line 236 | Line 365 | dir_brdf(
365          double          dtmp;
366          COLOR           ctmp, ctmp1, ctmp2;
367  
368 <        setcolor(cval, .0, .0, .0);
368 >        setcolor(cval,  0, 0, 0);
369  
370          ldot = DOT(np->pnorm, ldir);
371          
# Line 245 | Line 374 | dir_brdf(
374  
375          if (bright(np->rdiff) > FTINY) {
376                  /*
377 <                 *  Compute added diffuse reflected component.
377 >                 *  Compute diffuse reflected component
378                   */
379                  copycolor(ctmp, np->rdiff);
380                  dtmp = ldot * omega * (1./PI);
381                  scalecolor(ctmp, dtmp);
382                  addcolor(cval, ctmp);
383          }
384 +        if (ambRayInPmap(np->pr))
385 +                return;         /* specular already in photon map */
386          /*
387 <         *  Compute reflection coefficient using BSDF.
387 >         *  Compute specular reflection coefficient using BSDF
388           */
389 <        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
389 >        if (!direct_specular_OK(ctmp, ldir, omega, np))
390                  return;
260                                        /* pattern only diffuse reflection */
261        dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
262                                : np->sd->rLambBack.cieY;
263        dtmp /= PI * bright(ctmp);      /* diffuse fraction */
264        copycolor(ctmp2, np->pr->pcol);
265        scalecolor(ctmp2, dtmp);
266        setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
267        addcolor(ctmp1, ctmp2);
268        multcolor(ctmp, ctmp1);         /* apply derated pattern */
391          dtmp = ldot * omega;
392          scalecolor(ctmp, dtmp);
393          addcolor(cval, ctmp);
# Line 285 | Line 407 | dir_btdf(
407          double          dtmp;
408          COLOR           ctmp;
409  
410 <        setcolor(cval, .0, .0, .0);
410 >        setcolor(cval,  0, 0, 0);
411  
412          ldot = DOT(np->pnorm, ldir);
413  
# Line 294 | Line 416 | dir_btdf(
416  
417          if (bright(np->tdiff) > FTINY) {
418                  /*
419 <                 *  Compute added diffuse transmission.
419 >                 *  Compute diffuse transmission
420                   */
421                  copycolor(ctmp, np->tdiff);
422                  dtmp = -ldot * omega * (1.0/PI);
423                  scalecolor(ctmp, dtmp);
424                  addcolor(cval, ctmp);
425          }
426 +        if (ambRayInPmap(np->pr))
427 +                return;         /* specular already in photon map */
428          /*
429 <         *  Compute scattering coefficient using BSDF.
429 >         *  Compute specular scattering coefficient using BSDF
430           */
431 <        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
431 >        if (!direct_specular_OK(ctmp, ldir, omega, np))
432                  return;
433                                          /* full pattern on transmission */
434          multcolor(ctmp, np->pr->pcol);
# Line 315 | Line 439 | dir_btdf(
439  
440   /* Sample separate BSDF component */
441   static int
442 < sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
442 > sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int xmit)
443   {
444 <        int     nstarget = 1;
445 <        int     nsent;
446 <        SDError ec;
447 <        SDValue bsv;
448 <        double  xrand;
449 <        FVECT   vsmp;
450 <        RAY     sr;
444 >        const int       hasthru = (xmit &&
445 >                                !(ndp->pr->crtype & (SPECULAR|AMBIENT)) &&
446 >                                bright(ndp->cthru) > FTINY);
447 >        int             nstarget = 1;
448 >        int             nsent = 0;
449 >        int             n;
450 >        SDError         ec;
451 >        SDValue         bsv;
452 >        double          xrand;
453 >        FVECT           vsmp, vinc;
454 >        RAY             sr;
455                                                  /* multiple samples? */
456          if (specjitter > 1.5) {
457                  nstarget = specjitter*ndp->pr->rweight + .5;
458                  nstarget += !nstarget;
459          }
460                                                  /* run through our samples */
461 <        for (nsent = 0; nsent < nstarget; nsent++) {
461 >        for (n = 0; n < nstarget; n++) {
462                  if (nstarget == 1) {            /* stratify random variable */
463                          xrand = urand(ilhash(dimlist,ndims)+samplendx);
464                          if (specjitter < 1.)
465                                  xrand = .5 + specjitter*(xrand-.5);
466                  } else {
467 <                        xrand = (nsent + frandom())/(double)nstarget;
467 >                        xrand = (n + frandom())/(double)nstarget;
468                  }
469                  SDerrorDetail[0] = '\0';        /* sample direction & coef. */
470                  bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]);
471 +                VCOPY(vinc, vsmp);              /* to compare after */
472                  ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
473                  if (ec)
474                          objerror(ndp->mp, USER, transSDError(ec));
475                  if (bsv.cieY <= FTINY)          /* zero component? */
476                          break;
477 <                                                /* map vector to world */
477 >                if (hasthru) {                  /* check for view ray */
478 >                        double  dx = vinc[0] + vsmp[0];
479 >                        double  dy = vinc[1] + vsmp[1];
480 >                        if (dx*dx + dy*dy <= ndp->sr_vpsa[0]*ndp->sr_vpsa[0])
481 >                                continue;       /* exclude view sample */
482 >                }
483 >                                                /* map non-view sample->world */
484                  if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
485                          break;
486                                                  /* spawn a specular ray */
487                  if (nstarget > 1)
488                          bsv.cieY /= (double)nstarget;
489                  cvt_sdcolor(sr.rcoef, &bsv);    /* use sample color */
490 <                if (usepat)                     /* apply pattern? */
490 >                if (xmit)                       /* apply pattern on transmit */
491                          multcolor(sr.rcoef, ndp->pr->pcol);
492                  if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
493                          if (maxdepth > 0)
494                                  break;
495                          continue;               /* Russian roulette victim */
496                  }
497 <                                                /* need to offset origin? */
363 <                if (ndp->thick != 0 && ndp->pr->rod > 0 ^ vsmp[2] > 0)
497 >                if (xmit && ndp->thick != 0)    /* need to offset origin? */
498                          VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
499                  rayvalue(&sr);                  /* send & evaluate sample */
500                  multcolor(sr.rcol, sr.rcoef);
501                  addcolor(ndp->pr->rcol, sr.rcol);
502 +                ++nsent;
503          }
504          return(nsent);
505   }
# Line 373 | Line 508 | sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usep
508   static int
509   sample_sdf(BSDFDAT *ndp, int sflags)
510   {
511 +        int             hasthru = (sflags == SDsampSpT
512 +                                    && !(ndp->pr->crtype & (SPECULAR|AMBIENT))
513 +                                    && bright(ndp->cthru) > FTINY);
514          int             n, ntotal = 0;
515 +        double          b = 0;
516          SDSpectralDF    *dfp;
517          COLORV          *unsc;
518  
519          if (sflags == SDsampSpT) {
520                  unsc = ndp->tunsamp;
521 <                dfp = ndp->sd->tf;
522 <                cvt_sdcolor(unsc, &ndp->sd->tLamb);
521 >                if (ndp->pr->rod > 0)
522 >                        dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
523 >                else
524 >                        dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
525          } else /* sflags == SDsampSpR */ {
526                  unsc = ndp->runsamp;
527 <                if (ndp->pr->rod > 0) {
527 >                if (ndp->pr->rod > 0)
528                          dfp = ndp->sd->rf;
529 <                        cvt_sdcolor(unsc, &ndp->sd->rLambFront);
389 <                } else {
529 >                else
530                          dfp = ndp->sd->rb;
391                        cvt_sdcolor(unsc, &ndp->sd->rLambBack);
392                }
531          }
532 <        multcolor(unsc, ndp->pr->pcol);
532 >        setcolor(unsc,  0, 0, 0);
533          if (dfp == NULL)                        /* no specular component? */
534                  return(0);
535 <                                                /* below sampling threshold? */
536 <        if (dfp->maxHemi <= specthresh+FTINY) {
537 <                if (dfp->maxHemi > FTINY) {     /* XXX no color from BSDF */
538 <                        FVECT   vjit;
539 <                        double  d;
540 <                        COLOR   ctmp;
541 <                        bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
542 <                        d = SDdirectHemi(vjit, sflags, ndp->sd);
535 >
536 >        if (hasthru) {                          /* separate view sample? */
537 >                RAY     tr;
538 >                if (rayorigin(&tr, TRANS, ndp->pr, ndp->cthru) == 0) {
539 >                        VCOPY(tr.rdir, ndp->pr->rdir);
540 >                        rayvalue(&tr);
541 >                        multcolor(tr.rcol, tr.rcoef);
542 >                        addcolor(ndp->pr->rcol, tr.rcol);
543 >                        ++ntotal;
544 >                        b = bright(ndp->cthru);
545 >                } else
546 >                        hasthru = 0;
547 >        }
548 >        if (dfp->maxHemi - b <= FTINY) {        /* have specular to sample? */
549 >                b = 0;
550 >        } else {
551 >                FVECT   vjit;
552 >                bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
553 >                b = SDdirectHemi(vjit, sflags, ndp->sd) - b;
554 >                if (b < 0) b = 0;
555 >        }
556 >        if (b <= specthresh+FTINY) {            /* below sampling threshold? */
557 >                if (b > FTINY) {                /* XXX no color from BSDF */
558                          if (sflags == SDsampSpT) {
559 <                                copycolor(ctmp, ndp->pr->pcol);
560 <                                scalecolor(ctmp, d);
559 >                                copycolor(unsc, ndp->pr->pcol);
560 >                                scalecolor(unsc, b);
561                          } else                  /* no pattern on reflection */
562 <                                setcolor(ctmp, d, d, d);
410 <                        addcolor(unsc, ctmp);
562 >                                setcolor(unsc, b, b, b);
563                  }
564 <                return(0);
564 >                return(ntotal);
565          }
566 <                                                /* else need to sample */
567 <        dimlist[ndims++] = (int)(size_t)ndp->mp;
416 <        ndims++;
566 >        dimlist[ndims] = (int)(size_t)ndp->mp;  /* else sample specular */
567 >        ndims += 2;
568          for (n = dfp->ncomp; n--; ) {           /* loop over components */
569                  dimlist[ndims-1] = n + 9438;
570                  ntotal += sample_sdcomp(ndp, &dfp->comp[n], sflags==SDsampSpT);
# Line 440 | Line 591 | m_bsdf(OBJREC *m, RAY *r)
591          hitfront = (r->rod > 0);
592                                                  /* load cal file */
593          mf = getfunc(m, 5, 0x1d, 1);
594 +        setfunc(m, r);
595                                                  /* get thickness */
596          nd.thick = evalue(mf->ep[0]);
597          if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
598 <                nd.thick = .0;
599 <                                                /* check shadow */
600 <        if (r->crtype & SHADOW) {
601 <                if (nd.thick != 0)
602 <                        raytrans(r);            /* pass-through */
451 <                return(1);                      /* or shadow */
598 >                nd.thick = 0;
599 >                                                /* check backface visibility */
600 >        if (!hitfront & !backvis) {
601 >                raytrans(r);
602 >                return(1);
603          }
604                                                  /* check other rays to pass */
605 <        if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) ||
606 <                                nd.thick > 0 ^ hitfront)) {
605 >        if (nd.thick != 0 && (r->crtype & SHADOW ||
606 >                                !(r->crtype & (SPECULAR|AMBIENT)) ||
607 >                                (nd.thick > 0) ^ hitfront)) {
608                  raytrans(r);                    /* hide our proxy */
609                  return(1);
610          }
611 +        nd.mp = m;
612 +        nd.pr = r;
613                                                  /* get BSDF data */
614          nd.sd = loadBSDF(m->oargs.sarg[1]);
615 +                                                /* early shadow check */
616 +        if (r->crtype & SHADOW && (nd.sd->tf == NULL) & (nd.sd->tb == NULL))
617 +                return(1);
618                                                  /* diffuse reflectance */
619          if (hitfront) {
620 <                if (m->oargs.nfargs < 3)
621 <                        setcolor(nd.rdiff, .0, .0, .0);
622 <                else
466 <                        setcolor(nd.rdiff, m->oargs.farg[0],
620 >                cvt_sdcolor(nd.rdiff, &nd.sd->rLambFront);
621 >                if (m->oargs.nfargs >= 3) {
622 >                        setcolor(ctmp, m->oargs.farg[0],
623                                          m->oargs.farg[1],
624                                          m->oargs.farg[2]);
625 +                        addcolor(nd.rdiff, ctmp);
626 +                }
627          } else {
628 <                if (m->oargs.nfargs < 6) {      /* check invisible backside */
629 <                        if (!backvis && (nd.sd->rb == NULL) &
630 <                                                (nd.sd->tf == NULL)) {
473 <                                SDfreeCache(nd.sd);
474 <                                raytrans(r);
475 <                                return(1);
476 <                        }
477 <                        setcolor(nd.rdiff, .0, .0, .0);
478 <                } else
479 <                        setcolor(nd.rdiff, m->oargs.farg[3],
628 >                cvt_sdcolor(nd.rdiff, &nd.sd->rLambBack);
629 >                if (m->oargs.nfargs >= 6) {
630 >                        setcolor(ctmp, m->oargs.farg[3],
631                                          m->oargs.farg[4],
632                                          m->oargs.farg[5]);
633 +                        addcolor(nd.rdiff, ctmp);
634 +                }
635          }
636                                                  /* diffuse transmittance */
637 <        if (m->oargs.nfargs < 9)
638 <                setcolor(nd.tdiff, .0, .0, .0);
639 <        else
487 <                setcolor(nd.tdiff, m->oargs.farg[6],
637 >        cvt_sdcolor(nd.tdiff, &nd.sd->tLamb);
638 >        if (m->oargs.nfargs >= 9) {
639 >                setcolor(ctmp, m->oargs.farg[6],
640                                  m->oargs.farg[7],
641                                  m->oargs.farg[8]);
642 <        nd.mp = m;
643 <        nd.pr = r;
642 >                addcolor(nd.tdiff, ctmp);
643 >        }
644                                                  /* get modifiers */
645          raytexture(r, m->omod);
646                                                  /* modify diffuse values */
# Line 499 | Line 651 | m_bsdf(OBJREC *m, RAY *r)
651          upvec[1] = evalue(mf->ep[2]);
652          upvec[2] = evalue(mf->ep[3]);
653                                                  /* return to world coords */
654 <        if (mf->f != &unitxf) {
655 <                multv3(upvec, upvec, mf->f->xfm);
656 <                nd.thick *= mf->f->sca;
654 >        if (mf->fxp != &unitxf) {
655 >                multv3(upvec, upvec, mf->fxp->xfm);
656 >                nd.thick *= mf->fxp->sca;
657          }
658 +        if (r->rox != NULL) {
659 +                multv3(upvec, upvec, r->rox->f.xfm);
660 +                nd.thick *= r->rox->f.sca;
661 +        }
662          raynormal(nd.pnorm, r);
663                                                  /* compute local BSDF xform */
664          ec = SDcompXform(nd.toloc, nd.pnorm, upvec);
# Line 512 | Line 668 | m_bsdf(OBJREC *m, RAY *r)
668                  nd.vray[2] = -r->rdir[2];
669                  ec = SDmapDir(nd.vray, nd.toloc, nd.vray);
670          }
515        if (!ec)
516                ec = SDinvXform(nd.fromloc, nd.toloc);
517                                                /* determine BSDF resolution */
518        if (!ec)
519                ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL,
520                                                SDqueryMin+SDqueryMax, nd.sd);
671          if (ec) {
672 <                objerror(m, WARNING, transSDError(ec));
523 <                SDfreeCache(nd.sd);
672 >                objerror(m, WARNING, "Illegal orientation vector");
673                  return(1);
674          }
675 +        compute_through(&nd);                   /* compute through component */
676 +        if (r->crtype & SHADOW) {
677 +                RAY     tr;                     /* attempt to pass shadow ray */
678 +                if (rayorigin(&tr, TRANS, r, nd.cthru) < 0)
679 +                        return(1);              /* blocked */
680 +                VCOPY(tr.rdir, r->rdir);
681 +                rayvalue(&tr);                  /* transmit with scaling */
682 +                multcolor(tr.rcol, tr.rcoef);
683 +                copycolor(r->rcol, tr.rcol);
684 +                return(1);                      /* we're done */
685 +        }
686 +        ec = SDinvXform(nd.fromloc, nd.toloc);
687 +        if (!ec)                                /* determine BSDF resolution */
688 +                ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL,
689 +                                        SDqueryMin+SDqueryMax, nd.sd);
690 +        if (ec)
691 +                objerror(m, USER, transSDError(ec));
692 +
693          nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]);
694          nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]);
695          if (!hitfront) {                        /* perturb normal towards hit */
# Line 556 | Line 723 | m_bsdf(OBJREC *m, RAY *r)
723                  bnorm[2] = -nd.pnorm[2];
724                  if (nd.thick != 0) {            /* proxy with offset? */
725                          VCOPY(vtmp, r->rop);
726 <                        VSUM(r->rop, vtmp, r->ron, -nd.thick);
726 >                        VSUM(r->rop, vtmp, r->ron, nd.thick);
727                          multambient(ctmp, r, bnorm);
728                          VCOPY(r->rop, vtmp);
729                  } else
# Line 566 | Line 733 | m_bsdf(OBJREC *m, RAY *r)
733                          flipsurface(r);
734          }
735                                                  /* add direct component */
736 <        if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL)) {
736 >        if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) &
737 >                                        (nd.sd->tb == NULL)) {
738                  direct(r, dir_brdf, &nd);       /* reflection only */
739          } else if (nd.thick == 0) {
740                  direct(r, dir_bsdf, &nd);       /* thin surface scattering */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines