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