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.5 by greg, Sun Feb 20 06:34:19 2011 UTC vs.
Revision 2.32 by greg, Sat Feb 18 19:12:49 2017 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 */
75          SDData  *sd;            /* loaded BSDF data */
76 <        COLOR   runsamp;        /* BSDF hemispherical reflection */
77 <        COLOR   rdiff;          /* added diffuse reflection */
77 <        COLOR   tunsamp;        /* BSDF hemispherical transmission */
78 <        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)
81  
82   /* Jitter ray sample according to projected solid angle and specjitter */
83   static void
84 < bsdf_jitter(FVECT vres, BSDFDAT *ndp)
84 > bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
85   {
87        double  sr_psa = ndp->sr_vpsa;
88
86          VCOPY(vres, ndp->vray);
87          if (specjitter < 1.)
88                  sr_psa *= specjitter;
# Line 96 | Line 93 | bsdf_jitter(FVECT vres, BSDFDAT *ndp)
93          normalize(vres);
94   }
95  
96 + /* Evaluate BSDF for direct component, returning true if OK to proceed */
97 + static int
98 + direct_bsdf_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, 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 *= 1./PI) > FTINY) {
131 +                diffY = sv.cieY;
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)) {
144 +                double  dx = vsrc[0] + ndp->vray[0];
145 +                double  dy = vsrc[1] + ndp->vray[1];
146 +                if (dx*dx + dy*dy <= omega+tomega)
147 +                        return(0);
148 +        }
149 +        sf = specjitter * ndp->pr->rweight;
150 +        if (tomega <= .0)
151 +                nsamp = 1;
152 +        else if (25.*tomega <= omega)
153 +                nsamp = 100.*sf + .5;
154 +        else
155 +                nsamp = 4.*sf*omega/tomega + .5;
156 +        nsamp += !nsamp;
157 +        setcolor(cval, .0, .0, .0);     /* sample our source area */
158 +        sf = sqrt(omega);
159 +        tsr = sqrt(tomega);
160 +        for (i = nsamp; i--; ) {
161 +                VCOPY(vsmp, vsrc);      /* jitter query directions */
162 +                if (nsamp > 1) {
163 +                        multisamp(sd, 2, (i + frandom())/(double)nsamp);
164 +                        vsmp[0] += (sd[0] - .5)*sf;
165 +                        vsmp[1] += (sd[1] - .5)*sf;
166 +                        if (normalize(vsmp) == 0) {
167 +                                --nsamp;
168 +                                continue;
169 +                        }
170 +                }
171 +                bsdf_jitter(vjit, ndp, tsr);
172 +                                        /* compute BSDF */
173 +                ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
174 +                if (ec)
175 +                        goto baderror;
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);   /* 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 +                                        /* 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 */
198 + }
199 +
200   /* Compute source contribution for BSDF (reflected & transmitted) */
201   static void
202   dir_bsdf(
# Line 106 | Line 207 | dir_bsdf(
207   )
208   {
209          BSDFDAT         *np = (BSDFDAT *)nnp;
109        SDError         ec;
110        SDValue         sv;
111        FVECT           vsrc;
112        FVECT           vjit;
210          double          ldot;
211          double          dtmp;
212          COLOR           ctmp;
# Line 120 | Line 217 | dir_bsdf(
217          if ((-FTINY <= ldot) & (ldot <= FTINY))
218                  return;
219  
220 <        if (ldot > .0 && bright(np->rdiff) > FTINY) {
220 >        if (ldot > 0 && bright(np->rdiff) > FTINY) {
221                  /*
222                   *  Compute added diffuse reflected component.
223                   */
# Line 129 | Line 226 | dir_bsdf(
226                  scalecolor(ctmp, dtmp);
227                  addcolor(cval, ctmp);
228          }
229 <        if (ldot < .0 && bright(np->tdiff) > FTINY) {
229 >        if (ldot < 0 && bright(np->tdiff) > FTINY) {
230                  /*
231                   *  Compute added diffuse transmission.
232                   */
# Line 138 | Line 235 | dir_bsdf(
235                  scalecolor(ctmp, dtmp);
236                  addcolor(cval, ctmp);
237          }
238 +        if (ambRayInPmap(np->pr))
239 +                return;         /* specular already in photon map */
240          /*
241           *  Compute scattering coefficient using BSDF.
242           */
243 <        if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone)
243 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
244                  return;
245 <        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 */
155 <                COLOR   ctmp1, ctmp2;
156 <                dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY
157 <                                        : np->sd->rLambBack.cieY;
158 <                dtmp /= PI * sv.cieY;   /* diffuse fraction */
159 <                copycolor(ctmp2, np->pr->pcol);
160 <                scalecolor(ctmp2, dtmp);
161 <                setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
162 <                addcolor(ctmp1, ctmp2);
163 <                multcolor(ctmp, ctmp1); /* apply derated pattern */
164 <                dtmp = ldot * omega;
165 <        } 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 180 | Line 261 | dir_brdf(
261   )
262   {
263          BSDFDAT         *np = (BSDFDAT *)nnp;
183        SDError         ec;
184        SDValue         sv;
185        FVECT           vsrc;
186        FVECT           vjit;
264          double          ldot;
265          double          dtmp;
266          COLOR           ctmp, ctmp1, ctmp2;
# Line 204 | Line 281 | dir_brdf(
281                  scalecolor(ctmp, dtmp);
282                  addcolor(cval, ctmp);
283          }
284 +        if (ambRayInPmap(np->pr))
285 +                return;         /* specular already in photon map */
286          /*
287           *  Compute reflection coefficient using BSDF.
288           */
289 <        if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone)
289 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
290                  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);
220                                        /* pattern only diffuse reflection */
221        dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY
222                                : np->sd->rLambBack.cieY;
223        dtmp /= PI * sv.cieY;           /* diffuse fraction */
224        copycolor(ctmp2, np->pr->pcol);
225        scalecolor(ctmp2, dtmp);
226        setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
227        addcolor(ctmp1, ctmp2);
228        multcolor(ctmp, ctmp1);         /* apply derated pattern */
291          dtmp = ldot * omega;
292          scalecolor(ctmp, dtmp);
293          addcolor(cval, ctmp);
# Line 241 | Line 303 | dir_btdf(
303   )
304   {
305          BSDFDAT         *np = (BSDFDAT *)nnp;
244        SDError         ec;
245        SDValue         sv;
246        FVECT           vsrc;
247        FVECT           vjit;
306          double          ldot;
307          double          dtmp;
308          COLOR           ctmp;
# Line 265 | Line 323 | dir_btdf(
323                  scalecolor(ctmp, dtmp);
324                  addcolor(cval, ctmp);
325          }
326 +        if (ambRayInPmap(np->pr))
327 +                return;         /* specular already in photon map */
328          /*
329           *  Compute scattering coefficient using BSDF.
330           */
331 <        if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone)
331 >        if (!direct_bsdf_OK(ctmp, ldir, omega, np))
332                  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);
333                                          /* full pattern on transmission */
334          multcolor(ctmp, np->pr->pcol);
335          dtmp = -ldot * omega;
# Line 290 | Line 342 | static int
342   sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
343   {
344          int     nstarget = 1;
345 <        int     nsent = 0;
345 >        int     nsent;
346          SDError ec;
347          SDValue bsv;
348 <        double  sthick;
349 <        FVECT   vjit, vsmp;
348 >        double  xrand;
349 >        FVECT   vsmp;
350          RAY     sr;
299        int     ntrials;
351                                                  /* multiple samples? */
352          if (specjitter > 1.5) {
353                  nstarget = specjitter*ndp->pr->rweight + .5;
354 <                if (nstarget < 1)
304 <                        nstarget = 1;
354 >                nstarget += !nstarget;
355          }
356 <                                                /* run through our trials */
357 <        for (ntrials = 0; nsent < nstarget && ntrials < 9*nstarget; ntrials++) {
358 <                SDerrorDetail[0] = '\0';
359 <                                                /* sample direction & coef. */
360 <                bsdf_jitter(vjit, ndp);
361 <                ec = SDsampComponent(&bsv, vsmp, vjit, ntrials ? frandom()
362 <                                : urand(ilhash(dimlist,ndims)+samplendx), dcp);
356 >                                                /* run through our samples */
357 >        for (nsent = 0; nsent < nstarget; nsent++) {
358 >                if (nstarget == 1) {            /* stratify random variable */
359 >                        xrand = urand(ilhash(dimlist,ndims)+samplendx);
360 >                        if (specjitter < 1.)
361 >                                xrand = .5 + specjitter*(xrand-.5);
362 >                } else {
363 >                        xrand = (nsent + frandom())/(double)nstarget;
364 >                }
365 >                SDerrorDetail[0] = '\0';        /* sample direction & coef. */
366 >                bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]);
367 >                ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
368                  if (ec)
369                          objerror(ndp->mp, USER, transSDError(ec));
370 <                                                /* zero component? */
316 <                if (bsv.cieY <= FTINY)
370 >                if (bsv.cieY <= FTINY)          /* zero component? */
371                          break;
372                                                  /* map vector to world */
373                  if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
374                          break;
321                                                /* unintentional penetration? */
322                if (DOT(sr.rdir, ndp->pr->ron) > .0 ^ vsmp[2] > .0)
323                        continue;
375                                                  /* spawn a specular ray */
376                  if (nstarget > 1)
377                          bsv.cieY /= (double)nstarget;
378 <                cvt_sdcolor(sr.rcoef, &bsv);    /* use color */
379 <                if (usepat)                     /* pattern on transmission */
378 >                cvt_sdcolor(sr.rcoef, &bsv);    /* use sample color */
379 >                if (usepat)                     /* apply pattern? */
380                          multcolor(sr.rcoef, ndp->pr->pcol);
381                  if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
382 <                        if (maxdepth  > 0)
382 >                        if (maxdepth > 0)
383                                  break;
384 <                        ++nsent;                /* Russian roulette victim */
334 <                        continue;
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);
391                  addcolor(ndp->pr->rcol, sr.rcol);
342                ++nsent;
392          }
393          return(nsent);
394   }
# Line 353 | Line 402 | sample_sdf(BSDFDAT *ndp, int sflags)
402          COLORV          *unsc;
403  
404          if (sflags == SDsampSpT) {
405 <                unsc = ndp->tunsamp;
406 <                dfp = ndp->sd->tf;
407 <                cvt_sdcolor(unsc, &ndp->sd->tLamb);
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;
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);
364 <                } else {
414 >                else
415                          dfp = ndp->sd->rb;
366                        cvt_sdcolor(unsc, &ndp->sd->rLambBack);
367                }
416          }
369        multcolor(unsc, ndp->pr->pcol);
417          if (dfp == NULL)                        /* no specular component? */
418                  return(0);
419                                                  /* below sampling threshold? */
# Line 375 | Line 422 | sample_sdf(BSDFDAT *ndp, int sflags)
422                          FVECT   vjit;
423                          double  d;
424                          COLOR   ctmp;
425 <                        bsdf_jitter(vjit, ndp);
425 >                        bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
426                          d = SDdirectHemi(vjit, sflags, ndp->sd);
427                          if (sflags == SDsampSpT) {
428                                  copycolor(ctmp, ndp->pr->pcol);
# Line 401 | Line 448 | sample_sdf(BSDFDAT *ndp, int sflags)
448   int
449   m_bsdf(OBJREC *m, RAY *r)
450   {
451 +        int     hitfront;
452          COLOR   ctmp;
453          SDError ec;
454          FVECT   upvec, vtmp;
# Line 410 | Line 458 | m_bsdf(OBJREC *m, RAY *r)
458          if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) |
459                                  (m->oargs.nfargs % 3))
460                  objerror(m, USER, "bad # arguments");
461 <
461 >                                                /* record surface struck */
462 >        hitfront = (r->rod > 0);
463                                                  /* load cal file */
464          mf = getfunc(m, 5, 0x1d, 1);
465 +        setfunc(m, r);
466                                                  /* get thickness */
467          nd.thick = evalue(mf->ep[0]);
468          if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
469                  nd.thick = .0;
470                                                  /* check shadow */
471          if (r->crtype & SHADOW) {
472 <                if (nd.thick != .0)
472 >                if (nd.thick != 0)
473                          raytrans(r);            /* pass-through */
474                  return(1);                      /* or shadow */
475          }
476 +                                                /* check backface visibility */
477 +        if (!hitfront & !backvis) {
478 +                raytrans(r);
479 +                return(1);
480 +        }
481                                                  /* check other rays to pass */
482          if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) ||
483 <                                nd.thick > .0 ^ r->rod > .0)) {
483 >                                (nd.thick > 0) ^ hitfront)) {
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 (r->rod > .0) {
493 <                if (m->oargs.nfargs < 3)
494 <                        setcolor(nd.rdiff, .0, .0, .0);
495 <                else
439 <                        setcolor(nd.rdiff, m->oargs.farg[0],
492 >        if (hitfront) {
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) {      /* check invisible backside */
502 <                        if (!backvis && (nd.sd->rb == NULL) &
503 <                                                (nd.sd->tf == NULL)) {
446 <                                SDfreeCache(nd.sd);
447 <                                raytrans(r);
448 <                                return(1);
449 <                        }
450 <                        setcolor(nd.rdiff, .0, .0, .0);
451 <                } else
452 <                        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
460 <                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 472 | Line 524 | m_bsdf(OBJREC *m, RAY *r)
524          upvec[1] = evalue(mf->ep[2]);
525          upvec[2] = evalue(mf->ep[3]);
526                                                  /* return to world coords */
527 <        if (mf->f != &unitxf) {
528 <                multv3(upvec, upvec, mf->f->xfm);
529 <                nd.thick *= mf->f->sca;
527 >        if (mf->fxp != &unitxf) {
528 >                multv3(upvec, upvec, mf->fxp->xfm);
529 >                nd.thick *= mf->fxp->sca;
530          }
531 +        if (r->rox != NULL) {
532 +                multv3(upvec, upvec, r->rox->f.xfm);
533 +                nd.thick *= r->rox->f.sca;
534 +        }
535          raynormal(nd.pnorm, r);
536                                                  /* compute local BSDF xform */
537          ec = SDcompXform(nd.toloc, nd.pnorm, upvec);
# Line 487 | Line 543 | m_bsdf(OBJREC *m, RAY *r)
543          }
544          if (!ec)
545                  ec = SDinvXform(nd.fromloc, nd.toloc);
546 <                                                /* determine BSDF resolution */
547 <        if (!ec)
492 <                ec = SDsizeBSDF(&nd.sr_vpsa, nd.vray, SDqueryMin, nd.sd);
493 <        if (!ec)
494 <                nd.sr_vpsa = sqrt(nd.sr_vpsa);
495 <        else {
496 <                objerror(m, WARNING, transSDError(ec));
497 <                SDfreeCache(nd.sd);
546 >        if (ec) {
547 >                objerror(m, WARNING, "Illegal orientation vector");
548                  return(1);
549          }
550 <        if (r->rod < .0) {                      /* perturb normal towards hit */
550 >                                                /* determine BSDF resolution */
551 >        ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin+SDqueryMax, nd.sd);
552 >        if (ec)
553 >                objerror(m, USER, transSDError(ec));
554 >
555 >        nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]);
556 >        nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]);
557 >        if (!hitfront) {                        /* perturb normal towards hit */
558                  nd.pnorm[0] = -nd.pnorm[0];
559                  nd.pnorm[1] = -nd.pnorm[1];
560                  nd.pnorm[2] = -nd.pnorm[2];
# Line 507 | 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);
568 <        addcolor(ctmp, nd.runsamp);
512 <        if (bright(ctmp) > FTINY) {             /* ambient from reflection */
513 <                if (r->rod < .0)
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 (r->rod < .0)
573 >                if (!hitfront)
574                          flipsurface(r);
575          }
576 <        copycolor(ctmp, nd.tdiff);
521 <        addcolor(ctmp, nd.tunsamp);
522 <        if (bright(ctmp) > FTINY) {             /* ambient from other side */
576 >        if (bright(nd.tdiff) > FTINY) {         /* ambient from other side */
577                  FVECT  bnorm;
578 <                if (r->rod > .0)
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 <                if (nd.thick != .0) {           /* proxy with offset? */
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);
586 >                        VSUM(r->rop, vtmp, r->ron, nd.thick);
587                          multambient(ctmp, r, bnorm);
588                          VCOPY(r->rop, vtmp);
589                  } else
590                          multambient(ctmp, r, bnorm);
591                  addcolor(r->rcol, ctmp);
592 <                if (r->rod > .0)
592 >                if (hitfront)
593                          flipsurface(r);
594          }
595                                                  /* add direct component */
596 <        if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL)) {
596 >        if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) &
597 >                                        (nd.sd->tb == NULL)) {
598                  direct(r, dir_brdf, &nd);       /* reflection only */
599 <        } else if (nd.thick == .0) {
599 >        } else if (nd.thick == 0) {
600                  direct(r, dir_bsdf, &nd);       /* thin surface scattering */
601          } else {
602                  direct(r, dir_brdf, &nd);       /* reflection first */
603                  VCOPY(vtmp, r->rop);            /* offset for transmitted */
604                  VSUM(r->rop, vtmp, r->ron, -nd.thick);
605 <                direct(r, dir_btdf, &nd);
605 >                direct(r, dir_btdf, &nd);       /* separate transmission */
606                  VCOPY(r->rop, vtmp);
607          }
608                                                  /* clean up */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines