ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/aniso.c
Revision: 2.11
Committed: Tue Mar 3 16:20:00 1992 UTC (32 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.10: +2 -1 lines
Log Message:
precautionary test before accessing r->ro member

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 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     double u_alpha; /* u roughness */
57     double v_alpha; /* v roughness */
58     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     double dtmp, dtmp2;
74     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.1 au2 += np->u_alpha * np->u_alpha;
107     av2 += np->v_alpha * np->v_alpha;
108     /* 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     dtmp = DOT(np->u, h);
115     dtmp *= dtmp / au2;
116     dtmp2 = DOT(np->v, h);
117     dtmp2 *= dtmp2 / av2;
118     /* gaussian */
119     dtmp = (dtmp + dtmp2) / (1.0 + DOT(np->pnorm, h));
120     dtmp = exp(-2.0*dtmp) / (4.0*PI * sqrt(au2*av2));
121     /* worth using? */
122     if (dtmp > FTINY) {
123     copycolor(ctmp, np->scolor);
124     dtmp *= omega / np->pdot;
125     scalecolor(ctmp, dtmp);
126     addcolor(cval, ctmp);
127     }
128     }
129     if (ldot < -FTINY && np->tdiff > FTINY) {
130     /*
131     * Compute diffuse transmission.
132     */
133     copycolor(ctmp, np->mcolor);
134     dtmp = -ldot * omega * np->tdiff / PI;
135     scalecolor(ctmp, dtmp);
136     addcolor(cval, ctmp);
137     }
138 greg 2.10 if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_BADU)) == SP_TRAN) {
139 greg 2.1 /*
140     * Compute specular transmission. Specular transmission
141     * is always modified by material color.
142     */
143     /* roughness + source */
144     /* gaussian */
145     dtmp = 0.0;
146     /* worth using? */
147     if (dtmp > FTINY) {
148     copycolor(ctmp, np->mcolor);
149     dtmp *= np->tspec * omega / np->pdot;
150     scalecolor(ctmp, dtmp);
151     addcolor(cval, ctmp);
152     }
153     }
154     }
155    
156    
157     m_aniso(m, r) /* shade ray that hit something anisotropic */
158     register OBJREC *m;
159     register RAY *r;
160     {
161     ANISODAT nd;
162     double dtmp;
163     COLOR ctmp;
164     register int i;
165     /* easy shadow test */
166 greg 2.10 if (r->crtype & SHADOW)
167 greg 2.1 return;
168    
169     if (m->oargs.nfargs != (m->otype == MAT_TRANS2 ? 8 : 6))
170     objerror(m, USER, "bad number of real arguments");
171 greg 2.2 nd.mp = m;
172 greg 2.1 nd.rp = r;
173     /* get material color */
174     setcolor(nd.mcolor, m->oargs.farg[0],
175     m->oargs.farg[1],
176     m->oargs.farg[2]);
177     /* get roughness */
178     nd.specfl = 0;
179     nd.u_alpha = m->oargs.farg[4];
180     nd.v_alpha = m->oargs.farg[5];
181 greg 2.10 if (nd.u_alpha < 1e-6 || nd.v_alpha <= 1e-6)
182     objerror(m, USER, "roughness too small");
183 greg 2.1 /* reorient if necessary */
184     if (r->rod < 0.0)
185     flipsurface(r);
186     /* get modifiers */
187     raytexture(r, m->omod);
188     nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */
189     if (nd.pdot < .001)
190     nd.pdot = .001; /* non-zero for diraniso() */
191     multcolor(nd.mcolor, r->pcol); /* modify material color */
192     /* get specular component */
193     if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
194     nd.specfl |= SP_REFL;
195     /* compute specular color */
196     if (m->otype == MAT_METAL2)
197     copycolor(nd.scolor, nd.mcolor);
198     else
199     setcolor(nd.scolor, 1.0, 1.0, 1.0);
200     scalecolor(nd.scolor, nd.rspec);
201     /* improved model */
202     dtmp = exp(-BSPEC(m)*nd.pdot);
203     for (i = 0; i < 3; i++)
204     colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
205     nd.rspec += (1.0-nd.rspec)*dtmp;
206 greg 2.4 /* check threshold */
207 greg 2.5 if (specthresh > FTINY &&
208     ((specthresh >= 1.-FTINY ||
209 greg 2.9 specthresh + (.05 - .1*frandom()) > nd.rspec)))
210 greg 2.4 nd.specfl |= SP_RBLT;
211 greg 2.6 /* compute refl. direction */
212     for (i = 0; i < 3; i++)
213     nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
214     if (DOT(nd.vrefl, r->ron) <= FTINY) /* penetration? */
215     for (i = 0; i < 3; i++) /* safety measure */
216     nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
217 greg 2.1 }
218     /* compute transmission */
219     if (m->otype == MAT_TRANS) {
220     nd.trans = m->oargs.farg[6]*(1.0 - nd.rspec);
221     nd.tspec = nd.trans * m->oargs.farg[7];
222     nd.tdiff = nd.trans - nd.tspec;
223     if (nd.tspec > FTINY) {
224     nd.specfl |= SP_TRAN;
225 greg 2.4 /* check threshold */
226 greg 2.5 if (specthresh > FTINY &&
227     ((specthresh >= 1.-FTINY ||
228     specthresh +
229 greg 2.9 (.05 - .1*frandom()) > nd.tspec)))
230 greg 2.4 nd.specfl |= SP_TBLT;
231 greg 2.10 if (DOT(r->pert,r->pert) <= FTINY*FTINY) {
232 greg 2.1 VCOPY(nd.prdir, r->rdir);
233     } else {
234     for (i = 0; i < 3; i++) /* perturb */
235     nd.prdir[i] = r->rdir[i] -
236 greg 2.7 0.5*r->pert[i];
237 greg 2.6 if (DOT(nd.prdir, r->ron) < -FTINY)
238     normalize(nd.prdir); /* OK */
239     else
240     VCOPY(nd.prdir, r->rdir);
241 greg 2.1 }
242     }
243     } else
244     nd.tdiff = nd.tspec = nd.trans = 0.0;
245    
246     /* diffuse reflection */
247     nd.rdiff = 1.0 - nd.trans - nd.rspec;
248    
249 greg 2.11 if (r->ro != NULL && (r->ro->otype == OBJ_FACE ||
250     r->ro->otype == OBJ_RING))
251 greg 2.4 nd.specfl |= SP_FLAT;
252    
253 greg 2.1 getacoords(r, &nd); /* set up coordinates */
254    
255 greg 2.10 if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_BADU))
256 greg 2.1 agaussamp(r, &nd);
257    
258     if (nd.rdiff > FTINY) { /* ambient from this side */
259     ambient(ctmp, r);
260 greg 2.4 if (nd.specfl & SP_RBLT)
261     scalecolor(ctmp, 1.0-nd.trans);
262     else
263     scalecolor(ctmp, nd.rdiff);
264 greg 2.1 multcolor(ctmp, nd.mcolor); /* modified by material color */
265     addcolor(r->rcol, ctmp); /* add to returned color */
266     }
267     if (nd.tdiff > FTINY) { /* ambient from other side */
268     flipsurface(r);
269     ambient(ctmp, r);
270 greg 2.4 if (nd.specfl & SP_TBLT)
271     scalecolor(ctmp, nd.trans);
272     else
273     scalecolor(ctmp, nd.tdiff);
274 greg 2.1 multcolor(ctmp, nd.mcolor); /* modified by color */
275     addcolor(r->rcol, ctmp);
276     flipsurface(r);
277     }
278     /* add direct component */
279     direct(r, diraniso, &nd);
280     }
281    
282    
283     static
284     getacoords(r, np) /* set up coordinate system */
285     RAY *r;
286     register ANISODAT *np;
287     {
288     register MFUNC *mf;
289     register int i;
290    
291     mf = getfunc(np->mp, 3, 0x7, 1);
292     setfunc(np->mp, r);
293     errno = 0;
294     for (i = 0; i < 3; i++)
295     np->u[i] = evalue(mf->ep[i]);
296     if (errno) {
297     objerror(np->mp, WARNING, "compute error");
298     np->specfl |= SP_BADU;
299     return;
300     }
301     multv3(np->u, np->u, mf->f->xfm);
302     fcross(np->v, np->pnorm, np->u);
303     if (normalize(np->v) == 0.0) {
304     objerror(np->mp, WARNING, "illegal orientation vector");
305     np->specfl |= SP_BADU;
306     return;
307     }
308     fcross(np->u, np->v, np->pnorm);
309     }
310    
311    
312     static
313     agaussamp(r, np) /* sample anisotropic gaussian specular */
314     RAY *r;
315     register ANISODAT *np;
316     {
317     RAY sr;
318     FVECT h;
319     double rv[2];
320     double d, sinp, cosp;
321     register int i;
322     /* compute reflection */
323 greg 2.4 if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
324 greg 2.1 rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
325     dimlist[ndims++] = (int)np->mp;
326 greg 2.6 d = urand(ilhash(dimlist,ndims)+samplendx);
327     multisamp(rv, 2, d);
328     d = 2.0*PI * rv[0];
329     cosp = np->u_alpha * cos(d);
330     sinp = np->v_alpha * sin(d);
331     d = sqrt(cosp*cosp + sinp*sinp);
332     cosp /= d;
333     sinp /= d;
334     rv[1] = 1.0 - specjitter*rv[1];
335     if (rv[1] <= FTINY)
336     d = 1.0;
337     else
338     d = sqrt(-log(rv[1]) /
339     (cosp*cosp/(np->u_alpha*np->u_alpha) +
340     sinp*sinp/(np->v_alpha*np->v_alpha)));
341     for (i = 0; i < 3; i++)
342     h[i] = np->pnorm[i] +
343     d*(cosp*np->u[i] + sinp*np->v[i]);
344     d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
345     for (i = 0; i < 3; i++)
346     sr.rdir[i] = r->rdir[i] + d*h[i];
347     if (DOT(sr.rdir, r->ron) <= FTINY) /* penetration? */
348     VCOPY(sr.rdir, np->vrefl); /* jitter no good */
349     rayvalue(&sr);
350     multcolor(sr.rcol, np->scolor);
351     addcolor(r->rcol, sr.rcol);
352 greg 2.1 ndims--;
353     }
354     /* compute transmission */
355 greg 2.7 if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
356     rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
357     dimlist[ndims++] = (int)np->mp;
358     d = urand(ilhash(dimlist,ndims)+1823+samplendx);
359     multisamp(rv, 2, d);
360     d = 2.0*PI * rv[0];
361     cosp = cos(d);
362     sinp = sin(d);
363     rv[1] = 1.0 - specjitter*rv[1];
364     if (rv[1] <= FTINY)
365     d = 1.0;
366     else
367     d = sqrt(-log(rv[1]) /
368     (cosp*cosp*4./(np->u_alpha*np->u_alpha) +
369     sinp*sinp*4./(np->v_alpha*np->v_alpha)));
370     for (i = 0; i < 3; i++)
371     sr.rdir[i] = np->prdir[i] +
372     d*(cosp*np->u[i] + sinp*np->v[i]);
373     if (DOT(sr.rdir, r->ron) < -FTINY)
374     normalize(sr.rdir); /* OK, normalize */
375     else
376     VCOPY(sr.rdir, np->prdir); /* else no jitter */
377     rayvalue(&sr);
378 greg 2.10 scalecolor(sr.rcol, np->tspec);
379     multcolor(sr.rcol, np->mcolor); /* modify by color */
380 greg 2.7 addcolor(r->rcol, sr.rcol);
381     ndims--;
382     }
383 greg 2.1 }