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 "ray.h" |
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 transmissivity (tn) from transmittance (Tn) use: |
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 |
|
{ |
51 |
|
COLOR mcolor; |
52 |
|
double pdot; |
53 |
|
FVECT pnorm; |
54 |
< |
double rindex, cos2; |
54 |
> |
double rindex=0, cos2; |
55 |
|
COLOR trans, refl; |
56 |
< |
int hastexture; |
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 |
> |
/* PMAP: skip refracted shadow or ambient ray if accounted for in |
64 |
> |
photon map */ |
65 |
> |
if (shadowRayInPmap(r) || ambRayInPmap(r)) |
66 |
> |
return(1); |
67 |
|
/* check arguments */ |
68 |
|
if (m->oargs.nfargs == 3) |
69 |
|
rindex = RINDEX; /* default value of n */ |
71 |
|
rindex = m->oargs.farg[3]; /* use their value */ |
72 |
|
else |
73 |
|
objerror(m, USER, "bad arguments"); |
74 |
< |
|
74 |
> |
/* check back face visibility */ |
75 |
> |
if (!backvis && r->rod <= 0.0) { |
76 |
> |
raytrans(r); |
77 |
> |
return(1); |
78 |
> |
} |
79 |
> |
/* check transmission */ |
80 |
|
setcolor(mcolor, m->oargs.farg[0], m->oargs.farg[1], m->oargs.farg[2]); |
81 |
< |
|
81 |
> |
if ((hastrans = (intens(mcolor) > 1e-15))) { |
82 |
> |
for (i = 0; i < 3; i++) |
83 |
> |
if (colval(mcolor,i) < 1e-15) |
84 |
> |
colval(mcolor,i) = 1e-15; |
85 |
> |
} else if (r->crtype & SHADOW) |
86 |
> |
return(1); |
87 |
> |
/* get modifiers */ |
88 |
> |
raytexture(r, m->omod); |
89 |
|
if (r->rod < 0.0) /* reorient if necessary */ |
90 |
|
flipsurface(r); |
91 |
|
mirtest = transtest = 0; |
92 |
|
mirdist = transdist = r->rot; |
93 |
< |
/* get modifiers */ |
94 |
< |
raytexture(r, m->omod); |
78 |
< |
if (hastexture = DOT(r->pert,r->pert) > FTINY*FTINY) |
93 |
> |
/* perturb normal */ |
94 |
> |
if ( (hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY)) ) { |
95 |
|
pdot = raynormal(pnorm, r); |
96 |
< |
else { |
96 |
> |
} else { |
97 |
|
VCOPY(pnorm, r->ron); |
98 |
|
pdot = r->rod; |
99 |
|
} |
100 |
|
/* angular transmission */ |
101 |
|
cos2 = sqrt( (1.0-1.0/(rindex*rindex)) + |
102 |
|
pdot*pdot/(rindex*rindex) ); |
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)); |
103 |
> |
if (hastrans) |
104 |
> |
setcolor(mcolor, pow(colval(mcolor,RED), 1.0/cos2), |
105 |
> |
pow(colval(mcolor,GRN), 1.0/cos2), |
106 |
> |
pow(colval(mcolor,BLU), 1.0/cos2)); |
107 |
|
|
108 |
|
/* compute reflection */ |
109 |
|
r1e = (pdot - rindex*cos2) / (pdot + rindex*cos2); |
110 |
|
r1e *= r1e; |
111 |
|
r1m = (1.0/pdot - rindex/cos2) / (1.0/pdot + rindex/cos2); |
112 |
|
r1m *= r1m; |
113 |
< |
/* compute transmittance */ |
114 |
< |
for (i = 0; i < 3; i++) { |
115 |
< |
d = colval(mcolor, i); |
116 |
< |
colval(trans,i) = .5*(1.0-r1e)*(1.0-r1e)*d/(1.0-r1e*r1e*d*d); |
117 |
< |
colval(trans,i) += .5*(1.0-r1m)*(1.0-r1m)*d/(1.0-r1m*r1m*d*d); |
118 |
< |
} |
113 |
> |
/* compute transmission */ |
114 |
> |
if (hastrans) { |
115 |
> |
for (i = 0; i < 3; i++) { |
116 |
> |
d = colval(mcolor, i); |
117 |
> |
colval(trans,i) = .5*(1.0-r1e)*(1.0-r1e)*d / |
118 |
> |
(1.0-r1e*r1e*d*d); |
119 |
> |
colval(trans,i) += .5*(1.0-r1m)*(1.0-r1m)*d / |
120 |
> |
(1.0-r1m*r1m*d*d); |
121 |
> |
} |
122 |
> |
multcolor(trans, r->pcol); /* modify by pattern */ |
123 |
|
/* transmitted ray */ |
124 |
< |
if (rayorigin(&p, r, TRANS, bright(trans)) == 0) { |
125 |
< |
if (!(r->crtype & SHADOW) && hastexture) { |
126 |
< |
for (i = 0; i < 3; i++) /* perturb direction */ |
127 |
< |
p.rdir[i] = r->rdir[i] + |
128 |
< |
2.*(1.-rindex)*r->pert[i]; |
129 |
< |
if (normalize(p.rdir) == 0.0) { |
130 |
< |
objerror(m, WARNING, "bad perturbation"); |
124 |
> |
if (rayorigin(&p, TRANS, r, trans) == 0) { |
125 |
> |
if (!(r->crtype & (SHADOW|AMBIENT)) && hastexture) { |
126 |
> |
VSUM(p.rdir, r->rdir, r->pert, 2.*(1.-rindex)); |
127 |
> |
if (normalize(p.rdir) == 0.0) { |
128 |
> |
objerror(m, WARNING, "bad perturbation"); |
129 |
> |
VCOPY(p.rdir, r->rdir); |
130 |
> |
} |
131 |
> |
} else { |
132 |
|
VCOPY(p.rdir, r->rdir); |
133 |
+ |
transtest = 2; |
134 |
|
} |
135 |
< |
} else { |
136 |
< |
VCOPY(p.rdir, r->rdir); |
137 |
< |
transtest = 2; |
135 |
> |
rayvalue(&p); |
136 |
> |
multcolor(p.rcol, p.rcoef); |
137 |
> |
addcolor(r->rcol, p.rcol); |
138 |
> |
transtest *= bright(p.rcol); |
139 |
> |
transdist = r->rot + p.rt; |
140 |
|
} |
116 |
– |
rayvalue(&p); |
117 |
– |
multcolor(p.rcol, r->pcol); /* modify */ |
118 |
– |
multcolor(p.rcol, trans); |
119 |
– |
addcolor(r->rcol, p.rcol); |
120 |
– |
transtest *= bright(p.rcol); |
121 |
– |
transdist = r->rot + p.rt; |
141 |
|
} |
123 |
– |
|
142 |
|
if (r->crtype & SHADOW) { /* skip reflected ray */ |
143 |
|
r->rt = transdist; |
144 |
|
return(1); |
151 |
|
colval(refl,i) += .5*r1m*(1.0+(1.0-2.0*r1m)*d)/(1.0-r1m*r1m*d); |
152 |
|
} |
153 |
|
/* reflected ray */ |
154 |
< |
if (rayorigin(&p, r, REFLECTED, bright(refl)) == 0) { |
155 |
< |
for (i = 0; i < 3; i++) |
156 |
< |
p.rdir[i] = r->rdir[i] + 2.0*pdot*pnorm[i]; |
154 |
> |
if (rayorigin(&p, REFLECTED, r, refl) == 0) { |
155 |
> |
VSUM(p.rdir, r->rdir, pnorm, 2.*pdot); |
156 |
> |
checknorm(p.rdir); |
157 |
|
rayvalue(&p); |
158 |
< |
multcolor(p.rcol, refl); |
158 |
> |
multcolor(p.rcol, p.rcoef); |
159 |
|
addcolor(r->rcol, p.rcol); |
160 |
< |
if (!hastexture && r->ro != NULL && isflat(r->ro->otype)) { |
160 |
> |
if (r->ro != NULL && isflat(r->ro->otype) && |
161 |
> |
!hastexture | (r->crtype & AMBIENT)) { |
162 |
|
mirtest = 2.0*bright(p.rcol); |
163 |
|
mirdist = r->rot + p.rt; |
164 |
|
} |