| 13 |
|
#include "func.h" |
| 14 |
|
#include "bsdf.h" |
| 15 |
|
#include "random.h" |
| 16 |
+ |
#include "pmapmat.h" |
| 17 |
|
|
| 18 |
|
/* |
| 19 |
|
* Arguments to this material include optional diffuse colors. |
| 23 |
|
* (opposite the surface normal) to bypass any intervening geometry. |
| 24 |
|
* Translation only affects scattered, non-source-directed samples. |
| 25 |
|
* A non-zero thickness has the further side-effect that an unscattered |
| 26 |
< |
* (view) ray will pass right through our material if it has any |
| 27 |
< |
* non-diffuse transmission, making the BSDF surface invisible. This |
| 28 |
< |
* shows the proxied geometry instead. Thickness has the further |
| 29 |
< |
* effect of turning off reflection on the hidden side so that rays |
| 29 |
< |
* heading in the opposite direction pass unimpeded through the BSDF |
| 26 |
> |
* (view) ray will pass right through our material, making the BSDF |
| 27 |
> |
* surface invisible and showing the proxied geometry instead. Thickness |
| 28 |
> |
* has the further effect of turning off reflection on the reverse side so |
| 29 |
> |
* rays heading in the opposite direction pass unimpeded through the BSDF |
| 30 |
|
* surface. A paired surface may be placed on the opposide side of |
| 31 |
|
* the detail geometry, less than this thickness away, if a two-way |
| 32 |
|
* proxy is desired. Note that the sign of the thickness is important. |
| 35 |
|
* hides geometry in front of the surface when rays hit from behind, |
| 36 |
|
* and applies only the transmission and backside reflectance properties. |
| 37 |
|
* Reflection is ignored on the hidden side, as those rays pass through. |
| 38 |
+ |
* When thickness is set to zero, shadow rays will be blocked unless |
| 39 |
+ |
* a BTDF has a strong "through" component in the source direction. |
| 40 |
+ |
* A separate test prevents over-counting by dropping specular & ambient |
| 41 |
+ |
* samples that are too close to this "through" direction. The same |
| 42 |
+ |
* restriction applies for the proxy case (thickness != 0). |
| 43 |
|
* The "up" vector for the BSDF is given by three variables, defined |
| 44 |
|
* (along with the thickness) by the named function file, or '.' if none. |
| 45 |
|
* Together with the surface normal, this defines the local coordinate |
| 47 |
|
* We do not reorient the surface, so if the BSDF has no back-side |
| 48 |
|
* reflectance and none is given in the real arguments, a BSDF surface |
| 49 |
|
* with zero thickness will appear black when viewed from behind |
| 50 |
< |
* unless backface visibility is off. |
| 50 |
> |
* unless backface visibility is on, when it becomes invisible. |
| 51 |
|
* The diffuse arguments are added to components in the BSDF file, |
| 52 |
|
* not multiplied. However, patterns affect this material as a multiplier |
| 53 |
|
* on everything except non-diffuse reflection. |
| 55 |
|
* Arguments for MAT_BSDF are: |
| 56 |
|
* 6+ thick BSDFfile ux uy uz funcfile transform |
| 57 |
|
* 0 |
| 58 |
< |
* 0|3|9 rdf gdf bdf |
| 58 |
> |
* 0|3|6|9 rdf gdf bdf |
| 59 |
|
* rdb gdb bdb |
| 60 |
|
* rdt gdt bdt |
| 61 |
|
*/ |
| 63 |
|
/* |
| 64 |
|
* Note that our reverse ray-tracing process means that the positions |
| 65 |
|
* of incoming and outgoing vectors may be reversed in our calls |
| 66 |
< |
* to the BSDF library. This is fine, since the bidirectional nature |
| 66 |
> |
* to the BSDF library. This is usually fine, since the bidirectional nature |
| 67 |
|
* of the BSDF (that's what the 'B' stands for) means it all works out. |
| 68 |
|
*/ |
| 69 |
|
|
| 72 |
|
RAY *pr; /* intersected ray */ |
| 73 |
|
FVECT pnorm; /* perturbed surface normal */ |
| 74 |
|
FVECT vray; /* local outgoing (return) vector */ |
| 75 |
< |
double sr_vpsa; /* sqrt of BSDF projected solid angle */ |
| 75 |
> |
double sr_vpsa[2]; /* sqrt of BSDF projected solid angle extrema */ |
| 76 |
|
RREAL toloc[3][3]; /* world to local BSDF coords */ |
| 77 |
|
RREAL fromloc[3][3]; /* local BSDF coords to world */ |
| 78 |
|
double thick; /* surface thickness */ |
| 79 |
+ |
COLOR cthru; /* "through" component multiplier */ |
| 80 |
|
SDData *sd; /* loaded BSDF data */ |
| 81 |
+ |
COLOR rdiff; /* diffuse reflection */ |
| 82 |
|
COLOR runsamp; /* BSDF hemispherical reflection */ |
| 83 |
< |
COLOR rdiff; /* added diffuse reflection */ |
| 83 |
> |
COLOR tdiff; /* diffuse transmission */ |
| 84 |
|
COLOR tunsamp; /* BSDF hemispherical transmission */ |
| 78 |
– |
COLOR tdiff; /* added diffuse transmission */ |
| 85 |
|
} BSDFDAT; /* BSDF material data */ |
| 86 |
|
|
| 87 |
|
#define cvt_sdcolor(cv, svp) ccy2rgb(&(svp)->spec, (svp)->cieY, cv) |
| 88 |
|
|
| 89 |
< |
/* Jitter ray sample according to projected solid angle and specjitter */ |
| 89 |
> |
/* Compute "through" component color */ |
| 90 |
|
static void |
| 91 |
< |
bsdf_jitter(FVECT vres, BSDFDAT *ndp) |
| 91 |
> |
compute_through(BSDFDAT *ndp) |
| 92 |
|
{ |
| 93 |
< |
double sr_psa = ndp->sr_vpsa; |
| 93 |
> |
#define NDIR2CHECK 13 |
| 94 |
> |
static const float dir2check[NDIR2CHECK][2] = { |
| 95 |
> |
{0, 0}, |
| 96 |
> |
{-0.8, 0}, |
| 97 |
> |
{0, 0.8}, |
| 98 |
> |
{0, -0.8}, |
| 99 |
> |
{0.8, 0}, |
| 100 |
> |
{-0.8, 0.8}, |
| 101 |
> |
{-0.8, -0.8}, |
| 102 |
> |
{0.8, 0.8}, |
| 103 |
> |
{0.8, -0.8}, |
| 104 |
> |
{-1.6, 0}, |
| 105 |
> |
{0, 1.6}, |
| 106 |
> |
{0, -1.6}, |
| 107 |
> |
{1.6, 0}, |
| 108 |
> |
}; |
| 109 |
> |
const double peak_over = 2.0; |
| 110 |
> |
SDSpectralDF *dfp; |
| 111 |
> |
FVECT pdir; |
| 112 |
> |
double tomega, srchrad; |
| 113 |
> |
COLOR vpeak, vsum; |
| 114 |
> |
int nsum, i; |
| 115 |
> |
SDError ec; |
| 116 |
|
|
| 117 |
+ |
setcolor(ndp->cthru, .0, .0, .0); /* starting assumption */ |
| 118 |
+ |
|
| 119 |
+ |
if (!(ndp->pr->crtype & (SPECULAR|AMBIENT|SHADOW))) |
| 120 |
+ |
return; /* simply don't need to know */ |
| 121 |
+ |
|
| 122 |
+ |
if (ndp->pr->rod > 0) |
| 123 |
+ |
dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb; |
| 124 |
+ |
else |
| 125 |
+ |
dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf; |
| 126 |
+ |
|
| 127 |
+ |
if (dfp == NULL) |
| 128 |
+ |
return; /* no specular transmission */ |
| 129 |
+ |
if (bright(ndp->pr->pcol) <= FTINY) |
| 130 |
+ |
return; /* pattern is black, here */ |
| 131 |
+ |
srchrad = sqrt(dfp->minProjSA); /* else search for peak */ |
| 132 |
+ |
setcolor(vpeak, .0, .0, .0); |
| 133 |
+ |
setcolor(vsum, .0, .0, .0); |
| 134 |
+ |
nsum = 0; |
| 135 |
+ |
for (i = 0; i < NDIR2CHECK; i++) { |
| 136 |
+ |
FVECT tdir; |
| 137 |
+ |
SDValue sv; |
| 138 |
+ |
COLOR vcol; |
| 139 |
+ |
tdir[0] = -ndp->vray[0] + dir2check[i][0]*srchrad; |
| 140 |
+ |
tdir[1] = -ndp->vray[1] + dir2check[i][1]*srchrad; |
| 141 |
+ |
tdir[2] = -ndp->vray[2]; |
| 142 |
+ |
normalize(tdir); |
| 143 |
+ |
ec = SDevalBSDF(&sv, tdir, ndp->vray, ndp->sd); |
| 144 |
+ |
if (ec) |
| 145 |
+ |
goto baderror; |
| 146 |
+ |
cvt_sdcolor(vcol, &sv); |
| 147 |
+ |
addcolor(vsum, vcol); |
| 148 |
+ |
++nsum; |
| 149 |
+ |
if (bright(vcol) > bright(vpeak)) { |
| 150 |
+ |
copycolor(vpeak, vcol); |
| 151 |
+ |
VCOPY(pdir, tdir); |
| 152 |
+ |
} |
| 153 |
+ |
} |
| 154 |
+ |
ec = SDsizeBSDF(&tomega, pdir, ndp->vray, SDqueryMin, ndp->sd); |
| 155 |
+ |
if (ec) |
| 156 |
+ |
goto baderror; |
| 157 |
+ |
if (tomega > 1.5*dfp->minProjSA) |
| 158 |
+ |
return; /* not really a peak? */ |
| 159 |
+ |
if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .007) |
| 160 |
+ |
return; /* < 0.7% transmission */ |
| 161 |
+ |
for (i = 3; i--; ) /* remove peak from average */ |
| 162 |
+ |
colval(vsum,i) -= colval(vpeak,i); |
| 163 |
+ |
--nsum; |
| 164 |
+ |
if (peak_over*bright(vsum) >= nsum*bright(vpeak)) |
| 165 |
+ |
return; /* not peaky enough */ |
| 166 |
+ |
copycolor(ndp->cthru, vpeak); /* else use it */ |
| 167 |
+ |
scalecolor(ndp->cthru, tomega); |
| 168 |
+ |
multcolor(ndp->cthru, ndp->pr->pcol); /* modify by pattern */ |
| 169 |
+ |
return; |
| 170 |
+ |
baderror: |
| 171 |
+ |
objerror(ndp->mp, USER, transSDError(ec)); |
| 172 |
+ |
#undef NDIR2CHECK |
| 173 |
+ |
} |
| 174 |
+ |
|
| 175 |
+ |
/* Jitter ray sample according to projected solid angle and specjitter */ |
| 176 |
+ |
static void |
| 177 |
+ |
bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa) |
| 178 |
+ |
{ |
| 179 |
|
VCOPY(vres, ndp->vray); |
| 180 |
|
if (specjitter < 1.) |
| 181 |
|
sr_psa *= specjitter; |
| 186 |
|
normalize(vres); |
| 187 |
|
} |
| 188 |
|
|
| 189 |
+ |
/* Get BSDF specular for direct component, returning true if OK to proceed */ |
| 190 |
+ |
static int |
| 191 |
+ |
direct_specular_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp) |
| 192 |
+ |
{ |
| 193 |
+ |
int nsamp, ok = 0; |
| 194 |
+ |
FVECT vsrc, vsmp, vjit; |
| 195 |
+ |
double tomega, tomega2; |
| 196 |
+ |
double sf, tsr, sd[2]; |
| 197 |
+ |
COLOR csmp, cdiff; |
| 198 |
+ |
double diffY; |
| 199 |
+ |
SDValue sv; |
| 200 |
+ |
SDError ec; |
| 201 |
+ |
int i; |
| 202 |
+ |
/* in case we fail */ |
| 203 |
+ |
setcolor(cval, .0, .0, .0); |
| 204 |
+ |
/* transform source direction */ |
| 205 |
+ |
if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone) |
| 206 |
+ |
return(0); |
| 207 |
+ |
/* will discount diffuse portion */ |
| 208 |
+ |
switch ((vsrc[2] > 0)<<1 | (ndp->vray[2] > 0)) { |
| 209 |
+ |
case 3: |
| 210 |
+ |
if (ndp->sd->rf == NULL) |
| 211 |
+ |
return(0); /* all diffuse */ |
| 212 |
+ |
sv = ndp->sd->rLambFront; |
| 213 |
+ |
break; |
| 214 |
+ |
case 0: |
| 215 |
+ |
if (ndp->sd->rb == NULL) |
| 216 |
+ |
return(0); /* all diffuse */ |
| 217 |
+ |
sv = ndp->sd->rLambBack; |
| 218 |
+ |
break; |
| 219 |
+ |
default: |
| 220 |
+ |
if ((ndp->sd->tf == NULL) & (ndp->sd->tb == NULL)) |
| 221 |
+ |
return(0); /* all diffuse */ |
| 222 |
+ |
sv = ndp->sd->tLamb; |
| 223 |
+ |
break; |
| 224 |
+ |
} |
| 225 |
+ |
if (sv.cieY > FTINY) { |
| 226 |
+ |
diffY = sv.cieY *= 1./PI; |
| 227 |
+ |
cvt_sdcolor(cdiff, &sv); |
| 228 |
+ |
} else { |
| 229 |
+ |
diffY = .0; |
| 230 |
+ |
setcolor(cdiff, .0, .0, .0); |
| 231 |
+ |
} |
| 232 |
+ |
/* need projected solid angles */ |
| 233 |
+ |
omega *= fabs(vsrc[2]); |
| 234 |
+ |
ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd); |
| 235 |
+ |
if (ec) |
| 236 |
+ |
goto baderror; |
| 237 |
+ |
/* check indirect over-counting */ |
| 238 |
+ |
if (ndp->pr->crtype & (SPECULAR|AMBIENT) |
| 239 |
+ |
&& (vsrc[2] > 0) ^ (ndp->vray[2] > 0) |
| 240 |
+ |
&& bright(ndp->cthru) > FTINY) { |
| 241 |
+ |
double dx = vsrc[0] + ndp->vray[0]; |
| 242 |
+ |
double dy = vsrc[1] + ndp->vray[1]; |
| 243 |
+ |
if (dx*dx + dy*dy <= (4./PI)*(omega + tomega + |
| 244 |
+ |
2.*sqrt(omega*tomega))) |
| 245 |
+ |
return(0); |
| 246 |
+ |
} |
| 247 |
+ |
/* assign number of samples */ |
| 248 |
+ |
sf = specjitter * ndp->pr->rweight; |
| 249 |
+ |
if (tomega <= .0) |
| 250 |
+ |
nsamp = 1; |
| 251 |
+ |
else if (25.*tomega <= omega) |
| 252 |
+ |
nsamp = 100.*sf + .5; |
| 253 |
+ |
else |
| 254 |
+ |
nsamp = 4.*sf*omega/tomega + .5; |
| 255 |
+ |
nsamp += !nsamp; |
| 256 |
+ |
sf = sqrt(omega); /* sample our source area */ |
| 257 |
+ |
tsr = sqrt(tomega); |
| 258 |
+ |
for (i = nsamp; i--; ) { |
| 259 |
+ |
VCOPY(vsmp, vsrc); /* jitter query directions */ |
| 260 |
+ |
if (nsamp > 1) { |
| 261 |
+ |
multisamp(sd, 2, (i + frandom())/(double)nsamp); |
| 262 |
+ |
vsmp[0] += (sd[0] - .5)*sf; |
| 263 |
+ |
vsmp[1] += (sd[1] - .5)*sf; |
| 264 |
+ |
normalize(vsmp); |
| 265 |
+ |
} |
| 266 |
+ |
bsdf_jitter(vjit, ndp, tsr); |
| 267 |
+ |
/* compute BSDF */ |
| 268 |
+ |
ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd); |
| 269 |
+ |
if (ec) |
| 270 |
+ |
goto baderror; |
| 271 |
+ |
if (sv.cieY - diffY <= FTINY) |
| 272 |
+ |
continue; /* no specular part */ |
| 273 |
+ |
/* check for variable resolution */ |
| 274 |
+ |
ec = SDsizeBSDF(&tomega2, vjit, vsmp, SDqueryMin, ndp->sd); |
| 275 |
+ |
if (ec) |
| 276 |
+ |
goto baderror; |
| 277 |
+ |
if (tomega2 < .12*tomega) |
| 278 |
+ |
continue; /* not safe to include */ |
| 279 |
+ |
cvt_sdcolor(csmp, &sv); |
| 280 |
+ |
addcolor(cval, csmp); /* else average it in */ |
| 281 |
+ |
++ok; |
| 282 |
+ |
} |
| 283 |
+ |
if (!ok) /* no valid specular samples? */ |
| 284 |
+ |
return(0); |
| 285 |
+ |
|
| 286 |
+ |
sf = 1./(double)ok; /* compute average BSDF */ |
| 287 |
+ |
scalecolor(cval, sf); |
| 288 |
+ |
/* subtract diffuse contribution */ |
| 289 |
+ |
for (i = 3*(diffY > FTINY); i--; ) |
| 290 |
+ |
if ((colval(cval,i) -= colval(cdiff,i)) < .0) |
| 291 |
+ |
colval(cval,i) = .0; |
| 292 |
+ |
return(1); |
| 293 |
+ |
baderror: |
| 294 |
+ |
objerror(ndp->mp, USER, transSDError(ec)); |
| 295 |
+ |
return(0); /* gratis return */ |
| 296 |
+ |
} |
| 297 |
+ |
|
| 298 |
|
/* Compute source contribution for BSDF (reflected & transmitted) */ |
| 299 |
|
static void |
| 300 |
|
dir_bsdf( |
| 305 |
|
) |
| 306 |
|
{ |
| 307 |
|
BSDFDAT *np = (BSDFDAT *)nnp; |
| 109 |
– |
SDError ec; |
| 110 |
– |
SDValue sv; |
| 111 |
– |
FVECT vsrc; |
| 112 |
– |
FVECT vjit; |
| 308 |
|
double ldot; |
| 309 |
|
double dtmp; |
| 310 |
|
COLOR ctmp; |
| 315 |
|
if ((-FTINY <= ldot) & (ldot <= FTINY)) |
| 316 |
|
return; |
| 317 |
|
|
| 318 |
< |
if (ldot > .0 && bright(np->rdiff) > FTINY) { |
| 318 |
> |
if (ldot > 0 && bright(np->rdiff) > FTINY) { |
| 319 |
|
/* |
| 320 |
< |
* Compute added diffuse reflected component. |
| 320 |
> |
* Compute diffuse reflected component |
| 321 |
|
*/ |
| 322 |
|
copycolor(ctmp, np->rdiff); |
| 323 |
|
dtmp = ldot * omega * (1./PI); |
| 324 |
|
scalecolor(ctmp, dtmp); |
| 325 |
|
addcolor(cval, ctmp); |
| 326 |
|
} |
| 327 |
< |
if (ldot < .0 && bright(np->tdiff) > FTINY) { |
| 327 |
> |
if (ldot < 0 && bright(np->tdiff) > FTINY) { |
| 328 |
|
/* |
| 329 |
< |
* Compute added diffuse transmission. |
| 329 |
> |
* Compute diffuse transmission |
| 330 |
|
*/ |
| 331 |
|
copycolor(ctmp, np->tdiff); |
| 332 |
|
dtmp = -ldot * omega * (1.0/PI); |
| 333 |
|
scalecolor(ctmp, dtmp); |
| 334 |
|
addcolor(cval, ctmp); |
| 335 |
|
} |
| 336 |
+ |
if (ambRayInPmap(np->pr)) |
| 337 |
+ |
return; /* specular already in photon map */ |
| 338 |
|
/* |
| 339 |
< |
* Compute scattering coefficient using BSDF. |
| 339 |
> |
* Compute specular scattering coefficient using BSDF |
| 340 |
|
*/ |
| 341 |
< |
if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone) |
| 341 |
> |
if (!direct_specular_OK(ctmp, ldir, omega, np)) |
| 342 |
|
return; |
| 343 |
< |
bsdf_jitter(vjit, np); |
| 147 |
< |
ec = SDevalBSDF(&sv, vjit, vsrc, np->sd); |
| 148 |
< |
if (ec) |
| 149 |
< |
objerror(np->mp, USER, transSDError(ec)); |
| 150 |
< |
|
| 151 |
< |
if (sv.cieY <= FTINY) /* not worth using? */ |
| 152 |
< |
return; |
| 153 |
< |
cvt_sdcolor(ctmp, &sv); |
| 154 |
< |
if (ldot > .0) { /* pattern only diffuse reflection */ |
| 155 |
< |
COLOR ctmp1, ctmp2; |
| 156 |
< |
dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY |
| 157 |
< |
: np->sd->rLambBack.cieY; |
| 158 |
< |
dtmp /= PI * sv.cieY; /* diffuse fraction */ |
| 159 |
< |
copycolor(ctmp2, np->pr->pcol); |
| 160 |
< |
scalecolor(ctmp2, dtmp); |
| 161 |
< |
setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp); |
| 162 |
< |
addcolor(ctmp1, ctmp2); |
| 163 |
< |
multcolor(ctmp, ctmp1); /* apply derated pattern */ |
| 164 |
< |
dtmp = ldot * omega; |
| 165 |
< |
} else { /* full pattern on transmission */ |
| 343 |
> |
if (ldot < 0) { /* pattern for specular transmission */ |
| 344 |
|
multcolor(ctmp, np->pr->pcol); |
| 345 |
|
dtmp = -ldot * omega; |
| 346 |
< |
} |
| 346 |
> |
} else |
| 347 |
> |
dtmp = ldot * omega; |
| 348 |
|
scalecolor(ctmp, dtmp); |
| 349 |
|
addcolor(cval, ctmp); |
| 350 |
|
} |
| 359 |
|
) |
| 360 |
|
{ |
| 361 |
|
BSDFDAT *np = (BSDFDAT *)nnp; |
| 183 |
– |
SDError ec; |
| 184 |
– |
SDValue sv; |
| 185 |
– |
FVECT vsrc; |
| 186 |
– |
FVECT vjit; |
| 362 |
|
double ldot; |
| 363 |
|
double dtmp; |
| 364 |
|
COLOR ctmp, ctmp1, ctmp2; |
| 372 |
|
|
| 373 |
|
if (bright(np->rdiff) > FTINY) { |
| 374 |
|
/* |
| 375 |
< |
* Compute added diffuse reflected component. |
| 375 |
> |
* Compute diffuse reflected component |
| 376 |
|
*/ |
| 377 |
|
copycolor(ctmp, np->rdiff); |
| 378 |
|
dtmp = ldot * omega * (1./PI); |
| 379 |
|
scalecolor(ctmp, dtmp); |
| 380 |
|
addcolor(cval, ctmp); |
| 381 |
|
} |
| 382 |
+ |
if (ambRayInPmap(np->pr)) |
| 383 |
+ |
return; /* specular already in photon map */ |
| 384 |
|
/* |
| 385 |
< |
* Compute reflection coefficient using BSDF. |
| 385 |
> |
* Compute specular reflection coefficient using BSDF |
| 386 |
|
*/ |
| 387 |
< |
if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone) |
| 387 |
> |
if (!direct_specular_OK(ctmp, ldir, omega, np)) |
| 388 |
|
return; |
| 212 |
– |
bsdf_jitter(vjit, np); |
| 213 |
– |
ec = SDevalBSDF(&sv, vjit, vsrc, np->sd); |
| 214 |
– |
if (ec) |
| 215 |
– |
objerror(np->mp, USER, transSDError(ec)); |
| 216 |
– |
|
| 217 |
– |
if (sv.cieY <= FTINY) /* not worth using? */ |
| 218 |
– |
return; |
| 219 |
– |
cvt_sdcolor(ctmp, &sv); |
| 220 |
– |
/* pattern only diffuse reflection */ |
| 221 |
– |
dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY |
| 222 |
– |
: np->sd->rLambBack.cieY; |
| 223 |
– |
dtmp /= PI * sv.cieY; /* diffuse fraction */ |
| 224 |
– |
copycolor(ctmp2, np->pr->pcol); |
| 225 |
– |
scalecolor(ctmp2, dtmp); |
| 226 |
– |
setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp); |
| 227 |
– |
addcolor(ctmp1, ctmp2); |
| 228 |
– |
multcolor(ctmp, ctmp1); /* apply derated pattern */ |
| 389 |
|
dtmp = ldot * omega; |
| 390 |
|
scalecolor(ctmp, dtmp); |
| 391 |
|
addcolor(cval, ctmp); |
| 401 |
|
) |
| 402 |
|
{ |
| 403 |
|
BSDFDAT *np = (BSDFDAT *)nnp; |
| 244 |
– |
SDError ec; |
| 245 |
– |
SDValue sv; |
| 246 |
– |
FVECT vsrc; |
| 247 |
– |
FVECT vjit; |
| 404 |
|
double ldot; |
| 405 |
|
double dtmp; |
| 406 |
|
COLOR ctmp; |
| 414 |
|
|
| 415 |
|
if (bright(np->tdiff) > FTINY) { |
| 416 |
|
/* |
| 417 |
< |
* Compute added diffuse transmission. |
| 417 |
> |
* Compute diffuse transmission |
| 418 |
|
*/ |
| 419 |
|
copycolor(ctmp, np->tdiff); |
| 420 |
|
dtmp = -ldot * omega * (1.0/PI); |
| 421 |
|
scalecolor(ctmp, dtmp); |
| 422 |
|
addcolor(cval, ctmp); |
| 423 |
|
} |
| 424 |
+ |
if (ambRayInPmap(np->pr)) |
| 425 |
+ |
return; /* specular already in photon map */ |
| 426 |
|
/* |
| 427 |
< |
* Compute scattering coefficient using BSDF. |
| 427 |
> |
* Compute specular scattering coefficient using BSDF |
| 428 |
|
*/ |
| 429 |
< |
if (SDmapDir(vsrc, np->toloc, ldir) != SDEnone) |
| 429 |
> |
if (!direct_specular_OK(ctmp, ldir, omega, np)) |
| 430 |
|
return; |
| 273 |
– |
bsdf_jitter(vjit, np); |
| 274 |
– |
ec = SDevalBSDF(&sv, vjit, vsrc, np->sd); |
| 275 |
– |
if (ec) |
| 276 |
– |
objerror(np->mp, USER, transSDError(ec)); |
| 277 |
– |
|
| 278 |
– |
if (sv.cieY <= FTINY) /* not worth using? */ |
| 279 |
– |
return; |
| 280 |
– |
cvt_sdcolor(ctmp, &sv); |
| 431 |
|
/* full pattern on transmission */ |
| 432 |
|
multcolor(ctmp, np->pr->pcol); |
| 433 |
|
dtmp = -ldot * omega; |
| 440 |
|
sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat) |
| 441 |
|
{ |
| 442 |
|
int nstarget = 1; |
| 443 |
< |
int nsent = 0; |
| 443 |
> |
int nsent; |
| 444 |
|
SDError ec; |
| 445 |
|
SDValue bsv; |
| 446 |
< |
double sthick; |
| 447 |
< |
FVECT vjit, vsmp; |
| 446 |
> |
double xrand; |
| 447 |
> |
FVECT vsmp; |
| 448 |
|
RAY sr; |
| 299 |
– |
int ntrials; |
| 449 |
|
/* multiple samples? */ |
| 450 |
|
if (specjitter > 1.5) { |
| 451 |
|
nstarget = specjitter*ndp->pr->rweight + .5; |
| 452 |
< |
if (nstarget < 1) |
| 304 |
< |
nstarget = 1; |
| 452 |
> |
nstarget += !nstarget; |
| 453 |
|
} |
| 454 |
< |
/* run through our trials */ |
| 455 |
< |
for (ntrials = 0; nsent < nstarget && ntrials < 9*nstarget; ntrials++) { |
| 456 |
< |
SDerrorDetail[0] = '\0'; |
| 457 |
< |
/* sample direction & coef. */ |
| 458 |
< |
bsdf_jitter(vjit, ndp); |
| 459 |
< |
ec = SDsampComponent(&bsv, vsmp, vjit, ntrials ? frandom() |
| 460 |
< |
: urand(ilhash(dimlist,ndims)+samplendx), dcp); |
| 454 |
> |
/* run through our samples */ |
| 455 |
> |
for (nsent = 0; nsent < nstarget; nsent++) { |
| 456 |
> |
if (nstarget == 1) { /* stratify random variable */ |
| 457 |
> |
xrand = urand(ilhash(dimlist,ndims)+samplendx); |
| 458 |
> |
if (specjitter < 1.) |
| 459 |
> |
xrand = .5 + specjitter*(xrand-.5); |
| 460 |
> |
} else { |
| 461 |
> |
xrand = (nsent + frandom())/(double)nstarget; |
| 462 |
> |
} |
| 463 |
> |
SDerrorDetail[0] = '\0'; /* sample direction & coef. */ |
| 464 |
> |
bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]); |
| 465 |
> |
ec = SDsampComponent(&bsv, vsmp, xrand, dcp); |
| 466 |
|
if (ec) |
| 467 |
|
objerror(ndp->mp, USER, transSDError(ec)); |
| 468 |
< |
/* zero component? */ |
| 316 |
< |
if (bsv.cieY <= FTINY) |
| 468 |
> |
if (bsv.cieY <= FTINY) /* zero component? */ |
| 469 |
|
break; |
| 470 |
|
/* map vector to world */ |
| 471 |
|
if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone) |
| 472 |
|
break; |
| 321 |
– |
/* unintentional penetration? */ |
| 322 |
– |
if (DOT(sr.rdir, ndp->pr->ron) > .0 ^ vsmp[2] > .0) |
| 323 |
– |
continue; |
| 473 |
|
/* spawn a specular ray */ |
| 474 |
|
if (nstarget > 1) |
| 475 |
|
bsv.cieY /= (double)nstarget; |
| 476 |
< |
cvt_sdcolor(sr.rcoef, &bsv); /* use color */ |
| 477 |
< |
if (usepat) /* pattern on transmission */ |
| 476 |
> |
cvt_sdcolor(sr.rcoef, &bsv); /* use sample color */ |
| 477 |
> |
if (usepat) /* apply pattern? */ |
| 478 |
|
multcolor(sr.rcoef, ndp->pr->pcol); |
| 479 |
|
if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) { |
| 480 |
< |
if (maxdepth > 0) |
| 480 |
> |
if (maxdepth > 0) |
| 481 |
|
break; |
| 482 |
< |
++nsent; /* Russian roulette victim */ |
| 334 |
< |
continue; |
| 482 |
> |
continue; /* Russian roulette victim */ |
| 483 |
|
} |
| 484 |
|
/* need to offset origin? */ |
| 485 |
< |
if (ndp->thick != .0 && ndp->pr->rod > .0 ^ vsmp[2] > .0) |
| 485 |
> |
if (ndp->thick != 0 && (ndp->pr->rod > 0) ^ (vsmp[2] > 0)) |
| 486 |
|
VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick); |
| 487 |
|
rayvalue(&sr); /* send & evaluate sample */ |
| 488 |
|
multcolor(sr.rcol, sr.rcoef); |
| 489 |
|
addcolor(ndp->pr->rcol, sr.rcol); |
| 342 |
– |
++nsent; |
| 490 |
|
} |
| 491 |
|
return(nsent); |
| 492 |
|
} |
| 501 |
|
|
| 502 |
|
if (sflags == SDsampSpT) { |
| 503 |
|
unsc = ndp->tunsamp; |
| 504 |
< |
dfp = ndp->sd->tf; |
| 505 |
< |
cvt_sdcolor(unsc, &ndp->sd->tLamb); |
| 504 |
> |
if (ndp->pr->rod > 0) |
| 505 |
> |
dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb; |
| 506 |
> |
else |
| 507 |
> |
dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf; |
| 508 |
|
} else /* sflags == SDsampSpR */ { |
| 509 |
|
unsc = ndp->runsamp; |
| 510 |
< |
if (ndp->pr->rod > .0) { |
| 510 |
> |
if (ndp->pr->rod > 0) |
| 511 |
|
dfp = ndp->sd->rf; |
| 512 |
< |
cvt_sdcolor(unsc, &ndp->sd->rLambFront); |
| 364 |
< |
} else { |
| 512 |
> |
else |
| 513 |
|
dfp = ndp->sd->rb; |
| 366 |
– |
cvt_sdcolor(unsc, &ndp->sd->rLambBack); |
| 367 |
– |
} |
| 514 |
|
} |
| 515 |
< |
multcolor(unsc, ndp->pr->pcol); |
| 515 |
> |
setcolor(unsc, 0., 0., 0.); |
| 516 |
|
if (dfp == NULL) /* no specular component? */ |
| 517 |
|
return(0); |
| 518 |
|
/* below sampling threshold? */ |
| 520 |
|
if (dfp->maxHemi > FTINY) { /* XXX no color from BSDF */ |
| 521 |
|
FVECT vjit; |
| 522 |
|
double d; |
| 523 |
< |
COLOR ctmp; |
| 378 |
< |
bsdf_jitter(vjit, ndp); |
| 523 |
> |
bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]); |
| 524 |
|
d = SDdirectHemi(vjit, sflags, ndp->sd); |
| 525 |
|
if (sflags == SDsampSpT) { |
| 526 |
< |
copycolor(ctmp, ndp->pr->pcol); |
| 527 |
< |
scalecolor(ctmp, d); |
| 526 |
> |
copycolor(unsc, ndp->pr->pcol); |
| 527 |
> |
scalecolor(unsc, d); |
| 528 |
|
} else /* no pattern on reflection */ |
| 529 |
< |
setcolor(ctmp, d, d, d); |
| 385 |
< |
addcolor(unsc, ctmp); |
| 529 |
> |
setcolor(unsc, d, d, d); |
| 530 |
|
} |
| 531 |
|
return(0); |
| 532 |
|
} |
| 545 |
|
int |
| 546 |
|
m_bsdf(OBJREC *m, RAY *r) |
| 547 |
|
{ |
| 548 |
+ |
int hitfront; |
| 549 |
|
COLOR ctmp; |
| 550 |
|
SDError ec; |
| 551 |
|
FVECT upvec, vtmp; |
| 555 |
|
if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) | |
| 556 |
|
(m->oargs.nfargs % 3)) |
| 557 |
|
objerror(m, USER, "bad # arguments"); |
| 558 |
< |
|
| 558 |
> |
/* record surface struck */ |
| 559 |
> |
hitfront = (r->rod > 0); |
| 560 |
|
/* load cal file */ |
| 561 |
|
mf = getfunc(m, 5, 0x1d, 1); |
| 562 |
+ |
setfunc(m, r); |
| 563 |
|
/* get thickness */ |
| 564 |
|
nd.thick = evalue(mf->ep[0]); |
| 565 |
|
if ((-FTINY <= nd.thick) & (nd.thick <= FTINY)) |
| 566 |
|
nd.thick = .0; |
| 567 |
< |
/* check shadow */ |
| 568 |
< |
if (r->crtype & SHADOW) { |
| 569 |
< |
if (nd.thick != .0) |
| 570 |
< |
raytrans(r); /* pass-through */ |
| 424 |
< |
return(1); /* or shadow */ |
| 567 |
> |
/* check backface visibility */ |
| 568 |
> |
if (!hitfront & !backvis) { |
| 569 |
> |
raytrans(r); |
| 570 |
> |
return(1); |
| 571 |
|
} |
| 572 |
|
/* check other rays to pass */ |
| 573 |
< |
if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) || |
| 574 |
< |
nd.thick > .0 ^ r->rod > .0)) { |
| 573 |
> |
if (nd.thick != 0 && (r->crtype & SHADOW || |
| 574 |
> |
!(r->crtype & (SPECULAR|AMBIENT)) || |
| 575 |
> |
(nd.thick > 0) ^ hitfront)) { |
| 576 |
|
raytrans(r); /* hide our proxy */ |
| 577 |
|
return(1); |
| 578 |
|
} |
| 579 |
+ |
nd.mp = m; |
| 580 |
+ |
nd.pr = r; |
| 581 |
|
/* get BSDF data */ |
| 582 |
|
nd.sd = loadBSDF(m->oargs.sarg[1]); |
| 583 |
+ |
/* early shadow check */ |
| 584 |
+ |
if (r->crtype & SHADOW && (nd.sd->tf == NULL) & (nd.sd->tb == NULL)) |
| 585 |
+ |
return(1); |
| 586 |
|
/* diffuse reflectance */ |
| 587 |
< |
if (r->rod > .0) { |
| 588 |
< |
if (m->oargs.nfargs < 3) |
| 589 |
< |
setcolor(nd.rdiff, .0, .0, .0); |
| 590 |
< |
else |
| 439 |
< |
setcolor(nd.rdiff, m->oargs.farg[0], |
| 587 |
> |
if (hitfront) { |
| 588 |
> |
cvt_sdcolor(nd.rdiff, &nd.sd->rLambFront); |
| 589 |
> |
if (m->oargs.nfargs >= 3) { |
| 590 |
> |
setcolor(ctmp, m->oargs.farg[0], |
| 591 |
|
m->oargs.farg[1], |
| 592 |
|
m->oargs.farg[2]); |
| 593 |
+ |
addcolor(nd.rdiff, ctmp); |
| 594 |
+ |
} |
| 595 |
|
} else { |
| 596 |
< |
if (m->oargs.nfargs < 6) { /* check invisible backside */ |
| 597 |
< |
if (!backvis && (nd.sd->rb == NULL) & |
| 598 |
< |
(nd.sd->tf == NULL)) { |
| 446 |
< |
SDfreeCache(nd.sd); |
| 447 |
< |
raytrans(r); |
| 448 |
< |
return(1); |
| 449 |
< |
} |
| 450 |
< |
setcolor(nd.rdiff, .0, .0, .0); |
| 451 |
< |
} else |
| 452 |
< |
setcolor(nd.rdiff, m->oargs.farg[3], |
| 596 |
> |
cvt_sdcolor(nd.rdiff, &nd.sd->rLambBack); |
| 597 |
> |
if (m->oargs.nfargs >= 6) { |
| 598 |
> |
setcolor(ctmp, m->oargs.farg[3], |
| 599 |
|
m->oargs.farg[4], |
| 600 |
|
m->oargs.farg[5]); |
| 601 |
+ |
addcolor(nd.rdiff, ctmp); |
| 602 |
+ |
} |
| 603 |
|
} |
| 604 |
|
/* diffuse transmittance */ |
| 605 |
< |
if (m->oargs.nfargs < 9) |
| 606 |
< |
setcolor(nd.tdiff, .0, .0, .0); |
| 607 |
< |
else |
| 460 |
< |
setcolor(nd.tdiff, m->oargs.farg[6], |
| 605 |
> |
cvt_sdcolor(nd.tdiff, &nd.sd->tLamb); |
| 606 |
> |
if (m->oargs.nfargs >= 9) { |
| 607 |
> |
setcolor(ctmp, m->oargs.farg[6], |
| 608 |
|
m->oargs.farg[7], |
| 609 |
|
m->oargs.farg[8]); |
| 610 |
< |
nd.mp = m; |
| 611 |
< |
nd.pr = r; |
| 610 |
> |
addcolor(nd.tdiff, ctmp); |
| 611 |
> |
} |
| 612 |
|
/* get modifiers */ |
| 613 |
|
raytexture(r, m->omod); |
| 614 |
|
/* modify diffuse values */ |
| 619 |
|
upvec[1] = evalue(mf->ep[2]); |
| 620 |
|
upvec[2] = evalue(mf->ep[3]); |
| 621 |
|
/* return to world coords */ |
| 622 |
< |
if (mf->f != &unitxf) { |
| 623 |
< |
multv3(upvec, upvec, mf->f->xfm); |
| 624 |
< |
nd.thick *= mf->f->sca; |
| 622 |
> |
if (mf->fxp != &unitxf) { |
| 623 |
> |
multv3(upvec, upvec, mf->fxp->xfm); |
| 624 |
> |
nd.thick *= mf->fxp->sca; |
| 625 |
|
} |
| 626 |
+ |
if (r->rox != NULL) { |
| 627 |
+ |
multv3(upvec, upvec, r->rox->f.xfm); |
| 628 |
+ |
nd.thick *= r->rox->f.sca; |
| 629 |
+ |
} |
| 630 |
|
raynormal(nd.pnorm, r); |
| 631 |
|
/* compute local BSDF xform */ |
| 632 |
|
ec = SDcompXform(nd.toloc, nd.pnorm, upvec); |
| 636 |
|
nd.vray[2] = -r->rdir[2]; |
| 637 |
|
ec = SDmapDir(nd.vray, nd.toloc, nd.vray); |
| 638 |
|
} |
| 639 |
< |
if (!ec) |
| 640 |
< |
ec = SDinvXform(nd.fromloc, nd.toloc); |
| 490 |
< |
/* determine BSDF resolution */ |
| 491 |
< |
if (!ec) |
| 492 |
< |
ec = SDsizeBSDF(&nd.sr_vpsa, nd.vray, SDqueryMin, nd.sd); |
| 493 |
< |
if (!ec) |
| 494 |
< |
nd.sr_vpsa = sqrt(nd.sr_vpsa); |
| 495 |
< |
else { |
| 496 |
< |
objerror(m, WARNING, transSDError(ec)); |
| 497 |
< |
SDfreeCache(nd.sd); |
| 639 |
> |
if (ec) { |
| 640 |
> |
objerror(m, WARNING, "Illegal orientation vector"); |
| 641 |
|
return(1); |
| 642 |
|
} |
| 643 |
< |
if (r->rod < .0) { /* perturb normal towards hit */ |
| 643 |
> |
compute_through(&nd); /* compute through component */ |
| 644 |
> |
if (r->crtype & SHADOW) { |
| 645 |
> |
RAY tr; /* attempt to pass shadow ray */ |
| 646 |
> |
if (rayorigin(&tr, TRANS, r, nd.cthru) < 0) |
| 647 |
> |
return(1); /* blocked */ |
| 648 |
> |
VCOPY(tr.rdir, r->rdir); |
| 649 |
> |
rayvalue(&tr); /* transmit with scaling */ |
| 650 |
> |
multcolor(tr.rcol, tr.rcoef); |
| 651 |
> |
copycolor(r->rcol, tr.rcol); |
| 652 |
> |
return(1); /* we're done */ |
| 653 |
> |
} |
| 654 |
> |
ec = SDinvXform(nd.fromloc, nd.toloc); |
| 655 |
> |
if (!ec) /* determine BSDF resolution */ |
| 656 |
> |
ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, |
| 657 |
> |
SDqueryMin+SDqueryMax, nd.sd); |
| 658 |
> |
if (ec) |
| 659 |
> |
objerror(m, USER, transSDError(ec)); |
| 660 |
> |
|
| 661 |
> |
nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]); |
| 662 |
> |
nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]); |
| 663 |
> |
if (!hitfront) { /* perturb normal towards hit */ |
| 664 |
|
nd.pnorm[0] = -nd.pnorm[0]; |
| 665 |
|
nd.pnorm[1] = -nd.pnorm[1]; |
| 666 |
|
nd.pnorm[2] = -nd.pnorm[2]; |
| 673 |
|
copycolor(ctmp, nd.rdiff); |
| 674 |
|
addcolor(ctmp, nd.runsamp); |
| 675 |
|
if (bright(ctmp) > FTINY) { /* ambient from reflection */ |
| 676 |
< |
if (r->rod < .0) |
| 676 |
> |
if (!hitfront) |
| 677 |
|
flipsurface(r); |
| 678 |
|
multambient(ctmp, r, nd.pnorm); |
| 679 |
|
addcolor(r->rcol, ctmp); |
| 680 |
< |
if (r->rod < .0) |
| 680 |
> |
if (!hitfront) |
| 681 |
|
flipsurface(r); |
| 682 |
|
} |
| 683 |
|
copycolor(ctmp, nd.tdiff); |
| 684 |
|
addcolor(ctmp, nd.tunsamp); |
| 685 |
|
if (bright(ctmp) > FTINY) { /* ambient from other side */ |
| 686 |
|
FVECT bnorm; |
| 687 |
< |
if (r->rod > .0) |
| 687 |
> |
if (hitfront) |
| 688 |
|
flipsurface(r); |
| 689 |
|
bnorm[0] = -nd.pnorm[0]; |
| 690 |
|
bnorm[1] = -nd.pnorm[1]; |
| 691 |
|
bnorm[2] = -nd.pnorm[2]; |
| 692 |
< |
if (nd.thick != .0) { /* proxy with offset? */ |
| 692 |
> |
if (nd.thick != 0) { /* proxy with offset? */ |
| 693 |
|
VCOPY(vtmp, r->rop); |
| 694 |
< |
VSUM(r->rop, vtmp, r->ron, -nd.thick); |
| 694 |
> |
VSUM(r->rop, vtmp, r->ron, nd.thick); |
| 695 |
|
multambient(ctmp, r, bnorm); |
| 696 |
|
VCOPY(r->rop, vtmp); |
| 697 |
|
} else |
| 698 |
|
multambient(ctmp, r, bnorm); |
| 699 |
|
addcolor(r->rcol, ctmp); |
| 700 |
< |
if (r->rod > .0) |
| 700 |
> |
if (hitfront) |
| 701 |
|
flipsurface(r); |
| 702 |
|
} |
| 703 |
|
/* add direct component */ |
| 704 |
< |
if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL)) { |
| 704 |
> |
if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) & |
| 705 |
> |
(nd.sd->tb == NULL)) { |
| 706 |
|
direct(r, dir_brdf, &nd); /* reflection only */ |
| 707 |
< |
} else if (nd.thick == .0) { |
| 707 |
> |
} else if (nd.thick == 0) { |
| 708 |
|
direct(r, dir_bsdf, &nd); /* thin surface scattering */ |
| 709 |
|
} else { |
| 710 |
|
direct(r, dir_brdf, &nd); /* reflection first */ |
| 711 |
|
VCOPY(vtmp, r->rop); /* offset for transmitted */ |
| 712 |
|
VSUM(r->rop, vtmp, r->ron, -nd.thick); |
| 713 |
< |
direct(r, dir_btdf, &nd); |
| 713 |
> |
direct(r, dir_btdf, &nd); /* separate transmission */ |
| 714 |
|
VCOPY(r->rop, vtmp); |
| 715 |
|
} |
| 716 |
|
/* clean up */ |