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.33 by greg, Sun Feb 19 17:22:18 2017 UTC

# Line 73 | Line 73 | typedef struct {
73          RREAL   fromloc[3][3];  /* local BSDF coords to world */
74          double  thick;          /* surface thickness */
75          SDData  *sd;            /* loaded BSDF data */
76 <        COLOR   runsamp;        /* BSDF hemispherical reflection */
77 <        COLOR   rdiff;          /* added diffuse reflection */
78 <        COLOR   tunsamp;        /* BSDF hemispherical transmission */
79 <        COLOR   tdiff;          /* added diffuse transmission */
76 >        COLOR   rdiff;          /* diffuse reflection */
77 >        COLOR   tdiff;          /* diffuse transmission */
78   }  BSDFDAT;             /* BSDF material data */
79  
80   #define cvt_sdcolor(cv, svp)    ccy2rgb(&(svp)->spec, (svp)->cieY, cv)
# Line 95 | Line 93 | bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
93          normalize(vres);
94   }
95  
96 < /* Evaluate BSDF for direct component, returning true if OK to proceed */
96 > /* Get BSDF specular for direct component, returning true if OK to proceed */
97   static int
98 < direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
98 > direct_specular_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
99   {
100          int     nsamp, ok = 0;
101          FVECT   vsrc, vsmp, vjit;
102          double  tomega;
103          double  sf, tsr, sd[2];
104 <        COLOR   csmp;
104 >        COLOR   csmp, cdiff;
105 >        double  diffY;
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 +                                        /* will discount diffuse portion */
113 +        switch ((vsrc[2] > 0)<<1 | (ndp->vray[2] > 0)) {
114 +        case 3:
115 +                if (ndp->sd->rf == NULL)
116 +                        return(0);      /* all diffuse */
117 +                sv = ndp->sd->rLambFront;
118 +                break;
119 +        case 0:
120 +                if (ndp->sd->rb == NULL)
121 +                        return(0);      /* all diffuse */
122 +                sv = ndp->sd->rLambBack;
123 +                break;
124 +        default:
125 +                if ((ndp->sd->tf == NULL) & (ndp->sd->tb == NULL))
126 +                        return(0);      /* all diffuse */
127 +                sv = ndp->sd->tLamb;
128 +                break;
129 +        }
130 +        if (sv.cieY > FTINY) {
131 +                diffY = sv.cieY *= 1./PI;
132 +                cvt_sdcolor(cdiff, &sv);
133 +        } else {
134 +                diffY = .0;
135 +                setcolor(cdiff, .0, .0, .0);
136 +        }
137                                          /* assign number of samples */
138          ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
139          if (ec)
140                  goto baderror;
141                                          /* check indirect over-counting */
142          if (ndp->thick != 0 && ndp->pr->crtype & (SPECULAR|AMBIENT)
143 <                                && vsrc[2] > 0 ^ ndp->vray[2] > 0) {
143 >                                && (vsrc[2] > 0) ^ (ndp->vray[2] > 0)) {
144                  double  dx = vsrc[0] + ndp->vray[0];
145                  double  dy = vsrc[1] + ndp->vray[1];
146                  if (dx*dx + dy*dy <= omega+tomega)
# Line 149 | Line 173 | direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, B
173                  ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
174                  if (ec)
175                          goto baderror;
176 <                if (sv.cieY <= FTINY)   /* worth using? */
177 <                        continue;
176 >                if (sv.cieY - diffY <= FTINY) {
177 >                        addcolor(cval, cdiff);
178 >                        continue;       /* no specular part */
179 >                }
180                  cvt_sdcolor(csmp, &sv);
181 <                addcolor(cval, csmp);   /* average it in */
181 >                addcolor(cval, csmp);   /* else average it in */
182                  ++ok;
183          }
184 +        if (!ok) {
185 +                setcolor(cval, .0, .0, .0);
186 +                return(0);              /* no valid specular samples */
187 +        }
188          sf = 1./(double)nsamp;
189          scalecolor(cval, sf);
190 <        return(ok);
190 >                                        /* subtract diffuse contribution */
191 >        for (i = 3*(diffY > FTINY); i--; )
192 >                if ((colval(cval,i) -= colval(cdiff,i)) < .0)
193 >                        colval(cval,i) = .0;
194 >        return(1);
195   baderror:
196          objerror(ndp->mp, USER, transSDError(ec));
197          return(0);                      /* gratis return */
# Line 204 | Line 238 | dir_bsdf(
238          if (ambRayInPmap(np->pr))
239                  return;         /* specular already in photon map */
240          /*
241 <         *  Compute scattering coefficient using BSDF.
241 >         *  Compute specular scattering coefficient using BSDF.
242           */
243 <        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
243 >        if (!direct_specular_OK(ctmp, ldir, omega, np))
244                  return;
245 <        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 */
245 >        if (ldot < 0) {         /* pattern for specular transmission */
246                  multcolor(ctmp, np->pr->pcol);
247                  dtmp = -ldot * omega;
248 <        }
248 >        } else
249 >                dtmp = ldot * omega;
250          scalecolor(ctmp, dtmp);
251          addcolor(cval, ctmp);
252   }
# Line 261 | Line 284 | dir_brdf(
284          if (ambRayInPmap(np->pr))
285                  return;         /* specular already in photon map */
286          /*
287 <         *  Compute reflection coefficient using BSDF.
287 >         *  Compute specular reflection coefficient using BSDF.
288           */
289 <        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
289 >        if (!direct_specular_OK(ctmp, ldir, omega, np))
290                  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 */
291          dtmp = ldot * omega;
292          scalecolor(ctmp, dtmp);
293          addcolor(cval, ctmp);
# Line 312 | Line 326 | dir_btdf(
326          if (ambRayInPmap(np->pr))
327                  return;         /* specular already in photon map */
328          /*
329 <         *  Compute scattering coefficient using BSDF.
329 >         *  Compute specular scattering coefficient using BSDF.
330           */
331 <        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
331 >        if (!direct_specular_OK(ctmp, ldir, omega, np))
332                  return;
333                                          /* full pattern on transmission */
334          multcolor(ctmp, np->pr->pcol);
# Line 370 | Line 384 | sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usep
384                          continue;               /* Russian roulette victim */
385                  }
386                                                  /* need to offset origin? */
387 <                if (ndp->thick != 0 && ndp->pr->rod > 0 ^ vsmp[2] > 0)
387 >                if (ndp->thick != 0 && (ndp->pr->rod > 0) ^ (vsmp[2] > 0))
388                          VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
389                  rayvalue(&sr);                  /* send & evaluate sample */
390                  multcolor(sr.rcol, sr.rcoef);
# Line 388 | Line 402 | sample_sdf(BSDFDAT *ndp, int sflags)
402          COLORV          *unsc;
403  
404          if (sflags == SDsampSpT) {
405 <                unsc = ndp->tunsamp;
405 >                unsc = ndp->tdiff;
406                  if (ndp->pr->rod > 0)
407                          dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
408                  else
409                          dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
396                cvt_sdcolor(unsc, &ndp->sd->tLamb);
410          } else /* sflags == SDsampSpR */ {
411 <                unsc = ndp->runsamp;
412 <                if (ndp->pr->rod > 0) {
411 >                unsc = ndp->rdiff;
412 >                if (ndp->pr->rod > 0)
413                          dfp = ndp->sd->rf;
414 <                        cvt_sdcolor(unsc, &ndp->sd->rLambFront);
402 <                } else {
414 >                else
415                          dfp = ndp->sd->rb;
404                        cvt_sdcolor(unsc, &ndp->sd->rLambBack);
405                }
416          }
407        multcolor(unsc, ndp->pr->pcol);
417          if (dfp == NULL)                        /* no specular component? */
418                  return(0);
419                                                  /* below sampling threshold? */
# Line 475 | Line 484 | m_bsdf(OBJREC *m, RAY *r)
484                  raytrans(r);                    /* hide our proxy */
485                  return(1);
486          }
487 +        nd.mp = m;
488 +        nd.pr = r;
489                                                  /* get BSDF data */
490          nd.sd = loadBSDF(m->oargs.sarg[1]);
491                                                  /* diffuse reflectance */
492          if (hitfront) {
493 <                if (m->oargs.nfargs < 3)
494 <                        setcolor(nd.rdiff, .0, .0, .0);
495 <                else
485 <                        setcolor(nd.rdiff, m->oargs.farg[0],
493 >                cvt_sdcolor(nd.rdiff, &nd.sd->rLambFront);
494 >                if (m->oargs.nfargs >= 3) {
495 >                        setcolor(ctmp, m->oargs.farg[0],
496                                          m->oargs.farg[1],
497                                          m->oargs.farg[2]);
498 +                        addcolor(nd.rdiff, ctmp);
499 +                }
500          } else {
501 <                if (m->oargs.nfargs < 6)
502 <                        setcolor(nd.rdiff, .0, .0, .0);
503 <                else
492 <                        setcolor(nd.rdiff, m->oargs.farg[3],
501 >                cvt_sdcolor(nd.rdiff, &nd.sd->rLambBack);
502 >                if (m->oargs.nfargs >= 6) {
503 >                        setcolor(ctmp, m->oargs.farg[3],
504                                          m->oargs.farg[4],
505                                          m->oargs.farg[5]);
506 +                        addcolor(nd.rdiff, ctmp);
507 +                }
508          }
509                                                  /* diffuse transmittance */
510 <        if (m->oargs.nfargs < 9)
511 <                setcolor(nd.tdiff, .0, .0, .0);
512 <        else
500 <                setcolor(nd.tdiff, m->oargs.farg[6],
510 >        cvt_sdcolor(nd.tdiff, &nd.sd->tLamb);
511 >        if (m->oargs.nfargs >= 9) {
512 >                setcolor(ctmp, m->oargs.farg[6],
513                                  m->oargs.farg[7],
514                                  m->oargs.farg[8]);
515 <        nd.mp = m;
516 <        nd.pr = r;
515 >                addcolor(nd.tdiff, ctmp);
516 >        }
517                                                  /* get modifiers */
518          raytexture(r, m->omod);
519                                                  /* modify diffuse values */
# Line 552 | Line 564 | m_bsdf(OBJREC *m, RAY *r)
564                                                  /* sample transmission */
565          sample_sdf(&nd, SDsampSpT);
566                                                  /* compute indirect diffuse */
567 <        copycolor(ctmp, nd.rdiff);
556 <        addcolor(ctmp, nd.runsamp);
557 <        if (bright(ctmp) > FTINY) {             /* ambient from reflection */
567 >        if (bright(nd.rdiff) > FTINY) {         /* ambient from reflection */
568                  if (!hitfront)
569                          flipsurface(r);
570 +                copycolor(ctmp, nd.rdiff);
571                  multambient(ctmp, r, nd.pnorm);
572                  addcolor(r->rcol, ctmp);
573                  if (!hitfront)
574                          flipsurface(r);
575          }
576 <        copycolor(ctmp, nd.tdiff);
566 <        addcolor(ctmp, nd.tunsamp);
567 <        if (bright(ctmp) > FTINY) {             /* ambient from other side */
576 >        if (bright(nd.tdiff) > FTINY) {         /* ambient from other side */
577                  FVECT  bnorm;
578                  if (hitfront)
579                          flipsurface(r);
580                  bnorm[0] = -nd.pnorm[0];
581                  bnorm[1] = -nd.pnorm[1];
582                  bnorm[2] = -nd.pnorm[2];
583 +                copycolor(ctmp, nd.tdiff);
584                  if (nd.thick != 0) {            /* proxy with offset? */
585                          VCOPY(vtmp, r->rop);
586                          VSUM(r->rop, vtmp, r->ron, nd.thick);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines