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