ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/pmapmat.c
Revision: 2.21
Committed: Thu Dec 6 20:00:35 2018 UTC (5 years, 5 months ago) by rschregle
Content type: text/plain
Branch: MAIN
Changes since 2.20: +28 -28 lines
Log Message:
Fixed material pointer init bug in normal & aniso photon scattering routines

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: pmapmat.c,v 2.20 2018/12/06 12:21:38 rschregle 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.mp = mat;
323 nd.rp = rayIn;
324
325 /* Get material color */
326 copycolor(nd.mcolor, mat -> oargs.farg);
327
328 /* Get roughness */
329 nd.specfl = 0;
330 nd.alpha2 = mat -> oargs.farg [4];
331
332 if ((nd.alpha2 *= nd.alpha2) <= FTINY)
333 nd.specfl |= SP_PURE;
334
335 if (rayIn -> ro != NULL && isflat(rayIn -> ro -> otype))
336 nd.specfl |= SP_FLAT;
337
338 /* Perturb normal */
339 if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY)) ))
340 nd.pdot = raynormal(nd.pnorm, rayIn);
341 else {
342 VCOPY(nd.pnorm, rayIn -> ron);
343 nd.pdot = rayIn -> rod;
344 }
345
346 nd.pdot = max(nd.pdot, .001);
347
348 /* Modify material color */
349 multcolor(nd.mcolor, rayIn -> pcol);
350 nd.rspec = mat -> oargs.farg [3];
351
352 /* Approximate Fresnel term */
353 if (nd.specfl & SP_PURE && nd.rspec > FTINY) {
354 fresnel = FRESNE(rayIn -> rod);
355 nd.rspec += fresnel * (1 - nd.rspec);
356 }
357 else fresnel = 0;
358
359 /* Transmission params */
360 if (mat -> otype == MAT_TRANS) {
361 nd.trans = mat -> oargs.farg [5] * (1 - nd.rspec);
362 nd.tspec = nd.trans * mat -> oargs.farg [6];
363 nd.tdiff = nd.trans - nd.tspec;
364 }
365 else nd.tdiff = nd.tspec = nd.trans = 0;
366
367 /* Specular reflection params */
368 if (nd.rspec > FTINY) {
369 /* Specular color */
370 if (mat -> otype != MAT_METAL)
371 setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec);
372 else if (fresnel > FTINY) {
373 d = nd.rspec * (1 - fresnel);
374 for (i = 0; i < 3; i++)
375 nd.scolor [i] = fresnel + nd.mcolor [i] * d;
376 }
377 else {
378 copycolor(nd.scolor, nd.mcolor);
379 scalecolor(nd.scolor, nd.rspec);
380 }
381 }
382 else setcolor(nd.scolor, 0, 0, 0);
383
384 /* Diffuse reflection params */
385 nd.rdiff = 1 - nd.trans - nd.rspec;
386
387 /* Set up probabilities */
388 prdiff = ptdiff = ptspec = colorAvg(nd.mcolor);
389 prdiff *= nd.rdiff;
390 ptdiff *= nd.tdiff;
391 prspec = colorAvg(nd.scolor);
392 ptspec *= nd.tspec;
393 albedo = prdiff + ptdiff + prspec + ptspec;
394
395 /* Insert direct and indirect photon hits if diffuse component */
396 if (prdiff > FTINY || ptdiff > FTINY)
397 addPhotons(rayIn);
398
399 xi = pmapRandom(rouletteState);
400
401 if (xi > albedo)
402 /* Absorbed */
403 return 0;
404
405 if (xi > (albedo -= prspec)) {
406 /* Specular reflection */
407 nd.specfl |= SP_REFL;
408
409 if (nd.specfl & SP_PURE) {
410 /* Perfect specular reflection */
411 for (i = 0; i < 3; i++) {
412 /* Reflected ray */
413 nd.vrefl [i] = rayIn -> rdir [i] + 2 * nd.pdot * nd.pnorm [i];
414 }
415
416 /* Penetration? */
417 if (hastexture && DOT(nd.vrefl, rayIn -> ron) <= FTINY)
418 for (i = 0; i < 3; i++) {
419 /* Safety measure */
420 nd.vrefl [i] = rayIn -> rdir [i] +
421 2 * rayIn -> rod * rayIn -> ron [i];
422 }
423
424 VCOPY(rayOut.rdir, nd.vrefl);
425 }
426
427 else if (!isoSpecPhotonScatter(&nd, &rayOut))
428 return 0;
429
430 photonRay(rayIn, &rayOut, PMAP_SPECREFL, nd.scolor);
431 }
432
433 else if (xi > (albedo -= ptspec)) {
434 /* Specular transmission */
435 nd.specfl |= SP_TRAN;
436
437 if (hastexture) {
438 /* Perturb */
439 for (i = 0; i < 3; i++)
440 nd.prdir [i] = rayIn -> rdir [i] - rayIn -> pert [i];
441
442 if (DOT(nd.prdir, rayIn -> ron) < -FTINY)
443 normalize(nd.prdir);
444 else VCOPY(nd.prdir, rayIn -> rdir);
445 }
446 else VCOPY(nd.prdir, rayIn -> rdir);
447
448 if ((nd.specfl & (SP_TRAN | SP_PURE)) == (SP_TRAN | SP_PURE))
449 /* Perfect specular transmission */
450 VCOPY(rayOut.rdir, nd.prdir);
451 else if (!isoSpecPhotonScatter(&nd, &rayOut))
452 return 0;
453
454 photonRay(rayIn, &rayOut, PMAP_SPECTRANS, nd.mcolor);
455 }
456
457 else if (xi > (albedo -= prdiff)) {
458 /* Diffuse reflection */
459 photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.mcolor);
460 diffPhotonScatter(hastexture ? nd.pnorm : rayIn -> ron, &rayOut);
461 }
462
463 else {
464 /* Diffuse transmission */
465 flipsurface(rayIn);
466 photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.mcolor);
467
468 if (hastexture) {
469 FVECT bnorm;
470 bnorm [0] = -nd.pnorm [0];
471 bnorm [1] = -nd.pnorm [1];
472 bnorm [2] = -nd.pnorm [2];
473 diffPhotonScatter(bnorm, &rayOut);
474 }
475 else diffPhotonScatter(rayIn -> ron, &rayOut);
476 }
477
478 tracePhoton(&rayOut);
479 return 0;
480 }
481
482
483
484 static void getacoords (ANISODAT *nd)
485 /* Set up coordinate system for anisotropic sampling; cloned from aniso.c */
486 {
487 MFUNC *mf;
488 int i;
489
490 mf = getfunc(nd -> mp, 3, 0x7, 1);
491 setfunc(nd -> mp, nd -> rp);
492 errno = 0;
493
494 for (i = 0; i < 3; i++)
495 nd -> u [i] = evalue(mf -> ep [i]);
496
497 if (errno == EDOM || errno == ERANGE)
498 nd -> u [0] = nd -> u [1] = nd -> u [2] = 0.0;
499
500 if (mf -> fxp != &unitxf)
501 multv3(nd -> u, nd -> u, mf -> fxp -> xfm);
502
503 fcross(nd -> v, nd -> pnorm, nd -> u);
504
505 if (normalize(nd -> v) == 0.0) {
506 if (fabs(nd -> u_alpha - nd -> v_alpha) > 0.001)
507 objerror(nd -> mp, WARNING, "illegal orientation vector");
508 getperpendicular(nd -> u, nd -> pnorm, 1);
509 fcross(nd -> v, nd -> pnorm, nd -> u);
510 nd -> u_alpha = nd -> v_alpha =
511 sqrt(0.5 * (sqr(nd -> u_alpha) + sqr(nd -> v_alpha)));
512 }
513 else fcross(nd -> u, nd -> v, nd -> 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.mp = mat;
620 nd.rp = rayIn;
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 /* compute 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 /* Avoid potential memory leak? */
1037 if (aliasRec.os != aliasPtr -> os) {
1038 if (aliasPtr -> os)
1039 free_os(aliasPtr);
1040 aliasPtr -> os = aliasRec.os;
1041 }
1042
1043 return 0;
1044 }
1045
1046
1047
1048 static int clipPhotonScatter (OBJREC *mat, RAY *rayIn)
1049 /* Generate new photon ray for antimatter material and recurse */
1050 {
1051 OBJECT obj = objndx(mat), mod, cset [MAXSET + 1], *modset;
1052 int entering, inside = 0, i;
1053 const RAY *rp;
1054 RAY rayOut;
1055
1056 if ((modset = (OBJECT*)mat -> os) == NULL) {
1057 if (mat -> oargs.nsargs < 1 || mat -> oargs.nsargs > MAXSET)
1058 objerror(mat, USER, "bad # arguments");
1059
1060 modset = (OBJECT*)malloc((mat -> oargs.nsargs + 1) * sizeof(OBJECT));
1061
1062 if (modset == NULL)
1063 error(SYSTEM, "out of memory in clipPhotonScatter");
1064 modset [0] = 0;
1065
1066 for (i = 0; i < mat -> oargs.nsargs; i++) {
1067 if (!strcmp(mat -> oargs.sarg [i], VOIDID))
1068 continue;
1069
1070 if ((mod = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) {
1071 sprintf(errmsg, "unknown modifier \"%s\"", mat->oargs.sarg[i]);
1072 objerror(mat, WARNING, errmsg);
1073 continue;
1074 }
1075
1076 if (inset(modset, mod)) {
1077 objerror(mat, WARNING, "duplicate modifier");
1078 continue;
1079 }
1080
1081 insertelem(modset, mod);
1082 }
1083
1084 mat -> os = (char*)modset;
1085 }
1086
1087 if (rayIn -> clipset != NULL)
1088 setcopy(cset, rayIn -> clipset);
1089 else cset [0] = 0;
1090
1091 entering = rayIn -> rod > 0;
1092
1093 /* Store photon incident from front if material defined as sensor */
1094 if (entering && inset(photonSensorSet, obj))
1095 addPhotons(rayIn);
1096
1097 for (i = modset [0]; i > 0; i--) {
1098 if (entering) {
1099 if (!inset(cset, modset [i])) {
1100 if (cset [0] >= MAXSET)
1101 error(INTERNAL, "set overflow in clipPhotonScatter");
1102 insertelem(cset, modset [i]);
1103 }
1104 }
1105 else if (inset(cset, modset [i]))
1106 deletelem(cset, modset [i]);
1107 }
1108
1109 rayIn -> newcset = cset;
1110
1111 if (strcmp(mat -> oargs.sarg [0], VOIDID)) {
1112 for (rp = rayIn; rp -> parent != NULL; rp = rp -> parent) {
1113 if ( !(rp -> rtype & RAYREFL) && rp->parent->ro != NULL &&
1114 inset(modset, rp -> parent -> ro -> omod)) {
1115
1116 if (rp -> parent -> rod > 0)
1117 inside++;
1118 else inside--;
1119 }
1120 }
1121
1122 if (inside > 0) {
1123 flipsurface(rayIn);
1124 mat = objptr(lastmod(obj, mat -> oargs.sarg [0]));
1125 photonScatter [mat -> otype] (mat, rayIn);
1126 return 0;
1127 }
1128 }
1129
1130 /* Else transfer ray */
1131 photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1132 tracePhoton(&rayOut);
1133
1134 return 0;
1135 }
1136
1137
1138
1139 static int mirrorPhotonScatter (OBJREC *mat, RAY *rayIn)
1140 /* Generate new photon ray for mirror material and recurse */
1141 {
1142 RAY rayOut;
1143 int rpure = 1, i;
1144 FVECT pnorm;
1145 double pdot;
1146 float albedo;
1147 COLOR mcolor;
1148
1149 /* check arguments */
1150 if (mat -> oargs.nfargs != 3 || mat -> oargs.nsargs > 1)
1151 objerror(mat, USER, "bad number of arguments");
1152
1153 /* back is black */
1154 if (rayIn -> rod < 0)
1155 return 0;
1156
1157 /* get modifiers */
1158 raytexture(rayIn, mat -> omod);
1159
1160 /* assign material color */
1161 copycolor(mcolor, mat -> oargs.farg);
1162 multcolor(mcolor, rayIn -> pcol);
1163
1164 /* Set up probabilities */
1165 albedo = colorAvg(mcolor);
1166
1167 if (pmapRandom(rouletteState) > albedo)
1168 /* Absorbed */
1169 return 0;
1170
1171 /* compute reflected ray */
1172 photonRay(rayIn, &rayOut, PMAP_SPECREFL, mcolor);
1173
1174 if (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY)) {
1175 /* use textures */
1176 pdot = raynormal(pnorm, rayIn);
1177
1178 for (i = 0; i < 3; i++)
1179 rayOut.rdir [i] = rayIn -> rdir [i] + 2 * pdot * pnorm [i];
1180
1181 rpure = 0;
1182 }
1183
1184 /* Check for penetration */
1185 if (rpure || DOT(rayOut.rdir, rayIn -> ron) <= FTINY)
1186 for (i = 0; i < 3; i++)
1187 rayOut.rdir [i] = rayIn -> rdir [i] +
1188 2 * rayIn -> rod * rayIn -> ron [i];
1189
1190 tracePhoton(&rayOut);
1191 return 0;
1192 }
1193
1194
1195
1196 static int mistPhotonScatter (OBJREC *mat, RAY *rayIn)
1197 /* Generate new photon ray within mist and recurse */
1198 {
1199 COLOR mext;
1200 RREAL re, ge, be;
1201 RAY rayOut;
1202
1203 /* check arguments */
1204 if (mat -> oargs.nfargs > 7)
1205 objerror(mat, USER, "bad arguments");
1206
1207 if (mat -> oargs.nfargs > 2) {
1208 /* compute extinction */
1209 copycolor(mext, mat -> oargs.farg);
1210 /* get modifiers */
1211 raytexture(rayIn, mat -> omod);
1212 multcolor(mext, rayIn -> pcol);
1213 }
1214 else setcolor(mext, 0, 0, 0);
1215
1216 photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1217
1218 if (rayIn -> rod > 0) {
1219 /* entering ray */
1220 addcolor(rayOut.cext, mext);
1221
1222 if (mat -> oargs.nfargs > 5)
1223 copycolor(rayOut.albedo, mat -> oargs.farg + 3);
1224 if (mat -> oargs.nfargs > 6)
1225 rayOut.gecc = mat -> oargs.farg [6];
1226 }
1227
1228 else {
1229 /* leaving ray */
1230 re = max(rayIn -> cext [0] - mext [0], cextinction [0]);
1231 ge = max(rayIn -> cext [1] - mext [1], cextinction [1]);
1232 be = max(rayIn -> cext [2] - mext [2], cextinction [2]);
1233 setcolor(rayOut.cext, re, ge, be);
1234
1235 if (mat -> oargs.nfargs > 5)
1236 copycolor(rayOut.albedo, salbedo);
1237 if (mat -> oargs.nfargs > 6)
1238 rayOut.gecc = seccg;
1239 }
1240
1241 tracePhoton(&rayOut);
1242
1243 return 0;
1244 }
1245
1246
1247
1248 static int mx_dataPhotonScatter (OBJREC *mat, RAY *rayIn)
1249 /* Pass photon on to materials selected by mixture data */
1250 {
1251 OBJECT obj;
1252 double coef, pt [MAXDIM];
1253 DATARRAY *dp;
1254 OBJECT mod [2];
1255 MFUNC *mf;
1256 int i;
1257
1258 if (mat -> oargs.nsargs < 6)
1259 objerror(mat, USER, "bad # arguments");
1260
1261 obj = objndx(mat);
1262
1263 for (i = 0; i < 2; i++)
1264 if (!strcmp(mat -> oargs.sarg [i], VOIDID))
1265 mod [i] = OVOID;
1266 else if ((mod [i] = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) {
1267 sprintf(errmsg, "undefined modifier \"%s\"", mat->oargs.sarg[i]);
1268 objerror(mat, USER, errmsg);
1269 }
1270
1271 dp = getdata(mat -> oargs.sarg [3]);
1272 i = (1 << dp -> nd) - 1;
1273 mf = getfunc(mat, 4, i << 5, 0);
1274 setfunc(mat, rayIn);
1275 errno = 0;
1276
1277 for (i = 0; i < dp -> nd; i++) {
1278 pt [i] = evalue(mf -> ep [i]);
1279
1280 if (errno) {
1281 objerror(mat, WARNING, "compute error");
1282 return 0;
1283 }
1284 }
1285
1286 coef = datavalue(dp, pt);
1287 errno = 0;
1288 coef = funvalue(mat -> oargs.sarg [2], 1, &coef);
1289
1290 if (errno)
1291 objerror(mat, WARNING, "compute error");
1292 else {
1293 OBJECT mxMod = mod [pmapRandom(rouletteState) < coef ? 0 : 1];
1294
1295 if (mxMod != OVOID) {
1296 mat = objptr(mxMod);
1297 photonScatter [mat -> otype] (mat, rayIn);
1298 }
1299 else {
1300 /* Transfer ray if no modifier */
1301 RAY rayOut;
1302
1303 photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1304 tracePhoton(&rayOut);
1305 }
1306 }
1307
1308 return 0;
1309 }
1310
1311
1312
1313 static int mx_pdataPhotonScatter (OBJREC *mat, RAY *rayIn)
1314 /* Pass photon on to materials selected by mixture picture */
1315 {
1316 OBJECT obj;
1317 double col [3], coef, pt [MAXDIM];
1318 DATARRAY *dp;
1319 OBJECT mod [2];
1320 MFUNC *mf;
1321 int i;
1322
1323 if (mat -> oargs.nsargs < 7)
1324 objerror(mat, USER, "bad # arguments");
1325
1326 obj = objndx(mat);
1327
1328 for (i = 0; i < 2; i++)
1329 if (!strcmp(mat -> oargs.sarg [i], VOIDID))
1330 mod [i] = OVOID;
1331 else if ((mod [i] = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) {
1332 sprintf(errmsg, "undefined modifier \"%s\"", mat->oargs.sarg[i]);
1333 objerror(mat, USER, errmsg);
1334 }
1335
1336 dp = getpict(mat -> oargs.sarg [3]);
1337 mf = getfunc(mat, 4, 0x3 << 5, 0);
1338 setfunc(mat, rayIn);
1339 errno = 0;
1340 pt [1] = evalue(mf -> ep [0]);
1341 pt [0] = evalue(mf -> ep [1]);
1342
1343 if (errno) {
1344 objerror(mat, WARNING, "compute error");
1345 return 0;
1346 }
1347
1348 for (i = 0; i < 3; i++)
1349 col [i] = datavalue(dp + i, pt);
1350
1351 errno = 0;
1352 coef = funvalue(mat -> oargs.sarg [2], 3, col);
1353
1354 if (errno)
1355 objerror(mat, WARNING, "compute error");
1356 else {
1357 OBJECT mxMod = mod [pmapRandom(rouletteState) < coef ? 0 : 1];
1358
1359 if (mxMod != OVOID) {
1360 mat = objptr(mxMod);
1361 photonScatter [mat -> otype] (mat, rayIn);
1362 }
1363 else {
1364 /* Transfer ray if no modifier */
1365 RAY rayOut;
1366
1367 photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1368 tracePhoton(&rayOut);
1369 }
1370 }
1371
1372 return 0;
1373 }
1374
1375
1376
1377 static int mx_funcPhotonScatter (OBJREC *mat, RAY *rayIn)
1378 /* Pass photon on to materials selected by mixture function */
1379 {
1380 OBJECT obj, mod [2];
1381 int i;
1382 double coef;
1383 MFUNC *mf;
1384
1385 if (mat -> oargs.nsargs < 4)
1386 objerror(mat, USER, "bad # arguments");
1387
1388 obj = objndx(mat);
1389
1390 for (i = 0; i < 2; i++)
1391 if (!strcmp(mat -> oargs.sarg [i], VOIDID))
1392 mod [i] = OVOID;
1393 else if ((mod [i] = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) {
1394 sprintf(errmsg, "undefined modifier \"%s\"", mat->oargs.sarg[i]);
1395 objerror(mat, USER, errmsg);
1396 }
1397
1398 mf = getfunc(mat, 3, 0x4, 0);
1399 setfunc(mat, rayIn);
1400 errno = 0;
1401
1402 /* bound coefficient */
1403 coef = min(1, max(0, evalue(mf -> ep [0])));
1404
1405 if (errno)
1406 objerror(mat, WARNING, "compute error");
1407 else {
1408 OBJECT mxMod = mod [pmapRandom(rouletteState) < coef ? 0 : 1];
1409
1410 if (mxMod != OVOID) {
1411 mat = objptr(mxMod);
1412 photonScatter [mat -> otype] (mat, rayIn);
1413 }
1414 else {
1415 /* Transfer ray if no modifier */
1416 RAY rayOut;
1417
1418 photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1419 tracePhoton(&rayOut);
1420 }
1421 }
1422
1423 return 0;
1424 }
1425
1426
1427
1428 static int pattexPhotonScatter (OBJREC *mat, RAY *rayIn)
1429 /* Generate new photon ray for pattern or texture modifier and recurse.
1430 This code is brought to you by Henkel! :^) */
1431 {
1432 RAY rayOut;
1433
1434 /* Get pattern */
1435 ofun [mat -> otype].funp(mat, rayIn);
1436 if (mat -> omod != OVOID) {
1437 /* Scatter using modifier (if any) */
1438 mat = objptr(mat -> omod);
1439 photonScatter [mat -> otype] (mat, rayIn);
1440 }
1441 else {
1442 /* Transfer ray if no modifier */
1443 photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1444 tracePhoton(&rayOut);
1445 }
1446
1447 return 0;
1448 }
1449
1450
1451
1452 static int setbrdfunc(BRDFDAT *bd)
1453 /* Set up brdf function and variables; ripped off from m_brdf.c */
1454 {
1455 FVECT v;
1456
1457 if (setfunc(bd -> mp, bd -> pr) == 0)
1458 return 0;
1459
1460 /* (Re)Assign func variables */
1461 multv3(v, bd -> pnorm, funcxf.xfm);
1462 varset("NxP", '=', v [0] / funcxf.sca);
1463 varset("NyP", '=', v [1] / funcxf.sca);
1464 varset("NzP", '=', v [2] / funcxf.sca);
1465 varset("RdotP", '=',
1466 bd -> pdot <= -1. ? -1. : bd -> pdot >= 1. ? 1. : bd -> pdot);
1467 varset("CrP", '=', colval(bd -> mcolor, RED));
1468 varset("CgP", '=', colval(bd -> mcolor, GRN));
1469 varset("CbP", '=', colval(bd -> mcolor, BLU));
1470
1471 return 1;
1472 }
1473
1474
1475
1476 static int brdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1477 /* Generate new photon ray for BRTDfunc material and recurse. Only ideal
1478 reflection and transmission are sampled for the specular componentent. */
1479 {
1480 int hitfront = 1, hastexture, i;
1481 BRDFDAT nd;
1482 RAY rayOut;
1483 COLOR rspecCol, tspecCol;
1484 double prDiff, ptDiff, prSpec, ptSpec, albedo, xi;
1485 MFUNC *mf;
1486 FVECT bnorm;
1487
1488 /* Check argz */
1489 if (mat -> oargs.nsargs < 10 || mat -> oargs.nfargs < 9)
1490 objerror(mat, USER, "bad # arguments");
1491
1492 nd.mp = mat;
1493 nd.pr = rayIn;
1494 /* Dummiez */
1495 nd.rspec = nd.tspec = 1.0;
1496 nd.trans = 0.5;
1497
1498 /* Diffuz reflektanz */
1499 if (rayIn -> rod > 0.0)
1500 setcolor(nd.rdiff, mat -> oargs.farg[0], mat -> oargs.farg [1],
1501 mat -> oargs.farg [2]);
1502 else
1503 setcolor(nd.rdiff, mat-> oargs.farg [3], mat -> oargs.farg [4],
1504 mat -> oargs.farg [5]);
1505 /* Diffuz tranzmittanz */
1506 setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7],
1507 mat -> oargs.farg [8]);
1508
1509 /* Get modz */
1510 raytexture(rayIn, mat -> omod);
1511 hastexture = (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY));
1512 if (hastexture) {
1513 /* Perturb normal */
1514 nd.pdot = raynormal(nd.pnorm, rayIn);
1515 }
1516 else {
1517 VCOPY(nd.pnorm, rayIn -> ron);
1518 nd.pdot = rayIn -> rod;
1519 }
1520
1521 if (rayIn -> rod < 0.0) {
1522 /* Orient perturbed valuz */
1523 nd.pdot = -nd.pdot;
1524 for (i = 0; i < 3; i++) {
1525 nd.pnorm [i] = -nd.pnorm [i];
1526 rayIn -> pert [i] = -rayIn -> pert [i];
1527 }
1528
1529 hitfront = 0;
1530 }
1531
1532 /* Get pattern kolour, modify diffuz valuz */
1533 copycolor(nd.mcolor, rayIn -> pcol);
1534 multcolor(nd.rdiff, nd.mcolor);
1535 multcolor(nd.tdiff, nd.mcolor);
1536
1537 /* Load cal file, evaluate spekula refl/tranz varz */
1538 nd.dp = NULL;
1539 mf = getfunc(mat, 9, 0x3f, 0);
1540 setbrdfunc(&nd);
1541 errno = 0;
1542 setcolor(rspecCol,
1543 evalue(mf->ep[0]), evalue(mf->ep[1]), evalue(mf->ep[2]));
1544 setcolor(tspecCol,
1545 evalue(mf->ep[3]), evalue(mf->ep[4]), evalue(mf->ep[5]));
1546 if (errno == EDOM || errno == ERANGE)
1547 objerror(mat, WARNING, "compute error");
1548 else {
1549 /* Set up probz */
1550 prDiff = colorAvg(nd.rdiff);
1551 ptDiff = colorAvg(nd.tdiff);
1552 prSpec = colorAvg(rspecCol);
1553 ptSpec = colorAvg(tspecCol);
1554 albedo = prDiff + ptDiff + prSpec + ptSpec;
1555 }
1556
1557 /* Insert direct and indirect photon hitz if diffuz komponent */
1558 if (prDiff > FTINY || ptDiff > FTINY)
1559 addPhotons(rayIn);
1560
1561 /* Stochastically sample absorption or scattering evenz */
1562 if ((xi = pmapRandom(rouletteState)) > albedo)
1563 /* Absorbed */
1564 return 0;
1565
1566 if (xi > (albedo -= prSpec)) {
1567 /* Ideal spekula reflekzion */
1568 photonRay(rayIn, &rayOut, PMAP_SPECREFL, rspecCol);
1569 VSUM(rayOut.rdir, rayIn -> rdir, nd.pnorm, 2 * nd.pdot);
1570 checknorm(rayOut.rdir);
1571 }
1572 else if (xi > (albedo -= ptSpec)) {
1573 /* Ideal spekula tranzmission */
1574 photonRay(rayIn, &rayOut, PMAP_SPECTRANS, tspecCol);
1575 if (hastexture) {
1576 /* Perturb direkzion */
1577 VSUB(rayOut.rdir, rayIn -> rdir, rayIn -> pert);
1578 if (normalize(rayOut.rdir) == 0.0) {
1579 objerror(mat, WARNING, "illegal perturbation");
1580 VCOPY(rayOut.rdir, rayIn -> rdir);
1581 }
1582 else VCOPY(rayOut.rdir, rayIn -> rdir);
1583 }
1584 }
1585 else if (xi > (albedo -= prDiff)) {
1586 /* Diffuz reflekzion */
1587 if (!hitfront)
1588 flipsurface(rayIn);
1589 photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.mcolor);
1590 diffPhotonScatter(nd.pnorm, &rayOut);
1591 }
1592 else {
1593 /* Diffuz tranzmission */
1594 if (hitfront)
1595 flipsurface(rayIn);
1596 photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.mcolor);
1597 bnorm [0] = -nd.pnorm [0];
1598 bnorm [1] = -nd.pnorm [1];
1599 bnorm [2] = -nd.pnorm [2];
1600 diffPhotonScatter(bnorm, &rayOut);
1601 }
1602
1603 tracePhoton(&rayOut);
1604 return 0;
1605 }
1606
1607
1608
1609 int brdf2PhotonScatter (OBJREC *mat, RAY *rayIn)
1610 /* Generate new photon ray for procedural or data driven BRDF material and
1611 recurse. Only diffuse reflection and transmission are sampled. */
1612 {
1613 BRDFDAT nd;
1614 RAY rayOut;
1615 double dtmp, prDiff, ptDiff, albedo, xi;
1616 MFUNC *mf;
1617 FVECT bnorm;
1618
1619 /* Check argz */
1620 if (mat -> oargs.nsargs < (hasdata(mat -> otype) ? 4 : 2) ||
1621 mat -> oargs.nfargs < (mat -> otype == MAT_TFUNC ||
1622 mat -> otype == MAT_TDATA ? 6 : 4))
1623 objerror(mat, USER, "bad # arguments");
1624
1625 if (rayIn -> rod < 0.0) {
1626 /* Hit backside; reorient if visible, else transfer photon */
1627 if (!backvis) {
1628 photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1629 tracePhoton(&rayOut);
1630 return 0;
1631 }
1632
1633 raytexture(rayIn, mat -> omod);
1634 flipsurface(rayIn);
1635 }
1636 else raytexture(rayIn, mat -> omod);
1637
1638 nd.mp = mat;
1639 nd.pr = rayIn;
1640
1641 /* Material kolour */
1642 setcolor(nd.mcolor, mat -> oargs.farg [0], mat -> oargs.farg [1],
1643 mat -> oargs.farg [2]);
1644 /* Spekula komponent */
1645 nd.rspec = mat -> oargs.farg [3];
1646
1647 /* Tranzmittanz */
1648 if (mat -> otype == MAT_TFUNC || mat -> otype == MAT_TDATA) {
1649 nd.trans = mat -> oargs.farg [4] * (1.0 - nd.rspec);
1650 nd.tspec = nd.trans * mat -> oargs.farg [5];
1651 dtmp = nd.trans - nd.tspec;
1652 setcolor(nd.tdiff, dtmp, dtmp, dtmp);
1653 }
1654 else {
1655 nd.tspec = nd.trans = 0.0;
1656 setcolor(nd.tdiff, 0.0, 0.0, 0.0);
1657 }
1658
1659 /* Reflektanz */
1660 dtmp = 1.0 - nd.trans - nd.rspec;
1661 setcolor(nd.rdiff, dtmp, dtmp, dtmp);
1662 /* Perturb normal */
1663 nd.pdot = raynormal(nd.pnorm, rayIn);
1664 /* Modify material kolour */
1665 multcolor(nd.mcolor, rayIn -> pcol);
1666 multcolor(nd.rdiff, nd.mcolor);
1667 multcolor(nd.tdiff, nd.mcolor);
1668
1669 /* Load auxiliary filez */
1670 if (hasdata(mat -> otype)) {
1671 nd.dp = getdata(mat -> oargs.sarg [1]);
1672 getfunc(mat, 2, 0, 0);
1673 }
1674 else {
1675 nd.dp = NULL;
1676 getfunc(mat, 1, 0, 0);
1677 }
1678
1679 /* Set up probz */
1680 prDiff = colorAvg(nd.rdiff);
1681 ptDiff = colorAvg(nd.tdiff);
1682 albedo = prDiff + ptDiff;
1683
1684 /* Insert direct and indirect photon hitz if diffuz komponent */
1685 if (prDiff > FTINY || ptDiff > FTINY)
1686 addPhotons(rayIn);
1687
1688 /* Stochastically sample absorption or scattering evenz */
1689 if ((xi = pmapRandom(rouletteState)) > albedo)
1690 /* Absorbed */
1691 return 0;
1692
1693 if (xi > (albedo -= prDiff)) {
1694 /* Diffuz reflekzion */
1695 photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.rdiff);
1696 diffPhotonScatter(nd.pnorm, &rayOut);
1697 }
1698 else {
1699 /* Diffuz tranzmission */
1700 flipsurface(rayIn);
1701 photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.tdiff);
1702 bnorm [0] = -nd.pnorm [0];
1703 bnorm [1] = -nd.pnorm [1];
1704 bnorm [2] = -nd.pnorm [2];
1705 diffPhotonScatter(bnorm, &rayOut);
1706 }
1707
1708 tracePhoton(&rayOut);
1709 return 0;
1710 }
1711
1712
1713
1714 /*
1715 ==================================================================
1716 The following code is
1717 (c) Lucerne University of Applied Sciences and Arts,
1718 supported by the Swiss National Science Foundation (SNSF, #147053)
1719 ==================================================================
1720 */
1721
1722 static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1723 /* Generate new photon ray for BSDF modifier and recurse. */
1724 {
1725 int hasthick = (mat->otype == MAT_BSDF);
1726 int hitFront;
1727 SDError err;
1728 SDValue bsdfVal;
1729 FVECT upvec;
1730 MFUNC *mf;
1731 BSDFDAT nd;
1732 RAY rayOut;
1733 COLOR bsdfRGB;
1734 int transmitted;
1735 double prDiff, ptDiff, prDiffSD, ptDiffSD, prSpecSD, ptSpecSD,
1736 albedo, xi;
1737 const double patAlb = bright(rayIn -> pcol);
1738
1739 /* Following code adapted from m_bsdf() */
1740 /* Check arguments */
1741 if (mat -> oargs.nsargs < hasthick+5 || mat -> oargs.nfargs > 9 ||
1742 mat -> oargs.nfargs % 3)
1743 objerror(mat, USER, "bad # arguments");
1744
1745 hitFront = (rayIn -> rod > 0);
1746
1747 /* Load cal file */
1748 mf = hasthick ? getfunc(mat, 5, 0x1d, 1) : getfunc(mat, 4, 0xe, 1);
1749
1750 /* Get thickness */
1751 nd.thick = 0;
1752 if (hasthick) {
1753 nd.thick = evalue(mf -> ep [0]);
1754 if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
1755 nd.thick = .0;
1756 }
1757
1758 /* Get BSDF data */
1759 nd.sd = loadBSDF(mat -> oargs.sarg [hasthick]);
1760
1761 /* Extra diffuse reflectance from material def */
1762 if (hitFront) {
1763 if (mat -> oargs.nfargs < 3)
1764 setcolor(nd.rdiff, .0, .0, .0);
1765 else setcolor(nd.rdiff, mat -> oargs.farg [0], mat -> oargs.farg [1],
1766 mat -> oargs.farg [2]);
1767 }
1768 else if (mat -> oargs.nfargs < 6) {
1769 /* Check for absorbing backside */
1770 if (!backvis && !nd.sd -> rb && !nd.sd -> tf) {
1771 SDfreeCache(nd.sd);
1772 return 0;
1773 }
1774
1775 setcolor(nd.rdiff, .0, .0, .0);
1776 }
1777 else setcolor(nd.rdiff, mat -> oargs.farg [3], mat -> oargs.farg [4],
1778 mat -> oargs.farg [5]);
1779
1780 /* Extra diffuse transmittance from material def */
1781 if (mat -> oargs.nfargs < 9)
1782 setcolor(nd.tdiff, .0, .0, .0);
1783 else setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7],
1784 mat -> oargs.farg [8]);
1785
1786 nd.mp = mat;
1787 nd.pr = rayIn;
1788
1789 /* Get modifiers */
1790 raytexture(rayIn, mat -> omod);
1791
1792 /* Modify diffuse values */
1793 multcolor(nd.rdiff, rayIn -> pcol);
1794 multcolor(nd.tdiff, rayIn -> pcol);
1795
1796 /* Get up vector & xform to world coords */
1797 upvec [0] = evalue(mf -> ep [hasthick+0]);
1798 upvec [1] = evalue(mf -> ep [hasthick+1]);
1799 upvec [2] = evalue(mf -> ep [hasthick+2]);
1800
1801 if (mf -> fxp != &unitxf) {
1802 multv3(upvec, upvec, mf -> fxp -> xfm);
1803 nd.thick *= mf -> fxp -> sca;
1804 }
1805
1806 if (rayIn -> rox) {
1807 multv3(upvec, upvec, rayIn -> rox -> f.xfm);
1808 nd.thick *= rayIn -> rox -> f.sca;
1809 }
1810
1811 /* Perturb normal */
1812 raynormal(nd.pnorm, rayIn);
1813
1814 /* Xform incident dir to local BSDF coords */
1815 err = SDcompXform(nd.toloc, nd.pnorm, upvec);
1816
1817 if (!err) {
1818 nd.vray [0] = -rayIn -> rdir [0];
1819 nd.vray [1] = -rayIn -> rdir [1];
1820 nd.vray [2] = -rayIn -> rdir [2];
1821 err = SDmapDir(nd.vray, nd.toloc, nd.vray);
1822 }
1823
1824 if (!err)
1825 err = SDinvXform(nd.fromloc, nd.toloc);
1826
1827 if (err) {
1828 objerror(mat, WARNING, "Illegal orientation vector");
1829 return 0;
1830 }
1831
1832 /* Determine BSDF resolution */
1833 err = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL,
1834 SDqueryMin + SDqueryMax, nd.sd);
1835
1836 if (err)
1837 objerror(mat, USER, transSDError(err));
1838
1839 nd.sr_vpsa [0] = sqrt(nd.sr_vpsa [0]);
1840 nd.sr_vpsa [1] = sqrt(nd.sr_vpsa [1]);
1841
1842 /* Orient perturbed normal towards incident side */
1843 if (!hitFront) {
1844 nd.pnorm [0] = -nd.pnorm [0];
1845 nd.pnorm [1] = -nd.pnorm [1];
1846 nd.pnorm [2] = -nd.pnorm [2];
1847 }
1848
1849 /* Get scatter probabilities (weighted by pattern except for spec refl)
1850 * prDiff, ptDiff: extra diffuse component in material def
1851 * prDiffSD, ptDiffSD: diffuse (constant) component in SDF
1852 * prSpecSD, ptSpecSD: non-diffuse ("specular") component in SDF
1853 * albedo: sum of above, inverse absorption probability */
1854 prDiff = colorAvg(nd.rdiff);
1855 ptDiff = colorAvg(nd.tdiff);
1856 prDiffSD = patAlb * SDdirectHemi(nd.vray, SDsampDf | SDsampR, nd.sd);
1857 ptDiffSD = patAlb * SDdirectHemi(nd.vray, SDsampDf | SDsampT, nd.sd);
1858 prSpecSD = SDdirectHemi(nd.vray, SDsampSp | SDsampR, nd.sd);
1859 ptSpecSD = patAlb * SDdirectHemi(nd.vray, SDsampSp | SDsampT, nd.sd);
1860 albedo = prDiff + ptDiff + prDiffSD + ptDiffSD + prSpecSD + ptSpecSD;
1861
1862 /*
1863 if (albedo > 1)
1864 objerror(mat, WARNING, "Invalid albedo");
1865 */
1866
1867 /* Insert direct and indirect photon hits if diffuse component */
1868 if (prDiff + ptDiff + prDiffSD + ptDiffSD > FTINY)
1869 addPhotons(rayIn);
1870
1871 xi = pmapRandom(rouletteState);
1872
1873 if (xi > albedo)
1874 /* Absorbtion */
1875 return 0;
1876
1877 transmitted = 0;
1878
1879 if ((xi -= prDiff) <= 0) {
1880 /* Diffuse reflection (extra component in material def) */
1881 photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.rdiff);
1882 diffPhotonScatter(nd.pnorm, &rayOut);
1883 }
1884
1885 else if ((xi -= ptDiff) <= 0) {
1886 /* Diffuse transmission (extra component in material def) */
1887 photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.tdiff);
1888 diffPhotonScatter(nd.pnorm, &rayOut);
1889 transmitted = 1;
1890 }
1891
1892 else { /* Sample SDF */
1893 if ((xi -= prDiffSD) <= 0) {
1894 /* Diffuse SDF reflection (constant component) */
1895 if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1896 SDsampDf | SDsampR, nd.sd)))
1897 objerror(mat, USER, transSDError(err));
1898
1899 /* Apply pattern to spectral component */
1900 ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1901 multcolor(bsdfRGB, rayIn -> pcol);
1902 photonRay(rayIn, &rayOut, PMAP_DIFFREFL, bsdfRGB);
1903 }
1904
1905 else if ((xi -= ptDiffSD) <= 0) {
1906 /* Diffuse SDF transmission (constant component) */
1907 if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1908 SDsampDf | SDsampT, nd.sd)))
1909 objerror(mat, USER, transSDError(err));
1910
1911 /* Apply pattern to spectral component */
1912 ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1913 multcolor(bsdfRGB, rayIn -> pcol);
1914 addcolor(bsdfRGB, nd.tdiff);
1915 photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, bsdfRGB);
1916 transmitted = 1;
1917 }
1918
1919 else if ((xi -= prSpecSD) <= 0) {
1920 /* Non-diffuse ("specular") SDF reflection */
1921 if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1922 SDsampSp | SDsampR, nd.sd)))
1923 objerror(mat, USER, transSDError(err));
1924
1925 ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1926 photonRay(rayIn, &rayOut, PMAP_SPECREFL, bsdfRGB);
1927 }
1928
1929 else {
1930 /* Non-diffuse ("specular") SDF transmission */
1931 if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1932 SDsampSp | SDsampT, nd.sd)))
1933 objerror(mat, USER, transSDError(err));
1934
1935 /* Apply pattern to spectral component */
1936 ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1937 multcolor(bsdfRGB, rayIn -> pcol);
1938 photonRay(rayIn, &rayOut, PMAP_SPECTRANS, bsdfRGB);
1939 transmitted = 1;
1940 }
1941
1942 /* Xform outgoing dir to world coords */
1943 if ((err = SDmapDir(rayOut.rdir, nd.fromloc, nd.vray))) {
1944 objerror(mat, USER, transSDError(err));
1945 return 0;
1946 }
1947 }
1948
1949 /* Clean up */
1950 SDfreeCache(nd.sd);
1951
1952 /* Offset outgoing photon origin by thickness to bypass proxy geometry */
1953 if (transmitted && nd.thick != 0)
1954 VSUM(rayOut.rorg, rayOut.rorg, rayIn -> ron, -nd.thick);
1955
1956 tracePhoton(&rayOut);
1957 return 0;
1958 }
1959
1960
1961
1962 static int lightPhotonScatter (OBJREC* mat, RAY* ray)
1963 /* Light sources doan' reflect, mang */
1964 {
1965 return 0;
1966 }
1967
1968
1969
1970 void initPhotonScatterFuncs ()
1971 /* Init photonScatter[] dispatch table */
1972 {
1973 int i;
1974
1975 /* Catch-all for inconsistencies */
1976 for (i = 0; i < NUMOTYPE; i++)
1977 photonScatter [i] = o_default;
1978
1979 photonScatter [MAT_LIGHT] = photonScatter [MAT_ILLUM] =
1980 photonScatter [MAT_GLOW] = photonScatter [MAT_SPOT] =
1981 lightPhotonScatter;
1982
1983 photonScatter [MAT_PLASTIC] = photonScatter [MAT_METAL] =
1984 photonScatter [MAT_TRANS] = normalPhotonScatter;
1985
1986 photonScatter [MAT_PLASTIC2] = photonScatter [MAT_METAL2] =
1987 photonScatter [MAT_TRANS2] = anisoPhotonScatter;
1988
1989 photonScatter [MAT_DIELECTRIC] = photonScatter [MAT_INTERFACE] =
1990 dielectricPhotonScatter;
1991
1992 photonScatter [MAT_MIST] = mistPhotonScatter;
1993 photonScatter [MAT_GLASS] = glassPhotonScatter;
1994 photonScatter [MAT_CLIP] = clipPhotonScatter;
1995 photonScatter [MAT_MIRROR] = mirrorPhotonScatter;
1996 photonScatter [MIX_FUNC] = mx_funcPhotonScatter;
1997 photonScatter [MIX_DATA] = mx_dataPhotonScatter;
1998 photonScatter [MIX_PICT]= mx_pdataPhotonScatter;
1999
2000 photonScatter [PAT_BDATA] = photonScatter [PAT_CDATA] =
2001 photonScatter [PAT_BFUNC] = photonScatter [PAT_CFUNC] =
2002 photonScatter [PAT_CPICT] = photonScatter [TEX_FUNC] =
2003 photonScatter [TEX_DATA] = pattexPhotonScatter;
2004
2005 photonScatter [MOD_ALIAS] = aliasPhotonScatter;
2006 photonScatter [MAT_BRTDF] = brdfPhotonScatter;
2007
2008 photonScatter [MAT_PFUNC] = photonScatter [MAT_MFUNC] =
2009 photonScatter [MAT_PDATA] = photonScatter [MAT_MDATA] =
2010 photonScatter [MAT_TFUNC] = photonScatter [MAT_TDATA] =
2011 brdf2PhotonScatter;
2012
2013 photonScatter [MAT_BSDF] = photonScatter [MAT_ABSDF] =
2014 bsdfPhotonScatter;
2015 }