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.12 by greg, Sun Aug 21 16:55:29 2011 UTC vs.
Revision 2.16 by greg, Wed Aug 24 04:31:13 2011 UTC

# Line 68 | Line 68 | typedef struct {
68          FVECT   pnorm;          /* perturbed surface normal */
69          FVECT   vray;           /* local outgoing (return) vector */
70          double  sr_vpsa[2];     /* sqrt of BSDF projected solid angle extrema */
71        double  thru_r2;        /* through rejection angle squared */
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 83 | 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, int domax)
85 > bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
86   {
88        double  sr_psa = ndp->sr_vpsa[domax];
89
87          VCOPY(vres, ndp->vray);
88          if (specjitter < 1.)
89                  sr_psa *= specjitter;
# Line 99 | Line 96 | bsdf_jitter(FVECT vres, BSDFDAT *ndp, int domax)
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, 0);
114 <                                        /* avoid indirect over-counting */
115 <        if (ndp->thru_r2 > FTINY && vsrc[2] > 0 ^ vjit[2] > 0) {
116 <                double  dx = vsrc[0] + vjit[0];
117 <                double  dy = vsrc[1] + vjit[1];
118 <                if (dx*dx + dy*dy <= ndp->thru_r2)
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 (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) */
# Line 168 | Line 200 | dir_bsdf(
200          /*
201           *  Compute scattering coefficient using BSDF.
202           */
203 <        if (!direct_bsdf_OK(ctmp, ldir, np))
203 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
204                  return;
205          if (ldot > 0) {         /* pattern only diffuse reflection */
206                  COLOR   ctmp1, ctmp2;
# Line 223 | Line 255 | dir_brdf(
255          /*
256           *  Compute reflection coefficient using BSDF.
257           */
258 <        if (!direct_bsdf_OK(ctmp, ldir, np))
258 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
259                  return;
260                                          /* pattern only diffuse reflection */
261          dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
# Line 272 | Line 304 | dir_btdf(
304          /*
305           *  Compute scattering coefficient using BSDF.
306           */
307 <        if (!direct_bsdf_OK(ctmp, ldir, np))
307 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
308                  return;
309                                          /* full pattern on transmission */
310          multcolor(ctmp, np->pr->pcol);
# Line 295 | Line 327 | sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usep
327                                                  /* multiple samples? */
328          if (specjitter > 1.5) {
329                  nstarget = specjitter*ndp->pr->rweight + .5;
330 <                if (nstarget < 1)
299 <                        nstarget = 1;
330 >                nstarget += !nstarget;
331          }
332                                                  /* run through our samples */
333          for (nsent = 0; nsent < nstarget; nsent++) {
334 <                if (nstarget == 1)              /* stratify random variable */
334 >                if (nstarget == 1) {            /* stratify random variable */
335                          xrand = urand(ilhash(dimlist,ndims)+samplendx);
336 <                else
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, 0);
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));
# Line 366 | Line 400 | sample_sdf(BSDFDAT *ndp, int sflags)
400                          FVECT   vjit;
401                          double  d;
402                          COLOR   ctmp;
403 <                        bsdf_jitter(vjit, ndp, 1);
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 484 | Line 518 | m_bsdf(OBJREC *m, RAY *r)
518          if (!ec)
519                  ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL,
520                                                  SDqueryMin+SDqueryMax, nd.sd);
487        nd.thru_r2 = .0;
488        if (!ec && nd.thick != 0 && r->crtype & (SPECULAR|AMBIENT)) {
489                FVECT   vthru;
490                vthru[0] = -nd.vray[0];
491                vthru[1] = -nd.vray[1];
492                vthru[2] = -nd.vray[2];
493                ec = SDsizeBSDF(&nd.thru_r2, nd.vray, vthru,
494                                                SDqueryMin, nd.sd);
495                nd.thru_r2 *= 1./PI;
496        }
521          if (ec) {
522                  objerror(m, WARNING, transSDError(ec));
523                  SDfreeCache(nd.sd);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines