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.16 by greg, Wed May 6 17:36:00 1992 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   /*
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 <        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 >        register int    i;
78          COLOR  ctmp;
79  
80          setcolor(cval, 0.0, 0.0, 0.0);
# Line 79 | Line 95 | double  omega;                 /* light source size */
95                  scalecolor(ctmp, dtmp);
96                  addcolor(cval, ctmp);
97          }
98 <        if (ldot > FTINY && np->rspec > FTINY && np->alpha2 > FTINY) {
98 >        if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) {
99                  /*
100                   *  Compute specular reflection coefficient using
101                   *  gaussian distribution model.
102                   */
103 <                                                /* roughness + source */
104 <                dtmp = np->alpha2 + omega/(2.0*PI);
103 >                                                /* roughness */
104 >                dtmp = np->alpha2;
105 >                                                /* + source if flat */
106 >                if (np->specfl & SP_FLAT)
107 >                        dtmp += omega/(4.0*PI);
108 >                                                /* delta */
109 >                for (i = 0; i < 3; i++)
110 >                        vtmp[i] = ldir[i] - np->rp->rdir[i];
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;
118 >                        dtmp *= omega * sqrt(ldot/np->pdot);
119                          scalecolor(ctmp, dtmp);
120                          addcolor(cval, ctmp);
121                  }
# Line 105 | Line 129 | double  omega;                 /* light source size */
129                  scalecolor(ctmp, dtmp);
130                  addcolor(cval, ctmp);
131          }
132 <        if (ldot < -FTINY && np->tspec > FTINY && np->alpha2 > FTINY) {
132 >        if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) {
133                  /*
134                   *  Compute specular transmission.  Specular transmission
135 <                 *  is unaffected by material color.
135 >                 *  is always modified by material color.
136                   */
137                                                  /* roughness + source */
138 <                dtmp = np->alpha2 + omega/(2.0*PI);
138 >                dtmp = np->alpha2/2.0 + omega/(2.0*PI);
139                                                  /* gaussian */
140 <                dtmp = exp((DOT(np->pr->rdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp;
140 >                dtmp = exp((DOT(np->prdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp;
141                                                  /* worth using? */
142                  if (dtmp > FTINY) {
143 <                        dtmp *= np->tspec * omega;
144 <                        setcolor(ctmp, dtmp, dtmp, dtmp);
143 >                        copycolor(ctmp, np->mcolor);
144 >                        dtmp *= np->tspec * omega * sqrt(ldot/np->pdot);
145 >                        scalecolor(ctmp, dtmp);
146                          addcolor(cval, ctmp);
147                  }
148          }
149   }
150  
151  
152 < m_normal(m, r)                  /* color a ray which hit something normal */
152 > m_normal(m, r)                  /* color a ray that hit something normal */
153   register OBJREC  *m;
154   register RAY  *r;
155   {
156          NORMDAT  nd;
157 <        double  ldot;
133 <        double  omega;
157 >        double  transtest, transdist;
158          double  dtmp;
159          COLOR  ctmp;
160          register int  i;
137
138        if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
139                objerror(m, USER, "bad # arguments");
161                                                  /* easy shadow test */
162          if (r->crtype & SHADOW && m->otype != MAT_TRANS)
163                  return;
164 +
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.pr = r;
168 >        nd.rp = r;
169                                                  /* get material color */
170          setcolor(nd.mcolor, m->oargs.farg[0],
171                             m->oargs.farg[1],
172                             m->oargs.farg[2]);
173                                                  /* get roughness */
174 +        nd.specfl = 0;
175          nd.alpha2 = m->oargs.farg[4];
176 <        nd.alpha2 *= 2.0 * nd.alpha2;
176 >        if ((nd.alpha2 *= nd.alpha2) <= FTINY)
177 >                nd.specfl |= SP_PURE;
178                                                  /* reorient if necessary */
179          if (r->rod < 0.0)
180                  flipsurface(r);
181                                                  /* get modifiers */
182          raytexture(r, m->omod);
183          nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
184 +        if (nd.pdot < .001)
185 +                nd.pdot = .001;                 /* non-zero for dirnorm() */
186          multcolor(nd.mcolor, r->pcol);          /* modify material color */
187 <        r->rt = r->rot;                         /* default ray length */
187 >        transtest = 0;
188                                                  /* get specular component */
189 <        nd.rspec = m->oargs.farg[3];
190 <
163 <        if (nd.rspec > FTINY) {                 /* has specular component */
189 >        if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
190 >                nd.specfl |= SP_REFL;
191                                                  /* compute specular color */
192                  if (m->otype == MAT_METAL)
193                          copycolor(nd.scolor, nd.mcolor);
# Line 172 | 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 > 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 (nd.alpha2 <= FTINY && !(r->crtype & SHADOW)) {
215 >                if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
216                          RAY  lr;
217                          if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
218                                  VCOPY(lr.rdir, nd.vrefl);
219                                  rayvalue(&lr);
220                                  multcolor(lr.rcol, nd.scolor);
221                                  addcolor(r->rcol, lr.rcol);
186                                if (nd.rspec > 0.5 && m->omod == OVOID)
187                                        r->rt = r->rot + lr.rt;
222                          }
223                  }
224          }
# Line 193 | Line 227 | register RAY  *r;
227                  nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec);
228                  nd.tspec = nd.trans * m->oargs.farg[6];
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 > 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 +                                                        0.5*r->pert[i];
245 +                                if (DOT(nd.prdir, r->ron) < -FTINY)
246 +                                        normalize(nd.prdir);    /* OK */
247 +                                else
248 +                                        VCOPY(nd.prdir, r->rdir);
249 +                        }
250 +                }
251          } else
252                  nd.tdiff = nd.tspec = nd.trans = 0.0;
253                                                  /* transmitted ray */
254 <        if (nd.tspec > FTINY && nd.alpha2 <= FTINY) {
254 >        if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
255                  RAY  lr;
256                  if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
257 <                        VCOPY(lr.rdir, r->rdir);
257 >                        VCOPY(lr.rdir, nd.prdir);
258                          rayvalue(&lr);
259                          scalecolor(lr.rcol, nd.tspec);
260 +                        multcolor(lr.rcol, nd.mcolor);  /* modified by color */
261                          addcolor(r->rcol, lr.rcol);
262 <                        if (nd.tspec > .5)
263 <                                r->rt = r->rot + lr.rt;
262 >                        transtest *= bright(lr.rcol);
263 >                        transdist = r->rot + lr.rt;
264                  }
265 <        }
265 >        } else
266 >                transtest = 0;
267 >
268          if (r->crtype & SHADOW)                 /* the rest is shadow */
269                  return;
270                                                  /* diffuse reflection */
271          nd.rdiff = 1.0 - nd.trans - nd.rspec;
272  
273 <        if (nd.rdiff <= FTINY && nd.tdiff <= FTINY && nd.alpha2 <= FTINY)
274 <                return;                         /* purely specular */
273 >        if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
274 >                return;                         /* 100% pure specular */
275  
276 +        if (r->ro != NULL && (r->ro->otype == OBJ_FACE ||
277 +                        r->ro->otype == OBJ_RING))
278 +                nd.specfl |= SP_FLAT;
279 +
280 +        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE))
281 +                gaussamp(r, &nd);
282 +
283          if (nd.rdiff > FTINY) {         /* ambient from this side */
284                  ambient(ctmp, r);
285 <                if (nd.alpha2 <= FTINY)
221 <                        scalecolor(ctmp, nd.rdiff);
222 <                else
285 >                if (nd.specfl & SP_RBLT)
286                          scalecolor(ctmp, 1.0-nd.trans);
287 +                else
288 +                        scalecolor(ctmp, nd.rdiff);
289                  multcolor(ctmp, nd.mcolor);     /* modified by material color */
290                  addcolor(r->rcol, ctmp);        /* add to returned color */
291          }
292          if (nd.tdiff > FTINY) {         /* ambient from other side */
293                  flipsurface(r);
294                  ambient(ctmp, r);
295 <                if (nd.alpha2 <= FTINY)
231 <                        scalecolor(ctmp, nd.tdiff);
232 <                else
295 >                if (nd.specfl & SP_TBLT)
296                          scalecolor(ctmp, nd.trans);
297 <                multcolor(ctmp, nd.mcolor);
297 >                else
298 >                        scalecolor(ctmp, nd.tdiff);
299 >                multcolor(ctmp, nd.mcolor);     /* modified by color */
300                  addcolor(r->rcol, ctmp);
301                  flipsurface(r);
302          }
303                                          /* add direct component */
304          direct(r, dirnorm, &nd);
305 +                                        /* check distance */
306 +        if (transtest > bright(r->rcol))
307 +                r->rt = transdist;
308 + }
309 +
310 +
311 + static
312 + gaussamp(r, np)                 /* sample gaussian specular */
313 + RAY  *r;
314 + register NORMDAT  *np;
315 + {
316 +        RAY  sr;
317 +        FVECT  u, v, h;
318 +        double  rv[2];
319 +        double  d, sinp, cosp;
320 +        register int  i;
321 +                                        /* quick test */
322 +        if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL &&
323 +                        (np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN)
324 +                return;
325 +                                        /* set up sample coordinates */
326 +        v[0] = v[1] = v[2] = 0.0;
327 +        for (i = 0; i < 3; i++)
328 +                if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6)
329 +                        break;
330 +        v[i] = 1.0;
331 +        fcross(u, v, np->pnorm);
332 +        normalize(u);
333 +        fcross(v, np->pnorm, u);
334 +                                        /* compute reflection */
335 +        if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
336 +                        rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
337 +                dimlist[ndims++] = (int)np->mp;
338 +                d = urand(ilhash(dimlist,ndims)+samplendx);
339 +                multisamp(rv, 2, d);
340 +                d = 2.0*PI * rv[0];
341 +                cosp = cos(d);
342 +                sinp = sin(d);
343 +                rv[1] = 1.0 - specjitter*rv[1];
344 +                if (rv[1] <= FTINY)
345 +                        d = 1.0;
346 +                else
347 +                        d = sqrt( np->alpha2 * -log(rv[1]) );
348 +                for (i = 0; i < 3; i++)
349 +                        h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
350 +                d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
351 +                for (i = 0; i < 3; i++)
352 +                        sr.rdir[i] = r->rdir[i] + d*h[i];
353 +                if (DOT(sr.rdir, r->ron) <= FTINY)
354 +                        VCOPY(sr.rdir, np->vrefl);      /* jitter no good */
355 +                rayvalue(&sr);
356 +                multcolor(sr.rcol, np->scolor);
357 +                addcolor(r->rcol, sr.rcol);
358 +                ndims--;
359 +        }
360 +                                        /* compute transmission */
361 +        if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
362 +                        rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
363 +                dimlist[ndims++] = (int)np->mp;
364 +                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
365 +                multisamp(rv, 2, d);
366 +                d = 2.0*PI * rv[0];
367 +                cosp = cos(d);
368 +                sinp = sin(d);
369 +                rv[1] = 1.0 - specjitter*rv[1];
370 +                if (rv[1] <= FTINY)
371 +                        d = 1.0;
372 +                else
373 +                        d = sqrt( np->alpha2/4.0 * -log(rv[1]) );
374 +                for (i = 0; i < 3; i++)
375 +                        sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]);
376 +                if (DOT(sr.rdir, r->ron) < -FTINY)
377 +                        normalize(sr.rdir);             /* OK, normalize */
378 +                else
379 +                        VCOPY(sr.rdir, np->prdir);      /* else no jitter */
380 +                rayvalue(&sr);
381 +                scalecolor(sr.rcol, np->tspec);
382 +                multcolor(sr.rcol, np->mcolor);         /* modified by color */
383 +                addcolor(r->rcol, sr.rcol);
384 +                ndims--;
385 +        }
386   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines