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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines