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.6 by greg, Sun Feb 20 17:43:43 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 50 | Line 51 | static const char RCSid[] = "$Id$";
51   *  Arguments for MAT_BSDF are:
52   *      6+      thick   BSDFfile        ux uy uz        funcfile        transform
53   *      0
54 < *      0|3|9   rdf     gdf     bdf
54 > *      0|3|6|9 rdf     gdf     bdf
55   *              rdb     gdb     bdb
56   *              rdt     gdt     bdt
57   */
# Line 67 | Line 68 | typedef struct {
68          RAY     *pr;            /* intersected ray */
69          FVECT   pnorm;          /* perturbed surface normal */
70          FVECT   vray;           /* local outgoing (return) vector */
71 <        double  sr_vpsa;        /* sqrt of BSDF projected solid angle */
71 >        double  sr_vpsa[2];     /* sqrt of BSDF projected solid angle extrema */
72          RREAL   toloc[3][3];    /* world to local BSDF coords */
73          RREAL   fromloc[3][3];  /* local BSDF coords to world */
74          double  thick;          /* surface thickness */
# 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)
86 > bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
87   {
87        double  sr_psa = ndp->sr_vpsa;
88
88          VCOPY(vres, ndp->vray);
89          if (specjitter < 1.)
90                  sr_psa *= specjitter;
# Line 96 | Line 95 | bsdf_jitter(FVECT vres, BSDFDAT *ndp)
95          normalize(vres);
96   }
97  
98 + /* Evaluate BSDF for direct component, returning true if OK to proceed */
99 + static int
100 + direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
101 + {
102 +        int     nsamp, ok = 0;
103 +        FVECT   vsrc, vsmp, vjit;
104 +        double  tomega;
105 +        double  sf, tsr, sd[2];
106 +        COLOR   csmp;
107 +        SDValue sv;
108 +        SDError ec;
109 +        int     i;
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+tomega)
123 +                        return(0);
124 +        }
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 = 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) {
139 +                        multisamp(sd, 2, (i + frandom())/(double)nsamp);
140 +                        vsmp[0] += (sd[0] - .5)*sf;
141 +                        vsmp[1] += (sd[1] - .5)*sf;
142 +                        if (normalize(vsmp) == 0) {
143 +                                --nsamp;
144 +                                continue;
145 +                        }
146 +                }
147 +                bsdf_jitter(vjit, ndp, tsr);
148 +                                        /* compute BSDF */
149 +                ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
150 +                if (ec)
151 +                        goto baderror;
152 +                if (sv.cieY <= FTINY)   /* worth using? */
153 +                        continue;
154 +                cvt_sdcolor(csmp, &sv);
155 +                addcolor(cval, csmp);   /* average it in */
156 +                ++ok;
157 +        }
158 +        sf = 1./(double)nsamp;
159 +        scalecolor(cval, sf);
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) */
167   static void
168   dir_bsdf(
# Line 106 | Line 173 | dir_bsdf(
173   )
174   {
175          BSDFDAT         *np = (BSDFDAT *)nnp;
109        SDError         ec;
110        SDValue         sv;
111        FVECT           vsrc;
112        FVECT           vjit;
176          double          ldot;
177          double          dtmp;
178          COLOR           ctmp;
# Line 120 | Line 183 | dir_bsdf(
183          if ((-FTINY <= ldot) & (ldot <= FTINY))
184                  return;
185  
186 <        if (ldot > .0 && bright(np->rdiff) > FTINY) {
186 >        if (ldot > 0 && bright(np->rdiff) > FTINY) {
187                  /*
188                   *  Compute added diffuse reflected component.
189                   */
# Line 129 | Line 192 | dir_bsdf(
192                  scalecolor(ctmp, dtmp);
193                  addcolor(cval, ctmp);
194          }
195 <        if (ldot < .0 && bright(np->tdiff) > FTINY) {
195 >        if (ldot < 0 && bright(np->tdiff) > FTINY) {
196                  /*
197                   *  Compute added diffuse transmission.
198                   */
# Line 138 | 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           */
209 <        if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone)
209 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
210                  return;
211 <        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 */
211 >        if (ldot > 0) {         /* pattern only diffuse reflection */
212                  COLOR   ctmp1, ctmp2;
213 <                dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY
213 >                dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
214                                          : np->sd->rLambBack.cieY;
215 <                dtmp /= PI * sv.cieY;   /* diffuse fraction */
215 >                                        /* diffuse fraction */
216 >                dtmp /= PI * bright(ctmp);
217                  copycolor(ctmp2, np->pr->pcol);
218                  scalecolor(ctmp2, dtmp);
219                  setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
# Line 180 | Line 238 | dir_brdf(
238   )
239   {
240          BSDFDAT         *np = (BSDFDAT *)nnp;
183        SDError         ec;
184        SDValue         sv;
185        FVECT           vsrc;
186        FVECT           vjit;
241          double          ldot;
242          double          dtmp;
243          COLOR           ctmp, ctmp1, ctmp2;
# Line 204 | 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           */
266 <        if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone)
266 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
267                  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);
268                                          /* pattern only diffuse reflection */
269 <        dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY
269 >        dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
270                                  : np->sd->rLambBack.cieY;
271 <        dtmp /= PI * sv.cieY;           /* diffuse fraction */
271 >        dtmp /= PI * bright(ctmp);      /* diffuse fraction */
272          copycolor(ctmp2, np->pr->pcol);
273          scalecolor(ctmp2, dtmp);
274          setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
# Line 241 | Line 289 | dir_btdf(
289   )
290   {
291          BSDFDAT         *np = (BSDFDAT *)nnp;
244        SDError         ec;
245        SDValue         sv;
246        FVECT           vsrc;
247        FVECT           vjit;
292          double          ldot;
293          double          dtmp;
294          COLOR           ctmp;
# Line 265 | 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           */
317 <        if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone)
317 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
318                  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);
319                                          /* full pattern on transmission */
320          multcolor(ctmp, np->pr->pcol);
321          dtmp = -ldot * omega;
# Line 290 | Line 328 | static int
328   sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
329   {
330          int     nstarget = 1;
331 <        int     nsent = 0;
331 >        int     nsent;
332          SDError ec;
333          SDValue bsv;
334 <        double  sthick;
335 <        FVECT   vjit, vsmp;
334 >        double  xrand;
335 >        FVECT   vsmp;
336          RAY     sr;
299        int     ntrials;
337                                                  /* multiple samples? */
338          if (specjitter > 1.5) {
339                  nstarget = specjitter*ndp->pr->rweight + .5;
340 <                if (nstarget < 1)
304 <                        nstarget = 1;
340 >                nstarget += !nstarget;
341          }
342 <                                                /* run through our trials */
343 <        for (ntrials = 0; nsent < nstarget && ntrials < 9*nstarget; ntrials++) {
344 <                SDerrorDetail[0] = '\0';
345 <                                                /* sample direction & coef. */
346 <                bsdf_jitter(vjit, ndp);
347 <                ec = SDsampComponent(&bsv, vsmp, vjit, ntrials ? frandom()
348 <                                : urand(ilhash(dimlist,ndims)+samplendx), dcp);
342 >                                                /* run through our samples */
343 >        for (nsent = 0; nsent < nstarget; nsent++) {
344 >                if (nstarget == 1) {            /* stratify random variable */
345 >                        xrand = urand(ilhash(dimlist,ndims)+samplendx);
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, ndp->sr_vpsa[0]);
353 >                ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
354                  if (ec)
355                          objerror(ndp->mp, USER, transSDError(ec));
356 <                                                /* zero component? */
316 <                if (bsv.cieY <= FTINY)
356 >                if (bsv.cieY <= FTINY)          /* zero component? */
357                          break;
358                                                  /* map vector to world */
359                  if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
360                          break;
321                                                /* unintentional penetration? */
322                if (DOT(sr.rdir, ndp->pr->ron) > .0 ^ vsmp[2] > .0)
323                        continue;
361                                                  /* spawn a specular ray */
362                  if (nstarget > 1)
363                          bsv.cieY /= (double)nstarget;
364 <                cvt_sdcolor(sr.rcoef, &bsv);    /* use color */
365 <                if (usepat)                     /* pattern on transmission */
364 >                cvt_sdcolor(sr.rcoef, &bsv);    /* use sample color */
365 >                if (usepat)                     /* apply pattern? */
366                          multcolor(sr.rcoef, ndp->pr->pcol);
367                  if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
368 <                        if (maxdepth  > 0)
368 >                        if (maxdepth > 0)
369                                  break;
370 <                        ++nsent;                /* Russian roulette victim */
334 <                        continue;
370 >                        continue;               /* Russian roulette victim */
371                  }
372                                                  /* need to offset origin? */
373 <                if (ndp->thick != .0 && ndp->pr->rod > .0 ^ vsmp[2] > .0)
373 >                if (ndp->thick != 0 && ndp->pr->rod > 0 ^ vsmp[2] > 0)
374                          VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
375                  rayvalue(&sr);                  /* send & evaluate sample */
376                  multcolor(sr.rcol, sr.rcoef);
377                  addcolor(ndp->pr->rcol, sr.rcol);
342                ++nsent;
378          }
379          return(nsent);
380   }
# Line 354 | 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;
399 <                if (ndp->pr->rod > .0) {
399 >                if (ndp->pr->rod > 0) {
400                          dfp = ndp->sd->rf;
401                          cvt_sdcolor(unsc, &ndp->sd->rLambFront);
402                  } else {
# Line 375 | Line 413 | sample_sdf(BSDFDAT *ndp, int sflags)
413                          FVECT   vjit;
414                          double  d;
415                          COLOR   ctmp;
416 <                        bsdf_jitter(vjit, ndp);
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 412 | Line 450 | m_bsdf(OBJREC *m, RAY *r)
450                                  (m->oargs.nfargs % 3))
451                  objerror(m, USER, "bad # arguments");
452                                                  /* record surface struck */
453 <        hitfront = (r->rod > .0);
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))
460                  nd.thick = .0;
461                                                  /* check shadow */
462          if (r->crtype & SHADOW) {
463 <                if (nd.thick != .0)
463 >                if (nd.thick != 0)
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 442 | 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 */
446 <                        if (!backvis && (nd.sd->rb == NULL) &
447 <                                                (nd.sd->tf == NULL)) {
448 <                                SDfreeCache(nd.sd);
449 <                                raytrans(r);
450 <                                return(1);
451 <                        }
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 474 | 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 489 | Line 531 | m_bsdf(OBJREC *m, RAY *r)
531          }
532          if (!ec)
533                  ec = SDinvXform(nd.fromloc, nd.toloc);
534 <                                                /* determine BSDF resolution */
535 <        if (!ec)
494 <                ec = SDsizeBSDF(&nd.sr_vpsa, nd.vray, SDqueryMin, nd.sd);
495 <        if (!ec)
496 <                nd.sr_vpsa = sqrt(nd.sr_vpsa);
497 <        else {
498 <                objerror(m, WARNING, transSDError(ec));
499 <                SDfreeCache(nd.sd);
534 >        if (ec) {
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 */
546                  nd.pnorm[0] = -nd.pnorm[0];
547                  nd.pnorm[1] = -nd.pnorm[1];
# Line 528 | Line 571 | m_bsdf(OBJREC *m, RAY *r)
571                  bnorm[0] = -nd.pnorm[0];
572                  bnorm[1] = -nd.pnorm[1];
573                  bnorm[2] = -nd.pnorm[2];
574 <                if (nd.thick != .0) {           /* proxy with offset? */
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 540 | 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) {
589 >        } else if (nd.thick == 0) {
590                  direct(r, dir_bsdf, &nd);       /* thin surface scattering */
591          } else {
592                  direct(r, dir_brdf, &nd);       /* reflection first */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines