ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/aniso.c
Revision: 2.7
Committed: Wed Jan 15 16:59:55 1992 UTC (32 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.6: +28 -1 lines
Log Message:
finally added sampling of transmitted specular

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     specthresh + (.1 - .2*urand(8199+samplendx))
213     > nd.rspec)))
214 greg 2.4 nd.specfl |= SP_RBLT;
215 greg 2.6 /* compute refl. direction */
216     for (i = 0; i < 3; i++)
217     nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
218     if (DOT(nd.vrefl, r->ron) <= FTINY) /* penetration? */
219     for (i = 0; i < 3; i++) /* safety measure */
220     nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
221 greg 2.1
222     if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
223     RAY lr;
224     if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
225 greg 2.6 VCOPY(lr.rdir, nd.vrefl);
226 greg 2.1 rayvalue(&lr);
227     multcolor(lr.rcol, nd.scolor);
228     addcolor(r->rcol, lr.rcol);
229     }
230     }
231     }
232     /* compute transmission */
233     if (m->otype == MAT_TRANS) {
234     nd.trans = m->oargs.farg[6]*(1.0 - nd.rspec);
235     nd.tspec = nd.trans * m->oargs.farg[7];
236     nd.tdiff = nd.trans - nd.tspec;
237     if (nd.tspec > FTINY) {
238     nd.specfl |= SP_TRAN;
239 greg 2.4 /* check threshold */
240 greg 2.5 if (specthresh > FTINY &&
241     ((specthresh >= 1.-FTINY ||
242     specthresh +
243     (.1 - .2*urand(7241+samplendx))
244     > nd.tspec)))
245 greg 2.4 nd.specfl |= SP_TBLT;
246 greg 2.1 if (r->crtype & SHADOW ||
247     DOT(r->pert,r->pert) <= FTINY*FTINY) {
248     VCOPY(nd.prdir, r->rdir);
249     transtest = 2;
250     } else {
251     for (i = 0; i < 3; i++) /* perturb */
252     nd.prdir[i] = r->rdir[i] -
253 greg 2.7 0.5*r->pert[i];
254 greg 2.6 if (DOT(nd.prdir, r->ron) < -FTINY)
255     normalize(nd.prdir); /* OK */
256     else
257     VCOPY(nd.prdir, r->rdir);
258 greg 2.1 }
259     }
260     } else
261     nd.tdiff = nd.tspec = nd.trans = 0.0;
262     /* transmitted ray */
263     if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
264     RAY lr;
265     if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
266     VCOPY(lr.rdir, nd.prdir);
267     rayvalue(&lr);
268     scalecolor(lr.rcol, nd.tspec);
269     multcolor(lr.rcol, nd.mcolor); /* modified by color */
270     addcolor(r->rcol, lr.rcol);
271     transtest *= bright(lr.rcol);
272     transdist = r->rot + lr.rt;
273     }
274     }
275    
276     if (r->crtype & SHADOW) /* the rest is shadow */
277     return;
278     /* diffuse reflection */
279     nd.rdiff = 1.0 - nd.trans - nd.rspec;
280    
281     if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
282     return; /* 100% pure specular */
283    
284 greg 2.4 if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING)
285     nd.specfl |= SP_FLAT;
286    
287 greg 2.1 getacoords(r, &nd); /* set up coordinates */
288    
289     if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & (SP_PURE|SP_BADU)))
290     agaussamp(r, &nd);
291    
292     if (nd.rdiff > FTINY) { /* ambient from this side */
293     ambient(ctmp, r);
294 greg 2.4 if (nd.specfl & SP_RBLT)
295     scalecolor(ctmp, 1.0-nd.trans);
296     else
297     scalecolor(ctmp, nd.rdiff);
298 greg 2.1 multcolor(ctmp, nd.mcolor); /* modified by material color */
299     addcolor(r->rcol, ctmp); /* add to returned color */
300     }
301     if (nd.tdiff > FTINY) { /* ambient from other side */
302     flipsurface(r);
303     ambient(ctmp, r);
304 greg 2.4 if (nd.specfl & SP_TBLT)
305     scalecolor(ctmp, nd.trans);
306     else
307     scalecolor(ctmp, nd.tdiff);
308 greg 2.1 multcolor(ctmp, nd.mcolor); /* modified by color */
309     addcolor(r->rcol, ctmp);
310     flipsurface(r);
311     }
312     /* add direct component */
313     direct(r, diraniso, &nd);
314     /* check distance */
315     if (transtest > bright(r->rcol))
316     r->rt = transdist;
317     }
318    
319    
320     static
321     getacoords(r, np) /* set up coordinate system */
322     RAY *r;
323     register ANISODAT *np;
324     {
325     register MFUNC *mf;
326     register int i;
327    
328     mf = getfunc(np->mp, 3, 0x7, 1);
329     setfunc(np->mp, r);
330     errno = 0;
331     for (i = 0; i < 3; i++)
332     np->u[i] = evalue(mf->ep[i]);
333     if (errno) {
334     objerror(np->mp, WARNING, "compute error");
335     np->specfl |= SP_BADU;
336     return;
337     }
338     multv3(np->u, np->u, mf->f->xfm);
339     fcross(np->v, np->pnorm, np->u);
340     if (normalize(np->v) == 0.0) {
341     objerror(np->mp, WARNING, "illegal orientation vector");
342     np->specfl |= SP_BADU;
343     return;
344     }
345     fcross(np->u, np->v, np->pnorm);
346     }
347    
348    
349     static
350     agaussamp(r, np) /* sample anisotropic gaussian specular */
351     RAY *r;
352     register ANISODAT *np;
353     {
354     RAY sr;
355     FVECT h;
356     double rv[2];
357     double d, sinp, cosp;
358     register int i;
359     /* compute reflection */
360 greg 2.4 if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
361 greg 2.1 rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
362     dimlist[ndims++] = (int)np->mp;
363 greg 2.6 d = urand(ilhash(dimlist,ndims)+samplendx);
364     multisamp(rv, 2, d);
365     d = 2.0*PI * rv[0];
366     cosp = np->u_alpha * cos(d);
367     sinp = np->v_alpha * sin(d);
368     d = sqrt(cosp*cosp + sinp*sinp);
369     cosp /= d;
370     sinp /= d;
371     rv[1] = 1.0 - specjitter*rv[1];
372     if (rv[1] <= FTINY)
373     d = 1.0;
374     else
375     d = sqrt(-log(rv[1]) /
376     (cosp*cosp/(np->u_alpha*np->u_alpha) +
377     sinp*sinp/(np->v_alpha*np->v_alpha)));
378     for (i = 0; i < 3; i++)
379     h[i] = np->pnorm[i] +
380     d*(cosp*np->u[i] + sinp*np->v[i]);
381     d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
382     for (i = 0; i < 3; i++)
383     sr.rdir[i] = r->rdir[i] + d*h[i];
384     if (DOT(sr.rdir, r->ron) <= FTINY) /* penetration? */
385     VCOPY(sr.rdir, np->vrefl); /* jitter no good */
386     rayvalue(&sr);
387     multcolor(sr.rcol, np->scolor);
388     addcolor(r->rcol, sr.rcol);
389 greg 2.1 ndims--;
390     }
391     /* compute transmission */
392 greg 2.7 if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
393     rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
394     dimlist[ndims++] = (int)np->mp;
395     d = urand(ilhash(dimlist,ndims)+1823+samplendx);
396     multisamp(rv, 2, d);
397     d = 2.0*PI * rv[0];
398     cosp = cos(d);
399     sinp = sin(d);
400     rv[1] = 1.0 - specjitter*rv[1];
401     if (rv[1] <= FTINY)
402     d = 1.0;
403     else
404     d = sqrt(-log(rv[1]) /
405     (cosp*cosp*4./(np->u_alpha*np->u_alpha) +
406     sinp*sinp*4./(np->v_alpha*np->v_alpha)));
407     for (i = 0; i < 3; i++)
408     sr.rdir[i] = np->prdir[i] +
409     d*(cosp*np->u[i] + sinp*np->v[i]);
410     if (DOT(sr.rdir, r->ron) < -FTINY)
411     normalize(sr.rdir); /* OK, normalize */
412     else
413     VCOPY(sr.rdir, np->prdir); /* else no jitter */
414     rayvalue(&sr);
415     multcolor(sr.rcol, np->scolor);
416     addcolor(r->rcol, sr.rcol);
417     ndims--;
418     }
419 greg 2.1 }