ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/normal.c
Revision: 2.42
Committed: Wed Mar 12 17:26:58 2003 UTC (21 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.41: +2 -2 lines
Log Message:
Returned old behavior with flat surfaces and improved documentation

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.41 static const char RCSid[] = "$Id$";
3 greg 1.1 #endif
4     /*
5     * normal.c - shading function for normal materials.
6     *
7     * 8/19/85
8     * 12/19/85 - added stuff for metals.
9     * 6/26/87 - improved specular model.
10     * 9/28/87 - added model for translucent materials.
11 greg 2.2 * Later changes described in delta comments.
12 greg 1.1 */
13    
14 greg 2.39 #include "copyright.h"
15 greg 2.38
16 greg 1.1 #include "ray.h"
17    
18     #include "otypes.h"
19    
20 greg 2.2 #include "random.h"
21    
22 greg 2.34 #ifndef MAXITER
23     #define MAXITER 10 /* maximum # specular ray attempts */
24     #endif
25 greg 2.38 /* estimate of Fresnel function */
26     #define FRESNE(ci) (exp(-6.0*(ci)) - 0.00247875217)
27 greg 2.34
28 greg 2.38 static void gaussamp();
29 greg 2.24
30 greg 1.1 /*
31 greg 2.22 * This routine implements the isotropic Gaussian
32     * model described by Ward in Siggraph `92 article.
33 greg 1.1 * We orient the surface towards the incoming ray, so a single
34     * surface can be used to represent an infinitely thin object.
35     *
36     * Arguments for MAT_PLASTIC and MAT_METAL are:
37     * red grn blu specular-frac. facet-slope
38     *
39     * Arguments for MAT_TRANS are:
40     * red grn blu rspec rough trans tspec
41     */
42    
43 greg 2.2 /* specularity flags */
44     #define SP_REFL 01 /* has reflected specular component */
45     #define SP_TRAN 02 /* has transmitted specular */
46 greg 2.11 #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 greg 1.1
51 greg 1.3 typedef struct {
52     OBJREC *mp; /* material pointer */
53 greg 2.16 RAY *rp; /* ray pointer */
54 greg 2.2 short specfl; /* specularity flags, defined above */
55 greg 1.1 COLOR mcolor; /* color of this material */
56     COLOR scolor; /* color of specular component */
57     FVECT vrefl; /* vector in direction of reflected ray */
58 greg 1.14 FVECT prdir; /* vector in transmitted direction */
59 greg 2.2 double alpha2; /* roughness squared */
60 greg 1.1 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 greg 1.3 } NORMDAT; /* normal material data */
66    
67    
68 greg 2.38 static void
69 greg 1.3 dirnorm(cval, np, ldir, omega) /* compute source contribution */
70     COLOR cval; /* returned coefficient */
71     register NORMDAT *np; /* material data */
72     FVECT ldir; /* light source direction */
73     double omega; /* light source size */
74     {
75 greg 1.1 double ldot;
76 greg 2.38 double ldiff;
77 greg 2.16 double dtmp, d2;
78     FVECT vtmp;
79 greg 1.3 COLOR ctmp;
80    
81     setcolor(cval, 0.0, 0.0, 0.0);
82    
83     ldot = DOT(np->pnorm, ldir);
84    
85     if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY)
86     return; /* wrong side */
87    
88 greg 2.38 /* Fresnel estimate */
89     ldiff = np->rdiff;
90     if (np->specfl & SP_PURE && (np->rspec > FTINY & ldiff > FTINY))
91     ldiff *= 1. - FRESNE(fabs(ldot));
92    
93     if (ldot > FTINY && ldiff > FTINY) {
94 greg 1.3 /*
95 greg 1.4 * Compute and add diffuse reflected component to returned
96     * color. The diffuse reflected component will always be
97     * modified by the color of the material.
98 greg 1.3 */
99     copycolor(ctmp, np->mcolor);
100 greg 2.38 dtmp = ldot * omega * ldiff / PI;
101 greg 1.3 scalecolor(ctmp, dtmp);
102     addcolor(cval, ctmp);
103     }
104 greg 2.2 if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) {
105 greg 1.3 /*
106     * Compute specular reflection coefficient using
107     * gaussian distribution model.
108     */
109 greg 2.3 /* roughness */
110 greg 2.16 dtmp = np->alpha2;
111 greg 2.3 /* + source if flat */
112     if (np->specfl & SP_FLAT)
113 greg 2.16 dtmp += omega/(4.0*PI);
114 greg 2.23 /* half vector */
115 greg 2.18 vtmp[0] = ldir[0] - np->rp->rdir[0];
116     vtmp[1] = ldir[1] - np->rp->rdir[1];
117     vtmp[2] = ldir[2] - np->rp->rdir[2];
118 greg 2.16 d2 = DOT(vtmp, np->pnorm);
119 greg 2.23 d2 *= d2;
120     d2 = (DOT(vtmp,vtmp) - d2) / d2;
121 greg 1.3 /* gaussian */
122 greg 2.16 dtmp = exp(-d2/dtmp)/(4.*PI*dtmp);
123 greg 1.3 /* worth using? */
124     if (dtmp > FTINY) {
125     copycolor(ctmp, np->scolor);
126 greg 2.14 dtmp *= omega * sqrt(ldot/np->pdot);
127 greg 1.3 scalecolor(ctmp, dtmp);
128     addcolor(cval, ctmp);
129     }
130     }
131     if (ldot < -FTINY && np->tdiff > FTINY) {
132     /*
133     * Compute diffuse transmission.
134     */
135     copycolor(ctmp, np->mcolor);
136     dtmp = -ldot * omega * np->tdiff / PI;
137     scalecolor(ctmp, dtmp);
138     addcolor(cval, ctmp);
139     }
140 greg 2.2 if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) {
141 greg 1.3 /*
142 greg 1.4 * Compute specular transmission. Specular transmission
143 greg 1.13 * is always modified by material color.
144 greg 1.3 */
145     /* roughness + source */
146 greg 2.19 dtmp = np->alpha2 + omega/PI;
147 greg 1.3 /* gaussian */
148 greg 2.21 dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(PI*dtmp);
149 greg 1.3 /* worth using? */
150     if (dtmp > FTINY) {
151 greg 1.13 copycolor(ctmp, np->mcolor);
152 greg 2.18 dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot);
153 greg 1.13 scalecolor(ctmp, dtmp);
154 greg 1.3 addcolor(cval, ctmp);
155     }
156     }
157     }
158    
159    
160 greg 2.38 int
161 greg 2.2 m_normal(m, r) /* color a ray that hit something normal */
162 greg 1.3 register OBJREC *m;
163     register RAY *r;
164     {
165     NORMDAT nd;
166 greg 2.38 double fest;
167 greg 1.9 double transtest, transdist;
168 greg 2.29 double mirtest, mirdist;
169     int hastexture;
170     double d;
171 greg 1.1 COLOR ctmp;
172     register int i;
173     /* easy shadow test */
174     if (r->crtype & SHADOW && m->otype != MAT_TRANS)
175 greg 2.27 return(1);
176 greg 2.2
177     if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
178     objerror(m, USER, "bad number of arguments");
179 greg 2.29 /* check for back side */
180     if (r->rod < 0.0) {
181     if (!backvis && m->otype != MAT_TRANS) {
182     raytrans(r);
183     return(1);
184     }
185 greg 2.40 raytexture(r, m->omod);
186 greg 2.29 flipsurface(r); /* reorient if backvis */
187 greg 2.40 } else
188     raytexture(r, m->omod);
189 greg 1.3 nd.mp = m;
190 greg 2.16 nd.rp = r;
191 greg 1.1 /* get material color */
192 greg 1.3 setcolor(nd.mcolor, m->oargs.farg[0],
193 greg 1.1 m->oargs.farg[1],
194     m->oargs.farg[2]);
195     /* get roughness */
196 greg 2.2 nd.specfl = 0;
197 greg 1.3 nd.alpha2 = m->oargs.farg[4];
198 greg 2.2 if ((nd.alpha2 *= nd.alpha2) <= FTINY)
199     nd.specfl |= SP_PURE;
200 greg 2.40
201 greg 2.41 if (hastexture = (DOT(r->pert,r->pert) > FTINY*FTINY)) {
202 greg 2.29 nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */
203 greg 2.41 } else {
204 greg 2.29 VCOPY(nd.pnorm, r->ron);
205     nd.pdot = r->rod;
206     }
207 greg 2.42 if (r->ro != NULL && isflat(r->ro->otype))
208     nd.specfl |= SP_FLAT;
209 greg 1.13 if (nd.pdot < .001)
210     nd.pdot = .001; /* non-zero for dirnorm() */
211 greg 1.3 multcolor(nd.mcolor, r->pcol); /* modify material color */
212 greg 2.29 mirtest = transtest = 0;
213     mirdist = transdist = r->rot;
214 greg 2.30 nd.rspec = m->oargs.farg[3];
215 greg 2.38 /* compute Fresnel approx. */
216     if (nd.specfl & SP_PURE && nd.rspec > FTINY) {
217     fest = FRESNE(r->rod);
218     nd.rspec += fest*(1. - nd.rspec);
219     } else
220     fest = 0.;
221 greg 1.3 /* compute transmission */
222 greg 1.1 if (m->otype == MAT_TRANS) {
223 greg 1.3 nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec);
224     nd.tspec = nd.trans * m->oargs.farg[6];
225     nd.tdiff = nd.trans - nd.tspec;
226 greg 2.2 if (nd.tspec > FTINY) {
227     nd.specfl |= SP_TRAN;
228 greg 2.5 /* check threshold */
229 greg 2.25 if (!(nd.specfl & SP_PURE) &&
230     specthresh >= nd.tspec-FTINY)
231 greg 2.5 nd.specfl |= SP_TBLT;
232 greg 2.29 if (!hastexture || r->crtype & SHADOW) {
233 greg 2.2 VCOPY(nd.prdir, r->rdir);
234     transtest = 2;
235     } else {
236     for (i = 0; i < 3; i++) /* perturb */
237 greg 2.19 nd.prdir[i] = r->rdir[i] - r->pert[i];
238 greg 2.7 if (DOT(nd.prdir, r->ron) < -FTINY)
239     normalize(nd.prdir); /* OK */
240     else
241     VCOPY(nd.prdir, r->rdir);
242 greg 2.2 }
243 greg 1.14 }
244 greg 1.1 } else
245 greg 1.3 nd.tdiff = nd.tspec = nd.trans = 0.0;
246 greg 1.1 /* transmitted ray */
247 gregl 2.36 if ((nd.specfl&(SP_TRAN|SP_PURE|SP_TBLT)) == (SP_TRAN|SP_PURE)) {
248 greg 1.3 RAY lr;
249     if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
250 greg 1.14 VCOPY(lr.rdir, nd.prdir);
251 greg 1.1 rayvalue(&lr);
252 greg 1.3 scalecolor(lr.rcol, nd.tspec);
253 greg 1.8 multcolor(lr.rcol, nd.mcolor); /* modified by color */
254 greg 1.1 addcolor(r->rcol, lr.rcol);
255 greg 1.9 transtest *= bright(lr.rcol);
256     transdist = r->rot + lr.rt;
257 greg 1.1 }
258 greg 2.11 } else
259     transtest = 0;
260 greg 2.2
261 greg 2.29 if (r->crtype & SHADOW) { /* the rest is shadow */
262     r->rt = transdist;
263 greg 2.27 return(1);
264 greg 2.30 }
265     /* get specular reflection */
266     if (nd.rspec > FTINY) {
267     nd.specfl |= SP_REFL;
268     /* compute specular color */
269 greg 2.38 if (m->otype != MAT_METAL) {
270     setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec);
271     } else if (fest > FTINY) {
272     d = nd.rspec*(1. - fest);
273     for (i = 0; i < 3; i++)
274     nd.scolor[i] = fest + nd.mcolor[i]*d;
275     } else {
276 greg 2.30 copycolor(nd.scolor, nd.mcolor);
277 greg 2.38 scalecolor(nd.scolor, nd.rspec);
278     }
279 greg 2.30 /* check threshold */
280     if (!(nd.specfl & SP_PURE) && specthresh >= nd.rspec-FTINY)
281     nd.specfl |= SP_RBLT;
282     /* compute reflected ray */
283     for (i = 0; i < 3; i++)
284     nd.vrefl[i] = r->rdir[i] + 2.*nd.pdot*nd.pnorm[i];
285     /* penetration? */
286     if (hastexture && DOT(nd.vrefl, r->ron) <= FTINY)
287     for (i = 0; i < 3; i++) /* safety measure */
288     nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
289 gregl 2.36 }
290     /* reflected ray */
291     if ((nd.specfl&(SP_REFL|SP_PURE|SP_RBLT)) == (SP_REFL|SP_PURE)) {
292     RAY lr;
293     if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
294     VCOPY(lr.rdir, nd.vrefl);
295     rayvalue(&lr);
296     multcolor(lr.rcol, nd.scolor);
297     addcolor(r->rcol, lr.rcol);
298     if (!hastexture && nd.specfl & SP_FLAT) {
299     mirtest = 2.*bright(lr.rcol);
300     mirdist = r->rot + lr.rt;
301 greg 2.30 }
302     }
303 greg 2.29 }
304 greg 1.1 /* diffuse reflection */
305 greg 1.3 nd.rdiff = 1.0 - nd.trans - nd.rspec;
306 greg 1.1
307 greg 2.2 if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
308 greg 2.27 return(1); /* 100% pure specular */
309 greg 2.3
310 gregl 2.36 if (!(nd.specfl & SP_PURE))
311     gaussamp(r, &nd); /* checks *BLT flags */
312 greg 2.2
313 greg 1.3 if (nd.rdiff > FTINY) { /* ambient from this side */
314 greg 2.31 ambient(ctmp, r, hastexture?nd.pnorm:r->ron);
315 greg 2.5 if (nd.specfl & SP_RBLT)
316     scalecolor(ctmp, 1.0-nd.trans);
317     else
318     scalecolor(ctmp, nd.rdiff);
319 greg 1.3 multcolor(ctmp, nd.mcolor); /* modified by material color */
320 greg 1.2 addcolor(r->rcol, ctmp); /* add to returned color */
321     }
322 greg 1.3 if (nd.tdiff > FTINY) { /* ambient from other side */
323 greg 1.1 flipsurface(r);
324 greg 2.32 if (hastexture) {
325     FVECT bnorm;
326     bnorm[0] = -nd.pnorm[0];
327     bnorm[1] = -nd.pnorm[1];
328     bnorm[2] = -nd.pnorm[2];
329     ambient(ctmp, r, bnorm);
330     } else
331     ambient(ctmp, r, r->ron);
332 greg 2.5 if (nd.specfl & SP_TBLT)
333     scalecolor(ctmp, nd.trans);
334     else
335     scalecolor(ctmp, nd.tdiff);
336 greg 1.13 multcolor(ctmp, nd.mcolor); /* modified by color */
337 greg 1.1 addcolor(r->rcol, ctmp);
338     flipsurface(r);
339     }
340 greg 1.3 /* add direct component */
341     direct(r, dirnorm, &nd);
342 greg 1.9 /* check distance */
343 greg 2.29 d = bright(r->rcol);
344     if (transtest > d)
345 greg 1.9 r->rt = transdist;
346 greg 2.29 else if (mirtest > d)
347     r->rt = mirdist;
348 greg 2.27
349     return(1);
350 greg 2.2 }
351    
352    
353 greg 2.38 static void
354 greg 2.2 gaussamp(r, np) /* sample gaussian specular */
355     RAY *r;
356     register NORMDAT *np;
357     {
358     RAY sr;
359     FVECT u, v, h;
360     double rv[2];
361     double d, sinp, cosp;
362 greg 2.34 int niter;
363 greg 2.2 register int i;
364 greg 2.13 /* quick test */
365     if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL &&
366     (np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN)
367     return;
368 greg 2.2 /* set up sample coordinates */
369     v[0] = v[1] = v[2] = 0.0;
370     for (i = 0; i < 3; i++)
371     if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6)
372     break;
373     v[i] = 1.0;
374     fcross(u, v, np->pnorm);
375     normalize(u);
376     fcross(v, np->pnorm, u);
377     /* compute reflection */
378 greg 2.5 if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
379 greg 2.2 rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
380     dimlist[ndims++] = (int)np->mp;
381 greg 2.34 for (niter = 0; niter < MAXITER; niter++) {
382     if (niter)
383     d = frandom();
384     else
385     d = urand(ilhash(dimlist,ndims)+samplendx);
386     multisamp(rv, 2, d);
387     d = 2.0*PI * rv[0];
388 gwlarson 2.37 cosp = tcos(d);
389     sinp = tsin(d);
390 greg 2.34 rv[1] = 1.0 - specjitter*rv[1];
391     if (rv[1] <= FTINY)
392     d = 1.0;
393     else
394     d = sqrt( np->alpha2 * -log(rv[1]) );
395     for (i = 0; i < 3; i++)
396     h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
397     d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
398     for (i = 0; i < 3; i++)
399     sr.rdir[i] = r->rdir[i] + d*h[i];
400     if (DOT(sr.rdir, r->ron) > FTINY) {
401     rayvalue(&sr);
402     multcolor(sr.rcol, np->scolor);
403     addcolor(r->rcol, sr.rcol);
404     break;
405     }
406     }
407 greg 2.2 ndims--;
408     }
409     /* compute transmission */
410 greg 2.8 if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
411     rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
412     dimlist[ndims++] = (int)np->mp;
413 greg 2.34 for (niter = 0; niter < MAXITER; niter++) {
414     if (niter)
415     d = frandom();
416     else
417     d = urand(ilhash(dimlist,ndims)+1823+samplendx);
418     multisamp(rv, 2, d);
419     d = 2.0*PI * rv[0];
420 gwlarson 2.37 cosp = tcos(d);
421     sinp = tsin(d);
422 greg 2.34 rv[1] = 1.0 - specjitter*rv[1];
423     if (rv[1] <= FTINY)
424     d = 1.0;
425     else
426 gwlarson 2.37 d = sqrt( np->alpha2 * -log(rv[1]) );
427 greg 2.34 for (i = 0; i < 3; i++)
428     sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]);
429     if (DOT(sr.rdir, r->ron) < -FTINY) {
430     normalize(sr.rdir); /* OK, normalize */
431     rayvalue(&sr);
432     scalecolor(sr.rcol, np->tspec);
433     multcolor(sr.rcol, np->mcolor); /* modified */
434     addcolor(r->rcol, sr.rcol);
435     break;
436     }
437     }
438 greg 2.8 ndims--;
439     }
440 greg 1.1 }