13 |
|
#include "func.h" |
14 |
|
#include "bsdf.h" |
15 |
|
#include "random.h" |
16 |
+ |
#include "pmapmat.h" |
17 |
|
|
18 |
|
/* |
19 |
|
* Arguments to this material include optional diffuse colors. |
51 |
|
* Arguments for MAT_BSDF are: |
52 |
|
* 6+ thick BSDFfile ux uy uz funcfile transform |
53 |
|
* 0 |
54 |
< |
* 0|3|9 rdf gdf bdf |
54 |
> |
* 0|3|6|9 rdf gdf bdf |
55 |
|
* rdb gdb bdb |
56 |
|
* rdt gdt bdt |
57 |
|
*/ |
68 |
|
RAY *pr; /* intersected ray */ |
69 |
|
FVECT pnorm; /* perturbed surface normal */ |
70 |
|
FVECT vray; /* local outgoing (return) vector */ |
71 |
< |
double sr_vpsa; /* sqrt of BSDF projected solid angle */ |
71 |
> |
double sr_vpsa[2]; /* sqrt of BSDF projected solid angle extrema */ |
72 |
|
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 */ |
77 |
< |
COLOR tunsamp; /* BSDF hemispherical transmission */ |
78 |
< |
COLOR tdiff; /* added diffuse transmission */ |
76 |
> |
COLOR rdiff; /* diffuse reflection */ |
77 |
> |
COLOR tdiff; /* diffuse transmission */ |
78 |
|
} BSDFDAT; /* BSDF material data */ |
79 |
|
|
80 |
|
#define cvt_sdcolor(cv, svp) ccy2rgb(&(svp)->spec, (svp)->cieY, cv) |
81 |
|
|
82 |
|
/* Jitter ray sample according to projected solid angle and specjitter */ |
83 |
|
static void |
84 |
< |
bsdf_jitter(FVECT vres, BSDFDAT *ndp) |
84 |
> |
bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa) |
85 |
|
{ |
87 |
– |
double sr_psa = ndp->sr_vpsa; |
88 |
– |
|
86 |
|
VCOPY(vres, ndp->vray); |
87 |
|
if (specjitter < 1.) |
88 |
|
sr_psa *= specjitter; |
93 |
|
normalize(vres); |
94 |
|
} |
95 |
|
|
96 |
+ |
/* Evaluate BSDF for direct component, returning true if OK to proceed */ |
97 |
+ |
static int |
98 |
+ |
direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp) |
99 |
+ |
{ |
100 |
+ |
int nsamp, ok = 0; |
101 |
+ |
FVECT vsrc, vsmp, vjit; |
102 |
+ |
double tomega; |
103 |
+ |
double sf, tsr, sd[2]; |
104 |
+ |
COLOR csmp; |
105 |
+ |
SDValue sv; |
106 |
+ |
SDError ec; |
107 |
+ |
int i; |
108 |
+ |
/* transform source direction */ |
109 |
+ |
if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone) |
110 |
+ |
return(0); |
111 |
+ |
/* assign number of samples */ |
112 |
+ |
ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd); |
113 |
+ |
if (ec) |
114 |
+ |
goto baderror; |
115 |
+ |
/* 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 |
+ |
if (dx*dx + dy*dy <= omega+tomega) |
121 |
+ |
return(0); |
122 |
+ |
} |
123 |
+ |
sf = specjitter * ndp->pr->rweight; |
124 |
+ |
if (tomega <= .0) |
125 |
+ |
nsamp = 1; |
126 |
+ |
else if (25.*tomega <= omega) |
127 |
+ |
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 |
+ |
sf = sqrt(omega); |
133 |
+ |
tsr = sqrt(tomega); |
134 |
+ |
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 |
+ |
bsdf_jitter(vjit, ndp, tsr); |
146 |
+ |
/* 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 |
+ |
return(0); /* gratis return */ |
162 |
+ |
} |
163 |
+ |
|
164 |
|
/* Compute source contribution for BSDF (reflected & transmitted) */ |
165 |
|
static void |
166 |
|
dir_bsdf( |
171 |
|
) |
172 |
|
{ |
173 |
|
BSDFDAT *np = (BSDFDAT *)nnp; |
109 |
– |
SDError ec; |
110 |
– |
SDValue sv; |
111 |
– |
FVECT vsrc; |
112 |
– |
FVECT vjit; |
174 |
|
double ldot; |
175 |
|
double dtmp; |
176 |
|
COLOR ctmp; |
181 |
|
if ((-FTINY <= ldot) & (ldot <= FTINY)) |
182 |
|
return; |
183 |
|
|
184 |
< |
if (ldot > .0 && bright(np->rdiff) > FTINY) { |
184 |
> |
if (ldot > 0 && bright(np->rdiff) > FTINY) { |
185 |
|
/* |
186 |
|
* Compute added diffuse reflected component. |
187 |
|
*/ |
190 |
|
scalecolor(ctmp, dtmp); |
191 |
|
addcolor(cval, ctmp); |
192 |
|
} |
193 |
< |
if (ldot < .0 && bright(np->tdiff) > FTINY) { |
193 |
> |
if (ldot < 0 && bright(np->tdiff) > FTINY) { |
194 |
|
/* |
195 |
|
* Compute added diffuse transmission. |
196 |
|
*/ |
199 |
|
scalecolor(ctmp, dtmp); |
200 |
|
addcolor(cval, ctmp); |
201 |
|
} |
202 |
+ |
if (ambRayInPmap(np->pr)) |
203 |
+ |
return; /* specular already in photon map */ |
204 |
|
/* |
205 |
|
* Compute scattering coefficient using BSDF. |
206 |
|
*/ |
207 |
< |
if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone) |
207 |
> |
if (!direct_bsdf_OK(ctmp, ldir, omega, np)) |
208 |
|
return; |
209 |
< |
bsdf_jitter(vjit, np); |
147 |
< |
ec = SDevalBSDF(&sv, vjit, vsrc, np->sd); |
148 |
< |
if (ec) |
149 |
< |
objerror(np->mp, USER, transSDError(ec)); |
150 |
< |
|
151 |
< |
if (sv.cieY <= FTINY) /* not worth using? */ |
152 |
< |
return; |
153 |
< |
cvt_sdcolor(ctmp, &sv); |
154 |
< |
if (ldot > .0) { /* pattern only diffuse reflection */ |
155 |
< |
COLOR ctmp1, ctmp2; |
156 |
< |
dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY |
157 |
< |
: np->sd->rLambBack.cieY; |
158 |
< |
dtmp /= PI * sv.cieY; /* diffuse fraction */ |
159 |
< |
copycolor(ctmp2, np->pr->pcol); |
160 |
< |
scalecolor(ctmp2, dtmp); |
161 |
< |
setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp); |
162 |
< |
addcolor(ctmp1, ctmp2); |
163 |
< |
multcolor(ctmp, ctmp1); /* apply derated pattern */ |
164 |
< |
dtmp = ldot * omega; |
165 |
< |
} else { /* full pattern on transmission */ |
209 |
> |
if (ldot < 0) { /* pattern for specular transmission */ |
210 |
|
multcolor(ctmp, np->pr->pcol); |
211 |
|
dtmp = -ldot * omega; |
212 |
< |
} |
212 |
> |
} else |
213 |
> |
dtmp = ldot * omega; |
214 |
|
scalecolor(ctmp, dtmp); |
215 |
|
addcolor(cval, ctmp); |
216 |
|
} |
225 |
|
) |
226 |
|
{ |
227 |
|
BSDFDAT *np = (BSDFDAT *)nnp; |
183 |
– |
SDError ec; |
184 |
– |
SDValue sv; |
185 |
– |
FVECT vsrc; |
186 |
– |
FVECT vjit; |
228 |
|
double ldot; |
229 |
|
double dtmp; |
230 |
|
COLOR ctmp, ctmp1, ctmp2; |
245 |
|
scalecolor(ctmp, dtmp); |
246 |
|
addcolor(cval, ctmp); |
247 |
|
} |
248 |
+ |
if (ambRayInPmap(np->pr)) |
249 |
+ |
return; /* specular already in photon map */ |
250 |
|
/* |
251 |
|
* Compute reflection coefficient using BSDF. |
252 |
|
*/ |
253 |
< |
if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone) |
253 |
> |
if (!direct_bsdf_OK(ctmp, ldir, omega, np)) |
254 |
|
return; |
212 |
– |
bsdf_jitter(vjit, np); |
213 |
– |
ec = SDevalBSDF(&sv, vjit, vsrc, np->sd); |
214 |
– |
if (ec) |
215 |
– |
objerror(np->mp, USER, transSDError(ec)); |
216 |
– |
|
217 |
– |
if (sv.cieY <= FTINY) /* not worth using? */ |
218 |
– |
return; |
219 |
– |
cvt_sdcolor(ctmp, &sv); |
220 |
– |
/* pattern only diffuse reflection */ |
221 |
– |
dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY |
222 |
– |
: np->sd->rLambBack.cieY; |
223 |
– |
dtmp /= PI * sv.cieY; /* diffuse fraction */ |
224 |
– |
copycolor(ctmp2, np->pr->pcol); |
225 |
– |
scalecolor(ctmp2, dtmp); |
226 |
– |
setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp); |
227 |
– |
addcolor(ctmp1, ctmp2); |
228 |
– |
multcolor(ctmp, ctmp1); /* apply derated pattern */ |
255 |
|
dtmp = ldot * omega; |
256 |
|
scalecolor(ctmp, dtmp); |
257 |
|
addcolor(cval, ctmp); |
267 |
|
) |
268 |
|
{ |
269 |
|
BSDFDAT *np = (BSDFDAT *)nnp; |
244 |
– |
SDError ec; |
245 |
– |
SDValue sv; |
246 |
– |
FVECT vsrc; |
247 |
– |
FVECT vjit; |
270 |
|
double ldot; |
271 |
|
double dtmp; |
272 |
|
COLOR ctmp; |
287 |
|
scalecolor(ctmp, dtmp); |
288 |
|
addcolor(cval, ctmp); |
289 |
|
} |
290 |
+ |
if (ambRayInPmap(np->pr)) |
291 |
+ |
return; /* specular already in photon map */ |
292 |
|
/* |
293 |
|
* Compute scattering coefficient using BSDF. |
294 |
|
*/ |
295 |
< |
if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone) |
295 |
> |
if (!direct_bsdf_OK(ctmp, ldir, omega, np)) |
296 |
|
return; |
273 |
– |
bsdf_jitter(vjit, np); |
274 |
– |
ec = SDevalBSDF(&sv, vjit, vsrc, np->sd); |
275 |
– |
if (ec) |
276 |
– |
objerror(np->mp, USER, transSDError(ec)); |
277 |
– |
|
278 |
– |
if (sv.cieY <= FTINY) /* not worth using? */ |
279 |
– |
return; |
280 |
– |
cvt_sdcolor(ctmp, &sv); |
297 |
|
/* full pattern on transmission */ |
298 |
|
multcolor(ctmp, np->pr->pcol); |
299 |
|
dtmp = -ldot * omega; |
306 |
|
sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat) |
307 |
|
{ |
308 |
|
int nstarget = 1; |
309 |
< |
int nsent = 0; |
309 |
> |
int nsent; |
310 |
|
SDError ec; |
311 |
|
SDValue bsv; |
312 |
< |
double sthick; |
313 |
< |
FVECT vjit, vsmp; |
312 |
> |
double xrand; |
313 |
> |
FVECT vsmp; |
314 |
|
RAY sr; |
299 |
– |
int ntrials; |
315 |
|
/* multiple samples? */ |
316 |
|
if (specjitter > 1.5) { |
317 |
|
nstarget = specjitter*ndp->pr->rweight + .5; |
318 |
< |
if (nstarget < 1) |
304 |
< |
nstarget = 1; |
318 |
> |
nstarget += !nstarget; |
319 |
|
} |
320 |
< |
/* run through our trials */ |
321 |
< |
for (ntrials = 0; nsent < nstarget && ntrials < 9*nstarget; ntrials++) { |
322 |
< |
SDerrorDetail[0] = '\0'; |
323 |
< |
/* sample direction & coef. */ |
324 |
< |
bsdf_jitter(vjit, ndp); |
325 |
< |
ec = SDsampComponent(&bsv, vsmp, vjit, ntrials ? frandom() |
326 |
< |
: urand(ilhash(dimlist,ndims)+samplendx), dcp); |
320 |
> |
/* run through our samples */ |
321 |
> |
for (nsent = 0; nsent < nstarget; nsent++) { |
322 |
> |
if (nstarget == 1) { /* stratify random variable */ |
323 |
> |
xrand = urand(ilhash(dimlist,ndims)+samplendx); |
324 |
> |
if (specjitter < 1.) |
325 |
> |
xrand = .5 + specjitter*(xrand-.5); |
326 |
> |
} else { |
327 |
> |
xrand = (nsent + frandom())/(double)nstarget; |
328 |
> |
} |
329 |
> |
SDerrorDetail[0] = '\0'; /* sample direction & coef. */ |
330 |
> |
bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]); |
331 |
> |
ec = SDsampComponent(&bsv, vsmp, xrand, dcp); |
332 |
|
if (ec) |
333 |
|
objerror(ndp->mp, USER, transSDError(ec)); |
334 |
< |
/* zero component? */ |
316 |
< |
if (bsv.cieY <= FTINY) |
334 |
> |
if (bsv.cieY <= FTINY) /* zero component? */ |
335 |
|
break; |
336 |
|
/* map vector to world */ |
337 |
|
if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone) |
338 |
|
break; |
321 |
– |
/* unintentional penetration? */ |
322 |
– |
if (DOT(sr.rdir, ndp->pr->ron) > .0 ^ vsmp[2] > .0) |
323 |
– |
continue; |
339 |
|
/* spawn a specular ray */ |
340 |
|
if (nstarget > 1) |
341 |
|
bsv.cieY /= (double)nstarget; |
342 |
< |
cvt_sdcolor(sr.rcoef, &bsv); /* use color */ |
343 |
< |
if (usepat) /* pattern on transmission */ |
342 |
> |
cvt_sdcolor(sr.rcoef, &bsv); /* use sample color */ |
343 |
> |
if (usepat) /* apply pattern? */ |
344 |
|
multcolor(sr.rcoef, ndp->pr->pcol); |
345 |
|
if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) { |
346 |
< |
if (maxdepth > 0) |
346 |
> |
if (maxdepth > 0) |
347 |
|
break; |
348 |
< |
++nsent; /* Russian roulette victim */ |
334 |
< |
continue; |
348 |
> |
continue; /* Russian roulette victim */ |
349 |
|
} |
350 |
|
/* need to offset origin? */ |
351 |
< |
if (ndp->thick != .0 && ndp->pr->rod > .0 ^ vsmp[2] > .0) |
351 |
> |
if (ndp->thick != 0 && ndp->pr->rod > 0 ^ vsmp[2] > 0) |
352 |
|
VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick); |
353 |
|
rayvalue(&sr); /* send & evaluate sample */ |
354 |
|
multcolor(sr.rcol, sr.rcoef); |
355 |
|
addcolor(ndp->pr->rcol, sr.rcol); |
342 |
– |
++nsent; |
356 |
|
} |
357 |
|
return(nsent); |
358 |
|
} |
366 |
|
COLORV *unsc; |
367 |
|
|
368 |
|
if (sflags == SDsampSpT) { |
369 |
< |
unsc = ndp->tunsamp; |
370 |
< |
dfp = ndp->sd->tf; |
371 |
< |
cvt_sdcolor(unsc, &ndp->sd->tLamb); |
369 |
> |
unsc = ndp->tdiff; |
370 |
> |
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 |
|
} else /* sflags == SDsampSpR */ { |
375 |
< |
unsc = ndp->runsamp; |
376 |
< |
if (ndp->pr->rod > .0) { |
375 |
> |
unsc = ndp->rdiff; |
376 |
> |
if (ndp->pr->rod > 0) |
377 |
|
dfp = ndp->sd->rf; |
378 |
< |
cvt_sdcolor(unsc, &ndp->sd->rLambFront); |
364 |
< |
} else { |
378 |
> |
else |
379 |
|
dfp = ndp->sd->rb; |
366 |
– |
cvt_sdcolor(unsc, &ndp->sd->rLambBack); |
367 |
– |
} |
380 |
|
} |
369 |
– |
multcolor(unsc, ndp->pr->pcol); |
381 |
|
if (dfp == NULL) /* no specular component? */ |
382 |
|
return(0); |
383 |
|
/* below sampling threshold? */ |
386 |
|
FVECT vjit; |
387 |
|
double d; |
388 |
|
COLOR ctmp; |
389 |
< |
bsdf_jitter(vjit, ndp); |
389 |
> |
bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]); |
390 |
|
d = SDdirectHemi(vjit, sflags, ndp->sd); |
391 |
|
if (sflags == SDsampSpT) { |
392 |
|
copycolor(ctmp, ndp->pr->pcol); |
423 |
|
(m->oargs.nfargs % 3)) |
424 |
|
objerror(m, USER, "bad # arguments"); |
425 |
|
/* record surface struck */ |
426 |
< |
hitfront = (r->rod > .0); |
426 |
> |
hitfront = (r->rod > 0); |
427 |
|
/* load cal file */ |
428 |
|
mf = getfunc(m, 5, 0x1d, 1); |
429 |
+ |
setfunc(m, r); |
430 |
|
/* get thickness */ |
431 |
|
nd.thick = evalue(mf->ep[0]); |
432 |
|
if ((-FTINY <= nd.thick) & (nd.thick <= FTINY)) |
433 |
|
nd.thick = .0; |
434 |
|
/* check shadow */ |
435 |
|
if (r->crtype & SHADOW) { |
436 |
< |
if (nd.thick != .0) |
436 |
> |
if (nd.thick != 0) |
437 |
|
raytrans(r); /* pass-through */ |
438 |
|
return(1); /* or shadow */ |
439 |
|
} |
440 |
+ |
/* check backface visibility */ |
441 |
+ |
if (!hitfront & !backvis) { |
442 |
+ |
raytrans(r); |
443 |
+ |
return(1); |
444 |
+ |
} |
445 |
|
/* check other rays to pass */ |
446 |
|
if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) || |
447 |
< |
nd.thick > .0 ^ hitfront)) { |
447 |
> |
(nd.thick > 0) ^ hitfront)) { |
448 |
|
raytrans(r); /* hide our proxy */ |
449 |
|
return(1); |
450 |
|
} |
451 |
+ |
nd.mp = m; |
452 |
+ |
nd.pr = r; |
453 |
|
/* get BSDF data */ |
454 |
|
nd.sd = loadBSDF(m->oargs.sarg[1]); |
455 |
|
/* diffuse reflectance */ |
456 |
|
if (hitfront) { |
457 |
< |
if (m->oargs.nfargs < 3) |
458 |
< |
setcolor(nd.rdiff, .0, .0, .0); |
459 |
< |
else |
441 |
< |
setcolor(nd.rdiff, m->oargs.farg[0], |
457 |
> |
cvt_sdcolor(nd.rdiff, &nd.sd->rLambFront); |
458 |
> |
if (m->oargs.nfargs >= 3) { |
459 |
> |
setcolor(ctmp, m->oargs.farg[0], |
460 |
|
m->oargs.farg[1], |
461 |
|
m->oargs.farg[2]); |
462 |
+ |
addcolor(nd.rdiff, ctmp); |
463 |
+ |
} |
464 |
|
} else { |
465 |
< |
if (m->oargs.nfargs < 6) { /* check invisible backside */ |
466 |
< |
if (!backvis && (nd.sd->rb == NULL) & |
467 |
< |
(nd.sd->tf == NULL)) { |
448 |
< |
SDfreeCache(nd.sd); |
449 |
< |
raytrans(r); |
450 |
< |
return(1); |
451 |
< |
} |
452 |
< |
setcolor(nd.rdiff, .0, .0, .0); |
453 |
< |
} else |
454 |
< |
setcolor(nd.rdiff, m->oargs.farg[3], |
465 |
> |
cvt_sdcolor(nd.rdiff, &nd.sd->rLambBack); |
466 |
> |
if (m->oargs.nfargs >= 6) { |
467 |
> |
setcolor(ctmp, m->oargs.farg[3], |
468 |
|
m->oargs.farg[4], |
469 |
|
m->oargs.farg[5]); |
470 |
+ |
addcolor(nd.rdiff, ctmp); |
471 |
+ |
} |
472 |
|
} |
473 |
|
/* diffuse transmittance */ |
474 |
< |
if (m->oargs.nfargs < 9) |
475 |
< |
setcolor(nd.tdiff, .0, .0, .0); |
476 |
< |
else |
462 |
< |
setcolor(nd.tdiff, m->oargs.farg[6], |
474 |
> |
cvt_sdcolor(nd.tdiff, &nd.sd->tLamb); |
475 |
> |
if (m->oargs.nfargs >= 9) { |
476 |
> |
setcolor(ctmp, m->oargs.farg[6], |
477 |
|
m->oargs.farg[7], |
478 |
|
m->oargs.farg[8]); |
479 |
< |
nd.mp = m; |
480 |
< |
nd.pr = r; |
479 |
> |
addcolor(nd.tdiff, ctmp); |
480 |
> |
} |
481 |
|
/* get modifiers */ |
482 |
|
raytexture(r, m->omod); |
483 |
|
/* modify diffuse values */ |
488 |
|
upvec[1] = evalue(mf->ep[2]); |
489 |
|
upvec[2] = evalue(mf->ep[3]); |
490 |
|
/* return to world coords */ |
491 |
< |
if (mf->f != &unitxf) { |
492 |
< |
multv3(upvec, upvec, mf->f->xfm); |
493 |
< |
nd.thick *= mf->f->sca; |
491 |
> |
if (mf->fxp != &unitxf) { |
492 |
> |
multv3(upvec, upvec, mf->fxp->xfm); |
493 |
> |
nd.thick *= mf->fxp->sca; |
494 |
|
} |
495 |
+ |
if (r->rox != NULL) { |
496 |
+ |
multv3(upvec, upvec, r->rox->f.xfm); |
497 |
+ |
nd.thick *= r->rox->f.sca; |
498 |
+ |
} |
499 |
|
raynormal(nd.pnorm, r); |
500 |
|
/* compute local BSDF xform */ |
501 |
|
ec = SDcompXform(nd.toloc, nd.pnorm, upvec); |
507 |
|
} |
508 |
|
if (!ec) |
509 |
|
ec = SDinvXform(nd.fromloc, nd.toloc); |
510 |
< |
/* determine BSDF resolution */ |
511 |
< |
if (!ec) |
494 |
< |
ec = SDsizeBSDF(&nd.sr_vpsa, nd.vray, SDqueryMin, nd.sd); |
495 |
< |
if (!ec) |
496 |
< |
nd.sr_vpsa = sqrt(nd.sr_vpsa); |
497 |
< |
else { |
498 |
< |
objerror(m, WARNING, transSDError(ec)); |
499 |
< |
SDfreeCache(nd.sd); |
510 |
> |
if (ec) { |
511 |
> |
objerror(m, WARNING, "Illegal orientation vector"); |
512 |
|
return(1); |
513 |
|
} |
514 |
+ |
/* determine BSDF resolution */ |
515 |
+ |
ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin+SDqueryMax, nd.sd); |
516 |
+ |
if (ec) |
517 |
+ |
objerror(m, USER, transSDError(ec)); |
518 |
+ |
|
519 |
+ |
nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]); |
520 |
+ |
nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]); |
521 |
|
if (!hitfront) { /* perturb normal towards hit */ |
522 |
|
nd.pnorm[0] = -nd.pnorm[0]; |
523 |
|
nd.pnorm[1] = -nd.pnorm[1]; |
528 |
|
/* sample transmission */ |
529 |
|
sample_sdf(&nd, SDsampSpT); |
530 |
|
/* compute indirect diffuse */ |
531 |
< |
copycolor(ctmp, nd.rdiff); |
513 |
< |
addcolor(ctmp, nd.runsamp); |
514 |
< |
if (bright(ctmp) > FTINY) { /* ambient from reflection */ |
531 |
> |
if (bright(nd.rdiff) > FTINY) { /* ambient from reflection */ |
532 |
|
if (!hitfront) |
533 |
|
flipsurface(r); |
534 |
+ |
copycolor(ctmp, nd.rdiff); |
535 |
|
multambient(ctmp, r, nd.pnorm); |
536 |
|
addcolor(r->rcol, ctmp); |
537 |
|
if (!hitfront) |
538 |
|
flipsurface(r); |
539 |
|
} |
540 |
< |
copycolor(ctmp, nd.tdiff); |
523 |
< |
addcolor(ctmp, nd.tunsamp); |
524 |
< |
if (bright(ctmp) > FTINY) { /* ambient from other side */ |
540 |
> |
if (bright(nd.tdiff) > FTINY) { /* ambient from other side */ |
541 |
|
FVECT bnorm; |
542 |
|
if (hitfront) |
543 |
|
flipsurface(r); |
544 |
|
bnorm[0] = -nd.pnorm[0]; |
545 |
|
bnorm[1] = -nd.pnorm[1]; |
546 |
|
bnorm[2] = -nd.pnorm[2]; |
547 |
< |
if (nd.thick != .0) { /* proxy with offset? */ |
547 |
> |
copycolor(ctmp, nd.tdiff); |
548 |
> |
if (nd.thick != 0) { /* proxy with offset? */ |
549 |
|
VCOPY(vtmp, r->rop); |
550 |
< |
VSUM(r->rop, vtmp, r->ron, -nd.thick); |
550 |
> |
VSUM(r->rop, vtmp, r->ron, nd.thick); |
551 |
|
multambient(ctmp, r, bnorm); |
552 |
|
VCOPY(r->rop, vtmp); |
553 |
|
} else |
557 |
|
flipsurface(r); |
558 |
|
} |
559 |
|
/* add direct component */ |
560 |
< |
if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL)) { |
560 |
> |
if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) & |
561 |
> |
(nd.sd->tb == NULL)) { |
562 |
|
direct(r, dir_brdf, &nd); /* reflection only */ |
563 |
< |
} else if (nd.thick == .0) { |
563 |
> |
} else if (nd.thick == 0) { |
564 |
|
direct(r, dir_bsdf, &nd); /* thin surface scattering */ |
565 |
|
} else { |
566 |
|
direct(r, dir_brdf, &nd); /* reflection first */ |