1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1995 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 |
|
|
18 |
– |
#include "source.h" |
19 |
– |
|
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. |
25 |
< |
* The computation of specular components has been simplified by |
26 |
< |
* 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 |
|
|
51 |
< |
|
52 |
< |
m_normal(m, r) /* color a ray which hit something normal */ |
53 |
< |
register OBJREC *m; |
54 |
< |
register RAY *r; |
43 |
< |
{ |
44 |
< |
double exp(); |
51 |
> |
typedef struct { |
52 |
> |
OBJREC *mp; /* material pointer */ |
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 */ |
59 |
< |
RAY lr; /* ray to illumination source */ |
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 */ |
63 |
|
FVECT pnorm; /* perturbed surface normal */ |
64 |
|
double pdot; /* perturbed dot product */ |
65 |
+ |
} NORMDAT; /* normal material data */ |
66 |
+ |
|
67 |
+ |
|
68 |
+ |
dirnorm(cval, np, ldir, omega) /* compute source contribution */ |
69 |
+ |
COLOR cval; /* returned coefficient */ |
70 |
+ |
register NORMDAT *np; /* material data */ |
71 |
+ |
FVECT ldir; /* light source direction */ |
72 |
+ |
double omega; /* light source size */ |
73 |
+ |
{ |
74 |
|
double ldot; |
75 |
< |
double omega; |
76 |
< |
double dtmp; |
75 |
> |
double dtmp, d2; |
76 |
> |
FVECT vtmp; |
77 |
|
COLOR ctmp; |
59 |
– |
register int i; |
78 |
|
|
79 |
< |
if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) |
80 |
< |
objerror(m, USER, "bad # arguments"); |
79 |
> |
setcolor(cval, 0.0, 0.0, 0.0); |
80 |
> |
|
81 |
> |
ldot = DOT(np->pnorm, ldir); |
82 |
> |
|
83 |
> |
if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY) |
84 |
> |
return; /* wrong side */ |
85 |
> |
|
86 |
> |
if (ldot > FTINY && np->rdiff > FTINY) { |
87 |
> |
/* |
88 |
> |
* Compute and add diffuse reflected component to returned |
89 |
> |
* color. The diffuse reflected component will always be |
90 |
> |
* modified by the color of the material. |
91 |
> |
*/ |
92 |
> |
copycolor(ctmp, np->mcolor); |
93 |
> |
dtmp = ldot * omega * np->rdiff / PI; |
94 |
> |
scalecolor(ctmp, dtmp); |
95 |
> |
addcolor(cval, ctmp); |
96 |
> |
} |
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 */ |
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(-d2/dtmp)/(4.*PI*dtmp); |
116 |
> |
/* worth using? */ |
117 |
> |
if (dtmp > FTINY) { |
118 |
> |
copycolor(ctmp, np->scolor); |
119 |
> |
dtmp *= omega * sqrt(ldot/np->pdot); |
120 |
> |
scalecolor(ctmp, dtmp); |
121 |
> |
addcolor(cval, ctmp); |
122 |
> |
} |
123 |
> |
} |
124 |
> |
if (ldot < -FTINY && np->tdiff > FTINY) { |
125 |
> |
/* |
126 |
> |
* Compute diffuse transmission. |
127 |
> |
*/ |
128 |
> |
copycolor(ctmp, np->mcolor); |
129 |
> |
dtmp = -ldot * omega * np->tdiff / PI; |
130 |
> |
scalecolor(ctmp, dtmp); |
131 |
> |
addcolor(cval, ctmp); |
132 |
> |
} |
133 |
> |
if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) { |
134 |
> |
/* |
135 |
> |
* Compute specular transmission. Specular transmission |
136 |
> |
* is always modified by material color. |
137 |
> |
*/ |
138 |
> |
/* roughness + source */ |
139 |
> |
dtmp = np->alpha2 + omega/PI; |
140 |
> |
/* gaussian */ |
141 |
> |
dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp); |
142 |
> |
/* worth using? */ |
143 |
> |
if (dtmp > FTINY) { |
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 that hit something normal */ |
154 |
> |
register OBJREC *m; |
155 |
> |
register RAY *r; |
156 |
> |
{ |
157 |
> |
NORMDAT nd; |
158 |
> |
double transtest, transdist; |
159 |
> |
double mirtest, mirdist; |
160 |
> |
int hastexture; |
161 |
> |
double d; |
162 |
> |
COLOR ctmp; |
163 |
> |
register int i; |
164 |
|
/* easy shadow test */ |
165 |
|
if (r->crtype & SHADOW && m->otype != MAT_TRANS) |
166 |
< |
return; |
166 |
> |
return(1); |
167 |
> |
|
168 |
> |
if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) |
169 |
> |
objerror(m, USER, "bad number of arguments"); |
170 |
> |
/* check for back side */ |
171 |
> |
if (r->rod < 0.0) { |
172 |
> |
if (!backvis && m->otype != MAT_TRANS) { |
173 |
> |
raytrans(r); |
174 |
> |
return(1); |
175 |
> |
} |
176 |
> |
flipsurface(r); /* reorient if backvis */ |
177 |
> |
} |
178 |
> |
nd.mp = m; |
179 |
> |
nd.rp = r; |
180 |
|
/* get material color */ |
181 |
< |
setcolor(mcolor, m->oargs.farg[0], |
181 |
> |
setcolor(nd.mcolor, m->oargs.farg[0], |
182 |
|
m->oargs.farg[1], |
183 |
|
m->oargs.farg[2]); |
184 |
|
/* get roughness */ |
185 |
< |
alpha2 = m->oargs.farg[4]; |
186 |
< |
alpha2 *= 2.0 * alpha2; |
187 |
< |
/* reorient if necessary */ |
188 |
< |
if (r->rod < 0.0) |
189 |
< |
flipsurface(r); |
185 |
> |
nd.specfl = 0; |
186 |
> |
nd.alpha2 = m->oargs.farg[4]; |
187 |
> |
if ((nd.alpha2 *= nd.alpha2) <= FTINY) |
188 |
> |
nd.specfl |= SP_PURE; |
189 |
> |
if (r->ro != NULL && isflat(r->ro->otype)) |
190 |
> |
nd.specfl |= SP_FLAT; |
191 |
|
/* get modifiers */ |
192 |
|
raytexture(r, m->omod); |
193 |
< |
pdot = raynormal(pnorm, r); /* perturb normal */ |
194 |
< |
multcolor(mcolor, r->pcol); /* modify material color */ |
195 |
< |
/* get specular component */ |
196 |
< |
rspec = m->oargs.farg[3]; |
193 |
> |
if (hastexture = DOT(r->pert,r->pert) > FTINY*FTINY) |
194 |
> |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
195 |
> |
else { |
196 |
> |
VCOPY(nd.pnorm, r->ron); |
197 |
> |
nd.pdot = r->rod; |
198 |
> |
} |
199 |
> |
if (nd.pdot < .001) |
200 |
> |
nd.pdot = .001; /* non-zero for dirnorm() */ |
201 |
> |
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
202 |
> |
mirtest = transtest = 0; |
203 |
> |
mirdist = transdist = r->rot; |
204 |
> |
nd.rspec = m->oargs.farg[3]; |
205 |
> |
/* compute transmission */ |
206 |
> |
if (m->otype == MAT_TRANS) { |
207 |
> |
nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec); |
208 |
> |
nd.tspec = nd.trans * m->oargs.farg[6]; |
209 |
> |
nd.tdiff = nd.trans - nd.tspec; |
210 |
> |
if (nd.tspec > FTINY) { |
211 |
> |
nd.specfl |= SP_TRAN; |
212 |
> |
/* check threshold */ |
213 |
> |
if (!(nd.specfl & SP_PURE) && |
214 |
> |
specthresh >= nd.tspec-FTINY) |
215 |
> |
nd.specfl |= SP_TBLT; |
216 |
> |
if (!hastexture || r->crtype & SHADOW) { |
217 |
> |
VCOPY(nd.prdir, r->rdir); |
218 |
> |
transtest = 2; |
219 |
> |
} else { |
220 |
> |
for (i = 0; i < 3; i++) /* perturb */ |
221 |
> |
nd.prdir[i] = r->rdir[i] - r->pert[i]; |
222 |
> |
if (DOT(nd.prdir, r->ron) < -FTINY) |
223 |
> |
normalize(nd.prdir); /* OK */ |
224 |
> |
else |
225 |
> |
VCOPY(nd.prdir, r->rdir); |
226 |
> |
} |
227 |
> |
} |
228 |
> |
} else |
229 |
> |
nd.tdiff = nd.tspec = nd.trans = 0.0; |
230 |
> |
/* transmitted ray */ |
231 |
> |
if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) { |
232 |
> |
RAY lr; |
233 |
> |
if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) { |
234 |
> |
VCOPY(lr.rdir, nd.prdir); |
235 |
> |
rayvalue(&lr); |
236 |
> |
scalecolor(lr.rcol, nd.tspec); |
237 |
> |
multcolor(lr.rcol, nd.mcolor); /* modified by color */ |
238 |
> |
addcolor(r->rcol, lr.rcol); |
239 |
> |
transtest *= bright(lr.rcol); |
240 |
> |
transdist = r->rot + lr.rt; |
241 |
> |
} |
242 |
> |
} else |
243 |
> |
transtest = 0; |
244 |
|
|
245 |
< |
if (rspec > FTINY) { /* has specular component */ |
245 |
> |
if (r->crtype & SHADOW) { /* the rest is shadow */ |
246 |
> |
r->rt = transdist; |
247 |
> |
return(1); |
248 |
> |
} |
249 |
> |
/* get specular reflection */ |
250 |
> |
if (nd.rspec > FTINY) { |
251 |
> |
nd.specfl |= SP_REFL; |
252 |
|
/* compute specular color */ |
253 |
|
if (m->otype == MAT_METAL) |
254 |
< |
copycolor(scolor, mcolor); |
254 |
> |
copycolor(nd.scolor, nd.mcolor); |
255 |
|
else |
256 |
< |
setcolor(scolor, 1.0, 1.0, 1.0); |
257 |
< |
scalecolor(scolor, rspec); |
258 |
< |
/* improved model */ |
259 |
< |
dtmp = exp(-BSPEC(m)*pdot); |
260 |
< |
for (i = 0; i < 3; i++) |
93 |
< |
colval(scolor,i) += (1.0-colval(scolor,i))*dtmp; |
94 |
< |
rspec += (1.0-rspec)*dtmp; |
256 |
> |
setcolor(nd.scolor, 1.0, 1.0, 1.0); |
257 |
> |
scalecolor(nd.scolor, nd.rspec); |
258 |
> |
/* check threshold */ |
259 |
> |
if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY) |
260 |
> |
nd.specfl |= SP_RBLT; |
261 |
|
/* compute reflected ray */ |
262 |
|
for (i = 0; i < 3; i++) |
263 |
< |
vrefl[i] = r->rdir[i] + 2.0*pdot*pnorm[i]; |
263 |
> |
nd.vrefl[i] = r->rdir[i] + 2.*nd.pdot*nd.pnorm[i]; |
264 |
> |
/* penetration? */ |
265 |
> |
if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY) |
266 |
> |
for (i = 0; i < 3; i++) /* safety measure */ |
267 |
> |
nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; |
268 |
|
|
269 |
< |
if (alpha2 <= FTINY && !(r->crtype & SHADOW)) |
270 |
< |
if (rayorigin(&lr, r, REFLECTED, rspec) == 0) { |
271 |
< |
VCOPY(lr.rdir, vrefl); |
269 |
> |
if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) { |
270 |
> |
RAY lr; |
271 |
> |
if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { |
272 |
> |
VCOPY(lr.rdir, nd.vrefl); |
273 |
|
rayvalue(&lr); |
274 |
< |
multcolor(lr.rcol, scolor); |
274 |
> |
multcolor(lr.rcol, nd.scolor); |
275 |
|
addcolor(r->rcol, lr.rcol); |
276 |
+ |
if (!hastexture && nd.specfl & SP_FLAT) { |
277 |
+ |
mirtest = 2.*bright(lr.rcol); |
278 |
+ |
mirdist = r->rot + lr.rt; |
279 |
+ |
} |
280 |
|
} |
106 |
– |
} |
107 |
– |
|
108 |
– |
if (m->otype == MAT_TRANS) { |
109 |
– |
trans = m->oargs.farg[5]*(1.0 - rspec); |
110 |
– |
tspec = trans * m->oargs.farg[6]; |
111 |
– |
tdiff = trans - tspec; |
112 |
– |
} else |
113 |
– |
tdiff = tspec = trans = 0.0; |
114 |
– |
/* transmitted ray */ |
115 |
– |
if (tspec > FTINY && alpha2 <= FTINY) |
116 |
– |
if (rayorigin(&lr, r, TRANS, tspec) == 0) { |
117 |
– |
VCOPY(lr.rdir, r->rdir); |
118 |
– |
rayvalue(&lr); |
119 |
– |
scalecolor(lr.rcol, tspec); |
120 |
– |
addcolor(r->rcol, lr.rcol); |
281 |
|
} |
282 |
< |
if (r->crtype & SHADOW) /* the rest is shadow */ |
123 |
< |
return; |
282 |
> |
} |
283 |
|
/* diffuse reflection */ |
284 |
< |
rdiff = 1.0 - trans - rspec; |
284 |
> |
nd.rdiff = 1.0 - nd.trans - nd.rspec; |
285 |
|
|
286 |
< |
if (rdiff <= FTINY && tdiff <= FTINY && alpha2 <= FTINY) |
287 |
< |
return; /* purely specular */ |
286 |
> |
if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) |
287 |
> |
return(1); /* 100% pure specular */ |
288 |
|
|
289 |
< |
ambient(ctmp, r); /* compute ambient component */ |
290 |
< |
scalecolor(ctmp, 1.0-trans); /* from this side */ |
132 |
< |
multcolor(ctmp, mcolor); /* modified by material color */ |
133 |
< |
addcolor(r->rcol, ctmp); /* add to returned color */ |
289 |
> |
if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE)) |
290 |
> |
gaussamp(r, &nd); |
291 |
|
|
292 |
< |
if (trans > FTINY) { /* ambient from other side */ |
292 |
> |
if (nd.rdiff > FTINY) { /* ambient from this side */ |
293 |
> |
ambient(ctmp, r, hastexture?nd.pnorm:r->ron); |
294 |
> |
if (nd.specfl & SP_RBLT) |
295 |
> |
scalecolor(ctmp, 1.0-nd.trans); |
296 |
> |
else |
297 |
> |
scalecolor(ctmp, nd.rdiff); |
298 |
> |
multcolor(ctmp, nd.mcolor); /* modified by material color */ |
299 |
> |
addcolor(r->rcol, ctmp); /* add to returned color */ |
300 |
> |
} |
301 |
> |
if (nd.tdiff > FTINY) { /* ambient from other side */ |
302 |
|
flipsurface(r); |
303 |
< |
scalecolor(ctmp, trans); |
304 |
< |
multcolor(ctmp, mcolor); |
303 |
> |
ambient(ctmp, r, hastexture?nd.pnorm:r->ron); |
304 |
> |
if (nd.specfl & SP_TBLT) |
305 |
> |
scalecolor(ctmp, nd.trans); |
306 |
> |
else |
307 |
> |
scalecolor(ctmp, nd.tdiff); |
308 |
> |
multcolor(ctmp, nd.mcolor); /* modified by color */ |
309 |
|
addcolor(r->rcol, ctmp); |
310 |
|
flipsurface(r); |
311 |
|
} |
312 |
< |
|
313 |
< |
for (i = 0; i < nsources; i++) { /* add specular and diffuse */ |
312 |
> |
/* add direct component */ |
313 |
> |
direct(r, dirnorm, &nd); |
314 |
> |
/* check distance */ |
315 |
> |
d = bright(r->rcol); |
316 |
> |
if (transtest > d) |
317 |
> |
r->rt = transdist; |
318 |
> |
else if (mirtest > d) |
319 |
> |
r->rt = mirdist; |
320 |
|
|
321 |
< |
if ((omega = srcray(&lr, r, i)) == 0.0) |
322 |
< |
continue; /* bad source */ |
321 |
> |
return(1); |
322 |
> |
} |
323 |
|
|
148 |
– |
ldot = DOT(pnorm, lr.rdir); |
149 |
– |
|
150 |
– |
if (ldot < 0.0 ? trans <= FTINY : trans >= 1.0-FTINY) |
151 |
– |
continue; /* wrong side */ |
152 |
– |
|
153 |
– |
rayvalue(&lr); /* compute light ray value */ |
154 |
– |
|
155 |
– |
if (intens(lr.rcol) <= FTINY) |
156 |
– |
continue; /* didn't hit light source */ |
324 |
|
|
325 |
< |
if (ldot > FTINY && rdiff > FTINY) { |
326 |
< |
/* |
327 |
< |
* Compute and add diffuse component to returned color. |
328 |
< |
* The diffuse component will always be modified by the |
329 |
< |
* color of the material. |
330 |
< |
*/ |
331 |
< |
copycolor(ctmp, lr.rcol); |
332 |
< |
dtmp = ldot * omega * rdiff / PI; |
333 |
< |
scalecolor(ctmp, dtmp); |
334 |
< |
multcolor(ctmp, mcolor); |
335 |
< |
addcolor(r->rcol, ctmp); |
336 |
< |
} |
337 |
< |
if (ldot > FTINY && rspec > FTINY && alpha2 > FTINY) { |
338 |
< |
/* |
339 |
< |
* Compute specular reflection coefficient using |
340 |
< |
* gaussian distribution model. |
341 |
< |
*/ |
342 |
< |
/* roughness + source */ |
343 |
< |
dtmp = alpha2 + omega/(2.0*PI); |
344 |
< |
/* gaussian */ |
345 |
< |
dtmp = exp((DOT(vrefl,lr.rdir)-1.)/dtmp)/(2.*PI)/dtmp; |
346 |
< |
/* worth using? */ |
347 |
< |
if (dtmp > FTINY) { |
348 |
< |
copycolor(ctmp, lr.rcol); |
349 |
< |
dtmp *= omega; |
350 |
< |
scalecolor(ctmp, dtmp); |
351 |
< |
multcolor(ctmp, scolor); |
352 |
< |
addcolor(r->rcol, ctmp); |
353 |
< |
} |
354 |
< |
} |
355 |
< |
if (ldot < -FTINY && tdiff > FTINY) { |
356 |
< |
/* |
357 |
< |
* Compute diffuse transmission. |
358 |
< |
*/ |
359 |
< |
copycolor(ctmp, lr.rcol); |
360 |
< |
dtmp = -ldot * omega * tdiff / PI; |
361 |
< |
scalecolor(ctmp, dtmp); |
362 |
< |
multcolor(ctmp, mcolor); |
363 |
< |
addcolor(r->rcol, ctmp); |
364 |
< |
} |
365 |
< |
if (ldot < -FTINY && tspec > FTINY && alpha2 > FTINY) { |
366 |
< |
/* |
367 |
< |
* Compute specular transmission. |
368 |
< |
*/ |
369 |
< |
/* roughness + source */ |
370 |
< |
dtmp = alpha2 + omega/(2.0*PI); |
371 |
< |
/* gaussian */ |
372 |
< |
dtmp = exp((DOT(r->rdir,lr.rdir)-1.)/dtmp)/(2.*PI)/dtmp; |
373 |
< |
/* worth using? */ |
374 |
< |
if (dtmp > FTINY) { |
375 |
< |
copycolor(ctmp, lr.rcol); |
376 |
< |
dtmp *= tspec * omega; |
377 |
< |
scalecolor(ctmp, dtmp); |
378 |
< |
addcolor(r->rcol, ctmp); |
379 |
< |
} |
380 |
< |
} |
325 |
> |
static |
326 |
> |
gaussamp(r, np) /* sample gaussian specular */ |
327 |
> |
RAY *r; |
328 |
> |
register NORMDAT *np; |
329 |
> |
{ |
330 |
> |
RAY sr; |
331 |
> |
FVECT u, v, h; |
332 |
> |
double rv[2]; |
333 |
> |
double d, sinp, cosp; |
334 |
> |
register int i; |
335 |
> |
/* quick test */ |
336 |
> |
if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL && |
337 |
> |
(np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN) |
338 |
> |
return; |
339 |
> |
/* set up sample coordinates */ |
340 |
> |
v[0] = v[1] = v[2] = 0.0; |
341 |
> |
for (i = 0; i < 3; i++) |
342 |
> |
if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6) |
343 |
> |
break; |
344 |
> |
v[i] = 1.0; |
345 |
> |
fcross(u, v, np->pnorm); |
346 |
> |
normalize(u); |
347 |
> |
fcross(v, np->pnorm, u); |
348 |
> |
/* compute reflection */ |
349 |
> |
if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && |
350 |
> |
rayorigin(&sr, r, SPECULAR, np->rspec) == 0) { |
351 |
> |
dimlist[ndims++] = (int)np->mp; |
352 |
> |
d = urand(ilhash(dimlist,ndims)+samplendx); |
353 |
> |
multisamp(rv, 2, d); |
354 |
> |
d = 2.0*PI * rv[0]; |
355 |
> |
cosp = cos(d); |
356 |
> |
sinp = sin(d); |
357 |
> |
rv[1] = 1.0 - specjitter*rv[1]; |
358 |
> |
if (rv[1] <= FTINY) |
359 |
> |
d = 1.0; |
360 |
> |
else |
361 |
> |
d = sqrt( np->alpha2 * -log(rv[1]) ); |
362 |
> |
for (i = 0; i < 3; i++) |
363 |
> |
h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]); |
364 |
> |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
365 |
> |
for (i = 0; i < 3; i++) |
366 |
> |
sr.rdir[i] = r->rdir[i] + d*h[i]; |
367 |
> |
if (DOT(sr.rdir, r->ron) <= FTINY) |
368 |
> |
VCOPY(sr.rdir, np->vrefl); /* jitter no good */ |
369 |
> |
rayvalue(&sr); |
370 |
> |
multcolor(sr.rcol, np->scolor); |
371 |
> |
addcolor(r->rcol, sr.rcol); |
372 |
> |
ndims--; |
373 |
> |
} |
374 |
> |
/* compute transmission */ |
375 |
> |
if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && |
376 |
> |
rayorigin(&sr, r, SPECULAR, np->tspec) == 0) { |
377 |
> |
dimlist[ndims++] = (int)np->mp; |
378 |
> |
d = urand(ilhash(dimlist,ndims)+1823+samplendx); |
379 |
> |
multisamp(rv, 2, d); |
380 |
> |
d = 2.0*PI * rv[0]; |
381 |
> |
cosp = cos(d); |
382 |
> |
sinp = sin(d); |
383 |
> |
rv[1] = 1.0 - specjitter*rv[1]; |
384 |
> |
if (rv[1] <= FTINY) |
385 |
> |
d = 1.0; |
386 |
> |
else |
387 |
> |
d = sqrt( -log(rv[1]) * np->alpha2 ); |
388 |
> |
for (i = 0; i < 3; i++) |
389 |
> |
sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]); |
390 |
> |
if (DOT(sr.rdir, r->ron) < -FTINY) |
391 |
> |
normalize(sr.rdir); /* OK, normalize */ |
392 |
> |
else |
393 |
> |
VCOPY(sr.rdir, np->prdir); /* else no jitter */ |
394 |
> |
rayvalue(&sr); |
395 |
> |
scalecolor(sr.rcol, np->tspec); |
396 |
> |
multcolor(sr.rcol, np->mcolor); /* modified by color */ |
397 |
> |
addcolor(r->rcol, sr.rcol); |
398 |
> |
ndims--; |
399 |
|
} |
400 |
|
} |