--- ray/src/rt/dielectric.c 1991/10/21 12:58:12 1.7 +++ ray/src/rt/dielectric.c 1996/07/11 15:14:05 2.12 @@ -16,6 +16,8 @@ static char SCCSid[] = "$SunId$ LBL"; #ifdef DISPERSE #include "source.h" +static disperse(); +static int lambda(); #endif /* @@ -48,14 +50,29 @@ static char SCCSid[] = "$SunId$ LBL"; #define MINCOS 0.997 /* minimum dot product for dispersion */ +extern COLOR cextinction; /* global coefficient of extinction */ +extern COLOR salbedo; /* global scattering albedo */ -m_dielectric(m, r) /* color a ray which hit something transparent */ + +static double +mylog(x) /* special log for extinction coefficients */ +double x; +{ + if (x < 1e-40) + return(-100.); + if (x >= 1.) + return(0.); + return(log(x)); +} + + +m_dielectric(m, r) /* color a ray which hit a dielectric interface */ OBJREC *m; register RAY *r; { - double sqrt(), pow(); double cos1, cos2, nratio; - COLOR mcolor; + COLOR ctrans; + COLOR talb; double mabsorp; double refl, trans; FVECT dnorm; @@ -66,8 +83,6 @@ register RAY *r; if (m->oargs.nfargs != (m->otype==MAT_DIELECTRIC ? 5 : 8)) objerror(m, USER, "bad arguments"); - r->rt = r->rot; /* just use ray length */ - raytexture(r, m->omod); /* get modifiers */ cos1 = raynormal(dnorm, r); /* cosine of theta1 */ @@ -82,19 +97,45 @@ register RAY *r; dnorm[0] = -dnorm[0]; dnorm[1] = -dnorm[1]; dnorm[2] = -dnorm[2]; - setcolor(mcolor, pow(m->oargs.farg[0], r->rot), - pow(m->oargs.farg[1], r->rot), - pow(m->oargs.farg[2], r->rot)); + setcolor(r->cext, -mylog(m->oargs.farg[0]*colval(r->pcol,RED)), + -mylog(m->oargs.farg[1]*colval(r->pcol,GRN)), + -mylog(m->oargs.farg[2]*colval(r->pcol,BLU))); + setcolor(r->albedo, 0., 0., 0.); + r->gecc = 0.; + if (m->otype == MAT_INTERFACE) { + setcolor(ctrans, + -mylog(m->oargs.farg[4]*colval(r->pcol,RED)), + -mylog(m->oargs.farg[5]*colval(r->pcol,GRN)), + -mylog(m->oargs.farg[6]*colval(r->pcol,BLU))); + setcolor(talb, 0., 0., 0.); + } else { + copycolor(ctrans, cextinction); + copycolor(talb, salbedo); + } } else { /* outside */ nratio = 1.0 / nratio; - if (m->otype == MAT_INTERFACE) - setcolor(mcolor, pow(m->oargs.farg[4], r->rot), - pow(m->oargs.farg[5], r->rot), - pow(m->oargs.farg[6], r->rot)); - else - setcolor(mcolor, 1.0, 1.0, 1.0); + + setcolor(ctrans, -mylog(m->oargs.farg[0]*colval(r->pcol,RED)), + -mylog(m->oargs.farg[1]*colval(r->pcol,GRN)), + -mylog(m->oargs.farg[2]*colval(r->pcol,BLU))); + setcolor(talb, 0., 0., 0.); + if (m->otype == MAT_INTERFACE) { + setcolor(r->cext, + -mylog(m->oargs.farg[4]*colval(r->pcol,RED)), + -mylog(m->oargs.farg[5]*colval(r->pcol,GRN)), + -mylog(m->oargs.farg[6]*colval(r->pcol,BLU))); + setcolor(r->albedo, 0., 0., 0.); + r->gecc = 0.; + } } - mabsorp = bright(mcolor); + /* estimate absorption */ + mabsorp = colval(r->cext,RED) < colval(r->cext,GRN) ? + colval(r->cext,RED) : colval(r->cext,GRN); + if (colval(r->cext,BLU) < mabsorp) mabsorp = colval(r->cext,BLU); + if (mabsorp > 0.) + mabsorp = exp(-mabsorp*r->rot); /* conservative */ + else + mabsorp = 1.0; d2 = 1.0 - nratio*nratio*(1.0 - cos1*cos1); /* compute cos theta2 */ @@ -115,7 +156,7 @@ register RAY *r; d1 = (d1 - d2) / (d1 + d2); refl += d1 * d1; - refl /= 2.0; + refl *= 0.5; trans = 1.0 - refl; if (rayorigin(&p, r, REFRACTED, mabsorp*trans) == 0) { @@ -129,15 +170,19 @@ register RAY *r; if (m->otype != MAT_DIELECTRIC || r->rod > 0.0 || r->crtype & SHADOW - || directinvis + || !directvis || m->oargs.farg[4] == 0.0 - || !disperse(m, r, p.rdir, trans)) + || !disperse(m, r, p.rdir, + trans, ctrans, talb)) #endif { + copycolor(p.cext, ctrans); + copycolor(p.albedo, talb); rayvalue(&p); - multcolor(mcolor, r->pcol); /* modify */ scalecolor(p.rcol, trans); addcolor(r->rcol, p.rcol); + if (nratio >= 1.0-FTINY && nratio <= 1.0+FTINY) + r->rt = r->rot + p.rt; } } } @@ -154,21 +199,21 @@ register RAY *r; scalecolor(p.rcol, refl); /* color contribution */ addcolor(r->rcol, p.rcol); } - - multcolor(r->rcol, mcolor); /* multiply by transmittance */ + /* rayvalue() computes absorption */ + return(1); } #ifdef DISPERSE static -disperse(m, r, vt, tr) /* check light sources for dispersion */ +disperse(m, r, vt, tr, cet, abt) /* check light sources for dispersion */ OBJREC *m; RAY *r; FVECT vt; double tr; +COLOR cet, abt; { - double sqrt(); RAY sray, *entray; FVECT v1, v2, n1, n2; FVECT dv, v2Xdv; @@ -253,6 +298,8 @@ double tr; if (l1 > MAXLAMBDA || l1 < MINLAMBDA) /* not visible */ continue; /* trace source ray */ + copycolor(sray.cext, cet); + copycolor(sray.albedo, abt); normalize(sray.rdir); rayvalue(&sray); if (bright(sray.rcol) <= FTINY) /* missed it */