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 |
+ |
#include "copyright.h" |
9 |
+ |
|
10 |
|
#include "ray.h" |
11 |
|
|
12 |
|
#include "otypes.h" |
15 |
|
|
16 |
|
#include "random.h" |
17 |
|
|
18 |
< |
extern double specthresh; /* specular sampling threshold */ |
19 |
< |
extern double specjitter; /* specular sampling jitter */ |
18 |
> |
#ifndef MAXITER |
19 |
> |
#define MAXITER 10 /* maximum # specular ray attempts */ |
20 |
> |
#endif |
21 |
|
|
22 |
– |
static agaussamp(), getacoords(); |
23 |
– |
|
22 |
|
/* |
23 |
|
* This routine implements the anisotropic Gaussian |
24 |
|
* model described by Ward in Siggraph `92 article. |
60 |
|
double pdot; /* perturbed dot product */ |
61 |
|
} ANISODAT; /* anisotropic material data */ |
62 |
|
|
63 |
+ |
static void getacoords(); |
64 |
+ |
static void agaussamp(); |
65 |
|
|
66 |
+ |
|
67 |
+ |
static void |
68 |
|
diraniso(cval, np, ldir, omega) /* compute source contribution */ |
69 |
|
COLOR cval; /* returned coefficient */ |
70 |
|
register ANISODAT *np; /* material data */ |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
+ |
int |
182 |
|
m_aniso(m, r) /* shade ray that hit something anisotropic */ |
183 |
|
register OBJREC *m; |
184 |
|
register RAY *r; |
204 |
|
nd.v_alpha = m->oargs.farg[5]; |
205 |
|
if (nd.u_alpha < FTINY || nd.v_alpha <= FTINY) |
206 |
|
objerror(m, USER, "roughness too small"); |
207 |
< |
/* reorient if necessary */ |
208 |
< |
if (r->rod < 0.0) |
209 |
< |
flipsurface(r); |
207 |
> |
/* check for back side */ |
208 |
> |
if (r->rod < 0.0) { |
209 |
> |
if (!backvis && m->otype != MAT_TRANS2) { |
210 |
> |
raytrans(r); |
211 |
> |
return(1); |
212 |
> |
} |
213 |
> |
flipsurface(r); /* reorient if backvis */ |
214 |
> |
} |
215 |
|
/* get modifiers */ |
216 |
|
raytexture(r, m->omod); |
217 |
|
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
264 |
|
/* diffuse reflection */ |
265 |
|
nd.rdiff = 1.0 - nd.trans - nd.rspec; |
266 |
|
|
267 |
< |
if (r->ro != NULL && (r->ro->otype == OBJ_FACE || |
260 |
< |
r->ro->otype == OBJ_RING)) |
267 |
> |
if (r->ro != NULL && isflat(r->ro->otype)) |
268 |
|
nd.specfl |= SP_FLAT; |
269 |
|
|
270 |
|
getacoords(r, &nd); /* set up coordinates */ |
273 |
|
agaussamp(r, &nd); |
274 |
|
|
275 |
|
if (nd.rdiff > FTINY) { /* ambient from this side */ |
276 |
< |
ambient(ctmp, r); |
276 |
> |
ambient(ctmp, r, nd.pnorm); |
277 |
|
if (nd.specfl & SP_RBLT) |
278 |
|
scalecolor(ctmp, 1.0-nd.trans); |
279 |
|
else |
282 |
|
addcolor(r->rcol, ctmp); /* add to returned color */ |
283 |
|
} |
284 |
|
if (nd.tdiff > FTINY) { /* ambient from other side */ |
285 |
+ |
FVECT bnorm; |
286 |
+ |
|
287 |
|
flipsurface(r); |
288 |
< |
ambient(ctmp, r); |
288 |
> |
bnorm[0] = -nd.pnorm[0]; |
289 |
> |
bnorm[1] = -nd.pnorm[1]; |
290 |
> |
bnorm[2] = -nd.pnorm[2]; |
291 |
> |
ambient(ctmp, r, bnorm); |
292 |
|
if (nd.specfl & SP_TBLT) |
293 |
|
scalecolor(ctmp, nd.trans); |
294 |
|
else |
304 |
|
} |
305 |
|
|
306 |
|
|
307 |
< |
static |
307 |
> |
static void |
308 |
|
getacoords(r, np) /* set up coordinate system */ |
309 |
|
RAY *r; |
310 |
|
register ANISODAT *np; |
334 |
|
} |
335 |
|
|
336 |
|
|
337 |
< |
static |
337 |
> |
static void |
338 |
|
agaussamp(r, np) /* sample anisotropic gaussian specular */ |
339 |
|
RAY *r; |
340 |
|
register ANISODAT *np; |
343 |
|
FVECT h; |
344 |
|
double rv[2]; |
345 |
|
double d, sinp, cosp; |
346 |
+ |
int niter; |
347 |
|
register int i; |
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) * np->u_alpha; |
356 |
< |
sinp = sin(d) * np->v_alpha; |
357 |
< |
d = sqrt(cosp*cosp + sinp*sinp); |
358 |
< |
cosp /= d; |
359 |
< |
sinp /= d; |
360 |
< |
rv[1] = 1.0 - specjitter*rv[1]; |
361 |
< |
if (rv[1] <= FTINY) |
362 |
< |
d = 1.0; |
363 |
< |
else |
364 |
< |
d = sqrt(-log(rv[1]) / |
365 |
< |
(cosp*cosp/(np->u_alpha*np->u_alpha) + |
366 |
< |
sinp*sinp/(np->v_alpha*np->v_alpha))); |
367 |
< |
for (i = 0; i < 3; i++) |
368 |
< |
h[i] = np->pnorm[i] + |
369 |
< |
d*(cosp*np->u[i] + sinp*np->v[i]); |
370 |
< |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
371 |
< |
for (i = 0; i < 3; i++) |
372 |
< |
sr.rdir[i] = r->rdir[i] + d*h[i]; |
373 |
< |
if (DOT(sr.rdir, r->ron) <= FTINY) /* penetration? */ |
374 |
< |
VCOPY(sr.rdir, np->vrefl); /* jitter no good */ |
375 |
< |
rayvalue(&sr); |
376 |
< |
multcolor(sr.rcol, np->scolor); |
377 |
< |
addcolor(r->rcol, sr.rcol); |
352 |
> |
for (niter = 0; niter < MAXITER; niter++) { |
353 |
> |
if (niter) |
354 |
> |
d = frandom(); |
355 |
> |
else |
356 |
> |
d = urand(ilhash(dimlist,ndims)+samplendx); |
357 |
> |
multisamp(rv, 2, d); |
358 |
> |
d = 2.0*PI * rv[0]; |
359 |
> |
cosp = tcos(d) * np->u_alpha; |
360 |
> |
sinp = tsin(d) * np->v_alpha; |
361 |
> |
d = sqrt(cosp*cosp + sinp*sinp); |
362 |
> |
cosp /= d; |
363 |
> |
sinp /= d; |
364 |
> |
rv[1] = 1.0 - specjitter*rv[1]; |
365 |
> |
if (rv[1] <= FTINY) |
366 |
> |
d = 1.0; |
367 |
> |
else |
368 |
> |
d = sqrt(-log(rv[1]) / |
369 |
> |
(cosp*cosp/(np->u_alpha*np->u_alpha) + |
370 |
> |
sinp*sinp/(np->v_alpha*np->v_alpha))); |
371 |
> |
for (i = 0; i < 3; i++) |
372 |
> |
h[i] = np->pnorm[i] + |
373 |
> |
d*(cosp*np->u[i] + sinp*np->v[i]); |
374 |
> |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
375 |
> |
for (i = 0; i < 3; i++) |
376 |
> |
sr.rdir[i] = r->rdir[i] + d*h[i]; |
377 |
> |
if (DOT(sr.rdir, r->ron) > FTINY) { |
378 |
> |
rayvalue(&sr); |
379 |
> |
multcolor(sr.rcol, np->scolor); |
380 |
> |
addcolor(r->rcol, sr.rcol); |
381 |
> |
break; |
382 |
> |
} |
383 |
> |
} |
384 |
|
ndims--; |
385 |
|
} |
386 |
|
/* compute transmission */ |
387 |
|
if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && |
388 |
|
rayorigin(&sr, r, SPECULAR, np->tspec) == 0) { |
389 |
|
dimlist[ndims++] = (int)np->mp; |
390 |
< |
d = urand(ilhash(dimlist,ndims)+1823+samplendx); |
391 |
< |
multisamp(rv, 2, d); |
392 |
< |
d = 2.0*PI * rv[0]; |
393 |
< |
cosp = cos(d) * np->u_alpha; |
394 |
< |
sinp = sin(d) * np->v_alpha; |
395 |
< |
d = sqrt(cosp*cosp + sinp*sinp); |
396 |
< |
cosp /= d; |
397 |
< |
sinp /= d; |
398 |
< |
rv[1] = 1.0 - specjitter*rv[1]; |
399 |
< |
if (rv[1] <= FTINY) |
400 |
< |
d = 1.0; |
401 |
< |
else |
402 |
< |
d = sqrt(-log(rv[1]) / |
403 |
< |
(cosp*cosp/(np->u_alpha*np->u_alpha) + |
404 |
< |
sinp*sinp/(np->v_alpha*np->u_alpha))); |
405 |
< |
for (i = 0; i < 3; i++) |
406 |
< |
sr.rdir[i] = np->prdir[i] + |
407 |
< |
d*(cosp*np->u[i] + sinp*np->v[i]); |
408 |
< |
if (DOT(sr.rdir, r->ron) < -FTINY) |
409 |
< |
normalize(sr.rdir); /* OK, normalize */ |
410 |
< |
else |
411 |
< |
VCOPY(sr.rdir, np->prdir); /* else no jitter */ |
412 |
< |
rayvalue(&sr); |
413 |
< |
scalecolor(sr.rcol, np->tspec); |
414 |
< |
multcolor(sr.rcol, np->mcolor); /* modify by color */ |
415 |
< |
addcolor(r->rcol, sr.rcol); |
390 |
> |
for (niter = 0; niter < MAXITER; niter++) { |
391 |
> |
if (niter) |
392 |
> |
d = frandom(); |
393 |
> |
else |
394 |
> |
d = urand(ilhash(dimlist,ndims)+1823+samplendx); |
395 |
> |
multisamp(rv, 2, d); |
396 |
> |
d = 2.0*PI * rv[0]; |
397 |
> |
cosp = tcos(d) * np->u_alpha; |
398 |
> |
sinp = tsin(d) * np->v_alpha; |
399 |
> |
d = sqrt(cosp*cosp + sinp*sinp); |
400 |
> |
cosp /= d; |
401 |
> |
sinp /= d; |
402 |
> |
rv[1] = 1.0 - specjitter*rv[1]; |
403 |
> |
if (rv[1] <= FTINY) |
404 |
> |
d = 1.0; |
405 |
> |
else |
406 |
> |
d = sqrt(-log(rv[1]) / |
407 |
> |
(cosp*cosp/(np->u_alpha*np->u_alpha) + |
408 |
> |
sinp*sinp/(np->v_alpha*np->v_alpha))); |
409 |
> |
for (i = 0; i < 3; i++) |
410 |
> |
sr.rdir[i] = np->prdir[i] + |
411 |
> |
d*(cosp*np->u[i] + sinp*np->v[i]); |
412 |
> |
if (DOT(sr.rdir, r->ron) < -FTINY) { |
413 |
> |
normalize(sr.rdir); /* OK, normalize */ |
414 |
> |
rayvalue(&sr); |
415 |
> |
scalecolor(sr.rcol, np->tspec); |
416 |
> |
multcolor(sr.rcol, np->mcolor); /* modify */ |
417 |
> |
addcolor(r->rcol, sr.rcol); |
418 |
> |
break; |
419 |
> |
} |
420 |
> |
} |
421 |
|
ndims--; |
422 |
|
} |
423 |
|
} |