ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/aniso.c
Revision: 2.4
Committed: Tue Jan 14 16:16:48 1992 UTC (32 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +26 -5 lines
Log Message:
added specular jitter and threshold controls

File Contents

# Content
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 extern double specthresh; /* specular sampling threshold */
20 extern double specjitter; /* specular sampling jitter */
21
22 /*
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 #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
48 typedef struct {
49 OBJREC *mp; /* material pointer */
50 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 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 if (ldot > FTINY && (np->specfl&(SP_REFL|SP_PURE|SP_BADU)) == SP_REFL) {
97 /*
98 * Compute specular reflection coefficient using
99 * anisotropic gaussian distribution model.
100 */
101 /* 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 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 if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_PURE|SP_BADU)) == SP_TRAN) {
139 /*
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 transtest, transdist;
163 double dtmp;
164 COLOR ctmp;
165 register int i;
166 /* easy shadow test */
167 if (r->crtype & SHADOW && m->otype != MAT_TRANS2)
168 return;
169
170 if (m->oargs.nfargs != (m->otype == MAT_TRANS2 ? 8 : 6))
171 objerror(m, USER, "bad number of real arguments");
172 nd.mp = m;
173 nd.rp = r;
174 /* get material color */
175 setcolor(nd.mcolor, m->oargs.farg[0],
176 m->oargs.farg[1],
177 m->oargs.farg[2]);
178 /* get roughness */
179 nd.specfl = 0;
180 nd.u_alpha = m->oargs.farg[4];
181 nd.v_alpha = m->oargs.farg[5];
182 if (nd.u_alpha <= FTINY || nd.v_alpha <= FTINY)
183 nd.specfl |= SP_PURE;
184 /* reorient if necessary */
185 if (r->rod < 0.0)
186 flipsurface(r);
187 /* get modifiers */
188 raytexture(r, m->omod);
189 nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */
190 if (nd.pdot < .001)
191 nd.pdot = .001; /* non-zero for diraniso() */
192 multcolor(nd.mcolor, r->pcol); /* modify material color */
193 transtest = 0;
194 /* get specular component */
195 if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
196 nd.specfl |= SP_REFL;
197 /* compute specular color */
198 if (m->otype == MAT_METAL2)
199 copycolor(nd.scolor, nd.mcolor);
200 else
201 setcolor(nd.scolor, 1.0, 1.0, 1.0);
202 scalecolor(nd.scolor, nd.rspec);
203 /* improved model */
204 dtmp = exp(-BSPEC(m)*nd.pdot);
205 for (i = 0; i < 3; i++)
206 colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
207 nd.rspec += (1.0-nd.rspec)*dtmp;
208 /* check threshold */
209 if (nd.rspec <= specthresh+FTINY)
210 nd.specfl |= SP_RBLT;
211
212 if (!(r->crtype & SHADOW) && nd.specfl & SP_PURE) {
213 RAY lr;
214 if (rayorigin(&lr, r, REFLECTED, nd.rspec) == 0) {
215 for (i = 0; i < 3; i++)
216 lr.rdir[i] = r->rdir[i] +
217 2.0*nd.pdot*nd.pnorm[i];
218 rayvalue(&lr);
219 multcolor(lr.rcol, nd.scolor);
220 addcolor(r->rcol, lr.rcol);
221 }
222 }
223 }
224 /* compute transmission */
225 if (m->otype == MAT_TRANS) {
226 nd.trans = m->oargs.farg[6]*(1.0 - nd.rspec);
227 nd.tspec = nd.trans * m->oargs.farg[7];
228 nd.tdiff = nd.trans - nd.tspec;
229 if (nd.tspec > FTINY) {
230 nd.specfl |= SP_TRAN;
231 /* check threshold */
232 if (nd.tspec <= specthresh+FTINY)
233 nd.specfl |= SP_TBLT;
234 if (r->crtype & SHADOW ||
235 DOT(r->pert,r->pert) <= FTINY*FTINY) {
236 VCOPY(nd.prdir, r->rdir);
237 transtest = 2;
238 } else {
239 for (i = 0; i < 3; i++) /* perturb */
240 nd.prdir[i] = r->rdir[i] -
241 .75*r->pert[i];
242 normalize(nd.prdir);
243 }
244 }
245 } else
246 nd.tdiff = nd.tspec = nd.trans = 0.0;
247 /* transmitted ray */
248 if ((nd.specfl&(SP_TRAN|SP_PURE)) == (SP_TRAN|SP_PURE)) {
249 RAY lr;
250 if (rayorigin(&lr, r, TRANS, nd.tspec) == 0) {
251 VCOPY(lr.rdir, nd.prdir);
252 rayvalue(&lr);
253 scalecolor(lr.rcol, nd.tspec);
254 multcolor(lr.rcol, nd.mcolor); /* modified by color */
255 addcolor(r->rcol, lr.rcol);
256 transtest *= bright(lr.rcol);
257 transdist = r->rot + lr.rt;
258 }
259 }
260
261 if (r->crtype & SHADOW) /* the rest is shadow */
262 return;
263 /* diffuse reflection */
264 nd.rdiff = 1.0 - nd.trans - nd.rspec;
265
266 if (nd.specfl & SP_PURE && nd.rdiff <= FTINY && nd.tdiff <= FTINY)
267 return; /* 100% pure specular */
268
269 if (r->ro->otype == OBJ_FACE || r->ro->otype == OBJ_RING)
270 nd.specfl |= SP_FLAT;
271
272 getacoords(r, &nd); /* set up coordinates */
273
274 if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & (SP_PURE|SP_BADU)))
275 agaussamp(r, &nd);
276
277 if (nd.rdiff > FTINY) { /* ambient from this side */
278 ambient(ctmp, r);
279 if (nd.specfl & SP_RBLT)
280 scalecolor(ctmp, 1.0-nd.trans);
281 else
282 scalecolor(ctmp, nd.rdiff);
283 multcolor(ctmp, nd.mcolor); /* modified by material color */
284 addcolor(r->rcol, ctmp); /* add to returned color */
285 }
286 if (nd.tdiff > FTINY) { /* ambient from other side */
287 flipsurface(r);
288 ambient(ctmp, r);
289 if (nd.specfl & SP_TBLT)
290 scalecolor(ctmp, nd.trans);
291 else
292 scalecolor(ctmp, nd.tdiff);
293 multcolor(ctmp, nd.mcolor); /* modified by color */
294 addcolor(r->rcol, ctmp);
295 flipsurface(r);
296 }
297 /* add direct component */
298 direct(r, diraniso, &nd);
299 /* check distance */
300 if (transtest > bright(r->rcol))
301 r->rt = transdist;
302 }
303
304
305 static
306 getacoords(r, np) /* set up coordinate system */
307 RAY *r;
308 register ANISODAT *np;
309 {
310 register MFUNC *mf;
311 register int i;
312
313 mf = getfunc(np->mp, 3, 0x7, 1);
314 setfunc(np->mp, r);
315 errno = 0;
316 for (i = 0; i < 3; i++)
317 np->u[i] = evalue(mf->ep[i]);
318 if (errno) {
319 objerror(np->mp, WARNING, "compute error");
320 np->specfl |= SP_BADU;
321 return;
322 }
323 multv3(np->u, np->u, mf->f->xfm);
324 fcross(np->v, np->pnorm, np->u);
325 if (normalize(np->v) == 0.0) {
326 objerror(np->mp, WARNING, "illegal orientation vector");
327 np->specfl |= SP_BADU;
328 return;
329 }
330 fcross(np->u, np->v, np->pnorm);
331 }
332
333
334 static
335 agaussamp(r, np) /* sample anisotropic gaussian specular */
336 RAY *r;
337 register ANISODAT *np;
338 {
339 RAY sr;
340 FVECT h;
341 double rv[2];
342 double d, sinp, cosp;
343 int ntries;
344 register int i;
345 /* compute reflection */
346 if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
347 rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
348 dimlist[ndims++] = (int)np->mp;
349 for (ntries = 0; ntries < 10; ntries++) {
350 dimlist[ndims] = ntries * 3601;
351 d = urand(ilhash(dimlist,ndims+1)+samplendx);
352 multisamp(rv, 2, d);
353 d = 2.0*PI * rv[0];
354 cosp = np->u_alpha * cos(d);
355 sinp = np->v_alpha * sin(d);
356 d = sqrt(cosp*cosp + sinp*sinp);
357 cosp /= d;
358 sinp /= d;
359 rv[1] = 1.0 - specjitter*rv[1];
360 if (rv[1] <= FTINY)
361 d = 1.0;
362 else
363 d = sqrt(-log(rv[1]) /
364 (cosp*cosp/(np->u_alpha*np->u_alpha) +
365 sinp*sinp/(np->v_alpha*np->v_alpha)));
366 for (i = 0; i < 3; i++)
367 h[i] = np->pnorm[i] +
368 d*(cosp*np->u[i] + sinp*np->v[i]);
369 d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
370 for (i = 0; i < 3; i++)
371 sr.rdir[i] = r->rdir[i] + d*h[i];
372 if (DOT(sr.rdir, r->ron) > FTINY) {
373 rayvalue(&sr);
374 multcolor(sr.rcol, np->scolor);
375 addcolor(r->rcol, sr.rcol);
376 break;
377 }
378 }
379 ndims--;
380 }
381 /* compute transmission */
382 }