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