ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/glass.c
Revision: 2.17
Committed: Tue Jun 21 15:06:50 2005 UTC (18 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R7P2, rad3R7P1, rad4R0, rad3R8, rad3R9
Changes since 2.16: +38 -29 lines
Log Message:
Fixed bugs in black glass transmission and source weight computation

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.17 static const char RCSid[] = "$Id: glass.c,v 2.16 2005/04/19 01:15:06 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * glass.c - simpler shading function for thin glass surfaces.
6 greg 2.10 */
7    
8 greg 2.11 #include "copyright.h"
9 greg 1.1
10     #include "ray.h"
11 greg 2.8 #include "otypes.h"
12 schorsch 2.15 #include "rtotypes.h"
13 greg 2.8
14 greg 1.1 /*
15     * This definition of glass provides for a quick calculation
16     * using a single surface where two closely spaced parallel
17     * dielectric surfaces would otherwise be used. The chief
18     * advantage to using this material is speed, since internal
19     * reflections are avoided.
20     *
21     * The specification for glass is as follows:
22     *
23     * modifier glass id
24     * 0
25     * 0
26 greg 2.9 * 3+ red grn blu [refractive_index]
27 greg 1.1 *
28     * The color is used for the transmission at normal incidence.
29 greg 2.5 * To compute transmissivity (tn) from transmittance (Tn) use:
30 greg 1.1 *
31     * tn = (sqrt(.8402528435+.0072522239*Tn*Tn)-.9166530661)/.0036261119/Tn
32     *
33 greg 2.5 * The transmissivity of standard 88% transmittance glass is 0.96.
34 greg 1.12 * A refractive index other than the default can be used by giving
35     * it as the fourth real argument. The above formula no longer applies.
36     *
37 greg 1.1 * If we appear to hit the back side of the surface, then we
38     * turn the normal around.
39     */
40    
41     #define RINDEX 1.52 /* refractive index of glass */
42    
43    
44 schorsch 2.15 extern int
45     m_glass( /* color a ray which hit a thin glass surface */
46     OBJREC *m,
47     register RAY *r
48     )
49 greg 1.1 {
50     COLOR mcolor;
51     double pdot;
52     FVECT pnorm;
53 greg 1.12 double rindex, cos2;
54 greg 1.1 COLOR trans, refl;
55 greg 2.17 int hastexture, hastrans;
56 greg 1.11 double d, r1e, r1m;
57 greg 1.7 double transtest, transdist;
58 greg 2.8 double mirtest, mirdist;
59 greg 1.1 RAY p;
60     register int i;
61 greg 1.12 /* check arguments */
62     if (m->oargs.nfargs == 3)
63     rindex = RINDEX; /* default value of n */
64     else if (m->oargs.nfargs == 4)
65     rindex = m->oargs.farg[3]; /* use their value */
66     else
67 greg 1.1 objerror(m, USER, "bad arguments");
68 greg 2.17 /* check transmission */
69 greg 1.1 setcolor(mcolor, m->oargs.farg[0], m->oargs.farg[1], m->oargs.farg[2]);
70 greg 2.17 if ((hastrans = (intens(mcolor) > 1e-15))) {
71     for (i = 0; i < 3; i++)
72     if (colval(mcolor,i) < 1e-15)
73     colval(mcolor,i) = 1e-15;
74     } else if (r->crtype & SHADOW)
75     return(1);
76 greg 2.12 /* get modifiers */
77     raytexture(r, m->omod);
78 greg 1.1 if (r->rod < 0.0) /* reorient if necessary */
79     flipsurface(r);
80 greg 2.8 mirtest = transtest = 0;
81     mirdist = transdist = r->rot;
82 greg 2.12 /* perturb normal */
83 schorsch 2.14 if ( (hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY)) ) {
84 greg 2.8 pdot = raynormal(pnorm, r);
85 greg 2.13 } else {
86 greg 2.8 VCOPY(pnorm, r->ron);
87     pdot = r->rod;
88     }
89 greg 1.1 /* angular transmission */
90 greg 1.12 cos2 = sqrt( (1.0-1.0/(rindex*rindex)) +
91     pdot*pdot/(rindex*rindex) );
92 greg 2.17 if (hastrans)
93     setcolor(mcolor, pow(colval(mcolor,RED), 1.0/cos2),
94     pow(colval(mcolor,GRN), 1.0/cos2),
95     pow(colval(mcolor,BLU), 1.0/cos2));
96 greg 1.1
97     /* compute reflection */
98 greg 1.12 r1e = (pdot - rindex*cos2) / (pdot + rindex*cos2);
99 greg 1.11 r1e *= r1e;
100 greg 1.12 r1m = (1.0/pdot - rindex/cos2) / (1.0/pdot + rindex/cos2);
101 greg 1.11 r1m *= r1m;
102 greg 2.17 /* compute transmission */
103     if (hastrans) {
104     for (i = 0; i < 3; i++) {
105     d = colval(mcolor, i);
106     colval(trans,i) = .5*(1.0-r1e)*(1.0-r1e)*d /
107     (1.0-r1e*r1e*d*d);
108     colval(trans,i) += .5*(1.0-r1m)*(1.0-r1m)*d /
109     (1.0-r1m*r1m*d*d);
110     }
111     multcolor(trans, r->pcol); /* modify by pattern */
112 greg 1.1 /* transmitted ray */
113 greg 2.17 if (rayorigin(&p, TRANS, r, trans) == 0) {
114     if (!(r->crtype & SHADOW) && hastexture) {
115     for (i = 0; i < 3; i++) /* perturb direction */
116     p.rdir[i] = r->rdir[i] +
117 greg 2.2 2.*(1.-rindex)*r->pert[i];
118 greg 2.17 if (normalize(p.rdir) == 0.0) {
119     objerror(m, WARNING, "bad perturbation");
120     VCOPY(p.rdir, r->rdir);
121     }
122     } else {
123 greg 2.4 VCOPY(p.rdir, r->rdir);
124 greg 2.17 transtest = 2;
125 greg 2.4 }
126 greg 2.17 rayvalue(&p);
127     multcolor(p.rcol, p.rcoef);
128     addcolor(r->rcol, p.rcol);
129     transtest *= bright(p.rcol);
130     transdist = r->rot + p.rt;
131 greg 1.8 }
132 greg 1.1 }
133 greg 2.8 if (r->crtype & SHADOW) { /* skip reflected ray */
134     r->rt = transdist;
135 greg 2.7 return(1);
136 greg 2.8 }
137 greg 1.1 /* compute reflectance */
138     for (i = 0; i < 3; i++) {
139     d = colval(mcolor, i);
140     d *= d;
141 greg 1.11 colval(refl,i) = .5*r1e*(1.0+(1.0-2.0*r1e)*d)/(1.0-r1e*r1e*d);
142     colval(refl,i) += .5*r1m*(1.0+(1.0-2.0*r1m)*d)/(1.0-r1m*r1m*d);
143 greg 1.1 }
144     /* reflected ray */
145 greg 2.16 if (rayorigin(&p, REFLECTED, r, refl) == 0) {
146 greg 1.1 for (i = 0; i < 3; i++)
147     p.rdir[i] = r->rdir[i] + 2.0*pdot*pnorm[i];
148     rayvalue(&p);
149 greg 2.16 multcolor(p.rcol, p.rcoef);
150 greg 1.1 addcolor(r->rcol, p.rcol);
151 greg 2.8 if (!hastexture && r->ro != NULL && isflat(r->ro->otype)) {
152     mirtest = 2.0*bright(p.rcol);
153     mirdist = r->rot + p.rt;
154     }
155 greg 1.1 }
156 greg 2.8 /* check distance */
157     d = bright(r->rcol);
158     if (transtest > d)
159 greg 1.7 r->rt = transdist;
160 greg 2.8 else if (mirtest > d)
161     r->rt = mirdist;
162 greg 2.7 return(1);
163 greg 1.1 }