1 |
< |
/* Copyright (c) 1992 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
4 |
> |
static char SCCSid[] = "$SunId$ SGI"; |
5 |
|
#endif |
6 |
|
|
7 |
|
/* |
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 |
+ |
#ifndef MAXITER |
29 |
+ |
#define MAXITER 10 /* maximum # specular ray attempts */ |
30 |
+ |
#endif |
31 |
+ |
|
32 |
+ |
static gaussamp(); |
33 |
+ |
|
34 |
|
/* |
35 |
< |
* This routine uses portions of the reflection |
36 |
< |
* model described by Cook and Torrance. |
26 |
< |
* The computation of specular components has been simplified by |
27 |
< |
* numerous approximations and ommisions to improve speed. |
35 |
> |
* This routine implements the isotropic Gaussian |
36 |
> |
* model described by Ward in Siggraph `92 article. |
37 |
|
* We orient the surface towards the incoming ray, so a single |
38 |
|
* surface can be used to represent an infinitely thin object. |
39 |
|
* |
44 |
|
* red grn blu rspec rough trans tspec |
45 |
|
*/ |
46 |
|
|
38 |
– |
#define BSPEC(m) (6.0) /* specularity parameter b */ |
39 |
– |
|
47 |
|
/* specularity flags */ |
48 |
|
#define SP_REFL 01 /* has reflected specular component */ |
49 |
|
#define SP_TRAN 02 /* has transmitted specular */ |
50 |
< |
#define SP_PURE 010 /* purely specular (zero roughness) */ |
50 |
> |
#define SP_PURE 04 /* purely specular (zero roughness) */ |
51 |
> |
#define SP_FLAT 010 /* flat reflecting surface */ |
52 |
> |
#define SP_RBLT 020 /* reflection below sample threshold */ |
53 |
> |
#define SP_TBLT 040 /* transmission below threshold */ |
54 |
|
|
55 |
|
typedef struct { |
56 |
|
OBJREC *mp; /* material pointer */ |
57 |
+ |
RAY *rp; /* ray pointer */ |
58 |
|
short specfl; /* specularity flags, defined above */ |
59 |
|
COLOR mcolor; /* color of this material */ |
60 |
|
COLOR scolor; /* color of specular component */ |
76 |
|
double omega; /* light source size */ |
77 |
|
{ |
78 |
|
double ldot; |
79 |
< |
double dtmp; |
79 |
> |
double dtmp, d2; |
80 |
> |
FVECT vtmp; |
81 |
|
COLOR ctmp; |
82 |
|
|
83 |
|
setcolor(cval, 0.0, 0.0, 0.0); |
103 |
|
* Compute specular reflection coefficient using |
104 |
|
* gaussian distribution model. |
105 |
|
*/ |
106 |
< |
/* roughness + source */ |
107 |
< |
dtmp = 2.0*np->alpha2 + omega/(2.0*PI); |
106 |
> |
/* roughness */ |
107 |
> |
dtmp = np->alpha2; |
108 |
> |
/* + source if flat */ |
109 |
> |
if (np->specfl & SP_FLAT) |
110 |
> |
dtmp += omega/(4.0*PI); |
111 |
> |
/* half vector */ |
112 |
> |
vtmp[0] = ldir[0] - np->rp->rdir[0]; |
113 |
> |
vtmp[1] = ldir[1] - np->rp->rdir[1]; |
114 |
> |
vtmp[2] = ldir[2] - np->rp->rdir[2]; |
115 |
> |
d2 = DOT(vtmp, np->pnorm); |
116 |
> |
d2 *= d2; |
117 |
> |
d2 = (DOT(vtmp,vtmp) - d2) / d2; |
118 |
|
/* gaussian */ |
119 |
< |
dtmp = exp((DOT(np->vrefl,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
119 |
> |
dtmp = exp(-d2/dtmp)/(4.*PI*dtmp); |
120 |
|
/* worth using? */ |
121 |
|
if (dtmp > FTINY) { |
122 |
|
copycolor(ctmp, np->scolor); |
123 |
< |
dtmp *= omega / np->pdot; |
123 |
> |
dtmp *= omega * sqrt(ldot/np->pdot); |
124 |
|
scalecolor(ctmp, dtmp); |
125 |
|
addcolor(cval, ctmp); |
126 |
|
} |
140 |
|
* is always modified by material color. |
141 |
|
*/ |
142 |
|
/* roughness + source */ |
143 |
< |
dtmp = np->alpha2 + omega/(2.0*PI); |
143 |
> |
dtmp = np->alpha2 + omega/PI; |
144 |
|
/* gaussian */ |
145 |
< |
dtmp = exp((DOT(np->prdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
145 |
> |
dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp); |
146 |
|
/* worth using? */ |
147 |
|
if (dtmp > FTINY) { |
148 |
|
copycolor(ctmp, np->mcolor); |
149 |
< |
dtmp *= np->tspec * omega / np->pdot; |
149 |
> |
dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot); |
150 |
|
scalecolor(ctmp, dtmp); |
151 |
|
addcolor(cval, ctmp); |
152 |
|
} |
160 |
|
{ |
161 |
|
NORMDAT nd; |
162 |
|
double transtest, transdist; |
163 |
< |
double dtmp; |
163 |
> |
double mirtest, mirdist; |
164 |
> |
int hastexture; |
165 |
> |
double d; |
166 |
|
COLOR ctmp; |
167 |
|
register int i; |
168 |
|
/* easy shadow test */ |
169 |
|
if (r->crtype & SHADOW && m->otype != MAT_TRANS) |
170 |
< |
return; |
170 |
> |
return(1); |
171 |
|
|
172 |
|
if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) |
173 |
|
objerror(m, USER, "bad number of arguments"); |
174 |
+ |
/* check for back side */ |
175 |
+ |
if (r->rod < 0.0) { |
176 |
+ |
if (!backvis && m->otype != MAT_TRANS) { |
177 |
+ |
raytrans(r); |
178 |
+ |
return(1); |
179 |
+ |
} |
180 |
+ |
flipsurface(r); /* reorient if backvis */ |
181 |
+ |
} |
182 |
|
nd.mp = m; |
183 |
+ |
nd.rp = r; |
184 |
|
/* get material color */ |
185 |
|
setcolor(nd.mcolor, m->oargs.farg[0], |
186 |
|
m->oargs.farg[1], |
190 |
|
nd.alpha2 = m->oargs.farg[4]; |
191 |
|
if ((nd.alpha2 *= nd.alpha2) <= FTINY) |
192 |
|
nd.specfl |= SP_PURE; |
193 |
< |
/* reorient if necessary */ |
194 |
< |
if (r->rod < 0.0) |
162 |
< |
flipsurface(r); |
193 |
> |
if (r->ro != NULL && isflat(r->ro->otype)) |
194 |
> |
nd.specfl |= SP_FLAT; |
195 |
|
/* get modifiers */ |
196 |
|
raytexture(r, m->omod); |
197 |
< |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
197 |
> |
if (hastexture = DOT(r->pert,r->pert) > FTINY*FTINY) |
198 |
> |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
199 |
> |
else { |
200 |
> |
VCOPY(nd.pnorm, r->ron); |
201 |
> |
nd.pdot = r->rod; |
202 |
> |
} |
203 |
|
if (nd.pdot < .001) |
204 |
|
nd.pdot = .001; /* non-zero for dirnorm() */ |
205 |
|
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
206 |
< |
transtest = 0; |
207 |
< |
/* get specular component */ |
208 |
< |
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 |
< |
} |
206 |
> |
mirtest = transtest = 0; |
207 |
> |
mirdist = transdist = r->rot; |
208 |
> |
nd.rspec = m->oargs.farg[3]; |
209 |
|
/* compute transmission */ |
210 |
|
if (m->otype == MAT_TRANS) { |
211 |
|
nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec); |
213 |
|
nd.tdiff = nd.trans - nd.tspec; |
214 |
|
if (nd.tspec > FTINY) { |
215 |
|
nd.specfl |= SP_TRAN; |
216 |
< |
if (r->crtype & SHADOW || |
217 |
< |
DOT(r->pert,r->pert) <= FTINY*FTINY) { |
216 |
> |
/* check threshold */ |
217 |
> |
if (!(nd.specfl & SP_PURE) && |
218 |
> |
specthresh >= nd.tspec-FTINY) |
219 |
> |
nd.specfl |= SP_TBLT; |
220 |
> |
if (!hastexture || r->crtype & SHADOW) { |
221 |
|
VCOPY(nd.prdir, r->rdir); |
222 |
|
transtest = 2; |
223 |
|
} else { |
224 |
|
for (i = 0; i < 3; i++) /* perturb */ |
225 |
< |
nd.prdir[i] = r->rdir[i] - |
226 |
< |
.75*r->pert[i]; |
227 |
< |
normalize(nd.prdir); |
225 |
> |
nd.prdir[i] = r->rdir[i] - r->pert[i]; |
226 |
> |
if (DOT(nd.prdir, r->ron) < -FTINY) |
227 |
> |
normalize(nd.prdir); /* OK */ |
228 |
> |
else |
229 |
> |
VCOPY(nd.prdir, r->rdir); |
230 |
|
} |
231 |
|
} |
232 |
|
} else |
233 |
|
nd.tdiff = nd.tspec = nd.trans = 0.0; |
234 |
|
/* transmitted ray */ |
235 |
< |
if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) { |
235 |
> |
if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) { |
236 |
|
RAY lr; |
237 |
|
if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) { |
238 |
|
VCOPY(lr.rdir, nd.prdir); |
243 |
|
transtest *= bright(lr.rcol); |
244 |
|
transdist = r->rot + lr.rt; |
245 |
|
} |
246 |
< |
} |
246 |
> |
} else |
247 |
> |
transtest = 0; |
248 |
|
|
249 |
< |
if (r->crtype & SHADOW) /* the rest is shadow */ |
250 |
< |
return; |
249 |
> |
if (r->crtype & SHADOW) { /* the rest is shadow */ |
250 |
> |
r->rt = transdist; |
251 |
> |
return(1); |
252 |
> |
} |
253 |
> |
/* get specular reflection */ |
254 |
> |
if (nd.rspec > FTINY) { |
255 |
> |
nd.specfl |= SP_REFL; |
256 |
> |
/* compute specular color */ |
257 |
> |
if (m->otype == MAT_METAL) |
258 |
> |
copycolor(nd.scolor, nd.mcolor); |
259 |
> |
else |
260 |
> |
setcolor(nd.scolor, 1.0, 1.0, 1.0); |
261 |
> |
scalecolor(nd.scolor, nd.rspec); |
262 |
> |
/* check threshold */ |
263 |
> |
if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY) |
264 |
> |
nd.specfl |= SP_RBLT; |
265 |
> |
/* compute reflected ray */ |
266 |
> |
for (i = 0; i < 3; i++) |
267 |
> |
nd.vrefl[i] = r->rdir[i] + 2.*nd.pdot*nd.pnorm[i]; |
268 |
> |
/* penetration? */ |
269 |
> |
if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY) |
270 |
> |
for (i = 0; i < 3; i++) /* safety measure */ |
271 |
> |
nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; |
272 |
> |
} |
273 |
> |
/* reflected ray */ |
274 |
> |
if ((nd.specfl&(SP_REFL|SP_PURE|SP_RBLT)) == (SP_REFL|SP_PURE)) { |
275 |
> |
RAY lr; |
276 |
> |
if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { |
277 |
> |
VCOPY(lr.rdir, nd.vrefl); |
278 |
> |
rayvalue(&lr); |
279 |
> |
multcolor(lr.rcol, nd.scolor); |
280 |
> |
addcolor(r->rcol, lr.rcol); |
281 |
> |
if (!hastexture && nd.specfl & SP_FLAT) { |
282 |
> |
mirtest = 2.*bright(lr.rcol); |
283 |
> |
mirdist = r->rot + lr.rt; |
284 |
> |
} |
285 |
> |
} |
286 |
> |
} |
287 |
|
/* diffuse reflection */ |
288 |
|
nd.rdiff = 1.0 - nd.trans - nd.rspec; |
289 |
|
|
290 |
|
if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) |
291 |
< |
return; /* 100% pure specular */ |
291 |
> |
return(1); /* 100% pure specular */ |
292 |
|
|
293 |
< |
if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE)) |
294 |
< |
gaussamp(r, &nd); |
293 |
> |
if (!(nd.specfl & SP_PURE)) |
294 |
> |
gaussamp(r, &nd); /* checks *BLT flags */ |
295 |
|
|
296 |
|
if (nd.rdiff > FTINY) { /* ambient from this side */ |
297 |
< |
ambient(ctmp, r); |
298 |
< |
scalecolor(ctmp, nd.rdiff); |
297 |
> |
ambient(ctmp, r, hastexture?nd.pnorm:r->ron); |
298 |
> |
if (nd.specfl & SP_RBLT) |
299 |
> |
scalecolor(ctmp, 1.0-nd.trans); |
300 |
> |
else |
301 |
> |
scalecolor(ctmp, nd.rdiff); |
302 |
|
multcolor(ctmp, nd.mcolor); /* modified by material color */ |
303 |
|
addcolor(r->rcol, ctmp); /* add to returned color */ |
304 |
|
} |
305 |
|
if (nd.tdiff > FTINY) { /* ambient from other side */ |
306 |
|
flipsurface(r); |
307 |
< |
ambient(ctmp, r); |
308 |
< |
scalecolor(ctmp, nd.tdiff); |
307 |
> |
if (hastexture) { |
308 |
> |
FVECT bnorm; |
309 |
> |
bnorm[0] = -nd.pnorm[0]; |
310 |
> |
bnorm[1] = -nd.pnorm[1]; |
311 |
> |
bnorm[2] = -nd.pnorm[2]; |
312 |
> |
ambient(ctmp, r, bnorm); |
313 |
> |
} else |
314 |
> |
ambient(ctmp, r, r->ron); |
315 |
> |
if (nd.specfl & SP_TBLT) |
316 |
> |
scalecolor(ctmp, nd.trans); |
317 |
> |
else |
318 |
> |
scalecolor(ctmp, nd.tdiff); |
319 |
|
multcolor(ctmp, nd.mcolor); /* modified by color */ |
320 |
|
addcolor(r->rcol, ctmp); |
321 |
|
flipsurface(r); |
323 |
|
/* add direct component */ |
324 |
|
direct(r, dirnorm, &nd); |
325 |
|
/* check distance */ |
326 |
< |
if (transtest > bright(r->rcol)) |
326 |
> |
d = bright(r->rcol); |
327 |
> |
if (transtest > d) |
328 |
|
r->rt = transdist; |
329 |
+ |
else if (mirtest > d) |
330 |
+ |
r->rt = mirdist; |
331 |
+ |
|
332 |
+ |
return(1); |
333 |
|
} |
334 |
|
|
335 |
|
|
342 |
|
FVECT u, v, h; |
343 |
|
double rv[2]; |
344 |
|
double d, sinp, cosp; |
345 |
< |
int confuse; |
345 |
> |
int niter; |
346 |
|
register int i; |
347 |
+ |
/* quick test */ |
348 |
+ |
if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL && |
349 |
+ |
(np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN) |
350 |
+ |
return; |
351 |
|
/* set up sample coordinates */ |
352 |
|
v[0] = v[1] = v[2] = 0.0; |
353 |
|
for (i = 0; i < 3; i++) |
358 |
|
normalize(u); |
359 |
|
fcross(v, np->pnorm, u); |
360 |
|
/* compute reflection */ |
361 |
< |
if (np->specfl & SP_REFL && |
361 |
> |
if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && |
362 |
|
rayorigin(&sr, r, SPECULAR, np->rspec) == 0) { |
288 |
– |
confuse = 0; |
363 |
|
dimlist[ndims++] = (int)np->mp; |
364 |
< |
refagain: |
365 |
< |
dimlist[ndims] = confuse += 3601; |
366 |
< |
d = urand(ilhash(dimlist,ndims+1)+samplendx); |
367 |
< |
multisamp(rv, 2, d); |
368 |
< |
d = 2.0*PI * rv[0]; |
369 |
< |
cosp = cos(d); |
370 |
< |
sinp = sin(d); |
371 |
< |
if (rv[1] <= FTINY) |
372 |
< |
d = 1.0; |
373 |
< |
else |
374 |
< |
d = sqrt( np->alpha2 * -log(rv[1]) ); |
375 |
< |
for (i = 0; i < 3; i++) |
376 |
< |
h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]); |
377 |
< |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
378 |
< |
for (i = 0; i < 3; i++) |
379 |
< |
sr.rdir[i] = r->rdir[i] + d*h[i]; |
380 |
< |
if (DOT(sr.rdir, r->ron) <= FTINY) /* oops! */ |
381 |
< |
goto refagain; |
382 |
< |
rayvalue(&sr); |
383 |
< |
multcolor(sr.rcol, np->scolor); |
384 |
< |
addcolor(r->rcol, sr.rcol); |
364 |
> |
for (niter = 0; niter < MAXITER; niter++) { |
365 |
> |
if (niter) |
366 |
> |
d = frandom(); |
367 |
> |
else |
368 |
> |
d = urand(ilhash(dimlist,ndims)+samplendx); |
369 |
> |
multisamp(rv, 2, d); |
370 |
> |
d = 2.0*PI * rv[0]; |
371 |
> |
cosp = tcos(d); |
372 |
> |
sinp = tsin(d); |
373 |
> |
rv[1] = 1.0 - specjitter*rv[1]; |
374 |
> |
if (rv[1] <= FTINY) |
375 |
> |
d = 1.0; |
376 |
> |
else |
377 |
> |
d = sqrt( np->alpha2 * -log(rv[1]) ); |
378 |
> |
for (i = 0; i < 3; i++) |
379 |
> |
h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]); |
380 |
> |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
381 |
> |
for (i = 0; i < 3; i++) |
382 |
> |
sr.rdir[i] = r->rdir[i] + d*h[i]; |
383 |
> |
if (DOT(sr.rdir, r->ron) > FTINY) { |
384 |
> |
rayvalue(&sr); |
385 |
> |
multcolor(sr.rcol, np->scolor); |
386 |
> |
addcolor(r->rcol, sr.rcol); |
387 |
> |
break; |
388 |
> |
} |
389 |
> |
} |
390 |
|
ndims--; |
391 |
|
} |
392 |
|
/* compute transmission */ |
393 |
+ |
if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && |
394 |
+ |
rayorigin(&sr, r, SPECULAR, np->tspec) == 0) { |
395 |
+ |
dimlist[ndims++] = (int)np->mp; |
396 |
+ |
for (niter = 0; niter < MAXITER; niter++) { |
397 |
+ |
if (niter) |
398 |
+ |
d = frandom(); |
399 |
+ |
else |
400 |
+ |
d = urand(ilhash(dimlist,ndims)+1823+samplendx); |
401 |
+ |
multisamp(rv, 2, d); |
402 |
+ |
d = 2.0*PI * rv[0]; |
403 |
+ |
cosp = tcos(d); |
404 |
+ |
sinp = tsin(d); |
405 |
+ |
rv[1] = 1.0 - specjitter*rv[1]; |
406 |
+ |
if (rv[1] <= FTINY) |
407 |
+ |
d = 1.0; |
408 |
+ |
else |
409 |
+ |
d = sqrt( np->alpha2 * -log(rv[1]) ); |
410 |
+ |
for (i = 0; i < 3; i++) |
411 |
+ |
sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]); |
412 |
+ |
if (DOT(sr.rdir, r->ron) < -FTINY) { |
413 |
+ |
normalize(sr.rdir); /* OK, normalize */ |
414 |
+ |
rayvalue(&sr); |
415 |
+ |
scalecolor(sr.rcol, np->tspec); |
416 |
+ |
multcolor(sr.rcol, np->mcolor); /* modified */ |
417 |
+ |
addcolor(r->rcol, sr.rcol); |
418 |
+ |
break; |
419 |
+ |
} |
420 |
+ |
} |
421 |
+ |
ndims--; |
422 |
+ |
} |
423 |
|
} |