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 |
– |
extern int backvis; /* back faces visible? */ |
23 |
– |
|
24 |
– |
static agaussamp(), getacoords(); |
25 |
– |
|
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; |
192 |
|
|
193 |
|
if (m->oargs.nfargs != (m->otype == MAT_TRANS2 ? 8 : 6)) |
194 |
|
objerror(m, USER, "bad number of real arguments"); |
195 |
+ |
/* check for back side */ |
196 |
+ |
if (r->rod < 0.0) { |
197 |
+ |
if (!backvis && m->otype != MAT_TRANS2) { |
198 |
+ |
raytrans(r); |
199 |
+ |
return(1); |
200 |
+ |
} |
201 |
+ |
raytexture(r, m->omod); |
202 |
+ |
flipsurface(r); /* reorient if backvis */ |
203 |
+ |
} else |
204 |
+ |
raytexture(r, m->omod); |
205 |
+ |
/* get material color */ |
206 |
|
nd.mp = m; |
207 |
|
nd.rp = r; |
196 |
– |
/* get material color */ |
208 |
|
setcolor(nd.mcolor, m->oargs.farg[0], |
209 |
|
m->oargs.farg[1], |
210 |
|
m->oargs.farg[2]); |
214 |
|
nd.v_alpha = m->oargs.farg[5]; |
215 |
|
if (nd.u_alpha < FTINY || nd.v_alpha <= FTINY) |
216 |
|
objerror(m, USER, "roughness too small"); |
217 |
< |
/* check for back side */ |
207 |
< |
if (r->rod < 0.0) { |
208 |
< |
if (!backvis && m->otype != MAT_TRANS2) { |
209 |
< |
raytrans(r); |
210 |
< |
return(1); |
211 |
< |
} |
212 |
< |
flipsurface(r); /* reorient if backvis */ |
213 |
< |
} |
214 |
< |
/* get modifiers */ |
215 |
< |
raytexture(r, m->omod); |
217 |
> |
|
218 |
|
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
219 |
|
if (nd.pdot < .001) |
220 |
|
nd.pdot = .001; /* non-zero for diraniso() */ |
265 |
|
/* diffuse reflection */ |
266 |
|
nd.rdiff = 1.0 - nd.trans - nd.rspec; |
267 |
|
|
268 |
< |
if (r->ro != NULL && isflat(r->ro->otype)) |
268 |
> |
if (r->ro != NULL && isflat(r->ro->otype) && |
269 |
> |
DOT(r->pert,r->pert) <= FTINY*FTINY) |
270 |
|
nd.specfl |= SP_FLAT; |
271 |
|
|
272 |
|
getacoords(r, &nd); /* set up coordinates */ |
306 |
|
} |
307 |
|
|
308 |
|
|
309 |
< |
static |
309 |
> |
static void |
310 |
|
getacoords(r, np) /* set up coordinate system */ |
311 |
|
RAY *r; |
312 |
|
register ANISODAT *np; |
319 |
|
errno = 0; |
320 |
|
for (i = 0; i < 3; i++) |
321 |
|
np->u[i] = evalue(mf->ep[i]); |
322 |
< |
if (errno) { |
322 |
> |
if (errno == EDOM || errno == ERANGE) { |
323 |
|
objerror(np->mp, WARNING, "compute error"); |
324 |
|
np->specfl |= SP_BADU; |
325 |
|
return; |
336 |
|
} |
337 |
|
|
338 |
|
|
339 |
< |
static |
339 |
> |
static void |
340 |
|
agaussamp(r, np) /* sample anisotropic gaussian specular */ |
341 |
|
RAY *r; |
342 |
|
register ANISODAT *np; |
345 |
|
FVECT h; |
346 |
|
double rv[2]; |
347 |
|
double d, sinp, cosp; |
348 |
+ |
int niter; |
349 |
|
register int i; |
350 |
|
/* compute reflection */ |
351 |
|
if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && |
352 |
|
rayorigin(&sr, r, SPECULAR, np->rspec) == 0) { |
353 |
|
dimlist[ndims++] = (int)np->mp; |
354 |
< |
d = urand(ilhash(dimlist,ndims)+samplendx); |
355 |
< |
multisamp(rv, 2, d); |
356 |
< |
d = 2.0*PI * rv[0]; |
357 |
< |
cosp = cos(d) * np->u_alpha; |
358 |
< |
sinp = sin(d) * np->v_alpha; |
359 |
< |
d = sqrt(cosp*cosp + sinp*sinp); |
360 |
< |
cosp /= d; |
361 |
< |
sinp /= 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/(np->u_alpha*np->u_alpha) + |
368 |
< |
sinp*sinp/(np->v_alpha*np->v_alpha))); |
369 |
< |
for (i = 0; i < 3; i++) |
370 |
< |
h[i] = np->pnorm[i] + |
371 |
< |
d*(cosp*np->u[i] + sinp*np->v[i]); |
372 |
< |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
373 |
< |
for (i = 0; i < 3; i++) |
374 |
< |
sr.rdir[i] = r->rdir[i] + d*h[i]; |
375 |
< |
if (DOT(sr.rdir, r->ron) <= FTINY) /* penetration? */ |
376 |
< |
VCOPY(sr.rdir, np->vrefl); /* jitter no good */ |
377 |
< |
rayvalue(&sr); |
378 |
< |
multcolor(sr.rcol, np->scolor); |
379 |
< |
addcolor(r->rcol, sr.rcol); |
354 |
> |
for (niter = 0; niter < MAXITER; niter++) { |
355 |
> |
if (niter) |
356 |
> |
d = frandom(); |
357 |
> |
else |
358 |
> |
d = urand(ilhash(dimlist,ndims)+samplendx); |
359 |
> |
multisamp(rv, 2, d); |
360 |
> |
d = 2.0*PI * rv[0]; |
361 |
> |
cosp = tcos(d) * np->u_alpha; |
362 |
> |
sinp = tsin(d) * np->v_alpha; |
363 |
> |
d = sqrt(cosp*cosp + sinp*sinp); |
364 |
> |
cosp /= d; |
365 |
> |
sinp /= d; |
366 |
> |
rv[1] = 1.0 - specjitter*rv[1]; |
367 |
> |
if (rv[1] <= FTINY) |
368 |
> |
d = 1.0; |
369 |
> |
else |
370 |
> |
d = sqrt(-log(rv[1]) / |
371 |
> |
(cosp*cosp/(np->u_alpha*np->u_alpha) + |
372 |
> |
sinp*sinp/(np->v_alpha*np->v_alpha))); |
373 |
> |
for (i = 0; i < 3; i++) |
374 |
> |
h[i] = np->pnorm[i] + |
375 |
> |
d*(cosp*np->u[i] + sinp*np->v[i]); |
376 |
> |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
377 |
> |
for (i = 0; i < 3; i++) |
378 |
> |
sr.rdir[i] = r->rdir[i] + d*h[i]; |
379 |
> |
if (DOT(sr.rdir, r->ron) > FTINY) { |
380 |
> |
rayvalue(&sr); |
381 |
> |
multcolor(sr.rcol, np->scolor); |
382 |
> |
addcolor(r->rcol, sr.rcol); |
383 |
> |
break; |
384 |
> |
} |
385 |
> |
} |
386 |
|
ndims--; |
387 |
|
} |
388 |
|
/* compute transmission */ |
389 |
|
if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && |
390 |
|
rayorigin(&sr, r, SPECULAR, np->tspec) == 0) { |
391 |
|
dimlist[ndims++] = (int)np->mp; |
392 |
< |
d = urand(ilhash(dimlist,ndims)+1823+samplendx); |
393 |
< |
multisamp(rv, 2, d); |
394 |
< |
d = 2.0*PI * rv[0]; |
395 |
< |
cosp = cos(d) * np->u_alpha; |
396 |
< |
sinp = sin(d) * np->v_alpha; |
397 |
< |
d = sqrt(cosp*cosp + sinp*sinp); |
398 |
< |
cosp /= d; |
399 |
< |
sinp /= d; |
400 |
< |
rv[1] = 1.0 - specjitter*rv[1]; |
401 |
< |
if (rv[1] <= FTINY) |
402 |
< |
d = 1.0; |
403 |
< |
else |
404 |
< |
d = sqrt(-log(rv[1]) / |
405 |
< |
(cosp*cosp/(np->u_alpha*np->u_alpha) + |
406 |
< |
sinp*sinp/(np->v_alpha*np->u_alpha))); |
407 |
< |
for (i = 0; i < 3; i++) |
408 |
< |
sr.rdir[i] = np->prdir[i] + |
409 |
< |
d*(cosp*np->u[i] + sinp*np->v[i]); |
410 |
< |
if (DOT(sr.rdir, r->ron) < -FTINY) |
411 |
< |
normalize(sr.rdir); /* OK, normalize */ |
412 |
< |
else |
413 |
< |
VCOPY(sr.rdir, np->prdir); /* else no jitter */ |
414 |
< |
rayvalue(&sr); |
415 |
< |
scalecolor(sr.rcol, np->tspec); |
416 |
< |
multcolor(sr.rcol, np->mcolor); /* modify by color */ |
417 |
< |
addcolor(r->rcol, sr.rcol); |
392 |
> |
for (niter = 0; niter < MAXITER; niter++) { |
393 |
> |
if (niter) |
394 |
> |
d = frandom(); |
395 |
> |
else |
396 |
> |
d = urand(ilhash(dimlist,ndims)+1823+samplendx); |
397 |
> |
multisamp(rv, 2, d); |
398 |
> |
d = 2.0*PI * rv[0]; |
399 |
> |
cosp = tcos(d) * np->u_alpha; |
400 |
> |
sinp = tsin(d) * np->v_alpha; |
401 |
> |
d = sqrt(cosp*cosp + sinp*sinp); |
402 |
> |
cosp /= d; |
403 |
> |
sinp /= d; |
404 |
> |
rv[1] = 1.0 - specjitter*rv[1]; |
405 |
> |
if (rv[1] <= FTINY) |
406 |
> |
d = 1.0; |
407 |
> |
else |
408 |
> |
d = sqrt(-log(rv[1]) / |
409 |
> |
(cosp*cosp/(np->u_alpha*np->u_alpha) + |
410 |
> |
sinp*sinp/(np->v_alpha*np->v_alpha))); |
411 |
> |
for (i = 0; i < 3; i++) |
412 |
> |
sr.rdir[i] = np->prdir[i] + |
413 |
> |
d*(cosp*np->u[i] + sinp*np->v[i]); |
414 |
> |
if (DOT(sr.rdir, r->ron) < -FTINY) { |
415 |
> |
normalize(sr.rdir); /* OK, normalize */ |
416 |
> |
rayvalue(&sr); |
417 |
> |
scalecolor(sr.rcol, np->tspec); |
418 |
> |
multcolor(sr.rcol, np->mcolor); /* modify */ |
419 |
> |
addcolor(r->rcol, sr.rcol); |
420 |
> |
break; |
421 |
> |
} |
422 |
> |
} |
423 |
|
ndims--; |
424 |
|
} |
425 |
|
} |