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