ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/aniso.c
Revision: 2.9
Committed: Thu Jan 30 11:37:04 1992 UTC (32 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.8: +2 -4 lines
Log Message:
changed urand() call to frandom() for specular threshold testing

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     #define BSPEC(m) (6.0) /* specularity parameter b */
38    
39     /* specularity flags */
40     #define SP_REFL 01 /* has reflected specular component */
41     #define SP_TRAN 02 /* has transmitted specular */
42     #define SP_PURE 010 /* purely specular (zero roughness) */
43 greg 2.4 #define SP_FLAT 020 /* reflecting surface is flat */
44     #define SP_RBLT 040 /* reflection below sample threshold */
45     #define SP_TBLT 0100 /* transmission below threshold */
46     #define SP_BADU 0200 /* bad u direction calculation */
47 greg 2.1
48     typedef struct {
49 greg 2.2 OBJREC *mp; /* material pointer */
50 greg 2.1 RAY *rp; /* ray pointer */
51     short specfl; /* specularity flags, defined above */
52     COLOR mcolor; /* color of this material */
53     COLOR scolor; /* color of specular component */
54 greg 2.6 FVECT vrefl; /* vector in reflected direction */
55 greg 2.1 FVECT prdir; /* vector in transmitted direction */
56     FVECT u, v; /* u and v vectors orienting anisotropy */
57     double u_alpha; /* u roughness */
58     double v_alpha; /* v roughness */
59     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     } ANISODAT; /* anisotropic material data */
65    
66    
67     diraniso(cval, np, ldir, omega) /* compute source contribution */
68     COLOR cval; /* returned coefficient */
69     register ANISODAT *np; /* material data */
70     FVECT ldir; /* light source direction */
71     double omega; /* light source size */
72     {
73     double ldot;
74     double dtmp, dtmp2;
75     FVECT h;
76     double au2, av2;
77     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     * 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     */
92     copycolor(ctmp, np->mcolor);
93     dtmp = ldot * omega * np->rdiff / PI;
94     scalecolor(ctmp, dtmp);
95     addcolor(cval, ctmp);
96     }
97     if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE|SP_BADU)) == SP_REFL) {
98     /*
99     * Compute specular reflection coefficient using
100     * anisotropic gaussian distribution model.
101     */
102 greg 2.2 /* add source width if flat */
103     if (np->specfl & SP_FLAT)
104     au2 = av2 = omega/(4.0*PI);
105     else
106     au2 = av2 = 0.0;
107 greg 2.1 au2 += np->u_alpha * np->u_alpha;
108     av2 += np->v_alpha * np->v_alpha;
109     /* half vector */
110     h[0] = ldir[0] - np->rp->rdir[0];
111     h[1] = ldir[1] - np->rp->rdir[1];
112     h[2] = ldir[2] - np->rp->rdir[2];
113     normalize(h);
114     /* ellipse */
115     dtmp = DOT(np->u, h);
116     dtmp *= dtmp / au2;
117     dtmp2 = DOT(np->v, h);
118     dtmp2 *= dtmp2 / av2;
119     /* gaussian */
120     dtmp = (dtmp + dtmp2) / (1.0 + DOT(np->pnorm, h));
121     dtmp = exp(-2.0*dtmp) / (4.0*PI * sqrt(au2*av2));
122     /* worth using? */
123     if (dtmp > FTINY) {
124     copycolor(ctmp, np->scolor);
125     dtmp *= omega / np->pdot;
126     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     if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE|SP_BADU)) == SP_TRAN) {
140     /*
141     * Compute specular transmission. Specular transmission
142     * is always modified by material color.
143     */
144     /* roughness + source */
145     /* gaussian */
146     dtmp = 0.0;
147     /* worth using? */
148     if (dtmp > FTINY) {
149     copycolor(ctmp, np->mcolor);
150     dtmp *= np->tspec * omega / np->pdot;
151     scalecolor(ctmp, dtmp);
152     addcolor(cval, ctmp);
153     }
154     }
155     }
156    
157    
158     m_aniso(m, r) /* shade ray that hit something anisotropic */
159     register OBJREC *m;
160     register RAY *r;
161     {
162     ANISODAT nd;
163     double transtest, transdist;
164     double dtmp;
165     COLOR ctmp;
166     register int i;
167     /* easy shadow test */
168     if (r->crtype & SHADOW && m->otype != MAT_TRANS2)
169     return;
170    
171     if (m->oargs.nfargs != (m->otype == MAT_TRANS2 ? 8 : 6))
172     objerror(m, USER, "bad number of real arguments");
173 greg 2.2 nd.mp = m;
174 greg 2.1 nd.rp = r;
175     /* get material color */
176     setcolor(nd.mcolor, m->oargs.farg[0],
177     m->oargs.farg[1],
178     m->oargs.farg[2]);
179     /* get roughness */
180     nd.specfl = 0;
181     nd.u_alpha = m->oargs.farg[4];
182     nd.v_alpha = m->oargs.farg[5];
183     if (nd.u_alpha <= FTINY || nd.v_alpha <= FTINY)
184     nd.specfl |= SP_PURE;
185     /* reorient if necessary */
186     if (r->rod < 0.0)
187     flipsurface(r);
188     /* get modifiers */
189     raytexture(r, m->omod);
190     nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */
191     if (nd.pdot < .001)
192     nd.pdot = .001; /* non-zero for diraniso() */
193     multcolor(nd.mcolor, r->pcol); /* modify material color */
194     transtest = 0;
195     /* get specular component */
196     if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
197     nd.specfl |= SP_REFL;
198     /* compute specular color */
199     if (m->otype == MAT_METAL2)
200     copycolor(nd.scolor, nd.mcolor);
201     else
202     setcolor(nd.scolor, 1.0, 1.0, 1.0);
203     scalecolor(nd.scolor, nd.rspec);
204     /* improved model */
205     dtmp = exp(-BSPEC(m)*nd.pdot);
206     for (i = 0; i < 3; i++)
207     colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
208     nd.rspec += (1.0-nd.rspec)*dtmp;
209 greg 2.4 /* check threshold */
210 greg 2.5 if (specthresh > FTINY &&
211     ((specthresh >= 1.-FTINY ||
212 greg 2.9 specthresh + (.05 - .1*frandom()) > nd.rspec)))
213 greg 2.4 nd.specfl |= SP_RBLT;
214 greg 2.6 /* compute refl. direction */
215     for (i = 0; i < 3; i++)
216     nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
217     if (DOT(nd.vrefl, r->ron) <= FTINY) /* penetration? */
218     for (i = 0; i < 3; i++) /* safety measure */
219     nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
220 greg 2.1
221     if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
222     RAY lr;
223     if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
224 greg 2.6 VCOPY(lr.rdir, nd.vrefl);
225 greg 2.1 rayvalue(&lr);
226     multcolor(lr.rcol, nd.scolor);
227     addcolor(r->rcol, lr.rcol);
228     }
229     }
230     }
231     /* compute transmission */
232     if (m->otype == MAT_TRANS) {
233     nd.trans = m->oargs.farg[6]*(1.0 - nd.rspec);
234     nd.tspec = nd.trans * m->oargs.farg[7];
235     nd.tdiff = nd.trans - nd.tspec;
236     if (nd.tspec > FTINY) {
237     nd.specfl |= SP_TRAN;
238 greg 2.4 /* check threshold */
239 greg 2.5 if (specthresh > FTINY &&
240     ((specthresh >= 1.-FTINY ||
241     specthresh +
242 greg 2.9 (.05 - .1*frandom()) > nd.tspec)))
243 greg 2.4 nd.specfl |= SP_TBLT;
244 greg 2.1 if (r->crtype & SHADOW ||
245     DOT(r->pert,r->pert) <= FTINY*FTINY) {
246     VCOPY(nd.prdir, r->rdir);
247     transtest = 2;
248     } else {
249     for (i = 0; i < 3; i++) /* perturb */
250     nd.prdir[i] = r->rdir[i] -
251 greg 2.7 0.5*r->pert[i];
252 greg 2.6 if (DOT(nd.prdir, r->ron) < -FTINY)
253     normalize(nd.prdir); /* OK */
254     else
255     VCOPY(nd.prdir, r->rdir);
256 greg 2.1 }
257     }
258     } else
259     nd.tdiff = nd.tspec = nd.trans = 0.0;
260     /* transmitted ray */
261     if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
262     RAY lr;
263     if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
264     VCOPY(lr.rdir, nd.prdir);
265     rayvalue(&lr);
266     scalecolor(lr.rcol, nd.tspec);
267     multcolor(lr.rcol, nd.mcolor); /* modified by color */
268     addcolor(r->rcol, lr.rcol);
269     transtest *= bright(lr.rcol);
270     transdist = r->rot + lr.rt;
271     }
272     }
273    
274     if (r->crtype & SHADOW) /* the rest is shadow */
275     return;
276     /* diffuse reflection */
277     nd.rdiff = 1.0 - nd.trans - nd.rspec;
278    
279     if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
280     return; /* 100% pure specular */
281    
282 greg 2.4 if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING)
283     nd.specfl |= SP_FLAT;
284    
285 greg 2.1 getacoords(r, &nd); /* set up coordinates */
286    
287     if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & (SP_PURE|SP_BADU)))
288     agaussamp(r, &nd);
289    
290     if (nd.rdiff > FTINY) { /* ambient from this side */
291     ambient(ctmp, r);
292 greg 2.4 if (nd.specfl & SP_RBLT)
293     scalecolor(ctmp, 1.0-nd.trans);
294     else
295     scalecolor(ctmp, nd.rdiff);
296 greg 2.1 multcolor(ctmp, nd.mcolor); /* modified by material color */
297     addcolor(r->rcol, ctmp); /* add to returned color */
298     }
299     if (nd.tdiff > FTINY) { /* ambient from other side */
300     flipsurface(r);
301     ambient(ctmp, r);
302 greg 2.4 if (nd.specfl & SP_TBLT)
303     scalecolor(ctmp, nd.trans);
304     else
305     scalecolor(ctmp, nd.tdiff);
306 greg 2.1 multcolor(ctmp, nd.mcolor); /* modified by color */
307     addcolor(r->rcol, ctmp);
308     flipsurface(r);
309     }
310     /* add direct component */
311     direct(r, diraniso, &nd);
312     /* check distance */
313     if (transtest > bright(r->rcol))
314     r->rt = transdist;
315     }
316    
317    
318     static
319     getacoords(r, np) /* set up coordinate system */
320     RAY *r;
321     register ANISODAT *np;
322     {
323     register MFUNC *mf;
324     register int i;
325    
326     mf = getfunc(np->mp, 3, 0x7, 1);
327     setfunc(np->mp, r);
328     errno = 0;
329     for (i = 0; i < 3; i++)
330     np->u[i] = evalue(mf->ep[i]);
331     if (errno) {
332     objerror(np->mp, WARNING, "compute error");
333     np->specfl |= SP_BADU;
334     return;
335     }
336     multv3(np->u, np->u, mf->f->xfm);
337     fcross(np->v, np->pnorm, np->u);
338     if (normalize(np->v) == 0.0) {
339     objerror(np->mp, WARNING, "illegal orientation vector");
340     np->specfl |= SP_BADU;
341     return;
342     }
343     fcross(np->u, np->v, np->pnorm);
344     }
345    
346    
347     static
348     agaussamp(r, np) /* sample anisotropic gaussian specular */
349     RAY *r;
350     register ANISODAT *np;
351     {
352     RAY sr;
353     FVECT h;
354     double rv[2];
355     double d, sinp, cosp;
356     register int i;
357     /* compute reflection */
358 greg 2.4 if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
359 greg 2.1 rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
360     dimlist[ndims++] = (int)np->mp;
361 greg 2.6 d = urand(ilhash(dimlist,ndims)+samplendx);
362     multisamp(rv, 2, d);
363     d = 2.0*PI * rv[0];
364     cosp = np->u_alpha * cos(d);
365     sinp = np->v_alpha * sin(d);
366     d = sqrt(cosp*cosp + sinp*sinp);
367     cosp /= d;
368     sinp /= d;
369     rv[1] = 1.0 - specjitter*rv[1];
370     if (rv[1] <= FTINY)
371     d = 1.0;
372     else
373     d = sqrt(-log(rv[1]) /
374     (cosp*cosp/(np->u_alpha*np->u_alpha) +
375     sinp*sinp/(np->v_alpha*np->v_alpha)));
376     for (i = 0; i < 3; i++)
377     h[i] = np->pnorm[i] +
378     d*(cosp*np->u[i] + sinp*np->v[i]);
379     d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
380     for (i = 0; i < 3; i++)
381     sr.rdir[i] = r->rdir[i] + d*h[i];
382     if (DOT(sr.rdir, r->ron) <= FTINY) /* penetration? */
383     VCOPY(sr.rdir, np->vrefl); /* jitter no good */
384     rayvalue(&sr);
385     multcolor(sr.rcol, np->scolor);
386     addcolor(r->rcol, sr.rcol);
387 greg 2.1 ndims--;
388     }
389     /* compute transmission */
390 greg 2.7 if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
391     rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
392     dimlist[ndims++] = (int)np->mp;
393     d = urand(ilhash(dimlist,ndims)+1823+samplendx);
394     multisamp(rv, 2, d);
395     d = 2.0*PI * rv[0];
396     cosp = cos(d);
397     sinp = sin(d);
398     rv[1] = 1.0 - specjitter*rv[1];
399     if (rv[1] <= FTINY)
400     d = 1.0;
401     else
402     d = sqrt(-log(rv[1]) /
403     (cosp*cosp*4./(np->u_alpha*np->u_alpha) +
404     sinp*sinp*4./(np->v_alpha*np->v_alpha)));
405     for (i = 0; i < 3; i++)
406     sr.rdir[i] = np->prdir[i] +
407     d*(cosp*np->u[i] + sinp*np->v[i]);
408     if (DOT(sr.rdir, r->ron) < -FTINY)
409     normalize(sr.rdir); /* OK, normalize */
410     else
411     VCOPY(sr.rdir, np->prdir); /* else no jitter */
412     rayvalue(&sr);
413     multcolor(sr.rcol, np->scolor);
414     addcolor(r->rcol, sr.rcol);
415     ndims--;
416     }
417 greg 2.1 }