ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/normal.c
Revision: 2.19
Committed: Sat May 16 08:37:06 1992 UTC (31 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.18: +2 -3 lines
Log Message:
changed effect of perturbation and roughness on transmission

File Contents

# User Rev Content
1 greg 2.2 /* Copyright (c) 1992 Regents of the University of California */
2 greg 1.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * normal.c - shading function for normal materials.
9     *
10     * 8/19/85
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 greg 2.2 * Later changes described in delta comments.
15 greg 1.1 */
16    
17     #include "ray.h"
18    
19     #include "otypes.h"
20    
21 greg 2.2 #include "random.h"
22    
23 greg 2.5 extern double specthresh; /* specular sampling threshold */
24     extern double specjitter; /* specular sampling jitter */
25    
26 greg 1.1 /*
27     * This routine uses portions of the reflection
28     * model described by Cook and Torrance.
29     * The computation of specular components has been simplified by
30     * numerous approximations and ommisions to improve speed.
31     * We orient the surface towards the incoming ray, so a single
32     * surface can be used to represent an infinitely thin object.
33     *
34     * Arguments for MAT_PLASTIC and MAT_METAL are:
35     * red grn blu specular-frac. facet-slope
36     *
37     * Arguments for MAT_TRANS are:
38     * red grn blu rspec rough trans tspec
39     */
40    
41     #define BSPEC(m) (6.0) /* specularity parameter b */
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     dirnorm(cval, np, ldir, omega) /* compute source contribution */
69     COLOR cval; /* returned coefficient */
70     register NORMDAT *np; /* material data */
71     FVECT ldir; /* light source direction */
72     double omega; /* light source size */
73     {
74 greg 1.1 double ldot;
75 greg 2.16 double dtmp, d2;
76     FVECT vtmp;
77 greg 1.3 COLOR ctmp;
78    
79     setcolor(cval, 0.0, 0.0, 0.0);
80    
81     ldot = DOT(np->pnorm, ldir);
82    
83     if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY)
84     return; /* wrong side */
85    
86     if (ldot > FTINY && np->rdiff > FTINY) {
87     /*
88 greg 1.4 * Compute and add diffuse reflected component to returned
89     * color. The diffuse reflected component will always be
90     * modified by the color of the material.
91 greg 1.3 */
92     copycolor(ctmp, np->mcolor);
93     dtmp = ldot * omega * np->rdiff / PI;
94     scalecolor(ctmp, dtmp);
95     addcolor(cval, ctmp);
96     }
97 greg 2.2 if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) {
98 greg 1.3 /*
99     * Compute specular reflection coefficient using
100     * gaussian distribution model.
101     */
102 greg 2.3 /* roughness */
103 greg 2.16 dtmp = np->alpha2;
104 greg 2.3 /* + source if flat */
105     if (np->specfl & SP_FLAT)
106 greg 2.16 dtmp += omega/(4.0*PI);
107     /* delta */
108 greg 2.18 vtmp[0] = ldir[0] - np->rp->rdir[0];
109     vtmp[1] = ldir[1] - np->rp->rdir[1];
110     vtmp[2] = ldir[2] - np->rp->rdir[2];
111 greg 2.16 d2 = DOT(vtmp, np->pnorm);
112     d2 = 2.0 - 2.0*d2/sqrt(DOT(vtmp,vtmp));
113 greg 1.3 /* gaussian */
114 greg 2.16 dtmp = exp(-d2/dtmp)/(4.*PI*dtmp);
115 greg 1.3 /* worth using? */
116     if (dtmp > FTINY) {
117     copycolor(ctmp, np->scolor);
118 greg 2.14 dtmp *= omega * sqrt(ldot/np->pdot);
119 greg 1.3 scalecolor(ctmp, dtmp);
120     addcolor(cval, ctmp);
121     }
122     }
123     if (ldot < -FTINY && np->tdiff > FTINY) {
124     /*
125     * Compute diffuse transmission.
126     */
127     copycolor(ctmp, np->mcolor);
128     dtmp = -ldot * omega * np->tdiff / PI;
129     scalecolor(ctmp, dtmp);
130     addcolor(cval, ctmp);
131     }
132 greg 2.2 if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) {
133 greg 1.3 /*
134 greg 1.4 * Compute specular transmission. Specular transmission
135 greg 1.13 * is always modified by material color.
136 greg 1.3 */
137     /* roughness + source */
138 greg 2.19 dtmp = np->alpha2 + omega/PI;
139 greg 1.3 /* gaussian */
140 greg 2.18 dtmp = exp((2.*DOT(np->prdir,ldir)-2.)/dtmp)/(4.*PI*dtmp);
141 greg 1.3 /* worth using? */
142     if (dtmp > FTINY) {
143 greg 1.13 copycolor(ctmp, np->mcolor);
144 greg 2.18 dtmp *= np->tspec * omega * sqrt(-ldot/np->pdot);
145 greg 1.13 scalecolor(ctmp, dtmp);
146 greg 1.3 addcolor(cval, ctmp);
147     }
148     }
149     }
150    
151    
152 greg 2.2 m_normal(m, r) /* color a ray that hit something normal */
153 greg 1.3 register OBJREC *m;
154     register RAY *r;
155     {
156     NORMDAT nd;
157 greg 1.9 double transtest, transdist;
158 greg 1.1 double dtmp;
159     COLOR ctmp;
160     register int i;
161     /* easy shadow test */
162     if (r->crtype & SHADOW && m->otype != MAT_TRANS)
163     return;
164 greg 2.2
165     if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
166     objerror(m, USER, "bad number of arguments");
167 greg 1.3 nd.mp = m;
168 greg 2.16 nd.rp = r;
169 greg 1.1 /* get material color */
170 greg 1.3 setcolor(nd.mcolor, m->oargs.farg[0],
171 greg 1.1 m->oargs.farg[1],
172     m->oargs.farg[2]);
173     /* get roughness */
174 greg 2.2 nd.specfl = 0;
175 greg 1.3 nd.alpha2 = m->oargs.farg[4];
176 greg 2.2 if ((nd.alpha2 *= nd.alpha2) <= FTINY)
177     nd.specfl |= SP_PURE;
178 greg 1.1 /* reorient if necessary */
179     if (r->rod < 0.0)
180     flipsurface(r);
181     /* get modifiers */
182     raytexture(r, m->omod);
183 greg 1.3 nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */
184 greg 1.13 if (nd.pdot < .001)
185     nd.pdot = .001; /* non-zero for dirnorm() */
186 greg 1.3 multcolor(nd.mcolor, r->pcol); /* modify material color */
187 greg 1.9 transtest = 0;
188 greg 1.1 /* get specular component */
189 greg 2.2 if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
190     nd.specfl |= SP_REFL;
191 greg 1.1 /* compute specular color */
192     if (m->otype == MAT_METAL)
193 greg 1.3 copycolor(nd.scolor, nd.mcolor);
194 greg 1.1 else
195 greg 1.3 setcolor(nd.scolor, 1.0, 1.0, 1.0);
196     scalecolor(nd.scolor, nd.rspec);
197 greg 2.15 /* improved model */
198     dtmp = exp(-BSPEC(m)*nd.pdot);
199     for (i = 0; i < 3; i++)
200     colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
201     nd.rspec += (1.0-nd.rspec)*dtmp;
202     /* check threshold */
203     if (!(nd.specfl & SP_PURE) &&
204     specthresh > FTINY &&
205 greg 2.13 (specthresh >= 1.-FTINY ||
206 greg 2.17 specthresh + .05 - .1*frandom() > nd.rspec))
207 greg 2.5 nd.specfl |= SP_RBLT;
208 greg 1.1 /* compute reflected ray */
209     for (i = 0; i < 3; i++)
210 greg 1.3 nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
211 greg 2.7 if (DOT(nd.vrefl, r->ron) <= FTINY) /* penetration? */
212     for (i = 0; i < 3; i++) /* safety measure */
213     nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
214 greg 1.1
215 greg 2.2 if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
216 greg 1.3 RAY lr;
217     if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
218     VCOPY(lr.rdir, nd.vrefl);
219 greg 1.1 rayvalue(&lr);
220 greg 1.3 multcolor(lr.rcol, nd.scolor);
221 greg 1.1 addcolor(r->rcol, lr.rcol);
222     }
223 greg 1.3 }
224 greg 1.1 }
225 greg 1.3 /* compute transmission */
226 greg 1.1 if (m->otype == MAT_TRANS) {
227 greg 1.3 nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec);
228     nd.tspec = nd.trans * m->oargs.farg[6];
229     nd.tdiff = nd.trans - nd.tspec;
230 greg 2.2 if (nd.tspec > FTINY) {
231     nd.specfl |= SP_TRAN;
232 greg 2.5 /* check threshold */
233 greg 2.13 if (!(nd.specfl & SP_PURE) && specthresh > FTINY &&
234     (specthresh >= 1.-FTINY ||
235 greg 2.17 specthresh + .05 - .1*frandom() > nd.tspec))
236 greg 2.5 nd.specfl |= SP_TBLT;
237 greg 2.2 if (r->crtype & SHADOW ||
238     DOT(r->pert,r->pert) <= FTINY*FTINY) {
239     VCOPY(nd.prdir, r->rdir);
240     transtest = 2;
241     } else {
242     for (i = 0; i < 3; i++) /* perturb */
243 greg 2.19 nd.prdir[i] = r->rdir[i] - r->pert[i];
244 greg 2.7 if (DOT(nd.prdir, r->ron) < -FTINY)
245     normalize(nd.prdir); /* OK */
246     else
247     VCOPY(nd.prdir, r->rdir);
248 greg 2.2 }
249 greg 1.14 }
250 greg 1.1 } else
251 greg 1.3 nd.tdiff = nd.tspec = nd.trans = 0.0;
252 greg 1.1 /* transmitted ray */
253 greg 2.2 if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
254 greg 1.3 RAY lr;
255     if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
256 greg 1.14 VCOPY(lr.rdir, nd.prdir);
257 greg 1.1 rayvalue(&lr);
258 greg 1.3 scalecolor(lr.rcol, nd.tspec);
259 greg 1.8 multcolor(lr.rcol, nd.mcolor); /* modified by color */
260 greg 1.1 addcolor(r->rcol, lr.rcol);
261 greg 1.9 transtest *= bright(lr.rcol);
262     transdist = r->rot + lr.rt;
263 greg 1.1 }
264 greg 2.11 } else
265     transtest = 0;
266 greg 2.2
267 greg 1.1 if (r->crtype & SHADOW) /* the rest is shadow */
268     return;
269     /* diffuse reflection */
270 greg 1.3 nd.rdiff = 1.0 - nd.trans - nd.rspec;
271 greg 1.1
272 greg 2.2 if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
273     return; /* 100% pure specular */
274 greg 2.3
275 greg 2.12 if (r->ro != NULL && (r->ro->otype == OBJ_FACE ||
276     r->ro->otype == OBJ_RING))
277 greg 2.3 nd.specfl |= SP_FLAT;
278 greg 1.1
279 greg 2.2 if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE))
280     gaussamp(r, &nd);
281    
282 greg 1.3 if (nd.rdiff > FTINY) { /* ambient from this side */
283 greg 1.2 ambient(ctmp, r);
284 greg 2.5 if (nd.specfl & SP_RBLT)
285     scalecolor(ctmp, 1.0-nd.trans);
286     else
287     scalecolor(ctmp, nd.rdiff);
288 greg 1.3 multcolor(ctmp, nd.mcolor); /* modified by material color */
289 greg 1.2 addcolor(r->rcol, ctmp); /* add to returned color */
290     }
291 greg 1.3 if (nd.tdiff > FTINY) { /* ambient from other side */
292 greg 1.1 flipsurface(r);
293 greg 1.2 ambient(ctmp, r);
294 greg 2.5 if (nd.specfl & SP_TBLT)
295     scalecolor(ctmp, nd.trans);
296     else
297     scalecolor(ctmp, nd.tdiff);
298 greg 1.13 multcolor(ctmp, nd.mcolor); /* modified by color */
299 greg 1.1 addcolor(r->rcol, ctmp);
300     flipsurface(r);
301     }
302 greg 1.3 /* add direct component */
303     direct(r, dirnorm, &nd);
304 greg 1.9 /* check distance */
305     if (transtest > bright(r->rcol))
306     r->rt = transdist;
307 greg 2.2 }
308    
309    
310     static
311     gaussamp(r, np) /* sample gaussian specular */
312     RAY *r;
313     register NORMDAT *np;
314     {
315     RAY sr;
316     FVECT u, v, h;
317     double rv[2];
318     double d, sinp, cosp;
319     register int i;
320 greg 2.13 /* quick test */
321     if ((np->specfl & (SP_REFL|SP_RBLT)) != SP_REFL &&
322     (np->specfl & (SP_TRAN|SP_TBLT)) != SP_TRAN)
323     return;
324 greg 2.2 /* set up sample coordinates */
325     v[0] = v[1] = v[2] = 0.0;
326     for (i = 0; i < 3; i++)
327     if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6)
328     break;
329     v[i] = 1.0;
330     fcross(u, v, np->pnorm);
331     normalize(u);
332     fcross(v, np->pnorm, u);
333     /* compute reflection */
334 greg 2.5 if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
335 greg 2.2 rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
336     dimlist[ndims++] = (int)np->mp;
337 greg 2.7 d = urand(ilhash(dimlist,ndims)+samplendx);
338     multisamp(rv, 2, d);
339     d = 2.0*PI * rv[0];
340     cosp = cos(d);
341     sinp = sin(d);
342     rv[1] = 1.0 - specjitter*rv[1];
343     if (rv[1] <= FTINY)
344     d = 1.0;
345     else
346     d = sqrt( np->alpha2 * -log(rv[1]) );
347     for (i = 0; i < 3; i++)
348     h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
349     d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
350     for (i = 0; i < 3; i++)
351     sr.rdir[i] = r->rdir[i] + d*h[i];
352     if (DOT(sr.rdir, r->ron) <= FTINY)
353     VCOPY(sr.rdir, np->vrefl); /* jitter no good */
354     rayvalue(&sr);
355     multcolor(sr.rcol, np->scolor);
356     addcolor(r->rcol, sr.rcol);
357 greg 2.2 ndims--;
358     }
359     /* compute transmission */
360 greg 2.8 if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
361     rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
362     dimlist[ndims++] = (int)np->mp;
363     d = urand(ilhash(dimlist,ndims)+1823+samplendx);
364     multisamp(rv, 2, d);
365     d = 2.0*PI * rv[0];
366     cosp = cos(d);
367     sinp = sin(d);
368     rv[1] = 1.0 - specjitter*rv[1];
369     if (rv[1] <= FTINY)
370     d = 1.0;
371     else
372     d = sqrt( np->alpha2/4.0 * -log(rv[1]) );
373     for (i = 0; i < 3; i++)
374     sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]);
375     if (DOT(sr.rdir, r->ron) < -FTINY)
376     normalize(sr.rdir); /* OK, normalize */
377     else
378     VCOPY(sr.rdir, np->prdir); /* else no jitter */
379     rayvalue(&sr);
380 greg 2.11 scalecolor(sr.rcol, np->tspec);
381     multcolor(sr.rcol, np->mcolor); /* modified by color */
382 greg 2.8 addcolor(r->rcol, sr.rcol);
383     ndims--;
384     }
385 greg 1.1 }