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.5 by greg, Tue Jan 14 16:16:45 1992 UTC vs.
Revision 2.27 by greg, Wed Jan 12 16:46:45 1994 UTC

# Line 23 | Line 23 | static char SCCSid[] = "$SunId$ LBL";
23   extern double  specthresh;              /* specular sampling threshold */
24   extern double  specjitter;              /* specular sampling jitter */
25  
26 + static  gaussamp();
27 +
28   /*
29 < *      This routine uses portions of the reflection
30 < *  model described by Cook and Torrance.
29 < *      The computation of specular components has been simplified by
30 < *  numerous approximations and ommisions to improve speed.
29 > *      This routine implements the isotropic Gaussian
30 > *  model described by Ward in Siggraph `92 article.
31   *      We orient the surface towards the incoming ray, so a single
32   *  surface can be used to represent an infinitely thin object.
33   *
# Line 38 | Line 38 | extern double  specjitter;             /* specular sampling jitte
38   *      red     grn     blu     rspec   rough   trans   tspec
39   */
40  
41 #define  BSPEC(m)       (6.0)           /* specularity parameter b */
42
41                                  /* specularity flags */
42   #define  SP_REFL        01              /* has reflected specular component */
43   #define  SP_TRAN        02              /* has transmitted specular */
44 < #define  SP_PURE        010             /* purely specular (zero roughness) */
45 < #define  SP_FLAT        020             /* flat reflecting surface */
46 < #define  SP_RBLT        040             /* reflection below sample threshold */
47 < #define  SP_TBLT        0100            /* transmission below threshold */
44 > #define  SP_PURE        04              /* purely specular (zero roughness) */
45 > #define  SP_FLAT        010             /* flat reflecting surface */
46 > #define  SP_RBLT        020             /* reflection below sample threshold */
47 > #define  SP_TBLT        040             /* transmission below threshold */
48  
49   typedef struct {
50          OBJREC  *mp;            /* material pointer */
51 +        RAY  *rp;               /* ray pointer */
52          short  specfl;          /* specularity flags, defined above */
53          COLOR  mcolor;          /* color of this material */
54          COLOR  scolor;          /* color of specular component */
# Line 71 | Line 70 | FVECT  ldir;                   /* light source direction */
70   double  omega;                  /* light source size */
71   {
72          double  ldot;
73 <        double  dtmp;
74 <        int     i;
73 >        double  dtmp, d2;
74 >        FVECT  vtmp;
75          COLOR  ctmp;
76  
77          setcolor(cval, 0.0, 0.0, 0.0);
# Line 99 | Line 98 | double  omega;                 /* light source size */
98                   *  gaussian distribution model.
99                   */
100                                                  /* roughness */
101 <                dtmp = 2.0*np->alpha2;
101 >                dtmp = np->alpha2;
102                                                  /* + source if flat */
103                  if (np->specfl & SP_FLAT)
104 <                        dtmp += omega/(2.0*PI);
104 >                        dtmp += omega/(4.0*PI);
105 >                                                /* half vector */
106 >                vtmp[0] = ldir[0] - np->rp->rdir[0];
107 >                vtmp[1] = ldir[1] - np->rp->rdir[1];
108 >                vtmp[2] = ldir[2] - np->rp->rdir[2];
109 >                d2 = DOT(vtmp, np->pnorm);
110 >                d2 *= d2;
111 >                d2 = (DOT(vtmp,vtmp) - d2) / d2;
112                                                  /* gaussian */
113 <                dtmp = exp((DOT(np->vrefl,ldir)-1.)/dtmp)/(2.*PI)/dtmp;
113 >                dtmp = exp(-d2/dtmp)/(4.*PI*dtmp);
114                                                  /* worth using? */
115                  if (dtmp > FTINY) {
116                          copycolor(ctmp, np->scolor);
117 <                        dtmp *= omega / np->pdot;
117 >                        dtmp *= omega * sqrt(ldot/np->pdot);
118                          scalecolor(ctmp, dtmp);
119                          addcolor(cval, ctmp);
120                  }
# Line 128 | Line 134 | double  omega;                 /* light source size */
134                   *  is always modified by material color.
135                   */
136                                                  /* roughness + source */
137 <                dtmp = np->alpha2 + omega/(2.0*PI);
137 >                dtmp = np->alpha2 + omega/PI;
138                                                  /* gaussian */
139 <                dtmp = exp((DOT(np->prdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp;
139 >                dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp);
140                                                  /* worth using? */
141                  if (dtmp > FTINY) {
142                          copycolor(ctmp, np->mcolor);
143 <                        dtmp *= np->tspec * omega / np->pdot;
143 >                        dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot);
144                          scalecolor(ctmp, dtmp);
145                          addcolor(cval, ctmp);
146                  }
# Line 148 | Line 154 | register RAY  *r;
154   {
155          NORMDAT  nd;
156          double  transtest, transdist;
151        double  dtmp;
157          COLOR  ctmp;
158          register int  i;
159                                                  /* easy shadow test */
160          if (r->crtype & SHADOW && m->otype != MAT_TRANS)
161 <                return;
161 >                return(1);
162  
163          if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
164                  objerror(m, USER, "bad number of arguments");
165          nd.mp = m;
166 +        nd.rp = r;
167                                                  /* get material color */
168          setcolor(nd.mcolor, m->oargs.farg[0],
169                             m->oargs.farg[1],
# Line 177 | Line 183 | register RAY  *r;
183                  nd.pdot = .001;                 /* non-zero for dirnorm() */
184          multcolor(nd.mcolor, r->pcol);          /* modify material color */
185          transtest = 0;
186 +        transdist = r->rot;
187                                                  /* get specular component */
188          if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
189                  nd.specfl |= SP_REFL;
# Line 186 | Line 193 | register RAY  *r;
193                  else
194                          setcolor(nd.scolor, 1.0, 1.0, 1.0);
195                  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;
196                                                  /* check threshold */
197 <                if (nd.rspec <= specthresh+FTINY)
197 >                if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY)
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 (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
207                          RAY  lr;
# Line 216 | Line 221 | register RAY  *r;
221                  if (nd.tspec > FTINY) {
222                          nd.specfl |= SP_TRAN;
223                                                          /* check threshold */
224 <                        if (nd.tspec <= specthresh+FTINY)
224 >                        if (!(nd.specfl & SP_PURE) &&
225 >                                        specthresh >= nd.tspec-FTINY)
226                                  nd.specfl |= SP_TBLT;
227                          if (r->crtype & SHADOW ||
228                                          DOT(r->pert,r->pert) <= FTINY*FTINY) {
# Line 224 | Line 230 | register RAY  *r;
230                                  transtest = 2;
231                          } else {
232                                  for (i = 0; i < 3; i++)         /* perturb */
233 <                                        nd.prdir[i] = r->rdir[i] -
234 <                                                        .75*r->pert[i];
235 <                                normalize(nd.prdir);
233 >                                        nd.prdir[i] = r->rdir[i] - r->pert[i];
234 >                                if (DOT(nd.prdir, r->ron) < -FTINY)
235 >                                        normalize(nd.prdir);    /* OK */
236 >                                else
237 >                                        VCOPY(nd.prdir, r->rdir);
238                          }
239                  }
240          } else
# Line 243 | Line 251 | register RAY  *r;
251                          transtest *= bright(lr.rcol);
252                          transdist = r->rot + lr.rt;
253                  }
254 <        }
254 >        } else
255 >                transtest = 0;
256  
257          if (r->crtype & SHADOW)                 /* the rest is shadow */
258 <                return;
258 >                return(1);
259                                                  /* diffuse reflection */
260          nd.rdiff = 1.0 - nd.trans - nd.rspec;
261  
262          if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
263 <                return;                         /* 100% pure specular */
263 >                return(1);                      /* 100% pure specular */
264  
265 <        if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING)
265 >        if (r->ro != NULL && (r->ro->otype == OBJ_FACE ||
266 >                        r->ro->otype == OBJ_RING))
267                  nd.specfl |= SP_FLAT;
268  
269          if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE))
# Line 284 | Line 294 | register RAY  *r;
294                                          /* check distance */
295          if (transtest > bright(r->rcol))
296                  r->rt = transdist;
297 +
298 +        return(1);
299   }
300  
301  
# Line 296 | Line 308 | register NORMDAT  *np;
308          FVECT  u, v, h;
309          double  rv[2];
310          double  d, sinp, cosp;
299        int  ntries;
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++)
# Line 311 | Line 326 | register NORMDAT  *np;
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 <                for (ntries = 0; ntries < 10; ntries++) {
330 <                        dimlist[ndims] = ntries * 8912;
331 <                        d = urand(ilhash(dimlist,ndims+1)+samplendx);
332 <                        multisamp(rv, 2, d);
333 <                        d = 2.0*PI * rv[0];
334 <                        cosp = cos(d);
335 <                        sinp = sin(d);
336 <                        rv[1] = 1.0 - specjitter*rv[1];
337 <                        if (rv[1] <= FTINY)
338 <                                d = 1.0;
339 <                        else
340 <                                d = sqrt( np->alpha2 * -log(rv[1]) );
341 <                        for (i = 0; i < 3; i++)
342 <                                h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
343 <                        d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
344 <                        for (i = 0; i < 3; i++)
345 <                                sr.rdir[i] = r->rdir[i] + d*h[i];
346 <                        if (DOT(sr.rdir, r->ron) > FTINY) {
347 <                                rayvalue(&sr);
348 <                                multcolor(sr.rcol, np->scolor);
334 <                                addcolor(r->rcol, sr.rcol);
335 <                                break;
336 <                        }
337 <                }
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( -log(rv[1]) * np->alpha2 );
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