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.5 by greg, Sun Feb 20 06:34:19 2011 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   *
# Line 88 | Line 96 | bsdf_jitter(FVECT vres, BSDFDAT *ndp)
96          normalize(vres);
97   }
98  
99 < /* Compute source contribution for BSDF */
99 > /* Compute source contribution for BSDF (reflected & transmitted) */
100   static void
101 < dirbsdf(
101 > dir_bsdf(
102          COLOR  cval,                    /* returned coefficient */
103          void  *nnp,                     /* material data */
104          FVECT  ldir,                    /* light source direction */
# Line 162 | Line 170 | dirbsdf(
170          addcolor(cval, ctmp);
171   }
172  
173 + /* Compute source contribution for BSDF (reflected only) */
174 + static void
175 + dir_brdf(
176 +        COLOR  cval,                    /* returned coefficient */
177 +        void  *nnp,                     /* material data */
178 +        FVECT  ldir,                    /* light source direction */
179 +        double  omega                   /* light source size */
180 + )
181 + {
182 +        BSDFDAT         *np = (BSDFDAT *)nnp;
183 +        SDError         ec;
184 +        SDValue         sv;
185 +        FVECT           vsrc;
186 +        FVECT           vjit;
187 +        double          ldot;
188 +        double          dtmp;
189 +        COLOR           ctmp, ctmp1, ctmp2;
190 +
191 +        setcolor(cval, .0, .0, .0);
192 +
193 +        ldot = DOT(np->pnorm, ldir);
194 +        
195 +        if (ldot <= FTINY)
196 +                return;
197 +
198 +        if (bright(np->rdiff) > FTINY) {
199 +                /*
200 +                 *  Compute added diffuse reflected component.
201 +                 */
202 +                copycolor(ctmp, np->rdiff);
203 +                dtmp = ldot * omega * (1./PI);
204 +                scalecolor(ctmp, dtmp);
205 +                addcolor(cval, ctmp);
206 +        }
207 +        /*
208 +         *  Compute reflection coefficient using BSDF.
209 +         */
210 +        if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone)
211 +                return;
212 +        bsdf_jitter(vjit, np);
213 +        ec = SDevalBSDF(&sv, vjit, vsrc, np->sd);
214 +        if (ec)
215 +                objerror(np->mp, USER, transSDError(ec));
216 +
217 +        if (sv.cieY <= FTINY)           /* not worth using? */
218 +                return;
219 +        cvt_sdcolor(ctmp, &sv);
220 +                                        /* pattern only diffuse reflection */
221 +        dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY
222 +                                : np->sd->rLambBack.cieY;
223 +        dtmp /= PI * sv.cieY;           /* diffuse fraction */
224 +        copycolor(ctmp2, np->pr->pcol);
225 +        scalecolor(ctmp2, dtmp);
226 +        setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
227 +        addcolor(ctmp1, ctmp2);
228 +        multcolor(ctmp, ctmp1);         /* apply derated pattern */
229 +        dtmp = ldot * omega;
230 +        scalecolor(ctmp, dtmp);
231 +        addcolor(cval, ctmp);
232 + }
233 +
234 + /* Compute source contribution for BSDF (transmitted only) */
235 + static void
236 + dir_btdf(
237 +        COLOR  cval,                    /* returned coefficient */
238 +        void  *nnp,                     /* material data */
239 +        FVECT  ldir,                    /* light source direction */
240 +        double  omega                   /* light source size */
241 + )
242 + {
243 +        BSDFDAT         *np = (BSDFDAT *)nnp;
244 +        SDError         ec;
245 +        SDValue         sv;
246 +        FVECT           vsrc;
247 +        FVECT           vjit;
248 +        double          ldot;
249 +        double          dtmp;
250 +        COLOR           ctmp;
251 +
252 +        setcolor(cval, .0, .0, .0);
253 +
254 +        ldot = DOT(np->pnorm, ldir);
255 +
256 +        if (ldot >= -FTINY)
257 +                return;
258 +
259 +        if (bright(np->tdiff) > FTINY) {
260 +                /*
261 +                 *  Compute added diffuse transmission.
262 +                 */
263 +                copycolor(ctmp, np->tdiff);
264 +                dtmp = -ldot * omega * (1.0/PI);
265 +                scalecolor(ctmp, dtmp);
266 +                addcolor(cval, ctmp);
267 +        }
268 +        /*
269 +         *  Compute scattering coefficient using BSDF.
270 +         */
271 +        if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone)
272 +                return;
273 +        bsdf_jitter(vjit, np);
274 +        ec = SDevalBSDF(&sv, vjit, vsrc, np->sd);
275 +        if (ec)
276 +                objerror(np->mp, USER, transSDError(ec));
277 +
278 +        if (sv.cieY <= FTINY)           /* not worth using? */
279 +                return;
280 +        cvt_sdcolor(ctmp, &sv);
281 +                                        /* full pattern on transmission */
282 +        multcolor(ctmp, np->pr->pcol);
283 +        dtmp = -ldot * omega;
284 +        scalecolor(ctmp, dtmp);
285 +        addcolor(cval, ctmp);
286 + }
287 +
288   /* Sample separate BSDF component */
289   static int
290   sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
# Line 210 | Line 333 | sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usep
333                          ++nsent;                /* Russian roulette victim */
334                          continue;
335                  }
336 <                if (ndp->thick > FTINY) {       /* need to move origin? */
337 <                        sthick = (ndp->pr->rod > .0) ? -ndp->thick : ndp->thick;
338 <                        if (sthick < .0 ^ vsmp[2] > .0)
216 <                                VSUM(sr.rorg, sr.rorg, ndp->pr->ron, sthick);
217 <                }
336 >                                                /* need to offset origin? */
337 >                if (ndp->thick != .0 && ndp->pr->rod > .0 ^ vsmp[2] > .0)
338 >                        VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
339                  rayvalue(&sr);                  /* send & evaluate sample */
340                  multcolor(sr.rcol, sr.rcoef);
341                  addcolor(ndp->pr->rcol, sr.rcol);
# Line 282 | Line 403 | m_bsdf(OBJREC *m, RAY *r)
403   {
404          COLOR   ctmp;
405          SDError ec;
406 <        FVECT   upvec, outVec;
406 >        FVECT   upvec, vtmp;
407          MFUNC   *mf;
408          BSDFDAT nd;
409                                                  /* check arguments */
# Line 290 | Line 411 | m_bsdf(OBJREC *m, RAY *r)
411                                  (m->oargs.nfargs % 3))
412                  objerror(m, USER, "bad # arguments");
413  
293                                                /* get BSDF data */
294        nd.sd = loadBSDF(m->oargs.sarg[1]);
414                                                  /* load cal file */
415          mf = getfunc(m, 5, 0x1d, 1);
416                                                  /* get thickness */
417          nd.thick = evalue(mf->ep[0]);
418 <        if (nd.thick < .0)
418 >        if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
419                  nd.thick = .0;
420                                                  /* check shadow */
421          if (r->crtype & SHADOW) {
422 <                if ((nd.thick > FTINY) & (nd.sd->tf != NULL))
422 >                if (nd.thick != .0)
423                          raytrans(r);            /* pass-through */
424 <                SDfreeCache(nd.sd);
306 <                return(1);                      /* else shadow */
424 >                return(1);                      /* or shadow */
425          }
426 <                                                /* check unscattered ray */
427 <        if (!(r->crtype & (SPECULAR|AMBIENT)) &&
428 <                        (nd.thick > FTINY) & (nd.sd->tf != NULL)) {
429 <                SDfreeCache(nd.sd);
312 <                raytrans(r);                    /* pass-through */
426 >                                                /* check other rays to pass */
427 >        if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) ||
428 >                                nd.thick > .0 ^ r->rod > .0)) {
429 >                raytrans(r);                    /* hide our proxy */
430                  return(1);
431          }
432 +                                                /* get BSDF data */
433 +        nd.sd = loadBSDF(m->oargs.sarg[1]);
434                                                  /* diffuse reflectance */
435          if (r->rod > .0) {
436                  if (m->oargs.nfargs < 3)
# Line 345 | Line 464 | m_bsdf(OBJREC *m, RAY *r)
464          nd.pr = r;
465                                                  /* get modifiers */
466          raytexture(r, m->omod);
348        if (bright(r->pcol) <= FTINY) {         /* black pattern?! */
349                SDfreeCache(nd.sd);
350                return(1);
351        }
467                                                  /* modify diffuse values */
468          multcolor(nd.rdiff, r->pcol);
469          multcolor(nd.tdiff, r->pcol);
# Line 382 | Line 497 | m_bsdf(OBJREC *m, RAY *r)
497                  SDfreeCache(nd.sd);
498                  return(1);
499          }
385        
500          if (r->rod < .0) {                      /* perturb normal towards hit */
501                  nd.pnorm[0] = -nd.pnorm[0];
502                  nd.pnorm[1] = -nd.pnorm[1];
# Line 395 | Line 509 | m_bsdf(OBJREC *m, RAY *r)
509                                                  /* compute indirect diffuse */
510          copycolor(ctmp, nd.rdiff);
511          addcolor(ctmp, nd.runsamp);
512 <        if (bright(ctmp) > FTINY) {             /* ambient from this side */
512 >        if (bright(ctmp) > FTINY) {             /* ambient from reflection */
513                  if (r->rod < .0)
514                          flipsurface(r);
515                  multambient(ctmp, r, nd.pnorm);
# Line 412 | Line 526 | m_bsdf(OBJREC *m, RAY *r)
526                  bnorm[0] = -nd.pnorm[0];
527                  bnorm[1] = -nd.pnorm[1];
528                  bnorm[2] = -nd.pnorm[2];
529 <                multambient(ctmp, r, bnorm);
529 >                if (nd.thick != .0) {           /* proxy with offset? */
530 >                        VCOPY(vtmp, r->rop);
531 >                        VSUM(r->rop, vtmp, r->ron, -nd.thick);
532 >                        multambient(ctmp, r, bnorm);
533 >                        VCOPY(r->rop, vtmp);
534 >                } else
535 >                        multambient(ctmp, r, bnorm);
536                  addcolor(r->rcol, ctmp);
537                  if (r->rod > .0)
538                          flipsurface(r);
539          }
540                                                  /* add direct component */
541 <        direct(r, dirbsdf, &nd);
541 >        if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL)) {
542 >                direct(r, dir_brdf, &nd);       /* reflection only */
543 >        } else if (nd.thick == .0) {
544 >                direct(r, dir_bsdf, &nd);       /* thin surface scattering */
545 >        } else {
546 >                direct(r, dir_brdf, &nd);       /* reflection first */
547 >                VCOPY(vtmp, r->rop);            /* offset for transmitted */
548 >                VSUM(r->rop, vtmp, r->ron, -nd.thick);
549 >                direct(r, dir_btdf, &nd);
550 >                VCOPY(r->rop, vtmp);
551 >        }
552                                                  /* clean up */
553          SDfreeCache(nd.sd);
554          return(1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines