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.14 by greg, Sun Aug 21 22:38:12 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, int domax)
86   {
87 <        double  sr_psa = ndp->sr_vpsa;
87 >        double  sr_psa = ndp->sr_vpsa[domax];
88  
89          VCOPY(vres, ndp->vray);
90          if (specjitter < 1.)
# Line 98 | Line 98 | bsdf_jitter(FVECT vres, BSDFDAT *ndp)
98  
99   /* Evaluate BSDF for direct component, returning true if OK to proceed */
100   static int
101 < direct_bsdf_OK(COLOR cval, FVECT ldir, BSDFDAT *ndp)
101 > direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
102   {
103 <        FVECT   vsrc, vjit;
103 >        int     nsamp = 1, ok = 0;
104 >        FVECT   vsrc, vsmp, vjit;
105 >        double  tomega;
106 >        double  sf, sd[2];
107 >        COLOR   csmp;
108          SDValue sv;
109          SDError ec;
110 +        int     i;
111                                          /* transform source direction */
112          if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
113                  return(0);
114 <                                        /* jitter query direction */
115 <        bsdf_jitter(vjit, ndp);
116 <                                        /* avoid indirect over-counting */
117 <        if (ndp->thick != .0 && ndp->pr->crtype & (SPECULAR|AMBIENT) &&
118 <                                vsrc[2] > .0 ^ vjit[2] > .0) {
119 <                double  dx = vsrc[0] + vjit[0];
115 <                double  dy = vsrc[1] + vjit[1];
116 <                if (dx*dx + dy*dy <= ndp->sr_vpsa*ndp->sr_vpsa)
114 >                                        /* check indirect over-counting */
115 >        if (ndp->thick != 0 && ndp->pr->crtype & (SPECULAR|AMBIENT)
116 >                                && vsrc[2] > 0 ^ ndp->vray[2] > 0) {
117 >                double  dx = vsrc[0] + ndp->vray[0];
118 >                double  dy = vsrc[1] + ndp->vray[1];
119 >                if (dx*dx + dy*dy <= omega*(1./PI))
120                          return(0);
121          }
122 <        ec = SDevalBSDF(&sv, vjit, vsrc, ndp->sd);
123 <        if (ec)
124 <                objerror(ndp->mp, USER, transSDError(ec));
125 <
126 <        if (sv.cieY <= FTINY)           /* not worth using? */
127 <                return(0);
128 <                                        /* else we're good to go */
129 <        cvt_sdcolor(cval, &sv);
130 <        return(1);
122 >        if (specjitter > FTINY) {       /* assign number of samples */
123 >                ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
124 >                if (ec)
125 >                        goto baderror;
126 >                sf = specjitter * ndp->pr->rweight;
127 >                if (tomega <= omega*.01)
128 >                        nsamp = 100.*sf + .5;
129 >                else
130 >                        nsamp = 4.*sf*omega/tomega + .5;
131 >                nsamp += !nsamp;
132 >        }
133 >        sf = sqrt(omega);
134 >        setcolor(cval, .0, .0, .0);     /* sample our source area */
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, 0);
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   }
163  
164   /* Compute source contribution for BSDF (reflected & transmitted) */
# Line 147 | Line 181 | dir_bsdf(
181          if ((-FTINY <= ldot) & (ldot <= FTINY))
182                  return;
183  
184 <        if (ldot > .0 && bright(np->rdiff) > FTINY) {
184 >        if (ldot > 0 && bright(np->rdiff) > FTINY) {
185                  /*
186                   *  Compute added diffuse reflected component.
187                   */
# Line 156 | Line 190 | dir_bsdf(
190                  scalecolor(ctmp, dtmp);
191                  addcolor(cval, ctmp);
192          }
193 <        if (ldot < .0 && bright(np->tdiff) > FTINY) {
193 >        if (ldot < 0 && bright(np->tdiff) > FTINY) {
194                  /*
195                   *  Compute added diffuse transmission.
196                   */
# Line 168 | Line 202 | dir_bsdf(
202          /*
203           *  Compute scattering coefficient using BSDF.
204           */
205 <        if (!direct_bsdf_OK(ctmp, ldir, np))
205 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
206                  return;
207 <        if (ldot > .0) {                /* pattern only diffuse reflection */
207 >        if (ldot > 0) {         /* pattern only diffuse reflection */
208                  COLOR   ctmp1, ctmp2;
209 <                dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY
209 >                dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
210                                          : np->sd->rLambBack.cieY;
211                                          /* diffuse fraction */
212                  dtmp /= PI * bright(ctmp);
# Line 223 | Line 257 | dir_brdf(
257          /*
258           *  Compute reflection coefficient using BSDF.
259           */
260 <        if (!direct_bsdf_OK(ctmp, ldir, np))
260 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
261                  return;
262                                          /* pattern only diffuse reflection */
263 <        dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY
263 >        dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
264                                  : np->sd->rLambBack.cieY;
265          dtmp /= PI * bright(ctmp);      /* diffuse fraction */
266          copycolor(ctmp2, np->pr->pcol);
# Line 272 | Line 306 | dir_btdf(
306          /*
307           *  Compute scattering coefficient using BSDF.
308           */
309 <        if (!direct_bsdf_OK(ctmp, ldir, np))
309 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
310                  return;
311                                          /* full pattern on transmission */
312          multcolor(ctmp, np->pr->pcol);
# Line 286 | Line 320 | static int
320   sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
321   {
322          int     nstarget = 1;
323 <        int     nsent = 0;
323 >        int     nsent;
324          SDError ec;
325          SDValue bsv;
326 <        double  sthick;
327 <        FVECT   vjit, vsmp;
326 >        double  xrand;
327 >        FVECT   vsmp;
328          RAY     sr;
295        int     ntrials;
329                                                  /* multiple samples? */
330          if (specjitter > 1.5) {
331                  nstarget = specjitter*ndp->pr->rweight + .5;
332 <                if (nstarget < 1)
300 <                        nstarget = 1;
332 >                nstarget += !nstarget;
333          }
334 <                                                /* run through our trials */
335 <        for (ntrials = 0; nsent < nstarget && ntrials < 9*nstarget; ntrials++) {
336 <                SDerrorDetail[0] = '\0';
337 <                                                /* sample direction & coef. */
338 <                bsdf_jitter(vjit, ndp);
339 <                ec = SDsampComponent(&bsv, vsmp, vjit, ntrials ? frandom()
340 <                                : urand(ilhash(dimlist,ndims)+samplendx), dcp);
334 >                                                /* run through our samples */
335 >        for (nsent = 0; nsent < nstarget; nsent++) {
336 >                if (nstarget == 1)              /* stratify random variable */
337 >                        xrand = urand(ilhash(dimlist,ndims)+samplendx);
338 >                else
339 >                        xrand = (nsent + frandom())/(double)nstarget;
340 >                SDerrorDetail[0] = '\0';        /* sample direction & coef. */
341 >                bsdf_jitter(vsmp, ndp, 0);
342 >                ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
343                  if (ec)
344                          objerror(ndp->mp, USER, transSDError(ec));
345 <                                                /* zero component? */
312 <                if (bsv.cieY <= FTINY)
345 >                if (bsv.cieY <= FTINY)          /* zero component? */
346                          break;
347                                                  /* map vector to world */
348                  if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
349                          break;
317                                                /* unintentional penetration? */
318                if (DOT(sr.rdir, ndp->pr->ron) > .0 ^ vsmp[2] > .0)
319                        continue;
350                                                  /* spawn a specular ray */
351                  if (nstarget > 1)
352                          bsv.cieY /= (double)nstarget;
353 <                cvt_sdcolor(sr.rcoef, &bsv);    /* use color */
354 <                if (usepat)                     /* pattern on transmission */
353 >                cvt_sdcolor(sr.rcoef, &bsv);    /* use sample color */
354 >                if (usepat)                     /* apply pattern? */
355                          multcolor(sr.rcoef, ndp->pr->pcol);
356                  if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
357 <                        if (maxdepth  > 0)
357 >                        if (maxdepth > 0)
358                                  break;
359 <                        ++nsent;                /* Russian roulette victim */
330 <                        continue;
359 >                        continue;               /* Russian roulette victim */
360                  }
361                                                  /* need to offset origin? */
362 <                if (ndp->thick != .0 && ndp->pr->rod > .0 ^ vsmp[2] > .0)
362 >                if (ndp->thick != 0 && ndp->pr->rod > 0 ^ vsmp[2] > 0)
363                          VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
364                  rayvalue(&sr);                  /* send & evaluate sample */
365                  multcolor(sr.rcol, sr.rcoef);
366                  addcolor(ndp->pr->rcol, sr.rcol);
338                ++nsent;
367          }
368          return(nsent);
369   }
# Line 354 | Line 382 | sample_sdf(BSDFDAT *ndp, int sflags)
382                  cvt_sdcolor(unsc, &ndp->sd->tLamb);
383          } else /* sflags == SDsampSpR */ {
384                  unsc = ndp->runsamp;
385 <                if (ndp->pr->rod > .0) {
385 >                if (ndp->pr->rod > 0) {
386                          dfp = ndp->sd->rf;
387                          cvt_sdcolor(unsc, &ndp->sd->rLambFront);
388                  } else {
# Line 371 | Line 399 | sample_sdf(BSDFDAT *ndp, int sflags)
399                          FVECT   vjit;
400                          double  d;
401                          COLOR   ctmp;
402 <                        bsdf_jitter(vjit, ndp);
402 >                        bsdf_jitter(vjit, ndp, 1);
403                          d = SDdirectHemi(vjit, sflags, ndp->sd);
404                          if (sflags == SDsampSpT) {
405                                  copycolor(ctmp, ndp->pr->pcol);
# Line 408 | Line 436 | m_bsdf(OBJREC *m, RAY *r)
436                                  (m->oargs.nfargs % 3))
437                  objerror(m, USER, "bad # arguments");
438                                                  /* record surface struck */
439 <        hitfront = (r->rod > .0);
439 >        hitfront = (r->rod > 0);
440                                                  /* load cal file */
441          mf = getfunc(m, 5, 0x1d, 1);
442                                                  /* get thickness */
# Line 417 | Line 445 | m_bsdf(OBJREC *m, RAY *r)
445                  nd.thick = .0;
446                                                  /* check shadow */
447          if (r->crtype & SHADOW) {
448 <                if (nd.thick != .0)
448 >                if (nd.thick != 0)
449                          raytrans(r);            /* pass-through */
450                  return(1);                      /* or shadow */
451          }
452                                                  /* check other rays to pass */
453 <        if (nd.thick != .0 && (!(r->crtype & (SPECULAR|AMBIENT)) ||
454 <                                nd.thick > .0 ^ hitfront)) {
453 >        if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) ||
454 >                                nd.thick > 0 ^ hitfront)) {
455                  raytrans(r);                    /* hide our proxy */
456                  return(1);
457          }
# Line 487 | Line 515 | m_bsdf(OBJREC *m, RAY *r)
515                  ec = SDinvXform(nd.fromloc, nd.toloc);
516                                                  /* determine BSDF resolution */
517          if (!ec)
518 <                ec = SDsizeBSDF(&nd.sr_vpsa, nd.vray, SDqueryMin, nd.sd);
519 <        if (!ec)
520 <                nd.sr_vpsa = sqrt(nd.sr_vpsa);
493 <        else {
518 >                ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL,
519 >                                                SDqueryMin+SDqueryMax, nd.sd);
520 >        if (ec) {
521                  objerror(m, WARNING, transSDError(ec));
522                  SDfreeCache(nd.sd);
523                  return(1);
524          }
525 +        nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]);
526 +        nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]);
527          if (!hitfront) {                        /* perturb normal towards hit */
528                  nd.pnorm[0] = -nd.pnorm[0];
529                  nd.pnorm[1] = -nd.pnorm[1];
# Line 524 | Line 553 | m_bsdf(OBJREC *m, RAY *r)
553                  bnorm[0] = -nd.pnorm[0];
554                  bnorm[1] = -nd.pnorm[1];
555                  bnorm[2] = -nd.pnorm[2];
556 <                if (nd.thick != .0) {           /* proxy with offset? */
556 >                if (nd.thick != 0) {            /* proxy with offset? */
557                          VCOPY(vtmp, r->rop);
558                          VSUM(r->rop, vtmp, r->ron, -nd.thick);
559                          multambient(ctmp, r, bnorm);
# Line 538 | Line 567 | m_bsdf(OBJREC *m, RAY *r)
567                                                  /* add direct component */
568          if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL)) {
569                  direct(r, dir_brdf, &nd);       /* reflection only */
570 <        } else if (nd.thick == .0) {
570 >        } else if (nd.thick == 0) {
571                  direct(r, dir_bsdf, &nd);       /* thin surface scattering */
572          } else {
573                  direct(r, dir_brdf, &nd);       /* reflection first */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines