1 |
– |
/* Copyright (c) 1990 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 |
+ |
#include "rtotypes.h" |
16 |
+ |
#include "func.h" |
17 |
+ |
#include "pmapmat.h" |
18 |
|
|
19 |
|
/* |
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 |
< |
* 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 |
|
* |
35 |
|
* 4+ func datafile funcfile v0 .. transform |
36 |
|
* 0 |
37 |
|
* 4+ red grn blu specularity A5 .. |
38 |
+ |
* |
39 |
+ |
* Arguments for MAT_TFUNC are: |
40 |
+ |
* 2+ func funcfile transform |
41 |
+ |
* 0 |
42 |
+ |
* 6+ red grn blu rspec trans tspec A7 .. |
43 |
+ |
* |
44 |
+ |
* Arguments for MAT_TDATA are: |
45 |
+ |
* 4+ func datafile funcfile v0 .. transform |
46 |
+ |
* 0 |
47 |
+ |
* 6+ 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 |
+ |
* 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 (or pattern) |
64 |
|
*/ |
65 |
|
|
37 |
– |
extern double funvalue(), varvalue(); |
38 |
– |
|
66 |
|
typedef struct { |
67 |
|
OBJREC *mp; /* material pointer */ |
68 |
|
RAY *pr; /* intersected ray */ |
69 |
< |
DATARRAY *dp; /* data array for PDATA or MDATA */ |
70 |
< |
COLOR mcolor; /* color of this material */ |
71 |
< |
COLOR scolor; /* color of specular component */ |
72 |
< |
double rspec; /* specular reflection */ |
73 |
< |
double rdiff; /* diffuse reflection */ |
69 |
> |
DATARRAY *dp; /* data array for PDATA, MDATA or TDATA */ |
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 */ |
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 */ |
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 |
< |
extern XF funcxf; |
92 |
> |
BRDFDAT *np = nnp; |
93 |
|
double ldot; |
94 |
|
double dtmp; |
95 |
|
COLOR ctmp; |
96 |
|
FVECT ldx; |
97 |
< |
double pt[MAXDIM]; |
98 |
< |
register int i; |
97 |
> |
static double vldx[5], pt[MAXDIM]; |
98 |
> |
char **sa; |
99 |
> |
int i; |
100 |
> |
#define lddx (vldx+1) |
101 |
|
|
102 |
|
setcolor(cval, 0.0, 0.0, 0.0); |
103 |
|
|
104 |
|
ldot = DOT(np->pnorm, ldir); |
105 |
|
|
106 |
< |
if (ldot < 0.0) |
106 |
> |
if (ldot <= FTINY && ldot >= -FTINY) |
107 |
> |
return; /* too close to grazing */ |
108 |
> |
|
109 |
> |
if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY) |
110 |
|
return; /* wrong side */ |
111 |
|
|
112 |
< |
if (np->rdiff > FTINY) { |
112 |
> |
if (ldot > 0.0) { |
113 |
|
/* |
114 |
|
* Compute and add diffuse reflected component to returned |
115 |
|
* color. The diffuse reflected component will always be |
116 |
|
* modified by the color of the material. |
117 |
|
*/ |
118 |
< |
copycolor(ctmp, np->mcolor); |
119 |
< |
dtmp = ldot * omega * np->rdiff / PI; |
118 |
> |
copycolor(ctmp, np->rdiff); |
119 |
> |
dtmp = ldot * omega / PI; |
120 |
|
scalecolor(ctmp, dtmp); |
121 |
|
addcolor(cval, ctmp); |
122 |
+ |
} else { |
123 |
+ |
/* |
124 |
+ |
* Diffuse transmitted component. |
125 |
+ |
*/ |
126 |
+ |
copycolor(ctmp, np->tdiff); |
127 |
+ |
dtmp = -ldot * omega / PI; |
128 |
+ |
scalecolor(ctmp, dtmp); |
129 |
+ |
addcolor(cval, ctmp); |
130 |
|
} |
131 |
< |
if (np->rspec > FTINY) { |
131 |
> |
if ((ldot > 0.0 ? np->rspec <= FTINY : np->tspec <= FTINY) || |
132 |
> |
ambRayInPmap(np->pr)) |
133 |
> |
return; /* diffuse only */ |
134 |
> |
/* set up function */ |
135 |
> |
setbrdfunc(np); |
136 |
> |
sa = np->mp->oargs.sarg; |
137 |
> |
errno = 0; |
138 |
> |
/* transform light vector */ |
139 |
> |
multv3(ldx, ldir, funcxf.xfm); |
140 |
> |
for (i = 0; i < 3; i++) |
141 |
> |
lddx[i] = ldx[i]/funcxf.sca; |
142 |
> |
lddx[3] = omega; |
143 |
> |
/* compute BRTDF */ |
144 |
> |
if (np->mp->otype == MAT_BRTDF) { |
145 |
> |
if (sa[6][0] == '0' && !sa[6][1]) /* special case */ |
146 |
> |
colval(ctmp,RED) = 0.0; |
147 |
> |
else |
148 |
> |
colval(ctmp,RED) = funvalue(sa[6], 4, lddx); |
149 |
> |
if (sa[7][0] == '0' && !sa[7][1]) |
150 |
> |
colval(ctmp,GRN) = 0.0; |
151 |
> |
else if (!strcmp(sa[7],sa[6])) |
152 |
> |
colval(ctmp,GRN) = colval(ctmp,RED); |
153 |
> |
else |
154 |
> |
colval(ctmp,GRN) = funvalue(sa[7], 4, lddx); |
155 |
> |
if (sa[8][0] == '0' && !sa[8][1]) |
156 |
> |
colval(ctmp,BLU) = 0.0; |
157 |
> |
else if (!strcmp(sa[8],sa[6])) |
158 |
> |
colval(ctmp,BLU) = colval(ctmp,RED); |
159 |
> |
else if (!strcmp(sa[8],sa[7])) |
160 |
> |
colval(ctmp,BLU) = colval(ctmp,GRN); |
161 |
> |
else |
162 |
> |
colval(ctmp,BLU) = funvalue(sa[8], 4, lddx); |
163 |
> |
dtmp = bright(ctmp); |
164 |
> |
} else if (np->dp == NULL) { |
165 |
> |
dtmp = funvalue(sa[0], 4, lddx); |
166 |
> |
setcolor(ctmp, dtmp, dtmp, dtmp); |
167 |
> |
} else { |
168 |
> |
for (i = 0; i < np->dp->nd; i++) |
169 |
> |
pt[i] = funvalue(sa[3+i], 4, lddx); |
170 |
> |
vldx[0] = datavalue(np->dp, pt); |
171 |
> |
dtmp = funvalue(sa[0], 5, vldx); |
172 |
> |
setcolor(ctmp, dtmp, dtmp, dtmp); |
173 |
> |
} |
174 |
> |
if ((errno == EDOM) | (errno == ERANGE)) { |
175 |
> |
objerror(np->mp, WARNING, "compute error"); |
176 |
> |
return; |
177 |
> |
} |
178 |
> |
if (dtmp <= FTINY) |
179 |
> |
return; |
180 |
> |
if (ldot > 0.0) { |
181 |
|
/* |
182 |
< |
* Compute specular component. |
182 |
> |
* Compute reflected non-diffuse component. |
183 |
|
*/ |
184 |
< |
setfunc(np->mp, np->pr); |
185 |
< |
/* transform light vector */ |
186 |
< |
multv3(ldx, ldir, funcxf.xfm); |
187 |
< |
for (i = 0; i < 3; i++) |
188 |
< |
ldx[i] /= funcxf.sca; |
189 |
< |
/* evaluate BRDF */ |
190 |
< |
errno = 0; |
191 |
< |
if (np->dp == NULL) |
192 |
< |
dtmp = funvalue(np->mp->oargs.sarg[0], 3, ldx); |
193 |
< |
else { |
194 |
< |
for (i = 0; i < np->dp->nd; i++) |
195 |
< |
pt[i] = funvalue(np->mp->oargs.sarg[3+i], |
196 |
< |
3, ldx); |
197 |
< |
dtmp = datavalue(np->dp, pt); |
102 |
< |
dtmp = funvalue(np->mp->oargs.sarg[0], 1, &dtmp); |
103 |
< |
} |
104 |
< |
if (errno) |
105 |
< |
goto computerr; |
106 |
< |
if (dtmp > FTINY) { |
107 |
< |
copycolor(ctmp, np->scolor); |
108 |
< |
dtmp *= ldot * omega; |
109 |
< |
scalecolor(ctmp, dtmp); |
110 |
< |
addcolor(cval, ctmp); |
111 |
< |
} |
184 |
> |
if ((np->mp->otype == MAT_MFUNC) | (np->mp->otype == MAT_MDATA)) |
185 |
> |
multcolor(ctmp, np->mcolor); |
186 |
> |
dtmp = ldot * omega * np->rspec; |
187 |
> |
scalecolor(ctmp, dtmp); |
188 |
> |
addcolor(cval, ctmp); |
189 |
> |
} else { |
190 |
> |
/* |
191 |
> |
* Compute transmitted non-diffuse component. |
192 |
> |
*/ |
193 |
> |
if ((np->mp->otype == MAT_TFUNC) | (np->mp->otype == MAT_TDATA)) |
194 |
> |
multcolor(ctmp, np->mcolor); |
195 |
> |
dtmp = -ldot * omega * np->tspec; |
196 |
> |
scalecolor(ctmp, dtmp); |
197 |
> |
addcolor(cval, ctmp); |
198 |
|
} |
199 |
< |
return; |
114 |
< |
computerr: |
115 |
< |
objerror(np->mp, WARNING, "compute error"); |
116 |
< |
return; |
199 |
> |
#undef lddx |
200 |
|
} |
201 |
|
|
202 |
|
|
203 |
< |
m_brdf(m, r) /* color a ray which hit a BRDF material */ |
204 |
< |
register OBJREC *m; |
205 |
< |
register RAY *r; |
203 |
> |
int |
204 |
> |
m_brdf( /* color a ray that hit a BRDTfunc material */ |
205 |
> |
OBJREC *m, |
206 |
> |
RAY *r |
207 |
> |
) |
208 |
|
{ |
209 |
+ |
int hitfront = 1; |
210 |
|
BRDFDAT nd; |
211 |
+ |
RAY sr; |
212 |
+ |
int hasrefl, hastrans; |
213 |
+ |
int hastexture; |
214 |
|
COLOR ctmp; |
215 |
< |
register int i; |
216 |
< |
|
217 |
< |
if (m->oargs.nsargs < 2 || m->oargs.nfargs < 4) |
215 |
> |
FVECT vtmp; |
216 |
> |
double d; |
217 |
> |
MFUNC *mf; |
218 |
> |
int i; |
219 |
> |
/* check arguments */ |
220 |
> |
if ((m->oargs.nsargs < 10) | (m->oargs.nfargs < 9)) |
221 |
|
objerror(m, USER, "bad # arguments"); |
130 |
– |
/* easy shadow test */ |
131 |
– |
if (r->crtype & SHADOW) |
132 |
– |
return; |
222 |
|
nd.mp = m; |
223 |
|
nd.pr = r; |
224 |
< |
/* load auxiliary files */ |
225 |
< |
if (m->otype == MAT_PDATA || m->otype == MAT_MDATA) { |
226 |
< |
nd.dp = getdata(m->oargs.sarg[1]); |
227 |
< |
for (i = 3; i < m->oargs.nsargs; i++) |
228 |
< |
if (m->oargs.sarg[i][0] == '-') |
229 |
< |
break; |
230 |
< |
if (i-3 != nd.dp->nd) |
231 |
< |
objerror(m, USER, "dimension error"); |
232 |
< |
if (!fundefined(m->oargs.sarg[3])) |
233 |
< |
loadfunc(m->oargs.sarg[2]); |
224 |
> |
/* dummy values */ |
225 |
> |
nd.rspec = nd.tspec = 1.0; |
226 |
> |
nd.trans = 0.5; |
227 |
> |
/* diffuse reflectance */ |
228 |
> |
if (r->rod > 0.0) |
229 |
> |
setcolor(nd.rdiff, m->oargs.farg[0], |
230 |
> |
m->oargs.farg[1], |
231 |
> |
m->oargs.farg[2]); |
232 |
> |
else |
233 |
> |
setcolor(nd.rdiff, m->oargs.farg[3], |
234 |
> |
m->oargs.farg[4], |
235 |
> |
m->oargs.farg[5]); |
236 |
> |
/* diffuse transmittance */ |
237 |
> |
setcolor(nd.tdiff, m->oargs.farg[6], |
238 |
> |
m->oargs.farg[7], |
239 |
> |
m->oargs.farg[8]); |
240 |
> |
/* get modifiers */ |
241 |
> |
raytexture(r, m->omod); |
242 |
> |
hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY); |
243 |
> |
if (hastexture) { /* perturb normal */ |
244 |
> |
nd.pdot = raynormal(nd.pnorm, r); |
245 |
|
} else { |
246 |
< |
nd.dp = NULL; |
247 |
< |
if (!fundefined(m->oargs.sarg[0])) |
148 |
< |
loadfunc(m->oargs.sarg[1]); |
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++) { |
252 |
+ |
nd.pnorm[i] = -nd.pnorm[i]; |
253 |
+ |
r->pert[i] = -r->pert[i]; |
254 |
+ |
} |
255 |
+ |
hitfront = 0; |
256 |
+ |
} |
257 |
+ |
copycolor(nd.mcolor, r->pcol); /* get pattern color */ |
258 |
+ |
multcolor(nd.rdiff, nd.mcolor); /* modify diffuse values */ |
259 |
+ |
multcolor(nd.tdiff, nd.mcolor); |
260 |
+ |
hasrefl = (bright(nd.rdiff) > FTINY); |
261 |
+ |
hastrans = (bright(nd.tdiff) > FTINY); |
262 |
+ |
/* load cal file */ |
263 |
+ |
nd.dp = NULL; |
264 |
+ |
mf = getfunc(m, 9, 0x3f, 0); |
265 |
+ |
/* compute transmitted ray */ |
266 |
+ |
setbrdfunc(&nd); |
267 |
+ |
errno = 0; |
268 |
+ |
setcolor(ctmp, evalue(mf->ep[3]), |
269 |
+ |
evalue(mf->ep[4]), |
270 |
+ |
evalue(mf->ep[5])); |
271 |
+ |
if ((errno == EDOM) | (errno == ERANGE)) |
272 |
+ |
objerror(m, WARNING, "compute error"); |
273 |
+ |
else if (rayorigin(&sr, TRANS, r, ctmp) == 0) { |
274 |
+ |
if (hastexture && !(r->crtype & (SHADOW|AMBIENT))) { |
275 |
+ |
/* perturb direction */ |
276 |
+ |
VSUB(sr.rdir, r->rdir, r->pert); |
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); |
283 |
+ |
} |
284 |
+ |
rayvalue(&sr); |
285 |
+ |
multcolor(sr.rcol, sr.rcoef); |
286 |
+ |
addcolor(r->rcol, sr.rcol); |
287 |
+ |
if (!hastexture || r->crtype & (SHADOW|AMBIENT)) |
288 |
+ |
r->rxt = r->rot + raydistance(&sr); |
289 |
+ |
} |
290 |
+ |
if (r->crtype & SHADOW) /* the rest is shadow */ |
291 |
+ |
return(1); |
292 |
+ |
|
293 |
+ |
/* compute reflected ray */ |
294 |
+ |
setbrdfunc(&nd); |
295 |
+ |
errno = 0; |
296 |
+ |
setcolor(ctmp, evalue(mf->ep[0]), |
297 |
+ |
evalue(mf->ep[1]), |
298 |
+ |
evalue(mf->ep[2])); |
299 |
+ |
if ((errno == EDOM) | (errno == ERANGE)) |
300 |
+ |
objerror(m, WARNING, "compute error"); |
301 |
+ |
else if (rayorigin(&sr, REFLECTED, r, ctmp) == 0) { |
302 |
+ |
VSUM(sr.rdir, r->rdir, nd.pnorm, 2.*nd.pdot); |
303 |
+ |
checknorm(sr.rdir); |
304 |
+ |
rayvalue(&sr); |
305 |
+ |
multcolor(sr.rcol, sr.rcoef); |
306 |
+ |
copycolor(r->mcol, sr.rcol); |
307 |
+ |
addcolor(r->rcol, sr.rcol); |
308 |
+ |
if (r->ro != NULL && isflat(r->ro->otype) && |
309 |
+ |
!hastexture | (r->crtype & AMBIENT)) |
310 |
+ |
r->rmt = r->rot + raydistance(&sr); |
311 |
+ |
} |
312 |
+ |
/* compute ambient */ |
313 |
+ |
if (hasrefl) { |
314 |
+ |
if (!hitfront) |
315 |
+ |
flipsurface(r); |
316 |
+ |
copycolor(ctmp, nd.rdiff); |
317 |
+ |
multambient(ctmp, r, nd.pnorm); |
318 |
+ |
addcolor(r->rcol, ctmp); /* add to returned color */ |
319 |
+ |
if (!hitfront) |
320 |
+ |
flipsurface(r); |
321 |
+ |
} |
322 |
+ |
if (hastrans) { /* from other side */ |
323 |
+ |
if (hitfront) |
324 |
+ |
flipsurface(r); |
325 |
+ |
vtmp[0] = -nd.pnorm[0]; |
326 |
+ |
vtmp[1] = -nd.pnorm[1]; |
327 |
+ |
vtmp[2] = -nd.pnorm[2]; |
328 |
+ |
copycolor(ctmp, nd.tdiff); |
329 |
+ |
multambient(ctmp, r, vtmp); |
330 |
+ |
addcolor(r->rcol, ctmp); |
331 |
+ |
if (hitfront) |
332 |
+ |
flipsurface(r); |
333 |
+ |
} |
334 |
+ |
if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0') |
335 |
+ |
direct(r, dirbrdf, &nd); /* add direct component */ |
336 |
+ |
|
337 |
+ |
return(1); |
338 |
+ |
} |
339 |
+ |
|
340 |
+ |
|
341 |
+ |
|
342 |
+ |
int |
343 |
+ |
m_brdf2( /* color a ray that hit a BRDF material */ |
344 |
+ |
OBJREC *m, |
345 |
+ |
RAY *r |
346 |
+ |
) |
347 |
+ |
{ |
348 |
+ |
BRDFDAT nd; |
349 |
+ |
COLOR ctmp; |
350 |
+ |
FVECT vtmp; |
351 |
+ |
double dtmp; |
352 |
+ |
/* always a shadow */ |
353 |
+ |
if (r->crtype & SHADOW) |
354 |
+ |
return(1); |
355 |
+ |
/* check arguments */ |
356 |
+ |
if ((m->oargs.nsargs < (hasdata(m->otype)?4:2)) | (m->oargs.nfargs < |
357 |
+ |
((m->otype==MAT_TFUNC)|(m->otype==MAT_TDATA)?6:4))) |
358 |
+ |
objerror(m, USER, "bad # arguments"); |
359 |
+ |
/* check for back side */ |
360 |
+ |
if (r->rod < 0.0) { |
361 |
+ |
if (!backvis) { |
362 |
+ |
raytrans(r); |
363 |
+ |
return(1); |
364 |
+ |
} |
365 |
+ |
raytexture(r, m->omod); |
366 |
+ |
flipsurface(r); /* reorient if backvis */ |
367 |
+ |
} else |
368 |
+ |
raytexture(r, m->omod); |
369 |
+ |
|
370 |
+ |
nd.mp = m; |
371 |
+ |
nd.pr = r; |
372 |
|
/* get material color */ |
373 |
|
setcolor(nd.mcolor, m->oargs.farg[0], |
374 |
< |
m->oargs.farg[1], |
375 |
< |
m->oargs.farg[2]); |
154 |
< |
/* get roughness */ |
155 |
< |
if (r->rod < 0.0) |
156 |
< |
flipsurface(r); |
157 |
< |
/* get modifiers */ |
158 |
< |
raytexture(r, m->omod); |
159 |
< |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
160 |
< |
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
161 |
< |
r->rt = r->rot; /* default ray length */ |
374 |
> |
m->oargs.farg[1], |
375 |
> |
m->oargs.farg[2]); |
376 |
|
/* get specular component */ |
377 |
|
nd.rspec = m->oargs.farg[3]; |
378 |
< |
|
379 |
< |
if (nd.rspec > FTINY) { /* has specular component */ |
380 |
< |
/* compute specular color */ |
381 |
< |
if (m->otype == MAT_MFUNC || m->otype == MAT_MDATA) |
382 |
< |
copycolor(nd.scolor, nd.mcolor); |
383 |
< |
else |
384 |
< |
setcolor(nd.scolor, 1.0, 1.0, 1.0); |
385 |
< |
scalecolor(nd.scolor, nd.rspec); |
378 |
> |
/* compute transmittance */ |
379 |
> |
if ((m->otype == MAT_TFUNC) | (m->otype == MAT_TDATA)) { |
380 |
> |
nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec); |
381 |
> |
nd.tspec = nd.trans * m->oargs.farg[5]; |
382 |
> |
dtmp = nd.trans - nd.tspec; |
383 |
> |
setcolor(nd.tdiff, dtmp, dtmp, dtmp); |
384 |
> |
} else { |
385 |
> |
nd.tspec = nd.trans = 0.0; |
386 |
> |
setcolor(nd.tdiff, 0.0, 0.0, 0.0); |
387 |
|
} |
388 |
< |
/* diffuse reflection */ |
389 |
< |
nd.rdiff = 1.0 - nd.rspec; |
388 |
> |
/* compute reflectance */ |
389 |
> |
dtmp = 1.0 - nd.trans - nd.rspec; |
390 |
> |
setcolor(nd.rdiff, dtmp, dtmp, dtmp); |
391 |
> |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
392 |
> |
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
393 |
> |
multcolor(nd.rdiff, nd.mcolor); |
394 |
> |
multcolor(nd.tdiff, nd.mcolor); |
395 |
> |
/* load auxiliary files */ |
396 |
> |
if (hasdata(m->otype)) { |
397 |
> |
nd.dp = getdata(m->oargs.sarg[1]); |
398 |
> |
getfunc(m, 2, 0, 0); |
399 |
> |
} else { |
400 |
> |
nd.dp = NULL; |
401 |
> |
getfunc(m, 1, 0, 0); |
402 |
> |
} |
403 |
|
/* compute ambient */ |
404 |
< |
if (nd.rdiff > FTINY) { |
405 |
< |
ambient(ctmp, r); |
406 |
< |
multcolor(ctmp, nd.mcolor); /* modified by material color */ |
404 |
> |
if (nd.trans < 1.0-FTINY) { |
405 |
> |
copycolor(ctmp, nd.mcolor); /* modified by material color */ |
406 |
> |
scalecolor(ctmp, 1.0-nd.trans); |
407 |
> |
multambient(ctmp, r, nd.pnorm); |
408 |
|
addcolor(r->rcol, ctmp); /* add to returned color */ |
409 |
|
} |
410 |
+ |
if (nd.trans > FTINY) { /* from other side */ |
411 |
+ |
flipsurface(r); |
412 |
+ |
vtmp[0] = -nd.pnorm[0]; |
413 |
+ |
vtmp[1] = -nd.pnorm[1]; |
414 |
+ |
vtmp[2] = -nd.pnorm[2]; |
415 |
+ |
copycolor(ctmp, nd.mcolor); |
416 |
+ |
scalecolor(ctmp, nd.trans); |
417 |
+ |
multambient(ctmp, r, vtmp); |
418 |
+ |
addcolor(r->rcol, ctmp); |
419 |
+ |
flipsurface(r); |
420 |
+ |
} |
421 |
|
/* add direct component */ |
422 |
|
direct(r, dirbrdf, &nd); |
423 |
+ |
|
424 |
+ |
return(1); |
425 |
+ |
} |
426 |
+ |
|
427 |
+ |
|
428 |
+ |
static int |
429 |
+ |
setbrdfunc( /* set up brdf function and variables */ |
430 |
+ |
BRDFDAT *np |
431 |
+ |
) |
432 |
+ |
{ |
433 |
+ |
FVECT vec; |
434 |
+ |
|
435 |
+ |
if (setfunc(np->mp, np->pr) == 0) |
436 |
+ |
return(0); /* it's OK, setfunc says we're done */ |
437 |
+ |
/* else (re)assign special variables */ |
438 |
+ |
multv3(vec, np->pnorm, funcxf.xfm); |
439 |
+ |
varset("NxP`", '=', vec[0]/funcxf.sca); |
440 |
+ |
varset("NyP`", '=', vec[1]/funcxf.sca); |
441 |
+ |
varset("NzP`", '=', vec[2]/funcxf.sca); |
442 |
+ |
varset("RdotP`", '=', np->pdot <= -1.0 ? -1.0 : |
443 |
+ |
np->pdot >= 1.0 ? 1.0 : np->pdot); |
444 |
+ |
varset("CrP", '=', colval(np->mcolor,RED)); |
445 |
+ |
varset("CgP", '=', colval(np->mcolor,GRN)); |
446 |
+ |
varset("CbP", '=', colval(np->mcolor,BLU)); |
447 |
+ |
return(1); |
448 |
|
} |