ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/aniso.c
Revision: 2.5
Committed: Wed Jan 15 11:02:43 1992 UTC (32 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +9 -2 lines
Log Message:
added jittering to specular threshold test

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