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.13 by greg, Sun Aug 21 21:24:30 2011 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"
# Line 18 | Line 17 | static const char RCSid[] = "$Id$";
17   /*
18   *      Arguments to this material include optional diffuse colors.
19   *  String arguments include the BSDF and function files.
20 < *      A thickness variable causes the strange but useful behavior
21 < *  of translating transmitted rays this distance past the surface
22 < *  intersection in the normal direction to bypass intervening geometry.
23 < *  This only affects scattered, non-source directed samples.  Thus,
24 < *  thickness is relevant only if there is a transmitted component.
26 < *  A positive thickness has the further side-effect that an unscattered
20 > *      A non-zero thickness causes the strange but useful behavior
21 > *  of translating transmitted rays this distance beneath the surface
22 > *  (opposite the surface normal) to bypass any intervening geometry.
23 > *  Translation only affects scattered, non-source-directed samples.
24 > *  A non-zero thickness has the further side-effect that an unscattered
25   *  (view) ray will pass right through our material if it has any
26 < *  non-diffuse transmission, making our BSDF invisible.  This allows the
27 < *  underlying geometry to become visible.  A matching surface should be
28 < *  placed on the other side, less than the thickness away, if the backside
29 < *  reflectance is non-zero.
26 > *  non-diffuse transmission, making the BSDF surface invisible.  This
27 > *  shows the proxied geometry instead. Thickness has the further
28 > *  effect of turning off reflection on the hidden side so that rays
29 > *  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   *      The "up" vector for the BSDF is given by three variables, defined
39   *  (along with the thickness) by the named function file, or '.' if none.
40   *  Together with the surface normal, this defines the local coordinate
41   *  system for the BSDF.
42   *      We do not reorient the surface, so if the BSDF has no back-side
43 < *  reflectance and none is given in the real arguments, the surface will
44 < *  appear as black when viewed from behind (unless backvis is false).
45 < *  The diffuse compnent arguments are added to components in the BSDF file,
43 > *  reflectance and none is given in the real arguments, a BSDF surface
44 > *  with zero thickness will appear black when viewed from behind
45 > *  unless backface visibility is off.
46 > *      The diffuse arguments are added to components in the BSDF file,
47   *  not multiplied.  However, patterns affect this material as a multiplier
48   *  on everything except non-diffuse reflection.
49   *
50   *  Arguments for MAT_BSDF are:
51   *      6+      thick   BSDFfile        ux uy uz        funcfile        transform
52   *      0
53 < *      0|3|9   rdf     gdf     bdf
53 > *      0|3|6|9 rdf     gdf     bdf
54   *              rdb     gdb     bdb
55   *              rdt     gdt     bdt
56   */
57  
58 + /*
59 + * Note that our reverse ray-tracing process means that the positions
60 + * of incoming and outgoing vectors may be reversed in our calls
61 + * to the BSDF library.  This is fine, since the bidirectional nature
62 + * of the BSDF (that's what the 'B' stands for) means it all works out.
63 + */
64 +
65   typedef struct {
66          OBJREC  *mp;            /* material pointer */
67          RAY     *pr;            /* intersected ray */
68          FVECT   pnorm;          /* perturbed surface normal */
69 <        FVECT   vinc;           /* local incident vector */
69 >        FVECT   vray;           /* local outgoing (return) vector */
70 >        double  sr_vpsa[2];     /* sqrt of BSDF projected solid angle extrema */
71          RREAL   toloc[3][3];    /* world to local BSDF coords */
72          RREAL   fromloc[3][3];  /* local BSDF coords to world */
73          double  thick;          /* surface thickness */
# Line 65 | Line 80 | typedef struct {
80  
81   #define cvt_sdcolor(cv, svp)    ccy2rgb(&(svp)->spec, (svp)->cieY, cv)
82  
83 < /* Convert error from BSDF library */
84 < static char *
85 < cvt_sderr(SDError ec)
83 > /* Jitter ray sample according to projected solid angle and specjitter */
84 > static void
85 > bsdf_jitter(FVECT vres, BSDFDAT *ndp, int domax)
86   {
87 <        if (!SDerrorDetail[0])
88 <                return(strcpy(errmsg, SDerrorEnglish[ec]));
89 <        sprintf(errmsg, "%s: %s", SDerrorEnglish[ec], SDerrorDetail);
90 <        return(errmsg);
87 >        double  sr_psa = ndp->sr_vpsa[domax];
88 >
89 >        VCOPY(vres, ndp->vray);
90 >        if (specjitter < 1.)
91 >                sr_psa *= specjitter;
92 >        if (sr_psa <= FTINY)
93 >                return;
94 >        vres[0] += sr_psa*(.5 - frandom());
95 >        vres[1] += sr_psa*(.5 - frandom());
96 >        normalize(vres);
97   }
98  
99 < /* Compute source contribution for BSDF */
99 > /* Evaluate BSDF for direct component, returning true if OK to proceed */
100 > static int
101 > direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
102 > {
103 >        int     nsamp, ok = 0;
104 >        FVECT   vsrc, vsmp, vjit;
105 >        double  tomega;
106 >        double  sf, sd[2];
107 >        COLOR   csmp;
108 >        SDValue sv;
109 >        SDError ec;
110 >        int     i;
111 >                                        /* transform source direction */
112 >        if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
113 >                return(0);
114 >                                        /* check indirect over-counting */
115 >        if (ndp->thick != 0 && ndp->pr->crtype & (SPECULAR|AMBIENT)
116 >                                && vsrc[2] > 0 ^ ndp->vray[2] > 0) {
117 >                double  dx = vsrc[0] + ndp->vray[0];
118 >                double  dy = vsrc[1] + ndp->vray[1];
119 >                if (dx*dx + dy*dy <= omega*(1./PI))
120 >                        return(0);
121 >        }
122 >                                        /* get local BSDF resolution */
123 >        ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
124 >        if (ec)
125 >                goto baderror;
126 >                                        /* assign number of samples */
127 >        if (tomega <= omega*.02)
128 >                nsamp = 50;
129 >        else
130 >                nsamp = 2.*omega/tomega + 1.;
131 >        sf = sqrt(omega);
132 >        setcolor(cval, .0, .0, .0);     /* sample our source area */
133 >        for (i = nsamp; i--; ) {
134 >                VCOPY(vsmp, vsrc);      /* jitter query directions */
135 >                if (nsamp > 1) {
136 >                        multisamp(sd, 2, (i + frandom())/(double)nsamp);
137 >                        vsmp[0] += (sd[0] - .5)*sf;
138 >                        vsmp[1] += (sd[1] - .5)*sf;
139 >                        if (normalize(vsmp) == 0) {
140 >                                --nsamp;
141 >                                continue;
142 >                        }
143 >                }
144 >                bsdf_jitter(vjit, ndp, 0);
145 >                                        /* compute BSDF */
146 >                ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
147 >                if (ec)
148 >                        goto baderror;
149 >                if (sv.cieY <= FTINY)   /* worth using? */
150 >                        continue;
151 >                cvt_sdcolor(csmp, &sv);
152 >                addcolor(cval, csmp);   /* average it in */
153 >                ++ok;
154 >        }
155 >        sf = 1./(double)nsamp;
156 >        scalecolor(cval, sf);
157 >        return(ok);
158 > baderror:
159 >        objerror(ndp->mp, USER, transSDError(ec));
160 > }
161 >
162 > /* Compute source contribution for BSDF (reflected & transmitted) */
163   static void
164 < dirbsdf(
164 > dir_bsdf(
165          COLOR  cval,                    /* returned coefficient */
166          void  *nnp,                     /* material data */
167          FVECT  ldir,                    /* light source direction */
168          double  omega                   /* light source size */
169   )
170   {
171 <        BSDFDAT         *np = nnp;
88 <        SDError         ec;
89 <        SDValue         sv;
90 <        FVECT           vout;
171 >        BSDFDAT         *np = (BSDFDAT *)nnp;
172          double          ldot;
173          double          dtmp;
174          COLOR           ctmp;
# Line 98 | Line 179 | dirbsdf(
179          if ((-FTINY <= ldot) & (ldot <= FTINY))
180                  return;
181  
182 <        if (ldot > .0 && bright(np->rdiff) > FTINY) {
182 >        if (ldot > 0 && bright(np->rdiff) > FTINY) {
183                  /*
184                   *  Compute added diffuse reflected component.
185                   */
# Line 107 | Line 188 | dirbsdf(
188                  scalecolor(ctmp, dtmp);
189                  addcolor(cval, ctmp);
190          }
191 <        if (ldot < .0 && bright(np->tdiff) > FTINY) {
191 >        if (ldot < 0 && bright(np->tdiff) > FTINY) {
192                  /*
193                   *  Compute added diffuse transmission.
194                   */
# Line 119 | Line 200 | dirbsdf(
200          /*
201           *  Compute scattering coefficient using BSDF.
202           */
203 <        if (SDmapDir(vout, np->toloc, ldir) != SDEnone)
203 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
204                  return;
205 <        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 */
205 >        if (ldot > 0) {         /* pattern only diffuse reflection */
206                  COLOR   ctmp1, ctmp2;
207 <                dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY
207 >                dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
208                                          : np->sd->rLambBack.cieY;
209 <                dtmp /= PI * sv.cieY;   /* diffuse fraction */
209 >                                        /* diffuse fraction */
210 >                dtmp /= PI * bright(ctmp);
211                  copycolor(ctmp2, np->pr->pcol);
212                  scalecolor(ctmp2, dtmp);
213                  setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
214                  addcolor(ctmp1, ctmp2);
215 <                multcolor(ctmp, ctmp1); /* apply desaturated pattern */
215 >                multcolor(ctmp, ctmp1); /* apply derated pattern */
216                  dtmp = ldot * omega;
217          } else {                        /* full pattern on transmission */
218                  multcolor(ctmp, np->pr->pcol);
# Line 147 | Line 222 | dirbsdf(
222          addcolor(cval, ctmp);
223   }
224  
225 + /* Compute source contribution for BSDF (reflected only) */
226 + static void
227 + dir_brdf(
228 +        COLOR  cval,                    /* returned coefficient */
229 +        void  *nnp,                     /* material data */
230 +        FVECT  ldir,                    /* light source direction */
231 +        double  omega                   /* light source size */
232 + )
233 + {
234 +        BSDFDAT         *np = (BSDFDAT *)nnp;
235 +        double          ldot;
236 +        double          dtmp;
237 +        COLOR           ctmp, ctmp1, ctmp2;
238 +
239 +        setcolor(cval, .0, .0, .0);
240 +
241 +        ldot = DOT(np->pnorm, ldir);
242 +        
243 +        if (ldot <= FTINY)
244 +                return;
245 +
246 +        if (bright(np->rdiff) > FTINY) {
247 +                /*
248 +                 *  Compute added diffuse reflected component.
249 +                 */
250 +                copycolor(ctmp, np->rdiff);
251 +                dtmp = ldot * omega * (1./PI);
252 +                scalecolor(ctmp, dtmp);
253 +                addcolor(cval, ctmp);
254 +        }
255 +        /*
256 +         *  Compute reflection coefficient using BSDF.
257 +         */
258 +        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
259 +                return;
260 +                                        /* pattern only diffuse reflection */
261 +        dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
262 +                                : np->sd->rLambBack.cieY;
263 +        dtmp /= PI * bright(ctmp);      /* diffuse fraction */
264 +        copycolor(ctmp2, np->pr->pcol);
265 +        scalecolor(ctmp2, dtmp);
266 +        setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
267 +        addcolor(ctmp1, ctmp2);
268 +        multcolor(ctmp, ctmp1);         /* apply derated pattern */
269 +        dtmp = ldot * omega;
270 +        scalecolor(ctmp, dtmp);
271 +        addcolor(cval, ctmp);
272 + }
273 +
274 + /* Compute source contribution for BSDF (transmitted only) */
275 + static void
276 + dir_btdf(
277 +        COLOR  cval,                    /* returned coefficient */
278 +        void  *nnp,                     /* material data */
279 +        FVECT  ldir,                    /* light source direction */
280 +        double  omega                   /* light source size */
281 + )
282 + {
283 +        BSDFDAT         *np = (BSDFDAT *)nnp;
284 +        double          ldot;
285 +        double          dtmp;
286 +        COLOR           ctmp;
287 +
288 +        setcolor(cval, .0, .0, .0);
289 +
290 +        ldot = DOT(np->pnorm, ldir);
291 +
292 +        if (ldot >= -FTINY)
293 +                return;
294 +
295 +        if (bright(np->tdiff) > FTINY) {
296 +                /*
297 +                 *  Compute added diffuse transmission.
298 +                 */
299 +                copycolor(ctmp, np->tdiff);
300 +                dtmp = -ldot * omega * (1.0/PI);
301 +                scalecolor(ctmp, dtmp);
302 +                addcolor(cval, ctmp);
303 +        }
304 +        /*
305 +         *  Compute scattering coefficient using BSDF.
306 +         */
307 +        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
308 +                return;
309 +                                        /* full pattern on transmission */
310 +        multcolor(ctmp, np->pr->pcol);
311 +        dtmp = -ldot * omega;
312 +        scalecolor(ctmp, dtmp);
313 +        addcolor(cval, ctmp);
314 + }
315 +
316   /* Sample separate BSDF component */
317   static int
318   sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
319   {
320          int     nstarget = 1;
321 <        int     nsent = 0;
321 >        int     nsent;
322          SDError ec;
323          SDValue bsv;
324 <        double  sthick;
325 <        FVECT   vout;
324 >        double  xrand;
325 >        FVECT   vsmp;
326          RAY     sr;
161        int     ntrials;
327                                                  /* multiple samples? */
328          if (specjitter > 1.5) {
329                  nstarget = specjitter*ndp->pr->rweight + .5;
330                  if (nstarget < 1)
331                          nstarget = 1;
332          }
333 <                                                /* run through our trials */
334 <        for (ntrials = 0; nsent < nstarget && ntrials < 9*nstarget; ntrials++) {
335 <                SDerrorDetail[0] = '\0';
336 <                                                /* sample direction & coef. */
337 <                ec = SDsampComponent(&bsv, vout, ndp->vinc,
338 <                                ntrials ? frandom()
339 <                                        : urand(ilhash(dimlist,ndims)+samplendx),
340 <                                                dcp);
333 >                                                /* run through our samples */
334 >        for (nsent = 0; nsent < nstarget; nsent++) {
335 >                if (nstarget == 1)              /* stratify random variable */
336 >                        xrand = urand(ilhash(dimlist,ndims)+samplendx);
337 >                else
338 >                        xrand = (nsent + frandom())/(double)nstarget;
339 >                SDerrorDetail[0] = '\0';        /* sample direction & coef. */
340 >                bsdf_jitter(vsmp, ndp, 0);
341 >                ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
342                  if (ec)
343 <                        objerror(ndp->mp, USER, cvt_sderr(ec));
344 <                                                /* zero component? */
179 <                if (bsv.cieY <= FTINY)
343 >                        objerror(ndp->mp, USER, transSDError(ec));
344 >                if (bsv.cieY <= FTINY)          /* zero component? */
345                          break;
346                                                  /* map vector to world */
347 <                if (SDmapDir(sr.rdir, ndp->fromloc, vout) != SDEnone)
347 >                if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
348                          break;
184                                                /* unintentional penetration? */
185                if (DOT(sr.rdir, ndp->pr->ron) > .0 ^ vout[2] > .0)
186                        continue;
349                                                  /* spawn a specular ray */
350                  if (nstarget > 1)
351                          bsv.cieY /= (double)nstarget;
352 <                cvt_sdcolor(sr.rcoef, &bsv);    /* use color */
353 <                if (usepat)                     /* pattern on transmission */
352 >                cvt_sdcolor(sr.rcoef, &bsv);    /* use sample color */
353 >                if (usepat)                     /* apply pattern? */
354                          multcolor(sr.rcoef, ndp->pr->pcol);
355                  if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
356 <                        if (maxdepth  > 0)
356 >                        if (maxdepth > 0)
357                                  break;
358 <                        ++nsent;                /* Russian roulette victim */
197 <                        continue;
358 >                        continue;               /* Russian roulette victim */
359                  }
360 <                                                /* need to move origin? */
361 <                sthick = (ndp->pr->rod > .0) ? -ndp->thick : ndp->thick;
362 <                if (sthick < .0 ^ vout[2] > .0)
202 <                        VSUM(sr.rorg, sr.rorg, ndp->pr->ron, sthick);
203 <
360 >                                                /* need to offset origin? */
361 >                if (ndp->thick != 0 && ndp->pr->rod > 0 ^ vsmp[2] > 0)
362 >                        VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
363                  rayvalue(&sr);                  /* send & evaluate sample */
364                  multcolor(sr.rcol, sr.rcoef);
365                  addcolor(ndp->pr->rcol, sr.rcol);
207                ++nsent;
366          }
367          return(nsent);
368   }
# Line 223 | Line 381 | sample_sdf(BSDFDAT *ndp, int sflags)
381                  cvt_sdcolor(unsc, &ndp->sd->tLamb);
382          } else /* sflags == SDsampSpR */ {
383                  unsc = ndp->runsamp;
384 <                if (ndp->pr->rod > .0) {
384 >                if (ndp->pr->rod > 0) {
385                          dfp = ndp->sd->rf;
386                          cvt_sdcolor(unsc, &ndp->sd->rLambFront);
387                  } else {
# Line 236 | Line 394 | sample_sdf(BSDFDAT *ndp, int sflags)
394                  return(0);
395                                                  /* below sampling threshold? */
396          if (dfp->maxHemi <= specthresh+FTINY) {
397 <                if (dfp->maxHemi > FTINY) {     /* XXX no color from BSDF! */
398 <                        double  d = SDdirectHemi(ndp->vinc, sflags, ndp->sd);
397 >                if (dfp->maxHemi > FTINY) {     /* XXX no color from BSDF */
398 >                        FVECT   vjit;
399 >                        double  d;
400                          COLOR   ctmp;
401 +                        bsdf_jitter(vjit, ndp, 1);
402 +                        d = SDdirectHemi(vjit, sflags, ndp->sd);
403                          if (sflags == SDsampSpT) {
404                                  copycolor(ctmp, ndp->pr->pcol);
405                                  scalecolor(ctmp, d);
# Line 263 | Line 424 | sample_sdf(BSDFDAT *ndp, int sflags)
424   int
425   m_bsdf(OBJREC *m, RAY *r)
426   {
427 +        int     hitfront;
428          COLOR   ctmp;
429          SDError ec;
430 <        FVECT   upvec, outVec;
430 >        FVECT   upvec, vtmp;
431          MFUNC   *mf;
432          BSDFDAT nd;
433                                                  /* check arguments */
434          if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) |
435                                  (m->oargs.nfargs % 3))
436                  objerror(m, USER, "bad # arguments");
437 <
438 <        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 <        }
437 >                                                /* record surface struck */
438 >        hitfront = (r->rod > 0);
439                                                  /* load cal file */
440          mf = getfunc(m, 5, 0x1d, 1);
441                                                  /* get thickness */
442          nd.thick = evalue(mf->ep[0]);
443 <        if (nd.thick < .0)
443 >        if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
444                  nd.thick = .0;
445                                                  /* check shadow */
446          if (r->crtype & SHADOW) {
447 <                SDfreeCache(nd.sd);
301 <                if (nd.thick > FTINY && nd.sd->tf != NULL &&
302 <                                nd.sd->tf->maxHemi > FTINY)
447 >                if (nd.thick != 0)
448                          raytrans(r);            /* pass-through */
449 <                return(1);                      /* else shadow */
449 >                return(1);                      /* or shadow */
450          }
451 <                                                /* check unscattered ray */
452 <        if (!(r->crtype & (SPECULAR|AMBIENT)) && nd.thick > FTINY &&
453 <                        nd.sd->tf != NULL && nd.sd->tf->maxHemi > FTINY) {
454 <                SDfreeCache(nd.sd);
310 <                raytrans(r);                    /* pass-through */
451 >                                                /* check other rays to pass */
452 >        if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) ||
453 >                                nd.thick > 0 ^ hitfront)) {
454 >                raytrans(r);                    /* hide our proxy */
455                  return(1);
456          }
457 +                                                /* get BSDF data */
458 +        nd.sd = loadBSDF(m->oargs.sarg[1]);
459                                                  /* diffuse reflectance */
460 <        if (r->rod > .0) {
460 >        if (hitfront) {
461                  if (m->oargs.nfargs < 3)
462                          setcolor(nd.rdiff, .0, .0, .0);
463                  else
# Line 320 | Line 466 | m_bsdf(OBJREC *m, RAY *r)
466                                          m->oargs.farg[2]);
467          } else {
468                  if (m->oargs.nfargs < 6) {      /* check invisible backside */
469 <                        if (!backvis && (nd.sd->rb == NULL ||
470 <                                                nd.sd->rb->maxHemi <= FTINY) &&
325 <                                        (nd.sd->tf == NULL ||
326 <                                                nd.sd->tf->maxHemi <= FTINY)) {
469 >                        if (!backvis && (nd.sd->rb == NULL) &
470 >                                                (nd.sd->tf == NULL)) {
471                                  SDfreeCache(nd.sd);
472                                  raytrans(r);
473                                  return(1);
# Line 345 | Line 489 | m_bsdf(OBJREC *m, RAY *r)
489          nd.pr = r;
490                                                  /* get modifiers */
491          raytexture(r, m->omod);
348        if (bright(r->pcol) <= FTINY) {         /* black pattern?! */
349                SDfreeCache(nd.sd);
350                return(1);
351        }
492                                                  /* modify diffuse values */
493          multcolor(nd.rdiff, r->pcol);
494          multcolor(nd.tdiff, r->pcol);
# Line 365 | Line 505 | m_bsdf(OBJREC *m, RAY *r)
505                                                  /* compute local BSDF xform */
506          ec = SDcompXform(nd.toloc, nd.pnorm, upvec);
507          if (!ec) {
508 <                nd.vinc[0] = -r->rdir[0];
509 <                nd.vinc[1] = -r->rdir[1];
510 <                nd.vinc[2] = -r->rdir[2];
511 <                ec = SDmapDir(nd.vinc, nd.toloc, nd.vinc);
508 >                nd.vray[0] = -r->rdir[0];
509 >                nd.vray[1] = -r->rdir[1];
510 >                nd.vray[2] = -r->rdir[2];
511 >                ec = SDmapDir(nd.vray, nd.toloc, nd.vray);
512          }
513          if (!ec)
514                  ec = SDinvXform(nd.fromloc, nd.toloc);
515 +                                                /* determine BSDF resolution */
516 +        if (!ec)
517 +                ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL,
518 +                                                SDqueryMin+SDqueryMax, nd.sd);
519          if (ec) {
520 <                objerror(m, WARNING, cvt_sderr(ec));
520 >                objerror(m, WARNING, transSDError(ec));
521                  SDfreeCache(nd.sd);
522                  return(1);
523          }
524 <        if (r->rod < .0) {                      /* perturb normal towards hit */
524 >        nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]);
525 >        nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]);
526 >        if (!hitfront) {                        /* perturb normal towards hit */
527                  nd.pnorm[0] = -nd.pnorm[0];
528                  nd.pnorm[1] = -nd.pnorm[1];
529                  nd.pnorm[2] = -nd.pnorm[2];
# Line 389 | Line 535 | m_bsdf(OBJREC *m, RAY *r)
535                                                  /* compute indirect diffuse */
536          copycolor(ctmp, nd.rdiff);
537          addcolor(ctmp, nd.runsamp);
538 <        if (bright(ctmp) > FTINY) {             /* ambient from this side */
539 <                if (r->rod < .0)
538 >        if (bright(ctmp) > FTINY) {             /* ambient from reflection */
539 >                if (!hitfront)
540                          flipsurface(r);
541                  multambient(ctmp, r, nd.pnorm);
542                  addcolor(r->rcol, ctmp);
543 <                if (r->rod < .0)
543 >                if (!hitfront)
544                          flipsurface(r);
545          }
546          copycolor(ctmp, nd.tdiff);
547          addcolor(ctmp, nd.tunsamp);
548          if (bright(ctmp) > FTINY) {             /* ambient from other side */
549                  FVECT  bnorm;
550 <                if (r->rod > .0)
550 >                if (hitfront)
551                          flipsurface(r);
552                  bnorm[0] = -nd.pnorm[0];
553                  bnorm[1] = -nd.pnorm[1];
554                  bnorm[2] = -nd.pnorm[2];
555 <                multambient(ctmp, r, bnorm);
555 >                if (nd.thick != 0) {            /* proxy with offset? */
556 >                        VCOPY(vtmp, r->rop);
557 >                        VSUM(r->rop, vtmp, r->ron, -nd.thick);
558 >                        multambient(ctmp, r, bnorm);
559 >                        VCOPY(r->rop, vtmp);
560 >                } else
561 >                        multambient(ctmp, r, bnorm);
562                  addcolor(r->rcol, ctmp);
563 <                if (r->rod > .0)
563 >                if (hitfront)
564                          flipsurface(r);
565          }
566                                                  /* add direct component */
567 <        direct(r, dirbsdf, &nd);
567 >        if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL)) {
568 >                direct(r, dir_brdf, &nd);       /* reflection only */
569 >        } else if (nd.thick == 0) {
570 >                direct(r, dir_bsdf, &nd);       /* thin surface scattering */
571 >        } else {
572 >                direct(r, dir_brdf, &nd);       /* reflection first */
573 >                VCOPY(vtmp, r->rop);            /* offset for transmitted */
574 >                VSUM(r->rop, vtmp, r->ron, -nd.thick);
575 >                direct(r, dir_btdf, &nd);       /* separate transmission */
576 >                VCOPY(r->rop, vtmp);
577 >        }
578                                                  /* clean up */
579          SDfreeCache(nd.sd);
580          return(1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines