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.1 by greg, Fri Feb 18 00:40:25 2011 UTC vs.
Revision 2.43 by greg, Fri Dec 1 02:45:14 2017 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines