ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/aniso.c
Revision: 2.34
Committed: Sat Feb 22 02:07:28 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.33: +65 -13 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id$";
3 #endif
4 /*
5 * Shading functions for anisotropic materials.
6 */
7
8 /* ====================================================================
9 * The Radiance Software License, Version 1.0
10 *
11 * Copyright (c) 1990 - 2002 The Regents of the University of California,
12 * through Lawrence Berkeley National Laboratory. All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 *
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 *
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in
23 * the documentation and/or other materials provided with the
24 * distribution.
25 *
26 * 3. The end-user documentation included with the redistribution,
27 * if any, must include the following acknowledgment:
28 * "This product includes Radiance software
29 * (http://radsite.lbl.gov/)
30 * developed by the Lawrence Berkeley National Laboratory
31 * (http://www.lbl.gov/)."
32 * Alternately, this acknowledgment may appear in the software itself,
33 * if and wherever such third-party acknowledgments normally appear.
34 *
35 * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
36 * and "The Regents of the University of California" must
37 * not be used to endorse or promote products derived from this
38 * software without prior written permission. For written
39 * permission, please contact [email protected].
40 *
41 * 5. Products derived from this software may not be called "Radiance",
42 * nor may "Radiance" appear in their name, without prior written
43 * permission of Lawrence Berkeley National Laboratory.
44 *
45 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
46 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48 * DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
49 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
52 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 * ====================================================================
58 *
59 * This software consists of voluntary contributions made by many
60 * individuals on behalf of Lawrence Berkeley National Laboratory. For more
61 * information on Lawrence Berkeley National Laboratory, please see
62 * <http://www.lbl.gov/>.
63 */
64
65 #include "ray.h"
66
67 #include "otypes.h"
68
69 #include "func.h"
70
71 #include "random.h"
72
73 #ifndef MAXITER
74 #define MAXITER 10 /* maximum # specular ray attempts */
75 #endif
76
77 /*
78 * This routine implements the anisotropic Gaussian
79 * model described by Ward in Siggraph `92 article.
80 * We orient the surface towards the incoming ray, so a single
81 * surface can be used to represent an infinitely thin object.
82 *
83 * Arguments for MAT_PLASTIC2 and MAT_METAL2 are:
84 * 4+ ux uy uz funcfile [transform...]
85 * 0
86 * 6 red grn blu specular-frac. u-facet-slope v-facet-slope
87 *
88 * Real arguments for MAT_TRANS2 are:
89 * 8 red grn blu rspec u-rough v-rough trans tspec
90 */
91
92 /* specularity flags */
93 #define SP_REFL 01 /* has reflected specular component */
94 #define SP_TRAN 02 /* has transmitted specular */
95 #define SP_FLAT 04 /* reflecting surface is flat */
96 #define SP_RBLT 010 /* reflection below sample threshold */
97 #define SP_TBLT 020 /* transmission below threshold */
98 #define SP_BADU 040 /* bad u direction calculation */
99
100 typedef struct {
101 OBJREC *mp; /* material pointer */
102 RAY *rp; /* ray pointer */
103 short specfl; /* specularity flags, defined above */
104 COLOR mcolor; /* color of this material */
105 COLOR scolor; /* color of specular component */
106 FVECT vrefl; /* vector in reflected direction */
107 FVECT prdir; /* vector in transmitted direction */
108 FVECT u, v; /* u and v vectors orienting anisotropy */
109 double u_alpha; /* u roughness */
110 double v_alpha; /* v roughness */
111 double rdiff, rspec; /* reflected specular, diffuse */
112 double trans; /* transmissivity */
113 double tdiff, tspec; /* transmitted specular, diffuse */
114 FVECT pnorm; /* perturbed surface normal */
115 double pdot; /* perturbed dot product */
116 } ANISODAT; /* anisotropic material data */
117
118 static void getacoords();
119 static void agaussamp();
120
121
122 static void
123 diraniso(cval, np, ldir, omega) /* compute source contribution */
124 COLOR cval; /* returned coefficient */
125 register ANISODAT *np; /* material data */
126 FVECT ldir; /* light source direction */
127 double omega; /* light source size */
128 {
129 double ldot;
130 double dtmp, dtmp1, dtmp2;
131 FVECT h;
132 double au2, av2;
133 COLOR ctmp;
134
135 setcolor(cval, 0.0, 0.0, 0.0);
136
137 ldot = DOT(np->pnorm, ldir);
138
139 if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY)
140 return; /* wrong side */
141
142 if (ldot > FTINY && np->rdiff > FTINY) {
143 /*
144 * Compute and add diffuse reflected component to returned
145 * color. The diffuse reflected component will always be
146 * modified by the color of the material.
147 */
148 copycolor(ctmp, np->mcolor);
149 dtmp = ldot * omega * np->rdiff / PI;
150 scalecolor(ctmp, dtmp);
151 addcolor(cval, ctmp);
152 }
153 if (ldot > FTINY && (np->specfl&(SP_REFL|SP_BADU)) == SP_REFL) {
154 /*
155 * Compute specular reflection coefficient using
156 * anisotropic gaussian distribution model.
157 */
158 /* add source width if flat */
159 if (np->specfl & SP_FLAT)
160 au2 = av2 = omega/(4.0*PI);
161 else
162 au2 = av2 = 0.0;
163 au2 += np->u_alpha*np->u_alpha;
164 av2 += np->v_alpha*np->v_alpha;
165 /* half vector */
166 h[0] = ldir[0] - np->rp->rdir[0];
167 h[1] = ldir[1] - np->rp->rdir[1];
168 h[2] = ldir[2] - np->rp->rdir[2];
169 /* ellipse */
170 dtmp1 = DOT(np->u, h);
171 dtmp1 *= dtmp1 / au2;
172 dtmp2 = DOT(np->v, h);
173 dtmp2 *= dtmp2 / av2;
174 /* gaussian */
175 dtmp = DOT(np->pnorm, h);
176 dtmp = (dtmp1 + dtmp2) / (dtmp*dtmp);
177 dtmp = exp(-dtmp) * (0.25/PI)
178 * sqrt(ldot/(np->pdot*au2*av2));
179 /* worth using? */
180 if (dtmp > FTINY) {
181 copycolor(ctmp, np->scolor);
182 dtmp *= omega;
183 scalecolor(ctmp, dtmp);
184 addcolor(cval, ctmp);
185 }
186 }
187 if (ldot < -FTINY && np->tdiff > FTINY) {
188 /*
189 * Compute diffuse transmission.
190 */
191 copycolor(ctmp, np->mcolor);
192 dtmp = -ldot * omega * np->tdiff / PI;
193 scalecolor(ctmp, dtmp);
194 addcolor(cval, ctmp);
195 }
196 if (ldot < -FTINY && (np->specfl&(SP_TRAN|SP_BADU)) == SP_TRAN) {
197 /*
198 * Compute specular transmission. Specular transmission
199 * is always modified by material color.
200 */
201 /* roughness + source */
202 au2 = av2 = omega / PI;
203 au2 += np->u_alpha*np->u_alpha;
204 av2 += np->v_alpha*np->v_alpha;
205 /* "half vector" */
206 h[0] = ldir[0] - np->prdir[0];
207 h[1] = ldir[1] - np->prdir[1];
208 h[2] = ldir[2] - np->prdir[2];
209 dtmp = DOT(h,h);
210 if (dtmp > FTINY*FTINY) {
211 dtmp1 = DOT(h,np->pnorm);
212 dtmp = 1.0 - dtmp1*dtmp1/dtmp;
213 if (dtmp > FTINY*FTINY) {
214 dtmp1 = DOT(h,np->u);
215 dtmp1 *= dtmp1 / au2;
216 dtmp2 = DOT(h,np->v);
217 dtmp2 *= dtmp2 / av2;
218 dtmp = (dtmp1 + dtmp2) / dtmp;
219 }
220 } else
221 dtmp = 0.0;
222 /* gaussian */
223 dtmp = exp(-dtmp) * (1.0/PI)
224 * sqrt(-ldot/(np->pdot*au2*av2));
225 /* worth using? */
226 if (dtmp > FTINY) {
227 copycolor(ctmp, np->mcolor);
228 dtmp *= np->tspec * omega;
229 scalecolor(ctmp, dtmp);
230 addcolor(cval, ctmp);
231 }
232 }
233 }
234
235
236 int
237 m_aniso(m, r) /* shade ray that hit something anisotropic */
238 register OBJREC *m;
239 register RAY *r;
240 {
241 ANISODAT nd;
242 COLOR ctmp;
243 register int i;
244 /* easy shadow test */
245 if (r->crtype & SHADOW)
246 return(1);
247
248 if (m->oargs.nfargs != (m->otype == MAT_TRANS2 ? 8 : 6))
249 objerror(m, USER, "bad number of real arguments");
250 nd.mp = m;
251 nd.rp = r;
252 /* get material color */
253 setcolor(nd.mcolor, m->oargs.farg[0],
254 m->oargs.farg[1],
255 m->oargs.farg[2]);
256 /* get roughness */
257 nd.specfl = 0;
258 nd.u_alpha = m->oargs.farg[4];
259 nd.v_alpha = m->oargs.farg[5];
260 if (nd.u_alpha < FTINY || nd.v_alpha <= FTINY)
261 objerror(m, USER, "roughness too small");
262 /* check for back side */
263 if (r->rod < 0.0) {
264 if (!backvis && m->otype != MAT_TRANS2) {
265 raytrans(r);
266 return(1);
267 }
268 flipsurface(r); /* reorient if backvis */
269 }
270 /* get modifiers */
271 raytexture(r, m->omod);
272 nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */
273 if (nd.pdot < .001)
274 nd.pdot = .001; /* non-zero for diraniso() */
275 multcolor(nd.mcolor, r->pcol); /* modify material color */
276 /* get specular component */
277 if ((nd.rspec = m->oargs.farg[3]) > FTINY) {
278 nd.specfl |= SP_REFL;
279 /* compute specular color */
280 if (m->otype == MAT_METAL2)
281 copycolor(nd.scolor, nd.mcolor);
282 else
283 setcolor(nd.scolor, 1.0, 1.0, 1.0);
284 scalecolor(nd.scolor, nd.rspec);
285 /* check threshold */
286 if (specthresh >= nd.rspec-FTINY)
287 nd.specfl |= SP_RBLT;
288 /* compute refl. direction */
289 for (i = 0; i < 3; i++)
290 nd.vrefl[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
291 if (DOT(nd.vrefl, r->ron) <= FTINY) /* penetration? */
292 for (i = 0; i < 3; i++) /* safety measure */
293 nd.vrefl[i] = r->rdir[i] + 2.*r->rod*r->ron[i];
294 }
295 /* compute transmission */
296 if (m->otype == MAT_TRANS2) {
297 nd.trans = m->oargs.farg[6]*(1.0 - nd.rspec);
298 nd.tspec = nd.trans * m->oargs.farg[7];
299 nd.tdiff = nd.trans - nd.tspec;
300 if (nd.tspec > FTINY) {
301 nd.specfl |= SP_TRAN;
302 /* check threshold */
303 if (specthresh >= nd.tspec-FTINY)
304 nd.specfl |= SP_TBLT;
305 if (DOT(r->pert,r->pert) <= FTINY*FTINY) {
306 VCOPY(nd.prdir, r->rdir);
307 } else {
308 for (i = 0; i < 3; i++) /* perturb */
309 nd.prdir[i] = r->rdir[i] - r->pert[i];
310 if (DOT(nd.prdir, r->ron) < -FTINY)
311 normalize(nd.prdir); /* OK */
312 else
313 VCOPY(nd.prdir, r->rdir);
314 }
315 }
316 } else
317 nd.tdiff = nd.tspec = nd.trans = 0.0;
318
319 /* diffuse reflection */
320 nd.rdiff = 1.0 - nd.trans - nd.rspec;
321
322 if (r->ro != NULL && isflat(r->ro->otype))
323 nd.specfl |= SP_FLAT;
324
325 getacoords(r, &nd); /* set up coordinates */
326
327 if (nd.specfl & (SP_REFL|SP_TRAN) && !(nd.specfl & SP_BADU))
328 agaussamp(r, &nd);
329
330 if (nd.rdiff > FTINY) { /* ambient from this side */
331 ambient(ctmp, r, nd.pnorm);
332 if (nd.specfl & SP_RBLT)
333 scalecolor(ctmp, 1.0-nd.trans);
334 else
335 scalecolor(ctmp, nd.rdiff);
336 multcolor(ctmp, nd.mcolor); /* modified by material color */
337 addcolor(r->rcol, ctmp); /* add to returned color */
338 }
339 if (nd.tdiff > FTINY) { /* ambient from other side */
340 FVECT bnorm;
341
342 flipsurface(r);
343 bnorm[0] = -nd.pnorm[0];
344 bnorm[1] = -nd.pnorm[1];
345 bnorm[2] = -nd.pnorm[2];
346 ambient(ctmp, r, bnorm);
347 if (nd.specfl & SP_TBLT)
348 scalecolor(ctmp, nd.trans);
349 else
350 scalecolor(ctmp, nd.tdiff);
351 multcolor(ctmp, nd.mcolor); /* modified by color */
352 addcolor(r->rcol, ctmp);
353 flipsurface(r);
354 }
355 /* add direct component */
356 direct(r, diraniso, &nd);
357
358 return(1);
359 }
360
361
362 static void
363 getacoords(r, np) /* set up coordinate system */
364 RAY *r;
365 register ANISODAT *np;
366 {
367 register MFUNC *mf;
368 register int i;
369
370 mf = getfunc(np->mp, 3, 0x7, 1);
371 setfunc(np->mp, r);
372 errno = 0;
373 for (i = 0; i < 3; i++)
374 np->u[i] = evalue(mf->ep[i]);
375 if (errno) {
376 objerror(np->mp, WARNING, "compute error");
377 np->specfl |= SP_BADU;
378 return;
379 }
380 if (mf->f != &unitxf)
381 multv3(np->u, np->u, mf->f->xfm);
382 fcross(np->v, np->pnorm, np->u);
383 if (normalize(np->v) == 0.0) {
384 objerror(np->mp, WARNING, "illegal orientation vector");
385 np->specfl |= SP_BADU;
386 return;
387 }
388 fcross(np->u, np->v, np->pnorm);
389 }
390
391
392 static void
393 agaussamp(r, np) /* sample anisotropic gaussian specular */
394 RAY *r;
395 register ANISODAT *np;
396 {
397 RAY sr;
398 FVECT h;
399 double rv[2];
400 double d, sinp, cosp;
401 int niter;
402 register int i;
403 /* compute reflection */
404 if ((np->specfl & (SP_REFL|SP_RBLT)) == SP_REFL &&
405 rayorigin(&sr, r, SPECULAR, np->rspec) == 0) {
406 dimlist[ndims++] = (int)np->mp;
407 for (niter = 0; niter < MAXITER; niter++) {
408 if (niter)
409 d = frandom();
410 else
411 d = urand(ilhash(dimlist,ndims)+samplendx);
412 multisamp(rv, 2, d);
413 d = 2.0*PI * rv[0];
414 cosp = tcos(d) * np->u_alpha;
415 sinp = tsin(d) * np->v_alpha;
416 d = sqrt(cosp*cosp + sinp*sinp);
417 cosp /= d;
418 sinp /= d;
419 rv[1] = 1.0 - specjitter*rv[1];
420 if (rv[1] <= FTINY)
421 d = 1.0;
422 else
423 d = sqrt(-log(rv[1]) /
424 (cosp*cosp/(np->u_alpha*np->u_alpha) +
425 sinp*sinp/(np->v_alpha*np->v_alpha)));
426 for (i = 0; i < 3; i++)
427 h[i] = np->pnorm[i] +
428 d*(cosp*np->u[i] + sinp*np->v[i]);
429 d = -2.0 * DOT(h, r->rdir) / (1.0 + d*d);
430 for (i = 0; i < 3; i++)
431 sr.rdir[i] = r->rdir[i] + d*h[i];
432 if (DOT(sr.rdir, r->ron) > FTINY) {
433 rayvalue(&sr);
434 multcolor(sr.rcol, np->scolor);
435 addcolor(r->rcol, sr.rcol);
436 break;
437 }
438 }
439 ndims--;
440 }
441 /* compute transmission */
442 if ((np->specfl & (SP_TRAN|SP_TBLT)) == SP_TRAN &&
443 rayorigin(&sr, r, SPECULAR, np->tspec) == 0) {
444 dimlist[ndims++] = (int)np->mp;
445 for (niter = 0; niter < MAXITER; niter++) {
446 if (niter)
447 d = frandom();
448 else
449 d = urand(ilhash(dimlist,ndims)+1823+samplendx);
450 multisamp(rv, 2, d);
451 d = 2.0*PI * rv[0];
452 cosp = tcos(d) * np->u_alpha;
453 sinp = tsin(d) * np->v_alpha;
454 d = sqrt(cosp*cosp + sinp*sinp);
455 cosp /= d;
456 sinp /= d;
457 rv[1] = 1.0 - specjitter*rv[1];
458 if (rv[1] <= FTINY)
459 d = 1.0;
460 else
461 d = sqrt(-log(rv[1]) /
462 (cosp*cosp/(np->u_alpha*np->u_alpha) +
463 sinp*sinp/(np->v_alpha*np->v_alpha)));
464 for (i = 0; i < 3; i++)
465 sr.rdir[i] = np->prdir[i] +
466 d*(cosp*np->u[i] + sinp*np->v[i]);
467 if (DOT(sr.rdir, r->ron) < -FTINY) {
468 normalize(sr.rdir); /* OK, normalize */
469 rayvalue(&sr);
470 scalecolor(sr.rcol, np->tspec);
471 multcolor(sr.rcol, np->mcolor); /* modify */
472 addcolor(r->rcol, sr.rcol);
473 break;
474 }
475 }
476 ndims--;
477 }
478 }