1 |
greg |
1.1 |
/* Copyright (c) 1986 Regents of the University of California */ |
2 |
|
|
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
5 |
|
|
#endif |
6 |
|
|
|
7 |
|
|
/* |
8 |
|
|
* normal.c - shading function for normal materials. |
9 |
|
|
* |
10 |
|
|
* 8/19/85 |
11 |
|
|
* 12/19/85 - added stuff for metals. |
12 |
|
|
* 6/26/87 - improved specular model. |
13 |
|
|
* 9/28/87 - added model for translucent materials. |
14 |
|
|
*/ |
15 |
|
|
|
16 |
|
|
#include "ray.h" |
17 |
|
|
|
18 |
|
|
#include "otypes.h" |
19 |
|
|
|
20 |
|
|
/* |
21 |
|
|
* This routine uses portions of the reflection |
22 |
|
|
* model described by Cook and Torrance. |
23 |
|
|
* The computation of specular components has been simplified by |
24 |
|
|
* numerous approximations and ommisions to improve speed. |
25 |
|
|
* We orient the surface towards the incoming ray, so a single |
26 |
|
|
* surface can be used to represent an infinitely thin object. |
27 |
|
|
* |
28 |
|
|
* Arguments for MAT_PLASTIC and MAT_METAL are: |
29 |
|
|
* red grn blu specular-frac. facet-slope |
30 |
|
|
* |
31 |
|
|
* Arguments for MAT_TRANS are: |
32 |
|
|
* red grn blu rspec rough trans tspec |
33 |
|
|
*/ |
34 |
|
|
|
35 |
|
|
#define BSPEC(m) (6.0) /* specularity parameter b */ |
36 |
|
|
|
37 |
greg |
1.3 |
extern double exp(); |
38 |
greg |
1.1 |
|
39 |
greg |
1.3 |
typedef struct { |
40 |
|
|
OBJREC *mp; /* material pointer */ |
41 |
|
|
RAY *pr; /* intersected ray */ |
42 |
greg |
1.1 |
COLOR mcolor; /* color of this material */ |
43 |
|
|
COLOR scolor; /* color of specular component */ |
44 |
|
|
FVECT vrefl; /* vector in direction of reflected ray */ |
45 |
|
|
double alpha2; /* roughness squared times 2 */ |
46 |
|
|
double rdiff, rspec; /* reflected specular, diffuse */ |
47 |
|
|
double trans; /* transmissivity */ |
48 |
|
|
double tdiff, tspec; /* transmitted specular, diffuse */ |
49 |
|
|
FVECT pnorm; /* perturbed surface normal */ |
50 |
|
|
double pdot; /* perturbed dot product */ |
51 |
greg |
1.3 |
} NORMDAT; /* normal material data */ |
52 |
|
|
|
53 |
|
|
|
54 |
|
|
dirnorm(cval, np, ldir, omega) /* compute source contribution */ |
55 |
|
|
COLOR cval; /* returned coefficient */ |
56 |
|
|
register NORMDAT *np; /* material data */ |
57 |
|
|
FVECT ldir; /* light source direction */ |
58 |
|
|
double omega; /* light source size */ |
59 |
|
|
{ |
60 |
greg |
1.1 |
double ldot; |
61 |
greg |
1.3 |
double dtmp; |
62 |
|
|
COLOR ctmp; |
63 |
|
|
|
64 |
|
|
setcolor(cval, 0.0, 0.0, 0.0); |
65 |
|
|
|
66 |
|
|
ldot = DOT(np->pnorm, ldir); |
67 |
|
|
|
68 |
|
|
if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY) |
69 |
|
|
return; /* wrong side */ |
70 |
|
|
|
71 |
|
|
if (ldot > FTINY && np->rdiff > FTINY) { |
72 |
|
|
/* |
73 |
|
|
* Compute and add diffuse component to returned color. |
74 |
|
|
* The diffuse component will always be modified by the |
75 |
|
|
* color of the material. |
76 |
|
|
*/ |
77 |
|
|
copycolor(ctmp, np->mcolor); |
78 |
|
|
dtmp = ldot * omega * np->rdiff / PI; |
79 |
|
|
scalecolor(ctmp, dtmp); |
80 |
|
|
addcolor(cval, ctmp); |
81 |
|
|
} |
82 |
|
|
if (ldot > FTINY && np->rspec > FTINY && np->alpha2 > FTINY) { |
83 |
|
|
/* |
84 |
|
|
* Compute specular reflection coefficient using |
85 |
|
|
* gaussian distribution model. |
86 |
|
|
*/ |
87 |
|
|
/* roughness + source */ |
88 |
|
|
dtmp = np->alpha2 + omega/(2.0*PI); |
89 |
|
|
/* gaussian */ |
90 |
|
|
dtmp = exp((DOT(np->vrefl,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
91 |
|
|
/* worth using? */ |
92 |
|
|
if (dtmp > FTINY) { |
93 |
|
|
copycolor(ctmp, np->scolor); |
94 |
|
|
dtmp *= omega; |
95 |
|
|
scalecolor(ctmp, dtmp); |
96 |
|
|
addcolor(cval, ctmp); |
97 |
|
|
} |
98 |
|
|
} |
99 |
|
|
if (ldot < -FTINY && np->tdiff > FTINY) { |
100 |
|
|
/* |
101 |
|
|
* Compute diffuse transmission. |
102 |
|
|
*/ |
103 |
|
|
copycolor(ctmp, np->mcolor); |
104 |
|
|
dtmp = -ldot * omega * np->tdiff / PI; |
105 |
|
|
scalecolor(ctmp, dtmp); |
106 |
|
|
addcolor(cval, ctmp); |
107 |
|
|
} |
108 |
|
|
if (ldot < -FTINY && np->tspec > FTINY && np->alpha2 > FTINY) { |
109 |
|
|
/* |
110 |
|
|
* Compute specular transmission. |
111 |
|
|
*/ |
112 |
|
|
/* roughness + source */ |
113 |
|
|
dtmp = np->alpha2 + omega/(2.0*PI); |
114 |
|
|
/* gaussian */ |
115 |
|
|
dtmp = exp((DOT(np->pr->rdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
116 |
|
|
/* worth using? */ |
117 |
|
|
if (dtmp > FTINY) { |
118 |
|
|
copycolor(ctmp, np->mcolor); |
119 |
|
|
dtmp *= np->tspec * omega; |
120 |
|
|
scalecolor(ctmp, dtmp); |
121 |
|
|
addcolor(cval, ctmp); |
122 |
|
|
} |
123 |
|
|
} |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
|
127 |
|
|
m_normal(m, r) /* color a ray which hit something normal */ |
128 |
|
|
register OBJREC *m; |
129 |
|
|
register RAY *r; |
130 |
|
|
{ |
131 |
|
|
NORMDAT nd; |
132 |
|
|
double ldot; |
133 |
greg |
1.1 |
double omega; |
134 |
|
|
double dtmp; |
135 |
|
|
COLOR ctmp; |
136 |
|
|
register int i; |
137 |
|
|
|
138 |
|
|
if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) |
139 |
|
|
objerror(m, USER, "bad # arguments"); |
140 |
|
|
/* easy shadow test */ |
141 |
|
|
if (r->crtype & SHADOW && m->otype != MAT_TRANS) |
142 |
|
|
return; |
143 |
greg |
1.3 |
nd.mp = m; |
144 |
|
|
nd.pr = r; |
145 |
greg |
1.1 |
/* get material color */ |
146 |
greg |
1.3 |
setcolor(nd.mcolor, m->oargs.farg[0], |
147 |
greg |
1.1 |
m->oargs.farg[1], |
148 |
|
|
m->oargs.farg[2]); |
149 |
|
|
/* get roughness */ |
150 |
greg |
1.3 |
nd.alpha2 = m->oargs.farg[4]; |
151 |
|
|
nd.alpha2 *= 2.0 * nd.alpha2; |
152 |
greg |
1.1 |
/* reorient if necessary */ |
153 |
|
|
if (r->rod < 0.0) |
154 |
|
|
flipsurface(r); |
155 |
|
|
/* get modifiers */ |
156 |
|
|
raytexture(r, m->omod); |
157 |
greg |
1.3 |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
158 |
|
|
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
159 |
greg |
1.1 |
/* get specular component */ |
160 |
greg |
1.3 |
nd.rspec = m->oargs.farg[3]; |
161 |
greg |
1.1 |
|
162 |
greg |
1.3 |
if (nd.rspec > FTINY) { /* has specular component */ |
163 |
greg |
1.1 |
/* compute specular color */ |
164 |
|
|
if (m->otype == MAT_METAL) |
165 |
greg |
1.3 |
copycolor(nd.scolor, nd.mcolor); |
166 |
greg |
1.1 |
else |
167 |
greg |
1.3 |
setcolor(nd.scolor, 1.0, 1.0, 1.0); |
168 |
|
|
scalecolor(nd.scolor, nd.rspec); |
169 |
greg |
1.1 |
/* improved model */ |
170 |
greg |
1.3 |
dtmp = exp(-BSPEC(m)*nd.pdot); |
171 |
greg |
1.1 |
for (i = 0; i < 3; i++) |
172 |
greg |
1.3 |
colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp; |
173 |
|
|
nd.rspec += (1.0-nd.rspec)*dtmp; |
174 |
greg |
1.1 |
/* compute reflected ray */ |
175 |
|
|
for (i = 0; i < 3; i++) |
176 |
greg |
1.3 |
nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i]; |
177 |
greg |
1.1 |
|
178 |
greg |
1.3 |
if (nd.alpha2 <= FTINY && !(r->crtype & SHADOW)) { |
179 |
|
|
RAY lr; |
180 |
|
|
if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { |
181 |
|
|
VCOPY(lr.rdir, nd.vrefl); |
182 |
greg |
1.1 |
rayvalue(&lr); |
183 |
greg |
1.3 |
multcolor(lr.rcol, nd.scolor); |
184 |
greg |
1.1 |
addcolor(r->rcol, lr.rcol); |
185 |
|
|
} |
186 |
greg |
1.3 |
} |
187 |
greg |
1.1 |
} |
188 |
greg |
1.3 |
/* compute transmission */ |
189 |
greg |
1.1 |
if (m->otype == MAT_TRANS) { |
190 |
greg |
1.3 |
nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec); |
191 |
|
|
nd.tspec = nd.trans * m->oargs.farg[6]; |
192 |
|
|
nd.tdiff = nd.trans - nd.tspec; |
193 |
greg |
1.1 |
} else |
194 |
greg |
1.3 |
nd.tdiff = nd.tspec = nd.trans = 0.0; |
195 |
greg |
1.1 |
/* transmitted ray */ |
196 |
greg |
1.3 |
if (nd.tspec > FTINY && nd.alpha2 <= FTINY) { |
197 |
|
|
RAY lr; |
198 |
|
|
if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) { |
199 |
greg |
1.1 |
VCOPY(lr.rdir, r->rdir); |
200 |
|
|
rayvalue(&lr); |
201 |
greg |
1.3 |
scalecolor(lr.rcol, nd.tspec); |
202 |
greg |
1.1 |
addcolor(r->rcol, lr.rcol); |
203 |
|
|
} |
204 |
greg |
1.3 |
} |
205 |
greg |
1.1 |
if (r->crtype & SHADOW) /* the rest is shadow */ |
206 |
|
|
return; |
207 |
|
|
/* diffuse reflection */ |
208 |
greg |
1.3 |
nd.rdiff = 1.0 - nd.trans - nd.rspec; |
209 |
greg |
1.1 |
|
210 |
greg |
1.3 |
if (nd.rdiff <= FTINY && nd.tdiff <= FTINY && nd.alpha2 <= FTINY) |
211 |
greg |
1.1 |
return; /* purely specular */ |
212 |
|
|
|
213 |
greg |
1.3 |
if (nd.rdiff > FTINY) { /* ambient from this side */ |
214 |
greg |
1.2 |
ambient(ctmp, r); |
215 |
greg |
1.3 |
if (nd.alpha2 <= FTINY) |
216 |
|
|
scalecolor(ctmp, nd.rdiff); |
217 |
greg |
1.2 |
else |
218 |
greg |
1.3 |
scalecolor(ctmp, 1.0-nd.trans); |
219 |
|
|
multcolor(ctmp, nd.mcolor); /* modified by material color */ |
220 |
greg |
1.2 |
addcolor(r->rcol, ctmp); /* add to returned color */ |
221 |
|
|
} |
222 |
greg |
1.3 |
if (nd.tdiff > FTINY) { /* ambient from other side */ |
223 |
greg |
1.1 |
flipsurface(r); |
224 |
greg |
1.2 |
ambient(ctmp, r); |
225 |
greg |
1.3 |
if (nd.alpha2 <= FTINY) |
226 |
|
|
scalecolor(ctmp, nd.tdiff); |
227 |
greg |
1.2 |
else |
228 |
greg |
1.3 |
scalecolor(ctmp, nd.trans); |
229 |
|
|
multcolor(ctmp, nd.mcolor); |
230 |
greg |
1.1 |
addcolor(r->rcol, ctmp); |
231 |
|
|
flipsurface(r); |
232 |
|
|
} |
233 |
greg |
1.3 |
/* add direct component */ |
234 |
|
|
direct(r, dirnorm, &nd); |
235 |
greg |
1.1 |
} |