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