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.7 by greg, Mon Oct 15 20:39:35 1990 UTC vs.
Revision 2.5 by greg, Tue Jan 14 16:16:45 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        010             /* purely specular (zero roughness) */
47 > #define  SP_FLAT        020             /* flat reflecting surface */
48 > #define  SP_RBLT        040             /* reflection below sample threshold */
49 > #define  SP_TBLT        0100            /* transmission below threshold */
50  
51   typedef struct {
52          OBJREC  *mp;            /* material pointer */
53 <        RAY  *pr;               /* intersected ray */
53 >        short  specfl;          /* specularity flags, defined above */
54          COLOR  mcolor;          /* color of this material */
55          COLOR  scolor;          /* color of specular component */
56          FVECT  vrefl;           /* vector in direction of reflected ray */
57 <        double  alpha2;         /* roughness squared times 2 */
57 >        FVECT  prdir;           /* vector in transmitted direction */
58 >        double  alpha2;         /* roughness squared */
59          double  rdiff, rspec;   /* reflected specular, diffuse */
60          double  trans;          /* transmissivity */
61          double  tdiff, tspec;   /* transmitted specular, diffuse */
# Line 59 | Line 72 | double  omega;                 /* light source size */
72   {
73          double  ldot;
74          double  dtmp;
75 +        int     i;
76          COLOR  ctmp;
77  
78          setcolor(cval, 0.0, 0.0, 0.0);
# Line 79 | Line 93 | double  omega;                 /* light source size */
93                  scalecolor(ctmp, dtmp);
94                  addcolor(cval, ctmp);
95          }
96 <        if (ldot > FTINY && np->rspec > FTINY && np->alpha2 > FTINY) {
96 >        if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) {
97                  /*
98                   *  Compute specular reflection coefficient using
99                   *  gaussian distribution model.
100                   */
101 <                                                /* roughness + source */
102 <                dtmp = np->alpha2 + omega/(2.0*PI);
101 >                                                /* roughness */
102 >                dtmp = 2.0*np->alpha2;
103 >                                                /* + source if flat */
104 >                if (np->specfl & SP_FLAT)
105 >                        dtmp += omega/(2.0*PI);
106                                                  /* gaussian */
107                  dtmp = exp((DOT(np->vrefl,ldir)-1.)/dtmp)/(2.*PI)/dtmp;
108                                                  /* worth using? */
109                  if (dtmp > FTINY) {
110                          copycolor(ctmp, np->scolor);
111 <                        dtmp *= omega;
111 >                        dtmp *= omega / np->pdot;
112                          scalecolor(ctmp, dtmp);
113                          addcolor(cval, ctmp);
114                  }
# Line 105 | Line 122 | double  omega;                 /* light source size */
122                  scalecolor(ctmp, dtmp);
123                  addcolor(cval, ctmp);
124          }
125 <        if (ldot < -FTINY && np->tspec > FTINY && np->alpha2 > FTINY) {
125 >        if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) {
126                  /*
127                   *  Compute specular transmission.  Specular transmission
128 <                 *  is unaffected by material color.
128 >                 *  is always modified by material color.
129                   */
130                                                  /* roughness + source */
131                  dtmp = np->alpha2 + omega/(2.0*PI);
132                                                  /* gaussian */
133 <                dtmp = exp((DOT(np->pr->rdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp;
133 >                dtmp = exp((DOT(np->prdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp;
134                                                  /* worth using? */
135                  if (dtmp > FTINY) {
136 <                        dtmp *= np->tspec * omega;
137 <                        setcolor(ctmp, dtmp, dtmp, dtmp);
136 >                        copycolor(ctmp, np->mcolor);
137 >                        dtmp *= np->tspec * omega / np->pdot;
138 >                        scalecolor(ctmp, dtmp);
139                          addcolor(cval, ctmp);
140                  }
141          }
142   }
143  
144  
145 < m_normal(m, r)                  /* color a ray which hit something normal */
145 > m_normal(m, r)                  /* color a ray that hit something normal */
146   register OBJREC  *m;
147   register RAY  *r;
148   {
149          NORMDAT  nd;
150 +        double  transtest, transdist;
151          double  dtmp;
152          COLOR  ctmp;
153          register int  i;
135
136        if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
137                objerror(m, USER, "bad # arguments");
154                                                  /* easy shadow test */
155          if (r->crtype & SHADOW && m->otype != MAT_TRANS)
156                  return;
157 +
158 +        if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
159 +                objerror(m, USER, "bad number of arguments");
160          nd.mp = m;
142        nd.pr = r;
161                                                  /* get material color */
162          setcolor(nd.mcolor, m->oargs.farg[0],
163                             m->oargs.farg[1],
164                             m->oargs.farg[2]);
165                                                  /* get roughness */
166 +        nd.specfl = 0;
167          nd.alpha2 = m->oargs.farg[4];
168 <        nd.alpha2 *= 2.0 * nd.alpha2;
168 >        if ((nd.alpha2 *= nd.alpha2) <= FTINY)
169 >                nd.specfl |= SP_PURE;
170                                                  /* reorient if necessary */
171          if (r->rod < 0.0)
172                  flipsurface(r);
173                                                  /* get modifiers */
174          raytexture(r, m->omod);
175          nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
176 +        if (nd.pdot < .001)
177 +                nd.pdot = .001;                 /* non-zero for dirnorm() */
178          multcolor(nd.mcolor, r->pcol);          /* modify material color */
179 <        r->rt = r->rot;                         /* default ray length */
179 >        transtest = 0;
180                                                  /* get specular component */
181 <        nd.rspec = m->oargs.farg[3];
182 <
161 <        if (nd.rspec > FTINY) {                 /* has specular component */
181 >        if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
182 >                nd.specfl |= SP_REFL;
183                                                  /* compute specular color */
184                  if (m->otype == MAT_METAL)
185                          copycolor(nd.scolor, nd.mcolor);
# Line 170 | Line 191 | register RAY  *r;
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;
194 +                                                /* check threshold */
195 +                if (nd.rspec <= specthresh+FTINY)
196 +                        nd.specfl |= SP_RBLT;
197                                                  /* compute reflected ray */
198                  for (i = 0; i < 3; i++)
199                          nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
200  
201 <                if (nd.alpha2 <= FTINY && !(r->crtype & SHADOW)) {
201 >                if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
202                          RAY  lr;
203                          if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
204                                  VCOPY(lr.rdir, nd.vrefl);
# Line 189 | Line 213 | register RAY  *r;
213                  nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec);
214                  nd.tspec = nd.trans * m->oargs.farg[6];
215                  nd.tdiff = nd.trans - nd.tspec;
216 +                if (nd.tspec > FTINY) {
217 +                        nd.specfl |= SP_TRAN;
218 +                                                        /* check threshold */
219 +                        if (nd.tspec <= specthresh+FTINY)
220 +                                nd.specfl |= SP_TBLT;
221 +                        if (r->crtype & SHADOW ||
222 +                                        DOT(r->pert,r->pert) <= FTINY*FTINY) {
223 +                                VCOPY(nd.prdir, r->rdir);
224 +                                transtest = 2;
225 +                        } else {
226 +                                for (i = 0; i < 3; i++)         /* perturb */
227 +                                        nd.prdir[i] = r->rdir[i] -
228 +                                                        .75*r->pert[i];
229 +                                normalize(nd.prdir);
230 +                        }
231 +                }
232          } else
233                  nd.tdiff = nd.tspec = nd.trans = 0.0;
234                                                  /* transmitted ray */
235 <        if (nd.tspec > FTINY && nd.alpha2 <= FTINY) {
235 >        if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
236                  RAY  lr;
237                  if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
238 <                        VCOPY(lr.rdir, r->rdir);
238 >                        VCOPY(lr.rdir, nd.prdir);
239                          rayvalue(&lr);
240                          scalecolor(lr.rcol, nd.tspec);
241 +                        multcolor(lr.rcol, nd.mcolor);  /* modified by color */
242                          addcolor(r->rcol, lr.rcol);
243 <                        if (nd.tspec > .5)
244 <                                r->rt = r->rot + lr.rt;
243 >                        transtest *= bright(lr.rcol);
244 >                        transdist = r->rot + lr.rt;
245                  }
246          }
247 +
248          if (r->crtype & SHADOW)                 /* the rest is shadow */
249                  return;
250                                                  /* diffuse reflection */
251          nd.rdiff = 1.0 - nd.trans - nd.rspec;
252  
253 <        if (nd.rdiff <= FTINY && nd.tdiff <= FTINY && nd.alpha2 <= FTINY)
254 <                return;                         /* purely specular */
253 >        if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
254 >                return;                         /* 100% pure specular */
255  
256 +        if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING)
257 +                nd.specfl |= SP_FLAT;
258 +
259 +        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE))
260 +                gaussamp(r, &nd);
261 +
262          if (nd.rdiff > FTINY) {         /* ambient from this side */
263                  ambient(ctmp, r);
264 <                if (nd.alpha2 <= FTINY)
217 <                        scalecolor(ctmp, nd.rdiff);
218 <                else
264 >                if (nd.specfl & SP_RBLT)
265                          scalecolor(ctmp, 1.0-nd.trans);
266 +                else
267 +                        scalecolor(ctmp, nd.rdiff);
268                  multcolor(ctmp, nd.mcolor);     /* modified by material color */
269                  addcolor(r->rcol, ctmp);        /* add to returned color */
270          }
271          if (nd.tdiff > FTINY) {         /* ambient from other side */
272                  flipsurface(r);
273                  ambient(ctmp, r);
274 <                if (nd.alpha2 <= FTINY)
227 <                        scalecolor(ctmp, nd.tdiff);
228 <                else
274 >                if (nd.specfl & SP_TBLT)
275                          scalecolor(ctmp, nd.trans);
276 <                multcolor(ctmp, nd.mcolor);
276 >                else
277 >                        scalecolor(ctmp, nd.tdiff);
278 >                multcolor(ctmp, nd.mcolor);     /* modified by color */
279                  addcolor(r->rcol, ctmp);
280                  flipsurface(r);
281          }
282                                          /* add direct component */
283          direct(r, dirnorm, &nd);
284 +                                        /* check distance */
285 +        if (transtest > bright(r->rcol))
286 +                r->rt = transdist;
287 + }
288 +
289 +
290 + static
291 + gaussamp(r, np)                 /* sample gaussian specular */
292 + RAY  *r;
293 + register NORMDAT  *np;
294 + {
295 +        RAY  sr;
296 +        FVECT  u, v, h;
297 +        double  rv[2];
298 +        double  d, sinp, cosp;
299 +        int  ntries;
300 +        register int  i;
301 +                                        /* set up sample coordinates */
302 +        v[0] = v[1] = v[2] = 0.0;
303 +        for (i = 0; i < 3; i++)
304 +                if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6)
305 +                        break;
306 +        v[i] = 1.0;
307 +        fcross(u, v, np->pnorm);
308 +        normalize(u);
309 +        fcross(v, np->pnorm, u);
310 +                                        /* compute reflection */
311 +        if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
312 +                        rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
313 +                dimlist[ndims++] = (int)np->mp;
314 +                for (ntries = 0; ntries < 10; ntries++) {
315 +                        dimlist[ndims] = ntries * 8912;
316 +                        d = urand(ilhash(dimlist,ndims+1)+samplendx);
317 +                        multisamp(rv, 2, d);
318 +                        d = 2.0*PI * rv[0];
319 +                        cosp = cos(d);
320 +                        sinp = sin(d);
321 +                        rv[1] = 1.0 - specjitter*rv[1];
322 +                        if (rv[1] <= FTINY)
323 +                                d = 1.0;
324 +                        else
325 +                                d = sqrt( np->alpha2 * -log(rv[1]) );
326 +                        for (i = 0; i < 3; i++)
327 +                                h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
328 +                        d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
329 +                        for (i = 0; i < 3; i++)
330 +                                sr.rdir[i] = r->rdir[i] + d*h[i];
331 +                        if (DOT(sr.rdir, r->ron) > FTINY) {
332 +                                rayvalue(&sr);
333 +                                multcolor(sr.rcol, np->scolor);
334 +                                addcolor(r->rcol, sr.rcol);
335 +                                break;
336 +                        }
337 +                }
338 +                ndims--;
339 +        }
340 +                                        /* compute transmission */
341   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines