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 |
|
* normal.c - shading function for normal materials. |
6 |
|
* |
8 |
|
* 12/19/85 - added stuff for metals. |
9 |
|
* 6/26/87 - improved specular model. |
10 |
|
* 9/28/87 - added model for translucent materials. |
11 |
+ |
* Later changes described in delta comments. |
12 |
|
*/ |
13 |
|
|
14 |
< |
#include "ray.h" |
14 |
> |
#include "copyright.h" |
15 |
|
|
16 |
+ |
#include "ray.h" |
17 |
+ |
#include "ambient.h" |
18 |
+ |
#include "source.h" |
19 |
|
#include "otypes.h" |
20 |
+ |
#include "rtotypes.h" |
21 |
+ |
#include "random.h" |
22 |
+ |
#include "pmapmat.h" |
23 |
|
|
24 |
+ |
#ifndef MAXITER |
25 |
+ |
#define MAXITER 10 /* maximum # specular ray attempts */ |
26 |
+ |
#endif |
27 |
+ |
/* estimate of Fresnel function */ |
28 |
+ |
#define FRESNE(ci) (exp(-5.85*(ci)) - 0.00287989916) |
29 |
+ |
#define FRESTHRESH 0.017999 /* minimum specularity for approx. */ |
30 |
+ |
|
31 |
+ |
|
32 |
|
/* |
33 |
< |
* This routine uses portions of the reflection |
34 |
< |
* model described by Cook and Torrance. |
23 |
< |
* The computation of specular components has been simplified by |
24 |
< |
* numerous approximations and ommisions to improve speed. |
33 |
> |
* This routine implements the isotropic Gaussian |
34 |
> |
* model described by Ward in Siggraph `92 article. |
35 |
|
* We orient the surface towards the incoming ray, so a single |
36 |
|
* surface can be used to represent an infinitely thin object. |
37 |
|
* |
42 |
|
* red grn blu rspec rough trans tspec |
43 |
|
*/ |
44 |
|
|
45 |
< |
#define BSPEC(m) (6.0) /* specularity parameter b */ |
45 |
> |
/* specularity flags */ |
46 |
> |
#define SP_REFL 01 /* has reflected specular component */ |
47 |
> |
#define SP_TRAN 02 /* has transmitted specular */ |
48 |
> |
#define SP_PURE 04 /* purely specular (zero roughness) */ |
49 |
> |
#define SP_FLAT 010 /* flat reflecting surface */ |
50 |
> |
#define SP_RBLT 020 /* reflection below sample threshold */ |
51 |
> |
#define SP_TBLT 040 /* transmission below threshold */ |
52 |
|
|
37 |
– |
extern double exp(); |
38 |
– |
|
53 |
|
typedef struct { |
54 |
|
OBJREC *mp; /* material pointer */ |
55 |
< |
RAY *pr; /* intersected ray */ |
55 |
> |
RAY *rp; /* ray pointer */ |
56 |
> |
short specfl; /* specularity flags, defined above */ |
57 |
|
COLOR mcolor; /* color of this material */ |
58 |
|
COLOR scolor; /* color of specular component */ |
59 |
|
FVECT vrefl; /* vector in direction of reflected ray */ |
60 |
< |
double alpha2; /* roughness squared times 2 */ |
60 |
> |
FVECT prdir; /* vector in transmitted direction */ |
61 |
> |
double alpha2; /* roughness squared */ |
62 |
|
double rdiff, rspec; /* reflected specular, diffuse */ |
63 |
|
double trans; /* transmissivity */ |
64 |
|
double tdiff, tspec; /* transmitted specular, diffuse */ |
66 |
|
double pdot; /* perturbed dot product */ |
67 |
|
} NORMDAT; /* normal material data */ |
68 |
|
|
69 |
+ |
static void gaussamp(NORMDAT *np); |
70 |
|
|
71 |
< |
dirnorm(cval, np, ldir, omega) /* compute source contribution */ |
72 |
< |
COLOR cval; /* returned coefficient */ |
73 |
< |
register NORMDAT *np; /* material data */ |
74 |
< |
FVECT ldir; /* light source direction */ |
75 |
< |
double omega; /* light source size */ |
71 |
> |
|
72 |
> |
static void |
73 |
> |
dirnorm( /* compute source contribution */ |
74 |
> |
COLOR cval, /* returned coefficient */ |
75 |
> |
void *nnp, /* material data */ |
76 |
> |
FVECT ldir, /* light source direction */ |
77 |
> |
double omega /* light source size */ |
78 |
> |
) |
79 |
|
{ |
80 |
+ |
NORMDAT *np = nnp; |
81 |
|
double ldot; |
82 |
< |
double dtmp; |
82 |
> |
double lrdiff, ltdiff; |
83 |
> |
double dtmp, d2, d3, d4; |
84 |
> |
FVECT vtmp; |
85 |
|
COLOR ctmp; |
86 |
|
|
87 |
|
setcolor(cval, 0.0, 0.0, 0.0); |
91 |
|
if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY) |
92 |
|
return; /* wrong side */ |
93 |
|
|
94 |
< |
if (ldot > FTINY && np->rdiff > FTINY) { |
94 |
> |
/* Fresnel estimate */ |
95 |
> |
lrdiff = np->rdiff; |
96 |
> |
ltdiff = np->tdiff; |
97 |
> |
if (np->specfl & SP_PURE && np->rspec >= FRESTHRESH && |
98 |
> |
(lrdiff > FTINY) | (ltdiff > FTINY)) { |
99 |
> |
dtmp = 1. - FRESNE(fabs(ldot)); |
100 |
> |
lrdiff *= dtmp; |
101 |
> |
ltdiff *= dtmp; |
102 |
> |
} |
103 |
> |
|
104 |
> |
if (ldot > FTINY && lrdiff > FTINY) { |
105 |
|
/* |
106 |
|
* Compute and add diffuse reflected component to returned |
107 |
|
* color. The diffuse reflected component will always be |
108 |
|
* modified by the color of the material. |
109 |
|
*/ |
110 |
|
copycolor(ctmp, np->mcolor); |
111 |
< |
dtmp = ldot * omega * np->rdiff / PI; |
111 |
> |
dtmp = ldot * omega * lrdiff * (1.0/PI); |
112 |
|
scalecolor(ctmp, dtmp); |
113 |
|
addcolor(cval, ctmp); |
114 |
|
} |
115 |
< |
if (ldot > FTINY && np->rspec > FTINY && np->alpha2 > FTINY) { |
115 |
> |
|
116 |
> |
if (ldot < -FTINY && ltdiff > FTINY) { |
117 |
|
/* |
118 |
+ |
* Compute diffuse transmission. |
119 |
+ |
*/ |
120 |
+ |
copycolor(ctmp, np->mcolor); |
121 |
+ |
dtmp = -ldot * omega * ltdiff * (1.0/PI); |
122 |
+ |
scalecolor(ctmp, dtmp); |
123 |
+ |
addcolor(cval, ctmp); |
124 |
+ |
} |
125 |
+ |
|
126 |
+ |
if (ambRayInPmap(np->rp)) |
127 |
+ |
return; /* specular already in photon map */ |
128 |
+ |
|
129 |
+ |
if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) { |
130 |
+ |
/* |
131 |
|
* Compute specular reflection coefficient using |
132 |
< |
* gaussian distribution model. |
132 |
> |
* Gaussian distribution model. |
133 |
|
*/ |
134 |
< |
/* roughness + source */ |
135 |
< |
dtmp = np->alpha2 + omega/(2.0*PI); |
136 |
< |
/* gaussian */ |
137 |
< |
dtmp = exp((DOT(np->vrefl,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
134 |
> |
/* roughness */ |
135 |
> |
dtmp = np->alpha2; |
136 |
> |
/* + source if flat */ |
137 |
> |
if (np->specfl & SP_FLAT) |
138 |
> |
dtmp += omega * (0.25/PI); |
139 |
> |
/* half vector */ |
140 |
> |
VSUB(vtmp, ldir, np->rp->rdir); |
141 |
> |
d2 = DOT(vtmp, np->pnorm); |
142 |
> |
d2 *= d2; |
143 |
> |
d3 = DOT(vtmp,vtmp); |
144 |
> |
d4 = (d3 - d2) / d2; |
145 |
> |
/* new W-G-M-D model */ |
146 |
> |
dtmp = exp(-d4/dtmp) * d3 / (PI * d2*d2 * dtmp); |
147 |
|
/* worth using? */ |
148 |
|
if (dtmp > FTINY) { |
149 |
|
copycolor(ctmp, np->scolor); |
150 |
< |
dtmp *= omega; |
150 |
> |
dtmp *= ldot * omega; |
151 |
|
scalecolor(ctmp, dtmp); |
152 |
|
addcolor(cval, ctmp); |
153 |
|
} |
154 |
|
} |
155 |
< |
if (ldot < -FTINY && np->tdiff > FTINY) { |
155 |
> |
|
156 |
> |
|
157 |
> |
if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) { |
158 |
|
/* |
101 |
– |
* Compute diffuse transmission. |
102 |
– |
*/ |
103 |
– |
copycolor(ctmp, np->mcolor); |
104 |
– |
dtmp = -ldot * omega * np->tdiff / PI; |
105 |
– |
scalecolor(ctmp, dtmp); |
106 |
– |
addcolor(cval, ctmp); |
107 |
– |
} |
108 |
– |
if (ldot < -FTINY && np->tspec > FTINY && np->alpha2 > FTINY) { |
109 |
– |
/* |
159 |
|
* Compute specular transmission. Specular transmission |
160 |
< |
* is unaffected by material color. |
160 |
> |
* is always modified by material color. |
161 |
|
*/ |
162 |
|
/* roughness + source */ |
163 |
< |
dtmp = np->alpha2 + omega/(2.0*PI); |
164 |
< |
/* gaussian */ |
165 |
< |
dtmp = exp((DOT(np->pr->rdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
163 |
> |
dtmp = np->alpha2 + omega*(1.0/PI); |
164 |
> |
/* Gaussian */ |
165 |
> |
dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp); |
166 |
|
/* worth using? */ |
167 |
|
if (dtmp > FTINY) { |
168 |
< |
dtmp *= np->tspec * omega; |
169 |
< |
setcolor(ctmp, dtmp, dtmp, dtmp); |
168 |
> |
copycolor(ctmp, np->mcolor); |
169 |
> |
dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot); |
170 |
> |
scalecolor(ctmp, dtmp); |
171 |
|
addcolor(cval, ctmp); |
172 |
|
} |
173 |
|
} |
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
< |
m_normal(m, r) /* color a ray which hit something normal */ |
178 |
< |
register OBJREC *m; |
179 |
< |
register RAY *r; |
177 |
> |
int |
178 |
> |
m_normal( /* color a ray that hit something normal */ |
179 |
> |
OBJREC *m, |
180 |
> |
RAY *r |
181 |
> |
) |
182 |
|
{ |
183 |
|
NORMDAT nd; |
184 |
+ |
double fest; |
185 |
|
double transtest, transdist; |
186 |
< |
double dtmp; |
186 |
> |
double mirtest, mirdist; |
187 |
> |
int hastexture; |
188 |
> |
double d; |
189 |
|
COLOR ctmp; |
190 |
< |
register int i; |
190 |
> |
int i; |
191 |
|
|
192 |
< |
if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) |
193 |
< |
objerror(m, USER, "bad # arguments"); |
192 |
> |
/* PMAP: skip transmitted shadow ray if accounted for in photon map */ |
193 |
> |
/* No longer needed? |
194 |
> |
if (shadowRayInPmap(r) || ambRayInPmap(r)) |
195 |
> |
return(1); */ |
196 |
> |
|
197 |
|
/* easy shadow test */ |
198 |
|
if (r->crtype & SHADOW && m->otype != MAT_TRANS) |
199 |
< |
return; |
199 |
> |
return(1); |
200 |
> |
|
201 |
> |
if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) |
202 |
> |
objerror(m, USER, "bad number of arguments"); |
203 |
> |
/* check for back side */ |
204 |
> |
if (r->rod < 0.0) { |
205 |
> |
if (!backvis) { |
206 |
> |
raytrans(r); |
207 |
> |
return(1); |
208 |
> |
} |
209 |
> |
raytexture(r, m->omod); |
210 |
> |
flipsurface(r); /* reorient if backvis */ |
211 |
> |
} else |
212 |
> |
raytexture(r, m->omod); |
213 |
|
nd.mp = m; |
214 |
< |
nd.pr = r; |
214 |
> |
nd.rp = r; |
215 |
|
/* get material color */ |
216 |
|
setcolor(nd.mcolor, m->oargs.farg[0], |
217 |
|
m->oargs.farg[1], |
218 |
|
m->oargs.farg[2]); |
219 |
|
/* get roughness */ |
220 |
+ |
nd.specfl = 0; |
221 |
|
nd.alpha2 = m->oargs.farg[4]; |
222 |
< |
nd.alpha2 *= 2.0 * nd.alpha2; |
223 |
< |
/* reorient if necessary */ |
152 |
< |
if (r->rod < 0.0) |
153 |
< |
flipsurface(r); |
154 |
< |
/* get modifiers */ |
155 |
< |
raytexture(r, m->omod); |
156 |
< |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
157 |
< |
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
158 |
< |
r->rt = r->rot; /* default ray length */ |
159 |
< |
transtest = 0; |
160 |
< |
/* get specular component */ |
161 |
< |
nd.rspec = m->oargs.farg[3]; |
222 |
> |
if ((nd.alpha2 *= nd.alpha2) <= FTINY) |
223 |
> |
nd.specfl |= SP_PURE; |
224 |
|
|
225 |
< |
if (nd.rspec > FTINY) { /* has specular component */ |
226 |
< |
/* compute specular color */ |
227 |
< |
if (m->otype == MAT_METAL) |
228 |
< |
copycolor(nd.scolor, nd.mcolor); |
229 |
< |
else |
168 |
< |
setcolor(nd.scolor, 1.0, 1.0, 1.0); |
169 |
< |
scalecolor(nd.scolor, nd.rspec); |
170 |
< |
/* improved model */ |
171 |
< |
dtmp = exp(-BSPEC(m)*nd.pdot); |
172 |
< |
for (i = 0; i < 3; i++) |
173 |
< |
colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp; |
174 |
< |
nd.rspec += (1.0-nd.rspec)*dtmp; |
175 |
< |
/* compute reflected ray */ |
176 |
< |
for (i = 0; i < 3; i++) |
177 |
< |
nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i]; |
178 |
< |
|
179 |
< |
if (nd.alpha2 <= FTINY && !(r->crtype & SHADOW)) { |
180 |
< |
RAY lr; |
181 |
< |
if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { |
182 |
< |
VCOPY(lr.rdir, nd.vrefl); |
183 |
< |
rayvalue(&lr); |
184 |
< |
multcolor(lr.rcol, nd.scolor); |
185 |
< |
addcolor(r->rcol, lr.rcol); |
186 |
< |
} |
187 |
< |
} |
225 |
> |
if ( (hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY)) ) { |
226 |
> |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
227 |
> |
} else { |
228 |
> |
VCOPY(nd.pnorm, r->ron); |
229 |
> |
nd.pdot = r->rod; |
230 |
|
} |
231 |
+ |
if (r->ro != NULL && isflat(r->ro->otype)) |
232 |
+ |
nd.specfl |= SP_FLAT; |
233 |
+ |
if (nd.pdot < .001) |
234 |
+ |
nd.pdot = .001; /* non-zero for dirnorm() */ |
235 |
+ |
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
236 |
+ |
mirtest = transtest = 0; |
237 |
+ |
mirdist = transdist = r->rot; |
238 |
+ |
nd.rspec = m->oargs.farg[3]; |
239 |
+ |
/* compute Fresnel approx. */ |
240 |
+ |
if (nd.specfl & SP_PURE && nd.rspec >= FRESTHRESH) { |
241 |
+ |
fest = FRESNE(nd.pdot); |
242 |
+ |
nd.rspec += fest*(1. - nd.rspec); |
243 |
+ |
} else |
244 |
+ |
fest = 0.; |
245 |
|
/* compute transmission */ |
246 |
|
if (m->otype == MAT_TRANS) { |
247 |
|
nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec); |
248 |
|
nd.tspec = nd.trans * m->oargs.farg[6]; |
249 |
|
nd.tdiff = nd.trans - nd.tspec; |
250 |
+ |
if (nd.tspec > FTINY) { |
251 |
+ |
nd.specfl |= SP_TRAN; |
252 |
+ |
/* check threshold */ |
253 |
+ |
if (!(nd.specfl & SP_PURE) && |
254 |
+ |
specthresh >= nd.tspec-FTINY) |
255 |
+ |
nd.specfl |= SP_TBLT; |
256 |
+ |
if (!hastexture || r->crtype & (SHADOW|AMBIENT)) { |
257 |
+ |
VCOPY(nd.prdir, r->rdir); |
258 |
+ |
transtest = 2; |
259 |
+ |
} else { |
260 |
+ |
for (i = 0; i < 3; i++) /* perturb */ |
261 |
+ |
nd.prdir[i] = r->rdir[i] - r->pert[i]; |
262 |
+ |
if (DOT(nd.prdir, r->ron) < -FTINY) |
263 |
+ |
normalize(nd.prdir); /* OK */ |
264 |
+ |
else |
265 |
+ |
VCOPY(nd.prdir, r->rdir); |
266 |
+ |
} |
267 |
+ |
} |
268 |
|
} else |
269 |
|
nd.tdiff = nd.tspec = nd.trans = 0.0; |
270 |
|
/* transmitted ray */ |
271 |
< |
if (nd.tspec > FTINY && nd.alpha2 <= FTINY) { |
271 |
> |
|
272 |
> |
if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) { |
273 |
|
RAY lr; |
274 |
< |
if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) { |
275 |
< |
if (DOT(r->pert,r->pert) > FTINY*FTINY) { |
276 |
< |
for (i = 0; i < 3; i++) /* perturb direction */ |
277 |
< |
lr.rdir[i] = r->rdir[i] - |
203 |
< |
.75*r->pert[i]; |
204 |
< |
normalize(lr.rdir); |
205 |
< |
} else |
206 |
< |
transtest = 2; |
274 |
> |
copycolor(lr.rcoef, nd.mcolor); /* modified by color */ |
275 |
> |
scalecolor(lr.rcoef, nd.tspec); |
276 |
> |
if (rayorigin(&lr, TRANS, r, lr.rcoef) == 0) { |
277 |
> |
VCOPY(lr.rdir, nd.prdir); |
278 |
|
rayvalue(&lr); |
279 |
< |
scalecolor(lr.rcol, nd.tspec); |
209 |
< |
multcolor(lr.rcol, nd.mcolor); /* modified by color */ |
279 |
> |
multcolor(lr.rcol, lr.rcoef); |
280 |
|
addcolor(r->rcol, lr.rcol); |
281 |
|
transtest *= bright(lr.rcol); |
282 |
|
transdist = r->rot + lr.rt; |
283 |
|
} |
284 |
+ |
} else |
285 |
+ |
transtest = 0; |
286 |
+ |
|
287 |
+ |
if (r->crtype & SHADOW) { /* the rest is shadow */ |
288 |
+ |
r->rt = transdist; |
289 |
+ |
return(1); |
290 |
|
} |
291 |
< |
if (r->crtype & SHADOW) /* the rest is shadow */ |
292 |
< |
return; |
291 |
> |
/* get specular reflection */ |
292 |
> |
if (nd.rspec > FTINY) { |
293 |
> |
nd.specfl |= SP_REFL; |
294 |
> |
/* compute specular color */ |
295 |
> |
if (m->otype != MAT_METAL) { |
296 |
> |
setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec); |
297 |
> |
} else if (fest > FTINY) { |
298 |
> |
d = m->oargs.farg[3]*(1. - fest); |
299 |
> |
for (i = 0; i < 3; i++) |
300 |
> |
colval(nd.scolor,i) = fest + |
301 |
> |
colval(nd.mcolor,i)*d; |
302 |
> |
} else { |
303 |
> |
copycolor(nd.scolor, nd.mcolor); |
304 |
> |
scalecolor(nd.scolor, nd.rspec); |
305 |
> |
} |
306 |
> |
/* check threshold */ |
307 |
> |
if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY) |
308 |
> |
nd.specfl |= SP_RBLT; |
309 |
> |
/* compute reflected ray */ |
310 |
> |
VSUM(nd.vrefl, r->rdir, nd.pnorm, 2.*nd.pdot); |
311 |
> |
/* penetration? */ |
312 |
> |
if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY) |
313 |
> |
VSUM(nd.vrefl, r->rdir, r->ron, 2.*r->rod); |
314 |
> |
checknorm(nd.vrefl); |
315 |
> |
} |
316 |
> |
/* reflected ray */ |
317 |
> |
if ((nd.specfl&(SP_REFL|SP_PURE|SP_RBLT)) == (SP_REFL|SP_PURE)) { |
318 |
> |
RAY lr; |
319 |
> |
if (rayorigin(&lr, REFLECTED, r, nd.scolor) == 0) { |
320 |
> |
VCOPY(lr.rdir, nd.vrefl); |
321 |
> |
rayvalue(&lr); |
322 |
> |
multcolor(lr.rcol, lr.rcoef); |
323 |
> |
addcolor(r->rcol, lr.rcol); |
324 |
> |
if (nd.specfl & SP_FLAT && |
325 |
> |
!hastexture | (r->crtype & AMBIENT)) { |
326 |
> |
mirtest = 2.*bright(lr.rcol); |
327 |
> |
mirdist = r->rot + lr.rt; |
328 |
> |
} |
329 |
> |
} |
330 |
> |
} |
331 |
|
/* diffuse reflection */ |
332 |
|
nd.rdiff = 1.0 - nd.trans - nd.rspec; |
333 |
|
|
334 |
< |
if (nd.rdiff <= FTINY && nd.tdiff <= FTINY && nd.alpha2 <= FTINY) |
335 |
< |
return; /* purely specular */ |
334 |
> |
if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) |
335 |
> |
return(1); /* 100% pure specular */ |
336 |
|
|
337 |
+ |
if (!(nd.specfl & SP_PURE)) |
338 |
+ |
gaussamp(&nd); /* checks *BLT flags */ |
339 |
+ |
|
340 |
|
if (nd.rdiff > FTINY) { /* ambient from this side */ |
341 |
< |
ambient(ctmp, r); |
342 |
< |
if (nd.alpha2 <= FTINY) |
343 |
< |
scalecolor(ctmp, nd.rdiff); |
344 |
< |
else |
345 |
< |
scalecolor(ctmp, 1.0-nd.trans); |
229 |
< |
multcolor(ctmp, nd.mcolor); /* modified by material color */ |
341 |
> |
copycolor(ctmp, nd.mcolor); /* modified by material color */ |
342 |
> |
scalecolor(ctmp, nd.rdiff); |
343 |
> |
if (nd.specfl & SP_RBLT) /* add in specular as well? */ |
344 |
> |
addcolor(ctmp, nd.scolor); |
345 |
> |
multambient(ctmp, r, hastexture ? nd.pnorm : r->ron); |
346 |
|
addcolor(r->rcol, ctmp); /* add to returned color */ |
347 |
|
} |
348 |
|
if (nd.tdiff > FTINY) { /* ambient from other side */ |
349 |
< |
flipsurface(r); |
350 |
< |
ambient(ctmp, r); |
235 |
< |
if (nd.alpha2 <= FTINY) |
236 |
< |
scalecolor(ctmp, nd.tdiff); |
237 |
< |
else |
349 |
> |
copycolor(ctmp, nd.mcolor); /* modified by color */ |
350 |
> |
if (nd.specfl & SP_TBLT) |
351 |
|
scalecolor(ctmp, nd.trans); |
352 |
< |
multcolor(ctmp, nd.mcolor); |
352 |
> |
else |
353 |
> |
scalecolor(ctmp, nd.tdiff); |
354 |
> |
flipsurface(r); |
355 |
> |
if (hastexture) { |
356 |
> |
FVECT bnorm; |
357 |
> |
bnorm[0] = -nd.pnorm[0]; |
358 |
> |
bnorm[1] = -nd.pnorm[1]; |
359 |
> |
bnorm[2] = -nd.pnorm[2]; |
360 |
> |
multambient(ctmp, r, bnorm); |
361 |
> |
} else |
362 |
> |
multambient(ctmp, r, r->ron); |
363 |
|
addcolor(r->rcol, ctmp); |
364 |
|
flipsurface(r); |
365 |
|
} |
366 |
|
/* add direct component */ |
367 |
|
direct(r, dirnorm, &nd); |
368 |
|
/* check distance */ |
369 |
< |
if (transtest > bright(r->rcol)) |
369 |
> |
d = bright(r->rcol); |
370 |
> |
if (transtest > d) |
371 |
|
r->rt = transdist; |
372 |
+ |
else if (mirtest > d) |
373 |
+ |
r->rt = mirdist; |
374 |
+ |
|
375 |
+ |
return(1); |
376 |
+ |
} |
377 |
+ |
|
378 |
+ |
|
379 |
+ |
static void |
380 |
+ |
gaussamp( /* sample Gaussian specular */ |
381 |
+ |
NORMDAT *np |
382 |
+ |
) |
383 |
+ |
{ |
384 |
+ |
RAY sr; |
385 |
+ |
FVECT u, v, h; |
386 |
+ |
double rv[2]; |
387 |
+ |
double d, sinp, cosp; |
388 |
+ |
COLOR scol; |
389 |
+ |
int maxiter, ntrials, nstarget, nstaken; |
390 |
+ |
int i; |
391 |
+ |
/* quick test */ |
392 |
+ |
if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL && |
393 |
+ |
(np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN) |
394 |
+ |
return; |
395 |
+ |
/* set up sample coordinates */ |
396 |
+ |
getperpendicular(u, np->pnorm, rand_samp); |
397 |
+ |
fcross(v, np->pnorm, u); |
398 |
+ |
/* compute reflection */ |
399 |
+ |
if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && |
400 |
+ |
rayorigin(&sr, SPECULAR, np->rp, np->scolor) == 0) { |
401 |
+ |
nstarget = 1; |
402 |
+ |
if (specjitter > 1.5) { /* multiple samples? */ |
403 |
+ |
nstarget = specjitter*np->rp->rweight + .5; |
404 |
+ |
if (sr.rweight <= minweight*nstarget) |
405 |
+ |
nstarget = sr.rweight/minweight; |
406 |
+ |
if (nstarget > 1) { |
407 |
+ |
d = 1./nstarget; |
408 |
+ |
scalecolor(sr.rcoef, d); |
409 |
+ |
sr.rweight *= d; |
410 |
+ |
} else |
411 |
+ |
nstarget = 1; |
412 |
+ |
} |
413 |
+ |
setcolor(scol, 0., 0., 0.); |
414 |
+ |
dimlist[ndims++] = (int)(size_t)np->mp; |
415 |
+ |
maxiter = MAXITER*nstarget; |
416 |
+ |
for (nstaken = ntrials = 0; nstaken < nstarget && |
417 |
+ |
ntrials < maxiter; ntrials++) { |
418 |
+ |
if (ntrials) |
419 |
+ |
d = frandom(); |
420 |
+ |
else |
421 |
+ |
d = urand(ilhash(dimlist,ndims)+samplendx); |
422 |
+ |
multisamp(rv, 2, d); |
423 |
+ |
d = 2.0*PI * rv[0]; |
424 |
+ |
cosp = tcos(d); |
425 |
+ |
sinp = tsin(d); |
426 |
+ |
if ((0. <= specjitter) & (specjitter < 1.)) |
427 |
+ |
rv[1] = 1.0 - specjitter*rv[1]; |
428 |
+ |
if (rv[1] <= FTINY) |
429 |
+ |
d = 1.0; |
430 |
+ |
else |
431 |
+ |
d = sqrt( np->alpha2 * -log(rv[1]) ); |
432 |
+ |
for (i = 0; i < 3; i++) |
433 |
+ |
h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]); |
434 |
+ |
d = -2.0 * DOT(h, np->rp->rdir) / (1.0 + d*d); |
435 |
+ |
VSUM(sr.rdir, np->rp->rdir, h, d); |
436 |
+ |
/* sample rejection test */ |
437 |
+ |
if ((d = DOT(sr.rdir, np->rp->ron)) <= FTINY) |
438 |
+ |
continue; |
439 |
+ |
checknorm(sr.rdir); |
440 |
+ |
if (nstarget > 1) { /* W-G-M-D adjustment */ |
441 |
+ |
if (nstaken) rayclear(&sr); |
442 |
+ |
rayvalue(&sr); |
443 |
+ |
d = 2./(1. + np->rp->rod/d); |
444 |
+ |
scalecolor(sr.rcol, d); |
445 |
+ |
addcolor(scol, sr.rcol); |
446 |
+ |
} else { |
447 |
+ |
rayvalue(&sr); |
448 |
+ |
multcolor(sr.rcol, sr.rcoef); |
449 |
+ |
addcolor(np->rp->rcol, sr.rcol); |
450 |
+ |
} |
451 |
+ |
++nstaken; |
452 |
+ |
} |
453 |
+ |
if (nstarget > 1) { /* final W-G-M-D weighting */ |
454 |
+ |
multcolor(scol, sr.rcoef); |
455 |
+ |
d = (double)nstarget/ntrials; |
456 |
+ |
scalecolor(scol, d); |
457 |
+ |
addcolor(np->rp->rcol, scol); |
458 |
+ |
} |
459 |
+ |
ndims--; |
460 |
+ |
} |
461 |
+ |
/* compute transmission */ |
462 |
+ |
copycolor(sr.rcoef, np->mcolor); /* modified by color */ |
463 |
+ |
scalecolor(sr.rcoef, np->tspec); |
464 |
+ |
if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && |
465 |
+ |
rayorigin(&sr, SPECULAR, np->rp, sr.rcoef) == 0) { |
466 |
+ |
nstarget = 1; |
467 |
+ |
if (specjitter > 1.5) { /* multiple samples? */ |
468 |
+ |
nstarget = specjitter*np->rp->rweight + .5; |
469 |
+ |
if (sr.rweight <= minweight*nstarget) |
470 |
+ |
nstarget = sr.rweight/minweight; |
471 |
+ |
if (nstarget > 1) { |
472 |
+ |
d = 1./nstarget; |
473 |
+ |
scalecolor(sr.rcoef, d); |
474 |
+ |
sr.rweight *= d; |
475 |
+ |
} else |
476 |
+ |
nstarget = 1; |
477 |
+ |
} |
478 |
+ |
dimlist[ndims++] = (int)(size_t)np->mp; |
479 |
+ |
maxiter = MAXITER*nstarget; |
480 |
+ |
for (nstaken = ntrials = 0; nstaken < nstarget && |
481 |
+ |
ntrials < maxiter; ntrials++) { |
482 |
+ |
if (ntrials) |
483 |
+ |
d = frandom(); |
484 |
+ |
else |
485 |
+ |
d = urand(ilhash(dimlist,ndims)+samplendx); |
486 |
+ |
multisamp(rv, 2, d); |
487 |
+ |
d = 2.0*PI * rv[0]; |
488 |
+ |
cosp = tcos(d); |
489 |
+ |
sinp = tsin(d); |
490 |
+ |
if ((0. <= specjitter) & (specjitter < 1.)) |
491 |
+ |
rv[1] = 1.0 - specjitter*rv[1]; |
492 |
+ |
if (rv[1] <= FTINY) |
493 |
+ |
d = 1.0; |
494 |
+ |
else |
495 |
+ |
d = sqrt( np->alpha2 * -log(rv[1]) ); |
496 |
+ |
for (i = 0; i < 3; i++) |
497 |
+ |
sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]); |
498 |
+ |
/* sample rejection test */ |
499 |
+ |
if (DOT(sr.rdir, np->rp->ron) >= -FTINY) |
500 |
+ |
continue; |
501 |
+ |
normalize(sr.rdir); /* OK, normalize */ |
502 |
+ |
if (nstaken) /* multi-sampling */ |
503 |
+ |
rayclear(&sr); |
504 |
+ |
rayvalue(&sr); |
505 |
+ |
multcolor(sr.rcol, sr.rcoef); |
506 |
+ |
addcolor(np->rp->rcol, sr.rcol); |
507 |
+ |
++nstaken; |
508 |
+ |
} |
509 |
+ |
ndims--; |
510 |
+ |
} |
511 |
|
} |