| 1 |
greg |
1.1 |
/* Copyright (c) 1986 Regents of the University of California */
|
| 2 |
|
|
|
| 3 |
|
|
#ifndef lint
|
| 4 |
|
|
static char SCCSid[] = "$SunId$ LBL";
|
| 5 |
|
|
#endif
|
| 6 |
|
|
|
| 7 |
|
|
/*
|
| 8 |
|
|
* dielectric.c - shading function for transparent materials.
|
| 9 |
|
|
*
|
| 10 |
|
|
* 9/6/85
|
| 11 |
|
|
*/
|
| 12 |
|
|
|
| 13 |
|
|
#include "ray.h"
|
| 14 |
|
|
|
| 15 |
|
|
#include "otypes.h"
|
| 16 |
|
|
|
| 17 |
|
|
#ifdef DISPERSE
|
| 18 |
|
|
#include "source.h"
|
| 19 |
|
|
#endif
|
| 20 |
|
|
|
| 21 |
|
|
/*
|
| 22 |
|
|
* Explicit calculations for Fresnel's equation are performed,
|
| 23 |
|
|
* but only one square root computation is necessary.
|
| 24 |
|
|
* The index of refraction is given as a Hartmann equation
|
| 25 |
|
|
* with lambda0 equal to zero. If the slope of Hartmann's
|
| 26 |
|
|
* equation is non-zero, the material disperses light upon
|
| 27 |
|
|
* refraction. This condition is examined on rays traced to
|
| 28 |
|
|
* light sources. If a ray is exiting a dielectric material, we
|
| 29 |
|
|
* check the sources to see if any would cause bright color to be
|
| 30 |
|
|
* directed to the viewer due to dispersion. This gives colorful
|
| 31 |
|
|
* sparkle to crystals, etc. (Only if DISPERSE is defined!)
|
| 32 |
|
|
*
|
| 33 |
|
|
* Arguments for MAT_DIELECTRIC are:
|
| 34 |
|
|
* red grn blu rndx Hartmann
|
| 35 |
|
|
*
|
| 36 |
|
|
* Arguments for MAT_INTERFACE are:
|
| 37 |
|
|
* red1 grn1 blu1 rndx1 red2 grn2 blu2 rndx2
|
| 38 |
|
|
*
|
| 39 |
|
|
* The primaries are material transmission per unit length.
|
| 40 |
|
|
* MAT_INTERFACE uses dielectric1 for inside and dielectric2 for
|
| 41 |
|
|
* outside.
|
| 42 |
|
|
*/
|
| 43 |
|
|
|
| 44 |
|
|
|
| 45 |
|
|
#define MLAMBDA 500 /* mean lambda */
|
| 46 |
|
|
#define MAXLAMBDA 779 /* maximum lambda */
|
| 47 |
|
|
#define MINLAMBDA 380 /* minimum lambda */
|
| 48 |
|
|
|
| 49 |
|
|
#define MINCOS 0.997 /* minimum dot product for dispersion */
|
| 50 |
|
|
|
| 51 |
|
|
|
| 52 |
|
|
m_dielectric(m, r) /* color a ray which hit something transparent */
|
| 53 |
|
|
OBJREC *m;
|
| 54 |
|
|
register RAY *r;
|
| 55 |
|
|
{
|
| 56 |
|
|
double sqrt(), pow();
|
| 57 |
|
|
double cos1, cos2, nratio;
|
| 58 |
|
|
COLOR mcolor;
|
| 59 |
|
|
double mabsorp;
|
| 60 |
greg |
1.5 |
double refl, trans;
|
| 61 |
greg |
1.1 |
FVECT dnorm;
|
| 62 |
|
|
double d1, d2;
|
| 63 |
|
|
RAY p;
|
| 64 |
|
|
register int i;
|
| 65 |
|
|
|
| 66 |
|
|
if (m->oargs.nfargs != (m->otype==MAT_DIELECTRIC ? 5 : 8))
|
| 67 |
|
|
objerror(m, USER, "bad arguments");
|
| 68 |
|
|
|
| 69 |
greg |
1.5 |
r->rt = r->rot; /* just use ray length */
|
| 70 |
|
|
|
| 71 |
greg |
1.1 |
raytexture(r, m->omod); /* get modifiers */
|
| 72 |
|
|
|
| 73 |
|
|
cos1 = raynormal(dnorm, r); /* cosine of theta1 */
|
| 74 |
|
|
/* index of refraction */
|
| 75 |
|
|
if (m->otype == MAT_DIELECTRIC)
|
| 76 |
|
|
nratio = m->oargs.farg[3] + m->oargs.farg[4]/MLAMBDA;
|
| 77 |
|
|
else
|
| 78 |
|
|
nratio = m->oargs.farg[3] / m->oargs.farg[7];
|
| 79 |
|
|
|
| 80 |
|
|
if (cos1 < 0.0) { /* inside */
|
| 81 |
|
|
cos1 = -cos1;
|
| 82 |
|
|
dnorm[0] = -dnorm[0];
|
| 83 |
|
|
dnorm[1] = -dnorm[1];
|
| 84 |
|
|
dnorm[2] = -dnorm[2];
|
| 85 |
|
|
setcolor(mcolor, pow(m->oargs.farg[0], r->rot),
|
| 86 |
|
|
pow(m->oargs.farg[1], r->rot),
|
| 87 |
|
|
pow(m->oargs.farg[2], r->rot));
|
| 88 |
|
|
} else { /* outside */
|
| 89 |
|
|
nratio = 1.0 / nratio;
|
| 90 |
|
|
if (m->otype == MAT_INTERFACE)
|
| 91 |
|
|
setcolor(mcolor, pow(m->oargs.farg[4], r->rot),
|
| 92 |
|
|
pow(m->oargs.farg[5], r->rot),
|
| 93 |
|
|
pow(m->oargs.farg[6], r->rot));
|
| 94 |
|
|
else
|
| 95 |
|
|
setcolor(mcolor, 1.0, 1.0, 1.0);
|
| 96 |
|
|
}
|
| 97 |
greg |
1.2 |
mabsorp = bright(mcolor);
|
| 98 |
greg |
1.1 |
|
| 99 |
|
|
d2 = 1.0 - nratio*nratio*(1.0 - cos1*cos1); /* compute cos theta2 */
|
| 100 |
|
|
|
| 101 |
|
|
if (d2 < FTINY) /* total reflection */
|
| 102 |
|
|
|
| 103 |
|
|
refl = 1.0;
|
| 104 |
|
|
|
| 105 |
|
|
else { /* refraction occurs */
|
| 106 |
|
|
/* compute Fresnel's equations */
|
| 107 |
|
|
cos2 = sqrt(d2);
|
| 108 |
|
|
d1 = cos1;
|
| 109 |
|
|
d2 = nratio*cos2;
|
| 110 |
|
|
d1 = (d1 - d2) / (d1 + d2);
|
| 111 |
|
|
refl = d1 * d1;
|
| 112 |
|
|
|
| 113 |
|
|
d1 = 1.0 / cos1;
|
| 114 |
|
|
d2 = nratio / cos2;
|
| 115 |
|
|
d1 = (d1 - d2) / (d1 + d2);
|
| 116 |
|
|
refl += d1 * d1;
|
| 117 |
|
|
|
| 118 |
|
|
refl /= 2.0;
|
| 119 |
|
|
trans = 1.0 - refl;
|
| 120 |
|
|
|
| 121 |
|
|
if (rayorigin(&p, r, REFRACTED, mabsorp*trans) == 0) {
|
| 122 |
|
|
|
| 123 |
|
|
/* compute refracted ray */
|
| 124 |
|
|
d1 = nratio*cos1 - cos2;
|
| 125 |
|
|
for (i = 0; i < 3; i++)
|
| 126 |
|
|
p.rdir[i] = nratio*r->rdir[i] + d1*dnorm[i];
|
| 127 |
|
|
|
| 128 |
|
|
#ifdef DISPERSE
|
| 129 |
|
|
if (m->otype != MAT_DIELECTRIC
|
| 130 |
|
|
|| r->rod > 0.0
|
| 131 |
|
|
|| r->crtype & SHADOW
|
| 132 |
greg |
1.6 |
|| directinvis
|
| 133 |
greg |
1.1 |
|| m->oargs.farg[4] == 0.0
|
| 134 |
|
|
|| !disperse(m, r, p.rdir, trans))
|
| 135 |
|
|
#endif
|
| 136 |
|
|
{
|
| 137 |
|
|
rayvalue(&p);
|
| 138 |
|
|
multcolor(mcolor, r->pcol); /* modify */
|
| 139 |
|
|
scalecolor(p.rcol, trans);
|
| 140 |
|
|
addcolor(r->rcol, p.rcol);
|
| 141 |
|
|
}
|
| 142 |
|
|
}
|
| 143 |
|
|
}
|
| 144 |
|
|
|
| 145 |
|
|
if (!(r->crtype & SHADOW) &&
|
| 146 |
|
|
rayorigin(&p, r, REFLECTED, mabsorp*refl) == 0) {
|
| 147 |
|
|
|
| 148 |
|
|
/* compute reflected ray */
|
| 149 |
|
|
for (i = 0; i < 3; i++)
|
| 150 |
|
|
p.rdir[i] = r->rdir[i] + 2.0*cos1*dnorm[i];
|
| 151 |
|
|
|
| 152 |
|
|
rayvalue(&p); /* reflected ray value */
|
| 153 |
|
|
|
| 154 |
|
|
scalecolor(p.rcol, refl); /* color contribution */
|
| 155 |
|
|
addcolor(r->rcol, p.rcol);
|
| 156 |
|
|
}
|
| 157 |
|
|
|
| 158 |
|
|
multcolor(r->rcol, mcolor); /* multiply by transmittance */
|
| 159 |
|
|
}
|
| 160 |
|
|
|
| 161 |
|
|
|
| 162 |
|
|
#ifdef DISPERSE
|
| 163 |
|
|
|
| 164 |
|
|
static
|
| 165 |
|
|
disperse(m, r, vt, tr) /* check light sources for dispersion */
|
| 166 |
|
|
OBJREC *m;
|
| 167 |
|
|
RAY *r;
|
| 168 |
|
|
FVECT vt;
|
| 169 |
|
|
double tr;
|
| 170 |
|
|
{
|
| 171 |
|
|
double sqrt();
|
| 172 |
|
|
RAY sray, *entray;
|
| 173 |
|
|
FVECT v1, v2, n1, n2;
|
| 174 |
|
|
FVECT dv, v2Xdv;
|
| 175 |
|
|
double v2Xdvv2Xdv;
|
| 176 |
greg |
1.7 |
int success = 0;
|
| 177 |
|
|
SRCINDEX si;
|
| 178 |
greg |
1.1 |
FVECT vtmp1, vtmp2;
|
| 179 |
|
|
double dtmp1, dtmp2;
|
| 180 |
|
|
int l1, l2;
|
| 181 |
|
|
COLOR ctmp;
|
| 182 |
|
|
int i;
|
| 183 |
|
|
|
| 184 |
|
|
/*
|
| 185 |
|
|
* This routine computes dispersion to the first order using
|
| 186 |
|
|
* the following assumptions:
|
| 187 |
|
|
*
|
| 188 |
|
|
* 1) The dependency of the index of refraction on wavelength
|
| 189 |
|
|
* is approximated by Hartmann's equation with lambda0
|
| 190 |
|
|
* equal to zero.
|
| 191 |
|
|
* 2) The entry and exit locations are constant with respect
|
| 192 |
|
|
* to dispersion.
|
| 193 |
|
|
*
|
| 194 |
|
|
* The second assumption permits us to model dispersion without
|
| 195 |
|
|
* having to sample refracted directions. We assume that the
|
| 196 |
|
|
* geometry inside the material is constant, and concern ourselves
|
| 197 |
|
|
* only with the relationship between the entering and exiting ray.
|
| 198 |
|
|
* We compute the first derivatives of the entering and exiting
|
| 199 |
|
|
* refraction with respect to the index of refraction. This
|
| 200 |
|
|
* is then used in a first order Taylor series to determine the
|
| 201 |
|
|
* index of refraction necessary to send the exiting ray to each
|
| 202 |
|
|
* light source.
|
| 203 |
|
|
* If an exiting ray hits a light source within the refraction
|
| 204 |
|
|
* boundaries, we sum all the frequencies over the disc of the
|
| 205 |
|
|
* light source to determine the resulting color. A smaller light
|
| 206 |
|
|
* source will therefore exhibit a sharper spectrum.
|
| 207 |
|
|
*/
|
| 208 |
|
|
|
| 209 |
|
|
if (!(r->crtype & REFRACTED)) { /* ray started in material */
|
| 210 |
|
|
VCOPY(v1, r->rdir);
|
| 211 |
|
|
n1[0] = -r->rdir[0]; n1[1] = -r->rdir[1]; n1[2] = -r->rdir[2];
|
| 212 |
|
|
} else {
|
| 213 |
|
|
/* find entry point */
|
| 214 |
|
|
for (entray = r; entray->rtype != REFRACTED;
|
| 215 |
|
|
entray = entray->parent)
|
| 216 |
|
|
;
|
| 217 |
|
|
entray = entray->parent;
|
| 218 |
|
|
if (entray->crtype & REFRACTED) /* too difficult */
|
| 219 |
|
|
return(0);
|
| 220 |
|
|
VCOPY(v1, entray->rdir);
|
| 221 |
|
|
VCOPY(n1, entray->ron);
|
| 222 |
|
|
}
|
| 223 |
|
|
VCOPY(v2, vt); /* exiting ray */
|
| 224 |
|
|
VCOPY(n2, r->ron);
|
| 225 |
|
|
|
| 226 |
|
|
/* first order dispersion approx. */
|
| 227 |
|
|
dtmp1 = DOT(n1, v1);
|
| 228 |
|
|
dtmp2 = DOT(n2, v2);
|
| 229 |
|
|
for (i = 0; i < 3; i++)
|
| 230 |
|
|
dv[i] = v1[i] + v2[i] - n1[i]/dtmp1 - n2[i]/dtmp2;
|
| 231 |
|
|
|
| 232 |
|
|
if (DOT(dv, dv) <= FTINY) /* null effect */
|
| 233 |
|
|
return(0);
|
| 234 |
|
|
/* compute plane normal */
|
| 235 |
|
|
fcross(v2Xdv, v2, dv);
|
| 236 |
|
|
v2Xdvv2Xdv = DOT(v2Xdv, v2Xdv);
|
| 237 |
|
|
|
| 238 |
|
|
/* check sources */
|
| 239 |
greg |
1.7 |
initsrcindex(&si);
|
| 240 |
|
|
while (srcray(&sray, r, &si)) {
|
| 241 |
greg |
1.1 |
|
| 242 |
greg |
1.7 |
if (DOT(sray.rdir, v2) < MINCOS)
|
| 243 |
greg |
1.1 |
continue; /* bad source */
|
| 244 |
|
|
/* adjust source ray */
|
| 245 |
|
|
|
| 246 |
|
|
dtmp1 = DOT(v2Xdv, sray.rdir) / v2Xdvv2Xdv;
|
| 247 |
|
|
sray.rdir[0] -= dtmp1 * v2Xdv[0];
|
| 248 |
|
|
sray.rdir[1] -= dtmp1 * v2Xdv[1];
|
| 249 |
|
|
sray.rdir[2] -= dtmp1 * v2Xdv[2];
|
| 250 |
|
|
|
| 251 |
|
|
l1 = lambda(m, v2, dv, sray.rdir); /* mean lambda */
|
| 252 |
|
|
|
| 253 |
|
|
if (l1 > MAXLAMBDA || l1 < MINLAMBDA) /* not visible */
|
| 254 |
|
|
continue;
|
| 255 |
|
|
/* trace source ray */
|
| 256 |
|
|
normalize(sray.rdir);
|
| 257 |
|
|
rayvalue(&sray);
|
| 258 |
greg |
1.2 |
if (bright(sray.rcol) <= FTINY) /* missed it */
|
| 259 |
greg |
1.1 |
continue;
|
| 260 |
|
|
|
| 261 |
|
|
/*
|
| 262 |
|
|
* Compute spectral sum over diameter of source.
|
| 263 |
|
|
* First find directions for rays going to opposite
|
| 264 |
|
|
* sides of source, then compute wavelengths for each.
|
| 265 |
|
|
*/
|
| 266 |
|
|
|
| 267 |
|
|
fcross(vtmp1, v2Xdv, sray.rdir);
|
| 268 |
greg |
1.7 |
dtmp1 = sqrt(si.dom / v2Xdvv2Xdv / PI);
|
| 269 |
greg |
1.1 |
|
| 270 |
|
|
/* compute first ray */
|
| 271 |
|
|
for (i = 0; i < 3; i++)
|
| 272 |
|
|
vtmp2[i] = sray.rdir[i] + dtmp1*vtmp1[i];
|
| 273 |
|
|
|
| 274 |
|
|
l1 = lambda(m, v2, dv, vtmp2); /* first lambda */
|
| 275 |
|
|
if (l1 < 0)
|
| 276 |
|
|
continue;
|
| 277 |
|
|
/* compute second ray */
|
| 278 |
|
|
for (i = 0; i < 3; i++)
|
| 279 |
|
|
vtmp2[i] = sray.rdir[i] - dtmp1*vtmp1[i];
|
| 280 |
|
|
|
| 281 |
|
|
l2 = lambda(m, v2, dv, vtmp2); /* second lambda */
|
| 282 |
|
|
if (l2 < 0)
|
| 283 |
|
|
continue;
|
| 284 |
|
|
/* compute color from spectrum */
|
| 285 |
|
|
if (l1 < l2)
|
| 286 |
|
|
spec_rgb(ctmp, l1, l2);
|
| 287 |
|
|
else
|
| 288 |
|
|
spec_rgb(ctmp, l2, l1);
|
| 289 |
|
|
multcolor(ctmp, sray.rcol);
|
| 290 |
|
|
scalecolor(ctmp, tr);
|
| 291 |
|
|
addcolor(r->rcol, ctmp);
|
| 292 |
|
|
success++;
|
| 293 |
|
|
}
|
| 294 |
|
|
return(success);
|
| 295 |
|
|
}
|
| 296 |
|
|
|
| 297 |
|
|
|
| 298 |
|
|
static int
|
| 299 |
|
|
lambda(m, v2, dv, lr) /* compute lambda for material */
|
| 300 |
|
|
register OBJREC *m;
|
| 301 |
|
|
FVECT v2, dv, lr;
|
| 302 |
|
|
{
|
| 303 |
|
|
FVECT lrXdv, v2Xlr;
|
| 304 |
|
|
double dtmp, denom;
|
| 305 |
|
|
int i;
|
| 306 |
|
|
|
| 307 |
|
|
fcross(lrXdv, lr, dv);
|
| 308 |
|
|
for (i = 0; i < 3; i++)
|
| 309 |
|
|
if (lrXdv[i] > FTINY || lrXdv[i] < -FTINY)
|
| 310 |
|
|
break;
|
| 311 |
|
|
if (i >= 3)
|
| 312 |
|
|
return(-1);
|
| 313 |
|
|
|
| 314 |
|
|
fcross(v2Xlr, v2, lr);
|
| 315 |
|
|
|
| 316 |
|
|
dtmp = m->oargs.farg[4] / MLAMBDA;
|
| 317 |
|
|
denom = dtmp + v2Xlr[i]/lrXdv[i] * (m->oargs.farg[3] + dtmp);
|
| 318 |
|
|
|
| 319 |
|
|
if (denom < FTINY)
|
| 320 |
|
|
return(-1);
|
| 321 |
|
|
|
| 322 |
|
|
return(m->oargs.farg[4] / denom);
|
| 323 |
|
|
}
|
| 324 |
|
|
|
| 325 |
|
|
#endif /* DISPERSE */
|