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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines