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.3 by greg, Sat Jan 4 23:36:42 1992 UTC vs.
Revision 2.21 by greg, Wed May 20 14:23:47 1992 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.
# Line 40 | Line 43 | static char SCCSid[] = "$SunId$ LBL";
43                                  /* specularity flags */
44   #define  SP_REFL        01              /* has reflected specular component */
45   #define  SP_TRAN        02              /* has transmitted specular */
46 < #define  SP_PURE        010             /* purely specular (zero roughness) */
47 < #define  SP_FLAT        020             /* flat reflecting surface */
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 +        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 */
# Line 66 | Line 72 | FVECT  ldir;                   /* light source direction */
72   double  omega;                  /* light source size */
73   {
74          double  ldot;
75 <        double  dtmp;
76 <        int     i;
75 >        double  dtmp, d2;
76 >        FVECT  vtmp;
77          COLOR  ctmp;
78  
79          setcolor(cval, 0.0, 0.0, 0.0);
# Line 94 | Line 100 | double  omega;                 /* light source size */
100                   *  gaussian distribution model.
101                   */
102                                                  /* roughness */
103 <                dtmp = 2.0*np->alpha2;
103 >                dtmp = np->alpha2;
104                                                  /* + source if flat */
105                  if (np->specfl & SP_FLAT)
106 <                        dtmp += omega/(2.0*PI);
106 >                        dtmp += omega/(4.0*PI);
107 >                                                /* delta */
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 = 2.0 - 2.0*d2/sqrt(DOT(vtmp,vtmp));
113                                                  /* gaussian */
114 <                dtmp = exp((DOT(np->vrefl,ldir)-1.)/dtmp)/(2.*PI)/dtmp;
114 >                dtmp = exp(-d2/dtmp)/(4.*PI*dtmp);
115                                                  /* worth using? */
116                  if (dtmp > FTINY) {
117                          copycolor(ctmp, np->scolor);
118 <                        dtmp *= omega / np->pdot;
118 >                        dtmp *= omega * sqrt(ldot/np->pdot);
119                          scalecolor(ctmp, dtmp);
120                          addcolor(cval, ctmp);
121                  }
# Line 123 | Line 135 | double  omega;                 /* light source size */
135                   *  is always modified by material color.
136                   */
137                                                  /* roughness + source */
138 <                dtmp = np->alpha2 + omega/(2.0*PI);
138 >                dtmp = np->alpha2 + omega/PI;
139                                                  /* gaussian */
140 <                dtmp = exp((DOT(np->prdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp;
140 >                dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp);
141                                                  /* worth using? */
142                  if (dtmp > FTINY) {
143                          copycolor(ctmp, np->mcolor);
144 <                        dtmp *= np->tspec * omega / np->pdot;
144 >                        dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot);
145                          scalecolor(ctmp, dtmp);
146                          addcolor(cval, ctmp);
147                  }
# Line 153 | Line 165 | register RAY  *r;
165          if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
166                  objerror(m, USER, "bad number of arguments");
167          nd.mp = m;
168 +        nd.rp = r;
169                                                  /* get material color */
170          setcolor(nd.mcolor, m->oargs.farg[0],
171                             m->oargs.farg[1],
# Line 186 | Line 199 | register RAY  *r;
199                  for (i = 0; i < 3; i++)
200                          colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
201                  nd.rspec += (1.0-nd.rspec)*dtmp;
202 +                                                /* check threshold */
203 +                if (!(nd.specfl & SP_PURE) &&
204 +                                specthresh > FTINY &&
205 +                                (specthresh >= 1.-FTINY ||
206 +                                specthresh + .05 - .1*frandom() > nd.rspec))
207 +                        nd.specfl |= SP_RBLT;
208                                                  /* compute reflected ray */
209                  for (i = 0; i < 3; i++)
210                          nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
211 +                if (DOT(nd.vrefl, r->ron) <= FTINY)     /* penetration? */
212 +                        for (i = 0; i < 3; i++)         /* safety measure */
213 +                                nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
214  
215                  if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
216                          RAY  lr;
# Line 207 | Line 229 | register RAY  *r;
229                  nd.tdiff = nd.trans - nd.tspec;
230                  if (nd.tspec > FTINY) {
231                          nd.specfl |= SP_TRAN;
232 +                                                        /* check threshold */
233 +                        if (!(nd.specfl & SP_PURE) && specthresh > FTINY &&
234 +                                        (specthresh >= 1.-FTINY ||
235 +                                specthresh + .05 - .1*frandom() > nd.tspec))
236 +                                nd.specfl |= SP_TBLT;
237                          if (r->crtype & SHADOW ||
238                                          DOT(r->pert,r->pert) <= FTINY*FTINY) {
239                                  VCOPY(nd.prdir, r->rdir);
240                                  transtest = 2;
241                          } else {
242                                  for (i = 0; i < 3; i++)         /* perturb */
243 <                                        nd.prdir[i] = r->rdir[i] -
244 <                                                        .75*r->pert[i];
245 <                                normalize(nd.prdir);
243 >                                        nd.prdir[i] = r->rdir[i] - r->pert[i];
244 >                                if (DOT(nd.prdir, r->ron) < -FTINY)
245 >                                        normalize(nd.prdir);    /* OK */
246 >                                else
247 >                                        VCOPY(nd.prdir, r->rdir);
248                          }
249                  }
250          } else
# Line 232 | Line 261 | register RAY  *r;
261                          transtest *= bright(lr.rcol);
262                          transdist = r->rot + lr.rt;
263                  }
264 <        }
264 >        } else
265 >                transtest = 0;
266  
267          if (r->crtype & SHADOW)                 /* the rest is shadow */
268                  return;
# Line 242 | Line 272 | register RAY  *r;
272          if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
273                  return;                         /* 100% pure specular */
274  
275 <        if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING)
275 >        if (r->ro != NULL && (r->ro->otype == OBJ_FACE ||
276 >                        r->ro->otype == OBJ_RING))
277                  nd.specfl |= SP_FLAT;
278  
279          if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE))
# Line 250 | Line 281 | register RAY  *r;
281  
282          if (nd.rdiff > FTINY) {         /* ambient from this side */
283                  ambient(ctmp, r);
284 <                scalecolor(ctmp, nd.rdiff);
284 >                if (nd.specfl & SP_RBLT)
285 >                        scalecolor(ctmp, 1.0-nd.trans);
286 >                else
287 >                        scalecolor(ctmp, nd.rdiff);
288                  multcolor(ctmp, nd.mcolor);     /* modified by material color */
289                  addcolor(r->rcol, ctmp);        /* add to returned color */
290          }
291          if (nd.tdiff > FTINY) {         /* ambient from other side */
292                  flipsurface(r);
293                  ambient(ctmp, r);
294 <                scalecolor(ctmp, nd.tdiff);
294 >                if (nd.specfl & SP_TBLT)
295 >                        scalecolor(ctmp, nd.trans);
296 >                else
297 >                        scalecolor(ctmp, nd.tdiff);
298                  multcolor(ctmp, nd.mcolor);     /* modified by color */
299                  addcolor(r->rcol, ctmp);
300                  flipsurface(r);
# Line 279 | Line 316 | register NORMDAT  *np;
316          FVECT  u, v, h;
317          double  rv[2];
318          double  d, sinp, cosp;
282        int  confuse;
319          register int  i;
320 +                                        /* quick test */
321 +        if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL &&
322 +                        (np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN)
323 +                return;
324                                          /* set up sample coordinates */
325          v[0] = v[1] = v[2] = 0.0;
326          for (i = 0; i < 3; i++)
# Line 291 | Line 331 | register NORMDAT  *np;
331          normalize(u);
332          fcross(v, np->pnorm, u);
333                                          /* compute reflection */
334 <        if (np->specfl & SP_REFL &&
334 >        if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
335                          rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
296                confuse = 0;
336                  dimlist[ndims++] = (int)np->mp;
337 <        refagain:
299 <                dimlist[ndims] = confuse += 3601;
300 <                d = urand(ilhash(dimlist,ndims+1)+samplendx);
337 >                d = urand(ilhash(dimlist,ndims)+samplendx);
338                  multisamp(rv, 2, d);
339                  d = 2.0*PI * rv[0];
340                  cosp = cos(d);
341                  sinp = sin(d);
342 +                rv[1] = 1.0 - specjitter*rv[1];
343                  if (rv[1] <= FTINY)
344                          d = 1.0;
345                  else
# Line 311 | Line 349 | register NORMDAT  *np;
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)      /* oops! */
353 <                        goto refagain;
352 >                if (DOT(sr.rdir, r->ron) <= FTINY)
353 >                        VCOPY(sr.rdir, np->vrefl);      /* jitter no good */
354                  rayvalue(&sr);
355                  multcolor(sr.rcol, np->scolor);
356                  addcolor(r->rcol, sr.rcol);
357                  ndims--;
358          }
359                                          /* compute transmission */
360 +        if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
361 +                        rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
362 +                dimlist[ndims++] = (int)np->mp;
363 +                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
364 +                multisamp(rv, 2, d);
365 +                d = 2.0*PI * rv[0];
366 +                cosp = cos(d);
367 +                sinp = sin(d);
368 +                rv[1] = 1.0 - specjitter*rv[1];
369 +                if (rv[1] <= FTINY)
370 +                        d = 1.0;
371 +                else
372 +                        d = sqrt( -log(rv[1]) * np->alpha2 );
373 +                for (i = 0; i < 3; i++)
374 +                        sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]);
375 +                if (DOT(sr.rdir, r->ron) < -FTINY)
376 +                        normalize(sr.rdir);             /* OK, normalize */
377 +                else
378 +                        VCOPY(sr.rdir, np->prdir);      /* else no jitter */
379 +                rayvalue(&sr);
380 +                scalecolor(sr.rcol, np->tspec);
381 +                multcolor(sr.rcol, np->mcolor);         /* modified by color */
382 +                addcolor(r->rcol, sr.rcol);
383 +                ndims--;
384 +        }
385   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines