1 |
greg |
1.1 |
/* Copyright (c) 1994 Regents of the University of California */ |
2 |
|
|
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
5 |
|
|
#endif |
6 |
|
|
|
7 |
|
|
/* |
8 |
|
|
* Context handlers |
9 |
|
|
*/ |
10 |
|
|
|
11 |
|
|
#include <stdio.h> |
12 |
greg |
1.2 |
#include <math.h> |
13 |
greg |
1.1 |
#include <string.h> |
14 |
|
|
#include "parser.h" |
15 |
|
|
#include "lookup.h" |
16 |
|
|
|
17 |
|
|
/* default context values */ |
18 |
|
|
static C_COLOR c_dfcolor = C_DEFCOLOR; |
19 |
|
|
static C_MATERIAL c_dfmaterial = C_DEFMATERIAL; |
20 |
|
|
static C_VERTEX c_dfvertex = C_DEFVERTEX; |
21 |
|
|
|
22 |
|
|
/* the unnamed contexts */ |
23 |
|
|
static C_COLOR c_uncolor = C_DEFCOLOR; |
24 |
|
|
static C_MATERIAL c_unmaterial = C_DEFMATERIAL; |
25 |
|
|
static C_VERTEX c_unvertex = C_DEFVERTEX; |
26 |
|
|
|
27 |
|
|
/* the current contexts */ |
28 |
|
|
C_COLOR *c_ccolor = &c_uncolor; |
29 |
|
|
C_MATERIAL *c_cmaterial = &c_unmaterial; |
30 |
|
|
C_VERTEX *c_cvertex = &c_unvertex; |
31 |
|
|
|
32 |
greg |
1.3 |
static LUTAB clr_tab = LU_SINIT(free,free); /* color lookup table */ |
33 |
|
|
static LUTAB mat_tab = LU_SINIT(free,free); /* material lookup table */ |
34 |
|
|
static LUTAB vtx_tab = LU_SINIT(free,free); /* vertex lookup table */ |
35 |
greg |
1.1 |
|
36 |
|
|
|
37 |
|
|
int |
38 |
|
|
c_hcolor(ac, av) /* handle color entity */ |
39 |
|
|
int ac; |
40 |
|
|
register char **av; |
41 |
|
|
{ |
42 |
|
|
register LUENT *lp; |
43 |
|
|
|
44 |
|
|
switch (mg_entity(av[0])) { |
45 |
|
|
case MG_E_COLOR: /* get/set color context */ |
46 |
greg |
1.4 |
if (ac > 4) |
47 |
|
|
return(MG_EARGC); |
48 |
greg |
1.1 |
if (ac == 1) { /* set unnamed color context */ |
49 |
|
|
c_uncolor = c_dfcolor; |
50 |
|
|
c_ccolor = &c_uncolor; |
51 |
|
|
return(MG_OK); |
52 |
|
|
} |
53 |
|
|
lp = lu_find(&clr_tab, av[1]); /* lookup context */ |
54 |
|
|
if (lp == NULL) |
55 |
|
|
return(MG_EMEM); |
56 |
|
|
if (ac == 2) { /* reestablish previous context */ |
57 |
|
|
if (lp->data == NULL) |
58 |
|
|
return(MG_EUNDEF); |
59 |
|
|
c_ccolor = (C_COLOR *)lp->data; |
60 |
|
|
return(MG_OK); |
61 |
|
|
} |
62 |
|
|
if (av[2][0] != '=' || av[2][1]) |
63 |
|
|
return(MG_ETYPE); |
64 |
|
|
if (lp->key == NULL) { /* create new color context */ |
65 |
|
|
lp->key = (char *)malloc(strlen(av[1])+1); |
66 |
|
|
if (lp->key == NULL) |
67 |
|
|
return(MG_EMEM); |
68 |
|
|
strcpy(lp->key, av[1]); |
69 |
|
|
lp->data = (char *)malloc(sizeof(C_COLOR)); |
70 |
|
|
if (lp->data == NULL) |
71 |
|
|
return(MG_EMEM); |
72 |
|
|
} |
73 |
|
|
c_ccolor = (C_COLOR *)lp->data; |
74 |
|
|
if (ac == 3) { /* use default template */ |
75 |
|
|
*c_ccolor = c_dfcolor; |
76 |
|
|
return(MG_OK); |
77 |
|
|
} |
78 |
|
|
lp = lu_find(&clr_tab, av[3]); /* lookup template */ |
79 |
|
|
if (lp == NULL) |
80 |
|
|
return(MG_EMEM); |
81 |
|
|
if (lp->data == NULL) |
82 |
|
|
return(MG_EUNDEF); |
83 |
|
|
*c_ccolor = *(C_COLOR *)lp->data; |
84 |
|
|
if (ac > 4) |
85 |
|
|
return(MG_EARGC); |
86 |
|
|
return(MG_OK); |
87 |
|
|
case MG_E_CXY: /* assign CIE XY value */ |
88 |
|
|
if (ac != 3) |
89 |
|
|
return(MG_EARGC); |
90 |
|
|
if (!isflt(av[1]) || !isflt(av[2])) |
91 |
|
|
return(MG_ETYPE); |
92 |
|
|
c_ccolor->cx = atof(av[1]); |
93 |
|
|
c_ccolor->cy = atof(av[2]); |
94 |
|
|
if (c_ccolor->cx < 0. | c_ccolor->cy < 0. | |
95 |
|
|
c_ccolor->cx + c_ccolor->cy > 1.) |
96 |
|
|
return(MG_EILL); |
97 |
|
|
return(MG_OK); |
98 |
|
|
} |
99 |
|
|
return(MG_EUNK); |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
|
103 |
|
|
int |
104 |
|
|
c_hmaterial(ac, av) /* handle material entity */ |
105 |
|
|
int ac; |
106 |
|
|
register char **av; |
107 |
|
|
{ |
108 |
|
|
register LUENT *lp; |
109 |
|
|
|
110 |
|
|
switch (mg_entity(av[0])) { |
111 |
|
|
case MG_E_MATERIAL: /* get/set material context */ |
112 |
greg |
1.4 |
if (ac > 4) |
113 |
|
|
return(MG_EARGC); |
114 |
greg |
1.1 |
if (ac == 1) { /* set unnamed material context */ |
115 |
|
|
c_unmaterial = c_dfmaterial; |
116 |
|
|
c_cmaterial = &c_unmaterial; |
117 |
|
|
return(MG_OK); |
118 |
|
|
} |
119 |
|
|
lp = lu_find(&mat_tab, av[1]); /* lookup context */ |
120 |
|
|
if (lp == NULL) |
121 |
|
|
return(MG_EMEM); |
122 |
|
|
if (ac == 2) { /* reestablish previous context */ |
123 |
|
|
if (lp->data == NULL) |
124 |
|
|
return(MG_EUNDEF); |
125 |
|
|
c_cmaterial = (C_MATERIAL *)lp->data; |
126 |
|
|
return(MG_OK); |
127 |
|
|
} |
128 |
|
|
if (av[2][0] != '=' || av[2][1]) |
129 |
|
|
return(MG_ETYPE); |
130 |
|
|
if (lp->key == NULL) { /* create new material context */ |
131 |
|
|
lp->key = (char *)malloc(strlen(av[1])+1); |
132 |
|
|
if (lp->key == NULL) |
133 |
|
|
return(MG_EMEM); |
134 |
|
|
strcpy(lp->key, av[1]); |
135 |
|
|
lp->data = (char *)malloc(sizeof(C_MATERIAL)); |
136 |
|
|
if (lp->data == NULL) |
137 |
|
|
return(MG_EMEM); |
138 |
|
|
} |
139 |
|
|
c_cmaterial = (C_MATERIAL *)lp->data; |
140 |
|
|
if (ac == 3) { /* use default template */ |
141 |
|
|
*c_cmaterial = c_dfmaterial; |
142 |
greg |
1.2 |
c_cmaterial->name = lp->key; |
143 |
greg |
1.1 |
return(MG_OK); |
144 |
|
|
} |
145 |
|
|
lp = lu_find(&mat_tab, av[3]); /* lookup template */ |
146 |
|
|
if (lp == NULL) |
147 |
|
|
return(MG_EMEM); |
148 |
|
|
if (lp->data == NULL) |
149 |
|
|
return(MG_EUNDEF); |
150 |
|
|
*c_cmaterial = *(C_MATERIAL *)lp->data; |
151 |
greg |
1.2 |
c_cmaterial->name = lp->key; |
152 |
|
|
c_cmaterial->clock = 1; |
153 |
greg |
1.1 |
if (ac > 4) |
154 |
|
|
return(MG_EARGC); |
155 |
|
|
return(MG_OK); |
156 |
|
|
case MG_E_RD: /* set diffuse reflectance */ |
157 |
|
|
if (ac != 2) |
158 |
|
|
return(MG_EARGC); |
159 |
|
|
if (!isflt(av[1])) |
160 |
|
|
return(MG_ETYPE); |
161 |
|
|
c_cmaterial->rd = atof(av[1]); |
162 |
|
|
if (c_cmaterial->rd < 0. | c_cmaterial->rd > 1.) |
163 |
|
|
return(MG_EILL); |
164 |
|
|
c_cmaterial->rd_c = *c_ccolor; |
165 |
greg |
1.2 |
c_cmaterial->clock++; |
166 |
greg |
1.1 |
return(MG_OK); |
167 |
|
|
case MG_E_ED: /* set diffuse emittance */ |
168 |
|
|
if (ac != 2) |
169 |
|
|
return(MG_EARGC); |
170 |
|
|
if (!isflt(av[1])) |
171 |
|
|
return(MG_ETYPE); |
172 |
|
|
c_cmaterial->ed = atof(av[1]); |
173 |
|
|
if (c_cmaterial->ed < 0.) |
174 |
|
|
return(MG_EILL); |
175 |
|
|
c_cmaterial->ed_c = *c_ccolor; |
176 |
greg |
1.2 |
c_cmaterial->clock++; |
177 |
greg |
1.1 |
return(MG_OK); |
178 |
|
|
case MG_E_TD: /* set diffuse transmittance */ |
179 |
|
|
if (ac != 2) |
180 |
|
|
return(MG_EARGC); |
181 |
|
|
if (!isflt(av[1])) |
182 |
|
|
return(MG_ETYPE); |
183 |
|
|
c_cmaterial->td = atof(av[1]); |
184 |
|
|
if (c_cmaterial->td < 0. | c_cmaterial->td > 1.) |
185 |
|
|
return(MG_EILL); |
186 |
|
|
c_cmaterial->td_c = *c_ccolor; |
187 |
|
|
return(MG_OK); |
188 |
|
|
case MG_E_RS: /* set specular reflectance */ |
189 |
|
|
if (ac != 3) |
190 |
|
|
return(MG_EARGC); |
191 |
|
|
if (!isflt(av[1]) || !isflt(av[2])) |
192 |
|
|
return(MG_ETYPE); |
193 |
|
|
c_cmaterial->rs = atof(av[1]); |
194 |
|
|
c_cmaterial->rs_a = atof(av[2]); |
195 |
|
|
if (c_cmaterial->rs < 0. | c_cmaterial->rs > 1. | |
196 |
|
|
c_cmaterial->rs_a < 0.) |
197 |
|
|
return(MG_EILL); |
198 |
|
|
c_cmaterial->rs_c = *c_ccolor; |
199 |
greg |
1.2 |
c_cmaterial->clock++; |
200 |
greg |
1.1 |
return(MG_OK); |
201 |
|
|
case MG_E_TS: /* set specular transmittance */ |
202 |
|
|
if (ac != 3) |
203 |
|
|
return(MG_EARGC); |
204 |
|
|
if (!isflt(av[1]) || !isflt(av[2])) |
205 |
|
|
return(MG_ETYPE); |
206 |
|
|
c_cmaterial->ts = atof(av[1]); |
207 |
|
|
c_cmaterial->ts_a = atof(av[2]); |
208 |
|
|
if (c_cmaterial->ts < 0. | c_cmaterial->ts > 1. | |
209 |
|
|
c_cmaterial->ts_a < 0.) |
210 |
|
|
return(MG_EILL); |
211 |
|
|
c_cmaterial->ts_c = *c_ccolor; |
212 |
greg |
1.2 |
c_cmaterial->clock++; |
213 |
greg |
1.1 |
return(MG_OK); |
214 |
|
|
} |
215 |
|
|
return(MG_EUNK); |
216 |
|
|
} |
217 |
|
|
|
218 |
|
|
|
219 |
|
|
int |
220 |
|
|
c_hvertex(ac, av) /* handle a vertex entity */ |
221 |
|
|
int ac; |
222 |
|
|
register char **av; |
223 |
|
|
{ |
224 |
|
|
register LUENT *lp; |
225 |
|
|
|
226 |
|
|
switch (mg_entity(av[0])) { |
227 |
|
|
case MG_E_VERTEX: /* get/set vertex context */ |
228 |
greg |
1.4 |
if (ac > 4) |
229 |
|
|
return(MG_EARGC); |
230 |
greg |
1.1 |
if (ac == 1) { /* set unnamed vertex context */ |
231 |
|
|
c_unvertex = c_dfvertex; |
232 |
|
|
c_cvertex = &c_unvertex; |
233 |
|
|
return(MG_OK); |
234 |
|
|
} |
235 |
|
|
lp = lu_find(&vtx_tab, av[1]); /* lookup context */ |
236 |
|
|
if (lp == NULL) |
237 |
|
|
return(MG_EMEM); |
238 |
|
|
if (ac == 2) { /* reestablish previous context */ |
239 |
|
|
if (lp->data == NULL) |
240 |
|
|
return(MG_EUNDEF); |
241 |
|
|
c_cvertex = (C_VERTEX *)lp->data; |
242 |
|
|
return(MG_OK); |
243 |
|
|
} |
244 |
|
|
if (av[2][0] != '=' || av[2][1]) |
245 |
|
|
return(MG_ETYPE); |
246 |
|
|
if (lp->key == NULL) { /* create new vertex context */ |
247 |
|
|
lp->key = (char *)malloc(strlen(av[1])+1); |
248 |
|
|
if (lp->key == NULL) |
249 |
|
|
return(MG_EMEM); |
250 |
|
|
strcpy(lp->key, av[1]); |
251 |
|
|
lp->data = (char *)malloc(sizeof(C_VERTEX)); |
252 |
|
|
if (lp->data == NULL) |
253 |
|
|
return(MG_EMEM); |
254 |
|
|
} |
255 |
|
|
c_cvertex = (C_VERTEX *)lp->data; |
256 |
|
|
if (ac == 3) { /* use default template */ |
257 |
|
|
*c_cvertex = c_dfvertex; |
258 |
|
|
return(MG_OK); |
259 |
|
|
} |
260 |
|
|
lp = lu_find(&vtx_tab, av[3]); /* lookup template */ |
261 |
|
|
if (lp == NULL) |
262 |
|
|
return(MG_EMEM); |
263 |
|
|
if (lp->data == NULL) |
264 |
|
|
return(MG_EUNDEF); |
265 |
|
|
*c_cvertex = *(C_VERTEX *)lp->data; |
266 |
|
|
if (ac > 4) |
267 |
|
|
return(MG_EARGC); |
268 |
|
|
return(MG_OK); |
269 |
|
|
case MG_E_POINT: /* set point */ |
270 |
|
|
if (ac != 4) |
271 |
|
|
return(MG_EARGC); |
272 |
|
|
if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3])) |
273 |
|
|
return(MG_ETYPE); |
274 |
|
|
c_cvertex->p[0] = atof(av[1]); |
275 |
|
|
c_cvertex->p[1] = atof(av[2]); |
276 |
|
|
c_cvertex->p[2] = atof(av[3]); |
277 |
|
|
return(MG_OK); |
278 |
|
|
case MG_E_NORMAL: /* set normal */ |
279 |
|
|
if (ac != 4) |
280 |
|
|
return(MG_EARGC); |
281 |
|
|
if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3])) |
282 |
|
|
return(MG_ETYPE); |
283 |
|
|
c_cvertex->n[0] = atof(av[1]); |
284 |
|
|
c_cvertex->n[1] = atof(av[2]); |
285 |
|
|
c_cvertex->n[2] = atof(av[3]); |
286 |
|
|
(void)normalize(c_cvertex->n); |
287 |
|
|
return(MG_OK); |
288 |
|
|
} |
289 |
|
|
return(MG_EUNK); |
290 |
|
|
} |
291 |
|
|
|
292 |
|
|
|
293 |
|
|
void |
294 |
|
|
c_clearall() /* empty context tables */ |
295 |
|
|
{ |
296 |
|
|
c_uncolor = c_dfcolor; |
297 |
|
|
c_ccolor = &c_uncolor; |
298 |
greg |
1.3 |
lu_done(&clr_tab); |
299 |
greg |
1.1 |
c_unmaterial = c_dfmaterial; |
300 |
|
|
c_cmaterial = &c_unmaterial; |
301 |
greg |
1.3 |
lu_done(&mat_tab); |
302 |
greg |
1.1 |
c_unvertex = c_dfvertex; |
303 |
|
|
c_cvertex = &c_unvertex; |
304 |
greg |
1.3 |
lu_done(&vtx_tab); |
305 |
greg |
1.1 |
} |
306 |
|
|
|
307 |
|
|
|
308 |
|
|
C_VERTEX * |
309 |
|
|
c_getvert(name) /* get a named vertex */ |
310 |
|
|
char *name; |
311 |
|
|
{ |
312 |
|
|
register LUENT *lp; |
313 |
|
|
|
314 |
|
|
if ((lp = lu_find(&vtx_tab, name)) == NULL) |
315 |
|
|
return(NULL); |
316 |
|
|
return((C_VERTEX *)lp->data); |
317 |
|
|
} |