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 */ |
66 |
|
double pdot; /* perturbed dot product */ |
67 |
|
} NORMDAT; /* normal material data */ |
68 |
|
|
69 |
< |
static srcdirf_t dirnorm; |
69 |
< |
static void gaussamp(RAY *r, NORMDAT *np); |
69 |
> |
static void gaussamp(NORMDAT *np); |
70 |
|
|
71 |
|
|
72 |
|
static void |
73 |
|
dirnorm( /* compute source contribution */ |
74 |
|
COLOR cval, /* returned coefficient */ |
75 |
< |
void *nnp, /* material data */ |
75 |
> |
void *nnp, /* material data */ |
76 |
|
FVECT ldir, /* light source direction */ |
77 |
|
double omega /* light source size */ |
78 |
|
) |
79 |
|
{ |
80 |
< |
register NORMDAT *np = nnp; |
80 |
> |
NORMDAT *np = nnp; |
81 |
|
double ldot; |
82 |
|
double lrdiff, ltdiff; |
83 |
|
double dtmp, d2, d3, d4; |
112 |
|
scalecolor(ctmp, dtmp); |
113 |
|
addcolor(cval, ctmp); |
114 |
|
} |
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 |
137 |
|
if (np->specfl & SP_FLAT) |
138 |
|
dtmp += omega * (0.25/PI); |
139 |
|
/* half vector */ |
140 |
< |
vtmp[0] = ldir[0] - np->rp->rdir[0]; |
127 |
< |
vtmp[1] = ldir[1] - np->rp->rdir[1]; |
128 |
< |
vtmp[2] = ldir[2] - np->rp->rdir[2]; |
140 |
> |
VSUB(vtmp, ldir, np->rp->rdir); |
141 |
|
d2 = DOT(vtmp, np->pnorm); |
142 |
|
d2 *= d2; |
143 |
|
d3 = DOT(vtmp,vtmp); |
152 |
|
addcolor(cval, ctmp); |
153 |
|
} |
154 |
|
} |
155 |
< |
if (ldot < -FTINY && ltdiff > FTINY) { |
156 |
< |
/* |
145 |
< |
* Compute diffuse transmission. |
146 |
< |
*/ |
147 |
< |
copycolor(ctmp, np->mcolor); |
148 |
< |
dtmp = -ldot * omega * ltdiff * (1.0/PI); |
149 |
< |
scalecolor(ctmp, dtmp); |
150 |
< |
addcolor(cval, ctmp); |
151 |
< |
} |
155 |
> |
|
156 |
> |
|
157 |
|
if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) { |
158 |
|
/* |
159 |
|
* Compute specular transmission. Specular transmission |
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
< |
extern int |
177 |
> |
int |
178 |
|
m_normal( /* color a ray that hit something normal */ |
179 |
< |
register OBJREC *m, |
180 |
< |
register RAY *r |
179 |
> |
OBJREC *m, |
180 |
> |
RAY *r |
181 |
|
) |
182 |
|
{ |
183 |
|
NORMDAT nd; |
187 |
|
int hastexture; |
188 |
|
double d; |
189 |
|
COLOR ctmp; |
190 |
< |
register int i; |
190 |
> |
int i; |
191 |
> |
|
192 |
> |
/* PMAP: skip transmitted shadow ray if accounted for in photon map */ |
193 |
> |
if (shadowRayInPmap(r)) |
194 |
> |
return(1); |
195 |
|
/* easy shadow test */ |
196 |
|
if (r->crtype & SHADOW && m->otype != MAT_TRANS) |
197 |
|
return(1); |
200 |
|
objerror(m, USER, "bad number of arguments"); |
201 |
|
/* check for back side */ |
202 |
|
if (r->rod < 0.0) { |
203 |
< |
if (!backvis && m->otype != MAT_TRANS) { |
203 |
> |
if (!backvis) { |
204 |
|
raytrans(r); |
205 |
|
return(1); |
206 |
|
} |
236 |
|
nd.rspec = m->oargs.farg[3]; |
237 |
|
/* compute Fresnel approx. */ |
238 |
|
if (nd.specfl & SP_PURE && nd.rspec >= FRESTHRESH) { |
239 |
< |
fest = FRESNE(r->rod); |
239 |
> |
fest = FRESNE(nd.pdot); |
240 |
|
nd.rspec += fest*(1. - nd.rspec); |
241 |
|
} else |
242 |
|
fest = 0.; |
251 |
|
if (!(nd.specfl & SP_PURE) && |
252 |
|
specthresh >= nd.tspec-FTINY) |
253 |
|
nd.specfl |= SP_TBLT; |
254 |
< |
if (!hastexture || r->crtype & SHADOW) { |
254 |
> |
if (!hastexture || r->crtype & (SHADOW|AMBIENT)) { |
255 |
|
VCOPY(nd.prdir, r->rdir); |
256 |
|
transtest = 2; |
257 |
|
} else { |
266 |
|
} else |
267 |
|
nd.tdiff = nd.tspec = nd.trans = 0.0; |
268 |
|
/* transmitted ray */ |
269 |
+ |
|
270 |
|
if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) { |
271 |
|
RAY lr; |
272 |
|
copycolor(lr.rcoef, nd.mcolor); /* modified by color */ |
293 |
|
if (m->otype != MAT_METAL) { |
294 |
|
setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec); |
295 |
|
} else if (fest > FTINY) { |
296 |
< |
d = nd.rspec*(1. - fest); |
296 |
> |
d = m->oargs.farg[3]*(1. - fest); |
297 |
|
for (i = 0; i < 3; i++) |
298 |
< |
nd.scolor[i] = fest + nd.mcolor[i]*d; |
298 |
> |
colval(nd.scolor,i) = fest + |
299 |
> |
colval(nd.mcolor,i)*d; |
300 |
|
} else { |
301 |
|
copycolor(nd.scolor, nd.mcolor); |
302 |
|
scalecolor(nd.scolor, nd.rspec); |
319 |
|
rayvalue(&lr); |
320 |
|
multcolor(lr.rcol, lr.rcoef); |
321 |
|
addcolor(r->rcol, lr.rcol); |
322 |
< |
if (!hastexture && nd.specfl & SP_FLAT) { |
322 |
> |
if (nd.specfl & SP_FLAT && |
323 |
> |
!hastexture | (r->crtype & AMBIENT)) { |
324 |
|
mirtest = 2.*bright(lr.rcol); |
325 |
|
mirdist = r->rot + lr.rt; |
326 |
|
} |
333 |
|
return(1); /* 100% pure specular */ |
334 |
|
|
335 |
|
if (!(nd.specfl & SP_PURE)) |
336 |
< |
gaussamp(r, &nd); /* checks *BLT flags */ |
336 |
> |
gaussamp(&nd); /* checks *BLT flags */ |
337 |
|
|
338 |
|
if (nd.rdiff > FTINY) { /* ambient from this side */ |
339 |
|
copycolor(ctmp, nd.mcolor); /* modified by material color */ |
340 |
< |
if (nd.specfl & SP_RBLT) |
341 |
< |
scalecolor(ctmp, 1.0-nd.trans); |
342 |
< |
else |
331 |
< |
scalecolor(ctmp, nd.rdiff); |
340 |
> |
scalecolor(ctmp, nd.rdiff); |
341 |
> |
if (nd.specfl & SP_RBLT) /* add in specular as well? */ |
342 |
> |
addcolor(ctmp, nd.scolor); |
343 |
|
multambient(ctmp, r, hastexture ? nd.pnorm : r->ron); |
344 |
|
addcolor(r->rcol, ctmp); /* add to returned color */ |
345 |
|
} |
376 |
|
|
377 |
|
static void |
378 |
|
gaussamp( /* sample Gaussian specular */ |
379 |
< |
RAY *r, |
369 |
< |
register NORMDAT *np |
379 |
> |
NORMDAT *np |
380 |
|
) |
381 |
|
{ |
382 |
|
RAY sr; |
383 |
|
FVECT u, v, h; |
384 |
|
double rv[2]; |
385 |
|
double d, sinp, cosp; |
386 |
< |
COLOR scol; |
386 |
> |
COLOR scol; |
387 |
|
int maxiter, ntrials, nstarget, nstaken; |
388 |
< |
register int i; |
388 |
> |
int i; |
389 |
|
/* quick test */ |
390 |
|
if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL && |
391 |
|
(np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN) |
392 |
|
return; |
393 |
|
/* set up sample coordinates */ |
394 |
< |
v[0] = v[1] = v[2] = 0.0; |
385 |
< |
for (i = 0; i < 3; i++) |
386 |
< |
if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6) |
387 |
< |
break; |
388 |
< |
v[i] = 1.0; |
389 |
< |
fcross(u, v, np->pnorm); |
390 |
< |
normalize(u); |
394 |
> |
getperpendicular(u, np->pnorm, rand_samp); |
395 |
|
fcross(v, np->pnorm, u); |
396 |
|
/* compute reflection */ |
397 |
|
if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL && |
398 |
< |
rayorigin(&sr, SPECULAR, r, np->scolor) == 0) { |
398 |
> |
rayorigin(&sr, SPECULAR, np->rp, np->scolor) == 0) { |
399 |
|
nstarget = 1; |
400 |
|
if (specjitter > 1.5) { /* multiple samples? */ |
401 |
< |
nstarget = specjitter*r->rweight + .5; |
401 |
> |
nstarget = specjitter*np->rp->rweight + .5; |
402 |
|
if (sr.rweight <= minweight*nstarget) |
403 |
|
nstarget = sr.rweight/minweight; |
404 |
|
if (nstarget > 1) { |
429 |
|
d = sqrt( np->alpha2 * -log(rv[1]) ); |
430 |
|
for (i = 0; i < 3; i++) |
431 |
|
h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]); |
432 |
< |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
433 |
< |
VSUM(sr.rdir, r->rdir, h, d); |
432 |
> |
d = -2.0 * DOT(h, np->rp->rdir) / (1.0 + d*d); |
433 |
> |
VSUM(sr.rdir, np->rp->rdir, h, d); |
434 |
|
/* sample rejection test */ |
435 |
< |
if ((d = DOT(sr.rdir, r->ron)) <= FTINY) |
435 |
> |
if ((d = DOT(sr.rdir, np->rp->ron)) <= FTINY) |
436 |
|
continue; |
437 |
|
checknorm(sr.rdir); |
438 |
|
if (nstarget > 1) { /* W-G-M-D adjustment */ |
439 |
|
if (nstaken) rayclear(&sr); |
440 |
|
rayvalue(&sr); |
441 |
< |
d = 2./(1. + r->rod/d); |
441 |
> |
d = 2./(1. + np->rp->rod/d); |
442 |
|
scalecolor(sr.rcol, d); |
443 |
|
addcolor(scol, sr.rcol); |
444 |
|
} else { |
445 |
|
rayvalue(&sr); |
446 |
|
multcolor(sr.rcol, sr.rcoef); |
447 |
< |
addcolor(r->rcol, sr.rcol); |
447 |
> |
addcolor(np->rp->rcol, sr.rcol); |
448 |
|
} |
449 |
|
++nstaken; |
450 |
|
} |
452 |
|
multcolor(scol, sr.rcoef); |
453 |
|
d = (double)nstarget/ntrials; |
454 |
|
scalecolor(scol, d); |
455 |
< |
addcolor(r->rcol, scol); |
455 |
> |
addcolor(np->rp->rcol, scol); |
456 |
|
} |
457 |
|
ndims--; |
458 |
|
} |
460 |
|
copycolor(sr.rcoef, np->mcolor); /* modified by color */ |
461 |
|
scalecolor(sr.rcoef, np->tspec); |
462 |
|
if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN && |
463 |
< |
rayorigin(&sr, SPECULAR, r, sr.rcoef) == 0) { |
463 |
> |
rayorigin(&sr, SPECULAR, np->rp, sr.rcoef) == 0) { |
464 |
|
nstarget = 1; |
465 |
|
if (specjitter > 1.5) { /* multiple samples? */ |
466 |
< |
nstarget = specjitter*r->rweight + .5; |
466 |
> |
nstarget = specjitter*np->rp->rweight + .5; |
467 |
|
if (sr.rweight <= minweight*nstarget) |
468 |
|
nstarget = sr.rweight/minweight; |
469 |
|
if (nstarget > 1) { |
494 |
|
for (i = 0; i < 3; i++) |
495 |
|
sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]); |
496 |
|
/* sample rejection test */ |
497 |
< |
if (DOT(sr.rdir, r->ron) >= -FTINY) |
497 |
> |
if (DOT(sr.rdir, np->rp->ron) >= -FTINY) |
498 |
|
continue; |
499 |
|
normalize(sr.rdir); /* OK, normalize */ |
500 |
|
if (nstaken) /* multi-sampling */ |
501 |
|
rayclear(&sr); |
502 |
|
rayvalue(&sr); |
503 |
|
multcolor(sr.rcol, sr.rcoef); |
504 |
< |
addcolor(r->rcol, sr.rcol); |
504 |
> |
addcolor(np->rp->rcol, sr.rcol); |
505 |
|
++nstaken; |
506 |
|
} |
507 |
|
ndims--; |