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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines