ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/aniso.c
Revision: 2.3
Committed: Mon Jan 6 18:01:41 1992 UTC (32 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.2: +29 -28 lines
Log Message:
fixed semi-infinite loop bug

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