1 |
greg |
2.2 |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
greg |
1.1 |
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
5 |
|
|
#endif |
6 |
|
|
|
7 |
|
|
/* |
8 |
|
|
* normal.c - shading function for normal materials. |
9 |
|
|
* |
10 |
|
|
* 8/19/85 |
11 |
|
|
* 12/19/85 - added stuff for metals. |
12 |
|
|
* 6/26/87 - improved specular model. |
13 |
|
|
* 9/28/87 - added model for translucent materials. |
14 |
greg |
2.2 |
* Later changes described in delta comments. |
15 |
greg |
1.1 |
*/ |
16 |
|
|
|
17 |
|
|
#include "ray.h" |
18 |
|
|
|
19 |
|
|
#include "otypes.h" |
20 |
|
|
|
21 |
greg |
2.2 |
#include "random.h" |
22 |
|
|
|
23 |
greg |
1.1 |
/* |
24 |
|
|
* This routine uses portions of the reflection |
25 |
|
|
* model described by Cook and Torrance. |
26 |
|
|
* The computation of specular components has been simplified by |
27 |
|
|
* numerous approximations and ommisions to improve speed. |
28 |
|
|
* We orient the surface towards the incoming ray, so a single |
29 |
|
|
* surface can be used to represent an infinitely thin object. |
30 |
|
|
* |
31 |
|
|
* Arguments for MAT_PLASTIC and MAT_METAL are: |
32 |
|
|
* red grn blu specular-frac. facet-slope |
33 |
|
|
* |
34 |
|
|
* Arguments for MAT_TRANS are: |
35 |
|
|
* red grn blu rspec rough trans tspec |
36 |
|
|
*/ |
37 |
|
|
|
38 |
|
|
#define BSPEC(m) (6.0) /* specularity parameter b */ |
39 |
|
|
|
40 |
greg |
2.2 |
/* specularity flags */ |
41 |
|
|
#define SP_REFL 01 /* has reflected specular component */ |
42 |
|
|
#define SP_TRAN 02 /* has transmitted specular */ |
43 |
|
|
#define SP_PURE 010 /* purely specular (zero roughness) */ |
44 |
greg |
1.1 |
|
45 |
greg |
1.3 |
typedef struct { |
46 |
|
|
OBJREC *mp; /* material pointer */ |
47 |
greg |
2.2 |
short specfl; /* specularity flags, defined above */ |
48 |
greg |
1.1 |
COLOR mcolor; /* color of this material */ |
49 |
|
|
COLOR scolor; /* color of specular component */ |
50 |
|
|
FVECT vrefl; /* vector in direction of reflected ray */ |
51 |
greg |
1.14 |
FVECT prdir; /* vector in transmitted direction */ |
52 |
greg |
2.2 |
double alpha2; /* roughness squared */ |
53 |
greg |
1.1 |
double rdiff, rspec; /* reflected specular, diffuse */ |
54 |
|
|
double trans; /* transmissivity */ |
55 |
|
|
double tdiff, tspec; /* transmitted specular, diffuse */ |
56 |
|
|
FVECT pnorm; /* perturbed surface normal */ |
57 |
|
|
double pdot; /* perturbed dot product */ |
58 |
greg |
1.3 |
} NORMDAT; /* normal material data */ |
59 |
|
|
|
60 |
|
|
|
61 |
|
|
dirnorm(cval, np, ldir, omega) /* compute source contribution */ |
62 |
|
|
COLOR cval; /* returned coefficient */ |
63 |
|
|
register NORMDAT *np; /* material data */ |
64 |
|
|
FVECT ldir; /* light source direction */ |
65 |
|
|
double omega; /* light source size */ |
66 |
|
|
{ |
67 |
greg |
1.1 |
double ldot; |
68 |
greg |
1.3 |
double dtmp; |
69 |
|
|
COLOR ctmp; |
70 |
|
|
|
71 |
|
|
setcolor(cval, 0.0, 0.0, 0.0); |
72 |
|
|
|
73 |
|
|
ldot = DOT(np->pnorm, ldir); |
74 |
|
|
|
75 |
|
|
if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY) |
76 |
|
|
return; /* wrong side */ |
77 |
|
|
|
78 |
|
|
if (ldot > FTINY && np->rdiff > FTINY) { |
79 |
|
|
/* |
80 |
greg |
1.4 |
* Compute and add diffuse reflected component to returned |
81 |
|
|
* color. The diffuse reflected component will always be |
82 |
|
|
* modified by the color of the material. |
83 |
greg |
1.3 |
*/ |
84 |
|
|
copycolor(ctmp, np->mcolor); |
85 |
|
|
dtmp = ldot * omega * np->rdiff / PI; |
86 |
|
|
scalecolor(ctmp, dtmp); |
87 |
|
|
addcolor(cval, ctmp); |
88 |
|
|
} |
89 |
greg |
2.2 |
if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) { |
90 |
greg |
1.3 |
/* |
91 |
|
|
* Compute specular reflection coefficient using |
92 |
|
|
* gaussian distribution model. |
93 |
|
|
*/ |
94 |
|
|
/* roughness + source */ |
95 |
greg |
2.2 |
dtmp = 2.0*np->alpha2 + omega/(2.0*PI); |
96 |
greg |
1.3 |
/* gaussian */ |
97 |
|
|
dtmp = exp((DOT(np->vrefl,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
98 |
|
|
/* worth using? */ |
99 |
|
|
if (dtmp > FTINY) { |
100 |
|
|
copycolor(ctmp, np->scolor); |
101 |
greg |
1.13 |
dtmp *= omega / np->pdot; |
102 |
greg |
1.3 |
scalecolor(ctmp, dtmp); |
103 |
|
|
addcolor(cval, ctmp); |
104 |
|
|
} |
105 |
|
|
} |
106 |
|
|
if (ldot < -FTINY && np->tdiff > FTINY) { |
107 |
|
|
/* |
108 |
|
|
* Compute diffuse transmission. |
109 |
|
|
*/ |
110 |
|
|
copycolor(ctmp, np->mcolor); |
111 |
|
|
dtmp = -ldot * omega * np->tdiff / PI; |
112 |
|
|
scalecolor(ctmp, dtmp); |
113 |
|
|
addcolor(cval, ctmp); |
114 |
|
|
} |
115 |
greg |
2.2 |
if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) { |
116 |
greg |
1.3 |
/* |
117 |
greg |
1.4 |
* Compute specular transmission. Specular transmission |
118 |
greg |
1.13 |
* is always modified by material color. |
119 |
greg |
1.3 |
*/ |
120 |
|
|
/* roughness + source */ |
121 |
|
|
dtmp = np->alpha2 + omega/(2.0*PI); |
122 |
|
|
/* gaussian */ |
123 |
greg |
1.14 |
dtmp = exp((DOT(np->prdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
124 |
greg |
1.3 |
/* worth using? */ |
125 |
|
|
if (dtmp > FTINY) { |
126 |
greg |
1.13 |
copycolor(ctmp, np->mcolor); |
127 |
|
|
dtmp *= np->tspec * omega / np->pdot; |
128 |
|
|
scalecolor(ctmp, dtmp); |
129 |
greg |
1.3 |
addcolor(cval, ctmp); |
130 |
|
|
} |
131 |
|
|
} |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
|
135 |
greg |
2.2 |
m_normal(m, r) /* color a ray that hit something normal */ |
136 |
greg |
1.3 |
register OBJREC *m; |
137 |
|
|
register RAY *r; |
138 |
|
|
{ |
139 |
|
|
NORMDAT nd; |
140 |
greg |
1.9 |
double transtest, transdist; |
141 |
greg |
1.1 |
double dtmp; |
142 |
|
|
COLOR ctmp; |
143 |
|
|
register int i; |
144 |
|
|
/* easy shadow test */ |
145 |
|
|
if (r->crtype & SHADOW && m->otype != MAT_TRANS) |
146 |
|
|
return; |
147 |
greg |
2.2 |
|
148 |
|
|
if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) |
149 |
|
|
objerror(m, USER, "bad number of arguments"); |
150 |
greg |
1.3 |
nd.mp = m; |
151 |
greg |
1.1 |
/* get material color */ |
152 |
greg |
1.3 |
setcolor(nd.mcolor, m->oargs.farg[0], |
153 |
greg |
1.1 |
m->oargs.farg[1], |
154 |
|
|
m->oargs.farg[2]); |
155 |
|
|
/* get roughness */ |
156 |
greg |
2.2 |
nd.specfl = 0; |
157 |
greg |
1.3 |
nd.alpha2 = m->oargs.farg[4]; |
158 |
greg |
2.2 |
if ((nd.alpha2 *= nd.alpha2) <= FTINY) |
159 |
|
|
nd.specfl |= SP_PURE; |
160 |
greg |
1.1 |
/* reorient if necessary */ |
161 |
|
|
if (r->rod < 0.0) |
162 |
|
|
flipsurface(r); |
163 |
|
|
/* get modifiers */ |
164 |
|
|
raytexture(r, m->omod); |
165 |
greg |
1.3 |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
166 |
greg |
1.13 |
if (nd.pdot < .001) |
167 |
|
|
nd.pdot = .001; /* non-zero for dirnorm() */ |
168 |
greg |
1.3 |
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
169 |
greg |
1.9 |
transtest = 0; |
170 |
greg |
1.1 |
/* get specular component */ |
171 |
greg |
2.2 |
if ((nd.rspec = m->oargs.farg[3]) > FTINY) { |
172 |
|
|
nd.specfl |= SP_REFL; |
173 |
greg |
1.1 |
/* compute specular color */ |
174 |
|
|
if (m->otype == MAT_METAL) |
175 |
greg |
1.3 |
copycolor(nd.scolor, nd.mcolor); |
176 |
greg |
1.1 |
else |
177 |
greg |
1.3 |
setcolor(nd.scolor, 1.0, 1.0, 1.0); |
178 |
|
|
scalecolor(nd.scolor, nd.rspec); |
179 |
greg |
1.1 |
/* improved model */ |
180 |
greg |
1.3 |
dtmp = exp(-BSPEC(m)*nd.pdot); |
181 |
greg |
1.1 |
for (i = 0; i < 3; i++) |
182 |
greg |
1.3 |
colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp; |
183 |
|
|
nd.rspec += (1.0-nd.rspec)*dtmp; |
184 |
greg |
1.1 |
/* compute reflected ray */ |
185 |
|
|
for (i = 0; i < 3; i++) |
186 |
greg |
1.3 |
nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i]; |
187 |
greg |
1.1 |
|
188 |
greg |
2.2 |
if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) { |
189 |
greg |
1.3 |
RAY lr; |
190 |
|
|
if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { |
191 |
|
|
VCOPY(lr.rdir, nd.vrefl); |
192 |
greg |
1.1 |
rayvalue(&lr); |
193 |
greg |
1.3 |
multcolor(lr.rcol, nd.scolor); |
194 |
greg |
1.1 |
addcolor(r->rcol, lr.rcol); |
195 |
|
|
} |
196 |
greg |
1.3 |
} |
197 |
greg |
1.1 |
} |
198 |
greg |
1.3 |
/* compute transmission */ |
199 |
greg |
1.1 |
if (m->otype == MAT_TRANS) { |
200 |
greg |
1.3 |
nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec); |
201 |
|
|
nd.tspec = nd.trans * m->oargs.farg[6]; |
202 |
|
|
nd.tdiff = nd.trans - nd.tspec; |
203 |
greg |
2.2 |
if (nd.tspec > FTINY) { |
204 |
|
|
nd.specfl |= SP_TRAN; |
205 |
|
|
if (r->crtype & SHADOW || |
206 |
|
|
DOT(r->pert,r->pert) <= FTINY*FTINY) { |
207 |
|
|
VCOPY(nd.prdir, r->rdir); |
208 |
|
|
transtest = 2; |
209 |
|
|
} else { |
210 |
|
|
for (i = 0; i < 3; i++) /* perturb */ |
211 |
|
|
nd.prdir[i] = r->rdir[i] - |
212 |
|
|
.75*r->pert[i]; |
213 |
|
|
normalize(nd.prdir); |
214 |
|
|
} |
215 |
greg |
1.14 |
} |
216 |
greg |
1.1 |
} else |
217 |
greg |
1.3 |
nd.tdiff = nd.tspec = nd.trans = 0.0; |
218 |
greg |
1.1 |
/* transmitted ray */ |
219 |
greg |
2.2 |
if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) { |
220 |
greg |
1.3 |
RAY lr; |
221 |
|
|
if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) { |
222 |
greg |
1.14 |
VCOPY(lr.rdir, nd.prdir); |
223 |
greg |
1.1 |
rayvalue(&lr); |
224 |
greg |
1.3 |
scalecolor(lr.rcol, nd.tspec); |
225 |
greg |
1.8 |
multcolor(lr.rcol, nd.mcolor); /* modified by color */ |
226 |
greg |
1.1 |
addcolor(r->rcol, lr.rcol); |
227 |
greg |
1.9 |
transtest *= bright(lr.rcol); |
228 |
|
|
transdist = r->rot + lr.rt; |
229 |
greg |
1.1 |
} |
230 |
greg |
1.3 |
} |
231 |
greg |
2.2 |
|
232 |
greg |
1.1 |
if (r->crtype & SHADOW) /* the rest is shadow */ |
233 |
|
|
return; |
234 |
|
|
/* diffuse reflection */ |
235 |
greg |
1.3 |
nd.rdiff = 1.0 - nd.trans - nd.rspec; |
236 |
greg |
1.1 |
|
237 |
greg |
2.2 |
if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) |
238 |
|
|
return; /* 100% pure specular */ |
239 |
greg |
1.1 |
|
240 |
greg |
2.2 |
if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE)) |
241 |
|
|
gaussamp(r, &nd); |
242 |
|
|
|
243 |
greg |
1.3 |
if (nd.rdiff > FTINY) { /* ambient from this side */ |
244 |
greg |
1.2 |
ambient(ctmp, r); |
245 |
greg |
2.2 |
scalecolor(ctmp, nd.rdiff); |
246 |
greg |
1.3 |
multcolor(ctmp, nd.mcolor); /* modified by material color */ |
247 |
greg |
1.2 |
addcolor(r->rcol, ctmp); /* add to returned color */ |
248 |
|
|
} |
249 |
greg |
1.3 |
if (nd.tdiff > FTINY) { /* ambient from other side */ |
250 |
greg |
1.1 |
flipsurface(r); |
251 |
greg |
1.2 |
ambient(ctmp, r); |
252 |
greg |
2.2 |
scalecolor(ctmp, nd.tdiff); |
253 |
greg |
1.13 |
multcolor(ctmp, nd.mcolor); /* modified by color */ |
254 |
greg |
1.1 |
addcolor(r->rcol, ctmp); |
255 |
|
|
flipsurface(r); |
256 |
|
|
} |
257 |
greg |
1.3 |
/* add direct component */ |
258 |
|
|
direct(r, dirnorm, &nd); |
259 |
greg |
1.9 |
/* check distance */ |
260 |
|
|
if (transtest > bright(r->rcol)) |
261 |
|
|
r->rt = transdist; |
262 |
greg |
2.2 |
} |
263 |
|
|
|
264 |
|
|
|
265 |
|
|
static |
266 |
|
|
gaussamp(r, np) /* sample gaussian specular */ |
267 |
|
|
RAY *r; |
268 |
|
|
register NORMDAT *np; |
269 |
|
|
{ |
270 |
|
|
RAY sr; |
271 |
|
|
FVECT u, v, h; |
272 |
|
|
double rv[2]; |
273 |
|
|
double d, sinp, cosp; |
274 |
|
|
int confuse; |
275 |
|
|
register int i; |
276 |
|
|
/* set up sample coordinates */ |
277 |
|
|
v[0] = v[1] = v[2] = 0.0; |
278 |
|
|
for (i = 0; i < 3; i++) |
279 |
|
|
if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6) |
280 |
|
|
break; |
281 |
|
|
v[i] = 1.0; |
282 |
|
|
fcross(u, v, np->pnorm); |
283 |
|
|
normalize(u); |
284 |
|
|
fcross(v, np->pnorm, u); |
285 |
|
|
/* compute reflection */ |
286 |
|
|
if (np->specfl & SP_REFL && |
287 |
|
|
rayorigin(&sr, r, SPECULAR, np->rspec) == 0) { |
288 |
|
|
confuse = 0; |
289 |
|
|
dimlist[ndims++] = (int)np->mp; |
290 |
|
|
refagain: |
291 |
|
|
dimlist[ndims] = confuse += 3601; |
292 |
|
|
d = urand(ilhash(dimlist,ndims+1)+samplendx); |
293 |
|
|
multisamp(rv, 2, d); |
294 |
|
|
d = 2.0*PI * rv[0]; |
295 |
|
|
cosp = cos(d); |
296 |
|
|
sinp = sin(d); |
297 |
|
|
if (rv[1] <= FTINY) |
298 |
|
|
d = 1.0; |
299 |
|
|
else |
300 |
|
|
d = sqrt( np->alpha2 * -log(rv[1]) ); |
301 |
|
|
for (i = 0; i < 3; i++) |
302 |
|
|
h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]); |
303 |
|
|
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
304 |
|
|
for (i = 0; i < 3; i++) |
305 |
|
|
sr.rdir[i] = r->rdir[i] + d*h[i]; |
306 |
|
|
if (DOT(sr.rdir, r->ron) <= FTINY) /* oops! */ |
307 |
|
|
goto refagain; |
308 |
|
|
rayvalue(&sr); |
309 |
|
|
multcolor(sr.rcol, np->scolor); |
310 |
|
|
addcolor(r->rcol, sr.rcol); |
311 |
|
|
ndims--; |
312 |
|
|
} |
313 |
|
|
/* compute transmission */ |
314 |
greg |
1.1 |
} |