| 1 |
< |
/* Copyright (c) 1991 Regents of the University of California */ |
| 1 |
> |
/* Copyright (c) 1992 Regents of the University of California */ |
| 2 |
|
|
| 3 |
|
#ifndef lint |
| 4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
| 11 |
|
* 12/19/85 - added stuff for metals. |
| 12 |
|
* 6/26/87 - improved specular model. |
| 13 |
|
* 9/28/87 - added model for translucent materials. |
| 14 |
+ |
* Later changes described in delta comments. |
| 15 |
|
*/ |
| 16 |
|
|
| 17 |
|
#include "ray.h" |
| 18 |
|
|
| 19 |
|
#include "otypes.h" |
| 20 |
|
|
| 21 |
+ |
#include "random.h" |
| 22 |
+ |
|
| 23 |
|
/* |
| 24 |
|
* This routine uses portions of the reflection |
| 25 |
|
* model described by Cook and Torrance. |
| 37 |
|
|
| 38 |
|
#define BSPEC(m) (6.0) /* specularity parameter b */ |
| 39 |
|
|
| 40 |
< |
extern double exp(); |
| 40 |
> |
/* specularity flags */ |
| 41 |
> |
#define SP_REFL 01 /* has reflected specular component */ |
| 42 |
> |
#define SP_TRAN 02 /* has transmitted specular */ |
| 43 |
> |
#define SP_PURE 010 /* purely specular (zero roughness) */ |
| 44 |
> |
#define SP_FLAT 020 /* flat reflecting surface */ |
| 45 |
|
|
| 46 |
|
typedef struct { |
| 47 |
|
OBJREC *mp; /* material pointer */ |
| 48 |
+ |
short specfl; /* specularity flags, defined above */ |
| 49 |
|
COLOR mcolor; /* color of this material */ |
| 50 |
|
COLOR scolor; /* color of specular component */ |
| 51 |
|
FVECT vrefl; /* vector in direction of reflected ray */ |
| 52 |
|
FVECT prdir; /* vector in transmitted direction */ |
| 53 |
< |
double alpha2; /* roughness squared times 2 */ |
| 53 |
> |
double alpha2; /* roughness squared */ |
| 54 |
|
double rdiff, rspec; /* reflected specular, diffuse */ |
| 55 |
|
double trans; /* transmissivity */ |
| 56 |
|
double tdiff, tspec; /* transmitted specular, diffuse */ |
| 67 |
|
{ |
| 68 |
|
double ldot; |
| 69 |
|
double dtmp; |
| 70 |
+ |
int i; |
| 71 |
|
COLOR ctmp; |
| 72 |
|
|
| 73 |
|
setcolor(cval, 0.0, 0.0, 0.0); |
| 88 |
|
scalecolor(ctmp, dtmp); |
| 89 |
|
addcolor(cval, ctmp); |
| 90 |
|
} |
| 91 |
< |
if (ldot > FTINY && np->rspec > FTINY && np->alpha2 > FTINY) { |
| 91 |
> |
if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) { |
| 92 |
|
/* |
| 93 |
|
* Compute specular reflection coefficient using |
| 94 |
|
* gaussian distribution model. |
| 95 |
|
*/ |
| 96 |
< |
/* roughness + source */ |
| 97 |
< |
dtmp = np->alpha2 + omega/(2.0*PI); |
| 96 |
> |
/* roughness */ |
| 97 |
> |
dtmp = 2.0*np->alpha2; |
| 98 |
> |
/* + source if flat */ |
| 99 |
> |
if (np->specfl & SP_FLAT) |
| 100 |
> |
dtmp += omega/(2.0*PI); |
| 101 |
|
/* gaussian */ |
| 102 |
|
dtmp = exp((DOT(np->vrefl,ldir)-1.)/dtmp)/(2.*PI)/dtmp; |
| 103 |
|
/* worth using? */ |
| 117 |
|
scalecolor(ctmp, dtmp); |
| 118 |
|
addcolor(cval, ctmp); |
| 119 |
|
} |
| 120 |
< |
if (ldot < -FTINY && np->tspec > FTINY && np->alpha2 > FTINY) { |
| 120 |
> |
if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) { |
| 121 |
|
/* |
| 122 |
|
* Compute specular transmission. Specular transmission |
| 123 |
|
* is always modified by material color. |
| 137 |
|
} |
| 138 |
|
|
| 139 |
|
|
| 140 |
< |
m_normal(m, r) /* color a ray which hit something normal */ |
| 140 |
> |
m_normal(m, r) /* color a ray that hit something normal */ |
| 141 |
|
register OBJREC *m; |
| 142 |
|
register RAY *r; |
| 143 |
|
{ |
| 146 |
|
double dtmp; |
| 147 |
|
COLOR ctmp; |
| 148 |
|
register int i; |
| 137 |
– |
|
| 138 |
– |
if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) |
| 139 |
– |
objerror(m, USER, "bad # arguments"); |
| 149 |
|
/* easy shadow test */ |
| 150 |
|
if (r->crtype & SHADOW && m->otype != MAT_TRANS) |
| 151 |
|
return; |
| 152 |
+ |
|
| 153 |
+ |
if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5)) |
| 154 |
+ |
objerror(m, USER, "bad number of arguments"); |
| 155 |
|
nd.mp = m; |
| 156 |
|
/* get material color */ |
| 157 |
|
setcolor(nd.mcolor, m->oargs.farg[0], |
| 158 |
|
m->oargs.farg[1], |
| 159 |
|
m->oargs.farg[2]); |
| 160 |
|
/* get roughness */ |
| 161 |
+ |
nd.specfl = 0; |
| 162 |
|
nd.alpha2 = m->oargs.farg[4]; |
| 163 |
< |
nd.alpha2 *= 2.0 * nd.alpha2; |
| 163 |
> |
if ((nd.alpha2 *= nd.alpha2) <= FTINY) |
| 164 |
> |
nd.specfl |= SP_PURE; |
| 165 |
|
/* reorient if necessary */ |
| 166 |
|
if (r->rod < 0.0) |
| 167 |
|
flipsurface(r); |
| 173 |
|
multcolor(nd.mcolor, r->pcol); /* modify material color */ |
| 174 |
|
transtest = 0; |
| 175 |
|
/* get specular component */ |
| 176 |
< |
nd.rspec = m->oargs.farg[3]; |
| 177 |
< |
|
| 164 |
< |
if (nd.rspec > FTINY) { /* has specular component */ |
| 176 |
> |
if ((nd.rspec = m->oargs.farg[3]) > FTINY) { |
| 177 |
> |
nd.specfl |= SP_REFL; |
| 178 |
|
/* compute specular color */ |
| 179 |
|
if (m->otype == MAT_METAL) |
| 180 |
|
copycolor(nd.scolor, nd.mcolor); |
| 190 |
|
for (i = 0; i < 3; i++) |
| 191 |
|
nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i]; |
| 192 |
|
|
| 193 |
< |
if (nd.alpha2 <= FTINY && !(r->crtype & SHADOW)) { |
| 193 |
> |
if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) { |
| 194 |
|
RAY lr; |
| 195 |
|
if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { |
| 196 |
|
VCOPY(lr.rdir, nd.vrefl); |
| 205 |
|
nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec); |
| 206 |
|
nd.tspec = nd.trans * m->oargs.farg[6]; |
| 207 |
|
nd.tdiff = nd.trans - nd.tspec; |
| 208 |
< |
if (r->crtype & SHADOW || DOT(r->pert,r->pert) <= FTINY*FTINY) { |
| 209 |
< |
VCOPY(nd.prdir, r->rdir); |
| 210 |
< |
transtest = 2; |
| 211 |
< |
} else { |
| 212 |
< |
for (i = 0; i < 3; i++) /* perturb direction */ |
| 213 |
< |
nd.prdir[i] = r->rdir[i] - .75*r->pert[i]; |
| 214 |
< |
normalize(nd.prdir); |
| 208 |
> |
if (nd.tspec > FTINY) { |
| 209 |
> |
nd.specfl |= SP_TRAN; |
| 210 |
> |
if (r->crtype & SHADOW || |
| 211 |
> |
DOT(r->pert,r->pert) <= FTINY*FTINY) { |
| 212 |
> |
VCOPY(nd.prdir, r->rdir); |
| 213 |
> |
transtest = 2; |
| 214 |
> |
} else { |
| 215 |
> |
for (i = 0; i < 3; i++) /* perturb */ |
| 216 |
> |
nd.prdir[i] = r->rdir[i] - |
| 217 |
> |
.75*r->pert[i]; |
| 218 |
> |
normalize(nd.prdir); |
| 219 |
> |
} |
| 220 |
|
} |
| 221 |
|
} else |
| 222 |
|
nd.tdiff = nd.tspec = nd.trans = 0.0; |
| 223 |
|
/* transmitted ray */ |
| 224 |
< |
if (nd.tspec > FTINY && nd.alpha2 <= FTINY) { |
| 224 |
> |
if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) { |
| 225 |
|
RAY lr; |
| 226 |
|
if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) { |
| 227 |
|
VCOPY(lr.rdir, nd.prdir); |
| 233 |
|
transdist = r->rot + lr.rt; |
| 234 |
|
} |
| 235 |
|
} |
| 236 |
+ |
|
| 237 |
|
if (r->crtype & SHADOW) /* the rest is shadow */ |
| 238 |
|
return; |
| 239 |
|
/* diffuse reflection */ |
| 240 |
|
nd.rdiff = 1.0 - nd.trans - nd.rspec; |
| 241 |
|
|
| 242 |
< |
if (nd.rdiff <= FTINY && nd.tdiff <= FTINY && nd.alpha2 <= FTINY) |
| 243 |
< |
return; /* purely specular */ |
| 242 |
> |
if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) |
| 243 |
> |
return; /* 100% pure specular */ |
| 244 |
|
|
| 245 |
+ |
if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING) |
| 246 |
+ |
nd.specfl |= SP_FLAT; |
| 247 |
+ |
|
| 248 |
+ |
if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE)) |
| 249 |
+ |
gaussamp(r, &nd); |
| 250 |
+ |
|
| 251 |
|
if (nd.rdiff > FTINY) { /* ambient from this side */ |
| 252 |
|
ambient(ctmp, r); |
| 253 |
< |
if (nd.alpha2 <= FTINY) |
| 229 |
< |
scalecolor(ctmp, nd.rdiff); |
| 230 |
< |
else |
| 231 |
< |
scalecolor(ctmp, 1.0-nd.trans); |
| 253 |
> |
scalecolor(ctmp, nd.rdiff); |
| 254 |
|
multcolor(ctmp, nd.mcolor); /* modified by material color */ |
| 255 |
|
addcolor(r->rcol, ctmp); /* add to returned color */ |
| 256 |
|
} |
| 257 |
|
if (nd.tdiff > FTINY) { /* ambient from other side */ |
| 258 |
|
flipsurface(r); |
| 259 |
|
ambient(ctmp, r); |
| 260 |
< |
if (nd.alpha2 <= FTINY) |
| 239 |
< |
scalecolor(ctmp, nd.tdiff); |
| 240 |
< |
else |
| 241 |
< |
scalecolor(ctmp, nd.trans); |
| 260 |
> |
scalecolor(ctmp, nd.tdiff); |
| 261 |
|
multcolor(ctmp, nd.mcolor); /* modified by color */ |
| 262 |
|
addcolor(r->rcol, ctmp); |
| 263 |
|
flipsurface(r); |
| 267 |
|
/* check distance */ |
| 268 |
|
if (transtest > bright(r->rcol)) |
| 269 |
|
r->rt = transdist; |
| 270 |
+ |
} |
| 271 |
+ |
|
| 272 |
+ |
|
| 273 |
+ |
static |
| 274 |
+ |
gaussamp(r, np) /* sample gaussian specular */ |
| 275 |
+ |
RAY *r; |
| 276 |
+ |
register NORMDAT *np; |
| 277 |
+ |
{ |
| 278 |
+ |
RAY sr; |
| 279 |
+ |
FVECT u, v, h; |
| 280 |
+ |
double rv[2]; |
| 281 |
+ |
double d, sinp, cosp; |
| 282 |
+ |
int ntries; |
| 283 |
+ |
register int i; |
| 284 |
+ |
/* set up sample coordinates */ |
| 285 |
+ |
v[0] = v[1] = v[2] = 0.0; |
| 286 |
+ |
for (i = 0; i < 3; i++) |
| 287 |
+ |
if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6) |
| 288 |
+ |
break; |
| 289 |
+ |
v[i] = 1.0; |
| 290 |
+ |
fcross(u, v, np->pnorm); |
| 291 |
+ |
normalize(u); |
| 292 |
+ |
fcross(v, np->pnorm, u); |
| 293 |
+ |
/* compute reflection */ |
| 294 |
+ |
if (np->specfl & SP_REFL && |
| 295 |
+ |
rayorigin(&sr, r, SPECULAR, np->rspec) == 0) { |
| 296 |
+ |
dimlist[ndims++] = (int)np->mp; |
| 297 |
+ |
for (ntries = 0; ntries < 10; ntries++) { |
| 298 |
+ |
dimlist[ndims] = ntries * 8912; |
| 299 |
+ |
d = urand(ilhash(dimlist,ndims+1)+samplendx); |
| 300 |
+ |
multisamp(rv, 2, d); |
| 301 |
+ |
d = 2.0*PI * rv[0]; |
| 302 |
+ |
cosp = cos(d); |
| 303 |
+ |
sinp = sin(d); |
| 304 |
+ |
if (rv[1] <= FTINY) |
| 305 |
+ |
d = 1.0; |
| 306 |
+ |
else |
| 307 |
+ |
d = sqrt( np->alpha2 * -log(rv[1]) ); |
| 308 |
+ |
for (i = 0; i < 3; i++) |
| 309 |
+ |
h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]); |
| 310 |
+ |
d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d); |
| 311 |
+ |
for (i = 0; i < 3; i++) |
| 312 |
+ |
sr.rdir[i] = r->rdir[i] + d*h[i]; |
| 313 |
+ |
if (DOT(sr.rdir, r->ron) > FTINY) { |
| 314 |
+ |
rayvalue(&sr); |
| 315 |
+ |
multcolor(sr.rcol, np->scolor); |
| 316 |
+ |
addcolor(r->rcol, sr.rcol); |
| 317 |
+ |
break; |
| 318 |
+ |
} |
| 319 |
+ |
} |
| 320 |
+ |
ndims--; |
| 321 |
+ |
} |
| 322 |
+ |
/* compute transmission */ |
| 323 |
|
} |