| 1 |
greg |
2.1 |
#ifndef lint |
| 2 |
greg |
2.24 |
static const char RCSid[] = "$Id: m_bsdf.c,v 2.23 2013/04/09 16:55:15 greg Exp $"; |
| 3 |
greg |
2.1 |
#endif |
| 4 |
|
|
/* |
| 5 |
|
|
* Shading for materials with BSDFs taken from XML data files |
| 6 |
|
|
*/ |
| 7 |
|
|
|
| 8 |
|
|
#include "copyright.h" |
| 9 |
|
|
|
| 10 |
|
|
#include "ray.h" |
| 11 |
|
|
#include "ambient.h" |
| 12 |
|
|
#include "source.h" |
| 13 |
|
|
#include "func.h" |
| 14 |
|
|
#include "bsdf.h" |
| 15 |
|
|
#include "random.h" |
| 16 |
|
|
|
| 17 |
|
|
/* |
| 18 |
|
|
* Arguments to this material include optional diffuse colors. |
| 19 |
|
|
* String arguments include the BSDF and function files. |
| 20 |
greg |
2.5 |
* A non-zero thickness causes the strange but useful behavior |
| 21 |
|
|
* of translating transmitted rays this distance beneath the surface |
| 22 |
|
|
* (opposite the surface normal) to bypass any intervening geometry. |
| 23 |
|
|
* Translation only affects scattered, non-source-directed samples. |
| 24 |
|
|
* A non-zero thickness has the further side-effect that an unscattered |
| 25 |
greg |
2.1 |
* (view) ray will pass right through our material if it has any |
| 26 |
greg |
2.5 |
* non-diffuse transmission, making the BSDF surface invisible. This |
| 27 |
|
|
* shows the proxied geometry instead. Thickness has the further |
| 28 |
|
|
* effect of turning off reflection on the hidden side so that rays |
| 29 |
|
|
* 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. |
| 33 |
|
|
* A positive thickness hides geometry behind the BSDF surface and uses |
| 34 |
|
|
* front reflectance and transmission properties. A negative thickness |
| 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 |
greg |
2.1 |
* The "up" vector for the BSDF is given by three variables, defined |
| 39 |
|
|
* (along with the thickness) by the named function file, or '.' if none. |
| 40 |
|
|
* Together with the surface normal, this defines the local coordinate |
| 41 |
|
|
* system for the BSDF. |
| 42 |
|
|
* We do not reorient the surface, so if the BSDF has no back-side |
| 43 |
greg |
2.5 |
* reflectance and none is given in the real arguments, a BSDF surface |
| 44 |
|
|
* with zero thickness will appear black when viewed from behind |
| 45 |
|
|
* unless backface visibility is off. |
| 46 |
|
|
* The diffuse arguments are added to components in the BSDF file, |
| 47 |
greg |
2.1 |
* not multiplied. However, patterns affect this material as a multiplier |
| 48 |
|
|
* on everything except non-diffuse reflection. |
| 49 |
|
|
* |
| 50 |
|
|
* Arguments for MAT_BSDF are: |
| 51 |
|
|
* 6+ thick BSDFfile ux uy uz funcfile transform |
| 52 |
|
|
* 0 |
| 53 |
greg |
2.8 |
* 0|3|6|9 rdf gdf bdf |
| 54 |
greg |
2.1 |
* rdb gdb bdb |
| 55 |
|
|
* rdt gdt bdt |
| 56 |
|
|
*/ |
| 57 |
|
|
|
| 58 |
greg |
2.4 |
/* |
| 59 |
|
|
* Note that our reverse ray-tracing process means that the positions |
| 60 |
|
|
* of incoming and outgoing vectors may be reversed in our calls |
| 61 |
|
|
* to the BSDF library. This is fine, since the bidirectional nature |
| 62 |
|
|
* of the BSDF (that's what the 'B' stands for) means it all works out. |
| 63 |
|
|
*/ |
| 64 |
|
|
|
| 65 |
greg |
2.1 |
typedef struct { |
| 66 |
|
|
OBJREC *mp; /* material pointer */ |
| 67 |
|
|
RAY *pr; /* intersected ray */ |
| 68 |
|
|
FVECT pnorm; /* perturbed surface normal */ |
| 69 |
greg |
2.4 |
FVECT vray; /* local outgoing (return) vector */ |
| 70 |
greg |
2.9 |
double sr_vpsa[2]; /* sqrt of BSDF projected solid angle extrema */ |
| 71 |
greg |
2.1 |
RREAL toloc[3][3]; /* world to local BSDF coords */ |
| 72 |
|
|
RREAL fromloc[3][3]; /* local BSDF coords to world */ |
| 73 |
|
|
double thick; /* surface thickness */ |
| 74 |
|
|
SDData *sd; /* loaded BSDF data */ |
| 75 |
|
|
COLOR runsamp; /* BSDF hemispherical reflection */ |
| 76 |
|
|
COLOR rdiff; /* added diffuse reflection */ |
| 77 |
|
|
COLOR tunsamp; /* BSDF hemispherical transmission */ |
| 78 |
|
|
COLOR tdiff; /* added diffuse transmission */ |
| 79 |
|
|
} BSDFDAT; /* BSDF material data */ |
| 80 |
|
|
|
| 81 |
|
|
#define cvt_sdcolor(cv, svp) ccy2rgb(&(svp)->spec, (svp)->cieY, cv) |
| 82 |
|
|
|
| 83 |
greg |
2.4 |
/* Jitter ray sample according to projected solid angle and specjitter */ |
| 84 |
|
|
static void |
| 85 |
greg |
2.15 |
bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa) |
| 86 |
greg |
2.4 |
{ |
| 87 |
|
|
VCOPY(vres, ndp->vray); |
| 88 |
|
|
if (specjitter < 1.) |
| 89 |
|
|
sr_psa *= specjitter; |
| 90 |
|
|
if (sr_psa <= FTINY) |
| 91 |
|
|
return; |
| 92 |
|
|
vres[0] += sr_psa*(.5 - frandom()); |
| 93 |
|
|
vres[1] += sr_psa*(.5 - frandom()); |
| 94 |
|
|
normalize(vres); |
| 95 |
|
|
} |
| 96 |
|
|
|
| 97 |
greg |
2.7 |
/* Evaluate BSDF for direct component, returning true if OK to proceed */ |
| 98 |
|
|
static int |
| 99 |
greg |
2.13 |
direct_bsdf_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp) |
| 100 |
greg |
2.7 |
{ |
| 101 |
greg |
2.15 |
int nsamp, ok = 0; |
| 102 |
greg |
2.13 |
FVECT vsrc, vsmp, vjit; |
| 103 |
|
|
double tomega; |
| 104 |
greg |
2.15 |
double sf, tsr, sd[2]; |
| 105 |
greg |
2.13 |
COLOR csmp; |
| 106 |
greg |
2.7 |
SDValue sv; |
| 107 |
|
|
SDError ec; |
| 108 |
greg |
2.13 |
int i; |
| 109 |
greg |
2.7 |
/* transform source direction */ |
| 110 |
|
|
if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone) |
| 111 |
|
|
return(0); |
| 112 |
greg |
2.16 |
/* assign number of samples */ |
| 113 |
|
|
ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd); |
| 114 |
|
|
if (ec) |
| 115 |
|
|
goto baderror; |
| 116 |
greg |
2.13 |
/* check indirect over-counting */ |
| 117 |
|
|
if (ndp->thick != 0 && ndp->pr->crtype & (SPECULAR|AMBIENT) |
| 118 |
|
|
&& vsrc[2] > 0 ^ ndp->vray[2] > 0) { |
| 119 |
|
|
double dx = vsrc[0] + ndp->vray[0]; |
| 120 |
|
|
double dy = vsrc[1] + ndp->vray[1]; |
| 121 |
greg |
2.16 |
if (dx*dx + dy*dy <= omega+tomega) |
| 122 |
greg |
2.7 |
return(0); |
| 123 |
|
|
} |
| 124 |
greg |
2.15 |
sf = specjitter * ndp->pr->rweight; |
| 125 |
greg |
2.24 |
if (tomega <= .0) |
| 126 |
|
|
nsamp = 1; |
| 127 |
|
|
else if (25.*tomega <= omega) |
| 128 |
greg |
2.15 |
nsamp = 100.*sf + .5; |
| 129 |
|
|
else |
| 130 |
|
|
nsamp = 4.*sf*omega/tomega + .5; |
| 131 |
|
|
nsamp += !nsamp; |
| 132 |
|
|
setcolor(cval, .0, .0, .0); /* sample our source area */ |
| 133 |
greg |
2.13 |
sf = sqrt(omega); |
| 134 |
greg |
2.15 |
tsr = sqrt(tomega); |
| 135 |
greg |
2.13 |
for (i = nsamp; i--; ) { |
| 136 |
|
|
VCOPY(vsmp, vsrc); /* jitter query directions */ |
| 137 |
|
|
if (nsamp > 1) { |
| 138 |
|
|
multisamp(sd, 2, (i + frandom())/(double)nsamp); |
| 139 |
|
|
vsmp[0] += (sd[0] - .5)*sf; |
| 140 |
|
|
vsmp[1] += (sd[1] - .5)*sf; |
| 141 |
|
|
if (normalize(vsmp) == 0) { |
| 142 |
|
|
--nsamp; |
| 143 |
|
|
continue; |
| 144 |
|
|
} |
| 145 |
|
|
} |
| 146 |
greg |
2.15 |
bsdf_jitter(vjit, ndp, tsr); |
| 147 |
greg |
2.13 |
/* compute BSDF */ |
| 148 |
|
|
ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd); |
| 149 |
|
|
if (ec) |
| 150 |
|
|
goto baderror; |
| 151 |
|
|
if (sv.cieY <= FTINY) /* worth using? */ |
| 152 |
|
|
continue; |
| 153 |
|
|
cvt_sdcolor(csmp, &sv); |
| 154 |
|
|
addcolor(cval, csmp); /* average it in */ |
| 155 |
|
|
++ok; |
| 156 |
|
|
} |
| 157 |
|
|
sf = 1./(double)nsamp; |
| 158 |
|
|
scalecolor(cval, sf); |
| 159 |
|
|
return(ok); |
| 160 |
|
|
baderror: |
| 161 |
|
|
objerror(ndp->mp, USER, transSDError(ec)); |
| 162 |
greg |
2.17 |
return(0); /* gratis return */ |
| 163 |
greg |
2.7 |
} |
| 164 |
|
|
|
| 165 |
greg |
2.5 |
/* Compute source contribution for BSDF (reflected & transmitted) */ |
| 166 |
greg |
2.1 |
static void |
| 167 |
greg |
2.5 |
dir_bsdf( |
| 168 |
greg |
2.1 |
COLOR cval, /* returned coefficient */ |
| 169 |
|
|
void *nnp, /* material data */ |
| 170 |
|
|
FVECT ldir, /* light source direction */ |
| 171 |
|
|
double omega /* light source size */ |
| 172 |
|
|
) |
| 173 |
|
|
{ |
| 174 |
greg |
2.3 |
BSDFDAT *np = (BSDFDAT *)nnp; |
| 175 |
greg |
2.1 |
double ldot; |
| 176 |
|
|
double dtmp; |
| 177 |
|
|
COLOR ctmp; |
| 178 |
|
|
|
| 179 |
|
|
setcolor(cval, .0, .0, .0); |
| 180 |
|
|
|
| 181 |
|
|
ldot = DOT(np->pnorm, ldir); |
| 182 |
|
|
if ((-FTINY <= ldot) & (ldot <= FTINY)) |
| 183 |
|
|
return; |
| 184 |
|
|
|
| 185 |
greg |
2.9 |
if (ldot > 0 && bright(np->rdiff) > FTINY) { |
| 186 |
greg |
2.1 |
/* |
| 187 |
|
|
* Compute added diffuse reflected component. |
| 188 |
|
|
*/ |
| 189 |
|
|
copycolor(ctmp, np->rdiff); |
| 190 |
|
|
dtmp = ldot * omega * (1./PI); |
| 191 |
|
|
scalecolor(ctmp, dtmp); |
| 192 |
|
|
addcolor(cval, ctmp); |
| 193 |
|
|
} |
| 194 |
greg |
2.9 |
if (ldot < 0 && bright(np->tdiff) > FTINY) { |
| 195 |
greg |
2.1 |
/* |
| 196 |
|
|
* Compute added diffuse transmission. |
| 197 |
|
|
*/ |
| 198 |
|
|
copycolor(ctmp, np->tdiff); |
| 199 |
|
|
dtmp = -ldot * omega * (1.0/PI); |
| 200 |
|
|
scalecolor(ctmp, dtmp); |
| 201 |
|
|
addcolor(cval, ctmp); |
| 202 |
|
|
} |
| 203 |
|
|
/* |
| 204 |
|
|
* Compute scattering coefficient using BSDF. |
| 205 |
|
|
*/ |
| 206 |
greg |
2.13 |
if (!direct_bsdf_OK(ctmp, ldir, omega, np)) |
| 207 |
greg |
2.1 |
return; |
| 208 |
greg |
2.9 |
if (ldot > 0) { /* pattern only diffuse reflection */ |
| 209 |
greg |
2.1 |
COLOR ctmp1, ctmp2; |
| 210 |
greg |
2.9 |
dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY |
| 211 |
greg |
2.1 |
: np->sd->rLambBack.cieY; |
| 212 |
greg |
2.7 |
/* diffuse fraction */ |
| 213 |
|
|
dtmp /= PI * bright(ctmp); |
| 214 |
greg |
2.1 |
copycolor(ctmp2, np->pr->pcol); |
| 215 |
|
|
scalecolor(ctmp2, dtmp); |
| 216 |
|
|
setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp); |
| 217 |
|
|
addcolor(ctmp1, ctmp2); |
| 218 |
greg |
2.3 |
multcolor(ctmp, ctmp1); /* apply derated pattern */ |
| 219 |
greg |
2.1 |
dtmp = ldot * omega; |
| 220 |
|
|
} else { /* full pattern on transmission */ |
| 221 |
|
|
multcolor(ctmp, np->pr->pcol); |
| 222 |
|
|
dtmp = -ldot * omega; |
| 223 |
|
|
} |
| 224 |
|
|
scalecolor(ctmp, dtmp); |
| 225 |
|
|
addcolor(cval, ctmp); |
| 226 |
|
|
} |
| 227 |
|
|
|
| 228 |
greg |
2.5 |
/* Compute source contribution for BSDF (reflected only) */ |
| 229 |
|
|
static void |
| 230 |
|
|
dir_brdf( |
| 231 |
|
|
COLOR cval, /* returned coefficient */ |
| 232 |
|
|
void *nnp, /* material data */ |
| 233 |
|
|
FVECT ldir, /* light source direction */ |
| 234 |
|
|
double omega /* light source size */ |
| 235 |
|
|
) |
| 236 |
|
|
{ |
| 237 |
|
|
BSDFDAT *np = (BSDFDAT *)nnp; |
| 238 |
|
|
double ldot; |
| 239 |
|
|
double dtmp; |
| 240 |
|
|
COLOR ctmp, ctmp1, ctmp2; |
| 241 |
|
|
|
| 242 |
|
|
setcolor(cval, .0, .0, .0); |
| 243 |
|
|
|
| 244 |
|
|
ldot = DOT(np->pnorm, ldir); |
| 245 |
|
|
|
| 246 |
|
|
if (ldot <= FTINY) |
| 247 |
|
|
return; |
| 248 |
|
|
|
| 249 |
|
|
if (bright(np->rdiff) > FTINY) { |
| 250 |
|
|
/* |
| 251 |
|
|
* Compute added diffuse reflected component. |
| 252 |
|
|
*/ |
| 253 |
|
|
copycolor(ctmp, np->rdiff); |
| 254 |
|
|
dtmp = ldot * omega * (1./PI); |
| 255 |
|
|
scalecolor(ctmp, dtmp); |
| 256 |
|
|
addcolor(cval, ctmp); |
| 257 |
|
|
} |
| 258 |
|
|
/* |
| 259 |
|
|
* Compute reflection coefficient using BSDF. |
| 260 |
|
|
*/ |
| 261 |
greg |
2.13 |
if (!direct_bsdf_OK(ctmp, ldir, omega, np)) |
| 262 |
greg |
2.5 |
return; |
| 263 |
|
|
/* pattern only diffuse reflection */ |
| 264 |
greg |
2.9 |
dtmp = (np->pr->rod > 0) ? np->sd->rLambFront.cieY |
| 265 |
greg |
2.5 |
: np->sd->rLambBack.cieY; |
| 266 |
greg |
2.7 |
dtmp /= PI * bright(ctmp); /* diffuse fraction */ |
| 267 |
greg |
2.5 |
copycolor(ctmp2, np->pr->pcol); |
| 268 |
|
|
scalecolor(ctmp2, dtmp); |
| 269 |
|
|
setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp); |
| 270 |
|
|
addcolor(ctmp1, ctmp2); |
| 271 |
|
|
multcolor(ctmp, ctmp1); /* apply derated pattern */ |
| 272 |
|
|
dtmp = ldot * omega; |
| 273 |
|
|
scalecolor(ctmp, dtmp); |
| 274 |
|
|
addcolor(cval, ctmp); |
| 275 |
|
|
} |
| 276 |
|
|
|
| 277 |
|
|
/* Compute source contribution for BSDF (transmitted only) */ |
| 278 |
|
|
static void |
| 279 |
|
|
dir_btdf( |
| 280 |
|
|
COLOR cval, /* returned coefficient */ |
| 281 |
|
|
void *nnp, /* material data */ |
| 282 |
|
|
FVECT ldir, /* light source direction */ |
| 283 |
|
|
double omega /* light source size */ |
| 284 |
|
|
) |
| 285 |
|
|
{ |
| 286 |
|
|
BSDFDAT *np = (BSDFDAT *)nnp; |
| 287 |
|
|
double ldot; |
| 288 |
|
|
double dtmp; |
| 289 |
|
|
COLOR ctmp; |
| 290 |
|
|
|
| 291 |
|
|
setcolor(cval, .0, .0, .0); |
| 292 |
|
|
|
| 293 |
|
|
ldot = DOT(np->pnorm, ldir); |
| 294 |
|
|
|
| 295 |
|
|
if (ldot >= -FTINY) |
| 296 |
|
|
return; |
| 297 |
|
|
|
| 298 |
|
|
if (bright(np->tdiff) > FTINY) { |
| 299 |
|
|
/* |
| 300 |
|
|
* Compute added diffuse transmission. |
| 301 |
|
|
*/ |
| 302 |
|
|
copycolor(ctmp, np->tdiff); |
| 303 |
|
|
dtmp = -ldot * omega * (1.0/PI); |
| 304 |
|
|
scalecolor(ctmp, dtmp); |
| 305 |
|
|
addcolor(cval, ctmp); |
| 306 |
|
|
} |
| 307 |
|
|
/* |
| 308 |
|
|
* Compute scattering coefficient using BSDF. |
| 309 |
|
|
*/ |
| 310 |
greg |
2.13 |
if (!direct_bsdf_OK(ctmp, ldir, omega, np)) |
| 311 |
greg |
2.5 |
return; |
| 312 |
|
|
/* full pattern on transmission */ |
| 313 |
|
|
multcolor(ctmp, np->pr->pcol); |
| 314 |
|
|
dtmp = -ldot * omega; |
| 315 |
|
|
scalecolor(ctmp, dtmp); |
| 316 |
|
|
addcolor(cval, ctmp); |
| 317 |
|
|
} |
| 318 |
|
|
|
| 319 |
greg |
2.1 |
/* Sample separate BSDF component */ |
| 320 |
|
|
static int |
| 321 |
|
|
sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat) |
| 322 |
|
|
{ |
| 323 |
|
|
int nstarget = 1; |
| 324 |
greg |
2.11 |
int nsent; |
| 325 |
greg |
2.1 |
SDError ec; |
| 326 |
|
|
SDValue bsv; |
| 327 |
greg |
2.11 |
double xrand; |
| 328 |
greg |
2.10 |
FVECT vsmp; |
| 329 |
greg |
2.1 |
RAY sr; |
| 330 |
|
|
/* multiple samples? */ |
| 331 |
|
|
if (specjitter > 1.5) { |
| 332 |
|
|
nstarget = specjitter*ndp->pr->rweight + .5; |
| 333 |
greg |
2.14 |
nstarget += !nstarget; |
| 334 |
greg |
2.1 |
} |
| 335 |
greg |
2.11 |
/* run through our samples */ |
| 336 |
|
|
for (nsent = 0; nsent < nstarget; nsent++) { |
| 337 |
greg |
2.15 |
if (nstarget == 1) { /* stratify random variable */ |
| 338 |
greg |
2.11 |
xrand = urand(ilhash(dimlist,ndims)+samplendx); |
| 339 |
greg |
2.15 |
if (specjitter < 1.) |
| 340 |
|
|
xrand = .5 + specjitter*(xrand-.5); |
| 341 |
|
|
} else { |
| 342 |
greg |
2.11 |
xrand = (nsent + frandom())/(double)nstarget; |
| 343 |
greg |
2.15 |
} |
| 344 |
greg |
2.11 |
SDerrorDetail[0] = '\0'; /* sample direction & coef. */ |
| 345 |
greg |
2.15 |
bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]); |
| 346 |
greg |
2.11 |
ec = SDsampComponent(&bsv, vsmp, xrand, dcp); |
| 347 |
greg |
2.1 |
if (ec) |
| 348 |
greg |
2.2 |
objerror(ndp->mp, USER, transSDError(ec)); |
| 349 |
greg |
2.11 |
if (bsv.cieY <= FTINY) /* zero component? */ |
| 350 |
greg |
2.1 |
break; |
| 351 |
|
|
/* map vector to world */ |
| 352 |
greg |
2.4 |
if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone) |
| 353 |
greg |
2.1 |
break; |
| 354 |
|
|
/* spawn a specular ray */ |
| 355 |
|
|
if (nstarget > 1) |
| 356 |
|
|
bsv.cieY /= (double)nstarget; |
| 357 |
greg |
2.11 |
cvt_sdcolor(sr.rcoef, &bsv); /* use sample color */ |
| 358 |
|
|
if (usepat) /* apply pattern? */ |
| 359 |
greg |
2.1 |
multcolor(sr.rcoef, ndp->pr->pcol); |
| 360 |
|
|
if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) { |
| 361 |
greg |
2.11 |
if (maxdepth > 0) |
| 362 |
greg |
2.1 |
break; |
| 363 |
greg |
2.11 |
continue; /* Russian roulette victim */ |
| 364 |
greg |
2.1 |
} |
| 365 |
greg |
2.5 |
/* need to offset origin? */ |
| 366 |
greg |
2.9 |
if (ndp->thick != 0 && ndp->pr->rod > 0 ^ vsmp[2] > 0) |
| 367 |
greg |
2.5 |
VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick); |
| 368 |
greg |
2.1 |
rayvalue(&sr); /* send & evaluate sample */ |
| 369 |
|
|
multcolor(sr.rcol, sr.rcoef); |
| 370 |
|
|
addcolor(ndp->pr->rcol, sr.rcol); |
| 371 |
|
|
} |
| 372 |
|
|
return(nsent); |
| 373 |
|
|
} |
| 374 |
|
|
|
| 375 |
|
|
/* Sample non-diffuse components of BSDF */ |
| 376 |
|
|
static int |
| 377 |
|
|
sample_sdf(BSDFDAT *ndp, int sflags) |
| 378 |
|
|
{ |
| 379 |
|
|
int n, ntotal = 0; |
| 380 |
|
|
SDSpectralDF *dfp; |
| 381 |
|
|
COLORV *unsc; |
| 382 |
|
|
|
| 383 |
|
|
if (sflags == SDsampSpT) { |
| 384 |
|
|
unsc = ndp->tunsamp; |
| 385 |
greg |
2.22 |
if (ndp->pr->rod > 0) |
| 386 |
|
|
dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb; |
| 387 |
|
|
else |
| 388 |
|
|
dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf; |
| 389 |
greg |
2.1 |
cvt_sdcolor(unsc, &ndp->sd->tLamb); |
| 390 |
|
|
} else /* sflags == SDsampSpR */ { |
| 391 |
|
|
unsc = ndp->runsamp; |
| 392 |
greg |
2.9 |
if (ndp->pr->rod > 0) { |
| 393 |
greg |
2.1 |
dfp = ndp->sd->rf; |
| 394 |
|
|
cvt_sdcolor(unsc, &ndp->sd->rLambFront); |
| 395 |
|
|
} else { |
| 396 |
|
|
dfp = ndp->sd->rb; |
| 397 |
|
|
cvt_sdcolor(unsc, &ndp->sd->rLambBack); |
| 398 |
|
|
} |
| 399 |
|
|
} |
| 400 |
|
|
multcolor(unsc, ndp->pr->pcol); |
| 401 |
|
|
if (dfp == NULL) /* no specular component? */ |
| 402 |
|
|
return(0); |
| 403 |
|
|
/* below sampling threshold? */ |
| 404 |
|
|
if (dfp->maxHemi <= specthresh+FTINY) { |
| 405 |
greg |
2.3 |
if (dfp->maxHemi > FTINY) { /* XXX no color from BSDF */ |
| 406 |
greg |
2.4 |
FVECT vjit; |
| 407 |
|
|
double d; |
| 408 |
greg |
2.1 |
COLOR ctmp; |
| 409 |
greg |
2.15 |
bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]); |
| 410 |
greg |
2.4 |
d = SDdirectHemi(vjit, sflags, ndp->sd); |
| 411 |
greg |
2.1 |
if (sflags == SDsampSpT) { |
| 412 |
|
|
copycolor(ctmp, ndp->pr->pcol); |
| 413 |
|
|
scalecolor(ctmp, d); |
| 414 |
|
|
} else /* no pattern on reflection */ |
| 415 |
|
|
setcolor(ctmp, d, d, d); |
| 416 |
|
|
addcolor(unsc, ctmp); |
| 417 |
|
|
} |
| 418 |
|
|
return(0); |
| 419 |
|
|
} |
| 420 |
|
|
/* else need to sample */ |
| 421 |
|
|
dimlist[ndims++] = (int)(size_t)ndp->mp; |
| 422 |
|
|
ndims++; |
| 423 |
|
|
for (n = dfp->ncomp; n--; ) { /* loop over components */ |
| 424 |
|
|
dimlist[ndims-1] = n + 9438; |
| 425 |
|
|
ntotal += sample_sdcomp(ndp, &dfp->comp[n], sflags==SDsampSpT); |
| 426 |
|
|
} |
| 427 |
|
|
ndims -= 2; |
| 428 |
|
|
return(ntotal); |
| 429 |
|
|
} |
| 430 |
|
|
|
| 431 |
|
|
/* Color a ray that hit a BSDF material */ |
| 432 |
|
|
int |
| 433 |
|
|
m_bsdf(OBJREC *m, RAY *r) |
| 434 |
|
|
{ |
| 435 |
greg |
2.6 |
int hitfront; |
| 436 |
greg |
2.1 |
COLOR ctmp; |
| 437 |
|
|
SDError ec; |
| 438 |
greg |
2.5 |
FVECT upvec, vtmp; |
| 439 |
greg |
2.1 |
MFUNC *mf; |
| 440 |
|
|
BSDFDAT nd; |
| 441 |
|
|
/* check arguments */ |
| 442 |
|
|
if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) | |
| 443 |
|
|
(m->oargs.nfargs % 3)) |
| 444 |
|
|
objerror(m, USER, "bad # arguments"); |
| 445 |
greg |
2.6 |
/* record surface struck */ |
| 446 |
greg |
2.9 |
hitfront = (r->rod > 0); |
| 447 |
greg |
2.1 |
/* load cal file */ |
| 448 |
|
|
mf = getfunc(m, 5, 0x1d, 1); |
| 449 |
|
|
/* get thickness */ |
| 450 |
|
|
nd.thick = evalue(mf->ep[0]); |
| 451 |
greg |
2.5 |
if ((-FTINY <= nd.thick) & (nd.thick <= FTINY)) |
| 452 |
greg |
2.1 |
nd.thick = .0; |
| 453 |
|
|
/* check shadow */ |
| 454 |
|
|
if (r->crtype & SHADOW) { |
| 455 |
greg |
2.9 |
if (nd.thick != 0) |
| 456 |
greg |
2.3 |
raytrans(r); /* pass-through */ |
| 457 |
greg |
2.5 |
return(1); /* or shadow */ |
| 458 |
greg |
2.1 |
} |
| 459 |
greg |
2.5 |
/* check other rays to pass */ |
| 460 |
greg |
2.9 |
if (nd.thick != 0 && (!(r->crtype & (SPECULAR|AMBIENT)) || |
| 461 |
|
|
nd.thick > 0 ^ hitfront)) { |
| 462 |
greg |
2.5 |
raytrans(r); /* hide our proxy */ |
| 463 |
greg |
2.1 |
return(1); |
| 464 |
|
|
} |
| 465 |
greg |
2.5 |
/* get BSDF data */ |
| 466 |
|
|
nd.sd = loadBSDF(m->oargs.sarg[1]); |
| 467 |
greg |
2.1 |
/* diffuse reflectance */ |
| 468 |
greg |
2.6 |
if (hitfront) { |
| 469 |
greg |
2.1 |
if (m->oargs.nfargs < 3) |
| 470 |
|
|
setcolor(nd.rdiff, .0, .0, .0); |
| 471 |
|
|
else |
| 472 |
|
|
setcolor(nd.rdiff, m->oargs.farg[0], |
| 473 |
|
|
m->oargs.farg[1], |
| 474 |
|
|
m->oargs.farg[2]); |
| 475 |
|
|
} else { |
| 476 |
|
|
if (m->oargs.nfargs < 6) { /* check invisible backside */ |
| 477 |
greg |
2.3 |
if (!backvis && (nd.sd->rb == NULL) & |
| 478 |
greg |
2.22 |
(nd.sd->tb == NULL)) { |
| 479 |
greg |
2.1 |
SDfreeCache(nd.sd); |
| 480 |
|
|
raytrans(r); |
| 481 |
|
|
return(1); |
| 482 |
|
|
} |
| 483 |
|
|
setcolor(nd.rdiff, .0, .0, .0); |
| 484 |
|
|
} else |
| 485 |
|
|
setcolor(nd.rdiff, m->oargs.farg[3], |
| 486 |
|
|
m->oargs.farg[4], |
| 487 |
|
|
m->oargs.farg[5]); |
| 488 |
|
|
} |
| 489 |
|
|
/* diffuse transmittance */ |
| 490 |
|
|
if (m->oargs.nfargs < 9) |
| 491 |
|
|
setcolor(nd.tdiff, .0, .0, .0); |
| 492 |
|
|
else |
| 493 |
|
|
setcolor(nd.tdiff, m->oargs.farg[6], |
| 494 |
|
|
m->oargs.farg[7], |
| 495 |
|
|
m->oargs.farg[8]); |
| 496 |
|
|
nd.mp = m; |
| 497 |
|
|
nd.pr = r; |
| 498 |
|
|
/* get modifiers */ |
| 499 |
|
|
raytexture(r, m->omod); |
| 500 |
|
|
/* modify diffuse values */ |
| 501 |
|
|
multcolor(nd.rdiff, r->pcol); |
| 502 |
|
|
multcolor(nd.tdiff, r->pcol); |
| 503 |
|
|
/* get up vector */ |
| 504 |
|
|
upvec[0] = evalue(mf->ep[1]); |
| 505 |
|
|
upvec[1] = evalue(mf->ep[2]); |
| 506 |
|
|
upvec[2] = evalue(mf->ep[3]); |
| 507 |
|
|
/* return to world coords */ |
| 508 |
greg |
2.21 |
if (mf->fxp != &unitxf) { |
| 509 |
|
|
multv3(upvec, upvec, mf->fxp->xfm); |
| 510 |
|
|
nd.thick *= mf->fxp->sca; |
| 511 |
greg |
2.1 |
} |
| 512 |
greg |
2.23 |
if (r->rox != NULL) { |
| 513 |
|
|
multv3(upvec, upvec, r->rox->f.xfm); |
| 514 |
|
|
nd.thick *= r->rox->f.sca; |
| 515 |
|
|
} |
| 516 |
greg |
2.1 |
raynormal(nd.pnorm, r); |
| 517 |
|
|
/* compute local BSDF xform */ |
| 518 |
|
|
ec = SDcompXform(nd.toloc, nd.pnorm, upvec); |
| 519 |
|
|
if (!ec) { |
| 520 |
greg |
2.4 |
nd.vray[0] = -r->rdir[0]; |
| 521 |
|
|
nd.vray[1] = -r->rdir[1]; |
| 522 |
|
|
nd.vray[2] = -r->rdir[2]; |
| 523 |
|
|
ec = SDmapDir(nd.vray, nd.toloc, nd.vray); |
| 524 |
greg |
2.20 |
} |
| 525 |
|
|
if (!ec) |
| 526 |
|
|
ec = SDinvXform(nd.fromloc, nd.toloc); |
| 527 |
greg |
2.19 |
if (ec) { |
| 528 |
|
|
objerror(m, WARNING, "Illegal orientation vector"); |
| 529 |
|
|
return(1); |
| 530 |
greg |
2.1 |
} |
| 531 |
greg |
2.4 |
/* determine BSDF resolution */ |
| 532 |
greg |
2.20 |
ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin+SDqueryMax, nd.sd); |
| 533 |
|
|
if (ec) |
| 534 |
|
|
objerror(m, USER, transSDError(ec)); |
| 535 |
|
|
|
| 536 |
greg |
2.9 |
nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]); |
| 537 |
|
|
nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]); |
| 538 |
greg |
2.6 |
if (!hitfront) { /* perturb normal towards hit */ |
| 539 |
greg |
2.1 |
nd.pnorm[0] = -nd.pnorm[0]; |
| 540 |
|
|
nd.pnorm[1] = -nd.pnorm[1]; |
| 541 |
|
|
nd.pnorm[2] = -nd.pnorm[2]; |
| 542 |
|
|
} |
| 543 |
|
|
/* sample reflection */ |
| 544 |
|
|
sample_sdf(&nd, SDsampSpR); |
| 545 |
|
|
/* sample transmission */ |
| 546 |
|
|
sample_sdf(&nd, SDsampSpT); |
| 547 |
|
|
/* compute indirect diffuse */ |
| 548 |
|
|
copycolor(ctmp, nd.rdiff); |
| 549 |
|
|
addcolor(ctmp, nd.runsamp); |
| 550 |
greg |
2.5 |
if (bright(ctmp) > FTINY) { /* ambient from reflection */ |
| 551 |
greg |
2.6 |
if (!hitfront) |
| 552 |
greg |
2.1 |
flipsurface(r); |
| 553 |
|
|
multambient(ctmp, r, nd.pnorm); |
| 554 |
|
|
addcolor(r->rcol, ctmp); |
| 555 |
greg |
2.6 |
if (!hitfront) |
| 556 |
greg |
2.1 |
flipsurface(r); |
| 557 |
|
|
} |
| 558 |
|
|
copycolor(ctmp, nd.tdiff); |
| 559 |
|
|
addcolor(ctmp, nd.tunsamp); |
| 560 |
|
|
if (bright(ctmp) > FTINY) { /* ambient from other side */ |
| 561 |
|
|
FVECT bnorm; |
| 562 |
greg |
2.6 |
if (hitfront) |
| 563 |
greg |
2.1 |
flipsurface(r); |
| 564 |
|
|
bnorm[0] = -nd.pnorm[0]; |
| 565 |
|
|
bnorm[1] = -nd.pnorm[1]; |
| 566 |
|
|
bnorm[2] = -nd.pnorm[2]; |
| 567 |
greg |
2.9 |
if (nd.thick != 0) { /* proxy with offset? */ |
| 568 |
greg |
2.5 |
VCOPY(vtmp, r->rop); |
| 569 |
greg |
2.18 |
VSUM(r->rop, vtmp, r->ron, nd.thick); |
| 570 |
greg |
2.5 |
multambient(ctmp, r, bnorm); |
| 571 |
|
|
VCOPY(r->rop, vtmp); |
| 572 |
|
|
} else |
| 573 |
|
|
multambient(ctmp, r, bnorm); |
| 574 |
greg |
2.1 |
addcolor(r->rcol, ctmp); |
| 575 |
greg |
2.6 |
if (hitfront) |
| 576 |
greg |
2.1 |
flipsurface(r); |
| 577 |
|
|
} |
| 578 |
|
|
/* add direct component */ |
| 579 |
greg |
2.22 |
if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) & |
| 580 |
|
|
(nd.sd->tb == NULL)) { |
| 581 |
greg |
2.5 |
direct(r, dir_brdf, &nd); /* reflection only */ |
| 582 |
greg |
2.9 |
} else if (nd.thick == 0) { |
| 583 |
greg |
2.5 |
direct(r, dir_bsdf, &nd); /* thin surface scattering */ |
| 584 |
|
|
} else { |
| 585 |
|
|
direct(r, dir_brdf, &nd); /* reflection first */ |
| 586 |
|
|
VCOPY(vtmp, r->rop); /* offset for transmitted */ |
| 587 |
|
|
VSUM(r->rop, vtmp, r->ron, -nd.thick); |
| 588 |
greg |
2.6 |
direct(r, dir_btdf, &nd); /* separate transmission */ |
| 589 |
greg |
2.5 |
VCOPY(r->rop, vtmp); |
| 590 |
|
|
} |
| 591 |
greg |
2.1 |
/* clean up */ |
| 592 |
|
|
SDfreeCache(nd.sd); |
| 593 |
|
|
return(1); |
| 594 |
|
|
} |