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 |
< |
* of the direction to the light source. |
24 |
> |
* of the direction to the light source. (Data modification functions |
25 |
> |
* are passed the source direction as args 2-4.) |
26 |
|
* We orient the surface towards the incoming ray, so a single |
27 |
|
* surface can be used to represent an infinitely thin object. |
28 |
|
* |
52 |
|
* rbrtd gbrtd bbrtd |
53 |
|
* funcfile transform |
54 |
|
* 0 |
55 |
< |
* 6+ red grn blu rspec trans tspec A7 .. |
55 |
> |
* 9+ rdf gdf bdf |
56 |
> |
* rdb gdb bdb |
57 |
> |
* rdt gdt bdt A10 .. |
58 |
|
* |
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 |
< |
* CrP, CgP, CbP - perturbed material color |
63 |
> |
* CrP, CgP, CbP - perturbed material color (or pattern) |
64 |
|
*/ |
65 |
|
|
66 |
|
typedef struct { |
67 |
|
OBJREC *mp; /* material pointer */ |
68 |
|
RAY *pr; /* intersected ray */ |
69 |
|
DATARRAY *dp; /* data array for PDATA, MDATA or TDATA */ |
70 |
< |
COLOR mcolor; /* color of this material */ |
71 |
< |
double rspec; /* specular reflection */ |
72 |
< |
double rdiff; /* diffuse reflection */ |
73 |
< |
double trans; /* transmissivity */ |
74 |
< |
double tspec; /* specular transmission */ |
75 |
< |
double tdiff; /* diffuse transmission */ |
70 |
> |
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 |
|
FVECT pnorm; /* perturbed surface normal */ |
77 |
|
double pdot; /* perturbed dot product */ |
78 |
|
} BRDFDAT; /* BRDF material data */ |
89 |
|
COLOR ctmp; |
90 |
|
FVECT ldx; |
91 |
|
double lddx[3], pt[MAXDIM]; |
92 |
+ |
double vldx[4]; |
93 |
|
register char **sa; |
94 |
|
register int i; |
95 |
|
|
99 |
|
|
100 |
|
if (ldot <= FTINY && ldot >= -FTINY) |
101 |
|
return; /* too close to grazing */ |
102 |
+ |
|
103 |
|
if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY) |
104 |
|
return; /* wrong side */ |
105 |
|
|
106 |
< |
if (ldot > 0.0 && np->rdiff > FTINY) { |
106 |
> |
if (ldot > 0.0) { |
107 |
|
/* |
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 |
< |
copycolor(ctmp, np->mcolor); |
113 |
< |
dtmp = ldot * omega * np->rdiff / PI; |
112 |
> |
copycolor(ctmp, np->rdiff); |
113 |
> |
dtmp = ldot * omega / PI; |
114 |
|
scalecolor(ctmp, dtmp); |
115 |
|
addcolor(cval, ctmp); |
116 |
< |
} |
112 |
< |
if (ldot < 0.0 && np->tdiff > FTINY) { |
116 |
> |
} else { |
117 |
|
/* |
118 |
|
* Diffuse transmitted component. |
119 |
|
*/ |
120 |
< |
copycolor(ctmp, np->mcolor); |
121 |
< |
dtmp = -ldot * omega * np->tdiff / PI; |
120 |
> |
copycolor(ctmp, np->tdiff); |
121 |
> |
dtmp = -ldot * omega / PI; |
122 |
|
scalecolor(ctmp, dtmp); |
123 |
|
addcolor(cval, ctmp); |
124 |
|
} |
134 |
|
lddx[i] = ldx[i]/funcxf.sca; |
135 |
|
/* compute BRTDF */ |
136 |
|
if (np->mp->otype == MAT_BRTDF) { |
137 |
< |
colval(ctmp,RED) = funvalue(sa[6], 3, lddx); |
137 |
> |
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 |
|
if (!strcmp(sa[7],sa[6])) |
142 |
|
colval(ctmp,GRN) = colval(ctmp,RED); |
143 |
|
else |
155 |
|
} else { |
156 |
|
for (i = 0; i < np->dp->nd; i++) |
157 |
|
pt[i] = funvalue(sa[3+i], 3, lddx); |
158 |
< |
dtmp = datavalue(np->dp, pt); |
159 |
< |
dtmp = funvalue(sa[0], 1, &dtmp); |
158 |
> |
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 |
|
setcolor(ctmp, dtmp, dtmp, dtmp); |
162 |
|
} |
163 |
|
if (errno) { |
170 |
|
/* |
171 |
|
* Compute reflected non-diffuse component. |
172 |
|
*/ |
173 |
< |
if (np->mp->otype == MAT_MFUNC || np->mp->otype == MAT_MDATA) |
173 |
> |
if (np->mp->otype == MAT_MFUNC | np->mp->otype == MAT_MDATA) |
174 |
|
multcolor(ctmp, np->mcolor); |
175 |
|
dtmp = ldot * omega * np->rspec; |
176 |
|
scalecolor(ctmp, dtmp); |
179 |
|
/* |
180 |
|
* Compute transmitted non-diffuse component. |
181 |
|
*/ |
182 |
< |
if (np->mp->otype == MAT_TFUNC || np->mp->otype == MAT_TDATA) |
182 |
> |
if (np->mp->otype == MAT_TFUNC | np->mp->otype == MAT_TDATA) |
183 |
|
multcolor(ctmp, np->mcolor); |
184 |
|
dtmp = -ldot * omega * np->tspec; |
185 |
|
scalecolor(ctmp, dtmp); |
188 |
|
} |
189 |
|
|
190 |
|
|
191 |
< |
m_brdf(m, r) /* color a ray which hit a BRDF material */ |
191 |
> |
m_brdf(m, r) /* color a ray which hit a BRDTF material */ |
192 |
|
register OBJREC *m; |
193 |
|
register RAY *r; |
194 |
|
{ |
187 |
– |
int minsa, minfa; |
195 |
|
BRDFDAT nd; |
196 |
+ |
RAY sr; |
197 |
|
double transtest, transdist; |
198 |
+ |
int hasrefl, hastrans; |
199 |
|
COLOR ctmp; |
200 |
< |
double dtmp, tspect, rspecr; |
201 |
< |
MFUNC *mf; |
200 |
> |
double dtmp; |
201 |
> |
register MFUNC *mf; |
202 |
|
register int i; |
203 |
|
/* check arguments */ |
204 |
< |
switch (m->otype) { |
205 |
< |
case MAT_PFUNC: case MAT_MFUNC: |
206 |
< |
minsa = 2; minfa = 4; break; |
207 |
< |
case MAT_PDATA: case MAT_MDATA: |
208 |
< |
minsa = 4; minfa = 4; break; |
209 |
< |
case MAT_TFUNC: |
210 |
< |
minsa = 2; minfa = 6; break; |
211 |
< |
case MAT_TDATA: |
212 |
< |
minsa = 4; minfa = 6; break; |
213 |
< |
case MAT_BRTDF: |
214 |
< |
minsa = 10; minfa = 6; break; |
204 |
> |
if (m->oargs.nsargs < 10 | m->oargs.nfargs < 9) |
205 |
> |
objerror(m, USER, "bad # arguments"); |
206 |
> |
nd.mp = m; |
207 |
> |
nd.pr = r; |
208 |
> |
/* dummy values */ |
209 |
> |
nd.rspec = nd.tspec = 1.0; |
210 |
> |
nd.trans = 0.5; |
211 |
> |
/* diffuse reflectance */ |
212 |
> |
if (r->rod > 0.0) |
213 |
> |
setcolor(nd.rdiff, m->oargs.farg[0], |
214 |
> |
m->oargs.farg[1], |
215 |
> |
m->oargs.farg[2]); |
216 |
> |
else |
217 |
> |
setcolor(nd.rdiff, m->oargs.farg[3], |
218 |
> |
m->oargs.farg[4], |
219 |
> |
m->oargs.farg[5]); |
220 |
> |
/* diffuse transmittance */ |
221 |
> |
setcolor(nd.tdiff, m->oargs.farg[6], |
222 |
> |
m->oargs.farg[7], |
223 |
> |
m->oargs.farg[8]); |
224 |
> |
/* get modifiers */ |
225 |
> |
raytexture(r, m->omod); |
226 |
> |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
227 |
> |
if (r->rod < 0.0) { /* orient perturbed values */ |
228 |
> |
nd.pdot = -nd.pdot; |
229 |
> |
for (i = 0; i < 3; i++) { |
230 |
> |
nd.pnorm[i] = -nd.pnorm[i]; |
231 |
> |
r->pert[i] = -r->pert[i]; |
232 |
> |
} |
233 |
|
} |
234 |
< |
if (m->oargs.nsargs < minsa || m->oargs.nfargs < minfa) |
234 |
> |
copycolor(nd.mcolor, r->pcol); /* get pattern color */ |
235 |
> |
multcolor(nd.rdiff, nd.mcolor); /* modify diffuse values */ |
236 |
> |
multcolor(nd.tdiff, nd.mcolor); |
237 |
> |
hasrefl = bright(nd.rdiff) > FTINY; |
238 |
> |
hastrans = bright(nd.tdiff) > FTINY; |
239 |
> |
/* load cal file */ |
240 |
> |
nd.dp = NULL; |
241 |
> |
mf = getfunc(m, 9, 0x3f, 0); |
242 |
> |
/* compute transmitted ray */ |
243 |
> |
setbrdfunc(&nd); |
244 |
> |
transtest = 0; |
245 |
> |
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 |
|
objerror(m, USER, "bad # arguments"); |
329 |
|
nd.mp = m; |
330 |
|
nd.pr = r; |
331 |
+ |
/* get material color */ |
332 |
+ |
setcolor(nd.mcolor, m->oargs.farg[0], |
333 |
+ |
m->oargs.farg[1], |
334 |
+ |
m->oargs.farg[2]); |
335 |
|
/* get specular component */ |
336 |
|
nd.rspec = m->oargs.farg[3]; |
337 |
< |
/* compute transmission */ |
338 |
< |
if (m->otype == MAT_TFUNC || m->otype == MAT_TDATA |
215 |
< |
|| m->otype == MAT_BRTDF) { |
337 |
> |
/* compute transmittance */ |
338 |
> |
if (m->otype == MAT_TFUNC | m->otype == MAT_TDATA) { |
339 |
|
nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec); |
340 |
|
nd.tspec = nd.trans * m->oargs.farg[5]; |
341 |
< |
nd.tdiff = nd.trans - nd.tspec; |
342 |
< |
} else |
343 |
< |
nd.tdiff = nd.tspec = nd.trans = 0.0; |
344 |
< |
/* early shadow check */ |
345 |
< |
if (r->crtype & SHADOW && (m->otype != MAT_BRTDF || nd.tspec <= FTINY)) |
346 |
< |
return; |
347 |
< |
/* diffuse reflection */ |
348 |
< |
nd.rdiff = 1.0 - nd.trans - nd.rspec; |
349 |
< |
/* get material color */ |
227 |
< |
setcolor(nd.mcolor, m->oargs.farg[0], |
228 |
< |
m->oargs.farg[1], |
229 |
< |
m->oargs.farg[2]); |
341 |
> |
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 |
|
/* fix orientation */ |
351 |
|
if (r->rod < 0.0) |
352 |
|
flipsurface(r); |
354 |
|
raytexture(r, m->omod); |
355 |
|
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
356 |
|
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
357 |
< |
transtest = 0; |
357 |
> |
multcolor(nd.rdiff, nd.mcolor); |
358 |
> |
multcolor(nd.tdiff, nd.mcolor); |
359 |
|
/* load auxiliary files */ |
360 |
|
if (hasdata(m->otype)) { |
361 |
|
nd.dp = getdata(m->oargs.sarg[1]); |
362 |
< |
i = (1 << nd.dp->nd) - 1; |
242 |
< |
mf = getfunc(m, 2, i<<3, 0); |
243 |
< |
} else if (m->otype == MAT_BRTDF) { |
244 |
< |
nd.dp = NULL; |
245 |
< |
mf = getfunc(m, 9, 0x3f, 0); |
362 |
> |
getfunc(m, 2, 0, 0); |
363 |
|
} else { |
364 |
|
nd.dp = NULL; |
365 |
< |
mf = getfunc(m, 1, 0, 0); |
365 |
> |
getfunc(m, 1, 0, 0); |
366 |
|
} |
250 |
– |
/* set special variables */ |
251 |
– |
setbrdfunc(&nd); |
252 |
– |
/* compute transmitted ray */ |
253 |
– |
tspect = 0.; |
254 |
– |
if (m->otype == MAT_BRTDF && nd.tspec > FTINY) { |
255 |
– |
RAY sr; |
256 |
– |
errno = 0; |
257 |
– |
setcolor(ctmp, evalue(mf->ep[3]), |
258 |
– |
evalue(mf->ep[4]), |
259 |
– |
evalue(mf->ep[5])); |
260 |
– |
scalecolor(ctmp, nd.trans); |
261 |
– |
if (errno) |
262 |
– |
objerror(m, WARNING, "compute error"); |
263 |
– |
else if ((tspect = bright(ctmp)) > FTINY && |
264 |
– |
rayorigin(&sr, r, TRANS, tspect) == 0) { |
265 |
– |
if (!(r->crtype & SHADOW) && |
266 |
– |
DOT(r->pert,r->pert) > FTINY*FTINY) { |
267 |
– |
for (i = 0; i < 3; i++) /* perturb direction */ |
268 |
– |
sr.rdir[i] = r->rdir[i] - |
269 |
– |
.75*r->pert[i]; |
270 |
– |
normalize(sr.rdir); |
271 |
– |
} else { |
272 |
– |
VCOPY(sr.rdir, r->rdir); |
273 |
– |
transtest = 2; |
274 |
– |
} |
275 |
– |
rayvalue(&sr); |
276 |
– |
multcolor(sr.rcol, ctmp); |
277 |
– |
addcolor(r->rcol, sr.rcol); |
278 |
– |
transtest *= bright(sr.rcol); |
279 |
– |
transdist = r->rot + sr.rt; |
280 |
– |
} |
281 |
– |
} |
282 |
– |
if (r->crtype & SHADOW) /* the rest is shadow */ |
283 |
– |
return; |
284 |
– |
/* compute reflected ray */ |
285 |
– |
rspecr = 0.; |
286 |
– |
if (m->otype == MAT_BRTDF && nd.rspec > FTINY) { |
287 |
– |
RAY sr; |
288 |
– |
errno = 0; |
289 |
– |
setcolor(ctmp, evalue(mf->ep[0]), |
290 |
– |
evalue(mf->ep[1]), |
291 |
– |
evalue(mf->ep[2])); |
292 |
– |
if (errno) |
293 |
– |
objerror(m, WARNING, "compute error"); |
294 |
– |
else if ((rspecr = bright(ctmp)) > FTINY && |
295 |
– |
rayorigin(&sr, r, REFLECTED, rspecr) == 0) { |
296 |
– |
for (i = 0; i < 3; i++) |
297 |
– |
sr.rdir[i] = r->rdir[i] + |
298 |
– |
2.0*nd.pdot*nd.pnorm[i]; |
299 |
– |
rayvalue(&sr); |
300 |
– |
multcolor(sr.rcol, ctmp); |
301 |
– |
addcolor(r->rcol, sr.rcol); |
302 |
– |
} |
303 |
– |
} |
367 |
|
/* compute ambient */ |
368 |
< |
if ((dtmp = 1.0-nd.trans-rspecr) > FTINY) { |
368 |
> |
if (nd.trans < 1.0-FTINY) { |
369 |
|
ambient(ctmp, r); |
370 |
< |
scalecolor(ctmp, dtmp); |
370 |
> |
scalecolor(ctmp, 1.0-nd.trans); |
371 |
|
multcolor(ctmp, nd.mcolor); /* modified by material color */ |
372 |
|
addcolor(r->rcol, ctmp); /* add to returned color */ |
373 |
|
} |
374 |
< |
if ((dtmp = nd.trans-tspect) > FTINY) { /* from other side */ |
374 |
> |
if (nd.trans > FTINY) { /* from other side */ |
375 |
|
flipsurface(r); |
376 |
|
ambient(ctmp, r); |
377 |
< |
scalecolor(ctmp, dtmp); |
377 |
> |
scalecolor(ctmp, nd.trans); |
378 |
|
multcolor(ctmp, nd.mcolor); |
379 |
|
addcolor(r->rcol, ctmp); |
380 |
|
flipsurface(r); |
381 |
|
} |
382 |
|
/* add direct component */ |
383 |
|
direct(r, dirbrdf, &nd); |
321 |
– |
/* check distance */ |
322 |
– |
if (transtest > bright(r->rcol)) |
323 |
– |
r->rt = transdist; |
384 |
|
} |
385 |
|
|
386 |
|
|