| 1 |
greg |
2.1 |
#ifndef lint
|
| 2 |
greg |
2.8 |
static const char RCSid[] = "$Id: m_bsdf.c,v 2.7 2011/02/22 05:41:02 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 |
|
|
double sr_vpsa; /* sqrt of BSDF projected solid angle */
|
| 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 |
|
|
bsdf_jitter(FVECT vres, BSDFDAT *ndp)
|
| 86 |
|
|
{
|
| 87 |
|
|
double sr_psa = ndp->sr_vpsa;
|
| 88 |
|
|
|
| 89 |
|
|
VCOPY(vres, ndp->vray);
|
| 90 |
|
|
if (specjitter < 1.)
|
| 91 |
|
|
sr_psa *= specjitter;
|
| 92 |
|
|
if (sr_psa <= FTINY)
|
| 93 |
|
|
return;
|
| 94 |
|
|
vres[0] += sr_psa*(.5 - frandom());
|
| 95 |
|
|
vres[1] += sr_psa*(.5 - frandom());
|
| 96 |
|
|
normalize(vres);
|
| 97 |
|
|
}
|
| 98 |
|
|
|
| 99 |
greg |
2.7 |
/* Evaluate BSDF for direct component, returning true if OK to proceed */
|
| 100 |
|
|
static int
|
| 101 |
|
|
direct_bsdf_OK(COLOR cval, FVECT ldir, BSDFDAT *ndp)
|
| 102 |
|
|
{
|
| 103 |
|
|
FVECT vsrc, vjit;
|
| 104 |
|
|
SDValue sv;
|
| 105 |
|
|
SDError ec;
|
| 106 |
|
|
/* transform source direction */
|
| 107 |
|
|
if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone)
|
| 108 |
|
|
return(0);
|
| 109 |
|
|
/* jitter query direction */
|
| 110 |
|
|
bsdf_jitter(vjit, ndp);
|
| 111 |
|
|
/* avoid indirect over-counting */
|
| 112 |
|
|
if (ndp->thick != .0 && ndp->pr->crtype & (SPECULAR|AMBIENT) &&
|
| 113 |
|
|
vsrc[2] > .0 ^ vjit[2] > .0) {
|
| 114 |
|
|
double dx = vsrc[0] + vjit[0];
|
| 115 |
|
|
double dy = vsrc[1] + vjit[1];
|
| 116 |
|
|
if (dx*dx + dy*dy <= ndp->sr_vpsa*ndp->sr_vpsa)
|
| 117 |
|
|
return(0);
|
| 118 |
|
|
}
|
| 119 |
|
|
ec = SDevalBSDF(&sv, vjit, vsrc, ndp->sd);
|
| 120 |
|
|
if (ec)
|
| 121 |
|
|
objerror(ndp->mp, USER, transSDError(ec));
|
| 122 |
|
|
|
| 123 |
|
|
if (sv.cieY <= FTINY) /* not worth using? */
|
| 124 |
|
|
return(0);
|
| 125 |
|
|
/* else we're good to go */
|
| 126 |
|
|
cvt_sdcolor(cval, &sv);
|
| 127 |
|
|
return(1);
|
| 128 |
|
|
}
|
| 129 |
|
|
|
| 130 |
greg |
2.5 |
/* Compute source contribution for BSDF (reflected & transmitted) */
|
| 131 |
greg |
2.1 |
static void
|
| 132 |
greg |
2.5 |
dir_bsdf(
|
| 133 |
greg |
2.1 |
COLOR cval, /* returned coefficient */
|
| 134 |
|
|
void *nnp, /* material data */
|
| 135 |
|
|
FVECT ldir, /* light source direction */
|
| 136 |
|
|
double omega /* light source size */
|
| 137 |
|
|
)
|
| 138 |
|
|
{
|
| 139 |
greg |
2.3 |
BSDFDAT *np = (BSDFDAT *)nnp;
|
| 140 |
greg |
2.1 |
double ldot;
|
| 141 |
|
|
double dtmp;
|
| 142 |
|
|
COLOR ctmp;
|
| 143 |
|
|
|
| 144 |
|
|
setcolor(cval, .0, .0, .0);
|
| 145 |
|
|
|
| 146 |
|
|
ldot = DOT(np->pnorm, ldir);
|
| 147 |
|
|
if ((-FTINY <= ldot) & (ldot <= FTINY))
|
| 148 |
|
|
return;
|
| 149 |
|
|
|
| 150 |
|
|
if (ldot > .0 && bright(np->rdiff) > FTINY) {
|
| 151 |
|
|
/*
|
| 152 |
|
|
* Compute added diffuse reflected component.
|
| 153 |
|
|
*/
|
| 154 |
|
|
copycolor(ctmp, np->rdiff);
|
| 155 |
|
|
dtmp = ldot * omega * (1./PI);
|
| 156 |
|
|
scalecolor(ctmp, dtmp);
|
| 157 |
|
|
addcolor(cval, ctmp);
|
| 158 |
|
|
}
|
| 159 |
|
|
if (ldot < .0 && bright(np->tdiff) > FTINY) {
|
| 160 |
|
|
/*
|
| 161 |
|
|
* Compute added diffuse transmission.
|
| 162 |
|
|
*/
|
| 163 |
|
|
copycolor(ctmp, np->tdiff);
|
| 164 |
|
|
dtmp = -ldot * omega * (1.0/PI);
|
| 165 |
|
|
scalecolor(ctmp, dtmp);
|
| 166 |
|
|
addcolor(cval, ctmp);
|
| 167 |
|
|
}
|
| 168 |
|
|
/*
|
| 169 |
|
|
* Compute scattering coefficient using BSDF.
|
| 170 |
|
|
*/
|
| 171 |
greg |
2.7 |
if (!direct_bsdf_OK(ctmp, ldir, np))
|
| 172 |
greg |
2.1 |
return;
|
| 173 |
|
|
if (ldot > .0) { /* pattern only diffuse reflection */
|
| 174 |
|
|
COLOR ctmp1, ctmp2;
|
| 175 |
|
|
dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY
|
| 176 |
|
|
: np->sd->rLambBack.cieY;
|
| 177 |
greg |
2.7 |
/* diffuse fraction */
|
| 178 |
|
|
dtmp /= PI * bright(ctmp);
|
| 179 |
greg |
2.1 |
copycolor(ctmp2, np->pr->pcol);
|
| 180 |
|
|
scalecolor(ctmp2, dtmp);
|
| 181 |
|
|
setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
|
| 182 |
|
|
addcolor(ctmp1, ctmp2);
|
| 183 |
greg |
2.3 |
multcolor(ctmp, ctmp1); /* apply derated pattern */
|
| 184 |
greg |
2.1 |
dtmp = ldot * omega;
|
| 185 |
|
|
} else { /* full pattern on transmission */
|
| 186 |
|
|
multcolor(ctmp, np->pr->pcol);
|
| 187 |
|
|
dtmp = -ldot * omega;
|
| 188 |
|
|
}
|
| 189 |
|
|
scalecolor(ctmp, dtmp);
|
| 190 |
|
|
addcolor(cval, ctmp);
|
| 191 |
|
|
}
|
| 192 |
|
|
|
| 193 |
greg |
2.5 |
/* Compute source contribution for BSDF (reflected only) */
|
| 194 |
|
|
static void
|
| 195 |
|
|
dir_brdf(
|
| 196 |
|
|
COLOR cval, /* returned coefficient */
|
| 197 |
|
|
void *nnp, /* material data */
|
| 198 |
|
|
FVECT ldir, /* light source direction */
|
| 199 |
|
|
double omega /* light source size */
|
| 200 |
|
|
)
|
| 201 |
|
|
{
|
| 202 |
|
|
BSDFDAT *np = (BSDFDAT *)nnp;
|
| 203 |
|
|
double ldot;
|
| 204 |
|
|
double dtmp;
|
| 205 |
|
|
COLOR ctmp, ctmp1, ctmp2;
|
| 206 |
|
|
|
| 207 |
|
|
setcolor(cval, .0, .0, .0);
|
| 208 |
|
|
|
| 209 |
|
|
ldot = DOT(np->pnorm, ldir);
|
| 210 |
|
|
|
| 211 |
|
|
if (ldot <= FTINY)
|
| 212 |
|
|
return;
|
| 213 |
|
|
|
| 214 |
|
|
if (bright(np->rdiff) > FTINY) {
|
| 215 |
|
|
/*
|
| 216 |
|
|
* Compute added diffuse reflected component.
|
| 217 |
|
|
*/
|
| 218 |
|
|
copycolor(ctmp, np->rdiff);
|
| 219 |
|
|
dtmp = ldot * omega * (1./PI);
|
| 220 |
|
|
scalecolor(ctmp, dtmp);
|
| 221 |
|
|
addcolor(cval, ctmp);
|
| 222 |
|
|
}
|
| 223 |
|
|
/*
|
| 224 |
|
|
* Compute reflection coefficient using BSDF.
|
| 225 |
|
|
*/
|
| 226 |
greg |
2.7 |
if (!direct_bsdf_OK(ctmp, ldir, np))
|
| 227 |
greg |
2.5 |
return;
|
| 228 |
|
|
/* pattern only diffuse reflection */
|
| 229 |
|
|
dtmp = (np->pr->rod > .0) ? np->sd->rLambFront.cieY
|
| 230 |
|
|
: np->sd->rLambBack.cieY;
|
| 231 |
greg |
2.7 |
dtmp /= PI * bright(ctmp); /* diffuse fraction */
|
| 232 |
greg |
2.5 |
copycolor(ctmp2, np->pr->pcol);
|
| 233 |
|
|
scalecolor(ctmp2, dtmp);
|
| 234 |
|
|
setcolor(ctmp1, 1.-dtmp, 1.-dtmp, 1.-dtmp);
|
| 235 |
|
|
addcolor(ctmp1, ctmp2);
|
| 236 |
|
|
multcolor(ctmp, ctmp1); /* apply derated pattern */
|
| 237 |
|
|
dtmp = ldot * omega;
|
| 238 |
|
|
scalecolor(ctmp, dtmp);
|
| 239 |
|
|
addcolor(cval, ctmp);
|
| 240 |
|
|
}
|
| 241 |
|
|
|
| 242 |
|
|
/* Compute source contribution for BSDF (transmitted only) */
|
| 243 |
|
|
static void
|
| 244 |
|
|
dir_btdf(
|
| 245 |
|
|
COLOR cval, /* returned coefficient */
|
| 246 |
|
|
void *nnp, /* material data */
|
| 247 |
|
|
FVECT ldir, /* light source direction */
|
| 248 |
|
|
double omega /* light source size */
|
| 249 |
|
|
)
|
| 250 |
|
|
{
|
| 251 |
|
|
BSDFDAT *np = (BSDFDAT *)nnp;
|
| 252 |
|
|
double ldot;
|
| 253 |
|
|
double dtmp;
|
| 254 |
|
|
COLOR ctmp;
|
| 255 |
|
|
|
| 256 |
|
|
setcolor(cval, .0, .0, .0);
|
| 257 |
|
|
|
| 258 |
|
|
ldot = DOT(np->pnorm, ldir);
|
| 259 |
|
|
|
| 260 |
|
|
if (ldot >= -FTINY)
|
| 261 |
|
|
return;
|
| 262 |
|
|
|
| 263 |
|
|
if (bright(np->tdiff) > FTINY) {
|
| 264 |
|
|
/*
|
| 265 |
|
|
* Compute added diffuse transmission.
|
| 266 |
|
|
*/
|
| 267 |
|
|
copycolor(ctmp, np->tdiff);
|
| 268 |
|
|
dtmp = -ldot * omega * (1.0/PI);
|
| 269 |
|
|
scalecolor(ctmp, dtmp);
|
| 270 |
|
|
addcolor(cval, ctmp);
|
| 271 |
|
|
}
|
| 272 |
|
|
/*
|
| 273 |
|
|
* Compute scattering coefficient using BSDF.
|
| 274 |
|
|
*/
|
| 275 |
greg |
2.7 |
if (!direct_bsdf_OK(ctmp, ldir, np))
|
| 276 |
greg |
2.5 |
return;
|
| 277 |
|
|
/* full pattern on transmission */
|
| 278 |
|
|
multcolor(ctmp, np->pr->pcol);
|
| 279 |
|
|
dtmp = -ldot * omega;
|
| 280 |
|
|
scalecolor(ctmp, dtmp);
|
| 281 |
|
|
addcolor(cval, ctmp);
|
| 282 |
|
|
}
|
| 283 |
|
|
|
| 284 |
greg |
2.1 |
/* Sample separate BSDF component */
|
| 285 |
|
|
static int
|
| 286 |
|
|
sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int usepat)
|
| 287 |
|
|
{
|
| 288 |
|
|
int nstarget = 1;
|
| 289 |
|
|
int nsent = 0;
|
| 290 |
|
|
SDError ec;
|
| 291 |
|
|
SDValue bsv;
|
| 292 |
|
|
double sthick;
|
| 293 |
greg |
2.4 |
FVECT vjit, vsmp;
|
| 294 |
greg |
2.1 |
RAY sr;
|
| 295 |
|
|
int ntrials;
|
| 296 |
|
|
/* multiple samples? */
|
| 297 |
|
|
if (specjitter > 1.5) {
|
| 298 |
|
|
nstarget = specjitter*ndp->pr->rweight + .5;
|
| 299 |
|
|
if (nstarget < 1)
|
| 300 |
|
|
nstarget = 1;
|
| 301 |
|
|
}
|
| 302 |
|
|
/* run through our trials */
|
| 303 |
|
|
for (ntrials = 0; nsent < nstarget && ntrials < 9*nstarget; ntrials++) {
|
| 304 |
|
|
SDerrorDetail[0] = '\0';
|
| 305 |
|
|
/* sample direction & coef. */
|
| 306 |
greg |
2.4 |
bsdf_jitter(vjit, ndp);
|
| 307 |
|
|
ec = SDsampComponent(&bsv, vsmp, vjit, ntrials ? frandom()
|
| 308 |
|
|
: urand(ilhash(dimlist,ndims)+samplendx), dcp);
|
| 309 |
greg |
2.1 |
if (ec)
|
| 310 |
greg |
2.2 |
objerror(ndp->mp, USER, transSDError(ec));
|
| 311 |
greg |
2.1 |
/* zero component? */
|
| 312 |
|
|
if (bsv.cieY <= FTINY)
|
| 313 |
|
|
break;
|
| 314 |
|
|
/* map vector to world */
|
| 315 |
greg |
2.4 |
if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone)
|
| 316 |
greg |
2.1 |
break;
|
| 317 |
|
|
/* unintentional penetration? */
|
| 318 |
greg |
2.4 |
if (DOT(sr.rdir, ndp->pr->ron) > .0 ^ vsmp[2] > .0)
|
| 319 |
greg |
2.1 |
continue;
|
| 320 |
|
|
/* spawn a specular ray */
|
| 321 |
|
|
if (nstarget > 1)
|
| 322 |
|
|
bsv.cieY /= (double)nstarget;
|
| 323 |
|
|
cvt_sdcolor(sr.rcoef, &bsv); /* use color */
|
| 324 |
|
|
if (usepat) /* pattern on transmission */
|
| 325 |
|
|
multcolor(sr.rcoef, ndp->pr->pcol);
|
| 326 |
|
|
if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) {
|
| 327 |
|
|
if (maxdepth > 0)
|
| 328 |
|
|
break;
|
| 329 |
|
|
++nsent; /* Russian roulette victim */
|
| 330 |
|
|
continue;
|
| 331 |
|
|
}
|
| 332 |
greg |
2.5 |
/* need to offset origin? */
|
| 333 |
|
|
if (ndp->thick != .0 && ndp->pr->rod > .0 ^ vsmp[2] > .0)
|
| 334 |
|
|
VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick);
|
| 335 |
greg |
2.1 |
rayvalue(&sr); /* send & evaluate sample */
|
| 336 |
|
|
multcolor(sr.rcol, sr.rcoef);
|
| 337 |
|
|
addcolor(ndp->pr->rcol, sr.rcol);
|
| 338 |
|
|
++nsent;
|
| 339 |
|
|
}
|
| 340 |
|
|
return(nsent);
|
| 341 |
|
|
}
|
| 342 |
|
|
|
| 343 |
|
|
/* Sample non-diffuse components of BSDF */
|
| 344 |
|
|
static int
|
| 345 |
|
|
sample_sdf(BSDFDAT *ndp, int sflags)
|
| 346 |
|
|
{
|
| 347 |
|
|
int n, ntotal = 0;
|
| 348 |
|
|
SDSpectralDF *dfp;
|
| 349 |
|
|
COLORV *unsc;
|
| 350 |
|
|
|
| 351 |
|
|
if (sflags == SDsampSpT) {
|
| 352 |
|
|
unsc = ndp->tunsamp;
|
| 353 |
|
|
dfp = ndp->sd->tf;
|
| 354 |
|
|
cvt_sdcolor(unsc, &ndp->sd->tLamb);
|
| 355 |
|
|
} else /* sflags == SDsampSpR */ {
|
| 356 |
|
|
unsc = ndp->runsamp;
|
| 357 |
|
|
if (ndp->pr->rod > .0) {
|
| 358 |
|
|
dfp = ndp->sd->rf;
|
| 359 |
|
|
cvt_sdcolor(unsc, &ndp->sd->rLambFront);
|
| 360 |
|
|
} else {
|
| 361 |
|
|
dfp = ndp->sd->rb;
|
| 362 |
|
|
cvt_sdcolor(unsc, &ndp->sd->rLambBack);
|
| 363 |
|
|
}
|
| 364 |
|
|
}
|
| 365 |
|
|
multcolor(unsc, ndp->pr->pcol);
|
| 366 |
|
|
if (dfp == NULL) /* no specular component? */
|
| 367 |
|
|
return(0);
|
| 368 |
|
|
/* below sampling threshold? */
|
| 369 |
|
|
if (dfp->maxHemi <= specthresh+FTINY) {
|
| 370 |
greg |
2.3 |
if (dfp->maxHemi > FTINY) { /* XXX no color from BSDF */
|
| 371 |
greg |
2.4 |
FVECT vjit;
|
| 372 |
|
|
double d;
|
| 373 |
greg |
2.1 |
COLOR ctmp;
|
| 374 |
greg |
2.4 |
bsdf_jitter(vjit, ndp);
|
| 375 |
|
|
d = SDdirectHemi(vjit, sflags, ndp->sd);
|
| 376 |
greg |
2.1 |
if (sflags == SDsampSpT) {
|
| 377 |
|
|
copycolor(ctmp, ndp->pr->pcol);
|
| 378 |
|
|
scalecolor(ctmp, d);
|
| 379 |
|
|
} else /* no pattern on reflection */
|
| 380 |
|
|
setcolor(ctmp, d, d, d);
|
| 381 |
|
|
addcolor(unsc, ctmp);
|
| 382 |
|
|
}
|
| 383 |
|
|
return(0);
|
| 384 |
|
|
}
|
| 385 |
|
|
/* else need to sample */
|
| 386 |
|
|
dimlist[ndims++] = (int)(size_t)ndp->mp;
|
| 387 |
|
|
ndims++;
|
| 388 |
|
|
for (n = dfp->ncomp; n--; ) { /* loop over components */
|
| 389 |
|
|
dimlist[ndims-1] = n + 9438;
|
| 390 |
|
|
ntotal += sample_sdcomp(ndp, &dfp->comp[n], sflags==SDsampSpT);
|
| 391 |
|
|
}
|
| 392 |
|
|
ndims -= 2;
|
| 393 |
|
|
return(ntotal);
|
| 394 |
|
|
}
|
| 395 |
|
|
|
| 396 |
|
|
/* Color a ray that hit a BSDF material */
|
| 397 |
|
|
int
|
| 398 |
|
|
m_bsdf(OBJREC *m, RAY *r)
|
| 399 |
|
|
{
|
| 400 |
greg |
2.6 |
int hitfront;
|
| 401 |
greg |
2.1 |
COLOR ctmp;
|
| 402 |
|
|
SDError ec;
|
| 403 |
greg |
2.5 |
FVECT upvec, vtmp;
|
| 404 |
greg |
2.1 |
MFUNC *mf;
|
| 405 |
|
|
BSDFDAT nd;
|
| 406 |
|
|
/* check arguments */
|
| 407 |
|
|
if ((m->oargs.nsargs < 6) | (m->oargs.nfargs > 9) |
|
| 408 |
|
|
(m->oargs.nfargs % 3))
|
| 409 |
|
|
objerror(m, USER, "bad # arguments");
|
| 410 |
greg |
2.6 |
/* record surface struck */
|
| 411 |
|
|
hitfront = (r->rod > .0);
|
| 412 |
greg |
2.1 |
/* load cal file */
|
| 413 |
|
|
mf = getfunc(m, 5, 0x1d, 1);
|
| 414 |
|
|
/* get thickness */
|
| 415 |
|
|
nd.thick = evalue(mf->ep[0]);
|
| 416 |
greg |
2.5 |
if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
|
| 417 |
greg |
2.1 |
nd.thick = .0;
|
| 418 |
|
|
/* check shadow */
|
| 419 |
|
|
if (r->crtype & SHADOW) {
|
| 420 |
greg |
2.5 |
if (nd.thick != .0)
|
| 421 |
greg |
2.3 |
raytrans(r); /* pass-through */
|
| 422 |
greg |
2.5 |
return(1); /* or shadow */
|
| 423 |
greg |
2.1 |
}
|
| 424 |
greg |
2.5 |
/* check other rays to pass */
|
| 425 |
greg |
2.7 |
if (nd.thick != .0 && (!(r->crtype & (SPECULAR|AMBIENT)) ||
|
| 426 |
greg |
2.6 |
nd.thick > .0 ^ hitfront)) {
|
| 427 |
greg |
2.5 |
raytrans(r); /* hide our proxy */
|
| 428 |
greg |
2.1 |
return(1);
|
| 429 |
|
|
}
|
| 430 |
greg |
2.5 |
/* get BSDF data */
|
| 431 |
|
|
nd.sd = loadBSDF(m->oargs.sarg[1]);
|
| 432 |
greg |
2.1 |
/* diffuse reflectance */
|
| 433 |
greg |
2.6 |
if (hitfront) {
|
| 434 |
greg |
2.1 |
if (m->oargs.nfargs < 3)
|
| 435 |
|
|
setcolor(nd.rdiff, .0, .0, .0);
|
| 436 |
|
|
else
|
| 437 |
|
|
setcolor(nd.rdiff, m->oargs.farg[0],
|
| 438 |
|
|
m->oargs.farg[1],
|
| 439 |
|
|
m->oargs.farg[2]);
|
| 440 |
|
|
} else {
|
| 441 |
|
|
if (m->oargs.nfargs < 6) { /* check invisible backside */
|
| 442 |
greg |
2.3 |
if (!backvis && (nd.sd->rb == NULL) &
|
| 443 |
|
|
(nd.sd->tf == NULL)) {
|
| 444 |
greg |
2.1 |
SDfreeCache(nd.sd);
|
| 445 |
|
|
raytrans(r);
|
| 446 |
|
|
return(1);
|
| 447 |
|
|
}
|
| 448 |
|
|
setcolor(nd.rdiff, .0, .0, .0);
|
| 449 |
|
|
} else
|
| 450 |
|
|
setcolor(nd.rdiff, m->oargs.farg[3],
|
| 451 |
|
|
m->oargs.farg[4],
|
| 452 |
|
|
m->oargs.farg[5]);
|
| 453 |
|
|
}
|
| 454 |
|
|
/* diffuse transmittance */
|
| 455 |
|
|
if (m->oargs.nfargs < 9)
|
| 456 |
|
|
setcolor(nd.tdiff, .0, .0, .0);
|
| 457 |
|
|
else
|
| 458 |
|
|
setcolor(nd.tdiff, m->oargs.farg[6],
|
| 459 |
|
|
m->oargs.farg[7],
|
| 460 |
|
|
m->oargs.farg[8]);
|
| 461 |
|
|
nd.mp = m;
|
| 462 |
|
|
nd.pr = r;
|
| 463 |
|
|
/* get modifiers */
|
| 464 |
|
|
raytexture(r, m->omod);
|
| 465 |
|
|
/* modify diffuse values */
|
| 466 |
|
|
multcolor(nd.rdiff, r->pcol);
|
| 467 |
|
|
multcolor(nd.tdiff, r->pcol);
|
| 468 |
|
|
/* get up vector */
|
| 469 |
|
|
upvec[0] = evalue(mf->ep[1]);
|
| 470 |
|
|
upvec[1] = evalue(mf->ep[2]);
|
| 471 |
|
|
upvec[2] = evalue(mf->ep[3]);
|
| 472 |
|
|
/* return to world coords */
|
| 473 |
|
|
if (mf->f != &unitxf) {
|
| 474 |
|
|
multv3(upvec, upvec, mf->f->xfm);
|
| 475 |
|
|
nd.thick *= mf->f->sca;
|
| 476 |
|
|
}
|
| 477 |
|
|
raynormal(nd.pnorm, r);
|
| 478 |
|
|
/* compute local BSDF xform */
|
| 479 |
|
|
ec = SDcompXform(nd.toloc, nd.pnorm, upvec);
|
| 480 |
|
|
if (!ec) {
|
| 481 |
greg |
2.4 |
nd.vray[0] = -r->rdir[0];
|
| 482 |
|
|
nd.vray[1] = -r->rdir[1];
|
| 483 |
|
|
nd.vray[2] = -r->rdir[2];
|
| 484 |
|
|
ec = SDmapDir(nd.vray, nd.toloc, nd.vray);
|
| 485 |
greg |
2.1 |
}
|
| 486 |
|
|
if (!ec)
|
| 487 |
|
|
ec = SDinvXform(nd.fromloc, nd.toloc);
|
| 488 |
greg |
2.4 |
/* determine BSDF resolution */
|
| 489 |
|
|
if (!ec)
|
| 490 |
|
|
ec = SDsizeBSDF(&nd.sr_vpsa, nd.vray, SDqueryMin, nd.sd);
|
| 491 |
|
|
if (!ec)
|
| 492 |
|
|
nd.sr_vpsa = sqrt(nd.sr_vpsa);
|
| 493 |
|
|
else {
|
| 494 |
greg |
2.2 |
objerror(m, WARNING, transSDError(ec));
|
| 495 |
greg |
2.1 |
SDfreeCache(nd.sd);
|
| 496 |
|
|
return(1);
|
| 497 |
|
|
}
|
| 498 |
greg |
2.6 |
if (!hitfront) { /* perturb normal towards hit */
|
| 499 |
greg |
2.1 |
nd.pnorm[0] = -nd.pnorm[0];
|
| 500 |
|
|
nd.pnorm[1] = -nd.pnorm[1];
|
| 501 |
|
|
nd.pnorm[2] = -nd.pnorm[2];
|
| 502 |
|
|
}
|
| 503 |
|
|
/* sample reflection */
|
| 504 |
|
|
sample_sdf(&nd, SDsampSpR);
|
| 505 |
|
|
/* sample transmission */
|
| 506 |
|
|
sample_sdf(&nd, SDsampSpT);
|
| 507 |
|
|
/* compute indirect diffuse */
|
| 508 |
|
|
copycolor(ctmp, nd.rdiff);
|
| 509 |
|
|
addcolor(ctmp, nd.runsamp);
|
| 510 |
greg |
2.5 |
if (bright(ctmp) > FTINY) { /* ambient from reflection */
|
| 511 |
greg |
2.6 |
if (!hitfront)
|
| 512 |
greg |
2.1 |
flipsurface(r);
|
| 513 |
|
|
multambient(ctmp, r, nd.pnorm);
|
| 514 |
|
|
addcolor(r->rcol, ctmp);
|
| 515 |
greg |
2.6 |
if (!hitfront)
|
| 516 |
greg |
2.1 |
flipsurface(r);
|
| 517 |
|
|
}
|
| 518 |
|
|
copycolor(ctmp, nd.tdiff);
|
| 519 |
|
|
addcolor(ctmp, nd.tunsamp);
|
| 520 |
|
|
if (bright(ctmp) > FTINY) { /* ambient from other side */
|
| 521 |
|
|
FVECT bnorm;
|
| 522 |
greg |
2.6 |
if (hitfront)
|
| 523 |
greg |
2.1 |
flipsurface(r);
|
| 524 |
|
|
bnorm[0] = -nd.pnorm[0];
|
| 525 |
|
|
bnorm[1] = -nd.pnorm[1];
|
| 526 |
|
|
bnorm[2] = -nd.pnorm[2];
|
| 527 |
greg |
2.5 |
if (nd.thick != .0) { /* proxy with offset? */
|
| 528 |
|
|
VCOPY(vtmp, r->rop);
|
| 529 |
|
|
VSUM(r->rop, vtmp, r->ron, -nd.thick);
|
| 530 |
|
|
multambient(ctmp, r, bnorm);
|
| 531 |
|
|
VCOPY(r->rop, vtmp);
|
| 532 |
|
|
} else
|
| 533 |
|
|
multambient(ctmp, r, bnorm);
|
| 534 |
greg |
2.1 |
addcolor(r->rcol, ctmp);
|
| 535 |
greg |
2.6 |
if (hitfront)
|
| 536 |
greg |
2.1 |
flipsurface(r);
|
| 537 |
|
|
}
|
| 538 |
|
|
/* add direct component */
|
| 539 |
greg |
2.5 |
if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL)) {
|
| 540 |
|
|
direct(r, dir_brdf, &nd); /* reflection only */
|
| 541 |
|
|
} else if (nd.thick == .0) {
|
| 542 |
|
|
direct(r, dir_bsdf, &nd); /* thin surface scattering */
|
| 543 |
|
|
} else {
|
| 544 |
|
|
direct(r, dir_brdf, &nd); /* reflection first */
|
| 545 |
|
|
VCOPY(vtmp, r->rop); /* offset for transmitted */
|
| 546 |
|
|
VSUM(r->rop, vtmp, r->ron, -nd.thick);
|
| 547 |
greg |
2.6 |
direct(r, dir_btdf, &nd); /* separate transmission */
|
| 548 |
greg |
2.5 |
VCOPY(r->rop, vtmp);
|
| 549 |
|
|
}
|
| 550 |
greg |
2.1 |
/* clean up */
|
| 551 |
|
|
SDfreeCache(nd.sd);
|
| 552 |
|
|
return(1);
|
| 553 |
|
|
}
|