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

Comparing ray/src/rt/normal.c (file contents):
Revision 2.1 by greg, Tue Nov 12 17:09:11 1991 UTC vs.
Revision 2.2 by greg, Sat Jan 4 19:53:53 1992 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 Regents of the University of California */
1 > /* Copyright (c) 1992 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 11 | Line 11 | static char SCCSid[] = "$SunId$ LBL";
11   *     12/19/85 - added stuff for metals.
12   *     6/26/87 - improved specular model.
13   *     9/28/87 - added model for translucent materials.
14 + *     Later changes described in delta comments.
15   */
16  
17   #include  "ray.h"
18  
19   #include  "otypes.h"
20  
21 + #include  "random.h"
22 +
23   /*
24   *      This routine uses portions of the reflection
25   *  model described by Cook and Torrance.
# Line 34 | Line 37 | static char SCCSid[] = "$SunId$ LBL";
37  
38   #define  BSPEC(m)       (6.0)           /* specularity parameter b */
39  
40 < extern double  exp();
40 >                                /* specularity flags */
41 > #define  SP_REFL        01              /* has reflected specular component */
42 > #define  SP_TRAN        02              /* has transmitted specular */
43 > #define  SP_PURE        010             /* purely specular (zero roughness) */
44  
45   typedef struct {
46          OBJREC  *mp;            /* material pointer */
47 +        short  specfl;          /* specularity flags, defined above */
48          COLOR  mcolor;          /* color of this material */
49          COLOR  scolor;          /* color of specular component */
50          FVECT  vrefl;           /* vector in direction of reflected ray */
51          FVECT  prdir;           /* vector in transmitted direction */
52 <        double  alpha2;         /* roughness squared times 2 */
52 >        double  alpha2;         /* roughness squared */
53          double  rdiff, rspec;   /* reflected specular, diffuse */
54          double  trans;          /* transmissivity */
55          double  tdiff, tspec;   /* transmitted specular, diffuse */
# Line 79 | Line 86 | double  omega;                 /* light source size */
86                  scalecolor(ctmp, dtmp);
87                  addcolor(cval, ctmp);
88          }
89 <        if (ldot > FTINY && np->rspec > FTINY && np->alpha2 > FTINY) {
89 >        if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) {
90                  /*
91                   *  Compute specular reflection coefficient using
92                   *  gaussian distribution model.
93                   */
94                                                  /* roughness + source */
95 <                dtmp = np->alpha2 + omega/(2.0*PI);
95 >                dtmp = 2.0*np->alpha2 + omega/(2.0*PI);
96                                                  /* gaussian */
97                  dtmp = exp((DOT(np->vrefl,ldir)-1.)/dtmp)/(2.*PI)/dtmp;
98                                                  /* worth using? */
# Line 105 | Line 112 | double  omega;                 /* light source size */
112                  scalecolor(ctmp, dtmp);
113                  addcolor(cval, ctmp);
114          }
115 <        if (ldot < -FTINY && np->tspec > FTINY && np->alpha2 > FTINY) {
115 >        if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) {
116                  /*
117                   *  Compute specular transmission.  Specular transmission
118                   *  is always modified by material color.
# Line 125 | Line 132 | double  omega;                 /* light source size */
132   }
133  
134  
135 < m_normal(m, r)                  /* color a ray which hit something normal */
135 > m_normal(m, r)                  /* color a ray that hit something normal */
136   register OBJREC  *m;
137   register RAY  *r;
138   {
# Line 134 | Line 141 | register RAY  *r;
141          double  dtmp;
142          COLOR  ctmp;
143          register int  i;
137
138        if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
139                objerror(m, USER, "bad # arguments");
144                                                  /* easy shadow test */
145          if (r->crtype & SHADOW && m->otype != MAT_TRANS)
146                  return;
147 +
148 +        if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
149 +                objerror(m, USER, "bad number of arguments");
150          nd.mp = m;
151                                                  /* get material color */
152          setcolor(nd.mcolor, m->oargs.farg[0],
153                             m->oargs.farg[1],
154                             m->oargs.farg[2]);
155                                                  /* get roughness */
156 +        nd.specfl = 0;
157          nd.alpha2 = m->oargs.farg[4];
158 <        nd.alpha2 *= 2.0 * nd.alpha2;
158 >        if ((nd.alpha2 *= nd.alpha2) <= FTINY)
159 >                nd.specfl |= SP_PURE;
160                                                  /* reorient if necessary */
161          if (r->rod < 0.0)
162                  flipsurface(r);
# Line 159 | Line 168 | register RAY  *r;
168          multcolor(nd.mcolor, r->pcol);          /* modify material color */
169          transtest = 0;
170                                                  /* get specular component */
171 <        nd.rspec = m->oargs.farg[3];
172 <
164 <        if (nd.rspec > FTINY) {                 /* has specular component */
171 >        if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
172 >                nd.specfl |= SP_REFL;
173                                                  /* compute specular color */
174                  if (m->otype == MAT_METAL)
175                          copycolor(nd.scolor, nd.mcolor);
# Line 177 | Line 185 | register RAY  *r;
185                  for (i = 0; i < 3; i++)
186                          nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
187  
188 <                if (nd.alpha2 <= FTINY && !(r->crtype & SHADOW)) {
188 >                if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
189                          RAY  lr;
190                          if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
191                                  VCOPY(lr.rdir, nd.vrefl);
# Line 192 | Line 200 | register RAY  *r;
200                  nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec);
201                  nd.tspec = nd.trans * m->oargs.farg[6];
202                  nd.tdiff = nd.trans - nd.tspec;
203 <                if (r->crtype & SHADOW || DOT(r->pert,r->pert) <= FTINY*FTINY) {
204 <                        VCOPY(nd.prdir, r->rdir);
205 <                        transtest = 2;
206 <                } else {
207 <                        for (i = 0; i < 3; i++)         /* perturb direction */
208 <                                nd.prdir[i] = r->rdir[i] - .75*r->pert[i];
209 <                        normalize(nd.prdir);
203 >                if (nd.tspec > FTINY) {
204 >                        nd.specfl |= SP_TRAN;
205 >                        if (r->crtype & SHADOW ||
206 >                                        DOT(r->pert,r->pert) <= FTINY*FTINY) {
207 >                                VCOPY(nd.prdir, r->rdir);
208 >                                transtest = 2;
209 >                        } else {
210 >                                for (i = 0; i < 3; i++)         /* perturb */
211 >                                        nd.prdir[i] = r->rdir[i] -
212 >                                                        .75*r->pert[i];
213 >                                normalize(nd.prdir);
214 >                        }
215                  }
216          } else
217                  nd.tdiff = nd.tspec = nd.trans = 0.0;
218                                                  /* transmitted ray */
219 <        if (nd.tspec > FTINY && nd.alpha2 <= FTINY) {
219 >        if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
220                  RAY  lr;
221                  if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
222                          VCOPY(lr.rdir, nd.prdir);
# Line 215 | Line 228 | register RAY  *r;
228                          transdist = r->rot + lr.rt;
229                  }
230          }
231 +
232          if (r->crtype & SHADOW)                 /* the rest is shadow */
233                  return;
234                                                  /* diffuse reflection */
235          nd.rdiff = 1.0 - nd.trans - nd.rspec;
236  
237 <        if (nd.rdiff <= FTINY && nd.tdiff <= FTINY && nd.alpha2 <= FTINY)
238 <                return;                         /* purely specular */
237 >        if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
238 >                return;                         /* 100% pure specular */
239  
240 +        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE))
241 +                gaussamp(r, &nd);
242 +
243          if (nd.rdiff > FTINY) {         /* ambient from this side */
244                  ambient(ctmp, r);
245 <                if (nd.alpha2 <= FTINY)
229 <                        scalecolor(ctmp, nd.rdiff);
230 <                else
231 <                        scalecolor(ctmp, 1.0-nd.trans);
245 >                scalecolor(ctmp, nd.rdiff);
246                  multcolor(ctmp, nd.mcolor);     /* modified by material color */
247                  addcolor(r->rcol, ctmp);        /* add to returned color */
248          }
249          if (nd.tdiff > FTINY) {         /* ambient from other side */
250                  flipsurface(r);
251                  ambient(ctmp, r);
252 <                if (nd.alpha2 <= FTINY)
239 <                        scalecolor(ctmp, nd.tdiff);
240 <                else
241 <                        scalecolor(ctmp, nd.trans);
252 >                scalecolor(ctmp, nd.tdiff);
253                  multcolor(ctmp, nd.mcolor);     /* modified by color */
254                  addcolor(r->rcol, ctmp);
255                  flipsurface(r);
# Line 248 | Line 259 | register RAY  *r;
259                                          /* check distance */
260          if (transtest > bright(r->rcol))
261                  r->rt = transdist;
262 + }
263 +
264 +
265 + static
266 + gaussamp(r, np)                 /* sample gaussian specular */
267 + RAY  *r;
268 + register NORMDAT  *np;
269 + {
270 +        RAY  sr;
271 +        FVECT  u, v, h;
272 +        double  rv[2];
273 +        double  d, sinp, cosp;
274 +        int  confuse;
275 +        register int  i;
276 +                                        /* set up sample coordinates */
277 +        v[0] = v[1] = v[2] = 0.0;
278 +        for (i = 0; i < 3; i++)
279 +                if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6)
280 +                        break;
281 +        v[i] = 1.0;
282 +        fcross(u, v, np->pnorm);
283 +        normalize(u);
284 +        fcross(v, np->pnorm, u);
285 +                                        /* compute reflection */
286 +        if (np->specfl & SP_REFL &&
287 +                        rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
288 +                confuse = 0;
289 +                dimlist[ndims++] = (int)np->mp;
290 +        refagain:
291 +                dimlist[ndims] = confuse += 3601;
292 +                d = urand(ilhash(dimlist,ndims+1)+samplendx);
293 +                multisamp(rv, 2, d);
294 +                d = 2.0*PI * rv[0];
295 +                cosp = cos(d);
296 +                sinp = sin(d);
297 +                if (rv[1] <= FTINY)
298 +                        d = 1.0;
299 +                else
300 +                        d = sqrt( np->alpha2 * -log(rv[1]) );
301 +                for (i = 0; i < 3; i++)
302 +                        h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
303 +                d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
304 +                for (i = 0; i < 3; i++)
305 +                        sr.rdir[i] = r->rdir[i] + d*h[i];
306 +                if (DOT(sr.rdir, r->ron) <= FTINY)      /* oops! */
307 +                        goto refagain;
308 +                rayvalue(&sr);
309 +                multcolor(sr.rcol, np->scolor);
310 +                addcolor(r->rcol, sr.rcol);
311 +                ndims--;
312 +        }
313 +                                        /* compute transmission */
314   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines