ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_bsdf.c
Revision: 2.50
Committed: Mon Jun 25 20:49:10 2018 UTC (5 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.49: +45 -30 lines
Log Message:
Added sBSDF material type for explicit control over peak extraction

File Contents

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