ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/glass.c
(Generate patch)

Comparing ray/src/rt/glass.c (file contents):
Revision 1.5 by greg, Mon Oct 15 20:39:32 1990 UTC vs.
Revision 2.27 by greg, Tue Nov 13 19:58:33 2018 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1986 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  glass.c - simpler shading function for thin glass surfaces.
9 *
10 *     11/14/86
6   */
7  
8 + #include "copyright.h"
9 +
10   #include  "ray.h"
11 + #include  "otypes.h"
12 + #include  "rtotypes.h"
13 + #include  "pmapmat.h"
14  
15   /*
16   *  This definition of glass provides for a quick calculation
# Line 24 | Line 24 | static char SCCSid[] = "$SunId$ LBL";
24   *      modifier glass id
25   *      0
26   *      0
27 < *      3 red grn blu
27 > *      3+ red grn blu [refractive_index]
28   *
29   *  The color is used for the transmission at normal incidence.
30 < *  To compute transmission (tn) from transmissivity (Tn) use:
30 > *  To compute transmissivity (tn) from transmittance (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.
34 > *  The transmissivity of standard 88% transmittance glass is 0.96.
35 > *  A refractive index other than the default can be used by giving
36 > *  it as the fourth real argument.  The above formula no longer applies.
37 > *
38   *  If we appear to hit the back side of the surface, then we
39   *  turn the normal around.
40   */
# Line 39 | Line 42 | static char SCCSid[] = "$SunId$ LBL";
42   #define  RINDEX         1.52            /* refractive index of glass */
43  
44  
45 < m_glass(m, r)           /* color a ray which hit a thin glass surface */
46 < OBJREC  *m;
47 < register RAY  *r;
45 > int
46 > m_glass(                /* color a ray which hit a thin glass surface */
47 >        OBJREC  *m,
48 >        RAY  *r
49 > )
50   {
46        double  sqrt(), pow();
51          COLOR  mcolor;
52          double  pdot;
53          FVECT  pnorm;
54 <        double  cos2;
54 >        double  rindex=0, cos2;
55          COLOR  trans, refl;
56 <        double  d, r1;
56 >        int  hastexture, hastrans;
57 >        double  d, r1e, r1m;
58          RAY  p;
59 <        register int  i;
59 >        int  i;
60  
61 <        if (m->oargs.nfargs != 3)
61 >        /* PMAP: skip refracted shadow or ambient ray if accounted for in
62 >           photon map */
63 >        if (shadowRayInPmap(r) || ambRayInPmap(r))
64 >                return(1);
65 >                                                /* check arguments */
66 >        if (m->oargs.nfargs == 3)
67 >                rindex = RINDEX;                /* default value of n */
68 >        else if (m->oargs.nfargs == 4)
69 >                rindex = m->oargs.farg[3];      /* use their value */
70 >        else
71                  objerror(m, USER, "bad arguments");
72 <
72 >                                                /* check back face visibility */
73 >        if (!backvis && r->rod <= 0.0) {
74 >                raytrans(r);
75 >                return(1);
76 >        }
77 >                                                /* check transmission */
78          setcolor(mcolor, m->oargs.farg[0], m->oargs.farg[1], m->oargs.farg[2]);
79 <
80 <        if (r->rod < 0.0)                       /* reorient if necessary */
81 <                flipsurface(r);
82 <        r->rt = r->rot;                         /* default ray length */
79 >        if ((hastrans = (intens(mcolor) > 1e-15))) {
80 >                for (i = 0; i < 3; i++)
81 >                        if (colval(mcolor,i) < 1e-15)
82 >                                colval(mcolor,i) = 1e-15;
83 >        } else if (r->crtype & SHADOW)
84 >                return(1);
85                                                  /* get modifiers */
86          raytexture(r, m->omod);
87 <        pdot = raynormal(pnorm, r);
87 >        if (r->rod < 0.0)                       /* reorient if necessary */
88 >                flipsurface(r);
89 >                                                /* perturb normal */
90 >        if ( (hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY)) ) {
91 >                pdot = raynormal(pnorm, r);
92 >        } else {
93 >                VCOPY(pnorm, r->ron);
94 >                pdot = r->rod;
95 >        }
96                                                  /* angular transmission */
97 <        cos2 = sqrt( (1.0-1.0/RINDEX/RINDEX) +
98 <                     pdot*pdot/(RINDEX*RINDEX) );
99 <        setcolor(mcolor, pow(colval(mcolor,RED), 1.0/cos2),
100 <                         pow(colval(mcolor,GRN), 1.0/cos2),
101 <                         pow(colval(mcolor,BLU), 1.0/cos2));
97 >        cos2 = sqrt( (1.0-1.0/(rindex*rindex)) +
98 >                     pdot*pdot/(rindex*rindex) );
99 >        if (hastrans)
100 >                setcolor(mcolor, pow(colval(mcolor,RED), 1.0/cos2),
101 >                                 pow(colval(mcolor,GRN), 1.0/cos2),
102 >                                 pow(colval(mcolor,BLU), 1.0/cos2));
103  
104                                                  /* compute reflection */
105 <        r1 = (pdot - RINDEX*cos2) / (pdot + RINDEX*cos2);
106 <        d = (1.0/pdot - RINDEX/cos2) / (1.0/pdot + RINDEX/cos2);
107 <        r1 = (r1*r1 + d*d) / 2.0;
108 <                                                /* compute transmittance */
109 <        for (i = 0; i < 3; i++) {
110 <                d = colval(mcolor, i);
111 <                colval(trans,i) = (1.0-r1)*(1.0-r1)*d / (1.0 - r1*r1*d*d);
112 <        }
105 >        r1e = (pdot - rindex*cos2) / (pdot + rindex*cos2);
106 >        r1e *= r1e;
107 >        r1m = (1.0/pdot - rindex/cos2) / (1.0/pdot + rindex/cos2);
108 >        r1m *= r1m;
109 >                                                /* compute transmission */
110 >        if (hastrans) {
111 >                for (i = 0; i < 3; i++) {
112 >                        d = colval(mcolor, i);
113 >                        colval(trans,i) = .5*(1.0-r1e)*(1.0-r1e)*d /
114 >                                                        (1.0-r1e*r1e*d*d);
115 >                        colval(trans,i) += .5*(1.0-r1m)*(1.0-r1m)*d /
116 >                                                        (1.0-r1m*r1m*d*d);
117 >                }
118 >                multcolor(trans, r->pcol);      /* modify by pattern */
119                                                  /* transmitted ray */
120 <        if (rayorigin(&p, r, TRANS, bright(trans)) == 0) {
121 <                VCOPY(p.rdir, r->rdir);
122 <                rayvalue(&p);
123 <                multcolor(p.rcol, r->pcol);     /* modify */
124 <                multcolor(p.rcol, trans);
125 <                addcolor(r->rcol, p.rcol);
126 <                if (bright(p.rcol) > .5)
127 <                        r->rt = r->rot + p.rt;
120 >                if (rayorigin(&p, TRANS, r, trans) == 0) {
121 >                        if (!(r->crtype & (SHADOW|AMBIENT)) && hastexture) {
122 >                                VSUM(p.rdir, r->rdir, r->pert, 2.*(1.-rindex));
123 >                                if (normalize(p.rdir) == 0.0) {
124 >                                        objerror(m, WARNING, "bad perturbation");
125 >                                        VCOPY(p.rdir, r->rdir);
126 >                                }
127 >                        } else {
128 >                                VCOPY(p.rdir, r->rdir);
129 >                        }
130 >                        rayvalue(&p);
131 >                        multcolor(p.rcol, p.rcoef);
132 >                        addcolor(r->rcol, p.rcol);
133 >                        if (!hastexture || r->crtype & (SHADOW|AMBIENT))
134 >                                r->rxt = r->rot + raydistance(&p);
135 >                }
136          }
93
137          if (r->crtype & SHADOW)                 /* skip reflected ray */
138 <                return;
138 >                return(1);
139                                                  /* compute reflectance */
140          for (i = 0; i < 3; i++) {
141                  d = colval(mcolor, i);
142                  d *= d;
143 <                colval(refl,i) = r1 * (1.0 + (1.0-2.0*r1)*d) / (1.0 - r1*r1*d);
143 >                colval(refl,i) = .5*r1e*(1.0+(1.0-2.0*r1e)*d)/(1.0-r1e*r1e*d);
144 >                colval(refl,i) += .5*r1m*(1.0+(1.0-2.0*r1m)*d)/(1.0-r1m*r1m*d);
145          }
146                                                  /* reflected ray */
147 <        if (rayorigin(&p, r, REFLECTED, bright(refl)) == 0) {
148 <                for (i = 0; i < 3; i++)
149 <                        p.rdir[i] = r->rdir[i] + 2.0*pdot*pnorm[i];
147 >        if (rayorigin(&p, REFLECTED, r, refl) == 0) {
148 >                VSUM(p.rdir, r->rdir, pnorm, 2.*pdot);
149 >                checknorm(p.rdir);
150                  rayvalue(&p);
151 <                multcolor(p.rcol, refl);
151 >                multcolor(p.rcol, p.rcoef);
152 >                copycolor(r->mcol, p.rcol);
153                  addcolor(r->rcol, p.rcol);
154 +                if (r->ro != NULL && isflat(r->ro->otype) &&
155 +                                !hastexture | (r->crtype & AMBIENT))
156 +                        r->rmt = r->rot + raydistance(&p);
157          }
158 +        return(1);
159   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines