ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_bsdf.c
Revision: 2.23
Committed: Tue Apr 9 16:55:15 2013 UTC (11 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.22: +5 -1 lines
Log Message:
Fixed bug for BSDF materials included via octree or mesh instances

File Contents

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