1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: normal.c,v 2.48 2004/09/20 17:32:04 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* normal.c - shading function for normal materials. |
6 |
* |
7 |
* 8/19/85 |
8 |
* 12/19/85 - added stuff for metals. |
9 |
* 6/26/87 - improved specular model. |
10 |
* 9/28/87 - added model for translucent materials. |
11 |
* Later changes described in delta comments. |
12 |
*/ |
13 |
|
14 |
#include "copyright.h" |
15 |
|
16 |
#include "ray.h" |
17 |
#include "ambient.h" |
18 |
#include "source.h" |
19 |
#include "otypes.h" |
20 |
#include "rtotypes.h" |
21 |
#include "random.h" |
22 |
|
23 |
#ifndef MAXITER |
24 |
#define MAXITER 10 /* maximum # specular ray attempts */ |
25 |
#endif |
26 |
/* estimate of Fresnel function */ |
27 |
#define FRESNE(ci) (exp(-5.85*(ci)) - 0.00287989916) |
28 |
|
29 |
|
30 |
/* |
31 |
* This routine implements the isotropic Gaussian |
32 |
* model described by Ward in Siggraph `92 article. |
33 |
* We orient the surface towards the incoming ray, so a single |
34 |
* surface can be used to represent an infinitely thin object. |
35 |
* |
36 |
* Arguments for MAT_PLASTIC and MAT_METAL are: |
37 |
* red grn blu specular-frac. facet-slope |
38 |
* |
39 |
* Arguments for MAT_TRANS are: |
40 |
* red grn blu rspec rough trans tspec |
41 |
*/ |
42 |
|
43 |
/* specularity flags */ |
44 |
#define SP_REFL 01 /* has reflected specular component */ |
45 |
#define SP_TRAN 02 /* has transmitted specular */ |
46 |
#define SP_PURE 04 /* purely specular (zero roughness) */ |
47 |
#define SP_FLAT 010 /* flat reflecting surface */ |
48 |
#define SP_RBLT 020 /* reflection below sample threshold */ |
49 |
#define SP_TBLT 040 /* transmission below threshold */ |
50 |
|
51 |
typedef struct { |
52 |
OBJREC *mp; /* material pointer */ |
53 |
RAY *rp; /* ray pointer */ |
54 |
short specfl; /* specularity flags, defined above */ |
55 |
COLOR mcolor; /* color of this material */ |
56 |
COLOR scolor; /* color of specular component */ |
57 |
FVECT vrefl; /* vector in direction of reflected ray */ |
58 |
FVECT prdir; /* vector in transmitted direction */ |
59 |
double alpha2; /* roughness squared */ |
60 |
double rdiff, rspec; /* reflected specular, diffuse */ |
61 |
double trans; /* transmissivity */ |
62 |
double tdiff, tspec; /* transmitted specular, diffuse */ |
63 |
FVECT pnorm; /* perturbed surface normal */ |
64 |
double pdot; /* perturbed dot product */ |
65 |
} NORMDAT; /* normal material data */ |
66 |
|
67 |
static srcdirf_t dirnorm; |
68 |
static void gaussamp(RAY *r, NORMDAT *np); |
69 |
|
70 |
|
71 |
static void |
72 |
dirnorm( /* compute source contribution */ |
73 |
COLOR cval, /* returned coefficient */ |
74 |
void *nnp, /* material data */ |
75 |
FVECT ldir, /* light source direction */ |
76 |
double omega /* light source size */ |
77 |
) |
78 |
{ |
79 |
register NORMDAT *np = nnp; |
80 |
double ldot; |
81 |
double lrdiff, ltdiff; |
82 |
double dtmp, d2; |
83 |
FVECT vtmp; |
84 |
COLOR ctmp; |
85 |
|
86 |
setcolor(cval, 0.0, 0.0, 0.0); |
87 |
|
88 |
ldot = DOT(np->pnorm, ldir); |
89 |
|
90 |
if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY) |
91 |
return; /* wrong side */ |
92 |
|
93 |
/* Fresnel estimate */ |
94 |
lrdiff = np->rdiff; |
95 |
ltdiff = np->tdiff; |
96 |
if (np->specfl & SP_PURE && np->rspec > FTINY && |
97 |
(lrdiff > FTINY) | (ltdiff > FTINY)) { |
98 |
dtmp = 1. - FRESNE(fabs(ldot)); |
99 |
lrdiff *= dtmp; |
100 |
ltdiff *= dtmp; |
101 |
} |
102 |
|
103 |
if (ldot > FTINY && lrdiff > FTINY) { |
104 |
/* |
105 |
* Compute and add diffuse reflected component to returned |
106 |
* color. The diffuse reflected component will always be |
107 |
* modified by the color of the material. |
108 |
*/ |
109 |
copycolor(ctmp, np->mcolor); |
110 |
dtmp = ldot * omega * lrdiff * (1.0/PI); |
111 |
scalecolor(ctmp, dtmp); |
112 |
addcolor(cval, ctmp); |
113 |
} |
114 |
if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) { |
115 |
/* |
116 |
* Compute specular reflection coefficient using |
117 |
* gaussian distribution model. |
118 |
*/ |
119 |
/* roughness */ |
120 |
dtmp = np->alpha2; |
121 |
/* + source if flat */ |
122 |
if (np->specfl & SP_FLAT) |
123 |
dtmp += omega * (0.25/PI); |
124 |
/* half vector */ |
125 |
vtmp[0] = ldir[0] - np->rp->rdir[0]; |
126 |
vtmp[1] = ldir[1] - np->rp->rdir[1]; |
127 |
vtmp[2] = ldir[2] - np->rp->rdir[2]; |
128 |
d2 = DOT(vtmp, np->pnorm); |
129 |
d2 *= d2; |
130 |
d2 = (DOT(vtmp,vtmp) - d2) / d2; |
131 |
/* gaussian */ |
132 |
dtmp = exp(-d2/dtmp)/(4.*PI * np->pdot * dtmp); |
133 |
/* worth using? */ |
134 |
if (dtmp > FTINY) { |
135 |
copycolor(ctmp, np->scolor); |
136 |
dtmp *= omega; |
137 |
scalecolor(ctmp, dtmp); |
138 |
addcolor(cval, ctmp); |
139 |
} |
140 |
} |
141 |
if (ldot < -FTINY && ltdiff > FTINY) { |
142 |
/* |
143 |
* Compute diffuse transmission. |
144 |
*/ |
145 |
copycolor(ctmp, np->mcolor); |
146 |
dtmp = -ldot * omega * ltdiff * (1.0/PI); |
147 |
scalecolor(ctmp, dtmp); |
148 |
addcolor(cval, ctmp); |
149 |
} |
150 |
if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) { |
151 |
/* |
152 |
* Compute specular transmission. Specular transmission |
153 |
* is always modified by material color. |
154 |
*/ |
155 |
/* roughness + source */ |
156 |
dtmp = np->alpha2 + omega*(1.0/PI); |
157 |
/* gaussian */ |
158 |
dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp) / |
159 |
(PI*np->pdot*dtmp); |
160 |
/* worth using? */ |
161 |
if (dtmp > FTINY) { |
162 |
copycolor(ctmp, np->mcolor); |
163 |
dtmp *= np->tspec * omega; |
164 |
scalecolor(ctmp, dtmp); |
165 |
addcolor(cval, ctmp); |
166 |
} |
167 |
} |
168 |
} |
169 |
|
170 |
|
171 |
extern int |
172 |
m_normal( /* color a ray that hit something normal */ |
173 |
register OBJREC *m, |
174 |
register RAY *r |
175 |
) |
176 |
{ |
177 |
NORMDAT nd; |
178 |
double fest; |
179 |
double transtest, transdist; |
180 |
double mirtest, mirdist; |
181 |
int hastexture; |
182 |
double d; |
183 |
COLOR ctmp; |
184 |
register int i; |
185 |
/* easy shadow test */ |
186 |
if (r->crtype & SHADOW && m->otype != MAT_TRANS) |
187 |
return(1); |
188 |
|
189 |
if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) |
190 |
objerror(m, USER, "bad number of arguments"); |
191 |
/* check for back side */ |
192 |
if (r->rod < 0.0) { |
193 |
if (!backvis && m->otype != MAT_TRANS) { |
194 |
raytrans(r); |
195 |
return(1); |
196 |
} |
197 |
raytexture(r, m->omod); |
198 |
flipsurface(r); /* reorient if backvis */ |
199 |
} else |
200 |
raytexture(r, m->omod); |
201 |
nd.mp = m; |
202 |
nd.rp = r; |
203 |
/* get material color */ |
204 |
setcolor(nd.mcolor, m->oargs.farg[0], |
205 |
m->oargs.farg[1], |
206 |
m->oargs.farg[2]); |
207 |
/* get roughness */ |
208 |
nd.specfl = 0; |
209 |
nd.alpha2 = m->oargs.farg[4]; |
210 |
if ((nd.alpha2 *= nd.alpha2) <= FTINY) |
211 |
nd.specfl |= SP_PURE; |
212 |
|
213 |
if ( (hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY)) ) { |
214 |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
215 |
} else { |
216 |
VCOPY(nd.pnorm, r->ron); |
217 |
nd.pdot = r->rod; |
218 |
} |
219 |
if (r->ro != NULL && isflat(r->ro->otype)) |
220 |
nd.specfl |= SP_FLAT; |
221 |
if (nd.pdot < .001) |
222 |
nd.pdot = .001; /* non-zero for dirnorm() */ |
223 |
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
224 |
mirtest = transtest = 0; |
225 |
mirdist = transdist = r->rot; |
226 |
nd.rspec = m->oargs.farg[3]; |
227 |
/* compute Fresnel approx. */ |
228 |
if (nd.specfl & SP_PURE && nd.rspec > FTINY) { |
229 |
fest = FRESNE(r->rod); |
230 |
nd.rspec += fest*(1. - nd.rspec); |
231 |
} else |
232 |
fest = 0.; |
233 |
/* compute transmission */ |
234 |
if (m->otype == MAT_TRANS) { |
235 |
nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec); |
236 |
nd.tspec = nd.trans * m->oargs.farg[6]; |
237 |
nd.tdiff = nd.trans - nd.tspec; |
238 |
if (nd.tspec > FTINY) { |
239 |
nd.specfl |= SP_TRAN; |
240 |
/* check threshold */ |
241 |
if (!(nd.specfl & SP_PURE) && |
242 |
specthresh >= nd.tspec-FTINY) |
243 |
nd.specfl |= SP_TBLT; |
244 |
if (!hastexture || r->crtype & SHADOW) { |
245 |
VCOPY(nd.prdir, r->rdir); |
246 |
transtest = 2; |
247 |
} else { |
248 |
for (i = 0; i < 3; i++) /* perturb */ |
249 |
nd.prdir[i] = r->rdir[i] - r->pert[i]; |
250 |
if (DOT(nd.prdir, r->ron) < -FTINY) |
251 |
normalize(nd.prdir); /* OK */ |
252 |
else |
253 |
VCOPY(nd.prdir, r->rdir); |
254 |
} |
255 |
} |
256 |
} else |
257 |
nd.tdiff = nd.tspec = nd.trans = 0.0; |
258 |
/* transmitted ray */ |
259 |
if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) { |
260 |
RAY lr; |
261 |
if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) { |
262 |
VCOPY(lr.rdir, nd.prdir); |
263 |
rayvalue(&lr); |
264 |
scalecolor(lr.rcol, nd.tspec); |
265 |
multcolor(lr.rcol, nd.mcolor); /* modified by color */ |
266 |
addcolor(r->rcol, lr.rcol); |
267 |
transtest *= bright(lr.rcol); |
268 |
transdist = r->rot + lr.rt; |
269 |
} |
270 |
} else |
271 |
transtest = 0; |
272 |
|
273 |
if (r->crtype & SHADOW) { /* the rest is shadow */ |
274 |
r->rt = transdist; |
275 |
return(1); |
276 |
} |
277 |
/* get specular reflection */ |
278 |
if (nd.rspec > FTINY) { |
279 |
nd.specfl |= SP_REFL; |
280 |
/* compute specular color */ |
281 |
if (m->otype != MAT_METAL) { |
282 |
setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec); |
283 |
} else if (fest > FTINY) { |
284 |
d = nd.rspec*(1. - fest); |
285 |
for (i = 0; i < 3; i++) |
286 |
nd.scolor[i] = fest + nd.mcolor[i]*d; |
287 |
} else { |
288 |
copycolor(nd.scolor, nd.mcolor); |
289 |
scalecolor(nd.scolor, nd.rspec); |
290 |
} |
291 |
/* check threshold */ |
292 |
if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY) |
293 |
nd.specfl |= SP_RBLT; |
294 |
/* compute reflected ray */ |
295 |
for (i = 0; i < 3; i++) |
296 |
nd.vrefl[i] = r->rdir[i] + 2.*nd.pdot*nd.pnorm[i]; |
297 |
/* penetration? */ |
298 |
if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY) |
299 |
for (i = 0; i < 3; i++) /* safety measure */ |
300 |
nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; |
301 |
} |
302 |
/* reflected ray */ |
303 |
if ((nd.specfl&(SP_REFL|SP_PURE|SP_RBLT)) == (SP_REFL|SP_PURE)) { |
304 |
RAY lr; |
305 |
if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { |
306 |
VCOPY(lr.rdir, nd.vrefl); |
307 |
rayvalue(&lr); |
308 |
multcolor(lr.rcol, nd.scolor); |
309 |
addcolor(r->rcol, lr.rcol); |
310 |
if (!hastexture && nd.specfl & SP_FLAT) { |
311 |
mirtest = 2.*bright(lr.rcol); |
312 |
mirdist = r->rot + lr.rt; |
313 |
} |
314 |
} |
315 |
} |
316 |
/* diffuse reflection */ |
317 |
nd.rdiff = 1.0 - nd.trans - nd.rspec; |
318 |
|
319 |
if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) |
320 |
return(1); /* 100% pure specular */ |
321 |
|
322 |
if (!(nd.specfl & SP_PURE)) |
323 |
gaussamp(r, &nd); /* checks *BLT flags */ |
324 |
|
325 |
if (nd.rdiff > FTINY) { /* ambient from this side */ |
326 |
ambient(ctmp, r, hastexture?nd.pnorm:r->ron); |
327 |
if (nd.specfl & SP_RBLT) |
328 |
scalecolor(ctmp, 1.0-nd.trans); |
329 |
else |
330 |
scalecolor(ctmp, nd.rdiff); |
331 |
multcolor(ctmp, nd.mcolor); /* modified by material color */ |
332 |
addcolor(r->rcol, ctmp); /* add to returned color */ |
333 |
} |
334 |
if (nd.tdiff > FTINY) { /* ambient from other side */ |
335 |
flipsurface(r); |
336 |
if (hastexture) { |
337 |
FVECT bnorm; |
338 |
bnorm[0] = -nd.pnorm[0]; |
339 |
bnorm[1] = -nd.pnorm[1]; |
340 |
bnorm[2] = -nd.pnorm[2]; |
341 |
ambient(ctmp, r, bnorm); |
342 |
} else |
343 |
ambient(ctmp, r, r->ron); |
344 |
if (nd.specfl & SP_TBLT) |
345 |
scalecolor(ctmp, nd.trans); |
346 |
else |
347 |
scalecolor(ctmp, nd.tdiff); |
348 |
multcolor(ctmp, nd.mcolor); /* modified by color */ |
349 |
addcolor(r->rcol, ctmp); |
350 |
flipsurface(r); |
351 |
} |
352 |
/* add direct component */ |
353 |
direct(r, dirnorm, &nd); |
354 |
/* check distance */ |
355 |
d = bright(r->rcol); |
356 |
if (transtest > d) |
357 |
r->rt = transdist; |
358 |
else if (mirtest > d) |
359 |
r->rt = mirdist; |
360 |
|
361 |
return(1); |
362 |
} |
363 |
|
364 |
|
365 |
static void |
366 |
gaussamp( /* sample gaussian specular */ |
367 |
RAY *r, |
368 |
register NORMDAT *np |
369 |
) |
370 |
{ |
371 |
RAY sr; |
372 |
FVECT u, v, h; |
373 |
double rv[2]; |
374 |
double d, sinp, cosp; |
375 |
int niter; |
376 |
register int i; |
377 |
/* quick test */ |
378 |
if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL && |
379 |
(np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN) |
380 |
return; |
381 |
/* set up sample coordinates */ |
382 |
v[0] = v[1] = v[2] = 0.0; |
383 |
for (i = 0; i < 3; i++) |
384 |
if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6) |
385 |
break; |
386 |
v[i] = 1.0; |
387 |
fcross(u, v, np->pnorm); |
388 |
normalize(u); |
389 |
fcross(v, np->pnorm, u); |
390 |
/* compute reflection */ |
391 |
if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && |
392 |
rayorigin(&sr, r, SPECULAR, np->rspec) == 0) { |
393 |
dimlist[ndims++] = (int)np->mp; |
394 |
for (niter = 0; niter < MAXITER; niter++) { |
395 |
if (niter) |
396 |
d = frandom(); |
397 |
else |
398 |
d = urand(ilhash(dimlist,ndims)+samplendx); |
399 |
multisamp(rv, 2, d); |
400 |
d = 2.0*PI * rv[0]; |
401 |
cosp = tcos(d); |
402 |
sinp = tsin(d); |
403 |
rv[1] = 1.0 - specjitter*rv[1]; |
404 |
if (rv[1] <= FTINY) |
405 |
d = 1.0; |
406 |
else |
407 |
d = sqrt( np->alpha2 * -log(rv[1]) ); |
408 |
for (i = 0; i < 3; i++) |
409 |
h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]); |
410 |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
411 |
for (i = 0; i < 3; i++) |
412 |
sr.rdir[i] = r->rdir[i] + d*h[i]; |
413 |
if (DOT(sr.rdir, r->ron) > FTINY) { |
414 |
rayvalue(&sr); |
415 |
multcolor(sr.rcol, np->scolor); |
416 |
addcolor(r->rcol, sr.rcol); |
417 |
break; |
418 |
} |
419 |
} |
420 |
ndims--; |
421 |
} |
422 |
/* compute transmission */ |
423 |
if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && |
424 |
rayorigin(&sr, r, SPECULAR, np->tspec) == 0) { |
425 |
dimlist[ndims++] = (int)np->mp; |
426 |
for (niter = 0; niter < MAXITER; niter++) { |
427 |
if (niter) |
428 |
d = frandom(); |
429 |
else |
430 |
d = urand(ilhash(dimlist,ndims)+1823+samplendx); |
431 |
multisamp(rv, 2, d); |
432 |
d = 2.0*PI * rv[0]; |
433 |
cosp = tcos(d); |
434 |
sinp = tsin(d); |
435 |
rv[1] = 1.0 - specjitter*rv[1]; |
436 |
if (rv[1] <= FTINY) |
437 |
d = 1.0; |
438 |
else |
439 |
d = sqrt( np->alpha2 * -log(rv[1]) ); |
440 |
for (i = 0; i < 3; i++) |
441 |
sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]); |
442 |
if (DOT(sr.rdir, r->ron) < -FTINY) { |
443 |
normalize(sr.rdir); /* OK, normalize */ |
444 |
rayvalue(&sr); |
445 |
scalecolor(sr.rcol, np->tspec); |
446 |
multcolor(sr.rcol, np->mcolor); /* modified */ |
447 |
addcolor(r->rcol, sr.rcol); |
448 |
break; |
449 |
} |
450 |
} |
451 |
ndims--; |
452 |
} |
453 |
} |