ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/normal.c
Revision: 2.46
Committed: Thu Aug 28 03:22:16 2003 UTC (20 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.45: +3 -1 lines
Log Message:
Created proper prototypes for function pointers and included missing headers

File Contents

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