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.38 by greg, Fri May 19 15:13:41 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 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
# 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   runsamp;        /* BSDF hemispherical reflection */
82 <        COLOR   rdiff;          /* added diffuse reflection */
77 <        COLOR   tunsamp;        /* BSDF hemispherical transmission */
78 <        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 < /* Jitter ray sample according to projected solid angle and specjitter */
87 > /* Compute "through" component color */
88   static void
89 < bsdf_jitter(FVECT vres, BSDFDAT *ndp)
89 > compute_through(BSDFDAT *ndp)
90   {
91 <        double  sr_psa = ndp->sr_vpsa;
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->crtype & (SPECULAR|AMBIENT|SHADOW)))
118 +                return;                         /* simply don't need to know */
119 +
120 +        if (ndp->pr->rod > 0)
121 +                dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
122 +        else
123 +                dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
124 +
125 +        if (dfp == NULL)
126 +                return;                         /* no specular transmission */
127 +        if (bright(ndp->pr->pcol) <= FTINY)
128 +                return;                         /* pattern is black, here */
129 +        srchrad = sqrt(dfp->minProjSA);         /* else search for peak */
130 +        setcolor(vpeak, .0, .0, .0);
131 +        setcolor(vsum, .0, .0, .0);
132 +        nsum = 0;
133 +        for (i = 0; i < NDIR2CHECK; i++) {
134 +                FVECT   tdir;
135 +                SDValue sv;
136 +                COLOR   vcol;
137 +                tdir[0] = -ndp->vray[0] + dir2check[i][0]*srchrad;
138 +                tdir[1] = -ndp->vray[1] + dir2check[i][1]*srchrad;
139 +                tdir[2] = -ndp->vray[2];
140 +                normalize(tdir);
141 +                ec = SDevalBSDF(&sv, tdir, ndp->vray, ndp->sd);
142 +                if (ec)
143 +                        goto baderror;
144 +                cvt_sdcolor(vcol, &sv);
145 +                addcolor(vsum, vcol);
146 +                ++nsum;
147 +                if (bright(vcol) > bright(vpeak)) {
148 +                        copycolor(vpeak, vcol);
149 +                        VCOPY(pdir, tdir);
150 +                }
151 +        }
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 <= .007)
158 +                return;                         /* < 0.7% transmission */
159 +        for (i = 3; i--; )                      /* remove peak from average */
160 +                colval(vsum,i) -= colval(vpeak,i);
161 +        --nsum;
162 +        if (peak_over*bright(vsum) >= nsum*bright(vpeak))
163 +                return;                         /* not peaky enough */
164 +        copycolor(ndp->cthru, vpeak);           /* else use it */
165 +        scalecolor(ndp->cthru, tomega);
166 +        multcolor(ndp->cthru, ndp->pr->pcol);   /* modify by pattern */
167 +        return;
168 + baderror:
169 +        objerror(ndp->mp, USER, transSDError(ec));
170 + #undef NDIR2CHECK
171 + }
172 +
173 + /* Jitter ray sample according to projected solid angle and specjitter */
174 + static void
175 + bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
176 + {
177          VCOPY(vres, ndp->vray);
178          if (specjitter < 1.)
179                  sr_psa *= specjitter;
# Line 96 | Line 184 | bsdf_jitter(FVECT vres, BSDFDAT *ndp)
184          normalize(vres);
185   }
186  
187 + /* Get BSDF specular for direct component, returning true if OK to proceed */
188 + static int
189 + direct_specular_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
190 + {
191 +        int     nsamp, ok = 0;
192 +        FVECT   vsrc, vsmp, vjit;
193 +        double  tomega, tomega2;
194 +        double  sf, tsr, sd[2];
195 +        COLOR   csmp, cdiff;
196 +        double  diffY;
197 +        SDValue sv;
198 +        SDError ec;
199 +        int     i;
200 +                                        /* in case we fail */
201 +        setcolor(cval, .0, .0, .0);
202 +                                        /* transform source direction */
203 +        if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
204 +                return(0);
205 +                                        /* will discount diffuse portion */
206 +        switch ((vsrc[2] > 0)<<1 | (ndp->vray[2] > 0)) {
207 +        case 3:
208 +                if (ndp->sd->rf == NULL)
209 +                        return(0);      /* all diffuse */
210 +                sv = ndp->sd->rLambFront;
211 +                break;
212 +        case 0:
213 +                if (ndp->sd->rb == NULL)
214 +                        return(0);      /* all diffuse */
215 +                sv = ndp->sd->rLambBack;
216 +                break;
217 +        default:
218 +                if ((ndp->sd->tf == NULL) & (ndp->sd->tb == NULL))
219 +                        return(0);      /* all diffuse */
220 +                sv = ndp->sd->tLamb;
221 +                break;
222 +        }
223 +        if (sv.cieY > FTINY) {
224 +                diffY = sv.cieY *= 1./PI;
225 +                cvt_sdcolor(cdiff, &sv);
226 +        } else {
227 +                diffY = .0;
228 +                setcolor(cdiff, .0, .0, .0);
229 +        }
230 +                                        /* need projected solid angles */
231 +        omega *= fabs(vsrc[2]);
232 +        ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
233 +        if (ec)
234 +                goto baderror;
235 +                                        /* check indirect over-counting */
236 +        if (ndp->pr->crtype & (SPECULAR|AMBIENT)
237 +                                && (vsrc[2] > 0) ^ (ndp->vray[2] > 0)
238 +                                && bright(ndp->cthru) > FTINY) {
239 +                double  dx = vsrc[0] + ndp->vray[0];
240 +                double  dy = vsrc[1] + ndp->vray[1];
241 +                if (dx*dx + dy*dy <= (4./PI)*(omega + tomega +
242 +                                                2.*sqrt(omega*tomega)))
243 +                        return(0);
244 +        }
245 +                                        /* assign number of samples */
246 +        sf = specjitter * ndp->pr->rweight;
247 +        if (tomega <= .0)
248 +                nsamp = 1;
249 +        else if (25.*tomega <= omega)
250 +                nsamp = 100.*sf + .5;
251 +        else
252 +                nsamp = 4.*sf*omega/tomega + .5;
253 +        nsamp += !nsamp;
254 +        sf = sqrt(omega);               /* sample our source area */
255 +        tsr = sqrt(tomega);
256 +        for (i = nsamp; i--; ) {
257 +                VCOPY(vsmp, vsrc);      /* jitter query directions */
258 +                if (nsamp > 1) {
259 +                        multisamp(sd, 2, (i + frandom())/(double)nsamp);
260 +                        vsmp[0] += (sd[0] - .5)*sf;
261 +                        vsmp[1] += (sd[1] - .5)*sf;
262 +                        normalize(vsmp);
263 +                }
264 +                bsdf_jitter(vjit, ndp, tsr);
265 +                                        /* compute BSDF */
266 +                ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
267 +                if (ec)
268 +                        goto baderror;
269 +                if (sv.cieY - diffY <= FTINY)
270 +                        continue;       /* no specular part */
271 +                                        /* check for variable resolution */
272 +                ec = SDsizeBSDF(&tomega2, vjit, vsmp, SDqueryMin, ndp->sd);
273 +                if (ec)
274 +                        goto baderror;
275 +                if (tomega2 < .12*tomega)
276 +                        continue;       /* not safe to include */
277 +                cvt_sdcolor(csmp, &sv);
278 +                addcolor(cval, csmp);   /* else average it in */
279 +                ++ok;
280 +        }
281 +        if (!ok)                        /* no valid specular samples? */
282 +                return(0);
283 +
284 +        sf = 1./(double)ok;             /* compute average BSDF */
285 +        scalecolor(cval, sf);
286 +                                        /* subtract diffuse contribution */
287 +        for (i = 3*(diffY > FTINY); i--; )
288 +                if ((colval(cval,i) -= colval(cdiff,i)) < .0)
289 +                        colval(cval,i) = .0;
290 +        return(1);
291 + baderror:
292 +        objerror(ndp->mp, USER, transSDError(ec));
293 +        return(0);                      /* gratis return */
294 + }
295 +
296   /* Compute source contribution for BSDF (reflected & transmitted) */
297   static void
298   dir_bsdf(
# Line 106 | Line 303 | dir_bsdf(
303   )
304   {
305          BSDFDAT         *np = (BSDFDAT *)nnp;
109        SDError         ec;
110        SDValue         sv;
111        FVECT           vsrc;
112        FVECT           vjit;
306          double          ldot;
307          double          dtmp;
308          COLOR           ctmp;
# Line 120 | Line 313 | dir_bsdf(
313          if ((-FTINY <= ldot) & (ldot <= FTINY))
314                  return;
315  
316 <        if (ldot > .0 && bright(np->rdiff) > FTINY) {
316 >        if (ldot > 0 && bright(np->rdiff) > FTINY) {
317                  /*
318                   *  Compute added diffuse reflected component.
319                   */
# Line 129 | Line 322 | dir_bsdf(
322                  scalecolor(ctmp, dtmp);
323                  addcolor(cval, ctmp);
324          }
325 <        if (ldot < .0 && bright(np->tdiff) > FTINY) {
325 >        if (ldot < 0 && bright(np->tdiff) > FTINY) {
326                  /*
327                   *  Compute added diffuse transmission.
328                   */
# Line 138 | Line 331 | dir_bsdf(
331                  scalecolor(ctmp, dtmp);
332                  addcolor(cval, ctmp);
333          }
334 +        if (ambRayInPmap(np->pr))
335 +                return;         /* specular already in photon map */
336          /*
337 <         *  Compute scattering coefficient using BSDF.
337 >         *  Compute specular scattering coefficient using BSDF.
338           */
339 <        if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone)
339 >        if (!direct_specular_OK(ctmp, ldir, omega, np))
340                  return;
341 <        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 */
341 >        if (ldot < 0) {         /* pattern for specular transmission */
342                  multcolor(ctmp, np->pr->pcol);
343                  dtmp = -ldot * omega;
344 <        }
344 >        } else
345 >                dtmp = ldot * omega;
346          scalecolor(ctmp, dtmp);
347          addcolor(cval, ctmp);
348   }
# Line 180 | Line 357 | dir_brdf(
357   )
358   {
359          BSDFDAT         *np = (BSDFDAT *)nnp;
183        SDError         ec;
184        SDValue         sv;
185        FVECT           vsrc;
186        FVECT           vjit;
360          double          ldot;
361          double          dtmp;
362          COLOR           ctmp, ctmp1, ctmp2;
# Line 204 | Line 377 | dir_brdf(
377                  scalecolor(ctmp, dtmp);
378                  addcolor(cval, ctmp);
379          }
380 +        if (ambRayInPmap(np->pr))
381 +                return;         /* specular already in photon map */
382          /*
383 <         *  Compute reflection coefficient using BSDF.
383 >         *  Compute specular reflection coefficient using BSDF.
384           */
385 <        if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone)
385 >        if (!direct_specular_OK(ctmp, ldir, omega, np))
386                  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 */
387          dtmp = ldot * omega;
388          scalecolor(ctmp, dtmp);
389          addcolor(cval, ctmp);
# Line 241 | Line 399 | dir_btdf(
399   )
400   {
401          BSDFDAT         *np = (BSDFDAT *)nnp;
244        SDError         ec;
245        SDValue         sv;
246        FVECT           vsrc;
247        FVECT           vjit;
402          double          ldot;
403          double          dtmp;
404          COLOR           ctmp;
# Line 265 | Line 419 | dir_btdf(
419                  scalecolor(ctmp, dtmp);
420                  addcolor(cval, ctmp);
421          }
422 +        if (ambRayInPmap(np->pr))
423 +                return;         /* specular already in photon map */
424          /*
425 <         *  Compute scattering coefficient using BSDF.
425 >         *  Compute specular scattering coefficient using BSDF.
426           */
427 <        if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone)
427 >        if (!direct_specular_OK(ctmp, ldir, omega, np))
428                  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);
429                                          /* full pattern on transmission */
430          multcolor(ctmp, np->pr->pcol);
431          dtmp = -ldot * omega;
# Line 290 | Line 438 | static int
438   sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
439   {
440          int     nstarget = 1;
441 <        int     nsent = 0;
441 >        int     nsent;
442          SDError ec;
443          SDValue bsv;
444 <        double  sthick;
445 <        FVECT   vjit, vsmp;
444 >        double  xrand;
445 >        FVECT   vsmp;
446          RAY     sr;
299        int     ntrials;
447                                                  /* multiple samples? */
448          if (specjitter > 1.5) {
449                  nstarget = specjitter*ndp->pr->rweight + .5;
450 <                if (nstarget < 1)
304 <                        nstarget = 1;
450 >                nstarget += !nstarget;
451          }
452 <                                                /* run through our trials */
453 <        for (ntrials = 0; nsent < nstarget && ntrials < 9*nstarget; ntrials++) {
454 <                SDerrorDetail[0] = '\0';
455 <                                                /* sample direction & coef. */
456 <                bsdf_jitter(vjit, ndp);
457 <                ec = SDsampComponent(&bsv, vsmp, vjit, ntrials ? frandom()
458 <                                : urand(ilhash(dimlist,ndims)+samplendx), dcp);
452 >                                                /* run through our samples */
453 >        for (nsent = 0; nsent < nstarget; nsent++) {
454 >                if (nstarget == 1) {            /* stratify random variable */
455 >                        xrand = urand(ilhash(dimlist,ndims)+samplendx);
456 >                        if (specjitter < 1.)
457 >                                xrand = .5 + specjitter*(xrand-.5);
458 >                } else {
459 >                        xrand = (nsent + frandom())/(double)nstarget;
460 >                }
461 >                SDerrorDetail[0] = '\0';        /* sample direction & coef. */
462 >                bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]);
463 >                ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
464                  if (ec)
465                          objerror(ndp->mp, USER, transSDError(ec));
466 <                                                /* zero component? */
316 <                if (bsv.cieY <= FTINY)
466 >                if (bsv.cieY <= FTINY)          /* zero component? */
467                          break;
468                                                  /* map vector to world */
469                  if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
470                          break;
321                                                /* unintentional penetration? */
322                if (DOT(sr.rdir, ndp->pr->ron) > .0 ^ vsmp[2] > .0)
323                        continue;
471                                                  /* spawn a specular ray */
472                  if (nstarget > 1)
473                          bsv.cieY /= (double)nstarget;
474 <                cvt_sdcolor(sr.rcoef, &bsv);    /* use color */
475 <                if (usepat)                     /* pattern on transmission */
474 >                cvt_sdcolor(sr.rcoef, &bsv);    /* use sample color */
475 >                if (usepat)                     /* apply pattern? */
476                          multcolor(sr.rcoef, ndp->pr->pcol);
477                  if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
478 <                        if (maxdepth  > 0)
478 >                        if (maxdepth > 0)
479                                  break;
480 <                        ++nsent;                /* Russian roulette victim */
334 <                        continue;
480 >                        continue;               /* Russian roulette victim */
481                  }
482                                                  /* need to offset origin? */
483 <                if (ndp->thick != .0 && ndp->pr->rod > .0 ^ vsmp[2] > .0)
483 >                if (ndp->thick != 0 && (ndp->pr->rod > 0) ^ (vsmp[2] > 0))
484                          VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
485                  rayvalue(&sr);                  /* send & evaluate sample */
486                  multcolor(sr.rcol, sr.rcoef);
487                  addcolor(ndp->pr->rcol, sr.rcol);
342                ++nsent;
488          }
489          return(nsent);
490   }
# Line 353 | Line 498 | sample_sdf(BSDFDAT *ndp, int sflags)
498          COLORV          *unsc;
499  
500          if (sflags == SDsampSpT) {
501 <                unsc = ndp->tunsamp;
502 <                dfp = ndp->sd->tf;
503 <                cvt_sdcolor(unsc, &ndp->sd->tLamb);
501 >                unsc = ndp->tdiff;
502 >                if (ndp->pr->rod > 0)
503 >                        dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
504 >                else
505 >                        dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
506          } else /* sflags == SDsampSpR */ {
507 <                unsc = ndp->runsamp;
508 <                if (ndp->pr->rod > .0) {
507 >                unsc = ndp->rdiff;
508 >                if (ndp->pr->rod > 0)
509                          dfp = ndp->sd->rf;
510 <                        cvt_sdcolor(unsc, &ndp->sd->rLambFront);
364 <                } else {
510 >                else
511                          dfp = ndp->sd->rb;
366                        cvt_sdcolor(unsc, &ndp->sd->rLambBack);
367                }
512          }
369        multcolor(unsc, ndp->pr->pcol);
513          if (dfp == NULL)                        /* no specular component? */
514                  return(0);
515                                                  /* below sampling threshold? */
# Line 375 | Line 518 | sample_sdf(BSDFDAT *ndp, int sflags)
518                          FVECT   vjit;
519                          double  d;
520                          COLOR   ctmp;
521 <                        bsdf_jitter(vjit, ndp);
521 >                        bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
522                          d = SDdirectHemi(vjit, sflags, ndp->sd);
523                          if (sflags == SDsampSpT) {
524                                  copycolor(ctmp, ndp->pr->pcol);
# Line 401 | Line 544 | sample_sdf(BSDFDAT *ndp, int sflags)
544   int
545   m_bsdf(OBJREC *m, RAY *r)
546   {
547 +        int     hitfront;
548          COLOR   ctmp;
549          SDError ec;
550          FVECT   upvec, vtmp;
# Line 410 | Line 554 | m_bsdf(OBJREC *m, RAY *r)
554          if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) |
555                                  (m->oargs.nfargs % 3))
556                  objerror(m, USER, "bad # arguments");
557 <
557 >                                                /* record surface struck */
558 >        hitfront = (r->rod > 0);
559                                                  /* load cal file */
560          mf = getfunc(m, 5, 0x1d, 1);
561 +        setfunc(m, r);
562                                                  /* get thickness */
563          nd.thick = evalue(mf->ep[0]);
564          if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
565                  nd.thick = .0;
566 <                                                /* check shadow */
567 <        if (r->crtype & SHADOW) {
568 <                if (nd.thick != .0)
569 <                        raytrans(r);            /* pass-through */
424 <                return(1);                      /* or shadow */
566 >                                                /* check backface visibility */
567 >        if (!hitfront & !backvis) {
568 >                raytrans(r);
569 >                return(1);
570          }
571                                                  /* check other rays to pass */
572 <        if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) ||
573 <                                nd.thick > .0 ^ r->rod > .0)) {
572 >        if (nd.thick != 0 && (r->crtype & SHADOW ||
573 >                                !(r->crtype & (SPECULAR|AMBIENT)) ||
574 >                                (nd.thick > 0) ^ hitfront)) {
575                  raytrans(r);                    /* hide our proxy */
576                  return(1);
577          }
578 +        nd.mp = m;
579 +        nd.pr = r;
580                                                  /* get BSDF data */
581          nd.sd = loadBSDF(m->oargs.sarg[1]);
582 +                                                /* early shadow check */
583 +        if (r->crtype & SHADOW && (nd.sd->tf == NULL) & (nd.sd->tb == NULL))
584 +                return(1);
585                                                  /* diffuse reflectance */
586 <        if (r->rod > .0) {
587 <                if (m->oargs.nfargs < 3)
588 <                        setcolor(nd.rdiff, .0, .0, .0);
589 <                else
439 <                        setcolor(nd.rdiff, m->oargs.farg[0],
586 >        if (hitfront) {
587 >                cvt_sdcolor(nd.rdiff, &nd.sd->rLambFront);
588 >                if (m->oargs.nfargs >= 3) {
589 >                        setcolor(ctmp, m->oargs.farg[0],
590                                          m->oargs.farg[1],
591                                          m->oargs.farg[2]);
592 +                        addcolor(nd.rdiff, ctmp);
593 +                }
594          } else {
595 <                if (m->oargs.nfargs < 6) {      /* check invisible backside */
596 <                        if (!backvis && (nd.sd->rb == NULL) &
597 <                                                (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],
595 >                cvt_sdcolor(nd.rdiff, &nd.sd->rLambBack);
596 >                if (m->oargs.nfargs >= 6) {
597 >                        setcolor(ctmp, m->oargs.farg[3],
598                                          m->oargs.farg[4],
599                                          m->oargs.farg[5]);
600 +                        addcolor(nd.rdiff, ctmp);
601 +                }
602          }
603                                                  /* diffuse transmittance */
604 <        if (m->oargs.nfargs < 9)
605 <                setcolor(nd.tdiff, .0, .0, .0);
606 <        else
460 <                setcolor(nd.tdiff, m->oargs.farg[6],
604 >        cvt_sdcolor(nd.tdiff, &nd.sd->tLamb);
605 >        if (m->oargs.nfargs >= 9) {
606 >                setcolor(ctmp, m->oargs.farg[6],
607                                  m->oargs.farg[7],
608                                  m->oargs.farg[8]);
609 <        nd.mp = m;
610 <        nd.pr = r;
609 >                addcolor(nd.tdiff, ctmp);
610 >        }
611                                                  /* get modifiers */
612          raytexture(r, m->omod);
613                                                  /* modify diffuse values */
# Line 472 | Line 618 | m_bsdf(OBJREC *m, RAY *r)
618          upvec[1] = evalue(mf->ep[2]);
619          upvec[2] = evalue(mf->ep[3]);
620                                                  /* return to world coords */
621 <        if (mf->f != &unitxf) {
622 <                multv3(upvec, upvec, mf->f->xfm);
623 <                nd.thick *= mf->f->sca;
621 >        if (mf->fxp != &unitxf) {
622 >                multv3(upvec, upvec, mf->fxp->xfm);
623 >                nd.thick *= mf->fxp->sca;
624          }
625 +        if (r->rox != NULL) {
626 +                multv3(upvec, upvec, r->rox->f.xfm);
627 +                nd.thick *= r->rox->f.sca;
628 +        }
629          raynormal(nd.pnorm, r);
630                                                  /* compute local BSDF xform */
631          ec = SDcompXform(nd.toloc, nd.pnorm, upvec);
# Line 485 | Line 635 | m_bsdf(OBJREC *m, RAY *r)
635                  nd.vray[2] = -r->rdir[2];
636                  ec = SDmapDir(nd.vray, nd.toloc, nd.vray);
637          }
638 <        if (!ec)
639 <                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);
638 >        if (ec) {
639 >                objerror(m, WARNING, "Illegal orientation vector");
640                  return(1);
641          }
642 <        if (r->rod < .0) {                      /* perturb normal towards hit */
642 >        compute_through(&nd);                   /* compute through component */
643 >        if (r->crtype & SHADOW) {
644 >                RAY     tr;                     /* attempt to pass shadow ray */
645 >                if (rayorigin(&tr, TRANS, r, nd.cthru) < 0)
646 >                        return(1);              /* blocked */
647 >                VCOPY(tr.rdir, r->rdir);
648 >                rayvalue(&tr);                  /* transmit with scaling */
649 >                multcolor(tr.rcol, tr.rcoef);
650 >                copycolor(r->rcol, tr.rcol);
651 >                return(1);                      /* we're done */
652 >        }
653 >        ec = SDinvXform(nd.fromloc, nd.toloc);
654 >        if (!ec)                                /* determine BSDF resolution */
655 >                ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL,
656 >                                        SDqueryMin+SDqueryMax, nd.sd);
657 >        if (ec)
658 >                objerror(m, USER, transSDError(ec));
659 >
660 >        nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]);
661 >        nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]);
662 >        if (!hitfront) {                        /* perturb normal towards hit */
663                  nd.pnorm[0] = -nd.pnorm[0];
664                  nd.pnorm[1] = -nd.pnorm[1];
665                  nd.pnorm[2] = -nd.pnorm[2];
# Line 507 | Line 669 | m_bsdf(OBJREC *m, RAY *r)
669                                                  /* sample transmission */
670          sample_sdf(&nd, SDsampSpT);
671                                                  /* compute indirect diffuse */
672 <        copycolor(ctmp, nd.rdiff);
673 <        addcolor(ctmp, nd.runsamp);
512 <        if (bright(ctmp) > FTINY) {             /* ambient from reflection */
513 <                if (r->rod < .0)
672 >        if (bright(nd.rdiff) > FTINY) {         /* ambient from reflection */
673 >                if (!hitfront)
674                          flipsurface(r);
675 +                copycolor(ctmp, nd.rdiff);
676                  multambient(ctmp, r, nd.pnorm);
677                  addcolor(r->rcol, ctmp);
678 <                if (r->rod < .0)
678 >                if (!hitfront)
679                          flipsurface(r);
680          }
681 <        copycolor(ctmp, nd.tdiff);
521 <        addcolor(ctmp, nd.tunsamp);
522 <        if (bright(ctmp) > FTINY) {             /* ambient from other side */
681 >        if (bright(nd.tdiff) > FTINY) {         /* ambient from other side */
682                  FVECT  bnorm;
683 <                if (r->rod > .0)
683 >                if (hitfront)
684                          flipsurface(r);
685                  bnorm[0] = -nd.pnorm[0];
686                  bnorm[1] = -nd.pnorm[1];
687                  bnorm[2] = -nd.pnorm[2];
688 <                if (nd.thick != .0) {           /* proxy with offset? */
688 >                copycolor(ctmp, nd.tdiff);
689 >                if (nd.thick != 0) {            /* proxy with offset? */
690                          VCOPY(vtmp, r->rop);
691 <                        VSUM(r->rop, vtmp, r->ron, -nd.thick);
691 >                        VSUM(r->rop, vtmp, r->ron, nd.thick);
692                          multambient(ctmp, r, bnorm);
693                          VCOPY(r->rop, vtmp);
694                  } else
695                          multambient(ctmp, r, bnorm);
696                  addcolor(r->rcol, ctmp);
697 <                if (r->rod > .0)
697 >                if (hitfront)
698                          flipsurface(r);
699          }
700                                                  /* add direct component */
701 <        if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL)) {
701 >        if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) &
702 >                                        (nd.sd->tb == NULL)) {
703                  direct(r, dir_brdf, &nd);       /* reflection only */
704 <        } else if (nd.thick == .0) {
704 >        } else if (nd.thick == 0) {
705                  direct(r, dir_bsdf, &nd);       /* thin surface scattering */
706          } else {
707                  direct(r, dir_brdf, &nd);       /* reflection first */
708                  VCOPY(vtmp, r->rop);            /* offset for transmitted */
709                  VSUM(r->rop, vtmp, r->ron, -nd.thick);
710 <                direct(r, dir_btdf, &nd);
710 >                direct(r, dir_btdf, &nd);       /* separate transmission */
711                  VCOPY(r->rop, vtmp);
712          }
713                                                  /* clean up */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines