ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_bsdf.c
Revision: 2.27
Committed: Tue Feb 24 19:39:26 2015 UTC (9 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.26: +7 -1 lines
Log Message:
Initial check-in of photon map addition by Roland Schregle

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.27 static const char RCSid[] = "$Id: m_bsdf.c,v 1.2 2014/12/18 17:05:22 taschreg Exp taschreg $";
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.27 #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     /*
205     * Compute scattering coefficient using BSDF.
206     */
207 greg 2.13 if (!direct_bsdf_OK(ctmp, ldir, omega, np))
208 greg 2.1 return;
209 greg 2.9 if (ldot > 0) { /* pattern only diffuse reflection */
210 greg 2.1 COLOR ctmp1, ctmp2;
211 greg 2.9 dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
212 greg 2.1 : np->sd->rLambBack.cieY;
213 greg 2.7 /* diffuse fraction */
214     dtmp /= PI * bright(ctmp);
215 greg 2.1 copycolor(ctmp2, np->pr->pcol);
216     scalecolor(ctmp2, dtmp);
217     setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
218     addcolor(ctmp1, ctmp2);
219 greg 2.3 multcolor(ctmp, ctmp1); /* apply derated pattern */
220 greg 2.1 dtmp = ldot * omega;
221     } else { /* full pattern on transmission */
222     multcolor(ctmp, np->pr->pcol);
223     dtmp = -ldot * omega;
224     }
225     scalecolor(ctmp, dtmp);
226     addcolor(cval, ctmp);
227     }
228    
229 greg 2.5 /* Compute source contribution for BSDF (reflected only) */
230     static void
231     dir_brdf(
232     COLOR cval, /* returned coefficient */
233     void *nnp, /* material data */
234     FVECT ldir, /* light source direction */
235     double omega /* light source size */
236     )
237     {
238     BSDFDAT *np = (BSDFDAT *)nnp;
239     double ldot;
240     double dtmp;
241     COLOR ctmp, ctmp1, ctmp2;
242    
243     setcolor(cval, .0, .0, .0);
244    
245     ldot = DOT(np->pnorm, ldir);
246    
247     if (ldot <= FTINY)
248     return;
249    
250     if (bright(np->rdiff) > FTINY) {
251     /*
252     * Compute added diffuse reflected component.
253     */
254     copycolor(ctmp, np->rdiff);
255     dtmp = ldot * omega * (1./PI);
256     scalecolor(ctmp, dtmp);
257     addcolor(cval, ctmp);
258     }
259     /*
260     * Compute reflection coefficient using BSDF.
261     */
262 greg 2.13 if (!direct_bsdf_OK(ctmp, ldir, omega, np))
263 greg 2.5 return;
264     /* pattern only diffuse reflection */
265 greg 2.9 dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY
266 greg 2.5 : np->sd->rLambBack.cieY;
267 greg 2.7 dtmp /= PI * bright(ctmp); /* diffuse fraction */
268 greg 2.5 copycolor(ctmp2, np->pr->pcol);
269     scalecolor(ctmp2, dtmp);
270     setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
271     addcolor(ctmp1, ctmp2);
272     multcolor(ctmp, ctmp1); /* apply derated pattern */
273     dtmp = ldot * omega;
274     scalecolor(ctmp, dtmp);
275     addcolor(cval, ctmp);
276     }
277    
278     /* Compute source contribution for BSDF (transmitted only) */
279     static void
280     dir_btdf(
281     COLOR cval, /* returned coefficient */
282     void *nnp, /* material data */
283     FVECT ldir, /* light source direction */
284     double omega /* light source size */
285     )
286     {
287     BSDFDAT *np = (BSDFDAT *)nnp;
288     double ldot;
289     double dtmp;
290     COLOR ctmp;
291    
292     setcolor(cval, .0, .0, .0);
293    
294     ldot = DOT(np->pnorm, ldir);
295    
296     if (ldot >= -FTINY)
297     return;
298    
299     if (bright(np->tdiff) > FTINY) {
300     /*
301     * Compute added diffuse transmission.
302     */
303     copycolor(ctmp, np->tdiff);
304     dtmp = -ldot * omega * (1.0/PI);
305     scalecolor(ctmp, dtmp);
306     addcolor(cval, ctmp);
307     }
308     /*
309     * Compute scattering coefficient using BSDF.
310     */
311 greg 2.13 if (!direct_bsdf_OK(ctmp, ldir, omega, np))
312 greg 2.5 return;
313     /* full pattern on transmission */
314     multcolor(ctmp, np->pr->pcol);
315     dtmp = -ldot * omega;
316     scalecolor(ctmp, dtmp);
317     addcolor(cval, ctmp);
318     }
319    
320 greg 2.1 /* Sample separate BSDF component */
321     static int
322     sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
323     {
324     int nstarget = 1;
325 greg 2.11 int nsent;
326 greg 2.1 SDError ec;
327     SDValue bsv;
328 greg 2.11 double xrand;
329 greg 2.10 FVECT vsmp;
330 greg 2.1 RAY sr;
331     /* multiple samples? */
332     if (specjitter > 1.5) {
333     nstarget = specjitter*ndp->pr->rweight + .5;
334 greg 2.14 nstarget += !nstarget;
335 greg 2.1 }
336 greg 2.11 /* run through our samples */
337     for (nsent = 0; nsent < nstarget; nsent++) {
338 greg 2.15 if (nstarget == 1) { /* stratify random variable */
339 greg 2.11 xrand = urand(ilhash(dimlist,ndims)+samplendx);
340 greg 2.15 if (specjitter < 1.)
341     xrand = .5 + specjitter*(xrand-.5);
342     } else {
343 greg 2.11 xrand = (nsent + frandom())/(double)nstarget;
344 greg 2.15 }
345 greg 2.11 SDerrorDetail[0] = '\0'; /* sample direction & coef. */
346 greg 2.15 bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]);
347 greg 2.11 ec = SDsampComponent(&bsv, vsmp, xrand, dcp);
348 greg 2.1 if (ec)
349 greg 2.2 objerror(ndp->mp, USER, transSDError(ec));
350 greg 2.11 if (bsv.cieY <= FTINY) /* zero component? */
351 greg 2.1 break;
352     /* map vector to world */
353 greg 2.4 if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
354 greg 2.1 break;
355     /* spawn a specular ray */
356     if (nstarget > 1)
357     bsv.cieY /= (double)nstarget;
358 greg 2.11 cvt_sdcolor(sr.rcoef, &bsv); /* use sample color */
359     if (usepat) /* apply pattern? */
360 greg 2.1 multcolor(sr.rcoef, ndp->pr->pcol);
361     if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
362 greg 2.11 if (maxdepth > 0)
363 greg 2.1 break;
364 greg 2.11 continue; /* Russian roulette victim */
365 greg 2.1 }
366 greg 2.5 /* need to offset origin? */
367 greg 2.9 if (ndp->thick != 0 && ndp->pr->rod > 0 ^ vsmp[2] > 0)
368 greg 2.5 VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
369 greg 2.1 rayvalue(&sr); /* send & evaluate sample */
370     multcolor(sr.rcol, sr.rcoef);
371     addcolor(ndp->pr->rcol, sr.rcol);
372     }
373     return(nsent);
374     }
375    
376     /* Sample non-diffuse components of BSDF */
377     static int
378     sample_sdf(BSDFDAT *ndp, int sflags)
379     {
380     int n, ntotal = 0;
381     SDSpectralDF *dfp;
382     COLORV *unsc;
383    
384     if (sflags == SDsampSpT) {
385     unsc = ndp->tunsamp;
386 greg 2.22 if (ndp->pr->rod > 0)
387     dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb;
388     else
389     dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf;
390 greg 2.1 cvt_sdcolor(unsc, &ndp->sd->tLamb);
391     } else /* sflags == SDsampSpR */ {
392     unsc = ndp->runsamp;
393 greg 2.9 if (ndp->pr->rod > 0) {
394 greg 2.1 dfp = ndp->sd->rf;
395     cvt_sdcolor(unsc, &ndp->sd->rLambFront);
396     } else {
397     dfp = ndp->sd->rb;
398     cvt_sdcolor(unsc, &ndp->sd->rLambBack);
399     }
400     }
401     multcolor(unsc, ndp->pr->pcol);
402     if (dfp == NULL) /* no specular component? */
403     return(0);
404     /* below sampling threshold? */
405     if (dfp->maxHemi <= specthresh+FTINY) {
406 greg 2.3 if (dfp->maxHemi > FTINY) { /* XXX no color from BSDF */
407 greg 2.4 FVECT vjit;
408     double d;
409 greg 2.1 COLOR ctmp;
410 greg 2.15 bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]);
411 greg 2.4 d = SDdirectHemi(vjit, sflags, ndp->sd);
412 greg 2.1 if (sflags == SDsampSpT) {
413     copycolor(ctmp, ndp->pr->pcol);
414     scalecolor(ctmp, d);
415     } else /* no pattern on reflection */
416     setcolor(ctmp, d, d, d);
417     addcolor(unsc, ctmp);
418     }
419     return(0);
420     }
421     /* else need to sample */
422     dimlist[ndims++] = (int)(size_t)ndp->mp;
423     ndims++;
424     for (n = dfp->ncomp; n--; ) { /* loop over components */
425     dimlist[ndims-1] = n + 9438;
426     ntotal += sample_sdcomp(ndp, &dfp->comp[n], sflags==SDsampSpT);
427     }
428     ndims -= 2;
429     return(ntotal);
430     }
431    
432     /* Color a ray that hit a BSDF material */
433     int
434     m_bsdf(OBJREC *m, RAY *r)
435     {
436 greg 2.6 int hitfront;
437 greg 2.1 COLOR ctmp;
438     SDError ec;
439 greg 2.5 FVECT upvec, vtmp;
440 greg 2.1 MFUNC *mf;
441     BSDFDAT nd;
442     /* check arguments */
443     if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) |
444     (m->oargs.nfargs % 3))
445     objerror(m, USER, "bad # arguments");
446 greg 2.6 /* record surface struck */
447 greg 2.9 hitfront = (r->rod > 0);
448 greg 2.1 /* load cal file */
449     mf = getfunc(m, 5, 0x1d, 1);
450 greg 2.25 setfunc(m, r);
451 greg 2.1 /* get thickness */
452     nd.thick = evalue(mf->ep[0]);
453 greg 2.5 if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
454 greg 2.1 nd.thick = .0;
455     /* check shadow */
456     if (r->crtype & SHADOW) {
457 greg 2.9 if (nd.thick != 0)
458 greg 2.3 raytrans(r); /* pass-through */
459 greg 2.5 return(1); /* or shadow */
460 greg 2.1 }
461 greg 2.26 /* check backface visibility */
462     if (!hitfront & !backvis) {
463     raytrans(r);
464     return(1);
465     }
466 greg 2.5 /* check other rays to pass */
467 greg 2.9 if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) ||
468     nd.thick > 0 ^ hitfront)) {
469 greg 2.5 raytrans(r); /* hide our proxy */
470 greg 2.1 return(1);
471     }
472 greg 2.27
473     /* PMAP: skip ambient ray if accounted for by photon map */
474     if (ambRayInPmap(r))
475     return(1);
476    
477 greg 2.5 /* get BSDF data */
478     nd.sd = loadBSDF(m->oargs.sarg[1]);
479 greg 2.1 /* diffuse reflectance */
480 greg 2.6 if (hitfront) {
481 greg 2.1 if (m->oargs.nfargs < 3)
482     setcolor(nd.rdiff, .0, .0, .0);
483     else
484     setcolor(nd.rdiff, m->oargs.farg[0],
485     m->oargs.farg[1],
486     m->oargs.farg[2]);
487     } else {
488 greg 2.26 if (m->oargs.nfargs < 6)
489 greg 2.1 setcolor(nd.rdiff, .0, .0, .0);
490 greg 2.26 else
491 greg 2.1 setcolor(nd.rdiff, m->oargs.farg[3],
492     m->oargs.farg[4],
493     m->oargs.farg[5]);
494     }
495     /* diffuse transmittance */
496     if (m->oargs.nfargs < 9)
497     setcolor(nd.tdiff, .0, .0, .0);
498     else
499     setcolor(nd.tdiff, m->oargs.farg[6],
500     m->oargs.farg[7],
501     m->oargs.farg[8]);
502     nd.mp = m;
503     nd.pr = r;
504     /* get modifiers */
505     raytexture(r, m->omod);
506     /* modify diffuse values */
507     multcolor(nd.rdiff, r->pcol);
508     multcolor(nd.tdiff, r->pcol);
509     /* get up vector */
510     upvec[0] = evalue(mf->ep[1]);
511     upvec[1] = evalue(mf->ep[2]);
512     upvec[2] = evalue(mf->ep[3]);
513     /* return to world coords */
514 greg 2.21 if (mf->fxp != &unitxf) {
515     multv3(upvec, upvec, mf->fxp->xfm);
516     nd.thick *= mf->fxp->sca;
517 greg 2.1 }
518 greg 2.23 if (r->rox != NULL) {
519     multv3(upvec, upvec, r->rox->f.xfm);
520     nd.thick *= r->rox->f.sca;
521     }
522 greg 2.1 raynormal(nd.pnorm, r);
523     /* compute local BSDF xform */
524     ec = SDcompXform(nd.toloc, nd.pnorm, upvec);
525     if (!ec) {
526 greg 2.4 nd.vray[0] = -r->rdir[0];
527     nd.vray[1] = -r->rdir[1];
528     nd.vray[2] = -r->rdir[2];
529     ec = SDmapDir(nd.vray, nd.toloc, nd.vray);
530 greg 2.20 }
531     if (!ec)
532     ec = SDinvXform(nd.fromloc, nd.toloc);
533 greg 2.19 if (ec) {
534     objerror(m, WARNING, "Illegal orientation vector");
535     return(1);
536 greg 2.1 }
537 greg 2.4 /* determine BSDF resolution */
538 greg 2.20 ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin+SDqueryMax, nd.sd);
539     if (ec)
540     objerror(m, USER, transSDError(ec));
541    
542 greg 2.9 nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]);
543     nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]);
544 greg 2.6 if (!hitfront) { /* perturb normal towards hit */
545 greg 2.1 nd.pnorm[0] = -nd.pnorm[0];
546     nd.pnorm[1] = -nd.pnorm[1];
547     nd.pnorm[2] = -nd.pnorm[2];
548     }
549     /* sample reflection */
550     sample_sdf(&nd, SDsampSpR);
551     /* sample transmission */
552     sample_sdf(&nd, SDsampSpT);
553     /* compute indirect diffuse */
554     copycolor(ctmp, nd.rdiff);
555     addcolor(ctmp, nd.runsamp);
556 greg 2.5 if (bright(ctmp) > FTINY) { /* ambient from reflection */
557 greg 2.6 if (!hitfront)
558 greg 2.1 flipsurface(r);
559     multambient(ctmp, r, nd.pnorm);
560     addcolor(r->rcol, ctmp);
561 greg 2.6 if (!hitfront)
562 greg 2.1 flipsurface(r);
563     }
564     copycolor(ctmp, nd.tdiff);
565     addcolor(ctmp, nd.tunsamp);
566     if (bright(ctmp) > FTINY) { /* ambient from other side */
567     FVECT bnorm;
568 greg 2.6 if (hitfront)
569 greg 2.1 flipsurface(r);
570     bnorm[0] = -nd.pnorm[0];
571     bnorm[1] = -nd.pnorm[1];
572     bnorm[2] = -nd.pnorm[2];
573 greg 2.9 if (nd.thick != 0) { /* proxy with offset? */
574 greg 2.5 VCOPY(vtmp, r->rop);
575 greg 2.18 VSUM(r->rop, vtmp, r->ron, nd.thick);
576 greg 2.5 multambient(ctmp, r, bnorm);
577     VCOPY(r->rop, vtmp);
578     } else
579     multambient(ctmp, r, bnorm);
580 greg 2.1 addcolor(r->rcol, ctmp);
581 greg 2.6 if (hitfront)
582 greg 2.1 flipsurface(r);
583     }
584     /* add direct component */
585 greg 2.22 if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) &
586     (nd.sd->tb == NULL)) {
587 greg 2.5 direct(r, dir_brdf, &nd); /* reflection only */
588 greg 2.9 } else if (nd.thick == 0) {
589 greg 2.5 direct(r, dir_bsdf, &nd); /* thin surface scattering */
590     } else {
591     direct(r, dir_brdf, &nd); /* reflection first */
592     VCOPY(vtmp, r->rop); /* offset for transmitted */
593     VSUM(r->rop, vtmp, r->ron, -nd.thick);
594 greg 2.6 direct(r, dir_btdf, &nd); /* separate transmission */
595 greg 2.5 VCOPY(r->rop, vtmp);
596     }
597 greg 2.1 /* clean up */
598     SDfreeCache(nd.sd);
599     return(1);
600     }