1 |
– |
/* Copyright (c) 1991 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 |
|
* Shading for materials with arbitrary BRDF's |
6 |
|
*/ |
7 |
|
|
8 |
< |
#include "ray.h" |
8 |
> |
#include "copyright.h" |
9 |
|
|
10 |
+ |
#include "ray.h" |
11 |
+ |
#include "ambient.h" |
12 |
|
#include "data.h" |
13 |
< |
|
13 |
> |
#include "source.h" |
14 |
|
#include "otypes.h" |
15 |
< |
|
15 |
> |
#include "rtotypes.h" |
16 |
|
#include "func.h" |
17 |
|
|
19 |
– |
extern int backvis; /* back faces visible? */ |
20 |
– |
|
18 |
|
/* |
19 |
|
* Arguments to this material include the color and specularity. |
20 |
|
* String arguments include the reflection function and files. |
77 |
|
} BRDFDAT; /* BRDF material data */ |
78 |
|
|
79 |
|
|
80 |
< |
dirbrdf(cval, np, ldir, omega) /* compute source contribution */ |
81 |
< |
COLOR cval; /* returned coefficient */ |
82 |
< |
register BRDFDAT *np; /* material data */ |
83 |
< |
FVECT ldir; /* light source direction */ |
84 |
< |
double omega; /* light source size */ |
80 |
> |
static srcdirf_t dirbrdf; |
81 |
> |
static int setbrdfunc(BRDFDAT *np); |
82 |
> |
|
83 |
> |
|
84 |
> |
static void |
85 |
> |
dirbrdf( /* compute source contribution */ |
86 |
> |
COLOR cval, /* returned coefficient */ |
87 |
> |
void *nnp, /* material data */ |
88 |
> |
FVECT ldir, /* light source direction */ |
89 |
> |
double omega /* light source size */ |
90 |
> |
) |
91 |
|
{ |
92 |
+ |
register BRDFDAT *np = nnp; |
93 |
|
double ldot; |
94 |
|
double dtmp; |
95 |
|
COLOR ctmp; |
166 |
|
dtmp = funvalue(sa[0], 5, vldx); |
167 |
|
setcolor(ctmp, dtmp, dtmp, dtmp); |
168 |
|
} |
169 |
< |
if (errno) { |
169 |
> |
if (errno == EDOM || errno == ERANGE) { |
170 |
|
objerror(np->mp, WARNING, "compute error"); |
171 |
|
return; |
172 |
|
} |
176 |
|
/* |
177 |
|
* Compute reflected non-diffuse component. |
178 |
|
*/ |
179 |
< |
if (np->mp->otype == MAT_MFUNC | np->mp->otype == MAT_MDATA) |
179 |
> |
if ((np->mp->otype == MAT_MFUNC) | (np->mp->otype == MAT_MDATA)) |
180 |
|
multcolor(ctmp, np->mcolor); |
181 |
|
dtmp = ldot * omega * np->rspec; |
182 |
|
scalecolor(ctmp, dtmp); |
185 |
|
/* |
186 |
|
* Compute transmitted non-diffuse component. |
187 |
|
*/ |
188 |
< |
if (np->mp->otype == MAT_TFUNC | np->mp->otype == MAT_TDATA) |
188 |
> |
if ((np->mp->otype == MAT_TFUNC) | (np->mp->otype == MAT_TDATA)) |
189 |
|
multcolor(ctmp, np->mcolor); |
190 |
|
dtmp = -ldot * omega * np->tspec; |
191 |
|
scalecolor(ctmp, dtmp); |
195 |
|
} |
196 |
|
|
197 |
|
|
198 |
< |
m_brdf(m, r) /* color a ray which hit a BRDTfunc material */ |
199 |
< |
register OBJREC *m; |
200 |
< |
register RAY *r; |
198 |
> |
extern int |
199 |
> |
m_brdf( /* color a ray that hit a BRDTfunc material */ |
200 |
> |
register OBJREC *m, |
201 |
> |
register RAY *r |
202 |
> |
) |
203 |
|
{ |
204 |
+ |
int hitfront = 1; |
205 |
|
BRDFDAT nd; |
206 |
|
RAY sr; |
207 |
+ |
double mirtest=0, mirdist=0; |
208 |
|
double transtest, transdist; |
209 |
|
int hasrefl, hastrans; |
210 |
+ |
int hastexture; |
211 |
|
COLOR ctmp; |
212 |
+ |
FVECT vtmp; |
213 |
+ |
double d; |
214 |
|
register MFUNC *mf; |
215 |
|
register int i; |
216 |
|
/* check arguments */ |
217 |
< |
if (m->oargs.nsargs < 10 | m->oargs.nfargs < 9) |
217 |
> |
if ((m->oargs.nsargs < 10) | (m->oargs.nfargs < 9)) |
218 |
|
objerror(m, USER, "bad # arguments"); |
219 |
|
nd.mp = m; |
220 |
|
nd.pr = r; |
234 |
|
setcolor(nd.tdiff, m->oargs.farg[6], |
235 |
|
m->oargs.farg[7], |
236 |
|
m->oargs.farg[8]); |
237 |
< |
/* get modifiers */ |
237 |
> |
/* get modifiers */ |
238 |
|
raytexture(r, m->omod); |
239 |
< |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
239 |
> |
hastexture = DOT(r->pert,r->pert) > FTINY*FTINY; |
240 |
> |
if (hastexture) { /* perturb normal */ |
241 |
> |
nd.pdot = raynormal(nd.pnorm, r); |
242 |
> |
} else { |
243 |
> |
VCOPY(nd.pnorm, r->ron); |
244 |
> |
nd.pdot = r->rod; |
245 |
> |
} |
246 |
|
if (r->rod < 0.0) { /* orient perturbed values */ |
247 |
|
nd.pdot = -nd.pdot; |
248 |
|
for (i = 0; i < 3; i++) { |
249 |
|
nd.pnorm[i] = -nd.pnorm[i]; |
250 |
|
r->pert[i] = -r->pert[i]; |
251 |
|
} |
252 |
+ |
hitfront = 0; |
253 |
|
} |
254 |
|
copycolor(nd.mcolor, r->pcol); /* get pattern color */ |
255 |
|
multcolor(nd.rdiff, nd.mcolor); /* modify diffuse values */ |
261 |
|
mf = getfunc(m, 9, 0x3f, 0); |
262 |
|
/* compute transmitted ray */ |
263 |
|
setbrdfunc(&nd); |
246 |
– |
transtest = 0; |
247 |
– |
transdist = r->rot; |
264 |
|
errno = 0; |
265 |
|
setcolor(ctmp, evalue(mf->ep[3]), |
266 |
|
evalue(mf->ep[4]), |
267 |
|
evalue(mf->ep[5])); |
268 |
< |
if (errno) |
268 |
> |
if (errno == EDOM || errno == ERANGE) |
269 |
|
objerror(m, WARNING, "compute error"); |
270 |
< |
else if (rayorigin(&sr, r, TRANS, bright(ctmp)) == 0) { |
271 |
< |
if (!(r->crtype & SHADOW) && |
256 |
< |
DOT(r->pert,r->pert) > FTINY*FTINY) { |
270 |
> |
else if (rayorigin(&sr, TRANS, r, ctmp) == 0) { |
271 |
> |
if (!(r->crtype & SHADOW) && hastexture) { |
272 |
|
for (i = 0; i < 3; i++) /* perturb direction */ |
273 |
|
sr.rdir[i] = r->rdir[i] - .75*r->pert[i]; |
274 |
|
if (normalize(sr.rdir) == 0.0) { |
277 |
|
} |
278 |
|
} else { |
279 |
|
VCOPY(sr.rdir, r->rdir); |
265 |
– |
transtest = 2; |
280 |
|
} |
281 |
|
rayvalue(&sr); |
282 |
< |
multcolor(sr.rcol, ctmp); |
282 |
> |
multcolor(sr.rcol, sr.rcoef); |
283 |
|
addcolor(r->rcol, sr.rcol); |
284 |
< |
transtest *= bright(sr.rcol); |
285 |
< |
transdist = r->rot + sr.rt; |
284 |
> |
if (!hastexture) { |
285 |
> |
transtest = 2.0*bright(sr.rcol); |
286 |
> |
transdist = r->rot + sr.rt; |
287 |
> |
} |
288 |
|
} |
289 |
|
if (r->crtype & SHADOW) /* the rest is shadow */ |
290 |
|
return(1); |
294 |
|
setcolor(ctmp, evalue(mf->ep[0]), |
295 |
|
evalue(mf->ep[1]), |
296 |
|
evalue(mf->ep[2])); |
297 |
< |
if (errno) |
297 |
> |
if (errno == EDOM || errno == ERANGE) |
298 |
|
objerror(m, WARNING, "compute error"); |
299 |
< |
else if (rayorigin(&sr, r, REFLECTED, bright(ctmp)) == 0) { |
299 |
> |
else if (rayorigin(&sr, REFLECTED, r, ctmp) == 0) { |
300 |
|
for (i = 0; i < 3; i++) |
301 |
|
sr.rdir[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i]; |
302 |
|
rayvalue(&sr); |
303 |
< |
multcolor(sr.rcol, ctmp); |
303 |
> |
multcolor(sr.rcol, sr.rcoef); |
304 |
|
addcolor(r->rcol, sr.rcol); |
305 |
+ |
if (!hastexture && r->ro != NULL && isflat(r->ro->otype)) { |
306 |
+ |
mirtest = 2.0*bright(sr.rcol); |
307 |
+ |
mirdist = r->rot + sr.rt; |
308 |
+ |
} |
309 |
|
} |
310 |
|
/* compute ambient */ |
311 |
|
if (hasrefl) { |
312 |
< |
if (nd.pdot < 0.0) |
312 |
> |
if (!hitfront) |
313 |
|
flipsurface(r); |
314 |
< |
ambient(ctmp, r, nd.pnorm); |
315 |
< |
multcolor(ctmp, nd.rdiff); |
314 |
> |
copycolor(ctmp, nd.rdiff); |
315 |
> |
multambient(ctmp, r, nd.pnorm); |
316 |
|
addcolor(r->rcol, ctmp); /* add to returned color */ |
317 |
< |
if (nd.pdot < 0.0) |
317 |
> |
if (!hitfront) |
318 |
|
flipsurface(r); |
319 |
|
} |
320 |
|
if (hastrans) { /* from other side */ |
321 |
< |
if (nd.pdot > 0.0) |
321 |
> |
if (hitfront) |
322 |
|
flipsurface(r); |
323 |
< |
ambient(ctmp, r, nd.pnorm); |
324 |
< |
multcolor(ctmp, nd.tdiff); |
323 |
> |
vtmp[0] = -nd.pnorm[0]; |
324 |
> |
vtmp[1] = -nd.pnorm[1]; |
325 |
> |
vtmp[2] = -nd.pnorm[2]; |
326 |
> |
copycolor(ctmp, nd.tdiff); |
327 |
> |
multambient(ctmp, r, vtmp); |
328 |
|
addcolor(r->rcol, ctmp); |
329 |
< |
if (nd.pdot > 0.0) |
329 |
> |
if (hitfront) |
330 |
|
flipsurface(r); |
331 |
|
} |
332 |
|
if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0') |
333 |
|
direct(r, dirbrdf, &nd); /* add direct component */ |
334 |
< |
/* check distance */ |
335 |
< |
if (transtest > bright(r->rcol)) |
334 |
> |
|
335 |
> |
d = bright(r->rcol); /* set effective distance */ |
336 |
> |
if (transtest > d) |
337 |
|
r->rt = transdist; |
338 |
+ |
else if (mirtest > d) |
339 |
+ |
r->rt = mirdist; |
340 |
|
|
341 |
|
return(1); |
342 |
|
} |
343 |
|
|
344 |
|
|
345 |
|
|
346 |
< |
m_brdf2(m, r) /* color a ray which hit a BRDF material */ |
347 |
< |
register OBJREC *m; |
348 |
< |
register RAY *r; |
346 |
> |
extern int |
347 |
> |
m_brdf2( /* color a ray that hit a BRDF material */ |
348 |
> |
register OBJREC *m, |
349 |
> |
register RAY *r |
350 |
> |
) |
351 |
|
{ |
352 |
|
BRDFDAT nd; |
353 |
|
COLOR ctmp; |
354 |
+ |
FVECT vtmp; |
355 |
|
double dtmp; |
356 |
|
/* always a shadow */ |
357 |
|
if (r->crtype & SHADOW) |
358 |
|
return(1); |
359 |
|
/* check arguments */ |
360 |
< |
if (m->oargs.nsargs < (hasdata(m->otype)?4:2) | m->oargs.nfargs < |
361 |
< |
(m->otype==MAT_TFUNC|m->otype==MAT_TDATA?6:4)) |
360 |
> |
if ((m->oargs.nsargs < (hasdata(m->otype)?4:2)) | (m->oargs.nfargs < |
361 |
> |
((m->otype==MAT_TFUNC)|(m->otype==MAT_TDATA)?6:4))) |
362 |
|
objerror(m, USER, "bad # arguments"); |
363 |
+ |
/* check for back side */ |
364 |
+ |
if (r->rod < 0.0) { |
365 |
+ |
if (!backvis && m->otype != MAT_TFUNC |
366 |
+ |
&& m->otype != MAT_TDATA) { |
367 |
+ |
raytrans(r); |
368 |
+ |
return(1); |
369 |
+ |
} |
370 |
+ |
raytexture(r, m->omod); |
371 |
+ |
flipsurface(r); /* reorient if backvis */ |
372 |
+ |
} else |
373 |
+ |
raytexture(r, m->omod); |
374 |
+ |
|
375 |
|
nd.mp = m; |
376 |
|
nd.pr = r; |
377 |
|
/* get material color */ |
381 |
|
/* get specular component */ |
382 |
|
nd.rspec = m->oargs.farg[3]; |
383 |
|
/* compute transmittance */ |
384 |
< |
if (m->otype == MAT_TFUNC | m->otype == MAT_TDATA) { |
384 |
> |
if ((m->otype == MAT_TFUNC) | (m->otype == MAT_TDATA)) { |
385 |
|
nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec); |
386 |
|
nd.tspec = nd.trans * m->oargs.farg[5]; |
387 |
|
dtmp = nd.trans - nd.tspec; |
393 |
|
/* compute reflectance */ |
394 |
|
dtmp = 1.0 - nd.trans - nd.rspec; |
395 |
|
setcolor(nd.rdiff, dtmp, dtmp, dtmp); |
355 |
– |
/* check for back side */ |
356 |
– |
if (r->rod < 0.0) { |
357 |
– |
if (!backvis && m->otype != MAT_TFUNC |
358 |
– |
&& m->otype != MAT_TDATA) { |
359 |
– |
raytrans(r); |
360 |
– |
return(1); |
361 |
– |
} |
362 |
– |
flipsurface(r); /* reorient if backvis */ |
363 |
– |
} |
364 |
– |
/* get modifiers */ |
365 |
– |
raytexture(r, m->omod); |
396 |
|
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
397 |
|
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
398 |
|
multcolor(nd.rdiff, nd.mcolor); |
407 |
|
} |
408 |
|
/* compute ambient */ |
409 |
|
if (nd.trans < 1.0-FTINY) { |
410 |
< |
ambient(ctmp, r, nd.pnorm); |
410 |
> |
copycolor(ctmp, nd.mcolor); /* modified by material color */ |
411 |
|
scalecolor(ctmp, 1.0-nd.trans); |
412 |
< |
multcolor(ctmp, nd.mcolor); /* modified by material color */ |
412 |
> |
multambient(ctmp, r, nd.pnorm); |
413 |
|
addcolor(r->rcol, ctmp); /* add to returned color */ |
414 |
|
} |
415 |
|
if (nd.trans > FTINY) { /* from other side */ |
416 |
|
flipsurface(r); |
417 |
< |
ambient(ctmp, r, nd.pnorm); |
417 |
> |
vtmp[0] = -nd.pnorm[0]; |
418 |
> |
vtmp[1] = -nd.pnorm[1]; |
419 |
> |
vtmp[2] = -nd.pnorm[2]; |
420 |
> |
copycolor(ctmp, nd.mcolor); |
421 |
|
scalecolor(ctmp, nd.trans); |
422 |
< |
multcolor(ctmp, nd.mcolor); |
422 |
> |
multambient(ctmp, r, vtmp); |
423 |
|
addcolor(r->rcol, ctmp); |
424 |
|
flipsurface(r); |
425 |
|
} |
430 |
|
} |
431 |
|
|
432 |
|
|
433 |
< |
setbrdfunc(np) /* set up brdf function and variables */ |
434 |
< |
register BRDFDAT *np; |
433 |
> |
static int |
434 |
> |
setbrdfunc( /* set up brdf function and variables */ |
435 |
> |
register BRDFDAT *np |
436 |
> |
) |
437 |
|
{ |
438 |
|
FVECT vec; |
439 |
|
|