| 1 |
greg |
1.7 |
/* Copyright (c) 1991 Regents of the University of California */ |
| 2 |
greg |
1.1 |
|
| 3 |
|
|
#ifndef lint |
| 4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
| 5 |
|
|
#endif |
| 6 |
|
|
|
| 7 |
|
|
/* |
| 8 |
|
|
* Shading for materials with arbitrary BRDF's |
| 9 |
|
|
*/ |
| 10 |
|
|
|
| 11 |
|
|
#include "ray.h" |
| 12 |
|
|
|
| 13 |
|
|
#include "data.h" |
| 14 |
|
|
|
| 15 |
|
|
#include "otypes.h" |
| 16 |
|
|
|
| 17 |
|
|
/* |
| 18 |
|
|
* Arguments to this material include the color and specularity. |
| 19 |
|
|
* String arguments include the reflection function and files. |
| 20 |
|
|
* The BRDF is currently used just for the specular component to light |
| 21 |
|
|
* sources. Reflectance values or data coordinates are functions |
| 22 |
|
|
* of the direction to the light source. |
| 23 |
|
|
* We orient the surface towards the incoming ray, so a single |
| 24 |
|
|
* surface can be used to represent an infinitely thin object. |
| 25 |
|
|
* |
| 26 |
|
|
* Arguments for MAT_PFUNC and MAT_MFUNC are: |
| 27 |
greg |
1.4 |
* 2+ func funcfile transform |
| 28 |
greg |
1.1 |
* 0 |
| 29 |
greg |
1.4 |
* 4+ red grn blu specularity A5 .. |
| 30 |
greg |
1.1 |
* |
| 31 |
|
|
* Arguments for MAT_PDATA and MAT_MDATA are: |
| 32 |
greg |
1.4 |
* 4+ func datafile funcfile v0 .. transform |
| 33 |
greg |
1.1 |
* 0 |
| 34 |
greg |
1.4 |
* 4+ red grn blu specularity A5 .. |
| 35 |
greg |
1.5 |
* |
| 36 |
|
|
* Arguments for MAT_TFUNC are: |
| 37 |
|
|
* 2+ func funcfile transform |
| 38 |
|
|
* 0 |
| 39 |
|
|
* 4+ red grn blu rspec trans tspec A7 .. |
| 40 |
|
|
* |
| 41 |
|
|
* Arguments for MAT_TDATA are: |
| 42 |
|
|
* 4+ func datafile funcfile v0 .. transform |
| 43 |
|
|
* 0 |
| 44 |
|
|
* 4+ red grn blu rspec trans tspec A7 .. |
| 45 |
|
|
* |
| 46 |
|
|
* Arguments for the more general MAT_BRTDF are: |
| 47 |
|
|
* 10+ rrefl grefl brefl |
| 48 |
|
|
* rtrns gtrns btrns |
| 49 |
|
|
* rbrtd gbrtd bbrtd |
| 50 |
|
|
* funcfile transform |
| 51 |
|
|
* 0 |
| 52 |
|
|
* 6+ red grn blu rspec trans tspec A7 .. |
| 53 |
|
|
* |
| 54 |
|
|
* In addition to the normal variables available to functions, |
| 55 |
|
|
* we define the following: |
| 56 |
|
|
* NxP, NyP, NzP - perturbed surface normal |
| 57 |
|
|
* RdotP - perturbed ray dot product |
| 58 |
|
|
* CrP, CgP, CbP - perturbed material color |
| 59 |
greg |
1.1 |
*/ |
| 60 |
|
|
|
| 61 |
greg |
1.2 |
extern double funvalue(), varvalue(); |
| 62 |
greg |
1.5 |
extern XF funcxf; |
| 63 |
greg |
1.2 |
|
| 64 |
greg |
1.1 |
typedef struct { |
| 65 |
|
|
OBJREC *mp; /* material pointer */ |
| 66 |
|
|
RAY *pr; /* intersected ray */ |
| 67 |
greg |
1.5 |
DATARRAY *dp; /* data array for PDATA, MDATA or TDATA */ |
| 68 |
greg |
1.1 |
COLOR mcolor; /* color of this material */ |
| 69 |
|
|
double rspec; /* specular reflection */ |
| 70 |
|
|
double rdiff; /* diffuse reflection */ |
| 71 |
greg |
1.5 |
double trans; /* transmissivity */ |
| 72 |
|
|
double tspec; /* specular transmission */ |
| 73 |
|
|
double tdiff; /* diffuse transmission */ |
| 74 |
greg |
1.1 |
FVECT pnorm; /* perturbed surface normal */ |
| 75 |
|
|
double pdot; /* perturbed dot product */ |
| 76 |
|
|
} BRDFDAT; /* BRDF material data */ |
| 77 |
|
|
|
| 78 |
|
|
|
| 79 |
|
|
dirbrdf(cval, np, ldir, omega) /* compute source contribution */ |
| 80 |
|
|
COLOR cval; /* returned coefficient */ |
| 81 |
|
|
register BRDFDAT *np; /* material data */ |
| 82 |
|
|
FVECT ldir; /* light source direction */ |
| 83 |
|
|
double omega; /* light source size */ |
| 84 |
|
|
{ |
| 85 |
|
|
double ldot; |
| 86 |
|
|
double dtmp; |
| 87 |
|
|
COLOR ctmp; |
| 88 |
greg |
1.4 |
FVECT ldx; |
| 89 |
greg |
1.1 |
double pt[MAXDIM]; |
| 90 |
greg |
1.5 |
register char **sa; |
| 91 |
greg |
1.1 |
register int i; |
| 92 |
|
|
|
| 93 |
|
|
setcolor(cval, 0.0, 0.0, 0.0); |
| 94 |
|
|
|
| 95 |
|
|
ldot = DOT(np->pnorm, ldir); |
| 96 |
|
|
|
| 97 |
greg |
1.5 |
if (ldot <= FTINY && ldot >= -FTINY) |
| 98 |
|
|
return; /* too close to grazing */ |
| 99 |
|
|
if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY) |
| 100 |
greg |
1.1 |
return; /* wrong side */ |
| 101 |
|
|
|
| 102 |
greg |
1.5 |
if (ldot > 0.0 && np->rdiff > FTINY) { |
| 103 |
greg |
1.1 |
/* |
| 104 |
|
|
* Compute and add diffuse reflected component to returned |
| 105 |
|
|
* color. The diffuse reflected component will always be |
| 106 |
|
|
* modified by the color of the material. |
| 107 |
|
|
*/ |
| 108 |
|
|
copycolor(ctmp, np->mcolor); |
| 109 |
|
|
dtmp = ldot * omega * np->rdiff / PI; |
| 110 |
|
|
scalecolor(ctmp, dtmp); |
| 111 |
|
|
addcolor(cval, ctmp); |
| 112 |
|
|
} |
| 113 |
greg |
1.5 |
if (ldot < 0.0 && np->tdiff > FTINY) { |
| 114 |
greg |
1.1 |
/* |
| 115 |
greg |
1.5 |
* Diffuse transmitted component. |
| 116 |
greg |
1.1 |
*/ |
| 117 |
greg |
1.5 |
copycolor(ctmp, np->mcolor); |
| 118 |
|
|
dtmp = -ldot * omega * np->tdiff / PI; |
| 119 |
|
|
scalecolor(ctmp, dtmp); |
| 120 |
|
|
addcolor(cval, ctmp); |
| 121 |
greg |
1.1 |
} |
| 122 |
greg |
1.5 |
if (ldot > 0.0 ? np->rspec <= FTINY : np->tspec <= FTINY) |
| 123 |
|
|
return; /* no specular component */ |
| 124 |
|
|
/* set up function */ |
| 125 |
greg |
1.10 |
setbrdfunc(np); |
| 126 |
greg |
1.5 |
sa = np->mp->oargs.sarg; |
| 127 |
|
|
errno = 0; |
| 128 |
|
|
/* transform light vector */ |
| 129 |
|
|
multv3(ldx, ldir, funcxf.xfm); |
| 130 |
|
|
for (i = 0; i < 3; i++) |
| 131 |
|
|
ldx[i] /= funcxf.sca; |
| 132 |
|
|
/* compute BRTDF */ |
| 133 |
|
|
if (np->mp->otype == MAT_BRTDF) { |
| 134 |
|
|
colval(ctmp,RED) = funvalue(sa[6], 3, ldx); |
| 135 |
greg |
1.7 |
if (!strcmp(sa[7],sa[6])) |
| 136 |
greg |
1.5 |
colval(ctmp,GRN) = colval(ctmp,RED); |
| 137 |
|
|
else |
| 138 |
|
|
colval(ctmp,GRN) = funvalue(sa[7], 3, ldx); |
| 139 |
greg |
1.7 |
if (!strcmp(sa[8],sa[6])) |
| 140 |
greg |
1.5 |
colval(ctmp,BLU) = colval(ctmp,RED); |
| 141 |
greg |
1.7 |
else if (!strcmp(sa[8],sa[7])) |
| 142 |
greg |
1.5 |
colval(ctmp,BLU) = colval(ctmp,GRN); |
| 143 |
|
|
else |
| 144 |
|
|
colval(ctmp,BLU) = funvalue(sa[8], 3, ldx); |
| 145 |
|
|
dtmp = bright(ctmp); |
| 146 |
|
|
} else if (np->dp == NULL) { |
| 147 |
|
|
dtmp = funvalue(sa[0], 3, ldx); |
| 148 |
|
|
setcolor(ctmp, dtmp, dtmp, dtmp); |
| 149 |
|
|
} else { |
| 150 |
|
|
for (i = 0; i < np->dp->nd; i++) |
| 151 |
|
|
pt[i] = funvalue(sa[3+i], 3, ldx); |
| 152 |
|
|
dtmp = datavalue(np->dp, pt); |
| 153 |
|
|
dtmp = funvalue(sa[0], 1, &dtmp); |
| 154 |
|
|
setcolor(ctmp, dtmp, dtmp, dtmp); |
| 155 |
|
|
} |
| 156 |
|
|
if (errno) |
| 157 |
|
|
goto computerr; |
| 158 |
|
|
if (dtmp <= FTINY) |
| 159 |
|
|
return; |
| 160 |
|
|
if (ldot > 0.0) { |
| 161 |
|
|
/* |
| 162 |
|
|
* Compute reflected non-diffuse component. |
| 163 |
|
|
*/ |
| 164 |
greg |
1.6 |
if (np->mp->otype == MAT_MFUNC || np->mp->otype == MAT_MDATA) |
| 165 |
|
|
multcolor(ctmp, np->mcolor); |
| 166 |
|
|
dtmp = ldot * omega * np->rspec; |
| 167 |
greg |
1.5 |
scalecolor(ctmp, dtmp); |
| 168 |
|
|
addcolor(cval, ctmp); |
| 169 |
|
|
} else { |
| 170 |
|
|
/* |
| 171 |
|
|
* Compute transmitted non-diffuse component. |
| 172 |
|
|
*/ |
| 173 |
greg |
1.6 |
if (np->mp->otype == MAT_TFUNC || np->mp->otype == MAT_TDATA) |
| 174 |
|
|
multcolor(ctmp, np->mcolor); |
| 175 |
greg |
1.5 |
dtmp = -ldot * omega * np->tspec; |
| 176 |
|
|
scalecolor(ctmp, dtmp); |
| 177 |
|
|
addcolor(cval, ctmp); |
| 178 |
|
|
} |
| 179 |
greg |
1.1 |
return; |
| 180 |
|
|
computerr: |
| 181 |
|
|
objerror(np->mp, WARNING, "compute error"); |
| 182 |
|
|
return; |
| 183 |
|
|
} |
| 184 |
|
|
|
| 185 |
|
|
|
| 186 |
|
|
m_brdf(m, r) /* color a ray which hit a BRDF material */ |
| 187 |
|
|
register OBJREC *m; |
| 188 |
|
|
register RAY *r; |
| 189 |
|
|
{ |
| 190 |
greg |
1.5 |
int minsa, minfa; |
| 191 |
greg |
1.1 |
BRDFDAT nd; |
| 192 |
greg |
1.7 |
double transtest, transdist; |
| 193 |
greg |
1.1 |
COLOR ctmp; |
| 194 |
greg |
1.13 |
double dtmp, tspect, rspecr; |
| 195 |
greg |
1.1 |
register int i; |
| 196 |
greg |
1.5 |
/* check arguments */ |
| 197 |
|
|
switch (m->otype) { |
| 198 |
|
|
case MAT_PFUNC: case MAT_MFUNC: |
| 199 |
|
|
minsa = 2; minfa = 4; break; |
| 200 |
|
|
case MAT_PDATA: case MAT_MDATA: |
| 201 |
|
|
minsa = 4; minfa = 4; break; |
| 202 |
|
|
case MAT_TFUNC: |
| 203 |
|
|
minsa = 2; minfa = 6; break; |
| 204 |
|
|
case MAT_TDATA: |
| 205 |
|
|
minsa = 4; minfa = 6; break; |
| 206 |
|
|
case MAT_BRTDF: |
| 207 |
|
|
minsa = 10; minfa = 6; break; |
| 208 |
|
|
} |
| 209 |
|
|
if (m->oargs.nsargs < minsa || m->oargs.nfargs < minfa) |
| 210 |
greg |
1.1 |
objerror(m, USER, "bad # arguments"); |
| 211 |
|
|
nd.mp = m; |
| 212 |
|
|
nd.pr = r; |
| 213 |
greg |
1.5 |
/* get specular component */ |
| 214 |
|
|
nd.rspec = m->oargs.farg[3]; |
| 215 |
|
|
/* compute transmission */ |
| 216 |
|
|
if (m->otype == MAT_TFUNC || m->otype == MAT_TDATA |
| 217 |
|
|
|| m->otype == MAT_BRTDF) { |
| 218 |
|
|
nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec); |
| 219 |
|
|
nd.tspec = nd.trans * m->oargs.farg[5]; |
| 220 |
|
|
nd.tdiff = nd.trans - nd.tspec; |
| 221 |
|
|
} else |
| 222 |
|
|
nd.tdiff = nd.tspec = nd.trans = 0.0; |
| 223 |
|
|
/* early shadow check */ |
| 224 |
|
|
if (r->crtype & SHADOW && (m->otype != MAT_BRTDF || nd.tspec <= FTINY)) |
| 225 |
|
|
return; |
| 226 |
|
|
/* diffuse reflection */ |
| 227 |
|
|
nd.rdiff = 1.0 - nd.trans - nd.rspec; |
| 228 |
|
|
/* get material color */ |
| 229 |
|
|
setcolor(nd.mcolor, m->oargs.farg[0], |
| 230 |
|
|
m->oargs.farg[1], |
| 231 |
|
|
m->oargs.farg[2]); |
| 232 |
|
|
/* fix orientation */ |
| 233 |
|
|
if (r->rod < 0.0) |
| 234 |
|
|
flipsurface(r); |
| 235 |
|
|
/* get modifiers */ |
| 236 |
|
|
raytexture(r, m->omod); |
| 237 |
|
|
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
| 238 |
|
|
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
| 239 |
greg |
1.7 |
transtest = 0; |
| 240 |
greg |
1.1 |
/* load auxiliary files */ |
| 241 |
greg |
1.5 |
if (m->otype == MAT_PDATA || m->otype == MAT_MDATA |
| 242 |
|
|
|| m->otype == MAT_TDATA) { |
| 243 |
greg |
1.1 |
nd.dp = getdata(m->oargs.sarg[1]); |
| 244 |
|
|
for (i = 3; i < m->oargs.nsargs; i++) |
| 245 |
|
|
if (m->oargs.sarg[i][0] == '-') |
| 246 |
|
|
break; |
| 247 |
|
|
if (i-3 != nd.dp->nd) |
| 248 |
|
|
objerror(m, USER, "dimension error"); |
| 249 |
greg |
1.15 |
funcfile(m->oargs.sarg[2]); |
| 250 |
greg |
1.5 |
} else if (m->otype == MAT_BRTDF) { |
| 251 |
|
|
nd.dp = NULL; |
| 252 |
greg |
1.15 |
funcfile(m->oargs.sarg[9]); |
| 253 |
greg |
1.1 |
} else { |
| 254 |
|
|
nd.dp = NULL; |
| 255 |
greg |
1.15 |
funcfile(m->oargs.sarg[1]); |
| 256 |
greg |
1.1 |
} |
| 257 |
greg |
1.5 |
/* set special variables */ |
| 258 |
greg |
1.10 |
setbrdfunc(&nd); |
| 259 |
greg |
1.5 |
/* compute transmitted ray */ |
| 260 |
greg |
1.13 |
tspect = 0.; |
| 261 |
greg |
1.5 |
if (m->otype == MAT_BRTDF && nd.tspec > FTINY) { |
| 262 |
|
|
RAY sr; |
| 263 |
|
|
errno = 0; |
| 264 |
greg |
1.12 |
setcolor(ctmp, varvalue(m->oargs.sarg[3]), |
| 265 |
|
|
varvalue(m->oargs.sarg[4]), |
| 266 |
|
|
varvalue(m->oargs.sarg[5])); |
| 267 |
greg |
1.14 |
scalecolor(ctmp, nd.trans); |
| 268 |
greg |
1.5 |
if (errno) |
| 269 |
|
|
objerror(m, WARNING, "compute error"); |
| 270 |
greg |
1.13 |
else if ((tspect = bright(ctmp)) > FTINY && |
| 271 |
|
|
rayorigin(&sr, r, TRANS, tspect) == 0) { |
| 272 |
greg |
1.16 |
if (!(r->crtype & SHADOW) && |
| 273 |
|
|
DOT(r->pert,r->pert) > FTINY*FTINY) { |
| 274 |
greg |
1.7 |
for (i = 0; i < 3; i++) /* perturb direction */ |
| 275 |
|
|
sr.rdir[i] = r->rdir[i] - |
| 276 |
|
|
.75*r->pert[i]; |
| 277 |
|
|
normalize(sr.rdir); |
| 278 |
greg |
1.8 |
} else { |
| 279 |
|
|
VCOPY(sr.rdir, r->rdir); |
| 280 |
greg |
1.7 |
transtest = 2; |
| 281 |
greg |
1.8 |
} |
| 282 |
greg |
1.5 |
rayvalue(&sr); |
| 283 |
|
|
multcolor(sr.rcol, ctmp); |
| 284 |
|
|
addcolor(r->rcol, sr.rcol); |
| 285 |
greg |
1.7 |
transtest *= bright(sr.rcol); |
| 286 |
|
|
transdist = r->rot + sr.rt; |
| 287 |
greg |
1.5 |
} |
| 288 |
|
|
} |
| 289 |
|
|
if (r->crtype & SHADOW) /* the rest is shadow */ |
| 290 |
|
|
return; |
| 291 |
|
|
/* compute reflected ray */ |
| 292 |
greg |
1.13 |
rspecr = 0.; |
| 293 |
greg |
1.6 |
if (m->otype == MAT_BRTDF && nd.rspec > FTINY) { |
| 294 |
|
|
RAY sr; |
| 295 |
|
|
errno = 0; |
| 296 |
greg |
1.12 |
setcolor(ctmp, varvalue(m->oargs.sarg[0]), |
| 297 |
|
|
varvalue(m->oargs.sarg[1]), |
| 298 |
|
|
varvalue(m->oargs.sarg[2])); |
| 299 |
greg |
1.6 |
if (errno) |
| 300 |
|
|
objerror(m, WARNING, "compute error"); |
| 301 |
greg |
1.13 |
else if ((rspecr = bright(ctmp)) > FTINY && |
| 302 |
|
|
rayorigin(&sr, r, REFLECTED, rspecr) == 0) { |
| 303 |
greg |
1.6 |
for (i = 0; i < 3; i++) |
| 304 |
|
|
sr.rdir[i] = r->rdir[i] + |
| 305 |
greg |
1.5 |
2.0*nd.pdot*nd.pnorm[i]; |
| 306 |
greg |
1.6 |
rayvalue(&sr); |
| 307 |
|
|
multcolor(sr.rcol, ctmp); |
| 308 |
|
|
addcolor(r->rcol, sr.rcol); |
| 309 |
greg |
1.5 |
} |
| 310 |
greg |
1.1 |
} |
| 311 |
|
|
/* compute ambient */ |
| 312 |
greg |
1.13 |
if ((dtmp = 1.0-nd.trans-rspecr) > FTINY) { |
| 313 |
greg |
1.1 |
ambient(ctmp, r); |
| 314 |
greg |
1.13 |
scalecolor(ctmp, dtmp); |
| 315 |
greg |
1.1 |
multcolor(ctmp, nd.mcolor); /* modified by material color */ |
| 316 |
|
|
addcolor(r->rcol, ctmp); /* add to returned color */ |
| 317 |
greg |
1.5 |
} |
| 318 |
greg |
1.13 |
if ((dtmp = nd.trans-tspect) > FTINY) { /* from other side */ |
| 319 |
greg |
1.5 |
flipsurface(r); |
| 320 |
|
|
ambient(ctmp, r); |
| 321 |
greg |
1.13 |
scalecolor(ctmp, dtmp); |
| 322 |
greg |
1.5 |
multcolor(ctmp, nd.mcolor); |
| 323 |
|
|
addcolor(r->rcol, ctmp); |
| 324 |
|
|
flipsurface(r); |
| 325 |
greg |
1.1 |
} |
| 326 |
|
|
/* add direct component */ |
| 327 |
|
|
direct(r, dirbrdf, &nd); |
| 328 |
greg |
1.7 |
/* check distance */ |
| 329 |
|
|
if (transtest > bright(r->rcol)) |
| 330 |
|
|
r->rt = transdist; |
| 331 |
greg |
1.10 |
} |
| 332 |
|
|
|
| 333 |
|
|
|
| 334 |
|
|
setbrdfunc(np) /* set up brdf function and variables */ |
| 335 |
|
|
register BRDFDAT *np; |
| 336 |
|
|
{ |
| 337 |
|
|
FVECT vec; |
| 338 |
|
|
|
| 339 |
|
|
if (setfunc(np->mp, np->pr) == 0) |
| 340 |
|
|
return(0); /* it's OK, setfunc says we're done */ |
| 341 |
|
|
/* else (re)assign special variables */ |
| 342 |
|
|
multv3(vec, np->pnorm, funcxf.xfm); |
| 343 |
|
|
varset("NxP", '=', vec[0]/funcxf.sca); |
| 344 |
|
|
varset("NyP", '=', vec[1]/funcxf.sca); |
| 345 |
|
|
varset("NzP", '=', vec[2]/funcxf.sca); |
| 346 |
greg |
1.11 |
varset("RdotP", '=', np->pdot <= -1.0 ? -1.0 : |
| 347 |
|
|
np->pdot >= 1.0 ? 1.0 : np->pdot); |
| 348 |
greg |
1.10 |
varset("CrP", '=', colval(np->mcolor,RED)); |
| 349 |
|
|
varset("CgP", '=', colval(np->mcolor,GRN)); |
| 350 |
|
|
varset("CbP", '=', colval(np->mcolor,BLU)); |
| 351 |
|
|
return(1); |
| 352 |
greg |
1.1 |
} |