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 |
+ |
#include "rtotypes.h" |
16 |
+ |
#include "func.h" |
17 |
|
|
18 |
|
/* |
19 |
|
* Arguments to this material include the color and specularity. |
20 |
|
* String arguments include the reflection function and files. |
21 |
|
* The BRDF is currently used just for the specular component to light |
22 |
|
* sources. Reflectance values or data coordinates are functions |
23 |
< |
* of the direction to the light source. |
23 |
> |
* of the direction to the light source. (Data modification functions |
24 |
> |
* are passed the source direction as args 2-4.) |
25 |
|
* We orient the surface towards the incoming ray, so a single |
26 |
|
* surface can be used to represent an infinitely thin object. |
27 |
|
* |
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 |
51 |
|
* rbrtd gbrtd bbrtd |
52 |
|
* funcfile transform |
53 |
|
* 0 |
54 |
< |
* 6+ red grn blu rspec trans tspec A7 .. |
54 |
> |
* 9+ rdf gdf bdf |
55 |
> |
* rdb gdb bdb |
56 |
> |
* rdt gdt bdt A10 .. |
57 |
|
* |
58 |
|
* In addition to the normal variables available to functions, |
59 |
|
* we define the following: |
60 |
|
* NxP, NyP, NzP - perturbed surface normal |
61 |
|
* RdotP - perturbed ray dot product |
62 |
< |
* CrP, CgP, CbP - perturbed material color |
62 |
> |
* CrP, CgP, CbP - perturbed material color (or pattern) |
63 |
|
*/ |
64 |
|
|
61 |
– |
extern double funvalue(), varvalue(); |
62 |
– |
extern XF funcxf; |
63 |
– |
|
65 |
|
typedef struct { |
66 |
|
OBJREC *mp; /* material pointer */ |
67 |
|
RAY *pr; /* intersected ray */ |
68 |
|
DATARRAY *dp; /* data array for PDATA, MDATA or TDATA */ |
69 |
< |
COLOR mcolor; /* color of this material */ |
70 |
< |
double rspec; /* specular reflection */ |
71 |
< |
double rdiff; /* diffuse reflection */ |
72 |
< |
double trans; /* transmissivity */ |
73 |
< |
double tspec; /* specular transmission */ |
74 |
< |
double tdiff; /* diffuse transmission */ |
69 |
> |
COLOR mcolor; /* material (or pattern) color */ |
70 |
> |
COLOR rdiff; /* diffuse reflection */ |
71 |
> |
COLOR tdiff; /* diffuse transmission */ |
72 |
> |
double rspec; /* specular reflectance (1 for BRDTF) */ |
73 |
> |
double trans; /* transmissivity (.5 for BRDTF) */ |
74 |
> |
double tspec; /* specular transmittance (1 for BRDTF) */ |
75 |
|
FVECT pnorm; /* perturbed surface normal */ |
76 |
|
double pdot; /* perturbed dot product */ |
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 int setbrdfunc(BRDFDAT *np); |
81 |
> |
|
82 |
> |
|
83 |
> |
static void |
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 |
< |
double pt[MAXDIM]; |
97 |
< |
register char **sa; |
98 |
< |
register int i; |
96 |
> |
static double vldx[5], pt[MAXDIM]; |
97 |
> |
char **sa; |
98 |
> |
int i; |
99 |
> |
#define lddx (vldx+1) |
100 |
|
|
101 |
|
setcolor(cval, 0.0, 0.0, 0.0); |
102 |
|
|
104 |
|
|
105 |
|
if (ldot <= FTINY && ldot >= -FTINY) |
106 |
|
return; /* too close to grazing */ |
107 |
+ |
|
108 |
|
if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY) |
109 |
|
return; /* wrong side */ |
110 |
|
|
111 |
< |
if (ldot > 0.0 && np->rdiff > FTINY) { |
111 |
> |
if (ldot > 0.0) { |
112 |
|
/* |
113 |
|
* Compute and add diffuse reflected component to returned |
114 |
|
* color. The diffuse reflected component will always be |
115 |
|
* modified by the color of the material. |
116 |
|
*/ |
117 |
< |
copycolor(ctmp, np->mcolor); |
118 |
< |
dtmp = ldot * omega * np->rdiff / PI; |
117 |
> |
copycolor(ctmp, np->rdiff); |
118 |
> |
dtmp = ldot * omega / PI; |
119 |
|
scalecolor(ctmp, dtmp); |
120 |
|
addcolor(cval, ctmp); |
121 |
< |
} |
113 |
< |
if (ldot < 0.0 && np->tdiff > FTINY) { |
121 |
> |
} else { |
122 |
|
/* |
123 |
|
* Diffuse transmitted component. |
124 |
|
*/ |
125 |
< |
copycolor(ctmp, np->mcolor); |
126 |
< |
dtmp = -ldot * omega * np->tdiff / PI; |
125 |
> |
copycolor(ctmp, np->tdiff); |
126 |
> |
dtmp = -ldot * omega / PI; |
127 |
|
scalecolor(ctmp, dtmp); |
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; |
136 |
|
/* transform light vector */ |
137 |
|
multv3(ldx, ldir, funcxf.xfm); |
138 |
|
for (i = 0; i < 3; i++) |
139 |
< |
ldx[i] /= funcxf.sca; |
139 |
> |
lddx[i] = ldx[i]/funcxf.sca; |
140 |
> |
lddx[3] = omega; |
141 |
|
/* compute BRTDF */ |
142 |
|
if (np->mp->otype == MAT_BRTDF) { |
143 |
< |
colval(ctmp,RED) = funvalue(sa[6], 3, ldx); |
144 |
< |
if (!strcmp(sa[7],sa[6])) |
143 |
> |
if (sa[6][0] == '0') /* special case */ |
144 |
> |
colval(ctmp,RED) = 0.0; |
145 |
> |
else |
146 |
> |
colval(ctmp,RED) = funvalue(sa[6], 4, lddx); |
147 |
> |
if (sa[7][0] == '0') |
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], 3, ldx); |
152 |
> |
colval(ctmp,GRN) = funvalue(sa[7], 4, lddx); |
153 |
|
if (!strcmp(sa[8],sa[6])) |
154 |
|
colval(ctmp,BLU) = colval(ctmp,RED); |
155 |
|
else if (!strcmp(sa[8],sa[7])) |
156 |
|
colval(ctmp,BLU) = colval(ctmp,GRN); |
157 |
|
else |
158 |
< |
colval(ctmp,BLU) = funvalue(sa[8], 3, ldx); |
158 |
> |
colval(ctmp,BLU) = funvalue(sa[8], 4, lddx); |
159 |
|
dtmp = bright(ctmp); |
160 |
|
} else if (np->dp == NULL) { |
161 |
< |
dtmp = funvalue(sa[0], 3, ldx); |
161 |
> |
dtmp = funvalue(sa[0], 4, lddx); |
162 |
|
setcolor(ctmp, dtmp, dtmp, dtmp); |
163 |
|
} else { |
164 |
|
for (i = 0; i < np->dp->nd; i++) |
165 |
< |
pt[i] = funvalue(sa[3+i], 3, ldx); |
166 |
< |
dtmp = datavalue(np->dp, pt); |
167 |
< |
dtmp = funvalue(sa[0], 1, &dtmp); |
165 |
> |
pt[i] = funvalue(sa[3+i], 4, lddx); |
166 |
> |
vldx[0] = datavalue(np->dp, pt); |
167 |
> |
dtmp = funvalue(sa[0], 5, vldx); |
168 |
|
setcolor(ctmp, dtmp, dtmp, dtmp); |
169 |
|
} |
170 |
< |
if (errno) |
171 |
< |
goto computerr; |
170 |
> |
if ((errno == EDOM) | (errno == ERANGE)) { |
171 |
> |
objerror(np->mp, WARNING, "compute error"); |
172 |
> |
return; |
173 |
> |
} |
174 |
|
if (dtmp <= FTINY) |
175 |
|
return; |
176 |
|
if (ldot > 0.0) { |
177 |
|
/* |
178 |
|
* Compute reflected non-diffuse component. |
179 |
|
*/ |
180 |
< |
if (np->mp->otype == MAT_MFUNC || np->mp->otype == MAT_MDATA) |
180 |
> |
if ((np->mp->otype == MAT_MFUNC) | (np->mp->otype == MAT_MDATA)) |
181 |
|
multcolor(ctmp, np->mcolor); |
182 |
|
dtmp = ldot * omega * np->rspec; |
183 |
|
scalecolor(ctmp, dtmp); |
186 |
|
/* |
187 |
|
* Compute transmitted non-diffuse component. |
188 |
|
*/ |
189 |
< |
if (np->mp->otype == MAT_TFUNC || np->mp->otype == MAT_TDATA) |
189 |
> |
if ((np->mp->otype == MAT_TFUNC) | (np->mp->otype == MAT_TDATA)) |
190 |
|
multcolor(ctmp, np->mcolor); |
191 |
|
dtmp = -ldot * omega * np->tspec; |
192 |
|
scalecolor(ctmp, dtmp); |
193 |
|
addcolor(cval, ctmp); |
194 |
|
} |
195 |
< |
return; |
180 |
< |
computerr: |
181 |
< |
objerror(np->mp, WARNING, "compute error"); |
182 |
< |
return; |
195 |
> |
#undef lddx |
196 |
|
} |
197 |
|
|
198 |
|
|
199 |
< |
m_brdf(m, r) /* color a ray which hit a BRDF material */ |
200 |
< |
register OBJREC *m; |
201 |
< |
register RAY *r; |
199 |
> |
int |
200 |
> |
m_brdf( /* color a ray that hit a BRDTfunc material */ |
201 |
> |
OBJREC *m, |
202 |
> |
RAY *r |
203 |
> |
) |
204 |
|
{ |
205 |
< |
int minsa, minfa; |
205 |
> |
int hitfront = 1; |
206 |
|
BRDFDAT nd; |
207 |
< |
double transtest, transdist; |
207 |
> |
RAY sr; |
208 |
> |
double mirtest=0, mirdist=0; |
209 |
> |
double transtest=0, transdist=0; |
210 |
> |
int hasrefl, hastrans; |
211 |
> |
int hastexture; |
212 |
|
COLOR ctmp; |
213 |
< |
double dtmp, tspect, rspecr; |
214 |
< |
register int i; |
213 |
> |
FVECT vtmp; |
214 |
> |
double d; |
215 |
> |
MFUNC *mf; |
216 |
> |
int i; |
217 |
|
/* check arguments */ |
218 |
< |
switch (m->otype) { |
198 |
< |
case MAT_PFUNC: case MAT_MFUNC: |
199 |
< |
minsa = 2; minfa = 4; break; |
200 |
< |
case MAT_PDATA: case MAT_MDATA: |
201 |
< |
minsa = 4; minfa = 4; break; |
202 |
< |
case MAT_TFUNC: |
203 |
< |
minsa = 2; minfa = 6; break; |
204 |
< |
case MAT_TDATA: |
205 |
< |
minsa = 4; minfa = 6; break; |
206 |
< |
case MAT_BRTDF: |
207 |
< |
minsa = 10; minfa = 6; break; |
208 |
< |
} |
209 |
< |
if (m->oargs.nsargs < minsa || m->oargs.nfargs < minfa) |
218 |
> |
if ((m->oargs.nsargs < 10) | (m->oargs.nfargs < 9)) |
219 |
|
objerror(m, USER, "bad # arguments"); |
220 |
|
nd.mp = m; |
221 |
|
nd.pr = r; |
222 |
< |
/* get specular component */ |
223 |
< |
nd.rspec = m->oargs.farg[3]; |
224 |
< |
/* compute transmission */ |
225 |
< |
if (m->otype == MAT_TFUNC || m->otype == MAT_TDATA |
226 |
< |
|| m->otype == MAT_BRTDF) { |
227 |
< |
nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec); |
228 |
< |
nd.tspec = nd.trans * m->oargs.farg[5]; |
229 |
< |
nd.tdiff = nd.trans - nd.tspec; |
230 |
< |
} else |
231 |
< |
nd.tdiff = nd.tspec = nd.trans = 0.0; |
232 |
< |
/* early shadow check */ |
233 |
< |
if (r->crtype & SHADOW && (m->otype != MAT_BRTDF || nd.tspec <= FTINY)) |
234 |
< |
return; |
235 |
< |
/* diffuse reflection */ |
236 |
< |
nd.rdiff = 1.0 - nd.trans - nd.rspec; |
237 |
< |
/* get material color */ |
229 |
< |
setcolor(nd.mcolor, m->oargs.farg[0], |
230 |
< |
m->oargs.farg[1], |
231 |
< |
m->oargs.farg[2]); |
232 |
< |
/* fix orientation */ |
233 |
< |
if (r->rod < 0.0) |
234 |
< |
flipsurface(r); |
222 |
> |
/* dummy values */ |
223 |
> |
nd.rspec = nd.tspec = 1.0; |
224 |
> |
nd.trans = 0.5; |
225 |
> |
/* diffuse reflectance */ |
226 |
> |
if (r->rod > 0.0) |
227 |
> |
setcolor(nd.rdiff, m->oargs.farg[0], |
228 |
> |
m->oargs.farg[1], |
229 |
> |
m->oargs.farg[2]); |
230 |
> |
else |
231 |
> |
setcolor(nd.rdiff, m->oargs.farg[3], |
232 |
> |
m->oargs.farg[4], |
233 |
> |
m->oargs.farg[5]); |
234 |
> |
/* diffuse transmittance */ |
235 |
> |
setcolor(nd.tdiff, m->oargs.farg[6], |
236 |
> |
m->oargs.farg[7], |
237 |
> |
m->oargs.farg[8]); |
238 |
|
/* get modifiers */ |
239 |
|
raytexture(r, m->omod); |
240 |
< |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
241 |
< |
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
242 |
< |
transtest = 0; |
240 |
< |
/* load auxiliary files */ |
241 |
< |
if (m->otype == MAT_PDATA || m->otype == MAT_MDATA |
242 |
< |
|| m->otype == MAT_TDATA) { |
243 |
< |
nd.dp = getdata(m->oargs.sarg[1]); |
244 |
< |
for (i = 3; i < m->oargs.nsargs; i++) |
245 |
< |
if (m->oargs.sarg[i][0] == '-') |
246 |
< |
break; |
247 |
< |
if (i-3 != nd.dp->nd) |
248 |
< |
objerror(m, USER, "dimension error"); |
249 |
< |
funcfile(m->oargs.sarg[2]); |
250 |
< |
} else if (m->otype == MAT_BRTDF) { |
251 |
< |
nd.dp = NULL; |
252 |
< |
funcfile(m->oargs.sarg[9]); |
240 |
> |
hastexture = DOT(r->pert,r->pert) > FTINY*FTINY; |
241 |
> |
if (hastexture) { /* perturb normal */ |
242 |
> |
nd.pdot = raynormal(nd.pnorm, r); |
243 |
|
} else { |
244 |
< |
nd.dp = NULL; |
245 |
< |
funcfile(m->oargs.sarg[1]); |
244 |
> |
VCOPY(nd.pnorm, r->ron); |
245 |
> |
nd.pdot = r->rod; |
246 |
|
} |
247 |
< |
/* set special variables */ |
248 |
< |
setbrdfunc(&nd); |
247 |
> |
if (r->rod < 0.0) { /* orient perturbed values */ |
248 |
> |
nd.pdot = -nd.pdot; |
249 |
> |
for (i = 0; i < 3; i++) { |
250 |
> |
nd.pnorm[i] = -nd.pnorm[i]; |
251 |
> |
r->pert[i] = -r->pert[i]; |
252 |
> |
} |
253 |
> |
hitfront = 0; |
254 |
> |
} |
255 |
> |
copycolor(nd.mcolor, r->pcol); /* get pattern color */ |
256 |
> |
multcolor(nd.rdiff, nd.mcolor); /* modify diffuse values */ |
257 |
> |
multcolor(nd.tdiff, nd.mcolor); |
258 |
> |
hasrefl = bright(nd.rdiff) > FTINY; |
259 |
> |
hastrans = bright(nd.tdiff) > FTINY; |
260 |
> |
/* load cal file */ |
261 |
> |
nd.dp = NULL; |
262 |
> |
mf = getfunc(m, 9, 0x3f, 0); |
263 |
|
/* compute transmitted ray */ |
264 |
< |
tspect = 0.; |
265 |
< |
if (m->otype == MAT_BRTDF && nd.tspec > FTINY) { |
266 |
< |
RAY sr; |
267 |
< |
errno = 0; |
268 |
< |
setcolor(ctmp, varvalue(m->oargs.sarg[3]), |
269 |
< |
varvalue(m->oargs.sarg[4]), |
270 |
< |
varvalue(m->oargs.sarg[5])); |
271 |
< |
scalecolor(ctmp, nd.trans); |
272 |
< |
if (errno) |
273 |
< |
objerror(m, WARNING, "compute error"); |
274 |
< |
else if ((tspect = bright(ctmp)) > FTINY && |
275 |
< |
rayorigin(&sr, r, TRANS, tspect) == 0) { |
276 |
< |
if (!(r->crtype & SHADOW) && |
273 |
< |
DOT(r->pert,r->pert) > FTINY*FTINY) { |
274 |
< |
for (i = 0; i < 3; i++) /* perturb direction */ |
275 |
< |
sr.rdir[i] = r->rdir[i] - |
276 |
< |
.75*r->pert[i]; |
277 |
< |
normalize(sr.rdir); |
278 |
< |
} else { |
264 |
> |
setbrdfunc(&nd); |
265 |
> |
errno = 0; |
266 |
> |
setcolor(ctmp, evalue(mf->ep[3]), |
267 |
> |
evalue(mf->ep[4]), |
268 |
> |
evalue(mf->ep[5])); |
269 |
> |
if ((errno == EDOM) | (errno == ERANGE)) |
270 |
> |
objerror(m, WARNING, "compute error"); |
271 |
> |
else if (rayorigin(&sr, TRANS, r, ctmp) == 0) { |
272 |
> |
if (!(r->crtype & SHADOW) && hastexture) { |
273 |
> |
/* perturb direction */ |
274 |
> |
VSUM(sr.rdir, r->rdir, r->pert, -.75); |
275 |
> |
if (normalize(sr.rdir) == 0.0) { |
276 |
> |
objerror(m, WARNING, "illegal perturbation"); |
277 |
|
VCOPY(sr.rdir, r->rdir); |
280 |
– |
transtest = 2; |
278 |
|
} |
279 |
< |
rayvalue(&sr); |
280 |
< |
multcolor(sr.rcol, ctmp); |
281 |
< |
addcolor(r->rcol, sr.rcol); |
282 |
< |
transtest *= bright(sr.rcol); |
279 |
> |
} else { |
280 |
> |
VCOPY(sr.rdir, r->rdir); |
281 |
> |
} |
282 |
> |
rayvalue(&sr); |
283 |
> |
multcolor(sr.rcol, sr.rcoef); |
284 |
> |
addcolor(r->rcol, sr.rcol); |
285 |
> |
if (!hastexture) { |
286 |
> |
transtest = 2.0*bright(sr.rcol); |
287 |
|
transdist = r->rot + sr.rt; |
288 |
|
} |
289 |
|
} |
290 |
|
if (r->crtype & SHADOW) /* the rest is shadow */ |
291 |
< |
return; |
291 |
> |
return(1); |
292 |
|
/* compute reflected ray */ |
293 |
< |
rspecr = 0.; |
294 |
< |
if (m->otype == MAT_BRTDF && nd.rspec > FTINY) { |
295 |
< |
RAY sr; |
296 |
< |
errno = 0; |
297 |
< |
setcolor(ctmp, varvalue(m->oargs.sarg[0]), |
298 |
< |
varvalue(m->oargs.sarg[1]), |
299 |
< |
varvalue(m->oargs.sarg[2])); |
300 |
< |
if (errno) |
301 |
< |
objerror(m, WARNING, "compute error"); |
302 |
< |
else if ((rspecr = bright(ctmp)) > FTINY && |
303 |
< |
rayorigin(&sr, r, REFLECTED, rspecr) == 0) { |
304 |
< |
for (i = 0; i < 3; i++) |
305 |
< |
sr.rdir[i] = r->rdir[i] + |
306 |
< |
2.0*nd.pdot*nd.pnorm[i]; |
307 |
< |
rayvalue(&sr); |
308 |
< |
multcolor(sr.rcol, ctmp); |
308 |
< |
addcolor(r->rcol, sr.rcol); |
293 |
> |
setbrdfunc(&nd); |
294 |
> |
errno = 0; |
295 |
> |
setcolor(ctmp, evalue(mf->ep[0]), |
296 |
> |
evalue(mf->ep[1]), |
297 |
> |
evalue(mf->ep[2])); |
298 |
> |
if ((errno == EDOM) | (errno == ERANGE)) |
299 |
> |
objerror(m, WARNING, "compute error"); |
300 |
> |
else if (rayorigin(&sr, REFLECTED, r, ctmp) == 0) { |
301 |
> |
VSUM(sr.rdir, r->rdir, nd.pnorm, 2.*nd.pdot); |
302 |
> |
checknorm(sr.rdir); |
303 |
> |
rayvalue(&sr); |
304 |
> |
multcolor(sr.rcol, sr.rcoef); |
305 |
> |
addcolor(r->rcol, sr.rcol); |
306 |
> |
if (!hastexture && r->ro != NULL && isflat(r->ro->otype)) { |
307 |
> |
mirtest = 2.0*bright(sr.rcol); |
308 |
> |
mirdist = r->rot + sr.rt; |
309 |
|
} |
310 |
|
} |
311 |
|
/* compute ambient */ |
312 |
< |
if ((dtmp = 1.0-nd.trans-rspecr) > FTINY) { |
313 |
< |
ambient(ctmp, r); |
314 |
< |
scalecolor(ctmp, dtmp); |
315 |
< |
multcolor(ctmp, nd.mcolor); /* modified by material color */ |
312 |
> |
if (hasrefl) { |
313 |
> |
if (!hitfront) |
314 |
> |
flipsurface(r); |
315 |
> |
copycolor(ctmp, nd.rdiff); |
316 |
> |
multambient(ctmp, r, nd.pnorm); |
317 |
|
addcolor(r->rcol, ctmp); /* add to returned color */ |
318 |
+ |
if (!hitfront) |
319 |
+ |
flipsurface(r); |
320 |
|
} |
321 |
< |
if ((dtmp = nd.trans-tspect) > FTINY) { /* from other side */ |
321 |
> |
if (hastrans) { /* from other side */ |
322 |
> |
if (hitfront) |
323 |
> |
flipsurface(r); |
324 |
> |
vtmp[0] = -nd.pnorm[0]; |
325 |
> |
vtmp[1] = -nd.pnorm[1]; |
326 |
> |
vtmp[2] = -nd.pnorm[2]; |
327 |
> |
copycolor(ctmp, nd.tdiff); |
328 |
> |
multambient(ctmp, r, vtmp); |
329 |
> |
addcolor(r->rcol, ctmp); |
330 |
> |
if (hitfront) |
331 |
> |
flipsurface(r); |
332 |
> |
} |
333 |
> |
if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0') |
334 |
> |
direct(r, dirbrdf, &nd); /* add direct component */ |
335 |
> |
|
336 |
> |
d = bright(r->rcol); /* set effective distance */ |
337 |
> |
if (transtest > d) |
338 |
> |
r->rt = transdist; |
339 |
> |
else if (mirtest > d) |
340 |
> |
r->rt = mirdist; |
341 |
> |
|
342 |
> |
return(1); |
343 |
> |
} |
344 |
> |
|
345 |
> |
|
346 |
> |
|
347 |
> |
int |
348 |
> |
m_brdf2( /* color a ray that hit a BRDF material */ |
349 |
> |
OBJREC *m, |
350 |
> |
RAY *r |
351 |
> |
) |
352 |
> |
{ |
353 |
> |
BRDFDAT nd; |
354 |
> |
COLOR ctmp; |
355 |
> |
FVECT vtmp; |
356 |
> |
double dtmp; |
357 |
> |
/* always a shadow */ |
358 |
> |
if (r->crtype & SHADOW) |
359 |
> |
return(1); |
360 |
> |
/* check arguments */ |
361 |
> |
if ((m->oargs.nsargs < (hasdata(m->otype)?4:2)) | (m->oargs.nfargs < |
362 |
> |
((m->otype==MAT_TFUNC)|(m->otype==MAT_TDATA)?6:4))) |
363 |
> |
objerror(m, USER, "bad # arguments"); |
364 |
> |
/* check for back side */ |
365 |
> |
if (r->rod < 0.0) { |
366 |
> |
if (!backvis) { |
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 */ |
378 |
> |
setcolor(nd.mcolor, m->oargs.farg[0], |
379 |
> |
m->oargs.farg[1], |
380 |
> |
m->oargs.farg[2]); |
381 |
> |
/* get specular component */ |
382 |
> |
nd.rspec = m->oargs.farg[3]; |
383 |
> |
/* compute transmittance */ |
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; |
388 |
> |
setcolor(nd.tdiff, dtmp, dtmp, dtmp); |
389 |
> |
} else { |
390 |
> |
nd.tspec = nd.trans = 0.0; |
391 |
> |
setcolor(nd.tdiff, 0.0, 0.0, 0.0); |
392 |
> |
} |
393 |
> |
/* compute reflectance */ |
394 |
> |
dtmp = 1.0 - nd.trans - nd.rspec; |
395 |
> |
setcolor(nd.rdiff, dtmp, dtmp, dtmp); |
396 |
> |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
397 |
> |
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
398 |
> |
multcolor(nd.rdiff, nd.mcolor); |
399 |
> |
multcolor(nd.tdiff, nd.mcolor); |
400 |
> |
/* load auxiliary files */ |
401 |
> |
if (hasdata(m->otype)) { |
402 |
> |
nd.dp = getdata(m->oargs.sarg[1]); |
403 |
> |
getfunc(m, 2, 0, 0); |
404 |
> |
} else { |
405 |
> |
nd.dp = NULL; |
406 |
> |
getfunc(m, 1, 0, 0); |
407 |
> |
} |
408 |
> |
/* compute ambient */ |
409 |
> |
if (nd.trans < 1.0-FTINY) { |
410 |
> |
copycolor(ctmp, nd.mcolor); /* modified by material color */ |
411 |
> |
scalecolor(ctmp, 1.0-nd.trans); |
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); |
418 |
< |
scalecolor(ctmp, dtmp); |
419 |
< |
multcolor(ctmp, nd.mcolor); |
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 |
> |
multambient(ctmp, r, vtmp); |
423 |
|
addcolor(r->rcol, ctmp); |
424 |
|
flipsurface(r); |
425 |
|
} |
426 |
|
/* add direct component */ |
427 |
|
direct(r, dirbrdf, &nd); |
428 |
< |
/* check distance */ |
429 |
< |
if (transtest > bright(r->rcol)) |
330 |
< |
r->rt = transdist; |
428 |
> |
|
429 |
> |
return(1); |
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 |
> |
BRDFDAT *np |
436 |
> |
) |
437 |
|
{ |
438 |
|
FVECT vec; |
439 |
|
|