39 |
|
/* specularity flags */ |
40 |
|
#define SP_REFL 01 /* has reflected specular component */ |
41 |
|
#define SP_TRAN 02 /* has transmitted specular */ |
42 |
< |
#define SP_PURE 010 /* purely specular (zero roughness) */ |
43 |
< |
#define SP_FLAT 020 /* reflecting surface is flat */ |
44 |
< |
#define SP_RBLT 040 /* reflection below sample threshold */ |
45 |
< |
#define SP_TBLT 0100 /* transmission below threshold */ |
46 |
< |
#define SP_BADU 0200 /* bad u direction calculation */ |
42 |
> |
#define SP_FLAT 04 /* reflecting surface is flat */ |
43 |
> |
#define SP_RBLT 010 /* reflection below sample threshold */ |
44 |
> |
#define SP_TBLT 020 /* transmission below threshold */ |
45 |
> |
#define SP_BADU 040 /* bad u direction calculation */ |
46 |
|
|
47 |
|
typedef struct { |
48 |
|
OBJREC *mp; /* material pointer */ |
50 |
|
short specfl; /* specularity flags, defined above */ |
51 |
|
COLOR mcolor; /* color of this material */ |
52 |
|
COLOR scolor; /* color of specular component */ |
53 |
+ |
FVECT vrefl; /* vector in reflected direction */ |
54 |
|
FVECT prdir; /* vector in transmitted direction */ |
55 |
|
FVECT u, v; /* u and v vectors orienting anisotropy */ |
56 |
|
double u_alpha; /* u roughness */ |
93 |
|
scalecolor(ctmp, dtmp); |
94 |
|
addcolor(cval, ctmp); |
95 |
|
} |
96 |
< |
if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE|SP_BADU)) == SP_REFL) { |
96 |
> |
if (ldot > FTINY && (np->specfl&(SP_REFL|SP_BADU)) == SP_REFL) { |
97 |
|
/* |
98 |
|
* Compute specular reflection coefficient using |
99 |
|
* anisotropic gaussian distribution model. |
121 |
|
/* worth using? */ |
122 |
|
if (dtmp > FTINY) { |
123 |
|
copycolor(ctmp, np->scolor); |
124 |
< |
dtmp *= omega / np->pdot; |
124 |
> |
dtmp *= omega * sqrt(ldot/np->pdot); |
125 |
|
scalecolor(ctmp, dtmp); |
126 |
|
addcolor(cval, ctmp); |
127 |
|
} |
135 |
|
scalecolor(ctmp, dtmp); |
136 |
|
addcolor(cval, ctmp); |
137 |
|
} |
138 |
< |
if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE|SP_BADU)) == SP_TRAN) { |
138 |
> |
if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_BADU)) == SP_TRAN) { |
139 |
|
/* |
140 |
|
* Compute specular transmission. Specular transmission |
141 |
|
* is always modified by material color. |
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 |
|
} |
159 |
|
register RAY *r; |
160 |
|
{ |
161 |
|
ANISODAT nd; |
162 |
– |
double transtest, transdist; |
162 |
|
double dtmp; |
163 |
|
COLOR ctmp; |
164 |
|
register int i; |
165 |
|
/* easy shadow test */ |
166 |
< |
if (r->crtype & SHADOW && m->otype != MAT_TRANS2) |
166 |
> |
if (r->crtype & SHADOW) |
167 |
|
return; |
168 |
|
|
169 |
|
if (m->oargs.nfargs != (m->otype == MAT_TRANS2 ? 8 : 6)) |
178 |
|
nd.specfl = 0; |
179 |
|
nd.u_alpha = m->oargs.farg[4]; |
180 |
|
nd.v_alpha = m->oargs.farg[5]; |
181 |
< |
if (nd.u_alpha <= FTINY || nd.v_alpha <= FTINY) |
182 |
< |
nd.specfl |= SP_PURE; |
181 |
> |
if (nd.u_alpha < 1e-6 || nd.v_alpha <= 1e-6) |
182 |
> |
objerror(m, USER, "roughness too small"); |
183 |
|
/* reorient if necessary */ |
184 |
|
if (r->rod < 0.0) |
185 |
|
flipsurface(r); |
189 |
|
if (nd.pdot < .001) |
190 |
|
nd.pdot = .001; /* non-zero for diraniso() */ |
191 |
|
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
193 |
– |
transtest = 0; |
192 |
|
/* get specular component */ |
193 |
|
if ((nd.rspec = m->oargs.farg[3]) > FTINY) { |
194 |
|
nd.specfl |= SP_REFL; |
204 |
|
colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp; |
205 |
|
nd.rspec += (1.0-nd.rspec)*dtmp; |
206 |
|
/* check threshold */ |
207 |
< |
if (nd.rspec <= specthresh+FTINY) |
207 |
> |
if (specthresh > FTINY && |
208 |
> |
(specthresh >= 1.-FTINY || |
209 |
> |
specthresh > nd.rspec)) |
210 |
|
nd.specfl |= SP_RBLT; |
211 |
< |
|
212 |
< |
if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) { |
213 |
< |
RAY lr; |
214 |
< |
if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { |
215 |
< |
for (i = 0; i < 3; i++) |
216 |
< |
lr.rdir[i] = r->rdir[i] + |
217 |
< |
2.0*nd.pdot*nd.pnorm[i]; |
218 |
< |
rayvalue(&lr); |
219 |
< |
multcolor(lr.rcol, nd.scolor); |
220 |
< |
addcolor(r->rcol, lr.rcol); |
221 |
< |
} |
222 |
< |
} |
211 |
> |
/* compute refl. direction */ |
212 |
> |
for (i = 0; i < 3; i++) |
213 |
> |
nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i]; |
214 |
> |
if (DOT(nd.vrefl, r->ron) <= FTINY) /* penetration? */ |
215 |
> |
for (i = 0; i < 3; i++) /* safety measure */ |
216 |
> |
nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; |
217 |
|
} |
218 |
|
/* compute transmission */ |
219 |
|
if (m->otype == MAT_TRANS) { |
223 |
|
if (nd.tspec > FTINY) { |
224 |
|
nd.specfl |= SP_TRAN; |
225 |
|
/* check threshold */ |
226 |
< |
if (nd.tspec <= specthresh+FTINY) |
226 |
> |
if (specthresh > FTINY && |
227 |
> |
(specthresh >= 1.-FTINY || |
228 |
> |
specthresh > nd.tspec)) |
229 |
|
nd.specfl |= SP_TBLT; |
230 |
< |
if (r->crtype & SHADOW || |
235 |
< |
DOT(r->pert,r->pert) <= FTINY*FTINY) { |
230 |
> |
if (DOT(r->pert,r->pert) <= FTINY*FTINY) { |
231 |
|
VCOPY(nd.prdir, r->rdir); |
237 |
– |
transtest = 2; |
232 |
|
} else { |
233 |
|
for (i = 0; i < 3; i++) /* perturb */ |
234 |
|
nd.prdir[i] = r->rdir[i] - |
235 |
< |
.75*r->pert[i]; |
236 |
< |
normalize(nd.prdir); |
235 |
> |
0.5*r->pert[i]; |
236 |
> |
if (DOT(nd.prdir, r->ron) < -FTINY) |
237 |
> |
normalize(nd.prdir); /* OK */ |
238 |
> |
else |
239 |
> |
VCOPY(nd.prdir, r->rdir); |
240 |
|
} |
241 |
|
} |
242 |
|
} else |
243 |
|
nd.tdiff = nd.tspec = nd.trans = 0.0; |
247 |
– |
/* transmitted ray */ |
248 |
– |
if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) { |
249 |
– |
RAY lr; |
250 |
– |
if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) { |
251 |
– |
VCOPY(lr.rdir, nd.prdir); |
252 |
– |
rayvalue(&lr); |
253 |
– |
scalecolor(lr.rcol, nd.tspec); |
254 |
– |
multcolor(lr.rcol, nd.mcolor); /* modified by color */ |
255 |
– |
addcolor(r->rcol, lr.rcol); |
256 |
– |
transtest *= bright(lr.rcol); |
257 |
– |
transdist = r->rot + lr.rt; |
258 |
– |
} |
259 |
– |
} |
244 |
|
|
261 |
– |
if (r->crtype & SHADOW) /* the rest is shadow */ |
262 |
– |
return; |
245 |
|
/* diffuse reflection */ |
246 |
|
nd.rdiff = 1.0 - nd.trans - nd.rspec; |
247 |
|
|
248 |
< |
if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) |
249 |
< |
return; /* 100% pure specular */ |
268 |
< |
|
269 |
< |
if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING) |
248 |
> |
if (r->ro != NULL && (r->ro->otype == OBJ_FACE || |
249 |
> |
r->ro->otype == OBJ_RING)) |
250 |
|
nd.specfl |= SP_FLAT; |
251 |
|
|
252 |
|
getacoords(r, &nd); /* set up coordinates */ |
253 |
|
|
254 |
< |
if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & (SP_PURE|SP_BADU))) |
254 |
> |
if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_BADU)) |
255 |
|
agaussamp(r, &nd); |
256 |
|
|
257 |
|
if (nd.rdiff > FTINY) { /* ambient from this side */ |
276 |
|
} |
277 |
|
/* add direct component */ |
278 |
|
direct(r, diraniso, &nd); |
299 |
– |
/* check distance */ |
300 |
– |
if (transtest > bright(r->rcol)) |
301 |
– |
r->rt = transdist; |
279 |
|
} |
280 |
|
|
281 |
|
|
317 |
|
FVECT h; |
318 |
|
double rv[2]; |
319 |
|
double d, sinp, cosp; |
343 |
– |
int ntries; |
320 |
|
register int i; |
321 |
|
/* compute reflection */ |
322 |
|
if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && |
323 |
|
rayorigin(&sr, r, SPECULAR, np->rspec) == 0) { |
324 |
|
dimlist[ndims++] = (int)np->mp; |
325 |
< |
for (ntries = 0; ntries < 10; ntries++) { |
326 |
< |
dimlist[ndims] = ntries * 3601; |
327 |
< |
d = urand(ilhash(dimlist,ndims+1)+samplendx); |
328 |
< |
multisamp(rv, 2, d); |
329 |
< |
d = 2.0*PI * rv[0]; |
330 |
< |
cosp = np->u_alpha * cos(d); |
331 |
< |
sinp = np->v_alpha * sin(d); |
332 |
< |
d = sqrt(cosp*cosp + sinp*sinp); |
333 |
< |
cosp /= d; |
334 |
< |
sinp /= d; |
335 |
< |
rv[1] = 1.0 - specjitter*rv[1]; |
336 |
< |
if (rv[1] <= FTINY) |
337 |
< |
d = 1.0; |
338 |
< |
else |
339 |
< |
d = sqrt(-log(rv[1]) / |
340 |
< |
(cosp*cosp/(np->u_alpha*np->u_alpha) + |
341 |
< |
sinp*sinp/(np->v_alpha*np->v_alpha))); |
342 |
< |
for (i = 0; i < 3; i++) |
343 |
< |
h[i] = np->pnorm[i] + |
344 |
< |
d*(cosp*np->u[i] + sinp*np->v[i]); |
345 |
< |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
346 |
< |
for (i = 0; i < 3; i++) |
347 |
< |
sr.rdir[i] = r->rdir[i] + d*h[i]; |
348 |
< |
if (DOT(sr.rdir, r->ron) > FTINY) { |
349 |
< |
rayvalue(&sr); |
350 |
< |
multcolor(sr.rcol, np->scolor); |
375 |
< |
addcolor(r->rcol, sr.rcol); |
376 |
< |
break; |
377 |
< |
} |
378 |
< |
} |
325 |
> |
d = urand(ilhash(dimlist,ndims)+samplendx); |
326 |
> |
multisamp(rv, 2, d); |
327 |
> |
d = 2.0*PI * rv[0]; |
328 |
> |
cosp = np->u_alpha * cos(d); |
329 |
> |
sinp = np->v_alpha * sin(d); |
330 |
> |
d = sqrt(cosp*cosp + sinp*sinp); |
331 |
> |
cosp /= d; |
332 |
> |
sinp /= d; |
333 |
> |
rv[1] = 1.0 - specjitter*rv[1]; |
334 |
> |
if (rv[1] <= FTINY) |
335 |
> |
d = 1.0; |
336 |
> |
else |
337 |
> |
d = sqrt(-log(rv[1]) / |
338 |
> |
(cosp*cosp/(np->u_alpha*np->u_alpha) + |
339 |
> |
sinp*sinp/(np->v_alpha*np->v_alpha))); |
340 |
> |
for (i = 0; i < 3; i++) |
341 |
> |
h[i] = np->pnorm[i] + |
342 |
> |
d*(cosp*np->u[i] + sinp*np->v[i]); |
343 |
> |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
344 |
> |
for (i = 0; i < 3; i++) |
345 |
> |
sr.rdir[i] = r->rdir[i] + d*h[i]; |
346 |
> |
if (DOT(sr.rdir, r->ron) <= FTINY) /* penetration? */ |
347 |
> |
VCOPY(sr.rdir, np->vrefl); /* jitter no good */ |
348 |
> |
rayvalue(&sr); |
349 |
> |
multcolor(sr.rcol, np->scolor); |
350 |
> |
addcolor(r->rcol, sr.rcol); |
351 |
|
ndims--; |
352 |
|
} |
353 |
|
/* compute transmission */ |
354 |
+ |
if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && |
355 |
+ |
rayorigin(&sr, r, SPECULAR, np->tspec) == 0) { |
356 |
+ |
dimlist[ndims++] = (int)np->mp; |
357 |
+ |
d = urand(ilhash(dimlist,ndims)+1823+samplendx); |
358 |
+ |
multisamp(rv, 2, d); |
359 |
+ |
d = 2.0*PI * rv[0]; |
360 |
+ |
cosp = cos(d); |
361 |
+ |
sinp = sin(d); |
362 |
+ |
rv[1] = 1.0 - specjitter*rv[1]; |
363 |
+ |
if (rv[1] <= FTINY) |
364 |
+ |
d = 1.0; |
365 |
+ |
else |
366 |
+ |
d = sqrt(-log(rv[1]) / |
367 |
+ |
(cosp*cosp*4./(np->u_alpha*np->u_alpha) + |
368 |
+ |
sinp*sinp*4./(np->v_alpha*np->v_alpha))); |
369 |
+ |
for (i = 0; i < 3; i++) |
370 |
+ |
sr.rdir[i] = np->prdir[i] + |
371 |
+ |
d*(cosp*np->u[i] + sinp*np->v[i]); |
372 |
+ |
if (DOT(sr.rdir, r->ron) < -FTINY) |
373 |
+ |
normalize(sr.rdir); /* OK, normalize */ |
374 |
+ |
else |
375 |
+ |
VCOPY(sr.rdir, np->prdir); /* else no jitter */ |
376 |
+ |
rayvalue(&sr); |
377 |
+ |
scalecolor(sr.rcol, np->tspec); |
378 |
+ |
multcolor(sr.rcol, np->mcolor); /* modify by color */ |
379 |
+ |
addcolor(r->rcol, sr.rcol); |
380 |
+ |
ndims--; |
381 |
+ |
} |
382 |
|
} |