1 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Shading for materials with arbitrary BRDF's |
9 |
*/ |
10 |
|
11 |
#include "ray.h" |
12 |
|
13 |
#include "data.h" |
14 |
|
15 |
#include "otypes.h" |
16 |
|
17 |
/* |
18 |
* Arguments to this material include the color and specularity. |
19 |
* String arguments include the reflection function and files. |
20 |
* The BRDF is currently used just for the specular component to light |
21 |
* sources. Reflectance values or data coordinates are functions |
22 |
* of the direction to the light source. |
23 |
* We orient the surface towards the incoming ray, so a single |
24 |
* surface can be used to represent an infinitely thin object. |
25 |
* |
26 |
* Arguments for MAT_PFUNC and MAT_MFUNC are: |
27 |
* 2+ func funcfile transform |
28 |
* 0 |
29 |
* 4+ red grn blu specularity A5 .. |
30 |
* |
31 |
* Arguments for MAT_PDATA and MAT_MDATA are: |
32 |
* 4+ func datafile funcfile v0 .. transform |
33 |
* 0 |
34 |
* 4+ red grn blu specularity A5 .. |
35 |
* |
36 |
* Arguments for MAT_TFUNC are: |
37 |
* 2+ func funcfile transform |
38 |
* 0 |
39 |
* 4+ red grn blu rspec trans tspec A7 .. |
40 |
* |
41 |
* Arguments for MAT_TDATA are: |
42 |
* 4+ func datafile funcfile v0 .. transform |
43 |
* 0 |
44 |
* 4+ red grn blu rspec trans tspec A7 .. |
45 |
* |
46 |
* Arguments for the more general MAT_BRTDF are: |
47 |
* 10+ rrefl grefl brefl |
48 |
* rtrns gtrns btrns |
49 |
* rbrtd gbrtd bbrtd |
50 |
* funcfile transform |
51 |
* 0 |
52 |
* 6+ red grn blu rspec trans tspec A7 .. |
53 |
* |
54 |
* In addition to the normal variables available to functions, |
55 |
* we define the following: |
56 |
* NxP, NyP, NzP - perturbed surface normal |
57 |
* RdotP - perturbed ray dot product |
58 |
* CrP, CgP, CbP - perturbed material color |
59 |
*/ |
60 |
|
61 |
extern double funvalue(), varvalue(); |
62 |
extern XF funcxf; |
63 |
|
64 |
typedef struct { |
65 |
OBJREC *mp; /* material pointer */ |
66 |
RAY *pr; /* intersected ray */ |
67 |
DATARRAY *dp; /* data array for PDATA, MDATA or TDATA */ |
68 |
COLOR mcolor; /* color of this material */ |
69 |
double rspec; /* specular reflection */ |
70 |
double rdiff; /* diffuse reflection */ |
71 |
double trans; /* transmissivity */ |
72 |
double tspec; /* specular transmission */ |
73 |
double tdiff; /* diffuse transmission */ |
74 |
FVECT pnorm; /* perturbed surface normal */ |
75 |
double pdot; /* perturbed dot product */ |
76 |
} BRDFDAT; /* BRDF material data */ |
77 |
|
78 |
|
79 |
dirbrdf(cval, np, ldir, omega) /* compute source contribution */ |
80 |
COLOR cval; /* returned coefficient */ |
81 |
register BRDFDAT *np; /* material data */ |
82 |
FVECT ldir; /* light source direction */ |
83 |
double omega; /* light source size */ |
84 |
{ |
85 |
double ldot; |
86 |
double dtmp; |
87 |
COLOR ctmp; |
88 |
FVECT ldx; |
89 |
double pt[MAXDIM]; |
90 |
register char **sa; |
91 |
register int i; |
92 |
|
93 |
setcolor(cval, 0.0, 0.0, 0.0); |
94 |
|
95 |
ldot = DOT(np->pnorm, ldir); |
96 |
|
97 |
if (ldot <= FTINY && ldot >= -FTINY) |
98 |
return; /* too close to grazing */ |
99 |
if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY) |
100 |
return; /* wrong side */ |
101 |
|
102 |
if (ldot > 0.0 && np->rdiff > FTINY) { |
103 |
/* |
104 |
* Compute and add diffuse reflected component to returned |
105 |
* color. The diffuse reflected component will always be |
106 |
* modified by the color of the material. |
107 |
*/ |
108 |
copycolor(ctmp, np->mcolor); |
109 |
dtmp = ldot * omega * np->rdiff / PI; |
110 |
scalecolor(ctmp, dtmp); |
111 |
addcolor(cval, ctmp); |
112 |
} |
113 |
if (ldot < 0.0 && np->tdiff > FTINY) { |
114 |
/* |
115 |
* Diffuse transmitted component. |
116 |
*/ |
117 |
copycolor(ctmp, np->mcolor); |
118 |
dtmp = -ldot * omega * np->tdiff / PI; |
119 |
scalecolor(ctmp, dtmp); |
120 |
addcolor(cval, ctmp); |
121 |
} |
122 |
if (ldot > 0.0 ? np->rspec <= FTINY : np->tspec <= FTINY) |
123 |
return; /* no specular component */ |
124 |
/* set up function */ |
125 |
setfunc(np->mp, np->pr); |
126 |
sa = np->mp->oargs.sarg; |
127 |
errno = 0; |
128 |
/* transform light vector */ |
129 |
multv3(ldx, ldir, funcxf.xfm); |
130 |
for (i = 0; i < 3; i++) |
131 |
ldx[i] /= funcxf.sca; |
132 |
/* compute BRTDF */ |
133 |
if (np->mp->otype == MAT_BRTDF) { |
134 |
colval(ctmp,RED) = funvalue(sa[6], 3, ldx); |
135 |
if (!strcmp(sa[7],sa[6])) |
136 |
colval(ctmp,GRN) = colval(ctmp,RED); |
137 |
else |
138 |
colval(ctmp,GRN) = funvalue(sa[7], 3, ldx); |
139 |
if (!strcmp(sa[8],sa[6])) |
140 |
colval(ctmp,BLU) = colval(ctmp,RED); |
141 |
else if (!strcmp(sa[8],sa[7])) |
142 |
colval(ctmp,BLU) = colval(ctmp,GRN); |
143 |
else |
144 |
colval(ctmp,BLU) = funvalue(sa[8], 3, ldx); |
145 |
dtmp = bright(ctmp); |
146 |
} else if (np->dp == NULL) { |
147 |
dtmp = funvalue(sa[0], 3, ldx); |
148 |
setcolor(ctmp, dtmp, dtmp, dtmp); |
149 |
} else { |
150 |
for (i = 0; i < np->dp->nd; i++) |
151 |
pt[i] = funvalue(sa[3+i], 3, ldx); |
152 |
dtmp = datavalue(np->dp, pt); |
153 |
dtmp = funvalue(sa[0], 1, &dtmp); |
154 |
setcolor(ctmp, dtmp, dtmp, dtmp); |
155 |
} |
156 |
if (errno) |
157 |
goto computerr; |
158 |
if (dtmp <= FTINY) |
159 |
return; |
160 |
if (ldot > 0.0) { |
161 |
/* |
162 |
* Compute reflected non-diffuse component. |
163 |
*/ |
164 |
if (np->mp->otype == MAT_MFUNC || np->mp->otype == MAT_MDATA) |
165 |
multcolor(ctmp, np->mcolor); |
166 |
dtmp = ldot * omega * np->rspec; |
167 |
scalecolor(ctmp, dtmp); |
168 |
addcolor(cval, ctmp); |
169 |
} else { |
170 |
/* |
171 |
* Compute transmitted non-diffuse component. |
172 |
*/ |
173 |
if (np->mp->otype == MAT_TFUNC || np->mp->otype == MAT_TDATA) |
174 |
multcolor(ctmp, np->mcolor); |
175 |
dtmp = -ldot * omega * np->tspec; |
176 |
scalecolor(ctmp, dtmp); |
177 |
addcolor(cval, ctmp); |
178 |
} |
179 |
return; |
180 |
computerr: |
181 |
objerror(np->mp, WARNING, "compute error"); |
182 |
return; |
183 |
} |
184 |
|
185 |
|
186 |
m_brdf(m, r) /* color a ray which hit a BRDF material */ |
187 |
register OBJREC *m; |
188 |
register RAY *r; |
189 |
{ |
190 |
int minsa, minfa; |
191 |
BRDFDAT nd; |
192 |
double transtest, transdist; |
193 |
COLOR ctmp; |
194 |
double dtmp; |
195 |
FVECT vec; |
196 |
register int i; |
197 |
/* check arguments */ |
198 |
switch (m->otype) { |
199 |
case MAT_PFUNC: case MAT_MFUNC: |
200 |
minsa = 2; minfa = 4; break; |
201 |
case MAT_PDATA: case MAT_MDATA: |
202 |
minsa = 4; minfa = 4; break; |
203 |
case MAT_TFUNC: |
204 |
minsa = 2; minfa = 6; break; |
205 |
case MAT_TDATA: |
206 |
minsa = 4; minfa = 6; break; |
207 |
case MAT_BRTDF: |
208 |
minsa = 10; minfa = 6; break; |
209 |
} |
210 |
if (m->oargs.nsargs < minsa || m->oargs.nfargs < minfa) |
211 |
objerror(m, USER, "bad # arguments"); |
212 |
nd.mp = m; |
213 |
nd.pr = r; |
214 |
/* get specular component */ |
215 |
nd.rspec = m->oargs.farg[3]; |
216 |
/* compute transmission */ |
217 |
if (m->otype == MAT_TFUNC || m->otype == MAT_TDATA |
218 |
|| m->otype == MAT_BRTDF) { |
219 |
nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec); |
220 |
nd.tspec = nd.trans * m->oargs.farg[5]; |
221 |
nd.tdiff = nd.trans - nd.tspec; |
222 |
} else |
223 |
nd.tdiff = nd.tspec = nd.trans = 0.0; |
224 |
/* early shadow check */ |
225 |
if (r->crtype & SHADOW && (m->otype != MAT_BRTDF || nd.tspec <= FTINY)) |
226 |
return; |
227 |
/* diffuse reflection */ |
228 |
nd.rdiff = 1.0 - nd.trans - nd.rspec; |
229 |
/* get material color */ |
230 |
setcolor(nd.mcolor, m->oargs.farg[0], |
231 |
m->oargs.farg[1], |
232 |
m->oargs.farg[2]); |
233 |
/* fix orientation */ |
234 |
if (r->rod < 0.0) |
235 |
flipsurface(r); |
236 |
/* get modifiers */ |
237 |
raytexture(r, m->omod); |
238 |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
239 |
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
240 |
transtest = 0; |
241 |
/* load auxiliary files */ |
242 |
if (m->otype == MAT_PDATA || m->otype == MAT_MDATA |
243 |
|| m->otype == MAT_TDATA) { |
244 |
nd.dp = getdata(m->oargs.sarg[1]); |
245 |
for (i = 3; i < m->oargs.nsargs; i++) |
246 |
if (m->oargs.sarg[i][0] == '-') |
247 |
break; |
248 |
if (i-3 != nd.dp->nd) |
249 |
objerror(m, USER, "dimension error"); |
250 |
if (!fundefined(m->oargs.sarg[3])) |
251 |
loadfunc(m->oargs.sarg[2]); |
252 |
} else if (m->otype == MAT_BRTDF) { |
253 |
nd.dp = NULL; |
254 |
if (!fundefined(m->oargs.sarg[7])) |
255 |
loadfunc(m->oargs.sarg[9]); |
256 |
} else { |
257 |
nd.dp = NULL; |
258 |
if (!fundefined(m->oargs.sarg[0])) |
259 |
loadfunc(m->oargs.sarg[1]); |
260 |
} |
261 |
/* set special variables */ |
262 |
setfunc(m, r); |
263 |
multv3(vec, nd.pnorm, funcxf.xfm); |
264 |
varset("NxP", '=', vec[0]/funcxf.sca); |
265 |
varset("NyP", '=', vec[1]/funcxf.sca); |
266 |
varset("NzP", '=', vec[2]/funcxf.sca); |
267 |
varset("RdotP", '=', nd.pdot); |
268 |
varset("CrP", '=', colval(nd.mcolor,RED)); |
269 |
varset("CgP", '=', colval(nd.mcolor,GRN)); |
270 |
varset("CbP", '=', colval(nd.mcolor,BLU)); |
271 |
/* compute transmitted ray */ |
272 |
if (m->otype == MAT_BRTDF && nd.tspec > FTINY) { |
273 |
RAY sr; |
274 |
errno = 0; |
275 |
setcolor(ctmp, varvalue(m->oargs.sarg[0]), |
276 |
varvalue(m->oargs.sarg[1]), |
277 |
varvalue(m->oargs.sarg[2])); |
278 |
scalecolor(ctmp, nd.tspec); |
279 |
if (errno) |
280 |
objerror(m, WARNING, "compute error"); |
281 |
else if ((dtmp = bright(ctmp)) > FTINY && |
282 |
rayorigin(&sr, r, TRANS, dtmp) == 0) { |
283 |
if (DOT(r->pert,r->pert) > FTINY*FTINY) { |
284 |
for (i = 0; i < 3; i++) /* perturb direction */ |
285 |
sr.rdir[i] = r->rdir[i] - |
286 |
.75*r->pert[i]; |
287 |
normalize(sr.rdir); |
288 |
} else { |
289 |
VCOPY(sr.rdir, r->rdir); |
290 |
transtest = 2; |
291 |
} |
292 |
rayvalue(&sr); |
293 |
multcolor(sr.rcol, ctmp); |
294 |
addcolor(r->rcol, sr.rcol); |
295 |
transtest *= bright(sr.rcol); |
296 |
transdist = r->rot + sr.rt; |
297 |
} |
298 |
} |
299 |
if (r->crtype & SHADOW) /* the rest is shadow */ |
300 |
return; |
301 |
/* compute reflected ray */ |
302 |
if (m->otype == MAT_BRTDF && nd.rspec > FTINY) { |
303 |
RAY sr; |
304 |
errno = 0; |
305 |
setcolor(ctmp, varvalue(m->oargs.sarg[3]), |
306 |
varvalue(m->oargs.sarg[4]), |
307 |
varvalue(m->oargs.sarg[5])); |
308 |
scalecolor(ctmp, nd.rspec); |
309 |
if (errno) |
310 |
objerror(m, WARNING, "compute error"); |
311 |
else if ((dtmp = bright(ctmp)) > FTINY && |
312 |
rayorigin(&sr, r, REFLECTED, dtmp) == 0) { |
313 |
for (i = 0; i < 3; i++) |
314 |
sr.rdir[i] = r->rdir[i] + |
315 |
2.0*nd.pdot*nd.pnorm[i]; |
316 |
rayvalue(&sr); |
317 |
multcolor(sr.rcol, ctmp); |
318 |
addcolor(r->rcol, sr.rcol); |
319 |
} |
320 |
} |
321 |
/* compute ambient */ |
322 |
if (nd.rdiff > FTINY) { |
323 |
ambient(ctmp, r); |
324 |
if (m->otype == MAT_BRTDF) |
325 |
scalecolor(ctmp, nd.rdiff); |
326 |
else |
327 |
scalecolor(ctmp, 1.0-nd.trans); |
328 |
multcolor(ctmp, nd.mcolor); /* modified by material color */ |
329 |
addcolor(r->rcol, ctmp); /* add to returned color */ |
330 |
} |
331 |
if (nd.tdiff > FTINY) { /* from other side */ |
332 |
flipsurface(r); |
333 |
ambient(ctmp, r); |
334 |
if (m->otype == MAT_BRTDF) |
335 |
scalecolor(ctmp, nd.tdiff); |
336 |
else |
337 |
scalecolor(ctmp, nd.trans); |
338 |
multcolor(ctmp, nd.mcolor); |
339 |
addcolor(r->rcol, ctmp); |
340 |
flipsurface(r); |
341 |
} |
342 |
/* add direct component */ |
343 |
direct(r, dirbrdf, &nd); |
344 |
/* check distance */ |
345 |
if (transtest > bright(r->rcol)) |
346 |
r->rt = transdist; |
347 |
} |