8 |
|
#include "copyright.h" |
9 |
|
|
10 |
|
#include "ray.h" |
11 |
+ |
#include "otypes.h" |
12 |
|
#include "ambient.h" |
13 |
|
#include "source.h" |
14 |
|
#include "func.h" |
17 |
|
#include "pmapmat.h" |
18 |
|
|
19 |
|
/* |
20 |
< |
* Arguments to this material include optional diffuse colors. |
20 |
> |
* Arguments to this material include optional diffuse colors. |
21 |
|
* String arguments include the BSDF and function files. |
22 |
< |
* A non-zero thickness causes the strange but useful behavior |
22 |
> |
* For the MAT_BSDF type, a non-zero thickness causes the useful behavior |
23 |
|
* of translating transmitted rays this distance beneath the surface |
24 |
|
* (opposite the surface normal) to bypass any intervening geometry. |
25 |
|
* Translation only affects scattered, non-source-directed samples. |
36 |
|
* hides geometry in front of the surface when rays hit from behind, |
37 |
|
* and applies only the transmission and backside reflectance properties. |
38 |
|
* Reflection is ignored on the hidden side, as those rays pass through. |
39 |
< |
* When thickness is set to zero, shadow rays will be blocked unless |
40 |
< |
* a BTDF has a strong "through" component in the source direction. |
39 |
> |
* For the MAT_ABSDF type, we check for a strong "through" component. |
40 |
> |
* Such a component will cause direct rays to pass through unscattered. |
41 |
|
* A separate test prevents over-counting by dropping samples that are |
42 |
|
* too close to this "through" direction. BSDFs with such a through direction |
43 |
|
* will also have a view component, meaning they are somewhat see-through. |
44 |
+ |
* A MAT_BSDF type with zero thickness behaves the same as a MAT_ABSDF |
45 |
+ |
* type with no strong through component. |
46 |
|
* The "up" vector for the BSDF is given by three variables, defined |
47 |
|
* (along with the thickness) by the named function file, or '.' if none. |
48 |
|
* Together with the surface normal, this defines the local coordinate |
55 |
|
* not multiplied. However, patterns affect this material as a multiplier |
56 |
|
* on everything except non-diffuse reflection. |
57 |
|
* |
58 |
+ |
* Arguments for MAT_ABSDF are: |
59 |
+ |
* 5+ BSDFfile ux uy uz funcfile transform |
60 |
+ |
* 0 |
61 |
+ |
* 0|3|6|9 rdf gdf bdf |
62 |
+ |
* rdb gdb bdb |
63 |
+ |
* rdt gdt bdt |
64 |
+ |
* |
65 |
|
* Arguments for MAT_BSDF are: |
66 |
|
* 6+ thick BSDFfile ux uy uz funcfile transform |
67 |
|
* 0 |
86 |
|
RREAL toloc[3][3]; /* world to local BSDF coords */ |
87 |
|
RREAL fromloc[3][3]; /* local BSDF coords to world */ |
88 |
|
double thick; /* surface thickness */ |
89 |
< |
COLOR cthru; /* "through" component multiplier */ |
89 |
> |
COLOR cthru; /* "through" component for MAT_ABSDF */ |
90 |
|
SDData *sd; /* loaded BSDF data */ |
91 |
|
COLOR rdiff; /* diffuse reflection */ |
92 |
|
COLOR runsamp; /* BSDF hemispherical reflection */ |
96 |
|
|
97 |
|
#define cvt_sdcolor(cv, svp) ccy2rgb(&(svp)->spec, (svp)->cieY, cv) |
98 |
|
|
99 |
< |
/* Compute "through" component color */ |
99 |
> |
/* Compute "through" component color for MAT_ABSDF */ |
100 |
|
static void |
101 |
|
compute_through(BSDFDAT *ndp) |
102 |
|
{ |
124 |
|
int i; |
125 |
|
SDError ec; |
126 |
|
|
117 |
– |
setcolor(ndp->cthru, 0, 0, 0); /* starting assumption */ |
118 |
– |
|
127 |
|
if (ndp->pr->rod > 0) |
128 |
|
dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb; |
129 |
|
else |
162 |
|
goto baderror; |
163 |
|
if (tomega > 1.5*dfp->minProjSA) |
164 |
|
return; /* not really a peak? */ |
157 |
– |
tomega /= fabs(pdir[2]); /* remove cosine factor */ |
165 |
|
if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .001) |
166 |
|
return; /* < 0.1% transmission */ |
167 |
|
for (i = 3; i--; ) /* remove peak from average */ |
235 |
|
diffY = 0; |
236 |
|
setcolor(cdiff, 0, 0, 0); |
237 |
|
} |
238 |
< |
/* need projected solid angles */ |
238 |
> |
/* need projected solid angle */ |
239 |
|
omega *= fabs(vsrc[2]); |
233 |
– |
ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd); |
234 |
– |
if (ec) |
235 |
– |
goto baderror; |
240 |
|
/* check indirect over-counting */ |
241 |
|
if ((vsrc[2] > 0) ^ (ndp->vray[2] > 0) && bright(ndp->cthru) > FTINY) { |
242 |
< |
double dx = vsrc[0] + ndp->vray[0]; |
243 |
< |
double dy = vsrc[1] + ndp->vray[1]; |
244 |
< |
if (dx*dx + dy*dy <= (1.5*4./PI)*(omega + tomega + |
245 |
< |
2.*sqrt(omega*tomega))) |
242 |
> |
double dx = vsrc[0] + ndp->vray[0]; |
243 |
> |
double dy = vsrc[1] + ndp->vray[1]; |
244 |
> |
SDSpectralDF *dfp = (ndp->pr->rod > 0) ? |
245 |
> |
((ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb) : |
246 |
> |
((ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf) ; |
247 |
> |
|
248 |
> |
if (dx*dx + dy*dy <= (2.5*4./PI)*(omega + dfp->minProjSA + |
249 |
> |
2.*sqrt(omega*dfp->minProjSA))) |
250 |
|
return(0); |
251 |
|
} |
252 |
+ |
ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd); |
253 |
+ |
if (ec) |
254 |
+ |
goto baderror; |
255 |
|
/* assign number of samples */ |
256 |
|
sf = specjitter * ndp->pr->rweight; |
257 |
|
if (tomega <= 0) |
502 |
|
multcolor(sr.rcoef, ndp->pr->pcol); |
503 |
|
if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) { |
504 |
|
if (!n & (nstarget > 1)) { |
505 |
+ |
n = nstarget; /* avoid infinitue loop */ |
506 |
|
nstarget = nstarget*sr.rweight/minweight; |
507 |
+ |
if (n == nstarget) break; |
508 |
|
n = -1; /* moved target */ |
509 |
|
} |
510 |
|
continue; /* try again */ |
555 |
|
rayvalue(&tr); |
556 |
|
multcolor(tr.rcol, tr.rcoef); |
557 |
|
addcolor(ndp->pr->rcol, tr.rcol); |
558 |
+ |
ndp->pr->rxt = ndp->pr->rot + raydistance(&tr); |
559 |
|
++ntotal; |
560 |
|
b = bright(ndp->cthru); |
561 |
|
} else |
593 |
|
int |
594 |
|
m_bsdf(OBJREC *m, RAY *r) |
595 |
|
{ |
596 |
+ |
int hasthick = (m->otype == MAT_BSDF); |
597 |
|
int hitfront; |
598 |
|
COLOR ctmp; |
599 |
|
SDError ec; |
601 |
|
MFUNC *mf; |
602 |
|
BSDFDAT nd; |
603 |
|
/* check arguments */ |
604 |
< |
if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) | |
604 |
> |
if ((m->oargs.nsargs < hasthick+5) | (m->oargs.nfargs > 9) | |
605 |
|
(m->oargs.nfargs % 3)) |
606 |
|
objerror(m, USER, "bad # arguments"); |
607 |
|
/* record surface struck */ |
608 |
|
hitfront = (r->rod > 0); |
609 |
|
/* load cal file */ |
610 |
< |
mf = getfunc(m, 5, 0x1d, 1); |
610 |
> |
mf = hasthick ? getfunc(m, 5, 0x1d, 1) |
611 |
> |
: getfunc(m, 4, 0xe, 1) ; |
612 |
|
setfunc(m, r); |
613 |
< |
/* get thickness */ |
614 |
< |
nd.thick = evalue(mf->ep[0]); |
615 |
< |
if ((-FTINY <= nd.thick) & (nd.thick <= FTINY)) |
616 |
< |
nd.thick = 0; |
613 |
> |
nd.thick = 0; /* set thickness */ |
614 |
> |
if (hasthick) { |
615 |
> |
nd.thick = evalue(mf->ep[0]); |
616 |
> |
if ((-FTINY <= nd.thick) & (nd.thick <= FTINY)) |
617 |
> |
nd.thick = 0; |
618 |
> |
} |
619 |
|
/* check backface visibility */ |
620 |
|
if (!hitfront & !backvis) { |
621 |
|
raytrans(r); |
628 |
|
raytrans(r); /* hide our proxy */ |
629 |
|
return(1); |
630 |
|
} |
631 |
+ |
if (hasthick && r->crtype & SHADOW) /* early shadow check #1 */ |
632 |
+ |
return(1); |
633 |
|
nd.mp = m; |
634 |
|
nd.pr = r; |
635 |
|
/* get BSDF data */ |
636 |
< |
nd.sd = loadBSDF(m->oargs.sarg[1]); |
637 |
< |
/* early shadow check */ |
638 |
< |
if (r->crtype & SHADOW && (nd.sd->tf == NULL) & (nd.sd->tb == NULL)) |
636 |
> |
nd.sd = loadBSDF(m->oargs.sarg[hasthick]); |
637 |
> |
/* early shadow check #2 */ |
638 |
> |
if (r->crtype & SHADOW && (nd.sd->tf == NULL) & (nd.sd->tb == NULL)) { |
639 |
> |
SDfreeCache(nd.sd); |
640 |
|
return(1); |
641 |
+ |
} |
642 |
|
/* diffuse reflectance */ |
643 |
|
if (hitfront) { |
644 |
|
cvt_sdcolor(nd.rdiff, &nd.sd->rLambFront); |
671 |
|
multcolor(nd.rdiff, r->pcol); |
672 |
|
multcolor(nd.tdiff, r->pcol); |
673 |
|
/* get up vector */ |
674 |
< |
upvec[0] = evalue(mf->ep[1]); |
675 |
< |
upvec[1] = evalue(mf->ep[2]); |
676 |
< |
upvec[2] = evalue(mf->ep[3]); |
674 |
> |
upvec[0] = evalue(mf->ep[hasthick+0]); |
675 |
> |
upvec[1] = evalue(mf->ep[hasthick+1]); |
676 |
> |
upvec[2] = evalue(mf->ep[hasthick+2]); |
677 |
|
/* return to world coords */ |
678 |
|
if (mf->fxp != &unitxf) { |
679 |
|
multv3(upvec, upvec, mf->fxp->xfm); |
694 |
|
} |
695 |
|
if (ec) { |
696 |
|
objerror(m, WARNING, "Illegal orientation vector"); |
697 |
+ |
SDfreeCache(nd.sd); |
698 |
|
return(1); |
699 |
|
} |
700 |
< |
compute_through(&nd); /* compute through component */ |
701 |
< |
if (r->crtype & SHADOW) { |
702 |
< |
RAY tr; /* attempt to pass shadow ray */ |
703 |
< |
if (rayorigin(&tr, TRANS, r, nd.cthru) < 0) |
704 |
< |
return(1); /* no through component */ |
705 |
< |
VCOPY(tr.rdir, r->rdir); |
706 |
< |
rayvalue(&tr); /* transmit with scaling */ |
707 |
< |
multcolor(tr.rcol, tr.rcoef); |
708 |
< |
copycolor(r->rcol, tr.rcol); |
709 |
< |
return(1); /* we're done */ |
700 |
> |
setcolor(nd.cthru, 0, 0, 0); /* consider through component */ |
701 |
> |
if (m->otype == MAT_ABSDF) { |
702 |
> |
compute_through(&nd); |
703 |
> |
if (r->crtype & SHADOW) { |
704 |
> |
RAY tr; /* attempt to pass shadow ray */ |
705 |
> |
SDfreeCache(nd.sd); |
706 |
> |
if (rayorigin(&tr, TRANS, r, nd.cthru) < 0) |
707 |
> |
return(1); /* no through component */ |
708 |
> |
VCOPY(tr.rdir, r->rdir); |
709 |
> |
rayvalue(&tr); /* transmit with scaling */ |
710 |
> |
multcolor(tr.rcol, tr.rcoef); |
711 |
> |
copycolor(r->rcol, tr.rcol); |
712 |
> |
return(1); /* we're done */ |
713 |
> |
} |
714 |
|
} |
715 |
|
ec = SDinvXform(nd.fromloc, nd.toloc); |
716 |
|
if (!ec) /* determine BSDF resolution */ |