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 1.5 by greg, Tue Mar 27 11:40:02 1990 UTC vs.
Revision 2.29 by greg, Fri Sep 15 15:47:30 1995 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 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 + extern int  backvis;                    /* back faces visible? */
27 +
28 + static  gaussamp();
29 +
30   /*
31 < *      This routine uses portions of the reflection
32 < *  model described by Cook and Torrance.
23 < *      The computation of specular components has been simplified by
24 < *  numerous approximations and ommisions to improve speed.
31 > *      This routine implements the isotropic Gaussian
32 > *  model described by Ward in Siggraph `92 article.
33   *      We orient the surface towards the incoming ray, so a single
34   *  surface can be used to represent an infinitely thin object.
35   *
# Line 32 | Line 40 | static char SCCSid[] = "$SunId$ LBL";
40   *      red     grn     blu     rspec   rough   trans   tspec
41   */
42  
43 < #define  BSPEC(m)       (6.0)           /* specularity parameter b */
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  
37 extern double  exp();
38
51   typedef struct {
52          OBJREC  *mp;            /* material pointer */
53 <        RAY  *pr;               /* intersected ray */
53 >        RAY  *rp;               /* ray pointer */
54 >        short  specfl;          /* specularity flags, defined above */
55          COLOR  mcolor;          /* color of this material */
56          COLOR  scolor;          /* color of specular component */
57          FVECT  vrefl;           /* vector in direction of reflected ray */
58 <        double  alpha2;         /* roughness squared times 2 */
58 >        FVECT  prdir;           /* vector in transmitted direction */
59 >        double  alpha2;         /* roughness squared */
60          double  rdiff, rspec;   /* reflected specular, diffuse */
61          double  trans;          /* transmissivity */
62          double  tdiff, tspec;   /* transmitted specular, diffuse */
# Line 58 | Line 72 | FVECT  ldir;                   /* light source direction */
72   double  omega;                  /* light source size */
73   {
74          double  ldot;
75 <        double  dtmp;
75 >        double  dtmp, d2;
76 >        FVECT  vtmp;
77          COLOR  ctmp;
78  
79          setcolor(cval, 0.0, 0.0, 0.0);
# Line 79 | Line 94 | double  omega;                 /* light source size */
94                  scalecolor(ctmp, dtmp);
95                  addcolor(cval, ctmp);
96          }
97 <        if (ldot > FTINY && np->rspec > FTINY && np->alpha2 > FTINY) {
97 >        if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) {
98                  /*
99                   *  Compute specular reflection coefficient using
100                   *  gaussian distribution model.
101                   */
102 <                                                /* roughness + source */
103 <                dtmp = np->alpha2 + omega/(2.0*PI);
102 >                                                /* roughness */
103 >                dtmp = np->alpha2;
104 >                                                /* + source if flat */
105 >                if (np->specfl & SP_FLAT)
106 >                        dtmp += omega/(4.0*PI);
107 >                                                /* half vector */
108 >                vtmp[0] = ldir[0] - np->rp->rdir[0];
109 >                vtmp[1] = ldir[1] - np->rp->rdir[1];
110 >                vtmp[2] = ldir[2] - np->rp->rdir[2];
111 >                d2 = DOT(vtmp, np->pnorm);
112 >                d2 *= d2;
113 >                d2 = (DOT(vtmp,vtmp) - d2) / d2;
114                                                  /* gaussian */
115 <                dtmp = exp((DOT(np->vrefl,ldir)-1.)/dtmp)/(2.*PI)/dtmp;
115 >                dtmp = exp(-d2/dtmp)/(4.*PI*dtmp);
116                                                  /* worth using? */
117                  if (dtmp > FTINY) {
118                          copycolor(ctmp, np->scolor);
119 <                        dtmp *= omega;
119 >                        dtmp *= omega * sqrt(ldot/np->pdot);
120                          scalecolor(ctmp, dtmp);
121                          addcolor(cval, ctmp);
122                  }
# Line 105 | Line 130 | double  omega;                 /* light source size */
130                  scalecolor(ctmp, dtmp);
131                  addcolor(cval, ctmp);
132          }
133 <        if (ldot < -FTINY && np->tspec > FTINY && np->alpha2 > FTINY) {
133 >        if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) {
134                  /*
135                   *  Compute specular transmission.  Specular transmission
136 <                 *  is unaffected by material color.
136 >                 *  is always modified by material color.
137                   */
138                                                  /* roughness + source */
139 <                dtmp = np->alpha2 + omega/(2.0*PI);
139 >                dtmp = np->alpha2 + omega/PI;
140                                                  /* gaussian */
141 <                dtmp = exp((DOT(np->pr->rdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp;
141 >                dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp);
142                                                  /* worth using? */
143                  if (dtmp > FTINY) {
144 <                        dtmp *= np->tspec * omega;
145 <                        setcolor(ctmp, dtmp, dtmp, dtmp);
144 >                        copycolor(ctmp, np->mcolor);
145 >                        dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot);
146 >                        scalecolor(ctmp, dtmp);
147                          addcolor(cval, ctmp);
148                  }
149          }
150   }
151  
152  
153 < m_normal(m, r)                  /* color a ray which hit something normal */
153 > m_normal(m, r)                  /* color a ray that hit something normal */
154   register OBJREC  *m;
155   register RAY  *r;
156   {
157          NORMDAT  nd;
158 <        double  ldot;
159 <        double  omega;
160 <        double  dtmp;
158 >        double  transtest, transdist;
159 >        double  mirtest, mirdist;
160 >        int     hastexture;
161 >        double  d;
162          COLOR  ctmp;
163          register int  i;
137
138        if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
139                objerror(m, USER, "bad # arguments");
164                                                  /* easy shadow test */
165          if (r->crtype & SHADOW && m->otype != MAT_TRANS)
166 <                return;
166 >                return(1);
167 >
168 >        if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
169 >                objerror(m, USER, "bad number of arguments");
170 >                                                /* check for back side */
171 >        if (r->rod < 0.0) {
172 >                if (!backvis && m->otype != MAT_TRANS) {
173 >                        raytrans(r);
174 >                        return(1);
175 >                }
176 >                flipsurface(r);                 /* reorient if backvis */
177 >        }
178          nd.mp = m;
179 <        nd.pr = r;
179 >        nd.rp = r;
180                                                  /* get material color */
181          setcolor(nd.mcolor, m->oargs.farg[0],
182                             m->oargs.farg[1],
183                             m->oargs.farg[2]);
184                                                  /* get roughness */
185 +        nd.specfl = 0;
186          nd.alpha2 = m->oargs.farg[4];
187 <        nd.alpha2 *= 2.0 * nd.alpha2;
188 <                                                /* reorient if necessary */
189 <        if (r->rod < 0.0)
190 <                flipsurface(r);
187 >        if ((nd.alpha2 *= nd.alpha2) <= FTINY)
188 >                nd.specfl |= SP_PURE;
189 >        if (r->ro != NULL && isflat(r->ro->otype))
190 >                nd.specfl |= SP_FLAT;
191                                                  /* get modifiers */
192          raytexture(r, m->omod);
193 <        nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
193 >        if (hastexture = DOT(r->pert,r->pert) > FTINY*FTINY)
194 >                nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
195 >        else {
196 >                VCOPY(nd.pnorm, r->ron);
197 >                nd.pdot = r->rod;
198 >        }
199 >        if (nd.pdot < .001)
200 >                nd.pdot = .001;                 /* non-zero for dirnorm() */
201          multcolor(nd.mcolor, r->pcol);          /* modify material color */
202 <        r->rt = r->rot;                         /* default ray length */
202 >        mirtest = transtest = 0;
203 >        mirdist = transdist = r->rot;
204                                                  /* get specular component */
205 <        nd.rspec = m->oargs.farg[3];
206 <
163 <        if (nd.rspec > FTINY) {                 /* has specular component */
205 >        if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
206 >                nd.specfl |= SP_REFL;
207                                                  /* compute specular color */
208                  if (m->otype == MAT_METAL)
209                          copycolor(nd.scolor, nd.mcolor);
210                  else
211                          setcolor(nd.scolor, 1.0, 1.0, 1.0);
212                  scalecolor(nd.scolor, nd.rspec);
213 <                                                /* improved model */
214 <                dtmp = exp(-BSPEC(m)*nd.pdot);
215 <                for (i = 0; i < 3; i++)
173 <                        colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
174 <                nd.rspec += (1.0-nd.rspec)*dtmp;
213 >                                                /* check threshold */
214 >                if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY)
215 >                        nd.specfl |= SP_RBLT;
216                                                  /* compute reflected ray */
217                  for (i = 0; i < 3; i++)
218 <                        nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
218 >                        nd.vrefl[i] = r->rdir[i] + 2.*nd.pdot*nd.pnorm[i];
219 >                                                /* penetration? */
220 >                if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY)
221 >                        for (i = 0; i < 3; i++)         /* safety measure */
222 >                                nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
223  
224 <                if (nd.alpha2 <= FTINY && !(r->crtype & SHADOW)) {
224 >                if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
225                          RAY  lr;
226                          if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
227                                  VCOPY(lr.rdir, nd.vrefl);
228                                  rayvalue(&lr);
229                                  multcolor(lr.rcol, nd.scolor);
230                                  addcolor(r->rcol, lr.rcol);
231 <                                if (nd.rspec > 0.5 && m->omod == OVOID)
232 <                                        r->rt = r->rot + lr.rt;
231 >                                if (!hastexture && nd.specfl & SP_FLAT) {
232 >                                        mirtest = 2.*bright(lr.rcol);
233 >                                        mirdist = r->rot + lr.rt;
234 >                                }
235                          }
236                  }
237          }
# Line 193 | Line 240 | register RAY  *r;
240                  nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec);
241                  nd.tspec = nd.trans * m->oargs.farg[6];
242                  nd.tdiff = nd.trans - nd.tspec;
243 +                if (nd.tspec > FTINY) {
244 +                        nd.specfl |= SP_TRAN;
245 +                                                        /* check threshold */
246 +                        if (!(nd.specfl & SP_PURE) &&
247 +                                        specthresh >= nd.tspec-FTINY)
248 +                                nd.specfl |= SP_TBLT;
249 +                        if (!hastexture || r->crtype & SHADOW) {
250 +                                VCOPY(nd.prdir, r->rdir);
251 +                                transtest = 2;
252 +                        } else {
253 +                                for (i = 0; i < 3; i++)         /* perturb */
254 +                                        nd.prdir[i] = r->rdir[i] - r->pert[i];
255 +                                if (DOT(nd.prdir, r->ron) < -FTINY)
256 +                                        normalize(nd.prdir);    /* OK */
257 +                                else
258 +                                        VCOPY(nd.prdir, r->rdir);
259 +                        }
260 +                }
261          } else
262                  nd.tdiff = nd.tspec = nd.trans = 0.0;
263                                                  /* transmitted ray */
264 <        if (nd.tspec > FTINY && nd.alpha2 <= FTINY) {
264 >        if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
265                  RAY  lr;
266                  if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
267 <                        VCOPY(lr.rdir, r->rdir);
267 >                        VCOPY(lr.rdir, nd.prdir);
268                          rayvalue(&lr);
269                          scalecolor(lr.rcol, nd.tspec);
270 +                        multcolor(lr.rcol, nd.mcolor);  /* modified by color */
271                          addcolor(r->rcol, lr.rcol);
272 <                        if (nd.tspec > .5)
273 <                                r->rt = r->rot + lr.rt;
272 >                        transtest *= bright(lr.rcol);
273 >                        transdist = r->rot + lr.rt;
274                  }
275 +        } else
276 +                transtest = 0;
277 +
278 +        if (r->crtype & SHADOW) {               /* the rest is shadow */
279 +                r->rt = transdist;
280 +                return(1);
281          }
210        if (r->crtype & SHADOW)                 /* the rest is shadow */
211                return;
282                                                  /* diffuse reflection */
283          nd.rdiff = 1.0 - nd.trans - nd.rspec;
284  
285 <        if (nd.rdiff <= FTINY && nd.tdiff <= FTINY && nd.alpha2 <= FTINY)
286 <                return;                         /* purely specular */
285 >        if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
286 >                return(1);                      /* 100% pure specular */
287  
288 +        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE))
289 +                gaussamp(r, &nd);
290 +
291          if (nd.rdiff > FTINY) {         /* ambient from this side */
292                  ambient(ctmp, r);
293 <                if (nd.alpha2 <= FTINY)
221 <                        scalecolor(ctmp, nd.rdiff);
222 <                else
293 >                if (nd.specfl & SP_RBLT)
294                          scalecolor(ctmp, 1.0-nd.trans);
295 +                else
296 +                        scalecolor(ctmp, nd.rdiff);
297                  multcolor(ctmp, nd.mcolor);     /* modified by material color */
298                  addcolor(r->rcol, ctmp);        /* add to returned color */
299          }
300          if (nd.tdiff > FTINY) {         /* ambient from other side */
301                  flipsurface(r);
302                  ambient(ctmp, r);
303 <                if (nd.alpha2 <= FTINY)
231 <                        scalecolor(ctmp, nd.tdiff);
232 <                else
303 >                if (nd.specfl & SP_TBLT)
304                          scalecolor(ctmp, nd.trans);
305 <                multcolor(ctmp, nd.mcolor);
305 >                else
306 >                        scalecolor(ctmp, nd.tdiff);
307 >                multcolor(ctmp, nd.mcolor);     /* modified by color */
308                  addcolor(r->rcol, ctmp);
309                  flipsurface(r);
310          }
311                                          /* add direct component */
312          direct(r, dirnorm, &nd);
313 +                                        /* check distance */
314 +        d = bright(r->rcol);
315 +        if (transtest > d)
316 +                r->rt = transdist;
317 +        else if (mirtest > d)
318 +                r->rt = mirdist;
319 +
320 +        return(1);
321 + }
322 +
323 +
324 + static
325 + gaussamp(r, np)                 /* sample gaussian specular */
326 + RAY  *r;
327 + register NORMDAT  *np;
328 + {
329 +        RAY  sr;
330 +        FVECT  u, v, h;
331 +        double  rv[2];
332 +        double  d, sinp, cosp;
333 +        register int  i;
334 +                                        /* quick test */
335 +        if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL &&
336 +                        (np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN)
337 +                return;
338 +                                        /* set up sample coordinates */
339 +        v[0] = v[1] = v[2] = 0.0;
340 +        for (i = 0; i < 3; i++)
341 +                if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6)
342 +                        break;
343 +        v[i] = 1.0;
344 +        fcross(u, v, np->pnorm);
345 +        normalize(u);
346 +        fcross(v, np->pnorm, u);
347 +                                        /* compute reflection */
348 +        if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
349 +                        rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
350 +                dimlist[ndims++] = (int)np->mp;
351 +                d = urand(ilhash(dimlist,ndims)+samplendx);
352 +                multisamp(rv, 2, d);
353 +                d = 2.0*PI * rv[0];
354 +                cosp = cos(d);
355 +                sinp = sin(d);
356 +                rv[1] = 1.0 - specjitter*rv[1];
357 +                if (rv[1] <= FTINY)
358 +                        d = 1.0;
359 +                else
360 +                        d = sqrt( np->alpha2 * -log(rv[1]) );
361 +                for (i = 0; i < 3; i++)
362 +                        h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
363 +                d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
364 +                for (i = 0; i < 3; i++)
365 +                        sr.rdir[i] = r->rdir[i] + d*h[i];
366 +                if (DOT(sr.rdir, r->ron) <= FTINY)
367 +                        VCOPY(sr.rdir, np->vrefl);      /* jitter no good */
368 +                rayvalue(&sr);
369 +                multcolor(sr.rcol, np->scolor);
370 +                addcolor(r->rcol, sr.rcol);
371 +                ndims--;
372 +        }
373 +                                        /* compute transmission */
374 +        if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
375 +                        rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
376 +                dimlist[ndims++] = (int)np->mp;
377 +                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
378 +                multisamp(rv, 2, d);
379 +                d = 2.0*PI * rv[0];
380 +                cosp = cos(d);
381 +                sinp = sin(d);
382 +                rv[1] = 1.0 - specjitter*rv[1];
383 +                if (rv[1] <= FTINY)
384 +                        d = 1.0;
385 +                else
386 +                        d = sqrt( -log(rv[1]) * np->alpha2 );
387 +                for (i = 0; i < 3; i++)
388 +                        sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]);
389 +                if (DOT(sr.rdir, r->ron) < -FTINY)
390 +                        normalize(sr.rdir);             /* OK, normalize */
391 +                else
392 +                        VCOPY(sr.rdir, np->prdir);      /* else no jitter */
393 +                rayvalue(&sr);
394 +                scalecolor(sr.rcol, np->tspec);
395 +                multcolor(sr.rcol, np->mcolor);         /* modified by color */
396 +                addcolor(r->rcol, sr.rcol);
397 +                ndims--;
398 +        }
399   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines