ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/rglmat.c
Revision: 3.3
Committed: Tue Feb 25 02:47:22 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 3.2: +1 -56 lines
Log Message:
Replaced inline copyright notice with #include "copyright.h"

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id$";
3 #endif
4 /*
5 * Routines for Radiance -> OpenGL materials.
6 */
7
8 #include "copyright.h"
9
10 #include "radogl.h"
11
12 int domats = 1; /* are we doing materials? */
13
14 LUTAB mtab = LU_SINIT(free,freemtl);
15
16
17 void
18 rgl_matclear() /* clean up materials */
19 {
20 lu_done(&mtab);
21 domats = 1;
22 }
23
24
25 MATREC *
26 getmatp(nam) /* find material record for modifier name */
27 char *nam;
28 {
29 register LUENT *lup;
30
31 if (nam == NULL)
32 return(NULL);
33 if ((lup = lu_find(&mtab, nam)) == NULL)
34 return(NULL);
35 return((MATREC *)lup->data);
36 }
37
38
39 int
40 o_default(o) /* default object is non-material modifier */
41 register OBJREC *o;
42 {
43 register LUENT *lup;
44 #ifdef DEBUG
45 if (o->otype >= 0 && !ismodifier(o->otype))
46 error(CONSISTENCY, "o_default handed non-modifier");
47 #endif
48 /* find name in lookup table */
49 if ((lup = lu_find(&mtab, o->oname)) == NULL)
50 goto memerr;
51 if (lup->key == NULL) { /* new entry? */
52 lup->key = (char *)malloc(strlen(o->oname)+1);
53 if (lup->key == NULL)
54 goto memerr;
55 strcpy(lup->key, o->oname);
56 } else if (lup->data != NULL)
57 freemtl((MATREC *)lup->data);
58 if ((lup->data = o->os) != NULL) /* make material reference */
59 ((MATREC *)lup->data)->nlinks++;
60 return;
61 memerr:
62 error(SYSTEM, "out of memory in o_default");
63 }
64
65
66 MATREC *
67 newmaterial(nam) /* get an entry for a new material */
68 char *nam;
69 {
70 register LUENT *lup;
71 /* look it up (assign entry) */
72 if ((lup = lu_find(&mtab, nam)) == NULL)
73 goto memerr;
74 if (lup->key == NULL) { /* new entry? */
75 lup->key = (char *)malloc(strlen(nam)+1);
76 if (lup->key == NULL)
77 goto memerr;
78 strcpy(lup->key, nam);
79 } else if (lup->data != NULL)
80 freemtl((MATREC *)lup->data);
81 lup->data = (char *)malloc(sizeof(MATREC));
82 if (lup->data == NULL)
83 goto memerr;
84 ((MATREC *)lup->data)->nlinks = 1;
85 return((MATREC *)lup->data);
86 memerr:
87 error(SYSTEM, "out of memory in newmaterial");
88 }
89
90
91 void
92 freemtl(mp) /* free a material */
93 register MATREC *mp;
94 {
95 if (!--mp->nlinks)
96 free((void *)mp);
97 }
98
99
100 int
101 m_normal(o) /* compute normal material parameters */
102 register OBJREC *o;
103 {
104 register MATREC *m;
105 /* check arguments */
106 if (o->oargs.nfargs != (o->otype == MAT_TRANS ? 7 : 5))
107 objerror(o, USER, "bad # of real arguments");
108 /* allocate/insert material */
109 m = newmaterial(o->oname);
110 /* assign parameters */
111 setcolor(m->u.m.ambdiff, o->oargs.farg[0],
112 o->oargs.farg[1], o->oargs.farg[2]);
113 if ((m->type = o->otype) == MAT_METAL)
114 copycolor(m->u.m.specular, m->u.m.ambdiff);
115 else
116 setcolor(m->u.m.specular, 1., 1., 1.);
117 scalecolor(m->u.m.specular, o->oargs.farg[3]);
118 scalecolor(m->u.m.ambdiff, 1.-o->oargs.farg[3]);
119 if (m->type == MAT_TRANS) {
120 scalecolor(m->u.m.specular, 1.-o->oargs.farg[5]);
121 scalecolor(m->u.m.ambdiff, 1.-o->oargs.farg[5]);
122 }
123 if (o->oargs.farg[4] <= FTINY)
124 m->u.m.specexp = MAXSPECEXP;
125 else
126 m->u.m.specexp = 2./(o->oargs.farg[4]*o->oargs.farg[4]);
127 if (m->u.m.specexp > MAXSPECEXP)
128 m->u.m.specexp = MAXSPECEXP;
129 }
130
131
132 int
133 m_aniso(o) /* anisotropic material */
134 register OBJREC *o;
135 {
136 register MATREC *m;
137 /* check arguments */
138 if (o->oargs.nfargs < (o->otype == MAT_TRANS2 ? 8 : 6))
139 objerror(o, USER, "bad # of real arguments");
140 /* allocate/insert material */
141 m = newmaterial(o->oname);
142 /* assign parameters */
143 setcolor(m->u.m.ambdiff, o->oargs.farg[0],
144 o->oargs.farg[1], o->oargs.farg[2]);
145 if ((m->type = o->otype) == MAT_METAL2)
146 copycolor(m->u.m.specular, m->u.m.ambdiff);
147 else
148 setcolor(m->u.m.specular, 1., 1., 1.);
149 scalecolor(m->u.m.specular, o->oargs.farg[3]);
150 scalecolor(m->u.m.ambdiff, 1.-o->oargs.farg[3]);
151 if (m->type == MAT_TRANS2) {
152 scalecolor(m->u.m.specular, 1.-o->oargs.farg[6]);
153 scalecolor(m->u.m.ambdiff, 1.-o->oargs.farg[6]);
154 }
155 if (o->oargs.farg[4]*o->oargs.farg[5] <= FTINY*FTINY)
156 m->u.m.specexp = MAXSPECEXP;
157 else
158 m->u.m.specexp = 2./(o->oargs.farg[4]*o->oargs.farg[5]);
159 if (m->u.m.specexp > MAXSPECEXP)
160 m->u.m.specexp = MAXSPECEXP;
161 }
162
163
164 int
165 m_glass(o) /* glass material (hopeless) */
166 OBJREC *o;
167 {
168 register MATREC *m;
169
170 m = newmaterial(o->oname);
171 m->type = o->otype;
172 setcolor(m->u.m.ambdiff, 0., 0., 0.);
173 setcolor(m->u.m.specular, .08, .08, .08);
174 m->u.m.specexp = MAXSPECEXP;
175 }
176
177
178 int
179 m_brdf(o) /* convert functional material */
180 register OBJREC *o;
181 {
182 register MATREC *m;
183 /* check arguments */
184 if (o->oargs.nfargs < (o->otype == MAT_TFUNC ? 6 : 4))
185 objerror(o, USER, "bad # of real arguments");
186 /* allocate/insert material */
187 m = newmaterial(o->oname);
188 /* assign parameters */
189 setcolor(m->u.m.ambdiff, o->oargs.farg[0],
190 o->oargs.farg[1], o->oargs.farg[2]);
191 if ((m->type = o->otype) == MAT_MFUNC)
192 copycolor(m->u.m.specular, m->u.m.ambdiff);
193 else
194 setcolor(m->u.m.specular, 1., 1., 1.);
195 scalecolor(m->u.m.specular, o->oargs.farg[3]);
196 scalecolor(m->u.m.ambdiff, 1.-o->oargs.farg[3]);
197 if (m->type == MAT_TFUNC) {
198 scalecolor(m->u.m.specular, 1.-o->oargs.farg[4]);
199 scalecolor(m->u.m.ambdiff, 1.-o->oargs.farg[4]);
200 }
201 m->u.m.specexp = UNKSPECEXP;
202 }
203
204
205 int
206 m_brdf2(o) /* convert advanced functional material */
207 register OBJREC *o;
208 {
209 register MATREC *m;
210
211 if (o->oargs.nfargs < 9)
212 objerror(o, USER, "bad # of real arguments");
213 m = newmaterial(o->oname);
214 m->type = o->otype;
215 /* assign average diffuse front+back */
216 setcolor(m->u.m.ambdiff, (o->oargs.farg[0]+o->oargs.farg[3])*.5,
217 (o->oargs.farg[1]+o->oargs.farg[4])*.5,
218 (o->oargs.farg[2]+o->oargs.farg[5])*.5);
219 /* guess the rest */
220 setcolor(m->u.m.specular, .1, .1, .1);
221 m->u.m.specexp = UNKSPECEXP;
222 }
223
224
225 int
226 m_light(o) /* convert light type */
227 register OBJREC *o;
228 {
229 FVECT v;
230 register MATREC *m;
231
232 if (o->oargs.nfargs < (o->otype == MAT_SPOT ? 7 : 3))
233 objerror(o, USER, "bad # of real arguments");
234 m = newmaterial(o->oname);
235 setcolor(m->u.l.emission, o->oargs.farg[0],
236 o->oargs.farg[1], o->oargs.farg[2]);
237 if ((m->type = o->otype) == MAT_SPOT) {
238 if ((m->u.l.spotang = o->oargs.farg[3]/2.) > 90.)
239 m->u.l.spotang = 180.;
240 v[0] = o->oargs.farg[4];
241 v[1] = o->oargs.farg[5];
242 v[2] = o->oargs.farg[6];
243 if (normalize(v) == 0.)
244 objerror(o, USER, "illegal direction");
245 VCOPY(m->u.l.spotdir, v);
246 } else {
247 m->u.l.spotang = 180.;
248 m->u.l.spotdir[0] = m->u.l.spotdir[1] = 0.;
249 m->u.l.spotdir[2] = -1.;
250 }
251 }
252
253
254 int
255 m_mirror(o) /* convert mirror type */
256 register OBJREC *o;
257 {
258 register MATREC *m;
259
260 if (o->oargs.nfargs != 3)
261 objerror(o, USER, "bad # real arguments");
262 m = newmaterial(o->oname);
263 m->type = o->otype;
264 setcolor(m->u.m.ambdiff, 0., 0., 0.);
265 setcolor(m->u.m.specular, o->oargs.farg[0],
266 o->oargs.farg[1], o->oargs.farg[2]);
267 m->u.m.specexp = MAXSPECEXP;
268 }
269
270
271 int
272 m_prism(o) /* convert prism type */
273 register OBJREC *o;
274 {
275 register MATREC *m;
276 /* can't really deal with this type */
277 m = newmaterial(o->oname);
278 m->type = o->otype;
279 setcolor(m->u.m.ambdiff, 0.2, 0.2, 0.2);
280 setcolor(m->u.m.specular, 0.1, 0.1, 0.1);
281 m->u.m.specexp = UNKSPECEXP;
282 }