ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/normal.c
Revision: 2.4
Committed: Tue Jan 14 15:33:08 1992 UTC (32 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +24 -23 lines
Log Message:
fixed bug in sampling code

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 1.1 /*
24     * This routine uses portions of the reflection
25     * model described by Cook and Torrance.
26     * The computation of specular components has been simplified by
27     * numerous approximations and ommisions to improve speed.
28     * We orient the surface towards the incoming ray, so a single
29     * surface can be used to represent an infinitely thin object.
30     *
31     * Arguments for MAT_PLASTIC and MAT_METAL are:
32     * red grn blu specular-frac. facet-slope
33     *
34     * Arguments for MAT_TRANS are:
35     * red grn blu rspec rough trans tspec
36     */
37    
38     #define BSPEC(m) (6.0) /* specularity parameter b */
39    
40 greg 2.2 /* 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 greg 2.3 #define SP_FLAT 020 /* flat reflecting surface */
45 greg 1.1
46 greg 1.3 typedef struct {
47     OBJREC *mp; /* material pointer */
48 greg 2.2 short specfl; /* specularity flags, defined above */
49 greg 1.1 COLOR mcolor; /* color of this material */
50     COLOR scolor; /* color of specular component */
51     FVECT vrefl; /* vector in direction of reflected ray */
52 greg 1.14 FVECT prdir; /* vector in transmitted direction */
53 greg 2.2 double alpha2; /* roughness squared */
54 greg 1.1 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 greg 1.3 } 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 greg 1.1 double ldot;
69 greg 1.3 double dtmp;
70 greg 2.3 int i;
71 greg 1.3 COLOR ctmp;
72    
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 greg 1.4 * 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 greg 1.3 */
86     copycolor(ctmp, np->mcolor);
87     dtmp = ldot * omega * np->rdiff / PI;
88     scalecolor(ctmp, dtmp);
89     addcolor(cval, ctmp);
90     }
91 greg 2.2 if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) {
92 greg 1.3 /*
93     * Compute specular reflection coefficient using
94     * gaussian distribution model.
95     */
96 greg 2.3 /* roughness */
97     dtmp = 2.0*np->alpha2;
98     /* + source if flat */
99     if (np->specfl & SP_FLAT)
100     dtmp += omega/(2.0*PI);
101 greg 1.3 /* 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 greg 1.13 dtmp *= omega / np->pdot;
107 greg 1.3 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 greg 2.2 if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) {
121 greg 1.3 /*
122 greg 1.4 * Compute specular transmission. Specular transmission
123 greg 1.13 * is always modified by material color.
124 greg 1.3 */
125     /* roughness + source */
126     dtmp = np->alpha2 + omega/(2.0*PI);
127     /* gaussian */
128 greg 1.14 dtmp = exp((DOT(np->prdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp;
129 greg 1.3 /* worth using? */
130     if (dtmp > FTINY) {
131 greg 1.13 copycolor(ctmp, np->mcolor);
132     dtmp *= np->tspec * omega / np->pdot;
133     scalecolor(ctmp, dtmp);
134 greg 1.3 addcolor(cval, ctmp);
135     }
136     }
137     }
138    
139    
140 greg 2.2 m_normal(m, r) /* color a ray that hit something normal */
141 greg 1.3 register OBJREC *m;
142     register RAY *r;
143     {
144     NORMDAT nd;
145 greg 1.9 double transtest, transdist;
146 greg 1.1 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 greg 2.2
153     if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
154     objerror(m, USER, "bad number of arguments");
155 greg 1.3 nd.mp = m;
156 greg 1.1 /* get material color */
157 greg 1.3 setcolor(nd.mcolor, m->oargs.farg[0],
158 greg 1.1 m->oargs.farg[1],
159     m->oargs.farg[2]);
160     /* get roughness */
161 greg 2.2 nd.specfl = 0;
162 greg 1.3 nd.alpha2 = m->oargs.farg[4];
163 greg 2.2 if ((nd.alpha2 *= nd.alpha2) <= FTINY)
164     nd.specfl |= SP_PURE;
165 greg 1.1 /* reorient if necessary */
166     if (r->rod < 0.0)
167     flipsurface(r);
168     /* get modifiers */
169     raytexture(r, m->omod);
170 greg 1.3 nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */
171 greg 1.13 if (nd.pdot < .001)
172     nd.pdot = .001; /* non-zero for dirnorm() */
173 greg 1.3 multcolor(nd.mcolor, r->pcol); /* modify material color */
174 greg 1.9 transtest = 0;
175 greg 1.1 /* get specular component */
176 greg 2.2 if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
177     nd.specfl |= SP_REFL;
178 greg 1.1 /* compute specular color */
179     if (m->otype == MAT_METAL)
180 greg 1.3 copycolor(nd.scolor, nd.mcolor);
181 greg 1.1 else
182 greg 1.3 setcolor(nd.scolor, 1.0, 1.0, 1.0);
183     scalecolor(nd.scolor, nd.rspec);
184 greg 1.1 /* improved model */
185 greg 1.3 dtmp = exp(-BSPEC(m)*nd.pdot);
186 greg 1.1 for (i = 0; i < 3; i++)
187 greg 1.3 colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
188     nd.rspec += (1.0-nd.rspec)*dtmp;
189 greg 1.1 /* compute reflected ray */
190     for (i = 0; i < 3; i++)
191 greg 1.3 nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
192 greg 1.1
193 greg 2.2 if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
194 greg 1.3 RAY lr;
195     if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
196     VCOPY(lr.rdir, nd.vrefl);
197 greg 1.1 rayvalue(&lr);
198 greg 1.3 multcolor(lr.rcol, nd.scolor);
199 greg 1.1 addcolor(r->rcol, lr.rcol);
200     }
201 greg 1.3 }
202 greg 1.1 }
203 greg 1.3 /* compute transmission */
204 greg 1.1 if (m->otype == MAT_TRANS) {
205 greg 1.3 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 greg 2.2 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 greg 1.14 }
221 greg 1.1 } else
222 greg 1.3 nd.tdiff = nd.tspec = nd.trans = 0.0;
223 greg 1.1 /* transmitted ray */
224 greg 2.2 if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
225 greg 1.3 RAY lr;
226     if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
227 greg 1.14 VCOPY(lr.rdir, nd.prdir);
228 greg 1.1 rayvalue(&lr);
229 greg 1.3 scalecolor(lr.rcol, nd.tspec);
230 greg 1.8 multcolor(lr.rcol, nd.mcolor); /* modified by color */
231 greg 1.1 addcolor(r->rcol, lr.rcol);
232 greg 1.9 transtest *= bright(lr.rcol);
233     transdist = r->rot + lr.rt;
234 greg 1.1 }
235 greg 1.3 }
236 greg 2.2
237 greg 1.1 if (r->crtype & SHADOW) /* the rest is shadow */
238     return;
239     /* diffuse reflection */
240 greg 1.3 nd.rdiff = 1.0 - nd.trans - nd.rspec;
241 greg 1.1
242 greg 2.2 if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
243     return; /* 100% pure specular */
244 greg 2.3
245     if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING)
246     nd.specfl |= SP_FLAT;
247 greg 1.1
248 greg 2.2 if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE))
249     gaussamp(r, &nd);
250    
251 greg 1.3 if (nd.rdiff > FTINY) { /* ambient from this side */
252 greg 1.2 ambient(ctmp, r);
253 greg 2.2 scalecolor(ctmp, nd.rdiff);
254 greg 1.3 multcolor(ctmp, nd.mcolor); /* modified by material color */
255 greg 1.2 addcolor(r->rcol, ctmp); /* add to returned color */
256     }
257 greg 1.3 if (nd.tdiff > FTINY) { /* ambient from other side */
258 greg 1.1 flipsurface(r);
259 greg 1.2 ambient(ctmp, r);
260 greg 2.2 scalecolor(ctmp, nd.tdiff);
261 greg 1.13 multcolor(ctmp, nd.mcolor); /* modified by color */
262 greg 1.1 addcolor(r->rcol, ctmp);
263     flipsurface(r);
264     }
265 greg 1.3 /* add direct component */
266     direct(r, dirnorm, &nd);
267 greg 1.9 /* check distance */
268     if (transtest > bright(r->rcol))
269     r->rt = transdist;
270 greg 2.2 }
271    
272    
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 greg 2.4 int ntries;
283 greg 2.2 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     dimlist[ndims++] = (int)np->mp;
297 greg 2.4 for (ntries = 0; ntries < 10; ntries++) {
298     dimlist[ndims] = ntries * 8912;
299     d = urand(ilhash(dimlist,ndims+1)+samplendx);
300     multisamp(rv, 2, d);
301     d = 2.0*PI * rv[0];
302     cosp = cos(d);
303     sinp = sin(d);
304     if (rv[1] <= FTINY)
305     d = 1.0;
306     else
307     d = sqrt( np->alpha2 * -log(rv[1]) );
308     for (i = 0; i < 3; i++)
309     h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
310     d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
311     for (i = 0; i < 3; i++)
312     sr.rdir[i] = r->rdir[i] + d*h[i];
313     if (DOT(sr.rdir, r->ron) > FTINY) {
314     rayvalue(&sr);
315     multcolor(sr.rcol, np->scolor);
316     addcolor(r->rcol, sr.rcol);
317     break;
318     }
319     }
320 greg 2.2 ndims--;
321     }
322     /* compute transmission */
323 greg 1.1 }