ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_bsdf.c
Revision: 2.30
Committed: Wed Sep 2 18:59:01 2015 UTC (9 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R0
Changes since 2.29: +8 -1 lines
Log Message:
Had to reinstate ambRayInPmap() macro to avoid over-counting bug

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.30 static const char RCSid[] = "$Id: m_bsdf.c,v 2.29 2015/08/01 23:27:04 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.1 * (view) ray will pass right through our material if it has any
27 greg 2.5 * non-diffuse transmission, making the BSDF surface invisible. This
28     * shows the proxied geometry instead. Thickness has the further
29     * effect of turning off reflection on the hidden side so that rays
30     * heading in the opposite direction pass unimpeded through the BSDF
31     * 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.1 * The "up" vector for the BSDF is given by three variables, defined
40     * (along with the thickness) by the named function file, or '.' if none.
41     * Together with the surface normal, this defines the local coordinate
42     * system for the BSDF.
43     * We do not reorient the surface, so if the BSDF has no back-side
44 greg 2.5 * reflectance and none is given in the real arguments, a BSDF surface
45     * with zero thickness will appear black when viewed from behind
46     * unless backface visibility is off.
47     * The diffuse arguments are added to components in the BSDF file,
48 greg 2.1 * not multiplied. However, patterns affect this material as a multiplier
49     * on everything except non-diffuse reflection.
50     *
51     * Arguments for MAT_BSDF are:
52     * 6+ thick BSDFfile ux uy uz funcfile transform
53     * 0
54 greg 2.8 * 0|3|6|9 rdf gdf bdf
55 greg 2.1 * rdb gdb bdb
56     * rdt gdt bdt
57     */
58    
59 greg 2.4 /*
60     * Note that our reverse ray-tracing process means that the positions
61     * of incoming and outgoing vectors may be reversed in our calls
62     * to the BSDF library. This is fine, since the bidirectional nature
63     * of the BSDF (that's what the 'B' stands for) means it all works out.
64     */
65    
66 greg 2.1 typedef struct {
67     OBJREC *mp; /* material pointer */
68     RAY *pr; /* intersected ray */
69     FVECT pnorm; /* perturbed surface normal */
70 greg 2.4 FVECT vray; /* local outgoing (return) vector */
71 greg 2.9 double sr_vpsa[2]; /* sqrt of BSDF projected solid angle extrema */
72 greg 2.1 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 */
78     COLOR tunsamp; /* BSDF hemispherical transmission */
79     COLOR tdiff; /* added diffuse transmission */
80     } BSDFDAT; /* BSDF material data */
81    
82     #define cvt_sdcolor(cv, svp) ccy2rgb(&(svp)->spec, (svp)->cieY, cv)
83    
84 greg 2.4 /* Jitter ray sample according to projected solid angle and specjitter */
85     static void
86 greg 2.15 bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa)
87 greg 2.4 {
88     VCOPY(vres, ndp->vray);
89     if (specjitter < 1.)
90     sr_psa *= specjitter;
91     if (sr_psa <= FTINY)
92     return;
93     vres[0] += sr_psa*(.5 - frandom());
94     vres[1] += sr_psa*(.5 - frandom());
95     normalize(vres);
96     }
97    
98 greg 2.7 /* Evaluate BSDF for direct component, returning true if OK to proceed */
99     static int
100 greg 2.13 direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp)
101 greg 2.7 {
102 greg 2.15 int nsamp, ok = 0;
103 greg 2.13 FVECT vsrc, vsmp, vjit;
104     double tomega;
105 greg 2.15 double sf, tsr, sd[2];
106 greg 2.13 COLOR csmp;
107 greg 2.7 SDValue sv;
108     SDError ec;
109 greg 2.13 int i;
110 greg 2.7 /* transform source direction */
111     if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
112     return(0);
113 greg 2.16 /* assign number of samples */
114     ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd);
115     if (ec)
116     goto baderror;
117 greg 2.13 /* check indirect over-counting */
118     if (ndp->thick != 0 && ndp->pr->crtype & (SPECULAR|AMBIENT)
119     && vsrc[2] > 0 ^ ndp->vray[2] > 0) {
120     double dx = vsrc[0] + ndp->vray[0];
121     double dy = vsrc[1] + ndp->vray[1];
122 greg 2.16 if (dx*dx + dy*dy <= omega+tomega)
123 greg 2.7 return(0);
124     }
125 greg 2.15 sf = specjitter * ndp->pr->rweight;
126 greg 2.24 if (tomega <= .0)
127     nsamp = 1;
128     else if (25.*tomega <= omega)
129 greg 2.15 nsamp = 100.*sf + .5;
130     else
131     nsamp = 4.*sf*omega/tomega + .5;
132     nsamp += !nsamp;
133     setcolor(cval, .0, .0, .0); /* sample our source area */
134 greg 2.13 sf = sqrt(omega);
135 greg 2.15 tsr = sqrt(tomega);
136 greg 2.13 for (i = nsamp; i--; ) {
137     VCOPY(vsmp, vsrc); /* jitter query directions */
138     if (nsamp > 1) {
139     multisamp(sd, 2, (i + frandom())/(double)nsamp);
140     vsmp[0] += (sd[0] - .5)*sf;
141     vsmp[1] += (sd[1] - .5)*sf;
142     if (normalize(vsmp) == 0) {
143     --nsamp;
144     continue;
145     }
146     }
147 greg 2.15 bsdf_jitter(vjit, ndp, tsr);
148 greg 2.13 /* compute BSDF */
149     ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd);
150     if (ec)
151     goto baderror;
152     if (sv.cieY <= FTINY) /* worth using? */
153     continue;
154     cvt_sdcolor(csmp, &sv);
155     addcolor(cval, csmp); /* average it in */
156     ++ok;
157     }
158     sf = 1./(double)nsamp;
159     scalecolor(cval, sf);
160     return(ok);
161     baderror:
162     objerror(ndp->mp, USER, transSDError(ec));
163 greg 2.17 return(0); /* gratis return */
164 greg 2.7 }
165    
166 greg 2.5 /* Compute source contribution for BSDF (reflected & transmitted) */
167 greg 2.1 static void
168 greg 2.5 dir_bsdf(
169 greg 2.1 COLOR cval, /* returned coefficient */
170     void *nnp, /* material data */
171     FVECT ldir, /* light source direction */
172     double omega /* light source size */
173     )
174     {
175 greg 2.3 BSDFDAT *np = (BSDFDAT *)nnp;
176 greg 2.1 double ldot;
177     double dtmp;
178     COLOR ctmp;
179    
180     setcolor(cval, .0, .0, .0);
181    
182     ldot = DOT(np->pnorm, ldir);
183     if ((-FTINY <= ldot) & (ldot <= FTINY))
184     return;
185    
186 greg 2.9 if (ldot > 0 && bright(np->rdiff) > FTINY) {
187 greg 2.1 /*
188     * Compute added diffuse reflected component.
189     */
190     copycolor(ctmp, np->rdiff);
191     dtmp = ldot * omega * (1./PI);
192     scalecolor(ctmp, dtmp);
193     addcolor(cval, ctmp);
194     }
195 greg 2.9 if (ldot < 0 && bright(np->tdiff) > FTINY) {
196 greg 2.1 /*
197     * Compute added diffuse transmission.
198     */
199     copycolor(ctmp, np->tdiff);
200     dtmp = -ldot * omega * (1.0/PI);
201     scalecolor(ctmp, dtmp);
202     addcolor(cval, ctmp);
203     }
204 greg 2.30 if (ambRayInPmap(np->pr))
205     return; /* specular already in photon map */
206 greg 2.1 /*
207     * Compute scattering coefficient using BSDF.
208     */
209 greg 2.13 if (!direct_bsdf_OK(ctmp, ldir, omega, np))
210 greg 2.1 return;
211 greg 2.9 if (ldot > 0) { /* pattern only diffuse reflection */
212 greg 2.1 COLOR ctmp1, ctmp2;
213 greg 2.9 dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
214 greg 2.1 : np->sd->rLambBack.cieY;
215 greg 2.7 /* diffuse fraction */
216     dtmp /= PI * bright(ctmp);
217 greg 2.1 copycolor(ctmp2, np->pr->pcol);
218     scalecolor(ctmp2, dtmp);
219     setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
220     addcolor(ctmp1, ctmp2);
221 greg 2.3 multcolor(ctmp, ctmp1); /* apply derated pattern */
222 greg 2.1 dtmp = ldot * omega;
223     } else { /* full pattern on transmission */
224     multcolor(ctmp, np->pr->pcol);
225     dtmp = -ldot * omega;
226     }
227     scalecolor(ctmp, dtmp);
228     addcolor(cval, ctmp);
229     }
230    
231 greg 2.5 /* Compute source contribution for BSDF (reflected only) */
232     static void
233     dir_brdf(
234     COLOR cval, /* returned coefficient */
235     void *nnp, /* material data */
236     FVECT ldir, /* light source direction */
237     double omega /* light source size */
238     )
239     {
240     BSDFDAT *np = (BSDFDAT *)nnp;
241     double ldot;
242     double dtmp;
243     COLOR ctmp, ctmp1, ctmp2;
244    
245     setcolor(cval, .0, .0, .0);
246    
247     ldot = DOT(np->pnorm, ldir);
248    
249     if (ldot <= FTINY)
250     return;
251    
252     if (bright(np->rdiff) > FTINY) {
253     /*
254     * Compute added diffuse reflected component.
255     */
256     copycolor(ctmp, np->rdiff);
257     dtmp = ldot * omega * (1./PI);
258     scalecolor(ctmp, dtmp);
259     addcolor(cval, ctmp);
260     }
261 greg 2.30 if (ambRayInPmap(np->pr))
262     return; /* specular already in photon map */
263 greg 2.5 /*
264     * Compute reflection coefficient using BSDF.
265     */
266 greg 2.13 if (!direct_bsdf_OK(ctmp, ldir, omega, np))
267 greg 2.5 return;
268     /* pattern only diffuse reflection */
269 greg 2.9 dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
270 greg 2.5 : np->sd->rLambBack.cieY;
271 greg 2.7 dtmp /= PI * bright(ctmp); /* diffuse fraction */
272 greg 2.5 copycolor(ctmp2, np->pr->pcol);
273     scalecolor(ctmp2, dtmp);
274     setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
275     addcolor(ctmp1, ctmp2);
276     multcolor(ctmp, ctmp1); /* apply derated pattern */
277     dtmp = ldot * omega;
278     scalecolor(ctmp, dtmp);
279     addcolor(cval, ctmp);
280     }
281    
282     /* Compute source contribution for BSDF (transmitted only) */
283     static void
284     dir_btdf(
285     COLOR cval, /* returned coefficient */
286     void *nnp, /* material data */
287     FVECT ldir, /* light source direction */
288     double omega /* light source size */
289     )
290     {
291     BSDFDAT *np = (BSDFDAT *)nnp;
292     double ldot;
293     double dtmp;
294     COLOR ctmp;
295    
296     setcolor(cval, .0, .0, .0);
297    
298     ldot = DOT(np->pnorm, ldir);
299    
300     if (ldot >= -FTINY)
301     return;
302    
303     if (bright(np->tdiff) > FTINY) {
304     /*
305     * Compute added diffuse transmission.
306     */
307     copycolor(ctmp, np->tdiff);
308     dtmp = -ldot * omega * (1.0/PI);
309     scalecolor(ctmp, dtmp);
310     addcolor(cval, ctmp);
311     }
312 greg 2.30 if (ambRayInPmap(np->pr))
313     return; /* specular already in photon map */
314 greg 2.5 /*
315     * Compute scattering coefficient using BSDF.
316     */
317 greg 2.13 if (!direct_bsdf_OK(ctmp, ldir, omega, np))
318 greg 2.5 return;
319     /* full pattern on transmission */
320     multcolor(ctmp, np->pr->pcol);
321     dtmp = -ldot * omega;
322     scalecolor(ctmp, dtmp);
323     addcolor(cval, ctmp);
324     }
325    
326 greg 2.1 /* Sample separate BSDF component */
327     static int
328     sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
329     {
330     int nstarget = 1;
331 greg 2.11 int nsent;
332 greg 2.1 SDError ec;
333     SDValue bsv;
334 greg 2.11 double xrand;
335 greg 2.10 FVECT vsmp;
336 greg 2.1 RAY sr;
337     /* multiple samples? */
338     if (specjitter > 1.5) {
339     nstarget = specjitter*ndp->pr->rweight + .5;
340 greg 2.14 nstarget += !nstarget;
341 greg 2.1 }
342 greg 2.11 /* run through our samples */
343     for (nsent = 0; nsent < nstarget; nsent++) {
344 greg 2.15 if (nstarget == 1) { /* stratify random variable */
345 greg 2.11 xrand = urand(ilhash(dimlist,ndims)+samplendx);
346 greg 2.15 if (specjitter < 1.)
347     xrand = .5 + specjitter*(xrand-.5);
348     } else {
349 greg 2.11 xrand = (nsent + frandom())/(double)nstarget;
350 greg 2.15 }
351 greg 2.11 SDerrorDetail[0] = '\0'; /* sample direction & coef. */
352 greg 2.15 bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]);
353 greg 2.11 ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
354 greg 2.1 if (ec)
355 greg 2.2 objerror(ndp->mp, USER, transSDError(ec));
356 greg 2.11 if (bsv.cieY <= FTINY) /* zero component? */
357 greg 2.1 break;
358     /* map vector to world */
359 greg 2.4 if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
360 greg 2.1 break;
361     /* spawn a specular ray */
362     if (nstarget > 1)
363     bsv.cieY /= (double)nstarget;
364 greg 2.11 cvt_sdcolor(sr.rcoef, &bsv); /* use sample color */
365     if (usepat) /* apply pattern? */
366 greg 2.1 multcolor(sr.rcoef, ndp->pr->pcol);
367     if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
368 greg 2.11 if (maxdepth > 0)
369 greg 2.1 break;
370 greg 2.11 continue; /* Russian roulette victim */
371 greg 2.1 }
372 greg 2.5 /* need to offset origin? */
373 greg 2.9 if (ndp->thick != 0 && ndp->pr->rod > 0 ^ vsmp[2] > 0)
374 greg 2.5 VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
375 greg 2.1 rayvalue(&sr); /* send & evaluate sample */
376     multcolor(sr.rcol, sr.rcoef);
377     addcolor(ndp->pr->rcol, sr.rcol);
378     }
379     return(nsent);
380     }
381    
382     /* Sample non-diffuse components of BSDF */
383     static int
384     sample_sdf(BSDFDAT *ndp, int sflags)
385     {
386     int n, ntotal = 0;
387     SDSpectralDF *dfp;
388     COLORV *unsc;
389    
390     if (sflags == SDsampSpT) {
391     unsc = ndp->tunsamp;
392 greg 2.22 if (ndp->pr->rod > 0)
393     dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
394     else
395     dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
396 greg 2.1 cvt_sdcolor(unsc, &ndp->sd->tLamb);
397     } else /* sflags == SDsampSpR */ {
398     unsc = ndp->runsamp;
399 greg 2.9 if (ndp->pr->rod > 0) {
400 greg 2.1 dfp = ndp->sd->rf;
401     cvt_sdcolor(unsc, &ndp->sd->rLambFront);
402     } else {
403     dfp = ndp->sd->rb;
404     cvt_sdcolor(unsc, &ndp->sd->rLambBack);
405     }
406     }
407     multcolor(unsc, ndp->pr->pcol);
408     if (dfp == NULL) /* no specular component? */
409     return(0);
410     /* below sampling threshold? */
411     if (dfp->maxHemi <= specthresh+FTINY) {
412 greg 2.3 if (dfp->maxHemi > FTINY) { /* XXX no color from BSDF */
413 greg 2.4 FVECT vjit;
414     double d;
415 greg 2.1 COLOR ctmp;
416 greg 2.15 bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
417 greg 2.4 d = SDdirectHemi(vjit, sflags, ndp->sd);
418 greg 2.1 if (sflags == SDsampSpT) {
419     copycolor(ctmp, ndp->pr->pcol);
420     scalecolor(ctmp, d);
421     } else /* no pattern on reflection */
422     setcolor(ctmp, d, d, d);
423     addcolor(unsc, ctmp);
424     }
425     return(0);
426     }
427     /* else need to sample */
428     dimlist[ndims++] = (int)(size_t)ndp->mp;
429     ndims++;
430     for (n = dfp->ncomp; n--; ) { /* loop over components */
431     dimlist[ndims-1] = n + 9438;
432     ntotal += sample_sdcomp(ndp, &dfp->comp[n], sflags==SDsampSpT);
433     }
434     ndims -= 2;
435     return(ntotal);
436     }
437    
438     /* Color a ray that hit a BSDF material */
439     int
440     m_bsdf(OBJREC *m, RAY *r)
441     {
442 greg 2.6 int hitfront;
443 greg 2.1 COLOR ctmp;
444     SDError ec;
445 greg 2.5 FVECT upvec, vtmp;
446 greg 2.1 MFUNC *mf;
447     BSDFDAT nd;
448     /* check arguments */
449     if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) |
450     (m->oargs.nfargs % 3))
451     objerror(m, USER, "bad # arguments");
452 greg 2.6 /* record surface struck */
453 greg 2.9 hitfront = (r->rod > 0);
454 greg 2.1 /* load cal file */
455     mf = getfunc(m, 5, 0x1d, 1);
456 greg 2.25 setfunc(m, r);
457 greg 2.1 /* get thickness */
458     nd.thick = evalue(mf->ep[0]);
459 greg 2.5 if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
460 greg 2.1 nd.thick = .0;
461     /* check shadow */
462     if (r->crtype & SHADOW) {
463 greg 2.9 if (nd.thick != 0)
464 greg 2.3 raytrans(r); /* pass-through */
465 greg 2.5 return(1); /* or shadow */
466 greg 2.1 }
467 greg 2.26 /* check backface visibility */
468     if (!hitfront & !backvis) {
469     raytrans(r);
470     return(1);
471     }
472 greg 2.5 /* check other rays to pass */
473 greg 2.9 if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) ||
474 greg 2.29 (nd.thick > 0) ^ hitfront)) {
475 greg 2.5 raytrans(r); /* hide our proxy */
476 greg 2.1 return(1);
477     }
478 greg 2.5 /* get BSDF data */
479     nd.sd = loadBSDF(m->oargs.sarg[1]);
480 greg 2.1 /* diffuse reflectance */
481 greg 2.6 if (hitfront) {
482 greg 2.1 if (m->oargs.nfargs < 3)
483     setcolor(nd.rdiff, .0, .0, .0);
484     else
485     setcolor(nd.rdiff, m->oargs.farg[0],
486     m->oargs.farg[1],
487     m->oargs.farg[2]);
488     } else {
489 greg 2.26 if (m->oargs.nfargs < 6)
490 greg 2.1 setcolor(nd.rdiff, .0, .0, .0);
491 greg 2.26 else
492 greg 2.1 setcolor(nd.rdiff, m->oargs.farg[3],
493     m->oargs.farg[4],
494     m->oargs.farg[5]);
495     }
496     /* diffuse transmittance */
497     if (m->oargs.nfargs < 9)
498     setcolor(nd.tdiff, .0, .0, .0);
499     else
500     setcolor(nd.tdiff, m->oargs.farg[6],
501     m->oargs.farg[7],
502     m->oargs.farg[8]);
503     nd.mp = m;
504     nd.pr = r;
505     /* get modifiers */
506     raytexture(r, m->omod);
507     /* modify diffuse values */
508     multcolor(nd.rdiff, r->pcol);
509     multcolor(nd.tdiff, r->pcol);
510     /* get up vector */
511     upvec[0] = evalue(mf->ep[1]);
512     upvec[1] = evalue(mf->ep[2]);
513     upvec[2] = evalue(mf->ep[3]);
514     /* return to world coords */
515 greg 2.21 if (mf->fxp != &unitxf) {
516     multv3(upvec, upvec, mf->fxp->xfm);
517     nd.thick *= mf->fxp->sca;
518 greg 2.1 }
519 greg 2.23 if (r->rox != NULL) {
520     multv3(upvec, upvec, r->rox->f.xfm);
521     nd.thick *= r->rox->f.sca;
522     }
523 greg 2.1 raynormal(nd.pnorm, r);
524     /* compute local BSDF xform */
525     ec = SDcompXform(nd.toloc, nd.pnorm, upvec);
526     if (!ec) {
527 greg 2.4 nd.vray[0] = -r->rdir[0];
528     nd.vray[1] = -r->rdir[1];
529     nd.vray[2] = -r->rdir[2];
530     ec = SDmapDir(nd.vray, nd.toloc, nd.vray);
531 greg 2.20 }
532     if (!ec)
533     ec = SDinvXform(nd.fromloc, nd.toloc);
534 greg 2.19 if (ec) {
535     objerror(m, WARNING, "Illegal orientation vector");
536     return(1);
537 greg 2.1 }
538 greg 2.4 /* determine BSDF resolution */
539 greg 2.20 ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin+SDqueryMax, nd.sd);
540     if (ec)
541     objerror(m, USER, transSDError(ec));
542    
543 greg 2.9 nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]);
544     nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]);
545 greg 2.6 if (!hitfront) { /* perturb normal towards hit */
546 greg 2.1 nd.pnorm[0] = -nd.pnorm[0];
547     nd.pnorm[1] = -nd.pnorm[1];
548     nd.pnorm[2] = -nd.pnorm[2];
549     }
550     /* sample reflection */
551     sample_sdf(&nd, SDsampSpR);
552     /* sample transmission */
553     sample_sdf(&nd, SDsampSpT);
554     /* compute indirect diffuse */
555     copycolor(ctmp, nd.rdiff);
556     addcolor(ctmp, nd.runsamp);
557 greg 2.5 if (bright(ctmp) > FTINY) { /* ambient from reflection */
558 greg 2.6 if (!hitfront)
559 greg 2.1 flipsurface(r);
560     multambient(ctmp, r, nd.pnorm);
561     addcolor(r->rcol, ctmp);
562 greg 2.6 if (!hitfront)
563 greg 2.1 flipsurface(r);
564     }
565     copycolor(ctmp, nd.tdiff);
566     addcolor(ctmp, nd.tunsamp);
567     if (bright(ctmp) > FTINY) { /* ambient from other side */
568     FVECT bnorm;
569 greg 2.6 if (hitfront)
570 greg 2.1 flipsurface(r);
571     bnorm[0] = -nd.pnorm[0];
572     bnorm[1] = -nd.pnorm[1];
573     bnorm[2] = -nd.pnorm[2];
574 greg 2.9 if (nd.thick != 0) { /* proxy with offset? */
575 greg 2.5 VCOPY(vtmp, r->rop);
576 greg 2.18 VSUM(r->rop, vtmp, r->ron, nd.thick);
577 greg 2.5 multambient(ctmp, r, bnorm);
578     VCOPY(r->rop, vtmp);
579     } else
580     multambient(ctmp, r, bnorm);
581 greg 2.1 addcolor(r->rcol, ctmp);
582 greg 2.6 if (hitfront)
583 greg 2.1 flipsurface(r);
584     }
585     /* add direct component */
586 greg 2.22 if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) &
587     (nd.sd->tb == NULL)) {
588 greg 2.5 direct(r, dir_brdf, &nd); /* reflection only */
589 greg 2.9 } else if (nd.thick == 0) {
590 greg 2.5 direct(r, dir_bsdf, &nd); /* thin surface scattering */
591     } else {
592     direct(r, dir_brdf, &nd); /* reflection first */
593     VCOPY(vtmp, r->rop); /* offset for transmitted */
594     VSUM(r->rop, vtmp, r->ron, -nd.thick);
595 greg 2.6 direct(r, dir_btdf, &nd); /* separate transmission */
596 greg 2.5 VCOPY(r->rop, vtmp);
597     }
598 greg 2.1 /* clean up */
599     SDfreeCache(nd.sd);
600     return(1);
601     }