--- ray/src/rt/dielectric.c 1991/08/05 13:52:20 1.6 +++ ray/src/rt/dielectric.c 2004/03/30 16:13:01 2.18 @@ -1,23 +1,25 @@ -/* Copyright (c) 1986 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: dielectric.c,v 2.18 2004/03/30 16:13:01 schorsch Exp $"; #endif - /* * dielectric.c - shading function for transparent materials. - * - * 9/6/85 */ -#include "ray.h" +#include "copyright.h" +#include "ray.h" #include "otypes.h" +#include "rtotypes.h" #ifdef DISPERSE #include "source.h" +static int disperse(OBJREC *m,RAY *r,FVECT vt,double tr,COLOR cet,COLOR abt); +static int lambda(OBJREC *m, FVECT v2, FVECT dv, FVECT lr); #endif +static double mylog(double x); + + /* * Explicit calculations for Fresnel's equation are performed, * but only one square root computation is necessary. @@ -49,14 +51,29 @@ static char SCCSid[] = "$SunId$ LBL"; #define MINCOS 0.997 /* minimum dot product for dispersion */ -m_dielectric(m, r) /* color a ray which hit something transparent */ -OBJREC *m; -register RAY *r; +static double +mylog( /* special log for extinction coefficients */ + double x +) { - double sqrt(), pow(); + if (x < 1e-40) + return(-100.); + if (x >= 1.) + return(0.); + return(log(x)); +} + + +extern int +m_dielectric( /* color a ray which hit a dielectric interface */ + OBJREC *m, + register RAY *r +) +{ double cos1, cos2, nratio; - COLOR mcolor; - double mabsorp; + COLOR ctrans; + COLOR talb; + int hastexture; double refl, trans; FVECT dnorm; double d1, d2; @@ -66,11 +83,14 @@ 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 */ + if ( (hastexture = DOT(r->pert,r->pert) > FTINY*FTINY) ) + cos1 = raynormal(dnorm, r); /* perturb normal */ + else { + VCOPY(dnorm, r->ron); + cos1 = r->rod; + } /* index of refraction */ if (m->otype == MAT_DIELECTRIC) nratio = m->oargs.farg[3] + m->oargs.farg[4]/MLAMBDA; @@ -78,23 +98,42 @@ register RAY *r; nratio = m->oargs.farg[3] / m->oargs.farg[7]; if (cos1 < 0.0) { /* inside */ + hastexture = -hastexture; cos1 = -cos1; 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); d2 = 1.0 - nratio*nratio*(1.0 - cos1*cos1); /* compute cos theta2 */ @@ -115,66 +154,86 @@ 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) { + trans *= nratio*nratio; /* solid angle ratio */ + if (rayorigin(&p, r, REFRACTED, trans) == 0) { + /* compute refracted ray */ d1 = nratio*cos1 - cos2; for (i = 0; i < 3; i++) p.rdir[i] = nratio*r->rdir[i] + d1*dnorm[i]; - + /* accidental reflection? */ + if (hastexture && + DOT(p.rdir,r->ron)*hastexture >= -FTINY) { + d1 *= (double)hastexture; + for (i = 0; i < 3; i++) /* ignore texture */ + p.rdir[i] = nratio*r->rdir[i] + + d1*r->ron[i]; + normalize(p.rdir); /* not exact */ + } #ifdef DISPERSE 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; } } } if (!(r->crtype & SHADOW) && - rayorigin(&p, r, REFLECTED, mabsorp*refl) == 0) { + rayorigin(&p, r, REFLECTED, refl) == 0) { /* compute reflected ray */ for (i = 0; i < 3; i++) p.rdir[i] = r->rdir[i] + 2.0*cos1*dnorm[i]; + /* accidental penetration? */ + if (hastexture && DOT(p.rdir,r->ron)*hastexture <= FTINY) + for (i = 0; i < 3; i++) /* ignore texture */ + p.rdir[i] = r->rdir[i] + 2.0*r->rod*r->ron[i]; rayvalue(&p); /* reflected ray value */ 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 */ -OBJREC *m; -RAY *r; -FVECT vt; -double tr; +static int +disperse( /* check light sources for dispersion */ + OBJREC *m, + RAY *r, + FVECT vt, + double tr, + COLOR cet, + COLOR abt +) { - double sqrt(); RAY sray, *entray; FVECT v1, v2, n1, n2; FVECT dv, v2Xdv; double v2Xdvv2Xdv; - int sn, success = 0; - double omega; + int success = 0; + SRCINDEX si; FVECT vtmp1, vtmp2; double dtmp1, dtmp2; int l1, l2; @@ -236,12 +295,11 @@ double tr; v2Xdvv2Xdv = DOT(v2Xdv, v2Xdv); /* check sources */ - for (sn = 0; sn < nsources; sn++) { + initsrcindex(&si); + while (srcray(&sray, r, &si)) { - if ((omega = srcray(&sray, r, sn)) == 0.0 || - DOT(sray.rdir, v2) < MINCOS) + if (DOT(sray.rdir, v2) < MINCOS) continue; /* bad source */ - /* adjust source ray */ dtmp1 = DOT(v2Xdv, sray.rdir) / v2Xdvv2Xdv; @@ -254,6 +312,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 */ @@ -266,7 +326,7 @@ double tr; */ fcross(vtmp1, v2Xdv, sray.rdir); - dtmp1 = sqrt(omega / v2Xdvv2Xdv / PI); + dtmp1 = sqrt(si.dom / v2Xdvv2Xdv / PI); /* compute first ray */ for (i = 0; i < 3; i++) @@ -297,9 +357,12 @@ double tr; static int -lambda(m, v2, dv, lr) /* compute lambda for material */ -register OBJREC *m; -FVECT v2, dv, lr; +lambda( /* compute lambda for material */ + register OBJREC *m, + FVECT v2, + FVECT dv, + FVECT lr +) { FVECT lrXdv, v2Xlr; double dtmp, denom;