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.4 by greg, Tue Jan 14 15:33:08 1992 UTC vs.
Revision 2.23 by greg, Fri Feb 12 10:41:02 1993 UTC

# Line 20 | Line 20 | static char SCCSid[] = "$SunId$ LBL";
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.
26 < *      The computation of specular components has been simplified by
27 < *  numerous approximations and ommisions to improve speed.
27 > *      This routine implements the isotropic Gaussian
28 > *  model described by Ward in Siggraph `92 article.
29   *      We orient the surface towards the incoming ray, so a single
30   *  surface can be used to represent an infinitely thin object.
31   *
# Line 40 | Line 41 | static char SCCSid[] = "$SunId$ LBL";
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 */
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 66 | 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 94 | 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 123 | 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 153 | Line 164 | register RAY  *r;
164          if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
165                  objerror(m, USER, "bad number of arguments");
166          nd.mp = m;
167 +        nd.rp = r;
168                                                  /* get material color */
169          setcolor(nd.mcolor, m->oargs.farg[0],
170                             m->oargs.farg[1],
# Line 186 | Line 198 | register RAY  *r;
198                  for (i = 0; i < 3; i++)
199                          colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
200                  nd.rspec += (1.0-nd.rspec)*dtmp;
201 +                                                /* check threshold */
202 +                if (!(nd.specfl & SP_PURE) &&
203 +                                specthresh > FTINY &&
204 +                                (specthresh >= 1.-FTINY ||
205 +                                specthresh + .05 - .1*frandom() > nd.rspec))
206 +                        nd.specfl |= SP_RBLT;
207                                                  /* compute reflected ray */
208                  for (i = 0; i < 3; i++)
209                          nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
210 +                if (DOT(nd.vrefl, r->ron) <= FTINY)     /* penetration? */
211 +                        for (i = 0; i < 3; i++)         /* safety measure */
212 +                                nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
213  
214                  if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
215                          RAY  lr;
# Line 207 | Line 228 | register RAY  *r;
228                  nd.tdiff = nd.trans - nd.tspec;
229                  if (nd.tspec > FTINY) {
230                          nd.specfl |= SP_TRAN;
231 +                                                        /* check threshold */
232 +                        if (!(nd.specfl & SP_PURE) && specthresh > FTINY &&
233 +                                        (specthresh >= 1.-FTINY ||
234 +                                specthresh + .05 - .1*frandom() > nd.tspec))
235 +                                nd.specfl |= SP_TBLT;
236                          if (r->crtype & SHADOW ||
237                                          DOT(r->pert,r->pert) <= FTINY*FTINY) {
238                                  VCOPY(nd.prdir, r->rdir);
239                                  transtest = 2;
240                          } else {
241                                  for (i = 0; i < 3; i++)         /* perturb */
242 <                                        nd.prdir[i] = r->rdir[i] -
243 <                                                        .75*r->pert[i];
244 <                                normalize(nd.prdir);
242 >                                        nd.prdir[i] = r->rdir[i] - r->pert[i];
243 >                                if (DOT(nd.prdir, r->ron) < -FTINY)
244 >                                        normalize(nd.prdir);    /* OK */
245 >                                else
246 >                                        VCOPY(nd.prdir, r->rdir);
247                          }
248                  }
249          } else
# Line 232 | Line 260 | register RAY  *r;
260                          transtest *= bright(lr.rcol);
261                          transdist = r->rot + lr.rt;
262                  }
263 <        }
263 >        } else
264 >                transtest = 0;
265  
266          if (r->crtype & SHADOW)                 /* the rest is shadow */
267                  return;
# Line 242 | Line 271 | register RAY  *r;
271          if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
272                  return;                         /* 100% pure specular */
273  
274 <        if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING)
274 >        if (r->ro != NULL && (r->ro->otype == OBJ_FACE ||
275 >                        r->ro->otype == OBJ_RING))
276                  nd.specfl |= SP_FLAT;
277  
278          if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE))
# Line 250 | Line 280 | register RAY  *r;
280  
281          if (nd.rdiff > FTINY) {         /* ambient from this side */
282                  ambient(ctmp, r);
283 <                scalecolor(ctmp, nd.rdiff);
283 >                if (nd.specfl & SP_RBLT)
284 >                        scalecolor(ctmp, 1.0-nd.trans);
285 >                else
286 >                        scalecolor(ctmp, nd.rdiff);
287                  multcolor(ctmp, nd.mcolor);     /* modified by material color */
288                  addcolor(r->rcol, ctmp);        /* add to returned color */
289          }
290          if (nd.tdiff > FTINY) {         /* ambient from other side */
291                  flipsurface(r);
292                  ambient(ctmp, r);
293 <                scalecolor(ctmp, nd.tdiff);
293 >                if (nd.specfl & SP_TBLT)
294 >                        scalecolor(ctmp, nd.trans);
295 >                else
296 >                        scalecolor(ctmp, nd.tdiff);
297                  multcolor(ctmp, nd.mcolor);     /* modified by color */
298                  addcolor(r->rcol, ctmp);
299                  flipsurface(r);
# Line 279 | Line 315 | register NORMDAT  *np;
315          FVECT  u, v, h;
316          double  rv[2];
317          double  d, sinp, cosp;
282        int  ntries;
318          register int  i;
319 +                                        /* quick test */
320 +        if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL &&
321 +                        (np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN)
322 +                return;
323                                          /* set up sample coordinates */
324          v[0] = v[1] = v[2] = 0.0;
325          for (i = 0; i < 3; i++)
# Line 291 | Line 330 | register NORMDAT  *np;
330          normalize(u);
331          fcross(v, np->pnorm, u);
332                                          /* compute reflection */
333 <        if (np->specfl & SP_REFL &&
333 >        if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
334                          rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
335                  dimlist[ndims++] = (int)np->mp;
336 <                for (ntries = 0; ntries < 10; ntries++) {
337 <                        dimlist[ndims] = ntries * 8912;
338 <                        d = urand(ilhash(dimlist,ndims+1)+samplendx);
339 <                        multisamp(rv, 2, d);
340 <                        d = 2.0*PI * rv[0];
341 <                        cosp = cos(d);
342 <                        sinp = sin(d);
343 <                        if (rv[1] <= FTINY)
344 <                                d = 1.0;
345 <                        else
346 <                                d = sqrt( np->alpha2 * -log(rv[1]) );
347 <                        for (i = 0; i < 3; i++)
348 <                                h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
349 <                        d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
350 <                        for (i = 0; i < 3; i++)
351 <                                sr.rdir[i] = r->rdir[i] + d*h[i];
352 <                        if (DOT(sr.rdir, r->ron) > FTINY) {
353 <                                rayvalue(&sr);
354 <                                multcolor(sr.rcol, np->scolor);
355 <                                addcolor(r->rcol, sr.rcol);
317 <                                break;
318 <                        }
319 <                }
336 >                d = urand(ilhash(dimlist,ndims)+samplendx);
337 >                multisamp(rv, 2, d);
338 >                d = 2.0*PI * rv[0];
339 >                cosp = cos(d);
340 >                sinp = sin(d);
341 >                rv[1] = 1.0 - specjitter*rv[1];
342 >                if (rv[1] <= FTINY)
343 >                        d = 1.0;
344 >                else
345 >                        d = sqrt( np->alpha2 * -log(rv[1]) );
346 >                for (i = 0; i < 3; i++)
347 >                        h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
348 >                d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
349 >                for (i = 0; i < 3; i++)
350 >                        sr.rdir[i] = r->rdir[i] + d*h[i];
351 >                if (DOT(sr.rdir, r->ron) <= FTINY)
352 >                        VCOPY(sr.rdir, np->vrefl);      /* jitter no good */
353 >                rayvalue(&sr);
354 >                multcolor(sr.rcol, np->scolor);
355 >                addcolor(r->rcol, sr.rcol);
356                  ndims--;
357          }
358                                          /* compute transmission */
359 +        if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
360 +                        rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
361 +                dimlist[ndims++] = (int)np->mp;
362 +                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
363 +                multisamp(rv, 2, d);
364 +                d = 2.0*PI * rv[0];
365 +                cosp = cos(d);
366 +                sinp = sin(d);
367 +                rv[1] = 1.0 - specjitter*rv[1];
368 +                if (rv[1] <= FTINY)
369 +                        d = 1.0;
370 +                else
371 +                        d = sqrt( -log(rv[1]) * np->alpha2 );
372 +                for (i = 0; i < 3; i++)
373 +                        sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]);
374 +                if (DOT(sr.rdir, r->ron) < -FTINY)
375 +                        normalize(sr.rdir);             /* OK, normalize */
376 +                else
377 +                        VCOPY(sr.rdir, np->prdir);      /* else no jitter */
378 +                rayvalue(&sr);
379 +                scalecolor(sr.rcol, np->tspec);
380 +                multcolor(sr.rcol, np->mcolor);         /* modified by color */
381 +                addcolor(r->rcol, sr.rcol);
382 +                ndims--;
383 +        }
384   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines