1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: pmapmat.c,v 2.18 2018/06/26 14:42:18 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
================================================================== |
6 |
Photon map support routines for scattering by materials. |
7 |
|
8 |
Roland Schregle (roland.schregle@{hslu.ch, gmail.com}) |
9 |
(c) Fraunhofer Institute for Solar Energy Systems, |
10 |
(c) Lucerne University of Applied Sciences and Arts, |
11 |
supported by the Swiss National Science Foundation (SNSF, #147053) |
12 |
================================================================== |
13 |
|
14 |
*/ |
15 |
|
16 |
|
17 |
|
18 |
#include "pmapmat.h" |
19 |
#include "pmapdata.h" |
20 |
#include "pmaprand.h" |
21 |
#include "otypes.h" |
22 |
#include "data.h" |
23 |
#include "func.h" |
24 |
#include "bsdf.h" |
25 |
#include <math.h> |
26 |
|
27 |
|
28 |
|
29 |
/* Stuff ripped off from material modules */ |
30 |
#define MAXITER 10 |
31 |
#define SP_REFL 01 |
32 |
#define SP_TRAN 02 |
33 |
#define SP_PURE 04 |
34 |
#define SP_FLAT 010 |
35 |
#define SP_BADU 040 |
36 |
#define MLAMBDA 500 |
37 |
#define RINDEX 1.52 |
38 |
#define FRESNE(ci) (exp(-5.85*(ci)) - 0.00287989916) |
39 |
|
40 |
|
41 |
|
42 |
typedef struct { |
43 |
OBJREC *mp; |
44 |
RAY *rp; |
45 |
short specfl; |
46 |
COLOR mcolor, scolor; |
47 |
FVECT vrefl, prdir, pnorm; |
48 |
double alpha2, rdiff, rspec, trans, tdiff, tspec, pdot; |
49 |
} NORMDAT; |
50 |
|
51 |
typedef struct { |
52 |
OBJREC *mp; |
53 |
RAY *rp; |
54 |
short specfl; |
55 |
COLOR mcolor, scolor; |
56 |
FVECT vrefl, prdir, u, v, pnorm; |
57 |
double u_alpha, v_alpha, rdiff, rspec, trans, tdiff, tspec, pdot; |
58 |
} ANISODAT; |
59 |
|
60 |
typedef struct { |
61 |
OBJREC *mp; |
62 |
RAY *pr; |
63 |
DATARRAY *dp; |
64 |
COLOR mcolor; |
65 |
COLOR rdiff; |
66 |
COLOR tdiff; |
67 |
double rspec; |
68 |
double trans; |
69 |
double tspec; |
70 |
FVECT pnorm; |
71 |
double pdot; |
72 |
} BRDFDAT; |
73 |
|
74 |
typedef struct { |
75 |
OBJREC *mp; |
76 |
RAY *pr; |
77 |
FVECT pnorm; |
78 |
FVECT vray; |
79 |
double sr_vpsa [2]; |
80 |
RREAL toloc [3][3]; |
81 |
RREAL fromloc [3][3]; |
82 |
double thick; |
83 |
SDData *sd; |
84 |
COLOR runsamp; |
85 |
COLOR rdiff; |
86 |
COLOR tunsamp; |
87 |
COLOR tdiff; |
88 |
} BSDFDAT; |
89 |
|
90 |
|
91 |
|
92 |
extern const SDCDst SDemptyCD; |
93 |
|
94 |
/* Per-material scattering function dispatch table; return value is usually |
95 |
* zero, indicating photon termination */ |
96 |
int (*photonScatter [NUMOTYPE]) (OBJREC*, RAY*); |
97 |
|
98 |
/* List of antimatter sensor modifier names and associated object set */ |
99 |
char *photonSensorList [MAXSET + 1] = {NULL}; |
100 |
static OBJECT photonSensorSet [MAXSET + 1] = {0}; |
101 |
|
102 |
|
103 |
|
104 |
/* ================ General support routines ================ */ |
105 |
|
106 |
|
107 |
void photonRay (const RAY *rayIn, RAY *rayOut, |
108 |
int rayOutType, COLOR fluxAtten) |
109 |
/* Spawn a new photon ray from a previous one; this is effectively a |
110 |
* customised rayorigin(). |
111 |
* A SPECULAR rayOutType flags this photon as _caustic_ for subsequent hits. |
112 |
* It is preserved for transferred rays (of type PMAP_XFER). |
113 |
* fluxAtten specifies the RGB attenuation of the photon flux effected by |
114 |
* the scattering material. The outgoing flux is then normalised to maintain |
115 |
* a uniform average of 1 over RGB. If fluxAtten == NULL, the flux remains |
116 |
* unchanged for the outgoing photon. fluxAtten is ignored for transferred |
117 |
* rays. |
118 |
* The ray direction is preserved for transferred rays, and undefined for |
119 |
* scattered rays and must be subsequently set by the caller. */ |
120 |
{ |
121 |
rayorigin(rayOut, rayOutType, rayIn, NULL); |
122 |
|
123 |
if (rayIn) { |
124 |
/* Transfer flux */ |
125 |
copycolor(rayOut -> rcol, rayIn -> rcol); |
126 |
|
127 |
/* Copy caustic flag & direction for transferred rays */ |
128 |
if (rayOutType == PMAP_XFER) { |
129 |
/* rayOut -> rtype |= rayIn -> rtype & SPECULAR; */ |
130 |
rayOut -> rtype |= rayIn -> rtype; |
131 |
VCOPY(rayOut -> rdir, rayIn -> rdir); |
132 |
} |
133 |
else if (fluxAtten) { |
134 |
/* Attenuate and normalise flux for scattered rays */ |
135 |
multcolor(rayOut -> rcol, fluxAtten); |
136 |
colorNorm(rayOut -> rcol); |
137 |
} |
138 |
|
139 |
/* Propagate index of emitting light source */ |
140 |
rayOut -> rsrc = rayIn -> rsrc; |
141 |
|
142 |
/* Update maximum photon path distance */ |
143 |
rayOut -> rmax = rayIn -> rmax - rayIn -> rot; |
144 |
} |
145 |
} |
146 |
|
147 |
|
148 |
|
149 |
static void addPhotons (const RAY *r) |
150 |
/* Insert photon hits, where applicable */ |
151 |
{ |
152 |
if (!r -> rlvl) |
153 |
/* Add direct photon map at primary hitpoint */ |
154 |
newPhoton(directPmap, r); |
155 |
else { |
156 |
/* Add global or precomputed photon map at indirect hitpoint */ |
157 |
newPhoton(preCompPmap ? preCompPmap : globalPmap, r); |
158 |
|
159 |
/* Store caustic photon if specular flag set */ |
160 |
if (PMAP_CAUSTICRAY(r)) |
161 |
newPhoton(causticPmap, r); |
162 |
|
163 |
/* Store in contribution photon map */ |
164 |
newPhoton(contribPmap, r); |
165 |
} |
166 |
} |
167 |
|
168 |
|
169 |
|
170 |
void getPhotonSensors (char **sensorList) |
171 |
/* Find antimatter geometry declared as photon sensors */ |
172 |
{ |
173 |
OBJECT i; |
174 |
OBJREC *obj; |
175 |
char **lp; |
176 |
|
177 |
/* Init sensor set */ |
178 |
photonSensorSet [0] = 0; |
179 |
|
180 |
if (!sensorList [0]) |
181 |
return; |
182 |
|
183 |
for (i = 0; i < nobjects; i++) { |
184 |
obj = objptr(i); |
185 |
|
186 |
/* Insert object in sensor set if it's in the specified sensor list |
187 |
* and of type antimatter */ |
188 |
for (lp = sensorList; *lp; lp++) { |
189 |
if (!strcmp(obj -> oname, *lp)) { |
190 |
if (obj -> otype != MAT_CLIP) { |
191 |
sprintf(errmsg, "photon sensor modifier %s is not antimatter", |
192 |
obj -> oname); |
193 |
error(USER, errmsg); |
194 |
} |
195 |
|
196 |
if (photonSensorSet [0] >= AMBLLEN) |
197 |
error(USER, "too many photon sensor modifiers"); |
198 |
|
199 |
insertelem(photonSensorSet, i); |
200 |
} |
201 |
} |
202 |
} |
203 |
|
204 |
if (!photonSensorSet [0]) |
205 |
error(USER, "no photon sensors found"); |
206 |
} |
207 |
|
208 |
|
209 |
|
210 |
/* ================ Material specific scattering routines ================ */ |
211 |
|
212 |
|
213 |
static int isoSpecPhotonScatter (NORMDAT *nd, RAY *rayOut) |
214 |
/* Generate direction for isotropically specularly reflected |
215 |
or transmitted ray. Returns 1 if successful. */ |
216 |
{ |
217 |
FVECT u, v, h; |
218 |
RAY *rayIn = nd -> rp; |
219 |
double d, d2, sinp, cosp; |
220 |
int niter, i = 0; |
221 |
|
222 |
/* Set up sample coordinates */ |
223 |
getperpendicular(u, nd -> pnorm, 1); |
224 |
fcross(v, nd -> pnorm, u); |
225 |
|
226 |
if (nd -> specfl & SP_REFL) { |
227 |
/* Specular reflection; make MAXITER attempts at getting a ray */ |
228 |
|
229 |
for (niter = 0; niter < MAXITER; niter++) { |
230 |
d = 2 * PI * pmapRandom(scatterState); |
231 |
cosp = cos(d); |
232 |
sinp = sin(d); |
233 |
d2 = pmapRandom(scatterState); |
234 |
d = d2 <= FTINY ? 1 : sqrt(nd -> alpha2 * -log(d2)); |
235 |
|
236 |
for (i = 0; i < 3; i++) |
237 |
h [i] = nd -> pnorm [i] + d * (cosp * u [i] + sinp * v [i]); |
238 |
|
239 |
d = -2 * DOT(h, rayIn -> rdir) / (1 + d * d); |
240 |
VSUM(rayOut -> rdir, rayIn -> rdir, h, d); |
241 |
|
242 |
if (DOT(rayOut -> rdir, rayIn -> ron) > FTINY) |
243 |
return 1; |
244 |
} |
245 |
|
246 |
return 0; |
247 |
} |
248 |
|
249 |
else { |
250 |
/* Specular transmission; make MAXITER attempts at getting a ray */ |
251 |
|
252 |
for (niter = 0; niter < MAXITER; niter++) { |
253 |
d = 2 * PI * pmapRandom(scatterState); |
254 |
cosp = cos(d); |
255 |
sinp = sin(d); |
256 |
d2 = pmapRandom(scatterState); |
257 |
d = d2 <= FTINY ? 1 : sqrt(-log(d2) * nd -> alpha2); |
258 |
|
259 |
for (i = 0; i < 3; i++) |
260 |
rayOut -> rdir [i] = nd -> prdir [i] + |
261 |
d * (cosp * u [i] + sinp * v [i]); |
262 |
|
263 |
if (DOT(rayOut -> rdir, rayIn -> ron) < -FTINY) { |
264 |
normalize(rayOut -> rdir); |
265 |
return 1; |
266 |
} |
267 |
} |
268 |
|
269 |
return 0; |
270 |
} |
271 |
} |
272 |
|
273 |
|
274 |
|
275 |
static void diffPhotonScatter (FVECT normal, RAY* rayOut) |
276 |
/* Generate cosine-weighted direction for diffuse ray */ |
277 |
{ |
278 |
const RREAL cosThetaSqr = pmapRandom(scatterState), |
279 |
cosTheta = sqrt(cosThetaSqr), |
280 |
sinTheta = sqrt(1 - cosThetaSqr), |
281 |
phi = 2 * PI * pmapRandom(scatterState), |
282 |
du = cos(phi) * sinTheta, dv = sin(phi) * sinTheta; |
283 |
FVECT u, v; |
284 |
int i = 0; |
285 |
|
286 |
/* Set up sample coordinates */ |
287 |
getperpendicular(u, normal, 1); |
288 |
fcross(v, normal, u); |
289 |
|
290 |
/* Convert theta & phi to cartesian */ |
291 |
for (i = 0; i < 3; i++) |
292 |
rayOut -> rdir [i] = du * u [i] + dv * v [i] + cosTheta * normal [i]; |
293 |
|
294 |
normalize(rayOut -> rdir); |
295 |
} |
296 |
|
297 |
|
298 |
|
299 |
static int normalPhotonScatter (OBJREC *mat, RAY *rayIn) |
300 |
/* Generate new photon ray for isotropic material and recurse */ |
301 |
{ |
302 |
NORMDAT nd; |
303 |
int i, hastexture; |
304 |
float xi, albedo, prdiff, ptdiff, prspec, ptspec; |
305 |
double d, fresnel; |
306 |
RAY rayOut; |
307 |
|
308 |
if (mat -> oargs.nfargs != (mat -> otype == MAT_TRANS ? 7 : 5)) |
309 |
objerror(mat, USER, "bad number of arguments"); |
310 |
|
311 |
/* Check for back side; reorient if back is visible */ |
312 |
if (rayIn -> rod < 0) |
313 |
if (!backvis && mat -> otype != MAT_TRANS) |
314 |
return 0; |
315 |
else { |
316 |
/* Get modifiers */ |
317 |
raytexture(rayIn, mat -> omod); |
318 |
flipsurface(rayIn); |
319 |
} |
320 |
else raytexture(rayIn, mat -> omod); |
321 |
|
322 |
nd.rp = rayIn; |
323 |
|
324 |
/* Get material color */ |
325 |
copycolor(nd.mcolor, mat -> oargs.farg); |
326 |
|
327 |
/* Get roughness */ |
328 |
nd.specfl = 0; |
329 |
nd.alpha2 = mat -> oargs.farg [4]; |
330 |
|
331 |
if ((nd.alpha2 *= nd.alpha2) <= FTINY) |
332 |
nd.specfl |= SP_PURE; |
333 |
|
334 |
if (rayIn -> ro != NULL && isflat(rayIn -> ro -> otype)) |
335 |
nd.specfl |= SP_FLAT; |
336 |
|
337 |
/* Perturb normal */ |
338 |
if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY)) )) |
339 |
nd.pdot = raynormal(nd.pnorm, rayIn); |
340 |
else { |
341 |
VCOPY(nd.pnorm, rayIn -> ron); |
342 |
nd.pdot = rayIn -> rod; |
343 |
} |
344 |
|
345 |
nd.pdot = max(nd.pdot, .001); |
346 |
|
347 |
/* Modify material color */ |
348 |
multcolor(nd.mcolor, rayIn -> pcol); |
349 |
nd.rspec = mat -> oargs.farg [3]; |
350 |
|
351 |
/* Approximate Fresnel term */ |
352 |
if (nd.specfl & SP_PURE && nd.rspec > FTINY) { |
353 |
fresnel = FRESNE(rayIn -> rod); |
354 |
nd.rspec += fresnel * (1 - nd.rspec); |
355 |
} |
356 |
else fresnel = 0; |
357 |
|
358 |
/* Transmission params */ |
359 |
if (mat -> otype == MAT_TRANS) { |
360 |
nd.trans = mat -> oargs.farg [5] * (1 - nd.rspec); |
361 |
nd.tspec = nd.trans * mat -> oargs.farg [6]; |
362 |
nd.tdiff = nd.trans - nd.tspec; |
363 |
} |
364 |
else nd.tdiff = nd.tspec = nd.trans = 0; |
365 |
|
366 |
/* Specular reflection params */ |
367 |
if (nd.rspec > FTINY) { |
368 |
/* Specular color */ |
369 |
if (mat -> otype != MAT_METAL) |
370 |
setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec); |
371 |
else if (fresnel > FTINY) { |
372 |
d = nd.rspec * (1 - fresnel); |
373 |
for (i = 0; i < 3; i++) |
374 |
nd.scolor [i] = fresnel + nd.mcolor [i] * d; |
375 |
} |
376 |
else { |
377 |
copycolor(nd.scolor, nd.mcolor); |
378 |
scalecolor(nd.scolor, nd.rspec); |
379 |
} |
380 |
} |
381 |
else setcolor(nd.scolor, 0, 0, 0); |
382 |
|
383 |
/* Diffuse reflection params */ |
384 |
nd.rdiff = 1 - nd.trans - nd.rspec; |
385 |
|
386 |
/* Set up probabilities */ |
387 |
prdiff = ptdiff = ptspec = colorAvg(nd.mcolor); |
388 |
prdiff *= nd.rdiff; |
389 |
ptdiff *= nd.tdiff; |
390 |
prspec = colorAvg(nd.scolor); |
391 |
ptspec *= nd.tspec; |
392 |
albedo = prdiff + ptdiff + prspec + ptspec; |
393 |
|
394 |
/* Insert direct and indirect photon hits if diffuse component */ |
395 |
if (prdiff > FTINY || ptdiff > FTINY) |
396 |
addPhotons(rayIn); |
397 |
|
398 |
xi = pmapRandom(rouletteState); |
399 |
|
400 |
if (xi > albedo) |
401 |
/* Absorbed */ |
402 |
return 0; |
403 |
|
404 |
if (xi > (albedo -= prspec)) { |
405 |
/* Specular reflection */ |
406 |
nd.specfl |= SP_REFL; |
407 |
|
408 |
if (nd.specfl & SP_PURE) { |
409 |
/* Perfect specular reflection */ |
410 |
for (i = 0; i < 3; i++) { |
411 |
/* Reflected ray */ |
412 |
nd.vrefl [i] = rayIn -> rdir [i] + 2 * nd.pdot * nd.pnorm [i]; |
413 |
} |
414 |
|
415 |
/* Penetration? */ |
416 |
if (hastexture && DOT(nd.vrefl, rayIn -> ron) <= FTINY) |
417 |
for (i = 0; i < 3; i++) { |
418 |
/* Safety measure */ |
419 |
nd.vrefl [i] = rayIn -> rdir [i] + |
420 |
2 * rayIn -> rod * rayIn -> ron [i]; |
421 |
} |
422 |
|
423 |
VCOPY(rayOut.rdir, nd.vrefl); |
424 |
} |
425 |
|
426 |
else if (!isoSpecPhotonScatter(&nd, &rayOut)) |
427 |
return 0; |
428 |
|
429 |
photonRay(rayIn, &rayOut, PMAP_SPECREFL, nd.scolor); |
430 |
} |
431 |
|
432 |
else if (xi > (albedo -= ptspec)) { |
433 |
/* Specular transmission */ |
434 |
nd.specfl |= SP_TRAN; |
435 |
|
436 |
if (hastexture) { |
437 |
/* Perturb */ |
438 |
for (i = 0; i < 3; i++) |
439 |
nd.prdir [i] = rayIn -> rdir [i] - rayIn -> pert [i]; |
440 |
|
441 |
if (DOT(nd.prdir, rayIn -> ron) < -FTINY) |
442 |
normalize(nd.prdir); |
443 |
else VCOPY(nd.prdir, rayIn -> rdir); |
444 |
} |
445 |
else VCOPY(nd.prdir, rayIn -> rdir); |
446 |
|
447 |
if ((nd.specfl & (SP_TRAN | SP_PURE)) == (SP_TRAN | SP_PURE)) |
448 |
/* Perfect specular transmission */ |
449 |
VCOPY(rayOut.rdir, nd.prdir); |
450 |
else if (!isoSpecPhotonScatter(&nd, &rayOut)) |
451 |
return 0; |
452 |
|
453 |
photonRay(rayIn, &rayOut, PMAP_SPECTRANS, nd.mcolor); |
454 |
} |
455 |
|
456 |
else if (xi > (albedo -= prdiff)) { |
457 |
/* Diffuse reflection */ |
458 |
photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.mcolor); |
459 |
diffPhotonScatter(hastexture ? nd.pnorm : rayIn -> ron, &rayOut); |
460 |
} |
461 |
|
462 |
else { |
463 |
/* Diffuse transmission */ |
464 |
flipsurface(rayIn); |
465 |
photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.mcolor); |
466 |
|
467 |
if (hastexture) { |
468 |
FVECT bnorm; |
469 |
bnorm [0] = -nd.pnorm [0]; |
470 |
bnorm [1] = -nd.pnorm [1]; |
471 |
bnorm [2] = -nd.pnorm [2]; |
472 |
diffPhotonScatter(bnorm, &rayOut); |
473 |
} |
474 |
else diffPhotonScatter(rayIn -> ron, &rayOut); |
475 |
} |
476 |
|
477 |
tracePhoton(&rayOut); |
478 |
return 0; |
479 |
} |
480 |
|
481 |
|
482 |
|
483 |
static void getacoords (ANISODAT *np) |
484 |
/* Set up coordinate system for anisotropic sampling; cloned from aniso.c */ |
485 |
{ |
486 |
MFUNC *mf; |
487 |
int i; |
488 |
|
489 |
mf = getfunc(np->mp, 3, 0x7, 1); |
490 |
setfunc(np->mp, np->rp); |
491 |
errno = 0; |
492 |
|
493 |
for (i = 0; i < 3; i++) |
494 |
np->u[i] = evalue(mf->ep[i]); |
495 |
|
496 |
if ((errno == EDOM) | (errno == ERANGE)) { |
497 |
objerror(np->mp, WARNING, "compute error"); |
498 |
np->specfl |= SP_BADU; |
499 |
return; |
500 |
} |
501 |
|
502 |
if (mf->fxp != &unitxf) |
503 |
multv3(np->u, np->u, mf->fxp->xfm); |
504 |
|
505 |
fcross(np->v, np->pnorm, np->u); |
506 |
|
507 |
if (normalize(np->v) == 0.0) { |
508 |
objerror(np->mp, WARNING, "illegal orientation vector"); |
509 |
np->specfl |= SP_BADU; |
510 |
return; |
511 |
} |
512 |
|
513 |
fcross(np->u, np->v, np->pnorm); |
514 |
} |
515 |
|
516 |
|
517 |
|
518 |
static int anisoSpecPhotonScatter (ANISODAT *nd, RAY *rayOut) |
519 |
/* Generate direction for anisotropically specularly reflected |
520 |
or transmitted ray. Returns 1 if successful. */ |
521 |
{ |
522 |
FVECT h; |
523 |
double d, d2, sinp, cosp; |
524 |
int niter, i; |
525 |
RAY *rayIn = nd -> rp; |
526 |
|
527 |
if (rayIn -> ro != NULL && isflat(rayIn -> ro -> otype)) |
528 |
nd -> specfl |= SP_FLAT; |
529 |
|
530 |
/* set up coordinates */ |
531 |
getacoords(nd); |
532 |
|
533 |
if (rayOut -> rtype & TRANS) { |
534 |
/* Specular transmission */ |
535 |
|
536 |
if (DOT(rayIn -> pert, rayIn -> pert) <= sqr(FTINY)) |
537 |
VCOPY(nd -> prdir, rayIn -> rdir); |
538 |
else { |
539 |
/* perturb */ |
540 |
for (i = 0; i < 3; i++) |
541 |
nd -> prdir [i] = rayIn -> rdir [i] - rayIn -> pert [i]; |
542 |
|
543 |
if (DOT(nd -> prdir, rayIn -> ron) < -FTINY) |
544 |
normalize(nd -> prdir); |
545 |
else VCOPY(nd -> prdir, rayIn -> rdir); |
546 |
} |
547 |
|
548 |
/* Make MAXITER attempts at getting a ray */ |
549 |
for (niter = 0; niter < MAXITER; niter++) { |
550 |
d = 2 * PI * pmapRandom(scatterState); |
551 |
cosp = cos(d) * nd -> u_alpha; |
552 |
sinp = sin(d) * nd -> v_alpha; |
553 |
d = sqrt(sqr(cosp) + sqr(sinp)); |
554 |
cosp /= d; |
555 |
sinp /= d; |
556 |
d2 = pmapRandom(scatterState); |
557 |
d = d2 <= FTINY ? 1 |
558 |
: sqrt(-log(d2) / |
559 |
(sqr(cosp) / sqr(nd -> u_alpha) + |
560 |
sqr(sinp) / (nd -> v_alpha * nd -> u_alpha))); |
561 |
|
562 |
for (i = 0; i < 3; i++) |
563 |
rayOut -> rdir [i] = nd -> prdir [i] + d * |
564 |
(cosp * nd -> u [i] + sinp * nd -> v [i]); |
565 |
|
566 |
if (DOT(rayOut -> rdir, rayIn -> ron) < -FTINY) { |
567 |
normalize(rayOut -> rdir); |
568 |
return 1; |
569 |
} |
570 |
} |
571 |
|
572 |
return 0; |
573 |
} |
574 |
|
575 |
else { |
576 |
/* Specular reflection */ |
577 |
|
578 |
/* Make MAXITER attempts at getting a ray */ |
579 |
for (niter = 0; niter < MAXITER; niter++) { |
580 |
d = 2 * PI * pmapRandom(scatterState); |
581 |
cosp = cos(d) * nd -> u_alpha; |
582 |
sinp = sin(d) * nd -> v_alpha; |
583 |
d = sqrt(sqr(cosp) + sqr(sinp)); |
584 |
cosp /= d; |
585 |
sinp /= d; |
586 |
d2 = pmapRandom(scatterState); |
587 |
d = d2 <= FTINY ? 1 |
588 |
: sqrt(-log(d2) / |
589 |
(sqr(cosp) / sqr(nd -> u_alpha) + |
590 |
sqr(sinp) / (nd->v_alpha * nd->v_alpha))); |
591 |
|
592 |
for (i = 0; i < 3; i++) |
593 |
h [i] = nd -> pnorm [i] + |
594 |
d * (cosp * nd -> u [i] + sinp * nd -> v [i]); |
595 |
|
596 |
d = -2 * DOT(h, rayIn -> rdir) / (1 + d * d); |
597 |
VSUM(rayOut -> rdir, rayIn -> rdir, h, d); |
598 |
|
599 |
if (DOT(rayOut -> rdir, rayIn -> ron) > FTINY) |
600 |
return 1; |
601 |
} |
602 |
|
603 |
return 0; |
604 |
} |
605 |
} |
606 |
|
607 |
|
608 |
|
609 |
static int anisoPhotonScatter (OBJREC *mat, RAY *rayIn) |
610 |
/* Generate new photon ray for anisotropic material and recurse */ |
611 |
{ |
612 |
ANISODAT nd; |
613 |
float xi, albedo, prdiff, ptdiff, prspec, ptspec; |
614 |
RAY rayOut; |
615 |
|
616 |
if (mat -> oargs.nfargs != (mat -> otype == MAT_TRANS2 ? 8 : 6)) |
617 |
objerror(mat, USER, "bad number of real arguments"); |
618 |
|
619 |
nd.rp = rayIn; |
620 |
nd.mp = objptr(rayIn -> ro -> omod); |
621 |
|
622 |
/* get material color */ |
623 |
copycolor(nd.mcolor, mat -> oargs.farg); |
624 |
|
625 |
/* get roughness */ |
626 |
nd.specfl = 0; |
627 |
nd.u_alpha = mat -> oargs.farg [4]; |
628 |
nd.v_alpha = mat -> oargs.farg [5]; |
629 |
if (nd.u_alpha < FTINY || nd.v_alpha <= FTINY) |
630 |
objerror(mat, USER, "roughness too small"); |
631 |
|
632 |
/* check for back side; reorient if back is visible */ |
633 |
if (rayIn -> rod < 0) |
634 |
if (!backvis && mat -> otype != MAT_TRANS2) |
635 |
return 0; |
636 |
else { |
637 |
/* get modifiers */ |
638 |
raytexture(rayIn, mat -> omod); |
639 |
flipsurface(rayIn); |
640 |
} |
641 |
else raytexture(rayIn, mat -> omod); |
642 |
|
643 |
/* perturb normal */ |
644 |
nd.pdot = max(raynormal(nd.pnorm, rayIn), .001); |
645 |
|
646 |
/* modify material color */ |
647 |
multcolor(nd.mcolor, rayIn -> pcol); |
648 |
nd.rspec = mat -> oargs.farg [3]; |
649 |
|
650 |
/* transmission params */ |
651 |
if (mat -> otype == MAT_TRANS2) { |
652 |
nd.trans = mat -> oargs.farg [6] * (1 - nd.rspec); |
653 |
nd.tspec = nd.trans * mat -> oargs.farg [7]; |
654 |
nd.tdiff = nd.trans - nd.tspec; |
655 |
if (nd.tspec > FTINY) |
656 |
nd.specfl |= SP_TRAN; |
657 |
} |
658 |
else nd.tdiff = nd.tspec = nd.trans = 0; |
659 |
|
660 |
/* specular reflection params */ |
661 |
if (nd.rspec > FTINY) { |
662 |
nd.specfl |= SP_REFL; |
663 |
|
664 |
/* comput e specular color */ |
665 |
if (mat -> otype == MAT_METAL2) |
666 |
copycolor(nd.scolor, nd.mcolor); |
667 |
else setcolor(nd.scolor, 1, 1, 1); |
668 |
|
669 |
scalecolor(nd.scolor, nd.rspec); |
670 |
} |
671 |
else setcolor(nd.scolor, 0, 0, 0); |
672 |
|
673 |
/* diffuse reflection params */ |
674 |
nd.rdiff = 1 - nd.trans - nd.rspec; |
675 |
|
676 |
/* Set up probabilities */ |
677 |
prdiff = ptdiff = ptspec = colorAvg(nd.mcolor); |
678 |
prdiff *= nd.rdiff; |
679 |
ptdiff *= nd.tdiff; |
680 |
prspec = colorAvg(nd.scolor); |
681 |
ptspec *= nd.tspec; |
682 |
albedo = prdiff + ptdiff + prspec + ptspec; |
683 |
|
684 |
/* Insert direct and indirect photon hits if diffuse component */ |
685 |
if (prdiff > FTINY || ptdiff > FTINY) |
686 |
addPhotons(rayIn); |
687 |
|
688 |
xi = pmapRandom(rouletteState); |
689 |
|
690 |
if (xi > albedo) |
691 |
/* Absorbed */ |
692 |
return 0; |
693 |
|
694 |
if (xi > (albedo -= prspec)) |
695 |
/* Specular reflection */ |
696 |
if (!(nd.specfl & SP_BADU)) { |
697 |
photonRay(rayIn, &rayOut, PMAP_SPECREFL, nd.scolor); |
698 |
|
699 |
if (!anisoSpecPhotonScatter(&nd, &rayOut)) |
700 |
return 0; |
701 |
} |
702 |
else return 0; |
703 |
|
704 |
else if (xi > (albedo -= ptspec)) |
705 |
/* Specular transmission */ |
706 |
|
707 |
if (!(nd.specfl & SP_BADU)) { |
708 |
/* Specular transmission */ |
709 |
photonRay(rayIn, &rayOut, PMAP_SPECTRANS, nd.mcolor); |
710 |
|
711 |
if (!anisoSpecPhotonScatter(&nd, &rayOut)) |
712 |
return 0; |
713 |
} |
714 |
else return 0; |
715 |
|
716 |
else if (xi > (albedo -= prdiff)) { |
717 |
/* Diffuse reflection */ |
718 |
photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.mcolor); |
719 |
diffPhotonScatter(nd.pnorm, &rayOut); |
720 |
} |
721 |
|
722 |
else { |
723 |
/* Diffuse transmission */ |
724 |
FVECT bnorm; |
725 |
flipsurface(rayIn); |
726 |
bnorm [0] = -nd.pnorm [0]; |
727 |
bnorm [1] = -nd.pnorm [1]; |
728 |
bnorm [2] = -nd.pnorm [2]; |
729 |
|
730 |
photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.mcolor); |
731 |
diffPhotonScatter(bnorm, &rayOut); |
732 |
} |
733 |
|
734 |
tracePhoton(&rayOut); |
735 |
return 0; |
736 |
} |
737 |
|
738 |
|
739 |
static double mylog (double x) |
740 |
/* special log for extinction coefficients; cloned from dielectric.c */ |
741 |
{ |
742 |
if (x < 1e-40) |
743 |
return(-100.); |
744 |
|
745 |
if (x >= 1.) |
746 |
return(0.); |
747 |
|
748 |
return(log(x)); |
749 |
} |
750 |
|
751 |
|
752 |
static int dielectricPhotonScatter (OBJREC *mat, RAY *rayIn) |
753 |
/* Generate new photon ray for dielectric material and recurse */ |
754 |
{ |
755 |
double cos1, cos2, nratio, d1, d2, refl; |
756 |
COLOR ctrans, talb; |
757 |
FVECT dnorm; |
758 |
int hastexture, i; |
759 |
RAY rayOut; |
760 |
|
761 |
if (mat -> oargs.nfargs != (mat -> otype == MAT_DIELECTRIC ? 5 : 8)) |
762 |
objerror(mat, USER, "bad arguments"); |
763 |
|
764 |
/* get modifiers */ |
765 |
raytexture(rayIn, mat -> omod); |
766 |
|
767 |
if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY)))) |
768 |
/* Perturb normal */ |
769 |
cos1 = raynormal(dnorm, rayIn); |
770 |
else { |
771 |
VCOPY(dnorm, rayIn -> ron); |
772 |
cos1 = rayIn -> rod; |
773 |
} |
774 |
|
775 |
/* index of refraction */ |
776 |
nratio = mat -> otype == |
777 |
MAT_DIELECTRIC ? mat->oargs.farg[3] + mat->oargs.farg[4] / MLAMBDA |
778 |
: mat->oargs.farg[3] / mat->oargs.farg[7]; |
779 |
|
780 |
if (cos1 < 0) { |
781 |
/* inside */ |
782 |
hastexture = -hastexture; |
783 |
cos1 = -cos1; |
784 |
dnorm [0] = -dnorm [0]; |
785 |
dnorm [1] = -dnorm [1]; |
786 |
dnorm [2] = -dnorm [2]; |
787 |
setcolor(rayIn -> cext, |
788 |
-mylog(mat -> oargs.farg [0] * rayIn -> pcol [0]), |
789 |
-mylog(mat -> oargs.farg [1] * rayIn -> pcol [1]), |
790 |
-mylog(mat -> oargs.farg [2] * rayIn -> pcol [2])); |
791 |
setcolor(rayIn -> albedo, 0, 0, 0); |
792 |
rayIn -> gecc = 0; |
793 |
|
794 |
if (mat -> otype == MAT_INTERFACE) { |
795 |
setcolor(ctrans, |
796 |
-mylog(mat -> oargs.farg [4] * rayIn -> pcol [0]), |
797 |
-mylog(mat -> oargs.farg [5] * rayIn -> pcol [1]), |
798 |
-mylog(mat -> oargs.farg [6] * rayIn -> pcol [2])); |
799 |
setcolor(talb, 0, 0, 0); |
800 |
} |
801 |
else { |
802 |
copycolor(ctrans, cextinction); |
803 |
copycolor(talb, salbedo); |
804 |
} |
805 |
} |
806 |
|
807 |
else { |
808 |
/* outside */ |
809 |
nratio = 1.0 / nratio; |
810 |
setcolor(ctrans, |
811 |
-mylog(mat -> oargs.farg [0] * rayIn -> pcol [0]), |
812 |
-mylog(mat -> oargs.farg [1] * rayIn -> pcol [1]), |
813 |
-mylog(mat -> oargs.farg [2] * rayIn -> pcol [2])); |
814 |
setcolor(talb, 0, 0, 0); |
815 |
|
816 |
if (mat -> otype == MAT_INTERFACE) { |
817 |
setcolor(rayIn -> cext, |
818 |
-mylog(mat -> oargs.farg [4] * rayIn -> pcol [0]), |
819 |
-mylog(mat -> oargs.farg [5] * rayIn -> pcol [1]), |
820 |
-mylog(mat -> oargs.farg [6] * rayIn -> pcol [2])); |
821 |
setcolor(rayIn -> albedo, 0, 0, 0); |
822 |
rayIn -> gecc = 0; |
823 |
} |
824 |
} |
825 |
|
826 |
/* compute cos theta2 */ |
827 |
d2 = 1 - sqr(nratio) * (1 - sqr(cos1)); |
828 |
|
829 |
if (d2 < FTINY) { |
830 |
/* Total reflection */ |
831 |
refl = cos2 = 1.0; |
832 |
} |
833 |
else { |
834 |
/* Refraction, compute Fresnel's equations */ |
835 |
cos2 = sqrt(d2); |
836 |
d1 = cos1; |
837 |
d2 = nratio * cos2; |
838 |
d1 = (d1 - d2) / (d1 + d2); |
839 |
refl = sqr(d1); |
840 |
d1 = 1 / cos1; |
841 |
d2 = nratio / cos2; |
842 |
d1 = (d1 - d2) / (d1 + d2); |
843 |
refl += sqr(d1); |
844 |
refl *= 0.5; |
845 |
} |
846 |
|
847 |
if (pmapRandom(rouletteState) > refl) { |
848 |
/* Refraction */ |
849 |
photonRay(rayIn, &rayOut, PMAP_REFRACT, NULL); |
850 |
d1 = nratio * cos1 - cos2; |
851 |
|
852 |
for (i = 0; i < 3; i++) |
853 |
rayOut.rdir [i] = nratio * rayIn -> rdir [i] + d1 * dnorm [i]; |
854 |
|
855 |
if (hastexture && DOT(rayOut.rdir, rayIn->ron)*hastexture >= -FTINY) { |
856 |
d1 *= hastexture; |
857 |
|
858 |
for (i = 0; i < 3; i++) |
859 |
rayOut.rdir [i] = nratio * rayIn -> rdir [i] + |
860 |
d1 * rayIn -> ron [i]; |
861 |
|
862 |
normalize(rayOut.rdir); |
863 |
} |
864 |
|
865 |
copycolor(rayOut.cext, ctrans); |
866 |
copycolor(rayOut.albedo, talb); |
867 |
} |
868 |
|
869 |
else { |
870 |
/* Reflection */ |
871 |
photonRay(rayIn, &rayOut, PMAP_SPECREFL, NULL); |
872 |
VSUM(rayOut.rdir, rayIn -> rdir, dnorm, 2 * cos1); |
873 |
|
874 |
if (hastexture && DOT(rayOut.rdir, rayIn->ron) * hastexture <= FTINY) |
875 |
for (i = 0; i < 3; i++) |
876 |
rayOut.rdir [i] = rayIn -> rdir [i] + |
877 |
2 * rayIn -> rod * rayIn -> ron [i]; |
878 |
} |
879 |
|
880 |
/* Ray is modified by medium defined by cext and albedo in |
881 |
* photonParticipate() */ |
882 |
tracePhoton(&rayOut); |
883 |
|
884 |
return 0; |
885 |
} |
886 |
|
887 |
|
888 |
|
889 |
static int glassPhotonScatter (OBJREC *mat, RAY *rayIn) |
890 |
/* Generate new photon ray for glass material and recurse */ |
891 |
{ |
892 |
float albedo, xi, ptrans; |
893 |
COLOR mcolor, refl, trans; |
894 |
double pdot, cos2, d, r1e, r1m, rindex = 0.0; |
895 |
FVECT pnorm, pdir; |
896 |
int hastexture, i; |
897 |
RAY rayOut; |
898 |
|
899 |
/* check arguments */ |
900 |
if (mat -> oargs.nfargs == 3) |
901 |
rindex = RINDEX; |
902 |
else if (mat -> oargs.nfargs == 4) |
903 |
rindex = mat -> oargs.farg [3]; |
904 |
else objerror(mat, USER, "bad arguments"); |
905 |
|
906 |
copycolor(mcolor, mat -> oargs.farg); |
907 |
|
908 |
/* get modifiers */ |
909 |
raytexture(rayIn, mat -> omod); |
910 |
|
911 |
/* reorient if necessary */ |
912 |
if (rayIn -> rod < 0) |
913 |
flipsurface(rayIn); |
914 |
if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY)))) |
915 |
pdot = raynormal(pnorm, rayIn); |
916 |
else { |
917 |
VCOPY(pnorm, rayIn -> ron); |
918 |
pdot = rayIn -> rod; |
919 |
} |
920 |
|
921 |
/* Modify material color */ |
922 |
multcolor(mcolor, rayIn -> pcol); |
923 |
|
924 |
/* angular transmission */ |
925 |
cos2 = sqrt((1 - 1 / sqr(rindex)) + sqr(pdot / rindex)); |
926 |
setcolor(mcolor, pow(mcolor [0], 1 / cos2), pow(mcolor [1], 1 / cos2), |
927 |
pow(mcolor [2], 1 / cos2)); |
928 |
|
929 |
/* compute reflection */ |
930 |
r1e = (pdot - rindex * cos2) / (pdot + rindex * cos2); |
931 |
r1e *= r1e; |
932 |
r1m = (1 / pdot - rindex / cos2) / (1 / pdot + rindex / cos2); |
933 |
r1m *= r1m; |
934 |
|
935 |
for (i = 0; i < 3; i++) { |
936 |
double r1ed2, r1md2, d2; |
937 |
|
938 |
d = mcolor [i]; |
939 |
d2 = sqr(d); |
940 |
r1ed2 = sqr(r1e) * d2; |
941 |
r1md2 = sqr(r1m) * d2; |
942 |
|
943 |
/* compute transmittance */ |
944 |
trans [i] = 0.5 * d * |
945 |
(sqr(1 - r1e) / (1 - r1ed2) + sqr(1 - r1m) / (1 - r1md2)); |
946 |
|
947 |
/* compute reflectance */ |
948 |
refl [i] = 0.5 * (r1e * (1 + (1 - 2 * r1e) * d2) / (1 - r1ed2) + |
949 |
r1m * (1 + (1 - 2 * r1m) * d2) / (1 - r1md2)); |
950 |
} |
951 |
|
952 |
/* Set up probabilities */ |
953 |
ptrans = colorAvg(trans); |
954 |
albedo = colorAvg(refl) + ptrans; |
955 |
xi = pmapRandom(rouletteState); |
956 |
|
957 |
|
958 |
if (xi > albedo) |
959 |
/* Absorbed */ |
960 |
return 0; |
961 |
|
962 |
if (xi > (albedo -= ptrans)) { |
963 |
/* Transmitted */ |
964 |
|
965 |
if (hastexture) { |
966 |
/* perturb direction */ |
967 |
VSUM(pdir, rayIn -> rdir, rayIn -> pert, 2 * (1 - rindex)); |
968 |
|
969 |
if (normalize(pdir) == 0) { |
970 |
objerror(mat, WARNING, "bad perturbation"); |
971 |
VCOPY(pdir, rayIn -> rdir); |
972 |
} |
973 |
} |
974 |
else VCOPY(pdir, rayIn -> rdir); |
975 |
|
976 |
VCOPY(rayOut.rdir, pdir); |
977 |
photonRay(rayIn, &rayOut, PMAP_SPECTRANS, mcolor); |
978 |
} |
979 |
|
980 |
else { |
981 |
/* reflected ray */ |
982 |
VSUM(rayOut.rdir, rayIn -> rdir, pnorm, 2 * pdot); |
983 |
photonRay(rayIn, &rayOut, PMAP_SPECREFL, mcolor); |
984 |
} |
985 |
|
986 |
tracePhoton(&rayOut); |
987 |
return 0; |
988 |
} |
989 |
|
990 |
|
991 |
|
992 |
static int aliasPhotonScatter (OBJREC *mat, RAY *rayIn) |
993 |
/* Transfer photon scattering to alias target */ |
994 |
{ |
995 |
OBJECT aliasObj; |
996 |
OBJREC aliasRec, *aliasPtr; |
997 |
|
998 |
/* Straight replacement? */ |
999 |
if (!mat -> oargs.nsargs) { |
1000 |
/* Skip void modifier! */ |
1001 |
if (mat -> omod != OVOID) { |
1002 |
mat = objptr(mat -> omod); |
1003 |
photonScatter [mat -> otype] (mat, rayIn); |
1004 |
} |
1005 |
|
1006 |
return 0; |
1007 |
} |
1008 |
|
1009 |
/* Else replace alias */ |
1010 |
if (mat -> oargs.nsargs != 1) |
1011 |
objerror(mat, INTERNAL, "bad # string arguments"); |
1012 |
|
1013 |
aliasPtr = mat; |
1014 |
aliasObj = objndx(aliasPtr); |
1015 |
|
1016 |
/* Follow alias trail */ |
1017 |
do { |
1018 |
aliasObj = aliasPtr -> oargs.nsargs == 1 |
1019 |
? lastmod(aliasObj, aliasPtr -> oargs.sarg [0]) |
1020 |
: aliasPtr -> omod; |
1021 |
if (aliasObj < 0) |
1022 |
objerror(aliasPtr, USER, "bad reference"); |
1023 |
|
1024 |
aliasPtr = objptr(aliasObj); |
1025 |
} while (aliasPtr -> otype == MOD_ALIAS); |
1026 |
|
1027 |
/* Copy alias object */ |
1028 |
aliasRec = *aliasPtr; |
1029 |
|
1030 |
/* Substitute modifier */ |
1031 |
aliasRec.omod = mat -> omod; |
1032 |
|
1033 |
/* Replacement scattering routine */ |
1034 |
photonScatter [aliasRec.otype] (&aliasRec, rayIn); |
1035 |
|
1036 |
#if 0 |
1037 |
/* Avoid potential memory leak? */ |
1038 |
if (aliasRec.os != aliasPtr -> os) { |
1039 |
if (aliasObj -> os) |
1040 |
free_os(aliasObj); |
1041 |
aliasPtr -> os = aliasRec.os; |
1042 |
} |
1043 |
#endif |
1044 |
|
1045 |
return 0; |
1046 |
} |
1047 |
|
1048 |
|
1049 |
|
1050 |
static int clipPhotonScatter (OBJREC *mat, RAY *rayIn) |
1051 |
/* Generate new photon ray for antimatter material and recurse */ |
1052 |
{ |
1053 |
OBJECT obj = objndx(mat), mod, cset [MAXSET + 1], *modset; |
1054 |
int entering, inside = 0, i; |
1055 |
const RAY *rp; |
1056 |
RAY rayOut; |
1057 |
|
1058 |
if ((modset = (OBJECT*)mat -> os) == NULL) { |
1059 |
if (mat -> oargs.nsargs < 1 || mat -> oargs.nsargs > MAXSET) |
1060 |
objerror(mat, USER, "bad # arguments"); |
1061 |
|
1062 |
modset = (OBJECT*)malloc((mat -> oargs.nsargs + 1) * sizeof(OBJECT)); |
1063 |
|
1064 |
if (modset == NULL) |
1065 |
error(SYSTEM, "out of memory in clipPhotonScatter"); |
1066 |
modset [0] = 0; |
1067 |
|
1068 |
for (i = 0; i < mat -> oargs.nsargs; i++) { |
1069 |
if (!strcmp(mat -> oargs.sarg [i], VOIDID)) |
1070 |
continue; |
1071 |
|
1072 |
if ((mod = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) { |
1073 |
sprintf(errmsg, "unknown modifier \"%s\"", mat->oargs.sarg[i]); |
1074 |
objerror(mat, WARNING, errmsg); |
1075 |
continue; |
1076 |
} |
1077 |
|
1078 |
if (inset(modset, mod)) { |
1079 |
objerror(mat, WARNING, "duplicate modifier"); |
1080 |
continue; |
1081 |
} |
1082 |
|
1083 |
insertelem(modset, mod); |
1084 |
} |
1085 |
|
1086 |
mat -> os = (char*)modset; |
1087 |
} |
1088 |
|
1089 |
if (rayIn -> clipset != NULL) |
1090 |
setcopy(cset, rayIn -> clipset); |
1091 |
else cset [0] = 0; |
1092 |
|
1093 |
entering = rayIn -> rod > 0; |
1094 |
|
1095 |
/* Store photon incident from front if material defined as sensor */ |
1096 |
if (entering && inset(photonSensorSet, obj)) |
1097 |
addPhotons(rayIn); |
1098 |
|
1099 |
for (i = modset [0]; i > 0; i--) { |
1100 |
if (entering) { |
1101 |
if (!inset(cset, modset [i])) { |
1102 |
if (cset [0] >= MAXSET) |
1103 |
error(INTERNAL, "set overflow in clipPhotonScatter"); |
1104 |
insertelem(cset, modset [i]); |
1105 |
} |
1106 |
} |
1107 |
else if (inset(cset, modset [i])) |
1108 |
deletelem(cset, modset [i]); |
1109 |
} |
1110 |
|
1111 |
rayIn -> newcset = cset; |
1112 |
|
1113 |
if (strcmp(mat -> oargs.sarg [0], VOIDID)) { |
1114 |
for (rp = rayIn; rp -> parent != NULL; rp = rp -> parent) { |
1115 |
if ( !(rp -> rtype & RAYREFL) && rp->parent->ro != NULL && |
1116 |
inset(modset, rp -> parent -> ro -> omod)) { |
1117 |
|
1118 |
if (rp -> parent -> rod > 0) |
1119 |
inside++; |
1120 |
else inside--; |
1121 |
} |
1122 |
} |
1123 |
|
1124 |
if (inside > 0) { |
1125 |
flipsurface(rayIn); |
1126 |
mat = objptr(lastmod(obj, mat -> oargs.sarg [0])); |
1127 |
photonScatter [mat -> otype] (mat, rayIn); |
1128 |
return 0; |
1129 |
} |
1130 |
} |
1131 |
|
1132 |
/* Else transfer ray */ |
1133 |
photonRay(rayIn, &rayOut, PMAP_XFER, NULL); |
1134 |
tracePhoton(&rayOut); |
1135 |
|
1136 |
return 0; |
1137 |
} |
1138 |
|
1139 |
|
1140 |
|
1141 |
static int mirrorPhotonScatter (OBJREC *mat, RAY *rayIn) |
1142 |
/* Generate new photon ray for mirror material and recurse */ |
1143 |
{ |
1144 |
RAY rayOut; |
1145 |
int rpure = 1, i; |
1146 |
FVECT pnorm; |
1147 |
double pdot; |
1148 |
float albedo; |
1149 |
COLOR mcolor; |
1150 |
|
1151 |
/* check arguments */ |
1152 |
if (mat -> oargs.nfargs != 3 || mat -> oargs.nsargs > 1) |
1153 |
objerror(mat, USER, "bad number of arguments"); |
1154 |
|
1155 |
/* back is black */ |
1156 |
if (rayIn -> rod < 0) |
1157 |
return 0; |
1158 |
|
1159 |
/* get modifiers */ |
1160 |
raytexture(rayIn, mat -> omod); |
1161 |
|
1162 |
/* assign material color */ |
1163 |
copycolor(mcolor, mat -> oargs.farg); |
1164 |
multcolor(mcolor, rayIn -> pcol); |
1165 |
|
1166 |
/* Set up probabilities */ |
1167 |
albedo = colorAvg(mcolor); |
1168 |
|
1169 |
if (pmapRandom(rouletteState) > albedo) |
1170 |
/* Absorbed */ |
1171 |
return 0; |
1172 |
|
1173 |
/* compute reflected ray */ |
1174 |
photonRay(rayIn, &rayOut, PMAP_SPECREFL, mcolor); |
1175 |
|
1176 |
if (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY)) { |
1177 |
/* use textures */ |
1178 |
pdot = raynormal(pnorm, rayIn); |
1179 |
|
1180 |
for (i = 0; i < 3; i++) |
1181 |
rayOut.rdir [i] = rayIn -> rdir [i] + 2 * pdot * pnorm [i]; |
1182 |
|
1183 |
rpure = 0; |
1184 |
} |
1185 |
|
1186 |
/* Check for penetration */ |
1187 |
if (rpure || DOT(rayOut.rdir, rayIn -> ron) <= FTINY) |
1188 |
for (i = 0; i < 3; i++) |
1189 |
rayOut.rdir [i] = rayIn -> rdir [i] + |
1190 |
2 * rayIn -> rod * rayIn -> ron [i]; |
1191 |
|
1192 |
tracePhoton(&rayOut); |
1193 |
return 0; |
1194 |
} |
1195 |
|
1196 |
|
1197 |
|
1198 |
static int mistPhotonScatter (OBJREC *mat, RAY *rayIn) |
1199 |
/* Generate new photon ray within mist and recurse */ |
1200 |
{ |
1201 |
COLOR mext; |
1202 |
RREAL re, ge, be; |
1203 |
RAY rayOut; |
1204 |
|
1205 |
/* check arguments */ |
1206 |
if (mat -> oargs.nfargs > 7) |
1207 |
objerror(mat, USER, "bad arguments"); |
1208 |
|
1209 |
if (mat -> oargs.nfargs > 2) { |
1210 |
/* compute extinction */ |
1211 |
copycolor(mext, mat -> oargs.farg); |
1212 |
/* get modifiers */ |
1213 |
raytexture(rayIn, mat -> omod); |
1214 |
multcolor(mext, rayIn -> pcol); |
1215 |
} |
1216 |
else setcolor(mext, 0, 0, 0); |
1217 |
|
1218 |
photonRay(rayIn, &rayOut, PMAP_XFER, NULL); |
1219 |
|
1220 |
if (rayIn -> rod > 0) { |
1221 |
/* entering ray */ |
1222 |
addcolor(rayOut.cext, mext); |
1223 |
|
1224 |
if (mat -> oargs.nfargs > 5) |
1225 |
copycolor(rayOut.albedo, mat -> oargs.farg + 3); |
1226 |
if (mat -> oargs.nfargs > 6) |
1227 |
rayOut.gecc = mat -> oargs.farg [6]; |
1228 |
} |
1229 |
|
1230 |
else { |
1231 |
/* leaving ray */ |
1232 |
re = max(rayIn -> cext [0] - mext [0], cextinction [0]); |
1233 |
ge = max(rayIn -> cext [1] - mext [1], cextinction [1]); |
1234 |
be = max(rayIn -> cext [2] - mext [2], cextinction [2]); |
1235 |
setcolor(rayOut.cext, re, ge, be); |
1236 |
|
1237 |
if (mat -> oargs.nfargs > 5) |
1238 |
copycolor(rayOut.albedo, salbedo); |
1239 |
if (mat -> oargs.nfargs > 6) |
1240 |
rayOut.gecc = seccg; |
1241 |
} |
1242 |
|
1243 |
tracePhoton(&rayOut); |
1244 |
|
1245 |
return 0; |
1246 |
} |
1247 |
|
1248 |
|
1249 |
|
1250 |
static int mx_dataPhotonScatter (OBJREC *mat, RAY *rayIn) |
1251 |
/* Pass photon on to materials selected by mixture data */ |
1252 |
{ |
1253 |
OBJECT obj; |
1254 |
double coef, pt [MAXDIM]; |
1255 |
DATARRAY *dp; |
1256 |
OBJECT mod [2]; |
1257 |
MFUNC *mf; |
1258 |
int i; |
1259 |
|
1260 |
if (mat -> oargs.nsargs < 6) |
1261 |
objerror(mat, USER, "bad # arguments"); |
1262 |
|
1263 |
obj = objndx(mat); |
1264 |
|
1265 |
for (i = 0; i < 2; i++) |
1266 |
if (!strcmp(mat -> oargs.sarg [i], VOIDID)) |
1267 |
mod [i] = OVOID; |
1268 |
else if ((mod [i] = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) { |
1269 |
sprintf(errmsg, "undefined modifier \"%s\"", mat->oargs.sarg[i]); |
1270 |
objerror(mat, USER, errmsg); |
1271 |
} |
1272 |
|
1273 |
dp = getdata(mat -> oargs.sarg [3]); |
1274 |
i = (1 << dp -> nd) - 1; |
1275 |
mf = getfunc(mat, 4, i << 5, 0); |
1276 |
setfunc(mat, rayIn); |
1277 |
errno = 0; |
1278 |
|
1279 |
for (i = 0; i < dp -> nd; i++) { |
1280 |
pt [i] = evalue(mf -> ep [i]); |
1281 |
|
1282 |
if (errno) { |
1283 |
objerror(mat, WARNING, "compute error"); |
1284 |
return 0; |
1285 |
} |
1286 |
} |
1287 |
|
1288 |
coef = datavalue(dp, pt); |
1289 |
errno = 0; |
1290 |
coef = funvalue(mat -> oargs.sarg [2], 1, &coef); |
1291 |
|
1292 |
if (errno) |
1293 |
objerror(mat, WARNING, "compute error"); |
1294 |
else { |
1295 |
OBJECT mxMod = mod [pmapRandom(rouletteState) < coef ? 0 : 1]; |
1296 |
|
1297 |
if (mxMod != OVOID) { |
1298 |
mat = objptr(mxMod); |
1299 |
photonScatter [mat -> otype] (mat, rayIn); |
1300 |
} |
1301 |
else { |
1302 |
/* Transfer ray if no modifier */ |
1303 |
RAY rayOut; |
1304 |
|
1305 |
photonRay(rayIn, &rayOut, PMAP_XFER, NULL); |
1306 |
tracePhoton(&rayOut); |
1307 |
} |
1308 |
} |
1309 |
|
1310 |
return 0; |
1311 |
} |
1312 |
|
1313 |
|
1314 |
|
1315 |
static int mx_pdataPhotonScatter (OBJREC *mat, RAY *rayIn) |
1316 |
/* Pass photon on to materials selected by mixture picture */ |
1317 |
{ |
1318 |
OBJECT obj; |
1319 |
double col [3], coef, pt [MAXDIM]; |
1320 |
DATARRAY *dp; |
1321 |
OBJECT mod [2]; |
1322 |
MFUNC *mf; |
1323 |
int i; |
1324 |
|
1325 |
if (mat -> oargs.nsargs < 7) |
1326 |
objerror(mat, USER, "bad # arguments"); |
1327 |
|
1328 |
obj = objndx(mat); |
1329 |
|
1330 |
for (i = 0; i < 2; i++) |
1331 |
if (!strcmp(mat -> oargs.sarg [i], VOIDID)) |
1332 |
mod [i] = OVOID; |
1333 |
else if ((mod [i] = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) { |
1334 |
sprintf(errmsg, "undefined modifier \"%s\"", mat->oargs.sarg[i]); |
1335 |
objerror(mat, USER, errmsg); |
1336 |
} |
1337 |
|
1338 |
dp = getpict(mat -> oargs.sarg [3]); |
1339 |
mf = getfunc(mat, 4, 0x3 << 5, 0); |
1340 |
setfunc(mat, rayIn); |
1341 |
errno = 0; |
1342 |
pt [1] = evalue(mf -> ep [0]); |
1343 |
pt [0] = evalue(mf -> ep [1]); |
1344 |
|
1345 |
if (errno) { |
1346 |
objerror(mat, WARNING, "compute error"); |
1347 |
return 0; |
1348 |
} |
1349 |
|
1350 |
for (i = 0; i < 3; i++) |
1351 |
col [i] = datavalue(dp + i, pt); |
1352 |
|
1353 |
errno = 0; |
1354 |
coef = funvalue(mat -> oargs.sarg [2], 3, col); |
1355 |
|
1356 |
if (errno) |
1357 |
objerror(mat, WARNING, "compute error"); |
1358 |
else { |
1359 |
OBJECT mxMod = mod [pmapRandom(rouletteState) < coef ? 0 : 1]; |
1360 |
|
1361 |
if (mxMod != OVOID) { |
1362 |
mat = objptr(mxMod); |
1363 |
photonScatter [mat -> otype] (mat, rayIn); |
1364 |
} |
1365 |
else { |
1366 |
/* Transfer ray if no modifier */ |
1367 |
RAY rayOut; |
1368 |
|
1369 |
photonRay(rayIn, &rayOut, PMAP_XFER, NULL); |
1370 |
tracePhoton(&rayOut); |
1371 |
} |
1372 |
} |
1373 |
|
1374 |
return 0; |
1375 |
} |
1376 |
|
1377 |
|
1378 |
|
1379 |
static int mx_funcPhotonScatter (OBJREC *mat, RAY *rayIn) |
1380 |
/* Pass photon on to materials selected by mixture function */ |
1381 |
{ |
1382 |
OBJECT obj, mod [2]; |
1383 |
int i; |
1384 |
double coef; |
1385 |
MFUNC *mf; |
1386 |
|
1387 |
if (mat -> oargs.nsargs < 4) |
1388 |
objerror(mat, USER, "bad # arguments"); |
1389 |
|
1390 |
obj = objndx(mat); |
1391 |
|
1392 |
for (i = 0; i < 2; i++) |
1393 |
if (!strcmp(mat -> oargs.sarg [i], VOIDID)) |
1394 |
mod [i] = OVOID; |
1395 |
else if ((mod [i] = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) { |
1396 |
sprintf(errmsg, "undefined modifier \"%s\"", mat->oargs.sarg[i]); |
1397 |
objerror(mat, USER, errmsg); |
1398 |
} |
1399 |
|
1400 |
mf = getfunc(mat, 3, 0x4, 0); |
1401 |
setfunc(mat, rayIn); |
1402 |
errno = 0; |
1403 |
|
1404 |
/* bound coefficient */ |
1405 |
coef = min(1, max(0, evalue(mf -> ep [0]))); |
1406 |
|
1407 |
if (errno) |
1408 |
objerror(mat, WARNING, "compute error"); |
1409 |
else { |
1410 |
OBJECT mxMod = mod [pmapRandom(rouletteState) < coef ? 0 : 1]; |
1411 |
|
1412 |
if (mxMod != OVOID) { |
1413 |
mat = objptr(mxMod); |
1414 |
photonScatter [mat -> otype] (mat, rayIn); |
1415 |
} |
1416 |
else { |
1417 |
/* Transfer ray if no modifier */ |
1418 |
RAY rayOut; |
1419 |
|
1420 |
photonRay(rayIn, &rayOut, PMAP_XFER, NULL); |
1421 |
tracePhoton(&rayOut); |
1422 |
} |
1423 |
} |
1424 |
|
1425 |
return 0; |
1426 |
} |
1427 |
|
1428 |
|
1429 |
|
1430 |
static int pattexPhotonScatter (OBJREC *mat, RAY *rayIn) |
1431 |
/* Generate new photon ray for pattern or texture modifier and recurse. |
1432 |
This code is brought to you by Henkel! :^) */ |
1433 |
{ |
1434 |
RAY rayOut; |
1435 |
|
1436 |
/* Get pattern */ |
1437 |
ofun [mat -> otype].funp(mat, rayIn); |
1438 |
if (mat -> omod != OVOID) { |
1439 |
/* Scatter using modifier (if any) */ |
1440 |
mat = objptr(mat -> omod); |
1441 |
photonScatter [mat -> otype] (mat, rayIn); |
1442 |
} |
1443 |
else { |
1444 |
/* Transfer ray if no modifier */ |
1445 |
photonRay(rayIn, &rayOut, PMAP_XFER, NULL); |
1446 |
tracePhoton(&rayOut); |
1447 |
} |
1448 |
|
1449 |
return 0; |
1450 |
} |
1451 |
|
1452 |
|
1453 |
|
1454 |
static int setbrdfunc(BRDFDAT *bd) |
1455 |
/* Set up brdf function and variables; ripped off from m_brdf.c */ |
1456 |
{ |
1457 |
FVECT v; |
1458 |
|
1459 |
if (setfunc(bd -> mp, bd -> pr) == 0) |
1460 |
return 0; |
1461 |
|
1462 |
/* (Re)Assign func variables */ |
1463 |
multv3(v, bd -> pnorm, funcxf.xfm); |
1464 |
varset("NxP", '=', v [0] / funcxf.sca); |
1465 |
varset("NyP", '=', v [1] / funcxf.sca); |
1466 |
varset("NzP", '=', v [2] / funcxf.sca); |
1467 |
varset("RdotP", '=', |
1468 |
bd -> pdot <= -1. ? -1. : bd -> pdot >= 1. ? 1. : bd -> pdot); |
1469 |
varset("CrP", '=', colval(bd -> mcolor, RED)); |
1470 |
varset("CgP", '=', colval(bd -> mcolor, GRN)); |
1471 |
varset("CbP", '=', colval(bd -> mcolor, BLU)); |
1472 |
|
1473 |
return 1; |
1474 |
} |
1475 |
|
1476 |
|
1477 |
|
1478 |
static int brtdFuncPhotonScatter (OBJREC *mat, RAY *rayIn) |
1479 |
/* Generate new photon ray for BRTDfunc material and recurse */ |
1480 |
{ |
1481 |
int hitfront = 1, hastexture, i; |
1482 |
BRDFDAT nd; |
1483 |
RAY rayOut; |
1484 |
COLOR rspecCol, tspecCol; |
1485 |
double prDiff, ptDiff, prSpec, ptSpec, albedo, xi; |
1486 |
MFUNC *mf; |
1487 |
FVECT bnorm; |
1488 |
|
1489 |
if (mat -> oargs.nsargs < 10 || mat -> oargs.nfargs < 9) |
1490 |
objerror(mat, USER, "bad # arguments"); |
1491 |
nd.mp = mat; |
1492 |
nd.pr = rayIn; |
1493 |
/* Dummies */ |
1494 |
nd.rspec = nd.tspec = 1.0; |
1495 |
nd.trans = 0.5; |
1496 |
|
1497 |
/* Diffuse reflectance */ |
1498 |
if (rayIn -> rod > 0.0) |
1499 |
setcolor(nd.rdiff, mat -> oargs.farg[0], mat -> oargs.farg [1], |
1500 |
mat -> oargs.farg [2]); |
1501 |
else |
1502 |
setcolor(nd.rdiff, mat-> oargs.farg [3], mat -> oargs.farg [4], |
1503 |
mat -> oargs.farg [5]); |
1504 |
/* Diffuse transmittance */ |
1505 |
setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7], |
1506 |
mat -> oargs.farg [8]); |
1507 |
|
1508 |
/* Get modifiers */ |
1509 |
raytexture(rayIn, mat -> omod); |
1510 |
hastexture = (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY)); |
1511 |
if (hastexture) { |
1512 |
/* Perturb normal */ |
1513 |
nd.pdot = raynormal(nd.pnorm, rayIn); |
1514 |
} |
1515 |
else { |
1516 |
VCOPY(nd.pnorm, rayIn -> ron); |
1517 |
nd.pdot = rayIn -> rod; |
1518 |
} |
1519 |
|
1520 |
if (rayIn -> rod < 0.0) { |
1521 |
/* Orient perturbed values */ |
1522 |
nd.pdot = -nd.pdot; |
1523 |
for (i = 0; i < 3; i++) { |
1524 |
nd.pnorm [i] = -nd.pnorm [i]; |
1525 |
rayIn -> pert [i] = -rayIn -> pert [i]; |
1526 |
} |
1527 |
|
1528 |
hitfront = 0; |
1529 |
} |
1530 |
|
1531 |
/* Get pattern color, modify diffuse values */ |
1532 |
copycolor(nd.mcolor, rayIn -> pcol); |
1533 |
multcolor(nd.rdiff, nd.mcolor); |
1534 |
multcolor(nd.tdiff, nd.mcolor); |
1535 |
|
1536 |
/* Load cal file, evaluate spec refl/trans vars */ |
1537 |
nd.dp = NULL; |
1538 |
mf = getfunc(mat, 9, 0x3f, 0); |
1539 |
setbrdfunc(&nd); |
1540 |
errno = 0; |
1541 |
setcolor(rspecCol, |
1542 |
evalue(mf->ep[0]), evalue(mf->ep[1]), evalue(mf->ep[2])); |
1543 |
setcolor(tspecCol, |
1544 |
evalue(mf->ep[3]), evalue(mf->ep[4]), evalue(mf->ep[5])); |
1545 |
if (errno == EDOM || errno == ERANGE) |
1546 |
objerror(mat, WARNING, "compute error"); |
1547 |
else { |
1548 |
/* Set up probabilities */ |
1549 |
prDiff = colorAvg(nd.rdiff); |
1550 |
ptDiff = colorAvg(nd.tdiff); |
1551 |
prSpec = colorAvg(rspecCol); |
1552 |
ptSpec = colorAvg(tspecCol); |
1553 |
albedo = prDiff + ptDiff + prSpec + ptSpec; |
1554 |
} |
1555 |
|
1556 |
/* Insert direct and indirect photon hits if diffuse component */ |
1557 |
if (prDiff > FTINY || ptDiff > FTINY) |
1558 |
addPhotons(rayIn); |
1559 |
|
1560 |
/* Stochastically sample absorption or scattering events */ |
1561 |
if ((xi = pmapRandom(rouletteState)) > albedo) |
1562 |
/* Absorbed */ |
1563 |
return 0; |
1564 |
|
1565 |
if (xi > (albedo -= prSpec)) { |
1566 |
/* Specular reflection */ |
1567 |
photonRay(rayIn, &rayOut, PMAP_SPECREFL, rspecCol); |
1568 |
VSUM(rayOut.rdir, rayIn -> rdir, nd.pnorm, 2 * nd.pdot); |
1569 |
checknorm(rayOut.rdir); |
1570 |
} |
1571 |
else if (xi > (albedo -= ptSpec)) { |
1572 |
/* Specular transmission */ |
1573 |
photonRay(rayIn, &rayOut, PMAP_SPECTRANS, tspecCol); |
1574 |
if (hastexture) { |
1575 |
/* Perturb direction */ |
1576 |
VSUB(rayOut.rdir, rayIn -> rdir, rayIn -> pert); |
1577 |
if (normalize(rayOut.rdir) == 0.0) { |
1578 |
objerror(mat, WARNING, "illegal perturbation"); |
1579 |
VCOPY(rayOut.rdir, rayIn -> rdir); |
1580 |
} |
1581 |
else VCOPY(rayOut.rdir, rayIn -> rdir); |
1582 |
} |
1583 |
} |
1584 |
else if (xi > (albedo -= prDiff)) { |
1585 |
/* Diffuse reflection */ |
1586 |
if (!hitfront) |
1587 |
flipsurface(rayIn); |
1588 |
photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.mcolor); |
1589 |
diffPhotonScatter(nd.pnorm, &rayOut); |
1590 |
} |
1591 |
else { |
1592 |
/* Diffuse transmission */ |
1593 |
if (hitfront) |
1594 |
flipsurface(rayIn); |
1595 |
photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.mcolor); |
1596 |
bnorm [0] = -nd.pnorm [0]; |
1597 |
bnorm [1] = -nd.pnorm [1]; |
1598 |
bnorm [2] = -nd.pnorm [2]; |
1599 |
diffPhotonScatter(bnorm, &rayOut); |
1600 |
} |
1601 |
|
1602 |
return 0; |
1603 |
} |
1604 |
|
1605 |
|
1606 |
|
1607 |
#if 0 |
1608 |
int |
1609 |
m_brdf2( /* color a ray that hit a BRDF material */ |
1610 |
OBJREC *m, |
1611 |
RAY *r |
1612 |
) |
1613 |
{ |
1614 |
BRDFDAT nd; |
1615 |
COLOR ctmp; |
1616 |
FVECT vtmp; |
1617 |
double dtmp; |
1618 |
/* always a shadow */ |
1619 |
if (r->crtype & SHADOW) |
1620 |
return(1); |
1621 |
/* check arguments */ |
1622 |
if ((m->oargs.nsargs < (hasdata(m->otype)?4:2)) | (m->oargs.nfargs < |
1623 |
((m->otype==MAT_TFUNC)|(m->otype==MAT_TDATA)?6:4))) |
1624 |
objerror(m, USER, "bad # arguments"); |
1625 |
/* check for back side */ |
1626 |
if (r->rod < 0.0) { |
1627 |
if (!backvis) { |
1628 |
raytrans(r); |
1629 |
return(1); |
1630 |
} |
1631 |
raytexture(r, m->omod); |
1632 |
flipsurface(r); /* reorient if backvis */ |
1633 |
} else |
1634 |
raytexture(r, m->omod); |
1635 |
|
1636 |
nd.mp = m; |
1637 |
nd.pr = r; |
1638 |
/* get material color */ |
1639 |
setcolor(nd.mcolor, m->oargs.farg[0], |
1640 |
m->oargs.farg[1], |
1641 |
m->oargs.farg[2]); |
1642 |
/* get specular component */ |
1643 |
nd.rspec = m->oargs.farg[3]; |
1644 |
/* compute transmittance */ |
1645 |
if ((m->otype == MAT_TFUNC) | (m->otype == MAT_TDATA)) { |
1646 |
nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec); |
1647 |
nd.tspec = nd.trans * m->oargs.farg[5]; |
1648 |
dtmp = nd.trans - nd.tspec; |
1649 |
setcolor(nd.tdiff, dtmp, dtmp, dtmp); |
1650 |
} else { |
1651 |
nd.tspec = nd.trans = 0.0; |
1652 |
setcolor(nd.tdiff, 0.0, 0.0, 0.0); |
1653 |
} |
1654 |
/* compute reflectance */ |
1655 |
dtmp = 1.0 - nd.trans - nd.rspec; |
1656 |
setcolor(nd.rdiff, dtmp, dtmp, dtmp); |
1657 |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
1658 |
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
1659 |
multcolor(nd.rdiff, nd.mcolor); |
1660 |
multcolor(nd.tdiff, nd.mcolor); |
1661 |
/* load auxiliary files */ |
1662 |
if (hasdata(m->otype)) { |
1663 |
nd.dp = getdata(m->oargs.sarg[1]); |
1664 |
getfunc(m, 2, 0, 0); |
1665 |
} else { |
1666 |
nd.dp = NULL; |
1667 |
getfunc(m, 1, 0, 0); |
1668 |
} |
1669 |
/* compute ambient */ |
1670 |
if (nd.trans < 1.0-FTINY) { |
1671 |
copycolor(ctmp, nd.mcolor); /* modified by material color */ |
1672 |
scalecolor(ctmp, 1.0-nd.trans); |
1673 |
multambient(ctmp, r, nd.pnorm); |
1674 |
addcolor(r->rcol, ctmp); /* add to returned color */ |
1675 |
} |
1676 |
if (nd.trans > FTINY) { /* from other side */ |
1677 |
flipsurface(r); |
1678 |
vtmp[0] = -nd.pnorm[0]; |
1679 |
vtmp[1] = -nd.pnorm[1]; |
1680 |
vtmp[2] = -nd.pnorm[2]; |
1681 |
copycolor(ctmp, nd.mcolor); |
1682 |
scalecolor(ctmp, nd.trans); |
1683 |
multambient(ctmp, r, vtmp); |
1684 |
addcolor(r->rcol, ctmp); |
1685 |
flipsurface(r); |
1686 |
} |
1687 |
/* add direct component */ |
1688 |
direct(r, dirbrdf, &nd); |
1689 |
|
1690 |
return(1); |
1691 |
} |
1692 |
#endif |
1693 |
|
1694 |
|
1695 |
|
1696 |
/* |
1697 |
================================================================== |
1698 |
The following code is |
1699 |
(c) Lucerne University of Applied Sciences and Arts, |
1700 |
supported by the Swiss National Science Foundation (SNSF, #147053) |
1701 |
================================================================== |
1702 |
*/ |
1703 |
|
1704 |
static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn) |
1705 |
/* Generate new photon ray for BSDF modifier and recurse. */ |
1706 |
{ |
1707 |
int hasthick = (mat->otype == MAT_BSDF); |
1708 |
int hitFront; |
1709 |
SDError err; |
1710 |
SDValue bsdfVal; |
1711 |
FVECT upvec; |
1712 |
MFUNC *mf; |
1713 |
BSDFDAT nd; |
1714 |
RAY rayOut; |
1715 |
COLOR bsdfRGB; |
1716 |
int transmitted; |
1717 |
double prDiff, ptDiff, prDiffSD, ptDiffSD, prSpecSD, ptSpecSD, |
1718 |
albedo, xi; |
1719 |
const double patAlb = bright(rayIn -> pcol); |
1720 |
|
1721 |
/* Following code adapted from m_bsdf() */ |
1722 |
/* Check arguments */ |
1723 |
if (mat -> oargs.nsargs < hasthick+5 || mat -> oargs.nfargs > 9 || |
1724 |
mat -> oargs.nfargs % 3) |
1725 |
objerror(mat, USER, "bad # arguments"); |
1726 |
|
1727 |
hitFront = (rayIn -> rod > 0); |
1728 |
|
1729 |
/* Load cal file */ |
1730 |
mf = hasthick ? getfunc(mat, 5, 0x1d, 1) : getfunc(mat, 4, 0xe, 1); |
1731 |
|
1732 |
/* Get thickness */ |
1733 |
nd.thick = 0; |
1734 |
if (hasthick) { |
1735 |
nd.thick = evalue(mf -> ep [0]); |
1736 |
if ((-FTINY <= nd.thick) & (nd.thick <= FTINY)) |
1737 |
nd.thick = .0; |
1738 |
} |
1739 |
|
1740 |
/* Get BSDF data */ |
1741 |
nd.sd = loadBSDF(mat -> oargs.sarg [hasthick]); |
1742 |
|
1743 |
/* Extra diffuse reflectance from material def */ |
1744 |
if (hitFront) { |
1745 |
if (mat -> oargs.nfargs < 3) |
1746 |
setcolor(nd.rdiff, .0, .0, .0); |
1747 |
else setcolor(nd.rdiff, mat -> oargs.farg [0], mat -> oargs.farg [1], |
1748 |
mat -> oargs.farg [2]); |
1749 |
} |
1750 |
else if (mat -> oargs.nfargs < 6) { |
1751 |
/* Check for absorbing backside */ |
1752 |
if (!backvis && !nd.sd -> rb && !nd.sd -> tf) { |
1753 |
SDfreeCache(nd.sd); |
1754 |
return 0; |
1755 |
} |
1756 |
|
1757 |
setcolor(nd.rdiff, .0, .0, .0); |
1758 |
} |
1759 |
else setcolor(nd.rdiff, mat -> oargs.farg [3], mat -> oargs.farg [4], |
1760 |
mat -> oargs.farg [5]); |
1761 |
|
1762 |
/* Extra diffuse transmittance from material def */ |
1763 |
if (mat -> oargs.nfargs < 9) |
1764 |
setcolor(nd.tdiff, .0, .0, .0); |
1765 |
else setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7], |
1766 |
mat -> oargs.farg [8]); |
1767 |
|
1768 |
nd.mp = mat; |
1769 |
nd.pr = rayIn; |
1770 |
|
1771 |
/* Get modifiers */ |
1772 |
raytexture(rayIn, mat -> omod); |
1773 |
|
1774 |
/* Modify diffuse values */ |
1775 |
multcolor(nd.rdiff, rayIn -> pcol); |
1776 |
multcolor(nd.tdiff, rayIn -> pcol); |
1777 |
|
1778 |
/* Get up vector & xform to world coords */ |
1779 |
upvec [0] = evalue(mf -> ep [hasthick+0]); |
1780 |
upvec [1] = evalue(mf -> ep [hasthick+1]); |
1781 |
upvec [2] = evalue(mf -> ep [hasthick+2]); |
1782 |
|
1783 |
if (mf -> fxp != &unitxf) { |
1784 |
multv3(upvec, upvec, mf -> fxp -> xfm); |
1785 |
nd.thick *= mf -> fxp -> sca; |
1786 |
} |
1787 |
|
1788 |
if (rayIn -> rox) { |
1789 |
multv3(upvec, upvec, rayIn -> rox -> f.xfm); |
1790 |
nd.thick *= rayIn -> rox -> f.sca; |
1791 |
} |
1792 |
|
1793 |
/* Perturb normal */ |
1794 |
raynormal(nd.pnorm, rayIn); |
1795 |
|
1796 |
/* Xform incident dir to local BSDF coords */ |
1797 |
err = SDcompXform(nd.toloc, nd.pnorm, upvec); |
1798 |
|
1799 |
if (!err) { |
1800 |
nd.vray [0] = -rayIn -> rdir [0]; |
1801 |
nd.vray [1] = -rayIn -> rdir [1]; |
1802 |
nd.vray [2] = -rayIn -> rdir [2]; |
1803 |
err = SDmapDir(nd.vray, nd.toloc, nd.vray); |
1804 |
} |
1805 |
|
1806 |
if (!err) |
1807 |
err = SDinvXform(nd.fromloc, nd.toloc); |
1808 |
|
1809 |
if (err) { |
1810 |
objerror(mat, WARNING, "Illegal orientation vector"); |
1811 |
return 0; |
1812 |
} |
1813 |
|
1814 |
/* Determine BSDF resolution */ |
1815 |
err = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, |
1816 |
SDqueryMin + SDqueryMax, nd.sd); |
1817 |
|
1818 |
if (err) |
1819 |
objerror(mat, USER, transSDError(err)); |
1820 |
|
1821 |
nd.sr_vpsa [0] = sqrt(nd.sr_vpsa [0]); |
1822 |
nd.sr_vpsa [1] = sqrt(nd.sr_vpsa [1]); |
1823 |
|
1824 |
/* Orient perturbed normal towards incident side */ |
1825 |
if (!hitFront) { |
1826 |
nd.pnorm [0] = -nd.pnorm [0]; |
1827 |
nd.pnorm [1] = -nd.pnorm [1]; |
1828 |
nd.pnorm [2] = -nd.pnorm [2]; |
1829 |
} |
1830 |
|
1831 |
/* Get scatter probabilities (weighted by pattern except for spec refl) |
1832 |
* prDiff, ptDiff: extra diffuse component in material def |
1833 |
* prDiffSD, ptDiffSD: diffuse (constant) component in SDF |
1834 |
* prSpecSD, ptSpecSD: non-diffuse ("specular") component in SDF |
1835 |
* albedo: sum of above, inverse absorption probability */ |
1836 |
prDiff = colorAvg(nd.rdiff); |
1837 |
ptDiff = colorAvg(nd.tdiff); |
1838 |
prDiffSD = patAlb * SDdirectHemi(nd.vray, SDsampDf | SDsampR, nd.sd); |
1839 |
ptDiffSD = patAlb * SDdirectHemi(nd.vray, SDsampDf | SDsampT, nd.sd); |
1840 |
prSpecSD = SDdirectHemi(nd.vray, SDsampSp | SDsampR, nd.sd); |
1841 |
ptSpecSD = patAlb * SDdirectHemi(nd.vray, SDsampSp | SDsampT, nd.sd); |
1842 |
albedo = prDiff + ptDiff + prDiffSD + ptDiffSD + prSpecSD + ptSpecSD; |
1843 |
|
1844 |
/* |
1845 |
if (albedo > 1) |
1846 |
objerror(mat, WARNING, "Invalid albedo"); |
1847 |
*/ |
1848 |
|
1849 |
/* Insert direct and indirect photon hits if diffuse component */ |
1850 |
if (prDiff + ptDiff + prDiffSD + ptDiffSD > FTINY) |
1851 |
addPhotons(rayIn); |
1852 |
|
1853 |
xi = pmapRandom(rouletteState); |
1854 |
|
1855 |
if (xi > albedo) |
1856 |
/* Absorbtion */ |
1857 |
return 0; |
1858 |
|
1859 |
transmitted = 0; |
1860 |
|
1861 |
if ((xi -= prDiff) <= 0) { |
1862 |
/* Diffuse reflection (extra component in material def) */ |
1863 |
photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.rdiff); |
1864 |
diffPhotonScatter(nd.pnorm, &rayOut); |
1865 |
} |
1866 |
|
1867 |
else if ((xi -= ptDiff) <= 0) { |
1868 |
/* Diffuse transmission (extra component in material def) */ |
1869 |
photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.tdiff); |
1870 |
diffPhotonScatter(nd.pnorm, &rayOut); |
1871 |
transmitted = 1; |
1872 |
} |
1873 |
|
1874 |
else { /* Sample SDF */ |
1875 |
if ((xi -= prDiffSD) <= 0) { |
1876 |
/* Diffuse SDF reflection (constant component) */ |
1877 |
if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState), |
1878 |
SDsampDf | SDsampR, nd.sd))) |
1879 |
objerror(mat, USER, transSDError(err)); |
1880 |
|
1881 |
/* Apply pattern to spectral component */ |
1882 |
ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB); |
1883 |
multcolor(bsdfRGB, rayIn -> pcol); |
1884 |
photonRay(rayIn, &rayOut, PMAP_DIFFREFL, bsdfRGB); |
1885 |
} |
1886 |
|
1887 |
else if ((xi -= ptDiffSD) <= 0) { |
1888 |
/* Diffuse SDF transmission (constant component) */ |
1889 |
if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState), |
1890 |
SDsampDf | SDsampT, nd.sd))) |
1891 |
objerror(mat, USER, transSDError(err)); |
1892 |
|
1893 |
/* Apply pattern to spectral component */ |
1894 |
ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB); |
1895 |
multcolor(bsdfRGB, rayIn -> pcol); |
1896 |
addcolor(bsdfRGB, nd.tdiff); |
1897 |
photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, bsdfRGB); |
1898 |
transmitted = 1; |
1899 |
} |
1900 |
|
1901 |
else if ((xi -= prSpecSD) <= 0) { |
1902 |
/* Non-diffuse ("specular") SDF reflection */ |
1903 |
if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState), |
1904 |
SDsampSp | SDsampR, nd.sd))) |
1905 |
objerror(mat, USER, transSDError(err)); |
1906 |
|
1907 |
ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB); |
1908 |
photonRay(rayIn, &rayOut, PMAP_SPECREFL, bsdfRGB); |
1909 |
} |
1910 |
|
1911 |
else { |
1912 |
/* Non-diffuse ("specular") SDF transmission */ |
1913 |
if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState), |
1914 |
SDsampSp | SDsampT, nd.sd))) |
1915 |
objerror(mat, USER, transSDError(err)); |
1916 |
|
1917 |
/* Apply pattern to spectral component */ |
1918 |
ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB); |
1919 |
multcolor(bsdfRGB, rayIn -> pcol); |
1920 |
photonRay(rayIn, &rayOut, PMAP_SPECTRANS, bsdfRGB); |
1921 |
transmitted = 1; |
1922 |
} |
1923 |
|
1924 |
/* Xform outgoing dir to world coords */ |
1925 |
if ((err = SDmapDir(rayOut.rdir, nd.fromloc, nd.vray))) { |
1926 |
objerror(mat, USER, transSDError(err)); |
1927 |
return 0; |
1928 |
} |
1929 |
} |
1930 |
|
1931 |
/* Clean up */ |
1932 |
SDfreeCache(nd.sd); |
1933 |
|
1934 |
/* Offset outgoing photon origin by thickness to bypass proxy geometry */ |
1935 |
if (transmitted && nd.thick != 0) |
1936 |
VSUM(rayOut.rorg, rayOut.rorg, rayIn -> ron, -nd.thick); |
1937 |
|
1938 |
tracePhoton(&rayOut); |
1939 |
return 0; |
1940 |
} |
1941 |
|
1942 |
|
1943 |
|
1944 |
static int lightPhotonScatter (OBJREC* mat, RAY* ray) |
1945 |
/* Light sources doan' reflect, mang */ |
1946 |
{ |
1947 |
return 0; |
1948 |
} |
1949 |
|
1950 |
|
1951 |
|
1952 |
void initPhotonScatterFuncs () |
1953 |
/* Init photonScatter[] dispatch table */ |
1954 |
{ |
1955 |
int i; |
1956 |
|
1957 |
/* Catch-all for inconsistencies */ |
1958 |
for (i = 0; i < NUMOTYPE; i++) |
1959 |
photonScatter [i] = o_default; |
1960 |
|
1961 |
photonScatter [MAT_LIGHT] = photonScatter [MAT_ILLUM] = |
1962 |
photonScatter [MAT_GLOW] = photonScatter [MAT_SPOT] = |
1963 |
lightPhotonScatter; |
1964 |
|
1965 |
photonScatter [MAT_PLASTIC] = photonScatter [MAT_METAL] = |
1966 |
photonScatter [MAT_TRANS] = normalPhotonScatter; |
1967 |
|
1968 |
photonScatter [MAT_PLASTIC2] = photonScatter [MAT_METAL2] = |
1969 |
photonScatter [MAT_TRANS2] = anisoPhotonScatter; |
1970 |
|
1971 |
photonScatter [MAT_DIELECTRIC] = photonScatter [MAT_INTERFACE] = |
1972 |
dielectricPhotonScatter; |
1973 |
|
1974 |
photonScatter [MAT_MIST] = mistPhotonScatter; |
1975 |
photonScatter [MAT_GLASS] = glassPhotonScatter; |
1976 |
photonScatter [MAT_CLIP] = clipPhotonScatter; |
1977 |
photonScatter [MAT_MIRROR] = mirrorPhotonScatter; |
1978 |
photonScatter [MIX_FUNC] = mx_funcPhotonScatter; |
1979 |
photonScatter [MIX_DATA] = mx_dataPhotonScatter; |
1980 |
photonScatter [MIX_PICT]= mx_pdataPhotonScatter; |
1981 |
|
1982 |
photonScatter [PAT_BDATA] = photonScatter [PAT_CDATA] = |
1983 |
photonScatter [PAT_BFUNC] = photonScatter [PAT_CFUNC] = |
1984 |
photonScatter [PAT_CPICT] = photonScatter [TEX_FUNC] = |
1985 |
photonScatter [TEX_DATA] = pattexPhotonScatter; |
1986 |
|
1987 |
photonScatter [MOD_ALIAS] = aliasPhotonScatter; |
1988 |
photonScatter [MAT_BRTDF] = brtdFuncPhotonScatter; |
1989 |
photonScatter [MAT_BSDF] = |
1990 |
photonScatter [MAT_ABSDF] = bsdfPhotonScatter; |
1991 |
} |