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.36 by greg, Tue May 16 20:06:40 2017 UTC vs.
Revision 2.43 by greg, Fri Dec 1 02:45:14 2017 UTC

# Line 37 | Line 37 | static const char RCSid[] = "$Id$";
37   *  Reflection is ignored on the hidden side, as those rays pass through.
38   *      When thickness is set to zero, shadow rays will be blocked unless
39   *  a BTDF has a strong "through" component in the source direction.
40 < *  A separate test prevents over-counting by dropping specular & ambient
41 < *  samples that are too close to this "through" direction.  The same
42 < *  restriction applies for the proxy case (thickness != 0).
40 > *  A separate test prevents over-counting by dropping samples that are
41 > *  too close to this "through" direction.  BSDFs with such a through direction
42 > *  will also have a view component, meaning they are somewhat see-through.
43   *      The "up" vector for the BSDF is given by three variables, defined
44   *  (along with the thickness) by the named function file, or '.' if none.
45   *  Together with the surface normal, this defines the local coordinate
# Line 79 | Line 79 | typedef struct {
79          COLOR   cthru;          /* "through" component multiplier */
80          SDData  *sd;            /* loaded BSDF data */
81          COLOR   rdiff;          /* diffuse reflection */
82 +        COLOR   runsamp;        /* BSDF hemispherical reflection */
83          COLOR   tdiff;          /* diffuse transmission */
84 +        COLOR   tunsamp;        /* BSDF hemispherical transmission */
85   }  BSDFDAT;             /* BSDF material data */
86  
87   #define cvt_sdcolor(cv, svp)    ccy2rgb(&(svp)->spec, (svp)->cieY, cv)
# Line 109 | Line 111 | compute_through(BSDFDAT *ndp)
111          FVECT           pdir;
112          double          tomega, srchrad;
113          COLOR           vpeak, vsum;
114 <        int             nsum, i;
114 >        int             i;
115          SDError         ec;
116  
117 <        setcolor(ndp->cthru, .0, .0, .0);       /* starting assumption */
117 >        setcolor(ndp->cthru, 0, 0, 0);          /* starting assumption */
118  
119          if (ndp->pr->rod > 0)
120                  dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
# Line 124 | Line 126 | compute_through(BSDFDAT *ndp)
126          if (bright(ndp->pr->pcol) <= FTINY)
127                  return;                         /* pattern is black, here */
128          srchrad = sqrt(dfp->minProjSA);         /* else search for peak */
129 <        setcolor(vpeak, .0, .0, .0);
130 <        setcolor(vsum, .0, .0, .0);
131 <        nsum = 0;
129 >        setcolor(vpeak, 0, 0, 0);
130 >        setcolor(vsum, 0, 0, 0);
131 >        pdir[2] = 0.0;
132          for (i = 0; i < NDIR2CHECK; i++) {
133                  FVECT   tdir;
134                  SDValue sv;
# Line 140 | Line 142 | compute_through(BSDFDAT *ndp)
142                          goto baderror;
143                  cvt_sdcolor(vcol, &sv);
144                  addcolor(vsum, vcol);
143                ++nsum;
145                  if (bright(vcol) > bright(vpeak)) {
146                          copycolor(vpeak, vcol);
147                          VCOPY(pdir, tdir);
148                  }
149          }
150 +        if (pdir[2] == 0.0)
151 +                return;                         /* zero neighborhood */
152          ec = SDsizeBSDF(&tomega, pdir, ndp->vray, SDqueryMin, ndp->sd);
153          if (ec)
154                  goto baderror;
155          if (tomega > 1.5*dfp->minProjSA)
156                  return;                         /* not really a peak? */
157 <        if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .007)
158 <                return;                         /* < 0.7% transmission */
157 >        tomega /= fabs(pdir[2]);                /* remove cosine factor */
158 >        if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .001)
159 >                return;                         /* < 0.1% transmission */
160          for (i = 3; i--; )                      /* remove peak from average */
161                  colval(vsum,i) -= colval(vpeak,i);
162 <        --nsum;
159 <        if (peak_over*bright(vsum) >= nsum*bright(vpeak))
162 >        if (peak_over*bright(vsum) >= (NDIR2CHECK-1)*bright(vpeak))
163                  return;                         /* not peaky enough */
164          copycolor(ndp->cthru, vpeak);           /* else use it */
165          scalecolor(ndp->cthru, tomega);
# Line 185 | Line 188 | bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
188   static int
189   direct_specular_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
190   {
191 <        int     nsamp, ok = 0;
191 >        int     nsamp;
192 >        double  wtot = 0;
193          FVECT   vsrc, vsmp, vjit;
194          double  tomega, tomega2;
195          double  sf, tsr, sd[2];
# Line 194 | Line 198 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
198          SDValue sv;
199          SDError ec;
200          int     i;
201 +                                        /* in case we fail */
202 +        setcolor(cval,  0, 0, 0);
203                                          /* transform source direction */
204          if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
205                  return(0);
# Line 219 | Line 225 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
225                  diffY = sv.cieY *= 1./PI;
226                  cvt_sdcolor(cdiff, &sv);
227          } else {
228 <                diffY = .0;
229 <                setcolor(cdiff, .0, .0, .0);
228 >                diffY = 0;
229 >                setcolor(cdiff,  0, 0, 0);
230          }
231 <                                        /* assign number of samples */
231 >                                        /* need projected solid angles */
232 >        omega *= fabs(vsrc[2]);
233          ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
234          if (ec)
235                  goto baderror;
236                                          /* check indirect over-counting */
237 <        if ((ndp->thick != 0 || bright(ndp->cthru) > FTINY)
231 <                                && ndp->pr->crtype & (SPECULAR|AMBIENT)
232 <                                && (vsrc[2] > 0) ^ (ndp->vray[2] > 0)) {
237 >        if ((vsrc[2] > 0) ^ (ndp->vray[2] > 0) && bright(ndp->cthru) > FTINY) {
238                  double  dx = vsrc[0] + ndp->vray[0];
239                  double  dy = vsrc[1] + ndp->vray[1];
240 <                if (dx*dx + dy*dy <= omega+tomega)
240 >                if (dx*dx + dy*dy <= (4./PI)*(omega + tomega +
241 >                                                2.*sqrt(omega*tomega)))
242                          return(0);
243          }
244 +                                        /* assign number of samples */
245          sf = specjitter * ndp->pr->rweight;
246 <        if (tomega <= .0)
246 >        if (tomega <= 0)
247                  nsamp = 1;
248          else if (25.*tomega <= omega)
249                  nsamp = 100.*sf + .5;
250          else
251                  nsamp = 4.*sf*omega/tomega + .5;
252          nsamp += !nsamp;
253 <        setcolor(cval, .0, .0, .0);     /* sample our source area */
247 <        sf = sqrt(omega);
253 >        sf = sqrt(omega);               /* sample our source area */
254          tsr = sqrt(tomega);
255          for (i = nsamp; i--; ) {
256                  VCOPY(vsmp, vsrc);      /* jitter query directions */
# Line 255 | Line 261 | direct_specular_OK(COLOR cval, FVECT ldir, double omeg
261                          normalize(vsmp);
262                  }
263                  bsdf_jitter(vjit, ndp, tsr);
264 +                                        /* compute BSDF */
265 +                ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
266 +                if (ec)
267 +                        goto baderror;
268 +                if (sv.cieY - diffY <= FTINY)
269 +                        continue;       /* no specular part */
270                                          /* check for variable resolution */
271                  ec = SDsizeBSDF(&tomega2, vjit, vsmp, SDqueryMin, ndp->sd);
272                  if (ec)
273                          goto baderror;
274                  if (tomega2 < .12*tomega)
275                          continue;       /* not safe to include */
264                                        /* else compute BSDF */
265                ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
266                if (ec)
267                        goto baderror;
268                if (sv.cieY - diffY <= FTINY)
269                        continue;       /* no specular part */
276                  cvt_sdcolor(csmp, &sv);
277 <                addcolor(cval, csmp);   /* else average it in */
278 <                ++ok;
277 >                                        /* weight average by Y */
278 >                scalecolor(csmp, sv.cieY);
279 >                addcolor(cval, csmp);
280 >                wtot += sv.cieY;
281          }
282 <        if (!ok) {
283 <                setcolor(cval, .0, .0, .0);
284 <                return(0);              /* no valid specular samples */
285 <        }
278 <        sf = 1./(double)ok;
282 >        if (wtot <= FTINY)              /* no valid specular samples? */
283 >                return(0);
284 >
285 >        sf = 1./wtot;                   /* weighted average BSDF */
286          scalecolor(cval, sf);
287                                          /* subtract diffuse contribution */
288          for (i = 3*(diffY > FTINY); i--; )
289 <                if ((colval(cval,i) -= colval(cdiff,i)) < .0)
290 <                        colval(cval,i) = .0;
289 >                if ((colval(cval,i) -= colval(cdiff,i)) < 0)
290 >                        colval(cval,i) = 0;
291          return(1);
292   baderror:
293          objerror(ndp->mp, USER, transSDError(ec));
# Line 301 | Line 308 | dir_bsdf(
308          double          dtmp;
309          COLOR           ctmp;
310  
311 <        setcolor(cval, .0, .0, .0);
311 >        setcolor(cval,  0, 0, 0);
312  
313          ldot = DOT(np->pnorm, ldir);
314          if ((-FTINY <= ldot) & (ldot <= FTINY))
# Line 309 | Line 316 | dir_bsdf(
316  
317          if (ldot > 0 && bright(np->rdiff) > FTINY) {
318                  /*
319 <                 *  Compute added diffuse reflected component.
319 >                 *  Compute diffuse reflected component
320                   */
321                  copycolor(ctmp, np->rdiff);
322                  dtmp = ldot * omega * (1./PI);
# Line 318 | Line 325 | dir_bsdf(
325          }
326          if (ldot < 0 && bright(np->tdiff) > FTINY) {
327                  /*
328 <                 *  Compute added diffuse transmission.
328 >                 *  Compute diffuse transmission
329                   */
330                  copycolor(ctmp, np->tdiff);
331                  dtmp = -ldot * omega * (1.0/PI);
# Line 328 | Line 335 | dir_bsdf(
335          if (ambRayInPmap(np->pr))
336                  return;         /* specular already in photon map */
337          /*
338 <         *  Compute specular scattering coefficient using BSDF.
338 >         *  Compute specular scattering coefficient using BSDF
339           */
340          if (!direct_specular_OK(ctmp, ldir, omega, np))
341                  return;
# Line 355 | Line 362 | dir_brdf(
362          double          dtmp;
363          COLOR           ctmp, ctmp1, ctmp2;
364  
365 <        setcolor(cval, .0, .0, .0);
365 >        setcolor(cval,  0, 0, 0);
366  
367          ldot = DOT(np->pnorm, ldir);
368          
# Line 364 | Line 371 | dir_brdf(
371  
372          if (bright(np->rdiff) > FTINY) {
373                  /*
374 <                 *  Compute added diffuse reflected component.
374 >                 *  Compute diffuse reflected component
375                   */
376                  copycolor(ctmp, np->rdiff);
377                  dtmp = ldot * omega * (1./PI);
# Line 374 | Line 381 | dir_brdf(
381          if (ambRayInPmap(np->pr))
382                  return;         /* specular already in photon map */
383          /*
384 <         *  Compute specular reflection coefficient using BSDF.
384 >         *  Compute specular reflection coefficient using BSDF
385           */
386          if (!direct_specular_OK(ctmp, ldir, omega, np))
387                  return;
# Line 397 | Line 404 | dir_btdf(
404          double          dtmp;
405          COLOR           ctmp;
406  
407 <        setcolor(cval, .0, .0, .0);
407 >        setcolor(cval,  0, 0, 0);
408  
409          ldot = DOT(np->pnorm, ldir);
410  
# Line 406 | Line 413 | dir_btdf(
413  
414          if (bright(np->tdiff) > FTINY) {
415                  /*
416 <                 *  Compute added diffuse transmission.
416 >                 *  Compute diffuse transmission
417                   */
418                  copycolor(ctmp, np->tdiff);
419                  dtmp = -ldot * omega * (1.0/PI);
# Line 416 | Line 423 | dir_btdf(
423          if (ambRayInPmap(np->pr))
424                  return;         /* specular already in photon map */
425          /*
426 <         *  Compute specular scattering coefficient using BSDF.
426 >         *  Compute specular scattering coefficient using BSDF
427           */
428          if (!direct_specular_OK(ctmp, ldir, omega, np))
429                  return;
# Line 429 | Line 436 | dir_btdf(
436  
437   /* Sample separate BSDF component */
438   static int
439 < sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
439 > sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int xmit)
440   {
441 <        int     nstarget = 1;
442 <        int     nsent;
443 <        SDError ec;
444 <        SDValue bsv;
445 <        double  xrand;
446 <        FVECT   vsmp;
447 <        RAY     sr;
441 >        const int       hasthru = (xmit &&
442 >                                !(ndp->pr->crtype & (SPECULAR|AMBIENT)) &&
443 >                                bright(ndp->cthru) > FTINY);
444 >        int             nstarget = 1;
445 >        int             nsent = 0;
446 >        int             n;
447 >        SDError         ec;
448 >        SDValue         bsv;
449 >        double          xrand;
450 >        FVECT           vsmp, vinc;
451 >        RAY             sr;
452                                                  /* multiple samples? */
453          if (specjitter > 1.5) {
454                  nstarget = specjitter*ndp->pr->rweight + .5;
455                  nstarget += !nstarget;
456          }
457                                                  /* run through our samples */
458 <        for (nsent = 0; nsent < nstarget; nsent++) {
458 >        for (n = 0; n < nstarget; n++) {
459                  if (nstarget == 1) {            /* stratify random variable */
460                          xrand = urand(ilhash(dimlist,ndims)+samplendx);
461                          if (specjitter < 1.)
462                                  xrand = .5 + specjitter*(xrand-.5);
463                  } else {
464 <                        xrand = (nsent + frandom())/(double)nstarget;
464 >                        xrand = (n + frandom())/(double)nstarget;
465                  }
466                  SDerrorDetail[0] = '\0';        /* sample direction & coef. */
467                  bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]);
468 +                VCOPY(vinc, vsmp);              /* to compare after */
469                  ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
470                  if (ec)
471                          objerror(ndp->mp, USER, transSDError(ec));
472                  if (bsv.cieY <= FTINY)          /* zero component? */
473                          break;
474 <                                                /* map vector to world */
474 >                if (hasthru) {                  /* check for view ray */
475 >                        double  dx = vinc[0] + vsmp[0];
476 >                        double  dy = vinc[1] + vsmp[1];
477 >                        if (dx*dx + dy*dy <= ndp->sr_vpsa[0]*ndp->sr_vpsa[0])
478 >                                continue;       /* exclude view sample */
479 >                }
480 >                                                /* map non-view sample->world */
481                  if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
482                          break;
483                                                  /* spawn a specular ray */
484                  if (nstarget > 1)
485                          bsv.cieY /= (double)nstarget;
486                  cvt_sdcolor(sr.rcoef, &bsv);    /* use sample color */
487 <                if (usepat)                     /* apply pattern? */
487 >                if (xmit)                       /* apply pattern on transmit */
488                          multcolor(sr.rcoef, ndp->pr->pcol);
489                  if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
490                          if (maxdepth > 0)
491                                  break;
492                          continue;               /* Russian roulette victim */
493                  }
494 <                                                /* need to offset origin? */
477 <                if (ndp->thick != 0 && (ndp->pr->rod > 0) ^ (vsmp[2] > 0))
494 >                if (xmit && ndp->thick != 0)    /* need to offset origin? */
495                          VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
496                  rayvalue(&sr);                  /* send & evaluate sample */
497                  multcolor(sr.rcol, sr.rcoef);
498                  addcolor(ndp->pr->rcol, sr.rcol);
499 +                ++nsent;
500          }
501          return(nsent);
502   }
# Line 487 | Line 505 | sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usep
505   static int
506   sample_sdf(BSDFDAT *ndp, int sflags)
507   {
508 +        int             hasthru = (sflags == SDsampSpT
509 +                                    && !(ndp->pr->crtype & (SPECULAR|AMBIENT))
510 +                                    && bright(ndp->cthru) > FTINY);
511          int             n, ntotal = 0;
512 +        double          b = 0;
513          SDSpectralDF    *dfp;
514          COLORV          *unsc;
515  
516          if (sflags == SDsampSpT) {
517 <                unsc = ndp->tdiff;
517 >                unsc = ndp->tunsamp;
518                  if (ndp->pr->rod > 0)
519                          dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
520                  else
521                          dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
522          } else /* sflags == SDsampSpR */ {
523 <                unsc = ndp->rdiff;
523 >                unsc = ndp->runsamp;
524                  if (ndp->pr->rod > 0)
525                          dfp = ndp->sd->rf;
526                  else
527                          dfp = ndp->sd->rb;
528          }
529 +        setcolor(unsc,  0, 0, 0);
530          if (dfp == NULL)                        /* no specular component? */
531                  return(0);
532 <                                                /* below sampling threshold? */
533 <        if (dfp->maxHemi <= specthresh+FTINY) {
534 <                if (dfp->maxHemi > FTINY) {     /* XXX no color from BSDF */
535 <                        FVECT   vjit;
536 <                        double  d;
537 <                        COLOR   ctmp;
538 <                        bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
539 <                        d = SDdirectHemi(vjit, sflags, ndp->sd);
532 >
533 >        if (hasthru) {                          /* separate view sample? */
534 >                RAY     tr;
535 >                if (rayorigin(&tr, TRANS, ndp->pr, ndp->cthru) == 0) {
536 >                        VCOPY(tr.rdir, ndp->pr->rdir);
537 >                        rayvalue(&tr);
538 >                        multcolor(tr.rcol, tr.rcoef);
539 >                        addcolor(ndp->pr->rcol, tr.rcol);
540 >                        ++ntotal;
541 >                        b = bright(ndp->cthru);
542 >                } else
543 >                        hasthru = 0;
544 >        }
545 >        if (dfp->maxHemi - b <= FTINY) {        /* have specular to sample? */
546 >                b = 0;
547 >        } else {
548 >                FVECT   vjit;
549 >                bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
550 >                b = SDdirectHemi(vjit, sflags, ndp->sd) - b;
551 >                if (b < 0) b = 0;
552 >        }
553 >        if (b <= specthresh+FTINY) {            /* below sampling threshold? */
554 >                if (b > FTINY) {                /* XXX no color from BSDF */
555                          if (sflags == SDsampSpT) {
556 <                                copycolor(ctmp, ndp->pr->pcol);
557 <                                scalecolor(ctmp, d);
556 >                                copycolor(unsc, ndp->pr->pcol);
557 >                                scalecolor(unsc, b);
558                          } else                  /* no pattern on reflection */
559 <                                setcolor(ctmp, d, d, d);
522 <                        addcolor(unsc, ctmp);
559 >                                setcolor(unsc, b, b, b);
560                  }
561 <                return(0);
561 >                return(ntotal);
562          }
563 <                                                /* else need to sample */
564 <        dimlist[ndims++] = (int)(size_t)ndp->mp;
528 <        ndims++;
563 >        dimlist[ndims] = (int)(size_t)ndp->mp;  /* else sample specular */
564 >        ndims += 2;
565          for (n = dfp->ncomp; n--; ) {           /* loop over components */
566                  dimlist[ndims-1] = n + 9438;
567                  ntotal += sample_sdcomp(ndp, &dfp->comp[n], sflags==SDsampSpT);
# Line 556 | Line 592 | m_bsdf(OBJREC *m, RAY *r)
592                                                  /* get thickness */
593          nd.thick = evalue(mf->ep[0]);
594          if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
595 <                nd.thick = .0;
595 >                nd.thick = 0;
596                                                  /* check backface visibility */
597          if (!hitfront & !backvis) {
598                  raytrans(r);
# Line 663 | Line 699 | m_bsdf(OBJREC *m, RAY *r)
699                                                  /* sample transmission */
700          sample_sdf(&nd, SDsampSpT);
701                                                  /* compute indirect diffuse */
702 <        if (bright(nd.rdiff) > FTINY) {         /* ambient from reflection */
702 >        copycolor(ctmp, nd.rdiff);
703 >        addcolor(ctmp, nd.runsamp);
704 >        if (bright(ctmp) > FTINY) {             /* ambient from reflection */
705                  if (!hitfront)
706                          flipsurface(r);
669                copycolor(ctmp, nd.rdiff);
707                  multambient(ctmp, r, nd.pnorm);
708                  addcolor(r->rcol, ctmp);
709                  if (!hitfront)
710                          flipsurface(r);
711          }
712 <        if (bright(nd.tdiff) > FTINY) {         /* ambient from other side */
712 >        copycolor(ctmp, nd.tdiff);
713 >        addcolor(ctmp, nd.tunsamp);
714 >        if (bright(ctmp) > FTINY) {             /* ambient from other side */
715                  FVECT  bnorm;
716                  if (hitfront)
717                          flipsurface(r);
718                  bnorm[0] = -nd.pnorm[0];
719                  bnorm[1] = -nd.pnorm[1];
720                  bnorm[2] = -nd.pnorm[2];
682                copycolor(ctmp, nd.tdiff);
721                  if (nd.thick != 0) {            /* proxy with offset? */
722                          VCOPY(vtmp, r->rop);
723                          VSUM(r->rop, vtmp, r->ron, nd.thick);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines