1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
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 |
+ |
extern double specthresh; /* specular sampling threshold */ |
24 |
+ |
extern double specjitter; /* specular sampling jitter */ |
25 |
+ |
|
26 |
+ |
extern int backvis; /* back faces visible? */ |
27 |
+ |
|
28 |
+ |
static gaussamp(); |
29 |
+ |
|
30 |
|
/* |
31 |
< |
* This routine uses portions of the reflection |
32 |
< |
* model described by Cook and Torrance. |
23 |
< |
* The computation of specular components has been simplified by |
24 |
< |
* numerous approximations and ommisions to improve speed. |
31 |
> |
* This routine implements the isotropic Gaussian |
32 |
> |
* model described by Ward in Siggraph `92 article. |
33 |
|
* We orient the surface towards the incoming ray, so a single |
34 |
|
* surface can be used to represent an infinitely thin object. |
35 |
|
* |
40 |
|
* red grn blu rspec rough trans tspec |
41 |
|
*/ |
42 |
|
|
43 |
< |
#define BSPEC(m) (6.0) /* specularity parameter b */ |
43 |
> |
/* specularity flags */ |
44 |
> |
#define SP_REFL 01 /* has reflected specular component */ |
45 |
> |
#define SP_TRAN 02 /* has transmitted specular */ |
46 |
> |
#define SP_PURE 04 /* purely specular (zero roughness) */ |
47 |
> |
#define SP_FLAT 010 /* flat reflecting surface */ |
48 |
> |
#define SP_RBLT 020 /* reflection below sample threshold */ |
49 |
> |
#define SP_TBLT 040 /* transmission below threshold */ |
50 |
|
|
37 |
– |
extern double exp(); |
38 |
– |
|
51 |
|
typedef struct { |
52 |
|
OBJREC *mp; /* material pointer */ |
53 |
< |
RAY *pr; /* intersected ray */ |
53 |
> |
RAY *rp; /* ray pointer */ |
54 |
> |
short specfl; /* specularity flags, defined above */ |
55 |
|
COLOR mcolor; /* color of this material */ |
56 |
|
COLOR scolor; /* color of specular component */ |
57 |
|
FVECT vrefl; /* vector in direction of reflected ray */ |
58 |
< |
double alpha2; /* roughness squared times 2 */ |
58 |
> |
FVECT prdir; /* vector in transmitted direction */ |
59 |
> |
double alpha2; /* roughness squared */ |
60 |
|
double rdiff, rspec; /* reflected specular, diffuse */ |
61 |
|
double trans; /* transmissivity */ |
62 |
|
double tdiff, tspec; /* transmitted specular, diffuse */ |
72 |
|
double omega; /* light source size */ |
73 |
|
{ |
74 |
|
double ldot; |
75 |
< |
double dtmp; |
75 |
> |
double dtmp, d2; |
76 |
> |
FVECT vtmp; |
77 |
|
COLOR ctmp; |
78 |
|
|
79 |
|
setcolor(cval, 0.0, 0.0, 0.0); |
94 |
|
scalecolor(ctmp, dtmp); |
95 |
|
addcolor(cval, ctmp); |
96 |
|
} |
97 |
< |
if (ldot > FTINY && np->rspec > FTINY && np->alpha2 > FTINY) { |
97 |
> |
if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) { |
98 |
|
/* |
99 |
|
* Compute specular reflection coefficient using |
100 |
|
* gaussian distribution model. |
101 |
|
*/ |
102 |
< |
/* roughness + source */ |
103 |
< |
dtmp = np->alpha2 + omega/(2.0*PI); |
102 |
> |
/* roughness */ |
103 |
> |
dtmp = np->alpha2; |
104 |
> |
/* + source if flat */ |
105 |
> |
if (np->specfl & SP_FLAT) |
106 |
> |
dtmp += omega/(4.0*PI); |
107 |
> |
/* half vector */ |
108 |
> |
vtmp[0] = ldir[0] - np->rp->rdir[0]; |
109 |
> |
vtmp[1] = ldir[1] - np->rp->rdir[1]; |
110 |
> |
vtmp[2] = ldir[2] - np->rp->rdir[2]; |
111 |
> |
d2 = DOT(vtmp, np->pnorm); |
112 |
> |
d2 *= d2; |
113 |
> |
d2 = (DOT(vtmp,vtmp) - d2) / d2; |
114 |
|
/* gaussian */ |
115 |
< |
dtmp = exp((DOT(np->vrefl,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
115 |
> |
dtmp = exp(-d2/dtmp)/(4.*PI*dtmp); |
116 |
|
/* worth using? */ |
117 |
|
if (dtmp > FTINY) { |
118 |
|
copycolor(ctmp, np->scolor); |
119 |
< |
dtmp *= omega; |
119 |
> |
dtmp *= omega * sqrt(ldot/np->pdot); |
120 |
|
scalecolor(ctmp, dtmp); |
121 |
|
addcolor(cval, ctmp); |
122 |
|
} |
130 |
|
scalecolor(ctmp, dtmp); |
131 |
|
addcolor(cval, ctmp); |
132 |
|
} |
133 |
< |
if (ldot < -FTINY && np->tspec > FTINY && np->alpha2 > FTINY) { |
133 |
> |
if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) { |
134 |
|
/* |
135 |
|
* Compute specular transmission. Specular transmission |
136 |
< |
* is unaffected by material color. |
136 |
> |
* is always modified by material color. |
137 |
|
*/ |
138 |
|
/* roughness + source */ |
139 |
< |
dtmp = np->alpha2 + omega/(2.0*PI); |
139 |
> |
dtmp = np->alpha2 + omega/PI; |
140 |
|
/* gaussian */ |
141 |
< |
dtmp = exp((DOT(np->pr->rdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
141 |
> |
dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp); |
142 |
|
/* worth using? */ |
143 |
|
if (dtmp > FTINY) { |
144 |
< |
dtmp *= np->tspec * omega; |
145 |
< |
setcolor(ctmp, dtmp, dtmp, dtmp); |
144 |
> |
copycolor(ctmp, np->mcolor); |
145 |
> |
dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot); |
146 |
> |
scalecolor(ctmp, dtmp); |
147 |
|
addcolor(cval, ctmp); |
148 |
|
} |
149 |
|
} |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
< |
m_normal(m, r) /* color a ray which hit something normal */ |
153 |
> |
m_normal(m, r) /* color a ray that hit something normal */ |
154 |
|
register OBJREC *m; |
155 |
|
register RAY *r; |
156 |
|
{ |
157 |
|
NORMDAT nd; |
158 |
< |
double ldot; |
133 |
< |
double omega; |
134 |
< |
double dtmp; |
158 |
> |
double transtest, transdist; |
159 |
|
COLOR ctmp; |
160 |
|
register int i; |
137 |
– |
|
138 |
– |
if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) |
139 |
– |
objerror(m, USER, "bad # arguments"); |
161 |
|
/* easy shadow test */ |
162 |
|
if (r->crtype & SHADOW && m->otype != MAT_TRANS) |
163 |
< |
return; |
163 |
> |
return(1); |
164 |
> |
|
165 |
> |
if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) |
166 |
> |
objerror(m, USER, "bad number of arguments"); |
167 |
|
nd.mp = m; |
168 |
< |
nd.pr = r; |
168 |
> |
nd.rp = r; |
169 |
|
/* get material color */ |
170 |
|
setcolor(nd.mcolor, m->oargs.farg[0], |
171 |
|
m->oargs.farg[1], |
172 |
|
m->oargs.farg[2]); |
173 |
|
/* get roughness */ |
174 |
+ |
nd.specfl = 0; |
175 |
|
nd.alpha2 = m->oargs.farg[4]; |
176 |
< |
nd.alpha2 *= 2.0 * nd.alpha2; |
177 |
< |
/* reorient if necessary */ |
178 |
< |
if (r->rod < 0.0) |
179 |
< |
flipsurface(r); |
176 |
> |
if ((nd.alpha2 *= nd.alpha2) <= FTINY) |
177 |
> |
nd.specfl |= SP_PURE; |
178 |
> |
/* check for back side */ |
179 |
> |
if (r->rod < 0.0) { |
180 |
> |
if (!backvis && m->otype != MAT_TRANS) { |
181 |
> |
raytrans(r); |
182 |
> |
return(1); |
183 |
> |
} |
184 |
> |
flipsurface(r); /* reorient if backvis */ |
185 |
> |
} |
186 |
|
/* get modifiers */ |
187 |
|
raytexture(r, m->omod); |
188 |
|
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
189 |
+ |
if (nd.pdot < .001) |
190 |
+ |
nd.pdot = .001; /* non-zero for dirnorm() */ |
191 |
|
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
192 |
+ |
transtest = 0; |
193 |
+ |
transdist = r->rot; |
194 |
|
/* get specular component */ |
195 |
< |
nd.rspec = m->oargs.farg[3]; |
196 |
< |
|
162 |
< |
if (nd.rspec > FTINY) { /* has specular component */ |
195 |
> |
if ((nd.rspec = m->oargs.farg[3]) > FTINY) { |
196 |
> |
nd.specfl |= SP_REFL; |
197 |
|
/* compute specular color */ |
198 |
|
if (m->otype == MAT_METAL) |
199 |
|
copycolor(nd.scolor, nd.mcolor); |
200 |
|
else |
201 |
|
setcolor(nd.scolor, 1.0, 1.0, 1.0); |
202 |
|
scalecolor(nd.scolor, nd.rspec); |
203 |
< |
/* improved model */ |
204 |
< |
dtmp = exp(-BSPEC(m)*nd.pdot); |
205 |
< |
for (i = 0; i < 3; i++) |
172 |
< |
colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp; |
173 |
< |
nd.rspec += (1.0-nd.rspec)*dtmp; |
203 |
> |
/* check threshold */ |
204 |
> |
if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY) |
205 |
> |
nd.specfl |= SP_RBLT; |
206 |
|
/* compute reflected ray */ |
207 |
|
for (i = 0; i < 3; i++) |
208 |
|
nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i]; |
209 |
+ |
if (DOT(nd.vrefl, r->ron) <= FTINY) /* penetration? */ |
210 |
+ |
for (i = 0; i < 3; i++) /* safety measure */ |
211 |
+ |
nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; |
212 |
|
|
213 |
< |
if (nd.alpha2 <= FTINY && !(r->crtype & SHADOW)) { |
213 |
> |
if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) { |
214 |
|
RAY lr; |
215 |
|
if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { |
216 |
|
VCOPY(lr.rdir, nd.vrefl); |
225 |
|
nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec); |
226 |
|
nd.tspec = nd.trans * m->oargs.farg[6]; |
227 |
|
nd.tdiff = nd.trans - nd.tspec; |
228 |
+ |
if (nd.tspec > FTINY) { |
229 |
+ |
nd.specfl |= SP_TRAN; |
230 |
+ |
/* check threshold */ |
231 |
+ |
if (!(nd.specfl & SP_PURE) && |
232 |
+ |
specthresh >= nd.tspec-FTINY) |
233 |
+ |
nd.specfl |= SP_TBLT; |
234 |
+ |
if (r->crtype & SHADOW || |
235 |
+ |
DOT(r->pert,r->pert) <= FTINY*FTINY) { |
236 |
+ |
VCOPY(nd.prdir, r->rdir); |
237 |
+ |
transtest = 2; |
238 |
+ |
} else { |
239 |
+ |
for (i = 0; i < 3; i++) /* perturb */ |
240 |
+ |
nd.prdir[i] = r->rdir[i] - r->pert[i]; |
241 |
+ |
if (DOT(nd.prdir, r->ron) < -FTINY) |
242 |
+ |
normalize(nd.prdir); /* OK */ |
243 |
+ |
else |
244 |
+ |
VCOPY(nd.prdir, r->rdir); |
245 |
+ |
} |
246 |
+ |
} |
247 |
|
} else |
248 |
|
nd.tdiff = nd.tspec = nd.trans = 0.0; |
249 |
|
/* transmitted ray */ |
250 |
< |
if (nd.tspec > FTINY && nd.alpha2 <= FTINY) { |
250 |
> |
if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) { |
251 |
|
RAY lr; |
252 |
|
if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) { |
253 |
< |
VCOPY(lr.rdir, r->rdir); |
253 |
> |
VCOPY(lr.rdir, nd.prdir); |
254 |
|
rayvalue(&lr); |
255 |
|
scalecolor(lr.rcol, nd.tspec); |
256 |
+ |
multcolor(lr.rcol, nd.mcolor); /* modified by color */ |
257 |
|
addcolor(r->rcol, lr.rcol); |
258 |
+ |
transtest *= bright(lr.rcol); |
259 |
+ |
transdist = r->rot + lr.rt; |
260 |
|
} |
261 |
< |
} |
261 |
> |
} else |
262 |
> |
transtest = 0; |
263 |
> |
|
264 |
|
if (r->crtype & SHADOW) /* the rest is shadow */ |
265 |
< |
return; |
265 |
> |
return(1); |
266 |
|
/* diffuse reflection */ |
267 |
|
nd.rdiff = 1.0 - nd.trans - nd.rspec; |
268 |
|
|
269 |
< |
if (nd.rdiff <= FTINY && nd.tdiff <= FTINY && nd.alpha2 <= FTINY) |
270 |
< |
return; /* purely specular */ |
269 |
> |
if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) |
270 |
> |
return(1); /* 100% pure specular */ |
271 |
|
|
272 |
+ |
if (r->ro != NULL && (r->ro->otype == OBJ_FACE || |
273 |
+ |
r->ro->otype == OBJ_RING)) |
274 |
+ |
nd.specfl |= SP_FLAT; |
275 |
+ |
|
276 |
+ |
if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE)) |
277 |
+ |
gaussamp(r, &nd); |
278 |
+ |
|
279 |
|
if (nd.rdiff > FTINY) { /* ambient from this side */ |
280 |
|
ambient(ctmp, r); |
281 |
< |
if (nd.alpha2 <= FTINY) |
216 |
< |
scalecolor(ctmp, nd.rdiff); |
217 |
< |
else |
281 |
> |
if (nd.specfl & SP_RBLT) |
282 |
|
scalecolor(ctmp, 1.0-nd.trans); |
283 |
+ |
else |
284 |
+ |
scalecolor(ctmp, nd.rdiff); |
285 |
|
multcolor(ctmp, nd.mcolor); /* modified by material color */ |
286 |
|
addcolor(r->rcol, ctmp); /* add to returned color */ |
287 |
|
} |
288 |
|
if (nd.tdiff > FTINY) { /* ambient from other side */ |
289 |
|
flipsurface(r); |
290 |
|
ambient(ctmp, r); |
291 |
< |
if (nd.alpha2 <= FTINY) |
226 |
< |
scalecolor(ctmp, nd.tdiff); |
227 |
< |
else |
291 |
> |
if (nd.specfl & SP_TBLT) |
292 |
|
scalecolor(ctmp, nd.trans); |
293 |
< |
multcolor(ctmp, nd.mcolor); |
293 |
> |
else |
294 |
> |
scalecolor(ctmp, nd.tdiff); |
295 |
> |
multcolor(ctmp, nd.mcolor); /* modified by color */ |
296 |
|
addcolor(r->rcol, ctmp); |
297 |
|
flipsurface(r); |
298 |
|
} |
299 |
|
/* add direct component */ |
300 |
|
direct(r, dirnorm, &nd); |
301 |
+ |
/* check distance */ |
302 |
+ |
if (transtest > bright(r->rcol)) |
303 |
+ |
r->rt = transdist; |
304 |
+ |
|
305 |
+ |
return(1); |
306 |
+ |
} |
307 |
+ |
|
308 |
+ |
|
309 |
+ |
static |
310 |
+ |
gaussamp(r, np) /* sample gaussian specular */ |
311 |
+ |
RAY *r; |
312 |
+ |
register NORMDAT *np; |
313 |
+ |
{ |
314 |
+ |
RAY sr; |
315 |
+ |
FVECT u, v, h; |
316 |
+ |
double rv[2]; |
317 |
+ |
double d, sinp, cosp; |
318 |
+ |
register int i; |
319 |
+ |
/* quick test */ |
320 |
+ |
if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL && |
321 |
+ |
(np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN) |
322 |
+ |
return; |
323 |
+ |
/* set up sample coordinates */ |
324 |
+ |
v[0] = v[1] = v[2] = 0.0; |
325 |
+ |
for (i = 0; i < 3; i++) |
326 |
+ |
if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6) |
327 |
+ |
break; |
328 |
+ |
v[i] = 1.0; |
329 |
+ |
fcross(u, v, np->pnorm); |
330 |
+ |
normalize(u); |
331 |
+ |
fcross(v, np->pnorm, u); |
332 |
+ |
/* compute reflection */ |
333 |
+ |
if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && |
334 |
+ |
rayorigin(&sr, r, SPECULAR, np->rspec) == 0) { |
335 |
+ |
dimlist[ndims++] = (int)np->mp; |
336 |
+ |
d = urand(ilhash(dimlist,ndims)+samplendx); |
337 |
+ |
multisamp(rv, 2, d); |
338 |
+ |
d = 2.0*PI * rv[0]; |
339 |
+ |
cosp = cos(d); |
340 |
+ |
sinp = sin(d); |
341 |
+ |
rv[1] = 1.0 - specjitter*rv[1]; |
342 |
+ |
if (rv[1] <= FTINY) |
343 |
+ |
d = 1.0; |
344 |
+ |
else |
345 |
+ |
d = sqrt( np->alpha2 * -log(rv[1]) ); |
346 |
+ |
for (i = 0; i < 3; i++) |
347 |
+ |
h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]); |
348 |
+ |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
349 |
+ |
for (i = 0; i < 3; i++) |
350 |
+ |
sr.rdir[i] = r->rdir[i] + d*h[i]; |
351 |
+ |
if (DOT(sr.rdir, r->ron) <= FTINY) |
352 |
+ |
VCOPY(sr.rdir, np->vrefl); /* jitter no good */ |
353 |
+ |
rayvalue(&sr); |
354 |
+ |
multcolor(sr.rcol, np->scolor); |
355 |
+ |
addcolor(r->rcol, sr.rcol); |
356 |
+ |
ndims--; |
357 |
+ |
} |
358 |
+ |
/* compute transmission */ |
359 |
+ |
if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && |
360 |
+ |
rayorigin(&sr, r, SPECULAR, np->tspec) == 0) { |
361 |
+ |
dimlist[ndims++] = (int)np->mp; |
362 |
+ |
d = urand(ilhash(dimlist,ndims)+1823+samplendx); |
363 |
+ |
multisamp(rv, 2, d); |
364 |
+ |
d = 2.0*PI * rv[0]; |
365 |
+ |
cosp = cos(d); |
366 |
+ |
sinp = sin(d); |
367 |
+ |
rv[1] = 1.0 - specjitter*rv[1]; |
368 |
+ |
if (rv[1] <= FTINY) |
369 |
+ |
d = 1.0; |
370 |
+ |
else |
371 |
+ |
d = sqrt( -log(rv[1]) * np->alpha2 ); |
372 |
+ |
for (i = 0; i < 3; i++) |
373 |
+ |
sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]); |
374 |
+ |
if (DOT(sr.rdir, r->ron) < -FTINY) |
375 |
+ |
normalize(sr.rdir); /* OK, normalize */ |
376 |
+ |
else |
377 |
+ |
VCOPY(sr.rdir, np->prdir); /* else no jitter */ |
378 |
+ |
rayvalue(&sr); |
379 |
+ |
scalecolor(sr.rcol, np->tspec); |
380 |
+ |
multcolor(sr.rcol, np->mcolor); /* modified by color */ |
381 |
+ |
addcolor(r->rcol, sr.rcol); |
382 |
+ |
ndims--; |
383 |
+ |
} |
384 |
|
} |