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.7 by greg, Tue Feb 22 05:41:02 2011 UTC vs.
Revision 2.24 by greg, Thu Jul 4 15:14:45 2013 UTC

# Line 50 | Line 50 | static const char RCSid[] = "$Id$";
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 67 | 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 82 | 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   {
87        double  sr_psa = ndp->sr_vpsa;
88
87          VCOPY(vres, ndp->vray);
88          if (specjitter < 1.)
89                  sr_psa *= specjitter;
# Line 98 | Line 96 | bsdf_jitter(FVECT vres, BSDFDAT *ndp)
96  
97   /* Evaluate BSDF for direct component, returning true if OK to proceed */
98   static int
99 < direct_bsdf_OK(COLOR cval, FVECT ldir, BSDFDAT *ndp)
99 > direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
100   {
101 <        FVECT   vsrc, vjit;
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 <                                        /* jitter query direction */
113 <        bsdf_jitter(vjit, ndp);
114 <                                        /* avoid indirect over-counting */
115 <        if (ndp->thick != .0 && ndp->pr->crtype & (SPECULAR|AMBIENT) &&
116 <                                vsrc[2] > .0 ^ vjit[2] > .0) {
117 <                double  dx = vsrc[0] + vjit[0];
118 <                double  dy = vsrc[1] + vjit[1];
119 <                if (dx*dx + dy*dy <= ndp->sr_vpsa*ndp->sr_vpsa)
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 <        ec = SDevalBSDF(&sv, vjit, vsrc, ndp->sd);
125 <        if (ec)
126 <                objerror(ndp->mp, USER, transSDError(ec));
127 <
128 <        if (sv.cieY <= FTINY)           /* not worth using? */
129 <                return(0);
130 <                                        /* else we're good to go */
131 <        cvt_sdcolor(cval, &sv);
132 <        return(1);
124 >        sf = specjitter * ndp->pr->rweight;
125 >        if (tomega <= .0)
126 >                nsamp = 1;
127 >        else if (25.*tomega <= omega)
128 >                nsamp = 100.*sf + .5;
129 >        else
130 >                nsamp = 4.*sf*omega/tomega + .5;
131 >        nsamp += !nsamp;
132 >        setcolor(cval, .0, .0, .0);     /* sample our source area */
133 >        sf = sqrt(omega);
134 >        tsr = sqrt(tomega);
135 >        for (i = nsamp; i--; ) {
136 >                VCOPY(vsmp, vsrc);      /* jitter query directions */
137 >                if (nsamp > 1) {
138 >                        multisamp(sd, 2, (i + frandom())/(double)nsamp);
139 >                        vsmp[0] += (sd[0] - .5)*sf;
140 >                        vsmp[1] += (sd[1] - .5)*sf;
141 >                        if (normalize(vsmp) == 0) {
142 >                                --nsamp;
143 >                                continue;
144 >                        }
145 >                }
146 >                bsdf_jitter(vjit, ndp, tsr);
147 >                                        /* compute BSDF */
148 >                ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
149 >                if (ec)
150 >                        goto baderror;
151 >                if (sv.cieY <= FTINY)   /* worth using? */
152 >                        continue;
153 >                cvt_sdcolor(csmp, &sv);
154 >                addcolor(cval, csmp);   /* average it in */
155 >                ++ok;
156 >        }
157 >        sf = 1./(double)nsamp;
158 >        scalecolor(cval, sf);
159 >        return(ok);
160 > baderror:
161 >        objerror(ndp->mp, USER, transSDError(ec));
162 >        return(0);                      /* gratis return */
163   }
164  
165   /* Compute source contribution for BSDF (reflected & transmitted) */
# Line 147 | Line 182 | dir_bsdf(
182          if ((-FTINY <= ldot) & (ldot <= FTINY))
183                  return;
184  
185 <        if (ldot > .0 && bright(np->rdiff) > FTINY) {
185 >        if (ldot > 0 && bright(np->rdiff) > FTINY) {
186                  /*
187                   *  Compute added diffuse reflected component.
188                   */
# Line 156 | Line 191 | dir_bsdf(
191                  scalecolor(ctmp, dtmp);
192                  addcolor(cval, ctmp);
193          }
194 <        if (ldot < .0 && bright(np->tdiff) > FTINY) {
194 >        if (ldot < 0 && bright(np->tdiff) > FTINY) {
195                  /*
196                   *  Compute added diffuse transmission.
197                   */
# Line 168 | Line 203 | dir_bsdf(
203          /*
204           *  Compute scattering coefficient using BSDF.
205           */
206 <        if (!direct_bsdf_OK(ctmp, ldir, np))
206 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
207                  return;
208 <        if (ldot > .0) {                /* pattern only diffuse reflection */
208 >        if (ldot > 0) {         /* pattern only diffuse reflection */
209                  COLOR   ctmp1, ctmp2;
210 <                dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY
210 >                dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
211                                          : np->sd->rLambBack.cieY;
212                                          /* diffuse fraction */
213                  dtmp /= PI * bright(ctmp);
# Line 223 | Line 258 | dir_brdf(
258          /*
259           *  Compute reflection coefficient using BSDF.
260           */
261 <        if (!direct_bsdf_OK(ctmp, ldir, np))
261 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
262                  return;
263                                          /* pattern only diffuse reflection */
264 <        dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY
264 >        dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
265                                  : np->sd->rLambBack.cieY;
266          dtmp /= PI * bright(ctmp);      /* diffuse fraction */
267          copycolor(ctmp2, np->pr->pcol);
# Line 272 | Line 307 | dir_btdf(
307          /*
308           *  Compute scattering coefficient using BSDF.
309           */
310 <        if (!direct_bsdf_OK(ctmp, ldir, np))
310 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
311                  return;
312                                          /* full pattern on transmission */
313          multcolor(ctmp, np->pr->pcol);
# Line 286 | Line 321 | static int
321   sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
322   {
323          int     nstarget = 1;
324 <        int     nsent = 0;
324 >        int     nsent;
325          SDError ec;
326          SDValue bsv;
327 <        double  sthick;
328 <        FVECT   vjit, vsmp;
327 >        double  xrand;
328 >        FVECT   vsmp;
329          RAY     sr;
295        int     ntrials;
330                                                  /* multiple samples? */
331          if (specjitter > 1.5) {
332                  nstarget = specjitter*ndp->pr->rweight + .5;
333 <                if (nstarget < 1)
300 <                        nstarget = 1;
333 >                nstarget += !nstarget;
334          }
335 <                                                /* run through our trials */
336 <        for (ntrials = 0; nsent < nstarget && ntrials < 9*nstarget; ntrials++) {
337 <                SDerrorDetail[0] = '\0';
338 <                                                /* sample direction & coef. */
339 <                bsdf_jitter(vjit, ndp);
340 <                ec = SDsampComponent(&bsv, vsmp, vjit, ntrials ? frandom()
341 <                                : urand(ilhash(dimlist,ndims)+samplendx), dcp);
335 >                                                /* run through our samples */
336 >        for (nsent = 0; nsent < nstarget; nsent++) {
337 >                if (nstarget == 1) {            /* stratify random variable */
338 >                        xrand = urand(ilhash(dimlist,ndims)+samplendx);
339 >                        if (specjitter < 1.)
340 >                                xrand = .5 + specjitter*(xrand-.5);
341 >                } else {
342 >                        xrand = (nsent + frandom())/(double)nstarget;
343 >                }
344 >                SDerrorDetail[0] = '\0';        /* sample direction & coef. */
345 >                bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]);
346 >                ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
347                  if (ec)
348                          objerror(ndp->mp, USER, transSDError(ec));
349 <                                                /* zero component? */
312 <                if (bsv.cieY <= FTINY)
349 >                if (bsv.cieY <= FTINY)          /* zero component? */
350                          break;
351                                                  /* map vector to world */
352                  if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
353                          break;
317                                                /* unintentional penetration? */
318                if (DOT(sr.rdir, ndp->pr->ron) > .0 ^ vsmp[2] > .0)
319                        continue;
354                                                  /* spawn a specular ray */
355                  if (nstarget > 1)
356                          bsv.cieY /= (double)nstarget;
357 <                cvt_sdcolor(sr.rcoef, &bsv);    /* use color */
358 <                if (usepat)                     /* pattern on transmission */
357 >                cvt_sdcolor(sr.rcoef, &bsv);    /* use sample color */
358 >                if (usepat)                     /* apply pattern? */
359                          multcolor(sr.rcoef, ndp->pr->pcol);
360                  if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
361 <                        if (maxdepth  > 0)
361 >                        if (maxdepth > 0)
362                                  break;
363 <                        ++nsent;                /* Russian roulette victim */
330 <                        continue;
363 >                        continue;               /* Russian roulette victim */
364                  }
365                                                  /* need to offset origin? */
366 <                if (ndp->thick != .0 && ndp->pr->rod > .0 ^ vsmp[2] > .0)
366 >                if (ndp->thick != 0 && ndp->pr->rod > 0 ^ vsmp[2] > 0)
367                          VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
368                  rayvalue(&sr);                  /* send & evaluate sample */
369                  multcolor(sr.rcol, sr.rcoef);
370                  addcolor(ndp->pr->rcol, sr.rcol);
338                ++nsent;
371          }
372          return(nsent);
373   }
# Line 350 | Line 382 | sample_sdf(BSDFDAT *ndp, int sflags)
382  
383          if (sflags == SDsampSpT) {
384                  unsc = ndp->tunsamp;
385 <                dfp = ndp->sd->tf;
385 >                if (ndp->pr->rod > 0)
386 >                        dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
387 >                else
388 >                        dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
389                  cvt_sdcolor(unsc, &ndp->sd->tLamb);
390          } else /* sflags == SDsampSpR */ {
391                  unsc = ndp->runsamp;
392 <                if (ndp->pr->rod > .0) {
392 >                if (ndp->pr->rod > 0) {
393                          dfp = ndp->sd->rf;
394                          cvt_sdcolor(unsc, &ndp->sd->rLambFront);
395                  } else {
# Line 371 | Line 406 | sample_sdf(BSDFDAT *ndp, int sflags)
406                          FVECT   vjit;
407                          double  d;
408                          COLOR   ctmp;
409 <                        bsdf_jitter(vjit, ndp);
409 >                        bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
410                          d = SDdirectHemi(vjit, sflags, ndp->sd);
411                          if (sflags == SDsampSpT) {
412                                  copycolor(ctmp, ndp->pr->pcol);
# Line 408 | Line 443 | m_bsdf(OBJREC *m, RAY *r)
443                                  (m->oargs.nfargs % 3))
444                  objerror(m, USER, "bad # arguments");
445                                                  /* record surface struck */
446 <        hitfront = (r->rod > .0);
446 >        hitfront = (r->rod > 0);
447                                                  /* load cal file */
448          mf = getfunc(m, 5, 0x1d, 1);
449                                                  /* get thickness */
# Line 417 | Line 452 | m_bsdf(OBJREC *m, RAY *r)
452                  nd.thick = .0;
453                                                  /* check shadow */
454          if (r->crtype & SHADOW) {
455 <                if (nd.thick != .0)
455 >                if (nd.thick != 0)
456                          raytrans(r);            /* pass-through */
457                  return(1);                      /* or shadow */
458          }
459                                                  /* check other rays to pass */
460 <        if (nd.thick != .0 && (!(r->crtype & (SPECULAR|AMBIENT)) ||
461 <                                nd.thick > .0 ^ hitfront)) {
460 >        if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) ||
461 >                                nd.thick > 0 ^ hitfront)) {
462                  raytrans(r);                    /* hide our proxy */
463                  return(1);
464          }
# Line 440 | Line 475 | m_bsdf(OBJREC *m, RAY *r)
475          } else {
476                  if (m->oargs.nfargs < 6) {      /* check invisible backside */
477                          if (!backvis && (nd.sd->rb == NULL) &
478 <                                                (nd.sd->tf == NULL)) {
478 >                                        (nd.sd->tb == NULL)) {
479                                  SDfreeCache(nd.sd);
480                                  raytrans(r);
481                                  return(1);
# Line 470 | Line 505 | m_bsdf(OBJREC *m, RAY *r)
505          upvec[1] = evalue(mf->ep[2]);
506          upvec[2] = evalue(mf->ep[3]);
507                                                  /* return to world coords */
508 <        if (mf->f != &unitxf) {
509 <                multv3(upvec, upvec, mf->f->xfm);
510 <                nd.thick *= mf->f->sca;
508 >        if (mf->fxp != &unitxf) {
509 >                multv3(upvec, upvec, mf->fxp->xfm);
510 >                nd.thick *= mf->fxp->sca;
511          }
512 +        if (r->rox != NULL) {
513 +                multv3(upvec, upvec, r->rox->f.xfm);
514 +                nd.thick *= r->rox->f.sca;
515 +        }
516          raynormal(nd.pnorm, r);
517                                                  /* compute local BSDF xform */
518          ec = SDcompXform(nd.toloc, nd.pnorm, upvec);
# Line 485 | Line 524 | m_bsdf(OBJREC *m, RAY *r)
524          }
525          if (!ec)
526                  ec = SDinvXform(nd.fromloc, nd.toloc);
527 <                                                /* determine BSDF resolution */
528 <        if (!ec)
490 <                ec = SDsizeBSDF(&nd.sr_vpsa, nd.vray, SDqueryMin, nd.sd);
491 <        if (!ec)
492 <                nd.sr_vpsa = sqrt(nd.sr_vpsa);
493 <        else {
494 <                objerror(m, WARNING, transSDError(ec));
495 <                SDfreeCache(nd.sd);
527 >        if (ec) {
528 >                objerror(m, WARNING, "Illegal orientation vector");
529                  return(1);
530          }
531 +                                                /* determine BSDF resolution */
532 +        ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin+SDqueryMax, nd.sd);
533 +        if (ec)
534 +                objerror(m, USER, transSDError(ec));
535 +
536 +        nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]);
537 +        nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]);
538          if (!hitfront) {                        /* perturb normal towards hit */
539                  nd.pnorm[0] = -nd.pnorm[0];
540                  nd.pnorm[1] = -nd.pnorm[1];
# Line 524 | Line 564 | m_bsdf(OBJREC *m, RAY *r)
564                  bnorm[0] = -nd.pnorm[0];
565                  bnorm[1] = -nd.pnorm[1];
566                  bnorm[2] = -nd.pnorm[2];
567 <                if (nd.thick != .0) {           /* proxy with offset? */
567 >                if (nd.thick != 0) {            /* proxy with offset? */
568                          VCOPY(vtmp, r->rop);
569 <                        VSUM(r->rop, vtmp, r->ron, -nd.thick);
569 >                        VSUM(r->rop, vtmp, r->ron, nd.thick);
570                          multambient(ctmp, r, bnorm);
571                          VCOPY(r->rop, vtmp);
572                  } else
# Line 536 | Line 576 | m_bsdf(OBJREC *m, RAY *r)
576                          flipsurface(r);
577          }
578                                                  /* add direct component */
579 <        if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL)) {
579 >        if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) &
580 >                                        (nd.sd->tb == NULL)) {
581                  direct(r, dir_brdf, &nd);       /* reflection only */
582 <        } else if (nd.thick == .0) {
582 >        } else if (nd.thick == 0) {
583                  direct(r, dir_bsdf, &nd);       /* thin surface scattering */
584          } else {
585                  direct(r, dir_brdf, &nd);       /* reflection first */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines