ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/pmapmat.c
Revision: 2.24
Committed: Mon Feb 22 13:27:49 2021 UTC (3 years, 2 months ago) by rschregle
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4, HEAD
Changes since 2.23: +63 -41 lines
Log Message:
style(mkpmap): revised headers and indentation/linebreaks

File Contents

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