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.30 by greg, Wed Sep 2 18:59:01 2015 UTC vs.
Revision 2.34 by greg, Mon May 15 22:50:33 2017 UTC

# Line 72 | Line 72 | typedef struct {
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 */
75 +        COLOR   cthru;          /* through component multiplier */
76          SDData  *sd;            /* loaded BSDF data */
77 <        COLOR   runsamp;        /* BSDF hemispherical reflection */
78 <        COLOR   rdiff;          /* added diffuse reflection */
78 <        COLOR   tunsamp;        /* BSDF hemispherical transmission */
79 <        COLOR   tdiff;          /* added diffuse transmission */
77 >        COLOR   rdiff;          /* diffuse reflection */
78 >        COLOR   tdiff;          /* diffuse transmission */
79   }  BSDFDAT;             /* BSDF material data */
80  
81   #define cvt_sdcolor(cv, svp)    ccy2rgb(&(svp)->spec, (svp)->cieY, cv)
82  
83 + /* Compute through component color */
84 + static void
85 + compute_through(BSDFDAT *ndp)
86 + {
87 + #define NDIR2CHECK      13
88 +        static const float      dir2check[NDIR2CHECK][2] = {
89 +                                        {0, 0},
90 +                                        {-0.8, 0},
91 +                                        {0, 0.8},
92 +                                        {0, -0.8},
93 +                                        {0.8, 0},
94 +                                        {-0.8, 0.8},
95 +                                        {-0.8, -0.8},
96 +                                        {0.8, 0.8},
97 +                                        {0.8, -0.8},
98 +                                        {-1.6, 0},
99 +                                        {0, 1.6},
100 +                                        {0, -1.6},
101 +                                        {1.6, 0},
102 +                                };
103 +        const double    peak_over = 2.0;
104 +        SDSpectralDF    *dfp;
105 +        FVECT           pdir;
106 +        double          tomega, srchrad;
107 +        COLOR           vpeak, vsum;
108 +        int             nsum, i;
109 +        SDError         ec;
110 +
111 +        setcolor(ndp->cthru, .0, .0, .0);       /* starting assumption */
112 +
113 +        if (ndp->pr->rod > 0)
114 +                dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
115 +        else
116 +                dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
117 +
118 +        if (dfp == NULL)
119 +                return;                         /* no specular transmission */
120 +        if (bright(ndp->pr->pcol) <= FTINY)
121 +                return;                         /* pattern is black, here */
122 +        srchrad = sqrt(dfp->minProjSA);         /* else search for peak */
123 +        setcolor(vpeak, .0, .0, .0);
124 +        setcolor(vsum, .0, .0, .0);
125 +        nsum = 0;
126 +        for (i = 0; i < NDIR2CHECK; i++) {
127 +                FVECT   tdir;
128 +                SDValue sv;
129 +                COLOR   vcol;
130 +                tdir[0] = -ndp->vray[0] + dir2check[i][0]*srchrad;
131 +                tdir[1] = -ndp->vray[1] + dir2check[i][1]*srchrad;
132 +                tdir[2] = -ndp->vray[2];
133 +                if (normalize(tdir) == 0)
134 +                        continue;
135 +                ec = SDevalBSDF(&sv, tdir, ndp->vray, ndp->sd);
136 +                if (ec)
137 +                        goto baderror;
138 +                cvt_sdcolor(vcol, &sv);
139 +                addcolor(vsum, vcol);
140 +                ++nsum;
141 +                if (bright(vcol) > bright(vpeak)) {
142 +                        copycolor(vpeak, vcol);
143 +                        VCOPY(pdir, tdir);
144 +                }
145 +        }
146 +        ec = SDsizeBSDF(&tomega, pdir, ndp->vray, SDqueryMin, ndp->sd);
147 +        if (ec)
148 +                goto baderror;
149 +        if (tomega > 1.5*dfp->minProjSA)
150 +                return;                         /* not really a peak? */
151 +        if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .001)
152 +                return;                         /* < 0.1% transmission */
153 +        for (i = 3; i--; )                      /* remove peak from average */
154 +                colval(vsum,i) -= colval(vpeak,i);
155 +        --nsum;
156 +        if (peak_over*bright(vsum) >= nsum*bright(vpeak))
157 +                return;                         /* not peaky enough */
158 +        copycolor(ndp->cthru, vpeak);           /* else use it */
159 +        scalecolor(ndp->cthru, tomega);
160 +        multcolor(ndp->cthru, ndp->pr->pcol);   /* modify by pattern */
161 +        return;
162 + baderror:
163 +        objerror(ndp->mp, USER, transSDError(ec));
164 + #undef NDIR2CHECK
165 + }
166 +
167   /* Jitter ray sample according to projected solid angle and specjitter */
168   static void
169   bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
# Line 95 | Line 178 | bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
178          normalize(vres);
179   }
180  
181 < /* Evaluate BSDF for direct component, returning true if OK to proceed */
181 > /* Get BSDF specular for direct component, returning true if OK to proceed */
182   static int
183 < direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
183 > direct_specular_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
184   {
185          int     nsamp, ok = 0;
186          FVECT   vsrc, vsmp, vjit;
187          double  tomega;
188          double  sf, tsr, sd[2];
189 <        COLOR   csmp;
189 >        COLOR   csmp, cdiff;
190 >        double  diffY;
191          SDValue sv;
192          SDError ec;
193          int     i;
194                                          /* transform source direction */
195          if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
196                  return(0);
197 +                                        /* will discount diffuse portion */
198 +        switch ((vsrc[2] > 0)<<1 | (ndp->vray[2] > 0)) {
199 +        case 3:
200 +                if (ndp->sd->rf == NULL)
201 +                        return(0);      /* all diffuse */
202 +                sv = ndp->sd->rLambFront;
203 +                break;
204 +        case 0:
205 +                if (ndp->sd->rb == NULL)
206 +                        return(0);      /* all diffuse */
207 +                sv = ndp->sd->rLambBack;
208 +                break;
209 +        default:
210 +                if ((ndp->sd->tf == NULL) & (ndp->sd->tb == NULL))
211 +                        return(0);      /* all diffuse */
212 +                sv = ndp->sd->tLamb;
213 +                break;
214 +        }
215 +        if (sv.cieY > FTINY) {
216 +                diffY = sv.cieY *= 1./PI;
217 +                cvt_sdcolor(cdiff, &sv);
218 +        } else {
219 +                diffY = .0;
220 +                setcolor(cdiff, .0, .0, .0);
221 +        }
222                                          /* assign number of samples */
223          ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
224          if (ec)
225                  goto baderror;
226                                          /* check indirect over-counting */
227 <        if (ndp->thick != 0 && ndp->pr->crtype & (SPECULAR|AMBIENT)
228 <                                && vsrc[2] > 0 ^ ndp->vray[2] > 0) {
227 >        if ((ndp->thick != 0 || bright(ndp->cthru) > FTINY)
228 >                                && ndp->pr->crtype & (SPECULAR|AMBIENT)
229 >                                && (vsrc[2] > 0) ^ (ndp->vray[2] > 0)) {
230                  double  dx = vsrc[0] + ndp->vray[0];
231                  double  dy = vsrc[1] + ndp->vray[1];
232                  if (dx*dx + dy*dy <= omega+tomega)
# Line 149 | Line 259 | direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, B
259                  ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
260                  if (ec)
261                          goto baderror;
262 <                if (sv.cieY <= FTINY)   /* worth using? */
263 <                        continue;
262 >                if (sv.cieY - diffY <= FTINY) {
263 >                        addcolor(cval, cdiff);
264 >                        continue;       /* no specular part */
265 >                }
266                  cvt_sdcolor(csmp, &sv);
267 <                addcolor(cval, csmp);   /* average it in */
267 >                addcolor(cval, csmp);   /* else average it in */
268                  ++ok;
269          }
270 +        if (!ok) {
271 +                setcolor(cval, .0, .0, .0);
272 +                return(0);              /* no valid specular samples */
273 +        }
274          sf = 1./(double)nsamp;
275          scalecolor(cval, sf);
276 <        return(ok);
276 >                                        /* subtract diffuse contribution */
277 >        for (i = 3*(diffY > FTINY); i--; )
278 >                if ((colval(cval,i) -= colval(cdiff,i)) < .0)
279 >                        colval(cval,i) = .0;
280 >        return(1);
281   baderror:
282          objerror(ndp->mp, USER, transSDError(ec));
283          return(0);                      /* gratis return */
# Line 204 | Line 324 | dir_bsdf(
324          if (ambRayInPmap(np->pr))
325                  return;         /* specular already in photon map */
326          /*
327 <         *  Compute scattering coefficient using BSDF.
327 >         *  Compute specular scattering coefficient using BSDF.
328           */
329 <        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
329 >        if (!direct_specular_OK(ctmp, ldir, omega, np))
330                  return;
331 <        if (ldot > 0) {         /* pattern only diffuse reflection */
212 <                COLOR   ctmp1, ctmp2;
213 <                dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
214 <                                        : np->sd->rLambBack.cieY;
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);
220 <                addcolor(ctmp1, ctmp2);
221 <                multcolor(ctmp, ctmp1); /* apply derated pattern */
222 <                dtmp = ldot * omega;
223 <        } else {                        /* full pattern on transmission */
331 >        if (ldot < 0) {         /* pattern for specular transmission */
332                  multcolor(ctmp, np->pr->pcol);
333                  dtmp = -ldot * omega;
334 <        }
334 >        } else
335 >                dtmp = ldot * omega;
336          scalecolor(ctmp, dtmp);
337          addcolor(cval, ctmp);
338   }
# Line 261 | Line 370 | dir_brdf(
370          if (ambRayInPmap(np->pr))
371                  return;         /* specular already in photon map */
372          /*
373 <         *  Compute reflection coefficient using BSDF.
373 >         *  Compute specular reflection coefficient using BSDF.
374           */
375 <        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
375 >        if (!direct_specular_OK(ctmp, ldir, omega, np))
376                  return;
268                                        /* pattern only diffuse reflection */
269        dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
270                                : np->sd->rLambBack.cieY;
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);
275        addcolor(ctmp1, ctmp2);
276        multcolor(ctmp, ctmp1);         /* apply derated pattern */
377          dtmp = ldot * omega;
378          scalecolor(ctmp, dtmp);
379          addcolor(cval, ctmp);
# Line 312 | Line 412 | dir_btdf(
412          if (ambRayInPmap(np->pr))
413                  return;         /* specular already in photon map */
414          /*
415 <         *  Compute scattering coefficient using BSDF.
415 >         *  Compute specular scattering coefficient using BSDF.
416           */
417 <        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
417 >        if (!direct_specular_OK(ctmp, ldir, omega, np))
418                  return;
419                                          /* full pattern on transmission */
420          multcolor(ctmp, np->pr->pcol);
# Line 370 | Line 470 | sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usep
470                          continue;               /* Russian roulette victim */
471                  }
472                                                  /* need to offset origin? */
473 <                if (ndp->thick != 0 && ndp->pr->rod > 0 ^ vsmp[2] > 0)
473 >                if (ndp->thick != 0 && (ndp->pr->rod > 0) ^ (vsmp[2] > 0))
474                          VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
475                  rayvalue(&sr);                  /* send & evaluate sample */
476                  multcolor(sr.rcol, sr.rcoef);
# Line 388 | Line 488 | sample_sdf(BSDFDAT *ndp, int sflags)
488          COLORV          *unsc;
489  
490          if (sflags == SDsampSpT) {
491 <                unsc = ndp->tunsamp;
491 >                unsc = ndp->tdiff;
492                  if (ndp->pr->rod > 0)
493                          dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
494                  else
495                          dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
396                cvt_sdcolor(unsc, &ndp->sd->tLamb);
496          } else /* sflags == SDsampSpR */ {
497 <                unsc = ndp->runsamp;
498 <                if (ndp->pr->rod > 0) {
497 >                unsc = ndp->rdiff;
498 >                if (ndp->pr->rod > 0)
499                          dfp = ndp->sd->rf;
500 <                        cvt_sdcolor(unsc, &ndp->sd->rLambFront);
402 <                } else {
500 >                else
501                          dfp = ndp->sd->rb;
404                        cvt_sdcolor(unsc, &ndp->sd->rLambBack);
405                }
502          }
407        multcolor(unsc, ndp->pr->pcol);
503          if (dfp == NULL)                        /* no specular component? */
504                  return(0);
505                                                  /* below sampling threshold? */
# Line 458 | Line 553 | m_bsdf(OBJREC *m, RAY *r)
553          nd.thick = evalue(mf->ep[0]);
554          if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
555                  nd.thick = .0;
461                                                /* check shadow */
462        if (r->crtype & SHADOW) {
463                if (nd.thick != 0)
464                        raytrans(r);            /* pass-through */
465                return(1);                      /* or shadow */
466        }
556                                                  /* check backface visibility */
557          if (!hitfront & !backvis) {
558                  raytrans(r);
559                  return(1);
560          }
561                                                  /* check other rays to pass */
562 <        if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) ||
562 >        if (nd.thick != 0 && (r->crtype & SHADOW ||
563 >                                !(r->crtype & (SPECULAR|AMBIENT)) ||
564                                  (nd.thick > 0) ^ hitfront)) {
565                  raytrans(r);                    /* hide our proxy */
566                  return(1);
567          }
568 +        nd.mp = m;
569 +        nd.pr = r;
570                                                  /* get BSDF data */
571          nd.sd = loadBSDF(m->oargs.sarg[1]);
572 +                                                /* early shadow check */
573 +        if (r->crtype & SHADOW && (nd.sd->tf == NULL) & (nd.sd->tb == NULL))
574 +                return(1);
575                                                  /* diffuse reflectance */
576          if (hitfront) {
577 <                if (m->oargs.nfargs < 3)
578 <                        setcolor(nd.rdiff, .0, .0, .0);
579 <                else
485 <                        setcolor(nd.rdiff, m->oargs.farg[0],
577 >                cvt_sdcolor(nd.rdiff, &nd.sd->rLambFront);
578 >                if (m->oargs.nfargs >= 3) {
579 >                        setcolor(ctmp, m->oargs.farg[0],
580                                          m->oargs.farg[1],
581                                          m->oargs.farg[2]);
582 +                        addcolor(nd.rdiff, ctmp);
583 +                }
584          } else {
585 <                if (m->oargs.nfargs < 6)
586 <                        setcolor(nd.rdiff, .0, .0, .0);
587 <                else
492 <                        setcolor(nd.rdiff, m->oargs.farg[3],
585 >                cvt_sdcolor(nd.rdiff, &nd.sd->rLambBack);
586 >                if (m->oargs.nfargs >= 6) {
587 >                        setcolor(ctmp, m->oargs.farg[3],
588                                          m->oargs.farg[4],
589                                          m->oargs.farg[5]);
590 +                        addcolor(nd.rdiff, ctmp);
591 +                }
592          }
593                                                  /* diffuse transmittance */
594 <        if (m->oargs.nfargs < 9)
595 <                setcolor(nd.tdiff, .0, .0, .0);
596 <        else
500 <                setcolor(nd.tdiff, m->oargs.farg[6],
594 >        cvt_sdcolor(nd.tdiff, &nd.sd->tLamb);
595 >        if (m->oargs.nfargs >= 9) {
596 >                setcolor(ctmp, m->oargs.farg[6],
597                                  m->oargs.farg[7],
598                                  m->oargs.farg[8]);
599 <        nd.mp = m;
600 <        nd.pr = r;
599 >                addcolor(nd.tdiff, ctmp);
600 >        }
601                                                  /* get modifiers */
602          raytexture(r, m->omod);
603                                                  /* modify diffuse values */
# Line 529 | Line 625 | m_bsdf(OBJREC *m, RAY *r)
625                  nd.vray[2] = -r->rdir[2];
626                  ec = SDmapDir(nd.vray, nd.toloc, nd.vray);
627          }
532        if (!ec)
533                ec = SDinvXform(nd.fromloc, nd.toloc);
628          if (ec) {
629                  objerror(m, WARNING, "Illegal orientation vector");
630                  return(1);
631          }
632 <                                                /* determine BSDF resolution */
633 <        ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin+SDqueryMax, nd.sd);
632 >        compute_through(&nd);                   /* compute through component */
633 >        if (r->crtype & SHADOW) {
634 >                RAY     tr;                     /* attempt to pass shadow ray */
635 >                if (rayorigin(&tr, TRANS, r, nd.cthru) < 0)
636 >                        return(1);              /* blocked */
637 >                VCOPY(tr.rdir, r->rdir);
638 >                rayvalue(&tr);                  /* transmit with scaling */
639 >                multcolor(tr.rcol, tr.rcoef);
640 >                copycolor(r->rcol, tr.rcol);
641 >                return(1);                      /* we're done */
642 >        }
643 >        ec = SDinvXform(nd.fromloc, nd.toloc);
644 >        if (!ec)                                /* determine BSDF resolution */
645 >                ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL,
646 >                                        SDqueryMin+SDqueryMax, nd.sd);
647          if (ec)
648                  objerror(m, USER, transSDError(ec));
649  
# Line 552 | Line 659 | m_bsdf(OBJREC *m, RAY *r)
659                                                  /* sample transmission */
660          sample_sdf(&nd, SDsampSpT);
661                                                  /* compute indirect diffuse */
662 <        copycolor(ctmp, nd.rdiff);
556 <        addcolor(ctmp, nd.runsamp);
557 <        if (bright(ctmp) > FTINY) {             /* ambient from reflection */
662 >        if (bright(nd.rdiff) > FTINY) {         /* ambient from reflection */
663                  if (!hitfront)
664                          flipsurface(r);
665 +                copycolor(ctmp, nd.rdiff);
666                  multambient(ctmp, r, nd.pnorm);
667                  addcolor(r->rcol, ctmp);
668                  if (!hitfront)
669                          flipsurface(r);
670          }
671 <        copycolor(ctmp, nd.tdiff);
566 <        addcolor(ctmp, nd.tunsamp);
567 <        if (bright(ctmp) > FTINY) {             /* ambient from other side */
671 >        if (bright(nd.tdiff) > FTINY) {         /* ambient from other side */
672                  FVECT  bnorm;
673                  if (hitfront)
674                          flipsurface(r);
675                  bnorm[0] = -nd.pnorm[0];
676                  bnorm[1] = -nd.pnorm[1];
677                  bnorm[2] = -nd.pnorm[2];
678 +                copycolor(ctmp, nd.tdiff);
679                  if (nd.thick != 0) {            /* proxy with offset? */
680                          VCOPY(vtmp, r->rop);
681                          VSUM(r->rop, vtmp, r->ron, nd.thick);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines