ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/aniso.c
Revision: 2.18
Committed: Tue May 19 17:09:06 1992 UTC (31 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.17: +21 -20 lines
Log Message:
fixed screwed-up normalization in sample generation

File Contents

# User Rev Content
1 greg 2.1 /* Copyright (c) 1992 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * Shading functions for anisotropic materials.
9     */
10    
11     #include "ray.h"
12    
13     #include "otypes.h"
14    
15     #include "func.h"
16    
17     #include "random.h"
18    
19 greg 2.4 extern double specthresh; /* specular sampling threshold */
20     extern double specjitter; /* specular sampling jitter */
21    
22 greg 2.1 /*
23     * This anisotropic reflection model uses a variant on the
24     * exponential Gaussian used in normal.c.
25     * We orient the surface towards the incoming ray, so a single
26     * surface can be used to represent an infinitely thin object.
27     *
28     * Arguments for MAT_PLASTIC2 and MAT_METAL2 are:
29     * 4+ ux uy uz funcfile [transform...]
30     * 0
31     * 6 red grn blu specular-frac. u-facet-slope v-facet-slope
32     *
33     * Real arguments for MAT_TRANS2 are:
34     * 8 red grn blu rspec u-rough v-rough trans tspec
35     */
36    
37 greg 2.14 #define BSPEC(m) (6.0) /* specularity parameter b */
38    
39 greg 2.1 /* specularity flags */
40     #define SP_REFL 01 /* has reflected specular component */
41     #define SP_TRAN 02 /* has transmitted specular */
42 greg 2.10 #define SP_FLAT 04 /* reflecting surface is flat */
43     #define SP_RBLT 010 /* reflection below sample threshold */
44     #define SP_TBLT 020 /* transmission below threshold */
45     #define SP_BADU 040 /* bad u direction calculation */
46 greg 2.1
47     typedef struct {
48 greg 2.2 OBJREC *mp; /* material pointer */
49 greg 2.1 RAY *rp; /* ray pointer */
50     short specfl; /* specularity flags, defined above */
51     COLOR mcolor; /* color of this material */
52     COLOR scolor; /* color of specular component */
53 greg 2.6 FVECT vrefl; /* vector in reflected direction */
54 greg 2.1 FVECT prdir; /* vector in transmitted direction */
55     FVECT u, v; /* u and v vectors orienting anisotropy */
56 greg 2.18 double u_alpha; /* u roughness */
57     double v_alpha; /* v roughness */
58 greg 2.1 double rdiff, rspec; /* reflected specular, diffuse */
59     double trans; /* transmissivity */
60     double tdiff, tspec; /* transmitted specular, diffuse */
61     FVECT pnorm; /* perturbed surface normal */
62     double pdot; /* perturbed dot product */
63     } ANISODAT; /* anisotropic material data */
64    
65    
66     diraniso(cval, np, ldir, omega) /* compute source contribution */
67     COLOR cval; /* returned coefficient */
68     register ANISODAT *np; /* material data */
69     FVECT ldir; /* light source direction */
70     double omega; /* light source size */
71     {
72     double ldot;
73 greg 2.16 double dtmp, dtmp1, dtmp2;
74 greg 2.1 FVECT h;
75     double au2, av2;
76     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     * 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     */
91     copycolor(ctmp, np->mcolor);
92     dtmp = ldot * omega * np->rdiff / PI;
93     scalecolor(ctmp, dtmp);
94     addcolor(cval, ctmp);
95     }
96 greg 2.10 if (ldot > FTINY && (np->specfl&(SP_REFL|SP_BADU)) == SP_REFL) {
97 greg 2.1 /*
98     * Compute specular reflection coefficient using
99     * anisotropic gaussian distribution model.
100     */
101 greg 2.2 /* add source width if flat */
102     if (np->specfl & SP_FLAT)
103     au2 = av2 = omega/(4.0*PI);
104     else
105     au2 = av2 = 0.0;
106 greg 2.18 au2 += np->u_alpha*np->u_alpha;
107     av2 += np->v_alpha*np->v_alpha;
108 greg 2.1 /* half vector */
109     h[0] = ldir[0] - np->rp->rdir[0];
110     h[1] = ldir[1] - np->rp->rdir[1];
111     h[2] = ldir[2] - np->rp->rdir[2];
112     normalize(h);
113     /* ellipse */
114 greg 2.16 dtmp1 = DOT(np->u, h);
115     dtmp1 *= dtmp1 / au2;
116 greg 2.1 dtmp2 = DOT(np->v, h);
117     dtmp2 *= dtmp2 / av2;
118     /* gaussian */
119 greg 2.16 dtmp = (dtmp1 + dtmp2) / (1.0 + DOT(np->pnorm, h));
120     dtmp = exp(-2.0*dtmp) * 1.0/(4.0*PI)
121     * sqrt(ldot/(np->pdot*au2*av2));
122 greg 2.1 /* worth using? */
123     if (dtmp > FTINY) {
124     copycolor(ctmp, np->scolor);
125 greg 2.16 dtmp *= omega;
126 greg 2.1 scalecolor(ctmp, dtmp);
127     addcolor(cval, ctmp);
128     }
129     }
130     if (ldot < -FTINY && np->tdiff > FTINY) {
131     /*
132     * Compute diffuse transmission.
133     */
134     copycolor(ctmp, np->mcolor);
135     dtmp = -ldot * omega * np->tdiff / PI;
136     scalecolor(ctmp, dtmp);
137     addcolor(cval, ctmp);
138     }
139 greg 2.10 if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_BADU)) == SP_TRAN) {
140 greg 2.1 /*
141     * Compute specular transmission. Specular transmission
142     * is always modified by material color.
143     */
144     /* roughness + source */
145 greg 2.16 au2 = av2 = omega / PI;
146 greg 2.18 au2 += np->u_alpha*np->u_alpha;
147     av2 += np->v_alpha*np->v_alpha;
148 greg 2.16 /* "half vector" */
149     h[0] = ldir[0] - np->prdir[0];
150     h[1] = ldir[1] - np->prdir[1];
151     h[2] = ldir[2] - np->prdir[2];
152     dtmp = DOT(h,np->pnorm);
153     dtmp = DOT(h,h) - dtmp*dtmp;
154     if (dtmp > FTINY*FTINY) {
155     dtmp1 = DOT(h,np->u);
156     dtmp1 = dtmp1*dtmp1 / (au2*dtmp);
157     dtmp2 = DOT(h,np->v);
158     dtmp2 = dtmp2*dtmp2 / (av2*dtmp);
159     dtmp = 2. - 2.*DOT(ldir,np->prdir);
160     dtmp *= dtmp1 + dtmp2;
161     } else
162     dtmp = 0.0;
163 greg 2.1 /* gaussian */
164 greg 2.16 dtmp = exp(-dtmp) * 1.0/(4.0*PI)
165     * sqrt(-ldot/(np->pdot*au2*av2));
166 greg 2.1 /* worth using? */
167     if (dtmp > FTINY) {
168     copycolor(ctmp, np->mcolor);
169 greg 2.16 dtmp *= np->tspec * omega;
170 greg 2.1 scalecolor(ctmp, dtmp);
171     addcolor(cval, ctmp);
172     }
173     }
174     }
175    
176    
177     m_aniso(m, r) /* shade ray that hit something anisotropic */
178     register OBJREC *m;
179     register RAY *r;
180     {
181     ANISODAT nd;
182     double dtmp;
183     COLOR ctmp;
184     register int i;
185     /* easy shadow test */
186 greg 2.10 if (r->crtype & SHADOW)
187 greg 2.1 return;
188    
189     if (m->oargs.nfargs != (m->otype == MAT_TRANS2 ? 8 : 6))
190     objerror(m, USER, "bad number of real arguments");
191 greg 2.2 nd.mp = m;
192 greg 2.1 nd.rp = r;
193     /* get material color */
194     setcolor(nd.mcolor, m->oargs.farg[0],
195     m->oargs.farg[1],
196     m->oargs.farg[2]);
197     /* get roughness */
198     nd.specfl = 0;
199 greg 2.18 nd.u_alpha = m->oargs.farg[4];
200     nd.v_alpha = m->oargs.farg[5];
201     if (nd.u_alpha < FTINY || nd.v_alpha <= FTINY)
202 greg 2.10 objerror(m, USER, "roughness too small");
203 greg 2.1 /* reorient if necessary */
204     if (r->rod < 0.0)
205     flipsurface(r);
206     /* get modifiers */
207     raytexture(r, m->omod);
208     nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */
209     if (nd.pdot < .001)
210     nd.pdot = .001; /* non-zero for diraniso() */
211     multcolor(nd.mcolor, r->pcol); /* modify material color */
212     /* get specular component */
213     if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
214     nd.specfl |= SP_REFL;
215     /* compute specular color */
216     if (m->otype == MAT_METAL2)
217     copycolor(nd.scolor, nd.mcolor);
218     else
219     setcolor(nd.scolor, 1.0, 1.0, 1.0);
220     scalecolor(nd.scolor, nd.rspec);
221 greg 2.14 /* improved model */
222     dtmp = exp(-BSPEC(m)*nd.pdot);
223     for (i = 0; i < 3; i++)
224     colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
225     nd.rspec += (1.0-nd.rspec)*dtmp;
226 greg 2.4 /* check threshold */
227 greg 2.5 if (specthresh > FTINY &&
228 greg 2.12 (specthresh >= 1.-FTINY ||
229 greg 2.15 specthresh + .05 - .1*frandom() > nd.rspec))
230 greg 2.4 nd.specfl |= SP_RBLT;
231 greg 2.6 /* compute refl. direction */
232     for (i = 0; i < 3; i++)
233     nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
234     if (DOT(nd.vrefl, r->ron) <= FTINY) /* penetration? */
235     for (i = 0; i < 3; i++) /* safety measure */
236     nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
237 greg 2.1 }
238     /* compute transmission */
239 greg 2.16 if (m->otype == MAT_TRANS2) {
240 greg 2.1 nd.trans = m->oargs.farg[6]*(1.0 - nd.rspec);
241     nd.tspec = nd.trans * m->oargs.farg[7];
242     nd.tdiff = nd.trans - nd.tspec;
243     if (nd.tspec > FTINY) {
244     nd.specfl |= SP_TRAN;
245 greg 2.4 /* check threshold */
246 greg 2.5 if (specthresh > FTINY &&
247 greg 2.12 (specthresh >= 1.-FTINY ||
248 greg 2.15 specthresh + .05 - .1*frandom() > nd.tspec))
249 greg 2.4 nd.specfl |= SP_TBLT;
250 greg 2.10 if (DOT(r->pert,r->pert) <= FTINY*FTINY) {
251 greg 2.1 VCOPY(nd.prdir, r->rdir);
252     } else {
253     for (i = 0; i < 3; i++) /* perturb */
254 greg 2.17 nd.prdir[i] = r->rdir[i] - r->pert[i];
255 greg 2.6 if (DOT(nd.prdir, r->ron) < -FTINY)
256     normalize(nd.prdir); /* OK */
257     else
258     VCOPY(nd.prdir, r->rdir);
259 greg 2.1 }
260     }
261     } else
262     nd.tdiff = nd.tspec = nd.trans = 0.0;
263    
264     /* diffuse reflection */
265     nd.rdiff = 1.0 - nd.trans - nd.rspec;
266    
267 greg 2.11 if (r->ro != NULL && (r->ro->otype == OBJ_FACE ||
268     r->ro->otype == OBJ_RING))
269 greg 2.4 nd.specfl |= SP_FLAT;
270    
271 greg 2.1 getacoords(r, &nd); /* set up coordinates */
272    
273 greg 2.10 if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_BADU))
274 greg 2.1 agaussamp(r, &nd);
275    
276     if (nd.rdiff > FTINY) { /* ambient from this side */
277     ambient(ctmp, r);
278 greg 2.4 if (nd.specfl & SP_RBLT)
279     scalecolor(ctmp, 1.0-nd.trans);
280     else
281     scalecolor(ctmp, nd.rdiff);
282 greg 2.1 multcolor(ctmp, nd.mcolor); /* modified by material color */
283     addcolor(r->rcol, ctmp); /* add to returned color */
284     }
285     if (nd.tdiff > FTINY) { /* ambient from other side */
286     flipsurface(r);
287     ambient(ctmp, r);
288 greg 2.4 if (nd.specfl & SP_TBLT)
289     scalecolor(ctmp, nd.trans);
290     else
291     scalecolor(ctmp, nd.tdiff);
292 greg 2.1 multcolor(ctmp, nd.mcolor); /* modified by color */
293     addcolor(r->rcol, ctmp);
294     flipsurface(r);
295     }
296     /* add direct component */
297     direct(r, diraniso, &nd);
298     }
299    
300    
301     static
302     getacoords(r, np) /* set up coordinate system */
303     RAY *r;
304     register ANISODAT *np;
305     {
306     register MFUNC *mf;
307     register int i;
308    
309     mf = getfunc(np->mp, 3, 0x7, 1);
310     setfunc(np->mp, r);
311     errno = 0;
312     for (i = 0; i < 3; i++)
313     np->u[i] = evalue(mf->ep[i]);
314     if (errno) {
315     objerror(np->mp, WARNING, "compute error");
316     np->specfl |= SP_BADU;
317     return;
318     }
319 greg 2.16 if (mf->f != &unitxf)
320     multv3(np->u, np->u, mf->f->xfm);
321 greg 2.1 fcross(np->v, np->pnorm, np->u);
322     if (normalize(np->v) == 0.0) {
323     objerror(np->mp, WARNING, "illegal orientation vector");
324     np->specfl |= SP_BADU;
325     return;
326     }
327     fcross(np->u, np->v, np->pnorm);
328     }
329    
330    
331     static
332     agaussamp(r, np) /* sample anisotropic gaussian specular */
333     RAY *r;
334     register ANISODAT *np;
335     {
336     RAY sr;
337     FVECT h;
338     double rv[2];
339     double d, sinp, cosp;
340     register int i;
341     /* compute reflection */
342 greg 2.4 if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
343 greg 2.1 rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
344     dimlist[ndims++] = (int)np->mp;
345 greg 2.6 d = urand(ilhash(dimlist,ndims)+samplendx);
346     multisamp(rv, 2, d);
347     d = 2.0*PI * rv[0];
348 greg 2.18 cosp = cos(d) * np->u_alpha;
349     sinp = sin(d) * np->v_alpha;
350     d = sqrt(cosp*cosp + sinp*sinp);
351 greg 2.6 cosp /= d;
352     sinp /= d;
353     rv[1] = 1.0 - specjitter*rv[1];
354     if (rv[1] <= FTINY)
355     d = 1.0;
356     else
357     d = sqrt(-log(rv[1]) /
358 greg 2.18 (cosp*cosp/(np->u_alpha*np->u_alpha) +
359     sinp*sinp/(np->v_alpha*np->v_alpha)));
360 greg 2.6 for (i = 0; i < 3; i++)
361     h[i] = np->pnorm[i] +
362     d*(cosp*np->u[i] + sinp*np->v[i]);
363     d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
364     for (i = 0; i < 3; i++)
365     sr.rdir[i] = r->rdir[i] + d*h[i];
366     if (DOT(sr.rdir, r->ron) <= FTINY) /* penetration? */
367     VCOPY(sr.rdir, np->vrefl); /* jitter no good */
368     rayvalue(&sr);
369     multcolor(sr.rcol, np->scolor);
370     addcolor(r->rcol, sr.rcol);
371 greg 2.1 ndims--;
372     }
373     /* compute transmission */
374 greg 2.7 if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
375     rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
376     dimlist[ndims++] = (int)np->mp;
377     d = urand(ilhash(dimlist,ndims)+1823+samplendx);
378     multisamp(rv, 2, d);
379     d = 2.0*PI * rv[0];
380 greg 2.18 cosp = cos(d) * np->u_alpha;
381     sinp = sin(d) * np->v_alpha;
382     d = sqrt(cosp*cosp + sinp*sinp);
383     cosp /= d;
384     sinp /= d;
385 greg 2.7 rv[1] = 1.0 - specjitter*rv[1];
386     if (rv[1] <= FTINY)
387     d = 1.0;
388     else
389     d = sqrt(-log(rv[1]) /
390 greg 2.18 (cosp*cosp/(np->u_alpha*np->u_alpha) +
391     sinp*sinp/(np->v_alpha*np->u_alpha)));
392 greg 2.7 for (i = 0; i < 3; i++)
393     sr.rdir[i] = np->prdir[i] +
394     d*(cosp*np->u[i] + sinp*np->v[i]);
395     if (DOT(sr.rdir, r->ron) < -FTINY)
396     normalize(sr.rdir); /* OK, normalize */
397     else
398     VCOPY(sr.rdir, np->prdir); /* else no jitter */
399     rayvalue(&sr);
400 greg 2.10 scalecolor(sr.rcol, np->tspec);
401     multcolor(sr.rcol, np->mcolor); /* modify by color */
402 greg 2.7 addcolor(r->rcol, sr.rcol);
403     ndims--;
404     }
405 greg 2.1 }