ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_brdf.c
Revision: 2.14
Committed: Wed Nov 22 09:27:53 1995 UTC (28 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.13: +20 -5 lines
Log Message:
fixed problem with back side normal computation for ambient()

File Contents

# Content
1 /* Copyright (c) 1991 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * Shading for materials with arbitrary BRDF's
9 */
10
11 #include "ray.h"
12
13 #include "data.h"
14
15 #include "otypes.h"
16
17 #include "func.h"
18
19 extern int backvis; /* back faces visible? */
20
21 /*
22 * Arguments to this material include the color and specularity.
23 * String arguments include the reflection function and files.
24 * The BRDF is currently used just for the specular component to light
25 * sources. Reflectance values or data coordinates are functions
26 * of the direction to the light source. (Data modification functions
27 * are passed the source direction as args 2-4.)
28 * We orient the surface towards the incoming ray, so a single
29 * surface can be used to represent an infinitely thin object.
30 *
31 * Arguments for MAT_PFUNC and MAT_MFUNC are:
32 * 2+ func funcfile transform
33 * 0
34 * 4+ red grn blu specularity A5 ..
35 *
36 * Arguments for MAT_PDATA and MAT_MDATA are:
37 * 4+ func datafile funcfile v0 .. transform
38 * 0
39 * 4+ red grn blu specularity A5 ..
40 *
41 * Arguments for MAT_TFUNC are:
42 * 2+ func funcfile transform
43 * 0
44 * 4+ red grn blu rspec trans tspec A7 ..
45 *
46 * Arguments for MAT_TDATA are:
47 * 4+ func datafile funcfile v0 .. transform
48 * 0
49 * 4+ red grn blu rspec trans tspec A7 ..
50 *
51 * Arguments for the more general MAT_BRTDF are:
52 * 10+ rrefl grefl brefl
53 * rtrns gtrns btrns
54 * rbrtd gbrtd bbrtd
55 * funcfile transform
56 * 0
57 * 9+ rdf gdf bdf
58 * rdb gdb bdb
59 * rdt gdt bdt A10 ..
60 *
61 * In addition to the normal variables available to functions,
62 * we define the following:
63 * NxP, NyP, NzP - perturbed surface normal
64 * RdotP - perturbed ray dot product
65 * CrP, CgP, CbP - perturbed material color (or pattern)
66 */
67
68 typedef struct {
69 OBJREC *mp; /* material pointer */
70 RAY *pr; /* intersected ray */
71 DATARRAY *dp; /* data array for PDATA, MDATA or TDATA */
72 COLOR mcolor; /* material (or pattern) color */
73 COLOR rdiff; /* diffuse reflection */
74 COLOR tdiff; /* diffuse transmission */
75 double rspec; /* specular reflectance (1 for BRDTF) */
76 double trans; /* transmissivity (.5 for BRDTF) */
77 double tspec; /* specular transmittance (1 for BRDTF) */
78 FVECT pnorm; /* perturbed surface normal */
79 double pdot; /* perturbed dot product */
80 } BRDFDAT; /* BRDF material data */
81
82
83 dirbrdf(cval, np, ldir, omega) /* compute source contribution */
84 COLOR cval; /* returned coefficient */
85 register BRDFDAT *np; /* material data */
86 FVECT ldir; /* light source direction */
87 double omega; /* light source size */
88 {
89 double ldot;
90 double dtmp;
91 COLOR ctmp;
92 FVECT ldx;
93 static double vldx[5], pt[MAXDIM];
94 register char **sa;
95 register int i;
96 #define lddx (vldx+1)
97
98 setcolor(cval, 0.0, 0.0, 0.0);
99
100 ldot = DOT(np->pnorm, ldir);
101
102 if (ldot <= FTINY && ldot >= -FTINY)
103 return; /* too close to grazing */
104
105 if (ldot < 0.0 ? np->trans <= FTINY : np->trans >= 1.0-FTINY)
106 return; /* wrong side */
107
108 if (ldot > 0.0) {
109 /*
110 * Compute and add diffuse reflected component to returned
111 * color. The diffuse reflected component will always be
112 * modified by the color of the material.
113 */
114 copycolor(ctmp, np->rdiff);
115 dtmp = ldot * omega / PI;
116 scalecolor(ctmp, dtmp);
117 addcolor(cval, ctmp);
118 } else {
119 /*
120 * Diffuse transmitted component.
121 */
122 copycolor(ctmp, np->tdiff);
123 dtmp = -ldot * omega / PI;
124 scalecolor(ctmp, dtmp);
125 addcolor(cval, ctmp);
126 }
127 if (ldot > 0.0 ? np->rspec <= FTINY : np->tspec <= FTINY)
128 return; /* no specular component */
129 /* set up function */
130 setbrdfunc(np);
131 sa = np->mp->oargs.sarg;
132 errno = 0;
133 /* transform light vector */
134 multv3(ldx, ldir, funcxf.xfm);
135 for (i = 0; i < 3; i++)
136 lddx[i] = ldx[i]/funcxf.sca;
137 lddx[3] = omega;
138 /* compute BRTDF */
139 if (np->mp->otype == MAT_BRTDF) {
140 if (sa[6][0] == '0') /* special case */
141 colval(ctmp,RED) = 0.0;
142 else
143 colval(ctmp,RED) = funvalue(sa[6], 4, lddx);
144 if (!strcmp(sa[7],sa[6]))
145 colval(ctmp,GRN) = colval(ctmp,RED);
146 else
147 colval(ctmp,GRN) = funvalue(sa[7], 4, lddx);
148 if (!strcmp(sa[8],sa[6]))
149 colval(ctmp,BLU) = colval(ctmp,RED);
150 else if (!strcmp(sa[8],sa[7]))
151 colval(ctmp,BLU) = colval(ctmp,GRN);
152 else
153 colval(ctmp,BLU) = funvalue(sa[8], 4, lddx);
154 dtmp = bright(ctmp);
155 } else if (np->dp == NULL) {
156 dtmp = funvalue(sa[0], 4, lddx);
157 setcolor(ctmp, dtmp, dtmp, dtmp);
158 } else {
159 for (i = 0; i < np->dp->nd; i++)
160 pt[i] = funvalue(sa[3+i], 4, lddx);
161 vldx[0] = datavalue(np->dp, pt);
162 dtmp = funvalue(sa[0], 5, vldx);
163 setcolor(ctmp, dtmp, dtmp, dtmp);
164 }
165 if (errno) {
166 objerror(np->mp, WARNING, "compute error");
167 return;
168 }
169 if (dtmp <= FTINY)
170 return;
171 if (ldot > 0.0) {
172 /*
173 * Compute reflected non-diffuse component.
174 */
175 if (np->mp->otype == MAT_MFUNC | np->mp->otype == MAT_MDATA)
176 multcolor(ctmp, np->mcolor);
177 dtmp = ldot * omega * np->rspec;
178 scalecolor(ctmp, dtmp);
179 addcolor(cval, ctmp);
180 } else {
181 /*
182 * Compute transmitted non-diffuse component.
183 */
184 if (np->mp->otype == MAT_TFUNC | np->mp->otype == MAT_TDATA)
185 multcolor(ctmp, np->mcolor);
186 dtmp = -ldot * omega * np->tspec;
187 scalecolor(ctmp, dtmp);
188 addcolor(cval, ctmp);
189 }
190 #undef lddx
191 }
192
193
194 m_brdf(m, r) /* color a ray which hit a BRDTfunc material */
195 register OBJREC *m;
196 register RAY *r;
197 {
198 BRDFDAT nd;
199 RAY sr;
200 double transtest, transdist;
201 int hasrefl, hastrans;
202 COLOR ctmp;
203 FVECT vtmp;
204 register MFUNC *mf;
205 register int i;
206 /* check arguments */
207 if (m->oargs.nsargs < 10 | m->oargs.nfargs < 9)
208 objerror(m, USER, "bad # arguments");
209 nd.mp = m;
210 nd.pr = r;
211 /* dummy values */
212 nd.rspec = nd.tspec = 1.0;
213 nd.trans = 0.5;
214 /* diffuse reflectance */
215 if (r->rod > 0.0)
216 setcolor(nd.rdiff, m->oargs.farg[0],
217 m->oargs.farg[1],
218 m->oargs.farg[2]);
219 else
220 setcolor(nd.rdiff, m->oargs.farg[3],
221 m->oargs.farg[4],
222 m->oargs.farg[5]);
223 /* diffuse transmittance */
224 setcolor(nd.tdiff, m->oargs.farg[6],
225 m->oargs.farg[7],
226 m->oargs.farg[8]);
227 /* get modifiers */
228 raytexture(r, m->omod);
229 nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */
230 if (r->rod < 0.0) { /* orient perturbed values */
231 nd.pdot = -nd.pdot;
232 for (i = 0; i < 3; i++) {
233 nd.pnorm[i] = -nd.pnorm[i];
234 r->pert[i] = -r->pert[i];
235 }
236 }
237 copycolor(nd.mcolor, r->pcol); /* get pattern color */
238 multcolor(nd.rdiff, nd.mcolor); /* modify diffuse values */
239 multcolor(nd.tdiff, nd.mcolor);
240 hasrefl = bright(nd.rdiff) > FTINY;
241 hastrans = bright(nd.tdiff) > FTINY;
242 /* load cal file */
243 nd.dp = NULL;
244 mf = getfunc(m, 9, 0x3f, 0);
245 /* compute transmitted ray */
246 setbrdfunc(&nd);
247 transtest = 0;
248 transdist = r->rot;
249 errno = 0;
250 setcolor(ctmp, evalue(mf->ep[3]),
251 evalue(mf->ep[4]),
252 evalue(mf->ep[5]));
253 if (errno)
254 objerror(m, WARNING, "compute error");
255 else if (rayorigin(&sr, r, TRANS, bright(ctmp)) == 0) {
256 if (!(r->crtype & SHADOW) &&
257 DOT(r->pert,r->pert) > FTINY*FTINY) {
258 for (i = 0; i < 3; i++) /* perturb direction */
259 sr.rdir[i] = r->rdir[i] - .75*r->pert[i];
260 if (normalize(sr.rdir) == 0.0) {
261 objerror(m, WARNING, "illegal perturbation");
262 VCOPY(sr.rdir, r->rdir);
263 }
264 } else {
265 VCOPY(sr.rdir, r->rdir);
266 transtest = 2;
267 }
268 rayvalue(&sr);
269 multcolor(sr.rcol, ctmp);
270 addcolor(r->rcol, sr.rcol);
271 transtest *= bright(sr.rcol);
272 transdist = r->rot + sr.rt;
273 }
274 if (r->crtype & SHADOW) /* the rest is shadow */
275 return(1);
276 /* compute reflected ray */
277 setbrdfunc(&nd);
278 errno = 0;
279 setcolor(ctmp, evalue(mf->ep[0]),
280 evalue(mf->ep[1]),
281 evalue(mf->ep[2]));
282 if (errno)
283 objerror(m, WARNING, "compute error");
284 else if (rayorigin(&sr, r, REFLECTED, bright(ctmp)) == 0) {
285 for (i = 0; i < 3; i++)
286 sr.rdir[i] = r->rdir[i] + 2.0*nd.pdot*nd.pnorm[i];
287 rayvalue(&sr);
288 multcolor(sr.rcol, ctmp);
289 addcolor(r->rcol, sr.rcol);
290 }
291 /* compute ambient */
292 if (hasrefl) {
293 if (nd.pdot < 0.0) {
294 flipsurface(r);
295 vtmp[0] = -nd.pnorm[0];
296 vtmp[1] = -nd.pnorm[1];
297 vtmp[2] = -nd.pnorm[2];
298 } else
299 VCOPY(vtmp, nd.pnorm);
300 ambient(ctmp, r, vtmp);
301 multcolor(ctmp, nd.rdiff);
302 addcolor(r->rcol, ctmp); /* add to returned color */
303 if (nd.pdot < 0.0)
304 flipsurface(r);
305 }
306 if (hastrans) { /* from other side */
307 if (nd.pdot > 0.0) {
308 flipsurface(r);
309 vtmp[0] = -nd.pnorm[0];
310 vtmp[1] = -nd.pnorm[1];
311 vtmp[2] = -nd.pnorm[2];
312 } else
313 VCOPY(vtmp, nd.pnorm);
314 ambient(ctmp, r, vtmp);
315 multcolor(ctmp, nd.tdiff);
316 addcolor(r->rcol, ctmp);
317 if (nd.pdot > 0.0)
318 flipsurface(r);
319 }
320 if (hasrefl | hastrans || m->oargs.sarg[6][0] != '0')
321 direct(r, dirbrdf, &nd); /* add direct component */
322 /* check distance */
323 if (transtest > bright(r->rcol))
324 r->rt = transdist;
325
326 return(1);
327 }
328
329
330
331 m_brdf2(m, r) /* color a ray which hit a BRDF material */
332 register OBJREC *m;
333 register RAY *r;
334 {
335 BRDFDAT nd;
336 COLOR ctmp;
337 FVECT vtmp;
338 double dtmp;
339 /* always a shadow */
340 if (r->crtype & SHADOW)
341 return(1);
342 /* check arguments */
343 if (m->oargs.nsargs < (hasdata(m->otype)?4:2) | m->oargs.nfargs <
344 (m->otype==MAT_TFUNC|m->otype==MAT_TDATA?6:4))
345 objerror(m, USER, "bad # arguments");
346 nd.mp = m;
347 nd.pr = r;
348 /* get material color */
349 setcolor(nd.mcolor, m->oargs.farg[0],
350 m->oargs.farg[1],
351 m->oargs.farg[2]);
352 /* get specular component */
353 nd.rspec = m->oargs.farg[3];
354 /* compute transmittance */
355 if (m->otype == MAT_TFUNC | m->otype == MAT_TDATA) {
356 nd.trans = m->oargs.farg[4]*(1.0 - nd.rspec);
357 nd.tspec = nd.trans * m->oargs.farg[5];
358 dtmp = nd.trans - nd.tspec;
359 setcolor(nd.tdiff, dtmp, dtmp, dtmp);
360 } else {
361 nd.tspec = nd.trans = 0.0;
362 setcolor(nd.tdiff, 0.0, 0.0, 0.0);
363 }
364 /* compute reflectance */
365 dtmp = 1.0 - nd.trans - nd.rspec;
366 setcolor(nd.rdiff, dtmp, dtmp, dtmp);
367 /* check for back side */
368 if (r->rod < 0.0) {
369 if (!backvis && m->otype != MAT_TFUNC
370 && m->otype != MAT_TDATA) {
371 raytrans(r);
372 return(1);
373 }
374 flipsurface(r); /* reorient if backvis */
375 }
376 /* get modifiers */
377 raytexture(r, m->omod);
378 nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */
379 multcolor(nd.mcolor, r->pcol); /* modify material color */
380 multcolor(nd.rdiff, nd.mcolor);
381 multcolor(nd.tdiff, nd.mcolor);
382 /* load auxiliary files */
383 if (hasdata(m->otype)) {
384 nd.dp = getdata(m->oargs.sarg[1]);
385 getfunc(m, 2, 0, 0);
386 } else {
387 nd.dp = NULL;
388 getfunc(m, 1, 0, 0);
389 }
390 /* compute ambient */
391 if (nd.trans < 1.0-FTINY) {
392 ambient(ctmp, r, nd.pnorm);
393 scalecolor(ctmp, 1.0-nd.trans);
394 multcolor(ctmp, nd.mcolor); /* modified by material color */
395 addcolor(r->rcol, ctmp); /* add to returned color */
396 }
397 if (nd.trans > FTINY) { /* from other side */
398 flipsurface(r);
399 vtmp[0] = -nd.pnorm[0];
400 vtmp[1] = -nd.pnorm[1];
401 vtmp[2] = -nd.pnorm[2];
402 ambient(ctmp, r, vtmp);
403 scalecolor(ctmp, nd.trans);
404 multcolor(ctmp, nd.mcolor);
405 addcolor(r->rcol, ctmp);
406 flipsurface(r);
407 }
408 /* add direct component */
409 direct(r, dirbrdf, &nd);
410
411 return(1);
412 }
413
414
415 setbrdfunc(np) /* set up brdf function and variables */
416 register BRDFDAT *np;
417 {
418 FVECT vec;
419
420 if (setfunc(np->mp, np->pr) == 0)
421 return(0); /* it's OK, setfunc says we're done */
422 /* else (re)assign special variables */
423 multv3(vec, np->pnorm, funcxf.xfm);
424 varset("NxP", '=', vec[0]/funcxf.sca);
425 varset("NyP", '=', vec[1]/funcxf.sca);
426 varset("NzP", '=', vec[2]/funcxf.sca);
427 varset("RdotP", '=', np->pdot <= -1.0 ? -1.0 :
428 np->pdot >= 1.0 ? 1.0 : np->pdot);
429 varset("CrP", '=', colval(np->mcolor,RED));
430 varset("CgP", '=', colval(np->mcolor,GRN));
431 varset("CbP", '=', colval(np->mcolor,BLU));
432 return(1);
433 }