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.3 by greg, Sat Feb 19 01:48:59 2011 UTC vs.
Revision 2.30 by greg, Wed Sep 2 18:59:01 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines