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 "ambient.h" |
19 |
+ |
|
20 |
|
#include "otypes.h" |
21 |
|
|
22 |
|
#include "random.h" |
23 |
|
|
23 |
– |
extern double specthresh; /* specular sampling threshold */ |
24 |
– |
extern double specjitter; /* specular sampling jitter */ |
25 |
– |
|
26 |
– |
extern int backvis; /* back faces visible? */ |
27 |
– |
|
24 |
|
#ifndef MAXITER |
25 |
|
#define MAXITER 10 /* maximum # specular ray attempts */ |
26 |
|
#endif |
27 |
+ |
/* estimate of Fresnel function */ |
28 |
+ |
#define FRESNE(ci) (exp(-5.85*(ci)) - 0.00287989916) |
29 |
|
|
30 |
< |
static gaussamp(); |
30 |
> |
static void gaussamp(); |
31 |
|
|
32 |
|
/* |
33 |
|
* This routine implements the isotropic Gaussian |
67 |
|
} NORMDAT; /* normal material data */ |
68 |
|
|
69 |
|
|
70 |
+ |
static void |
71 |
|
dirnorm(cval, np, ldir, omega) /* compute source contribution */ |
72 |
|
COLOR cval; /* returned coefficient */ |
73 |
|
register NORMDAT *np; /* material data */ |
75 |
|
double omega; /* light source size */ |
76 |
|
{ |
77 |
|
double ldot; |
78 |
+ |
double ldiff; |
79 |
|
double dtmp, d2; |
80 |
|
FVECT vtmp; |
81 |
|
COLOR ctmp; |
87 |
|
if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY) |
88 |
|
return; /* wrong side */ |
89 |
|
|
90 |
< |
if (ldot > FTINY && np->rdiff > FTINY) { |
90 |
> |
/* Fresnel estimate */ |
91 |
> |
ldiff = np->rdiff; |
92 |
> |
if (np->specfl & SP_PURE && (np->rspec > FTINY) & (ldiff > FTINY)) |
93 |
> |
ldiff *= 1. - FRESNE(fabs(ldot)); |
94 |
> |
|
95 |
> |
if (ldot > FTINY && ldiff > FTINY) { |
96 |
|
/* |
97 |
|
* Compute and add diffuse reflected component to returned |
98 |
|
* color. The diffuse reflected component will always be |
99 |
|
* modified by the color of the material. |
100 |
|
*/ |
101 |
|
copycolor(ctmp, np->mcolor); |
102 |
< |
dtmp = ldot * omega * np->rdiff / PI; |
102 |
> |
dtmp = ldot * omega * ldiff / PI; |
103 |
|
scalecolor(ctmp, dtmp); |
104 |
|
addcolor(cval, ctmp); |
105 |
|
} |
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
+ |
int |
163 |
|
m_normal(m, r) /* color a ray that hit something normal */ |
164 |
|
register OBJREC *m; |
165 |
|
register RAY *r; |
166 |
|
{ |
167 |
|
NORMDAT nd; |
168 |
+ |
double fest; |
169 |
|
double transtest, transdist; |
170 |
|
double mirtest, mirdist; |
171 |
|
int hastexture; |
184 |
|
raytrans(r); |
185 |
|
return(1); |
186 |
|
} |
187 |
+ |
raytexture(r, m->omod); |
188 |
|
flipsurface(r); /* reorient if backvis */ |
189 |
< |
} |
189 |
> |
} else |
190 |
> |
raytexture(r, m->omod); |
191 |
|
nd.mp = m; |
192 |
|
nd.rp = r; |
193 |
|
/* get material color */ |
199 |
|
nd.alpha2 = m->oargs.farg[4]; |
200 |
|
if ((nd.alpha2 *= nd.alpha2) <= FTINY) |
201 |
|
nd.specfl |= SP_PURE; |
202 |
< |
if (r->ro != NULL && isflat(r->ro->otype)) |
203 |
< |
nd.specfl |= SP_FLAT; |
195 |
< |
/* get modifiers */ |
196 |
< |
raytexture(r, m->omod); |
197 |
< |
if (hastexture = DOT(r->pert,r->pert) > FTINY*FTINY) |
202 |
> |
|
203 |
> |
if ( (hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY)) ) { |
204 |
|
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
205 |
< |
else { |
205 |
> |
} else { |
206 |
|
VCOPY(nd.pnorm, r->ron); |
207 |
|
nd.pdot = r->rod; |
208 |
|
} |
209 |
+ |
if (r->ro != NULL && isflat(r->ro->otype)) |
210 |
+ |
nd.specfl |= SP_FLAT; |
211 |
|
if (nd.pdot < .001) |
212 |
|
nd.pdot = .001; /* non-zero for dirnorm() */ |
213 |
|
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
214 |
|
mirtest = transtest = 0; |
215 |
|
mirdist = transdist = r->rot; |
216 |
|
nd.rspec = m->oargs.farg[3]; |
217 |
+ |
/* compute Fresnel approx. */ |
218 |
+ |
if (nd.specfl & SP_PURE && nd.rspec > FTINY) { |
219 |
+ |
fest = FRESNE(r->rod); |
220 |
+ |
nd.rspec += fest*(1. - nd.rspec); |
221 |
+ |
} else |
222 |
+ |
fest = 0.; |
223 |
|
/* compute transmission */ |
224 |
|
if (m->otype == MAT_TRANS) { |
225 |
|
nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec); |
268 |
|
if (nd.rspec > FTINY) { |
269 |
|
nd.specfl |= SP_REFL; |
270 |
|
/* compute specular color */ |
271 |
< |
if (m->otype == MAT_METAL) |
271 |
> |
if (m->otype != MAT_METAL) { |
272 |
> |
setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec); |
273 |
> |
} else if (fest > FTINY) { |
274 |
> |
d = nd.rspec*(1. - fest); |
275 |
> |
for (i = 0; i < 3; i++) |
276 |
> |
nd.scolor[i] = fest + nd.mcolor[i]*d; |
277 |
> |
} else { |
278 |
|
copycolor(nd.scolor, nd.mcolor); |
279 |
< |
else |
280 |
< |
setcolor(nd.scolor, 1.0, 1.0, 1.0); |
261 |
< |
scalecolor(nd.scolor, nd.rspec); |
279 |
> |
scalecolor(nd.scolor, nd.rspec); |
280 |
> |
} |
281 |
|
/* check threshold */ |
282 |
|
if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY) |
283 |
|
nd.specfl |= SP_RBLT; |
352 |
|
} |
353 |
|
|
354 |
|
|
355 |
< |
static |
355 |
> |
static void |
356 |
|
gaussamp(r, np) /* sample gaussian specular */ |
357 |
|
RAY *r; |
358 |
|
register NORMDAT *np; |