| 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 |
|
|
* glass.c - simpler shading function for thin glass surfaces.
|
| 9 |
|
|
*
|
| 10 |
|
|
* 11/14/86
|
| 11 |
|
|
*/
|
| 12 |
|
|
|
| 13 |
|
|
#include "ray.h"
|
| 14 |
|
|
|
| 15 |
|
|
/*
|
| 16 |
|
|
* This definition of glass provides for a quick calculation
|
| 17 |
|
|
* using a single surface where two closely spaced parallel
|
| 18 |
|
|
* dielectric surfaces would otherwise be used. The chief
|
| 19 |
|
|
* advantage to using this material is speed, since internal
|
| 20 |
|
|
* reflections are avoided.
|
| 21 |
|
|
*
|
| 22 |
|
|
* The specification for glass is as follows:
|
| 23 |
|
|
*
|
| 24 |
|
|
* modifier glass id
|
| 25 |
|
|
* 0
|
| 26 |
|
|
* 0
|
| 27 |
|
|
* 3 red grn blu
|
| 28 |
|
|
*
|
| 29 |
|
|
* The color is used for the transmission at normal incidence.
|
| 30 |
|
|
* To compute transmission (tn) from transmissivity (Tn) use:
|
| 31 |
|
|
*
|
| 32 |
|
|
* tn = (sqrt(.8402528435+.0072522239*Tn*Tn)-.9166530661)/.0036261119/Tn
|
| 33 |
|
|
*
|
| 34 |
|
|
* The transmission of standard 88% transmissivity glass is 0.96.
|
| 35 |
|
|
* If we appear to hit the back side of the surface, then we
|
| 36 |
|
|
* turn the normal around.
|
| 37 |
|
|
*/
|
| 38 |
|
|
|
| 39 |
|
|
#define RINDEX 1.52 /* refractive index of glass */
|
| 40 |
|
|
|
| 41 |
|
|
|
| 42 |
|
|
m_glass(m, r) /* color a ray which hit a thin glass surface */
|
| 43 |
|
|
OBJREC *m;
|
| 44 |
|
|
register RAY *r;
|
| 45 |
|
|
{
|
| 46 |
|
|
double sqrt(), pow();
|
| 47 |
|
|
COLOR mcolor;
|
| 48 |
|
|
double pdot;
|
| 49 |
|
|
FVECT pnorm;
|
| 50 |
|
|
double cos2;
|
| 51 |
|
|
COLOR trans, refl;
|
| 52 |
|
|
double d, r1;
|
| 53 |
|
|
RAY p;
|
| 54 |
|
|
register int i;
|
| 55 |
|
|
|
| 56 |
|
|
if (m->oargs.nfargs != 3)
|
| 57 |
|
|
objerror(m, USER, "bad arguments");
|
| 58 |
|
|
|
| 59 |
|
|
setcolor(mcolor, m->oargs.farg[0], m->oargs.farg[1], m->oargs.farg[2]);
|
| 60 |
|
|
|
| 61 |
|
|
if (r->rod < 0.0) /* reorient if necessary */
|
| 62 |
|
|
flipsurface(r);
|
| 63 |
greg |
1.5 |
r->rt = r->rot; /* default ray length */
|
| 64 |
greg |
1.1 |
/* get modifiers */
|
| 65 |
|
|
raytexture(r, m->omod);
|
| 66 |
|
|
pdot = raynormal(pnorm, r);
|
| 67 |
|
|
/* angular transmission */
|
| 68 |
|
|
cos2 = sqrt( (1.0-1.0/RINDEX/RINDEX) +
|
| 69 |
|
|
pdot*pdot/(RINDEX*RINDEX) );
|
| 70 |
|
|
setcolor(mcolor, pow(colval(mcolor,RED), 1.0/cos2),
|
| 71 |
|
|
pow(colval(mcolor,GRN), 1.0/cos2),
|
| 72 |
|
|
pow(colval(mcolor,BLU), 1.0/cos2));
|
| 73 |
|
|
|
| 74 |
|
|
/* compute reflection */
|
| 75 |
|
|
r1 = (pdot - RINDEX*cos2) / (pdot + RINDEX*cos2);
|
| 76 |
|
|
d = (1.0/pdot - RINDEX/cos2) / (1.0/pdot + RINDEX/cos2);
|
| 77 |
|
|
r1 = (r1*r1 + d*d) / 2.0;
|
| 78 |
|
|
/* compute transmittance */
|
| 79 |
|
|
for (i = 0; i < 3; i++) {
|
| 80 |
|
|
d = colval(mcolor, i);
|
| 81 |
|
|
colval(trans,i) = (1.0-r1)*(1.0-r1)*d / (1.0 - r1*r1*d*d);
|
| 82 |
|
|
}
|
| 83 |
|
|
/* transmitted ray */
|
| 84 |
greg |
1.2 |
if (rayorigin(&p, r, TRANS, bright(trans)) == 0) {
|
| 85 |
greg |
1.6 |
for (i = 0; i < 3; i++) /* perturb direction */
|
| 86 |
|
|
p.rdir[i] = r->rdir[i] - r->pert[i]/RINDEX;
|
| 87 |
|
|
normalize(p.rdir);
|
| 88 |
greg |
1.1 |
rayvalue(&p);
|
| 89 |
|
|
multcolor(p.rcol, r->pcol); /* modify */
|
| 90 |
|
|
multcolor(p.rcol, trans);
|
| 91 |
|
|
addcolor(r->rcol, p.rcol);
|
| 92 |
greg |
1.5 |
if (bright(p.rcol) > .5)
|
| 93 |
|
|
r->rt = r->rot + p.rt;
|
| 94 |
greg |
1.1 |
}
|
| 95 |
greg |
1.3 |
|
| 96 |
greg |
1.1 |
if (r->crtype & SHADOW) /* skip reflected ray */
|
| 97 |
|
|
return;
|
| 98 |
|
|
/* compute reflectance */
|
| 99 |
|
|
for (i = 0; i < 3; i++) {
|
| 100 |
|
|
d = colval(mcolor, i);
|
| 101 |
|
|
d *= d;
|
| 102 |
|
|
colval(refl,i) = r1 * (1.0 + (1.0-2.0*r1)*d) / (1.0 - r1*r1*d);
|
| 103 |
|
|
}
|
| 104 |
|
|
/* reflected ray */
|
| 105 |
greg |
1.2 |
if (rayorigin(&p, r, REFLECTED, bright(refl)) == 0) {
|
| 106 |
greg |
1.1 |
for (i = 0; i < 3; i++)
|
| 107 |
|
|
p.rdir[i] = r->rdir[i] + 2.0*pdot*pnorm[i];
|
| 108 |
|
|
rayvalue(&p);
|
| 109 |
|
|
multcolor(p.rcol, refl);
|
| 110 |
|
|
addcolor(r->rcol, p.rcol);
|
| 111 |
|
|
}
|
| 112 |
|
|
}
|