ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/aniso.c
Revision: 2.24
Committed: Mon Mar 8 12:37:18 1993 UTC (31 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.23: +2 -0 lines
Log Message:
portability fixes (removed gcc warnings)

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 static agaussamp();
23
24 /*
25 * This routine implements the anisotropic Gaussian
26 * model described by Ward in Siggraph `92 article.
27 * We orient the surface towards the incoming ray, so a single
28 * surface can be used to represent an infinitely thin object.
29 *
30 * Arguments for MAT_PLASTIC2 and MAT_METAL2 are:
31 * 4+ ux uy uz funcfile [transform...]
32 * 0
33 * 6 red grn blu specular-frac. u-facet-slope v-facet-slope
34 *
35 * Real arguments for MAT_TRANS2 are:
36 * 8 red grn blu rspec u-rough v-rough trans tspec
37 */
38
39 #define BSPEC(m) (6.0) /* specularity parameter b */
40
41 /* specularity flags */
42 #define SP_REFL 01 /* has reflected specular component */
43 #define SP_TRAN 02 /* has transmitted specular */
44 #define SP_FLAT 04 /* reflecting surface is flat */
45 #define SP_RBLT 010 /* reflection below sample threshold */
46 #define SP_TBLT 020 /* transmission below threshold */
47 #define SP_BADU 040 /* bad u direction calculation */
48
49 typedef struct {
50 OBJREC *mp; /* material pointer */
51 RAY *rp; /* ray pointer */
52 short specfl; /* specularity flags, defined above */
53 COLOR mcolor; /* color of this material */
54 COLOR scolor; /* color of specular component */
55 FVECT vrefl; /* vector in reflected direction */
56 FVECT prdir; /* vector in transmitted direction */
57 FVECT u, v; /* u and v vectors orienting anisotropy */
58 double u_alpha; /* u roughness */
59 double v_alpha; /* v roughness */
60 double rdiff, rspec; /* reflected specular, diffuse */
61 double trans; /* transmissivity */
62 double tdiff, tspec; /* transmitted specular, diffuse */
63 FVECT pnorm; /* perturbed surface normal */
64 double pdot; /* perturbed dot product */
65 } ANISODAT; /* anisotropic material data */
66
67
68 diraniso(cval, np, ldir, omega) /* compute source contribution */
69 COLOR cval; /* returned coefficient */
70 register ANISODAT *np; /* material data */
71 FVECT ldir; /* light source direction */
72 double omega; /* light source size */
73 {
74 double ldot;
75 double dtmp, dtmp1, dtmp2;
76 FVECT h;
77 double au2, av2;
78 COLOR ctmp;
79
80 setcolor(cval, 0.0, 0.0, 0.0);
81
82 ldot = DOT(np->pnorm, ldir);
83
84 if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY)
85 return; /* wrong side */
86
87 if (ldot > FTINY && np->rdiff > FTINY) {
88 /*
89 * Compute and add diffuse reflected component to returned
90 * color. The diffuse reflected component will always be
91 * modified by the color of the material.
92 */
93 copycolor(ctmp, np->mcolor);
94 dtmp = ldot * omega * np->rdiff / PI;
95 scalecolor(ctmp, dtmp);
96 addcolor(cval, ctmp);
97 }
98 if (ldot > FTINY && (np->specfl&(SP_REFL|SP_BADU)) == SP_REFL) {
99 /*
100 * Compute specular reflection coefficient using
101 * anisotropic gaussian distribution model.
102 */
103 /* add source width if flat */
104 if (np->specfl & SP_FLAT)
105 au2 = av2 = omega/(4.0*PI);
106 else
107 au2 = av2 = 0.0;
108 au2 += np->u_alpha*np->u_alpha;
109 av2 += np->v_alpha*np->v_alpha;
110 /* half vector */
111 h[0] = ldir[0] - np->rp->rdir[0];
112 h[1] = ldir[1] - np->rp->rdir[1];
113 h[2] = ldir[2] - np->rp->rdir[2];
114 /* ellipse */
115 dtmp1 = DOT(np->u, h);
116 dtmp1 *= dtmp1 / au2;
117 dtmp2 = DOT(np->v, h);
118 dtmp2 *= dtmp2 / av2;
119 /* gaussian */
120 dtmp = DOT(np->pnorm, h);
121 dtmp = (dtmp1 + dtmp2) / (dtmp*dtmp);
122 dtmp = exp(-dtmp) * (0.25/PI)
123 * sqrt(ldot/(np->pdot*au2*av2));
124 /* worth using? */
125 if (dtmp > FTINY) {
126 copycolor(ctmp, np->scolor);
127 dtmp *= omega;
128 scalecolor(ctmp, dtmp);
129 addcolor(cval, ctmp);
130 }
131 }
132 if (ldot < -FTINY && np->tdiff > FTINY) {
133 /*
134 * Compute diffuse transmission.
135 */
136 copycolor(ctmp, np->mcolor);
137 dtmp = -ldot * omega * np->tdiff / PI;
138 scalecolor(ctmp, dtmp);
139 addcolor(cval, ctmp);
140 }
141 if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_BADU)) == SP_TRAN) {
142 /*
143 * Compute specular transmission. Specular transmission
144 * is always modified by material color.
145 */
146 /* roughness + source */
147 au2 = av2 = omega / PI;
148 au2 += np->u_alpha*np->u_alpha;
149 av2 += np->v_alpha*np->v_alpha;
150 /* "half vector" */
151 h[0] = ldir[0] - np->prdir[0];
152 h[1] = ldir[1] - np->prdir[1];
153 h[2] = ldir[2] - np->prdir[2];
154 dtmp = DOT(h,h);
155 if (dtmp > FTINY*FTINY) {
156 dtmp1 = DOT(h,np->pnorm);
157 dtmp = 1.0 - dtmp1*dtmp1/dtmp;
158 if (dtmp > FTINY*FTINY) {
159 dtmp1 = DOT(h,np->u);
160 dtmp1 *= dtmp1 / au2;
161 dtmp2 = DOT(h,np->v);
162 dtmp2 *= dtmp2 / av2;
163 dtmp = (dtmp1 + dtmp2) / dtmp;
164 }
165 } else
166 dtmp = 0.0;
167 /* gaussian */
168 dtmp = exp(-dtmp) * (1.0/PI)
169 * sqrt(-ldot/(np->pdot*au2*av2));
170 /* worth using? */
171 if (dtmp > FTINY) {
172 copycolor(ctmp, np->mcolor);
173 dtmp *= np->tspec * omega;
174 scalecolor(ctmp, dtmp);
175 addcolor(cval, ctmp);
176 }
177 }
178 }
179
180
181 m_aniso(m, r) /* shade ray that hit something anisotropic */
182 register OBJREC *m;
183 register RAY *r;
184 {
185 ANISODAT nd;
186 double dtmp;
187 COLOR ctmp;
188 register int i;
189 /* easy shadow test */
190 if (r->crtype & SHADOW)
191 return;
192
193 if (m->oargs.nfargs != (m->otype == MAT_TRANS2 ? 8 : 6))
194 objerror(m, USER, "bad number of real arguments");
195 nd.mp = m;
196 nd.rp = r;
197 /* get material color */
198 setcolor(nd.mcolor, m->oargs.farg[0],
199 m->oargs.farg[1],
200 m->oargs.farg[2]);
201 /* get roughness */
202 nd.specfl = 0;
203 nd.u_alpha = m->oargs.farg[4];
204 nd.v_alpha = m->oargs.farg[5];
205 if (nd.u_alpha < FTINY || nd.v_alpha <= FTINY)
206 objerror(m, USER, "roughness too small");
207 /* reorient if necessary */
208 if (r->rod < 0.0)
209 flipsurface(r);
210 /* get modifiers */
211 raytexture(r, m->omod);
212 nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */
213 if (nd.pdot < .001)
214 nd.pdot = .001; /* non-zero for diraniso() */
215 multcolor(nd.mcolor, r->pcol); /* modify material color */
216 /* get specular component */
217 if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
218 nd.specfl |= SP_REFL;
219 /* compute specular color */
220 if (m->otype == MAT_METAL2)
221 copycolor(nd.scolor, nd.mcolor);
222 else
223 setcolor(nd.scolor, 1.0, 1.0, 1.0);
224 scalecolor(nd.scolor, nd.rspec);
225 /* improved model */
226 dtmp = exp(-BSPEC(m)*nd.pdot);
227 for (i = 0; i < 3; i++)
228 colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
229 nd.rspec += (1.0-nd.rspec)*dtmp;
230 /* check threshold */
231 if (specthresh > FTINY &&
232 (specthresh >= 1.-FTINY ||
233 specthresh + .05 - .1*frandom() > nd.rspec))
234 nd.specfl |= SP_RBLT;
235 /* compute refl. direction */
236 for (i = 0; i < 3; i++)
237 nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
238 if (DOT(nd.vrefl, r->ron) <= FTINY) /* penetration? */
239 for (i = 0; i < 3; i++) /* safety measure */
240 nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
241 }
242 /* compute transmission */
243 if (m->otype == MAT_TRANS2) {
244 nd.trans = m->oargs.farg[6]*(1.0 - nd.rspec);
245 nd.tspec = nd.trans * m->oargs.farg[7];
246 nd.tdiff = nd.trans - nd.tspec;
247 if (nd.tspec > FTINY) {
248 nd.specfl |= SP_TRAN;
249 /* check threshold */
250 if (specthresh > FTINY &&
251 (specthresh >= 1.-FTINY ||
252 specthresh + .05 - .1*frandom() > nd.tspec))
253 nd.specfl |= SP_TBLT;
254 if (DOT(r->pert,r->pert) <= FTINY*FTINY) {
255 VCOPY(nd.prdir, r->rdir);
256 } else {
257 for (i = 0; i < 3; i++) /* perturb */
258 nd.prdir[i] = r->rdir[i] - r->pert[i];
259 if (DOT(nd.prdir, r->ron) < -FTINY)
260 normalize(nd.prdir); /* OK */
261 else
262 VCOPY(nd.prdir, r->rdir);
263 }
264 }
265 } else
266 nd.tdiff = nd.tspec = nd.trans = 0.0;
267
268 /* diffuse reflection */
269 nd.rdiff = 1.0 - nd.trans - nd.rspec;
270
271 if (r->ro != NULL && (r->ro->otype == OBJ_FACE ||
272 r->ro->otype == OBJ_RING))
273 nd.specfl |= SP_FLAT;
274
275 getacoords(r, &nd); /* set up coordinates */
276
277 if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_BADU))
278 agaussamp(r, &nd);
279
280 if (nd.rdiff > FTINY) { /* ambient from this side */
281 ambient(ctmp, r);
282 if (nd.specfl & SP_RBLT)
283 scalecolor(ctmp, 1.0-nd.trans);
284 else
285 scalecolor(ctmp, nd.rdiff);
286 multcolor(ctmp, nd.mcolor); /* modified by material color */
287 addcolor(r->rcol, ctmp); /* add to returned color */
288 }
289 if (nd.tdiff > FTINY) { /* ambient from other side */
290 flipsurface(r);
291 ambient(ctmp, r);
292 if (nd.specfl & SP_TBLT)
293 scalecolor(ctmp, nd.trans);
294 else
295 scalecolor(ctmp, nd.tdiff);
296 multcolor(ctmp, nd.mcolor); /* modified by color */
297 addcolor(r->rcol, ctmp);
298 flipsurface(r);
299 }
300 /* add direct component */
301 direct(r, diraniso, &nd);
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 if (mf->f != &unitxf)
324 multv3(np->u, np->u, mf->f->xfm);
325 fcross(np->v, np->pnorm, np->u);
326 if (normalize(np->v) == 0.0) {
327 objerror(np->mp, WARNING, "illegal orientation vector");
328 np->specfl |= SP_BADU;
329 return;
330 }
331 fcross(np->u, np->v, np->pnorm);
332 }
333
334
335 static
336 agaussamp(r, np) /* sample anisotropic gaussian specular */
337 RAY *r;
338 register ANISODAT *np;
339 {
340 RAY sr;
341 FVECT h;
342 double rv[2];
343 double d, sinp, cosp;
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 d = urand(ilhash(dimlist,ndims)+samplendx);
350 multisamp(rv, 2, d);
351 d = 2.0*PI * rv[0];
352 cosp = cos(d) * np->u_alpha;
353 sinp = sin(d) * np->v_alpha;
354 d = sqrt(cosp*cosp + sinp*sinp);
355 cosp /= d;
356 sinp /= d;
357 rv[1] = 1.0 - specjitter*rv[1];
358 if (rv[1] <= FTINY)
359 d = 1.0;
360 else
361 d = sqrt(-log(rv[1]) /
362 (cosp*cosp/(np->u_alpha*np->u_alpha) +
363 sinp*sinp/(np->v_alpha*np->v_alpha)));
364 for (i = 0; i < 3; i++)
365 h[i] = np->pnorm[i] +
366 d*(cosp*np->u[i] + sinp*np->v[i]);
367 d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
368 for (i = 0; i < 3; i++)
369 sr.rdir[i] = r->rdir[i] + d*h[i];
370 if (DOT(sr.rdir, r->ron) <= FTINY) /* penetration? */
371 VCOPY(sr.rdir, np->vrefl); /* jitter no good */
372 rayvalue(&sr);
373 multcolor(sr.rcol, np->scolor);
374 addcolor(r->rcol, sr.rcol);
375 ndims--;
376 }
377 /* compute transmission */
378 if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
379 rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
380 dimlist[ndims++] = (int)np->mp;
381 d = urand(ilhash(dimlist,ndims)+1823+samplendx);
382 multisamp(rv, 2, d);
383 d = 2.0*PI * rv[0];
384 cosp = cos(d) * np->u_alpha;
385 sinp = sin(d) * np->v_alpha;
386 d = sqrt(cosp*cosp + sinp*sinp);
387 cosp /= d;
388 sinp /= d;
389 rv[1] = 1.0 - specjitter*rv[1];
390 if (rv[1] <= FTINY)
391 d = 1.0;
392 else
393 d = sqrt(-log(rv[1]) /
394 (cosp*cosp/(np->u_alpha*np->u_alpha) +
395 sinp*sinp/(np->v_alpha*np->u_alpha)));
396 for (i = 0; i < 3; i++)
397 sr.rdir[i] = np->prdir[i] +
398 d*(cosp*np->u[i] + sinp*np->v[i]);
399 if (DOT(sr.rdir, r->ron) < -FTINY)
400 normalize(sr.rdir); /* OK, normalize */
401 else
402 VCOPY(sr.rdir, np->prdir); /* else no jitter */
403 rayvalue(&sr);
404 scalecolor(sr.rcol, np->tspec);
405 multcolor(sr.rcol, np->mcolor); /* modify by color */
406 addcolor(r->rcol, sr.rcol);
407 ndims--;
408 }
409 }