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.13 by greg, Thu Apr 16 13:31:28 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 + extern double  specthresh;              /* specular sampling threshold */
24 + extern double  specjitter;              /* specular sampling jitter */
25 +
26   /*
27   *      This routine uses portions of the reflection
28   *  model described by Cook and Torrance.
# Line 34 | Line 40 | static char SCCSid[] = "$SunId$ LBL";
40  
41   #define  BSPEC(m)       (6.0)           /* specularity parameter b */
42  
43 < extern double  exp();
43 >                                /* specularity flags */
44 > #define  SP_REFL        01              /* has reflected specular component */
45 > #define  SP_TRAN        02              /* has transmitted specular */
46 > #define  SP_PURE        04              /* purely specular (zero roughness) */
47 > #define  SP_FLAT        010             /* flat reflecting surface */
48 > #define  SP_RBLT        020             /* reflection below sample threshold */
49 > #define  SP_TBLT        040             /* transmission below threshold */
50  
51   typedef struct {
52          OBJREC  *mp;            /* material pointer */
53 +        short  specfl;          /* specularity flags, defined above */
54          COLOR  mcolor;          /* color of this material */
55          COLOR  scolor;          /* color of specular component */
56          FVECT  vrefl;           /* vector in direction of reflected ray */
57          FVECT  prdir;           /* vector in transmitted direction */
58 <        double  alpha2;         /* roughness squared times 2 */
58 >        double  alpha2;         /* roughness squared */
59          double  rdiff, rspec;   /* reflected specular, diffuse */
60          double  trans;          /* transmissivity */
61          double  tdiff, tspec;   /* transmitted specular, diffuse */
# Line 59 | Line 72 | double  omega;                 /* light source size */
72   {
73          double  ldot;
74          double  dtmp;
75 +        int     i;
76          COLOR  ctmp;
77  
78          setcolor(cval, 0.0, 0.0, 0.0);
# Line 79 | Line 93 | double  omega;                 /* light source size */
93                  scalecolor(ctmp, dtmp);
94                  addcolor(cval, ctmp);
95          }
96 <        if (ldot > FTINY && np->rspec > FTINY && np->alpha2 > FTINY) {
96 >        if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) {
97                  /*
98                   *  Compute specular reflection coefficient using
99                   *  gaussian distribution model.
100                   */
101 <                                                /* roughness + source */
102 <                dtmp = np->alpha2 + omega/(2.0*PI);
101 >                                                /* roughness */
102 >                dtmp = 2.0*np->alpha2;
103 >                                                /* + source if flat */
104 >                if (np->specfl & SP_FLAT)
105 >                        dtmp += omega/(2.0*PI);
106                                                  /* gaussian */
107                  dtmp = exp((DOT(np->vrefl,ldir)-1.)/dtmp)/(2.*PI)/dtmp;
108                                                  /* worth using? */
# Line 105 | Line 122 | double  omega;                 /* light source size */
122                  scalecolor(ctmp, dtmp);
123                  addcolor(cval, ctmp);
124          }
125 <        if (ldot < -FTINY && np->tspec > FTINY && np->alpha2 > FTINY) {
125 >        if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) {
126                  /*
127                   *  Compute specular transmission.  Specular transmission
128                   *  is always modified by material color.
129                   */
130                                                  /* roughness + source */
131 <                dtmp = np->alpha2 + omega/(2.0*PI);
131 >                dtmp = np->alpha2/2.0 + omega/(2.0*PI);
132                                                  /* gaussian */
133                  dtmp = exp((DOT(np->prdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp;
134                                                  /* worth using? */
# Line 125 | Line 142 | double  omega;                 /* light source size */
142   }
143  
144  
145 < m_normal(m, r)                  /* color a ray which hit something normal */
145 > m_normal(m, r)                  /* color a ray that hit something normal */
146   register OBJREC  *m;
147   register RAY  *r;
148   {
# Line 134 | Line 151 | register RAY  *r;
151          double  dtmp;
152          COLOR  ctmp;
153          register int  i;
137
138        if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
139                objerror(m, USER, "bad # arguments");
154                                                  /* easy shadow test */
155          if (r->crtype & SHADOW && m->otype != MAT_TRANS)
156                  return;
157 +
158 +        if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
159 +                objerror(m, USER, "bad number of arguments");
160          nd.mp = m;
161                                                  /* get material color */
162          setcolor(nd.mcolor, m->oargs.farg[0],
163                             m->oargs.farg[1],
164                             m->oargs.farg[2]);
165                                                  /* get roughness */
166 +        nd.specfl = 0;
167          nd.alpha2 = m->oargs.farg[4];
168 <        nd.alpha2 *= 2.0 * nd.alpha2;
168 >        if ((nd.alpha2 *= nd.alpha2) <= FTINY)
169 >                nd.specfl |= SP_PURE;
170                                                  /* reorient if necessary */
171          if (r->rod < 0.0)
172                  flipsurface(r);
# Line 159 | Line 178 | register RAY  *r;
178          multcolor(nd.mcolor, r->pcol);          /* modify material color */
179          transtest = 0;
180                                                  /* get specular component */
181 <        nd.rspec = m->oargs.farg[3];
182 <
164 <        if (nd.rspec > FTINY) {                 /* has specular component */
181 >        if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
182 >                nd.specfl |= SP_REFL;
183                                                  /* compute specular color */
184                  if (m->otype == MAT_METAL)
185                          copycolor(nd.scolor, nd.mcolor);
186                  else
187                          setcolor(nd.scolor, 1.0, 1.0, 1.0);
188                  scalecolor(nd.scolor, nd.rspec);
189 <                                                /* improved model */
190 <                dtmp = exp(-BSPEC(m)*nd.pdot);
191 <                for (i = 0; i < 3; i++)
192 <                        colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
193 <                nd.rspec += (1.0-nd.rspec)*dtmp;
189 >                if (nd.specfl & SP_PURE) {              /* improved model */
190 >                        dtmp = exp(-BSPEC(m)*nd.pdot);
191 >                        for (i = 0; i < 3; i++)
192 >                                colval(nd.scolor,i) +=
193 >                                                (1.0-colval(nd.scolor,i))*dtmp;
194 >                        nd.rspec += (1.0-nd.rspec)*dtmp;
195 >                } else if (specthresh > FTINY &&        /* check threshold */
196 >                                (specthresh >= 1.-FTINY ||
197 >                                specthresh > nd.rspec))
198 >                        nd.specfl |= SP_RBLT;
199                                                  /* compute reflected ray */
200                  for (i = 0; i < 3; i++)
201                          nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
202 +                if (DOT(nd.vrefl, r->ron) <= FTINY)     /* penetration? */
203 +                        for (i = 0; i < 3; i++)         /* safety measure */
204 +                                nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
205  
206 <                if (nd.alpha2 <= FTINY && !(r->crtype & SHADOW)) {
206 >                if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
207                          RAY  lr;
208                          if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
209                                  VCOPY(lr.rdir, nd.vrefl);
# Line 192 | Line 218 | register RAY  *r;
218                  nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec);
219                  nd.tspec = nd.trans * m->oargs.farg[6];
220                  nd.tdiff = nd.trans - nd.tspec;
221 <                if (r->crtype & SHADOW || DOT(r->pert,r->pert) <= FTINY*FTINY) {
222 <                        VCOPY(nd.prdir, r->rdir);
223 <                        transtest = 2;
224 <                } else {
225 <                        for (i = 0; i < 3; i++)         /* perturb direction */
226 <                                nd.prdir[i] = r->rdir[i] - .75*r->pert[i];
227 <                        normalize(nd.prdir);
221 >                if (nd.tspec > FTINY) {
222 >                        nd.specfl |= SP_TRAN;
223 >                                                        /* check threshold */
224 >                        if (!(nd.specfl & SP_PURE) && specthresh > FTINY &&
225 >                                        (specthresh >= 1.-FTINY ||
226 >                                        specthresh > nd.tspec))
227 >                                nd.specfl |= SP_TBLT;
228 >                        if (r->crtype & SHADOW ||
229 >                                        DOT(r->pert,r->pert) <= FTINY*FTINY) {
230 >                                VCOPY(nd.prdir, r->rdir);
231 >                                transtest = 2;
232 >                        } else {
233 >                                for (i = 0; i < 3; i++)         /* perturb */
234 >                                        nd.prdir[i] = r->rdir[i] -
235 >                                                        0.5*r->pert[i];
236 >                                if (DOT(nd.prdir, r->ron) < -FTINY)
237 >                                        normalize(nd.prdir);    /* OK */
238 >                                else
239 >                                        VCOPY(nd.prdir, r->rdir);
240 >                        }
241                  }
242          } else
243                  nd.tdiff = nd.tspec = nd.trans = 0.0;
244                                                  /* transmitted ray */
245 <        if (nd.tspec > FTINY && nd.alpha2 <= FTINY) {
245 >        if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
246                  RAY  lr;
247                  if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
248                          VCOPY(lr.rdir, nd.prdir);
# Line 214 | Line 253 | register RAY  *r;
253                          transtest *= bright(lr.rcol);
254                          transdist = r->rot + lr.rt;
255                  }
256 <        }
256 >        } else
257 >                transtest = 0;
258 >
259          if (r->crtype & SHADOW)                 /* the rest is shadow */
260                  return;
261                                                  /* diffuse reflection */
262          nd.rdiff = 1.0 - nd.trans - nd.rspec;
263  
264 <        if (nd.rdiff <= FTINY && nd.tdiff <= FTINY && nd.alpha2 <= FTINY)
265 <                return;                         /* purely specular */
264 >        if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
265 >                return;                         /* 100% pure specular */
266  
267 +        if (r->ro != NULL && (r->ro->otype == OBJ_FACE ||
268 +                        r->ro->otype == OBJ_RING))
269 +                nd.specfl |= SP_FLAT;
270 +
271 +        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE))
272 +                gaussamp(r, &nd);
273 +
274          if (nd.rdiff > FTINY) {         /* ambient from this side */
275                  ambient(ctmp, r);
276 <                if (nd.alpha2 <= FTINY)
229 <                        scalecolor(ctmp, nd.rdiff);
230 <                else
276 >                if (nd.specfl & SP_RBLT)
277                          scalecolor(ctmp, 1.0-nd.trans);
278 +                else
279 +                        scalecolor(ctmp, nd.rdiff);
280                  multcolor(ctmp, nd.mcolor);     /* modified by material color */
281                  addcolor(r->rcol, ctmp);        /* add to returned color */
282          }
283          if (nd.tdiff > FTINY) {         /* ambient from other side */
284                  flipsurface(r);
285                  ambient(ctmp, r);
286 <                if (nd.alpha2 <= FTINY)
239 <                        scalecolor(ctmp, nd.tdiff);
240 <                else
286 >                if (nd.specfl & SP_TBLT)
287                          scalecolor(ctmp, nd.trans);
288 +                else
289 +                        scalecolor(ctmp, nd.tdiff);
290                  multcolor(ctmp, nd.mcolor);     /* modified by color */
291                  addcolor(r->rcol, ctmp);
292                  flipsurface(r);
# Line 248 | Line 296 | register RAY  *r;
296                                          /* check distance */
297          if (transtest > bright(r->rcol))
298                  r->rt = transdist;
299 + }
300 +
301 +
302 + static
303 + gaussamp(r, np)                 /* sample gaussian specular */
304 + RAY  *r;
305 + register NORMDAT  *np;
306 + {
307 +        RAY  sr;
308 +        FVECT  u, v, h;
309 +        double  rv[2];
310 +        double  d, sinp, cosp;
311 +        register int  i;
312 +                                        /* quick test */
313 +        if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL &&
314 +                        (np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN)
315 +                return;
316 +                                        /* set up sample coordinates */
317 +        v[0] = v[1] = v[2] = 0.0;
318 +        for (i = 0; i < 3; i++)
319 +                if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6)
320 +                        break;
321 +        v[i] = 1.0;
322 +        fcross(u, v, np->pnorm);
323 +        normalize(u);
324 +        fcross(v, np->pnorm, u);
325 +                                        /* compute reflection */
326 +        if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
327 +                        rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
328 +                dimlist[ndims++] = (int)np->mp;
329 +                d = urand(ilhash(dimlist,ndims)+samplendx);
330 +                multisamp(rv, 2, d);
331 +                d = 2.0*PI * rv[0];
332 +                cosp = cos(d);
333 +                sinp = sin(d);
334 +                rv[1] = 1.0 - specjitter*rv[1];
335 +                if (rv[1] <= FTINY)
336 +                        d = 1.0;
337 +                else
338 +                        d = sqrt( np->alpha2 * -log(rv[1]) );
339 +                for (i = 0; i < 3; i++)
340 +                        h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
341 +                d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
342 +                for (i = 0; i < 3; i++)
343 +                        sr.rdir[i] = r->rdir[i] + d*h[i];
344 +                if (DOT(sr.rdir, r->ron) <= FTINY)
345 +                        VCOPY(sr.rdir, np->vrefl);      /* jitter no good */
346 +                rayvalue(&sr);
347 +                multcolor(sr.rcol, np->scolor);
348 +                addcolor(r->rcol, sr.rcol);
349 +                ndims--;
350 +        }
351 +                                        /* compute transmission */
352 +        if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
353 +                        rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
354 +                dimlist[ndims++] = (int)np->mp;
355 +                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
356 +                multisamp(rv, 2, d);
357 +                d = 2.0*PI * rv[0];
358 +                cosp = cos(d);
359 +                sinp = sin(d);
360 +                rv[1] = 1.0 - specjitter*rv[1];
361 +                if (rv[1] <= FTINY)
362 +                        d = 1.0;
363 +                else
364 +                        d = sqrt( np->alpha2/4.0 * -log(rv[1]) );
365 +                for (i = 0; i < 3; i++)
366 +                        sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]);
367 +                if (DOT(sr.rdir, r->ron) < -FTINY)
368 +                        normalize(sr.rdir);             /* OK, normalize */
369 +                else
370 +                        VCOPY(sr.rdir, np->prdir);      /* else no jitter */
371 +                rayvalue(&sr);
372 +                scalecolor(sr.rcol, np->tspec);
373 +                multcolor(sr.rcol, np->mcolor);         /* modified by color */
374 +                addcolor(r->rcol, sr.rcol);
375 +                ndims--;
376 +        }
377   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines