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