1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: normal.c,v 2.66 2014/01/25 18:27:39 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 |
#define FRESTHRESH 0.017999 /* minimum specularity for approx. */ |
29 |
|
30 |
|
31 |
/* |
32 |
* This routine implements the isotropic Gaussian |
33 |
* model described by Ward in Siggraph `92 article. |
34 |
* We orient the surface towards the incoming ray, so a single |
35 |
* surface can be used to represent an infinitely thin object. |
36 |
* |
37 |
* Arguments for MAT_PLASTIC and MAT_METAL are: |
38 |
* red grn blu specular-frac. facet-slope |
39 |
* |
40 |
* Arguments for MAT_TRANS are: |
41 |
* red grn blu rspec rough trans tspec |
42 |
*/ |
43 |
|
44 |
/* specularity flags */ |
45 |
#define SP_REFL 01 /* has reflected specular component */ |
46 |
#define SP_TRAN 02 /* has transmitted specular */ |
47 |
#define SP_PURE 04 /* purely specular (zero roughness) */ |
48 |
#define SP_FLAT 010 /* flat reflecting surface */ |
49 |
#define SP_RBLT 020 /* reflection below sample threshold */ |
50 |
#define SP_TBLT 040 /* transmission below threshold */ |
51 |
|
52 |
typedef struct { |
53 |
OBJREC *mp; /* material pointer */ |
54 |
RAY *rp; /* ray pointer */ |
55 |
short specfl; /* specularity flags, defined above */ |
56 |
COLOR mcolor; /* color of this material */ |
57 |
COLOR scolor; /* color of specular component */ |
58 |
FVECT vrefl; /* vector in direction of reflected ray */ |
59 |
FVECT prdir; /* vector in transmitted direction */ |
60 |
double alpha2; /* roughness squared */ |
61 |
double rdiff, rspec; /* reflected specular, diffuse */ |
62 |
double trans; /* transmissivity */ |
63 |
double tdiff, tspec; /* transmitted specular, diffuse */ |
64 |
FVECT pnorm; /* perturbed surface normal */ |
65 |
double pdot; /* perturbed dot product */ |
66 |
} NORMDAT; /* normal material data */ |
67 |
|
68 |
static void gaussamp(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 |
NORMDAT *np = nnp; |
80 |
double ldot; |
81 |
double lrdiff, ltdiff; |
82 |
double dtmp, d2, d3, d4; |
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 >= FRESTHRESH && |
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 |
VSUB(vtmp, ldir, np->rp->rdir); |
126 |
d2 = DOT(vtmp, np->pnorm); |
127 |
d2 *= d2; |
128 |
d3 = DOT(vtmp,vtmp); |
129 |
d4 = (d3 - d2) / d2; |
130 |
/* new W-G-M-D model */ |
131 |
dtmp = exp(-d4/dtmp) * d3 / (PI * d2*d2 * dtmp); |
132 |
/* worth using? */ |
133 |
if (dtmp > FTINY) { |
134 |
copycolor(ctmp, np->scolor); |
135 |
dtmp *= ldot * omega; |
136 |
scalecolor(ctmp, dtmp); |
137 |
addcolor(cval, ctmp); |
138 |
} |
139 |
} |
140 |
if (ldot < -FTINY && ltdiff > FTINY) { |
141 |
/* |
142 |
* Compute diffuse transmission. |
143 |
*/ |
144 |
copycolor(ctmp, np->mcolor); |
145 |
dtmp = -ldot * omega * ltdiff * (1.0/PI); |
146 |
scalecolor(ctmp, dtmp); |
147 |
addcolor(cval, ctmp); |
148 |
} |
149 |
if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) { |
150 |
/* |
151 |
* Compute specular transmission. Specular transmission |
152 |
* is always modified by material color. |
153 |
*/ |
154 |
/* roughness + source */ |
155 |
dtmp = np->alpha2 + omega*(1.0/PI); |
156 |
/* Gaussian */ |
157 |
dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp); |
158 |
/* worth using? */ |
159 |
if (dtmp > FTINY) { |
160 |
copycolor(ctmp, np->mcolor); |
161 |
dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot); |
162 |
scalecolor(ctmp, dtmp); |
163 |
addcolor(cval, ctmp); |
164 |
} |
165 |
} |
166 |
} |
167 |
|
168 |
|
169 |
int |
170 |
m_normal( /* color a ray that hit something normal */ |
171 |
OBJREC *m, |
172 |
RAY *r |
173 |
) |
174 |
{ |
175 |
NORMDAT nd; |
176 |
double fest; |
177 |
double transtest, transdist; |
178 |
double mirtest, mirdist; |
179 |
int hastexture; |
180 |
double d; |
181 |
COLOR ctmp; |
182 |
int i; |
183 |
/* easy shadow test */ |
184 |
if (r->crtype & SHADOW && m->otype != MAT_TRANS) |
185 |
return(1); |
186 |
|
187 |
if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) |
188 |
objerror(m, USER, "bad number of arguments"); |
189 |
/* check for back side */ |
190 |
if (r->rod < 0.0) { |
191 |
if (!backvis) { |
192 |
raytrans(r); |
193 |
return(1); |
194 |
} |
195 |
raytexture(r, m->omod); |
196 |
flipsurface(r); /* reorient if backvis */ |
197 |
} else |
198 |
raytexture(r, m->omod); |
199 |
nd.mp = m; |
200 |
nd.rp = r; |
201 |
/* get material color */ |
202 |
setcolor(nd.mcolor, m->oargs.farg[0], |
203 |
m->oargs.farg[1], |
204 |
m->oargs.farg[2]); |
205 |
/* get roughness */ |
206 |
nd.specfl = 0; |
207 |
nd.alpha2 = m->oargs.farg[4]; |
208 |
if ((nd.alpha2 *= nd.alpha2) <= FTINY) |
209 |
nd.specfl |= SP_PURE; |
210 |
|
211 |
if ( (hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY)) ) { |
212 |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
213 |
} else { |
214 |
VCOPY(nd.pnorm, r->ron); |
215 |
nd.pdot = r->rod; |
216 |
} |
217 |
if (r->ro != NULL && isflat(r->ro->otype)) |
218 |
nd.specfl |= SP_FLAT; |
219 |
if (nd.pdot < .001) |
220 |
nd.pdot = .001; /* non-zero for dirnorm() */ |
221 |
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
222 |
mirtest = transtest = 0; |
223 |
mirdist = transdist = r->rot; |
224 |
nd.rspec = m->oargs.farg[3]; |
225 |
/* compute Fresnel approx. */ |
226 |
if (nd.specfl & SP_PURE && nd.rspec >= FRESTHRESH) { |
227 |
fest = FRESNE(nd.pdot); |
228 |
nd.rspec += fest*(1. - nd.rspec); |
229 |
} else |
230 |
fest = 0.; |
231 |
/* compute transmission */ |
232 |
if (m->otype == MAT_TRANS) { |
233 |
nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec); |
234 |
nd.tspec = nd.trans * m->oargs.farg[6]; |
235 |
nd.tdiff = nd.trans - nd.tspec; |
236 |
if (nd.tspec > FTINY) { |
237 |
nd.specfl |= SP_TRAN; |
238 |
/* check threshold */ |
239 |
if (!(nd.specfl & SP_PURE) && |
240 |
specthresh >= nd.tspec-FTINY) |
241 |
nd.specfl |= SP_TBLT; |
242 |
if (!hastexture || r->crtype & (SHADOW|AMBIENT)) { |
243 |
VCOPY(nd.prdir, r->rdir); |
244 |
transtest = 2; |
245 |
} else { |
246 |
for (i = 0; i < 3; i++) /* perturb */ |
247 |
nd.prdir[i] = r->rdir[i] - r->pert[i]; |
248 |
if (DOT(nd.prdir, r->ron) < -FTINY) |
249 |
normalize(nd.prdir); /* OK */ |
250 |
else |
251 |
VCOPY(nd.prdir, r->rdir); |
252 |
} |
253 |
} |
254 |
} else |
255 |
nd.tdiff = nd.tspec = nd.trans = 0.0; |
256 |
/* transmitted ray */ |
257 |
if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) { |
258 |
RAY lr; |
259 |
copycolor(lr.rcoef, nd.mcolor); /* modified by color */ |
260 |
scalecolor(lr.rcoef, nd.tspec); |
261 |
if (rayorigin(&lr, TRANS, r, lr.rcoef) == 0) { |
262 |
VCOPY(lr.rdir, nd.prdir); |
263 |
rayvalue(&lr); |
264 |
multcolor(lr.rcol, lr.rcoef); |
265 |
addcolor(r->rcol, lr.rcol); |
266 |
transtest *= bright(lr.rcol); |
267 |
transdist = r->rot + lr.rt; |
268 |
} |
269 |
} else |
270 |
transtest = 0; |
271 |
|
272 |
if (r->crtype & SHADOW) { /* the rest is shadow */ |
273 |
r->rt = transdist; |
274 |
return(1); |
275 |
} |
276 |
/* get specular reflection */ |
277 |
if (nd.rspec > FTINY) { |
278 |
nd.specfl |= SP_REFL; |
279 |
/* compute specular color */ |
280 |
if (m->otype != MAT_METAL) { |
281 |
setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec); |
282 |
} else if (fest > FTINY) { |
283 |
d = m->oargs.farg[3]*(1. - fest); |
284 |
for (i = 0; i < 3; i++) |
285 |
colval(nd.scolor,i) = fest + |
286 |
colval(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 |
VSUM(nd.vrefl, r->rdir, nd.pnorm, 2.*nd.pdot); |
296 |
/* penetration? */ |
297 |
if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY) |
298 |
VSUM(nd.vrefl, r->rdir, r->ron, 2.*r->rod); |
299 |
checknorm(nd.vrefl); |
300 |
} |
301 |
/* reflected ray */ |
302 |
if ((nd.specfl&(SP_REFL|SP_PURE|SP_RBLT)) == (SP_REFL|SP_PURE)) { |
303 |
RAY lr; |
304 |
if (rayorigin(&lr, REFLECTED, r, nd.scolor) == 0) { |
305 |
VCOPY(lr.rdir, nd.vrefl); |
306 |
rayvalue(&lr); |
307 |
multcolor(lr.rcol, lr.rcoef); |
308 |
addcolor(r->rcol, lr.rcol); |
309 |
if (nd.specfl & SP_FLAT && |
310 |
!hastexture | (r->crtype & AMBIENT)) { |
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(&nd); /* checks *BLT flags */ |
324 |
|
325 |
if (nd.rdiff > FTINY) { /* ambient from this side */ |
326 |
copycolor(ctmp, nd.mcolor); /* modified by material color */ |
327 |
scalecolor(ctmp, nd.rdiff); |
328 |
if (nd.specfl & SP_RBLT) /* add in specular as well? */ |
329 |
addcolor(ctmp, nd.scolor); |
330 |
multambient(ctmp, r, hastexture ? nd.pnorm : r->ron); |
331 |
addcolor(r->rcol, ctmp); /* add to returned color */ |
332 |
} |
333 |
if (nd.tdiff > FTINY) { /* ambient from other side */ |
334 |
copycolor(ctmp, nd.mcolor); /* modified by color */ |
335 |
if (nd.specfl & SP_TBLT) |
336 |
scalecolor(ctmp, nd.trans); |
337 |
else |
338 |
scalecolor(ctmp, nd.tdiff); |
339 |
flipsurface(r); |
340 |
if (hastexture) { |
341 |
FVECT bnorm; |
342 |
bnorm[0] = -nd.pnorm[0]; |
343 |
bnorm[1] = -nd.pnorm[1]; |
344 |
bnorm[2] = -nd.pnorm[2]; |
345 |
multambient(ctmp, r, bnorm); |
346 |
} else |
347 |
multambient(ctmp, r, r->ron); |
348 |
addcolor(r->rcol, ctmp); |
349 |
flipsurface(r); |
350 |
} |
351 |
/* add direct component */ |
352 |
direct(r, dirnorm, &nd); |
353 |
/* check distance */ |
354 |
d = bright(r->rcol); |
355 |
if (transtest > d) |
356 |
r->rt = transdist; |
357 |
else if (mirtest > d) |
358 |
r->rt = mirdist; |
359 |
|
360 |
return(1); |
361 |
} |
362 |
|
363 |
|
364 |
static void |
365 |
gaussamp( /* sample Gaussian specular */ |
366 |
NORMDAT *np |
367 |
) |
368 |
{ |
369 |
RAY sr; |
370 |
FVECT u, v, h; |
371 |
double rv[2]; |
372 |
double d, sinp, cosp; |
373 |
COLOR scol; |
374 |
int maxiter, ntrials, nstarget, nstaken; |
375 |
int i; |
376 |
/* quick test */ |
377 |
if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL && |
378 |
(np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN) |
379 |
return; |
380 |
/* set up sample coordinates */ |
381 |
v[0] = v[1] = v[2] = 0.0; |
382 |
for (i = 0; i < 3; i++) |
383 |
if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6) |
384 |
break; |
385 |
v[i] = 1.0; |
386 |
fcross(u, v, np->pnorm); |
387 |
normalize(u); |
388 |
fcross(v, np->pnorm, u); |
389 |
/* compute reflection */ |
390 |
if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && |
391 |
rayorigin(&sr, SPECULAR, np->rp, np->scolor) == 0) { |
392 |
nstarget = 1; |
393 |
if (specjitter > 1.5) { /* multiple samples? */ |
394 |
nstarget = specjitter*np->rp->rweight + .5; |
395 |
if (sr.rweight <= minweight*nstarget) |
396 |
nstarget = sr.rweight/minweight; |
397 |
if (nstarget > 1) { |
398 |
d = 1./nstarget; |
399 |
scalecolor(sr.rcoef, d); |
400 |
sr.rweight *= d; |
401 |
} else |
402 |
nstarget = 1; |
403 |
} |
404 |
setcolor(scol, 0., 0., 0.); |
405 |
dimlist[ndims++] = (int)(size_t)np->mp; |
406 |
maxiter = MAXITER*nstarget; |
407 |
for (nstaken = ntrials = 0; nstaken < nstarget && |
408 |
ntrials < maxiter; ntrials++) { |
409 |
if (ntrials) |
410 |
d = frandom(); |
411 |
else |
412 |
d = urand(ilhash(dimlist,ndims)+samplendx); |
413 |
multisamp(rv, 2, d); |
414 |
d = 2.0*PI * rv[0]; |
415 |
cosp = tcos(d); |
416 |
sinp = tsin(d); |
417 |
if ((0. <= specjitter) & (specjitter < 1.)) |
418 |
rv[1] = 1.0 - specjitter*rv[1]; |
419 |
if (rv[1] <= FTINY) |
420 |
d = 1.0; |
421 |
else |
422 |
d = sqrt( np->alpha2 * -log(rv[1]) ); |
423 |
for (i = 0; i < 3; i++) |
424 |
h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]); |
425 |
d = -2.0 * DOT(h, np->rp->rdir) / (1.0 + d*d); |
426 |
VSUM(sr.rdir, np->rp->rdir, h, d); |
427 |
/* sample rejection test */ |
428 |
if ((d = DOT(sr.rdir, np->rp->ron)) <= FTINY) |
429 |
continue; |
430 |
checknorm(sr.rdir); |
431 |
if (nstarget > 1) { /* W-G-M-D adjustment */ |
432 |
if (nstaken) rayclear(&sr); |
433 |
rayvalue(&sr); |
434 |
d = 2./(1. + np->rp->rod/d); |
435 |
scalecolor(sr.rcol, d); |
436 |
addcolor(scol, sr.rcol); |
437 |
} else { |
438 |
rayvalue(&sr); |
439 |
multcolor(sr.rcol, sr.rcoef); |
440 |
addcolor(np->rp->rcol, sr.rcol); |
441 |
} |
442 |
++nstaken; |
443 |
} |
444 |
if (nstarget > 1) { /* final W-G-M-D weighting */ |
445 |
multcolor(scol, sr.rcoef); |
446 |
d = (double)nstarget/ntrials; |
447 |
scalecolor(scol, d); |
448 |
addcolor(np->rp->rcol, scol); |
449 |
} |
450 |
ndims--; |
451 |
} |
452 |
/* compute transmission */ |
453 |
copycolor(sr.rcoef, np->mcolor); /* modified by color */ |
454 |
scalecolor(sr.rcoef, np->tspec); |
455 |
if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && |
456 |
rayorigin(&sr, SPECULAR, np->rp, sr.rcoef) == 0) { |
457 |
nstarget = 1; |
458 |
if (specjitter > 1.5) { /* multiple samples? */ |
459 |
nstarget = specjitter*np->rp->rweight + .5; |
460 |
if (sr.rweight <= minweight*nstarget) |
461 |
nstarget = sr.rweight/minweight; |
462 |
if (nstarget > 1) { |
463 |
d = 1./nstarget; |
464 |
scalecolor(sr.rcoef, d); |
465 |
sr.rweight *= d; |
466 |
} else |
467 |
nstarget = 1; |
468 |
} |
469 |
dimlist[ndims++] = (int)(size_t)np->mp; |
470 |
maxiter = MAXITER*nstarget; |
471 |
for (nstaken = ntrials = 0; nstaken < nstarget && |
472 |
ntrials < maxiter; ntrials++) { |
473 |
if (ntrials) |
474 |
d = frandom(); |
475 |
else |
476 |
d = urand(ilhash(dimlist,ndims)+samplendx); |
477 |
multisamp(rv, 2, d); |
478 |
d = 2.0*PI * rv[0]; |
479 |
cosp = tcos(d); |
480 |
sinp = tsin(d); |
481 |
if ((0. <= specjitter) & (specjitter < 1.)) |
482 |
rv[1] = 1.0 - specjitter*rv[1]; |
483 |
if (rv[1] <= FTINY) |
484 |
d = 1.0; |
485 |
else |
486 |
d = sqrt( np->alpha2 * -log(rv[1]) ); |
487 |
for (i = 0; i < 3; i++) |
488 |
sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]); |
489 |
/* sample rejection test */ |
490 |
if (DOT(sr.rdir, np->rp->ron) >= -FTINY) |
491 |
continue; |
492 |
normalize(sr.rdir); /* OK, normalize */ |
493 |
if (nstaken) /* multi-sampling */ |
494 |
rayclear(&sr); |
495 |
rayvalue(&sr); |
496 |
multcolor(sr.rcol, sr.rcoef); |
497 |
addcolor(np->rp->rcol, sr.rcol); |
498 |
++nstaken; |
499 |
} |
500 |
ndims--; |
501 |
} |
502 |
} |