ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/aniso.c
Revision: 2.35
Committed: Tue Feb 25 02:47:22 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.34: +1 -56 lines
Log Message:
Replaced inline copyright notice with #include "copyright.h"

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.34 static const char RCSid[] = "$Id$";
3 greg 2.1 #endif
4     /*
5     * Shading functions for anisotropic materials.
6     */
7    
8 greg 2.35 #include "copyright.h"
9 greg 2.34
10 greg 2.1 #include "ray.h"
11    
12     #include "otypes.h"
13    
14     #include "func.h"
15    
16     #include "random.h"
17    
18 greg 2.32 #ifndef MAXITER
19     #define MAXITER 10 /* maximum # specular ray attempts */
20     #endif
21    
22 greg 2.1 /*
23 greg 2.22 * This routine implements the anisotropic Gaussian
24     * model described by Ward in Siggraph `92 article.
25 greg 2.1 * 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     /* specularity flags */
38     #define SP_REFL 01 /* has reflected specular component */
39     #define SP_TRAN 02 /* has transmitted specular */
40 greg 2.10 #define SP_FLAT 04 /* reflecting surface is flat */
41     #define SP_RBLT 010 /* reflection below sample threshold */
42     #define SP_TBLT 020 /* transmission below threshold */
43     #define SP_BADU 040 /* bad u direction calculation */
44 greg 2.1
45     typedef struct {
46 greg 2.2 OBJREC *mp; /* material pointer */
47 greg 2.1 RAY *rp; /* ray pointer */
48     short specfl; /* specularity flags, defined above */
49     COLOR mcolor; /* color of this material */
50     COLOR scolor; /* color of specular component */
51 greg 2.6 FVECT vrefl; /* vector in reflected direction */
52 greg 2.1 FVECT prdir; /* vector in transmitted direction */
53     FVECT u, v; /* u and v vectors orienting anisotropy */
54 greg 2.18 double u_alpha; /* u roughness */
55     double v_alpha; /* v roughness */
56 greg 2.1 double rdiff, rspec; /* reflected specular, diffuse */
57     double trans; /* transmissivity */
58     double tdiff, tspec; /* transmitted specular, diffuse */
59     FVECT pnorm; /* perturbed surface normal */
60     double pdot; /* perturbed dot product */
61     } ANISODAT; /* anisotropic material data */
62    
63 greg 2.34 static void getacoords();
64     static void agaussamp();
65    
66 greg 2.1
67 greg 2.34 static void
68 greg 2.1 diraniso(cval, np, ldir, omega) /* compute source contribution */
69     COLOR cval; /* returned coefficient */
70     register ANISODAT *np; /* material data */
71     FVECT ldir; /* light source direction */
72     double omega; /* light source size */
73     {
74     double ldot;
75 greg 2.16 double dtmp, dtmp1, dtmp2;
76 greg 2.1 FVECT h;
77     double au2, av2;
78     COLOR ctmp;
79    
80     setcolor(cval, 0.0, 0.0, 0.0);
81    
82     ldot = DOT(np->pnorm, ldir);
83    
84     if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY)
85     return; /* wrong side */
86    
87     if (ldot > FTINY && np->rdiff > FTINY) {
88     /*
89     * Compute and add diffuse reflected component to returned
90     * color. The diffuse reflected component will always be
91     * modified by the color of the material.
92     */
93     copycolor(ctmp, np->mcolor);
94     dtmp = ldot * omega * np->rdiff / PI;
95     scalecolor(ctmp, dtmp);
96     addcolor(cval, ctmp);
97     }
98 greg 2.10 if (ldot > FTINY && (np->specfl&(SP_REFL|SP_BADU)) == SP_REFL) {
99 greg 2.1 /*
100     * Compute specular reflection coefficient using
101     * anisotropic gaussian distribution model.
102     */
103 greg 2.2 /* add source width if flat */
104     if (np->specfl & SP_FLAT)
105     au2 = av2 = omega/(4.0*PI);
106     else
107     au2 = av2 = 0.0;
108 greg 2.18 au2 += np->u_alpha*np->u_alpha;
109     av2 += np->v_alpha*np->v_alpha;
110 greg 2.1 /* half vector */
111     h[0] = ldir[0] - np->rp->rdir[0];
112     h[1] = ldir[1] - np->rp->rdir[1];
113     h[2] = ldir[2] - np->rp->rdir[2];
114     /* ellipse */
115 greg 2.16 dtmp1 = DOT(np->u, h);
116     dtmp1 *= dtmp1 / au2;
117 greg 2.1 dtmp2 = DOT(np->v, h);
118     dtmp2 *= dtmp2 / av2;
119     /* gaussian */
120 greg 2.23 dtmp = DOT(np->pnorm, h);
121     dtmp = (dtmp1 + dtmp2) / (dtmp*dtmp);
122     dtmp = exp(-dtmp) * (0.25/PI)
123 greg 2.16 * sqrt(ldot/(np->pdot*au2*av2));
124 greg 2.1 /* worth using? */
125     if (dtmp > FTINY) {
126     copycolor(ctmp, np->scolor);
127 greg 2.16 dtmp *= omega;
128 greg 2.1 scalecolor(ctmp, dtmp);
129     addcolor(cval, ctmp);
130     }
131     }
132     if (ldot < -FTINY && np->tdiff > FTINY) {
133     /*
134     * Compute diffuse transmission.
135     */
136     copycolor(ctmp, np->mcolor);
137     dtmp = -ldot * omega * np->tdiff / PI;
138     scalecolor(ctmp, dtmp);
139     addcolor(cval, ctmp);
140     }
141 greg 2.10 if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_BADU)) == SP_TRAN) {
142 greg 2.1 /*
143     * Compute specular transmission. Specular transmission
144     * is always modified by material color.
145     */
146     /* roughness + source */
147 greg 2.16 au2 = av2 = omega / PI;
148 greg 2.18 au2 += np->u_alpha*np->u_alpha;
149     av2 += np->v_alpha*np->v_alpha;
150 greg 2.16 /* "half vector" */
151     h[0] = ldir[0] - np->prdir[0];
152     h[1] = ldir[1] - np->prdir[1];
153     h[2] = ldir[2] - np->prdir[2];
154 greg 2.19 dtmp = DOT(h,h);
155 greg 2.16 if (dtmp > FTINY*FTINY) {
156 greg 2.19 dtmp1 = DOT(h,np->pnorm);
157     dtmp = 1.0 - dtmp1*dtmp1/dtmp;
158     if (dtmp > FTINY*FTINY) {
159     dtmp1 = DOT(h,np->u);
160 greg 2.23 dtmp1 *= dtmp1 / au2;
161 greg 2.19 dtmp2 = DOT(h,np->v);
162 greg 2.23 dtmp2 *= dtmp2 / av2;
163 greg 2.19 dtmp = (dtmp1 + dtmp2) / dtmp;
164     }
165 greg 2.16 } else
166     dtmp = 0.0;
167 greg 2.1 /* gaussian */
168 greg 2.21 dtmp = exp(-dtmp) * (1.0/PI)
169 greg 2.16 * sqrt(-ldot/(np->pdot*au2*av2));
170 greg 2.1 /* worth using? */
171     if (dtmp > FTINY) {
172     copycolor(ctmp, np->mcolor);
173 greg 2.16 dtmp *= np->tspec * omega;
174 greg 2.1 scalecolor(ctmp, dtmp);
175     addcolor(cval, ctmp);
176     }
177     }
178     }
179    
180    
181 greg 2.34 int
182 greg 2.1 m_aniso(m, r) /* shade ray that hit something anisotropic */
183     register OBJREC *m;
184     register RAY *r;
185     {
186     ANISODAT nd;
187     COLOR ctmp;
188     register int i;
189     /* easy shadow test */
190 greg 2.10 if (r->crtype & SHADOW)
191 greg 2.27 return(1);
192 greg 2.1
193     if (m->oargs.nfargs != (m->otype == MAT_TRANS2 ? 8 : 6))
194     objerror(m, USER, "bad number of real arguments");
195 greg 2.2 nd.mp = m;
196 greg 2.1 nd.rp = r;
197     /* get material color */
198     setcolor(nd.mcolor, m->oargs.farg[0],
199     m->oargs.farg[1],
200     m->oargs.farg[2]);
201     /* get roughness */
202     nd.specfl = 0;
203 greg 2.18 nd.u_alpha = m->oargs.farg[4];
204     nd.v_alpha = m->oargs.farg[5];
205     if (nd.u_alpha < FTINY || nd.v_alpha <= FTINY)
206 greg 2.10 objerror(m, USER, "roughness too small");
207 greg 2.28 /* check for back side */
208     if (r->rod < 0.0) {
209     if (!backvis && m->otype != MAT_TRANS2) {
210     raytrans(r);
211     return(1);
212     }
213     flipsurface(r); /* reorient if backvis */
214     }
215 greg 2.1 /* get modifiers */
216     raytexture(r, m->omod);
217     nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */
218     if (nd.pdot < .001)
219     nd.pdot = .001; /* non-zero for diraniso() */
220     multcolor(nd.mcolor, r->pcol); /* modify material color */
221     /* get specular component */
222     if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
223     nd.specfl |= SP_REFL;
224     /* compute specular color */
225     if (m->otype == MAT_METAL2)
226     copycolor(nd.scolor, nd.mcolor);
227     else
228     setcolor(nd.scolor, 1.0, 1.0, 1.0);
229     scalecolor(nd.scolor, nd.rspec);
230 greg 2.4 /* check threshold */
231 greg 2.25 if (specthresh >= nd.rspec-FTINY)
232 greg 2.4 nd.specfl |= SP_RBLT;
233 greg 2.6 /* compute refl. direction */
234     for (i = 0; i < 3; i++)
235     nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
236     if (DOT(nd.vrefl, r->ron) <= FTINY) /* penetration? */
237     for (i = 0; i < 3; i++) /* safety measure */
238     nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
239 greg 2.1 }
240     /* compute transmission */
241 greg 2.16 if (m->otype == MAT_TRANS2) {
242 greg 2.1 nd.trans = m->oargs.farg[6]*(1.0 - nd.rspec);
243     nd.tspec = nd.trans * m->oargs.farg[7];
244     nd.tdiff = nd.trans - nd.tspec;
245     if (nd.tspec > FTINY) {
246     nd.specfl |= SP_TRAN;
247 greg 2.4 /* check threshold */
248 greg 2.25 if (specthresh >= nd.tspec-FTINY)
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.29 if (r->ro != NULL && isflat(r->ro->otype))
268 greg 2.4 nd.specfl |= SP_FLAT;
269    
270 greg 2.1 getacoords(r, &nd); /* set up coordinates */
271    
272 greg 2.10 if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_BADU))
273 greg 2.1 agaussamp(r, &nd);
274    
275     if (nd.rdiff > FTINY) { /* ambient from this side */
276 greg 2.30 ambient(ctmp, r, nd.pnorm);
277 greg 2.4 if (nd.specfl & SP_RBLT)
278     scalecolor(ctmp, 1.0-nd.trans);
279     else
280     scalecolor(ctmp, nd.rdiff);
281 greg 2.1 multcolor(ctmp, nd.mcolor); /* modified by material color */
282     addcolor(r->rcol, ctmp); /* add to returned color */
283     }
284     if (nd.tdiff > FTINY) { /* ambient from other side */
285 greg 2.31 FVECT bnorm;
286    
287 greg 2.1 flipsurface(r);
288 greg 2.31 bnorm[0] = -nd.pnorm[0];
289     bnorm[1] = -nd.pnorm[1];
290     bnorm[2] = -nd.pnorm[2];
291     ambient(ctmp, r, bnorm);
292 greg 2.4 if (nd.specfl & SP_TBLT)
293     scalecolor(ctmp, nd.trans);
294     else
295     scalecolor(ctmp, nd.tdiff);
296 greg 2.1 multcolor(ctmp, nd.mcolor); /* modified by color */
297     addcolor(r->rcol, ctmp);
298     flipsurface(r);
299     }
300     /* add direct component */
301     direct(r, diraniso, &nd);
302 greg 2.27
303     return(1);
304 greg 2.1 }
305    
306    
307 greg 2.34 static void
308 greg 2.1 getacoords(r, np) /* set up coordinate system */
309     RAY *r;
310     register ANISODAT *np;
311     {
312     register MFUNC *mf;
313     register int i;
314    
315     mf = getfunc(np->mp, 3, 0x7, 1);
316     setfunc(np->mp, r);
317     errno = 0;
318     for (i = 0; i < 3; i++)
319     np->u[i] = evalue(mf->ep[i]);
320     if (errno) {
321     objerror(np->mp, WARNING, "compute error");
322     np->specfl |= SP_BADU;
323     return;
324     }
325 greg 2.16 if (mf->f != &unitxf)
326     multv3(np->u, np->u, mf->f->xfm);
327 greg 2.1 fcross(np->v, np->pnorm, np->u);
328     if (normalize(np->v) == 0.0) {
329     objerror(np->mp, WARNING, "illegal orientation vector");
330     np->specfl |= SP_BADU;
331     return;
332     }
333     fcross(np->u, np->v, np->pnorm);
334     }
335    
336    
337 greg 2.34 static void
338 greg 2.1 agaussamp(r, np) /* sample anisotropic gaussian specular */
339     RAY *r;
340     register ANISODAT *np;
341     {
342     RAY sr;
343     FVECT h;
344     double rv[2];
345     double d, sinp, cosp;
346 greg 2.32 int niter;
347 greg 2.1 register int i;
348     /* compute reflection */
349 greg 2.4 if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
350 greg 2.1 rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
351     dimlist[ndims++] = (int)np->mp;
352 greg 2.32 for (niter = 0; niter < MAXITER; niter++) {
353     if (niter)
354     d = frandom();
355     else
356     d = urand(ilhash(dimlist,ndims)+samplendx);
357     multisamp(rv, 2, d);
358     d = 2.0*PI * rv[0];
359 gwlarson 2.33 cosp = tcos(d) * np->u_alpha;
360     sinp = tsin(d) * np->v_alpha;
361 greg 2.32 d = sqrt(cosp*cosp + sinp*sinp);
362     cosp /= d;
363     sinp /= d;
364     rv[1] = 1.0 - specjitter*rv[1];
365     if (rv[1] <= FTINY)
366     d = 1.0;
367     else
368     d = sqrt(-log(rv[1]) /
369     (cosp*cosp/(np->u_alpha*np->u_alpha) +
370     sinp*sinp/(np->v_alpha*np->v_alpha)));
371     for (i = 0; i < 3; i++)
372     h[i] = np->pnorm[i] +
373     d*(cosp*np->u[i] + sinp*np->v[i]);
374     d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
375     for (i = 0; i < 3; i++)
376     sr.rdir[i] = r->rdir[i] + d*h[i];
377     if (DOT(sr.rdir, r->ron) > FTINY) {
378     rayvalue(&sr);
379     multcolor(sr.rcol, np->scolor);
380     addcolor(r->rcol, sr.rcol);
381     break;
382     }
383     }
384 greg 2.1 ndims--;
385     }
386     /* compute transmission */
387 greg 2.7 if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
388     rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
389     dimlist[ndims++] = (int)np->mp;
390 greg 2.32 for (niter = 0; niter < MAXITER; niter++) {
391     if (niter)
392     d = frandom();
393     else
394     d = urand(ilhash(dimlist,ndims)+1823+samplendx);
395     multisamp(rv, 2, d);
396     d = 2.0*PI * rv[0];
397 gwlarson 2.33 cosp = tcos(d) * np->u_alpha;
398     sinp = tsin(d) * np->v_alpha;
399 greg 2.32 d = sqrt(cosp*cosp + sinp*sinp);
400     cosp /= d;
401     sinp /= d;
402     rv[1] = 1.0 - specjitter*rv[1];
403     if (rv[1] <= FTINY)
404     d = 1.0;
405     else
406     d = sqrt(-log(rv[1]) /
407     (cosp*cosp/(np->u_alpha*np->u_alpha) +
408 gwlarson 2.33 sinp*sinp/(np->v_alpha*np->v_alpha)));
409 greg 2.32 for (i = 0; i < 3; i++)
410     sr.rdir[i] = np->prdir[i] +
411     d*(cosp*np->u[i] + sinp*np->v[i]);
412     if (DOT(sr.rdir, r->ron) < -FTINY) {
413     normalize(sr.rdir); /* OK, normalize */
414     rayvalue(&sr);
415     scalecolor(sr.rcol, np->tspec);
416     multcolor(sr.rcol, np->mcolor); /* modify */
417     addcolor(r->rcol, sr.rcol);
418     break;
419     }
420     }
421 greg 2.7 ndims--;
422     }
423 greg 2.1 }