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.4 by greg, Sat Feb 19 23:42:09 2011 UTC vs.
Revision 2.22 by greg, Sun Sep 2 15:33:15 2012 UTC

# Line 17 | 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.
25 < *  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   */
# Line 59 | Line 67 | typedef struct {
67          RAY     *pr;            /* intersected ray */
68          FVECT   pnorm;          /* perturbed surface normal */
69          FVECT   vray;           /* local outgoing (return) vector */
70 <        double  sr_vpsa;        /* sqrt of BSDF projected solid angle */
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 74 | Line 82 | typedef struct {
82  
83   /* Jitter ray sample according to projected solid angle and specjitter */
84   static void
85 < bsdf_jitter(FVECT vres, BSDFDAT *ndp)
85 > bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
86   {
79        double  sr_psa = ndp->sr_vpsa;
80
87          VCOPY(vres, ndp->vray);
88          if (specjitter < 1.)
89                  sr_psa *= specjitter;
# Line 88 | Line 94 | bsdf_jitter(FVECT vres, BSDFDAT *ndp)
94          normalize(vres);
95   }
96  
97 < /* Compute source contribution for BSDF */
97 > /* Evaluate BSDF for direct component, returning true if OK to proceed */
98 > static int
99 > direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
100 > {
101 >        int     nsamp, ok = 0;
102 >        FVECT   vsrc, vsmp, vjit;
103 >        double  tomega;
104 >        double  sf, tsr, sd[2];
105 >        COLOR   csmp;
106 >        SDValue sv;
107 >        SDError ec;
108 >        int     i;
109 >                                        /* transform source direction */
110 >        if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
111 >                return(0);
112 >                                        /* assign number of samples */
113 >        ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
114 >        if (ec)
115 >                goto baderror;
116 >                                        /* check indirect over-counting */
117 >        if (ndp->thick != 0 && ndp->pr->crtype & (SPECULAR|AMBIENT)
118 >                                && vsrc[2] > 0 ^ ndp->vray[2] > 0) {
119 >                double  dx = vsrc[0] + ndp->vray[0];
120 >                double  dy = vsrc[1] + ndp->vray[1];
121 >                if (dx*dx + dy*dy <= omega+tomega)
122 >                        return(0);
123 >        }
124 >        sf = specjitter * ndp->pr->rweight;
125 >        if (25.*tomega <= omega)
126 >                nsamp = 100.*sf + .5;
127 >        else
128 >                nsamp = 4.*sf*omega/tomega + .5;
129 >        nsamp += !nsamp;
130 >        setcolor(cval, .0, .0, .0);     /* sample our source area */
131 >        sf = sqrt(omega);
132 >        tsr = sqrt(tomega);
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, tsr);
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 >        return(0);                      /* gratis return */
161 > }
162 >
163 > /* Compute source contribution for BSDF (reflected & transmitted) */
164   static void
165 < dirbsdf(
165 > dir_bsdf(
166          COLOR  cval,                    /* returned coefficient */
167          void  *nnp,                     /* material data */
168          FVECT  ldir,                    /* light source direction */
# Line 98 | Line 170 | dirbsdf(
170   )
171   {
172          BSDFDAT         *np = (BSDFDAT *)nnp;
101        SDError         ec;
102        SDValue         sv;
103        FVECT           vsrc;
104        FVECT           vjit;
173          double          ldot;
174          double          dtmp;
175          COLOR           ctmp;
# Line 112 | Line 180 | dirbsdf(
180          if ((-FTINY <= ldot) & (ldot <= FTINY))
181                  return;
182  
183 <        if (ldot > .0 && bright(np->rdiff) > FTINY) {
183 >        if (ldot > 0 && bright(np->rdiff) > FTINY) {
184                  /*
185                   *  Compute added diffuse reflected component.
186                   */
# Line 121 | Line 189 | dirbsdf(
189                  scalecolor(ctmp, dtmp);
190                  addcolor(cval, ctmp);
191          }
192 <        if (ldot < .0 && bright(np->tdiff) > FTINY) {
192 >        if (ldot < 0 && bright(np->tdiff) > FTINY) {
193                  /*
194                   *  Compute added diffuse transmission.
195                   */
# Line 133 | Line 201 | dirbsdf(
201          /*
202           *  Compute scattering coefficient using BSDF.
203           */
204 <        if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone)
204 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
205                  return;
206 <        bsdf_jitter(vjit, np);
139 <        ec = SDevalBSDF(&sv, vjit, vsrc, np->sd);
140 <        if (ec)
141 <                objerror(np->mp, USER, transSDError(ec));
142 <
143 <        if (sv.cieY <= FTINY)           /* not worth using? */
144 <                return;
145 <        cvt_sdcolor(ctmp, &sv);
146 <        if (ldot > .0) {                /* pattern only diffuse reflection */
206 >        if (ldot > 0) {         /* pattern only diffuse reflection */
207                  COLOR   ctmp1, ctmp2;
208 <                dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY
208 >                dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
209                                          : np->sd->rLambBack.cieY;
210 <                dtmp /= PI * sv.cieY;   /* diffuse fraction */
210 >                                        /* diffuse fraction */
211 >                dtmp /= PI * bright(ctmp);
212                  copycolor(ctmp2, np->pr->pcol);
213                  scalecolor(ctmp2, dtmp);
214                  setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
# Line 162 | Line 223 | dirbsdf(
223          addcolor(cval, ctmp);
224   }
225  
226 + /* Compute source contribution for BSDF (reflected only) */
227 + static void
228 + dir_brdf(
229 +        COLOR  cval,                    /* returned coefficient */
230 +        void  *nnp,                     /* material data */
231 +        FVECT  ldir,                    /* light source direction */
232 +        double  omega                   /* light source size */
233 + )
234 + {
235 +        BSDFDAT         *np = (BSDFDAT *)nnp;
236 +        double          ldot;
237 +        double          dtmp;
238 +        COLOR           ctmp, ctmp1, ctmp2;
239 +
240 +        setcolor(cval, .0, .0, .0);
241 +
242 +        ldot = DOT(np->pnorm, ldir);
243 +        
244 +        if (ldot <= FTINY)
245 +                return;
246 +
247 +        if (bright(np->rdiff) > FTINY) {
248 +                /*
249 +                 *  Compute added diffuse reflected component.
250 +                 */
251 +                copycolor(ctmp, np->rdiff);
252 +                dtmp = ldot * omega * (1./PI);
253 +                scalecolor(ctmp, dtmp);
254 +                addcolor(cval, ctmp);
255 +        }
256 +        /*
257 +         *  Compute reflection coefficient using BSDF.
258 +         */
259 +        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
260 +                return;
261 +                                        /* pattern only diffuse reflection */
262 +        dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
263 +                                : np->sd->rLambBack.cieY;
264 +        dtmp /= PI * bright(ctmp);      /* diffuse fraction */
265 +        copycolor(ctmp2, np->pr->pcol);
266 +        scalecolor(ctmp2, dtmp);
267 +        setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
268 +        addcolor(ctmp1, ctmp2);
269 +        multcolor(ctmp, ctmp1);         /* apply derated pattern */
270 +        dtmp = ldot * omega;
271 +        scalecolor(ctmp, dtmp);
272 +        addcolor(cval, ctmp);
273 + }
274 +
275 + /* Compute source contribution for BSDF (transmitted only) */
276 + static void
277 + dir_btdf(
278 +        COLOR  cval,                    /* returned coefficient */
279 +        void  *nnp,                     /* material data */
280 +        FVECT  ldir,                    /* light source direction */
281 +        double  omega                   /* light source size */
282 + )
283 + {
284 +        BSDFDAT         *np = (BSDFDAT *)nnp;
285 +        double          ldot;
286 +        double          dtmp;
287 +        COLOR           ctmp;
288 +
289 +        setcolor(cval, .0, .0, .0);
290 +
291 +        ldot = DOT(np->pnorm, ldir);
292 +
293 +        if (ldot >= -FTINY)
294 +                return;
295 +
296 +        if (bright(np->tdiff) > FTINY) {
297 +                /*
298 +                 *  Compute added diffuse transmission.
299 +                 */
300 +                copycolor(ctmp, np->tdiff);
301 +                dtmp = -ldot * omega * (1.0/PI);
302 +                scalecolor(ctmp, dtmp);
303 +                addcolor(cval, ctmp);
304 +        }
305 +        /*
306 +         *  Compute scattering coefficient using BSDF.
307 +         */
308 +        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
309 +                return;
310 +                                        /* full pattern on transmission */
311 +        multcolor(ctmp, np->pr->pcol);
312 +        dtmp = -ldot * omega;
313 +        scalecolor(ctmp, dtmp);
314 +        addcolor(cval, ctmp);
315 + }
316 +
317   /* Sample separate BSDF component */
318   static int
319   sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
320   {
321          int     nstarget = 1;
322 <        int     nsent = 0;
322 >        int     nsent;
323          SDError ec;
324          SDValue bsv;
325 <        double  sthick;
326 <        FVECT   vjit, vsmp;
325 >        double  xrand;
326 >        FVECT   vsmp;
327          RAY     sr;
176        int     ntrials;
328                                                  /* multiple samples? */
329          if (specjitter > 1.5) {
330                  nstarget = specjitter*ndp->pr->rweight + .5;
331 <                if (nstarget < 1)
181 <                        nstarget = 1;
331 >                nstarget += !nstarget;
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 <                bsdf_jitter(vjit, ndp);
338 <                ec = SDsampComponent(&bsv, vsmp, vjit, ntrials ? frandom()
339 <                                : urand(ilhash(dimlist,ndims)+samplendx), 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 >                        if (specjitter < 1.)
338 >                                xrand = .5 + specjitter*(xrand-.5);
339 >                } else {
340 >                        xrand = (nsent + frandom())/(double)nstarget;
341 >                }
342 >                SDerrorDetail[0] = '\0';        /* sample direction & coef. */
343 >                bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]);
344 >                ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
345                  if (ec)
346                          objerror(ndp->mp, USER, transSDError(ec));
347 <                                                /* zero component? */
193 <                if (bsv.cieY <= FTINY)
347 >                if (bsv.cieY <= FTINY)          /* zero component? */
348                          break;
349                                                  /* map vector to world */
350                  if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
351                          break;
198                                                /* unintentional penetration? */
199                if (DOT(sr.rdir, ndp->pr->ron) > .0 ^ vsmp[2] > .0)
200                        continue;
352                                                  /* spawn a specular ray */
353                  if (nstarget > 1)
354                          bsv.cieY /= (double)nstarget;
355 <                cvt_sdcolor(sr.rcoef, &bsv);    /* use color */
356 <                if (usepat)                     /* pattern on transmission */
355 >                cvt_sdcolor(sr.rcoef, &bsv);    /* use sample color */
356 >                if (usepat)                     /* apply pattern? */
357                          multcolor(sr.rcoef, ndp->pr->pcol);
358                  if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
359 <                        if (maxdepth  > 0)
359 >                        if (maxdepth > 0)
360                                  break;
361 <                        ++nsent;                /* Russian roulette victim */
211 <                        continue;
361 >                        continue;               /* Russian roulette victim */
362                  }
363 <                if (ndp->thick > FTINY) {       /* need to move origin? */
364 <                        sthick = (ndp->pr->rod > .0) ? -ndp->thick : ndp->thick;
365 <                        if (sthick < .0 ^ vsmp[2] > .0)
216 <                                VSUM(sr.rorg, sr.rorg, ndp->pr->ron, sthick);
217 <                }
363 >                                                /* need to offset origin? */
364 >                if (ndp->thick != 0 && ndp->pr->rod > 0 ^ vsmp[2] > 0)
365 >                        VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
366                  rayvalue(&sr);                  /* send & evaluate sample */
367                  multcolor(sr.rcol, sr.rcoef);
368                  addcolor(ndp->pr->rcol, sr.rcol);
221                ++nsent;
369          }
370          return(nsent);
371   }
# Line 233 | Line 380 | sample_sdf(BSDFDAT *ndp, int sflags)
380  
381          if (sflags == SDsampSpT) {
382                  unsc = ndp->tunsamp;
383 <                dfp = ndp->sd->tf;
383 >                if (ndp->pr->rod > 0)
384 >                        dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
385 >                else
386 >                        dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
387                  cvt_sdcolor(unsc, &ndp->sd->tLamb);
388          } else /* sflags == SDsampSpR */ {
389                  unsc = ndp->runsamp;
390 <                if (ndp->pr->rod > .0) {
390 >                if (ndp->pr->rod > 0) {
391                          dfp = ndp->sd->rf;
392                          cvt_sdcolor(unsc, &ndp->sd->rLambFront);
393                  } else {
# Line 254 | Line 404 | sample_sdf(BSDFDAT *ndp, int sflags)
404                          FVECT   vjit;
405                          double  d;
406                          COLOR   ctmp;
407 <                        bsdf_jitter(vjit, ndp);
407 >                        bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
408                          d = SDdirectHemi(vjit, sflags, ndp->sd);
409                          if (sflags == SDsampSpT) {
410                                  copycolor(ctmp, ndp->pr->pcol);
# Line 280 | Line 430 | sample_sdf(BSDFDAT *ndp, int sflags)
430   int
431   m_bsdf(OBJREC *m, RAY *r)
432   {
433 +        int     hitfront;
434          COLOR   ctmp;
435          SDError ec;
436 <        FVECT   upvec, outVec;
436 >        FVECT   upvec, vtmp;
437          MFUNC   *mf;
438          BSDFDAT nd;
439                                                  /* check arguments */
440          if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) |
441                                  (m->oargs.nfargs % 3))
442                  objerror(m, USER, "bad # arguments");
443 <
444 <                                                /* get BSDF data */
294 <        nd.sd = loadBSDF(m->oargs.sarg[1]);
443 >                                                /* record surface struck */
444 >        hitfront = (r->rod > 0);
445                                                  /* load cal file */
446          mf = getfunc(m, 5, 0x1d, 1);
447                                                  /* get thickness */
448          nd.thick = evalue(mf->ep[0]);
449 <        if (nd.thick < .0)
449 >        if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
450                  nd.thick = .0;
451                                                  /* check shadow */
452          if (r->crtype & SHADOW) {
453 <                if ((nd.thick > FTINY) & (nd.sd->tf != NULL))
453 >                if (nd.thick != 0)
454                          raytrans(r);            /* pass-through */
455 <                SDfreeCache(nd.sd);
306 <                return(1);                      /* else shadow */
455 >                return(1);                      /* or shadow */
456          }
457 <                                                /* check unscattered ray */
458 <        if (!(r->crtype & (SPECULAR|AMBIENT)) &&
459 <                        (nd.thick > FTINY) & (nd.sd->tf != NULL)) {
460 <                SDfreeCache(nd.sd);
312 <                raytrans(r);                    /* pass-through */
457 >                                                /* check other rays to pass */
458 >        if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) ||
459 >                                nd.thick > 0 ^ hitfront)) {
460 >                raytrans(r);                    /* hide our proxy */
461                  return(1);
462          }
463 +                                                /* get BSDF data */
464 +        nd.sd = loadBSDF(m->oargs.sarg[1]);
465                                                  /* diffuse reflectance */
466 <        if (r->rod > .0) {
466 >        if (hitfront) {
467                  if (m->oargs.nfargs < 3)
468                          setcolor(nd.rdiff, .0, .0, .0);
469                  else
# Line 323 | Line 473 | m_bsdf(OBJREC *m, RAY *r)
473          } else {
474                  if (m->oargs.nfargs < 6) {      /* check invisible backside */
475                          if (!backvis && (nd.sd->rb == NULL) &
476 <                                                (nd.sd->tf == NULL)) {
476 >                                        (nd.sd->tb == NULL)) {
477                                  SDfreeCache(nd.sd);
478                                  raytrans(r);
479                                  return(1);
# Line 345 | Line 495 | m_bsdf(OBJREC *m, RAY *r)
495          nd.pr = r;
496                                                  /* get modifiers */
497          raytexture(r, m->omod);
348        if (bright(r->pcol) <= FTINY) {         /* black pattern?! */
349                SDfreeCache(nd.sd);
350                return(1);
351        }
498                                                  /* modify diffuse values */
499          multcolor(nd.rdiff, r->pcol);
500          multcolor(nd.tdiff, r->pcol);
# Line 357 | Line 503 | m_bsdf(OBJREC *m, RAY *r)
503          upvec[1] = evalue(mf->ep[2]);
504          upvec[2] = evalue(mf->ep[3]);
505                                                  /* return to world coords */
506 <        if (mf->f != &unitxf) {
507 <                multv3(upvec, upvec, mf->f->xfm);
508 <                nd.thick *= mf->f->sca;
506 >        if (mf->fxp != &unitxf) {
507 >                multv3(upvec, upvec, mf->fxp->xfm);
508 >                nd.thick *= mf->fxp->sca;
509          }
510          raynormal(nd.pnorm, r);
511                                                  /* compute local BSDF xform */
# Line 372 | Line 518 | m_bsdf(OBJREC *m, RAY *r)
518          }
519          if (!ec)
520                  ec = SDinvXform(nd.fromloc, nd.toloc);
521 <                                                /* determine BSDF resolution */
522 <        if (!ec)
377 <                ec = SDsizeBSDF(&nd.sr_vpsa, nd.vray, SDqueryMin, nd.sd);
378 <        if (!ec)
379 <                nd.sr_vpsa = sqrt(nd.sr_vpsa);
380 <        else {
381 <                objerror(m, WARNING, transSDError(ec));
382 <                SDfreeCache(nd.sd);
521 >        if (ec) {
522 >                objerror(m, WARNING, "Illegal orientation vector");
523                  return(1);
524          }
525 <        
526 <        if (r->rod < .0) {                      /* perturb normal towards hit */
525 >                                                /* determine BSDF resolution */
526 >        ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin+SDqueryMax, nd.sd);
527 >        if (ec)
528 >                objerror(m, USER, transSDError(ec));
529 >
530 >        nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]);
531 >        nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]);
532 >        if (!hitfront) {                        /* perturb normal towards hit */
533                  nd.pnorm[0] = -nd.pnorm[0];
534                  nd.pnorm[1] = -nd.pnorm[1];
535                  nd.pnorm[2] = -nd.pnorm[2];
# Line 395 | Line 541 | m_bsdf(OBJREC *m, RAY *r)
541                                                  /* compute indirect diffuse */
542          copycolor(ctmp, nd.rdiff);
543          addcolor(ctmp, nd.runsamp);
544 <        if (bright(ctmp) > FTINY) {             /* ambient from this side */
545 <                if (r->rod < .0)
544 >        if (bright(ctmp) > FTINY) {             /* ambient from reflection */
545 >                if (!hitfront)
546                          flipsurface(r);
547                  multambient(ctmp, r, nd.pnorm);
548                  addcolor(r->rcol, ctmp);
549 <                if (r->rod < .0)
549 >                if (!hitfront)
550                          flipsurface(r);
551          }
552          copycolor(ctmp, nd.tdiff);
553          addcolor(ctmp, nd.tunsamp);
554          if (bright(ctmp) > FTINY) {             /* ambient from other side */
555                  FVECT  bnorm;
556 <                if (r->rod > .0)
556 >                if (hitfront)
557                          flipsurface(r);
558                  bnorm[0] = -nd.pnorm[0];
559                  bnorm[1] = -nd.pnorm[1];
560                  bnorm[2] = -nd.pnorm[2];
561 <                multambient(ctmp, r, bnorm);
561 >                if (nd.thick != 0) {            /* proxy with offset? */
562 >                        VCOPY(vtmp, r->rop);
563 >                        VSUM(r->rop, vtmp, r->ron, nd.thick);
564 >                        multambient(ctmp, r, bnorm);
565 >                        VCOPY(r->rop, vtmp);
566 >                } else
567 >                        multambient(ctmp, r, bnorm);
568                  addcolor(r->rcol, ctmp);
569 <                if (r->rod > .0)
569 >                if (hitfront)
570                          flipsurface(r);
571          }
572                                                  /* add direct component */
573 <        direct(r, dirbsdf, &nd);
573 >        if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) &
574 >                                        (nd.sd->tb == NULL)) {
575 >                direct(r, dir_brdf, &nd);       /* reflection only */
576 >        } else if (nd.thick == 0) {
577 >                direct(r, dir_bsdf, &nd);       /* thin surface scattering */
578 >        } else {
579 >                direct(r, dir_brdf, &nd);       /* reflection first */
580 >                VCOPY(vtmp, r->rop);            /* offset for transmitted */
581 >                VSUM(r->rop, vtmp, r->ron, -nd.thick);
582 >                direct(r, dir_btdf, &nd);       /* separate transmission */
583 >                VCOPY(r->rop, vtmp);
584 >        }
585                                                  /* clean up */
586          SDfreeCache(nd.sd);
587          return(1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines