ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_brdf.c
Revision: 1.1
Committed: Wed Dec 12 22:02:58 1990 UTC (33 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

File Contents

# Content
1 /* Copyright (c) 1990 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 /*
18 * Arguments to this material include the color and specularity.
19 * String arguments include the reflection function and files.
20 * The BRDF is currently used just for the specular component to light
21 * sources. Reflectance values or data coordinates are functions
22 * of the direction to the light source.
23 * We orient the surface towards the incoming ray, so a single
24 * surface can be used to represent an infinitely thin object.
25 *
26 * Arguments for MAT_PFUNC and MAT_MFUNC are:
27 * 2+ func funcfile transform ..
28 * 0
29 * 4+ red grn blu specularity args ..
30 *
31 * Arguments for MAT_PDATA and MAT_MDATA are:
32 * 4+ func datafile funcfile v0 .. transform ..
33 * 0
34 * 4+ red grn blu specularity args ..
35 */
36
37 #define BSPEC(m) (6.0) /* specular parameter b */
38
39 typedef struct {
40 OBJREC *mp; /* material pointer */
41 RAY *pr; /* intersected ray */
42 DATARRAY *dp; /* data array for PDATA or MDATA */
43 COLOR mcolor; /* color of this material */
44 COLOR scolor; /* color of specular component */
45 double rspec; /* specular reflection */
46 double rdiff; /* diffuse reflection */
47 FVECT pnorm; /* perturbed surface normal */
48 double pdot; /* perturbed dot product */
49 } BRDFDAT; /* BRDF material data */
50
51
52 dirbrdf(cval, np, ldir, omega) /* compute source contribution */
53 COLOR cval; /* returned coefficient */
54 register BRDFDAT *np; /* material data */
55 FVECT ldir; /* light source direction */
56 double omega; /* light source size */
57 {
58 double ldot;
59 double dtmp;
60 COLOR ctmp;
61 double pt[MAXDIM];
62 register int i;
63
64 setcolor(cval, 0.0, 0.0, 0.0);
65
66 ldot = DOT(np->pnorm, ldir);
67
68 if (ldot < 0.0)
69 return; /* wrong side */
70
71 if (np->rdiff > FTINY) {
72 /*
73 * Compute and add diffuse reflected component to returned
74 * color. The diffuse reflected component will always be
75 * modified by the color of the material.
76 */
77 copycolor(ctmp, np->mcolor);
78 dtmp = ldot * omega * np->rdiff / PI;
79 scalecolor(ctmp, dtmp);
80 addcolor(cval, ctmp);
81 }
82 if (np->rspec > FTINY) {
83 /*
84 * Compute specular component.
85 */
86 setfunc(np->mp, np->pr);
87 errno = 0;
88 if (np->dp == NULL)
89 dtmp = funvalue(np->mp->oargs.sarg[0], 3, ldir);
90 else {
91 for (i = 0; i < np->dp->nd; i++)
92 pt[i] = funvalue(np->mp->oargs.sarg[3+i],
93 3, ldir);
94 dtmp = datavalue(np->dp, pt);
95 dtmp = funvalue(np->mp->oargs.sarg[0], 1, &dtmp);
96 }
97 if (errno)
98 goto computerr;
99 if (dtmp > FTINY) {
100 copycolor(ctmp, np->scolor);
101 dtmp *= ldot * omega;
102 scalecolor(ctmp, dtmp);
103 addcolor(cval, ctmp);
104 }
105 }
106 return;
107 computerr:
108 objerror(np->mp, WARNING, "compute error");
109 return;
110 }
111
112
113 m_brdf(m, r) /* color a ray which hit a BRDF material */
114 register OBJREC *m;
115 register RAY *r;
116 {
117 BRDFDAT nd;
118 double dtmp;
119 COLOR ctmp;
120 register int i;
121
122 if (m->oargs.nsargs < 2 || m->oargs.nfargs < 4)
123 objerror(m, USER, "bad # arguments");
124 /* easy shadow test */
125 if (r->crtype & SHADOW)
126 return;
127 nd.mp = m;
128 nd.pr = r;
129 /* load auxiliary files */
130 if (m->otype == MAT_PDATA || m->otype == MAT_MDATA) {
131 nd.dp = getdata(m->oargs.sarg[1]);
132 for (i = 3; i < m->oargs.nsargs; i++)
133 if (m->oargs.sarg[i][0] == '-')
134 break;
135 if (i-3 != nd.dp->nd)
136 objerror(m, USER, "dimension error");
137 if (!fundefined(m->oargs.sarg[3]))
138 loadfunc(m->oargs.sarg[2]);
139 } else {
140 nd.dp = NULL;
141 if (!fundefined(m->oargs.sarg[0]))
142 loadfunc(m->oargs.sarg[1]);
143 }
144 /* get material color */
145 setcolor(nd.mcolor, m->oargs.farg[0],
146 m->oargs.farg[1],
147 m->oargs.farg[2]);
148 /* get roughness */
149 if (r->rod < 0.0)
150 flipsurface(r);
151 /* get modifiers */
152 raytexture(r, m->omod);
153 nd.pdot = raynormal(nd.pnorm, r); /* perturb normal */
154 multcolor(nd.mcolor, r->pcol); /* modify material color */
155 r->rt = r->rot; /* default ray length */
156 /* get specular component */
157 nd.rspec = m->oargs.farg[3];
158
159 if (nd.rspec > FTINY) { /* has specular component */
160 /* compute specular color */
161 if (m->otype == MAT_MFUNC || m->otype == MAT_MDATA)
162 copycolor(nd.scolor, nd.mcolor);
163 else
164 setcolor(nd.scolor, 1.0, 1.0, 1.0);
165 scalecolor(nd.scolor, nd.rspec);
166 /* improved model */
167 dtmp = exp(-BSPEC(m)*nd.pdot);
168 for (i = 0; i < 3; i++)
169 colval(nd.scolor,i) += (1.0-colval(nd.scolor,i))*dtmp;
170 nd.rspec += (1.0-nd.rspec)*dtmp;
171 }
172 /* diffuse reflection */
173 nd.rdiff = 1.0 - nd.rspec;
174 /* compute ambient */
175 if (nd.rdiff > FTINY) {
176 ambient(ctmp, r);
177 multcolor(ctmp, nd.mcolor); /* modified by material color */
178 addcolor(r->rcol, ctmp); /* add to returned color */
179 }
180 /* add direct component */
181 direct(r, dirbrdf, &nd);
182 }