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.5 by greg, Sun Feb 20 06:34:19 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   *
# Line 48 | Line 55 | static const char RCSid[] = "$Id$";
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;        /* sqrt of BSDF projected solid angle */
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)
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;
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 > /* 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 */
105          double  omega                   /* light source size */
106   )
107   {
108 <        BSDFDAT         *np = nnp;
108 >        BSDFDAT         *np = (BSDFDAT *)nnp;
109          SDError         ec;
110          SDValue         sv;
111 <        FVECT           vout;
111 >        FVECT           vsrc;
112 >        FVECT           vjit;
113          double          ldot;
114          double          dtmp;
115          COLOR           ctmp;
# Line 119 | Line 141 | dirbsdf(
141          /*
142           *  Compute scattering coefficient using BSDF.
143           */
144 <        if (SDmapDir(vout, np->toloc, ldir) != SDEnone)
144 >        if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone)
145                  return;
146 <        ec = SDevalBSDF(&sv, vout, np->vinc, np->sd);
146 >        bsdf_jitter(vjit, np);
147 >        ec = SDevalBSDF(&sv, vjit, vsrc, np->sd);
148          if (ec)
149 <                objerror(np->mp, USER, cvt_sderr(ec));
149 >                objerror(np->mp, USER, transSDError(ec));
150  
151          if (sv.cieY <= FTINY)           /* not worth using? */
152                  return;
# Line 137 | Line 160 | dirbsdf(
160                  scalecolor(ctmp2, dtmp);
161                  setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
162                  addcolor(ctmp1, ctmp2);
163 <                multcolor(ctmp, ctmp1); /* apply desaturated pattern */
163 >                multcolor(ctmp, ctmp1); /* apply derated pattern */
164                  dtmp = ldot * omega;
165          } else {                        /* full pattern on transmission */
166                  multcolor(ctmp, np->pr->pcol);
# Line 147 | 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 156 | Line 294 | sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usep
294          SDError ec;
295          SDValue bsv;
296          double  sthick;
297 <        FVECT   vout;
297 >        FVECT   vjit, vsmp;
298          RAY     sr;
299          int     ntrials;
300                                                  /* multiple samples? */
# Line 169 | Line 307 | sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usep
307          for (ntrials = 0; nsent < nstarget && ntrials < 9*nstarget; ntrials++) {
308                  SDerrorDetail[0] = '\0';
309                                                  /* sample direction & coef. */
310 <                ec = SDsampComponent(&bsv, vout, ndp->vinc,
311 <                                ntrials ? frandom()
312 <                                        : urand(ilhash(dimlist,ndims)+samplendx),
175 <                                                dcp);
310 >                bsdf_jitter(vjit, ndp);
311 >                ec = SDsampComponent(&bsv, vsmp, vjit, ntrials ? frandom()
312 >                                : urand(ilhash(dimlist,ndims)+samplendx), dcp);
313                  if (ec)
314 <                        objerror(ndp->mp, USER, cvt_sderr(ec));
314 >                        objerror(ndp->mp, USER, transSDError(ec));
315                                                  /* zero component? */
316                  if (bsv.cieY <= FTINY)
317                          break;
318                                                  /* map vector to world */
319 <                if (SDmapDir(sr.rdir, ndp->fromloc, vout) != SDEnone)
319 >                if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
320                          break;
321                                                  /* unintentional penetration? */
322 <                if (DOT(sr.rdir, ndp->pr->ron) > .0 ^ vout[2] > .0)
322 >                if (DOT(sr.rdir, ndp->pr->ron) > .0 ^ vsmp[2] > .0)
323                          continue;
324                                                  /* spawn a specular ray */
325                  if (nstarget > 1)
# Line 196 | Line 333 | sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usep
333                          ++nsent;                /* Russian roulette victim */
334                          continue;
335                  }
336 <                                                /* need to move origin? */
337 <                sthick = (ndp->pr->rod > .0) ? -ndp->thick : ndp->thick;
338 <                if (sthick < .0 ^ vout[2] > .0)
202 <                        VSUM(sr.rorg, sr.rorg, ndp->pr->ron, sthick);
203 <
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 236 | Line 371 | sample_sdf(BSDFDAT *ndp, int sflags)
371                  return(0);
372                                                  /* below sampling threshold? */
373          if (dfp->maxHemi <= specthresh+FTINY) {
374 <                if (dfp->maxHemi > FTINY) {     /* XXX no color from BSDF! */
375 <                        double  d = SDdirectHemi(ndp->vinc, sflags, ndp->sd);
374 >                if (dfp->maxHemi > FTINY) {     /* XXX no color from BSDF */
375 >                        FVECT   vjit;
376 >                        double  d;
377                          COLOR   ctmp;
378 +                        bsdf_jitter(vjit, ndp);
379 +                        d = SDdirectHemi(vjit, sflags, ndp->sd);
380                          if (sflags == SDsampSpT) {
381                                  copycolor(ctmp, ndp->pr->pcol);
382                                  scalecolor(ctmp, d);
# Line 265 | 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 273 | Line 411 | m_bsdf(OBJREC *m, RAY *r)
411                                  (m->oargs.nfargs % 3))
412                  objerror(m, USER, "bad # arguments");
413  
276        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        }
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 <                SDfreeCache(nd.sd);
301 <                if (nd.thick > FTINY && nd.sd->tf != NULL &&
302 <                                nd.sd->tf->maxHemi > FTINY)
422 >                if (nd.thick != .0)
423                          raytrans(r);            /* pass-through */
424 <                return(1);                      /* else shadow */
424 >                return(1);                      /* or shadow */
425          }
426 <                                                /* check unscattered ray */
427 <        if (!(r->crtype & (SPECULAR|AMBIENT)) && nd.thick > FTINY &&
428 <                        nd.sd->tf != NULL && nd.sd->tf->maxHemi > FTINY) {
429 <                SDfreeCache(nd.sd);
310 <                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 320 | Line 441 | m_bsdf(OBJREC *m, RAY *r)
441                                          m->oargs.farg[2]);
442          } else {
443                  if (m->oargs.nfargs < 6) {      /* check invisible backside */
444 <                        if (!backvis && (nd.sd->rb == NULL ||
445 <                                                nd.sd->rb->maxHemi <= FTINY) &&
325 <                                        (nd.sd->tf == NULL ||
326 <                                                nd.sd->tf->maxHemi <= FTINY)) {
444 >                        if (!backvis && (nd.sd->rb == NULL) &
445 >                                                (nd.sd->tf == NULL)) {
446                                  SDfreeCache(nd.sd);
447                                  raytrans(r);
448                                  return(1);
# 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 365 | Line 480 | m_bsdf(OBJREC *m, RAY *r)
480                                                  /* compute local BSDF xform */
481          ec = SDcompXform(nd.toloc, nd.pnorm, upvec);
482          if (!ec) {
483 <                nd.vinc[0] = -r->rdir[0];
484 <                nd.vinc[1] = -r->rdir[1];
485 <                nd.vinc[2] = -r->rdir[2];
486 <                ec = SDmapDir(nd.vinc, nd.toloc, nd.vinc);
483 >                nd.vray[0] = -r->rdir[0];
484 >                nd.vray[1] = -r->rdir[1];
485 >                nd.vray[2] = -r->rdir[2];
486 >                ec = SDmapDir(nd.vray, nd.toloc, nd.vray);
487          }
488          if (!ec)
489                  ec = SDinvXform(nd.fromloc, nd.toloc);
490 <        if (ec) {
491 <                objerror(m, WARNING, cvt_sderr(ec));
490 >                                                /* determine BSDF resolution */
491 >        if (!ec)
492 >                ec = SDsizeBSDF(&nd.sr_vpsa, nd.vray, SDqueryMin, nd.sd);
493 >        if (!ec)
494 >                nd.sr_vpsa = sqrt(nd.sr_vpsa);
495 >        else {
496 >                objerror(m, WARNING, transSDError(ec));
497                  SDfreeCache(nd.sd);
498                  return(1);
499          }
# Line 389 | 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 406 | 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