ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/pmapmat.c
Revision: 2.23
Committed: Wed Jan 20 19:44:15 2021 UTC (3 years, 4 months ago) by rschregle
Content type: text/plain
Branch: MAIN
Changes since 2.22: +2 -2 lines
Log Message:
fix(mkpmap): undefined photon ray dir in brdfPhotonScatter()

File Contents

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