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.2 by greg, Sat Apr 15 12:23:22 1989 UTC vs.
Revision 2.23 by greg, Fri Feb 12 10:41:02 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   /*
27 < *      This routine uses portions of the reflection
28 < *  model described by Cook and Torrance.
25 < *      The computation of specular components has been simplified by
26 < *  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 36 | Line 38 | static char SCCSid[] = "$SunId$ LBL";
38  
39   #define  BSPEC(m)       (6.0)           /* specularity parameter b */
40  
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 < m_normal(m, r)                  /* color a ray which hit something normal */
50 < register OBJREC  *m;
51 < register RAY  *r;
52 < {
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;
73 >        double  dtmp, d2;
74 >        FVECT  vtmp;
75 >        COLOR  ctmp;
76 >
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          double  dtmp;
158          COLOR  ctmp;
159          register int  i;
60
61        if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
62                objerror(m, USER, "bad # arguments");
160                                                  /* easy shadow test */
161          if (r->crtype & SHADOW && m->otype != MAT_TRANS)
162                  return;
163 +
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(mcolor, m->oargs.farg[0],
169 >        setcolor(nd.mcolor, m->oargs.farg[0],
170                             m->oargs.farg[1],
171                             m->oargs.farg[2]);
172                                                  /* get roughness */
173 <        alpha2 = m->oargs.farg[4];
174 <        alpha2 *= 2.0 * alpha2;
173 >        nd.specfl = 0;
174 >        nd.alpha2 = m->oargs.farg[4];
175 >        if ((nd.alpha2 *= nd.alpha2) <= FTINY)
176 >                nd.specfl |= SP_PURE;
177                                                  /* reorient if necessary */
178          if (r->rod < 0.0)
179                  flipsurface(r);
180                                                  /* get modifiers */
181          raytexture(r, m->omod);
182 <        pdot = raynormal(pnorm, r);             /* perturb normal */
183 <        multcolor(mcolor, r->pcol);             /* modify material color */
182 >        nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
183 >        if (nd.pdot < .001)
184 >                nd.pdot = .001;                 /* non-zero for dirnorm() */
185 >        multcolor(nd.mcolor, r->pcol);          /* modify material color */
186 >        transtest = 0;
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);
194 >                        setcolor(nd.scolor, 1.0, 1.0, 1.0);
195 >                scalecolor(nd.scolor, nd.rspec);
196                                                  /* improved model */
197 <                dtmp = exp(-BSPEC(m)*pdot);
197 >                dtmp = exp(-BSPEC(m)*nd.pdot);
198                  for (i = 0; i < 3; i++)
199 <                        colval(scolor,i) += (1.0-colval(scolor,i))*dtmp;
200 <                rspec += (1.0-rspec)*dtmp;
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 <                        vrefl[i] = r->rdir[i] + 2.0*pdot*pnorm[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 (alpha2 <= FTINY && !(r->crtype & SHADOW))
215 <                        if (rayorigin(&lr, r, REFLECTED, rspec) == 0) {
216 <                                VCOPY(lr.rdir, vrefl);
214 >                if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
215 >                        RAY  lr;
216 >                        if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
217 >                                VCOPY(lr.rdir, nd.vrefl);
218                                  rayvalue(&lr);
219 <                                multcolor(lr.rcol, scolor);
219 >                                multcolor(lr.rcol, nd.scolor);
220                                  addcolor(r->rcol, lr.rcol);
221                          }
222 +                }
223          }
224 <
224 >                                                /* compute transmission */
225          if (m->otype == MAT_TRANS) {
226 <                trans = m->oargs.farg[5]*(1.0 - rspec);
227 <                tspec = trans * m->oargs.farg[6];
228 <                tdiff = trans - tspec;
226 >                nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec);
227 >                nd.tspec = nd.trans * m->oargs.farg[6];
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] - 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
250 <                tdiff = tspec = trans = 0.0;
250 >                nd.tdiff = nd.tspec = nd.trans = 0.0;
251                                                  /* transmitted ray */
252 <        if (tspec > FTINY && alpha2 <= FTINY)
253 <                if (rayorigin(&lr, r, TRANS, tspec) == 0) {
254 <                        VCOPY(lr.rdir, r->rdir);
252 >        if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
253 >                RAY  lr;
254 >                if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
255 >                        VCOPY(lr.rdir, nd.prdir);
256                          rayvalue(&lr);
257 <                        scalecolor(lr.rcol, tspec);
257 >                        scalecolor(lr.rcol, nd.tspec);
258 >                        multcolor(lr.rcol, nd.mcolor);  /* modified by color */
259                          addcolor(r->rcol, lr.rcol);
260 +                        transtest *= bright(lr.rcol);
261 +                        transdist = r->rot + lr.rt;
262                  }
263 +        } else
264 +                transtest = 0;
265 +
266          if (r->crtype & SHADOW)                 /* the rest is shadow */
267                  return;
268                                                  /* diffuse reflection */
269 <        rdiff = 1.0 - trans - rspec;
269 >        nd.rdiff = 1.0 - nd.trans - nd.rspec;
270  
271 <        if (rdiff <= FTINY && tdiff <= FTINY && alpha2 <= FTINY)
272 <                return;                         /* purely specular */
271 >        if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
272 >                return;                         /* 100% pure specular */
273  
274 <        if (rdiff > FTINY) {            /* ambient from this side */
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))
279 >                gaussamp(r, &nd);
280 >
281 >        if (nd.rdiff > FTINY) {         /* ambient from this side */
282                  ambient(ctmp, r);
283 <                if (alpha2 <= FTINY)
284 <                        scalecolor(ctmp, rdiff);
283 >                if (nd.specfl & SP_RBLT)
284 >                        scalecolor(ctmp, 1.0-nd.trans);
285                  else
286 <                        scalecolor(ctmp, 1.0-trans);
287 <                multcolor(ctmp, mcolor);        /* modified by material color */
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 (tdiff > FTINY) {            /* ambient from other side */
290 >        if (nd.tdiff > FTINY) {         /* ambient from other side */
291                  flipsurface(r);
292                  ambient(ctmp, r);
293 <                if (alpha2 <= FTINY)
294 <                        scalecolor(ctmp, tdiff);
293 >                if (nd.specfl & SP_TBLT)
294 >                        scalecolor(ctmp, nd.trans);
295                  else
296 <                        scalecolor(ctmp, trans);
297 <                multcolor(ctmp, mcolor);
296 >                        scalecolor(ctmp, nd.tdiff);
297 >                multcolor(ctmp, nd.mcolor);     /* modified by color */
298                  addcolor(r->rcol, ctmp);
299                  flipsurface(r);
300          }
301 <        
302 <        for (i = 0; i < nsources; i++) {        /* add specular and diffuse */
301 >                                        /* add direct component */
302 >        direct(r, dirnorm, &nd);
303 >                                        /* check distance */
304 >        if (transtest > bright(r->rcol))
305 >                r->rt = transdist;
306 > }
307  
153                if ((omega = srcray(&lr, r, i)) == 0.0)
154                        continue;               /* bad source */
308  
309 <                ldot = DOT(pnorm, lr.rdir);
310 <        
311 <                if (ldot < 0.0 ? trans <= FTINY : trans >= 1.0-FTINY)
312 <                        continue;               /* wrong side */
313 <        
314 <                rayvalue(&lr);                  /* compute light ray value */
315 <        
316 <                if (intens(lr.rcol) <= FTINY)
317 <                        continue;               /* didn't hit light source */
318 <
319 <                if (ldot > FTINY && rdiff > FTINY) {
320 <                        /*
321 <                         *  Compute and add diffuse component to returned color.
322 <                         *  The diffuse component will always be modified by the
323 <                         *  color of the material.
324 <                         */
325 <                        copycolor(ctmp, lr.rcol);
326 <                        dtmp = ldot * omega * rdiff / PI;
327 <                        scalecolor(ctmp, dtmp);
328 <                        multcolor(ctmp, mcolor);
329 <                        addcolor(r->rcol, ctmp);
330 <                }
331 <                if (ldot > FTINY && rspec > FTINY && alpha2 > FTINY) {
332 <                        /*
333 <                         *  Compute specular reflection coefficient using
334 <                         *  gaussian distribution model.
335 <                         */
336 <                                                        /* roughness + source */
337 <                        dtmp = alpha2 + omega/(2.0*PI);
338 <                                                        /* gaussian */
339 <                        dtmp = exp((DOT(vrefl,lr.rdir)-1.)/dtmp)/(2.*PI)/dtmp;
340 <                                                        /* worth using? */
341 <                        if (dtmp > FTINY) {
342 <                                copycolor(ctmp, lr.rcol);
343 <                                dtmp *= omega;
344 <                                scalecolor(ctmp, dtmp);
345 <                                multcolor(ctmp, scolor);
346 <                                addcolor(r->rcol, ctmp);
347 <                        }
348 <                }
349 <                if (ldot < -FTINY && tdiff > FTINY) {
350 <                        /*
351 <                         *  Compute diffuse transmission.
352 <                         */
353 <                        copycolor(ctmp, lr.rcol);
354 <                        dtmp = -ldot * omega * tdiff / PI;
355 <                        scalecolor(ctmp, dtmp);
356 <                        multcolor(ctmp, mcolor);
357 <                        addcolor(r->rcol, ctmp);
358 <                }
359 <                if (ldot < -FTINY && tspec > FTINY && alpha2 > FTINY) {
360 <                        /*
361 <                         *  Compute specular transmission.
362 <                         */
363 <                                                        /* roughness + source */
364 <                        dtmp = alpha2 + omega/(2.0*PI);
365 <                                                        /* gaussian */
366 <                        dtmp = exp((DOT(r->rdir,lr.rdir)-1.)/dtmp)/(2.*PI)/dtmp;
367 <                                                        /* worth using? */
368 <                        if (dtmp > FTINY) {
369 <                                copycolor(ctmp, lr.rcol);
370 <                                dtmp *= tspec * omega;
371 <                                scalecolor(ctmp, dtmp);
372 <                                addcolor(r->rcol, ctmp);
373 <                        }
374 <                }
309 > static
310 > gaussamp(r, np)                 /* sample gaussian specular */
311 > RAY  *r;
312 > register NORMDAT  *np;
313 > {
314 >        RAY  sr;
315 >        FVECT  u, v, h;
316 >        double  rv[2];
317 >        double  d, sinp, cosp;
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++)
326 >                if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6)
327 >                        break;
328 >        v[i] = 1.0;
329 >        fcross(u, v, np->pnorm);
330 >        normalize(u);
331 >        fcross(v, np->pnorm, u);
332 >                                        /* compute reflection */
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 >                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