1 |
– |
/* Copyright (c) 1986 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* normal.c - shading function for normal materials. |
6 |
|
* |
8 |
|
* 12/19/85 - added stuff for metals. |
9 |
|
* 6/26/87 - improved specular model. |
10 |
|
* 9/28/87 - added model for translucent materials. |
11 |
+ |
* Later changes described in delta comments. |
12 |
|
*/ |
13 |
|
|
14 |
< |
#include "ray.h" |
14 |
> |
#include "copyright.h" |
15 |
|
|
16 |
+ |
#include "ray.h" |
17 |
+ |
#include "ambient.h" |
18 |
|
#include "source.h" |
19 |
– |
|
19 |
|
#include "otypes.h" |
20 |
+ |
#include "rtotypes.h" |
21 |
+ |
#include "random.h" |
22 |
+ |
#include "pmapmat.h" |
23 |
|
|
24 |
+ |
#ifndef MAXITER |
25 |
+ |
#define MAXITER 10 /* maximum # specular ray attempts */ |
26 |
+ |
#endif |
27 |
+ |
/* estimate of Fresnel function */ |
28 |
+ |
#define FRESNE(ci) (exp(-5.85*(ci)) - 0.00287989916) |
29 |
+ |
#define FRESTHRESH 0.017999 /* minimum specularity for approx. */ |
30 |
+ |
|
31 |
+ |
|
32 |
|
/* |
33 |
< |
* This routine uses portions of the reflection |
34 |
< |
* model described by Cook and Torrance. |
25 |
< |
* The computation of specular components has been simplified by |
26 |
< |
* numerous approximations and ommisions to improve speed. |
33 |
> |
* This routine implements the isotropic Gaussian |
34 |
> |
* model described by Ward in Siggraph `92 article. |
35 |
|
* We orient the surface towards the incoming ray, so a single |
36 |
|
* surface can be used to represent an infinitely thin object. |
37 |
|
* |
42 |
|
* red grn blu rspec rough trans tspec |
43 |
|
*/ |
44 |
|
|
45 |
< |
#define BSPEC(m) (6.0) /* specularity parameter b */ |
45 |
> |
/* specularity flags */ |
46 |
> |
#define SP_REFL 01 /* has reflected specular component */ |
47 |
> |
#define SP_TRAN 02 /* has transmitted specular */ |
48 |
> |
#define SP_PURE 04 /* purely specular (zero roughness) */ |
49 |
> |
#define SP_FLAT 010 /* flat reflecting surface */ |
50 |
> |
#define SP_RBLT 020 /* reflection below sample threshold */ |
51 |
> |
#define SP_TBLT 040 /* transmission below threshold */ |
52 |
|
|
53 |
< |
|
54 |
< |
m_normal(m, r) /* color a ray which hit something normal */ |
55 |
< |
register OBJREC *m; |
56 |
< |
register RAY *r; |
43 |
< |
{ |
44 |
< |
double exp(); |
53 |
> |
typedef struct { |
54 |
> |
OBJREC *mp; /* material pointer */ |
55 |
> |
RAY *rp; /* ray pointer */ |
56 |
> |
short specfl; /* specularity flags, defined above */ |
57 |
|
COLOR mcolor; /* color of this material */ |
58 |
|
COLOR scolor; /* color of specular component */ |
59 |
|
FVECT vrefl; /* vector in direction of reflected ray */ |
60 |
< |
double alpha2; /* roughness squared times 2 */ |
61 |
< |
RAY lr; /* ray to illumination source */ |
60 |
> |
FVECT prdir; /* vector in transmitted direction */ |
61 |
> |
double alpha2; /* roughness squared */ |
62 |
|
double rdiff, rspec; /* reflected specular, diffuse */ |
63 |
|
double trans; /* transmissivity */ |
64 |
|
double tdiff, tspec; /* transmitted specular, diffuse */ |
65 |
|
FVECT pnorm; /* perturbed surface normal */ |
66 |
|
double pdot; /* perturbed dot product */ |
67 |
+ |
} NORMDAT; /* normal material data */ |
68 |
+ |
|
69 |
+ |
static void gaussamp(NORMDAT *np); |
70 |
+ |
|
71 |
+ |
|
72 |
+ |
static void |
73 |
+ |
dirnorm( /* compute source contribution */ |
74 |
+ |
COLOR cval, /* returned coefficient */ |
75 |
+ |
void *nnp, /* material data */ |
76 |
+ |
FVECT ldir, /* light source direction */ |
77 |
+ |
double omega /* light source size */ |
78 |
+ |
) |
79 |
+ |
{ |
80 |
+ |
NORMDAT *np = nnp; |
81 |
|
double ldot; |
82 |
< |
double omega; |
83 |
< |
double dtmp; |
82 |
> |
double lrdiff, ltdiff; |
83 |
> |
double dtmp, d2, d3, d4; |
84 |
> |
FVECT vtmp; |
85 |
|
COLOR ctmp; |
59 |
– |
register int i; |
86 |
|
|
87 |
< |
if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) |
88 |
< |
objerror(m, USER, "bad # arguments"); |
87 |
> |
setcolor(cval, 0.0, 0.0, 0.0); |
88 |
> |
|
89 |
> |
ldot = DOT(np->pnorm, ldir); |
90 |
> |
|
91 |
> |
if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY) |
92 |
> |
return; /* wrong side */ |
93 |
> |
|
94 |
> |
/* Fresnel estimate */ |
95 |
> |
lrdiff = np->rdiff; |
96 |
> |
ltdiff = np->tdiff; |
97 |
> |
if (np->specfl & SP_PURE && np->rspec >= FRESTHRESH && |
98 |
> |
(lrdiff > FTINY) | (ltdiff > FTINY)) { |
99 |
> |
dtmp = 1. - FRESNE(fabs(ldot)); |
100 |
> |
lrdiff *= dtmp; |
101 |
> |
ltdiff *= dtmp; |
102 |
> |
} |
103 |
> |
|
104 |
> |
if (ldot > FTINY && lrdiff > FTINY) { |
105 |
> |
/* |
106 |
> |
* Compute and add diffuse reflected component to returned |
107 |
> |
* color. The diffuse reflected component will always be |
108 |
> |
* modified by the color of the material. |
109 |
> |
*/ |
110 |
> |
copycolor(ctmp, np->mcolor); |
111 |
> |
dtmp = ldot * omega * lrdiff * (1.0/PI); |
112 |
> |
scalecolor(ctmp, dtmp); |
113 |
> |
addcolor(cval, ctmp); |
114 |
> |
} |
115 |
> |
|
116 |
> |
if (ldot < -FTINY && ltdiff > FTINY) { |
117 |
> |
/* |
118 |
> |
* Compute diffuse transmission. |
119 |
> |
*/ |
120 |
> |
copycolor(ctmp, np->mcolor); |
121 |
> |
dtmp = -ldot * omega * ltdiff * (1.0/PI); |
122 |
> |
scalecolor(ctmp, dtmp); |
123 |
> |
addcolor(cval, ctmp); |
124 |
> |
} |
125 |
> |
|
126 |
> |
if (ambRayInPmap(np->rp)) |
127 |
> |
return; /* specular already in photon map */ |
128 |
> |
|
129 |
> |
if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) { |
130 |
> |
/* |
131 |
> |
* Compute specular reflection coefficient using |
132 |
> |
* Gaussian distribution model. |
133 |
> |
*/ |
134 |
> |
/* roughness */ |
135 |
> |
dtmp = np->alpha2; |
136 |
> |
/* + source if flat */ |
137 |
> |
if (np->specfl & SP_FLAT) |
138 |
> |
dtmp += omega * (0.25/PI); |
139 |
> |
/* half vector */ |
140 |
> |
VSUB(vtmp, ldir, np->rp->rdir); |
141 |
> |
d2 = DOT(vtmp, np->pnorm); |
142 |
> |
d2 *= d2; |
143 |
> |
d3 = DOT(vtmp,vtmp); |
144 |
> |
d4 = (d3 - d2) / d2; |
145 |
> |
/* new W-G-M-D model */ |
146 |
> |
dtmp = exp(-d4/dtmp) * d3 / (PI * d2*d2 * dtmp); |
147 |
> |
/* worth using? */ |
148 |
> |
if (dtmp > FTINY) { |
149 |
> |
copycolor(ctmp, np->scolor); |
150 |
> |
dtmp *= ldot * omega; |
151 |
> |
scalecolor(ctmp, dtmp); |
152 |
> |
addcolor(cval, ctmp); |
153 |
> |
} |
154 |
> |
} |
155 |
> |
|
156 |
> |
|
157 |
> |
if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) { |
158 |
> |
/* |
159 |
> |
* Compute specular transmission. Specular transmission |
160 |
> |
* is always modified by material color. |
161 |
> |
*/ |
162 |
> |
/* roughness + source */ |
163 |
> |
dtmp = np->alpha2 + omega*(1.0/PI); |
164 |
> |
/* Gaussian */ |
165 |
> |
dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp); |
166 |
> |
/* worth using? */ |
167 |
> |
if (dtmp > FTINY) { |
168 |
> |
copycolor(ctmp, np->mcolor); |
169 |
> |
dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot); |
170 |
> |
scalecolor(ctmp, dtmp); |
171 |
> |
addcolor(cval, ctmp); |
172 |
> |
} |
173 |
> |
} |
174 |
> |
} |
175 |
> |
|
176 |
> |
|
177 |
> |
int |
178 |
> |
m_normal( /* color a ray that hit something normal */ |
179 |
> |
OBJREC *m, |
180 |
> |
RAY *r |
181 |
> |
) |
182 |
> |
{ |
183 |
> |
NORMDAT nd; |
184 |
> |
double fest; |
185 |
> |
double transtest, transdist; |
186 |
> |
double mirtest, mirdist; |
187 |
> |
int hastexture; |
188 |
> |
double d; |
189 |
> |
COLOR ctmp; |
190 |
> |
int i; |
191 |
> |
|
192 |
> |
/* PMAP: skip transmitted shadow ray if accounted for in photon map */ |
193 |
> |
if (shadowRayInPmap(r)) |
194 |
> |
return(1); |
195 |
|
/* easy shadow test */ |
196 |
|
if (r->crtype & SHADOW && m->otype != MAT_TRANS) |
197 |
< |
return; |
197 |
> |
return(1); |
198 |
> |
|
199 |
> |
if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) |
200 |
> |
objerror(m, USER, "bad number of arguments"); |
201 |
> |
/* check for back side */ |
202 |
> |
if (r->rod < 0.0) { |
203 |
> |
if (!backvis) { |
204 |
> |
raytrans(r); |
205 |
> |
return(1); |
206 |
> |
} |
207 |
> |
raytexture(r, m->omod); |
208 |
> |
flipsurface(r); /* reorient if backvis */ |
209 |
> |
} else |
210 |
> |
raytexture(r, m->omod); |
211 |
> |
nd.mp = m; |
212 |
> |
nd.rp = r; |
213 |
|
/* get material color */ |
214 |
< |
setcolor(mcolor, m->oargs.farg[0], |
214 |
> |
setcolor(nd.mcolor, m->oargs.farg[0], |
215 |
|
m->oargs.farg[1], |
216 |
|
m->oargs.farg[2]); |
217 |
|
/* get roughness */ |
218 |
< |
alpha2 = m->oargs.farg[4]; |
219 |
< |
alpha2 *= 2.0 * alpha2; |
220 |
< |
/* reorient if necessary */ |
221 |
< |
if (r->rod < 0.0) |
75 |
< |
flipsurface(r); |
76 |
< |
/* get modifiers */ |
77 |
< |
raytexture(r, m->omod); |
78 |
< |
pdot = raynormal(pnorm, r); /* perturb normal */ |
79 |
< |
multcolor(mcolor, r->pcol); /* modify material color */ |
80 |
< |
/* get specular component */ |
81 |
< |
rspec = m->oargs.farg[3]; |
218 |
> |
nd.specfl = 0; |
219 |
> |
nd.alpha2 = m->oargs.farg[4]; |
220 |
> |
if ((nd.alpha2 *= nd.alpha2) <= FTINY) |
221 |
> |
nd.specfl |= SP_PURE; |
222 |
|
|
223 |
< |
if (rspec > FTINY) { /* has specular component */ |
224 |
< |
/* compute specular color */ |
225 |
< |
if (m->otype == MAT_METAL) |
226 |
< |
copycolor(scolor, mcolor); |
227 |
< |
else |
88 |
< |
setcolor(scolor, 1.0, 1.0, 1.0); |
89 |
< |
scalecolor(scolor, rspec); |
90 |
< |
/* improved model */ |
91 |
< |
dtmp = exp(-BSPEC(m)*pdot); |
92 |
< |
for (i = 0; i < 3; i++) |
93 |
< |
colval(scolor,i) += (1.0-colval(scolor,i))*dtmp; |
94 |
< |
rspec += (1.0-rspec)*dtmp; |
95 |
< |
/* compute reflected ray */ |
96 |
< |
for (i = 0; i < 3; i++) |
97 |
< |
vrefl[i] = r->rdir[i] + 2.0*pdot*pnorm[i]; |
98 |
< |
|
99 |
< |
if (alpha2 <= FTINY && !(r->crtype & SHADOW)) |
100 |
< |
if (rayorigin(&lr, r, REFLECTED, rspec) == 0) { |
101 |
< |
VCOPY(lr.rdir, vrefl); |
102 |
< |
rayvalue(&lr); |
103 |
< |
multcolor(lr.rcol, scolor); |
104 |
< |
addcolor(r->rcol, lr.rcol); |
105 |
< |
} |
223 |
> |
if ( (hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY)) ) { |
224 |
> |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
225 |
> |
} else { |
226 |
> |
VCOPY(nd.pnorm, r->ron); |
227 |
> |
nd.pdot = r->rod; |
228 |
|
} |
229 |
< |
|
229 |
> |
if (r->ro != NULL && isflat(r->ro->otype)) |
230 |
> |
nd.specfl |= SP_FLAT; |
231 |
> |
if (nd.pdot < .001) |
232 |
> |
nd.pdot = .001; /* non-zero for dirnorm() */ |
233 |
> |
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
234 |
> |
mirtest = transtest = 0; |
235 |
> |
mirdist = transdist = r->rot; |
236 |
> |
nd.rspec = m->oargs.farg[3]; |
237 |
> |
/* compute Fresnel approx. */ |
238 |
> |
if (nd.specfl & SP_PURE && nd.rspec >= FRESTHRESH) { |
239 |
> |
fest = FRESNE(nd.pdot); |
240 |
> |
nd.rspec += fest*(1. - nd.rspec); |
241 |
> |
} else |
242 |
> |
fest = 0.; |
243 |
> |
/* compute transmission */ |
244 |
|
if (m->otype == MAT_TRANS) { |
245 |
< |
trans = m->oargs.farg[5]*(1.0 - rspec); |
246 |
< |
tspec = trans * m->oargs.farg[6]; |
247 |
< |
tdiff = trans - tspec; |
245 |
> |
nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec); |
246 |
> |
nd.tspec = nd.trans * m->oargs.farg[6]; |
247 |
> |
nd.tdiff = nd.trans - nd.tspec; |
248 |
> |
if (nd.tspec > FTINY) { |
249 |
> |
nd.specfl |= SP_TRAN; |
250 |
> |
/* check threshold */ |
251 |
> |
if (!(nd.specfl & SP_PURE) && |
252 |
> |
specthresh >= nd.tspec-FTINY) |
253 |
> |
nd.specfl |= SP_TBLT; |
254 |
> |
if (!hastexture || r->crtype & (SHADOW|AMBIENT)) { |
255 |
> |
VCOPY(nd.prdir, r->rdir); |
256 |
> |
transtest = 2; |
257 |
> |
} else { |
258 |
> |
for (i = 0; i < 3; i++) /* perturb */ |
259 |
> |
nd.prdir[i] = r->rdir[i] - r->pert[i]; |
260 |
> |
if (DOT(nd.prdir, r->ron) < -FTINY) |
261 |
> |
normalize(nd.prdir); /* OK */ |
262 |
> |
else |
263 |
> |
VCOPY(nd.prdir, r->rdir); |
264 |
> |
} |
265 |
> |
} |
266 |
|
} else |
267 |
< |
tdiff = tspec = trans = 0.0; |
267 |
> |
nd.tdiff = nd.tspec = nd.trans = 0.0; |
268 |
|
/* transmitted ray */ |
269 |
< |
if (tspec > FTINY && alpha2 <= FTINY) |
270 |
< |
if (rayorigin(&lr, r, TRANS, tspec) == 0) { |
271 |
< |
VCOPY(lr.rdir, r->rdir); |
269 |
> |
|
270 |
> |
if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) { |
271 |
> |
RAY lr; |
272 |
> |
copycolor(lr.rcoef, nd.mcolor); /* modified by color */ |
273 |
> |
scalecolor(lr.rcoef, nd.tspec); |
274 |
> |
if (rayorigin(&lr, TRANS, r, lr.rcoef) == 0) { |
275 |
> |
VCOPY(lr.rdir, nd.prdir); |
276 |
|
rayvalue(&lr); |
277 |
< |
scalecolor(lr.rcol, tspec); |
277 |
> |
multcolor(lr.rcol, lr.rcoef); |
278 |
|
addcolor(r->rcol, lr.rcol); |
279 |
+ |
transtest *= bright(lr.rcol); |
280 |
+ |
transdist = r->rot + lr.rt; |
281 |
|
} |
282 |
< |
if (r->crtype & SHADOW) /* the rest is shadow */ |
283 |
< |
return; |
282 |
> |
} else |
283 |
> |
transtest = 0; |
284 |
> |
|
285 |
> |
if (r->crtype & SHADOW) { /* the rest is shadow */ |
286 |
> |
r->rt = transdist; |
287 |
> |
return(1); |
288 |
> |
} |
289 |
> |
/* get specular reflection */ |
290 |
> |
if (nd.rspec > FTINY) { |
291 |
> |
nd.specfl |= SP_REFL; |
292 |
> |
/* compute specular color */ |
293 |
> |
if (m->otype != MAT_METAL) { |
294 |
> |
setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec); |
295 |
> |
} else if (fest > FTINY) { |
296 |
> |
d = m->oargs.farg[3]*(1. - fest); |
297 |
> |
for (i = 0; i < 3; i++) |
298 |
> |
colval(nd.scolor,i) = fest + |
299 |
> |
colval(nd.mcolor,i)*d; |
300 |
> |
} else { |
301 |
> |
copycolor(nd.scolor, nd.mcolor); |
302 |
> |
scalecolor(nd.scolor, nd.rspec); |
303 |
> |
} |
304 |
> |
/* check threshold */ |
305 |
> |
if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY) |
306 |
> |
nd.specfl |= SP_RBLT; |
307 |
> |
/* compute reflected ray */ |
308 |
> |
VSUM(nd.vrefl, r->rdir, nd.pnorm, 2.*nd.pdot); |
309 |
> |
/* penetration? */ |
310 |
> |
if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY) |
311 |
> |
VSUM(nd.vrefl, r->rdir, r->ron, 2.*r->rod); |
312 |
> |
checknorm(nd.vrefl); |
313 |
> |
} |
314 |
> |
/* reflected ray */ |
315 |
> |
if ((nd.specfl&(SP_REFL|SP_PURE|SP_RBLT)) == (SP_REFL|SP_PURE)) { |
316 |
> |
RAY lr; |
317 |
> |
if (rayorigin(&lr, REFLECTED, r, nd.scolor) == 0) { |
318 |
> |
VCOPY(lr.rdir, nd.vrefl); |
319 |
> |
rayvalue(&lr); |
320 |
> |
multcolor(lr.rcol, lr.rcoef); |
321 |
> |
addcolor(r->rcol, lr.rcol); |
322 |
> |
if (nd.specfl & SP_FLAT && |
323 |
> |
!hastexture | (r->crtype & AMBIENT)) { |
324 |
> |
mirtest = 2.*bright(lr.rcol); |
325 |
> |
mirdist = r->rot + lr.rt; |
326 |
> |
} |
327 |
> |
} |
328 |
> |
} |
329 |
|
/* diffuse reflection */ |
330 |
< |
rdiff = 1.0 - trans - rspec; |
330 |
> |
nd.rdiff = 1.0 - nd.trans - nd.rspec; |
331 |
|
|
332 |
< |
if (rdiff <= FTINY && tdiff <= FTINY && alpha2 <= FTINY) |
333 |
< |
return; /* purely specular */ |
332 |
> |
if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) |
333 |
> |
return(1); /* 100% pure specular */ |
334 |
|
|
335 |
< |
if (rdiff > FTINY) { /* ambient from this side */ |
336 |
< |
ambient(ctmp, r); |
337 |
< |
if (alpha2 <= FTINY) |
338 |
< |
scalecolor(ctmp, rdiff); |
339 |
< |
else |
340 |
< |
scalecolor(ctmp, 1.0-trans); |
341 |
< |
multcolor(ctmp, mcolor); /* modified by material color */ |
335 |
> |
if (!(nd.specfl & SP_PURE)) |
336 |
> |
gaussamp(&nd); /* checks *BLT flags */ |
337 |
> |
|
338 |
> |
if (nd.rdiff > FTINY) { /* ambient from this side */ |
339 |
> |
copycolor(ctmp, nd.mcolor); /* modified by material color */ |
340 |
> |
scalecolor(ctmp, nd.rdiff); |
341 |
> |
if (nd.specfl & SP_RBLT) /* add in specular as well? */ |
342 |
> |
addcolor(ctmp, nd.scolor); |
343 |
> |
multambient(ctmp, r, hastexture ? nd.pnorm : r->ron); |
344 |
|
addcolor(r->rcol, ctmp); /* add to returned color */ |
345 |
|
} |
346 |
< |
if (tdiff > FTINY) { /* ambient from other side */ |
347 |
< |
flipsurface(r); |
348 |
< |
ambient(ctmp, r); |
349 |
< |
if (alpha2 <= FTINY) |
143 |
< |
scalecolor(ctmp, tdiff); |
346 |
> |
if (nd.tdiff > FTINY) { /* ambient from other side */ |
347 |
> |
copycolor(ctmp, nd.mcolor); /* modified by color */ |
348 |
> |
if (nd.specfl & SP_TBLT) |
349 |
> |
scalecolor(ctmp, nd.trans); |
350 |
|
else |
351 |
< |
scalecolor(ctmp, trans); |
352 |
< |
multcolor(ctmp, mcolor); |
351 |
> |
scalecolor(ctmp, nd.tdiff); |
352 |
> |
flipsurface(r); |
353 |
> |
if (hastexture) { |
354 |
> |
FVECT bnorm; |
355 |
> |
bnorm[0] = -nd.pnorm[0]; |
356 |
> |
bnorm[1] = -nd.pnorm[1]; |
357 |
> |
bnorm[2] = -nd.pnorm[2]; |
358 |
> |
multambient(ctmp, r, bnorm); |
359 |
> |
} else |
360 |
> |
multambient(ctmp, r, r->ron); |
361 |
|
addcolor(r->rcol, ctmp); |
362 |
|
flipsurface(r); |
363 |
|
} |
364 |
< |
|
365 |
< |
for (i = 0; i < nsources; i++) { /* add specular and diffuse */ |
364 |
> |
/* add direct component */ |
365 |
> |
direct(r, dirnorm, &nd); |
366 |
> |
/* check distance */ |
367 |
> |
d = bright(r->rcol); |
368 |
> |
if (transtest > d) |
369 |
> |
r->rt = transdist; |
370 |
> |
else if (mirtest > d) |
371 |
> |
r->rt = mirdist; |
372 |
|
|
373 |
< |
if ((omega = srcray(&lr, r, i)) == 0.0) |
374 |
< |
continue; /* bad source */ |
373 |
> |
return(1); |
374 |
> |
} |
375 |
|
|
156 |
– |
ldot = DOT(pnorm, lr.rdir); |
157 |
– |
|
158 |
– |
if (ldot < 0.0 ? trans <= FTINY : trans >= 1.0-FTINY) |
159 |
– |
continue; /* wrong side */ |
160 |
– |
|
161 |
– |
rayvalue(&lr); /* compute light ray value */ |
162 |
– |
|
163 |
– |
if (intens(lr.rcol) <= FTINY) |
164 |
– |
continue; /* didn't hit light source */ |
376 |
|
|
377 |
< |
if (ldot > FTINY && rdiff > FTINY) { |
378 |
< |
/* |
379 |
< |
* Compute and add diffuse component to returned color. |
380 |
< |
* The diffuse component will always be modified by the |
381 |
< |
* color of the material. |
382 |
< |
*/ |
383 |
< |
copycolor(ctmp, lr.rcol); |
384 |
< |
dtmp = ldot * omega * rdiff / PI; |
385 |
< |
scalecolor(ctmp, dtmp); |
386 |
< |
multcolor(ctmp, mcolor); |
387 |
< |
addcolor(r->rcol, ctmp); |
377 |
> |
static void |
378 |
> |
gaussamp( /* sample Gaussian specular */ |
379 |
> |
NORMDAT *np |
380 |
> |
) |
381 |
> |
{ |
382 |
> |
RAY sr; |
383 |
> |
FVECT u, v, h; |
384 |
> |
double rv[2]; |
385 |
> |
double d, sinp, cosp; |
386 |
> |
COLOR scol; |
387 |
> |
int maxiter, ntrials, nstarget, nstaken; |
388 |
> |
int i; |
389 |
> |
/* quick test */ |
390 |
> |
if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL && |
391 |
> |
(np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN) |
392 |
> |
return; |
393 |
> |
/* set up sample coordinates */ |
394 |
> |
getperpendicular(u, np->pnorm, rand_samp); |
395 |
> |
fcross(v, np->pnorm, u); |
396 |
> |
/* compute reflection */ |
397 |
> |
if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && |
398 |
> |
rayorigin(&sr, SPECULAR, np->rp, np->scolor) == 0) { |
399 |
> |
nstarget = 1; |
400 |
> |
if (specjitter > 1.5) { /* multiple samples? */ |
401 |
> |
nstarget = specjitter*np->rp->rweight + .5; |
402 |
> |
if (sr.rweight <= minweight*nstarget) |
403 |
> |
nstarget = sr.rweight/minweight; |
404 |
> |
if (nstarget > 1) { |
405 |
> |
d = 1./nstarget; |
406 |
> |
scalecolor(sr.rcoef, d); |
407 |
> |
sr.rweight *= d; |
408 |
> |
} else |
409 |
> |
nstarget = 1; |
410 |
|
} |
411 |
< |
if (ldot > FTINY && rspec > FTINY && alpha2 > FTINY) { |
412 |
< |
/* |
413 |
< |
* Compute specular reflection coefficient using |
414 |
< |
* gaussian distribution model. |
415 |
< |
*/ |
416 |
< |
/* roughness + source */ |
417 |
< |
dtmp = alpha2 + omega/(2.0*PI); |
418 |
< |
/* gaussian */ |
419 |
< |
dtmp = exp((DOT(vrefl,lr.rdir)-1.)/dtmp)/(2.*PI)/dtmp; |
420 |
< |
/* worth using? */ |
421 |
< |
if (dtmp > FTINY) { |
422 |
< |
copycolor(ctmp, lr.rcol); |
423 |
< |
dtmp *= omega; |
424 |
< |
scalecolor(ctmp, dtmp); |
425 |
< |
multcolor(ctmp, scolor); |
426 |
< |
addcolor(r->rcol, ctmp); |
411 |
> |
setcolor(scol, 0., 0., 0.); |
412 |
> |
dimlist[ndims++] = (int)(size_t)np->mp; |
413 |
> |
maxiter = MAXITER*nstarget; |
414 |
> |
for (nstaken = ntrials = 0; nstaken < nstarget && |
415 |
> |
ntrials < maxiter; ntrials++) { |
416 |
> |
if (ntrials) |
417 |
> |
d = frandom(); |
418 |
> |
else |
419 |
> |
d = urand(ilhash(dimlist,ndims)+samplendx); |
420 |
> |
multisamp(rv, 2, d); |
421 |
> |
d = 2.0*PI * rv[0]; |
422 |
> |
cosp = tcos(d); |
423 |
> |
sinp = tsin(d); |
424 |
> |
if ((0. <= specjitter) & (specjitter < 1.)) |
425 |
> |
rv[1] = 1.0 - specjitter*rv[1]; |
426 |
> |
if (rv[1] <= FTINY) |
427 |
> |
d = 1.0; |
428 |
> |
else |
429 |
> |
d = sqrt( np->alpha2 * -log(rv[1]) ); |
430 |
> |
for (i = 0; i < 3; i++) |
431 |
> |
h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]); |
432 |
> |
d = -2.0 * DOT(h, np->rp->rdir) / (1.0 + d*d); |
433 |
> |
VSUM(sr.rdir, np->rp->rdir, h, d); |
434 |
> |
/* sample rejection test */ |
435 |
> |
if ((d = DOT(sr.rdir, np->rp->ron)) <= FTINY) |
436 |
> |
continue; |
437 |
> |
checknorm(sr.rdir); |
438 |
> |
if (nstarget > 1) { /* W-G-M-D adjustment */ |
439 |
> |
if (nstaken) rayclear(&sr); |
440 |
> |
rayvalue(&sr); |
441 |
> |
d = 2./(1. + np->rp->rod/d); |
442 |
> |
scalecolor(sr.rcol, d); |
443 |
> |
addcolor(scol, sr.rcol); |
444 |
> |
} else { |
445 |
> |
rayvalue(&sr); |
446 |
> |
multcolor(sr.rcol, sr.rcoef); |
447 |
> |
addcolor(np->rp->rcol, sr.rcol); |
448 |
|
} |
449 |
+ |
++nstaken; |
450 |
|
} |
451 |
< |
if (ldot < -FTINY && tdiff > FTINY) { |
452 |
< |
/* |
453 |
< |
* Compute diffuse transmission. |
454 |
< |
*/ |
455 |
< |
copycolor(ctmp, lr.rcol); |
201 |
< |
dtmp = -ldot * omega * tdiff / PI; |
202 |
< |
scalecolor(ctmp, dtmp); |
203 |
< |
multcolor(ctmp, mcolor); |
204 |
< |
addcolor(r->rcol, ctmp); |
451 |
> |
if (nstarget > 1) { /* final W-G-M-D weighting */ |
452 |
> |
multcolor(scol, sr.rcoef); |
453 |
> |
d = (double)nstarget/ntrials; |
454 |
> |
scalecolor(scol, d); |
455 |
> |
addcolor(np->rp->rcol, scol); |
456 |
|
} |
457 |
< |
if (ldot < -FTINY && tspec > FTINY && alpha2 > FTINY) { |
458 |
< |
/* |
459 |
< |
* Compute specular transmission. |
460 |
< |
*/ |
461 |
< |
/* roughness + source */ |
462 |
< |
dtmp = alpha2 + omega/(2.0*PI); |
463 |
< |
/* gaussian */ |
464 |
< |
dtmp = exp((DOT(r->rdir,lr.rdir)-1.)/dtmp)/(2.*PI)/dtmp; |
465 |
< |
/* worth using? */ |
466 |
< |
if (dtmp > FTINY) { |
467 |
< |
copycolor(ctmp, lr.rcol); |
468 |
< |
dtmp *= tspec * omega; |
469 |
< |
scalecolor(ctmp, dtmp); |
470 |
< |
addcolor(r->rcol, ctmp); |
471 |
< |
} |
457 |
> |
ndims--; |
458 |
> |
} |
459 |
> |
/* compute transmission */ |
460 |
> |
copycolor(sr.rcoef, np->mcolor); /* modified by color */ |
461 |
> |
scalecolor(sr.rcoef, np->tspec); |
462 |
> |
if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && |
463 |
> |
rayorigin(&sr, SPECULAR, np->rp, sr.rcoef) == 0) { |
464 |
> |
nstarget = 1; |
465 |
> |
if (specjitter > 1.5) { /* multiple samples? */ |
466 |
> |
nstarget = specjitter*np->rp->rweight + .5; |
467 |
> |
if (sr.rweight <= minweight*nstarget) |
468 |
> |
nstarget = sr.rweight/minweight; |
469 |
> |
if (nstarget > 1) { |
470 |
> |
d = 1./nstarget; |
471 |
> |
scalecolor(sr.rcoef, d); |
472 |
> |
sr.rweight *= d; |
473 |
> |
} else |
474 |
> |
nstarget = 1; |
475 |
|
} |
476 |
+ |
dimlist[ndims++] = (int)(size_t)np->mp; |
477 |
+ |
maxiter = MAXITER*nstarget; |
478 |
+ |
for (nstaken = ntrials = 0; nstaken < nstarget && |
479 |
+ |
ntrials < maxiter; ntrials++) { |
480 |
+ |
if (ntrials) |
481 |
+ |
d = frandom(); |
482 |
+ |
else |
483 |
+ |
d = urand(ilhash(dimlist,ndims)+samplendx); |
484 |
+ |
multisamp(rv, 2, d); |
485 |
+ |
d = 2.0*PI * rv[0]; |
486 |
+ |
cosp = tcos(d); |
487 |
+ |
sinp = tsin(d); |
488 |
+ |
if ((0. <= specjitter) & (specjitter < 1.)) |
489 |
+ |
rv[1] = 1.0 - specjitter*rv[1]; |
490 |
+ |
if (rv[1] <= FTINY) |
491 |
+ |
d = 1.0; |
492 |
+ |
else |
493 |
+ |
d = sqrt( np->alpha2 * -log(rv[1]) ); |
494 |
+ |
for (i = 0; i < 3; i++) |
495 |
+ |
sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]); |
496 |
+ |
/* sample rejection test */ |
497 |
+ |
if (DOT(sr.rdir, np->rp->ron) >= -FTINY) |
498 |
+ |
continue; |
499 |
+ |
normalize(sr.rdir); /* OK, normalize */ |
500 |
+ |
if (nstaken) /* multi-sampling */ |
501 |
+ |
rayclear(&sr); |
502 |
+ |
rayvalue(&sr); |
503 |
+ |
multcolor(sr.rcol, sr.rcoef); |
504 |
+ |
addcolor(np->rp->rcol, sr.rcol); |
505 |
+ |
++nstaken; |
506 |
+ |
} |
507 |
+ |
ndims--; |
508 |
|
} |
509 |
|
} |