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.24 by greg, Mon Mar 8 12:37:27 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 36 | Line 40 | static char SCCSid[] = "$SunId$ LBL";
40  
41   #define  BSPEC(m)       (6.0)           /* specularity parameter b */
42  
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 < m_normal(m, r)                  /* color a ray which hit something normal */
52 < register OBJREC  *m;
53 < register RAY  *r;
54 < {
44 <        double  exp();
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 */
57          FVECT  vrefl;           /* vector in direction of reflected ray */
58 <        double  alpha2;         /* roughness squared times 2 */
59 <        RAY  lr;                /* ray to illumination source */
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 */
63          FVECT  pnorm;           /* perturbed surface normal */
64          double  pdot;           /* perturbed dot product */
65 + }  NORMDAT;             /* normal material data */
66 +
67 +
68 + dirnorm(cval, np, ldir, omega)          /* compute source contribution */
69 + COLOR  cval;                    /* returned coefficient */
70 + register NORMDAT  *np;          /* material data */
71 + FVECT  ldir;                    /* light source direction */
72 + double  omega;                  /* light source size */
73 + {
74          double  ldot;
75 <        double  omega;
75 >        double  dtmp, d2;
76 >        FVECT  vtmp;
77 >        COLOR  ctmp;
78 >
79 >        setcolor(cval, 0.0, 0.0, 0.0);
80 >
81 >        ldot = DOT(np->pnorm, ldir);
82 >
83 >        if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY)
84 >                return;         /* wrong side */
85 >
86 >        if (ldot > FTINY && np->rdiff > FTINY) {
87 >                /*
88 >                 *  Compute and add diffuse reflected component to returned
89 >                 *  color.  The diffuse reflected component will always be
90 >                 *  modified by the color of the material.
91 >                 */
92 >                copycolor(ctmp, np->mcolor);
93 >                dtmp = ldot * omega * np->rdiff / PI;
94 >                scalecolor(ctmp, dtmp);
95 >                addcolor(cval, ctmp);
96 >        }
97 >        if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) {
98 >                /*
99 >                 *  Compute specular reflection coefficient using
100 >                 *  gaussian distribution model.
101 >                 */
102 >                                                /* roughness */
103 >                dtmp = np->alpha2;
104 >                                                /* + source if flat */
105 >                if (np->specfl & SP_FLAT)
106 >                        dtmp += omega/(4.0*PI);
107 >                                                /* half vector */
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 *= d2;
113 >                d2 = (DOT(vtmp,vtmp) - d2) / d2;
114 >                                                /* gaussian */
115 >                dtmp = exp(-d2/dtmp)/(4.*PI*dtmp);
116 >                                                /* worth using? */
117 >                if (dtmp > FTINY) {
118 >                        copycolor(ctmp, np->scolor);
119 >                        dtmp *= omega * sqrt(ldot/np->pdot);
120 >                        scalecolor(ctmp, dtmp);
121 >                        addcolor(cval, ctmp);
122 >                }
123 >        }
124 >        if (ldot < -FTINY && np->tdiff > FTINY) {
125 >                /*
126 >                 *  Compute diffuse transmission.
127 >                 */
128 >                copycolor(ctmp, np->mcolor);
129 >                dtmp = -ldot * omega * np->tdiff / PI;
130 >                scalecolor(ctmp, dtmp);
131 >                addcolor(cval, ctmp);
132 >        }
133 >        if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) {
134 >                /*
135 >                 *  Compute specular transmission.  Specular transmission
136 >                 *  is always modified by material color.
137 >                 */
138 >                                                /* roughness + source */
139 >                dtmp = np->alpha2 + omega/PI;
140 >                                                /* gaussian */
141 >                dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp);
142 >                                                /* worth using? */
143 >                if (dtmp > FTINY) {
144 >                        copycolor(ctmp, np->mcolor);
145 >                        dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot);
146 >                        scalecolor(ctmp, dtmp);
147 >                        addcolor(cval, ctmp);
148 >                }
149 >        }
150 > }
151 >
152 >
153 > m_normal(m, r)                  /* color a ray that hit something normal */
154 > register OBJREC  *m;
155 > register RAY  *r;
156 > {
157 >        NORMDAT  nd;
158 >        double  transtest, transdist;
159          double  dtmp;
160          COLOR  ctmp;
161          register int  i;
60
61        if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
62                objerror(m, USER, "bad # arguments");
162                                                  /* easy shadow test */
163          if (r->crtype & SHADOW && m->otype != MAT_TRANS)
164                  return;
165 +
166 +        if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
167 +                objerror(m, USER, "bad number of arguments");
168 +        nd.mp = m;
169 +        nd.rp = r;
170                                                  /* get material color */
171 <        setcolor(mcolor, m->oargs.farg[0],
171 >        setcolor(nd.mcolor, m->oargs.farg[0],
172                             m->oargs.farg[1],
173                             m->oargs.farg[2]);
174                                                  /* get roughness */
175 <        alpha2 = m->oargs.farg[4];
176 <        alpha2 *= 2.0 * alpha2;
175 >        nd.specfl = 0;
176 >        nd.alpha2 = m->oargs.farg[4];
177 >        if ((nd.alpha2 *= nd.alpha2) <= FTINY)
178 >                nd.specfl |= SP_PURE;
179                                                  /* reorient if necessary */
180          if (r->rod < 0.0)
181                  flipsurface(r);
182                                                  /* get modifiers */
183          raytexture(r, m->omod);
184 <        pdot = raynormal(pnorm, r);             /* perturb normal */
185 <        multcolor(mcolor, r->pcol);             /* modify material color */
184 >        nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
185 >        if (nd.pdot < .001)
186 >                nd.pdot = .001;                 /* non-zero for dirnorm() */
187 >        multcolor(nd.mcolor, r->pcol);          /* modify material color */
188 >        transtest = 0;
189                                                  /* get specular component */
190 <        rspec = m->oargs.farg[3];
191 <
83 <        if (rspec > FTINY) {                    /* has specular component */
190 >        if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
191 >                nd.specfl |= SP_REFL;
192                                                  /* compute specular color */
193                  if (m->otype == MAT_METAL)
194 <                        copycolor(scolor, mcolor);
194 >                        copycolor(nd.scolor, nd.mcolor);
195                  else
196 <                        setcolor(scolor, 1.0, 1.0, 1.0);
197 <                scalecolor(scolor, rspec);
196 >                        setcolor(nd.scolor, 1.0, 1.0, 1.0);
197 >                scalecolor(nd.scolor, nd.rspec);
198                                                  /* improved model */
199 <                dtmp = exp(-BSPEC(m)*pdot);
199 >                dtmp = exp(-BSPEC(m)*nd.pdot);
200                  for (i = 0; i < 3; i++)
201 <                        colval(scolor,i) += (1.0-colval(scolor,i))*dtmp;
202 <                rspec += (1.0-rspec)*dtmp;
201 >                        colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
202 >                nd.rspec += (1.0-nd.rspec)*dtmp;
203 >                                                /* check threshold */
204 >                if (!(nd.specfl & SP_PURE) &&
205 >                                specthresh > FTINY &&
206 >                                (specthresh >= 1.-FTINY ||
207 >                                specthresh + .05 - .1*frandom() > nd.rspec))
208 >                        nd.specfl |= SP_RBLT;
209                                                  /* compute reflected ray */
210                  for (i = 0; i < 3; i++)
211 <                        vrefl[i] = r->rdir[i] + 2.0*pdot*pnorm[i];
211 >                        nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
212 >                if (DOT(nd.vrefl, r->ron) <= FTINY)     /* penetration? */
213 >                        for (i = 0; i < 3; i++)         /* safety measure */
214 >                                nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
215  
216 <                if (alpha2 <= FTINY && !(r->crtype & SHADOW))
217 <                        if (rayorigin(&lr, r, REFLECTED, rspec) == 0) {
218 <                                VCOPY(lr.rdir, vrefl);
216 >                if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
217 >                        RAY  lr;
218 >                        if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
219 >                                VCOPY(lr.rdir, nd.vrefl);
220                                  rayvalue(&lr);
221 <                                multcolor(lr.rcol, scolor);
221 >                                multcolor(lr.rcol, nd.scolor);
222                                  addcolor(r->rcol, lr.rcol);
223                          }
224 +                }
225          }
226 <
226 >                                                /* compute transmission */
227          if (m->otype == MAT_TRANS) {
228 <                trans = m->oargs.farg[5]*(1.0 - rspec);
229 <                tspec = trans * m->oargs.farg[6];
230 <                tdiff = trans - tspec;
228 >                nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec);
229 >                nd.tspec = nd.trans * m->oargs.farg[6];
230 >                nd.tdiff = nd.trans - nd.tspec;
231 >                if (nd.tspec > FTINY) {
232 >                        nd.specfl |= SP_TRAN;
233 >                                                        /* check threshold */
234 >                        if (!(nd.specfl & SP_PURE) && specthresh > FTINY &&
235 >                                        (specthresh >= 1.-FTINY ||
236 >                                specthresh + .05 - .1*frandom() > nd.tspec))
237 >                                nd.specfl |= SP_TBLT;
238 >                        if (r->crtype & SHADOW ||
239 >                                        DOT(r->pert,r->pert) <= FTINY*FTINY) {
240 >                                VCOPY(nd.prdir, r->rdir);
241 >                                transtest = 2;
242 >                        } else {
243 >                                for (i = 0; i < 3; i++)         /* perturb */
244 >                                        nd.prdir[i] = r->rdir[i] - 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 <                tdiff = tspec = trans = 0.0;
252 >                nd.tdiff = nd.tspec = nd.trans = 0.0;
253                                                  /* transmitted ray */
254 <        if (tspec > FTINY && alpha2 <= FTINY)
255 <                if (rayorigin(&lr, r, TRANS, tspec) == 0) {
256 <                        VCOPY(lr.rdir, r->rdir);
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, nd.prdir);
258                          rayvalue(&lr);
259 <                        scalecolor(lr.rcol, tspec);
259 >                        scalecolor(lr.rcol, nd.tspec);
260 >                        multcolor(lr.rcol, nd.mcolor);  /* modified by color */
261                          addcolor(r->rcol, lr.rcol);
262 +                        transtest *= bright(lr.rcol);
263 +                        transdist = r->rot + lr.rt;
264                  }
265 +        } else
266 +                transtest = 0;
267 +
268          if (r->crtype & SHADOW)                 /* the rest is shadow */
269                  return;
270                                                  /* diffuse reflection */
271 <        rdiff = 1.0 - trans - rspec;
271 >        nd.rdiff = 1.0 - nd.trans - nd.rspec;
272  
273 <        if (rdiff <= FTINY && tdiff <= FTINY && 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 <        ambient(ctmp, r);               /* compute ambient component */
277 <        scalecolor(ctmp, 1.0-trans);    /* from this side */
278 <        multcolor(ctmp, mcolor);        /* modified by material color */
133 <        addcolor(r->rcol, ctmp);        /* add to returned color */
276 >        if (r->ro != NULL && (r->ro->otype == OBJ_FACE ||
277 >                        r->ro->otype == OBJ_RING))
278 >                nd.specfl |= SP_FLAT;
279  
280 <        if (trans > FTINY) {            /* ambient from other side */
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.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 <                scalecolor(ctmp, trans);
295 <                multcolor(ctmp, mcolor);
294 >                ambient(ctmp, r);
295 >                if (nd.specfl & SP_TBLT)
296 >                        scalecolor(ctmp, nd.trans);
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 <        
304 <        for (i = 0; i < nsources; i++) {        /* add specular and diffuse */
303 >                                        /* add direct component */
304 >        direct(r, dirnorm, &nd);
305 >                                        /* check distance */
306 >        if (transtest > bright(r->rcol))
307 >                r->rt = transdist;
308 > }
309  
145                if ((omega = srcray(&lr, r, i)) == 0.0)
146                        continue;               /* bad source */
310  
311 <                ldot = DOT(pnorm, lr.rdir);
312 <        
313 <                if (ldot < 0.0 ? trans <= FTINY : trans >= 1.0-FTINY)
314 <                        continue;               /* wrong side */
315 <        
316 <                rayvalue(&lr);                  /* compute light ray value */
317 <        
318 <                if (intens(lr.rcol) <= FTINY)
319 <                        continue;               /* didn't hit light source */
320 <
321 <                if (ldot > FTINY && rdiff > FTINY) {
322 <                        /*
323 <                         *  Compute and add diffuse component to returned color.
324 <                         *  The diffuse component will always be modified by the
325 <                         *  color of the material.
326 <                         */
327 <                        copycolor(ctmp, lr.rcol);
328 <                        dtmp = ldot * omega * rdiff / PI;
329 <                        scalecolor(ctmp, dtmp);
330 <                        multcolor(ctmp, mcolor);
331 <                        addcolor(r->rcol, ctmp);
332 <                }
333 <                if (ldot > FTINY && rspec > FTINY && alpha2 > FTINY) {
334 <                        /*
335 <                         *  Compute specular reflection coefficient using
336 <                         *  gaussian distribution model.
337 <                         */
338 <                                                        /* roughness + source */
339 <                        dtmp = alpha2 + omega/(2.0*PI);
340 <                                                        /* gaussian */
341 <                        dtmp = exp((DOT(vrefl,lr.rdir)-1.)/dtmp)/(2.*PI)/dtmp;
342 <                                                        /* worth using? */
343 <                        if (dtmp > FTINY) {
344 <                                copycolor(ctmp, lr.rcol);
345 <                                dtmp *= omega;
346 <                                scalecolor(ctmp, dtmp);
347 <                                multcolor(ctmp, scolor);
348 <                                addcolor(r->rcol, ctmp);
349 <                        }
350 <                }
351 <                if (ldot < -FTINY && tdiff > FTINY) {
352 <                        /*
353 <                         *  Compute diffuse transmission.
354 <                         */
355 <                        copycolor(ctmp, lr.rcol);
356 <                        dtmp = -ldot * omega * tdiff / PI;
357 <                        scalecolor(ctmp, dtmp);
358 <                        multcolor(ctmp, mcolor);
359 <                        addcolor(r->rcol, ctmp);
360 <                }
361 <                if (ldot < -FTINY && tspec > FTINY && alpha2 > FTINY) {
362 <                        /*
363 <                         *  Compute specular transmission.
364 <                         */
365 <                                                        /* roughness + source */
366 <                        dtmp = alpha2 + omega/(2.0*PI);
367 <                                                        /* gaussian */
368 <                        dtmp = exp((DOT(r->rdir,lr.rdir)-1.)/dtmp)/(2.*PI)/dtmp;
369 <                                                        /* worth using? */
370 <                        if (dtmp > FTINY) {
371 <                                copycolor(ctmp, lr.rcol);
372 <                                dtmp *= tspec * omega;
373 <                                scalecolor(ctmp, dtmp);
374 <                                addcolor(r->rcol, ctmp);
375 <                        }
376 <                }
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( -log(rv[1]) * np->alpha2 );
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