ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_bsdf.c
Revision: 2.46
Committed: Mon Feb 12 18:46:29 2018 UTC (6 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.45: +8 -8 lines
Log Message:
Fixed issue with under-counting specular

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.46 static const char RCSid[] = "$Id: m_bsdf.c,v 2.45 2018/01/05 02:47:46 greg Exp $";
3 greg 2.1 #endif
4     /*
5     * Shading for materials with BSDFs taken from XML data files
6     */
7    
8     #include "copyright.h"
9    
10     #include "ray.h"
11     #include "ambient.h"
12     #include "source.h"
13     #include "func.h"
14     #include "bsdf.h"
15     #include "random.h"
16 greg 2.30 #include "pmapmat.h"
17 greg 2.1
18     /*
19     * Arguments to this material include optional diffuse colors.
20     * String arguments include the BSDF and function files.
21 greg 2.5 * A non-zero thickness causes the strange but useful behavior
22     * of translating transmitted rays this distance beneath the surface
23     * (opposite the surface normal) to bypass any intervening geometry.
24     * Translation only affects scattered, non-source-directed samples.
25     * A non-zero thickness has the further side-effect that an unscattered
26 greg 2.35 * (view) ray will pass right through our material, making the BSDF
27     * surface invisible and showing the proxied geometry instead. Thickness
28     * has the further effect of turning off reflection on the reverse side so
29     * rays heading in the opposite direction pass unimpeded through the BSDF
30 greg 2.5 * surface. A paired surface may be placed on the opposide side of
31     * the detail geometry, less than this thickness away, if a two-way
32     * proxy is desired. Note that the sign of the thickness is important.
33     * A positive thickness hides geometry behind the BSDF surface and uses
34     * front reflectance and transmission properties. A negative thickness
35     * hides geometry in front of the surface when rays hit from behind,
36     * and applies only the transmission and backside reflectance properties.
37     * Reflection is ignored on the hidden side, as those rays pass through.
38 greg 2.35 * 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 greg 2.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 greg 2.1 * 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
46     * system for the BSDF.
47     * We do not reorient the surface, so if the BSDF has no back-side
48 greg 2.5 * reflectance and none is given in the real arguments, a BSDF surface
49     * with zero thickness will appear black when viewed from behind
50 greg 2.35 * unless backface visibility is on, when it becomes invisible.
51 greg 2.5 * The diffuse arguments are added to components in the BSDF file,
52 greg 2.1 * not multiplied. However, patterns affect this material as a multiplier
53     * on everything except non-diffuse reflection.
54     *
55     * Arguments for MAT_BSDF are:
56     * 6+ thick BSDFfile ux uy uz funcfile transform
57     * 0
58 greg 2.8 * 0|3|6|9 rdf gdf bdf
59 greg 2.1 * rdb gdb bdb
60     * rdt gdt bdt
61     */
62    
63 greg 2.4 /*
64     * Note that our reverse ray-tracing process means that the positions
65     * of incoming and outgoing vectors may be reversed in our calls
66 greg 2.35 * to the BSDF library. This is usually fine, since the bidirectional nature
67 greg 2.4 * of the BSDF (that's what the 'B' stands for) means it all works out.
68     */
69    
70 greg 2.1 typedef struct {
71     OBJREC *mp; /* material pointer */
72     RAY *pr; /* intersected ray */
73     FVECT pnorm; /* perturbed surface normal */
74 greg 2.4 FVECT vray; /* local outgoing (return) vector */
75 greg 2.9 double sr_vpsa[2]; /* sqrt of BSDF projected solid angle extrema */
76 greg 2.1 RREAL toloc[3][3]; /* world to local BSDF coords */
77     RREAL fromloc[3][3]; /* local BSDF coords to world */
78     double thick; /* surface thickness */
79 greg 2.35 COLOR cthru; /* "through" component multiplier */
80 greg 2.1 SDData *sd; /* loaded BSDF data */
81 greg 2.31 COLOR rdiff; /* diffuse reflection */
82 greg 2.39 COLOR runsamp; /* BSDF hemispherical reflection */
83 greg 2.31 COLOR tdiff; /* diffuse transmission */
84 greg 2.39 COLOR tunsamp; /* BSDF hemispherical transmission */
85 greg 2.1 } BSDFDAT; /* BSDF material data */
86    
87     #define cvt_sdcolor(cv, svp) ccy2rgb(&(svp)->spec, (svp)->cieY, cv)
88    
89 greg 2.35 /* Compute "through" component color */
90 greg 2.34 static void
91     compute_through(BSDFDAT *ndp)
92     {
93     #define NDIR2CHECK 13
94     static const float dir2check[NDIR2CHECK][2] = {
95     {0, 0},
96     {-0.8, 0},
97     {0, 0.8},
98     {0, -0.8},
99     {0.8, 0},
100     {-0.8, 0.8},
101     {-0.8, -0.8},
102     {0.8, 0.8},
103     {0.8, -0.8},
104     {-1.6, 0},
105     {0, 1.6},
106     {0, -1.6},
107     {1.6, 0},
108     };
109 greg 2.45 const double peak_over = 1.5;
110 greg 2.34 SDSpectralDF *dfp;
111     FVECT pdir;
112     double tomega, srchrad;
113     COLOR vpeak, vsum;
114 greg 2.40 int i;
115 greg 2.34 SDError ec;
116    
117 greg 2.40 setcolor(ndp->cthru, 0, 0, 0); /* starting assumption */
118 greg 2.38
119 greg 2.46 if (ndp->pr->crtype & (SPECULAR|AMBIENT) && !(ndp->pr->crtype & SHADOW))
120     return; /* no need for through comp. */
121    
122 greg 2.34 if (ndp->pr->rod > 0)
123     dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
124     else
125     dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
126    
127     if (dfp == NULL)
128     return; /* no specular transmission */
129     if (bright(ndp->pr->pcol) <= FTINY)
130     return; /* pattern is black, here */
131     srchrad = sqrt(dfp->minProjSA); /* else search for peak */
132 greg 2.40 setcolor(vpeak, 0, 0, 0);
133     setcolor(vsum, 0, 0, 0);
134 greg 2.42 pdir[2] = 0.0;
135 greg 2.34 for (i = 0; i < NDIR2CHECK; i++) {
136     FVECT tdir;
137     SDValue sv;
138     COLOR vcol;
139     tdir[0] = -ndp->vray[0] + dir2check[i][0]*srchrad;
140     tdir[1] = -ndp->vray[1] + dir2check[i][1]*srchrad;
141     tdir[2] = -ndp->vray[2];
142 greg 2.36 normalize(tdir);
143 greg 2.34 ec = SDevalBSDF(&sv, tdir, ndp->vray, ndp->sd);
144     if (ec)
145     goto baderror;
146     cvt_sdcolor(vcol, &sv);
147     addcolor(vsum, vcol);
148 greg 2.45 if (sv.cieY > bright(vpeak)) {
149 greg 2.34 copycolor(vpeak, vcol);
150     VCOPY(pdir, tdir);
151     }
152     }
153 greg 2.42 if (pdir[2] == 0.0)
154     return; /* zero neighborhood */
155 greg 2.34 ec = SDsizeBSDF(&tomega, pdir, ndp->vray, SDqueryMin, ndp->sd);
156     if (ec)
157     goto baderror;
158     if (tomega > 1.5*dfp->minProjSA)
159     return; /* not really a peak? */
160 greg 2.43 tomega /= fabs(pdir[2]); /* remove cosine factor */
161 greg 2.40 if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .001)
162     return; /* < 0.1% transmission */
163 greg 2.34 for (i = 3; i--; ) /* remove peak from average */
164     colval(vsum,i) -= colval(vpeak,i);
165 greg 2.40 if (peak_over*bright(vsum) >= (NDIR2CHECK-1)*bright(vpeak))
166 greg 2.34 return; /* not peaky enough */
167     copycolor(ndp->cthru, vpeak); /* else use it */
168     scalecolor(ndp->cthru, tomega);
169     multcolor(ndp->cthru, ndp->pr->pcol); /* modify by pattern */
170     return;
171     baderror:
172     objerror(ndp->mp, USER, transSDError(ec));
173     #undef NDIR2CHECK
174     }
175    
176 greg 2.4 /* Jitter ray sample according to projected solid angle and specjitter */
177     static void
178 greg 2.15 bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
179 greg 2.4 {
180     VCOPY(vres, ndp->vray);
181     if (specjitter < 1.)
182     sr_psa *= specjitter;
183     if (sr_psa <= FTINY)
184     return;
185     vres[0] += sr_psa*(.5 - frandom());
186     vres[1] += sr_psa*(.5 - frandom());
187     normalize(vres);
188     }
189    
190 greg 2.33 /* Get BSDF specular for direct component, returning true if OK to proceed */
191 greg 2.7 static int
192 greg 2.33 direct_specular_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
193 greg 2.7 {
194 greg 2.43 int nsamp;
195     double wtot = 0;
196 greg 2.13 FVECT vsrc, vsmp, vjit;
197 greg 2.36 double tomega, tomega2;
198 greg 2.15 double sf, tsr, sd[2];
199 greg 2.32 COLOR csmp, cdiff;
200     double diffY;
201 greg 2.7 SDValue sv;
202     SDError ec;
203 greg 2.13 int i;
204 greg 2.37 /* in case we fail */
205 greg 2.40 setcolor(cval, 0, 0, 0);
206 greg 2.7 /* transform source direction */
207     if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
208     return(0);
209 greg 2.32 /* will discount diffuse portion */
210     switch ((vsrc[2] > 0)<<1 | (ndp->vray[2] > 0)) {
211     case 3:
212     if (ndp->sd->rf == NULL)
213     return(0); /* all diffuse */
214     sv = ndp->sd->rLambFront;
215     break;
216     case 0:
217     if (ndp->sd->rb == NULL)
218     return(0); /* all diffuse */
219     sv = ndp->sd->rLambBack;
220     break;
221     default:
222     if ((ndp->sd->tf == NULL) & (ndp->sd->tb == NULL))
223     return(0); /* all diffuse */
224     sv = ndp->sd->tLamb;
225     break;
226     }
227 greg 2.33 if (sv.cieY > FTINY) {
228     diffY = sv.cieY *= 1./PI;
229 greg 2.32 cvt_sdcolor(cdiff, &sv);
230     } else {
231 greg 2.40 diffY = 0;
232     setcolor(cdiff, 0, 0, 0);
233 greg 2.32 }
234 greg 2.37 /* need projected solid angles */
235     omega *= fabs(vsrc[2]);
236 greg 2.16 ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
237     if (ec)
238     goto baderror;
239 greg 2.13 /* check indirect over-counting */
240 greg 2.40 if ((vsrc[2] > 0) ^ (ndp->vray[2] > 0) && bright(ndp->cthru) > FTINY) {
241 greg 2.13 double dx = vsrc[0] + ndp->vray[0];
242     double dy = vsrc[1] + ndp->vray[1];
243 greg 2.37 if (dx*dx + dy*dy <= (4./PI)*(omega + tomega +
244     2.*sqrt(omega*tomega)))
245 greg 2.7 return(0);
246     }
247 greg 2.37 /* assign number of samples */
248 greg 2.15 sf = specjitter * ndp->pr->rweight;
249 greg 2.40 if (tomega <= 0)
250 greg 2.24 nsamp = 1;
251     else if (25.*tomega <= omega)
252 greg 2.15 nsamp = 100.*sf + .5;
253     else
254     nsamp = 4.*sf*omega/tomega + .5;
255     nsamp += !nsamp;
256 greg 2.37 sf = sqrt(omega); /* sample our source area */
257 greg 2.15 tsr = sqrt(tomega);
258 greg 2.13 for (i = nsamp; i--; ) {
259     VCOPY(vsmp, vsrc); /* jitter query directions */
260     if (nsamp > 1) {
261     multisamp(sd, 2, (i + frandom())/(double)nsamp);
262     vsmp[0] += (sd[0] - .5)*sf;
263     vsmp[1] += (sd[1] - .5)*sf;
264 greg 2.36 normalize(vsmp);
265 greg 2.13 }
266 greg 2.15 bsdf_jitter(vjit, ndp, tsr);
267 greg 2.37 /* compute BSDF */
268     ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
269     if (ec)
270     goto baderror;
271     if (sv.cieY - diffY <= FTINY)
272     continue; /* no specular part */
273 greg 2.36 /* check for variable resolution */
274     ec = SDsizeBSDF(&tomega2, vjit, vsmp, SDqueryMin, ndp->sd);
275     if (ec)
276     goto baderror;
277     if (tomega2 < .12*tomega)
278     continue; /* not safe to include */
279 greg 2.13 cvt_sdcolor(csmp, &sv);
280 greg 2.44
281     if (sf < 2.5*tsr) { /* weight by Y for small sources */
282     scalecolor(csmp, sv.cieY);
283     wtot += sv.cieY;
284     } else
285     wtot += 1.;
286 greg 2.43 addcolor(cval, csmp);
287 greg 2.13 }
288 greg 2.43 if (wtot <= FTINY) /* no valid specular samples? */
289 greg 2.37 return(0);
290    
291 greg 2.43 sf = 1./wtot; /* weighted average BSDF */
292 greg 2.13 scalecolor(cval, sf);
293 greg 2.32 /* subtract diffuse contribution */
294     for (i = 3*(diffY > FTINY); i--; )
295 greg 2.40 if ((colval(cval,i) -= colval(cdiff,i)) < 0)
296     colval(cval,i) = 0;
297 greg 2.32 return(1);
298 greg 2.13 baderror:
299     objerror(ndp->mp, USER, transSDError(ec));
300 greg 2.17 return(0); /* gratis return */
301 greg 2.7 }
302    
303 greg 2.5 /* Compute source contribution for BSDF (reflected & transmitted) */
304 greg 2.1 static void
305 greg 2.5 dir_bsdf(
306 greg 2.1 COLOR cval, /* returned coefficient */
307     void *nnp, /* material data */
308     FVECT ldir, /* light source direction */
309     double omega /* light source size */
310     )
311     {
312 greg 2.3 BSDFDAT *np = (BSDFDAT *)nnp;
313 greg 2.1 double ldot;
314     double dtmp;
315     COLOR ctmp;
316    
317 greg 2.40 setcolor(cval, 0, 0, 0);
318 greg 2.1
319     ldot = DOT(np->pnorm, ldir);
320     if ((-FTINY <= ldot) & (ldot <= FTINY))
321     return;
322    
323 greg 2.9 if (ldot > 0 && bright(np->rdiff) > FTINY) {
324 greg 2.1 /*
325 greg 2.39 * Compute diffuse reflected component
326 greg 2.1 */
327     copycolor(ctmp, np->rdiff);
328     dtmp = ldot * omega * (1./PI);
329     scalecolor(ctmp, dtmp);
330     addcolor(cval, ctmp);
331     }
332 greg 2.9 if (ldot < 0 && bright(np->tdiff) > FTINY) {
333 greg 2.1 /*
334 greg 2.39 * Compute diffuse transmission
335 greg 2.1 */
336     copycolor(ctmp, np->tdiff);
337     dtmp = -ldot * omega * (1.0/PI);
338     scalecolor(ctmp, dtmp);
339     addcolor(cval, ctmp);
340     }
341 greg 2.30 if (ambRayInPmap(np->pr))
342     return; /* specular already in photon map */
343 greg 2.1 /*
344 greg 2.39 * Compute specular scattering coefficient using BSDF
345 greg 2.1 */
346 greg 2.33 if (!direct_specular_OK(ctmp, ldir, omega, np))
347 greg 2.1 return;
348 greg 2.31 if (ldot < 0) { /* pattern for specular transmission */
349 greg 2.1 multcolor(ctmp, np->pr->pcol);
350     dtmp = -ldot * omega;
351 greg 2.31 } else
352     dtmp = ldot * omega;
353 greg 2.1 scalecolor(ctmp, dtmp);
354     addcolor(cval, ctmp);
355     }
356    
357 greg 2.5 /* Compute source contribution for BSDF (reflected only) */
358     static void
359     dir_brdf(
360     COLOR cval, /* returned coefficient */
361     void *nnp, /* material data */
362     FVECT ldir, /* light source direction */
363     double omega /* light source size */
364     )
365     {
366     BSDFDAT *np = (BSDFDAT *)nnp;
367     double ldot;
368     double dtmp;
369     COLOR ctmp, ctmp1, ctmp2;
370    
371 greg 2.40 setcolor(cval, 0, 0, 0);
372 greg 2.5
373     ldot = DOT(np->pnorm, ldir);
374    
375     if (ldot <= FTINY)
376     return;
377    
378     if (bright(np->rdiff) > FTINY) {
379     /*
380 greg 2.39 * Compute diffuse reflected component
381 greg 2.5 */
382     copycolor(ctmp, np->rdiff);
383     dtmp = ldot * omega * (1./PI);
384     scalecolor(ctmp, dtmp);
385     addcolor(cval, ctmp);
386     }
387 greg 2.30 if (ambRayInPmap(np->pr))
388     return; /* specular already in photon map */
389 greg 2.5 /*
390 greg 2.39 * Compute specular reflection coefficient using BSDF
391 greg 2.5 */
392 greg 2.33 if (!direct_specular_OK(ctmp, ldir, omega, np))
393 greg 2.5 return;
394     dtmp = ldot * omega;
395     scalecolor(ctmp, dtmp);
396     addcolor(cval, ctmp);
397     }
398    
399     /* Compute source contribution for BSDF (transmitted only) */
400     static void
401     dir_btdf(
402     COLOR cval, /* returned coefficient */
403     void *nnp, /* material data */
404     FVECT ldir, /* light source direction */
405     double omega /* light source size */
406     )
407     {
408     BSDFDAT *np = (BSDFDAT *)nnp;
409     double ldot;
410     double dtmp;
411     COLOR ctmp;
412    
413 greg 2.40 setcolor(cval, 0, 0, 0);
414 greg 2.5
415     ldot = DOT(np->pnorm, ldir);
416    
417     if (ldot >= -FTINY)
418     return;
419    
420     if (bright(np->tdiff) > FTINY) {
421     /*
422 greg 2.39 * Compute diffuse transmission
423 greg 2.5 */
424     copycolor(ctmp, np->tdiff);
425     dtmp = -ldot * omega * (1.0/PI);
426     scalecolor(ctmp, dtmp);
427     addcolor(cval, ctmp);
428     }
429 greg 2.30 if (ambRayInPmap(np->pr))
430     return; /* specular already in photon map */
431 greg 2.5 /*
432 greg 2.39 * Compute specular scattering coefficient using BSDF
433 greg 2.5 */
434 greg 2.33 if (!direct_specular_OK(ctmp, ldir, omega, np))
435 greg 2.5 return;
436     /* full pattern on transmission */
437     multcolor(ctmp, np->pr->pcol);
438     dtmp = -ldot * omega;
439     scalecolor(ctmp, dtmp);
440     addcolor(cval, ctmp);
441     }
442    
443 greg 2.1 /* Sample separate BSDF component */
444     static int
445 greg 2.40 sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int xmit)
446 greg 2.1 {
447 greg 2.46 const int hasthru = (xmit && bright(ndp->cthru) > FTINY);
448 greg 2.41 int nstarget = 1;
449     int nsent = 0;
450     int n;
451     SDError ec;
452     SDValue bsv;
453     double xrand;
454     FVECT vsmp, vinc;
455     RAY sr;
456 greg 2.1 /* multiple samples? */
457     if (specjitter > 1.5) {
458     nstarget = specjitter*ndp->pr->rweight + .5;
459 greg 2.14 nstarget += !nstarget;
460 greg 2.1 }
461 greg 2.11 /* run through our samples */
462 greg 2.40 for (n = 0; n < nstarget; n++) {
463 greg 2.15 if (nstarget == 1) { /* stratify random variable */
464 greg 2.11 xrand = urand(ilhash(dimlist,ndims)+samplendx);
465 greg 2.15 if (specjitter < 1.)
466     xrand = .5 + specjitter*(xrand-.5);
467     } else {
468 greg 2.40 xrand = (n + frandom())/(double)nstarget;
469 greg 2.15 }
470 greg 2.11 SDerrorDetail[0] = '\0'; /* sample direction & coef. */
471 greg 2.15 bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]);
472 greg 2.40 VCOPY(vinc, vsmp); /* to compare after */
473 greg 2.11 ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
474 greg 2.1 if (ec)
475 greg 2.2 objerror(ndp->mp, USER, transSDError(ec));
476 greg 2.11 if (bsv.cieY <= FTINY) /* zero component? */
477 greg 2.1 break;
478 greg 2.40 if (hasthru) { /* check for view ray */
479     double dx = vinc[0] + vsmp[0];
480     double dy = vinc[1] + vsmp[1];
481     if (dx*dx + dy*dy <= ndp->sr_vpsa[0]*ndp->sr_vpsa[0])
482     continue; /* exclude view sample */
483     }
484     /* map non-view sample->world */
485 greg 2.4 if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
486 greg 2.1 break;
487     /* spawn a specular ray */
488     if (nstarget > 1)
489     bsv.cieY /= (double)nstarget;
490 greg 2.11 cvt_sdcolor(sr.rcoef, &bsv); /* use sample color */
491 greg 2.40 if (xmit) /* apply pattern on transmit */
492 greg 2.1 multcolor(sr.rcoef, ndp->pr->pcol);
493     if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
494 greg 2.11 if (maxdepth > 0)
495 greg 2.1 break;
496 greg 2.11 continue; /* Russian roulette victim */
497 greg 2.1 }
498 greg 2.40 if (xmit && ndp->thick != 0) /* need to offset origin? */
499 greg 2.5 VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
500 greg 2.1 rayvalue(&sr); /* send & evaluate sample */
501     multcolor(sr.rcol, sr.rcoef);
502     addcolor(ndp->pr->rcol, sr.rcol);
503 greg 2.40 ++nsent;
504 greg 2.1 }
505     return(nsent);
506     }
507    
508     /* Sample non-diffuse components of BSDF */
509     static int
510     sample_sdf(BSDFDAT *ndp, int sflags)
511     {
512 greg 2.46 int hasthru = (sflags == SDsampSpT &&
513     bright(ndp->cthru) > FTINY);
514 greg 2.1 int n, ntotal = 0;
515 greg 2.40 double b = 0;
516 greg 2.1 SDSpectralDF *dfp;
517     COLORV *unsc;
518    
519     if (sflags == SDsampSpT) {
520 greg 2.39 unsc = ndp->tunsamp;
521 greg 2.22 if (ndp->pr->rod > 0)
522     dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
523     else
524     dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
525 greg 2.1 } else /* sflags == SDsampSpR */ {
526 greg 2.39 unsc = ndp->runsamp;
527 greg 2.31 if (ndp->pr->rod > 0)
528 greg 2.1 dfp = ndp->sd->rf;
529 greg 2.31 else
530 greg 2.1 dfp = ndp->sd->rb;
531     }
532 greg 2.40 setcolor(unsc, 0, 0, 0);
533 greg 2.1 if (dfp == NULL) /* no specular component? */
534     return(0);
535 greg 2.40
536     if (hasthru) { /* separate view sample? */
537     RAY tr;
538     if (rayorigin(&tr, TRANS, ndp->pr, ndp->cthru) == 0) {
539     VCOPY(tr.rdir, ndp->pr->rdir);
540     rayvalue(&tr);
541     multcolor(tr.rcol, tr.rcoef);
542     addcolor(ndp->pr->rcol, tr.rcol);
543     ++ntotal;
544     b = bright(ndp->cthru);
545     } else
546     hasthru = 0;
547     }
548 greg 2.43 if (dfp->maxHemi - b <= FTINY) { /* have specular to sample? */
549 greg 2.40 b = 0;
550     } else {
551     FVECT vjit;
552     bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
553     b = SDdirectHemi(vjit, sflags, ndp->sd) - b;
554     if (b < 0) b = 0;
555     }
556     if (b <= specthresh+FTINY) { /* below sampling threshold? */
557     if (b > FTINY) { /* XXX no color from BSDF */
558 greg 2.1 if (sflags == SDsampSpT) {
559 greg 2.39 copycolor(unsc, ndp->pr->pcol);
560 greg 2.40 scalecolor(unsc, b);
561 greg 2.1 } else /* no pattern on reflection */
562 greg 2.40 setcolor(unsc, b, b, b);
563 greg 2.1 }
564 greg 2.40 return(ntotal);
565 greg 2.1 }
566 greg 2.41 dimlist[ndims] = (int)(size_t)ndp->mp; /* else sample specular */
567     ndims += 2;
568 greg 2.1 for (n = dfp->ncomp; n--; ) { /* loop over components */
569     dimlist[ndims-1] = n + 9438;
570     ntotal += sample_sdcomp(ndp, &dfp->comp[n], sflags==SDsampSpT);
571     }
572     ndims -= 2;
573     return(ntotal);
574     }
575    
576     /* Color a ray that hit a BSDF material */
577     int
578     m_bsdf(OBJREC *m, RAY *r)
579     {
580 greg 2.6 int hitfront;
581 greg 2.1 COLOR ctmp;
582     SDError ec;
583 greg 2.5 FVECT upvec, vtmp;
584 greg 2.1 MFUNC *mf;
585     BSDFDAT nd;
586     /* check arguments */
587     if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) |
588     (m->oargs.nfargs % 3))
589     objerror(m, USER, "bad # arguments");
590 greg 2.6 /* record surface struck */
591 greg 2.9 hitfront = (r->rod > 0);
592 greg 2.1 /* load cal file */
593     mf = getfunc(m, 5, 0x1d, 1);
594 greg 2.25 setfunc(m, r);
595 greg 2.1 /* get thickness */
596     nd.thick = evalue(mf->ep[0]);
597 greg 2.5 if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
598 greg 2.40 nd.thick = 0;
599 greg 2.26 /* check backface visibility */
600     if (!hitfront & !backvis) {
601     raytrans(r);
602     return(1);
603     }
604 greg 2.5 /* check other rays to pass */
605 greg 2.34 if (nd.thick != 0 && (r->crtype & SHADOW ||
606     !(r->crtype & (SPECULAR|AMBIENT)) ||
607 greg 2.29 (nd.thick > 0) ^ hitfront)) {
608 greg 2.5 raytrans(r); /* hide our proxy */
609 greg 2.1 return(1);
610     }
611 greg 2.31 nd.mp = m;
612     nd.pr = r;
613 greg 2.5 /* get BSDF data */
614     nd.sd = loadBSDF(m->oargs.sarg[1]);
615 greg 2.34 /* early shadow check */
616     if (r->crtype & SHADOW && (nd.sd->tf == NULL) & (nd.sd->tb == NULL))
617     return(1);
618 greg 2.1 /* diffuse reflectance */
619 greg 2.6 if (hitfront) {
620 greg 2.31 cvt_sdcolor(nd.rdiff, &nd.sd->rLambFront);
621     if (m->oargs.nfargs >= 3) {
622     setcolor(ctmp, m->oargs.farg[0],
623 greg 2.1 m->oargs.farg[1],
624     m->oargs.farg[2]);
625 greg 2.31 addcolor(nd.rdiff, ctmp);
626     }
627 greg 2.1 } else {
628 greg 2.31 cvt_sdcolor(nd.rdiff, &nd.sd->rLambBack);
629     if (m->oargs.nfargs >= 6) {
630     setcolor(ctmp, m->oargs.farg[3],
631 greg 2.1 m->oargs.farg[4],
632     m->oargs.farg[5]);
633 greg 2.31 addcolor(nd.rdiff, ctmp);
634     }
635 greg 2.1 }
636     /* diffuse transmittance */
637 greg 2.31 cvt_sdcolor(nd.tdiff, &nd.sd->tLamb);
638     if (m->oargs.nfargs >= 9) {
639     setcolor(ctmp, m->oargs.farg[6],
640 greg 2.1 m->oargs.farg[7],
641     m->oargs.farg[8]);
642 greg 2.31 addcolor(nd.tdiff, ctmp);
643     }
644 greg 2.1 /* get modifiers */
645     raytexture(r, m->omod);
646     /* modify diffuse values */
647     multcolor(nd.rdiff, r->pcol);
648     multcolor(nd.tdiff, r->pcol);
649     /* get up vector */
650     upvec[0] = evalue(mf->ep[1]);
651     upvec[1] = evalue(mf->ep[2]);
652     upvec[2] = evalue(mf->ep[3]);
653     /* return to world coords */
654 greg 2.21 if (mf->fxp != &unitxf) {
655     multv3(upvec, upvec, mf->fxp->xfm);
656     nd.thick *= mf->fxp->sca;
657 greg 2.1 }
658 greg 2.23 if (r->rox != NULL) {
659     multv3(upvec, upvec, r->rox->f.xfm);
660     nd.thick *= r->rox->f.sca;
661     }
662 greg 2.1 raynormal(nd.pnorm, r);
663     /* compute local BSDF xform */
664     ec = SDcompXform(nd.toloc, nd.pnorm, upvec);
665     if (!ec) {
666 greg 2.4 nd.vray[0] = -r->rdir[0];
667     nd.vray[1] = -r->rdir[1];
668     nd.vray[2] = -r->rdir[2];
669     ec = SDmapDir(nd.vray, nd.toloc, nd.vray);
670 greg 2.20 }
671 greg 2.19 if (ec) {
672     objerror(m, WARNING, "Illegal orientation vector");
673     return(1);
674 greg 2.1 }
675 greg 2.34 compute_through(&nd); /* compute through component */
676     if (r->crtype & SHADOW) {
677     RAY tr; /* attempt to pass shadow ray */
678     if (rayorigin(&tr, TRANS, r, nd.cthru) < 0)
679 greg 2.46 return(1); /* no through component */
680 greg 2.34 VCOPY(tr.rdir, r->rdir);
681     rayvalue(&tr); /* transmit with scaling */
682     multcolor(tr.rcol, tr.rcoef);
683     copycolor(r->rcol, tr.rcol);
684     return(1); /* we're done */
685     }
686     ec = SDinvXform(nd.fromloc, nd.toloc);
687     if (!ec) /* determine BSDF resolution */
688     ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL,
689     SDqueryMin+SDqueryMax, nd.sd);
690 greg 2.20 if (ec)
691     objerror(m, USER, transSDError(ec));
692    
693 greg 2.9 nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]);
694     nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]);
695 greg 2.6 if (!hitfront) { /* perturb normal towards hit */
696 greg 2.1 nd.pnorm[0] = -nd.pnorm[0];
697     nd.pnorm[1] = -nd.pnorm[1];
698     nd.pnorm[2] = -nd.pnorm[2];
699     }
700     /* sample reflection */
701     sample_sdf(&nd, SDsampSpR);
702     /* sample transmission */
703     sample_sdf(&nd, SDsampSpT);
704     /* compute indirect diffuse */
705 greg 2.39 copycolor(ctmp, nd.rdiff);
706     addcolor(ctmp, nd.runsamp);
707     if (bright(ctmp) > FTINY) { /* ambient from reflection */
708 greg 2.6 if (!hitfront)
709 greg 2.1 flipsurface(r);
710     multambient(ctmp, r, nd.pnorm);
711     addcolor(r->rcol, ctmp);
712 greg 2.6 if (!hitfront)
713 greg 2.1 flipsurface(r);
714     }
715 greg 2.39 copycolor(ctmp, nd.tdiff);
716     addcolor(ctmp, nd.tunsamp);
717     if (bright(ctmp) > FTINY) { /* ambient from other side */
718 greg 2.1 FVECT bnorm;
719 greg 2.6 if (hitfront)
720 greg 2.1 flipsurface(r);
721     bnorm[0] = -nd.pnorm[0];
722     bnorm[1] = -nd.pnorm[1];
723     bnorm[2] = -nd.pnorm[2];
724 greg 2.9 if (nd.thick != 0) { /* proxy with offset? */
725 greg 2.5 VCOPY(vtmp, r->rop);
726 greg 2.18 VSUM(r->rop, vtmp, r->ron, nd.thick);
727 greg 2.5 multambient(ctmp, r, bnorm);
728     VCOPY(r->rop, vtmp);
729     } else
730     multambient(ctmp, r, bnorm);
731 greg 2.1 addcolor(r->rcol, ctmp);
732 greg 2.6 if (hitfront)
733 greg 2.1 flipsurface(r);
734     }
735     /* add direct component */
736 greg 2.22 if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) &
737     (nd.sd->tb == NULL)) {
738 greg 2.5 direct(r, dir_brdf, &nd); /* reflection only */
739 greg 2.9 } else if (nd.thick == 0) {
740 greg 2.5 direct(r, dir_bsdf, &nd); /* thin surface scattering */
741     } else {
742     direct(r, dir_brdf, &nd); /* reflection first */
743     VCOPY(vtmp, r->rop); /* offset for transmitted */
744     VSUM(r->rop, vtmp, r->ron, -nd.thick);
745 greg 2.6 direct(r, dir_btdf, &nd); /* separate transmission */
746 greg 2.5 VCOPY(r->rop, vtmp);
747     }
748 greg 2.1 /* clean up */
749     SDfreeCache(nd.sd);
750     return(1);
751     }