ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/pmapmat.c
Revision: 2.20
Committed: Thu Dec 6 12:21:38 2018 UTC (5 years, 5 months ago) by rschregle
Content type: text/plain
Branch: MAIN
Changes since 2.19: +138 -114 lines
Log Message:
Added rudimentary (diffuse only) photon scattering for *data and *func

File Contents

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