1 |
– |
/* Copyright (c) 1996 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
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 |
+ |
/* ==================================================================== |
15 |
+ |
* The Radiance Software License, Version 1.0 |
16 |
+ |
* |
17 |
+ |
* Copyright (c) 1990 - 2002 The Regents of the University of California, |
18 |
+ |
* through Lawrence Berkeley National Laboratory. All rights reserved. |
19 |
+ |
* |
20 |
+ |
* Redistribution and use in source and binary forms, with or without |
21 |
+ |
* modification, are permitted provided that the following conditions |
22 |
+ |
* are met: |
23 |
+ |
* |
24 |
+ |
* 1. Redistributions of source code must retain the above copyright |
25 |
+ |
* notice, this list of conditions and the following disclaimer. |
26 |
+ |
* |
27 |
+ |
* 2. Redistributions in binary form must reproduce the above copyright |
28 |
+ |
* notice, this list of conditions and the following disclaimer in |
29 |
+ |
* the documentation and/or other materials provided with the |
30 |
+ |
* distribution. |
31 |
+ |
* |
32 |
+ |
* 3. The end-user documentation included with the redistribution, |
33 |
+ |
* if any, must include the following acknowledgment: |
34 |
+ |
* "This product includes Radiance software |
35 |
+ |
* (http://radsite.lbl.gov/) |
36 |
+ |
* developed by the Lawrence Berkeley National Laboratory |
37 |
+ |
* (http://www.lbl.gov/)." |
38 |
+ |
* Alternately, this acknowledgment may appear in the software itself, |
39 |
+ |
* if and wherever such third-party acknowledgments normally appear. |
40 |
+ |
* |
41 |
+ |
* 4. The names "Radiance," "Lawrence Berkeley National Laboratory" |
42 |
+ |
* and "The Regents of the University of California" must |
43 |
+ |
* not be used to endorse or promote products derived from this |
44 |
+ |
* software without prior written permission. For written |
45 |
+ |
* permission, please contact [email protected]. |
46 |
+ |
* |
47 |
+ |
* 5. Products derived from this software may not be called "Radiance", |
48 |
+ |
* nor may "Radiance" appear in their name, without prior written |
49 |
+ |
* permission of Lawrence Berkeley National Laboratory. |
50 |
+ |
* |
51 |
+ |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
52 |
+ |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
53 |
+ |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
54 |
+ |
* DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR |
55 |
+ |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
56 |
+ |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
57 |
+ |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
58 |
+ |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
59 |
+ |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
60 |
+ |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
61 |
+ |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
62 |
+ |
* SUCH DAMAGE. |
63 |
+ |
* ==================================================================== |
64 |
+ |
* |
65 |
+ |
* This software consists of voluntary contributions made by many |
66 |
+ |
* individuals on behalf of Lawrence Berkeley National Laboratory. For more |
67 |
+ |
* information on Lawrence Berkeley National Laboratory, please see |
68 |
+ |
* <http://www.lbl.gov/>. |
69 |
+ |
*/ |
70 |
+ |
|
71 |
|
#include "ray.h" |
72 |
|
|
73 |
|
#include "otypes.h" |
74 |
|
|
75 |
|
#include "random.h" |
76 |
|
|
23 |
– |
extern double specthresh; /* specular sampling threshold */ |
24 |
– |
extern double specjitter; /* specular sampling jitter */ |
25 |
– |
|
26 |
– |
extern int backvis; /* back faces visible? */ |
27 |
– |
|
77 |
|
#ifndef MAXITER |
78 |
|
#define MAXITER 10 /* maximum # specular ray attempts */ |
79 |
|
#endif |
80 |
+ |
/* estimate of Fresnel function */ |
81 |
+ |
#define FRESNE(ci) (exp(-6.0*(ci)) - 0.00247875217) |
82 |
|
|
83 |
< |
static gaussamp(); |
83 |
> |
static void gaussamp(); |
84 |
|
|
85 |
|
/* |
86 |
|
* This routine implements the isotropic Gaussian |
120 |
|
} NORMDAT; /* normal material data */ |
121 |
|
|
122 |
|
|
123 |
+ |
static void |
124 |
|
dirnorm(cval, np, ldir, omega) /* compute source contribution */ |
125 |
|
COLOR cval; /* returned coefficient */ |
126 |
|
register NORMDAT *np; /* material data */ |
128 |
|
double omega; /* light source size */ |
129 |
|
{ |
130 |
|
double ldot; |
131 |
+ |
double ldiff; |
132 |
|
double dtmp, d2; |
133 |
|
FVECT vtmp; |
134 |
|
COLOR ctmp; |
140 |
|
if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY) |
141 |
|
return; /* wrong side */ |
142 |
|
|
143 |
< |
if (ldot > FTINY && np->rdiff > FTINY) { |
143 |
> |
/* Fresnel estimate */ |
144 |
> |
ldiff = np->rdiff; |
145 |
> |
if (np->specfl & SP_PURE && (np->rspec > FTINY & ldiff > FTINY)) |
146 |
> |
ldiff *= 1. - FRESNE(fabs(ldot)); |
147 |
> |
|
148 |
> |
if (ldot > FTINY && ldiff > FTINY) { |
149 |
|
/* |
150 |
|
* Compute and add diffuse reflected component to returned |
151 |
|
* color. The diffuse reflected component will always be |
152 |
|
* modified by the color of the material. |
153 |
|
*/ |
154 |
|
copycolor(ctmp, np->mcolor); |
155 |
< |
dtmp = ldot * omega * np->rdiff / PI; |
155 |
> |
dtmp = ldot * omega * ldiff / PI; |
156 |
|
scalecolor(ctmp, dtmp); |
157 |
|
addcolor(cval, ctmp); |
158 |
|
} |
212 |
|
} |
213 |
|
|
214 |
|
|
215 |
+ |
int |
216 |
|
m_normal(m, r) /* color a ray that hit something normal */ |
217 |
|
register OBJREC *m; |
218 |
|
register RAY *r; |
219 |
|
{ |
220 |
|
NORMDAT nd; |
221 |
+ |
double fest; |
222 |
|
double transtest, transdist; |
223 |
|
double mirtest, mirdist; |
224 |
|
int hastexture; |
266 |
|
mirtest = transtest = 0; |
267 |
|
mirdist = transdist = r->rot; |
268 |
|
nd.rspec = m->oargs.farg[3]; |
269 |
+ |
/* compute Fresnel approx. */ |
270 |
+ |
if (nd.specfl & SP_PURE && nd.rspec > FTINY) { |
271 |
+ |
fest = FRESNE(r->rod); |
272 |
+ |
nd.rspec += fest*(1. - nd.rspec); |
273 |
+ |
} else |
274 |
+ |
fest = 0.; |
275 |
|
/* compute transmission */ |
276 |
|
if (m->otype == MAT_TRANS) { |
277 |
|
nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec); |
298 |
|
} else |
299 |
|
nd.tdiff = nd.tspec = nd.trans = 0.0; |
300 |
|
/* transmitted ray */ |
301 |
< |
if (nd.specfl&SP_TRAN && (nd.specfl&SP_PURE || r->crtype&SHADOW)) { |
301 |
> |
if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) { |
302 |
|
RAY lr; |
303 |
|
if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) { |
304 |
|
VCOPY(lr.rdir, nd.prdir); |
320 |
|
if (nd.rspec > FTINY) { |
321 |
|
nd.specfl |= SP_REFL; |
322 |
|
/* compute specular color */ |
323 |
< |
if (m->otype == MAT_METAL) |
323 |
> |
if (m->otype != MAT_METAL) { |
324 |
> |
setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec); |
325 |
> |
} else if (fest > FTINY) { |
326 |
> |
d = nd.rspec*(1. - fest); |
327 |
> |
for (i = 0; i < 3; i++) |
328 |
> |
nd.scolor[i] = fest + nd.mcolor[i]*d; |
329 |
> |
} else { |
330 |
|
copycolor(nd.scolor, nd.mcolor); |
331 |
< |
else |
332 |
< |
setcolor(nd.scolor, 1.0, 1.0, 1.0); |
261 |
< |
scalecolor(nd.scolor, nd.rspec); |
331 |
> |
scalecolor(nd.scolor, nd.rspec); |
332 |
> |
} |
333 |
|
/* check threshold */ |
334 |
|
if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY) |
335 |
|
nd.specfl |= SP_RBLT; |
340 |
|
if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY) |
341 |
|
for (i = 0; i < 3; i++) /* safety measure */ |
342 |
|
nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; |
343 |
< |
|
344 |
< |
if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) { |
345 |
< |
RAY lr; |
346 |
< |
if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { |
347 |
< |
VCOPY(lr.rdir, nd.vrefl); |
348 |
< |
rayvalue(&lr); |
349 |
< |
multcolor(lr.rcol, nd.scolor); |
350 |
< |
addcolor(r->rcol, lr.rcol); |
351 |
< |
if (!hastexture && nd.specfl & SP_FLAT) { |
352 |
< |
mirtest = 2.*bright(lr.rcol); |
353 |
< |
mirdist = r->rot + lr.rt; |
354 |
< |
} |
343 |
> |
} |
344 |
> |
/* reflected ray */ |
345 |
> |
if ((nd.specfl&(SP_REFL|SP_PURE|SP_RBLT)) == (SP_REFL|SP_PURE)) { |
346 |
> |
RAY lr; |
347 |
> |
if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { |
348 |
> |
VCOPY(lr.rdir, nd.vrefl); |
349 |
> |
rayvalue(&lr); |
350 |
> |
multcolor(lr.rcol, nd.scolor); |
351 |
> |
addcolor(r->rcol, lr.rcol); |
352 |
> |
if (!hastexture && nd.specfl & SP_FLAT) { |
353 |
> |
mirtest = 2.*bright(lr.rcol); |
354 |
> |
mirdist = r->rot + lr.rt; |
355 |
|
} |
356 |
|
} |
357 |
|
} |
361 |
|
if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) |
362 |
|
return(1); /* 100% pure specular */ |
363 |
|
|
364 |
< |
if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE)) |
365 |
< |
gaussamp(r, &nd); |
364 |
> |
if (!(nd.specfl & SP_PURE)) |
365 |
> |
gaussamp(r, &nd); /* checks *BLT flags */ |
366 |
|
|
367 |
|
if (nd.rdiff > FTINY) { /* ambient from this side */ |
368 |
|
ambient(ctmp, r, hastexture?nd.pnorm:r->ron); |
404 |
|
} |
405 |
|
|
406 |
|
|
407 |
< |
static |
407 |
> |
static void |
408 |
|
gaussamp(r, np) /* sample gaussian specular */ |
409 |
|
RAY *r; |
410 |
|
register NORMDAT *np; |
439 |
|
d = urand(ilhash(dimlist,ndims)+samplendx); |
440 |
|
multisamp(rv, 2, d); |
441 |
|
d = 2.0*PI * rv[0]; |
442 |
< |
cosp = cos(d); |
443 |
< |
sinp = sin(d); |
442 |
> |
cosp = tcos(d); |
443 |
> |
sinp = tsin(d); |
444 |
|
rv[1] = 1.0 - specjitter*rv[1]; |
445 |
|
if (rv[1] <= FTINY) |
446 |
|
d = 1.0; |
471 |
|
d = urand(ilhash(dimlist,ndims)+1823+samplendx); |
472 |
|
multisamp(rv, 2, d); |
473 |
|
d = 2.0*PI * rv[0]; |
474 |
< |
cosp = cos(d); |
475 |
< |
sinp = sin(d); |
474 |
> |
cosp = tcos(d); |
475 |
> |
sinp = tsin(d); |
476 |
|
rv[1] = 1.0 - specjitter*rv[1]; |
477 |
|
if (rv[1] <= FTINY) |
478 |
|
d = 1.0; |
479 |
|
else |
480 |
< |
d = sqrt( -log(rv[1]) * np->alpha2 ); |
480 |
> |
d = sqrt( np->alpha2 * -log(rv[1]) ); |
481 |
|
for (i = 0; i < 3; i++) |
482 |
|
sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]); |
483 |
|
if (DOT(sr.rdir, r->ron) < -FTINY) { |