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 |
|
* Shading functions for anisotropic materials. |
6 |
|
*/ |
7 |
|
|
8 |
+ |
/* ==================================================================== |
9 |
+ |
* The Radiance Software License, Version 1.0 |
10 |
+ |
* |
11 |
+ |
* Copyright (c) 1990 - 2002 The Regents of the University of California, |
12 |
+ |
* through Lawrence Berkeley National Laboratory. All rights reserved. |
13 |
+ |
* |
14 |
+ |
* Redistribution and use in source and binary forms, with or without |
15 |
+ |
* modification, are permitted provided that the following conditions |
16 |
+ |
* are met: |
17 |
+ |
* |
18 |
+ |
* 1. Redistributions of source code must retain the above copyright |
19 |
+ |
* notice, this list of conditions and the following disclaimer. |
20 |
+ |
* |
21 |
+ |
* 2. Redistributions in binary form must reproduce the above copyright |
22 |
+ |
* notice, this list of conditions and the following disclaimer in |
23 |
+ |
* the documentation and/or other materials provided with the |
24 |
+ |
* distribution. |
25 |
+ |
* |
26 |
+ |
* 3. The end-user documentation included with the redistribution, |
27 |
+ |
* if any, must include the following acknowledgment: |
28 |
+ |
* "This product includes Radiance software |
29 |
+ |
* (http://radsite.lbl.gov/) |
30 |
+ |
* developed by the Lawrence Berkeley National Laboratory |
31 |
+ |
* (http://www.lbl.gov/)." |
32 |
+ |
* Alternately, this acknowledgment may appear in the software itself, |
33 |
+ |
* if and wherever such third-party acknowledgments normally appear. |
34 |
+ |
* |
35 |
+ |
* 4. The names "Radiance," "Lawrence Berkeley National Laboratory" |
36 |
+ |
* and "The Regents of the University of California" must |
37 |
+ |
* not be used to endorse or promote products derived from this |
38 |
+ |
* software without prior written permission. For written |
39 |
+ |
* permission, please contact [email protected]. |
40 |
+ |
* |
41 |
+ |
* 5. Products derived from this software may not be called "Radiance", |
42 |
+ |
* nor may "Radiance" appear in their name, without prior written |
43 |
+ |
* permission of Lawrence Berkeley National Laboratory. |
44 |
+ |
* |
45 |
+ |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
46 |
+ |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
47 |
+ |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
48 |
+ |
* DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR |
49 |
+ |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
50 |
+ |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
51 |
+ |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
52 |
+ |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
53 |
+ |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
54 |
+ |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
55 |
+ |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
56 |
+ |
* SUCH DAMAGE. |
57 |
+ |
* ==================================================================== |
58 |
+ |
* |
59 |
+ |
* This software consists of voluntary contributions made by many |
60 |
+ |
* individuals on behalf of Lawrence Berkeley National Laboratory. For more |
61 |
+ |
* information on Lawrence Berkeley National Laboratory, please see |
62 |
+ |
* <http://www.lbl.gov/>. |
63 |
+ |
*/ |
64 |
+ |
|
65 |
|
#include "ray.h" |
66 |
|
|
67 |
|
#include "otypes.h" |
70 |
|
|
71 |
|
#include "random.h" |
72 |
|
|
73 |
< |
extern double specthresh; /* specular sampling threshold */ |
74 |
< |
extern double specjitter; /* specular sampling jitter */ |
73 |
> |
#ifndef MAXITER |
74 |
> |
#define MAXITER 10 /* maximum # specular ray attempts */ |
75 |
> |
#endif |
76 |
|
|
77 |
|
/* |
78 |
< |
* This anisotropic reflection model uses a variant on the |
79 |
< |
* exponential Gaussian used in normal.c. |
78 |
> |
* This routine implements the anisotropic Gaussian |
79 |
> |
* model described by Ward in Siggraph `92 article. |
80 |
|
* We orient the surface towards the incoming ray, so a single |
81 |
|
* surface can be used to represent an infinitely thin object. |
82 |
|
* |
89 |
|
* 8 red grn blu rspec u-rough v-rough trans tspec |
90 |
|
*/ |
91 |
|
|
37 |
– |
#define BSPEC(m) (6.0) /* specularity parameter b */ |
38 |
– |
|
92 |
|
/* specularity flags */ |
93 |
|
#define SP_REFL 01 /* has reflected specular component */ |
94 |
|
#define SP_TRAN 02 /* has transmitted specular */ |
95 |
< |
#define SP_PURE 010 /* purely specular (zero roughness) */ |
96 |
< |
#define SP_FLAT 020 /* reflecting surface is flat */ |
97 |
< |
#define SP_RBLT 040 /* reflection below sample threshold */ |
98 |
< |
#define SP_TBLT 0100 /* transmission below threshold */ |
46 |
< |
#define SP_BADU 0200 /* bad u direction calculation */ |
95 |
> |
#define SP_FLAT 04 /* reflecting surface is flat */ |
96 |
> |
#define SP_RBLT 010 /* reflection below sample threshold */ |
97 |
> |
#define SP_TBLT 020 /* transmission below threshold */ |
98 |
> |
#define SP_BADU 040 /* bad u direction calculation */ |
99 |
|
|
100 |
|
typedef struct { |
101 |
|
OBJREC *mp; /* material pointer */ |
115 |
|
double pdot; /* perturbed dot product */ |
116 |
|
} ANISODAT; /* anisotropic material data */ |
117 |
|
|
118 |
+ |
static void getacoords(); |
119 |
+ |
static void agaussamp(); |
120 |
|
|
121 |
+ |
|
122 |
+ |
static void |
123 |
|
diraniso(cval, np, ldir, omega) /* compute source contribution */ |
124 |
|
COLOR cval; /* returned coefficient */ |
125 |
|
register ANISODAT *np; /* material data */ |
127 |
|
double omega; /* light source size */ |
128 |
|
{ |
129 |
|
double ldot; |
130 |
< |
double dtmp, dtmp2; |
130 |
> |
double dtmp, dtmp1, dtmp2; |
131 |
|
FVECT h; |
132 |
|
double au2, av2; |
133 |
|
COLOR ctmp; |
150 |
|
scalecolor(ctmp, dtmp); |
151 |
|
addcolor(cval, ctmp); |
152 |
|
} |
153 |
< |
if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE|SP_BADU)) == SP_REFL) { |
153 |
> |
if (ldot > FTINY && (np->specfl&(SP_REFL|SP_BADU)) == SP_REFL) { |
154 |
|
/* |
155 |
|
* Compute specular reflection coefficient using |
156 |
|
* anisotropic gaussian distribution model. |
160 |
|
au2 = av2 = omega/(4.0*PI); |
161 |
|
else |
162 |
|
au2 = av2 = 0.0; |
163 |
< |
au2 += np->u_alpha * np->u_alpha; |
164 |
< |
av2 += np->v_alpha * np->v_alpha; |
163 |
> |
au2 += np->u_alpha*np->u_alpha; |
164 |
> |
av2 += np->v_alpha*np->v_alpha; |
165 |
|
/* half vector */ |
166 |
|
h[0] = ldir[0] - np->rp->rdir[0]; |
167 |
|
h[1] = ldir[1] - np->rp->rdir[1]; |
168 |
|
h[2] = ldir[2] - np->rp->rdir[2]; |
113 |
– |
normalize(h); |
169 |
|
/* ellipse */ |
170 |
< |
dtmp = DOT(np->u, h); |
171 |
< |
dtmp *= dtmp / au2; |
170 |
> |
dtmp1 = DOT(np->u, h); |
171 |
> |
dtmp1 *= dtmp1 / au2; |
172 |
|
dtmp2 = DOT(np->v, h); |
173 |
|
dtmp2 *= dtmp2 / av2; |
174 |
|
/* gaussian */ |
175 |
< |
dtmp = (dtmp + dtmp2) / (1.0 + DOT(np->pnorm, h)); |
176 |
< |
dtmp = exp(-2.0*dtmp) / (4.0*PI * sqrt(au2*av2)); |
175 |
> |
dtmp = DOT(np->pnorm, h); |
176 |
> |
dtmp = (dtmp1 + dtmp2) / (dtmp*dtmp); |
177 |
> |
dtmp = exp(-dtmp) * (0.25/PI) |
178 |
> |
* sqrt(ldot/(np->pdot*au2*av2)); |
179 |
|
/* worth using? */ |
180 |
|
if (dtmp > FTINY) { |
181 |
|
copycolor(ctmp, np->scolor); |
182 |
< |
dtmp *= omega / np->pdot; |
182 |
> |
dtmp *= omega; |
183 |
|
scalecolor(ctmp, dtmp); |
184 |
|
addcolor(cval, ctmp); |
185 |
|
} |
193 |
|
scalecolor(ctmp, dtmp); |
194 |
|
addcolor(cval, ctmp); |
195 |
|
} |
196 |
< |
if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE|SP_BADU)) == SP_TRAN) { |
196 |
> |
if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_BADU)) == SP_TRAN) { |
197 |
|
/* |
198 |
|
* Compute specular transmission. Specular transmission |
199 |
|
* is always modified by material color. |
200 |
|
*/ |
201 |
|
/* roughness + source */ |
202 |
+ |
au2 = av2 = omega / PI; |
203 |
+ |
au2 += np->u_alpha*np->u_alpha; |
204 |
+ |
av2 += np->v_alpha*np->v_alpha; |
205 |
+ |
/* "half vector" */ |
206 |
+ |
h[0] = ldir[0] - np->prdir[0]; |
207 |
+ |
h[1] = ldir[1] - np->prdir[1]; |
208 |
+ |
h[2] = ldir[2] - np->prdir[2]; |
209 |
+ |
dtmp = DOT(h,h); |
210 |
+ |
if (dtmp > FTINY*FTINY) { |
211 |
+ |
dtmp1 = DOT(h,np->pnorm); |
212 |
+ |
dtmp = 1.0 - dtmp1*dtmp1/dtmp; |
213 |
+ |
if (dtmp > FTINY*FTINY) { |
214 |
+ |
dtmp1 = DOT(h,np->u); |
215 |
+ |
dtmp1 *= dtmp1 / au2; |
216 |
+ |
dtmp2 = DOT(h,np->v); |
217 |
+ |
dtmp2 *= dtmp2 / av2; |
218 |
+ |
dtmp = (dtmp1 + dtmp2) / dtmp; |
219 |
+ |
} |
220 |
+ |
} else |
221 |
+ |
dtmp = 0.0; |
222 |
|
/* gaussian */ |
223 |
< |
dtmp = 0.0; |
223 |
> |
dtmp = exp(-dtmp) * (1.0/PI) |
224 |
> |
* sqrt(-ldot/(np->pdot*au2*av2)); |
225 |
|
/* worth using? */ |
226 |
|
if (dtmp > FTINY) { |
227 |
|
copycolor(ctmp, np->mcolor); |
228 |
< |
dtmp *= np->tspec * omega / np->pdot; |
228 |
> |
dtmp *= np->tspec * omega; |
229 |
|
scalecolor(ctmp, dtmp); |
230 |
|
addcolor(cval, ctmp); |
231 |
|
} |
233 |
|
} |
234 |
|
|
235 |
|
|
236 |
+ |
int |
237 |
|
m_aniso(m, r) /* shade ray that hit something anisotropic */ |
238 |
|
register OBJREC *m; |
239 |
|
register RAY *r; |
240 |
|
{ |
241 |
|
ANISODAT nd; |
163 |
– |
double transtest, transdist; |
164 |
– |
double dtmp; |
242 |
|
COLOR ctmp; |
243 |
|
register int i; |
244 |
|
/* easy shadow test */ |
245 |
< |
if (r->crtype & SHADOW && m->otype != MAT_TRANS2) |
246 |
< |
return; |
245 |
> |
if (r->crtype & SHADOW) |
246 |
> |
return(1); |
247 |
|
|
248 |
|
if (m->oargs.nfargs != (m->otype == MAT_TRANS2 ? 8 : 6)) |
249 |
|
objerror(m, USER, "bad number of real arguments"); |
257 |
|
nd.specfl = 0; |
258 |
|
nd.u_alpha = m->oargs.farg[4]; |
259 |
|
nd.v_alpha = m->oargs.farg[5]; |
260 |
< |
if (nd.u_alpha <= FTINY || nd.v_alpha <= FTINY) |
261 |
< |
nd.specfl |= SP_PURE; |
262 |
< |
/* reorient if necessary */ |
263 |
< |
if (r->rod < 0.0) |
264 |
< |
flipsurface(r); |
260 |
> |
if (nd.u_alpha < FTINY || nd.v_alpha <= FTINY) |
261 |
> |
objerror(m, USER, "roughness too small"); |
262 |
> |
/* check for back side */ |
263 |
> |
if (r->rod < 0.0) { |
264 |
> |
if (!backvis && m->otype != MAT_TRANS2) { |
265 |
> |
raytrans(r); |
266 |
> |
return(1); |
267 |
> |
} |
268 |
> |
flipsurface(r); /* reorient if backvis */ |
269 |
> |
} |
270 |
|
/* get modifiers */ |
271 |
|
raytexture(r, m->omod); |
272 |
|
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
273 |
|
if (nd.pdot < .001) |
274 |
|
nd.pdot = .001; /* non-zero for diraniso() */ |
275 |
|
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
194 |
– |
transtest = 0; |
276 |
|
/* get specular component */ |
277 |
|
if ((nd.rspec = m->oargs.farg[3]) > FTINY) { |
278 |
|
nd.specfl |= SP_REFL; |
282 |
|
else |
283 |
|
setcolor(nd.scolor, 1.0, 1.0, 1.0); |
284 |
|
scalecolor(nd.scolor, nd.rspec); |
204 |
– |
/* improved model */ |
205 |
– |
dtmp = exp(-BSPEC(m)*nd.pdot); |
206 |
– |
for (i = 0; i < 3; i++) |
207 |
– |
colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp; |
208 |
– |
nd.rspec += (1.0-nd.rspec)*dtmp; |
285 |
|
/* check threshold */ |
286 |
< |
if (specthresh > FTINY && |
211 |
< |
((specthresh >= 1.-FTINY || |
212 |
< |
specthresh + (.1 - .2*urand(8199+samplendx)) |
213 |
< |
> nd.rspec))) |
286 |
> |
if (specthresh >= nd.rspec-FTINY) |
287 |
|
nd.specfl |= SP_RBLT; |
288 |
|
/* compute refl. direction */ |
289 |
|
for (i = 0; i < 3; i++) |
291 |
|
if (DOT(nd.vrefl, r->ron) <= FTINY) /* penetration? */ |
292 |
|
for (i = 0; i < 3; i++) /* safety measure */ |
293 |
|
nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; |
221 |
– |
|
222 |
– |
if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) { |
223 |
– |
RAY lr; |
224 |
– |
if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { |
225 |
– |
VCOPY(lr.rdir, nd.vrefl); |
226 |
– |
rayvalue(&lr); |
227 |
– |
multcolor(lr.rcol, nd.scolor); |
228 |
– |
addcolor(r->rcol, lr.rcol); |
229 |
– |
} |
230 |
– |
} |
294 |
|
} |
295 |
|
/* compute transmission */ |
296 |
< |
if (m->otype == MAT_TRANS) { |
296 |
> |
if (m->otype == MAT_TRANS2) { |
297 |
|
nd.trans = m->oargs.farg[6]*(1.0 - nd.rspec); |
298 |
|
nd.tspec = nd.trans * m->oargs.farg[7]; |
299 |
|
nd.tdiff = nd.trans - nd.tspec; |
300 |
|
if (nd.tspec > FTINY) { |
301 |
|
nd.specfl |= SP_TRAN; |
302 |
|
/* check threshold */ |
303 |
< |
if (specthresh > FTINY && |
241 |
< |
((specthresh >= 1.-FTINY || |
242 |
< |
specthresh + |
243 |
< |
(.1 - .2*urand(7241+samplendx)) |
244 |
< |
> nd.tspec))) |
303 |
> |
if (specthresh >= nd.tspec-FTINY) |
304 |
|
nd.specfl |= SP_TBLT; |
305 |
< |
if (r->crtype & SHADOW || |
247 |
< |
DOT(r->pert,r->pert) <= FTINY*FTINY) { |
305 |
> |
if (DOT(r->pert,r->pert) <= FTINY*FTINY) { |
306 |
|
VCOPY(nd.prdir, r->rdir); |
249 |
– |
transtest = 2; |
307 |
|
} else { |
308 |
|
for (i = 0; i < 3; i++) /* perturb */ |
309 |
< |
nd.prdir[i] = r->rdir[i] - |
253 |
< |
.75*r->pert[i]; |
309 |
> |
nd.prdir[i] = r->rdir[i] - r->pert[i]; |
310 |
|
if (DOT(nd.prdir, r->ron) < -FTINY) |
311 |
|
normalize(nd.prdir); /* OK */ |
312 |
|
else |
315 |
|
} |
316 |
|
} else |
317 |
|
nd.tdiff = nd.tspec = nd.trans = 0.0; |
262 |
– |
/* transmitted ray */ |
263 |
– |
if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) { |
264 |
– |
RAY lr; |
265 |
– |
if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) { |
266 |
– |
VCOPY(lr.rdir, nd.prdir); |
267 |
– |
rayvalue(&lr); |
268 |
– |
scalecolor(lr.rcol, nd.tspec); |
269 |
– |
multcolor(lr.rcol, nd.mcolor); /* modified by color */ |
270 |
– |
addcolor(r->rcol, lr.rcol); |
271 |
– |
transtest *= bright(lr.rcol); |
272 |
– |
transdist = r->rot + lr.rt; |
273 |
– |
} |
274 |
– |
} |
318 |
|
|
276 |
– |
if (r->crtype & SHADOW) /* the rest is shadow */ |
277 |
– |
return; |
319 |
|
/* diffuse reflection */ |
320 |
|
nd.rdiff = 1.0 - nd.trans - nd.rspec; |
321 |
|
|
322 |
< |
if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) |
282 |
< |
return; /* 100% pure specular */ |
283 |
< |
|
284 |
< |
if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING) |
322 |
> |
if (r->ro != NULL && isflat(r->ro->otype)) |
323 |
|
nd.specfl |= SP_FLAT; |
324 |
|
|
325 |
|
getacoords(r, &nd); /* set up coordinates */ |
326 |
|
|
327 |
< |
if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & (SP_PURE|SP_BADU))) |
327 |
> |
if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_BADU)) |
328 |
|
agaussamp(r, &nd); |
329 |
|
|
330 |
|
if (nd.rdiff > FTINY) { /* ambient from this side */ |
331 |
< |
ambient(ctmp, r); |
331 |
> |
ambient(ctmp, r, nd.pnorm); |
332 |
|
if (nd.specfl & SP_RBLT) |
333 |
|
scalecolor(ctmp, 1.0-nd.trans); |
334 |
|
else |
337 |
|
addcolor(r->rcol, ctmp); /* add to returned color */ |
338 |
|
} |
339 |
|
if (nd.tdiff > FTINY) { /* ambient from other side */ |
340 |
+ |
FVECT bnorm; |
341 |
+ |
|
342 |
|
flipsurface(r); |
343 |
< |
ambient(ctmp, r); |
343 |
> |
bnorm[0] = -nd.pnorm[0]; |
344 |
> |
bnorm[1] = -nd.pnorm[1]; |
345 |
> |
bnorm[2] = -nd.pnorm[2]; |
346 |
> |
ambient(ctmp, r, bnorm); |
347 |
|
if (nd.specfl & SP_TBLT) |
348 |
|
scalecolor(ctmp, nd.trans); |
349 |
|
else |
354 |
|
} |
355 |
|
/* add direct component */ |
356 |
|
direct(r, diraniso, &nd); |
357 |
< |
/* check distance */ |
358 |
< |
if (transtest > bright(r->rcol)) |
316 |
< |
r->rt = transdist; |
357 |
> |
|
358 |
> |
return(1); |
359 |
|
} |
360 |
|
|
361 |
|
|
362 |
< |
static |
362 |
> |
static void |
363 |
|
getacoords(r, np) /* set up coordinate system */ |
364 |
|
RAY *r; |
365 |
|
register ANISODAT *np; |
377 |
|
np->specfl |= SP_BADU; |
378 |
|
return; |
379 |
|
} |
380 |
< |
multv3(np->u, np->u, mf->f->xfm); |
380 |
> |
if (mf->f != &unitxf) |
381 |
> |
multv3(np->u, np->u, mf->f->xfm); |
382 |
|
fcross(np->v, np->pnorm, np->u); |
383 |
|
if (normalize(np->v) == 0.0) { |
384 |
|
objerror(np->mp, WARNING, "illegal orientation vector"); |
389 |
|
} |
390 |
|
|
391 |
|
|
392 |
< |
static |
392 |
> |
static void |
393 |
|
agaussamp(r, np) /* sample anisotropic gaussian specular */ |
394 |
|
RAY *r; |
395 |
|
register ANISODAT *np; |
398 |
|
FVECT h; |
399 |
|
double rv[2]; |
400 |
|
double d, sinp, cosp; |
401 |
+ |
int niter; |
402 |
|
register int i; |
403 |
|
/* compute reflection */ |
404 |
|
if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && |
405 |
|
rayorigin(&sr, r, SPECULAR, np->rspec) == 0) { |
406 |
|
dimlist[ndims++] = (int)np->mp; |
407 |
< |
d = urand(ilhash(dimlist,ndims)+samplendx); |
408 |
< |
multisamp(rv, 2, d); |
409 |
< |
d = 2.0*PI * rv[0]; |
410 |
< |
cosp = np->u_alpha * cos(d); |
411 |
< |
sinp = np->v_alpha * sin(d); |
412 |
< |
d = sqrt(cosp*cosp + sinp*sinp); |
413 |
< |
cosp /= d; |
414 |
< |
sinp /= d; |
415 |
< |
rv[1] = 1.0 - specjitter*rv[1]; |
416 |
< |
if (rv[1] <= FTINY) |
417 |
< |
d = 1.0; |
418 |
< |
else |
419 |
< |
d = sqrt(-log(rv[1]) / |
420 |
< |
(cosp*cosp/(np->u_alpha*np->u_alpha) + |
421 |
< |
sinp*sinp/(np->v_alpha*np->v_alpha))); |
422 |
< |
for (i = 0; i < 3; i++) |
423 |
< |
h[i] = np->pnorm[i] + |
424 |
< |
d*(cosp*np->u[i] + sinp*np->v[i]); |
425 |
< |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
426 |
< |
for (i = 0; i < 3; i++) |
427 |
< |
sr.rdir[i] = r->rdir[i] + d*h[i]; |
428 |
< |
if (DOT(sr.rdir, r->ron) <= FTINY) /* penetration? */ |
429 |
< |
VCOPY(sr.rdir, np->vrefl); /* jitter no good */ |
430 |
< |
rayvalue(&sr); |
431 |
< |
multcolor(sr.rcol, np->scolor); |
432 |
< |
addcolor(r->rcol, sr.rcol); |
407 |
> |
for (niter = 0; niter < MAXITER; niter++) { |
408 |
> |
if (niter) |
409 |
> |
d = frandom(); |
410 |
> |
else |
411 |
> |
d = urand(ilhash(dimlist,ndims)+samplendx); |
412 |
> |
multisamp(rv, 2, d); |
413 |
> |
d = 2.0*PI * rv[0]; |
414 |
> |
cosp = tcos(d) * np->u_alpha; |
415 |
> |
sinp = tsin(d) * np->v_alpha; |
416 |
> |
d = sqrt(cosp*cosp + sinp*sinp); |
417 |
> |
cosp /= d; |
418 |
> |
sinp /= d; |
419 |
> |
rv[1] = 1.0 - specjitter*rv[1]; |
420 |
> |
if (rv[1] <= FTINY) |
421 |
> |
d = 1.0; |
422 |
> |
else |
423 |
> |
d = sqrt(-log(rv[1]) / |
424 |
> |
(cosp*cosp/(np->u_alpha*np->u_alpha) + |
425 |
> |
sinp*sinp/(np->v_alpha*np->v_alpha))); |
426 |
> |
for (i = 0; i < 3; i++) |
427 |
> |
h[i] = np->pnorm[i] + |
428 |
> |
d*(cosp*np->u[i] + sinp*np->v[i]); |
429 |
> |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
430 |
> |
for (i = 0; i < 3; i++) |
431 |
> |
sr.rdir[i] = r->rdir[i] + d*h[i]; |
432 |
> |
if (DOT(sr.rdir, r->ron) > FTINY) { |
433 |
> |
rayvalue(&sr); |
434 |
> |
multcolor(sr.rcol, np->scolor); |
435 |
> |
addcolor(r->rcol, sr.rcol); |
436 |
> |
break; |
437 |
> |
} |
438 |
> |
} |
439 |
|
ndims--; |
440 |
|
} |
441 |
|
/* compute transmission */ |
442 |
+ |
if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && |
443 |
+ |
rayorigin(&sr, r, SPECULAR, np->tspec) == 0) { |
444 |
+ |
dimlist[ndims++] = (int)np->mp; |
445 |
+ |
for (niter = 0; niter < MAXITER; niter++) { |
446 |
+ |
if (niter) |
447 |
+ |
d = frandom(); |
448 |
+ |
else |
449 |
+ |
d = urand(ilhash(dimlist,ndims)+1823+samplendx); |
450 |
+ |
multisamp(rv, 2, d); |
451 |
+ |
d = 2.0*PI * rv[0]; |
452 |
+ |
cosp = tcos(d) * np->u_alpha; |
453 |
+ |
sinp = tsin(d) * np->v_alpha; |
454 |
+ |
d = sqrt(cosp*cosp + sinp*sinp); |
455 |
+ |
cosp /= d; |
456 |
+ |
sinp /= d; |
457 |
+ |
rv[1] = 1.0 - specjitter*rv[1]; |
458 |
+ |
if (rv[1] <= FTINY) |
459 |
+ |
d = 1.0; |
460 |
+ |
else |
461 |
+ |
d = sqrt(-log(rv[1]) / |
462 |
+ |
(cosp*cosp/(np->u_alpha*np->u_alpha) + |
463 |
+ |
sinp*sinp/(np->v_alpha*np->v_alpha))); |
464 |
+ |
for (i = 0; i < 3; i++) |
465 |
+ |
sr.rdir[i] = np->prdir[i] + |
466 |
+ |
d*(cosp*np->u[i] + sinp*np->v[i]); |
467 |
+ |
if (DOT(sr.rdir, r->ron) < -FTINY) { |
468 |
+ |
normalize(sr.rdir); /* OK, normalize */ |
469 |
+ |
rayvalue(&sr); |
470 |
+ |
scalecolor(sr.rcol, np->tspec); |
471 |
+ |
multcolor(sr.rcol, np->mcolor); /* modify */ |
472 |
+ |
addcolor(r->rcol, sr.rcol); |
473 |
+ |
break; |
474 |
+ |
} |
475 |
+ |
} |
476 |
+ |
ndims--; |
477 |
+ |
} |
478 |
|
} |