Back to index

radiance  4R0+20100331
glass.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char RCSid[] = "$Id: glass.c,v 2.17 2005/06/21 15:06:50 greg Exp $";
00003 #endif
00004 /*
00005  *  glass.c - simpler shading function for thin glass surfaces.
00006  */
00007 
00008 #include "copyright.h"
00009 
00010 #include  "ray.h"
00011 #include  "otypes.h"
00012 #include  "rtotypes.h"
00013 
00014 /*
00015  *  This definition of glass provides for a quick calculation
00016  *  using a single surface where two closely spaced parallel
00017  *  dielectric surfaces would otherwise be used.  The chief
00018  *  advantage to using this material is speed, since internal
00019  *  reflections are avoided.
00020  *
00021  *  The specification for glass is as follows:
00022  *
00023  *     modifier glass id
00024  *     0
00025  *     0
00026  *     3+ red grn blu [refractive_index]
00027  *
00028  *  The color is used for the transmission at normal incidence.
00029  *  To compute transmissivity (tn) from transmittance (Tn) use:
00030  *
00031  *     tn = (sqrt(.8402528435+.0072522239*Tn*Tn)-.9166530661)/.0036261119/Tn
00032  *
00033  *  The transmissivity of standard 88% transmittance glass is 0.96.
00034  *  A refractive index other than the default can be used by giving
00035  *  it as the fourth real argument.  The above formula no longer applies.
00036  *
00037  *  If we appear to hit the back side of the surface, then we
00038  *  turn the normal around.
00039  */
00040 
00041 #define  RINDEX             1.52          /* refractive index of glass */
00042 
00043 
00044 extern int
00045 m_glass(             /* color a ray which hit a thin glass surface */
00046        OBJREC  *m,
00047        register RAY  *r
00048 )
00049 {
00050        COLOR  mcolor;
00051        double  pdot;
00052        FVECT  pnorm;
00053        double  rindex, cos2;
00054        COLOR  trans, refl;
00055        int  hastexture, hastrans;
00056        double  d, r1e, r1m;
00057        double  transtest, transdist;
00058        double  mirtest, mirdist;
00059        RAY  p;
00060        register int  i;
00061                                           /* check arguments */
00062        if (m->oargs.nfargs == 3)
00063               rindex = RINDEX;            /* default value of n */
00064        else if (m->oargs.nfargs == 4)
00065               rindex = m->oargs.farg[3];  /* use their value */
00066        else
00067               objerror(m, USER, "bad arguments");
00068                                           /* check transmission */
00069        setcolor(mcolor, m->oargs.farg[0], m->oargs.farg[1], m->oargs.farg[2]);
00070        if ((hastrans = (intens(mcolor) > 1e-15))) {
00071               for (i = 0; i < 3; i++)
00072                      if (colval(mcolor,i) < 1e-15)
00073                             colval(mcolor,i) = 1e-15;
00074        } else if (r->crtype & SHADOW)
00075               return(1);
00076                                           /* get modifiers */
00077        raytexture(r, m->omod);
00078        if (r->rod < 0.0)                  /* reorient if necessary */
00079               flipsurface(r);
00080        mirtest = transtest = 0;
00081        mirdist = transdist = r->rot;
00082                                           /* perturb normal */
00083        if ( (hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY)) ) {
00084               pdot = raynormal(pnorm, r);
00085        } else {
00086               VCOPY(pnorm, r->ron);
00087               pdot = r->rod;
00088        }
00089                                           /* angular transmission */
00090        cos2 = sqrt( (1.0-1.0/(rindex*rindex)) +
00091                    pdot*pdot/(rindex*rindex) );
00092        if (hastrans)
00093               setcolor(mcolor, pow(colval(mcolor,RED), 1.0/cos2),
00094                              pow(colval(mcolor,GRN), 1.0/cos2),
00095                              pow(colval(mcolor,BLU), 1.0/cos2));
00096 
00097                                           /* compute reflection */
00098        r1e = (pdot - rindex*cos2) / (pdot + rindex*cos2);
00099        r1e *= r1e;
00100        r1m = (1.0/pdot - rindex/cos2) / (1.0/pdot + rindex/cos2);
00101        r1m *= r1m;
00102                                           /* compute transmission */
00103        if (hastrans) {
00104               for (i = 0; i < 3; i++) {
00105                      d = colval(mcolor, i);
00106                      colval(trans,i) = .5*(1.0-r1e)*(1.0-r1e)*d /
00107                                                  (1.0-r1e*r1e*d*d);
00108                      colval(trans,i) += .5*(1.0-r1m)*(1.0-r1m)*d /
00109                                                  (1.0-r1m*r1m*d*d);
00110               }
00111               multcolor(trans, r->pcol);  /* modify by pattern */
00112                                           /* transmitted ray */
00113               if (rayorigin(&p, TRANS, r, trans) == 0) {
00114                      if (!(r->crtype & SHADOW) && hastexture) {
00115                             for (i = 0; i < 3; i++)     /* perturb direction */
00116                                    p.rdir[i] = r->rdir[i] +
00117                                           2.*(1.-rindex)*r->pert[i];
00118                             if (normalize(p.rdir) == 0.0) {
00119                                    objerror(m, WARNING, "bad perturbation");
00120                                    VCOPY(p.rdir, r->rdir);
00121                             }
00122                      } else {
00123                             VCOPY(p.rdir, r->rdir);
00124                             transtest = 2;
00125                      }
00126                      rayvalue(&p);
00127                      multcolor(p.rcol, p.rcoef);
00128                      addcolor(r->rcol, p.rcol);
00129                      transtest *= bright(p.rcol);
00130                      transdist = r->rot + p.rt;
00131               }
00132        }
00133        if (r->crtype & SHADOW) {          /* skip reflected ray */
00134               r->rt = transdist;
00135               return(1);
00136        }
00137                                           /* compute reflectance */
00138        for (i = 0; i < 3; i++) {
00139               d = colval(mcolor, i);
00140               d *= d;
00141               colval(refl,i) = .5*r1e*(1.0+(1.0-2.0*r1e)*d)/(1.0-r1e*r1e*d);
00142               colval(refl,i) += .5*r1m*(1.0+(1.0-2.0*r1m)*d)/(1.0-r1m*r1m*d);
00143        }
00144                                           /* reflected ray */
00145        if (rayorigin(&p, REFLECTED, r, refl) == 0) {
00146               for (i = 0; i < 3; i++)
00147                      p.rdir[i] = r->rdir[i] + 2.0*pdot*pnorm[i];
00148               rayvalue(&p);
00149               multcolor(p.rcol, p.rcoef);
00150               addcolor(r->rcol, p.rcol);
00151               if (!hastexture && r->ro != NULL && isflat(r->ro->otype)) {
00152                      mirtest = 2.0*bright(p.rcol);
00153                      mirdist = r->rot + p.rt;
00154               }
00155        }
00156                                         /* check distance */
00157        d = bright(r->rcol);
00158        if (transtest > d)
00159               r->rt = transdist;
00160        else if (mirtest > d)
00161               r->rt = mirdist;
00162        return(1);
00163 }