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.5 by greg, Sun Feb 20 06:34:19 2011 UTC vs.
Revision 2.27 by greg, Tue Feb 24 19:39:26 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.
# Line 50 | Line 51 | static const char RCSid[] = "$Id$";
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   */
# Line 67 | Line 68 | typedef struct {
68          RAY     *pr;            /* intersected ray */
69          FVECT   pnorm;          /* perturbed surface normal */
70          FVECT   vray;           /* local outgoing (return) vector */
71 <        double  sr_vpsa;        /* sqrt of BSDF projected solid angle */
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 82 | Line 83 | typedef struct {
83  
84   /* Jitter ray sample according to projected solid angle and specjitter */
85   static void
86 < bsdf_jitter(FVECT vres, BSDFDAT *ndp)
86 > bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
87   {
87        double  sr_psa = ndp->sr_vpsa;
88
88          VCOPY(vres, ndp->vray);
89          if (specjitter < 1.)
90                  sr_psa *= specjitter;
# Line 96 | Line 95 | bsdf_jitter(FVECT vres, BSDFDAT *ndp)
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(
# Line 106 | Line 173 | dir_bsdf(
173   )
174   {
175          BSDFDAT         *np = (BSDFDAT *)nnp;
109        SDError         ec;
110        SDValue         sv;
111        FVECT           vsrc;
112        FVECT           vjit;
176          double          ldot;
177          double          dtmp;
178          COLOR           ctmp;
# Line 120 | Line 183 | dir_bsdf(
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 129 | Line 192 | dir_bsdf(
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 141 | Line 204 | dir_bsdf(
204          /*
205           *  Compute scattering coefficient using BSDF.
206           */
207 <        if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone)
207 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
208                  return;
209 <        bsdf_jitter(vjit, np);
147 <        ec = SDevalBSDF(&sv, vjit, vsrc, np->sd);
148 <        if (ec)
149 <                objerror(np->mp, USER, transSDError(ec));
150 <
151 <        if (sv.cieY <= FTINY)           /* not worth using? */
152 <                return;
153 <        cvt_sdcolor(ctmp, &sv);
154 <        if (ldot > .0) {                /* pattern only diffuse reflection */
209 >        if (ldot > 0) {         /* pattern only diffuse reflection */
210                  COLOR   ctmp1, ctmp2;
211 <                dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY
211 >                dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
212                                          : np->sd->rLambBack.cieY;
213 <                dtmp /= PI * sv.cieY;   /* diffuse fraction */
213 >                                        /* diffuse fraction */
214 >                dtmp /= PI * bright(ctmp);
215                  copycolor(ctmp2, np->pr->pcol);
216                  scalecolor(ctmp2, dtmp);
217                  setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
# Line 180 | Line 236 | dir_brdf(
236   )
237   {
238          BSDFDAT         *np = (BSDFDAT *)nnp;
183        SDError         ec;
184        SDValue         sv;
185        FVECT           vsrc;
186        FVECT           vjit;
239          double          ldot;
240          double          dtmp;
241          COLOR           ctmp, ctmp1, ctmp2;
# Line 207 | Line 259 | dir_brdf(
259          /*
260           *  Compute reflection coefficient using BSDF.
261           */
262 <        if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone)
262 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
263                  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);
264                                          /* pattern only diffuse reflection */
265 <        dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY
265 >        dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
266                                  : np->sd->rLambBack.cieY;
267 <        dtmp /= PI * sv.cieY;           /* diffuse fraction */
267 >        dtmp /= PI * bright(ctmp);      /* diffuse fraction */
268          copycolor(ctmp2, np->pr->pcol);
269          scalecolor(ctmp2, dtmp);
270          setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
# Line 241 | Line 285 | dir_btdf(
285   )
286   {
287          BSDFDAT         *np = (BSDFDAT *)nnp;
244        SDError         ec;
245        SDValue         sv;
246        FVECT           vsrc;
247        FVECT           vjit;
288          double          ldot;
289          double          dtmp;
290          COLOR           ctmp;
# Line 268 | Line 308 | dir_btdf(
308          /*
309           *  Compute scattering coefficient using BSDF.
310           */
311 <        if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone)
311 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
312                  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);
313                                          /* full pattern on transmission */
314          multcolor(ctmp, np->pr->pcol);
315          dtmp = -ldot * omega;
# Line 290 | Line 322 | static int
322   sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
323   {
324          int     nstarget = 1;
325 <        int     nsent = 0;
325 >        int     nsent;
326          SDError ec;
327          SDValue bsv;
328 <        double  sthick;
329 <        FVECT   vjit, vsmp;
328 >        double  xrand;
329 >        FVECT   vsmp;
330          RAY     sr;
299        int     ntrials;
331                                                  /* multiple samples? */
332          if (specjitter > 1.5) {
333                  nstarget = specjitter*ndp->pr->rweight + .5;
334 <                if (nstarget < 1)
304 <                        nstarget = 1;
334 >                nstarget += !nstarget;
335          }
336 <                                                /* run through our trials */
337 <        for (ntrials = 0; nsent < nstarget && ntrials < 9*nstarget; ntrials++) {
338 <                SDerrorDetail[0] = '\0';
339 <                                                /* sample direction & coef. */
340 <                bsdf_jitter(vjit, ndp);
341 <                ec = SDsampComponent(&bsv, vsmp, vjit, ntrials ? frandom()
342 <                                : urand(ilhash(dimlist,ndims)+samplendx), dcp);
336 >                                                /* run through our samples */
337 >        for (nsent = 0; nsent < nstarget; nsent++) {
338 >                if (nstarget == 1) {            /* stratify random variable */
339 >                        xrand = urand(ilhash(dimlist,ndims)+samplendx);
340 >                        if (specjitter < 1.)
341 >                                xrand = .5 + specjitter*(xrand-.5);
342 >                } else {
343 >                        xrand = (nsent + frandom())/(double)nstarget;
344 >                }
345 >                SDerrorDetail[0] = '\0';        /* sample direction & coef. */
346 >                bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]);
347 >                ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
348                  if (ec)
349                          objerror(ndp->mp, USER, transSDError(ec));
350 <                                                /* zero component? */
316 <                if (bsv.cieY <= FTINY)
350 >                if (bsv.cieY <= FTINY)          /* zero component? */
351                          break;
352                                                  /* map vector to world */
353                  if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
354                          break;
321                                                /* unintentional penetration? */
322                if (DOT(sr.rdir, ndp->pr->ron) > .0 ^ vsmp[2] > .0)
323                        continue;
355                                                  /* spawn a specular ray */
356                  if (nstarget > 1)
357                          bsv.cieY /= (double)nstarget;
358 <                cvt_sdcolor(sr.rcoef, &bsv);    /* use color */
359 <                if (usepat)                     /* pattern on transmission */
358 >                cvt_sdcolor(sr.rcoef, &bsv);    /* use sample color */
359 >                if (usepat)                     /* apply pattern? */
360                          multcolor(sr.rcoef, ndp->pr->pcol);
361                  if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
362 <                        if (maxdepth  > 0)
362 >                        if (maxdepth > 0)
363                                  break;
364 <                        ++nsent;                /* Russian roulette victim */
334 <                        continue;
364 >                        continue;               /* Russian roulette victim */
365                  }
366                                                  /* need to offset origin? */
367 <                if (ndp->thick != .0 && ndp->pr->rod > .0 ^ vsmp[2] > .0)
367 >                if (ndp->thick != 0 && ndp->pr->rod > 0 ^ vsmp[2] > 0)
368                          VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
369                  rayvalue(&sr);                  /* send & evaluate sample */
370                  multcolor(sr.rcol, sr.rcoef);
371                  addcolor(ndp->pr->rcol, sr.rcol);
342                ++nsent;
372          }
373          return(nsent);
374   }
# Line 354 | Line 383 | sample_sdf(BSDFDAT *ndp, int sflags)
383  
384          if (sflags == SDsampSpT) {
385                  unsc = ndp->tunsamp;
386 <                dfp = ndp->sd->tf;
386 >                if (ndp->pr->rod > 0)
387 >                        dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
388 >                else
389 >                        dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
390                  cvt_sdcolor(unsc, &ndp->sd->tLamb);
391          } else /* sflags == SDsampSpR */ {
392                  unsc = ndp->runsamp;
393 <                if (ndp->pr->rod > .0) {
393 >                if (ndp->pr->rod > 0) {
394                          dfp = ndp->sd->rf;
395                          cvt_sdcolor(unsc, &ndp->sd->rLambFront);
396                  } else {
# Line 375 | Line 407 | sample_sdf(BSDFDAT *ndp, int sflags)
407                          FVECT   vjit;
408                          double  d;
409                          COLOR   ctmp;
410 <                        bsdf_jitter(vjit, ndp);
410 >                        bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
411                          d = SDdirectHemi(vjit, sflags, ndp->sd);
412                          if (sflags == SDsampSpT) {
413                                  copycolor(ctmp, ndp->pr->pcol);
# Line 401 | Line 433 | sample_sdf(BSDFDAT *ndp, int sflags)
433   int
434   m_bsdf(OBJREC *m, RAY *r)
435   {
436 +        int     hitfront;
437          COLOR   ctmp;
438          SDError ec;
439          FVECT   upvec, vtmp;
# Line 410 | Line 443 | m_bsdf(OBJREC *m, RAY *r)
443          if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) |
444                                  (m->oargs.nfargs % 3))
445                  objerror(m, USER, "bad # arguments");
446 <
446 >                                                /* record surface struck */
447 >        hitfront = (r->rod > 0);
448                                                  /* load cal file */
449          mf = getfunc(m, 5, 0x1d, 1);
450 +        setfunc(m, r);
451                                                  /* get thickness */
452          nd.thick = evalue(mf->ep[0]);
453          if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
454                  nd.thick = .0;
455                                                  /* check shadow */
456          if (r->crtype & SHADOW) {
457 <                if (nd.thick != .0)
457 >                if (nd.thick != 0)
458                          raytrans(r);            /* pass-through */
459                  return(1);                      /* or shadow */
460          }
461 +                                                /* check backface visibility */
462 +        if (!hitfront & !backvis) {
463 +                raytrans(r);
464 +                return(1);
465 +        }
466                                                  /* check other rays to pass */
467          if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) ||
468 <                                nd.thick > .0 ^ r->rod > .0)) {
468 >                                nd.thick > 0 ^ hitfront)) {
469                  raytrans(r);                    /* hide our proxy */
470                  return(1);
471          }
472 +        
473 +        /* PMAP: skip ambient ray if accounted for by photon map */
474 +        if (ambRayInPmap(r))
475 +           return(1);
476 +        
477                                                  /* get BSDF data */
478          nd.sd = loadBSDF(m->oargs.sarg[1]);
479                                                  /* diffuse reflectance */
480 <        if (r->rod > .0) {
480 >        if (hitfront) {
481                  if (m->oargs.nfargs < 3)
482                          setcolor(nd.rdiff, .0, .0, .0);
483                  else
# Line 440 | Line 485 | m_bsdf(OBJREC *m, RAY *r)
485                                          m->oargs.farg[1],
486                                          m->oargs.farg[2]);
487          } else {
488 <                if (m->oargs.nfargs < 6) {      /* check invisible backside */
444 <                        if (!backvis && (nd.sd->rb == NULL) &
445 <                                                (nd.sd->tf == NULL)) {
446 <                                SDfreeCache(nd.sd);
447 <                                raytrans(r);
448 <                                return(1);
449 <                        }
488 >                if (m->oargs.nfargs < 6)
489                          setcolor(nd.rdiff, .0, .0, .0);
490 <                } else
490 >                else
491                          setcolor(nd.rdiff, m->oargs.farg[3],
492                                          m->oargs.farg[4],
493                                          m->oargs.farg[5]);
# Line 472 | Line 511 | m_bsdf(OBJREC *m, RAY *r)
511          upvec[1] = evalue(mf->ep[2]);
512          upvec[2] = evalue(mf->ep[3]);
513                                                  /* return to world coords */
514 <        if (mf->f != &unitxf) {
515 <                multv3(upvec, upvec, mf->f->xfm);
516 <                nd.thick *= mf->f->sca;
514 >        if (mf->fxp != &unitxf) {
515 >                multv3(upvec, upvec, mf->fxp->xfm);
516 >                nd.thick *= mf->fxp->sca;
517          }
518 +        if (r->rox != NULL) {
519 +                multv3(upvec, upvec, r->rox->f.xfm);
520 +                nd.thick *= r->rox->f.sca;
521 +        }
522          raynormal(nd.pnorm, r);
523                                                  /* compute local BSDF xform */
524          ec = SDcompXform(nd.toloc, nd.pnorm, upvec);
# Line 487 | Line 530 | m_bsdf(OBJREC *m, RAY *r)
530          }
531          if (!ec)
532                  ec = SDinvXform(nd.fromloc, nd.toloc);
533 <                                                /* determine BSDF resolution */
534 <        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);
533 >        if (ec) {
534 >                objerror(m, WARNING, "Illegal orientation vector");
535                  return(1);
536          }
537 <        if (r->rod < .0) {                      /* perturb normal towards hit */
537 >                                                /* determine BSDF resolution */
538 >        ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin+SDqueryMax, nd.sd);
539 >        if (ec)
540 >                objerror(m, USER, transSDError(ec));
541 >
542 >        nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]);
543 >        nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]);
544 >        if (!hitfront) {                        /* perturb normal towards hit */
545                  nd.pnorm[0] = -nd.pnorm[0];
546                  nd.pnorm[1] = -nd.pnorm[1];
547                  nd.pnorm[2] = -nd.pnorm[2];
# Line 510 | Line 554 | m_bsdf(OBJREC *m, RAY *r)
554          copycolor(ctmp, nd.rdiff);
555          addcolor(ctmp, nd.runsamp);
556          if (bright(ctmp) > FTINY) {             /* ambient from reflection */
557 <                if (r->rod < .0)
557 >                if (!hitfront)
558                          flipsurface(r);
559                  multambient(ctmp, r, nd.pnorm);
560                  addcolor(r->rcol, ctmp);
561 <                if (r->rod < .0)
561 >                if (!hitfront)
562                          flipsurface(r);
563          }
564          copycolor(ctmp, nd.tdiff);
565          addcolor(ctmp, nd.tunsamp);
566          if (bright(ctmp) > FTINY) {             /* ambient from other side */
567                  FVECT  bnorm;
568 <                if (r->rod > .0)
568 >                if (hitfront)
569                          flipsurface(r);
570                  bnorm[0] = -nd.pnorm[0];
571                  bnorm[1] = -nd.pnorm[1];
572                  bnorm[2] = -nd.pnorm[2];
573 <                if (nd.thick != .0) {           /* proxy with offset? */
573 >                if (nd.thick != 0) {            /* proxy with offset? */
574                          VCOPY(vtmp, r->rop);
575 <                        VSUM(r->rop, vtmp, r->ron, -nd.thick);
575 >                        VSUM(r->rop, vtmp, r->ron, nd.thick);
576                          multambient(ctmp, r, bnorm);
577                          VCOPY(r->rop, vtmp);
578                  } else
579                          multambient(ctmp, r, bnorm);
580                  addcolor(r->rcol, ctmp);
581 <                if (r->rod > .0)
581 >                if (hitfront)
582                          flipsurface(r);
583          }
584                                                  /* add direct component */
585 <        if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL)) {
585 >        if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) &
586 >                                        (nd.sd->tb == NULL)) {
587                  direct(r, dir_brdf, &nd);       /* reflection only */
588 <        } else if (nd.thick == .0) {
588 >        } else if (nd.thick == 0) {
589                  direct(r, dir_bsdf, &nd);       /* thin surface scattering */
590          } else {
591                  direct(r, dir_brdf, &nd);       /* reflection first */
592                  VCOPY(vtmp, r->rop);            /* offset for transmitted */
593                  VSUM(r->rop, vtmp, r->ron, -nd.thick);
594 <                direct(r, dir_btdf, &nd);
594 >                direct(r, dir_btdf, &nd);       /* separate transmission */
595                  VCOPY(r->rop, vtmp);
596          }
597                                                  /* clean up */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines