| 1 |
– |
/* Copyright (c) 1996 Regents of the University of California */ |
| 2 |
– |
|
| 1 |
|
#ifndef lint |
| 2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
| 2 |
> |
static const char RCSid[] = "$Id$"; |
| 3 |
|
#endif |
| 6 |
– |
|
| 4 |
|
/* |
| 5 |
|
* normal.c - shading function for normal materials. |
| 6 |
|
* |
| 11 |
|
* Later changes described in delta comments. |
| 12 |
|
*/ |
| 13 |
|
|
| 14 |
+ |
#include "copyright.h" |
| 15 |
+ |
|
| 16 |
|
#include "ray.h" |
| 17 |
|
|
| 18 |
|
#include "otypes.h" |
| 19 |
|
|
| 20 |
|
#include "random.h" |
| 21 |
|
|
| 23 |
– |
extern double specthresh; /* specular sampling threshold */ |
| 24 |
– |
extern double specjitter; /* specular sampling jitter */ |
| 25 |
– |
|
| 26 |
– |
extern int backvis; /* back faces visible? */ |
| 27 |
– |
|
| 22 |
|
#ifndef MAXITER |
| 23 |
|
#define MAXITER 10 /* maximum # specular ray attempts */ |
| 24 |
|
#endif |
| 25 |
+ |
/* estimate of Fresnel function */ |
| 26 |
+ |
#define FRESNE(ci) (exp(-6.0*(ci)) - 0.00247875217) |
| 27 |
|
|
| 28 |
< |
static gaussamp(); |
| 28 |
> |
static void gaussamp(); |
| 29 |
|
|
| 30 |
|
/* |
| 31 |
|
* This routine implements the isotropic Gaussian |
| 65 |
|
} NORMDAT; /* normal material data */ |
| 66 |
|
|
| 67 |
|
|
| 68 |
+ |
static void |
| 69 |
|
dirnorm(cval, np, ldir, omega) /* compute source contribution */ |
| 70 |
|
COLOR cval; /* returned coefficient */ |
| 71 |
|
register NORMDAT *np; /* material data */ |
| 73 |
|
double omega; /* light source size */ |
| 74 |
|
{ |
| 75 |
|
double ldot; |
| 76 |
+ |
double ldiff; |
| 77 |
|
double dtmp, d2; |
| 78 |
|
FVECT vtmp; |
| 79 |
|
COLOR ctmp; |
| 85 |
|
if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY) |
| 86 |
|
return; /* wrong side */ |
| 87 |
|
|
| 88 |
< |
if (ldot > FTINY && np->rdiff > FTINY) { |
| 88 |
> |
/* Fresnel estimate */ |
| 89 |
> |
ldiff = np->rdiff; |
| 90 |
> |
if (np->specfl & SP_PURE && (np->rspec > FTINY & ldiff > FTINY)) |
| 91 |
> |
ldiff *= 1. - FRESNE(fabs(ldot)); |
| 92 |
> |
|
| 93 |
> |
if (ldot > FTINY && ldiff > FTINY) { |
| 94 |
|
/* |
| 95 |
|
* Compute and add diffuse reflected component to returned |
| 96 |
|
* color. The diffuse reflected component will always be |
| 97 |
|
* modified by the color of the material. |
| 98 |
|
*/ |
| 99 |
|
copycolor(ctmp, np->mcolor); |
| 100 |
< |
dtmp = ldot * omega * np->rdiff / PI; |
| 100 |
> |
dtmp = ldot * omega * ldiff / PI; |
| 101 |
|
scalecolor(ctmp, dtmp); |
| 102 |
|
addcolor(cval, ctmp); |
| 103 |
|
} |
| 157 |
|
} |
| 158 |
|
|
| 159 |
|
|
| 160 |
+ |
int |
| 161 |
|
m_normal(m, r) /* color a ray that hit something normal */ |
| 162 |
|
register OBJREC *m; |
| 163 |
|
register RAY *r; |
| 164 |
|
{ |
| 165 |
|
NORMDAT nd; |
| 166 |
+ |
double fest; |
| 167 |
|
double transtest, transdist; |
| 168 |
|
double mirtest, mirdist; |
| 169 |
|
int hastexture; |
| 182 |
|
raytrans(r); |
| 183 |
|
return(1); |
| 184 |
|
} |
| 185 |
+ |
raytexture(r, m->omod); |
| 186 |
|
flipsurface(r); /* reorient if backvis */ |
| 187 |
< |
} |
| 187 |
> |
} else |
| 188 |
> |
raytexture(r, m->omod); |
| 189 |
|
nd.mp = m; |
| 190 |
|
nd.rp = r; |
| 191 |
|
/* get material color */ |
| 197 |
|
nd.alpha2 = m->oargs.farg[4]; |
| 198 |
|
if ((nd.alpha2 *= nd.alpha2) <= FTINY) |
| 199 |
|
nd.specfl |= SP_PURE; |
| 200 |
< |
if (r->ro != NULL && isflat(r->ro->otype)) |
| 201 |
< |
nd.specfl |= SP_FLAT; |
| 195 |
< |
/* get modifiers */ |
| 196 |
< |
raytexture(r, m->omod); |
| 197 |
< |
if (hastexture = DOT(r->pert,r->pert) > FTINY*FTINY) |
| 200 |
> |
|
| 201 |
> |
if (hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY)) { |
| 202 |
|
nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */ |
| 203 |
< |
else { |
| 203 |
> |
} else { |
| 204 |
|
VCOPY(nd.pnorm, r->ron); |
| 205 |
|
nd.pdot = r->rod; |
| 206 |
+ |
if (r->ro != NULL && isflat(r->ro->otype)) |
| 207 |
+ |
nd.specfl |= SP_FLAT; |
| 208 |
|
} |
| 209 |
|
if (nd.pdot < .001) |
| 210 |
|
nd.pdot = .001; /* non-zero for dirnorm() */ |
| 212 |
|
mirtest = transtest = 0; |
| 213 |
|
mirdist = transdist = r->rot; |
| 214 |
|
nd.rspec = m->oargs.farg[3]; |
| 215 |
+ |
/* compute Fresnel approx. */ |
| 216 |
+ |
if (nd.specfl & SP_PURE && nd.rspec > FTINY) { |
| 217 |
+ |
fest = FRESNE(r->rod); |
| 218 |
+ |
nd.rspec += fest*(1. - nd.rspec); |
| 219 |
+ |
} else |
| 220 |
+ |
fest = 0.; |
| 221 |
|
/* compute transmission */ |
| 222 |
|
if (m->otype == MAT_TRANS) { |
| 223 |
|
nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec); |
| 244 |
|
} else |
| 245 |
|
nd.tdiff = nd.tspec = nd.trans = 0.0; |
| 246 |
|
/* transmitted ray */ |
| 247 |
< |
if (nd.specfl&SP_TRAN && (nd.specfl&SP_PURE || r->crtype&SHADOW)) { |
| 247 |
> |
if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) { |
| 248 |
|
RAY lr; |
| 249 |
|
if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) { |
| 250 |
|
VCOPY(lr.rdir, nd.prdir); |
| 266 |
|
if (nd.rspec > FTINY) { |
| 267 |
|
nd.specfl |= SP_REFL; |
| 268 |
|
/* compute specular color */ |
| 269 |
< |
if (m->otype == MAT_METAL) |
| 269 |
> |
if (m->otype != MAT_METAL) { |
| 270 |
> |
setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec); |
| 271 |
> |
} else if (fest > FTINY) { |
| 272 |
> |
d = nd.rspec*(1. - fest); |
| 273 |
> |
for (i = 0; i < 3; i++) |
| 274 |
> |
nd.scolor[i] = fest + nd.mcolor[i]*d; |
| 275 |
> |
} else { |
| 276 |
|
copycolor(nd.scolor, nd.mcolor); |
| 277 |
< |
else |
| 278 |
< |
setcolor(nd.scolor, 1.0, 1.0, 1.0); |
| 261 |
< |
scalecolor(nd.scolor, nd.rspec); |
| 277 |
> |
scalecolor(nd.scolor, nd.rspec); |
| 278 |
> |
} |
| 279 |
|
/* check threshold */ |
| 280 |
|
if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY) |
| 281 |
|
nd.specfl |= SP_RBLT; |
| 286 |
|
if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY) |
| 287 |
|
for (i = 0; i < 3; i++) /* safety measure */ |
| 288 |
|
nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i]; |
| 289 |
< |
|
| 290 |
< |
if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) { |
| 291 |
< |
RAY lr; |
| 292 |
< |
if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { |
| 293 |
< |
VCOPY(lr.rdir, nd.vrefl); |
| 294 |
< |
rayvalue(&lr); |
| 295 |
< |
multcolor(lr.rcol, nd.scolor); |
| 296 |
< |
addcolor(r->rcol, lr.rcol); |
| 297 |
< |
if (!hastexture && nd.specfl & SP_FLAT) { |
| 298 |
< |
mirtest = 2.*bright(lr.rcol); |
| 299 |
< |
mirdist = r->rot + lr.rt; |
| 300 |
< |
} |
| 289 |
> |
} |
| 290 |
> |
/* reflected ray */ |
| 291 |
> |
if ((nd.specfl&(SP_REFL|SP_PURE|SP_RBLT)) == (SP_REFL|SP_PURE)) { |
| 292 |
> |
RAY lr; |
| 293 |
> |
if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) { |
| 294 |
> |
VCOPY(lr.rdir, nd.vrefl); |
| 295 |
> |
rayvalue(&lr); |
| 296 |
> |
multcolor(lr.rcol, nd.scolor); |
| 297 |
> |
addcolor(r->rcol, lr.rcol); |
| 298 |
> |
if (!hastexture && nd.specfl & SP_FLAT) { |
| 299 |
> |
mirtest = 2.*bright(lr.rcol); |
| 300 |
> |
mirdist = r->rot + lr.rt; |
| 301 |
|
} |
| 302 |
|
} |
| 303 |
|
} |
| 307 |
|
if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY) |
| 308 |
|
return(1); /* 100% pure specular */ |
| 309 |
|
|
| 310 |
< |
if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE)) |
| 311 |
< |
gaussamp(r, &nd); |
| 310 |
> |
if (!(nd.specfl & SP_PURE)) |
| 311 |
> |
gaussamp(r, &nd); /* checks *BLT flags */ |
| 312 |
|
|
| 313 |
|
if (nd.rdiff > FTINY) { /* ambient from this side */ |
| 314 |
|
ambient(ctmp, r, hastexture?nd.pnorm:r->ron); |
| 350 |
|
} |
| 351 |
|
|
| 352 |
|
|
| 353 |
< |
static |
| 353 |
> |
static void |
| 354 |
|
gaussamp(r, np) /* sample gaussian specular */ |
| 355 |
|
RAY *r; |
| 356 |
|
register NORMDAT *np; |
| 385 |
|
d = urand(ilhash(dimlist,ndims)+samplendx); |
| 386 |
|
multisamp(rv, 2, d); |
| 387 |
|
d = 2.0*PI * rv[0]; |
| 388 |
< |
cosp = cos(d); |
| 389 |
< |
sinp = sin(d); |
| 388 |
> |
cosp = tcos(d); |
| 389 |
> |
sinp = tsin(d); |
| 390 |
|
rv[1] = 1.0 - specjitter*rv[1]; |
| 391 |
|
if (rv[1] <= FTINY) |
| 392 |
|
d = 1.0; |
| 417 |
|
d = urand(ilhash(dimlist,ndims)+1823+samplendx); |
| 418 |
|
multisamp(rv, 2, d); |
| 419 |
|
d = 2.0*PI * rv[0]; |
| 420 |
< |
cosp = cos(d); |
| 421 |
< |
sinp = sin(d); |
| 420 |
> |
cosp = tcos(d); |
| 421 |
> |
sinp = tsin(d); |
| 422 |
|
rv[1] = 1.0 - specjitter*rv[1]; |
| 423 |
|
if (rv[1] <= FTINY) |
| 424 |
|
d = 1.0; |
| 425 |
|
else |
| 426 |
< |
d = sqrt( -log(rv[1]) * np->alpha2 ); |
| 426 |
> |
d = sqrt( np->alpha2 * -log(rv[1]) ); |
| 427 |
|
for (i = 0; i < 3; i++) |
| 428 |
|
sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]); |
| 429 |
|
if (DOT(sr.rdir, r->ron) < -FTINY) { |