1 |
– |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* glass.c - simpler shading function for thin glass surfaces. |
9 |
– |
* |
10 |
– |
* 11/14/86 |
6 |
|
*/ |
7 |
|
|
8 |
+ |
#include "copyright.h" |
9 |
+ |
|
10 |
|
#include "ray.h" |
11 |
+ |
#include "otypes.h" |
12 |
+ |
#include "rtotypes.h" |
13 |
+ |
#include "pmapmat.h" |
14 |
|
|
15 |
|
/* |
16 |
|
* This definition of glass provides for a quick calculation |
24 |
|
* modifier glass id |
25 |
|
* 0 |
26 |
|
* 0 |
27 |
< |
* 3 red grn blu |
27 |
> |
* 3+ red grn blu [refractive_index] |
28 |
|
* |
29 |
|
* The color is used for the transmission at normal incidence. |
30 |
< |
* To compute transmission (tn) from transmissivity (Tn) use: |
30 |
> |
* To compute transmissivity (tn) from transmittance (Tn) use: |
31 |
|
* |
32 |
|
* tn = (sqrt(.8402528435+.0072522239*Tn*Tn)-.9166530661)/.0036261119/Tn |
33 |
|
* |
34 |
< |
* The transmission of standard 88% transmissivity glass is 0.96. |
34 |
> |
* The transmissivity of standard 88% transmittance glass is 0.96. |
35 |
> |
* 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 |
|
* If we appear to hit the back side of the surface, then we |
39 |
|
* turn the normal around. |
40 |
|
*/ |
42 |
|
#define RINDEX 1.52 /* refractive index of glass */ |
43 |
|
|
44 |
|
|
45 |
< |
m_glass(m, r) /* color a ray which hit a thin glass surface */ |
46 |
< |
OBJREC *m; |
47 |
< |
register RAY *r; |
45 |
> |
int |
46 |
> |
m_glass( /* color a ray which hit a thin glass surface */ |
47 |
> |
OBJREC *m, |
48 |
> |
RAY *r |
49 |
> |
) |
50 |
|
{ |
46 |
– |
double sqrt(), pow(); |
51 |
|
COLOR mcolor; |
52 |
|
double pdot; |
53 |
|
FVECT pnorm; |
54 |
< |
double cos2; |
54 |
> |
double rindex=0, cos2; |
55 |
|
COLOR trans, refl; |
56 |
< |
double d, r1; |
56 |
> |
int hastexture, hastrans; |
57 |
> |
double d, r1e, r1m; |
58 |
|
double transtest, transdist; |
59 |
+ |
double mirtest, mirdist; |
60 |
|
RAY p; |
61 |
< |
register int i; |
61 |
> |
int i; |
62 |
|
|
63 |
< |
if (m->oargs.nfargs != 3) |
63 |
> |
/* PMAP: skip refracted shadow ray if accounted for in photon map */ |
64 |
> |
if (shadowRayInPmap(r)) |
65 |
> |
return(1); |
66 |
> |
/* 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 |
|
objerror(m, USER, "bad arguments"); |
73 |
< |
|
73 |
> |
/* check back face visibility */ |
74 |
> |
if (!backvis && r->rod <= 0.0) { |
75 |
> |
raytrans(r); |
76 |
> |
return(1); |
77 |
> |
} |
78 |
> |
/* check transmission */ |
79 |
|
setcolor(mcolor, m->oargs.farg[0], m->oargs.farg[1], m->oargs.farg[2]); |
80 |
< |
|
81 |
< |
if (r->rod < 0.0) /* reorient if necessary */ |
82 |
< |
flipsurface(r); |
83 |
< |
r->rt = r->rot; /* default ray length */ |
84 |
< |
transtest = 0; |
80 |
> |
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 |
|
/* get modifiers */ |
87 |
|
raytexture(r, m->omod); |
88 |
< |
pdot = raynormal(pnorm, r); |
88 |
> |
if (r->rod < 0.0) /* reorient if necessary */ |
89 |
> |
flipsurface(r); |
90 |
> |
mirtest = transtest = 0; |
91 |
> |
mirdist = transdist = r->rot; |
92 |
> |
/* perturb normal */ |
93 |
> |
if ( (hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY)) ) { |
94 |
> |
pdot = raynormal(pnorm, r); |
95 |
> |
} else { |
96 |
> |
VCOPY(pnorm, r->ron); |
97 |
> |
pdot = r->rod; |
98 |
> |
} |
99 |
|
/* angular transmission */ |
100 |
< |
cos2 = sqrt( (1.0-1.0/RINDEX/RINDEX) + |
101 |
< |
pdot*pdot/(RINDEX*RINDEX) ); |
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)); |
100 |
> |
cos2 = sqrt( (1.0-1.0/(rindex*rindex)) + |
101 |
> |
pdot*pdot/(rindex*rindex) ); |
102 |
> |
if (hastrans) |
103 |
> |
setcolor(mcolor, pow(colval(mcolor,RED), 1.0/cos2), |
104 |
> |
pow(colval(mcolor,GRN), 1.0/cos2), |
105 |
> |
pow(colval(mcolor,BLU), 1.0/cos2)); |
106 |
|
|
107 |
|
/* compute reflection */ |
108 |
< |
r1 = (pdot - RINDEX*cos2) / (pdot + RINDEX*cos2); |
109 |
< |
d = (1.0/pdot - RINDEX/cos2) / (1.0/pdot + RINDEX/cos2); |
110 |
< |
r1 = (r1*r1 + d*d) / 2.0; |
111 |
< |
/* compute transmittance */ |
112 |
< |
for (i = 0; i < 3; i++) { |
113 |
< |
d = colval(mcolor, i); |
114 |
< |
colval(trans,i) = (1.0-r1)*(1.0-r1)*d / (1.0 - r1*r1*d*d); |
115 |
< |
} |
108 |
> |
r1e = (pdot - rindex*cos2) / (pdot + rindex*cos2); |
109 |
> |
r1e *= r1e; |
110 |
> |
r1m = (1.0/pdot - rindex/cos2) / (1.0/pdot + rindex/cos2); |
111 |
> |
r1m *= r1m; |
112 |
> |
/* compute transmission */ |
113 |
> |
if (hastrans) { |
114 |
> |
for (i = 0; i < 3; i++) { |
115 |
> |
d = colval(mcolor, i); |
116 |
> |
colval(trans,i) = .5*(1.0-r1e)*(1.0-r1e)*d / |
117 |
> |
(1.0-r1e*r1e*d*d); |
118 |
> |
colval(trans,i) += .5*(1.0-r1m)*(1.0-r1m)*d / |
119 |
> |
(1.0-r1m*r1m*d*d); |
120 |
> |
} |
121 |
> |
multcolor(trans, r->pcol); /* modify by pattern */ |
122 |
|
/* transmitted ray */ |
123 |
< |
if (rayorigin(&p, r, TRANS, bright(trans)) == 0) { |
124 |
< |
if (DOT(r->pert,r->pert) > FTINY*FTINY) { |
125 |
< |
for (i = 0; i < 3; i++) /* perturb direction */ |
126 |
< |
p.rdir[i] = r->rdir[i] - r->pert[i]/RINDEX; |
127 |
< |
normalize(p.rdir); |
128 |
< |
} else |
129 |
< |
transtest = 2; |
130 |
< |
rayvalue(&p); |
131 |
< |
multcolor(p.rcol, r->pcol); /* modify */ |
132 |
< |
multcolor(p.rcol, trans); |
133 |
< |
addcolor(r->rcol, p.rcol); |
134 |
< |
transtest *= bright(p.rcol); |
135 |
< |
transdist = r->rot + p.rt; |
123 |
> |
if (rayorigin(&p, TRANS, r, trans) == 0) { |
124 |
> |
if (!(r->crtype & (SHADOW|AMBIENT)) && hastexture) { |
125 |
> |
VSUM(p.rdir, r->rdir, r->pert, 2.*(1.-rindex)); |
126 |
> |
if (normalize(p.rdir) == 0.0) { |
127 |
> |
objerror(m, WARNING, "bad perturbation"); |
128 |
> |
VCOPY(p.rdir, r->rdir); |
129 |
> |
} |
130 |
> |
} else { |
131 |
> |
VCOPY(p.rdir, r->rdir); |
132 |
> |
transtest = 2; |
133 |
> |
} |
134 |
> |
rayvalue(&p); |
135 |
> |
multcolor(p.rcol, p.rcoef); |
136 |
> |
addcolor(r->rcol, p.rcol); |
137 |
> |
transtest *= bright(p.rcol); |
138 |
> |
transdist = r->rot + p.rt; |
139 |
> |
} |
140 |
|
} |
141 |
< |
|
142 |
< |
if (r->crtype & SHADOW) /* skip reflected ray */ |
143 |
< |
return; |
141 |
> |
if (r->crtype & SHADOW) { /* skip reflected ray */ |
142 |
> |
r->rt = transdist; |
143 |
> |
return(1); |
144 |
> |
} |
145 |
|
/* compute reflectance */ |
146 |
|
for (i = 0; i < 3; i++) { |
147 |
|
d = colval(mcolor, i); |
148 |
|
d *= d; |
149 |
< |
colval(refl,i) = r1 * (1.0 + (1.0-2.0*r1)*d) / (1.0 - r1*r1*d); |
149 |
> |
colval(refl,i) = .5*r1e*(1.0+(1.0-2.0*r1e)*d)/(1.0-r1e*r1e*d); |
150 |
> |
colval(refl,i) += .5*r1m*(1.0+(1.0-2.0*r1m)*d)/(1.0-r1m*r1m*d); |
151 |
|
} |
152 |
|
/* reflected ray */ |
153 |
< |
if (rayorigin(&p, r, REFLECTED, bright(refl)) == 0) { |
154 |
< |
for (i = 0; i < 3; i++) |
155 |
< |
p.rdir[i] = r->rdir[i] + 2.0*pdot*pnorm[i]; |
153 |
> |
if (rayorigin(&p, REFLECTED, r, refl) == 0) { |
154 |
> |
VSUM(p.rdir, r->rdir, pnorm, 2.*pdot); |
155 |
> |
checknorm(p.rdir); |
156 |
|
rayvalue(&p); |
157 |
< |
multcolor(p.rcol, refl); |
157 |
> |
multcolor(p.rcol, p.rcoef); |
158 |
|
addcolor(r->rcol, p.rcol); |
159 |
+ |
if (r->ro != NULL && isflat(r->ro->otype) && |
160 |
+ |
!hastexture | (r->crtype & AMBIENT)) { |
161 |
+ |
mirtest = 2.0*bright(p.rcol); |
162 |
+ |
mirdist = r->rot + p.rt; |
163 |
+ |
} |
164 |
|
} |
165 |
< |
if (transtest > bright(r->rcol)) |
165 |
> |
/* check distance */ |
166 |
> |
d = bright(r->rcol); |
167 |
> |
if (transtest > d) |
168 |
|
r->rt = transdist; |
169 |
+ |
else if (mirtest > d) |
170 |
+ |
r->rt = mirdist; |
171 |
+ |
return(1); |
172 |
|
} |