1 |
– |
/* Copyright (c) 1996 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 "ray.h" |
8 |
> |
#include "copyright.h" |
9 |
|
|
10 |
+ |
#include "ray.h" |
11 |
+ |
#include "ambient.h" |
12 |
|
#include "otypes.h" |
13 |
< |
|
13 |
> |
#include "rtotypes.h" |
14 |
> |
#include "source.h" |
15 |
|
#include "func.h" |
16 |
– |
|
16 |
|
#include "random.h" |
17 |
|
|
19 |
– |
extern double specthresh; /* specular sampling threshold */ |
20 |
– |
extern double specjitter; /* specular sampling jitter */ |
21 |
– |
|
22 |
– |
extern int backvis; /* back faces visible? */ |
23 |
– |
|
18 |
|
#ifndef MAXITER |
19 |
|
#define MAXITER 10 /* maximum # specular ray attempts */ |
20 |
|
#endif |
21 |
|
|
28 |
– |
static agaussamp(), getacoords(); |
29 |
– |
|
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 srcdirf_t diraniso; |
64 |
+ |
static void getacoords(RAY *r, ANISODAT *np); |
65 |
+ |
static void agaussamp(RAY *r, ANISODAT *np); |
66 |
|
|
67 |
< |
diraniso(cval, np, ldir, omega) /* compute source contribution */ |
68 |
< |
COLOR cval; /* returned coefficient */ |
69 |
< |
register ANISODAT *np; /* material data */ |
70 |
< |
FVECT ldir; /* light source direction */ |
71 |
< |
double omega; /* light source size */ |
67 |
> |
|
68 |
> |
static void |
69 |
> |
diraniso( /* compute source contribution */ |
70 |
> |
COLOR cval, /* returned coefficient */ |
71 |
> |
void *nnp, /* material data */ |
72 |
> |
FVECT ldir, /* light source direction */ |
73 |
> |
double omega /* light source size */ |
74 |
> |
) |
75 |
|
{ |
76 |
+ |
register ANISODAT *np = nnp; |
77 |
|
double ldot; |
78 |
|
double dtmp, dtmp1, dtmp2; |
79 |
|
FVECT h; |
94 |
|
* modified by the color of the material. |
95 |
|
*/ |
96 |
|
copycolor(ctmp, np->mcolor); |
97 |
< |
dtmp = ldot * omega * np->rdiff / PI; |
97 |
> |
dtmp = ldot * omega * np->rdiff * (1.0/PI); |
98 |
|
scalecolor(ctmp, dtmp); |
99 |
|
addcolor(cval, ctmp); |
100 |
|
} |
105 |
|
*/ |
106 |
|
/* add source width if flat */ |
107 |
|
if (np->specfl & SP_FLAT) |
108 |
< |
au2 = av2 = omega/(4.0*PI); |
108 |
> |
au2 = av2 = omega * (0.25/PI); |
109 |
|
else |
110 |
|
au2 = av2 = 0.0; |
111 |
|
au2 += np->u_alpha*np->u_alpha; |
122 |
|
/* gaussian */ |
123 |
|
dtmp = DOT(np->pnorm, h); |
124 |
|
dtmp = (dtmp1 + dtmp2) / (dtmp*dtmp); |
125 |
< |
dtmp = exp(-dtmp) * (0.25/PI) |
127 |
< |
* sqrt(ldot/(np->pdot*au2*av2)); |
125 |
> |
dtmp = exp(-dtmp) / (4.0*PI * np->pdot * sqrt(au2*av2)); |
126 |
|
/* worth using? */ |
127 |
|
if (dtmp > FTINY) { |
128 |
|
copycolor(ctmp, np->scolor); |
136 |
|
* Compute diffuse transmission. |
137 |
|
*/ |
138 |
|
copycolor(ctmp, np->mcolor); |
139 |
< |
dtmp = -ldot * omega * np->tdiff / PI; |
139 |
> |
dtmp = -ldot * omega * np->tdiff * (1.0/PI); |
140 |
|
scalecolor(ctmp, dtmp); |
141 |
|
addcolor(cval, ctmp); |
142 |
|
} |
146 |
|
* is always modified by material color. |
147 |
|
*/ |
148 |
|
/* roughness + source */ |
149 |
< |
au2 = av2 = omega / PI; |
149 |
> |
au2 = av2 = omega * (1.0/PI); |
150 |
|
au2 += np->u_alpha*np->u_alpha; |
151 |
|
av2 += np->v_alpha*np->v_alpha; |
152 |
|
/* "half vector" */ |
167 |
|
} else |
168 |
|
dtmp = 0.0; |
169 |
|
/* gaussian */ |
170 |
< |
dtmp = exp(-dtmp) * (1.0/PI) |
173 |
< |
* sqrt(-ldot/(np->pdot*au2*av2)); |
170 |
> |
dtmp = exp(-dtmp) / (PI * np->pdot * sqrt(au2*av2)); |
171 |
|
/* worth using? */ |
172 |
|
if (dtmp > FTINY) { |
173 |
|
copycolor(ctmp, np->mcolor); |
179 |
|
} |
180 |
|
|
181 |
|
|
182 |
< |
m_aniso(m, r) /* shade ray that hit something anisotropic */ |
183 |
< |
register OBJREC *m; |
184 |
< |
register RAY *r; |
182 |
> |
extern int |
183 |
> |
m_aniso( /* shade ray that hit something anisotropic */ |
184 |
> |
register OBJREC *m, |
185 |
> |
register RAY *r |
186 |
> |
) |
187 |
|
{ |
188 |
|
ANISODAT nd; |
189 |
|
COLOR ctmp; |
194 |
|
|
195 |
|
if (m->oargs.nfargs != (m->otype == MAT_TRANS2 ? 8 : 6)) |
196 |
|
objerror(m, USER, "bad number of real arguments"); |
197 |
+ |
/* check for back side */ |
198 |
+ |
if (r->rod < 0.0) { |
199 |
+ |
if (!backvis && m->otype != MAT_TRANS2) { |
200 |
+ |
raytrans(r); |
201 |
+ |
return(1); |
202 |
+ |
} |
203 |
+ |
raytexture(r, m->omod); |
204 |
+ |
flipsurface(r); /* reorient if backvis */ |
205 |
+ |
} else |
206 |
+ |
raytexture(r, m->omod); |
207 |
+ |
/* get material color */ |
208 |
|
nd.mp = m; |
209 |
|
nd.rp = r; |
200 |
– |
/* get material color */ |
210 |
|
setcolor(nd.mcolor, m->oargs.farg[0], |
211 |
|
m->oargs.farg[1], |
212 |
|
m->oargs.farg[2]); |
216 |
|
nd.v_alpha = m->oargs.farg[5]; |
217 |
|
if (nd.u_alpha < FTINY || nd.v_alpha <= FTINY) |
218 |
|
objerror(m, USER, "roughness too small"); |
219 |
< |
/* check for back side */ |
211 |
< |
if (r->rod < 0.0) { |
212 |
< |
if (!backvis && m->otype != MAT_TRANS2) { |
213 |
< |
raytrans(r); |
214 |
< |
return(1); |
215 |
< |
} |
216 |
< |
flipsurface(r); /* reorient if backvis */ |
217 |
< |
} |
218 |
< |
/* get modifiers */ |
219 |
< |
raytexture(r, m->omod); |
219 |
> |
|
220 |
|
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
221 |
|
if (nd.pdot < .001) |
222 |
|
nd.pdot = .001; /* non-zero for diraniso() */ |
307 |
|
} |
308 |
|
|
309 |
|
|
310 |
< |
static |
311 |
< |
getacoords(r, np) /* set up coordinate system */ |
312 |
< |
RAY *r; |
313 |
< |
register ANISODAT *np; |
310 |
> |
static void |
311 |
> |
getacoords( /* set up coordinate system */ |
312 |
> |
RAY *r, |
313 |
> |
register ANISODAT *np |
314 |
> |
) |
315 |
|
{ |
316 |
|
register MFUNC *mf; |
317 |
|
register int i; |
321 |
|
errno = 0; |
322 |
|
for (i = 0; i < 3; i++) |
323 |
|
np->u[i] = evalue(mf->ep[i]); |
324 |
< |
if (errno) { |
324 |
> |
if (errno == EDOM || errno == ERANGE) { |
325 |
|
objerror(np->mp, WARNING, "compute error"); |
326 |
|
np->specfl |= SP_BADU; |
327 |
|
return; |
338 |
|
} |
339 |
|
|
340 |
|
|
341 |
< |
static |
342 |
< |
agaussamp(r, np) /* sample anisotropic gaussian specular */ |
343 |
< |
RAY *r; |
344 |
< |
register ANISODAT *np; |
341 |
> |
static void |
342 |
> |
agaussamp( /* sample anisotropic gaussian specular */ |
343 |
> |
RAY *r, |
344 |
> |
register ANISODAT *np |
345 |
> |
) |
346 |
|
{ |
347 |
|
RAY sr; |
348 |
|
FVECT h; |
361 |
|
d = urand(ilhash(dimlist,ndims)+samplendx); |
362 |
|
multisamp(rv, 2, d); |
363 |
|
d = 2.0*PI * rv[0]; |
364 |
< |
cosp = cos(d) * np->u_alpha; |
365 |
< |
sinp = sin(d) * np->v_alpha; |
364 |
> |
cosp = tcos(d) * np->u_alpha; |
365 |
> |
sinp = tsin(d) * np->v_alpha; |
366 |
|
d = sqrt(cosp*cosp + sinp*sinp); |
367 |
|
cosp /= d; |
368 |
|
sinp /= d; |
399 |
|
d = urand(ilhash(dimlist,ndims)+1823+samplendx); |
400 |
|
multisamp(rv, 2, d); |
401 |
|
d = 2.0*PI * rv[0]; |
402 |
< |
cosp = cos(d) * np->u_alpha; |
403 |
< |
sinp = sin(d) * np->v_alpha; |
402 |
> |
cosp = tcos(d) * np->u_alpha; |
403 |
> |
sinp = tsin(d) * np->v_alpha; |
404 |
|
d = sqrt(cosp*cosp + sinp*sinp); |
405 |
|
cosp /= d; |
406 |
|
sinp /= d; |
410 |
|
else |
411 |
|
d = sqrt(-log(rv[1]) / |
412 |
|
(cosp*cosp/(np->u_alpha*np->u_alpha) + |
413 |
< |
sinp*sinp/(np->v_alpha*np->u_alpha))); |
413 |
> |
sinp*sinp/(np->v_alpha*np->v_alpha))); |
414 |
|
for (i = 0; i < 3; i++) |
415 |
|
sr.rdir[i] = np->prdir[i] + |
416 |
|
d*(cosp*np->u[i] + sinp*np->v[i]); |