ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_bsdf.c
Revision: 2.49
Committed: Sat May 12 02:32:10 2018 UTC (6 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.48: +3 -1 lines
Log Message:
Some paranoia to avoid infinite loops

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.49 static const char RCSid[] = "$Id: m_bsdf.c,v 2.48 2018/05/11 14:26:24 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.34 if (ndp->pr->rod > 0)
120     dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
121     else
122     dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
123    
124     if (dfp == NULL)
125     return; /* no specular transmission */
126     if (bright(ndp->pr->pcol) <= FTINY)
127     return; /* pattern is black, here */
128     srchrad = sqrt(dfp->minProjSA); /* else search for peak */
129 greg 2.40 setcolor(vpeak, 0, 0, 0);
130     setcolor(vsum, 0, 0, 0);
131 greg 2.42 pdir[2] = 0.0;
132 greg 2.34 for (i = 0; i < NDIR2CHECK; i++) {
133     FVECT tdir;
134     SDValue sv;
135     COLOR vcol;
136     tdir[0] = -ndp->vray[0] + dir2check[i][0]*srchrad;
137     tdir[1] = -ndp->vray[1] + dir2check[i][1]*srchrad;
138     tdir[2] = -ndp->vray[2];
139 greg 2.36 normalize(tdir);
140 greg 2.34 ec = SDevalBSDF(&sv, tdir, ndp->vray, ndp->sd);
141     if (ec)
142     goto baderror;
143     cvt_sdcolor(vcol, &sv);
144     addcolor(vsum, vcol);
145 greg 2.45 if (sv.cieY > bright(vpeak)) {
146 greg 2.34 copycolor(vpeak, vcol);
147     VCOPY(pdir, tdir);
148     }
149     }
150 greg 2.42 if (pdir[2] == 0.0)
151     return; /* zero neighborhood */
152 greg 2.34 ec = SDsizeBSDF(&tomega, pdir, ndp->vray, SDqueryMin, ndp->sd);
153     if (ec)
154     goto baderror;
155     if (tomega > 1.5*dfp->minProjSA)
156     return; /* not really a peak? */
157 greg 2.43 tomega /= fabs(pdir[2]); /* remove cosine factor */
158 greg 2.40 if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .001)
159     return; /* < 0.1% transmission */
160 greg 2.34 for (i = 3; i--; ) /* remove peak from average */
161     colval(vsum,i) -= colval(vpeak,i);
162 greg 2.40 if (peak_over*bright(vsum) >= (NDIR2CHECK-1)*bright(vpeak))
163 greg 2.34 return; /* not peaky enough */
164     copycolor(ndp->cthru, vpeak); /* else use it */
165     scalecolor(ndp->cthru, tomega);
166     multcolor(ndp->cthru, ndp->pr->pcol); /* modify by pattern */
167     return;
168     baderror:
169     objerror(ndp->mp, USER, transSDError(ec));
170     #undef NDIR2CHECK
171     }
172    
173 greg 2.4 /* Jitter ray sample according to projected solid angle and specjitter */
174     static void
175 greg 2.15 bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
176 greg 2.4 {
177     VCOPY(vres, ndp->vray);
178     if (specjitter < 1.)
179     sr_psa *= specjitter;
180     if (sr_psa <= FTINY)
181     return;
182     vres[0] += sr_psa*(.5 - frandom());
183     vres[1] += sr_psa*(.5 - frandom());
184     normalize(vres);
185     }
186    
187 greg 2.33 /* Get BSDF specular for direct component, returning true if OK to proceed */
188 greg 2.7 static int
189 greg 2.33 direct_specular_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
190 greg 2.7 {
191 greg 2.43 int nsamp;
192     double wtot = 0;
193 greg 2.13 FVECT vsrc, vsmp, vjit;
194 greg 2.36 double tomega, tomega2;
195 greg 2.15 double sf, tsr, sd[2];
196 greg 2.32 COLOR csmp, cdiff;
197     double diffY;
198 greg 2.7 SDValue sv;
199     SDError ec;
200 greg 2.13 int i;
201 greg 2.37 /* in case we fail */
202 greg 2.40 setcolor(cval, 0, 0, 0);
203 greg 2.7 /* transform source direction */
204     if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
205     return(0);
206 greg 2.32 /* will discount diffuse portion */
207     switch ((vsrc[2] > 0)<<1 | (ndp->vray[2] > 0)) {
208     case 3:
209     if (ndp->sd->rf == NULL)
210     return(0); /* all diffuse */
211     sv = ndp->sd->rLambFront;
212     break;
213     case 0:
214     if (ndp->sd->rb == NULL)
215     return(0); /* all diffuse */
216     sv = ndp->sd->rLambBack;
217     break;
218     default:
219     if ((ndp->sd->tf == NULL) & (ndp->sd->tb == NULL))
220     return(0); /* all diffuse */
221     sv = ndp->sd->tLamb;
222     break;
223     }
224 greg 2.33 if (sv.cieY > FTINY) {
225     diffY = sv.cieY *= 1./PI;
226 greg 2.32 cvt_sdcolor(cdiff, &sv);
227     } else {
228 greg 2.40 diffY = 0;
229     setcolor(cdiff, 0, 0, 0);
230 greg 2.32 }
231 greg 2.37 /* need projected solid angles */
232     omega *= fabs(vsrc[2]);
233 greg 2.16 ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
234     if (ec)
235     goto baderror;
236 greg 2.13 /* check indirect over-counting */
237 greg 2.40 if ((vsrc[2] > 0) ^ (ndp->vray[2] > 0) && bright(ndp->cthru) > FTINY) {
238 greg 2.13 double dx = vsrc[0] + ndp->vray[0];
239     double dy = vsrc[1] + ndp->vray[1];
240 greg 2.47 if (dx*dx + dy*dy <= (1.5*4./PI)*(omega + tomega +
241 greg 2.37 2.*sqrt(omega*tomega)))
242 greg 2.7 return(0);
243     }
244 greg 2.37 /* assign number of samples */
245 greg 2.15 sf = specjitter * ndp->pr->rweight;
246 greg 2.40 if (tomega <= 0)
247 greg 2.24 nsamp = 1;
248     else if (25.*tomega <= omega)
249 greg 2.15 nsamp = 100.*sf + .5;
250     else
251     nsamp = 4.*sf*omega/tomega + .5;
252     nsamp += !nsamp;
253 greg 2.37 sf = sqrt(omega); /* sample our source area */
254 greg 2.15 tsr = sqrt(tomega);
255 greg 2.13 for (i = nsamp; i--; ) {
256     VCOPY(vsmp, vsrc); /* jitter query directions */
257     if (nsamp > 1) {
258     multisamp(sd, 2, (i + frandom())/(double)nsamp);
259     vsmp[0] += (sd[0] - .5)*sf;
260     vsmp[1] += (sd[1] - .5)*sf;
261 greg 2.36 normalize(vsmp);
262 greg 2.13 }
263 greg 2.15 bsdf_jitter(vjit, ndp, tsr);
264 greg 2.37 /* compute BSDF */
265     ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
266     if (ec)
267     goto baderror;
268     if (sv.cieY - diffY <= FTINY)
269     continue; /* no specular part */
270 greg 2.36 /* check for variable resolution */
271     ec = SDsizeBSDF(&tomega2, vjit, vsmp, SDqueryMin, ndp->sd);
272     if (ec)
273     goto baderror;
274     if (tomega2 < .12*tomega)
275     continue; /* not safe to include */
276 greg 2.13 cvt_sdcolor(csmp, &sv);
277 greg 2.44
278     if (sf < 2.5*tsr) { /* weight by Y for small sources */
279     scalecolor(csmp, sv.cieY);
280     wtot += sv.cieY;
281     } else
282     wtot += 1.;
283 greg 2.43 addcolor(cval, csmp);
284 greg 2.13 }
285 greg 2.43 if (wtot <= FTINY) /* no valid specular samples? */
286 greg 2.37 return(0);
287    
288 greg 2.43 sf = 1./wtot; /* weighted average BSDF */
289 greg 2.13 scalecolor(cval, sf);
290 greg 2.32 /* subtract diffuse contribution */
291     for (i = 3*(diffY > FTINY); i--; )
292 greg 2.40 if ((colval(cval,i) -= colval(cdiff,i)) < 0)
293     colval(cval,i) = 0;
294 greg 2.32 return(1);
295 greg 2.13 baderror:
296     objerror(ndp->mp, USER, transSDError(ec));
297 greg 2.17 return(0); /* gratis return */
298 greg 2.7 }
299    
300 greg 2.5 /* Compute source contribution for BSDF (reflected & transmitted) */
301 greg 2.1 static void
302 greg 2.5 dir_bsdf(
303 greg 2.1 COLOR cval, /* returned coefficient */
304     void *nnp, /* material data */
305     FVECT ldir, /* light source direction */
306     double omega /* light source size */
307     )
308     {
309 greg 2.3 BSDFDAT *np = (BSDFDAT *)nnp;
310 greg 2.1 double ldot;
311     double dtmp;
312     COLOR ctmp;
313    
314 greg 2.40 setcolor(cval, 0, 0, 0);
315 greg 2.1
316     ldot = DOT(np->pnorm, ldir);
317     if ((-FTINY <= ldot) & (ldot <= FTINY))
318     return;
319    
320 greg 2.9 if (ldot > 0 && bright(np->rdiff) > FTINY) {
321 greg 2.1 /*
322 greg 2.39 * Compute diffuse reflected component
323 greg 2.1 */
324     copycolor(ctmp, np->rdiff);
325     dtmp = ldot * omega * (1./PI);
326     scalecolor(ctmp, dtmp);
327     addcolor(cval, ctmp);
328     }
329 greg 2.9 if (ldot < 0 && bright(np->tdiff) > FTINY) {
330 greg 2.1 /*
331 greg 2.39 * Compute diffuse transmission
332 greg 2.1 */
333     copycolor(ctmp, np->tdiff);
334     dtmp = -ldot * omega * (1.0/PI);
335     scalecolor(ctmp, dtmp);
336     addcolor(cval, ctmp);
337     }
338 greg 2.30 if (ambRayInPmap(np->pr))
339     return; /* specular already in photon map */
340 greg 2.1 /*
341 greg 2.39 * Compute specular scattering coefficient using BSDF
342 greg 2.1 */
343 greg 2.33 if (!direct_specular_OK(ctmp, ldir, omega, np))
344 greg 2.1 return;
345 greg 2.31 if (ldot < 0) { /* pattern for specular transmission */
346 greg 2.1 multcolor(ctmp, np->pr->pcol);
347     dtmp = -ldot * omega;
348 greg 2.31 } else
349     dtmp = ldot * omega;
350 greg 2.1 scalecolor(ctmp, dtmp);
351     addcolor(cval, ctmp);
352     }
353    
354 greg 2.5 /* Compute source contribution for BSDF (reflected only) */
355     static void
356     dir_brdf(
357     COLOR cval, /* returned coefficient */
358     void *nnp, /* material data */
359     FVECT ldir, /* light source direction */
360     double omega /* light source size */
361     )
362     {
363     BSDFDAT *np = (BSDFDAT *)nnp;
364     double ldot;
365     double dtmp;
366     COLOR ctmp, ctmp1, ctmp2;
367    
368 greg 2.40 setcolor(cval, 0, 0, 0);
369 greg 2.5
370     ldot = DOT(np->pnorm, ldir);
371    
372     if (ldot <= FTINY)
373     return;
374    
375     if (bright(np->rdiff) > FTINY) {
376     /*
377 greg 2.39 * Compute diffuse reflected component
378 greg 2.5 */
379     copycolor(ctmp, np->rdiff);
380     dtmp = ldot * omega * (1./PI);
381     scalecolor(ctmp, dtmp);
382     addcolor(cval, ctmp);
383     }
384 greg 2.30 if (ambRayInPmap(np->pr))
385     return; /* specular already in photon map */
386 greg 2.5 /*
387 greg 2.39 * Compute specular reflection coefficient using BSDF
388 greg 2.5 */
389 greg 2.33 if (!direct_specular_OK(ctmp, ldir, omega, np))
390 greg 2.5 return;
391     dtmp = ldot * omega;
392     scalecolor(ctmp, dtmp);
393     addcolor(cval, ctmp);
394     }
395    
396     /* Compute source contribution for BSDF (transmitted only) */
397     static void
398     dir_btdf(
399     COLOR cval, /* returned coefficient */
400     void *nnp, /* material data */
401     FVECT ldir, /* light source direction */
402     double omega /* light source size */
403     )
404     {
405     BSDFDAT *np = (BSDFDAT *)nnp;
406     double ldot;
407     double dtmp;
408     COLOR ctmp;
409    
410 greg 2.40 setcolor(cval, 0, 0, 0);
411 greg 2.5
412     ldot = DOT(np->pnorm, ldir);
413    
414     if (ldot >= -FTINY)
415     return;
416    
417     if (bright(np->tdiff) > FTINY) {
418     /*
419 greg 2.39 * Compute diffuse transmission
420 greg 2.5 */
421     copycolor(ctmp, np->tdiff);
422     dtmp = -ldot * omega * (1.0/PI);
423     scalecolor(ctmp, dtmp);
424     addcolor(cval, ctmp);
425     }
426 greg 2.30 if (ambRayInPmap(np->pr))
427     return; /* specular already in photon map */
428 greg 2.5 /*
429 greg 2.39 * Compute specular scattering coefficient using BSDF
430 greg 2.5 */
431 greg 2.33 if (!direct_specular_OK(ctmp, ldir, omega, np))
432 greg 2.5 return;
433     /* full pattern on transmission */
434     multcolor(ctmp, np->pr->pcol);
435     dtmp = -ldot * omega;
436     scalecolor(ctmp, dtmp);
437     addcolor(cval, ctmp);
438     }
439    
440 greg 2.1 /* Sample separate BSDF component */
441     static int
442 greg 2.40 sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int xmit)
443 greg 2.1 {
444 greg 2.47 const int hasthru = (xmit &&
445     !(ndp->pr->crtype & (SPECULAR|AMBIENT))
446     && bright(ndp->cthru) > FTINY);
447 greg 2.41 int nstarget = 1;
448     int nsent = 0;
449     int n;
450     SDError ec;
451     SDValue bsv;
452     double xrand;
453     FVECT vsmp, vinc;
454     RAY sr;
455 greg 2.1 /* multiple samples? */
456     if (specjitter > 1.5) {
457     nstarget = specjitter*ndp->pr->rweight + .5;
458 greg 2.14 nstarget += !nstarget;
459 greg 2.1 }
460 greg 2.11 /* run through our samples */
461 greg 2.40 for (n = 0; n < nstarget; n++) {
462 greg 2.15 if (nstarget == 1) { /* stratify random variable */
463 greg 2.11 xrand = urand(ilhash(dimlist,ndims)+samplendx);
464 greg 2.15 if (specjitter < 1.)
465     xrand = .5 + specjitter*(xrand-.5);
466     } else {
467 greg 2.40 xrand = (n + frandom())/(double)nstarget;
468 greg 2.15 }
469 greg 2.11 SDerrorDetail[0] = '\0'; /* sample direction & coef. */
470 greg 2.15 bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]);
471 greg 2.40 VCOPY(vinc, vsmp); /* to compare after */
472 greg 2.11 ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
473 greg 2.1 if (ec)
474 greg 2.2 objerror(ndp->mp, USER, transSDError(ec));
475 greg 2.11 if (bsv.cieY <= FTINY) /* zero component? */
476 greg 2.1 break;
477 greg 2.40 if (hasthru) { /* check for view ray */
478     double dx = vinc[0] + vsmp[0];
479     double dy = vinc[1] + vsmp[1];
480     if (dx*dx + dy*dy <= ndp->sr_vpsa[0]*ndp->sr_vpsa[0])
481     continue; /* exclude view sample */
482     }
483     /* map non-view sample->world */
484 greg 2.4 if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
485 greg 2.1 break;
486     /* spawn a specular ray */
487     if (nstarget > 1)
488     bsv.cieY /= (double)nstarget;
489 greg 2.11 cvt_sdcolor(sr.rcoef, &bsv); /* use sample color */
490 greg 2.40 if (xmit) /* apply pattern on transmit */
491 greg 2.1 multcolor(sr.rcoef, ndp->pr->pcol);
492     if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
493 greg 2.48 if (!n & (nstarget > 1)) {
494 greg 2.49 n = nstarget; /* avoid infinitue loop */
495 greg 2.48 nstarget = nstarget*sr.rweight/minweight;
496 greg 2.49 if (n == nstarget) break;
497 greg 2.48 n = -1; /* moved target */
498     }
499     continue; /* try again */
500 greg 2.1 }
501 greg 2.40 if (xmit && ndp->thick != 0) /* need to offset origin? */
502 greg 2.5 VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
503 greg 2.1 rayvalue(&sr); /* send & evaluate sample */
504     multcolor(sr.rcol, sr.rcoef);
505     addcolor(ndp->pr->rcol, sr.rcol);
506 greg 2.40 ++nsent;
507 greg 2.1 }
508     return(nsent);
509     }
510    
511     /* Sample non-diffuse components of BSDF */
512     static int
513     sample_sdf(BSDFDAT *ndp, int sflags)
514     {
515 greg 2.46 int hasthru = (sflags == SDsampSpT &&
516 greg 2.47 !(ndp->pr->crtype & (SPECULAR|AMBIENT))
517     && bright(ndp->cthru) > FTINY);
518 greg 2.1 int n, ntotal = 0;
519 greg 2.40 double b = 0;
520 greg 2.1 SDSpectralDF *dfp;
521     COLORV *unsc;
522    
523     if (sflags == SDsampSpT) {
524 greg 2.39 unsc = ndp->tunsamp;
525 greg 2.22 if (ndp->pr->rod > 0)
526     dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
527     else
528     dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
529 greg 2.1 } else /* sflags == SDsampSpR */ {
530 greg 2.39 unsc = ndp->runsamp;
531 greg 2.31 if (ndp->pr->rod > 0)
532 greg 2.1 dfp = ndp->sd->rf;
533 greg 2.31 else
534 greg 2.1 dfp = ndp->sd->rb;
535     }
536 greg 2.40 setcolor(unsc, 0, 0, 0);
537 greg 2.1 if (dfp == NULL) /* no specular component? */
538     return(0);
539 greg 2.40
540     if (hasthru) { /* separate view sample? */
541     RAY tr;
542     if (rayorigin(&tr, TRANS, ndp->pr, ndp->cthru) == 0) {
543     VCOPY(tr.rdir, ndp->pr->rdir);
544     rayvalue(&tr);
545     multcolor(tr.rcol, tr.rcoef);
546     addcolor(ndp->pr->rcol, tr.rcol);
547     ++ntotal;
548     b = bright(ndp->cthru);
549     } else
550     hasthru = 0;
551     }
552 greg 2.43 if (dfp->maxHemi - b <= FTINY) { /* have specular to sample? */
553 greg 2.40 b = 0;
554     } else {
555     FVECT vjit;
556     bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
557     b = SDdirectHemi(vjit, sflags, ndp->sd) - b;
558     if (b < 0) b = 0;
559     }
560     if (b <= specthresh+FTINY) { /* below sampling threshold? */
561     if (b > FTINY) { /* XXX no color from BSDF */
562 greg 2.1 if (sflags == SDsampSpT) {
563 greg 2.39 copycolor(unsc, ndp->pr->pcol);
564 greg 2.40 scalecolor(unsc, b);
565 greg 2.1 } else /* no pattern on reflection */
566 greg 2.40 setcolor(unsc, b, b, b);
567 greg 2.1 }
568 greg 2.40 return(ntotal);
569 greg 2.1 }
570 greg 2.41 dimlist[ndims] = (int)(size_t)ndp->mp; /* else sample specular */
571     ndims += 2;
572 greg 2.1 for (n = dfp->ncomp; n--; ) { /* loop over components */
573     dimlist[ndims-1] = n + 9438;
574     ntotal += sample_sdcomp(ndp, &dfp->comp[n], sflags==SDsampSpT);
575     }
576     ndims -= 2;
577     return(ntotal);
578     }
579    
580     /* Color a ray that hit a BSDF material */
581     int
582     m_bsdf(OBJREC *m, RAY *r)
583     {
584 greg 2.6 int hitfront;
585 greg 2.1 COLOR ctmp;
586     SDError ec;
587 greg 2.5 FVECT upvec, vtmp;
588 greg 2.1 MFUNC *mf;
589     BSDFDAT nd;
590     /* check arguments */
591     if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) |
592     (m->oargs.nfargs % 3))
593     objerror(m, USER, "bad # arguments");
594 greg 2.6 /* record surface struck */
595 greg 2.9 hitfront = (r->rod > 0);
596 greg 2.1 /* load cal file */
597     mf = getfunc(m, 5, 0x1d, 1);
598 greg 2.25 setfunc(m, r);
599 greg 2.1 /* get thickness */
600     nd.thick = evalue(mf->ep[0]);
601 greg 2.5 if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
602 greg 2.40 nd.thick = 0;
603 greg 2.26 /* check backface visibility */
604     if (!hitfront & !backvis) {
605     raytrans(r);
606     return(1);
607     }
608 greg 2.5 /* check other rays to pass */
609 greg 2.34 if (nd.thick != 0 && (r->crtype & SHADOW ||
610     !(r->crtype & (SPECULAR|AMBIENT)) ||
611 greg 2.29 (nd.thick > 0) ^ hitfront)) {
612 greg 2.5 raytrans(r); /* hide our proxy */
613 greg 2.1 return(1);
614     }
615 greg 2.31 nd.mp = m;
616     nd.pr = r;
617 greg 2.5 /* get BSDF data */
618     nd.sd = loadBSDF(m->oargs.sarg[1]);
619 greg 2.34 /* early shadow check */
620     if (r->crtype & SHADOW && (nd.sd->tf == NULL) & (nd.sd->tb == NULL))
621     return(1);
622 greg 2.1 /* diffuse reflectance */
623 greg 2.6 if (hitfront) {
624 greg 2.31 cvt_sdcolor(nd.rdiff, &nd.sd->rLambFront);
625     if (m->oargs.nfargs >= 3) {
626     setcolor(ctmp, m->oargs.farg[0],
627 greg 2.1 m->oargs.farg[1],
628     m->oargs.farg[2]);
629 greg 2.31 addcolor(nd.rdiff, ctmp);
630     }
631 greg 2.1 } else {
632 greg 2.31 cvt_sdcolor(nd.rdiff, &nd.sd->rLambBack);
633     if (m->oargs.nfargs >= 6) {
634     setcolor(ctmp, m->oargs.farg[3],
635 greg 2.1 m->oargs.farg[4],
636     m->oargs.farg[5]);
637 greg 2.31 addcolor(nd.rdiff, ctmp);
638     }
639 greg 2.1 }
640     /* diffuse transmittance */
641 greg 2.31 cvt_sdcolor(nd.tdiff, &nd.sd->tLamb);
642     if (m->oargs.nfargs >= 9) {
643     setcolor(ctmp, m->oargs.farg[6],
644 greg 2.1 m->oargs.farg[7],
645     m->oargs.farg[8]);
646 greg 2.31 addcolor(nd.tdiff, ctmp);
647     }
648 greg 2.1 /* get modifiers */
649     raytexture(r, m->omod);
650     /* modify diffuse values */
651     multcolor(nd.rdiff, r->pcol);
652     multcolor(nd.tdiff, r->pcol);
653     /* get up vector */
654     upvec[0] = evalue(mf->ep[1]);
655     upvec[1] = evalue(mf->ep[2]);
656     upvec[2] = evalue(mf->ep[3]);
657     /* return to world coords */
658 greg 2.21 if (mf->fxp != &unitxf) {
659     multv3(upvec, upvec, mf->fxp->xfm);
660     nd.thick *= mf->fxp->sca;
661 greg 2.1 }
662 greg 2.23 if (r->rox != NULL) {
663     multv3(upvec, upvec, r->rox->f.xfm);
664     nd.thick *= r->rox->f.sca;
665     }
666 greg 2.1 raynormal(nd.pnorm, r);
667     /* compute local BSDF xform */
668     ec = SDcompXform(nd.toloc, nd.pnorm, upvec);
669     if (!ec) {
670 greg 2.4 nd.vray[0] = -r->rdir[0];
671     nd.vray[1] = -r->rdir[1];
672     nd.vray[2] = -r->rdir[2];
673     ec = SDmapDir(nd.vray, nd.toloc, nd.vray);
674 greg 2.20 }
675 greg 2.19 if (ec) {
676     objerror(m, WARNING, "Illegal orientation vector");
677     return(1);
678 greg 2.1 }
679 greg 2.34 compute_through(&nd); /* compute through component */
680     if (r->crtype & SHADOW) {
681     RAY tr; /* attempt to pass shadow ray */
682     if (rayorigin(&tr, TRANS, r, nd.cthru) < 0)
683 greg 2.46 return(1); /* no through component */
684 greg 2.34 VCOPY(tr.rdir, r->rdir);
685     rayvalue(&tr); /* transmit with scaling */
686     multcolor(tr.rcol, tr.rcoef);
687     copycolor(r->rcol, tr.rcol);
688     return(1); /* we're done */
689     }
690     ec = SDinvXform(nd.fromloc, nd.toloc);
691     if (!ec) /* determine BSDF resolution */
692     ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL,
693     SDqueryMin+SDqueryMax, nd.sd);
694 greg 2.20 if (ec)
695     objerror(m, USER, transSDError(ec));
696    
697 greg 2.9 nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]);
698     nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]);
699 greg 2.6 if (!hitfront) { /* perturb normal towards hit */
700 greg 2.1 nd.pnorm[0] = -nd.pnorm[0];
701     nd.pnorm[1] = -nd.pnorm[1];
702     nd.pnorm[2] = -nd.pnorm[2];
703     }
704     /* sample reflection */
705     sample_sdf(&nd, SDsampSpR);
706     /* sample transmission */
707     sample_sdf(&nd, SDsampSpT);
708     /* compute indirect diffuse */
709 greg 2.39 copycolor(ctmp, nd.rdiff);
710     addcolor(ctmp, nd.runsamp);
711     if (bright(ctmp) > FTINY) { /* ambient from reflection */
712 greg 2.6 if (!hitfront)
713 greg 2.1 flipsurface(r);
714     multambient(ctmp, r, nd.pnorm);
715     addcolor(r->rcol, ctmp);
716 greg 2.6 if (!hitfront)
717 greg 2.1 flipsurface(r);
718     }
719 greg 2.39 copycolor(ctmp, nd.tdiff);
720     addcolor(ctmp, nd.tunsamp);
721     if (bright(ctmp) > FTINY) { /* ambient from other side */
722 greg 2.1 FVECT bnorm;
723 greg 2.6 if (hitfront)
724 greg 2.1 flipsurface(r);
725     bnorm[0] = -nd.pnorm[0];
726     bnorm[1] = -nd.pnorm[1];
727     bnorm[2] = -nd.pnorm[2];
728 greg 2.9 if (nd.thick != 0) { /* proxy with offset? */
729 greg 2.5 VCOPY(vtmp, r->rop);
730 greg 2.18 VSUM(r->rop, vtmp, r->ron, nd.thick);
731 greg 2.5 multambient(ctmp, r, bnorm);
732     VCOPY(r->rop, vtmp);
733     } else
734     multambient(ctmp, r, bnorm);
735 greg 2.1 addcolor(r->rcol, ctmp);
736 greg 2.6 if (hitfront)
737 greg 2.1 flipsurface(r);
738     }
739     /* add direct component */
740 greg 2.22 if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) &
741     (nd.sd->tb == NULL)) {
742 greg 2.5 direct(r, dir_brdf, &nd); /* reflection only */
743 greg 2.9 } else if (nd.thick == 0) {
744 greg 2.5 direct(r, dir_bsdf, &nd); /* thin surface scattering */
745     } else {
746     direct(r, dir_brdf, &nd); /* reflection first */
747     VCOPY(vtmp, r->rop); /* offset for transmitted */
748     VSUM(r->rop, vtmp, r->ron, -nd.thick);
749 greg 2.6 direct(r, dir_btdf, &nd); /* separate transmission */
750 greg 2.5 VCOPY(r->rop, vtmp);
751     }
752 greg 2.1 /* clean up */
753     SDfreeCache(nd.sd);
754     return(1);
755     }