ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/normal.c
Revision: 2.8
Committed: Wed Jan 15 16:59:52 1992 UTC (32 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.7: +26 -2 lines
Log Message:
finally added sampling of transmitted specular

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     #define SP_PURE 010 /* purely specular (zero roughness) */
47 greg 2.3 #define SP_FLAT 020 /* flat reflecting surface */
48 greg 2.5 #define SP_RBLT 040 /* reflection below sample threshold */
49     #define SP_TBLT 0100 /* transmission below threshold */
50 greg 1.1
51 greg 1.3 typedef struct {
52     OBJREC *mp; /* material pointer */
53 greg 2.2 short specfl; /* specularity flags, defined above */
54 greg 1.1 COLOR mcolor; /* color of this material */
55     COLOR scolor; /* color of specular component */
56     FVECT vrefl; /* vector in direction of reflected ray */
57 greg 1.14 FVECT prdir; /* vector in transmitted direction */
58 greg 2.2 double alpha2; /* roughness squared */
59 greg 1.1 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 greg 1.3 } 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 greg 1.1 double ldot;
74 greg 1.3 double dtmp;
75 greg 2.3 int i;
76 greg 1.3 COLOR ctmp;
77    
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 greg 1.4 * 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 greg 1.3 */
91     copycolor(ctmp, np->mcolor);
92     dtmp = ldot * omega * np->rdiff / PI;
93     scalecolor(ctmp, dtmp);
94     addcolor(cval, ctmp);
95     }
96 greg 2.2 if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE)) == SP_REFL) {
97 greg 1.3 /*
98     * Compute specular reflection coefficient using
99     * gaussian distribution model.
100     */
101 greg 2.3 /* roughness */
102     dtmp = 2.0*np->alpha2;
103     /* + source if flat */
104     if (np->specfl & SP_FLAT)
105     dtmp += omega/(2.0*PI);
106 greg 1.3 /* 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 greg 1.13 dtmp *= omega / np->pdot;
112 greg 1.3 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 greg 2.2 if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE)) == SP_TRAN) {
126 greg 1.3 /*
127 greg 1.4 * Compute specular transmission. Specular transmission
128 greg 1.13 * is always modified by material color.
129 greg 1.3 */
130     /* roughness + source */
131 greg 2.8 dtmp = np->alpha2/2.0 + omega/(2.0*PI);
132 greg 1.3 /* gaussian */
133 greg 1.14 dtmp = exp((DOT(np->prdir,ldir)-1.)/dtmp)/(2.*PI)/dtmp;
134 greg 1.3 /* worth using? */
135     if (dtmp > FTINY) {
136 greg 1.13 copycolor(ctmp, np->mcolor);
137     dtmp *= np->tspec * omega / np->pdot;
138     scalecolor(ctmp, dtmp);
139 greg 1.3 addcolor(cval, ctmp);
140     }
141     }
142     }
143    
144    
145 greg 2.2 m_normal(m, r) /* color a ray that hit something normal */
146 greg 1.3 register OBJREC *m;
147     register RAY *r;
148     {
149     NORMDAT nd;
150 greg 1.9 double transtest, transdist;
151 greg 1.1 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 greg 2.2
158     if (m->oargs.nfargs != (m->otype == MAT_TRANS ? 7 : 5))
159     objerror(m, USER, "bad number of arguments");
160 greg 1.3 nd.mp = m;
161 greg 1.1 /* get material color */
162 greg 1.3 setcolor(nd.mcolor, m->oargs.farg[0],
163 greg 1.1 m->oargs.farg[1],
164     m->oargs.farg[2]);
165     /* get roughness */
166 greg 2.2 nd.specfl = 0;
167 greg 1.3 nd.alpha2 = m->oargs.farg[4];
168 greg 2.2 if ((nd.alpha2 *= nd.alpha2) <= FTINY)
169     nd.specfl |= SP_PURE;
170 greg 1.1 /* reorient if necessary */
171     if (r->rod < 0.0)
172     flipsurface(r);
173     /* get modifiers */
174     raytexture(r, m->omod);
175 greg 1.3 nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */
176 greg 1.13 if (nd.pdot < .001)
177     nd.pdot = .001; /* non-zero for dirnorm() */
178 greg 1.3 multcolor(nd.mcolor, r->pcol); /* modify material color */
179 greg 1.9 transtest = 0;
180 greg 1.1 /* get specular component */
181 greg 2.2 if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
182     nd.specfl |= SP_REFL;
183 greg 1.1 /* compute specular color */
184     if (m->otype == MAT_METAL)
185 greg 1.3 copycolor(nd.scolor, nd.mcolor);
186 greg 1.1 else
187 greg 1.3 setcolor(nd.scolor, 1.0, 1.0, 1.0);
188     scalecolor(nd.scolor, nd.rspec);
189 greg 1.1 /* improved model */
190 greg 1.3 dtmp = exp(-BSPEC(m)*nd.pdot);
191 greg 1.1 for (i = 0; i < 3; i++)
192 greg 1.3 colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
193     nd.rspec += (1.0-nd.rspec)*dtmp;
194 greg 2.5 /* check threshold */
195 greg 2.6 if (specthresh > FTINY &&
196     ((specthresh >= 1.-FTINY ||
197     specthresh + (.1 - .2*urand(8199+samplendx))
198     > nd.rspec)))
199 greg 2.5 nd.specfl |= SP_RBLT;
200 greg 1.1 /* compute reflected ray */
201     for (i = 0; i < 3; i++)
202 greg 1.3 nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
203 greg 2.7 if (DOT(nd.vrefl, r->ron) <= FTINY) /* penetration? */
204     for (i = 0; i < 3; i++) /* safety measure */
205     nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
206 greg 1.1
207 greg 2.2 if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
208 greg 1.3 RAY lr;
209     if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
210     VCOPY(lr.rdir, nd.vrefl);
211 greg 1.1 rayvalue(&lr);
212 greg 1.3 multcolor(lr.rcol, nd.scolor);
213 greg 1.1 addcolor(r->rcol, lr.rcol);
214     }
215 greg 1.3 }
216 greg 1.1 }
217 greg 1.3 /* compute transmission */
218 greg 1.1 if (m->otype == MAT_TRANS) {
219 greg 1.3 nd.trans = m->oargs.farg[5]*(1.0 - nd.rspec);
220     nd.tspec = nd.trans * m->oargs.farg[6];
221     nd.tdiff = nd.trans - nd.tspec;
222 greg 2.2 if (nd.tspec > FTINY) {
223     nd.specfl |= SP_TRAN;
224 greg 2.5 /* check threshold */
225 greg 2.6 if (specthresh > FTINY &&
226     ((specthresh >= 1.-FTINY ||
227     specthresh +
228     (.1 - .2*urand(7241+samplendx))
229     > nd.tspec)))
230 greg 2.5 nd.specfl |= SP_TBLT;
231 greg 2.2 if (r->crtype & SHADOW ||
232     DOT(r->pert,r->pert) <= FTINY*FTINY) {
233     VCOPY(nd.prdir, r->rdir);
234     transtest = 2;
235     } else {
236     for (i = 0; i < 3; i++) /* perturb */
237     nd.prdir[i] = r->rdir[i] -
238 greg 2.8 0.5*r->pert[i];
239 greg 2.7 if (DOT(nd.prdir, r->ron) < -FTINY)
240     normalize(nd.prdir); /* OK */
241     else
242     VCOPY(nd.prdir, r->rdir);
243 greg 2.2 }
244 greg 1.14 }
245 greg 1.1 } else
246 greg 1.3 nd.tdiff = nd.tspec = nd.trans = 0.0;
247 greg 1.1 /* transmitted ray */
248 greg 2.2 if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
249 greg 1.3 RAY lr;
250     if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
251 greg 1.14 VCOPY(lr.rdir, nd.prdir);
252 greg 1.1 rayvalue(&lr);
253 greg 1.3 scalecolor(lr.rcol, nd.tspec);
254 greg 1.8 multcolor(lr.rcol, nd.mcolor); /* modified by color */
255 greg 1.1 addcolor(r->rcol, lr.rcol);
256 greg 1.9 transtest *= bright(lr.rcol);
257     transdist = r->rot + lr.rt;
258 greg 1.1 }
259 greg 1.3 }
260 greg 2.2
261 greg 1.1 if (r->crtype & SHADOW) /* the rest is shadow */
262     return;
263     /* diffuse reflection */
264 greg 1.3 nd.rdiff = 1.0 - nd.trans - nd.rspec;
265 greg 1.1
266 greg 2.2 if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
267     return; /* 100% pure specular */
268 greg 2.3
269     if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING)
270     nd.specfl |= SP_FLAT;
271 greg 1.1
272 greg 2.2 if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_PURE))
273     gaussamp(r, &nd);
274    
275 greg 1.3 if (nd.rdiff > FTINY) { /* ambient from this side */
276 greg 1.2 ambient(ctmp, r);
277 greg 2.5 if (nd.specfl & SP_RBLT)
278     scalecolor(ctmp, 1.0-nd.trans);
279     else
280     scalecolor(ctmp, nd.rdiff);
281 greg 1.3 multcolor(ctmp, nd.mcolor); /* modified by material color */
282 greg 1.2 addcolor(r->rcol, ctmp); /* add to returned color */
283     }
284 greg 1.3 if (nd.tdiff > FTINY) { /* ambient from other side */
285 greg 1.1 flipsurface(r);
286 greg 1.2 ambient(ctmp, r);
287 greg 2.5 if (nd.specfl & SP_TBLT)
288     scalecolor(ctmp, nd.trans);
289     else
290     scalecolor(ctmp, nd.tdiff);
291 greg 1.13 multcolor(ctmp, nd.mcolor); /* modified by color */
292 greg 1.1 addcolor(r->rcol, ctmp);
293     flipsurface(r);
294     }
295 greg 1.3 /* add direct component */
296     direct(r, dirnorm, &nd);
297 greg 1.9 /* check distance */
298     if (transtest > bright(r->rcol))
299     r->rt = transdist;
300 greg 2.2 }
301    
302    
303     static
304     gaussamp(r, np) /* sample gaussian specular */
305     RAY *r;
306     register NORMDAT *np;
307     {
308     RAY sr;
309     FVECT u, v, h;
310     double rv[2];
311     double d, sinp, cosp;
312     register int i;
313     /* set up sample coordinates */
314     v[0] = v[1] = v[2] = 0.0;
315     for (i = 0; i < 3; i++)
316     if (np->pnorm[i] < 0.6 && np->pnorm[i] > -0.6)
317     break;
318     v[i] = 1.0;
319     fcross(u, v, np->pnorm);
320     normalize(u);
321     fcross(v, np->pnorm, u);
322     /* compute reflection */
323 greg 2.5 if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
324 greg 2.2 rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
325     dimlist[ndims++] = (int)np->mp;
326 greg 2.7 d = urand(ilhash(dimlist,ndims)+samplendx);
327     multisamp(rv, 2, d);
328     d = 2.0*PI * rv[0];
329     cosp = cos(d);
330     sinp = sin(d);
331     rv[1] = 1.0 - specjitter*rv[1];
332     if (rv[1] <= FTINY)
333     d = 1.0;
334     else
335     d = sqrt( np->alpha2 * -log(rv[1]) );
336     for (i = 0; i < 3; i++)
337     h[i] = np->pnorm[i] + d*(cosp*u[i] + sinp*v[i]);
338     d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
339     for (i = 0; i < 3; i++)
340     sr.rdir[i] = r->rdir[i] + d*h[i];
341     if (DOT(sr.rdir, r->ron) <= FTINY)
342     VCOPY(sr.rdir, np->vrefl); /* jitter no good */
343     rayvalue(&sr);
344     multcolor(sr.rcol, np->scolor);
345     addcolor(r->rcol, sr.rcol);
346 greg 2.2 ndims--;
347     }
348     /* compute transmission */
349 greg 2.8 if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
350     rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
351     dimlist[ndims++] = (int)np->mp;
352     d = urand(ilhash(dimlist,ndims)+1823+samplendx);
353     multisamp(rv, 2, d);
354     d = 2.0*PI * rv[0];
355     cosp = cos(d);
356     sinp = sin(d);
357     rv[1] = 1.0 - specjitter*rv[1];
358     if (rv[1] <= FTINY)
359     d = 1.0;
360     else
361     d = sqrt( np->alpha2/4.0 * -log(rv[1]) );
362     for (i = 0; i < 3; i++)
363     sr.rdir[i] = np->prdir[i] + d*(cosp*u[i] + sinp*v[i]);
364     if (DOT(sr.rdir, r->ron) < -FTINY)
365     normalize(sr.rdir); /* OK, normalize */
366     else
367     VCOPY(sr.rdir, np->prdir); /* else no jitter */
368     rayvalue(&sr);
369     multcolor(sr.rcol, np->scolor);
370     addcolor(r->rcol, sr.rcol);
371     ndims--;
372     }
373 greg 1.1 }