ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_bsdf.c
Revision: 2.31
Committed: Fri Feb 17 23:24:56 2017 UTC (7 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.30: +31 -57 lines
Log Message:
Simplified handling of diffuse components

File Contents

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