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.13 by greg, Sun Aug 21 21:24:30 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.
# 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, int domax)
86 > bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
87   {
87        double  sr_psa = ndp->sr_vpsa[domax];
88
88          VCOPY(vres, ndp->vray);
89          if (specjitter < 1.)
90                  sr_psa *= specjitter;
# Line 103 | Line 102 | direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, B
102          int     nsamp, ok = 0;
103          FVECT   vsrc, vsmp, vjit;
104          double  tomega;
105 <        double  sf, sd[2];
105 >        double  sf, tsr, sd[2];
106          COLOR   csmp;
107          SDValue sv;
108          SDError ec;
# Line 111 | Line 110 | direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, B
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*(1./PI))
122 >                if (dx*dx + dy*dy <= omega+tomega)
123                          return(0);
124          }
125 <                                        /* get local BSDF resolution */
126 <        ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
127 <        if (ec)
128 <                goto baderror;
129 <                                        /* assign number of samples */
127 <        if (tomega <= omega*.02)
128 <                nsamp = 50;
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 = 2.*omega/tomega + 1.;
132 <        sf = sqrt(omega);
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) {
# Line 141 | Line 144 | direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, B
144                                  continue;
145                          }
146                  }
147 <                bsdf_jitter(vjit, ndp, 0);
147 >                bsdf_jitter(vjit, ndp, tsr);
148                                          /* compute BSDF */
149                  ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
150                  if (ec)
# Line 157 | Line 160 | direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, B
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) */
# Line 197 | Line 201 | dir_bsdf(
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           */
# Line 252 | Line 258 | dir_brdf(
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           */
# Line 301 | Line 309 | dir_btdf(
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           */
# Line 327 | Line 337 | sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usep
337                                                  /* multiple samples? */
338          if (specjitter > 1.5) {
339                  nstarget = specjitter*ndp->pr->rweight + .5;
340 <                if (nstarget < 1)
331 <                        nstarget = 1;
340 >                nstarget += !nstarget;
341          }
342                                                  /* run through our samples */
343          for (nsent = 0; nsent < nstarget; nsent++) {
344 <                if (nstarget == 1)              /* stratify random variable */
344 >                if (nstarget == 1) {            /* stratify random variable */
345                          xrand = urand(ilhash(dimlist,ndims)+samplendx);
346 <                else
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, 0);
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));
# Line 377 | 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;
# Line 398 | Line 413 | sample_sdf(BSDFDAT *ndp, int sflags)
413                          FVECT   vjit;
414                          double  d;
415                          COLOR   ctmp;
416 <                        bsdf_jitter(vjit, ndp, 1);
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);
# Line 438 | Line 453 | m_bsdf(OBJREC *m, RAY *r)
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 ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
# Line 448 | Line 464 | m_bsdf(OBJREC *m, RAY *r)
464                          raytrans(r);            /* pass-through */
465                  return(1);                      /* or shadow */
466          }
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)) {
474 >                                (nd.thick > 0) ^ hitfront)) {
475                  raytrans(r);                    /* hide our proxy */
476                  return(1);
477          }
# Line 465 | 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 */
469 <                        if (!backvis && (nd.sd->rb == NULL) &
470 <                                                (nd.sd->tf == NULL)) {
471 <                                SDfreeCache(nd.sd);
472 <                                raytrans(r);
473 <                                return(1);
474 <                        }
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 497 | 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);
# Line 512 | Line 531 | m_bsdf(OBJREC *m, RAY *r)
531          }
532          if (!ec)
533                  ec = SDinvXform(nd.fromloc, nd.toloc);
515                                                /* determine BSDF resolution */
516        if (!ec)
517                ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL,
518                                                SDqueryMin+SDqueryMax, nd.sd);
534          if (ec) {
535 <                objerror(m, WARNING, transSDError(ec));
521 <                SDfreeCache(nd.sd);
535 >                objerror(m, WARNING, "Illegal orientation vector");
536                  return(1);
537          }
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 */
# Line 554 | Line 573 | m_bsdf(OBJREC *m, RAY *r)
573                  bnorm[2] = -nd.pnorm[2];
574                  if (nd.thick != 0) {            /* proxy with offset? */
575                          VCOPY(vtmp, r->rop);
576 <                        VSUM(r->rop, vtmp, r->ron, -nd.thick);
576 >                        VSUM(r->rop, vtmp, r->ron, nd.thick);
577                          multambient(ctmp, r, bnorm);
578                          VCOPY(r->rop, vtmp);
579                  } else
# Line 564 | Line 583 | m_bsdf(OBJREC *m, RAY *r)
583                          flipsurface(r);
584          }
585                                                  /* add direct component */
586 <        if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL)) {
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 */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines