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 |
|
45 |
typedef struct { |
46 |
OBJREC *mp; /* material pointer */ |
47 |
short specfl; /* specularity flags, defined above */ |
48 |
COLOR mcolor; /* color of this material */ |
49 |
COLOR scolor; /* color of specular component */ |
50 |
FVECT vrefl; /* vector in direction of reflected ray */ |
51 |
FVECT prdir; /* vector in transmitted direction */ |
52 |
double alpha2; /* roughness squared */ |
53 |
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 |
} 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 |
double ldot; |
68 |
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 |
* 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 |
*/ |
84 |
copycolor(ctmp, np->mcolor); |
85 |
dtmp = ldot * omega * np->rdiff / PI; |
86 |
scalecolor(ctmp, dtmp); |
87 |
addcolor(cval, ctmp); |
88 |
} |
89 |
if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) { |
90 |
/* |
91 |
* Compute specular reflection coefficient using |
92 |
* gaussian distribution model. |
93 |
*/ |
94 |
/* roughness + source */ |
95 |
dtmp = 2.0*np->alpha2 + omega/(2.0*PI); |
96 |
/* 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 |
dtmp *= omega / np->pdot; |
102 |
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 |
if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) { |
116 |
/* |
117 |
* Compute specular transmission. Specular transmission |
118 |
* is always modified by material color. |
119 |
*/ |
120 |
/* roughness + source */ |
121 |
dtmp = np->alpha2 + omega/(2.0*PI); |
122 |
/* gaussian */ |
123 |
dtmp = exp((DOT(np->prdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
124 |
/* worth using? */ |
125 |
if (dtmp > FTINY) { |
126 |
copycolor(ctmp, np->mcolor); |
127 |
dtmp *= np->tspec * omega / np->pdot; |
128 |
scalecolor(ctmp, dtmp); |
129 |
addcolor(cval, ctmp); |
130 |
} |
131 |
} |
132 |
} |
133 |
|
134 |
|
135 |
m_normal(m, r) /* color a ray that hit something normal */ |
136 |
register OBJREC *m; |
137 |
register RAY *r; |
138 |
{ |
139 |
NORMDAT nd; |
140 |
double transtest, transdist; |
141 |
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 |
|
148 |
if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) |
149 |
objerror(m, USER, "bad number of arguments"); |
150 |
nd.mp = m; |
151 |
/* get material color */ |
152 |
setcolor(nd.mcolor, m->oargs.farg[0], |
153 |
m->oargs.farg[1], |
154 |
m->oargs.farg[2]); |
155 |
/* get roughness */ |
156 |
nd.specfl = 0; |
157 |
nd.alpha2 = m->oargs.farg[4]; |
158 |
if ((nd.alpha2 *= nd.alpha2) <= FTINY) |
159 |
nd.specfl |= SP_PURE; |
160 |
/* reorient if necessary */ |
161 |
if (r->rod < 0.0) |
162 |
flipsurface(r); |
163 |
/* get modifiers */ |
164 |
raytexture(r, m->omod); |
165 |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
166 |
if (nd.pdot < .001) |
167 |
nd.pdot = .001; /* non-zero for dirnorm() */ |
168 |
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
169 |
transtest = 0; |
170 |
/* get specular component */ |
171 |
if ((nd.rspec = m->oargs.farg[3]) > FTINY) { |
172 |
nd.specfl |= SP_REFL; |
173 |
/* compute specular color */ |
174 |
if (m->otype == MAT_METAL) |
175 |
copycolor(nd.scolor, nd.mcolor); |
176 |
else |
177 |
setcolor(nd.scolor, 1.0, 1.0, 1.0); |
178 |
scalecolor(nd.scolor, nd.rspec); |
179 |
/* improved model */ |
180 |
dtmp = exp(-BSPEC(m)*nd.pdot); |
181 |
for (i = 0; i < 3; i++) |
182 |
colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp; |
183 |
nd.rspec += (1.0-nd.rspec)*dtmp; |
184 |
/* compute reflected ray */ |
185 |
for (i = 0; i < 3; i++) |
186 |
nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i]; |
187 |
|
188 |
if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) { |
189 |
RAY lr; |
190 |
if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { |
191 |
VCOPY(lr.rdir, nd.vrefl); |
192 |
rayvalue(&lr); |
193 |
multcolor(lr.rcol, nd.scolor); |
194 |
addcolor(r->rcol, lr.rcol); |
195 |
} |
196 |
} |
197 |
} |
198 |
/* compute transmission */ |
199 |
if (m->otype == MAT_TRANS) { |
200 |
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 |
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 |
} |
216 |
} else |
217 |
nd.tdiff = nd.tspec = nd.trans = 0.0; |
218 |
/* transmitted ray */ |
219 |
if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) { |
220 |
RAY lr; |
221 |
if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) { |
222 |
VCOPY(lr.rdir, nd.prdir); |
223 |
rayvalue(&lr); |
224 |
scalecolor(lr.rcol, nd.tspec); |
225 |
multcolor(lr.rcol, nd.mcolor); /* modified by color */ |
226 |
addcolor(r->rcol, lr.rcol); |
227 |
transtest *= bright(lr.rcol); |
228 |
transdist = r->rot + lr.rt; |
229 |
} |
230 |
} |
231 |
|
232 |
if (r->crtype & SHADOW) /* the rest is shadow */ |
233 |
return; |
234 |
/* diffuse reflection */ |
235 |
nd.rdiff = 1.0 - nd.trans - nd.rspec; |
236 |
|
237 |
if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) |
238 |
return; /* 100% pure specular */ |
239 |
|
240 |
if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE)) |
241 |
gaussamp(r, &nd); |
242 |
|
243 |
if (nd.rdiff > FTINY) { /* ambient from this side */ |
244 |
ambient(ctmp, r); |
245 |
scalecolor(ctmp, nd.rdiff); |
246 |
multcolor(ctmp, nd.mcolor); /* modified by material color */ |
247 |
addcolor(r->rcol, ctmp); /* add to returned color */ |
248 |
} |
249 |
if (nd.tdiff > FTINY) { /* ambient from other side */ |
250 |
flipsurface(r); |
251 |
ambient(ctmp, r); |
252 |
scalecolor(ctmp, nd.tdiff); |
253 |
multcolor(ctmp, nd.mcolor); /* modified by color */ |
254 |
addcolor(r->rcol, ctmp); |
255 |
flipsurface(r); |
256 |
} |
257 |
/* add direct component */ |
258 |
direct(r, dirnorm, &nd); |
259 |
/* check distance */ |
260 |
if (transtest > bright(r->rcol)) |
261 |
r->rt = transdist; |
262 |
} |
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 |
} |