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.2 by greg, Sat Jan 4 19:53:53 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  
18 #include  "source.h"
19
19   #include  "otypes.h"
20  
21 + #include  "random.h"
22 +
23   /*
24   *      This routine uses portions of the reflection
25   *  model described by Cook and Torrance.
# Line 36 | Line 37 | static char SCCSid[] = "$SunId$ LBL";
37  
38   #define  BSPEC(m)       (6.0)           /* specularity parameter b */
39  
40 +                                /* specularity flags */
41 + #define  SP_REFL        01              /* has reflected specular component */
42 + #define  SP_TRAN        02              /* has transmitted specular */
43 + #define  SP_PURE        010             /* purely specular (zero roughness) */
44  
45 < m_normal(m, r)                  /* color a ray which hit something normal */
46 < register OBJREC  *m;
47 < register RAY  *r;
43 < {
44 <        double  exp();
45 > typedef struct {
46 >        OBJREC  *mp;            /* material pointer */
47 >        short  specfl;          /* specularity flags, defined above */
48          COLOR  mcolor;          /* color of this material */
49          COLOR  scolor;          /* color of specular component */
50          FVECT  vrefl;           /* vector in direction of reflected ray */
51 <        double  alpha2;         /* roughness squared times 2 */
52 <        RAY  lr;                /* ray to illumination source */
51 >        FVECT  prdir;           /* vector in transmitted direction */
52 >        double  alpha2;         /* roughness squared */
53          double  rdiff, rspec;   /* reflected specular, diffuse */
54          double  trans;          /* transmissivity */
55          double  tdiff, tspec;   /* transmitted specular, diffuse */
56          FVECT  pnorm;           /* perturbed surface normal */
57          double  pdot;           /* perturbed dot product */
58 + }  NORMDAT;             /* normal material data */
59 +
60 +
61 + dirnorm(cval, np, ldir, omega)          /* compute source contribution */
62 + COLOR  cval;                    /* returned coefficient */
63 + register NORMDAT  *np;          /* material data */
64 + FVECT  ldir;                    /* light source direction */
65 + double  omega;                  /* light source size */
66 + {
67          double  ldot;
56        double  omega;
68          double  dtmp;
69          COLOR  ctmp;
59        register int  i;
70  
71 <        if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
72 <                objerror(m, USER, "bad # arguments");
71 >        setcolor(cval, 0.0, 0.0, 0.0);
72 >
73 >        ldot = DOT(np->pnorm, ldir);
74 >
75 >        if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY)
76 >                return;         /* wrong side */
77 >
78 >        if (ldot > FTINY && np->rdiff > FTINY) {
79 >                /*
80 >                 *  Compute and add diffuse reflected component to returned
81 >                 *  color.  The diffuse reflected component will always be
82 >                 *  modified by the color of the material.
83 >                 */
84 >                copycolor(ctmp, np->mcolor);
85 >                dtmp = ldot * omega * np->rdiff / PI;
86 >                scalecolor(ctmp, dtmp);
87 >                addcolor(cval, ctmp);
88 >        }
89 >        if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) {
90 >                /*
91 >                 *  Compute specular reflection coefficient using
92 >                 *  gaussian distribution model.
93 >                 */
94 >                                                /* roughness + source */
95 >                dtmp = 2.0*np->alpha2 + omega/(2.0*PI);
96 >                                                /* gaussian */
97 >                dtmp = exp((DOT(np->vrefl,ldir)-1.)/dtmp)/(2.*PI)/dtmp;
98 >                                                /* worth using? */
99 >                if (dtmp > FTINY) {
100 >                        copycolor(ctmp, np->scolor);
101 >                        dtmp *= omega / np->pdot;
102 >                        scalecolor(ctmp, dtmp);
103 >                        addcolor(cval, ctmp);
104 >                }
105 >        }
106 >        if (ldot < -FTINY && np->tdiff > FTINY) {
107 >                /*
108 >                 *  Compute diffuse transmission.
109 >                 */
110 >                copycolor(ctmp, np->mcolor);
111 >                dtmp = -ldot * omega * np->tdiff / PI;
112 >                scalecolor(ctmp, dtmp);
113 >                addcolor(cval, ctmp);
114 >        }
115 >        if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) {
116 >                /*
117 >                 *  Compute specular transmission.  Specular transmission
118 >                 *  is always modified by material color.
119 >                 */
120 >                                                /* roughness + source */
121 >                dtmp = np->alpha2 + omega/(2.0*PI);
122 >                                                /* gaussian */
123 >                dtmp = exp((DOT(np->prdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp;
124 >                                                /* worth using? */
125 >                if (dtmp > FTINY) {
126 >                        copycolor(ctmp, np->mcolor);
127 >                        dtmp *= np->tspec * omega / np->pdot;
128 >                        scalecolor(ctmp, dtmp);
129 >                        addcolor(cval, ctmp);
130 >                }
131 >        }
132 > }
133 >
134 >
135 > m_normal(m, r)                  /* color a ray that hit something normal */
136 > register OBJREC  *m;
137 > register RAY  *r;
138 > {
139 >        NORMDAT  nd;
140 >        double  transtest, transdist;
141 >        double  dtmp;
142 >        COLOR  ctmp;
143 >        register int  i;
144                                                  /* easy shadow test */
145          if (r->crtype & SHADOW && m->otype != MAT_TRANS)
146                  return;
147 +
148 +        if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
149 +                objerror(m, USER, "bad number of arguments");
150 +        nd.mp = m;
151                                                  /* get material color */
152 <        setcolor(mcolor, m->oargs.farg[0],
152 >        setcolor(nd.mcolor, m->oargs.farg[0],
153                             m->oargs.farg[1],
154                             m->oargs.farg[2]);
155                                                  /* get roughness */
156 <        alpha2 = m->oargs.farg[4];
157 <        alpha2 *= 2.0 * alpha2;
156 >        nd.specfl = 0;
157 >        nd.alpha2 = m->oargs.farg[4];
158 >        if ((nd.alpha2 *= nd.alpha2) <= FTINY)
159 >                nd.specfl |= SP_PURE;
160                                                  /* reorient if necessary */
161          if (r->rod < 0.0)
162                  flipsurface(r);
163                                                  /* get modifiers */
164          raytexture(r, m->omod);
165 <        pdot = raynormal(pnorm, r);             /* perturb normal */
166 <        multcolor(mcolor, r->pcol);             /* modify material color */
165 >        nd.pdot = raynormal(nd.pnorm, r);       /* perturb normal */
166 >        if (nd.pdot < .001)
167 >                nd.pdot = .001;                 /* non-zero for dirnorm() */
168 >        multcolor(nd.mcolor, r->pcol);          /* modify material color */
169 >        transtest = 0;
170                                                  /* get specular component */
171 <        rspec = m->oargs.farg[3];
172 <
83 <        if (rspec > FTINY) {                    /* has specular component */
171 >        if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
172 >                nd.specfl |= SP_REFL;
173                                                  /* compute specular color */
174                  if (m->otype == MAT_METAL)
175 <                        copycolor(scolor, mcolor);
175 >                        copycolor(nd.scolor, nd.mcolor);
176                  else
177 <                        setcolor(scolor, 1.0, 1.0, 1.0);
178 <                scalecolor(scolor, rspec);
177 >                        setcolor(nd.scolor, 1.0, 1.0, 1.0);
178 >                scalecolor(nd.scolor, nd.rspec);
179                                                  /* improved model */
180 <                dtmp = exp(-BSPEC(m)*pdot);
180 >                dtmp = exp(-BSPEC(m)*nd.pdot);
181                  for (i = 0; i < 3; i++)
182 <                        colval(scolor,i) += (1.0-colval(scolor,i))*dtmp;
183 <                rspec += (1.0-rspec)*dtmp;
182 >                        colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
183 >                nd.rspec += (1.0-nd.rspec)*dtmp;
184                                                  /* compute reflected ray */
185                  for (i = 0; i < 3; i++)
186 <                        vrefl[i] = r->rdir[i] + 2.0*pdot*pnorm[i];
186 >                        nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
187  
188 <                if (alpha2 <= FTINY && !(r->crtype & SHADOW))
189 <                        if (rayorigin(&lr, r, REFLECTED, rspec) == 0) {
190 <                                VCOPY(lr.rdir, vrefl);
188 >                if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
189 >                        RAY  lr;
190 >                        if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
191 >                                VCOPY(lr.rdir, nd.vrefl);
192                                  rayvalue(&lr);
193 <                                multcolor(lr.rcol, scolor);
193 >                                multcolor(lr.rcol, nd.scolor);
194                                  addcolor(r->rcol, lr.rcol);
195                          }
196 +                }
197          }
198 <
198 >                                                /* compute transmission */
199          if (m->otype == MAT_TRANS) {
200 <                trans = m->oargs.farg[5]*(1.0 - rspec);
201 <                tspec = trans * m->oargs.farg[6];
202 <                tdiff = trans - tspec;
200 >                nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec);
201 >                nd.tspec = nd.trans * m->oargs.farg[6];
202 >                nd.tdiff = nd.trans - nd.tspec;
203 >                if (nd.tspec > FTINY) {
204 >                        nd.specfl |= SP_TRAN;
205 >                        if (r->crtype & SHADOW ||
206 >                                        DOT(r->pert,r->pert) <= FTINY*FTINY) {
207 >                                VCOPY(nd.prdir, r->rdir);
208 >                                transtest = 2;
209 >                        } else {
210 >                                for (i = 0; i < 3; i++)         /* perturb */
211 >                                        nd.prdir[i] = r->rdir[i] -
212 >                                                        .75*r->pert[i];
213 >                                normalize(nd.prdir);
214 >                        }
215 >                }
216          } else
217 <                tdiff = tspec = trans = 0.0;
217 >                nd.tdiff = nd.tspec = nd.trans = 0.0;
218                                                  /* transmitted ray */
219 <        if (tspec > FTINY && alpha2 <= FTINY)
220 <                if (rayorigin(&lr, r, TRANS, tspec) == 0) {
221 <                        VCOPY(lr.rdir, r->rdir);
219 >        if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
220 >                RAY  lr;
221 >                if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
222 >                        VCOPY(lr.rdir, nd.prdir);
223                          rayvalue(&lr);
224 <                        scalecolor(lr.rcol, tspec);
224 >                        scalecolor(lr.rcol, nd.tspec);
225 >                        multcolor(lr.rcol, nd.mcolor);  /* modified by color */
226                          addcolor(r->rcol, lr.rcol);
227 +                        transtest *= bright(lr.rcol);
228 +                        transdist = r->rot + lr.rt;
229                  }
230 +        }
231 +
232          if (r->crtype & SHADOW)                 /* the rest is shadow */
233                  return;
234                                                  /* diffuse reflection */
235 <        rdiff = 1.0 - trans - rspec;
235 >        nd.rdiff = 1.0 - nd.trans - nd.rspec;
236  
237 <        if (rdiff <= FTINY && tdiff <= FTINY && alpha2 <= FTINY)
238 <                return;                         /* purely specular */
237 >        if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
238 >                return;                         /* 100% pure specular */
239  
240 <        ambient(ctmp, r);               /* compute ambient component */
241 <        scalecolor(ctmp, 1.0-trans);    /* from this side */
132 <        multcolor(ctmp, mcolor);        /* modified by material color */
133 <        addcolor(r->rcol, ctmp);        /* add to returned color */
240 >        if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE))
241 >                gaussamp(r, &nd);
242  
243 <        if (trans > FTINY) {            /* ambient from other side */
243 >        if (nd.rdiff > FTINY) {         /* ambient from this side */
244 >                ambient(ctmp, r);
245 >                scalecolor(ctmp, nd.rdiff);
246 >                multcolor(ctmp, nd.mcolor);     /* modified by material color */
247 >                addcolor(r->rcol, ctmp);        /* add to returned color */
248 >        }
249 >        if (nd.tdiff > FTINY) {         /* ambient from other side */
250                  flipsurface(r);
251 <                scalecolor(ctmp, trans);
252 <                multcolor(ctmp, mcolor);
251 >                ambient(ctmp, r);
252 >                scalecolor(ctmp, nd.tdiff);
253 >                multcolor(ctmp, nd.mcolor);     /* modified by color */
254                  addcolor(r->rcol, ctmp);
255                  flipsurface(r);
256          }
257 <        
258 <        for (i = 0; i < nsources; i++) {        /* add specular and diffuse */
257 >                                        /* add direct component */
258 >        direct(r, dirnorm, &nd);
259 >                                        /* check distance */
260 >        if (transtest > bright(r->rcol))
261 >                r->rt = transdist;
262 > }
263  
145                if ((omega = srcray(&lr, r, i)) == 0.0)
146                        continue;               /* bad source */
264  
265 <                ldot = DOT(pnorm, lr.rdir);
266 <        
267 <                if (ldot < 0.0 ? trans <= FTINY : trans >= 1.0-FTINY)
268 <                        continue;               /* wrong side */
269 <        
270 <                rayvalue(&lr);                  /* compute light ray value */
271 <        
272 <                if (intens(lr.rcol) <= FTINY)
273 <                        continue;               /* didn't hit light source */
274 <
275 <                if (ldot > FTINY && rdiff > FTINY) {
276 <                        /*
277 <                         *  Compute and add diffuse component to returned color.
278 <                         *  The diffuse component will always be modified by the
279 <                         *  color of the material.
280 <                         */
281 <                        copycolor(ctmp, lr.rcol);
282 <                        dtmp = ldot * omega * rdiff / PI;
283 <                        scalecolor(ctmp, dtmp);
284 <                        multcolor(ctmp, mcolor);
285 <                        addcolor(r->rcol, ctmp);
286 <                }
287 <                if (ldot > FTINY && rspec > FTINY && alpha2 > FTINY) {
288 <                        /*
289 <                         *  Compute specular reflection coefficient using
290 <                         *  gaussian distribution model.
291 <                         */
292 <                                                        /* roughness + source */
293 <                        dtmp = alpha2 + omega/(2.0*PI);
294 <                                                        /* gaussian */
295 <                        dtmp = exp((DOT(vrefl,lr.rdir)-1.)/dtmp)/(2.*PI)/dtmp;
296 <                                                        /* worth using? */
297 <                        if (dtmp > FTINY) {
298 <                                copycolor(ctmp, lr.rcol);
299 <                                dtmp *= omega;
300 <                                scalecolor(ctmp, dtmp);
301 <                                multcolor(ctmp, scolor);
302 <                                addcolor(r->rcol, ctmp);
303 <                        }
304 <                }
305 <                if (ldot < -FTINY && tdiff > FTINY) {
306 <                        /*
307 <                         *  Compute diffuse transmission.
308 <                         */
309 <                        copycolor(ctmp, lr.rcol);
310 <                        dtmp = -ldot * omega * tdiff / PI;
311 <                        scalecolor(ctmp, dtmp);
195 <                        multcolor(ctmp, mcolor);
196 <                        addcolor(r->rcol, ctmp);
197 <                }
198 <                if (ldot < -FTINY && tspec > FTINY && alpha2 > FTINY) {
199 <                        /*
200 <                         *  Compute specular transmission.
201 <                         */
202 <                                                        /* roughness + source */
203 <                        dtmp = alpha2 + omega/(2.0*PI);
204 <                                                        /* gaussian */
205 <                        dtmp = exp((DOT(r->rdir,lr.rdir)-1.)/dtmp)/(2.*PI)/dtmp;
206 <                                                        /* worth using? */
207 <                        if (dtmp > FTINY) {
208 <                                copycolor(ctmp, lr.rcol);
209 <                                dtmp *= tspec * omega;
210 <                                scalecolor(ctmp, dtmp);
211 <                                addcolor(r->rcol, ctmp);
212 <                        }
213 <                }
265 > static
266 > gaussamp(r, np)                 /* sample gaussian specular */
267 > RAY  *r;
268 > register NORMDAT  *np;
269 > {
270 >        RAY  sr;
271 >        FVECT  u, v, h;
272 >        double  rv[2];
273 >        double  d, sinp, cosp;
274 >        int  confuse;
275 >        register int  i;
276 >                                        /* set up sample coordinates */
277 >        v[0] = v[1] = v[2] = 0.0;
278 >        for (i = 0; i < 3; i++)
279 >                if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6)
280 >                        break;
281 >        v[i] = 1.0;
282 >        fcross(u, v, np->pnorm);
283 >        normalize(u);
284 >        fcross(v, np->pnorm, u);
285 >                                        /* compute reflection */
286 >        if (np->specfl & SP_REFL &&
287 >                        rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
288 >                confuse = 0;
289 >                dimlist[ndims++] = (int)np->mp;
290 >        refagain:
291 >                dimlist[ndims] = confuse += 3601;
292 >                d = urand(ilhash(dimlist,ndims+1)+samplendx);
293 >                multisamp(rv, 2, d);
294 >                d = 2.0*PI * rv[0];
295 >                cosp = cos(d);
296 >                sinp = sin(d);
297 >                if (rv[1] <= FTINY)
298 >                        d = 1.0;
299 >                else
300 >                        d = sqrt( np->alpha2 * -log(rv[1]) );
301 >                for (i = 0; i < 3; i++)
302 >                        h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
303 >                d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
304 >                for (i = 0; i < 3; i++)
305 >                        sr.rdir[i] = r->rdir[i] + d*h[i];
306 >                if (DOT(sr.rdir, r->ron) <= FTINY)      /* oops! */
307 >                        goto refagain;
308 >                rayvalue(&sr);
309 >                multcolor(sr.rcol, np->scolor);
310 >                addcolor(r->rcol, sr.rcol);
311 >                ndims--;
312          }
313 +                                        /* compute transmission */
314   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines