1 |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Shading functions for anisotropic materials. |
9 |
*/ |
10 |
|
11 |
#include "ray.h" |
12 |
|
13 |
#include "otypes.h" |
14 |
|
15 |
#include "func.h" |
16 |
|
17 |
#include "random.h" |
18 |
|
19 |
extern double specthresh; /* specular sampling threshold */ |
20 |
extern double specjitter; /* specular sampling jitter */ |
21 |
|
22 |
/* |
23 |
* This anisotropic reflection model uses a variant on the |
24 |
* exponential Gaussian used in normal.c. |
25 |
* We orient the surface towards the incoming ray, so a single |
26 |
* surface can be used to represent an infinitely thin object. |
27 |
* |
28 |
* Arguments for MAT_PLASTIC2 and MAT_METAL2 are: |
29 |
* 4+ ux uy uz funcfile [transform...] |
30 |
* 0 |
31 |
* 6 red grn blu specular-frac. u-facet-slope v-facet-slope |
32 |
* |
33 |
* Real arguments for MAT_TRANS2 are: |
34 |
* 8 red grn blu rspec u-rough v-rough trans tspec |
35 |
*/ |
36 |
|
37 |
/* specularity flags */ |
38 |
#define SP_REFL 01 /* has reflected specular component */ |
39 |
#define SP_TRAN 02 /* has transmitted specular */ |
40 |
#define SP_FLAT 04 /* reflecting surface is flat */ |
41 |
#define SP_RBLT 010 /* reflection below sample threshold */ |
42 |
#define SP_TBLT 020 /* transmission below threshold */ |
43 |
#define SP_BADU 040 /* bad u direction calculation */ |
44 |
|
45 |
typedef struct { |
46 |
OBJREC *mp; /* material pointer */ |
47 |
RAY *rp; /* ray pointer */ |
48 |
short specfl; /* specularity flags, defined above */ |
49 |
COLOR mcolor; /* color of this material */ |
50 |
COLOR scolor; /* color of specular component */ |
51 |
FVECT vrefl; /* vector in reflected direction */ |
52 |
FVECT prdir; /* vector in transmitted direction */ |
53 |
FVECT u, v; /* u and v vectors orienting anisotropy */ |
54 |
double u_alpha; /* u roughness */ |
55 |
double v_alpha; /* v roughness */ |
56 |
double rdiff, rspec; /* reflected specular, diffuse */ |
57 |
double trans; /* transmissivity */ |
58 |
double tdiff, tspec; /* transmitted specular, diffuse */ |
59 |
FVECT pnorm; /* perturbed surface normal */ |
60 |
double pdot; /* perturbed dot product */ |
61 |
} ANISODAT; /* anisotropic material data */ |
62 |
|
63 |
|
64 |
diraniso(cval, np, ldir, omega) /* compute source contribution */ |
65 |
COLOR cval; /* returned coefficient */ |
66 |
register ANISODAT *np; /* material data */ |
67 |
FVECT ldir; /* light source direction */ |
68 |
double omega; /* light source size */ |
69 |
{ |
70 |
double ldot; |
71 |
double dtmp, dtmp2; |
72 |
FVECT h; |
73 |
double au2, av2; |
74 |
COLOR ctmp; |
75 |
|
76 |
setcolor(cval, 0.0, 0.0, 0.0); |
77 |
|
78 |
ldot = DOT(np->pnorm, ldir); |
79 |
|
80 |
if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY) |
81 |
return; /* wrong side */ |
82 |
|
83 |
if (ldot > FTINY && np->rdiff > FTINY) { |
84 |
/* |
85 |
* Compute and add diffuse reflected component to returned |
86 |
* color. The diffuse reflected component will always be |
87 |
* modified by the color of the material. |
88 |
*/ |
89 |
copycolor(ctmp, np->mcolor); |
90 |
dtmp = ldot * omega * np->rdiff / PI; |
91 |
scalecolor(ctmp, dtmp); |
92 |
addcolor(cval, ctmp); |
93 |
} |
94 |
if (ldot > FTINY && (np->specfl&(SP_REFL|SP_BADU)) == SP_REFL) { |
95 |
/* |
96 |
* Compute specular reflection coefficient using |
97 |
* anisotropic gaussian distribution model. |
98 |
*/ |
99 |
/* add source width if flat */ |
100 |
if (np->specfl & SP_FLAT) |
101 |
au2 = av2 = omega/(4.0*PI); |
102 |
else |
103 |
au2 = av2 = 0.0; |
104 |
au2 += np->u_alpha * np->u_alpha; |
105 |
av2 += np->v_alpha * np->v_alpha; |
106 |
/* half vector */ |
107 |
h[0] = ldir[0] - np->rp->rdir[0]; |
108 |
h[1] = ldir[1] - np->rp->rdir[1]; |
109 |
h[2] = ldir[2] - np->rp->rdir[2]; |
110 |
normalize(h); |
111 |
/* ellipse */ |
112 |
dtmp = DOT(np->u, h); |
113 |
dtmp *= dtmp / au2; |
114 |
dtmp2 = DOT(np->v, h); |
115 |
dtmp2 *= dtmp2 / av2; |
116 |
/* gaussian */ |
117 |
dtmp = (dtmp + dtmp2) / (1.0 + DOT(np->pnorm, h)); |
118 |
dtmp = exp(-2.0*dtmp) / (4.0*PI * sqrt(au2*av2)); |
119 |
/* worth using? */ |
120 |
if (dtmp > FTINY) { |
121 |
copycolor(ctmp, np->scolor); |
122 |
dtmp *= omega / np->pdot; |
123 |
scalecolor(ctmp, dtmp); |
124 |
addcolor(cval, ctmp); |
125 |
} |
126 |
} |
127 |
if (ldot < -FTINY && np->tdiff > FTINY) { |
128 |
/* |
129 |
* Compute diffuse transmission. |
130 |
*/ |
131 |
copycolor(ctmp, np->mcolor); |
132 |
dtmp = -ldot * omega * np->tdiff / PI; |
133 |
scalecolor(ctmp, dtmp); |
134 |
addcolor(cval, ctmp); |
135 |
} |
136 |
if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_BADU)) == SP_TRAN) { |
137 |
/* |
138 |
* Compute specular transmission. Specular transmission |
139 |
* is always modified by material color. |
140 |
*/ |
141 |
/* roughness + source */ |
142 |
/* gaussian */ |
143 |
dtmp = 0.0; |
144 |
/* worth using? */ |
145 |
if (dtmp > FTINY) { |
146 |
copycolor(ctmp, np->mcolor); |
147 |
dtmp *= np->tspec * omega / np->pdot; |
148 |
scalecolor(ctmp, dtmp); |
149 |
addcolor(cval, ctmp); |
150 |
} |
151 |
} |
152 |
} |
153 |
|
154 |
|
155 |
m_aniso(m, r) /* shade ray that hit something anisotropic */ |
156 |
register OBJREC *m; |
157 |
register RAY *r; |
158 |
{ |
159 |
ANISODAT nd; |
160 |
double dtmp; |
161 |
COLOR ctmp; |
162 |
register int i; |
163 |
/* easy shadow test */ |
164 |
if (r->crtype & SHADOW) |
165 |
return; |
166 |
|
167 |
if (m->oargs.nfargs != (m->otype == MAT_TRANS2 ? 8 : 6)) |
168 |
objerror(m, USER, "bad number of real arguments"); |
169 |
nd.mp = m; |
170 |
nd.rp = r; |
171 |
/* get material color */ |
172 |
setcolor(nd.mcolor, m->oargs.farg[0], |
173 |
m->oargs.farg[1], |
174 |
m->oargs.farg[2]); |
175 |
/* get roughness */ |
176 |
nd.specfl = 0; |
177 |
nd.u_alpha = m->oargs.farg[4]; |
178 |
nd.v_alpha = m->oargs.farg[5]; |
179 |
if (nd.u_alpha < 1e-6 || nd.v_alpha <= 1e-6) |
180 |
objerror(m, USER, "roughness too small"); |
181 |
/* reorient if necessary */ |
182 |
if (r->rod < 0.0) |
183 |
flipsurface(r); |
184 |
/* get modifiers */ |
185 |
raytexture(r, m->omod); |
186 |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
187 |
if (nd.pdot < .001) |
188 |
nd.pdot = .001; /* non-zero for diraniso() */ |
189 |
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
190 |
/* get specular component */ |
191 |
if ((nd.rspec = m->oargs.farg[3]) > FTINY) { |
192 |
nd.specfl |= SP_REFL; |
193 |
/* compute specular color */ |
194 |
if (m->otype == MAT_METAL2) |
195 |
copycolor(nd.scolor, nd.mcolor); |
196 |
else |
197 |
setcolor(nd.scolor, 1.0, 1.0, 1.0); |
198 |
scalecolor(nd.scolor, nd.rspec); |
199 |
/* check threshold */ |
200 |
if (specthresh > FTINY && |
201 |
(specthresh >= 1.-FTINY || |
202 |
specthresh > nd.rspec)) |
203 |
nd.specfl |= SP_RBLT; |
204 |
/* compute refl. direction */ |
205 |
for (i = 0; i < 3; i++) |
206 |
nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i]; |
207 |
if (DOT(nd.vrefl, r->ron) <= FTINY) /* penetration? */ |
208 |
for (i = 0; i < 3; i++) /* safety measure */ |
209 |
nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; |
210 |
} |
211 |
/* compute transmission */ |
212 |
if (m->otype == MAT_TRANS) { |
213 |
nd.trans = m->oargs.farg[6]*(1.0 - nd.rspec); |
214 |
nd.tspec = nd.trans * m->oargs.farg[7]; |
215 |
nd.tdiff = nd.trans - nd.tspec; |
216 |
if (nd.tspec > FTINY) { |
217 |
nd.specfl |= SP_TRAN; |
218 |
/* check threshold */ |
219 |
if (specthresh > FTINY && |
220 |
(specthresh >= 1.-FTINY || |
221 |
specthresh > nd.tspec)) |
222 |
nd.specfl |= SP_TBLT; |
223 |
if (DOT(r->pert,r->pert) <= FTINY*FTINY) { |
224 |
VCOPY(nd.prdir, r->rdir); |
225 |
} else { |
226 |
for (i = 0; i < 3; i++) /* perturb */ |
227 |
nd.prdir[i] = r->rdir[i] - |
228 |
0.5*r->pert[i]; |
229 |
if (DOT(nd.prdir, r->ron) < -FTINY) |
230 |
normalize(nd.prdir); /* OK */ |
231 |
else |
232 |
VCOPY(nd.prdir, r->rdir); |
233 |
} |
234 |
} |
235 |
} else |
236 |
nd.tdiff = nd.tspec = nd.trans = 0.0; |
237 |
|
238 |
/* diffuse reflection */ |
239 |
nd.rdiff = 1.0 - nd.trans - nd.rspec; |
240 |
|
241 |
if (r->ro != NULL && (r->ro->otype == OBJ_FACE || |
242 |
r->ro->otype == OBJ_RING)) |
243 |
nd.specfl |= SP_FLAT; |
244 |
|
245 |
getacoords(r, &nd); /* set up coordinates */ |
246 |
|
247 |
if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_BADU)) |
248 |
agaussamp(r, &nd); |
249 |
|
250 |
if (nd.rdiff > FTINY) { /* ambient from this side */ |
251 |
ambient(ctmp, r); |
252 |
if (nd.specfl & SP_RBLT) |
253 |
scalecolor(ctmp, 1.0-nd.trans); |
254 |
else |
255 |
scalecolor(ctmp, nd.rdiff); |
256 |
multcolor(ctmp, nd.mcolor); /* modified by material color */ |
257 |
addcolor(r->rcol, ctmp); /* add to returned color */ |
258 |
} |
259 |
if (nd.tdiff > FTINY) { /* ambient from other side */ |
260 |
flipsurface(r); |
261 |
ambient(ctmp, r); |
262 |
if (nd.specfl & SP_TBLT) |
263 |
scalecolor(ctmp, nd.trans); |
264 |
else |
265 |
scalecolor(ctmp, nd.tdiff); |
266 |
multcolor(ctmp, nd.mcolor); /* modified by color */ |
267 |
addcolor(r->rcol, ctmp); |
268 |
flipsurface(r); |
269 |
} |
270 |
/* add direct component */ |
271 |
direct(r, diraniso, &nd); |
272 |
} |
273 |
|
274 |
|
275 |
static |
276 |
getacoords(r, np) /* set up coordinate system */ |
277 |
RAY *r; |
278 |
register ANISODAT *np; |
279 |
{ |
280 |
register MFUNC *mf; |
281 |
register int i; |
282 |
|
283 |
mf = getfunc(np->mp, 3, 0x7, 1); |
284 |
setfunc(np->mp, r); |
285 |
errno = 0; |
286 |
for (i = 0; i < 3; i++) |
287 |
np->u[i] = evalue(mf->ep[i]); |
288 |
if (errno) { |
289 |
objerror(np->mp, WARNING, "compute error"); |
290 |
np->specfl |= SP_BADU; |
291 |
return; |
292 |
} |
293 |
multv3(np->u, np->u, mf->f->xfm); |
294 |
fcross(np->v, np->pnorm, np->u); |
295 |
if (normalize(np->v) == 0.0) { |
296 |
objerror(np->mp, WARNING, "illegal orientation vector"); |
297 |
np->specfl |= SP_BADU; |
298 |
return; |
299 |
} |
300 |
fcross(np->u, np->v, np->pnorm); |
301 |
} |
302 |
|
303 |
|
304 |
static |
305 |
agaussamp(r, np) /* sample anisotropic gaussian specular */ |
306 |
RAY *r; |
307 |
register ANISODAT *np; |
308 |
{ |
309 |
RAY sr; |
310 |
FVECT h; |
311 |
double rv[2]; |
312 |
double d, sinp, cosp; |
313 |
register int i; |
314 |
/* compute reflection */ |
315 |
if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && |
316 |
rayorigin(&sr, r, SPECULAR, np->rspec) == 0) { |
317 |
dimlist[ndims++] = (int)np->mp; |
318 |
d = urand(ilhash(dimlist,ndims)+samplendx); |
319 |
multisamp(rv, 2, d); |
320 |
d = 2.0*PI * rv[0]; |
321 |
cosp = np->u_alpha * cos(d); |
322 |
sinp = np->v_alpha * sin(d); |
323 |
d = sqrt(cosp*cosp + sinp*sinp); |
324 |
cosp /= d; |
325 |
sinp /= d; |
326 |
rv[1] = 1.0 - specjitter*rv[1]; |
327 |
if (rv[1] <= FTINY) |
328 |
d = 1.0; |
329 |
else |
330 |
d = sqrt(-log(rv[1]) / |
331 |
(cosp*cosp/(np->u_alpha*np->u_alpha) + |
332 |
sinp*sinp/(np->v_alpha*np->v_alpha))); |
333 |
for (i = 0; i < 3; i++) |
334 |
h[i] = np->pnorm[i] + |
335 |
d*(cosp*np->u[i] + sinp*np->v[i]); |
336 |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
337 |
for (i = 0; i < 3; i++) |
338 |
sr.rdir[i] = r->rdir[i] + d*h[i]; |
339 |
if (DOT(sr.rdir, r->ron) <= FTINY) /* penetration? */ |
340 |
VCOPY(sr.rdir, np->vrefl); /* jitter no good */ |
341 |
rayvalue(&sr); |
342 |
multcolor(sr.rcol, np->scolor); |
343 |
addcolor(r->rcol, sr.rcol); |
344 |
ndims--; |
345 |
} |
346 |
/* compute transmission */ |
347 |
if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && |
348 |
rayorigin(&sr, r, SPECULAR, np->tspec) == 0) { |
349 |
dimlist[ndims++] = (int)np->mp; |
350 |
d = urand(ilhash(dimlist,ndims)+1823+samplendx); |
351 |
multisamp(rv, 2, d); |
352 |
d = 2.0*PI * rv[0]; |
353 |
cosp = cos(d); |
354 |
sinp = sin(d); |
355 |
rv[1] = 1.0 - specjitter*rv[1]; |
356 |
if (rv[1] <= FTINY) |
357 |
d = 1.0; |
358 |
else |
359 |
d = sqrt(-log(rv[1]) / |
360 |
(cosp*cosp*4./(np->u_alpha*np->u_alpha) + |
361 |
sinp*sinp*4./(np->v_alpha*np->v_alpha))); |
362 |
for (i = 0; i < 3; i++) |
363 |
sr.rdir[i] = np->prdir[i] + |
364 |
d*(cosp*np->u[i] + sinp*np->v[i]); |
365 |
if (DOT(sr.rdir, r->ron) < -FTINY) |
366 |
normalize(sr.rdir); /* OK, normalize */ |
367 |
else |
368 |
VCOPY(sr.rdir, np->prdir); /* else no jitter */ |
369 |
rayvalue(&sr); |
370 |
scalecolor(sr.rcol, np->tspec); |
371 |
multcolor(sr.rcol, np->mcolor); /* modify by color */ |
372 |
addcolor(r->rcol, sr.rcol); |
373 |
ndims--; |
374 |
} |
375 |
} |