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.24 by greg, Thu Jul 4 15:14:45 2013 UTC vs.
Revision 2.53 by greg, Thu Aug 2 22:44:35 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"
15   #include  "bsdf.h"
16   #include  "random.h"
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.
26   *  A non-zero thickness has the further side-effect that an unscattered
27 < *  (view) ray will pass right through our material if it has any
28 < *  non-diffuse transmission, making the BSDF surface invisible.  This
29 < *  shows the proxied geometry instead. Thickness has the further
30 < *  effect of turning off reflection on the hidden side so that rays
29 < *  heading in the opposite direction pass unimpeded through the BSDF
27 > *  (view) ray will pass right through our material, making the BSDF
28 > *  surface invisible and showing the proxied geometry instead. Thickness
29 > *  has the further effect of turning off reflection on the reverse side so
30 > *  rays heading in the opposite direction pass unimpeded through the BSDF
31   *  surface.  A paired surface may be placed on the opposide side of
32   *  the detail geometry, less than this thickness away, if a two-way
33   *  proxy is desired.  Note that the sign of the thickness is important.
# 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 + *      For the MAT_ABSDF type, we check for a strong "through" component.
40 + *  Such a component will cause direct rays to pass through unscattered.
41 + *  A separate test prevents over-counting by dropping samples that are
42 + *  too close to this "through" direction.  BSDFs with such a through direction
43 + *  will also have a view component, meaning they are somewhat see-through.
44 + *  A MAT_BSDF type with zero thickness behaves the same as a MAT_ABSDF
45 + *  type with no strong through component.
46   *      The "up" vector for the BSDF is given by three variables, defined
47   *  (along with the thickness) by the named function file, or '.' if none.
48   *  Together with the surface normal, this defines the local coordinate
# Line 42 | Line 50 | static const char RCSid[] = "$Id$";
50   *      We do not reorient the surface, so if the BSDF has no back-side
51   *  reflectance and none is given in the real arguments, a BSDF surface
52   *  with zero thickness will appear black when viewed from behind
53 < *  unless backface visibility is off.
53 > *  unless backface visibility is on, when it becomes invisible.
54   *      The diffuse arguments are added to components in the BSDF file,
55   *  not multiplied.  However, patterns affect this material as a multiplier
56   *  on everything except non-diffuse reflection.
57   *
58 + *  Arguments for MAT_ABSDF are:
59 + *      5+      BSDFfile        ux uy uz        funcfile        transform
60 + *      0
61 + *      0|3|6|9 rdf     gdf     bdf
62 + *              rdb     gdb     bdb
63 + *              rdt     gdt     bdt
64 + *
65   *  Arguments for MAT_BSDF are:
66   *      6+      thick   BSDFfile        ux uy uz        funcfile        transform
67   *      0
# Line 58 | Line 73 | static const char RCSid[] = "$Id$";
73   /*
74   * Note that our reverse ray-tracing process means that the positions
75   * of incoming and outgoing vectors may be reversed in our calls
76 < * to the BSDF library.  This is fine, since the bidirectional nature
76 > * to the BSDF library.  This is usually fine, since the bidirectional nature
77   * of the BSDF (that's what the 'B' stands for) means it all works out.
78   */
79  
# Line 71 | 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 for MAT_ABSDF */
90          SDData  *sd;            /* loaded BSDF data */
91 +        COLOR   rdiff;          /* diffuse reflection */
92          COLOR   runsamp;        /* BSDF hemispherical reflection */
93 <        COLOR   rdiff;          /* added diffuse reflection */
93 >        COLOR   tdiff;          /* diffuse transmission */
94          COLOR   tunsamp;        /* BSDF hemispherical transmission */
78        COLOR   tdiff;          /* added diffuse 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 for MAT_ABSDF */
100 + static void
101 + compute_through(BSDFDAT *ndp)
102 + {
103 + #define NDIR2CHECK      13
104 +        static const float      dir2check[NDIR2CHECK][2] = {
105 +                                        {0, 0},
106 +                                        {-0.8, 0},
107 +                                        {0, 0.8},
108 +                                        {0, -0.8},
109 +                                        {0.8, 0},
110 +                                        {-0.8, 0.8},
111 +                                        {-0.8, -0.8},
112 +                                        {0.8, 0.8},
113 +                                        {0.8, -0.8},
114 +                                        {-1.6, 0},
115 +                                        {0, 1.6},
116 +                                        {0, -1.6},
117 +                                        {1.6, 0},
118 +                                };
119 +        const double    peak_over = 1.5;
120 +        SDSpectralDF    *dfp;
121 +        FVECT           pdir;
122 +        double          tomega, srchrad;
123 +        COLOR           vpeak, vsum;
124 +        int             i;
125 +        SDError         ec;
126 +
127 +        if (ndp->pr->rod > 0)
128 +                dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
129 +        else
130 +                dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
131 +
132 +        if (dfp == NULL)
133 +                return;                         /* no specular transmission */
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 +        pdir[2] = 0.0;
140 +        for (i = 0; i < NDIR2CHECK; i++) {
141 +                FVECT   tdir;
142 +                SDValue sv;
143 +                COLOR   vcol;
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 +                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 +                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 +        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 +        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);
174 +        multcolor(ndp->cthru, ndp->pr->pcol);   /* modify by pattern */
175 +        return;
176 + baderror:
177 +        objerror(ndp->mp, USER, transSDError(ec));
178 + #undef NDIR2CHECK
179 + }
180 +
181   /* Jitter ray sample according to projected solid angle and specjitter */
182   static void
183   bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
# Line 94 | Line 192 | bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
192          normalize(vres);
193   }
194  
195 < /* Evaluate BSDF for direct component, returning true if OK to proceed */
195 > /* Get BSDF specular for direct component, returning true if OK to proceed */
196   static int
197 < direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
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;
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);
214 <                                        /* assign number of samples */
215 <        ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
216 <        if (ec)
217 <                goto baderror;
214 >                                        /* will discount diffuse portion */
215 >        switch ((vsrc[2] > 0)<<1 | (ndp->vray[2] > 0)) {
216 >        case 3:
217 >                if (ndp->sd->rf == NULL)
218 >                        return(0);      /* all diffuse */
219 >                sv = ndp->sd->rLambFront;
220 >                break;
221 >        case 0:
222 >                if (ndp->sd->rb == NULL)
223 >                        return(0);      /* all diffuse */
224 >                sv = ndp->sd->rLambBack;
225 >                break;
226 >        default:
227 >                if ((ndp->sd->tf == NULL) & (ndp->sd->tb == NULL))
228 >                        return(0);      /* all diffuse */
229 >                sv = ndp->sd->tLamb;
230 >                break;
231 >        }
232 >        if (sv.cieY > FTINY) {
233 >                diffY = sv.cieY *= 1./PI;
234 >                cvt_sdcolor(cdiff, &sv);
235 >        } else {
236 >                diffY = 0;
237 >                setcolor(cdiff,  0, 0, 0);
238 >        }
239 >                                        /* need projected solid angle */
240 >        omega *= fabs(vsrc[2]);
241                                          /* check indirect over-counting */
242 <        if (ndp->thick != 0 && ndp->pr->crtype & (SPECULAR|AMBIENT)
243 <                                && vsrc[2] > 0 ^ ndp->vray[2] > 0) {
244 <                double  dx = vsrc[0] + ndp->vray[0];
245 <                double  dy = vsrc[1] + ndp->vray[1];
246 <                if (dx*dx + dy*dy <= omega+tomega)
242 >        if ((vsrc[2] > 0) ^ (ndp->vray[2] > 0) && bright(ndp->cthru) > FTINY) {
243 >                double          dx = vsrc[0] + ndp->vray[0];
244 >                double          dy = vsrc[1] + ndp->vray[1];
245 >                SDSpectralDF    *dfp = (ndp->pr->rod > 0) ?
246 >                        ((ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb) :
247 >                        ((ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf) ;
248 >
249 >                if (dx*dx + dy*dy <= (2.5*4./PI)*(omega + dfp->minProjSA +
250 >                                                2.*sqrt(omega*dfp->minProjSA)))
251                          return(0);
252          }
253 +        ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
254 +        if (ec)
255 +                goto baderror;
256 +                                        /* assign number of samples */
257          sf = specjitter * ndp->pr->rweight;
258 <        if (tomega <= .0)
258 >        if (tomega <= 0)
259                  nsamp = 1;
260          else if (25.*tomega <= omega)
261                  nsamp = 100.*sf + .5;
262          else
263                  nsamp = 4.*sf*omega/tomega + .5;
264          nsamp += !nsamp;
265 <        setcolor(cval, .0, .0, .0);     /* sample our source area */
133 <        sf = sqrt(omega);
265 >        sf = sqrt(omega);               /* sample our source area */
266          tsr = sqrt(tomega);
267          for (i = nsamp; i--; ) {
268                  VCOPY(vsmp, vsrc);      /* jitter query directions */
# Line 138 | Line 270 | direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, B
270                          multisamp(sd, 2, (i + frandom())/(double)nsamp);
271                          vsmp[0] += (sd[0] - .5)*sf;
272                          vsmp[1] += (sd[1] - .5)*sf;
273 <                        if (normalize(vsmp) == 0) {
142 <                                --nsamp;
143 <                                continue;
144 <                        }
273 >                        normalize(vsmp);
274                  }
275                  bsdf_jitter(vjit, ndp, tsr);
276                                          /* compute BSDF */
277                  ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
278                  if (ec)
279                          goto baderror;
280 <                if (sv.cieY <= FTINY)   /* worth using? */
281 <                        continue;
280 >                if (sv.cieY - diffY <= FTINY)
281 >                        continue;       /* no specular part */
282 >                                        /* check for variable resolution */
283 >                ec = SDsizeBSDF(&tomega2, vjit, vsmp, SDqueryMin, ndp->sd);
284 >                if (ec)
285 >                        goto baderror;
286 >                if (tomega2 < .12*tomega)
287 >                        continue;       /* not safe to include */
288                  cvt_sdcolor(csmp, &sv);
289 <                addcolor(cval, csmp);   /* average it in */
290 <                ++ok;
289 >
290 >                if (sf < 2.5*tsr) {     /* weight by Y for small sources */
291 >                        scalecolor(csmp, sv.cieY);
292 >                        wtot += sv.cieY;
293 >                } else
294 >                        wtot += 1.;
295 >                addcolor(cval, csmp);
296          }
297 <        sf = 1./(double)nsamp;
297 >        if (wtot <= FTINY)              /* no valid specular samples? */
298 >                return(0);
299 >
300 >        sf = 1./wtot;                   /* weighted average BSDF */
301          scalecolor(cval, sf);
302 <        return(ok);
302 >                                        /* subtract diffuse contribution */
303 >        for (i = 3*(diffY > FTINY); i--; )
304 >                if ((colval(cval,i) -= colval(cdiff,i)) < 0)
305 >                        colval(cval,i) = 0;
306 >        return(1);
307   baderror:
308          objerror(ndp->mp, USER, transSDError(ec));
309          return(0);                      /* gratis return */
# Line 176 | Line 323 | dir_bsdf(
323          double          dtmp;
324          COLOR           ctmp;
325  
326 <        setcolor(cval, .0, .0, .0);
326 >        setcolor(cval,  0, 0, 0);
327  
328          ldot = DOT(np->pnorm, ldir);
329          if ((-FTINY <= ldot) & (ldot <= FTINY))
# Line 184 | Line 331 | dir_bsdf(
331  
332          if (ldot > 0 && bright(np->rdiff) > FTINY) {
333                  /*
334 <                 *  Compute added diffuse reflected component.
334 >                 *  Compute diffuse reflected component
335                   */
336                  copycolor(ctmp, np->rdiff);
337                  dtmp = ldot * omega * (1./PI);
# Line 193 | Line 340 | dir_bsdf(
340          }
341          if (ldot < 0 && bright(np->tdiff) > FTINY) {
342                  /*
343 <                 *  Compute added diffuse transmission.
343 >                 *  Compute diffuse transmission
344                   */
345                  copycolor(ctmp, np->tdiff);
346                  dtmp = -ldot * omega * (1.0/PI);
347                  scalecolor(ctmp, dtmp);
348                  addcolor(cval, ctmp);
349          }
350 +        if (ambRayInPmap(np->pr))
351 +                return;         /* specular already in photon map */
352          /*
353 <         *  Compute scattering coefficient using BSDF.
353 >         *  Compute specular scattering coefficient using BSDF
354           */
355 <        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
355 >        if (!direct_specular_OK(ctmp, ldir, omega, np))
356                  return;
357 <        if (ldot > 0) {         /* pattern only diffuse reflection */
209 <                COLOR   ctmp1, ctmp2;
210 <                dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
211 <                                        : np->sd->rLambBack.cieY;
212 <                                        /* diffuse fraction */
213 <                dtmp /= PI * bright(ctmp);
214 <                copycolor(ctmp2, np->pr->pcol);
215 <                scalecolor(ctmp2, dtmp);
216 <                setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
217 <                addcolor(ctmp1, ctmp2);
218 <                multcolor(ctmp, ctmp1); /* apply derated pattern */
219 <                dtmp = ldot * omega;
220 <        } else {                        /* full pattern on transmission */
357 >        if (ldot < 0) {         /* pattern for specular transmission */
358                  multcolor(ctmp, np->pr->pcol);
359                  dtmp = -ldot * omega;
360 <        }
360 >        } else
361 >                dtmp = ldot * omega;
362          scalecolor(ctmp, dtmp);
363          addcolor(cval, ctmp);
364   }
# Line 239 | Line 377 | dir_brdf(
377          double          dtmp;
378          COLOR           ctmp, ctmp1, ctmp2;
379  
380 <        setcolor(cval, .0, .0, .0);
380 >        setcolor(cval,  0, 0, 0);
381  
382          ldot = DOT(np->pnorm, ldir);
383          
# Line 248 | Line 386 | dir_brdf(
386  
387          if (bright(np->rdiff) > FTINY) {
388                  /*
389 <                 *  Compute added diffuse reflected component.
389 >                 *  Compute diffuse reflected component
390                   */
391                  copycolor(ctmp, np->rdiff);
392                  dtmp = ldot * omega * (1./PI);
393                  scalecolor(ctmp, dtmp);
394                  addcolor(cval, ctmp);
395          }
396 +        if (ambRayInPmap(np->pr))
397 +                return;         /* specular already in photon map */
398          /*
399 <         *  Compute reflection coefficient using BSDF.
399 >         *  Compute specular reflection coefficient using BSDF
400           */
401 <        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
401 >        if (!direct_specular_OK(ctmp, ldir, omega, np))
402                  return;
263                                        /* pattern only diffuse reflection */
264        dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
265                                : np->sd->rLambBack.cieY;
266        dtmp /= PI * bright(ctmp);      /* diffuse fraction */
267        copycolor(ctmp2, np->pr->pcol);
268        scalecolor(ctmp2, dtmp);
269        setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
270        addcolor(ctmp1, ctmp2);
271        multcolor(ctmp, ctmp1);         /* apply derated pattern */
403          dtmp = ldot * omega;
404          scalecolor(ctmp, dtmp);
405          addcolor(cval, ctmp);
# Line 288 | Line 419 | dir_btdf(
419          double          dtmp;
420          COLOR           ctmp;
421  
422 <        setcolor(cval, .0, .0, .0);
422 >        setcolor(cval,  0, 0, 0);
423  
424          ldot = DOT(np->pnorm, ldir);
425  
# Line 297 | Line 428 | dir_btdf(
428  
429          if (bright(np->tdiff) > FTINY) {
430                  /*
431 <                 *  Compute added diffuse transmission.
431 >                 *  Compute diffuse transmission
432                   */
433                  copycolor(ctmp, np->tdiff);
434                  dtmp = -ldot * omega * (1.0/PI);
435                  scalecolor(ctmp, dtmp);
436                  addcolor(cval, ctmp);
437          }
438 +        if (ambRayInPmap(np->pr))
439 +                return;         /* specular already in photon map */
440          /*
441 <         *  Compute scattering coefficient using BSDF.
441 >         *  Compute specular scattering coefficient using BSDF
442           */
443 <        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
443 >        if (!direct_specular_OK(ctmp, ldir, omega, np))
444                  return;
445                                          /* full pattern on transmission */
446          multcolor(ctmp, np->pr->pcol);
# Line 318 | Line 451 | dir_btdf(
451  
452   /* Sample separate BSDF component */
453   static int
454 < sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
454 > sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int xmit)
455   {
456 <        int     nstarget = 1;
457 <        int     nsent;
458 <        SDError ec;
459 <        SDValue bsv;
460 <        double  xrand;
461 <        FVECT   vsmp;
462 <        RAY     sr;
456 >        const int       hasthru = (xmit &&
457 >                                        !(ndp->pr->crtype & (SPECULAR|AMBIENT))
458 >                                        && bright(ndp->cthru) > FTINY);
459 >        int             nstarget = 1;
460 >        int             nsent = 0;
461 >        int             n;
462 >        SDError         ec;
463 >        SDValue         bsv;
464 >        double          xrand;
465 >        FVECT           vsmp, vinc;
466 >        RAY             sr;
467                                                  /* multiple samples? */
468          if (specjitter > 1.5) {
469                  nstarget = specjitter*ndp->pr->rweight + .5;
470                  nstarget += !nstarget;
471          }
472                                                  /* run through our samples */
473 <        for (nsent = 0; nsent < nstarget; nsent++) {
473 >        for (n = 0; n < nstarget; n++) {
474                  if (nstarget == 1) {            /* stratify random variable */
475                          xrand = urand(ilhash(dimlist,ndims)+samplendx);
476                          if (specjitter < 1.)
477                                  xrand = .5 + specjitter*(xrand-.5);
478                  } else {
479 <                        xrand = (nsent + frandom())/(double)nstarget;
479 >                        xrand = (n + frandom())/(double)nstarget;
480                  }
481                  SDerrorDetail[0] = '\0';        /* sample direction & coef. */
482                  bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]);
483 +                VCOPY(vinc, vsmp);              /* to compare after */
484                  ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
485                  if (ec)
486                          objerror(ndp->mp, USER, transSDError(ec));
487                  if (bsv.cieY <= FTINY)          /* zero component? */
488                          break;
489 <                                                /* map vector to world */
489 >                if (hasthru) {                  /* check for view ray */
490 >                        double  dx = vinc[0] + vsmp[0];
491 >                        double  dy = vinc[1] + vsmp[1];
492 >                        if (dx*dx + dy*dy <= ndp->sr_vpsa[0]*ndp->sr_vpsa[0])
493 >                                continue;       /* exclude view sample */
494 >                }
495 >                                                /* map non-view sample->world */
496                  if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
497                          break;
498                                                  /* spawn a specular ray */
499                  if (nstarget > 1)
500                          bsv.cieY /= (double)nstarget;
501                  cvt_sdcolor(sr.rcoef, &bsv);    /* use sample color */
502 <                if (usepat)                     /* apply pattern? */
502 >                if (xmit)                       /* apply pattern on transmit */
503                          multcolor(sr.rcoef, ndp->pr->pcol);
504                  if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
505 <                        if (maxdepth > 0)
506 <                                break;
507 <                        continue;               /* Russian roulette victim */
505 >                        if (!n & (nstarget > 1)) {
506 >                                n = nstarget;   /* avoid infinitue loop */
507 >                                nstarget = nstarget*sr.rweight/minweight;
508 >                                if (n == nstarget) break;
509 >                                n = -1;         /* moved target */
510 >                        }
511 >                        continue;               /* try again */
512                  }
513 <                                                /* need to offset origin? */
366 <                if (ndp->thick != 0 && ndp->pr->rod > 0 ^ vsmp[2] > 0)
513 >                if (xmit && ndp->thick != 0)    /* need to offset origin? */
514                          VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
515                  rayvalue(&sr);                  /* send & evaluate sample */
516                  multcolor(sr.rcol, sr.rcoef);
517                  addcolor(ndp->pr->rcol, sr.rcol);
518 +                ++nsent;
519          }
520          return(nsent);
521   }
# Line 376 | Line 524 | sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usep
524   static int
525   sample_sdf(BSDFDAT *ndp, int sflags)
526   {
527 +        int             hasthru = (sflags == SDsampSpT &&
528 +                                        !(ndp->pr->crtype & (SPECULAR|AMBIENT))
529 +                                        && bright(ndp->cthru) > FTINY);
530          int             n, ntotal = 0;
531 +        double          b = 0;
532          SDSpectralDF    *dfp;
533          COLORV          *unsc;
534  
# Line 386 | Line 538 | sample_sdf(BSDFDAT *ndp, int sflags)
538                          dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
539                  else
540                          dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
389                cvt_sdcolor(unsc, &ndp->sd->tLamb);
541          } else /* sflags == SDsampSpR */ {
542                  unsc = ndp->runsamp;
543 <                if (ndp->pr->rod > 0) {
543 >                if (ndp->pr->rod > 0)
544                          dfp = ndp->sd->rf;
545 <                        cvt_sdcolor(unsc, &ndp->sd->rLambFront);
395 <                } else {
545 >                else
546                          dfp = ndp->sd->rb;
397                        cvt_sdcolor(unsc, &ndp->sd->rLambBack);
398                }
547          }
548 <        multcolor(unsc, ndp->pr->pcol);
548 >        setcolor(unsc,  0, 0, 0);
549          if (dfp == NULL)                        /* no specular component? */
550                  return(0);
551 <                                                /* below sampling threshold? */
552 <        if (dfp->maxHemi <= specthresh+FTINY) {
553 <                if (dfp->maxHemi > FTINY) {     /* XXX no color from BSDF */
554 <                        FVECT   vjit;
555 <                        double  d;
556 <                        COLOR   ctmp;
557 <                        bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
558 <                        d = SDdirectHemi(vjit, sflags, ndp->sd);
551 >
552 >        if (hasthru) {                          /* separate view sample? */
553 >                RAY     tr;
554 >                if (rayorigin(&tr, TRANS, ndp->pr, ndp->cthru) == 0) {
555 >                        VCOPY(tr.rdir, ndp->pr->rdir);
556 >                        rayvalue(&tr);
557 >                        multcolor(tr.rcol, tr.rcoef);
558 >                        addcolor(ndp->pr->rcol, tr.rcol);
559 >                        ++ntotal;
560 >                        b = bright(ndp->cthru);
561 >                } else
562 >                        hasthru = 0;
563 >        }
564 >        if (dfp->maxHemi - b <= FTINY) {        /* have specular to sample? */
565 >                b = 0;
566 >        } else {
567 >                FVECT   vjit;
568 >                bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
569 >                b = SDdirectHemi(vjit, sflags, ndp->sd) - b;
570 >                if (b < 0) b = 0;
571 >        }
572 >        if (b <= specthresh+FTINY) {            /* below sampling threshold? */
573 >                if (b > FTINY) {                /* XXX no color from BSDF */
574                          if (sflags == SDsampSpT) {
575 <                                copycolor(ctmp, ndp->pr->pcol);
576 <                                scalecolor(ctmp, d);
575 >                                copycolor(unsc, ndp->pr->pcol);
576 >                                scalecolor(unsc, b);
577                          } else                  /* no pattern on reflection */
578 <                                setcolor(ctmp, d, d, d);
416 <                        addcolor(unsc, ctmp);
578 >                                setcolor(unsc, b, b, b);
579                  }
580 <                return(0);
580 >                return(ntotal);
581          }
582 <                                                /* else need to sample */
583 <        dimlist[ndims++] = (int)(size_t)ndp->mp;
422 <        ndims++;
582 >        dimlist[ndims] = (int)(size_t)ndp->mp;  /* else sample specular */
583 >        ndims += 2;
584          for (n = dfp->ncomp; n--; ) {           /* loop over components */
585                  dimlist[ndims-1] = n + 9438;
586                  ntotal += sample_sdcomp(ndp, &dfp->comp[n], sflags==SDsampSpT);
# Line 432 | Line 593 | sample_sdf(BSDFDAT *ndp, int sflags)
593   int
594   m_bsdf(OBJREC *m, RAY *r)
595   {
596 +        int     hasthick = (m->otype == MAT_BSDF);
597          int     hitfront;
598          COLOR   ctmp;
599          SDError ec;
# Line 439 | Line 601 | m_bsdf(OBJREC *m, RAY *r)
601          MFUNC   *mf;
602          BSDFDAT nd;
603                                                  /* check arguments */
604 <        if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) |
604 >        if ((m->oargs.nsargs < hasthick+5) | (m->oargs.nfargs > 9) |
605                                  (m->oargs.nfargs % 3))
606                  objerror(m, USER, "bad # arguments");
607                                                  /* record surface struck */
608          hitfront = (r->rod > 0);
609                                                  /* load cal file */
610 <        mf = getfunc(m, 5, 0x1d, 1);
611 <                                                /* get thickness */
612 <        nd.thick = evalue(mf->ep[0]);
613 <        if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
614 <                nd.thick = .0;
615 <                                                /* check shadow */
616 <        if (r->crtype & SHADOW) {
617 <                if (nd.thick != 0)
456 <                        raytrans(r);            /* pass-through */
457 <                return(1);                      /* or shadow */
610 >        mf = hasthick   ? getfunc(m, 5, 0x1d, 1)
611 >                        : getfunc(m, 4, 0xe, 1) ;
612 >        setfunc(m, r);
613 >        nd.thick = 0;                           /* set thickness */
614 >        if (hasthick) {
615 >                nd.thick = evalue(mf->ep[0]);
616 >                if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
617 >                        nd.thick = 0;
618          }
619 +                                                /* check backface visibility */
620 +        if (!hitfront & !backvis) {
621 +                raytrans(r);
622 +                return(1);
623 +        }
624                                                  /* check other rays to pass */
625 <        if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) ||
626 <                                nd.thick > 0 ^ hitfront)) {
625 >        if (nd.thick != 0 && (r->crtype & SHADOW ||
626 >                                !(r->crtype & (SPECULAR|AMBIENT)) ||
627 >                                (nd.thick > 0) ^ hitfront)) {
628                  raytrans(r);                    /* hide our proxy */
629                  return(1);
630          }
631 +        if (hasthick && r->crtype & SHADOW)     /* early shadow check #1 */
632 +                return(1);
633 +        nd.mp = m;
634 +        nd.pr = r;
635                                                  /* get BSDF data */
636 <        nd.sd = loadBSDF(m->oargs.sarg[1]);
636 >        nd.sd = loadBSDF(m->oargs.sarg[hasthick]);
637 >                                                /* early shadow check #2 */
638 >        if (r->crtype & SHADOW && (nd.sd->tf == NULL) & (nd.sd->tb == NULL))
639 >                return(1);
640                                                  /* diffuse reflectance */
641          if (hitfront) {
642 <                if (m->oargs.nfargs < 3)
643 <                        setcolor(nd.rdiff, .0, .0, .0);
644 <                else
472 <                        setcolor(nd.rdiff, m->oargs.farg[0],
642 >                cvt_sdcolor(nd.rdiff, &nd.sd->rLambFront);
643 >                if (m->oargs.nfargs >= 3) {
644 >                        setcolor(ctmp, m->oargs.farg[0],
645                                          m->oargs.farg[1],
646                                          m->oargs.farg[2]);
647 +                        addcolor(nd.rdiff, ctmp);
648 +                }
649          } else {
650 <                if (m->oargs.nfargs < 6) {      /* check invisible backside */
651 <                        if (!backvis && (nd.sd->rb == NULL) &
652 <                                        (nd.sd->tb == NULL)) {
479 <                                SDfreeCache(nd.sd);
480 <                                raytrans(r);
481 <                                return(1);
482 <                        }
483 <                        setcolor(nd.rdiff, .0, .0, .0);
484 <                } else
485 <                        setcolor(nd.rdiff, m->oargs.farg[3],
650 >                cvt_sdcolor(nd.rdiff, &nd.sd->rLambBack);
651 >                if (m->oargs.nfargs >= 6) {
652 >                        setcolor(ctmp, m->oargs.farg[3],
653                                          m->oargs.farg[4],
654                                          m->oargs.farg[5]);
655 +                        addcolor(nd.rdiff, ctmp);
656 +                }
657          }
658                                                  /* diffuse transmittance */
659 <        if (m->oargs.nfargs < 9)
660 <                setcolor(nd.tdiff, .0, .0, .0);
661 <        else
493 <                setcolor(nd.tdiff, m->oargs.farg[6],
659 >        cvt_sdcolor(nd.tdiff, &nd.sd->tLamb);
660 >        if (m->oargs.nfargs >= 9) {
661 >                setcolor(ctmp, m->oargs.farg[6],
662                                  m->oargs.farg[7],
663                                  m->oargs.farg[8]);
664 <        nd.mp = m;
665 <        nd.pr = r;
664 >                addcolor(nd.tdiff, ctmp);
665 >        }
666                                                  /* get modifiers */
667          raytexture(r, m->omod);
668                                                  /* modify diffuse values */
669          multcolor(nd.rdiff, r->pcol);
670          multcolor(nd.tdiff, r->pcol);
671                                                  /* get up vector */
672 <        upvec[0] = evalue(mf->ep[1]);
673 <        upvec[1] = evalue(mf->ep[2]);
674 <        upvec[2] = evalue(mf->ep[3]);
672 >        upvec[0] = evalue(mf->ep[hasthick+0]);
673 >        upvec[1] = evalue(mf->ep[hasthick+1]);
674 >        upvec[2] = evalue(mf->ep[hasthick+2]);
675                                                  /* return to world coords */
676          if (mf->fxp != &unitxf) {
677                  multv3(upvec, upvec, mf->fxp->xfm);
# Line 522 | Line 690 | m_bsdf(OBJREC *m, RAY *r)
690                  nd.vray[2] = -r->rdir[2];
691                  ec = SDmapDir(nd.vray, nd.toloc, nd.vray);
692          }
525        if (!ec)
526                ec = SDinvXform(nd.fromloc, nd.toloc);
693          if (ec) {
694                  objerror(m, WARNING, "Illegal orientation vector");
695                  return(1);
696          }
697 <                                                /* determine BSDF resolution */
698 <        ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin+SDqueryMax, nd.sd);
697 >        setcolor(nd.cthru, 0, 0, 0);            /* consider through component */
698 >        if (m->otype == MAT_ABSDF) {
699 >                compute_through(&nd);
700 >                if (r->crtype & SHADOW) {
701 >                        RAY     tr;             /* attempt to pass shadow ray */
702 >                        if (rayorigin(&tr, TRANS, r, nd.cthru) < 0)
703 >                                return(1);      /* no through component */
704 >                        VCOPY(tr.rdir, r->rdir);
705 >                        rayvalue(&tr);          /* transmit with scaling */
706 >                        multcolor(tr.rcol, tr.rcoef);
707 >                        copycolor(r->rcol, tr.rcol);
708 >                        return(1);              /* we're done */
709 >                }
710 >        }
711 >        ec = SDinvXform(nd.fromloc, nd.toloc);
712 >        if (!ec)                                /* determine BSDF resolution */
713 >                ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL,
714 >                                        SDqueryMin+SDqueryMax, nd.sd);
715          if (ec)
716                  objerror(m, USER, transSDError(ec));
717  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines