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.3 by greg, Sat Feb 19 01:48:59 2011 UTC vs.
Revision 2.36 by greg, Tue May 16 20:06:40 2017 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.
20   *  String arguments include the BSDF and function files.
21 < *      A thickness variable causes the strange but useful behavior
22 < *  of translating transmitted rays this distance past the surface
23 < *  intersection in the normal direction to bypass intervening geometry.
24 < *  This only affects scattered, non-source directed samples.  Thus,
25 < *  thickness is relevant only if there is a transmitted component.
26 < *  A positive 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 our BSDF invisible.  This allows the
29 < *  underlying geometry to become visible.  A matching surface should be
30 < *  placed on the other side, less than the thickness away, if the backside
31 < *  reflectance is non-zero.
21 > *      A non-zero thickness causes the strange but useful behavior
22 > *  of translating transmitted rays this distance beneath the surface
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, 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.
33 > *  A positive thickness hides geometry behind the BSDF surface and uses
34 > *  front reflectance and transmission properties.  A negative thickness
35 > *  hides geometry in front of the surface when rays hit from behind,
36 > *  and applies only the transmission and backside reflectance properties.
37 > *  Reflection is ignored on the hidden side, as those rays pass through.
38 > *      When thickness is set to zero, shadow rays will be blocked unless
39 > *  a BTDF has a strong "through" component in the source direction.
40 > *  A separate test prevents over-counting by dropping specular & ambient
41 > *  samples that are too close to this "through" direction.  The same
42 > *  restriction applies for the proxy case (thickness != 0).
43   *      The "up" vector for the BSDF is given by three variables, defined
44   *  (along with the thickness) by the named function file, or '.' if none.
45   *  Together with the surface normal, this defines the local coordinate
46   *  system for the BSDF.
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, the surface will
49 < *  appear as black when viewed from behind (unless backvis is false).
50 < *  The diffuse compnent arguments are added to components in the BSDF file,
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 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.
54   *
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   */
62  
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 usually fine, since the bidirectional nature
67 + * of the BSDF (that's what the 'B' stands for) means it all works out.
68 + */
69 +
70   typedef struct {
71          OBJREC  *mp;            /* material pointer */
72          RAY     *pr;            /* intersected ray */
73          FVECT   pnorm;          /* perturbed surface normal */
74 <        FVECT   vinc;           /* local incident vector */
74 >        FVECT   vray;           /* local outgoing (return) vector */
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   runsamp;        /* BSDF hemispherical reflection */
82 <        COLOR   rdiff;          /* added diffuse reflection */
61 <        COLOR   tunsamp;        /* BSDF hemispherical transmission */
62 <        COLOR   tdiff;          /* added diffuse transmission */
81 >        COLOR   rdiff;          /* diffuse reflection */
82 >        COLOR   tdiff;          /* diffuse transmission */
83   }  BSDFDAT;             /* BSDF material data */
84  
85   #define cvt_sdcolor(cv, svp)    ccy2rgb(&(svp)->spec, (svp)->cieY, cv)
86  
87 < /* Compute source contribution for BSDF */
87 > /* Compute "through" component color */
88   static void
89 < dirbsdf(
89 > compute_through(BSDFDAT *ndp)
90 > {
91 > #define NDIR2CHECK      13
92 >        static const float      dir2check[NDIR2CHECK][2] = {
93 >                                        {0, 0},
94 >                                        {-0.8, 0},
95 >                                        {0, 0.8},
96 >                                        {0, -0.8},
97 >                                        {0.8, 0},
98 >                                        {-0.8, 0.8},
99 >                                        {-0.8, -0.8},
100 >                                        {0.8, 0.8},
101 >                                        {0.8, -0.8},
102 >                                        {-1.6, 0},
103 >                                        {0, 1.6},
104 >                                        {0, -1.6},
105 >                                        {1.6, 0},
106 >                                };
107 >        const double    peak_over = 2.0;
108 >        SDSpectralDF    *dfp;
109 >        FVECT           pdir;
110 >        double          tomega, srchrad;
111 >        COLOR           vpeak, vsum;
112 >        int             nsum, i;
113 >        SDError         ec;
114 >
115 >        setcolor(ndp->cthru, .0, .0, .0);       /* starting assumption */
116 >
117 >        if (ndp->pr->rod > 0)
118 >                dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
119 >        else
120 >                dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
121 >
122 >        if (dfp == NULL)
123 >                return;                         /* no specular transmission */
124 >        if (bright(ndp->pr->pcol) <= FTINY)
125 >                return;                         /* pattern is black, here */
126 >        srchrad = sqrt(dfp->minProjSA);         /* else search for peak */
127 >        setcolor(vpeak, .0, .0, .0);
128 >        setcolor(vsum, .0, .0, .0);
129 >        nsum = 0;
130 >        for (i = 0; i < NDIR2CHECK; i++) {
131 >                FVECT   tdir;
132 >                SDValue sv;
133 >                COLOR   vcol;
134 >                tdir[0] = -ndp->vray[0] + dir2check[i][0]*srchrad;
135 >                tdir[1] = -ndp->vray[1] + dir2check[i][1]*srchrad;
136 >                tdir[2] = -ndp->vray[2];
137 >                normalize(tdir);
138 >                ec = SDevalBSDF(&sv, tdir, ndp->vray, ndp->sd);
139 >                if (ec)
140 >                        goto baderror;
141 >                cvt_sdcolor(vcol, &sv);
142 >                addcolor(vsum, vcol);
143 >                ++nsum;
144 >                if (bright(vcol) > bright(vpeak)) {
145 >                        copycolor(vpeak, vcol);
146 >                        VCOPY(pdir, tdir);
147 >                }
148 >        }
149 >        ec = SDsizeBSDF(&tomega, pdir, ndp->vray, SDqueryMin, ndp->sd);
150 >        if (ec)
151 >                goto baderror;
152 >        if (tomega > 1.5*dfp->minProjSA)
153 >                return;                         /* not really a peak? */
154 >        if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .007)
155 >                return;                         /* < 0.7% transmission */
156 >        for (i = 3; i--; )                      /* remove peak from average */
157 >                colval(vsum,i) -= colval(vpeak,i);
158 >        --nsum;
159 >        if (peak_over*bright(vsum) >= nsum*bright(vpeak))
160 >                return;                         /* not peaky enough */
161 >        copycolor(ndp->cthru, vpeak);           /* else use it */
162 >        scalecolor(ndp->cthru, tomega);
163 >        multcolor(ndp->cthru, ndp->pr->pcol);   /* modify by pattern */
164 >        return;
165 > baderror:
166 >        objerror(ndp->mp, USER, transSDError(ec));
167 > #undef NDIR2CHECK
168 > }
169 >
170 > /* Jitter ray sample according to projected solid angle and specjitter */
171 > static void
172 > bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
173 > {
174 >        VCOPY(vres, ndp->vray);
175 >        if (specjitter < 1.)
176 >                sr_psa *= specjitter;
177 >        if (sr_psa <= FTINY)
178 >                return;
179 >        vres[0] += sr_psa*(.5 - frandom());
180 >        vres[1] += sr_psa*(.5 - frandom());
181 >        normalize(vres);
182 > }
183 >
184 > /* Get BSDF specular for direct component, returning true if OK to proceed */
185 > static int
186 > direct_specular_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
187 > {
188 >        int     nsamp, ok = 0;
189 >        FVECT   vsrc, vsmp, vjit;
190 >        double  tomega, tomega2;
191 >        double  sf, tsr, sd[2];
192 >        COLOR   csmp, cdiff;
193 >        double  diffY;
194 >        SDValue sv;
195 >        SDError ec;
196 >        int     i;
197 >                                        /* transform source direction */
198 >        if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
199 >                return(0);
200 >                                        /* will discount diffuse portion */
201 >        switch ((vsrc[2] > 0)<<1 | (ndp->vray[2] > 0)) {
202 >        case 3:
203 >                if (ndp->sd->rf == NULL)
204 >                        return(0);      /* all diffuse */
205 >                sv = ndp->sd->rLambFront;
206 >                break;
207 >        case 0:
208 >                if (ndp->sd->rb == NULL)
209 >                        return(0);      /* all diffuse */
210 >                sv = ndp->sd->rLambBack;
211 >                break;
212 >        default:
213 >                if ((ndp->sd->tf == NULL) & (ndp->sd->tb == NULL))
214 >                        return(0);      /* all diffuse */
215 >                sv = ndp->sd->tLamb;
216 >                break;
217 >        }
218 >        if (sv.cieY > FTINY) {
219 >                diffY = sv.cieY *= 1./PI;
220 >                cvt_sdcolor(cdiff, &sv);
221 >        } else {
222 >                diffY = .0;
223 >                setcolor(cdiff, .0, .0, .0);
224 >        }
225 >                                        /* assign number of samples */
226 >        ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
227 >        if (ec)
228 >                goto baderror;
229 >                                        /* check indirect over-counting */
230 >        if ((ndp->thick != 0 || bright(ndp->cthru) > FTINY)
231 >                                && ndp->pr->crtype & (SPECULAR|AMBIENT)
232 >                                && (vsrc[2] > 0) ^ (ndp->vray[2] > 0)) {
233 >                double  dx = vsrc[0] + ndp->vray[0];
234 >                double  dy = vsrc[1] + ndp->vray[1];
235 >                if (dx*dx + dy*dy <= omega+tomega)
236 >                        return(0);
237 >        }
238 >        sf = specjitter * ndp->pr->rweight;
239 >        if (tomega <= .0)
240 >                nsamp = 1;
241 >        else if (25.*tomega <= omega)
242 >                nsamp = 100.*sf + .5;
243 >        else
244 >                nsamp = 4.*sf*omega/tomega + .5;
245 >        nsamp += !nsamp;
246 >        setcolor(cval, .0, .0, .0);     /* sample our source area */
247 >        sf = sqrt(omega);
248 >        tsr = sqrt(tomega);
249 >        for (i = nsamp; i--; ) {
250 >                VCOPY(vsmp, vsrc);      /* jitter query directions */
251 >                if (nsamp > 1) {
252 >                        multisamp(sd, 2, (i + frandom())/(double)nsamp);
253 >                        vsmp[0] += (sd[0] - .5)*sf;
254 >                        vsmp[1] += (sd[1] - .5)*sf;
255 >                        normalize(vsmp);
256 >                }
257 >                bsdf_jitter(vjit, ndp, tsr);
258 >                                        /* check for variable resolution */
259 >                ec = SDsizeBSDF(&tomega2, vjit, vsmp, SDqueryMin, ndp->sd);
260 >                if (ec)
261 >                        goto baderror;
262 >                if (tomega2 < .12*tomega)
263 >                        continue;       /* not safe to include */
264 >                                        /* else compute BSDF */
265 >                ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
266 >                if (ec)
267 >                        goto baderror;
268 >                if (sv.cieY - diffY <= FTINY)
269 >                        continue;       /* no specular part */
270 >                cvt_sdcolor(csmp, &sv);
271 >                addcolor(cval, csmp);   /* else average it in */
272 >                ++ok;
273 >        }
274 >        if (!ok) {
275 >                setcolor(cval, .0, .0, .0);
276 >                return(0);              /* no valid specular samples */
277 >        }
278 >        sf = 1./(double)ok;
279 >        scalecolor(cval, sf);
280 >                                        /* subtract diffuse contribution */
281 >        for (i = 3*(diffY > FTINY); i--; )
282 >                if ((colval(cval,i) -= colval(cdiff,i)) < .0)
283 >                        colval(cval,i) = .0;
284 >        return(1);
285 > baderror:
286 >        objerror(ndp->mp, USER, transSDError(ec));
287 >        return(0);                      /* gratis return */
288 > }
289 >
290 > /* Compute source contribution for BSDF (reflected & transmitted) */
291 > static void
292 > dir_bsdf(
293          COLOR  cval,                    /* returned coefficient */
294          void  *nnp,                     /* material data */
295          FVECT  ldir,                    /* light source direction */
# Line 74 | Line 297 | dirbsdf(
297   )
298   {
299          BSDFDAT         *np = (BSDFDAT *)nnp;
77        SDError         ec;
78        SDValue         sv;
79        FVECT           vout;
300          double          ldot;
301          double          dtmp;
302          COLOR           ctmp;
# Line 87 | Line 307 | dirbsdf(
307          if ((-FTINY <= ldot) & (ldot <= FTINY))
308                  return;
309  
310 <        if (ldot > .0 && bright(np->rdiff) > FTINY) {
310 >        if (ldot > 0 && bright(np->rdiff) > FTINY) {
311                  /*
312                   *  Compute added diffuse reflected component.
313                   */
# Line 96 | Line 316 | dirbsdf(
316                  scalecolor(ctmp, dtmp);
317                  addcolor(cval, ctmp);
318          }
319 <        if (ldot < .0 && bright(np->tdiff) > FTINY) {
319 >        if (ldot < 0 && bright(np->tdiff) > FTINY) {
320                  /*
321                   *  Compute added diffuse transmission.
322                   */
# Line 105 | Line 325 | dirbsdf(
325                  scalecolor(ctmp, dtmp);
326                  addcolor(cval, ctmp);
327          }
328 +        if (ambRayInPmap(np->pr))
329 +                return;         /* specular already in photon map */
330          /*
331 <         *  Compute scattering coefficient using BSDF.
331 >         *  Compute specular scattering coefficient using BSDF.
332           */
333 <        if (SDmapDir(vout, np->toloc, ldir) != SDEnone)
333 >        if (!direct_specular_OK(ctmp, ldir, omega, np))
334                  return;
335 <        ec = SDevalBSDF(&sv, vout, np->vinc, np->sd);
114 <        if (ec)
115 <                objerror(np->mp, USER, transSDError(ec));
116 <
117 <        if (sv.cieY <= FTINY)           /* not worth using? */
118 <                return;
119 <        cvt_sdcolor(ctmp, &sv);
120 <        if (ldot > .0) {                /* pattern only diffuse reflection */
121 <                COLOR   ctmp1, ctmp2;
122 <                dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY
123 <                                        : np->sd->rLambBack.cieY;
124 <                dtmp /= PI * sv.cieY;   /* diffuse fraction */
125 <                copycolor(ctmp2, np->pr->pcol);
126 <                scalecolor(ctmp2, dtmp);
127 <                setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
128 <                addcolor(ctmp1, ctmp2);
129 <                multcolor(ctmp, ctmp1); /* apply derated pattern */
130 <                dtmp = ldot * omega;
131 <        } else {                        /* full pattern on transmission */
335 >        if (ldot < 0) {         /* pattern for specular transmission */
336                  multcolor(ctmp, np->pr->pcol);
337                  dtmp = -ldot * omega;
338 +        } else
339 +                dtmp = ldot * omega;
340 +        scalecolor(ctmp, dtmp);
341 +        addcolor(cval, ctmp);
342 + }
343 +
344 + /* Compute source contribution for BSDF (reflected only) */
345 + static void
346 + dir_brdf(
347 +        COLOR  cval,                    /* returned coefficient */
348 +        void  *nnp,                     /* material data */
349 +        FVECT  ldir,                    /* light source direction */
350 +        double  omega                   /* light source size */
351 + )
352 + {
353 +        BSDFDAT         *np = (BSDFDAT *)nnp;
354 +        double          ldot;
355 +        double          dtmp;
356 +        COLOR           ctmp, ctmp1, ctmp2;
357 +
358 +        setcolor(cval, .0, .0, .0);
359 +
360 +        ldot = DOT(np->pnorm, ldir);
361 +        
362 +        if (ldot <= FTINY)
363 +                return;
364 +
365 +        if (bright(np->rdiff) > FTINY) {
366 +                /*
367 +                 *  Compute added diffuse reflected component.
368 +                 */
369 +                copycolor(ctmp, np->rdiff);
370 +                dtmp = ldot * omega * (1./PI);
371 +                scalecolor(ctmp, dtmp);
372 +                addcolor(cval, ctmp);
373          }
374 +        if (ambRayInPmap(np->pr))
375 +                return;         /* specular already in photon map */
376 +        /*
377 +         *  Compute specular reflection coefficient using BSDF.
378 +         */
379 +        if (!direct_specular_OK(ctmp, ldir, omega, np))
380 +                return;
381 +        dtmp = ldot * omega;
382          scalecolor(ctmp, dtmp);
383          addcolor(cval, ctmp);
384   }
385  
386 + /* Compute source contribution for BSDF (transmitted only) */
387 + static void
388 + dir_btdf(
389 +        COLOR  cval,                    /* returned coefficient */
390 +        void  *nnp,                     /* material data */
391 +        FVECT  ldir,                    /* light source direction */
392 +        double  omega                   /* light source size */
393 + )
394 + {
395 +        BSDFDAT         *np = (BSDFDAT *)nnp;
396 +        double          ldot;
397 +        double          dtmp;
398 +        COLOR           ctmp;
399 +
400 +        setcolor(cval, .0, .0, .0);
401 +
402 +        ldot = DOT(np->pnorm, ldir);
403 +
404 +        if (ldot >= -FTINY)
405 +                return;
406 +
407 +        if (bright(np->tdiff) > FTINY) {
408 +                /*
409 +                 *  Compute added diffuse transmission.
410 +                 */
411 +                copycolor(ctmp, np->tdiff);
412 +                dtmp = -ldot * omega * (1.0/PI);
413 +                scalecolor(ctmp, dtmp);
414 +                addcolor(cval, ctmp);
415 +        }
416 +        if (ambRayInPmap(np->pr))
417 +                return;         /* specular already in photon map */
418 +        /*
419 +         *  Compute specular scattering coefficient using BSDF.
420 +         */
421 +        if (!direct_specular_OK(ctmp, ldir, omega, np))
422 +                return;
423 +                                        /* full pattern on transmission */
424 +        multcolor(ctmp, np->pr->pcol);
425 +        dtmp = -ldot * omega;
426 +        scalecolor(ctmp, dtmp);
427 +        addcolor(cval, ctmp);
428 + }
429 +
430   /* Sample separate BSDF component */
431   static int
432   sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
433   {
434          int     nstarget = 1;
435 <        int     nsent = 0;
435 >        int     nsent;
436          SDError ec;
437          SDValue bsv;
438 <        double  sthick;
439 <        FVECT   vout;
438 >        double  xrand;
439 >        FVECT   vsmp;
440          RAY     sr;
150        int     ntrials;
441                                                  /* multiple samples? */
442          if (specjitter > 1.5) {
443                  nstarget = specjitter*ndp->pr->rweight + .5;
444 <                if (nstarget < 1)
155 <                        nstarget = 1;
444 >                nstarget += !nstarget;
445          }
446 <                                                /* run through our trials */
447 <        for (ntrials = 0; nsent < nstarget && ntrials < 9*nstarget; ntrials++) {
448 <                SDerrorDetail[0] = '\0';
449 <                                                /* sample direction & coef. */
450 <                ec = SDsampComponent(&bsv, vout, ndp->vinc,
451 <                                ntrials ? frandom()
452 <                                        : urand(ilhash(dimlist,ndims)+samplendx),
453 <                                                dcp);
446 >                                                /* run through our samples */
447 >        for (nsent = 0; nsent < nstarget; nsent++) {
448 >                if (nstarget == 1) {            /* stratify random variable */
449 >                        xrand = urand(ilhash(dimlist,ndims)+samplendx);
450 >                        if (specjitter < 1.)
451 >                                xrand = .5 + specjitter*(xrand-.5);
452 >                } else {
453 >                        xrand = (nsent + frandom())/(double)nstarget;
454 >                }
455 >                SDerrorDetail[0] = '\0';        /* sample direction & coef. */
456 >                bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]);
457 >                ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
458                  if (ec)
459                          objerror(ndp->mp, USER, transSDError(ec));
460 <                                                /* zero component? */
168 <                if (bsv.cieY <= FTINY)
460 >                if (bsv.cieY <= FTINY)          /* zero component? */
461                          break;
462                                                  /* map vector to world */
463 <                if (SDmapDir(sr.rdir, ndp->fromloc, vout) != SDEnone)
463 >                if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
464                          break;
173                                                /* unintentional penetration? */
174                if (DOT(sr.rdir, ndp->pr->ron) > .0 ^ vout[2] > .0)
175                        continue;
465                                                  /* spawn a specular ray */
466                  if (nstarget > 1)
467                          bsv.cieY /= (double)nstarget;
468 <                cvt_sdcolor(sr.rcoef, &bsv);    /* use color */
469 <                if (usepat)                     /* pattern on transmission */
468 >                cvt_sdcolor(sr.rcoef, &bsv);    /* use sample color */
469 >                if (usepat)                     /* apply pattern? */
470                          multcolor(sr.rcoef, ndp->pr->pcol);
471                  if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
472 <                        if (maxdepth  > 0)
472 >                        if (maxdepth > 0)
473                                  break;
474 <                        ++nsent;                /* Russian roulette victim */
186 <                        continue;
474 >                        continue;               /* Russian roulette victim */
475                  }
476 <                if (ndp->thick > FTINY) {       /* need to move origin? */
477 <                        sthick = (ndp->pr->rod > .0) ? -ndp->thick : ndp->thick;
478 <                        if (sthick < .0 ^ vout[2] > .0)
191 <                                VSUM(sr.rorg, sr.rorg, ndp->pr->ron, sthick);
192 <                }
476 >                                                /* need to offset origin? */
477 >                if (ndp->thick != 0 && (ndp->pr->rod > 0) ^ (vsmp[2] > 0))
478 >                        VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
479                  rayvalue(&sr);                  /* send & evaluate sample */
480                  multcolor(sr.rcol, sr.rcoef);
481                  addcolor(ndp->pr->rcol, sr.rcol);
196                ++nsent;
482          }
483          return(nsent);
484   }
# Line 207 | Line 492 | sample_sdf(BSDFDAT *ndp, int sflags)
492          COLORV          *unsc;
493  
494          if (sflags == SDsampSpT) {
495 <                unsc = ndp->tunsamp;
496 <                dfp = ndp->sd->tf;
497 <                cvt_sdcolor(unsc, &ndp->sd->tLamb);
495 >                unsc = ndp->tdiff;
496 >                if (ndp->pr->rod > 0)
497 >                        dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
498 >                else
499 >                        dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
500          } else /* sflags == SDsampSpR */ {
501 <                unsc = ndp->runsamp;
502 <                if (ndp->pr->rod > .0) {
501 >                unsc = ndp->rdiff;
502 >                if (ndp->pr->rod > 0)
503                          dfp = ndp->sd->rf;
504 <                        cvt_sdcolor(unsc, &ndp->sd->rLambFront);
218 <                } else {
504 >                else
505                          dfp = ndp->sd->rb;
220                        cvt_sdcolor(unsc, &ndp->sd->rLambBack);
221                }
506          }
223        multcolor(unsc, ndp->pr->pcol);
507          if (dfp == NULL)                        /* no specular component? */
508                  return(0);
509                                                  /* below sampling threshold? */
510          if (dfp->maxHemi <= specthresh+FTINY) {
511                  if (dfp->maxHemi > FTINY) {     /* XXX no color from BSDF */
512 <                        double  d = SDdirectHemi(ndp->vinc, sflags, ndp->sd);
512 >                        FVECT   vjit;
513 >                        double  d;
514                          COLOR   ctmp;
515 +                        bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
516 +                        d = SDdirectHemi(vjit, sflags, ndp->sd);
517                          if (sflags == SDsampSpT) {
518                                  copycolor(ctmp, ndp->pr->pcol);
519                                  scalecolor(ctmp, d);
# Line 252 | Line 538 | sample_sdf(BSDFDAT *ndp, int sflags)
538   int
539   m_bsdf(OBJREC *m, RAY *r)
540   {
541 +        int     hitfront;
542          COLOR   ctmp;
543          SDError ec;
544 <        FVECT   upvec, outVec;
544 >        FVECT   upvec, vtmp;
545          MFUNC   *mf;
546          BSDFDAT nd;
547                                                  /* check arguments */
548          if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) |
549                                  (m->oargs.nfargs % 3))
550                  objerror(m, USER, "bad # arguments");
551 <
552 <                                                /* get BSDF data */
266 <        nd.sd = loadBSDF(m->oargs.sarg[1]);
551 >                                                /* record surface struck */
552 >        hitfront = (r->rod > 0);
553                                                  /* load cal file */
554          mf = getfunc(m, 5, 0x1d, 1);
555 +        setfunc(m, r);
556                                                  /* get thickness */
557          nd.thick = evalue(mf->ep[0]);
558 <        if (nd.thick < .0)
558 >        if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
559                  nd.thick = .0;
560 <                                                /* check shadow */
561 <        if (r->crtype & SHADOW) {
562 <                if ((nd.thick > FTINY) & (nd.sd->tf != NULL))
563 <                        raytrans(r);            /* pass-through */
277 <                SDfreeCache(nd.sd);
278 <                return(1);                      /* else shadow */
560 >                                                /* check backface visibility */
561 >        if (!hitfront & !backvis) {
562 >                raytrans(r);
563 >                return(1);
564          }
565 <                                                /* check unscattered ray */
566 <        if (!(r->crtype & (SPECULAR|AMBIENT)) &&
567 <                        (nd.thick > FTINY) & (nd.sd->tf != NULL)) {
568 <                SDfreeCache(nd.sd);
569 <                raytrans(r);                    /* pass-through */
565 >                                                /* check other rays to pass */
566 >        if (nd.thick != 0 && (r->crtype & SHADOW ||
567 >                                !(r->crtype & (SPECULAR|AMBIENT)) ||
568 >                                (nd.thick > 0) ^ hitfront)) {
569 >                raytrans(r);                    /* hide our proxy */
570                  return(1);
571          }
572 +        nd.mp = m;
573 +        nd.pr = r;
574 +                                                /* get BSDF data */
575 +        nd.sd = loadBSDF(m->oargs.sarg[1]);
576 +                                                /* early shadow check */
577 +        if (r->crtype & SHADOW && (nd.sd->tf == NULL) & (nd.sd->tb == NULL))
578 +                return(1);
579                                                  /* diffuse reflectance */
580 <        if (r->rod > .0) {
581 <                if (m->oargs.nfargs < 3)
582 <                        setcolor(nd.rdiff, .0, .0, .0);
583 <                else
292 <                        setcolor(nd.rdiff, m->oargs.farg[0],
580 >        if (hitfront) {
581 >                cvt_sdcolor(nd.rdiff, &nd.sd->rLambFront);
582 >                if (m->oargs.nfargs >= 3) {
583 >                        setcolor(ctmp, m->oargs.farg[0],
584                                          m->oargs.farg[1],
585                                          m->oargs.farg[2]);
586 +                        addcolor(nd.rdiff, ctmp);
587 +                }
588          } else {
589 <                if (m->oargs.nfargs < 6) {      /* check invisible backside */
590 <                        if (!backvis && (nd.sd->rb == NULL) &
591 <                                                (nd.sd->tf == NULL)) {
299 <                                SDfreeCache(nd.sd);
300 <                                raytrans(r);
301 <                                return(1);
302 <                        }
303 <                        setcolor(nd.rdiff, .0, .0, .0);
304 <                } else
305 <                        setcolor(nd.rdiff, m->oargs.farg[3],
589 >                cvt_sdcolor(nd.rdiff, &nd.sd->rLambBack);
590 >                if (m->oargs.nfargs >= 6) {
591 >                        setcolor(ctmp, m->oargs.farg[3],
592                                          m->oargs.farg[4],
593                                          m->oargs.farg[5]);
594 +                        addcolor(nd.rdiff, ctmp);
595 +                }
596          }
597                                                  /* diffuse transmittance */
598 <        if (m->oargs.nfargs < 9)
599 <                setcolor(nd.tdiff, .0, .0, .0);
600 <        else
313 <                setcolor(nd.tdiff, m->oargs.farg[6],
598 >        cvt_sdcolor(nd.tdiff, &nd.sd->tLamb);
599 >        if (m->oargs.nfargs >= 9) {
600 >                setcolor(ctmp, m->oargs.farg[6],
601                                  m->oargs.farg[7],
602                                  m->oargs.farg[8]);
603 <        nd.mp = m;
604 <        nd.pr = r;
603 >                addcolor(nd.tdiff, ctmp);
604 >        }
605                                                  /* get modifiers */
606          raytexture(r, m->omod);
320        if (bright(r->pcol) <= FTINY) {         /* black pattern?! */
321                SDfreeCache(nd.sd);
322                return(1);
323        }
607                                                  /* modify diffuse values */
608          multcolor(nd.rdiff, r->pcol);
609          multcolor(nd.tdiff, r->pcol);
# Line 329 | Line 612 | m_bsdf(OBJREC *m, RAY *r)
612          upvec[1] = evalue(mf->ep[2]);
613          upvec[2] = evalue(mf->ep[3]);
614                                                  /* return to world coords */
615 <        if (mf->f != &unitxf) {
616 <                multv3(upvec, upvec, mf->f->xfm);
617 <                nd.thick *= mf->f->sca;
615 >        if (mf->fxp != &unitxf) {
616 >                multv3(upvec, upvec, mf->fxp->xfm);
617 >                nd.thick *= mf->fxp->sca;
618          }
619 +        if (r->rox != NULL) {
620 +                multv3(upvec, upvec, r->rox->f.xfm);
621 +                nd.thick *= r->rox->f.sca;
622 +        }
623          raynormal(nd.pnorm, r);
624                                                  /* compute local BSDF xform */
625          ec = SDcompXform(nd.toloc, nd.pnorm, upvec);
626          if (!ec) {
627 <                nd.vinc[0] = -r->rdir[0];
628 <                nd.vinc[1] = -r->rdir[1];
629 <                nd.vinc[2] = -r->rdir[2];
630 <                ec = SDmapDir(nd.vinc, nd.toloc, nd.vinc);
627 >                nd.vray[0] = -r->rdir[0];
628 >                nd.vray[1] = -r->rdir[1];
629 >                nd.vray[2] = -r->rdir[2];
630 >                ec = SDmapDir(nd.vray, nd.toloc, nd.vray);
631          }
345        if (!ec)
346                ec = SDinvXform(nd.fromloc, nd.toloc);
632          if (ec) {
633 <                objerror(m, WARNING, transSDError(ec));
349 <                SDfreeCache(nd.sd);
633 >                objerror(m, WARNING, "Illegal orientation vector");
634                  return(1);
635          }
636 <        if (r->rod < .0) {                      /* perturb normal towards hit */
636 >        compute_through(&nd);                   /* compute through component */
637 >        if (r->crtype & SHADOW) {
638 >                RAY     tr;                     /* attempt to pass shadow ray */
639 >                if (rayorigin(&tr, TRANS, r, nd.cthru) < 0)
640 >                        return(1);              /* blocked */
641 >                VCOPY(tr.rdir, r->rdir);
642 >                rayvalue(&tr);                  /* transmit with scaling */
643 >                multcolor(tr.rcol, tr.rcoef);
644 >                copycolor(r->rcol, tr.rcol);
645 >                return(1);                      /* we're done */
646 >        }
647 >        ec = SDinvXform(nd.fromloc, nd.toloc);
648 >        if (!ec)                                /* determine BSDF resolution */
649 >                ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL,
650 >                                        SDqueryMin+SDqueryMax, nd.sd);
651 >        if (ec)
652 >                objerror(m, USER, transSDError(ec));
653 >
654 >        nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]);
655 >        nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]);
656 >        if (!hitfront) {                        /* perturb normal towards hit */
657                  nd.pnorm[0] = -nd.pnorm[0];
658                  nd.pnorm[1] = -nd.pnorm[1];
659                  nd.pnorm[2] = -nd.pnorm[2];
# Line 359 | Line 663 | m_bsdf(OBJREC *m, RAY *r)
663                                                  /* sample transmission */
664          sample_sdf(&nd, SDsampSpT);
665                                                  /* compute indirect diffuse */
666 <        copycolor(ctmp, nd.rdiff);
667 <        addcolor(ctmp, nd.runsamp);
364 <        if (bright(ctmp) > FTINY) {             /* ambient from this side */
365 <                if (r->rod < .0)
666 >        if (bright(nd.rdiff) > FTINY) {         /* ambient from reflection */
667 >                if (!hitfront)
668                          flipsurface(r);
669 +                copycolor(ctmp, nd.rdiff);
670                  multambient(ctmp, r, nd.pnorm);
671                  addcolor(r->rcol, ctmp);
672 <                if (r->rod < .0)
672 >                if (!hitfront)
673                          flipsurface(r);
674          }
675 <        copycolor(ctmp, nd.tdiff);
373 <        addcolor(ctmp, nd.tunsamp);
374 <        if (bright(ctmp) > FTINY) {             /* ambient from other side */
675 >        if (bright(nd.tdiff) > FTINY) {         /* ambient from other side */
676                  FVECT  bnorm;
677 <                if (r->rod > .0)
677 >                if (hitfront)
678                          flipsurface(r);
679                  bnorm[0] = -nd.pnorm[0];
680                  bnorm[1] = -nd.pnorm[1];
681                  bnorm[2] = -nd.pnorm[2];
682 <                multambient(ctmp, r, bnorm);
682 >                copycolor(ctmp, nd.tdiff);
683 >                if (nd.thick != 0) {            /* proxy with offset? */
684 >                        VCOPY(vtmp, r->rop);
685 >                        VSUM(r->rop, vtmp, r->ron, nd.thick);
686 >                        multambient(ctmp, r, bnorm);
687 >                        VCOPY(r->rop, vtmp);
688 >                } else
689 >                        multambient(ctmp, r, bnorm);
690                  addcolor(r->rcol, ctmp);
691 <                if (r->rod > .0)
691 >                if (hitfront)
692                          flipsurface(r);
693          }
694                                                  /* add direct component */
695 <        direct(r, dirbsdf, &nd);
695 >        if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) &
696 >                                        (nd.sd->tb == NULL)) {
697 >                direct(r, dir_brdf, &nd);       /* reflection only */
698 >        } else if (nd.thick == 0) {
699 >                direct(r, dir_bsdf, &nd);       /* thin surface scattering */
700 >        } else {
701 >                direct(r, dir_brdf, &nd);       /* reflection first */
702 >                VCOPY(vtmp, r->rop);            /* offset for transmitted */
703 >                VSUM(r->rop, vtmp, r->ron, -nd.thick);
704 >                direct(r, dir_btdf, &nd);       /* separate transmission */
705 >                VCOPY(r->rop, vtmp);
706 >        }
707                                                  /* clean up */
708          SDfreeCache(nd.sd);
709          return(1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines