5 |
|
* Shading for materials with arbitrary BRDF's |
6 |
|
*/ |
7 |
|
|
8 |
< |
/* ==================================================================== |
9 |
< |
* The Radiance Software License, Version 1.0 |
10 |
< |
* |
11 |
< |
* Copyright (c) 1990 - 2002 The Regents of the University of California, |
12 |
< |
* through Lawrence Berkeley National Laboratory. All rights reserved. |
13 |
< |
* |
14 |
< |
* Redistribution and use in source and binary forms, with or without |
15 |
< |
* modification, are permitted provided that the following conditions |
16 |
< |
* are met: |
17 |
< |
* |
18 |
< |
* 1. Redistributions of source code must retain the above copyright |
19 |
< |
* notice, this list of conditions and the following disclaimer. |
20 |
< |
* |
21 |
< |
* 2. Redistributions in binary form must reproduce the above copyright |
22 |
< |
* notice, this list of conditions and the following disclaimer in |
23 |
< |
* the documentation and/or other materials provided with the |
24 |
< |
* distribution. |
25 |
< |
* |
26 |
< |
* 3. The end-user documentation included with the redistribution, |
27 |
< |
* if any, must include the following acknowledgment: |
28 |
< |
* "This product includes Radiance software |
29 |
< |
* (http://radsite.lbl.gov/) |
30 |
< |
* developed by the Lawrence Berkeley National Laboratory |
31 |
< |
* (http://www.lbl.gov/)." |
32 |
< |
* Alternately, this acknowledgment may appear in the software itself, |
33 |
< |
* if and wherever such third-party acknowledgments normally appear. |
34 |
< |
* |
35 |
< |
* 4. The names "Radiance," "Lawrence Berkeley National Laboratory" |
36 |
< |
* and "The Regents of the University of California" must |
37 |
< |
* not be used to endorse or promote products derived from this |
38 |
< |
* software without prior written permission. For written |
39 |
< |
* permission, please contact [email protected]. |
40 |
< |
* |
41 |
< |
* 5. Products derived from this software may not be called "Radiance", |
42 |
< |
* nor may "Radiance" appear in their name, without prior written |
43 |
< |
* permission of Lawrence Berkeley National Laboratory. |
44 |
< |
* |
45 |
< |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
46 |
< |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
47 |
< |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
48 |
< |
* DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR |
49 |
< |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
50 |
< |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
51 |
< |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
52 |
< |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
53 |
< |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
54 |
< |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
55 |
< |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
56 |
< |
* SUCH DAMAGE. |
57 |
< |
* ==================================================================== |
58 |
< |
* |
59 |
< |
* This software consists of voluntary contributions made by many |
60 |
< |
* individuals on behalf of Lawrence Berkeley National Laboratory. For more |
61 |
< |
* information on Lawrence Berkeley National Laboratory, please see |
62 |
< |
* <http://www.lbl.gov/>. |
63 |
< |
*/ |
8 |
> |
#include "copyright.h" |
9 |
|
|
10 |
|
#include "ray.h" |
11 |
< |
|
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 |
|
|
18 |
|
/* |
38 |
|
* Arguments for MAT_TFUNC are: |
39 |
|
* 2+ func funcfile transform |
40 |
|
* 0 |
41 |
< |
* 4+ red grn blu rspec trans tspec A7 .. |
41 |
> |
* 6+ red grn blu rspec trans tspec A7 .. |
42 |
|
* |
43 |
|
* Arguments for MAT_TDATA are: |
44 |
|
* 4+ func datafile funcfile v0 .. transform |
45 |
|
* 0 |
46 |
< |
* 4+ red grn blu rspec trans tspec A7 .. |
46 |
> |
* 6+ red grn blu rspec trans tspec A7 .. |
47 |
|
* |
48 |
|
* Arguments for the more general MAT_BRTDF are: |
49 |
|
* 10+ rrefl grefl brefl |
77 |
|
} BRDFDAT; /* BRDF material data */ |
78 |
|
|
79 |
|
|
80 |
+ |
static int setbrdfunc(BRDFDAT *np); |
81 |
+ |
|
82 |
+ |
|
83 |
|
static void |
84 |
< |
dirbrdf(cval, np, ldir, omega) /* compute source contribution */ |
85 |
< |
COLOR cval; /* returned coefficient */ |
86 |
< |
register BRDFDAT *np; /* material data */ |
87 |
< |
FVECT ldir; /* light source direction */ |
88 |
< |
double omega; /* light source size */ |
84 |
> |
dirbrdf( /* compute source contribution */ |
85 |
> |
COLOR cval, /* returned coefficient */ |
86 |
> |
void *nnp, /* material data */ |
87 |
> |
FVECT ldir, /* light source direction */ |
88 |
> |
double omega /* light source size */ |
89 |
> |
) |
90 |
|
{ |
91 |
+ |
BRDFDAT *np = nnp; |
92 |
|
double ldot; |
93 |
|
double dtmp; |
94 |
|
COLOR ctmp; |
95 |
|
FVECT ldx; |
96 |
|
static double vldx[5], pt[MAXDIM]; |
97 |
< |
register char **sa; |
98 |
< |
register int i; |
97 |
> |
char **sa; |
98 |
> |
int i; |
99 |
|
#define lddx (vldx+1) |
100 |
|
|
101 |
|
setcolor(cval, 0.0, 0.0, 0.0); |
128 |
|
addcolor(cval, ctmp); |
129 |
|
} |
130 |
|
if (ldot > 0.0 ? np->rspec <= FTINY : np->tspec <= FTINY) |
131 |
< |
return; /* no specular component */ |
131 |
> |
return; /* diffuse only */ |
132 |
|
/* set up function */ |
133 |
|
setbrdfunc(np); |
134 |
|
sa = np->mp->oargs.sarg; |
140 |
|
lddx[3] = omega; |
141 |
|
/* compute BRTDF */ |
142 |
|
if (np->mp->otype == MAT_BRTDF) { |
143 |
< |
if (sa[6][0] == '0') /* special case */ |
143 |
> |
if (sa[6][0] == '0' && !sa[6][1]) /* special case */ |
144 |
|
colval(ctmp,RED) = 0.0; |
145 |
|
else |
146 |
|
colval(ctmp,RED) = funvalue(sa[6], 4, lddx); |
147 |
< |
if (!strcmp(sa[7],sa[6])) |
147 |
> |
if (sa[7][0] == '0' && !sa[7][1]) |
148 |
> |
colval(ctmp,GRN) = 0.0; |
149 |
> |
else if (!strcmp(sa[7],sa[6])) |
150 |
|
colval(ctmp,GRN) = colval(ctmp,RED); |
151 |
|
else |
152 |
|
colval(ctmp,GRN) = funvalue(sa[7], 4, lddx); |
153 |
< |
if (!strcmp(sa[8],sa[6])) |
153 |
> |
if (sa[8][0] == '0' && !sa[8][1]) |
154 |
> |
colval(ctmp,BLU) = 0.0; |
155 |
> |
else if (!strcmp(sa[8],sa[6])) |
156 |
|
colval(ctmp,BLU) = colval(ctmp,RED); |
157 |
|
else if (!strcmp(sa[8],sa[7])) |
158 |
|
colval(ctmp,BLU) = colval(ctmp,GRN); |
169 |
|
dtmp = funvalue(sa[0], 5, vldx); |
170 |
|
setcolor(ctmp, dtmp, dtmp, dtmp); |
171 |
|
} |
172 |
< |
if (errno) { |
172 |
> |
if ((errno == EDOM) | (errno == ERANGE)) { |
173 |
|
objerror(np->mp, WARNING, "compute error"); |
174 |
|
return; |
175 |
|
} |
179 |
|
/* |
180 |
|
* Compute reflected non-diffuse component. |
181 |
|
*/ |
182 |
< |
if (np->mp->otype == MAT_MFUNC | np->mp->otype == MAT_MDATA) |
182 |
> |
if ((np->mp->otype == MAT_MFUNC) | (np->mp->otype == MAT_MDATA)) |
183 |
|
multcolor(ctmp, np->mcolor); |
184 |
|
dtmp = ldot * omega * np->rspec; |
185 |
|
scalecolor(ctmp, dtmp); |
188 |
|
/* |
189 |
|
* Compute transmitted non-diffuse component. |
190 |
|
*/ |
191 |
< |
if (np->mp->otype == MAT_TFUNC | np->mp->otype == MAT_TDATA) |
191 |
> |
if ((np->mp->otype == MAT_TFUNC) | (np->mp->otype == MAT_TDATA)) |
192 |
|
multcolor(ctmp, np->mcolor); |
193 |
|
dtmp = -ldot * omega * np->tspec; |
194 |
|
scalecolor(ctmp, dtmp); |
199 |
|
|
200 |
|
|
201 |
|
int |
202 |
< |
m_brdf(m, r) /* color a ray that hit a BRDTfunc material */ |
203 |
< |
register OBJREC *m; |
204 |
< |
register RAY *r; |
202 |
> |
m_brdf( /* color a ray that hit a BRDTfunc material */ |
203 |
> |
OBJREC *m, |
204 |
> |
RAY *r |
205 |
> |
) |
206 |
|
{ |
207 |
|
int hitfront = 1; |
208 |
|
BRDFDAT nd; |
209 |
|
RAY sr; |
210 |
< |
double transtest, transdist; |
210 |
> |
double mirtest=0, mirdist=0; |
211 |
> |
double transtest=0, transdist=0; |
212 |
|
int hasrefl, hastrans; |
213 |
+ |
int hastexture; |
214 |
|
COLOR ctmp; |
215 |
|
FVECT vtmp; |
216 |
< |
register MFUNC *mf; |
217 |
< |
register int i; |
216 |
> |
double d; |
217 |
> |
MFUNC *mf; |
218 |
> |
int i; |
219 |
|
/* check arguments */ |
220 |
< |
if (m->oargs.nsargs < 10 | m->oargs.nfargs < 9) |
220 |
> |
if ((m->oargs.nsargs < 10) | (m->oargs.nfargs < 9)) |
221 |
|
objerror(m, USER, "bad # arguments"); |
222 |
|
nd.mp = m; |
223 |
|
nd.pr = r; |
237 |
|
setcolor(nd.tdiff, m->oargs.farg[6], |
238 |
|
m->oargs.farg[7], |
239 |
|
m->oargs.farg[8]); |
240 |
< |
/* get modifiers */ |
240 |
> |
/* get modifiers */ |
241 |
|
raytexture(r, m->omod); |
242 |
< |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
242 |
> |
hastexture = DOT(r->pert,r->pert) > FTINY*FTINY; |
243 |
> |
if (hastexture) { /* perturb normal */ |
244 |
> |
nd.pdot = raynormal(nd.pnorm, r); |
245 |
> |
} else { |
246 |
> |
VCOPY(nd.pnorm, r->ron); |
247 |
> |
nd.pdot = r->rod; |
248 |
> |
} |
249 |
|
if (r->rod < 0.0) { /* orient perturbed values */ |
250 |
|
nd.pdot = -nd.pdot; |
251 |
|
for (i = 0; i < 3; i++) { |
264 |
|
mf = getfunc(m, 9, 0x3f, 0); |
265 |
|
/* compute transmitted ray */ |
266 |
|
setbrdfunc(&nd); |
303 |
– |
transtest = 0; |
304 |
– |
transdist = r->rot; |
267 |
|
errno = 0; |
268 |
|
setcolor(ctmp, evalue(mf->ep[3]), |
269 |
|
evalue(mf->ep[4]), |
270 |
|
evalue(mf->ep[5])); |
271 |
< |
if (errno) |
271 |
> |
if ((errno == EDOM) | (errno == ERANGE)) |
272 |
|
objerror(m, WARNING, "compute error"); |
273 |
< |
else if (rayorigin(&sr, r, TRANS, bright(ctmp)) == 0) { |
274 |
< |
if (!(r->crtype & SHADOW) && |
275 |
< |
DOT(r->pert,r->pert) > FTINY*FTINY) { |
276 |
< |
for (i = 0; i < 3; i++) /* perturb direction */ |
315 |
< |
sr.rdir[i] = r->rdir[i] - .75*r->pert[i]; |
273 |
> |
else if (rayorigin(&sr, TRANS, r, ctmp) == 0) { |
274 |
> |
if (!(r->crtype & SHADOW) && hastexture) { |
275 |
> |
/* perturb direction */ |
276 |
> |
VSUM(sr.rdir, r->rdir, r->pert, -.75); |
277 |
|
if (normalize(sr.rdir) == 0.0) { |
278 |
|
objerror(m, WARNING, "illegal perturbation"); |
279 |
|
VCOPY(sr.rdir, r->rdir); |
280 |
|
} |
281 |
|
} else { |
282 |
|
VCOPY(sr.rdir, r->rdir); |
322 |
– |
transtest = 2; |
283 |
|
} |
284 |
|
rayvalue(&sr); |
285 |
< |
multcolor(sr.rcol, ctmp); |
285 |
> |
multcolor(sr.rcol, sr.rcoef); |
286 |
|
addcolor(r->rcol, sr.rcol); |
287 |
< |
transtest *= bright(sr.rcol); |
288 |
< |
transdist = r->rot + sr.rt; |
287 |
> |
if (!hastexture) { |
288 |
> |
transtest = 2.0*bright(sr.rcol); |
289 |
> |
transdist = r->rot + sr.rt; |
290 |
> |
} |
291 |
|
} |
292 |
|
if (r->crtype & SHADOW) /* the rest is shadow */ |
293 |
|
return(1); |
297 |
|
setcolor(ctmp, evalue(mf->ep[0]), |
298 |
|
evalue(mf->ep[1]), |
299 |
|
evalue(mf->ep[2])); |
300 |
< |
if (errno) |
300 |
> |
if ((errno == EDOM) | (errno == ERANGE)) |
301 |
|
objerror(m, WARNING, "compute error"); |
302 |
< |
else if (rayorigin(&sr, r, REFLECTED, bright(ctmp)) == 0) { |
303 |
< |
for (i = 0; i < 3; i++) |
304 |
< |
sr.rdir[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i]; |
302 |
> |
else if (rayorigin(&sr, REFLECTED, r, ctmp) == 0) { |
303 |
> |
VSUM(sr.rdir, r->rdir, nd.pnorm, 2.*nd.pdot); |
304 |
> |
checknorm(sr.rdir); |
305 |
|
rayvalue(&sr); |
306 |
< |
multcolor(sr.rcol, ctmp); |
306 |
> |
multcolor(sr.rcol, sr.rcoef); |
307 |
|
addcolor(r->rcol, sr.rcol); |
308 |
+ |
if (!hastexture && r->ro != NULL && isflat(r->ro->otype)) { |
309 |
+ |
mirtest = 2.0*bright(sr.rcol); |
310 |
+ |
mirdist = r->rot + sr.rt; |
311 |
+ |
} |
312 |
|
} |
313 |
|
/* compute ambient */ |
314 |
|
if (hasrefl) { |
315 |
|
if (!hitfront) |
316 |
|
flipsurface(r); |
317 |
< |
ambient(ctmp, r, nd.pnorm); |
318 |
< |
multcolor(ctmp, nd.rdiff); |
317 |
> |
copycolor(ctmp, nd.rdiff); |
318 |
> |
multambient(ctmp, r, nd.pnorm); |
319 |
|
addcolor(r->rcol, ctmp); /* add to returned color */ |
320 |
|
if (!hitfront) |
321 |
|
flipsurface(r); |
326 |
|
vtmp[0] = -nd.pnorm[0]; |
327 |
|
vtmp[1] = -nd.pnorm[1]; |
328 |
|
vtmp[2] = -nd.pnorm[2]; |
329 |
< |
ambient(ctmp, r, vtmp); |
330 |
< |
multcolor(ctmp, nd.tdiff); |
329 |
> |
copycolor(ctmp, nd.tdiff); |
330 |
> |
multambient(ctmp, r, vtmp); |
331 |
|
addcolor(r->rcol, ctmp); |
332 |
|
if (hitfront) |
333 |
|
flipsurface(r); |
334 |
|
} |
335 |
|
if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0') |
336 |
|
direct(r, dirbrdf, &nd); /* add direct component */ |
337 |
< |
/* check distance */ |
338 |
< |
if (transtest > bright(r->rcol)) |
337 |
> |
|
338 |
> |
d = bright(r->rcol); /* set effective distance */ |
339 |
> |
if (transtest > d) |
340 |
|
r->rt = transdist; |
341 |
+ |
else if (mirtest > d) |
342 |
+ |
r->rt = mirdist; |
343 |
|
|
344 |
|
return(1); |
345 |
|
} |
347 |
|
|
348 |
|
|
349 |
|
int |
350 |
< |
m_brdf2(m, r) /* color a ray that hit a BRDF material */ |
351 |
< |
register OBJREC *m; |
352 |
< |
register RAY *r; |
350 |
> |
m_brdf2( /* color a ray that hit a BRDF material */ |
351 |
> |
OBJREC *m, |
352 |
> |
RAY *r |
353 |
> |
) |
354 |
|
{ |
355 |
|
BRDFDAT nd; |
356 |
|
COLOR ctmp; |
360 |
|
if (r->crtype & SHADOW) |
361 |
|
return(1); |
362 |
|
/* check arguments */ |
363 |
< |
if (m->oargs.nsargs < (hasdata(m->otype)?4:2) | m->oargs.nfargs < |
364 |
< |
(m->otype==MAT_TFUNC|m->otype==MAT_TDATA?6:4)) |
363 |
> |
if ((m->oargs.nsargs < (hasdata(m->otype)?4:2)) | (m->oargs.nfargs < |
364 |
> |
((m->otype==MAT_TFUNC)|(m->otype==MAT_TDATA)?6:4))) |
365 |
|
objerror(m, USER, "bad # arguments"); |
366 |
+ |
/* check for back side */ |
367 |
+ |
if (r->rod < 0.0) { |
368 |
+ |
if (!backvis) { |
369 |
+ |
raytrans(r); |
370 |
+ |
return(1); |
371 |
+ |
} |
372 |
+ |
raytexture(r, m->omod); |
373 |
+ |
flipsurface(r); /* reorient if backvis */ |
374 |
+ |
} else |
375 |
+ |
raytexture(r, m->omod); |
376 |
+ |
|
377 |
|
nd.mp = m; |
378 |
|
nd.pr = r; |
379 |
|
/* get material color */ |
383 |
|
/* get specular component */ |
384 |
|
nd.rspec = m->oargs.farg[3]; |
385 |
|
/* compute transmittance */ |
386 |
< |
if (m->otype == MAT_TFUNC | m->otype == MAT_TDATA) { |
386 |
> |
if ((m->otype == MAT_TFUNC) | (m->otype == MAT_TDATA)) { |
387 |
|
nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec); |
388 |
|
nd.tspec = nd.trans * m->oargs.farg[5]; |
389 |
|
dtmp = nd.trans - nd.tspec; |
395 |
|
/* compute reflectance */ |
396 |
|
dtmp = 1.0 - nd.trans - nd.rspec; |
397 |
|
setcolor(nd.rdiff, dtmp, dtmp, dtmp); |
417 |
– |
/* check for back side */ |
418 |
– |
if (r->rod < 0.0) { |
419 |
– |
if (!backvis && m->otype != MAT_TFUNC |
420 |
– |
&& m->otype != MAT_TDATA) { |
421 |
– |
raytrans(r); |
422 |
– |
return(1); |
423 |
– |
} |
424 |
– |
flipsurface(r); /* reorient if backvis */ |
425 |
– |
} |
426 |
– |
/* get modifiers */ |
427 |
– |
raytexture(r, m->omod); |
398 |
|
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
399 |
|
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
400 |
|
multcolor(nd.rdiff, nd.mcolor); |
409 |
|
} |
410 |
|
/* compute ambient */ |
411 |
|
if (nd.trans < 1.0-FTINY) { |
412 |
< |
ambient(ctmp, r, nd.pnorm); |
412 |
> |
copycolor(ctmp, nd.mcolor); /* modified by material color */ |
413 |
|
scalecolor(ctmp, 1.0-nd.trans); |
414 |
< |
multcolor(ctmp, nd.mcolor); /* modified by material color */ |
414 |
> |
multambient(ctmp, r, nd.pnorm); |
415 |
|
addcolor(r->rcol, ctmp); /* add to returned color */ |
416 |
|
} |
417 |
|
if (nd.trans > FTINY) { /* from other side */ |
419 |
|
vtmp[0] = -nd.pnorm[0]; |
420 |
|
vtmp[1] = -nd.pnorm[1]; |
421 |
|
vtmp[2] = -nd.pnorm[2]; |
422 |
< |
ambient(ctmp, r, vtmp); |
422 |
> |
copycolor(ctmp, nd.mcolor); |
423 |
|
scalecolor(ctmp, nd.trans); |
424 |
< |
multcolor(ctmp, nd.mcolor); |
424 |
> |
multambient(ctmp, r, vtmp); |
425 |
|
addcolor(r->rcol, ctmp); |
426 |
|
flipsurface(r); |
427 |
|
} |
432 |
|
} |
433 |
|
|
434 |
|
|
435 |
< |
int |
436 |
< |
setbrdfunc(np) /* set up brdf function and variables */ |
437 |
< |
register BRDFDAT *np; |
435 |
> |
static int |
436 |
> |
setbrdfunc( /* set up brdf function and variables */ |
437 |
> |
BRDFDAT *np |
438 |
> |
) |
439 |
|
{ |
440 |
|
FVECT vec; |
441 |
|
|