1 |
– |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ SGI"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* normal.c - shading function for normal materials. |
6 |
|
* |
11 |
|
* Later changes described in delta comments. |
12 |
|
*/ |
13 |
|
|
14 |
+ |
#include "copyright.h" |
15 |
+ |
|
16 |
|
#include "ray.h" |
17 |
|
|
18 |
|
#include "otypes.h" |
19 |
|
|
20 |
|
#include "random.h" |
21 |
|
|
23 |
– |
extern double specthresh; /* specular sampling threshold */ |
24 |
– |
extern double specjitter; /* specular sampling jitter */ |
25 |
– |
|
26 |
– |
extern int backvis; /* back faces visible? */ |
27 |
– |
|
22 |
|
#ifndef MAXITER |
23 |
|
#define MAXITER 10 /* maximum # specular ray attempts */ |
24 |
|
#endif |
25 |
+ |
/* estimate of Fresnel function */ |
26 |
+ |
#define FRESNE(ci) (exp(-6.0*(ci)) - 0.00247875217) |
27 |
|
|
28 |
< |
static gaussamp(); |
28 |
> |
static void gaussamp(); |
29 |
|
|
30 |
|
/* |
31 |
|
* This routine implements the isotropic Gaussian |
65 |
|
} NORMDAT; /* normal material data */ |
66 |
|
|
67 |
|
|
68 |
+ |
static void |
69 |
|
dirnorm(cval, np, ldir, omega) /* compute source contribution */ |
70 |
|
COLOR cval; /* returned coefficient */ |
71 |
|
register NORMDAT *np; /* material data */ |
73 |
|
double omega; /* light source size */ |
74 |
|
{ |
75 |
|
double ldot; |
76 |
+ |
double ldiff; |
77 |
|
double dtmp, d2; |
78 |
|
FVECT vtmp; |
79 |
|
COLOR ctmp; |
85 |
|
if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY) |
86 |
|
return; /* wrong side */ |
87 |
|
|
88 |
< |
if (ldot > FTINY && np->rdiff > FTINY) { |
88 |
> |
/* Fresnel estimate */ |
89 |
> |
ldiff = np->rdiff; |
90 |
> |
if (np->specfl & SP_PURE && (np->rspec > FTINY & ldiff > FTINY)) |
91 |
> |
ldiff *= 1. - FRESNE(fabs(ldot)); |
92 |
> |
|
93 |
> |
if (ldot > FTINY && ldiff > FTINY) { |
94 |
|
/* |
95 |
|
* Compute and add diffuse reflected component to returned |
96 |
|
* color. The diffuse reflected component will always be |
97 |
|
* modified by the color of the material. |
98 |
|
*/ |
99 |
|
copycolor(ctmp, np->mcolor); |
100 |
< |
dtmp = ldot * omega * np->rdiff / PI; |
100 |
> |
dtmp = ldot * omega * ldiff / PI; |
101 |
|
scalecolor(ctmp, dtmp); |
102 |
|
addcolor(cval, ctmp); |
103 |
|
} |
157 |
|
} |
158 |
|
|
159 |
|
|
160 |
+ |
int |
161 |
|
m_normal(m, r) /* color a ray that hit something normal */ |
162 |
|
register OBJREC *m; |
163 |
|
register RAY *r; |
164 |
|
{ |
165 |
|
NORMDAT nd; |
166 |
+ |
double fest; |
167 |
|
double transtest, transdist; |
168 |
|
double mirtest, mirdist; |
169 |
|
int hastexture; |
211 |
|
mirtest = transtest = 0; |
212 |
|
mirdist = transdist = r->rot; |
213 |
|
nd.rspec = m->oargs.farg[3]; |
214 |
+ |
/* compute Fresnel approx. */ |
215 |
+ |
if (nd.specfl & SP_PURE && nd.rspec > FTINY) { |
216 |
+ |
fest = FRESNE(r->rod); |
217 |
+ |
nd.rspec += fest*(1. - nd.rspec); |
218 |
+ |
} else |
219 |
+ |
fest = 0.; |
220 |
|
/* compute transmission */ |
221 |
|
if (m->otype == MAT_TRANS) { |
222 |
|
nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec); |
265 |
|
if (nd.rspec > FTINY) { |
266 |
|
nd.specfl |= SP_REFL; |
267 |
|
/* compute specular color */ |
268 |
< |
if (m->otype == MAT_METAL) |
268 |
> |
if (m->otype != MAT_METAL) { |
269 |
> |
setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec); |
270 |
> |
} else if (fest > FTINY) { |
271 |
> |
d = nd.rspec*(1. - fest); |
272 |
> |
for (i = 0; i < 3; i++) |
273 |
> |
nd.scolor[i] = fest + nd.mcolor[i]*d; |
274 |
> |
} else { |
275 |
|
copycolor(nd.scolor, nd.mcolor); |
276 |
< |
else |
277 |
< |
setcolor(nd.scolor, 1.0, 1.0, 1.0); |
261 |
< |
scalecolor(nd.scolor, nd.rspec); |
276 |
> |
scalecolor(nd.scolor, nd.rspec); |
277 |
> |
} |
278 |
|
/* check threshold */ |
279 |
|
if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY) |
280 |
|
nd.specfl |= SP_RBLT; |
349 |
|
} |
350 |
|
|
351 |
|
|
352 |
< |
static |
352 |
> |
static void |
353 |
|
gaussamp(r, np) /* sample gaussian specular */ |
354 |
|
RAY *r; |
355 |
|
register NORMDAT *np; |