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

Comparing ray/src/rt/dielectric.c (file contents):
Revision 1.6 by greg, Mon Aug 5 13:52:20 1991 UTC vs.
Revision 2.18 by schorsch, Tue Mar 30 16:13:01 2004 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   *  dielectric.c - shading function for transparent materials.
9 *
10 *     9/6/85
6   */
7  
8 < #include  "ray.h"
8 > #include "copyright.h"
9  
10 + #include  "ray.h"
11   #include  "otypes.h"
12 + #include  "rtotypes.h"
13  
14   #ifdef  DISPERSE
15   #include  "source.h"
16 + static int disperse(OBJREC *m,RAY *r,FVECT vt,double tr,COLOR cet,COLOR abt);
17 + static int lambda(OBJREC  *m, FVECT  v2, FVECT  dv, FVECT  lr);
18   #endif
19  
20 + static double mylog(double  x);
21 +
22 +
23   /*
24   *     Explicit calculations for Fresnel's equation are performed,
25   *  but only one square root computation is necessary.
# Line 49 | Line 51 | static char SCCSid[] = "$SunId$ LBL";
51   #define  MINCOS         0.997           /* minimum dot product for dispersion */
52  
53  
54 < m_dielectric(m, r)      /* color a ray which hit something transparent */
55 < OBJREC  *m;
56 < register RAY  *r;
54 > static double
55 > mylog(          /* special log for extinction coefficients */
56 >        double  x
57 > )
58   {
59 <        double  sqrt(), pow();
59 >        if (x < 1e-40)
60 >                return(-100.);
61 >        if (x >= 1.)
62 >                return(0.);
63 >        return(log(x));
64 > }
65 >
66 >
67 > extern int
68 > m_dielectric(   /* color a ray which hit a dielectric interface */
69 >        OBJREC  *m,
70 >        register RAY  *r
71 > )
72 > {
73          double  cos1, cos2, nratio;
74 <        COLOR  mcolor;
75 <        double  mabsorp;
74 >        COLOR  ctrans;
75 >        COLOR  talb;
76 >        int  hastexture;
77          double  refl, trans;
78          FVECT  dnorm;
79          double  d1, d2;
# Line 66 | Line 83 | register RAY  *r;
83          if (m->oargs.nfargs != (m->otype==MAT_DIELECTRIC ? 5 : 8))
84                  objerror(m, USER, "bad arguments");
85  
69        r->rt = r->rot;                         /* just use ray length */
70
86          raytexture(r, m->omod);                 /* get modifiers */
87  
88 <        cos1 = raynormal(dnorm, r);             /* cosine of theta1 */
88 >        if ( (hastexture = DOT(r->pert,r->pert) > FTINY*FTINY) )
89 >                cos1 = raynormal(dnorm, r);     /* perturb normal */
90 >        else {
91 >                VCOPY(dnorm, r->ron);
92 >                cos1 = r->rod;
93 >        }
94                                                  /* index of refraction */
95          if (m->otype == MAT_DIELECTRIC)
96                  nratio = m->oargs.farg[3] + m->oargs.farg[4]/MLAMBDA;
# Line 78 | Line 98 | register RAY  *r;
98                  nratio = m->oargs.farg[3] / m->oargs.farg[7];
99          
100          if (cos1 < 0.0) {                       /* inside */
101 +                hastexture = -hastexture;
102                  cos1 = -cos1;
103                  dnorm[0] = -dnorm[0];
104                  dnorm[1] = -dnorm[1];
105                  dnorm[2] = -dnorm[2];
106 <                setcolor(mcolor, pow(m->oargs.farg[0], r->rot),
107 <                                 pow(m->oargs.farg[1], r->rot),
108 <                                 pow(m->oargs.farg[2], r->rot));
106 >                setcolor(r->cext, -mylog(m->oargs.farg[0]*colval(r->pcol,RED)),
107 >                                 -mylog(m->oargs.farg[1]*colval(r->pcol,GRN)),
108 >                                 -mylog(m->oargs.farg[2]*colval(r->pcol,BLU)));
109 >                setcolor(r->albedo, 0., 0., 0.);
110 >                r->gecc = 0.;
111 >                if (m->otype == MAT_INTERFACE) {
112 >                        setcolor(ctrans,
113 >                                -mylog(m->oargs.farg[4]*colval(r->pcol,RED)),
114 >                                -mylog(m->oargs.farg[5]*colval(r->pcol,GRN)),
115 >                                -mylog(m->oargs.farg[6]*colval(r->pcol,BLU)));
116 >                        setcolor(talb, 0., 0., 0.);
117 >                } else {
118 >                        copycolor(ctrans, cextinction);
119 >                        copycolor(talb, salbedo);
120 >                }
121          } else {                                /* outside */
122                  nratio = 1.0 / nratio;
123 <                if (m->otype == MAT_INTERFACE)
124 <                        setcolor(mcolor, pow(m->oargs.farg[4], r->rot),
125 <                                         pow(m->oargs.farg[5], r->rot),
126 <                                         pow(m->oargs.farg[6], r->rot));
127 <                else
128 <                        setcolor(mcolor, 1.0, 1.0, 1.0);
123 >
124 >                setcolor(ctrans, -mylog(m->oargs.farg[0]*colval(r->pcol,RED)),
125 >                                 -mylog(m->oargs.farg[1]*colval(r->pcol,GRN)),
126 >                                 -mylog(m->oargs.farg[2]*colval(r->pcol,BLU)));
127 >                setcolor(talb, 0., 0., 0.);
128 >                if (m->otype == MAT_INTERFACE) {
129 >                        setcolor(r->cext,
130 >                                -mylog(m->oargs.farg[4]*colval(r->pcol,RED)),
131 >                                -mylog(m->oargs.farg[5]*colval(r->pcol,GRN)),
132 >                                -mylog(m->oargs.farg[6]*colval(r->pcol,BLU)));
133 >                        setcolor(r->albedo, 0., 0., 0.);
134 >                        r->gecc = 0.;
135 >                }
136          }
97        mabsorp = bright(mcolor);
137  
138          d2 = 1.0 - nratio*nratio*(1.0 - cos1*cos1);     /* compute cos theta2 */
139  
# Line 115 | Line 154 | register RAY  *r;
154                  d1 = (d1 - d2) / (d1 + d2);
155                  refl += d1 * d1;
156  
157 <                refl /= 2.0;
157 >                refl *= 0.5;
158                  trans = 1.0 - refl;
159  
160 <                if (rayorigin(&p, r, REFRACTED, mabsorp*trans) == 0) {
160 >                trans *= nratio*nratio;         /* solid angle ratio */
161  
162 +                if (rayorigin(&p, r, REFRACTED, trans) == 0) {
163 +
164                                                  /* compute refracted ray */
165                          d1 = nratio*cos1 - cos2;
166                          for (i = 0; i < 3; i++)
167                                  p.rdir[i] = nratio*r->rdir[i] + d1*dnorm[i];
168 <
168 >                                                /* accidental reflection? */
169 >                        if (hastexture &&
170 >                                DOT(p.rdir,r->ron)*hastexture >= -FTINY) {
171 >                                d1 *= (double)hastexture;
172 >                                for (i = 0; i < 3; i++) /* ignore texture */
173 >                                        p.rdir[i] = nratio*r->rdir[i] +
174 >                                                        d1*r->ron[i];
175 >                                normalize(p.rdir);      /* not exact */
176 >                        }
177   #ifdef  DISPERSE
178                          if (m->otype != MAT_DIELECTRIC
179                                          || r->rod > 0.0
180                                          || r->crtype & SHADOW
181 <                                        || directinvis
181 >                                        || !directvis
182                                          || m->oargs.farg[4] == 0.0
183 <                                        || !disperse(m, r, p.rdir, trans))
183 >                                        || !disperse(m, r, p.rdir,
184 >                                                        trans, ctrans, talb))
185   #endif
186                          {
187 +                                copycolor(p.cext, ctrans);
188 +                                copycolor(p.albedo, talb);
189                                  rayvalue(&p);
138                                multcolor(mcolor, r->pcol);     /* modify */
190                                  scalecolor(p.rcol, trans);
191                                  addcolor(r->rcol, p.rcol);
192 +                                if (nratio >= 1.0-FTINY && nratio <= 1.0+FTINY)
193 +                                        r->rt = r->rot + p.rt;
194                          }
195                  }
196          }
197                  
198          if (!(r->crtype & SHADOW) &&
199 <                        rayorigin(&p, r, REFLECTED, mabsorp*refl) == 0) {
199 >                        rayorigin(&p, r, REFLECTED, refl) == 0) {
200  
201                                          /* compute reflected ray */
202                  for (i = 0; i < 3; i++)
203                          p.rdir[i] = r->rdir[i] + 2.0*cos1*dnorm[i];
204 +                                        /* accidental penetration? */
205 +                if (hastexture && DOT(p.rdir,r->ron)*hastexture <= FTINY)
206 +                        for (i = 0; i < 3; i++)         /* ignore texture */
207 +                                p.rdir[i] = r->rdir[i] + 2.0*r->rod*r->ron[i];
208  
209                  rayvalue(&p);                   /* reflected ray value */
210  
211                  scalecolor(p.rcol, refl);       /* color contribution */
212                  addcolor(r->rcol, p.rcol);
213          }
214 <
215 <        multcolor(r->rcol, mcolor);             /* multiply by transmittance */
214 >                                /* rayvalue() computes absorption */
215 >        return(1);
216   }
217  
218  
219   #ifdef  DISPERSE
220  
221 < static
222 < disperse(m, r, vt, tr)          /* check light sources for dispersion */
223 < OBJREC  *m;
224 < RAY  *r;
225 < FVECT  vt;
226 < double  tr;
221 > static int
222 > disperse(  /* check light sources for dispersion */
223 >        OBJREC  *m,
224 >        RAY  *r,
225 >        FVECT  vt,
226 >        double  tr,
227 >        COLOR  cet,
228 >        COLOR  abt
229 > )
230   {
171        double  sqrt();
231          RAY  sray, *entray;
232          FVECT  v1, v2, n1, n2;
233          FVECT  dv, v2Xdv;
234          double  v2Xdvv2Xdv;
235 <        int  sn, success = 0;
236 <        double  omega;
235 >        int  success = 0;
236 >        SRCINDEX  si;
237          FVECT  vtmp1, vtmp2;
238          double  dtmp1, dtmp2;
239          int  l1, l2;
# Line 236 | Line 295 | double  tr;
295          v2Xdvv2Xdv = DOT(v2Xdv, v2Xdv);
296  
297                                          /* check sources */
298 <        for (sn = 0; sn < nsources; sn++) {
298 >        initsrcindex(&si);
299 >        while (srcray(&sray, r, &si)) {
300  
301 <                if ((omega = srcray(&sray, r, sn)) == 0.0 ||
242 <                                DOT(sray.rdir, v2) < MINCOS)
301 >                if (DOT(sray.rdir, v2) < MINCOS)
302                          continue;                       /* bad source */
244                
303                                                  /* adjust source ray */
304  
305                  dtmp1 = DOT(v2Xdv, sray.rdir) / v2Xdvv2Xdv;
# Line 254 | Line 312 | double  tr;
312                  if (l1 > MAXLAMBDA || l1 < MINLAMBDA)   /* not visible */
313                          continue;
314                                                  /* trace source ray */
315 +                copycolor(sray.cext, cet);
316 +                copycolor(sray.albedo, abt);
317                  normalize(sray.rdir);
318                  rayvalue(&sray);
319                  if (bright(sray.rcol) <= FTINY) /* missed it */
# Line 266 | Line 326 | double  tr;
326                   */
327                  
328                  fcross(vtmp1, v2Xdv, sray.rdir);
329 <                dtmp1 = sqrt(omega  / v2Xdvv2Xdv / PI);
329 >                dtmp1 = sqrt(si.dom  / v2Xdvv2Xdv / PI);
330  
331                                                          /* compute first ray */
332                  for (i = 0; i < 3; i++)
# Line 297 | Line 357 | double  tr;
357  
358  
359   static int
360 < lambda(m, v2, dv, lr)                   /* compute lambda for material */
361 < register OBJREC  *m;
362 < FVECT  v2, dv, lr;
360 > lambda(                 /* compute lambda for material */
361 >        register OBJREC  *m,
362 >        FVECT  v2,
363 >        FVECT  dv,
364 >        FVECT  lr
365 > )
366   {
367          FVECT  lrXdv, v2Xlr;
368          double  dtmp, denom;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines