ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/pmapmat.c
Revision: 2.13
Committed: Tue May 17 17:39:47 2016 UTC (7 years, 11 months ago) by rschregle
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R1
Changes since 2.12: +24 -22 lines
Log Message:
Initial import of ooC photon map

File Contents

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