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.1 by greg, Thu Feb 2 10:41:30 1989 UTC vs.
Revision 2.26 by greg, Tue Aug 24 12:59:28 1993 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  
18 #include  "source.h"
19
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 + static  gaussamp();
27 +
28   /*
29 < *      This routine uses portions of the reflection
30 < *  model described by Cook and Torrance.
25 < *      The computation of specular components has been simplified by
26 < *  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 34 | Line 38 | static char SCCSid[] = "$SunId$ LBL";
38   *      red     grn     blu     rspec   rough   trans   tspec
39   */
40  
41 < #define  BSPEC(m)       (6.0)           /* specularity parameter b */
41 >                                /* specularity flags */
42 > #define  SP_REFL        01              /* has reflected specular component */
43 > #define  SP_TRAN        02              /* has transmitted specular */
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 <
50 < m_normal(m, r)                  /* color a ray which hit something normal */
51 < register OBJREC  *m;
52 < register RAY  *r;
43 < {
44 <        double  exp();
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 */
55          FVECT  vrefl;           /* vector in direction of reflected ray */
56 <        double  alpha2;         /* roughness squared times 2 */
57 <        RAY  lr;                /* ray to illumination source */
56 >        FVECT  prdir;           /* vector in transmitted direction */
57 >        double  alpha2;         /* roughness squared */
58          double  rdiff, rspec;   /* reflected specular, diffuse */
59          double  trans;          /* transmissivity */
60          double  tdiff, tspec;   /* transmitted specular, diffuse */
61          FVECT  pnorm;           /* perturbed surface normal */
62          double  pdot;           /* perturbed dot product */
63 + }  NORMDAT;             /* normal material data */
64 +
65 +
66 + dirnorm(cval, np, ldir, omega)          /* compute source contribution */
67 + COLOR  cval;                    /* returned coefficient */
68 + register NORMDAT  *np;          /* material data */
69 + FVECT  ldir;                    /* light source direction */
70 + double  omega;                  /* light source size */
71 + {
72          double  ldot;
73 <        double  omega;
74 <        double  dtmp;
73 >        double  dtmp, d2;
74 >        FVECT  vtmp;
75          COLOR  ctmp;
59        register int  i;
76  
77 <        if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
78 <                objerror(m, USER, "bad # arguments");
77 >        setcolor(cval, 0.0, 0.0, 0.0);
78 >
79 >        ldot = DOT(np->pnorm, ldir);
80 >
81 >        if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY)
82 >                return;         /* wrong side */
83 >
84 >        if (ldot > FTINY && np->rdiff > FTINY) {
85 >                /*
86 >                 *  Compute and add diffuse reflected component to returned
87 >                 *  color.  The diffuse reflected component will always be
88 >                 *  modified by the color of the material.
89 >                 */
90 >                copycolor(ctmp, np->mcolor);
91 >                dtmp = ldot * omega * np->rdiff / PI;
92 >                scalecolor(ctmp, dtmp);
93 >                addcolor(cval, ctmp);
94 >        }
95 >        if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) {
96 >                /*
97 >                 *  Compute specular reflection coefficient using
98 >                 *  gaussian distribution model.
99 >                 */
100 >                                                /* roughness */
101 >                dtmp = np->alpha2;
102 >                                                /* + source if flat */
103 >                if (np->specfl & SP_FLAT)
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(-d2/dtmp)/(4.*PI*dtmp);
114 >                                                /* worth using? */
115 >                if (dtmp > FTINY) {
116 >                        copycolor(ctmp, np->scolor);
117 >                        dtmp *= omega * sqrt(ldot/np->pdot);
118 >                        scalecolor(ctmp, dtmp);
119 >                        addcolor(cval, ctmp);
120 >                }
121 >        }
122 >        if (ldot < -FTINY && np->tdiff > FTINY) {
123 >                /*
124 >                 *  Compute diffuse transmission.
125 >                 */
126 >                copycolor(ctmp, np->mcolor);
127 >                dtmp = -ldot * omega * np->tdiff / PI;
128 >                scalecolor(ctmp, dtmp);
129 >                addcolor(cval, ctmp);
130 >        }
131 >        if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) {
132 >                /*
133 >                 *  Compute specular transmission.  Specular transmission
134 >                 *  is always modified by material color.
135 >                 */
136 >                                                /* roughness + source */
137 >                dtmp = np->alpha2 + omega/PI;
138 >                                                /* gaussian */
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 * sqrt(-ldot/np->pdot);
144 >                        scalecolor(ctmp, dtmp);
145 >                        addcolor(cval, ctmp);
146 >                }
147 >        }
148 > }
149 >
150 >
151 > m_normal(m, r)                  /* color a ray that hit something normal */
152 > register OBJREC  *m;
153 > register RAY  *r;
154 > {
155 >        NORMDAT  nd;
156 >        double  transtest, transdist;
157 >        COLOR  ctmp;
158 >        register int  i;
159                                                  /* easy shadow test */
160          if (r->crtype & SHADOW && m->otype != MAT_TRANS)
161                  return;
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(mcolor, m->oargs.farg[0],
168 >        setcolor(nd.mcolor, m->oargs.farg[0],
169                             m->oargs.farg[1],
170                             m->oargs.farg[2]);
171                                                  /* get roughness */
172 <        alpha2 = m->oargs.farg[4];
173 <        alpha2 *= 2.0 * alpha2;
172 >        nd.specfl = 0;
173 >        nd.alpha2 = m->oargs.farg[4];
174 >        if ((nd.alpha2 *= nd.alpha2) <= FTINY)
175 >                nd.specfl |= SP_PURE;
176                                                  /* reorient if necessary */
177          if (r->rod < 0.0)
178                  flipsurface(r);
179                                                  /* get modifiers */
180          raytexture(r, m->omod);
181 <        pdot = raynormal(pnorm, r);             /* perturb normal */
182 <        multcolor(mcolor, r->pcol);             /* modify material color */
181 >        nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
182 >        if (nd.pdot < .001)
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 <        rspec = m->oargs.farg[3];
189 <
83 <        if (rspec > FTINY) {                    /* has specular component */
188 >        if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
189 >                nd.specfl |= SP_REFL;
190                                                  /* compute specular color */
191                  if (m->otype == MAT_METAL)
192 <                        copycolor(scolor, mcolor);
192 >                        copycolor(nd.scolor, nd.mcolor);
193                  else
194 <                        setcolor(scolor, 1.0, 1.0, 1.0);
195 <                scalecolor(scolor, rspec);
196 <                                                /* improved model */
197 <                dtmp = exp(-BSPEC(m)*pdot);
198 <                for (i = 0; i < 3; i++)
93 <                        colval(scolor,i) += (1.0-colval(scolor,i))*dtmp;
94 <                rspec += (1.0-rspec)*dtmp;
194 >                        setcolor(nd.scolor, 1.0, 1.0, 1.0);
195 >                scalecolor(nd.scolor, nd.rspec);
196 >                                                /* check threshold */
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 <                        vrefl[i] = r->rdir[i] + 2.0*pdot*pnorm[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 (alpha2 <= FTINY && !(r->crtype & SHADOW))
207 <                        if (rayorigin(&lr, r, REFLECTED, rspec) == 0) {
208 <                                VCOPY(lr.rdir, vrefl);
206 >                if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
207 >                        RAY  lr;
208 >                        if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
209 >                                VCOPY(lr.rdir, nd.vrefl);
210                                  rayvalue(&lr);
211 <                                multcolor(lr.rcol, scolor);
211 >                                multcolor(lr.rcol, nd.scolor);
212                                  addcolor(r->rcol, lr.rcol);
213                          }
214 +                }
215          }
216 <
216 >                                                /* compute transmission */
217          if (m->otype == MAT_TRANS) {
218 <                trans = m->oargs.farg[5]*(1.0 - rspec);
219 <                tspec = trans * m->oargs.farg[6];
220 <                tdiff = trans - tspec;
218 >                nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec);
219 >                nd.tspec = nd.trans * m->oargs.farg[6];
220 >                nd.tdiff = nd.trans - nd.tspec;
221 >                if (nd.tspec > FTINY) {
222 >                        nd.specfl |= SP_TRAN;
223 >                                                        /* check threshold */
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) {
229 >                                VCOPY(nd.prdir, r->rdir);
230 >                                transtest = 2;
231 >                        } else {
232 >                                for (i = 0; i < 3; i++)         /* perturb */
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
241 <                tdiff = tspec = trans = 0.0;
241 >                nd.tdiff = nd.tspec = nd.trans = 0.0;
242                                                  /* transmitted ray */
243 <        if (tspec > FTINY && alpha2 <= FTINY)
244 <                if (rayorigin(&lr, r, TRANS, tspec) == 0) {
245 <                        VCOPY(lr.rdir, r->rdir);
243 >        if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
244 >                RAY  lr;
245 >                if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
246 >                        VCOPY(lr.rdir, nd.prdir);
247                          rayvalue(&lr);
248 <                        scalecolor(lr.rcol, tspec);
248 >                        scalecolor(lr.rcol, nd.tspec);
249 >                        multcolor(lr.rcol, nd.mcolor);  /* modified by color */
250                          addcolor(r->rcol, lr.rcol);
251 +                        transtest *= bright(lr.rcol);
252 +                        transdist = r->rot + lr.rt;
253                  }
254 +        } else
255 +                transtest = 0;
256 +
257          if (r->crtype & SHADOW)                 /* the rest is shadow */
258                  return;
259                                                  /* diffuse reflection */
260 <        rdiff = 1.0 - trans - rspec;
260 >        nd.rdiff = 1.0 - nd.trans - nd.rspec;
261  
262 <        if (rdiff <= FTINY && tdiff <= FTINY && alpha2 <= FTINY)
263 <                return;                         /* purely specular */
262 >        if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
263 >                return;                         /* 100% pure specular */
264  
265 <        ambient(ctmp, r);               /* compute ambient component */
266 <        scalecolor(ctmp, 1.0-trans);    /* from this side */
267 <        multcolor(ctmp, mcolor);        /* modified by material color */
133 <        addcolor(r->rcol, ctmp);        /* add to returned color */
265 >        if (r->ro != NULL && (r->ro->otype == OBJ_FACE ||
266 >                        r->ro->otype == OBJ_RING))
267 >                nd.specfl |= SP_FLAT;
268  
269 <        if (trans > FTINY) {            /* ambient from other side */
269 >        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE))
270 >                gaussamp(r, &nd);
271 >
272 >        if (nd.rdiff > FTINY) {         /* ambient from this side */
273 >                ambient(ctmp, r);
274 >                if (nd.specfl & SP_RBLT)
275 >                        scalecolor(ctmp, 1.0-nd.trans);
276 >                else
277 >                        scalecolor(ctmp, nd.rdiff);
278 >                multcolor(ctmp, nd.mcolor);     /* modified by material color */
279 >                addcolor(r->rcol, ctmp);        /* add to returned color */
280 >        }
281 >        if (nd.tdiff > FTINY) {         /* ambient from other side */
282                  flipsurface(r);
283 <                scalecolor(ctmp, trans);
284 <                multcolor(ctmp, mcolor);
283 >                ambient(ctmp, r);
284 >                if (nd.specfl & SP_TBLT)
285 >                        scalecolor(ctmp, nd.trans);
286 >                else
287 >                        scalecolor(ctmp, nd.tdiff);
288 >                multcolor(ctmp, nd.mcolor);     /* modified by color */
289                  addcolor(r->rcol, ctmp);
290                  flipsurface(r);
291          }
292 <        
293 <        for (i = 0; i < nsources; i++) {        /* add specular and diffuse */
292 >                                        /* add direct component */
293 >        direct(r, dirnorm, &nd);
294 >                                        /* check distance */
295 >        if (transtest > bright(r->rcol))
296 >                r->rt = transdist;
297 > }
298  
145                if ((omega = srcray(&lr, r, i)) == 0.0)
146                        continue;               /* bad source */
299  
300 <                ldot = DOT(pnorm, lr.rdir);
301 <        
302 <                if (ldot < 0.0 ? trans <= FTINY : trans >= 1.0-FTINY)
303 <                        continue;               /* wrong side */
304 <        
305 <                rayvalue(&lr);                  /* compute light ray value */
306 <        
307 <                if (intens(lr.rcol) <= FTINY)
308 <                        continue;               /* didn't hit light source */
309 <
310 <                if (ldot > FTINY && rdiff > FTINY) {
311 <                        /*
312 <                         *  Compute and add diffuse component to returned color.
313 <                         *  The diffuse component will always be modified by the
314 <                         *  color of the material.
315 <                         */
316 <                        copycolor(ctmp, lr.rcol);
317 <                        dtmp = ldot * omega * rdiff / PI;
318 <                        scalecolor(ctmp, dtmp);
319 <                        multcolor(ctmp, mcolor);
320 <                        addcolor(r->rcol, ctmp);
321 <                }
322 <                if (ldot > FTINY && rspec > FTINY && alpha2 > FTINY) {
323 <                        /*
324 <                         *  Compute specular reflection coefficient using
325 <                         *  gaussian distribution model.
326 <                         */
327 <                                                        /* roughness + source */
328 <                        dtmp = alpha2 + omega/(2.0*PI);
329 <                                                        /* gaussian */
330 <                        dtmp = exp((DOT(vrefl,lr.rdir)-1.)/dtmp)/(2.*PI)/dtmp;
331 <                                                        /* worth using? */
332 <                        if (dtmp > FTINY) {
333 <                                copycolor(ctmp, lr.rcol);
334 <                                dtmp *= omega;
335 <                                scalecolor(ctmp, dtmp);
336 <                                multcolor(ctmp, scolor);
337 <                                addcolor(r->rcol, ctmp);
338 <                        }
339 <                }
340 <                if (ldot < -FTINY && tdiff > FTINY) {
341 <                        /*
342 <                         *  Compute diffuse transmission.
343 <                         */
344 <                        copycolor(ctmp, lr.rcol);
345 <                        dtmp = -ldot * omega * tdiff / PI;
346 <                        scalecolor(ctmp, dtmp);
347 <                        multcolor(ctmp, mcolor);
348 <                        addcolor(r->rcol, ctmp);
349 <                }
350 <                if (ldot < -FTINY && tspec > FTINY && alpha2 > FTINY) {
351 <                        /*
352 <                         *  Compute specular transmission.
353 <                         */
354 <                                                        /* roughness + source */
355 <                        dtmp = alpha2 + omega/(2.0*PI);
356 <                                                        /* gaussian */
357 <                        dtmp = exp((DOT(r->rdir,lr.rdir)-1.)/dtmp)/(2.*PI)/dtmp;
358 <                                                        /* worth using? */
359 <                        if (dtmp > FTINY) {
360 <                                copycolor(ctmp, lr.rcol);
361 <                                dtmp *= tspec * omega;
362 <                                scalecolor(ctmp, dtmp);
363 <                                addcolor(r->rcol, ctmp);
364 <                        }
365 <                }
300 > static
301 > gaussamp(r, np)                 /* sample gaussian specular */
302 > RAY  *r;
303 > register NORMDAT  *np;
304 > {
305 >        RAY  sr;
306 >        FVECT  u, v, h;
307 >        double  rv[2];
308 >        double  d, sinp, cosp;
309 >        register int  i;
310 >                                        /* quick test */
311 >        if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL &&
312 >                        (np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN)
313 >                return;
314 >                                        /* set up sample coordinates */
315 >        v[0] = v[1] = v[2] = 0.0;
316 >        for (i = 0; i < 3; i++)
317 >                if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6)
318 >                        break;
319 >        v[i] = 1.0;
320 >        fcross(u, v, np->pnorm);
321 >        normalize(u);
322 >        fcross(v, np->pnorm, u);
323 >                                        /* compute reflection */
324 >        if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
325 >                        rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
326 >                dimlist[ndims++] = (int)np->mp;
327 >                d = urand(ilhash(dimlist,ndims)+samplendx);
328 >                multisamp(rv, 2, d);
329 >                d = 2.0*PI * rv[0];
330 >                cosp = cos(d);
331 >                sinp = sin(d);
332 >                rv[1] = 1.0 - specjitter*rv[1];
333 >                if (rv[1] <= FTINY)
334 >                        d = 1.0;
335 >                else
336 >                        d = sqrt( np->alpha2 * -log(rv[1]) );
337 >                for (i = 0; i < 3; i++)
338 >                        h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
339 >                d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
340 >                for (i = 0; i < 3; i++)
341 >                        sr.rdir[i] = r->rdir[i] + d*h[i];
342 >                if (DOT(sr.rdir, r->ron) <= FTINY)
343 >                        VCOPY(sr.rdir, np->vrefl);      /* jitter no good */
344 >                rayvalue(&sr);
345 >                multcolor(sr.rcol, np->scolor);
346 >                addcolor(r->rcol, sr.rcol);
347 >                ndims--;
348 >        }
349 >                                        /* compute transmission */
350 >        if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
351 >                        rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
352 >                dimlist[ndims++] = (int)np->mp;
353 >                d = urand(ilhash(dimlist,ndims)+1823+samplendx);
354 >                multisamp(rv, 2, d);
355 >                d = 2.0*PI * rv[0];
356 >                cosp = cos(d);
357 >                sinp = sin(d);
358 >                rv[1] = 1.0 - specjitter*rv[1];
359 >                if (rv[1] <= FTINY)
360 >                        d = 1.0;
361 >                else
362 >                        d = sqrt( -log(rv[1]) * np->alpha2 );
363 >                for (i = 0; i < 3; i++)
364 >                        sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]);
365 >                if (DOT(sr.rdir, r->ron) < -FTINY)
366 >                        normalize(sr.rdir);             /* OK, normalize */
367 >                else
368 >                        VCOPY(sr.rdir, np->prdir);      /* else no jitter */
369 >                rayvalue(&sr);
370 >                scalecolor(sr.rcol, np->tspec);
371 >                multcolor(sr.rcol, np->mcolor);         /* modified by color */
372 >                addcolor(r->rcol, sr.rcol);
373 >                ndims--;
374          }
375   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines