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.2 by greg, Fri Feb 18 02:41:55 2011 UTC vs.
Revision 2.42 by greg, Tue Nov 28 22:17:00 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 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
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   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 */
62        COLOR   tdiff;          /* added diffuse transmission */
85   }  BSDFDAT;             /* BSDF material data */
86  
87   #define cvt_sdcolor(cv, svp)    ccy2rgb(&(svp)->spec, (svp)->cieY, cv)
88  
89 < /* Compute source contribution for BSDF */
89 > /* Compute "through" component color */
90   static void
91 < dirbsdf(
91 > compute_through(BSDFDAT *ndp)
92 > {
93 > #define NDIR2CHECK      13
94 >        static const float      dir2check[NDIR2CHECK][2] = {
95 >                                        {0, 0},
96 >                                        {-0.8, 0},
97 >                                        {0, 0.8},
98 >                                        {0, -0.8},
99 >                                        {0.8, 0},
100 >                                        {-0.8, 0.8},
101 >                                        {-0.8, -0.8},
102 >                                        {0.8, 0.8},
103 >                                        {0.8, -0.8},
104 >                                        {-1.6, 0},
105 >                                        {0, 1.6},
106 >                                        {0, -1.6},
107 >                                        {1.6, 0},
108 >                                };
109 >        const double    peak_over = 2.0;
110 >        SDSpectralDF    *dfp;
111 >        FVECT           pdir;
112 >        double          tomega, srchrad;
113 >        COLOR           vpeak, vsum;
114 >        int             i;
115 >        SDError         ec;
116 >
117 >        setcolor(ndp->cthru, 0, 0, 0);          /* starting assumption */
118 >
119 >        if (ndp->pr->rod > 0)
120 >                dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
121 >        else
122 >                dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
123 >
124 >        if (dfp == NULL)
125 >                return;                         /* no specular transmission */
126 >        if (bright(ndp->pr->pcol) <= FTINY)
127 >                return;                         /* pattern is black, here */
128 >        srchrad = sqrt(dfp->minProjSA);         /* else search for peak */
129 >        setcolor(vpeak, 0, 0, 0);
130 >        setcolor(vsum, 0, 0, 0);
131 >        pdir[2] = 0.0;
132 >        for (i = 0; i < NDIR2CHECK; i++) {
133 >                FVECT   tdir;
134 >                SDValue sv;
135 >                COLOR   vcol;
136 >                tdir[0] = -ndp->vray[0] + dir2check[i][0]*srchrad;
137 >                tdir[1] = -ndp->vray[1] + dir2check[i][1]*srchrad;
138 >                tdir[2] = -ndp->vray[2];
139 >                normalize(tdir);
140 >                ec = SDevalBSDF(&sv, tdir, ndp->vray, ndp->sd);
141 >                if (ec)
142 >                        goto baderror;
143 >                cvt_sdcolor(vcol, &sv);
144 >                addcolor(vsum, vcol);
145 >                if (bright(vcol) > bright(vpeak)) {
146 >                        copycolor(vpeak, vcol);
147 >                        VCOPY(pdir, tdir);
148 >                }
149 >        }
150 >        if (pdir[2] == 0.0)
151 >                return;                         /* zero neighborhood */
152 >        ec = SDsizeBSDF(&tomega, pdir, ndp->vray, SDqueryMin, ndp->sd);
153 >        if (ec)
154 >                goto baderror;
155 >        if (tomega > 1.5*dfp->minProjSA)
156 >                return;                         /* not really a peak? */
157 >        if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .001)
158 >                return;                         /* < 0.1% transmission */
159 >        for (i = 3; i--; )                      /* remove peak from average */
160 >                colval(vsum,i) -= colval(vpeak,i);
161 >        if (peak_over*bright(vsum) >= (NDIR2CHECK-1)*bright(vpeak))
162 >                return;                         /* not peaky enough */
163 >        copycolor(ndp->cthru, vpeak);           /* else use it */
164 >        scalecolor(ndp->cthru, tomega);
165 >        multcolor(ndp->cthru, ndp->pr->pcol);   /* modify by pattern */
166 >        return;
167 > baderror:
168 >        objerror(ndp->mp, USER, transSDError(ec));
169 > #undef NDIR2CHECK
170 > }
171 >
172 > /* Jitter ray sample according to projected solid angle and specjitter */
173 > static void
174 > bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
175 > {
176 >        VCOPY(vres, ndp->vray);
177 >        if (specjitter < 1.)
178 >                sr_psa *= specjitter;
179 >        if (sr_psa <= FTINY)
180 >                return;
181 >        vres[0] += sr_psa*(.5 - frandom());
182 >        vres[1] += sr_psa*(.5 - frandom());
183 >        normalize(vres);
184 > }
185 >
186 > /* Get BSDF specular for direct component, returning true if OK to proceed */
187 > static int
188 > direct_specular_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
189 > {
190 >        int     nsamp, ok = 0;
191 >        FVECT   vsrc, vsmp, vjit;
192 >        double  tomega, tomega2;
193 >        double  sf, tsr, sd[2];
194 >        COLOR   csmp, cdiff;
195 >        double  diffY;
196 >        SDValue sv;
197 >        SDError ec;
198 >        int     i;
199 >                                        /* in case we fail */
200 >        setcolor(cval,  0, 0, 0);
201 >                                        /* transform source direction */
202 >        if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
203 >                return(0);
204 >                                        /* will discount diffuse portion */
205 >        switch ((vsrc[2] > 0)<<1 | (ndp->vray[2] > 0)) {
206 >        case 3:
207 >                if (ndp->sd->rf == NULL)
208 >                        return(0);      /* all diffuse */
209 >                sv = ndp->sd->rLambFront;
210 >                break;
211 >        case 0:
212 >                if (ndp->sd->rb == NULL)
213 >                        return(0);      /* all diffuse */
214 >                sv = ndp->sd->rLambBack;
215 >                break;
216 >        default:
217 >                if ((ndp->sd->tf == NULL) & (ndp->sd->tb == NULL))
218 >                        return(0);      /* all diffuse */
219 >                sv = ndp->sd->tLamb;
220 >                break;
221 >        }
222 >        if (sv.cieY > FTINY) {
223 >                diffY = sv.cieY *= 1./PI;
224 >                cvt_sdcolor(cdiff, &sv);
225 >        } else {
226 >                diffY = 0;
227 >                setcolor(cdiff,  0, 0, 0);
228 >        }
229 >                                        /* need projected solid angles */
230 >        omega *= fabs(vsrc[2]);
231 >        ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
232 >        if (ec)
233 >                goto baderror;
234 >                                        /* check indirect over-counting */
235 >        if ((vsrc[2] > 0) ^ (ndp->vray[2] > 0) && bright(ndp->cthru) > FTINY) {
236 >                double  dx = vsrc[0] + ndp->vray[0];
237 >                double  dy = vsrc[1] + ndp->vray[1];
238 >                if (dx*dx + dy*dy <= (4./PI)*(omega + tomega +
239 >                                                2.*sqrt(omega*tomega)))
240 >                        return(0);
241 >        }
242 >                                        /* assign number of samples */
243 >        sf = specjitter * ndp->pr->rweight;
244 >        if (tomega <= 0)
245 >                nsamp = 1;
246 >        else if (25.*tomega <= omega)
247 >                nsamp = 100.*sf + .5;
248 >        else
249 >                nsamp = 4.*sf*omega/tomega + .5;
250 >        nsamp += !nsamp;
251 >        sf = sqrt(omega);               /* sample our source area */
252 >        tsr = sqrt(tomega);
253 >        for (i = nsamp; i--; ) {
254 >                VCOPY(vsmp, vsrc);      /* jitter query directions */
255 >                if (nsamp > 1) {
256 >                        multisamp(sd, 2, (i + frandom())/(double)nsamp);
257 >                        vsmp[0] += (sd[0] - .5)*sf;
258 >                        vsmp[1] += (sd[1] - .5)*sf;
259 >                        normalize(vsmp);
260 >                }
261 >                bsdf_jitter(vjit, ndp, tsr);
262 >                                        /* compute BSDF */
263 >                ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
264 >                if (ec)
265 >                        goto baderror;
266 >                if (sv.cieY - diffY <= FTINY)
267 >                        continue;       /* no specular part */
268 >                                        /* check for variable resolution */
269 >                ec = SDsizeBSDF(&tomega2, vjit, vsmp, SDqueryMin, ndp->sd);
270 >                if (ec)
271 >                        goto baderror;
272 >                if (tomega2 < .12*tomega)
273 >                        continue;       /* not safe to include */
274 >                cvt_sdcolor(csmp, &sv);
275 >                addcolor(cval, csmp);   /* else average it in */
276 >                ++ok;
277 >        }
278 >        if (!ok)                        /* no valid specular samples? */
279 >                return(0);
280 >
281 >        sf = 1./(double)ok;             /* compute average BSDF */
282 >        scalecolor(cval, sf);
283 >                                        /* subtract diffuse contribution */
284 >        for (i = 3*(diffY > FTINY); i--; )
285 >                if ((colval(cval,i) -= colval(cdiff,i)) < 0)
286 >                        colval(cval,i) = 0;
287 >        return(1);
288 > baderror:
289 >        objerror(ndp->mp, USER, transSDError(ec));
290 >        return(0);                      /* gratis return */
291 > }
292 >
293 > /* Compute source contribution for BSDF (reflected & transmitted) */
294 > static void
295 > dir_bsdf(
296          COLOR  cval,                    /* returned coefficient */
297          void  *nnp,                     /* material data */
298          FVECT  ldir,                    /* light source direction */
299          double  omega                   /* light source size */
300   )
301   {
302 <        BSDFDAT         *np = nnp;
77 <        SDError         ec;
78 <        SDValue         sv;
79 <        FVECT           vout;
302 >        BSDFDAT         *np = (BSDFDAT *)nnp;
303          double          ldot;
304          double          dtmp;
305          COLOR           ctmp;
306  
307 <        setcolor(cval, .0, .0, .0);
307 >        setcolor(cval,  0, 0, 0);
308  
309          ldot = DOT(np->pnorm, ldir);
310          if ((-FTINY <= ldot) & (ldot <= FTINY))
311                  return;
312  
313 <        if (ldot > .0 && bright(np->rdiff) > FTINY) {
313 >        if (ldot > 0 && bright(np->rdiff) > FTINY) {
314                  /*
315 <                 *  Compute added diffuse reflected component.
315 >                 *  Compute diffuse reflected component
316                   */
317                  copycolor(ctmp, np->rdiff);
318                  dtmp = ldot * omega * (1./PI);
319                  scalecolor(ctmp, dtmp);
320                  addcolor(cval, ctmp);
321          }
322 <        if (ldot < .0 && bright(np->tdiff) > FTINY) {
322 >        if (ldot < 0 && bright(np->tdiff) > FTINY) {
323                  /*
324 <                 *  Compute added diffuse transmission.
324 >                 *  Compute diffuse transmission
325                   */
326                  copycolor(ctmp, np->tdiff);
327                  dtmp = -ldot * omega * (1.0/PI);
328                  scalecolor(ctmp, dtmp);
329                  addcolor(cval, ctmp);
330          }
331 +        if (ambRayInPmap(np->pr))
332 +                return;         /* specular already in photon map */
333          /*
334 <         *  Compute scattering coefficient using BSDF.
334 >         *  Compute specular scattering coefficient using BSDF
335           */
336 <        if (SDmapDir(vout, np->toloc, ldir) != SDEnone)
336 >        if (!direct_specular_OK(ctmp, ldir, omega, np))
337                  return;
338 <        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 desaturated pattern */
130 <                dtmp = ldot * omega;
131 <        } else {                        /* full pattern on transmission */
338 >        if (ldot < 0) {         /* pattern for specular transmission */
339                  multcolor(ctmp, np->pr->pcol);
340                  dtmp = -ldot * omega;
341 +        } else
342 +                dtmp = ldot * omega;
343 +        scalecolor(ctmp, dtmp);
344 +        addcolor(cval, ctmp);
345 + }
346 +
347 + /* Compute source contribution for BSDF (reflected only) */
348 + static void
349 + dir_brdf(
350 +        COLOR  cval,                    /* returned coefficient */
351 +        void  *nnp,                     /* material data */
352 +        FVECT  ldir,                    /* light source direction */
353 +        double  omega                   /* light source size */
354 + )
355 + {
356 +        BSDFDAT         *np = (BSDFDAT *)nnp;
357 +        double          ldot;
358 +        double          dtmp;
359 +        COLOR           ctmp, ctmp1, ctmp2;
360 +
361 +        setcolor(cval,  0, 0, 0);
362 +
363 +        ldot = DOT(np->pnorm, ldir);
364 +        
365 +        if (ldot <= FTINY)
366 +                return;
367 +
368 +        if (bright(np->rdiff) > FTINY) {
369 +                /*
370 +                 *  Compute diffuse reflected component
371 +                 */
372 +                copycolor(ctmp, np->rdiff);
373 +                dtmp = ldot * omega * (1./PI);
374 +                scalecolor(ctmp, dtmp);
375 +                addcolor(cval, ctmp);
376          }
377 +        if (ambRayInPmap(np->pr))
378 +                return;         /* specular already in photon map */
379 +        /*
380 +         *  Compute specular reflection coefficient using BSDF
381 +         */
382 +        if (!direct_specular_OK(ctmp, ldir, omega, np))
383 +                return;
384 +        dtmp = ldot * omega;
385          scalecolor(ctmp, dtmp);
386          addcolor(cval, ctmp);
387   }
388  
389 + /* Compute source contribution for BSDF (transmitted only) */
390 + static void
391 + dir_btdf(
392 +        COLOR  cval,                    /* returned coefficient */
393 +        void  *nnp,                     /* material data */
394 +        FVECT  ldir,                    /* light source direction */
395 +        double  omega                   /* light source size */
396 + )
397 + {
398 +        BSDFDAT         *np = (BSDFDAT *)nnp;
399 +        double          ldot;
400 +        double          dtmp;
401 +        COLOR           ctmp;
402 +
403 +        setcolor(cval,  0, 0, 0);
404 +
405 +        ldot = DOT(np->pnorm, ldir);
406 +
407 +        if (ldot >= -FTINY)
408 +                return;
409 +
410 +        if (bright(np->tdiff) > FTINY) {
411 +                /*
412 +                 *  Compute diffuse transmission
413 +                 */
414 +                copycolor(ctmp, np->tdiff);
415 +                dtmp = -ldot * omega * (1.0/PI);
416 +                scalecolor(ctmp, dtmp);
417 +                addcolor(cval, ctmp);
418 +        }
419 +        if (ambRayInPmap(np->pr))
420 +                return;         /* specular already in photon map */
421 +        /*
422 +         *  Compute specular scattering coefficient using BSDF
423 +         */
424 +        if (!direct_specular_OK(ctmp, ldir, omega, np))
425 +                return;
426 +                                        /* full pattern on transmission */
427 +        multcolor(ctmp, np->pr->pcol);
428 +        dtmp = -ldot * omega;
429 +        scalecolor(ctmp, dtmp);
430 +        addcolor(cval, ctmp);
431 + }
432 +
433   /* Sample separate BSDF component */
434   static int
435 < sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
435 > sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int xmit)
436   {
437 <        int     nstarget = 1;
438 <        int     nsent = 0;
439 <        SDError ec;
440 <        SDValue bsv;
441 <        double  sthick;
442 <        FVECT   vout;
443 <        RAY     sr;
444 <        int     ntrials;
437 >        const int       hasthru = (xmit &&
438 >                                !(ndp->pr->crtype & (SPECULAR|AMBIENT)) &&
439 >                                bright(ndp->cthru) > FTINY);
440 >        int             nstarget = 1;
441 >        int             nsent = 0;
442 >        int             n;
443 >        SDError         ec;
444 >        SDValue         bsv;
445 >        double          xrand;
446 >        FVECT           vsmp, vinc;
447 >        RAY             sr;
448                                                  /* multiple samples? */
449          if (specjitter > 1.5) {
450                  nstarget = specjitter*ndp->pr->rweight + .5;
451 <                if (nstarget < 1)
155 <                        nstarget = 1;
451 >                nstarget += !nstarget;
452          }
453 <                                                /* run through our trials */
454 <        for (ntrials = 0; nsent < nstarget && ntrials < 9*nstarget; ntrials++) {
455 <                SDerrorDetail[0] = '\0';
456 <                                                /* sample direction & coef. */
457 <                ec = SDsampComponent(&bsv, vout, ndp->vinc,
458 <                                ntrials ? frandom()
459 <                                        : urand(ilhash(dimlist,ndims)+samplendx),
460 <                                                dcp);
453 >                                                /* run through our samples */
454 >        for (n = 0; n < nstarget; n++) {
455 >                if (nstarget == 1) {            /* stratify random variable */
456 >                        xrand = urand(ilhash(dimlist,ndims)+samplendx);
457 >                        if (specjitter < 1.)
458 >                                xrand = .5 + specjitter*(xrand-.5);
459 >                } else {
460 >                        xrand = (n + frandom())/(double)nstarget;
461 >                }
462 >                SDerrorDetail[0] = '\0';        /* sample direction & coef. */
463 >                bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]);
464 >                VCOPY(vinc, vsmp);              /* to compare after */
465 >                ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
466                  if (ec)
467                          objerror(ndp->mp, USER, transSDError(ec));
468 <                                                /* zero component? */
168 <                if (bsv.cieY <= FTINY)
468 >                if (bsv.cieY <= FTINY)          /* zero component? */
469                          break;
470 <                                                /* map vector to world */
471 <                if (SDmapDir(sr.rdir, ndp->fromloc, vout) != SDEnone)
470 >                if (hasthru) {                  /* check for view ray */
471 >                        double  dx = vinc[0] + vsmp[0];
472 >                        double  dy = vinc[1] + vsmp[1];
473 >                        if (dx*dx + dy*dy <= ndp->sr_vpsa[0]*ndp->sr_vpsa[0])
474 >                                continue;       /* exclude view sample */
475 >                }
476 >                                                /* map non-view sample->world */
477 >                if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
478                          break;
173                                                /* unintentional penetration? */
174                if (DOT(sr.rdir, ndp->pr->ron) > .0 ^ vout[2] > .0)
175                        continue;
479                                                  /* spawn a specular ray */
480                  if (nstarget > 1)
481                          bsv.cieY /= (double)nstarget;
482 <                cvt_sdcolor(sr.rcoef, &bsv);    /* use color */
483 <                if (usepat)                     /* pattern on transmission */
482 >                cvt_sdcolor(sr.rcoef, &bsv);    /* use sample color */
483 >                if (xmit)                       /* apply pattern on transmit */
484                          multcolor(sr.rcoef, ndp->pr->pcol);
485                  if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
486 <                        if (maxdepth  > 0)
486 >                        if (maxdepth > 0)
487                                  break;
488 <                        ++nsent;                /* Russian roulette victim */
186 <                        continue;
488 >                        continue;               /* Russian roulette victim */
489                  }
490 <                                                /* need to move origin? */
491 <                sthick = (ndp->pr->rod > .0) ? -ndp->thick : ndp->thick;
190 <                if (sthick < .0 ^ vout[2] > .0)
191 <                        VSUM(sr.rorg, sr.rorg, ndp->pr->ron, sthick);
192 <
490 >                if (xmit && ndp->thick != 0)    /* need to offset origin? */
491 >                        VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
492                  rayvalue(&sr);                  /* send & evaluate sample */
493                  multcolor(sr.rcol, sr.rcoef);
494                  addcolor(ndp->pr->rcol, sr.rcol);
# Line 202 | Line 501 | sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usep
501   static int
502   sample_sdf(BSDFDAT *ndp, int sflags)
503   {
504 +        int             hasthru = (sflags == SDsampSpT
505 +                                    && !(ndp->pr->crtype & (SPECULAR|AMBIENT))
506 +                                    && bright(ndp->cthru) > FTINY);
507          int             n, ntotal = 0;
508 +        double          b = 0;
509          SDSpectralDF    *dfp;
510          COLORV          *unsc;
511  
512          if (sflags == SDsampSpT) {
513                  unsc = ndp->tunsamp;
514 <                dfp = ndp->sd->tf;
515 <                cvt_sdcolor(unsc, &ndp->sd->tLamb);
514 >                if (ndp->pr->rod > 0)
515 >                        dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
516 >                else
517 >                        dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
518          } else /* sflags == SDsampSpR */ {
519                  unsc = ndp->runsamp;
520 <                if (ndp->pr->rod > .0) {
520 >                if (ndp->pr->rod > 0)
521                          dfp = ndp->sd->rf;
522 <                        cvt_sdcolor(unsc, &ndp->sd->rLambFront);
218 <                } else {
522 >                else
523                          dfp = ndp->sd->rb;
220                        cvt_sdcolor(unsc, &ndp->sd->rLambBack);
221                }
524          }
525 <        multcolor(unsc, ndp->pr->pcol);
525 >        setcolor(unsc,  0, 0, 0);
526          if (dfp == NULL)                        /* no specular component? */
527                  return(0);
528 <                                                /* below sampling threshold? */
529 <        if (dfp->maxHemi <= specthresh+FTINY) {
530 <                if (dfp->maxHemi > FTINY) {     /* XXX no color from BSDF! */
531 <                        double  d = SDdirectHemi(ndp->vinc, sflags, ndp->sd);
532 <                        COLOR   ctmp;
528 >
529 >        if (hasthru) {                          /* separate view sample? */
530 >                RAY     tr;
531 >                if (rayorigin(&tr, TRANS, ndp->pr, ndp->cthru) == 0) {
532 >                        VCOPY(tr.rdir, ndp->pr->rdir);
533 >                        rayvalue(&tr);
534 >                        multcolor(tr.rcol, tr.rcoef);
535 >                        addcolor(ndp->pr->rcol, tr.rcol);
536 >                        ++ntotal;
537 >                        b = bright(ndp->cthru);
538 >                } else
539 >                        hasthru = 0;
540 >        }
541 >        if (dfp->maxHemi - b <= FTINY) {        /* how specular to sample? */
542 >                b = 0;
543 >        } else {
544 >                FVECT   vjit;
545 >                bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
546 >                b = SDdirectHemi(vjit, sflags, ndp->sd) - b;
547 >                if (b < 0) b = 0;
548 >        }
549 >        if (b <= specthresh+FTINY) {            /* below sampling threshold? */
550 >                if (b > FTINY) {                /* XXX no color from BSDF */
551                          if (sflags == SDsampSpT) {
552 <                                copycolor(ctmp, ndp->pr->pcol);
553 <                                scalecolor(ctmp, d);
552 >                                copycolor(unsc, ndp->pr->pcol);
553 >                                scalecolor(unsc, b);
554                          } else                  /* no pattern on reflection */
555 <                                setcolor(ctmp, d, d, d);
236 <                        addcolor(unsc, ctmp);
555 >                                setcolor(unsc, b, b, b);
556                  }
557 <                return(0);
557 >                return(ntotal);
558          }
559 <                                                /* else need to sample */
560 <        dimlist[ndims++] = (int)(size_t)ndp->mp;
242 <        ndims++;
559 >        dimlist[ndims] = (int)(size_t)ndp->mp;  /* else sample specular */
560 >        ndims += 2;
561          for (n = dfp->ncomp; n--; ) {           /* loop over components */
562                  dimlist[ndims-1] = n + 9438;
563                  ntotal += sample_sdcomp(ndp, &dfp->comp[n], sflags==SDsampSpT);
# Line 252 | Line 570 | sample_sdf(BSDFDAT *ndp, int sflags)
570   int
571   m_bsdf(OBJREC *m, RAY *r)
572   {
573 +        int     hitfront;
574          COLOR   ctmp;
575          SDError ec;
576 <        FVECT   upvec, outVec;
576 >        FVECT   upvec, vtmp;
577          MFUNC   *mf;
578          BSDFDAT nd;
579                                                  /* check arguments */
580          if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) |
581                                  (m->oargs.nfargs % 3))
582                  objerror(m, USER, "bad # arguments");
583 <
584 <                                                /* get BSDF data */
266 <        nd.sd = loadBSDF(m->oargs.sarg[1]);
583 >                                                /* record surface struck */
584 >        hitfront = (r->rod > 0);
585                                                  /* load cal file */
586          mf = getfunc(m, 5, 0x1d, 1);
587 +        setfunc(m, r);
588                                                  /* get thickness */
589          nd.thick = evalue(mf->ep[0]);
590 <        if (nd.thick < .0)
591 <                nd.thick = .0;
592 <                                                /* check shadow */
593 <        if (r->crtype & SHADOW) {
594 <                SDfreeCache(nd.sd);
595 <                if (nd.thick > FTINY && nd.sd->tf != NULL &&
277 <                                nd.sd->tf->maxHemi > FTINY)
278 <                        raytrans(r);            /* pass-through */
279 <                return(1);                      /* else shadow */
590 >        if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
591 >                nd.thick = 0;
592 >                                                /* check backface visibility */
593 >        if (!hitfront & !backvis) {
594 >                raytrans(r);
595 >                return(1);
596          }
597 <                                                /* check unscattered ray */
598 <        if (!(r->crtype & (SPECULAR|AMBIENT)) && nd.thick > FTINY &&
599 <                        nd.sd->tf != NULL && nd.sd->tf->maxHemi > FTINY) {
600 <                SDfreeCache(nd.sd);
601 <                raytrans(r);                    /* pass-through */
597 >                                                /* check other rays to pass */
598 >        if (nd.thick != 0 && (r->crtype & SHADOW ||
599 >                                !(r->crtype & (SPECULAR|AMBIENT)) ||
600 >                                (nd.thick > 0) ^ hitfront)) {
601 >                raytrans(r);                    /* hide our proxy */
602                  return(1);
603          }
604 +        nd.mp = m;
605 +        nd.pr = r;
606 +                                                /* get BSDF data */
607 +        nd.sd = loadBSDF(m->oargs.sarg[1]);
608 +                                                /* early shadow check */
609 +        if (r->crtype & SHADOW && (nd.sd->tf == NULL) & (nd.sd->tb == NULL))
610 +                return(1);
611                                                  /* diffuse reflectance */
612 <        if (r->rod > .0) {
613 <                if (m->oargs.nfargs < 3)
614 <                        setcolor(nd.rdiff, .0, .0, .0);
615 <                else
293 <                        setcolor(nd.rdiff, m->oargs.farg[0],
612 >        if (hitfront) {
613 >                cvt_sdcolor(nd.rdiff, &nd.sd->rLambFront);
614 >                if (m->oargs.nfargs >= 3) {
615 >                        setcolor(ctmp, m->oargs.farg[0],
616                                          m->oargs.farg[1],
617                                          m->oargs.farg[2]);
618 +                        addcolor(nd.rdiff, ctmp);
619 +                }
620          } else {
621 <                if (m->oargs.nfargs < 6) {      /* check invisible backside */
622 <                        if (!backvis && (nd.sd->rb == NULL ||
623 <                                                nd.sd->rb->maxHemi <= FTINY) &&
300 <                                        (nd.sd->tf == NULL ||
301 <                                                nd.sd->tf->maxHemi <= FTINY)) {
302 <                                SDfreeCache(nd.sd);
303 <                                raytrans(r);
304 <                                return(1);
305 <                        }
306 <                        setcolor(nd.rdiff, .0, .0, .0);
307 <                } else
308 <                        setcolor(nd.rdiff, m->oargs.farg[3],
621 >                cvt_sdcolor(nd.rdiff, &nd.sd->rLambBack);
622 >                if (m->oargs.nfargs >= 6) {
623 >                        setcolor(ctmp, m->oargs.farg[3],
624                                          m->oargs.farg[4],
625                                          m->oargs.farg[5]);
626 +                        addcolor(nd.rdiff, ctmp);
627 +                }
628          }
629                                                  /* diffuse transmittance */
630 <        if (m->oargs.nfargs < 9)
631 <                setcolor(nd.tdiff, .0, .0, .0);
632 <        else
316 <                setcolor(nd.tdiff, m->oargs.farg[6],
630 >        cvt_sdcolor(nd.tdiff, &nd.sd->tLamb);
631 >        if (m->oargs.nfargs >= 9) {
632 >                setcolor(ctmp, m->oargs.farg[6],
633                                  m->oargs.farg[7],
634                                  m->oargs.farg[8]);
635 <        nd.mp = m;
636 <        nd.pr = r;
635 >                addcolor(nd.tdiff, ctmp);
636 >        }
637                                                  /* get modifiers */
638          raytexture(r, m->omod);
323        if (bright(r->pcol) <= FTINY) {         /* black pattern?! */
324                SDfreeCache(nd.sd);
325                return(1);
326        }
639                                                  /* modify diffuse values */
640          multcolor(nd.rdiff, r->pcol);
641          multcolor(nd.tdiff, r->pcol);
# Line 332 | Line 644 | m_bsdf(OBJREC *m, RAY *r)
644          upvec[1] = evalue(mf->ep[2]);
645          upvec[2] = evalue(mf->ep[3]);
646                                                  /* return to world coords */
647 <        if (mf->f != &unitxf) {
648 <                multv3(upvec, upvec, mf->f->xfm);
649 <                nd.thick *= mf->f->sca;
647 >        if (mf->fxp != &unitxf) {
648 >                multv3(upvec, upvec, mf->fxp->xfm);
649 >                nd.thick *= mf->fxp->sca;
650          }
651 +        if (r->rox != NULL) {
652 +                multv3(upvec, upvec, r->rox->f.xfm);
653 +                nd.thick *= r->rox->f.sca;
654 +        }
655          raynormal(nd.pnorm, r);
656                                                  /* compute local BSDF xform */
657          ec = SDcompXform(nd.toloc, nd.pnorm, upvec);
658          if (!ec) {
659 <                nd.vinc[0] = -r->rdir[0];
660 <                nd.vinc[1] = -r->rdir[1];
661 <                nd.vinc[2] = -r->rdir[2];
662 <                ec = SDmapDir(nd.vinc, nd.toloc, nd.vinc);
659 >                nd.vray[0] = -r->rdir[0];
660 >                nd.vray[1] = -r->rdir[1];
661 >                nd.vray[2] = -r->rdir[2];
662 >                ec = SDmapDir(nd.vray, nd.toloc, nd.vray);
663          }
348        if (!ec)
349                ec = SDinvXform(nd.fromloc, nd.toloc);
664          if (ec) {
665 <                objerror(m, WARNING, transSDError(ec));
352 <                SDfreeCache(nd.sd);
665 >                objerror(m, WARNING, "Illegal orientation vector");
666                  return(1);
667          }
668 <        if (r->rod < .0) {                      /* perturb normal towards hit */
668 >        compute_through(&nd);                   /* compute through component */
669 >        if (r->crtype & SHADOW) {
670 >                RAY     tr;                     /* attempt to pass shadow ray */
671 >                if (rayorigin(&tr, TRANS, r, nd.cthru) < 0)
672 >                        return(1);              /* blocked */
673 >                VCOPY(tr.rdir, r->rdir);
674 >                rayvalue(&tr);                  /* transmit with scaling */
675 >                multcolor(tr.rcol, tr.rcoef);
676 >                copycolor(r->rcol, tr.rcol);
677 >                return(1);                      /* we're done */
678 >        }
679 >        ec = SDinvXform(nd.fromloc, nd.toloc);
680 >        if (!ec)                                /* determine BSDF resolution */
681 >                ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL,
682 >                                        SDqueryMin+SDqueryMax, nd.sd);
683 >        if (ec)
684 >                objerror(m, USER, transSDError(ec));
685 >
686 >        nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]);
687 >        nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]);
688 >        if (!hitfront) {                        /* perturb normal towards hit */
689                  nd.pnorm[0] = -nd.pnorm[0];
690                  nd.pnorm[1] = -nd.pnorm[1];
691                  nd.pnorm[2] = -nd.pnorm[2];
# Line 364 | Line 697 | m_bsdf(OBJREC *m, RAY *r)
697                                                  /* compute indirect diffuse */
698          copycolor(ctmp, nd.rdiff);
699          addcolor(ctmp, nd.runsamp);
700 <        if (bright(ctmp) > FTINY) {             /* ambient from this side */
701 <                if (r->rod < .0)
700 >        if (bright(ctmp) > FTINY) {             /* ambient from reflection */
701 >                if (!hitfront)
702                          flipsurface(r);
703                  multambient(ctmp, r, nd.pnorm);
704                  addcolor(r->rcol, ctmp);
705 <                if (r->rod < .0)
705 >                if (!hitfront)
706                          flipsurface(r);
707          }
708          copycolor(ctmp, nd.tdiff);
709          addcolor(ctmp, nd.tunsamp);
710          if (bright(ctmp) > FTINY) {             /* ambient from other side */
711                  FVECT  bnorm;
712 <                if (r->rod > .0)
712 >                if (hitfront)
713                          flipsurface(r);
714                  bnorm[0] = -nd.pnorm[0];
715                  bnorm[1] = -nd.pnorm[1];
716                  bnorm[2] = -nd.pnorm[2];
717 <                multambient(ctmp, r, bnorm);
717 >                if (nd.thick != 0) {            /* proxy with offset? */
718 >                        VCOPY(vtmp, r->rop);
719 >                        VSUM(r->rop, vtmp, r->ron, nd.thick);
720 >                        multambient(ctmp, r, bnorm);
721 >                        VCOPY(r->rop, vtmp);
722 >                } else
723 >                        multambient(ctmp, r, bnorm);
724                  addcolor(r->rcol, ctmp);
725 <                if (r->rod > .0)
725 >                if (hitfront)
726                          flipsurface(r);
727          }
728                                                  /* add direct component */
729 <        direct(r, dirbsdf, &nd);
729 >        if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) &
730 >                                        (nd.sd->tb == NULL)) {
731 >                direct(r, dir_brdf, &nd);       /* reflection only */
732 >        } else if (nd.thick == 0) {
733 >                direct(r, dir_bsdf, &nd);       /* thin surface scattering */
734 >        } else {
735 >                direct(r, dir_brdf, &nd);       /* reflection first */
736 >                VCOPY(vtmp, r->rop);            /* offset for transmitted */
737 >                VSUM(r->rop, vtmp, r->ron, -nd.thick);
738 >                direct(r, dir_btdf, &nd);       /* separate transmission */
739 >                VCOPY(r->rop, vtmp);
740 >        }
741                                                  /* clean up */
742          SDfreeCache(nd.sd);
743          return(1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines