1 |
– |
/* Copyright (c) 1992 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 |
|
* |
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 |
|
#include "otypes.h" |
20 |
< |
|
20 |
> |
#include "rtotypes.h" |
21 |
|
#include "random.h" |
22 |
+ |
#include "pmapmat.h" |
23 |
|
|
24 |
< |
extern double specthresh; /* specular sampling threshold */ |
25 |
< |
extern double specjitter; /* specular sampling jitter */ |
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. |
29 |
< |
* The computation of specular components has been simplified by |
30 |
< |
* 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 |
|
|
41 |
– |
#define BSPEC(m) (6.0) /* specularity parameter b */ |
42 |
– |
|
45 |
|
/* specularity flags */ |
46 |
|
#define SP_REFL 01 /* has reflected specular component */ |
47 |
|
#define SP_TRAN 02 /* has transmitted specular */ |
66 |
|
double pdot; /* perturbed dot product */ |
67 |
|
} NORMDAT; /* normal material data */ |
68 |
|
|
69 |
+ |
static void gaussamp(NORMDAT *np); |
70 |
|
|
71 |
< |
dirnorm(cval, np, ldir, omega) /* compute source contribution */ |
72 |
< |
COLOR cval; /* returned coefficient */ |
73 |
< |
register NORMDAT *np; /* material data */ |
74 |
< |
FVECT ldir; /* light source direction */ |
75 |
< |
double omega; /* light source size */ |
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 dtmp, d2; |
82 |
> |
double lrdiff, ltdiff; |
83 |
> |
double dtmp, d2, d3, d4; |
84 |
|
FVECT vtmp; |
77 |
– |
register int i; |
85 |
|
COLOR ctmp; |
86 |
|
|
87 |
|
setcolor(cval, 0.0, 0.0, 0.0); |
91 |
|
if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY) |
92 |
|
return; /* wrong side */ |
93 |
|
|
94 |
< |
if (ldot > FTINY && np->rdiff > FTINY) { |
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 * np->rdiff / PI; |
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. |
132 |
> |
* Gaussian distribution model. |
133 |
|
*/ |
134 |
|
/* roughness */ |
135 |
|
dtmp = np->alpha2; |
136 |
|
/* + source if flat */ |
137 |
|
if (np->specfl & SP_FLAT) |
138 |
< |
dtmp += omega/(4.0*PI); |
139 |
< |
/* delta */ |
140 |
< |
for (i = 0; i < 3; i++) |
110 |
< |
vtmp[i] = ldir[i] - np->rp->rdir[i]; |
138 |
> |
dtmp += omega * (0.25/PI); |
139 |
> |
/* half vector */ |
140 |
> |
VSUB(vtmp, ldir, np->rp->rdir); |
141 |
|
d2 = DOT(vtmp, np->pnorm); |
142 |
< |
d2 = 2.0 - 2.0*d2/sqrt(DOT(vtmp,vtmp)); |
143 |
< |
/* gaussian */ |
144 |
< |
dtmp = exp(-d2/dtmp)/(4.*PI*dtmp); |
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 *= omega * sqrt(ldot/np->pdot); |
150 |
> |
dtmp *= ldot * omega; |
151 |
|
scalecolor(ctmp, dtmp); |
152 |
|
addcolor(cval, ctmp); |
153 |
|
} |
154 |
|
} |
155 |
< |
if (ldot < -FTINY && np->tdiff > FTINY) { |
156 |
< |
/* |
125 |
< |
* Compute diffuse transmission. |
126 |
< |
*/ |
127 |
< |
copycolor(ctmp, np->mcolor); |
128 |
< |
dtmp = -ldot * omega * np->tdiff / PI; |
129 |
< |
scalecolor(ctmp, dtmp); |
130 |
< |
addcolor(cval, ctmp); |
131 |
< |
} |
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/2.0 + omega/(2.0*PI); |
164 |
< |
/* gaussian */ |
165 |
< |
dtmp = exp((DOT(np->prdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
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); |
169 |
> |
dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot); |
170 |
|
scalecolor(ctmp, dtmp); |
171 |
|
addcolor(cval, ctmp); |
172 |
|
} |
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
< |
m_normal(m, r) /* color a ray that hit something normal */ |
178 |
< |
register OBJREC *m; |
179 |
< |
register RAY *r; |
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 dtmp; |
186 |
> |
double mirtest, mirdist; |
187 |
> |
int hastexture; |
188 |
> |
double d; |
189 |
|
COLOR ctmp; |
190 |
< |
register int i; |
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 */ |
219 |
|
nd.alpha2 = m->oargs.farg[4]; |
220 |
|
if ((nd.alpha2 *= nd.alpha2) <= FTINY) |
221 |
|
nd.specfl |= SP_PURE; |
222 |
< |
/* reorient if necessary */ |
223 |
< |
if (r->rod < 0.0) |
224 |
< |
flipsurface(r); |
225 |
< |
/* get modifiers */ |
226 |
< |
raytexture(r, m->omod); |
227 |
< |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
222 |
> |
|
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 |
> |
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 |
< |
transtest = 0; |
235 |
< |
/* get specular component */ |
236 |
< |
if ((nd.rspec = m->oargs.farg[3]) > FTINY) { |
237 |
< |
nd.specfl |= SP_REFL; |
238 |
< |
/* compute specular color */ |
239 |
< |
if (m->otype == MAT_METAL) |
240 |
< |
copycolor(nd.scolor, nd.mcolor); |
241 |
< |
else |
242 |
< |
setcolor(nd.scolor, 1.0, 1.0, 1.0); |
196 |
< |
scalecolor(nd.scolor, nd.rspec); |
197 |
< |
/* improved model */ |
198 |
< |
dtmp = exp(-BSPEC(m)*nd.pdot); |
199 |
< |
for (i = 0; i < 3; i++) |
200 |
< |
colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp; |
201 |
< |
nd.rspec += (1.0-nd.rspec)*dtmp; |
202 |
< |
/* check threshold */ |
203 |
< |
if (!(nd.specfl & SP_PURE) && |
204 |
< |
specthresh > FTINY && |
205 |
< |
(specthresh >= 1.-FTINY || |
206 |
< |
specthresh + .05 - .1*frandom() > nd.rspec)) |
207 |
< |
nd.specfl |= SP_RBLT; |
208 |
< |
/* compute reflected ray */ |
209 |
< |
for (i = 0; i < 3; i++) |
210 |
< |
nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i]; |
211 |
< |
if (DOT(nd.vrefl, r->ron) <= FTINY) /* penetration? */ |
212 |
< |
for (i = 0; i < 3; i++) /* safety measure */ |
213 |
< |
nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; |
214 |
< |
|
215 |
< |
if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) { |
216 |
< |
RAY lr; |
217 |
< |
if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { |
218 |
< |
VCOPY(lr.rdir, nd.vrefl); |
219 |
< |
rayvalue(&lr); |
220 |
< |
multcolor(lr.rcol, nd.scolor); |
221 |
< |
addcolor(r->rcol, lr.rcol); |
222 |
< |
} |
223 |
< |
} |
224 |
< |
} |
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 |
|
nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec); |
248 |
|
if (nd.tspec > FTINY) { |
249 |
|
nd.specfl |= SP_TRAN; |
250 |
|
/* check threshold */ |
251 |
< |
if (!(nd.specfl & SP_PURE) && specthresh > FTINY && |
252 |
< |
(specthresh >= 1.-FTINY || |
235 |
< |
specthresh + .05 - .1*frandom() > nd.tspec)) |
251 |
> |
if (!(nd.specfl & SP_PURE) && |
252 |
> |
specthresh >= nd.tspec-FTINY) |
253 |
|
nd.specfl |= SP_TBLT; |
254 |
< |
if (r->crtype & SHADOW || |
238 |
< |
DOT(r->pert,r->pert) <= FTINY*FTINY) { |
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] - |
244 |
< |
0.5*r->pert[i]; |
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 |
266 |
|
} else |
267 |
|
nd.tdiff = nd.tspec = nd.trans = 0.0; |
268 |
|
/* transmitted ray */ |
269 |
< |
if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) { |
269 |
> |
|
270 |
> |
if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) { |
271 |
|
RAY lr; |
272 |
< |
if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) { |
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, nd.tspec); |
260 |
< |
multcolor(lr.rcol, nd.mcolor); /* modified by color */ |
277 |
> |
multcolor(lr.rcol, lr.rcoef); |
278 |
|
addcolor(r->rcol, lr.rcol); |
279 |
|
transtest *= bright(lr.rcol); |
280 |
|
transdist = r->rot + lr.rt; |
282 |
|
} else |
283 |
|
transtest = 0; |
284 |
|
|
285 |
< |
if (r->crtype & SHADOW) /* the rest is shadow */ |
286 |
< |
return; |
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 |
|
nd.rdiff = 1.0 - nd.trans - nd.rspec; |
331 |
|
|
332 |
|
if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) |
333 |
< |
return; /* 100% pure specular */ |
333 |
> |
return(1); /* 100% pure specular */ |
334 |
|
|
335 |
< |
if (r->ro != NULL && (r->ro->otype == OBJ_FACE || |
336 |
< |
r->ro->otype == OBJ_RING)) |
278 |
< |
nd.specfl |= SP_FLAT; |
335 |
> |
if (!(nd.specfl & SP_PURE)) |
336 |
> |
gaussamp(&nd); /* checks *BLT flags */ |
337 |
|
|
280 |
– |
if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE)) |
281 |
– |
gaussamp(r, &nd); |
282 |
– |
|
338 |
|
if (nd.rdiff > FTINY) { /* ambient from this side */ |
339 |
< |
ambient(ctmp, r); |
340 |
< |
if (nd.specfl & SP_RBLT) |
341 |
< |
scalecolor(ctmp, 1.0-nd.trans); |
342 |
< |
else |
343 |
< |
scalecolor(ctmp, nd.rdiff); |
289 |
< |
multcolor(ctmp, nd.mcolor); /* modified by material color */ |
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 (nd.tdiff > FTINY) { /* ambient from other side */ |
347 |
< |
flipsurface(r); |
294 |
< |
ambient(ctmp, r); |
347 |
> |
copycolor(ctmp, nd.mcolor); /* modified by color */ |
348 |
|
if (nd.specfl & SP_TBLT) |
349 |
|
scalecolor(ctmp, nd.trans); |
350 |
|
else |
351 |
|
scalecolor(ctmp, nd.tdiff); |
352 |
< |
multcolor(ctmp, nd.mcolor); /* modified by color */ |
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 |
|
/* add direct component */ |
365 |
|
direct(r, dirnorm, &nd); |
366 |
|
/* check distance */ |
367 |
< |
if (transtest > bright(r->rcol)) |
367 |
> |
d = bright(r->rcol); |
368 |
> |
if (transtest > d) |
369 |
|
r->rt = transdist; |
370 |
+ |
else if (mirtest > d) |
371 |
+ |
r->rt = mirdist; |
372 |
+ |
|
373 |
+ |
return(1); |
374 |
|
} |
375 |
|
|
376 |
|
|
377 |
< |
static |
378 |
< |
gaussamp(r, np) /* sample gaussian specular */ |
379 |
< |
RAY *r; |
380 |
< |
register NORMDAT *np; |
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 |
< |
register int i; |
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 |
< |
v[0] = v[1] = v[2] = 0.0; |
327 |
< |
for (i = 0; i < 3; i++) |
328 |
< |
if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6) |
329 |
< |
break; |
330 |
< |
v[i] = 1.0; |
331 |
< |
fcross(u, v, np->pnorm); |
332 |
< |
normalize(u); |
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, r, SPECULAR, np->rspec) == 0) { |
399 |
< |
dimlist[ndims++] = (int)np->mp; |
400 |
< |
d = urand(ilhash(dimlist,ndims)+samplendx); |
401 |
< |
multisamp(rv, 2, d); |
402 |
< |
d = 2.0*PI * rv[0]; |
403 |
< |
cosp = cos(d); |
404 |
< |
sinp = sin(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 |
< |
h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]); |
412 |
< |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
413 |
< |
for (i = 0; i < 3; i++) |
414 |
< |
sr.rdir[i] = r->rdir[i] + d*h[i]; |
415 |
< |
if (DOT(sr.rdir, r->ron) <= FTINY) |
416 |
< |
VCOPY(sr.rdir, np->vrefl); /* jitter no good */ |
417 |
< |
rayvalue(&sr); |
418 |
< |
multcolor(sr.rcol, np->scolor); |
419 |
< |
addcolor(r->rcol, sr.rcol); |
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 |
> |
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 (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 |
|
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, r, SPECULAR, np->tspec) == 0) { |
464 |
< |
dimlist[ndims++] = (int)np->mp; |
465 |
< |
d = urand(ilhash(dimlist,ndims)+1823+samplendx); |
466 |
< |
multisamp(rv, 2, d); |
467 |
< |
d = 2.0*PI * rv[0]; |
468 |
< |
cosp = cos(d); |
469 |
< |
sinp = sin(d); |
470 |
< |
rv[1] = 1.0 - specjitter*rv[1]; |
471 |
< |
if (rv[1] <= FTINY) |
472 |
< |
d = 1.0; |
473 |
< |
else |
474 |
< |
d = sqrt( np->alpha2/4.0 * -log(rv[1]) ); |
475 |
< |
for (i = 0; i < 3; i++) |
476 |
< |
sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]); |
477 |
< |
if (DOT(sr.rdir, r->ron) < -FTINY) |
478 |
< |
normalize(sr.rdir); /* OK, normalize */ |
479 |
< |
else |
480 |
< |
VCOPY(sr.rdir, np->prdir); /* else no jitter */ |
481 |
< |
rayvalue(&sr); |
482 |
< |
scalecolor(sr.rcol, np->tspec); |
483 |
< |
multcolor(sr.rcol, np->mcolor); /* modified by color */ |
484 |
< |
addcolor(r->rcol, sr.rcol); |
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 |
|
} |