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.5 by greg, Sun Feb 20 06:34:19 2011 UTC vs.
Revision 2.46 by greg, Mon Feb 12 18:46:29 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 50 | Line 55 | static const char RCSid[] = "$Id$";
55   *  Arguments for MAT_BSDF are:
56   *      6+      thick   BSDFfile        ux uy uz        funcfile        transform
57   *      0
58 < *      0|3|9   rdf     gdf     bdf
58 > *      0|3|6|9 rdf     gdf     bdf
59   *              rdb     gdb     bdb
60   *              rdt     gdt     bdt
61   */
# 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 67 | Line 72 | typedef struct {
72          RAY     *pr;            /* intersected ray */
73          FVECT   pnorm;          /* perturbed surface normal */
74          FVECT   vray;           /* local outgoing (return) vector */
75 <        double  sr_vpsa;        /* sqrt of BSDF projected solid angle */
75 >        double  sr_vpsa[2];     /* sqrt of BSDF projected solid angle extrema */
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 < /* Jitter ray sample according to projected solid angle and specjitter */
89 > /* Compute "through" component color */
90   static void
91 < bsdf_jitter(FVECT vres, BSDFDAT *ndp)
91 > compute_through(BSDFDAT *ndp)
92   {
93 <        double  sr_psa = ndp->sr_vpsa;
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->crtype & (SPECULAR|AMBIENT) && !(ndp->pr->crtype & SHADOW))
120 +                return;                         /* no need for through comp. */
121 +
122 +        if (ndp->pr->rod > 0)
123 +                dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
124 +        else
125 +                dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
126 +
127 +        if (dfp == NULL)
128 +                return;                         /* no specular transmission */
129 +        if (bright(ndp->pr->pcol) <= FTINY)
130 +                return;                         /* pattern is black, here */
131 +        srchrad = sqrt(dfp->minProjSA);         /* else search for peak */
132 +        setcolor(vpeak, 0, 0, 0);
133 +        setcolor(vsum, 0, 0, 0);
134 +        pdir[2] = 0.0;
135 +        for (i = 0; i < NDIR2CHECK; i++) {
136 +                FVECT   tdir;
137 +                SDValue sv;
138 +                COLOR   vcol;
139 +                tdir[0] = -ndp->vray[0] + dir2check[i][0]*srchrad;
140 +                tdir[1] = -ndp->vray[1] + dir2check[i][1]*srchrad;
141 +                tdir[2] = -ndp->vray[2];
142 +                normalize(tdir);
143 +                ec = SDevalBSDF(&sv, tdir, ndp->vray, ndp->sd);
144 +                if (ec)
145 +                        goto baderror;
146 +                cvt_sdcolor(vcol, &sv);
147 +                addcolor(vsum, vcol);
148 +                if (sv.cieY > bright(vpeak)) {
149 +                        copycolor(vpeak, vcol);
150 +                        VCOPY(pdir, tdir);
151 +                }
152 +        }
153 +        if (pdir[2] == 0.0)
154 +                return;                         /* zero neighborhood */
155 +        ec = SDsizeBSDF(&tomega, pdir, ndp->vray, SDqueryMin, ndp->sd);
156 +        if (ec)
157 +                goto baderror;
158 +        if (tomega > 1.5*dfp->minProjSA)
159 +                return;                         /* not really a peak? */
160 +        tomega /= fabs(pdir[2]);                /* remove cosine factor */
161 +        if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .001)
162 +                return;                         /* < 0.1% transmission */
163 +        for (i = 3; i--; )                      /* remove peak from average */
164 +                colval(vsum,i) -= colval(vpeak,i);
165 +        if (peak_over*bright(vsum) >= (NDIR2CHECK-1)*bright(vpeak))
166 +                return;                         /* not peaky enough */
167 +        copycolor(ndp->cthru, vpeak);           /* else use it */
168 +        scalecolor(ndp->cthru, tomega);
169 +        multcolor(ndp->cthru, ndp->pr->pcol);   /* modify by pattern */
170 +        return;
171 + baderror:
172 +        objerror(ndp->mp, USER, transSDError(ec));
173 + #undef NDIR2CHECK
174 + }
175 +
176 + /* Jitter ray sample according to projected solid angle and specjitter */
177 + static void
178 + bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
179 + {
180          VCOPY(vres, ndp->vray);
181          if (specjitter < 1.)
182                  sr_psa *= specjitter;
# Line 96 | Line 187 | bsdf_jitter(FVECT vres, BSDFDAT *ndp)
187          normalize(vres);
188   }
189  
190 + /* Get BSDF specular for direct component, returning true if OK to proceed */
191 + static int
192 + direct_specular_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
193 + {
194 +        int     nsamp;
195 +        double  wtot = 0;
196 +        FVECT   vsrc, vsmp, vjit;
197 +        double  tomega, tomega2;
198 +        double  sf, tsr, sd[2];
199 +        COLOR   csmp, cdiff;
200 +        double  diffY;
201 +        SDValue sv;
202 +        SDError ec;
203 +        int     i;
204 +                                        /* in case we fail */
205 +        setcolor(cval,  0, 0, 0);
206 +                                        /* transform source direction */
207 +        if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
208 +                return(0);
209 +                                        /* will discount diffuse portion */
210 +        switch ((vsrc[2] > 0)<<1 | (ndp->vray[2] > 0)) {
211 +        case 3:
212 +                if (ndp->sd->rf == NULL)
213 +                        return(0);      /* all diffuse */
214 +                sv = ndp->sd->rLambFront;
215 +                break;
216 +        case 0:
217 +                if (ndp->sd->rb == NULL)
218 +                        return(0);      /* all diffuse */
219 +                sv = ndp->sd->rLambBack;
220 +                break;
221 +        default:
222 +                if ((ndp->sd->tf == NULL) & (ndp->sd->tb == NULL))
223 +                        return(0);      /* all diffuse */
224 +                sv = ndp->sd->tLamb;
225 +                break;
226 +        }
227 +        if (sv.cieY > FTINY) {
228 +                diffY = sv.cieY *= 1./PI;
229 +                cvt_sdcolor(cdiff, &sv);
230 +        } else {
231 +                diffY = 0;
232 +                setcolor(cdiff,  0, 0, 0);
233 +        }
234 +                                        /* need projected solid angles */
235 +        omega *= fabs(vsrc[2]);
236 +        ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
237 +        if (ec)
238 +                goto baderror;
239 +                                        /* check indirect over-counting */
240 +        if ((vsrc[2] > 0) ^ (ndp->vray[2] > 0) && bright(ndp->cthru) > FTINY) {
241 +                double  dx = vsrc[0] + ndp->vray[0];
242 +                double  dy = vsrc[1] + ndp->vray[1];
243 +                if (dx*dx + dy*dy <= (4./PI)*(omega + tomega +
244 +                                                2.*sqrt(omega*tomega)))
245 +                        return(0);
246 +        }
247 +                                        /* assign number of samples */
248 +        sf = specjitter * ndp->pr->rweight;
249 +        if (tomega <= 0)
250 +                nsamp = 1;
251 +        else if (25.*tomega <= omega)
252 +                nsamp = 100.*sf + .5;
253 +        else
254 +                nsamp = 4.*sf*omega/tomega + .5;
255 +        nsamp += !nsamp;
256 +        sf = sqrt(omega);               /* sample our source area */
257 +        tsr = sqrt(tomega);
258 +        for (i = nsamp; i--; ) {
259 +                VCOPY(vsmp, vsrc);      /* jitter query directions */
260 +                if (nsamp > 1) {
261 +                        multisamp(sd, 2, (i + frandom())/(double)nsamp);
262 +                        vsmp[0] += (sd[0] - .5)*sf;
263 +                        vsmp[1] += (sd[1] - .5)*sf;
264 +                        normalize(vsmp);
265 +                }
266 +                bsdf_jitter(vjit, ndp, tsr);
267 +                                        /* compute BSDF */
268 +                ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
269 +                if (ec)
270 +                        goto baderror;
271 +                if (sv.cieY - diffY <= FTINY)
272 +                        continue;       /* no specular part */
273 +                                        /* check for variable resolution */
274 +                ec = SDsizeBSDF(&tomega2, vjit, vsmp, SDqueryMin, ndp->sd);
275 +                if (ec)
276 +                        goto baderror;
277 +                if (tomega2 < .12*tomega)
278 +                        continue;       /* not safe to include */
279 +                cvt_sdcolor(csmp, &sv);
280 +
281 +                if (sf < 2.5*tsr) {     /* weight by Y for small sources */
282 +                        scalecolor(csmp, sv.cieY);
283 +                        wtot += sv.cieY;
284 +                } else
285 +                        wtot += 1.;
286 +                addcolor(cval, csmp);
287 +        }
288 +        if (wtot <= FTINY)              /* no valid specular samples? */
289 +                return(0);
290 +
291 +        sf = 1./wtot;                   /* weighted average BSDF */
292 +        scalecolor(cval, sf);
293 +                                        /* subtract diffuse contribution */
294 +        for (i = 3*(diffY > FTINY); i--; )
295 +                if ((colval(cval,i) -= colval(cdiff,i)) < 0)
296 +                        colval(cval,i) = 0;
297 +        return(1);
298 + baderror:
299 +        objerror(ndp->mp, USER, transSDError(ec));
300 +        return(0);                      /* gratis return */
301 + }
302 +
303   /* Compute source contribution for BSDF (reflected & transmitted) */
304   static void
305   dir_bsdf(
# Line 106 | Line 310 | dir_bsdf(
310   )
311   {
312          BSDFDAT         *np = (BSDFDAT *)nnp;
109        SDError         ec;
110        SDValue         sv;
111        FVECT           vsrc;
112        FVECT           vjit;
313          double          ldot;
314          double          dtmp;
315          COLOR           ctmp;
316  
317 <        setcolor(cval, .0, .0, .0);
317 >        setcolor(cval,  0, 0, 0);
318  
319          ldot = DOT(np->pnorm, ldir);
320          if ((-FTINY <= ldot) & (ldot <= FTINY))
321                  return;
322  
323 <        if (ldot > .0 && bright(np->rdiff) > FTINY) {
323 >        if (ldot > 0 && bright(np->rdiff) > FTINY) {
324                  /*
325 <                 *  Compute added diffuse reflected component.
325 >                 *  Compute diffuse reflected component
326                   */
327                  copycolor(ctmp, np->rdiff);
328                  dtmp = ldot * omega * (1./PI);
329                  scalecolor(ctmp, dtmp);
330                  addcolor(cval, ctmp);
331          }
332 <        if (ldot < .0 && bright(np->tdiff) > FTINY) {
332 >        if (ldot < 0 && bright(np->tdiff) > FTINY) {
333                  /*
334 <                 *  Compute added diffuse transmission.
334 >                 *  Compute diffuse transmission
335                   */
336                  copycolor(ctmp, np->tdiff);
337                  dtmp = -ldot * omega * (1.0/PI);
338                  scalecolor(ctmp, dtmp);
339                  addcolor(cval, ctmp);
340          }
341 +        if (ambRayInPmap(np->pr))
342 +                return;         /* specular already in photon map */
343          /*
344 <         *  Compute scattering coefficient using BSDF.
344 >         *  Compute specular scattering coefficient using BSDF
345           */
346 <        if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone)
346 >        if (!direct_specular_OK(ctmp, ldir, omega, np))
347                  return;
348 <        bsdf_jitter(vjit, np);
147 <        ec = SDevalBSDF(&sv, vjit, vsrc, np->sd);
148 <        if (ec)
149 <                objerror(np->mp, USER, transSDError(ec));
150 <
151 <        if (sv.cieY <= FTINY)           /* not worth using? */
152 <                return;
153 <        cvt_sdcolor(ctmp, &sv);
154 <        if (ldot > .0) {                /* pattern only diffuse reflection */
155 <                COLOR   ctmp1, ctmp2;
156 <                dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY
157 <                                        : np->sd->rLambBack.cieY;
158 <                dtmp /= PI * sv.cieY;   /* diffuse fraction */
159 <                copycolor(ctmp2, np->pr->pcol);
160 <                scalecolor(ctmp2, dtmp);
161 <                setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
162 <                addcolor(ctmp1, ctmp2);
163 <                multcolor(ctmp, ctmp1); /* apply derated pattern */
164 <                dtmp = ldot * omega;
165 <        } else {                        /* full pattern on transmission */
348 >        if (ldot < 0) {         /* pattern for specular transmission */
349                  multcolor(ctmp, np->pr->pcol);
350                  dtmp = -ldot * omega;
351 <        }
351 >        } else
352 >                dtmp = ldot * omega;
353          scalecolor(ctmp, dtmp);
354          addcolor(cval, ctmp);
355   }
# Line 180 | Line 364 | dir_brdf(
364   )
365   {
366          BSDFDAT         *np = (BSDFDAT *)nnp;
183        SDError         ec;
184        SDValue         sv;
185        FVECT           vsrc;
186        FVECT           vjit;
367          double          ldot;
368          double          dtmp;
369          COLOR           ctmp, ctmp1, ctmp2;
370  
371 <        setcolor(cval, .0, .0, .0);
371 >        setcolor(cval,  0, 0, 0);
372  
373          ldot = DOT(np->pnorm, ldir);
374          
# Line 197 | Line 377 | dir_brdf(
377  
378          if (bright(np->rdiff) > FTINY) {
379                  /*
380 <                 *  Compute added diffuse reflected component.
380 >                 *  Compute diffuse reflected component
381                   */
382                  copycolor(ctmp, np->rdiff);
383                  dtmp = ldot * omega * (1./PI);
384                  scalecolor(ctmp, dtmp);
385                  addcolor(cval, ctmp);
386          }
387 +        if (ambRayInPmap(np->pr))
388 +                return;         /* specular already in photon map */
389          /*
390 <         *  Compute reflection coefficient using BSDF.
390 >         *  Compute specular reflection coefficient using BSDF
391           */
392 <        if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone)
392 >        if (!direct_specular_OK(ctmp, ldir, omega, np))
393                  return;
212        bsdf_jitter(vjit, np);
213        ec = SDevalBSDF(&sv, vjit, vsrc, np->sd);
214        if (ec)
215                objerror(np->mp, USER, transSDError(ec));
216
217        if (sv.cieY <= FTINY)           /* not worth using? */
218                return;
219        cvt_sdcolor(ctmp, &sv);
220                                        /* pattern only diffuse reflection */
221        dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY
222                                : np->sd->rLambBack.cieY;
223        dtmp /= PI * sv.cieY;           /* diffuse fraction */
224        copycolor(ctmp2, np->pr->pcol);
225        scalecolor(ctmp2, dtmp);
226        setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
227        addcolor(ctmp1, ctmp2);
228        multcolor(ctmp, ctmp1);         /* apply derated pattern */
394          dtmp = ldot * omega;
395          scalecolor(ctmp, dtmp);
396          addcolor(cval, ctmp);
# Line 241 | Line 406 | dir_btdf(
406   )
407   {
408          BSDFDAT         *np = (BSDFDAT *)nnp;
244        SDError         ec;
245        SDValue         sv;
246        FVECT           vsrc;
247        FVECT           vjit;
409          double          ldot;
410          double          dtmp;
411          COLOR           ctmp;
412  
413 <        setcolor(cval, .0, .0, .0);
413 >        setcolor(cval,  0, 0, 0);
414  
415          ldot = DOT(np->pnorm, ldir);
416  
# Line 258 | Line 419 | dir_btdf(
419  
420          if (bright(np->tdiff) > FTINY) {
421                  /*
422 <                 *  Compute added diffuse transmission.
422 >                 *  Compute diffuse transmission
423                   */
424                  copycolor(ctmp, np->tdiff);
425                  dtmp = -ldot * omega * (1.0/PI);
426                  scalecolor(ctmp, dtmp);
427                  addcolor(cval, ctmp);
428          }
429 +        if (ambRayInPmap(np->pr))
430 +                return;         /* specular already in photon map */
431          /*
432 <         *  Compute scattering coefficient using BSDF.
432 >         *  Compute specular scattering coefficient using BSDF
433           */
434 <        if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone)
434 >        if (!direct_specular_OK(ctmp, ldir, omega, np))
435                  return;
273        bsdf_jitter(vjit, np);
274        ec = SDevalBSDF(&sv, vjit, vsrc, np->sd);
275        if (ec)
276                objerror(np->mp, USER, transSDError(ec));
277
278        if (sv.cieY <= FTINY)           /* not worth using? */
279                return;
280        cvt_sdcolor(ctmp, &sv);
436                                          /* full pattern on transmission */
437          multcolor(ctmp, np->pr->pcol);
438          dtmp = -ldot * omega;
# Line 287 | Line 442 | dir_btdf(
442  
443   /* Sample separate BSDF component */
444   static int
445 < sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
445 > sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int xmit)
446   {
447 <        int     nstarget = 1;
448 <        int     nsent = 0;
449 <        SDError ec;
450 <        SDValue bsv;
451 <        double  sthick;
452 <        FVECT   vjit, vsmp;
453 <        RAY     sr;
454 <        int     ntrials;
447 >        const int       hasthru = (xmit && bright(ndp->cthru) > FTINY);
448 >        int             nstarget = 1;
449 >        int             nsent = 0;
450 >        int             n;
451 >        SDError         ec;
452 >        SDValue         bsv;
453 >        double          xrand;
454 >        FVECT           vsmp, vinc;
455 >        RAY             sr;
456                                                  /* multiple samples? */
457          if (specjitter > 1.5) {
458                  nstarget = specjitter*ndp->pr->rweight + .5;
459 <                if (nstarget < 1)
304 <                        nstarget = 1;
459 >                nstarget += !nstarget;
460          }
461 <                                                /* run through our trials */
462 <        for (ntrials = 0; nsent < nstarget && ntrials < 9*nstarget; ntrials++) {
463 <                SDerrorDetail[0] = '\0';
464 <                                                /* sample direction & coef. */
465 <                bsdf_jitter(vjit, ndp);
466 <                ec = SDsampComponent(&bsv, vsmp, vjit, ntrials ? frandom()
467 <                                : urand(ilhash(dimlist,ndims)+samplendx), dcp);
461 >                                                /* run through our samples */
462 >        for (n = 0; n < nstarget; n++) {
463 >                if (nstarget == 1) {            /* stratify random variable */
464 >                        xrand = urand(ilhash(dimlist,ndims)+samplendx);
465 >                        if (specjitter < 1.)
466 >                                xrand = .5 + specjitter*(xrand-.5);
467 >                } else {
468 >                        xrand = (n + frandom())/(double)nstarget;
469 >                }
470 >                SDerrorDetail[0] = '\0';        /* sample direction & coef. */
471 >                bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]);
472 >                VCOPY(vinc, vsmp);              /* to compare after */
473 >                ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
474                  if (ec)
475                          objerror(ndp->mp, USER, transSDError(ec));
476 <                                                /* zero component? */
316 <                if (bsv.cieY <= FTINY)
476 >                if (bsv.cieY <= FTINY)          /* zero component? */
477                          break;
478 <                                                /* map vector to world */
478 >                if (hasthru) {                  /* check for view ray */
479 >                        double  dx = vinc[0] + vsmp[0];
480 >                        double  dy = vinc[1] + vsmp[1];
481 >                        if (dx*dx + dy*dy <= ndp->sr_vpsa[0]*ndp->sr_vpsa[0])
482 >                                continue;       /* exclude view sample */
483 >                }
484 >                                                /* map non-view sample->world */
485                  if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
486                          break;
321                                                /* unintentional penetration? */
322                if (DOT(sr.rdir, ndp->pr->ron) > .0 ^ vsmp[2] > .0)
323                        continue;
487                                                  /* spawn a specular ray */
488                  if (nstarget > 1)
489                          bsv.cieY /= (double)nstarget;
490 <                cvt_sdcolor(sr.rcoef, &bsv);    /* use color */
491 <                if (usepat)                     /* pattern on transmission */
490 >                cvt_sdcolor(sr.rcoef, &bsv);    /* use sample color */
491 >                if (xmit)                       /* apply pattern on transmit */
492                          multcolor(sr.rcoef, ndp->pr->pcol);
493                  if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
494 <                        if (maxdepth  > 0)
494 >                        if (maxdepth > 0)
495                                  break;
496 <                        ++nsent;                /* Russian roulette victim */
334 <                        continue;
496 >                        continue;               /* Russian roulette victim */
497                  }
498 <                                                /* need to offset origin? */
337 <                if (ndp->thick != .0 && ndp->pr->rod > .0 ^ vsmp[2] > .0)
498 >                if (xmit && ndp->thick != 0)    /* need to offset origin? */
499                          VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
500                  rayvalue(&sr);                  /* send & evaluate sample */
501                  multcolor(sr.rcol, sr.rcoef);
# Line 348 | Line 509 | sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usep
509   static int
510   sample_sdf(BSDFDAT *ndp, int sflags)
511   {
512 +        int             hasthru = (sflags == SDsampSpT &&
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);
364 <                } else {
529 >                else
530                          dfp = ndp->sd->rb;
366                        cvt_sdcolor(unsc, &ndp->sd->rLambBack);
367                }
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);
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);
385 <                        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;
391 <        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 401 | Line 577 | sample_sdf(BSDFDAT *ndp, int sflags)
577   int
578   m_bsdf(OBJREC *m, RAY *r)
579   {
580 +        int     hitfront;
581          COLOR   ctmp;
582          SDError ec;
583          FVECT   upvec, vtmp;
# Line 410 | Line 587 | m_bsdf(OBJREC *m, RAY *r)
587          if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) |
588                                  (m->oargs.nfargs % 3))
589                  objerror(m, USER, "bad # arguments");
590 <
590 >                                                /* record surface struck */
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 */
424 <                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 ^ r->rod > .0)) {
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 (r->rod > .0) {
620 <                if (m->oargs.nfargs < 3)
621 <                        setcolor(nd.rdiff, .0, .0, .0);
622 <                else
439 <                        setcolor(nd.rdiff, m->oargs.farg[0],
619 >        if (hitfront) {
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)) {
446 <                                SDfreeCache(nd.sd);
447 <                                raytrans(r);
448 <                                return(1);
449 <                        }
450 <                        setcolor(nd.rdiff, .0, .0, .0);
451 <                } else
452 <                        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
460 <                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 472 | 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 485 | 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          }
671 <        if (!ec)
672 <                ec = SDinvXform(nd.fromloc, nd.toloc);
490 <                                                /* determine BSDF resolution */
491 <        if (!ec)
492 <                ec = SDsizeBSDF(&nd.sr_vpsa, nd.vray, SDqueryMin, nd.sd);
493 <        if (!ec)
494 <                nd.sr_vpsa = sqrt(nd.sr_vpsa);
495 <        else {
496 <                objerror(m, WARNING, transSDError(ec));
497 <                SDfreeCache(nd.sd);
671 >        if (ec) {
672 >                objerror(m, WARNING, "Illegal orientation vector");
673                  return(1);
674          }
675 <        if (r->rod < .0) {                      /* perturb normal towards hit */
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);              /* no through component */
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 */
696                  nd.pnorm[0] = -nd.pnorm[0];
697                  nd.pnorm[1] = -nd.pnorm[1];
698                  nd.pnorm[2] = -nd.pnorm[2];
# Line 510 | Line 705 | m_bsdf(OBJREC *m, RAY *r)
705          copycolor(ctmp, nd.rdiff);
706          addcolor(ctmp, nd.runsamp);
707          if (bright(ctmp) > FTINY) {             /* ambient from reflection */
708 <                if (r->rod < .0)
708 >                if (!hitfront)
709                          flipsurface(r);
710                  multambient(ctmp, r, nd.pnorm);
711                  addcolor(r->rcol, ctmp);
712 <                if (r->rod < .0)
712 >                if (!hitfront)
713                          flipsurface(r);
714          }
715          copycolor(ctmp, nd.tdiff);
716          addcolor(ctmp, nd.tunsamp);
717          if (bright(ctmp) > FTINY) {             /* ambient from other side */
718                  FVECT  bnorm;
719 <                if (r->rod > .0)
719 >                if (hitfront)
720                          flipsurface(r);
721                  bnorm[0] = -nd.pnorm[0];
722                  bnorm[1] = -nd.pnorm[1];
723                  bnorm[2] = -nd.pnorm[2];
724 <                if (nd.thick != .0) {           /* proxy with offset? */
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
730                          multambient(ctmp, r, bnorm);
731                  addcolor(r->rcol, ctmp);
732 <                if (r->rod > .0)
732 >                if (hitfront)
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) {
739 >        } else if (nd.thick == 0) {
740                  direct(r, dir_bsdf, &nd);       /* thin surface scattering */
741          } else {
742                  direct(r, dir_brdf, &nd);       /* reflection first */
743                  VCOPY(vtmp, r->rop);            /* offset for transmitted */
744                  VSUM(r->rop, vtmp, r->ron, -nd.thick);
745 <                direct(r, dir_btdf, &nd);
745 >                direct(r, dir_btdf, &nd);       /* separate transmission */
746                  VCOPY(r->rop, vtmp);
747          }
748                                                  /* clean up */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines