--- ray/src/rt/dielectric.c 1996/07/11 15:14:05 2.12 +++ ray/src/rt/dielectric.c 2004/09/09 06:46:07 2.19 @@ -1,25 +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.19 2004/09/09 06:46:07 greg 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 disperse(); -static int lambda(); +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. @@ -50,13 +50,11 @@ static int lambda(); #define MINCOS 0.997 /* minimum dot product for dispersion */ -extern COLOR cextinction; /* global coefficient of extinction */ -extern COLOR salbedo; /* global scattering albedo */ - static double -mylog(x) /* special log for extinction coefficients */ -double x; +mylog( /* special log for extinction coefficients */ + double x +) { if (x < 1e-40) return(-100.); @@ -66,14 +64,19 @@ double x; } -m_dielectric(m, r) /* color a ray which hit a dielectric interface */ -OBJREC *m; -register RAY *r; +extern int +m_dielectric( /* color a ray which hit a dielectric interface */ + OBJREC *m, + register RAY *r +) { double cos1, cos2, nratio; COLOR ctrans; COLOR talb; - double mabsorp; + int hastexture; + double transdist, transtest=0; + double mirdist, mirtest=0; + int flatsurface; double refl, trans; FVECT dnorm; double d1, d2; @@ -85,7 +88,14 @@ register RAY *r; 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; + } + flatsurface = !hastexture && r->ro != NULL && isflat(r->ro->otype); + /* index of refraction */ if (m->otype == MAT_DIELECTRIC) nratio = m->oargs.farg[3] + m->oargs.farg[4]/MLAMBDA; @@ -93,6 +103,7 @@ 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]; @@ -128,14 +139,6 @@ register RAY *r; r->gecc = 0.; } } - /* 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 */ @@ -159,13 +162,23 @@ register RAY *r; 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 @@ -181,24 +194,44 @@ register RAY *r; rayvalue(&p); scalecolor(p.rcol, trans); addcolor(r->rcol, p.rcol); - if (nratio >= 1.0-FTINY && nratio <= 1.0+FTINY) - r->rt = r->rot + p.rt; + /* virtual distance */ + if (flatsurface || + (1.-FTINY <= nratio && + nratio <= 1.+FTINY)) { + transtest = 2*bright(p.rcol); + transdist = 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); + /* virtual distance */ + if (flatsurface) { + mirtest = 2*bright(p.rcol); + mirdist = r->rot + p.rt; + } } + /* check distance to return */ + d1 = bright(r->rcol); + if (transtest > d1) + r->rt = transdist; + else if (mirtest > d1) + r->rt = mirdist; /* rayvalue() computes absorption */ return(1); } @@ -206,13 +239,15 @@ register RAY *r; #ifdef DISPERSE -static -disperse(m, r, vt, tr, cet, abt) /* check light sources for dispersion */ -OBJREC *m; -RAY *r; -FVECT vt; -double tr; -COLOR cet, abt; +static int +disperse( /* check light sources for dispersion */ + OBJREC *m, + RAY *r, + FVECT vt, + double tr, + COLOR cet, + COLOR abt +) { RAY sray, *entray; FVECT v1, v2, n1, n2; @@ -343,9 +378,12 @@ COLOR cet, abt; 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;