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 2.9 by greg, Fri Dec 8 18:22:07 1995 UTC vs.
Revision 2.25 by greg, Tue Feb 24 19:39:26 2015 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 + #include  "pmapmat.h"
14  
15   #ifdef  DISPERSE
16   #include  "source.h"
17 < static  disperse();
18 < static int  lambda();
17 > static int disperse(OBJREC *m,RAY *r,FVECT vt,double tr,COLOR cet,COLOR abt);
18 > static int lambda(OBJREC  *m, FVECT  v2, FVECT  dv, FVECT  lr);
19   #endif
20  
21 + static double mylog(double  x);
22 +
23 +
24   /*
25   *     Explicit calculations for Fresnel's equation are performed,
26   *  but only one square root computation is necessary.
# Line 50 | Line 51 | static int  lambda();
51  
52   #define  MINCOS         0.997           /* minimum dot product for dispersion */
53  
54 < extern COLOR  cextinction;              /* global coefficient of extinction */
55 < extern double  salbedo;                 /* global scattering albedo */
54 > static
55 > double
56 > mylog(          /* special log for extinction coefficients */
57 >        double  x
58 > )
59 > {
60 >        if (x < 1e-40)
61 >                return(-100.);
62 >        if (x >= 1.)
63 >                return(0.);
64 >        return(log(x));
65 > }
66  
67  
68 < m_dielectric(m, r)      /* color a ray which hit a dielectric interface */
69 < OBJREC  *m;
70 < register RAY  *r;
68 > int
69 > m_dielectric(   /* color a ray which hit a dielectric interface */
70 >        OBJREC  *m,
71 >        RAY  *r
72 > )
73   {
74          double  cos1, cos2, nratio;
75          COLOR  ctrans;
76 <        double  talb;
77 <        double  mabsorp;
76 >        COLOR  talb;
77 >        int  hastexture;
78 >        double  transdist=0, transtest=0;
79 >        double  mirdist=0, mirtest=0;
80 >        int     flatsurface;
81          double  refl, trans;
82          FVECT  dnorm;
83          double  d1, d2;
84          RAY  p;
85 <        register int  i;
85 >        int  i;
86  
87 +        /* PMAP: skip refracted shadow ray if accounted for by photon map */
88 +        if (shadowRayInPmap(r))
89 +                return(1);
90 +        
91          if (m->oargs.nfargs != (m->otype==MAT_DIELECTRIC ? 5 : 8))
92                  objerror(m, USER, "bad arguments");
93  
94          raytexture(r, m->omod);                 /* get modifiers */
95  
96 <        cos1 = raynormal(dnorm, r);             /* cosine of theta1 */
96 >        if ( (hastexture = DOT(r->pert,r->pert) > FTINY*FTINY) )
97 >                cos1 = raynormal(dnorm, r);     /* perturb normal */
98 >        else {
99 >                VCOPY(dnorm, r->ron);
100 >                cos1 = r->rod;
101 >        }
102 >        flatsurface = r->ro != NULL && isflat(r->ro->otype) &&
103 >                        !hastexture | (r->crtype & AMBIENT);
104 >
105                                                  /* index of refraction */
106          if (m->otype == MAT_DIELECTRIC)
107                  nratio = m->oargs.farg[3] + m->oargs.farg[4]/MLAMBDA;
# Line 81 | Line 109 | register RAY  *r;
109                  nratio = m->oargs.farg[3] / m->oargs.farg[7];
110          
111          if (cos1 < 0.0) {                       /* inside */
112 +                hastexture = -hastexture;
113                  cos1 = -cos1;
114                  dnorm[0] = -dnorm[0];
115                  dnorm[1] = -dnorm[1];
116                  dnorm[2] = -dnorm[2];
117 <                setcolor(r->cext, -log(m->oargs.farg[0]*colval(r->pcol,RED)),
118 <                                 -log(m->oargs.farg[1]*colval(r->pcol,GRN)),
119 <                                 -log(m->oargs.farg[2]*colval(r->pcol,BLU)));
120 <                r->albedo = 0.;
117 >                setcolor(r->cext, -mylog(m->oargs.farg[0]*colval(r->pcol,RED)),
118 >                                 -mylog(m->oargs.farg[1]*colval(r->pcol,GRN)),
119 >                                 -mylog(m->oargs.farg[2]*colval(r->pcol,BLU)));
120 >                setcolor(r->albedo, 0., 0., 0.);
121                  r->gecc = 0.;
122                  if (m->otype == MAT_INTERFACE) {
123                          setcolor(ctrans,
124 <                                -log(m->oargs.farg[4]*colval(r->pcol,RED)),
125 <                                -log(m->oargs.farg[5]*colval(r->pcol,GRN)),
126 <                                -log(m->oargs.farg[6]*colval(r->pcol,BLU)));
127 <                        talb = 0.;
124 >                                -mylog(m->oargs.farg[4]*colval(r->pcol,RED)),
125 >                                -mylog(m->oargs.farg[5]*colval(r->pcol,GRN)),
126 >                                -mylog(m->oargs.farg[6]*colval(r->pcol,BLU)));
127 >                        setcolor(talb, 0., 0., 0.);
128                  } else {
129                          copycolor(ctrans, cextinction);
130 <                        talb = salbedo;
130 >                        copycolor(talb, salbedo);
131                  }
132          } else {                                /* outside */
133                  nratio = 1.0 / nratio;
134  
135 <                setcolor(ctrans, -log(m->oargs.farg[0]*colval(r->pcol,RED)),
136 <                                 -log(m->oargs.farg[1]*colval(r->pcol,GRN)),
137 <                                 -log(m->oargs.farg[2]*colval(r->pcol,BLU)));
138 <                talb = 0.;
135 >                setcolor(ctrans, -mylog(m->oargs.farg[0]*colval(r->pcol,RED)),
136 >                                 -mylog(m->oargs.farg[1]*colval(r->pcol,GRN)),
137 >                                 -mylog(m->oargs.farg[2]*colval(r->pcol,BLU)));
138 >                setcolor(talb, 0., 0., 0.);
139                  if (m->otype == MAT_INTERFACE) {
140                          setcolor(r->cext,
141 <                                -log(m->oargs.farg[4]*colval(r->pcol,RED)),
142 <                                -log(m->oargs.farg[5]*colval(r->pcol,GRN)),
143 <                                -log(m->oargs.farg[6]*colval(r->pcol,BLU)));
144 <                        r->albedo = 0.;
141 >                                -mylog(m->oargs.farg[4]*colval(r->pcol,RED)),
142 >                                -mylog(m->oargs.farg[5]*colval(r->pcol,GRN)),
143 >                                -mylog(m->oargs.farg[6]*colval(r->pcol,BLU)));
144 >                        setcolor(r->albedo, 0., 0., 0.);
145                          r->gecc = 0.;
146                  }
147          }
119        mabsorp = exp(-bright(r->cext)*r->rot);         /* approximate */
148  
149          d2 = 1.0 - nratio*nratio*(1.0 - cos1*cos1);     /* compute cos theta2 */
150  
# Line 140 | Line 168 | register RAY  *r;
168                  refl *= 0.5;
169                  trans = 1.0 - refl;
170  
171 <                if (rayorigin(&p, r, REFRACTED, mabsorp*trans) == 0) {
171 >                trans *= nratio*nratio;         /* solid angle ratio */
172  
173 +                setcolor(p.rcoef, trans, trans, trans);
174 +
175 +                if (rayorigin(&p, REFRACTED, r, p.rcoef) == 0) {
176 +
177                                                  /* compute refracted ray */
178                          d1 = nratio*cos1 - cos2;
179                          for (i = 0; i < 3; i++)
180                                  p.rdir[i] = nratio*r->rdir[i] + d1*dnorm[i];
181 <
181 >                                                /* accidental reflection? */
182 >                        if (hastexture &&
183 >                                DOT(p.rdir,r->ron)*hastexture >= -FTINY) {
184 >                                d1 *= (double)hastexture;
185 >                                for (i = 0; i < 3; i++) /* ignore texture */
186 >                                        p.rdir[i] = nratio*r->rdir[i] +
187 >                                                        d1*r->ron[i];
188 >                                normalize(p.rdir);      /* not exact */
189 >                        } else
190 >                                checknorm(p.rdir);
191   #ifdef  DISPERSE
192                          if (m->otype != MAT_DIELECTRIC
193                                          || r->rod > 0.0
194                                          || r->crtype & SHADOW
195                                          || !directvis
196                                          || m->oargs.farg[4] == 0.0
197 <                                        || !disperse(m, r, p.rdir, trans))
197 >                                        || !disperse(m, r, p.rdir,
198 >                                                        trans, ctrans, talb))
199   #endif
200                          {
201                                  copycolor(p.cext, ctrans);
202 <                                p.albedo = talb;
202 >                                copycolor(p.albedo, talb);
203                                  rayvalue(&p);
204 <                                scalecolor(p.rcol, trans);
204 >                                multcolor(p.rcol, p.rcoef);
205                                  addcolor(r->rcol, p.rcol);
206 <                                if (nratio >= 1.0-FTINY && nratio <= 1.0+FTINY)
207 <                                        r->rt = r->rot + p.rt;
206 >                                                /* virtual distance */
207 >                                if (flatsurface ||
208 >                                        (1.-FTINY <= nratio) &
209 >                                                (nratio <= 1.+FTINY)) {
210 >                                        transtest = 2*bright(p.rcol);
211 >                                        transdist = r->rot + p.rt;
212 >                                }
213                          }
214                  }
215          }
216 <                
216 >        setcolor(p.rcoef, refl, refl, refl);
217 >
218          if (!(r->crtype & SHADOW) &&
219 <                        rayorigin(&p, r, REFLECTED, mabsorp*refl) == 0) {
219 >                        rayorigin(&p, REFLECTED, r, p.rcoef) == 0) {
220  
221                                          /* compute reflected ray */
222 <                for (i = 0; i < 3; i++)
223 <                        p.rdir[i] = r->rdir[i] + 2.0*cos1*dnorm[i];
224 <
222 >                VSUM(p.rdir, r->rdir, dnorm, 2.*cos1);
223 >                                        /* accidental penetration? */
224 >                if (hastexture && DOT(p.rdir,r->ron)*hastexture <= FTINY)
225 >                        VSUM(p.rdir, r->rdir, r->ron, 2.*r->rod);
226 >                checknorm(p.rdir);
227                  rayvalue(&p);                   /* reflected ray value */
228  
229 <                scalecolor(p.rcol, refl);       /* color contribution */
229 >                multcolor(p.rcol, p.rcoef);     /* color contribution */
230                  addcolor(r->rcol, p.rcol);
231 +                                                /* virtual distance */
232 +                if (flatsurface) {
233 +                        mirtest = 2*bright(p.rcol);
234 +                        mirdist = r->rot + p.rt;
235 +                }
236          }
237 +                                /* check distance to return */
238 +        d1 = bright(r->rcol);
239 +        if (transtest > d1)
240 +                r->rt = transdist;
241 +        else if (mirtest > d1)
242 +                r->rt = mirdist;
243                                  /* rayvalue() computes absorption */
244          return(1);
245   }
# Line 186 | Line 247 | register RAY  *r;
247  
248   #ifdef  DISPERSE
249  
250 < static
251 < disperse(m, r, vt, tr)          /* check light sources for dispersion */
252 < OBJREC  *m;
253 < RAY  *r;
254 < FVECT  vt;
255 < double  tr;
250 > static int
251 > disperse(  /* check light sources for dispersion */
252 >        OBJREC  *m,
253 >        RAY  *r,
254 >        FVECT  vt,
255 >        double  tr,
256 >        COLOR  cet,
257 >        COLOR  abt
258 > )
259   {
260 <        RAY  sray, *entray;
260 >        RAY  sray;
261 >        const RAY  *entray;
262          FVECT  v1, v2, n1, n2;
263          FVECT  dv, v2Xdv;
264          double  v2Xdvv2Xdv;
# Line 248 | Line 313 | double  tr;
313          VCOPY(n2, r->ron);
314  
315                                          /* first order dispersion approx. */
316 <        dtmp1 = DOT(n1, v1);
317 <        dtmp2 = DOT(n2, v2);
316 >        dtmp1 = 1./DOT(n1, v1);
317 >        dtmp2 = 1./DOT(n2, v2);
318          for (i = 0; i < 3; i++)
319 <                dv[i] = v1[i] + v2[i] - n1[i]/dtmp1 - n2[i]/dtmp2;
319 >                dv[i] = v1[i] + v2[i] - n1[i]*dtmp1 - n2[i]*dtmp2;
320                  
321          if (DOT(dv, dv) <= FTINY)       /* null effect */
322                  return(0);
# Line 277 | Line 342 | double  tr;
342                  if (l1 > MAXLAMBDA || l1 < MINLAMBDA)   /* not visible */
343                          continue;
344                                                  /* trace source ray */
345 +                copycolor(sray.cext, cet);
346 +                copycolor(sray.albedo, abt);
347                  normalize(sray.rdir);
348                  rayvalue(&sray);
349                  if (bright(sray.rcol) <= FTINY) /* missed it */
# Line 292 | Line 359 | double  tr;
359                  dtmp1 = sqrt(si.dom  / v2Xdvv2Xdv / PI);
360  
361                                                          /* compute first ray */
362 <                for (i = 0; i < 3; i++)
296 <                        vtmp2[i] = sray.rdir[i] + dtmp1*vtmp1[i];
362 >                VSUM(vtmp2, sray.rdir, vtmp1, dtmp1);
363  
364                  l1 = lambda(m, v2, dv, vtmp2);          /* first lambda */
365                  if (l1 < 0)
366                          continue;
367                                                          /* compute second ray */
368 <                for (i = 0; i < 3; i++)
303 <                        vtmp2[i] = sray.rdir[i] - dtmp1*vtmp1[i];
368 >                VSUM(vtmp2, sray.rdir, vtmp1, -dtmp1);
369  
370                  l2 = lambda(m, v2, dv, vtmp2);          /* second lambda */
371                  if (l2 < 0)
# Line 320 | Line 385 | double  tr;
385  
386  
387   static int
388 < lambda(m, v2, dv, lr)                   /* compute lambda for material */
389 < register OBJREC  *m;
390 < FVECT  v2, dv, lr;
388 > lambda(                 /* compute lambda for material */
389 >        OBJREC  *m,
390 >        FVECT  v2,
391 >        FVECT  dv,
392 >        FVECT  lr
393 > )
394   {
395          FVECT  lrXdv, v2Xlr;
396          double  dtmp, denom;
# Line 330 | Line 398 | FVECT  v2, dv, lr;
398  
399          fcross(lrXdv, lr, dv);
400          for (i = 0; i < 3; i++)
401 <                if (lrXdv[i] > FTINY || lrXdv[i] < -FTINY)
401 >                if ((lrXdv[i] > FTINY) | (lrXdv[i] < -FTINY))
402                          break;
403          if (i >= 3)
404                  return(-1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines