| 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" |
| 15 |
|
#include "bsdf.h" |
| 16 |
|
#include "random.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 thickness variable causes the strange but useful behavior |
| 23 |
< |
* of translating transmitted rays this distance past the surface |
| 24 |
< |
* intersection in the normal direction to bypass intervening geometry. |
| 25 |
< |
* This only affects scattered, non-source directed samples. Thus, |
| 26 |
< |
* thickness is relevant only if there is a transmitted component. |
| 27 |
< |
* A positive thickness has the further side-effect that an unscattered |
| 28 |
< |
* (view) ray will pass right through our material if it has any |
| 29 |
< |
* non-diffuse transmission, making our BSDF invisible. This allows the |
| 30 |
< |
* underlying geometry to become visible. A matching surface should be |
| 31 |
< |
* placed on the other side, less than the thickness away, if the backside |
| 32 |
< |
* reflectance is non-zero. |
| 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. |
| 26 |
> |
* A non-zero thickness has the further side-effect that an unscattered |
| 27 |
> |
* (view) ray will pass right through our material, making the BSDF |
| 28 |
> |
* surface invisible and showing the proxied geometry instead. Thickness |
| 29 |
> |
* has the further effect of turning off reflection on the reverse side so |
| 30 |
> |
* rays heading in the opposite direction pass unimpeded through the BSDF |
| 31 |
> |
* surface. A paired surface may be placed on the opposide side of |
| 32 |
> |
* the detail geometry, less than this thickness away, if a two-way |
| 33 |
> |
* proxy is desired. Note that the sign of the thickness is important. |
| 34 |
> |
* A positive thickness hides geometry behind the BSDF surface and uses |
| 35 |
> |
* front reflectance and transmission properties. A negative thickness |
| 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 |
> |
* 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 |
| 49 |
|
* system for the BSDF. |
| 50 |
|
* We do not reorient the surface, so if the BSDF has no back-side |
| 51 |
< |
* reflectance and none is given in the real arguments, the surface will |
| 52 |
< |
* appear as black when viewed from behind (unless backvis is false). |
| 53 |
< |
* The diffuse compnent arguments are added to components in the BSDF file, |
| 51 |
> |
* reflectance and none is given in the real arguments, a BSDF surface |
| 52 |
> |
* with zero thickness will appear black when viewed from behind |
| 53 |
> |
* unless backface visibility is on, when it becomes invisible. |
| 54 |
> |
* The diffuse arguments are added to components in the BSDF file, |
| 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 |
| 68 |
< |
* 0|3|9 rdf gdf bdf |
| 68 |
> |
* 0|3|6|9 rdf gdf bdf |
| 69 |
|
* rdb gdb bdb |
| 70 |
|
* rdt gdt bdt |
| 71 |
|
*/ |
| 72 |
|
|
| 73 |
+ |
/* |
| 74 |
+ |
* Note that our reverse ray-tracing process means that the positions |
| 75 |
+ |
* of incoming and outgoing vectors may be reversed in our calls |
| 76 |
+ |
* to the BSDF library. This is usually fine, since the bidirectional nature |
| 77 |
+ |
* of the BSDF (that's what the 'B' stands for) means it all works out. |
| 78 |
+ |
*/ |
| 79 |
+ |
|
| 80 |
|
typedef struct { |
| 81 |
|
OBJREC *mp; /* material pointer */ |
| 82 |
|
RAY *pr; /* intersected ray */ |
| 83 |
|
FVECT pnorm; /* perturbed surface normal */ |
| 84 |
< |
FVECT vinc; /* local incident vector */ |
| 84 |
> |
FVECT vray; /* local outgoing (return) vector */ |
| 85 |
> |
double sr_vpsa[2]; /* sqrt of BSDF projected solid angle extrema */ |
| 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 for MAT_ABSDF */ |
| 90 |
|
SDData *sd; /* loaded BSDF data */ |
| 91 |
+ |
COLOR rdiff; /* diffuse reflection */ |
| 92 |
|
COLOR runsamp; /* BSDF hemispherical reflection */ |
| 93 |
< |
COLOR rdiff; /* added diffuse reflection */ |
| 93 |
> |
COLOR tdiff; /* diffuse transmission */ |
| 94 |
|
COLOR tunsamp; /* BSDF hemispherical transmission */ |
| 62 |
– |
COLOR tdiff; /* added diffuse transmission */ |
| 95 |
|
} BSDFDAT; /* BSDF material data */ |
| 96 |
|
|
| 97 |
|
#define cvt_sdcolor(cv, svp) ccy2rgb(&(svp)->spec, (svp)->cieY, cv) |
| 98 |
|
|
| 99 |
< |
/* Compute source contribution for BSDF */ |
| 99 |
> |
/* Compute "through" component color for MAT_ABSDF */ |
| 100 |
|
static void |
| 101 |
< |
dirbsdf( |
| 101 |
> |
compute_through(BSDFDAT *ndp) |
| 102 |
> |
{ |
| 103 |
> |
#define NDIR2CHECK 13 |
| 104 |
> |
static const float dir2check[NDIR2CHECK][2] = { |
| 105 |
> |
{0, 0}, |
| 106 |
> |
{-0.8, 0}, |
| 107 |
> |
{0, 0.8}, |
| 108 |
> |
{0, -0.8}, |
| 109 |
> |
{0.8, 0}, |
| 110 |
> |
{-0.8, 0.8}, |
| 111 |
> |
{-0.8, -0.8}, |
| 112 |
> |
{0.8, 0.8}, |
| 113 |
> |
{0.8, -0.8}, |
| 114 |
> |
{-1.6, 0}, |
| 115 |
> |
{0, 1.6}, |
| 116 |
> |
{0, -1.6}, |
| 117 |
> |
{1.6, 0}, |
| 118 |
> |
}; |
| 119 |
> |
const double peak_over = 1.5; |
| 120 |
> |
SDSpectralDF *dfp; |
| 121 |
> |
FVECT pdir; |
| 122 |
> |
double tomega, srchrad; |
| 123 |
> |
COLOR vpeak, vsum; |
| 124 |
> |
int i; |
| 125 |
> |
SDError ec; |
| 126 |
> |
|
| 127 |
> |
if (ndp->pr->rod > 0) |
| 128 |
> |
dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb; |
| 129 |
> |
else |
| 130 |
> |
dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf; |
| 131 |
> |
|
| 132 |
> |
if (dfp == NULL) |
| 133 |
> |
return; /* no specular transmission */ |
| 134 |
> |
if (bright(ndp->pr->pcol) <= FTINY) |
| 135 |
> |
return; /* pattern is black, here */ |
| 136 |
> |
srchrad = sqrt(dfp->minProjSA); /* else search for peak */ |
| 137 |
> |
setcolor(vpeak, 0, 0, 0); |
| 138 |
> |
setcolor(vsum, 0, 0, 0); |
| 139 |
> |
pdir[2] = 0.0; |
| 140 |
> |
for (i = 0; i < NDIR2CHECK; i++) { |
| 141 |
> |
FVECT tdir; |
| 142 |
> |
SDValue sv; |
| 143 |
> |
COLOR vcol; |
| 144 |
> |
tdir[0] = -ndp->vray[0] + dir2check[i][0]*srchrad; |
| 145 |
> |
tdir[1] = -ndp->vray[1] + dir2check[i][1]*srchrad; |
| 146 |
> |
tdir[2] = -ndp->vray[2]; |
| 147 |
> |
normalize(tdir); |
| 148 |
> |
ec = SDevalBSDF(&sv, tdir, ndp->vray, ndp->sd); |
| 149 |
> |
if (ec) |
| 150 |
> |
goto baderror; |
| 151 |
> |
cvt_sdcolor(vcol, &sv); |
| 152 |
> |
addcolor(vsum, vcol); |
| 153 |
> |
if (sv.cieY > bright(vpeak)) { |
| 154 |
> |
copycolor(vpeak, vcol); |
| 155 |
> |
VCOPY(pdir, tdir); |
| 156 |
> |
} |
| 157 |
> |
} |
| 158 |
> |
if (pdir[2] == 0.0) |
| 159 |
> |
return; /* zero neighborhood */ |
| 160 |
> |
ec = SDsizeBSDF(&tomega, pdir, ndp->vray, SDqueryMin, ndp->sd); |
| 161 |
> |
if (ec) |
| 162 |
> |
goto baderror; |
| 163 |
> |
if (tomega > 1.5*dfp->minProjSA) |
| 164 |
> |
return; /* not really a peak? */ |
| 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 */ |
| 168 |
> |
colval(vsum,i) -= colval(vpeak,i); |
| 169 |
> |
if (peak_over*bright(vsum) >= (NDIR2CHECK-1)*bright(vpeak)) |
| 170 |
> |
return; /* not peaky enough */ |
| 171 |
> |
copycolor(ndp->cthru, vpeak); /* else use it */ |
| 172 |
> |
scalecolor(ndp->cthru, tomega); |
| 173 |
> |
multcolor(ndp->cthru, ndp->pr->pcol); /* modify by pattern */ |
| 174 |
> |
return; |
| 175 |
> |
baderror: |
| 176 |
> |
objerror(ndp->mp, USER, transSDError(ec)); |
| 177 |
> |
#undef NDIR2CHECK |
| 178 |
> |
} |
| 179 |
> |
|
| 180 |
> |
/* Jitter ray sample according to projected solid angle and specjitter */ |
| 181 |
> |
static void |
| 182 |
> |
bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa) |
| 183 |
> |
{ |
| 184 |
> |
VCOPY(vres, ndp->vray); |
| 185 |
> |
if (specjitter < 1.) |
| 186 |
> |
sr_psa *= specjitter; |
| 187 |
> |
if (sr_psa <= FTINY) |
| 188 |
> |
return; |
| 189 |
> |
vres[0] += sr_psa*(.5 - frandom()); |
| 190 |
> |
vres[1] += sr_psa*(.5 - frandom()); |
| 191 |
> |
normalize(vres); |
| 192 |
> |
} |
| 193 |
> |
|
| 194 |
> |
/* Get BSDF specular for direct component, returning true if OK to proceed */ |
| 195 |
> |
static int |
| 196 |
> |
direct_specular_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp) |
| 197 |
> |
{ |
| 198 |
> |
int nsamp; |
| 199 |
> |
double wtot = 0; |
| 200 |
> |
FVECT vsrc, vsmp, vjit; |
| 201 |
> |
double tomega, tomega2; |
| 202 |
> |
double sf, tsr, sd[2]; |
| 203 |
> |
COLOR csmp, cdiff; |
| 204 |
> |
double diffY; |
| 205 |
> |
SDValue sv; |
| 206 |
> |
SDError ec; |
| 207 |
> |
int i; |
| 208 |
> |
/* in case we fail */ |
| 209 |
> |
setcolor(cval, 0, 0, 0); |
| 210 |
> |
/* transform source direction */ |
| 211 |
> |
if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone) |
| 212 |
> |
return(0); |
| 213 |
> |
/* will discount diffuse portion */ |
| 214 |
> |
switch ((vsrc[2] > 0)<<1 | (ndp->vray[2] > 0)) { |
| 215 |
> |
case 3: |
| 216 |
> |
if (ndp->sd->rf == NULL) |
| 217 |
> |
return(0); /* all diffuse */ |
| 218 |
> |
sv = ndp->sd->rLambFront; |
| 219 |
> |
break; |
| 220 |
> |
case 0: |
| 221 |
> |
if (ndp->sd->rb == NULL) |
| 222 |
> |
return(0); /* all diffuse */ |
| 223 |
> |
sv = ndp->sd->rLambBack; |
| 224 |
> |
break; |
| 225 |
> |
default: |
| 226 |
> |
if ((ndp->sd->tf == NULL) & (ndp->sd->tb == NULL)) |
| 227 |
> |
return(0); /* all diffuse */ |
| 228 |
> |
sv = ndp->sd->tLamb; |
| 229 |
> |
break; |
| 230 |
> |
} |
| 231 |
> |
if (sv.cieY > FTINY) { |
| 232 |
> |
diffY = sv.cieY *= 1./PI; |
| 233 |
> |
cvt_sdcolor(cdiff, &sv); |
| 234 |
> |
} else { |
| 235 |
> |
diffY = 0; |
| 236 |
> |
setcolor(cdiff, 0, 0, 0); |
| 237 |
> |
} |
| 238 |
> |
/* need projected solid angle */ |
| 239 |
> |
omega *= fabs(vsrc[2]); |
| 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 |
> |
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) |
| 258 |
> |
nsamp = 1; |
| 259 |
> |
else if (25.*tomega <= omega) |
| 260 |
> |
nsamp = 100.*sf + .5; |
| 261 |
> |
else |
| 262 |
> |
nsamp = 4.*sf*omega/tomega + .5; |
| 263 |
> |
nsamp += !nsamp; |
| 264 |
> |
sf = sqrt(omega); /* sample our source area */ |
| 265 |
> |
tsr = sqrt(tomega); |
| 266 |
> |
for (i = nsamp; i--; ) { |
| 267 |
> |
VCOPY(vsmp, vsrc); /* jitter query directions */ |
| 268 |
> |
if (nsamp > 1) { |
| 269 |
> |
multisamp(sd, 2, (i + frandom())/(double)nsamp); |
| 270 |
> |
vsmp[0] += (sd[0] - .5)*sf; |
| 271 |
> |
vsmp[1] += (sd[1] - .5)*sf; |
| 272 |
> |
normalize(vsmp); |
| 273 |
> |
} |
| 274 |
> |
bsdf_jitter(vjit, ndp, tsr); |
| 275 |
> |
/* compute BSDF */ |
| 276 |
> |
ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd); |
| 277 |
> |
if (ec) |
| 278 |
> |
goto baderror; |
| 279 |
> |
if (sv.cieY - diffY <= FTINY) |
| 280 |
> |
continue; /* no specular part */ |
| 281 |
> |
/* check for variable resolution */ |
| 282 |
> |
ec = SDsizeBSDF(&tomega2, vjit, vsmp, SDqueryMin, ndp->sd); |
| 283 |
> |
if (ec) |
| 284 |
> |
goto baderror; |
| 285 |
> |
if (tomega2 < .12*tomega) |
| 286 |
> |
continue; /* not safe to include */ |
| 287 |
> |
cvt_sdcolor(csmp, &sv); |
| 288 |
> |
|
| 289 |
> |
if (sf < 2.5*tsr) { /* weight by Y for small sources */ |
| 290 |
> |
scalecolor(csmp, sv.cieY); |
| 291 |
> |
wtot += sv.cieY; |
| 292 |
> |
} else |
| 293 |
> |
wtot += 1.; |
| 294 |
> |
addcolor(cval, csmp); |
| 295 |
> |
} |
| 296 |
> |
if (wtot <= FTINY) /* no valid specular samples? */ |
| 297 |
> |
return(0); |
| 298 |
> |
|
| 299 |
> |
sf = 1./wtot; /* weighted average BSDF */ |
| 300 |
> |
scalecolor(cval, sf); |
| 301 |
> |
/* subtract diffuse contribution */ |
| 302 |
> |
for (i = 3*(diffY > FTINY); i--; ) |
| 303 |
> |
if ((colval(cval,i) -= colval(cdiff,i)) < 0) |
| 304 |
> |
colval(cval,i) = 0; |
| 305 |
> |
return(1); |
| 306 |
> |
baderror: |
| 307 |
> |
objerror(ndp->mp, USER, transSDError(ec)); |
| 308 |
> |
return(0); /* gratis return */ |
| 309 |
> |
} |
| 310 |
> |
|
| 311 |
> |
/* Compute source contribution for BSDF (reflected & transmitted) */ |
| 312 |
> |
static void |
| 313 |
> |
dir_bsdf( |
| 314 |
|
COLOR cval, /* returned coefficient */ |
| 315 |
|
void *nnp, /* material data */ |
| 316 |
|
FVECT ldir, /* light source direction */ |
| 318 |
|
) |
| 319 |
|
{ |
| 320 |
|
BSDFDAT *np = (BSDFDAT *)nnp; |
| 77 |
– |
SDError ec; |
| 78 |
– |
SDValue sv; |
| 79 |
– |
FVECT vout; |
| 321 |
|
double ldot; |
| 322 |
|
double dtmp; |
| 323 |
|
COLOR ctmp; |
| 324 |
|
|
| 325 |
< |
setcolor(cval, .0, .0, .0); |
| 325 |
> |
setcolor(cval, 0, 0, 0); |
| 326 |
|
|
| 327 |
|
ldot = DOT(np->pnorm, ldir); |
| 328 |
|
if ((-FTINY <= ldot) & (ldot <= FTINY)) |
| 329 |
|
return; |
| 330 |
|
|
| 331 |
< |
if (ldot > .0 && bright(np->rdiff) > FTINY) { |
| 331 |
> |
if (ldot > 0 && bright(np->rdiff) > FTINY) { |
| 332 |
|
/* |
| 333 |
< |
* Compute added diffuse reflected component. |
| 333 |
> |
* Compute diffuse reflected component |
| 334 |
|
*/ |
| 335 |
|
copycolor(ctmp, np->rdiff); |
| 336 |
|
dtmp = ldot * omega * (1./PI); |
| 337 |
|
scalecolor(ctmp, dtmp); |
| 338 |
|
addcolor(cval, ctmp); |
| 339 |
|
} |
| 340 |
< |
if (ldot < .0 && bright(np->tdiff) > FTINY) { |
| 340 |
> |
if (ldot < 0 && bright(np->tdiff) > FTINY) { |
| 341 |
|
/* |
| 342 |
< |
* Compute added diffuse transmission. |
| 342 |
> |
* Compute diffuse transmission |
| 343 |
|
*/ |
| 344 |
|
copycolor(ctmp, np->tdiff); |
| 345 |
|
dtmp = -ldot * omega * (1.0/PI); |
| 346 |
|
scalecolor(ctmp, dtmp); |
| 347 |
|
addcolor(cval, ctmp); |
| 348 |
|
} |
| 349 |
+ |
if (ambRayInPmap(np->pr)) |
| 350 |
+ |
return; /* specular already in photon map */ |
| 351 |
|
/* |
| 352 |
< |
* Compute scattering coefficient using BSDF. |
| 352 |
> |
* Compute specular scattering coefficient using BSDF |
| 353 |
|
*/ |
| 354 |
< |
if (SDmapDir(vout, np->toloc, ldir) != SDEnone) |
| 354 |
> |
if (!direct_specular_OK(ctmp, ldir, omega, np)) |
| 355 |
|
return; |
| 356 |
< |
ec = SDevalBSDF(&sv, vout, np->vinc, np->sd); |
| 114 |
< |
if (ec) |
| 115 |
< |
objerror(np->mp, USER, transSDError(ec)); |
| 116 |
< |
|
| 117 |
< |
if (sv.cieY <= FTINY) /* not worth using? */ |
| 118 |
< |
return; |
| 119 |
< |
cvt_sdcolor(ctmp, &sv); |
| 120 |
< |
if (ldot > .0) { /* pattern only diffuse reflection */ |
| 121 |
< |
COLOR ctmp1, ctmp2; |
| 122 |
< |
dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY |
| 123 |
< |
: np->sd->rLambBack.cieY; |
| 124 |
< |
dtmp /= PI * sv.cieY; /* diffuse fraction */ |
| 125 |
< |
copycolor(ctmp2, np->pr->pcol); |
| 126 |
< |
scalecolor(ctmp2, dtmp); |
| 127 |
< |
setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp); |
| 128 |
< |
addcolor(ctmp1, ctmp2); |
| 129 |
< |
multcolor(ctmp, ctmp1); /* apply derated pattern */ |
| 130 |
< |
dtmp = ldot * omega; |
| 131 |
< |
} else { /* full pattern on transmission */ |
| 356 |
> |
if (ldot < 0) { /* pattern for specular transmission */ |
| 357 |
|
multcolor(ctmp, np->pr->pcol); |
| 358 |
|
dtmp = -ldot * omega; |
| 359 |
+ |
} else |
| 360 |
+ |
dtmp = ldot * omega; |
| 361 |
+ |
scalecolor(ctmp, dtmp); |
| 362 |
+ |
addcolor(cval, ctmp); |
| 363 |
+ |
} |
| 364 |
+ |
|
| 365 |
+ |
/* Compute source contribution for BSDF (reflected only) */ |
| 366 |
+ |
static void |
| 367 |
+ |
dir_brdf( |
| 368 |
+ |
COLOR cval, /* returned coefficient */ |
| 369 |
+ |
void *nnp, /* material data */ |
| 370 |
+ |
FVECT ldir, /* light source direction */ |
| 371 |
+ |
double omega /* light source size */ |
| 372 |
+ |
) |
| 373 |
+ |
{ |
| 374 |
+ |
BSDFDAT *np = (BSDFDAT *)nnp; |
| 375 |
+ |
double ldot; |
| 376 |
+ |
double dtmp; |
| 377 |
+ |
COLOR ctmp, ctmp1, ctmp2; |
| 378 |
+ |
|
| 379 |
+ |
setcolor(cval, 0, 0, 0); |
| 380 |
+ |
|
| 381 |
+ |
ldot = DOT(np->pnorm, ldir); |
| 382 |
+ |
|
| 383 |
+ |
if (ldot <= FTINY) |
| 384 |
+ |
return; |
| 385 |
+ |
|
| 386 |
+ |
if (bright(np->rdiff) > FTINY) { |
| 387 |
+ |
/* |
| 388 |
+ |
* Compute diffuse reflected component |
| 389 |
+ |
*/ |
| 390 |
+ |
copycolor(ctmp, np->rdiff); |
| 391 |
+ |
dtmp = ldot * omega * (1./PI); |
| 392 |
+ |
scalecolor(ctmp, dtmp); |
| 393 |
+ |
addcolor(cval, ctmp); |
| 394 |
|
} |
| 395 |
+ |
if (ambRayInPmap(np->pr)) |
| 396 |
+ |
return; /* specular already in photon map */ |
| 397 |
+ |
/* |
| 398 |
+ |
* Compute specular reflection coefficient using BSDF |
| 399 |
+ |
*/ |
| 400 |
+ |
if (!direct_specular_OK(ctmp, ldir, omega, np)) |
| 401 |
+ |
return; |
| 402 |
+ |
dtmp = ldot * omega; |
| 403 |
|
scalecolor(ctmp, dtmp); |
| 404 |
|
addcolor(cval, ctmp); |
| 405 |
|
} |
| 406 |
|
|
| 407 |
+ |
/* Compute source contribution for BSDF (transmitted only) */ |
| 408 |
+ |
static void |
| 409 |
+ |
dir_btdf( |
| 410 |
+ |
COLOR cval, /* returned coefficient */ |
| 411 |
+ |
void *nnp, /* material data */ |
| 412 |
+ |
FVECT ldir, /* light source direction */ |
| 413 |
+ |
double omega /* light source size */ |
| 414 |
+ |
) |
| 415 |
+ |
{ |
| 416 |
+ |
BSDFDAT *np = (BSDFDAT *)nnp; |
| 417 |
+ |
double ldot; |
| 418 |
+ |
double dtmp; |
| 419 |
+ |
COLOR ctmp; |
| 420 |
+ |
|
| 421 |
+ |
setcolor(cval, 0, 0, 0); |
| 422 |
+ |
|
| 423 |
+ |
ldot = DOT(np->pnorm, ldir); |
| 424 |
+ |
|
| 425 |
+ |
if (ldot >= -FTINY) |
| 426 |
+ |
return; |
| 427 |
+ |
|
| 428 |
+ |
if (bright(np->tdiff) > FTINY) { |
| 429 |
+ |
/* |
| 430 |
+ |
* Compute diffuse transmission |
| 431 |
+ |
*/ |
| 432 |
+ |
copycolor(ctmp, np->tdiff); |
| 433 |
+ |
dtmp = -ldot * omega * (1.0/PI); |
| 434 |
+ |
scalecolor(ctmp, dtmp); |
| 435 |
+ |
addcolor(cval, ctmp); |
| 436 |
+ |
} |
| 437 |
+ |
if (ambRayInPmap(np->pr)) |
| 438 |
+ |
return; /* specular already in photon map */ |
| 439 |
+ |
/* |
| 440 |
+ |
* Compute specular scattering coefficient using BSDF |
| 441 |
+ |
*/ |
| 442 |
+ |
if (!direct_specular_OK(ctmp, ldir, omega, np)) |
| 443 |
+ |
return; |
| 444 |
+ |
/* full pattern on transmission */ |
| 445 |
+ |
multcolor(ctmp, np->pr->pcol); |
| 446 |
+ |
dtmp = -ldot * omega; |
| 447 |
+ |
scalecolor(ctmp, dtmp); |
| 448 |
+ |
addcolor(cval, ctmp); |
| 449 |
+ |
} |
| 450 |
+ |
|
| 451 |
|
/* Sample separate BSDF component */ |
| 452 |
|
static int |
| 453 |
< |
sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat) |
| 453 |
> |
sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int xmit) |
| 454 |
|
{ |
| 455 |
< |
int nstarget = 1; |
| 456 |
< |
int nsent = 0; |
| 457 |
< |
SDError ec; |
| 458 |
< |
SDValue bsv; |
| 459 |
< |
double sthick; |
| 460 |
< |
FVECT vout; |
| 461 |
< |
RAY sr; |
| 462 |
< |
int ntrials; |
| 455 |
> |
const int hasthru = (xmit && |
| 456 |
> |
!(ndp->pr->crtype & (SPECULAR|AMBIENT)) |
| 457 |
> |
&& bright(ndp->cthru) > FTINY); |
| 458 |
> |
int nstarget = 1; |
| 459 |
> |
int nsent = 0; |
| 460 |
> |
int n; |
| 461 |
> |
SDError ec; |
| 462 |
> |
SDValue bsv; |
| 463 |
> |
double xrand; |
| 464 |
> |
FVECT vsmp, vinc; |
| 465 |
> |
RAY sr; |
| 466 |
|
/* multiple samples? */ |
| 467 |
|
if (specjitter > 1.5) { |
| 468 |
|
nstarget = specjitter*ndp->pr->rweight + .5; |
| 469 |
< |
if (nstarget < 1) |
| 155 |
< |
nstarget = 1; |
| 469 |
> |
nstarget += !nstarget; |
| 470 |
|
} |
| 471 |
< |
/* run through our trials */ |
| 472 |
< |
for (ntrials = 0; nsent < nstarget && ntrials < 9*nstarget; ntrials++) { |
| 473 |
< |
SDerrorDetail[0] = '\0'; |
| 474 |
< |
/* sample direction & coef. */ |
| 475 |
< |
ec = SDsampComponent(&bsv, vout, ndp->vinc, |
| 476 |
< |
ntrials ? frandom() |
| 477 |
< |
: urand(ilhash(dimlist,ndims)+samplendx), |
| 478 |
< |
dcp); |
| 471 |
> |
/* run through our samples */ |
| 472 |
> |
for (n = 0; n < nstarget; n++) { |
| 473 |
> |
if (nstarget == 1) { /* stratify random variable */ |
| 474 |
> |
xrand = urand(ilhash(dimlist,ndims)+samplendx); |
| 475 |
> |
if (specjitter < 1.) |
| 476 |
> |
xrand = .5 + specjitter*(xrand-.5); |
| 477 |
> |
} else { |
| 478 |
> |
xrand = (n + frandom())/(double)nstarget; |
| 479 |
> |
} |
| 480 |
> |
SDerrorDetail[0] = '\0'; /* sample direction & coef. */ |
| 481 |
> |
bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]); |
| 482 |
> |
VCOPY(vinc, vsmp); /* to compare after */ |
| 483 |
> |
ec = SDsampComponent(&bsv, vsmp, xrand, dcp); |
| 484 |
|
if (ec) |
| 485 |
|
objerror(ndp->mp, USER, transSDError(ec)); |
| 486 |
< |
/* zero component? */ |
| 168 |
< |
if (bsv.cieY <= FTINY) |
| 486 |
> |
if (bsv.cieY <= FTINY) /* zero component? */ |
| 487 |
|
break; |
| 488 |
< |
/* map vector to world */ |
| 489 |
< |
if (SDmapDir(sr.rdir, ndp->fromloc, vout) != SDEnone) |
| 488 |
> |
if (hasthru) { /* check for view ray */ |
| 489 |
> |
double dx = vinc[0] + vsmp[0]; |
| 490 |
> |
double dy = vinc[1] + vsmp[1]; |
| 491 |
> |
if (dx*dx + dy*dy <= ndp->sr_vpsa[0]*ndp->sr_vpsa[0]) |
| 492 |
> |
continue; /* exclude view sample */ |
| 493 |
> |
} |
| 494 |
> |
/* map non-view sample->world */ |
| 495 |
> |
if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone) |
| 496 |
|
break; |
| 173 |
– |
/* unintentional penetration? */ |
| 174 |
– |
if (DOT(sr.rdir, ndp->pr->ron) > .0 ^ vout[2] > .0) |
| 175 |
– |
continue; |
| 497 |
|
/* spawn a specular ray */ |
| 498 |
|
if (nstarget > 1) |
| 499 |
|
bsv.cieY /= (double)nstarget; |
| 500 |
< |
cvt_sdcolor(sr.rcoef, &bsv); /* use color */ |
| 501 |
< |
if (usepat) /* pattern on transmission */ |
| 500 |
> |
cvt_sdcolor(sr.rcoef, &bsv); /* use sample color */ |
| 501 |
> |
if (xmit) /* apply pattern on transmit */ |
| 502 |
|
multcolor(sr.rcoef, ndp->pr->pcol); |
| 503 |
|
if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) { |
| 504 |
< |
if (maxdepth > 0) |
| 505 |
< |
break; |
| 506 |
< |
++nsent; /* Russian roulette victim */ |
| 507 |
< |
continue; |
| 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 */ |
| 511 |
|
} |
| 512 |
< |
if (ndp->thick > FTINY) { /* need to move origin? */ |
| 513 |
< |
sthick = (ndp->pr->rod > .0) ? -ndp->thick : ndp->thick; |
| 190 |
< |
if (sthick < .0 ^ vout[2] > .0) |
| 191 |
< |
VSUM(sr.rorg, sr.rorg, ndp->pr->ron, sthick); |
| 192 |
< |
} |
| 512 |
> |
if (xmit && ndp->thick != 0) /* need to offset origin? */ |
| 513 |
> |
VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick); |
| 514 |
|
rayvalue(&sr); /* send & evaluate sample */ |
| 515 |
|
multcolor(sr.rcol, sr.rcoef); |
| 516 |
|
addcolor(ndp->pr->rcol, sr.rcol); |
| 523 |
|
static int |
| 524 |
|
sample_sdf(BSDFDAT *ndp, int sflags) |
| 525 |
|
{ |
| 526 |
+ |
int hasthru = (sflags == SDsampSpT && |
| 527 |
+ |
!(ndp->pr->crtype & (SPECULAR|AMBIENT)) |
| 528 |
+ |
&& bright(ndp->cthru) > FTINY); |
| 529 |
|
int n, ntotal = 0; |
| 530 |
+ |
double b = 0; |
| 531 |
|
SDSpectralDF *dfp; |
| 532 |
|
COLORV *unsc; |
| 533 |
|
|
| 534 |
|
if (sflags == SDsampSpT) { |
| 535 |
|
unsc = ndp->tunsamp; |
| 536 |
< |
dfp = ndp->sd->tf; |
| 537 |
< |
cvt_sdcolor(unsc, &ndp->sd->tLamb); |
| 536 |
> |
if (ndp->pr->rod > 0) |
| 537 |
> |
dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb; |
| 538 |
> |
else |
| 539 |
> |
dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf; |
| 540 |
|
} else /* sflags == SDsampSpR */ { |
| 541 |
|
unsc = ndp->runsamp; |
| 542 |
< |
if (ndp->pr->rod > .0) { |
| 542 |
> |
if (ndp->pr->rod > 0) |
| 543 |
|
dfp = ndp->sd->rf; |
| 544 |
< |
cvt_sdcolor(unsc, &ndp->sd->rLambFront); |
| 218 |
< |
} else { |
| 544 |
> |
else |
| 545 |
|
dfp = ndp->sd->rb; |
| 220 |
– |
cvt_sdcolor(unsc, &ndp->sd->rLambBack); |
| 221 |
– |
} |
| 546 |
|
} |
| 547 |
< |
multcolor(unsc, ndp->pr->pcol); |
| 547 |
> |
setcolor(unsc, 0, 0, 0); |
| 548 |
|
if (dfp == NULL) /* no specular component? */ |
| 549 |
|
return(0); |
| 550 |
< |
/* below sampling threshold? */ |
| 551 |
< |
if (dfp->maxHemi <= specthresh+FTINY) { |
| 552 |
< |
if (dfp->maxHemi > FTINY) { /* XXX no color from BSDF */ |
| 553 |
< |
double d = SDdirectHemi(ndp->vinc, sflags, ndp->sd); |
| 554 |
< |
COLOR ctmp; |
| 550 |
> |
|
| 551 |
> |
if (hasthru) { /* separate view sample? */ |
| 552 |
> |
RAY tr; |
| 553 |
> |
if (rayorigin(&tr, TRANS, ndp->pr, ndp->cthru) == 0) { |
| 554 |
> |
VCOPY(tr.rdir, ndp->pr->rdir); |
| 555 |
> |
rayvalue(&tr); |
| 556 |
> |
multcolor(tr.rcol, tr.rcoef); |
| 557 |
> |
addcolor(ndp->pr->rcol, tr.rcol); |
| 558 |
> |
++ntotal; |
| 559 |
> |
b = bright(ndp->cthru); |
| 560 |
> |
} else |
| 561 |
> |
hasthru = 0; |
| 562 |
> |
} |
| 563 |
> |
if (dfp->maxHemi - b <= FTINY) { /* have specular to sample? */ |
| 564 |
> |
b = 0; |
| 565 |
> |
} else { |
| 566 |
> |
FVECT vjit; |
| 567 |
> |
bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]); |
| 568 |
> |
b = SDdirectHemi(vjit, sflags, ndp->sd) - b; |
| 569 |
> |
if (b < 0) b = 0; |
| 570 |
> |
} |
| 571 |
> |
if (b <= specthresh+FTINY) { /* below sampling threshold? */ |
| 572 |
> |
if (b > FTINY) { /* XXX no color from BSDF */ |
| 573 |
|
if (sflags == SDsampSpT) { |
| 574 |
< |
copycolor(ctmp, ndp->pr->pcol); |
| 575 |
< |
scalecolor(ctmp, d); |
| 574 |
> |
copycolor(unsc, ndp->pr->pcol); |
| 575 |
> |
scalecolor(unsc, b); |
| 576 |
|
} else /* no pattern on reflection */ |
| 577 |
< |
setcolor(ctmp, d, d, d); |
| 236 |
< |
addcolor(unsc, ctmp); |
| 577 |
> |
setcolor(unsc, b, b, b); |
| 578 |
|
} |
| 579 |
< |
return(0); |
| 579 |
> |
return(ntotal); |
| 580 |
|
} |
| 581 |
< |
/* else need to sample */ |
| 582 |
< |
dimlist[ndims++] = (int)(size_t)ndp->mp; |
| 242 |
< |
ndims++; |
| 581 |
> |
dimlist[ndims] = (int)(size_t)ndp->mp; /* else sample specular */ |
| 582 |
> |
ndims += 2; |
| 583 |
|
for (n = dfp->ncomp; n--; ) { /* loop over components */ |
| 584 |
|
dimlist[ndims-1] = n + 9438; |
| 585 |
|
ntotal += sample_sdcomp(ndp, &dfp->comp[n], sflags==SDsampSpT); |
| 592 |
|
int |
| 593 |
|
m_bsdf(OBJREC *m, RAY *r) |
| 594 |
|
{ |
| 595 |
+ |
int hasthick = (m->otype == MAT_BSDF); |
| 596 |
+ |
int hitfront; |
| 597 |
|
COLOR ctmp; |
| 598 |
|
SDError ec; |
| 599 |
< |
FVECT upvec, outVec; |
| 599 |
> |
FVECT upvec, vtmp; |
| 600 |
|
MFUNC *mf; |
| 601 |
|
BSDFDAT nd; |
| 602 |
|
/* check arguments */ |
| 603 |
< |
if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) | |
| 603 |
> |
if ((m->oargs.nsargs < hasthick+5) | (m->oargs.nfargs > 9) | |
| 604 |
|
(m->oargs.nfargs % 3)) |
| 605 |
|
objerror(m, USER, "bad # arguments"); |
| 606 |
< |
|
| 607 |
< |
/* get BSDF data */ |
| 266 |
< |
nd.sd = loadBSDF(m->oargs.sarg[1]); |
| 606 |
> |
/* record surface struck */ |
| 607 |
> |
hitfront = (r->rod > 0); |
| 608 |
|
/* load cal file */ |
| 609 |
< |
mf = getfunc(m, 5, 0x1d, 1); |
| 610 |
< |
/* get thickness */ |
| 611 |
< |
nd.thick = evalue(mf->ep[0]); |
| 612 |
< |
if (nd.thick < .0) |
| 613 |
< |
nd.thick = .0; |
| 614 |
< |
/* check shadow */ |
| 615 |
< |
if (r->crtype & SHADOW) { |
| 616 |
< |
if ((nd.thick > FTINY) & (nd.sd->tf != NULL)) |
| 276 |
< |
raytrans(r); /* pass-through */ |
| 277 |
< |
SDfreeCache(nd.sd); |
| 278 |
< |
return(1); /* else shadow */ |
| 609 |
> |
mf = hasthick ? getfunc(m, 5, 0x1d, 1) |
| 610 |
> |
: getfunc(m, 4, 0xe, 1) ; |
| 611 |
> |
setfunc(m, r); |
| 612 |
> |
nd.thick = 0; /* set thickness */ |
| 613 |
> |
if (hasthick) { |
| 614 |
> |
nd.thick = evalue(mf->ep[0]); |
| 615 |
> |
if ((-FTINY <= nd.thick) & (nd.thick <= FTINY)) |
| 616 |
> |
nd.thick = 0; |
| 617 |
|
} |
| 618 |
< |
/* check unscattered ray */ |
| 619 |
< |
if (!(r->crtype & (SPECULAR|AMBIENT)) && |
| 620 |
< |
(nd.thick > FTINY) & (nd.sd->tf != NULL)) { |
| 283 |
< |
SDfreeCache(nd.sd); |
| 284 |
< |
raytrans(r); /* pass-through */ |
| 618 |
> |
/* check backface visibility */ |
| 619 |
> |
if (!hitfront & !backvis) { |
| 620 |
> |
raytrans(r); |
| 621 |
|
return(1); |
| 622 |
|
} |
| 623 |
+ |
/* check other rays to pass */ |
| 624 |
+ |
if (nd.thick != 0 && (r->crtype & SHADOW || |
| 625 |
+ |
!(r->crtype & (SPECULAR|AMBIENT)) || |
| 626 |
+ |
(nd.thick > 0) ^ hitfront)) { |
| 627 |
+ |
raytrans(r); /* hide our proxy */ |
| 628 |
+ |
return(1); |
| 629 |
+ |
} |
| 630 |
+ |
if (hasthick && r->crtype & SHADOW) /* early shadow check #1 */ |
| 631 |
+ |
return(1); |
| 632 |
+ |
nd.mp = m; |
| 633 |
+ |
nd.pr = r; |
| 634 |
+ |
/* get BSDF data */ |
| 635 |
+ |
nd.sd = loadBSDF(m->oargs.sarg[hasthick]); |
| 636 |
+ |
/* early shadow check #2 */ |
| 637 |
+ |
if (r->crtype & SHADOW && (nd.sd->tf == NULL) & (nd.sd->tb == NULL)) |
| 638 |
+ |
return(1); |
| 639 |
|
/* diffuse reflectance */ |
| 640 |
< |
if (r->rod > .0) { |
| 641 |
< |
if (m->oargs.nfargs < 3) |
| 642 |
< |
setcolor(nd.rdiff, .0, .0, .0); |
| 643 |
< |
else |
| 292 |
< |
setcolor(nd.rdiff, m->oargs.farg[0], |
| 640 |
> |
if (hitfront) { |
| 641 |
> |
cvt_sdcolor(nd.rdiff, &nd.sd->rLambFront); |
| 642 |
> |
if (m->oargs.nfargs >= 3) { |
| 643 |
> |
setcolor(ctmp, m->oargs.farg[0], |
| 644 |
|
m->oargs.farg[1], |
| 645 |
|
m->oargs.farg[2]); |
| 646 |
+ |
addcolor(nd.rdiff, ctmp); |
| 647 |
+ |
} |
| 648 |
|
} else { |
| 649 |
< |
if (m->oargs.nfargs < 6) { /* check invisible backside */ |
| 650 |
< |
if (!backvis && (nd.sd->rb == NULL) & |
| 651 |
< |
(nd.sd->tf == NULL)) { |
| 299 |
< |
SDfreeCache(nd.sd); |
| 300 |
< |
raytrans(r); |
| 301 |
< |
return(1); |
| 302 |
< |
} |
| 303 |
< |
setcolor(nd.rdiff, .0, .0, .0); |
| 304 |
< |
} else |
| 305 |
< |
setcolor(nd.rdiff, m->oargs.farg[3], |
| 649 |
> |
cvt_sdcolor(nd.rdiff, &nd.sd->rLambBack); |
| 650 |
> |
if (m->oargs.nfargs >= 6) { |
| 651 |
> |
setcolor(ctmp, m->oargs.farg[3], |
| 652 |
|
m->oargs.farg[4], |
| 653 |
|
m->oargs.farg[5]); |
| 654 |
+ |
addcolor(nd.rdiff, ctmp); |
| 655 |
+ |
} |
| 656 |
|
} |
| 657 |
|
/* diffuse transmittance */ |
| 658 |
< |
if (m->oargs.nfargs < 9) |
| 659 |
< |
setcolor(nd.tdiff, .0, .0, .0); |
| 660 |
< |
else |
| 313 |
< |
setcolor(nd.tdiff, m->oargs.farg[6], |
| 658 |
> |
cvt_sdcolor(nd.tdiff, &nd.sd->tLamb); |
| 659 |
> |
if (m->oargs.nfargs >= 9) { |
| 660 |
> |
setcolor(ctmp, m->oargs.farg[6], |
| 661 |
|
m->oargs.farg[7], |
| 662 |
|
m->oargs.farg[8]); |
| 663 |
< |
nd.mp = m; |
| 664 |
< |
nd.pr = r; |
| 663 |
> |
addcolor(nd.tdiff, ctmp); |
| 664 |
> |
} |
| 665 |
|
/* get modifiers */ |
| 666 |
|
raytexture(r, m->omod); |
| 320 |
– |
if (bright(r->pcol) <= FTINY) { /* black pattern?! */ |
| 321 |
– |
SDfreeCache(nd.sd); |
| 322 |
– |
return(1); |
| 323 |
– |
} |
| 667 |
|
/* modify diffuse values */ |
| 668 |
|
multcolor(nd.rdiff, r->pcol); |
| 669 |
|
multcolor(nd.tdiff, r->pcol); |
| 670 |
|
/* get up vector */ |
| 671 |
< |
upvec[0] = evalue(mf->ep[1]); |
| 672 |
< |
upvec[1] = evalue(mf->ep[2]); |
| 673 |
< |
upvec[2] = evalue(mf->ep[3]); |
| 671 |
> |
upvec[0] = evalue(mf->ep[hasthick+0]); |
| 672 |
> |
upvec[1] = evalue(mf->ep[hasthick+1]); |
| 673 |
> |
upvec[2] = evalue(mf->ep[hasthick+2]); |
| 674 |
|
/* return to world coords */ |
| 675 |
< |
if (mf->f != &unitxf) { |
| 676 |
< |
multv3(upvec, upvec, mf->f->xfm); |
| 677 |
< |
nd.thick *= mf->f->sca; |
| 675 |
> |
if (mf->fxp != &unitxf) { |
| 676 |
> |
multv3(upvec, upvec, mf->fxp->xfm); |
| 677 |
> |
nd.thick *= mf->fxp->sca; |
| 678 |
|
} |
| 679 |
+ |
if (r->rox != NULL) { |
| 680 |
+ |
multv3(upvec, upvec, r->rox->f.xfm); |
| 681 |
+ |
nd.thick *= r->rox->f.sca; |
| 682 |
+ |
} |
| 683 |
|
raynormal(nd.pnorm, r); |
| 684 |
|
/* compute local BSDF xform */ |
| 685 |
|
ec = SDcompXform(nd.toloc, nd.pnorm, upvec); |
| 686 |
|
if (!ec) { |
| 687 |
< |
nd.vinc[0] = -r->rdir[0]; |
| 688 |
< |
nd.vinc[1] = -r->rdir[1]; |
| 689 |
< |
nd.vinc[2] = -r->rdir[2]; |
| 690 |
< |
ec = SDmapDir(nd.vinc, nd.toloc, nd.vinc); |
| 687 |
> |
nd.vray[0] = -r->rdir[0]; |
| 688 |
> |
nd.vray[1] = -r->rdir[1]; |
| 689 |
> |
nd.vray[2] = -r->rdir[2]; |
| 690 |
> |
ec = SDmapDir(nd.vray, nd.toloc, nd.vray); |
| 691 |
|
} |
| 345 |
– |
if (!ec) |
| 346 |
– |
ec = SDinvXform(nd.fromloc, nd.toloc); |
| 692 |
|
if (ec) { |
| 693 |
< |
objerror(m, WARNING, transSDError(ec)); |
| 349 |
< |
SDfreeCache(nd.sd); |
| 693 |
> |
objerror(m, WARNING, "Illegal orientation vector"); |
| 694 |
|
return(1); |
| 695 |
|
} |
| 696 |
< |
if (r->rod < .0) { /* perturb normal towards hit */ |
| 696 |
> |
setcolor(nd.cthru, 0, 0, 0); /* consider through component */ |
| 697 |
> |
if (m->otype == MAT_ABSDF) { |
| 698 |
> |
compute_through(&nd); |
| 699 |
> |
if (r->crtype & SHADOW) { |
| 700 |
> |
RAY tr; /* attempt to pass shadow ray */ |
| 701 |
> |
if (rayorigin(&tr, TRANS, r, nd.cthru) < 0) |
| 702 |
> |
return(1); /* no through component */ |
| 703 |
> |
VCOPY(tr.rdir, r->rdir); |
| 704 |
> |
rayvalue(&tr); /* transmit with scaling */ |
| 705 |
> |
multcolor(tr.rcol, tr.rcoef); |
| 706 |
> |
copycolor(r->rcol, tr.rcol); |
| 707 |
> |
return(1); /* we're done */ |
| 708 |
> |
} |
| 709 |
> |
} |
| 710 |
> |
ec = SDinvXform(nd.fromloc, nd.toloc); |
| 711 |
> |
if (!ec) /* determine BSDF resolution */ |
| 712 |
> |
ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, |
| 713 |
> |
SDqueryMin+SDqueryMax, nd.sd); |
| 714 |
> |
if (ec) |
| 715 |
> |
objerror(m, USER, transSDError(ec)); |
| 716 |
> |
|
| 717 |
> |
nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]); |
| 718 |
> |
nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]); |
| 719 |
> |
if (!hitfront) { /* perturb normal towards hit */ |
| 720 |
|
nd.pnorm[0] = -nd.pnorm[0]; |
| 721 |
|
nd.pnorm[1] = -nd.pnorm[1]; |
| 722 |
|
nd.pnorm[2] = -nd.pnorm[2]; |
| 728 |
|
/* compute indirect diffuse */ |
| 729 |
|
copycolor(ctmp, nd.rdiff); |
| 730 |
|
addcolor(ctmp, nd.runsamp); |
| 731 |
< |
if (bright(ctmp) > FTINY) { /* ambient from this side */ |
| 732 |
< |
if (r->rod < .0) |
| 731 |
> |
if (bright(ctmp) > FTINY) { /* ambient from reflection */ |
| 732 |
> |
if (!hitfront) |
| 733 |
|
flipsurface(r); |
| 734 |
|
multambient(ctmp, r, nd.pnorm); |
| 735 |
|
addcolor(r->rcol, ctmp); |
| 736 |
< |
if (r->rod < .0) |
| 736 |
> |
if (!hitfront) |
| 737 |
|
flipsurface(r); |
| 738 |
|
} |
| 739 |
|
copycolor(ctmp, nd.tdiff); |
| 740 |
|
addcolor(ctmp, nd.tunsamp); |
| 741 |
|
if (bright(ctmp) > FTINY) { /* ambient from other side */ |
| 742 |
|
FVECT bnorm; |
| 743 |
< |
if (r->rod > .0) |
| 743 |
> |
if (hitfront) |
| 744 |
|
flipsurface(r); |
| 745 |
|
bnorm[0] = -nd.pnorm[0]; |
| 746 |
|
bnorm[1] = -nd.pnorm[1]; |
| 747 |
|
bnorm[2] = -nd.pnorm[2]; |
| 748 |
< |
multambient(ctmp, r, bnorm); |
| 748 |
> |
if (nd.thick != 0) { /* proxy with offset? */ |
| 749 |
> |
VCOPY(vtmp, r->rop); |
| 750 |
> |
VSUM(r->rop, vtmp, r->ron, nd.thick); |
| 751 |
> |
multambient(ctmp, r, bnorm); |
| 752 |
> |
VCOPY(r->rop, vtmp); |
| 753 |
> |
} else |
| 754 |
> |
multambient(ctmp, r, bnorm); |
| 755 |
|
addcolor(r->rcol, ctmp); |
| 756 |
< |
if (r->rod > .0) |
| 756 |
> |
if (hitfront) |
| 757 |
|
flipsurface(r); |
| 758 |
|
} |
| 759 |
|
/* add direct component */ |
| 760 |
< |
direct(r, dirbsdf, &nd); |
| 760 |
> |
if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) & |
| 761 |
> |
(nd.sd->tb == NULL)) { |
| 762 |
> |
direct(r, dir_brdf, &nd); /* reflection only */ |
| 763 |
> |
} else if (nd.thick == 0) { |
| 764 |
> |
direct(r, dir_bsdf, &nd); /* thin surface scattering */ |
| 765 |
> |
} else { |
| 766 |
> |
direct(r, dir_brdf, &nd); /* reflection first */ |
| 767 |
> |
VCOPY(vtmp, r->rop); /* offset for transmitted */ |
| 768 |
> |
VSUM(r->rop, vtmp, r->ron, -nd.thick); |
| 769 |
> |
direct(r, dir_btdf, &nd); /* separate transmission */ |
| 770 |
> |
VCOPY(r->rop, vtmp); |
| 771 |
> |
} |
| 772 |
|
/* clean up */ |
| 773 |
|
SDfreeCache(nd.sd); |
| 774 |
|
return(1); |