| 1 |
#ifndef lint |
| 2 |
static const char RCSid[] = "$Id: m_brdf.c,v 2.26 2010/09/26 15:51:15 greg Exp $"; |
| 3 |
#endif |
| 4 |
/* |
| 5 |
* Shading for materials with arbitrary BRDF's |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "copyright.h" |
| 9 |
|
| 10 |
#include "ray.h" |
| 11 |
#include "ambient.h" |
| 12 |
#include "data.h" |
| 13 |
#include "source.h" |
| 14 |
#include "otypes.h" |
| 15 |
#include "rtotypes.h" |
| 16 |
#include "func.h" |
| 17 |
|
| 18 |
/* |
| 19 |
* Arguments to this material include the color and specularity. |
| 20 |
* String arguments include the reflection function and files. |
| 21 |
* The BRDF is currently used just for the specular component to light |
| 22 |
* sources. Reflectance values or data coordinates are functions |
| 23 |
* of the direction to the light source. (Data modification functions |
| 24 |
* are passed the source direction as args 2-4.) |
| 25 |
* We orient the surface towards the incoming ray, so a single |
| 26 |
* surface can be used to represent an infinitely thin object. |
| 27 |
* |
| 28 |
* Arguments for MAT_PFUNC and MAT_MFUNC are: |
| 29 |
* 2+ func funcfile transform |
| 30 |
* 0 |
| 31 |
* 4+ red grn blu specularity A5 .. |
| 32 |
* |
| 33 |
* Arguments for MAT_PDATA and MAT_MDATA are: |
| 34 |
* 4+ func datafile funcfile v0 .. transform |
| 35 |
* 0 |
| 36 |
* 4+ red grn blu specularity A5 .. |
| 37 |
* |
| 38 |
* Arguments for MAT_TFUNC are: |
| 39 |
* 2+ func funcfile transform |
| 40 |
* 0 |
| 41 |
* 4+ red grn blu rspec trans tspec A7 .. |
| 42 |
* |
| 43 |
* Arguments for MAT_TDATA are: |
| 44 |
* 4+ func datafile funcfile v0 .. transform |
| 45 |
* 0 |
| 46 |
* 4+ red grn blu rspec trans tspec A7 .. |
| 47 |
* |
| 48 |
* Arguments for the more general MAT_BRTDF are: |
| 49 |
* 10+ rrefl grefl brefl |
| 50 |
* rtrns gtrns btrns |
| 51 |
* rbrtd gbrtd bbrtd |
| 52 |
* funcfile transform |
| 53 |
* 0 |
| 54 |
* 9+ rdf gdf bdf |
| 55 |
* rdb gdb bdb |
| 56 |
* rdt gdt bdt A10 .. |
| 57 |
* |
| 58 |
* In addition to the normal variables available to functions, |
| 59 |
* we define the following: |
| 60 |
* NxP, NyP, NzP - perturbed surface normal |
| 61 |
* RdotP - perturbed ray dot product |
| 62 |
* CrP, CgP, CbP - perturbed material color (or pattern) |
| 63 |
*/ |
| 64 |
|
| 65 |
typedef struct { |
| 66 |
OBJREC *mp; /* material pointer */ |
| 67 |
RAY *pr; /* intersected ray */ |
| 68 |
DATARRAY *dp; /* data array for PDATA, MDATA or TDATA */ |
| 69 |
COLOR mcolor; /* material (or pattern) color */ |
| 70 |
COLOR rdiff; /* diffuse reflection */ |
| 71 |
COLOR tdiff; /* diffuse transmission */ |
| 72 |
double rspec; /* specular reflectance (1 for BRDTF) */ |
| 73 |
double trans; /* transmissivity (.5 for BRDTF) */ |
| 74 |
double tspec; /* specular transmittance (1 for BRDTF) */ |
| 75 |
FVECT pnorm; /* perturbed surface normal */ |
| 76 |
double pdot; /* perturbed dot product */ |
| 77 |
} BRDFDAT; /* BRDF material data */ |
| 78 |
|
| 79 |
|
| 80 |
static srcdirf_t dirbrdf; |
| 81 |
static int setbrdfunc(BRDFDAT *np); |
| 82 |
|
| 83 |
|
| 84 |
static void |
| 85 |
dirbrdf( /* compute source contribution */ |
| 86 |
COLOR cval, /* returned coefficient */ |
| 87 |
void *nnp, /* material data */ |
| 88 |
FVECT ldir, /* light source direction */ |
| 89 |
double omega /* light source size */ |
| 90 |
) |
| 91 |
{ |
| 92 |
register BRDFDAT *np = nnp; |
| 93 |
double ldot; |
| 94 |
double dtmp; |
| 95 |
COLOR ctmp; |
| 96 |
FVECT ldx; |
| 97 |
static double vldx[5], pt[MAXDIM]; |
| 98 |
register char **sa; |
| 99 |
register int i; |
| 100 |
#define lddx (vldx+1) |
| 101 |
|
| 102 |
setcolor(cval, 0.0, 0.0, 0.0); |
| 103 |
|
| 104 |
ldot = DOT(np->pnorm, ldir); |
| 105 |
|
| 106 |
if (ldot <= FTINY && ldot >= -FTINY) |
| 107 |
return; /* too close to grazing */ |
| 108 |
|
| 109 |
if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY) |
| 110 |
return; /* wrong side */ |
| 111 |
|
| 112 |
if (ldot > 0.0) { |
| 113 |
/* |
| 114 |
* Compute and add diffuse reflected component to returned |
| 115 |
* color. The diffuse reflected component will always be |
| 116 |
* modified by the color of the material. |
| 117 |
*/ |
| 118 |
copycolor(ctmp, np->rdiff); |
| 119 |
dtmp = ldot * omega / PI; |
| 120 |
scalecolor(ctmp, dtmp); |
| 121 |
addcolor(cval, ctmp); |
| 122 |
} else { |
| 123 |
/* |
| 124 |
* Diffuse transmitted component. |
| 125 |
*/ |
| 126 |
copycolor(ctmp, np->tdiff); |
| 127 |
dtmp = -ldot * omega / PI; |
| 128 |
scalecolor(ctmp, dtmp); |
| 129 |
addcolor(cval, ctmp); |
| 130 |
} |
| 131 |
if (ldot > 0.0 ? np->rspec <= FTINY : np->tspec <= FTINY) |
| 132 |
return; /* diffuse only */ |
| 133 |
/* set up function */ |
| 134 |
setbrdfunc(np); |
| 135 |
sa = np->mp->oargs.sarg; |
| 136 |
errno = 0; |
| 137 |
/* transform light vector */ |
| 138 |
multv3(ldx, ldir, funcxf.xfm); |
| 139 |
for (i = 0; i < 3; i++) |
| 140 |
lddx[i] = ldx[i]/funcxf.sca; |
| 141 |
lddx[3] = omega; |
| 142 |
/* compute BRTDF */ |
| 143 |
if (np->mp->otype == MAT_BRTDF) { |
| 144 |
if (sa[6][0] == '0') /* special case */ |
| 145 |
colval(ctmp,RED) = 0.0; |
| 146 |
else |
| 147 |
colval(ctmp,RED) = funvalue(sa[6], 4, lddx); |
| 148 |
if (sa[7][0] == '0') |
| 149 |
colval(ctmp,GRN) = 0.0; |
| 150 |
else if (!strcmp(sa[7],sa[6])) |
| 151 |
colval(ctmp,GRN) = colval(ctmp,RED); |
| 152 |
else |
| 153 |
colval(ctmp,GRN) = funvalue(sa[7], 4, lddx); |
| 154 |
if (!strcmp(sa[8],sa[6])) |
| 155 |
colval(ctmp,BLU) = colval(ctmp,RED); |
| 156 |
else if (!strcmp(sa[8],sa[7])) |
| 157 |
colval(ctmp,BLU) = colval(ctmp,GRN); |
| 158 |
else |
| 159 |
colval(ctmp,BLU) = funvalue(sa[8], 4, lddx); |
| 160 |
dtmp = bright(ctmp); |
| 161 |
} else if (np->dp == NULL) { |
| 162 |
dtmp = funvalue(sa[0], 4, lddx); |
| 163 |
setcolor(ctmp, dtmp, dtmp, dtmp); |
| 164 |
} else { |
| 165 |
for (i = 0; i < np->dp->nd; i++) |
| 166 |
pt[i] = funvalue(sa[3+i], 4, lddx); |
| 167 |
vldx[0] = datavalue(np->dp, pt); |
| 168 |
dtmp = funvalue(sa[0], 5, vldx); |
| 169 |
setcolor(ctmp, dtmp, dtmp, dtmp); |
| 170 |
} |
| 171 |
if ((errno == EDOM) | (errno == ERANGE)) { |
| 172 |
objerror(np->mp, WARNING, "compute error"); |
| 173 |
return; |
| 174 |
} |
| 175 |
if (dtmp <= FTINY) |
| 176 |
return; |
| 177 |
if (ldot > 0.0) { |
| 178 |
/* |
| 179 |
* Compute reflected non-diffuse component. |
| 180 |
*/ |
| 181 |
if ((np->mp->otype == MAT_MFUNC) | (np->mp->otype == MAT_MDATA)) |
| 182 |
multcolor(ctmp, np->mcolor); |
| 183 |
dtmp = ldot * omega * np->rspec; |
| 184 |
scalecolor(ctmp, dtmp); |
| 185 |
addcolor(cval, ctmp); |
| 186 |
} else { |
| 187 |
/* |
| 188 |
* Compute transmitted non-diffuse component. |
| 189 |
*/ |
| 190 |
if ((np->mp->otype == MAT_TFUNC) | (np->mp->otype == MAT_TDATA)) |
| 191 |
multcolor(ctmp, np->mcolor); |
| 192 |
dtmp = -ldot * omega * np->tspec; |
| 193 |
scalecolor(ctmp, dtmp); |
| 194 |
addcolor(cval, ctmp); |
| 195 |
} |
| 196 |
#undef lddx |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
extern int |
| 201 |
m_brdf( /* color a ray that hit a BRDTfunc material */ |
| 202 |
register OBJREC *m, |
| 203 |
register RAY *r |
| 204 |
) |
| 205 |
{ |
| 206 |
int hitfront = 1; |
| 207 |
BRDFDAT nd; |
| 208 |
RAY sr; |
| 209 |
double mirtest=0, mirdist=0; |
| 210 |
double transtest, transdist; |
| 211 |
int hasrefl, hastrans; |
| 212 |
int hastexture; |
| 213 |
COLOR ctmp; |
| 214 |
FVECT vtmp; |
| 215 |
double d; |
| 216 |
register MFUNC *mf; |
| 217 |
register int i; |
| 218 |
/* check arguments */ |
| 219 |
if ((m->oargs.nsargs < 10) | (m->oargs.nfargs < 9)) |
| 220 |
objerror(m, USER, "bad # arguments"); |
| 221 |
nd.mp = m; |
| 222 |
nd.pr = r; |
| 223 |
/* dummy values */ |
| 224 |
nd.rspec = nd.tspec = 1.0; |
| 225 |
nd.trans = 0.5; |
| 226 |
/* diffuse reflectance */ |
| 227 |
if (r->rod > 0.0) |
| 228 |
setcolor(nd.rdiff, m->oargs.farg[0], |
| 229 |
m->oargs.farg[1], |
| 230 |
m->oargs.farg[2]); |
| 231 |
else |
| 232 |
setcolor(nd.rdiff, m->oargs.farg[3], |
| 233 |
m->oargs.farg[4], |
| 234 |
m->oargs.farg[5]); |
| 235 |
/* diffuse transmittance */ |
| 236 |
setcolor(nd.tdiff, m->oargs.farg[6], |
| 237 |
m->oargs.farg[7], |
| 238 |
m->oargs.farg[8]); |
| 239 |
/* get modifiers */ |
| 240 |
raytexture(r, m->omod); |
| 241 |
hastexture = DOT(r->pert,r->pert) > FTINY*FTINY; |
| 242 |
if (hastexture) { /* perturb normal */ |
| 243 |
nd.pdot = raynormal(nd.pnorm, r); |
| 244 |
} else { |
| 245 |
VCOPY(nd.pnorm, r->ron); |
| 246 |
nd.pdot = r->rod; |
| 247 |
} |
| 248 |
if (r->rod < 0.0) { /* orient perturbed values */ |
| 249 |
nd.pdot = -nd.pdot; |
| 250 |
for (i = 0; i < 3; i++) { |
| 251 |
nd.pnorm[i] = -nd.pnorm[i]; |
| 252 |
r->pert[i] = -r->pert[i]; |
| 253 |
} |
| 254 |
hitfront = 0; |
| 255 |
} |
| 256 |
copycolor(nd.mcolor, r->pcol); /* get pattern color */ |
| 257 |
multcolor(nd.rdiff, nd.mcolor); /* modify diffuse values */ |
| 258 |
multcolor(nd.tdiff, nd.mcolor); |
| 259 |
hasrefl = bright(nd.rdiff) > FTINY; |
| 260 |
hastrans = bright(nd.tdiff) > FTINY; |
| 261 |
/* load cal file */ |
| 262 |
nd.dp = NULL; |
| 263 |
mf = getfunc(m, 9, 0x3f, 0); |
| 264 |
/* compute transmitted ray */ |
| 265 |
setbrdfunc(&nd); |
| 266 |
errno = 0; |
| 267 |
setcolor(ctmp, evalue(mf->ep[3]), |
| 268 |
evalue(mf->ep[4]), |
| 269 |
evalue(mf->ep[5])); |
| 270 |
if ((errno == EDOM) | (errno == ERANGE)) |
| 271 |
objerror(m, WARNING, "compute error"); |
| 272 |
else if (rayorigin(&sr, TRANS, r, ctmp) == 0) { |
| 273 |
if (!(r->crtype & SHADOW) && hastexture) { |
| 274 |
/* perturb direction */ |
| 275 |
VSUM(sr.rdir, r->rdir, r->pert, -.75); |
| 276 |
if (normalize(sr.rdir) == 0.0) { |
| 277 |
objerror(m, WARNING, "illegal perturbation"); |
| 278 |
VCOPY(sr.rdir, r->rdir); |
| 279 |
} |
| 280 |
} else { |
| 281 |
VCOPY(sr.rdir, r->rdir); |
| 282 |
} |
| 283 |
rayvalue(&sr); |
| 284 |
multcolor(sr.rcol, sr.rcoef); |
| 285 |
addcolor(r->rcol, sr.rcol); |
| 286 |
if (!hastexture) { |
| 287 |
transtest = 2.0*bright(sr.rcol); |
| 288 |
transdist = r->rot + sr.rt; |
| 289 |
} |
| 290 |
} |
| 291 |
if (r->crtype & SHADOW) /* the rest is shadow */ |
| 292 |
return(1); |
| 293 |
/* compute reflected ray */ |
| 294 |
setbrdfunc(&nd); |
| 295 |
errno = 0; |
| 296 |
setcolor(ctmp, evalue(mf->ep[0]), |
| 297 |
evalue(mf->ep[1]), |
| 298 |
evalue(mf->ep[2])); |
| 299 |
if ((errno == EDOM) | (errno == ERANGE)) |
| 300 |
objerror(m, WARNING, "compute error"); |
| 301 |
else if (rayorigin(&sr, REFLECTED, r, ctmp) == 0) { |
| 302 |
VSUM(sr.rdir, r->rdir, nd.pnorm, 2.*nd.pdot); |
| 303 |
checknorm(sr.rdir); |
| 304 |
rayvalue(&sr); |
| 305 |
multcolor(sr.rcol, sr.rcoef); |
| 306 |
addcolor(r->rcol, sr.rcol); |
| 307 |
if (!hastexture && r->ro != NULL && isflat(r->ro->otype)) { |
| 308 |
mirtest = 2.0*bright(sr.rcol); |
| 309 |
mirdist = r->rot + sr.rt; |
| 310 |
} |
| 311 |
} |
| 312 |
/* compute ambient */ |
| 313 |
if (hasrefl) { |
| 314 |
if (!hitfront) |
| 315 |
flipsurface(r); |
| 316 |
copycolor(ctmp, nd.rdiff); |
| 317 |
multambient(ctmp, r, nd.pnorm); |
| 318 |
addcolor(r->rcol, ctmp); /* add to returned color */ |
| 319 |
if (!hitfront) |
| 320 |
flipsurface(r); |
| 321 |
} |
| 322 |
if (hastrans) { /* from other side */ |
| 323 |
if (hitfront) |
| 324 |
flipsurface(r); |
| 325 |
vtmp[0] = -nd.pnorm[0]; |
| 326 |
vtmp[1] = -nd.pnorm[1]; |
| 327 |
vtmp[2] = -nd.pnorm[2]; |
| 328 |
copycolor(ctmp, nd.tdiff); |
| 329 |
multambient(ctmp, r, vtmp); |
| 330 |
addcolor(r->rcol, ctmp); |
| 331 |
if (hitfront) |
| 332 |
flipsurface(r); |
| 333 |
} |
| 334 |
if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0') |
| 335 |
direct(r, dirbrdf, &nd); /* add direct component */ |
| 336 |
|
| 337 |
d = bright(r->rcol); /* set effective distance */ |
| 338 |
if (transtest > d) |
| 339 |
r->rt = transdist; |
| 340 |
else if (mirtest > d) |
| 341 |
r->rt = mirdist; |
| 342 |
|
| 343 |
return(1); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
extern int |
| 349 |
m_brdf2( /* color a ray that hit a BRDF material */ |
| 350 |
register OBJREC *m, |
| 351 |
register RAY *r |
| 352 |
) |
| 353 |
{ |
| 354 |
BRDFDAT nd; |
| 355 |
COLOR ctmp; |
| 356 |
FVECT vtmp; |
| 357 |
double dtmp; |
| 358 |
/* always a shadow */ |
| 359 |
if (r->crtype & SHADOW) |
| 360 |
return(1); |
| 361 |
/* check arguments */ |
| 362 |
if ((m->oargs.nsargs < (hasdata(m->otype)?4:2)) | (m->oargs.nfargs < |
| 363 |
((m->otype==MAT_TFUNC)|(m->otype==MAT_TDATA)?6:4))) |
| 364 |
objerror(m, USER, "bad # arguments"); |
| 365 |
/* check for back side */ |
| 366 |
if (r->rod < 0.0) { |
| 367 |
if (!backvis && m->otype != MAT_TFUNC |
| 368 |
&& m->otype != MAT_TDATA) { |
| 369 |
raytrans(r); |
| 370 |
return(1); |
| 371 |
} |
| 372 |
raytexture(r, m->omod); |
| 373 |
flipsurface(r); /* reorient if backvis */ |
| 374 |
} else |
| 375 |
raytexture(r, m->omod); |
| 376 |
|
| 377 |
nd.mp = m; |
| 378 |
nd.pr = r; |
| 379 |
/* get material color */ |
| 380 |
setcolor(nd.mcolor, m->oargs.farg[0], |
| 381 |
m->oargs.farg[1], |
| 382 |
m->oargs.farg[2]); |
| 383 |
/* get specular component */ |
| 384 |
nd.rspec = m->oargs.farg[3]; |
| 385 |
/* compute transmittance */ |
| 386 |
if ((m->otype == MAT_TFUNC) | (m->otype == MAT_TDATA)) { |
| 387 |
nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec); |
| 388 |
nd.tspec = nd.trans * m->oargs.farg[5]; |
| 389 |
dtmp = nd.trans - nd.tspec; |
| 390 |
setcolor(nd.tdiff, dtmp, dtmp, dtmp); |
| 391 |
} else { |
| 392 |
nd.tspec = nd.trans = 0.0; |
| 393 |
setcolor(nd.tdiff, 0.0, 0.0, 0.0); |
| 394 |
} |
| 395 |
/* compute reflectance */ |
| 396 |
dtmp = 1.0 - nd.trans - nd.rspec; |
| 397 |
setcolor(nd.rdiff, dtmp, dtmp, dtmp); |
| 398 |
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
| 399 |
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
| 400 |
multcolor(nd.rdiff, nd.mcolor); |
| 401 |
multcolor(nd.tdiff, nd.mcolor); |
| 402 |
/* load auxiliary files */ |
| 403 |
if (hasdata(m->otype)) { |
| 404 |
nd.dp = getdata(m->oargs.sarg[1]); |
| 405 |
getfunc(m, 2, 0, 0); |
| 406 |
} else { |
| 407 |
nd.dp = NULL; |
| 408 |
getfunc(m, 1, 0, 0); |
| 409 |
} |
| 410 |
/* compute ambient */ |
| 411 |
if (nd.trans < 1.0-FTINY) { |
| 412 |
copycolor(ctmp, nd.mcolor); /* modified by material color */ |
| 413 |
scalecolor(ctmp, 1.0-nd.trans); |
| 414 |
multambient(ctmp, r, nd.pnorm); |
| 415 |
addcolor(r->rcol, ctmp); /* add to returned color */ |
| 416 |
} |
| 417 |
if (nd.trans > FTINY) { /* from other side */ |
| 418 |
flipsurface(r); |
| 419 |
vtmp[0] = -nd.pnorm[0]; |
| 420 |
vtmp[1] = -nd.pnorm[1]; |
| 421 |
vtmp[2] = -nd.pnorm[2]; |
| 422 |
copycolor(ctmp, nd.mcolor); |
| 423 |
scalecolor(ctmp, nd.trans); |
| 424 |
multambient(ctmp, r, vtmp); |
| 425 |
addcolor(r->rcol, ctmp); |
| 426 |
flipsurface(r); |
| 427 |
} |
| 428 |
/* add direct component */ |
| 429 |
direct(r, dirbrdf, &nd); |
| 430 |
|
| 431 |
return(1); |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
static int |
| 436 |
setbrdfunc( /* set up brdf function and variables */ |
| 437 |
register BRDFDAT *np |
| 438 |
) |
| 439 |
{ |
| 440 |
FVECT vec; |
| 441 |
|
| 442 |
if (setfunc(np->mp, np->pr) == 0) |
| 443 |
return(0); /* it's OK, setfunc says we're done */ |
| 444 |
/* else (re)assign special variables */ |
| 445 |
multv3(vec, np->pnorm, funcxf.xfm); |
| 446 |
varset("NxP", '=', vec[0]/funcxf.sca); |
| 447 |
varset("NyP", '=', vec[1]/funcxf.sca); |
| 448 |
varset("NzP", '=', vec[2]/funcxf.sca); |
| 449 |
varset("RdotP", '=', np->pdot <= -1.0 ? -1.0 : |
| 450 |
np->pdot >= 1.0 ? 1.0 : np->pdot); |
| 451 |
varset("CrP", '=', colval(np->mcolor,RED)); |
| 452 |
varset("CgP", '=', colval(np->mcolor,GRN)); |
| 453 |
varset("CbP", '=', colval(np->mcolor,BLU)); |
| 454 |
return(1); |
| 455 |
} |