ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/pmapmat.c
Revision: 2.9
Committed: Tue Sep 1 16:27:52 2015 UTC (8 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R0
Changes since 2.8: +1 -2 lines
Log Message:
Removed redundant $Id: in file

File Contents

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