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.15 by greg, Mon Aug 22 16:00:47 2011 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 96 | Line 94 | bsdf_jitter(FVECT vres, BSDFDAT *ndp)
94          normalize(vres);
95   }
96  
97 + /* Evaluate BSDF for direct component, returning true if OK to proceed */
98 + static int
99 + direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
100 + {
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 +                                        /* check indirect over-counting */
113 +        if (ndp->thick != 0 && ndp->pr->crtype & (SPECULAR|AMBIENT)
114 +                                && vsrc[2] > 0 ^ ndp->vray[2] > 0) {
115 +                double  dx = vsrc[0] + ndp->vray[0];
116 +                double  dy = vsrc[1] + ndp->vray[1];
117 +                if (dx*dx + dy*dy <= omega*(1./PI))
118 +                        return(0);
119 +        }
120 +                                        /* assign number of samples */
121 +        ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
122 +        if (ec)
123 +                goto baderror;
124 +        sf = specjitter * ndp->pr->rweight;
125 +        if (25.*tomega <= omega)
126 +                nsamp = 100.*sf + .5;
127 +        else
128 +                nsamp = 4.*sf*omega/tomega + .5;
129 +        nsamp += !nsamp;
130 +        setcolor(cval, .0, .0, .0);     /* sample our source area */
131 +        sf = sqrt(omega);
132 +        tsr = sqrt(tomega);
133 +        for (i = nsamp; i--; ) {
134 +                VCOPY(vsmp, vsrc);      /* jitter query directions */
135 +                if (nsamp > 1) {
136 +                        multisamp(sd, 2, (i + frandom())/(double)nsamp);
137 +                        vsmp[0] += (sd[0] - .5)*sf;
138 +                        vsmp[1] += (sd[1] - .5)*sf;
139 +                        if (normalize(vsmp) == 0) {
140 +                                --nsamp;
141 +                                continue;
142 +                        }
143 +                }
144 +                bsdf_jitter(vjit, ndp, tsr);
145 +                                        /* compute BSDF */
146 +                ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
147 +                if (ec)
148 +                        goto baderror;
149 +                if (sv.cieY <= FTINY)   /* worth using? */
150 +                        continue;
151 +                cvt_sdcolor(csmp, &sv);
152 +                addcolor(cval, csmp);   /* average it in */
153 +                ++ok;
154 +        }
155 +        sf = 1./(double)nsamp;
156 +        scalecolor(cval, sf);
157 +        return(ok);
158 + baderror:
159 +        objerror(ndp->mp, USER, transSDError(ec));
160 + }
161 +
162   /* Compute source contribution for BSDF (reflected & transmitted) */
163   static void
164   dir_bsdf(
# Line 106 | Line 169 | dir_bsdf(
169   )
170   {
171          BSDFDAT         *np = (BSDFDAT *)nnp;
109        SDError         ec;
110        SDValue         sv;
111        FVECT           vsrc;
112        FVECT           vjit;
172          double          ldot;
173          double          dtmp;
174          COLOR           ctmp;
# Line 120 | Line 179 | dir_bsdf(
179          if ((-FTINY <= ldot) & (ldot <= FTINY))
180                  return;
181  
182 <        if (ldot > .0 && bright(np->rdiff) > FTINY) {
182 >        if (ldot > 0 && bright(np->rdiff) > FTINY) {
183                  /*
184                   *  Compute added diffuse reflected component.
185                   */
# Line 129 | Line 188 | dir_bsdf(
188                  scalecolor(ctmp, dtmp);
189                  addcolor(cval, ctmp);
190          }
191 <        if (ldot < .0 && bright(np->tdiff) > FTINY) {
191 >        if (ldot < 0 && bright(np->tdiff) > FTINY) {
192                  /*
193                   *  Compute added diffuse transmission.
194                   */
# Line 141 | Line 200 | dir_bsdf(
200          /*
201           *  Compute scattering coefficient using BSDF.
202           */
203 <        if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone)
203 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
204                  return;
205 <        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 */
205 >        if (ldot > 0) {         /* pattern only diffuse reflection */
206                  COLOR   ctmp1, ctmp2;
207 <                dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY
207 >                dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
208                                          : np->sd->rLambBack.cieY;
209 <                dtmp /= PI * sv.cieY;   /* diffuse fraction */
209 >                                        /* diffuse fraction */
210 >                dtmp /= PI * bright(ctmp);
211                  copycolor(ctmp2, np->pr->pcol);
212                  scalecolor(ctmp2, dtmp);
213                  setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
# Line 180 | Line 232 | dir_brdf(
232   )
233   {
234          BSDFDAT         *np = (BSDFDAT *)nnp;
183        SDError         ec;
184        SDValue         sv;
185        FVECT           vsrc;
186        FVECT           vjit;
235          double          ldot;
236          double          dtmp;
237          COLOR           ctmp, ctmp1, ctmp2;
# Line 207 | Line 255 | dir_brdf(
255          /*
256           *  Compute reflection coefficient using BSDF.
257           */
258 <        if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone)
258 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
259                  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);
260                                          /* pattern only diffuse reflection */
261 <        dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY
261 >        dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
262                                  : np->sd->rLambBack.cieY;
263 <        dtmp /= PI * sv.cieY;           /* diffuse fraction */
263 >        dtmp /= PI * bright(ctmp);      /* diffuse fraction */
264          copycolor(ctmp2, np->pr->pcol);
265          scalecolor(ctmp2, dtmp);
266          setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
# Line 241 | Line 281 | dir_btdf(
281   )
282   {
283          BSDFDAT         *np = (BSDFDAT *)nnp;
244        SDError         ec;
245        SDValue         sv;
246        FVECT           vsrc;
247        FVECT           vjit;
284          double          ldot;
285          double          dtmp;
286          COLOR           ctmp;
# Line 268 | Line 304 | dir_btdf(
304          /*
305           *  Compute scattering coefficient using BSDF.
306           */
307 <        if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone)
307 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
308                  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);
309                                          /* full pattern on transmission */
310          multcolor(ctmp, np->pr->pcol);
311          dtmp = -ldot * omega;
# Line 290 | Line 318 | static int
318   sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
319   {
320          int     nstarget = 1;
321 <        int     nsent = 0;
321 >        int     nsent;
322          SDError ec;
323          SDValue bsv;
324 <        double  sthick;
325 <        FVECT   vjit, vsmp;
324 >        double  xrand;
325 >        FVECT   vsmp;
326          RAY     sr;
299        int     ntrials;
327                                                  /* multiple samples? */
328          if (specjitter > 1.5) {
329                  nstarget = specjitter*ndp->pr->rweight + .5;
330 <                if (nstarget < 1)
304 <                        nstarget = 1;
330 >                nstarget += !nstarget;
331          }
332 <                                                /* run through our trials */
333 <        for (ntrials = 0; nsent < nstarget && ntrials < 9*nstarget; ntrials++) {
334 <                SDerrorDetail[0] = '\0';
335 <                                                /* sample direction & coef. */
336 <                bsdf_jitter(vjit, ndp);
337 <                ec = SDsampComponent(&bsv, vsmp, vjit, ntrials ? frandom()
338 <                                : urand(ilhash(dimlist,ndims)+samplendx), dcp);
332 >                                                /* run through our samples */
333 >        for (nsent = 0; nsent < nstarget; nsent++) {
334 >                if (nstarget == 1) {            /* stratify random variable */
335 >                        xrand = urand(ilhash(dimlist,ndims)+samplendx);
336 >                        if (specjitter < 1.)
337 >                                xrand = .5 + specjitter*(xrand-.5);
338 >                } else {
339 >                        xrand = (nsent + frandom())/(double)nstarget;
340 >                }
341 >                SDerrorDetail[0] = '\0';        /* sample direction & coef. */
342 >                bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]);
343 >                ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
344                  if (ec)
345                          objerror(ndp->mp, USER, transSDError(ec));
346 <                                                /* zero component? */
316 <                if (bsv.cieY <= FTINY)
346 >                if (bsv.cieY <= FTINY)          /* zero component? */
347                          break;
348                                                  /* map vector to world */
349                  if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
350                          break;
321                                                /* unintentional penetration? */
322                if (DOT(sr.rdir, ndp->pr->ron) > .0 ^ vsmp[2] > .0)
323                        continue;
351                                                  /* spawn a specular ray */
352                  if (nstarget > 1)
353                          bsv.cieY /= (double)nstarget;
354 <                cvt_sdcolor(sr.rcoef, &bsv);    /* use color */
355 <                if (usepat)                     /* pattern on transmission */
354 >                cvt_sdcolor(sr.rcoef, &bsv);    /* use sample color */
355 >                if (usepat)                     /* apply pattern? */
356                          multcolor(sr.rcoef, ndp->pr->pcol);
357                  if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
358 <                        if (maxdepth  > 0)
358 >                        if (maxdepth > 0)
359                                  break;
360 <                        ++nsent;                /* Russian roulette victim */
334 <                        continue;
360 >                        continue;               /* Russian roulette victim */
361                  }
362                                                  /* need to offset origin? */
363 <                if (ndp->thick != .0 && ndp->pr->rod > .0 ^ vsmp[2] > .0)
363 >                if (ndp->thick != 0 && ndp->pr->rod > 0 ^ vsmp[2] > 0)
364                          VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
365                  rayvalue(&sr);                  /* send & evaluate sample */
366                  multcolor(sr.rcol, sr.rcoef);
367                  addcolor(ndp->pr->rcol, sr.rcol);
342                ++nsent;
368          }
369          return(nsent);
370   }
# Line 358 | Line 383 | sample_sdf(BSDFDAT *ndp, int sflags)
383                  cvt_sdcolor(unsc, &ndp->sd->tLamb);
384          } else /* sflags == SDsampSpR */ {
385                  unsc = ndp->runsamp;
386 <                if (ndp->pr->rod > .0) {
386 >                if (ndp->pr->rod > 0) {
387                          dfp = ndp->sd->rf;
388                          cvt_sdcolor(unsc, &ndp->sd->rLambFront);
389                  } else {
# Line 375 | Line 400 | sample_sdf(BSDFDAT *ndp, int sflags)
400                          FVECT   vjit;
401                          double  d;
402                          COLOR   ctmp;
403 <                        bsdf_jitter(vjit, ndp);
403 >                        bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
404                          d = SDdirectHemi(vjit, sflags, ndp->sd);
405                          if (sflags == SDsampSpT) {
406                                  copycolor(ctmp, ndp->pr->pcol);
# Line 401 | Line 426 | sample_sdf(BSDFDAT *ndp, int sflags)
426   int
427   m_bsdf(OBJREC *m, RAY *r)
428   {
429 +        int     hitfront;
430          COLOR   ctmp;
431          SDError ec;
432          FVECT   upvec, vtmp;
# Line 410 | Line 436 | m_bsdf(OBJREC *m, RAY *r)
436          if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) |
437                                  (m->oargs.nfargs % 3))
438                  objerror(m, USER, "bad # arguments");
439 <
439 >                                                /* record surface struck */
440 >        hitfront = (r->rod > 0);
441                                                  /* load cal file */
442          mf = getfunc(m, 5, 0x1d, 1);
443                                                  /* get thickness */
# Line 419 | Line 446 | m_bsdf(OBJREC *m, RAY *r)
446                  nd.thick = .0;
447                                                  /* check shadow */
448          if (r->crtype & SHADOW) {
449 <                if (nd.thick != .0)
449 >                if (nd.thick != 0)
450                          raytrans(r);            /* pass-through */
451                  return(1);                      /* or shadow */
452          }
453                                                  /* check other rays to pass */
454          if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) ||
455 <                                nd.thick > .0 ^ r->rod > .0)) {
455 >                                nd.thick > 0 ^ hitfront)) {
456                  raytrans(r);                    /* hide our proxy */
457                  return(1);
458          }
459                                                  /* get BSDF data */
460          nd.sd = loadBSDF(m->oargs.sarg[1]);
461                                                  /* diffuse reflectance */
462 <        if (r->rod > .0) {
462 >        if (hitfront) {
463                  if (m->oargs.nfargs < 3)
464                          setcolor(nd.rdiff, .0, .0, .0);
465                  else
# Line 489 | Line 516 | m_bsdf(OBJREC *m, RAY *r)
516                  ec = SDinvXform(nd.fromloc, nd.toloc);
517                                                  /* determine BSDF resolution */
518          if (!ec)
519 <                ec = SDsizeBSDF(&nd.sr_vpsa, nd.vray, SDqueryMin, nd.sd);
520 <        if (!ec)
521 <                nd.sr_vpsa = sqrt(nd.sr_vpsa);
495 <        else {
519 >                ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL,
520 >                                                SDqueryMin+SDqueryMax, nd.sd);
521 >        if (ec) {
522                  objerror(m, WARNING, transSDError(ec));
523                  SDfreeCache(nd.sd);
524                  return(1);
525          }
526 <        if (r->rod < .0) {                      /* perturb normal towards hit */
526 >        nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]);
527 >        nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]);
528 >        if (!hitfront) {                        /* perturb normal towards hit */
529                  nd.pnorm[0] = -nd.pnorm[0];
530                  nd.pnorm[1] = -nd.pnorm[1];
531                  nd.pnorm[2] = -nd.pnorm[2];
# Line 510 | Line 538 | m_bsdf(OBJREC *m, RAY *r)
538          copycolor(ctmp, nd.rdiff);
539          addcolor(ctmp, nd.runsamp);
540          if (bright(ctmp) > FTINY) {             /* ambient from reflection */
541 <                if (r->rod < .0)
541 >                if (!hitfront)
542                          flipsurface(r);
543                  multambient(ctmp, r, nd.pnorm);
544                  addcolor(r->rcol, ctmp);
545 <                if (r->rod < .0)
545 >                if (!hitfront)
546                          flipsurface(r);
547          }
548          copycolor(ctmp, nd.tdiff);
549          addcolor(ctmp, nd.tunsamp);
550          if (bright(ctmp) > FTINY) {             /* ambient from other side */
551                  FVECT  bnorm;
552 <                if (r->rod > .0)
552 >                if (hitfront)
553                          flipsurface(r);
554                  bnorm[0] = -nd.pnorm[0];
555                  bnorm[1] = -nd.pnorm[1];
556                  bnorm[2] = -nd.pnorm[2];
557 <                if (nd.thick != .0) {           /* proxy with offset? */
557 >                if (nd.thick != 0) {            /* proxy with offset? */
558                          VCOPY(vtmp, r->rop);
559                          VSUM(r->rop, vtmp, r->ron, -nd.thick);
560                          multambient(ctmp, r, bnorm);
# Line 534 | Line 562 | m_bsdf(OBJREC *m, RAY *r)
562                  } else
563                          multambient(ctmp, r, bnorm);
564                  addcolor(r->rcol, ctmp);
565 <                if (r->rod > .0)
565 >                if (hitfront)
566                          flipsurface(r);
567          }
568                                                  /* add direct component */
569          if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL)) {
570                  direct(r, dir_brdf, &nd);       /* reflection only */
571 <        } else if (nd.thick == .0) {
571 >        } else if (nd.thick == 0) {
572                  direct(r, dir_bsdf, &nd);       /* thin surface scattering */
573          } else {
574                  direct(r, dir_brdf, &nd);       /* reflection first */
575                  VCOPY(vtmp, r->rop);            /* offset for transmitted */
576                  VSUM(r->rop, vtmp, r->ron, -nd.thick);
577 <                direct(r, dir_btdf, &nd);
577 >                direct(r, dir_btdf, &nd);       /* separate transmission */
578                  VCOPY(r->rop, vtmp);
579          }
580                                                  /* clean up */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines