| 1 |
greg |
1.1 |
/* Copyright (c) 1994 Regents of the University of California */ |
| 2 |
|
|
|
| 3 |
|
|
/* SCCSid "$SunId$ LBL" */ |
| 4 |
|
|
|
| 5 |
|
|
/* |
| 6 |
|
|
* Header file for MGF interpreter |
| 7 |
|
|
*/ |
| 8 |
|
|
|
| 9 |
|
|
/* must include stdio.h before us */ |
| 10 |
|
|
|
| 11 |
|
|
/* Entities (order doesn't really matter) */ |
| 12 |
|
|
#define MG_E_COMMENT 0 |
| 13 |
|
|
#define MG_E_COLOR 1 |
| 14 |
|
|
#define MG_E_CONE 2 |
| 15 |
|
|
#define MG_E_CXY 3 |
| 16 |
|
|
#define MG_E_CYL 4 |
| 17 |
|
|
#define MG_E_ED 5 |
| 18 |
|
|
#define MG_E_FACE 6 |
| 19 |
|
|
#define MG_E_INCLUDE 7 |
| 20 |
|
|
#define MG_E_IES 8 |
| 21 |
|
|
#define MG_E_MATERIAL 9 |
| 22 |
|
|
#define MG_E_NORMAL 10 |
| 23 |
|
|
#define MG_E_OBJECT 11 |
| 24 |
|
|
#define MG_E_POINT 12 |
| 25 |
|
|
#define MG_E_RD 13 |
| 26 |
|
|
#define MG_E_RING 14 |
| 27 |
|
|
#define MG_E_RS 15 |
| 28 |
|
|
#define MG_E_SPH 16 |
| 29 |
|
|
#define MG_E_TD 17 |
| 30 |
|
|
#define MG_E_TORUS 18 |
| 31 |
|
|
#define MG_E_TS 19 |
| 32 |
|
|
#define MG_E_VERTEX 20 |
| 33 |
|
|
#define MG_E_XF 21 |
| 34 |
|
|
|
| 35 |
|
|
#define MG_NENTITIES 22 |
| 36 |
|
|
|
| 37 |
|
|
#define MG_NAMELIST {"#","c","cone","cxy","cyl","ed","f","i","ies",\ |
| 38 |
|
|
"m","n","o","p","rd","ring","rs","sph","td","torus","ts","v","xf"} |
| 39 |
|
|
|
| 40 |
|
|
#define MG_MAXELEN 6 |
| 41 |
|
|
|
| 42 |
|
|
extern char mg_ename[MG_NENTITIES][MG_MAXELEN]; |
| 43 |
|
|
|
| 44 |
|
|
/* Handler routines for each entity */ |
| 45 |
|
|
|
| 46 |
|
|
#ifdef NOPROTO |
| 47 |
|
|
extern int (*mg_ehand[MG_NENTITIES])(); |
| 48 |
|
|
#else |
| 49 |
|
|
extern int (*mg_ehand[MG_NENTITIES])(int argc, char **argv); |
| 50 |
|
|
#endif |
| 51 |
|
|
|
| 52 |
|
|
/* Error codes */ |
| 53 |
|
|
#define MG_OK 0 /* normal return value */ |
| 54 |
|
|
#define MG_EUNK 1 /* unknown entity */ |
| 55 |
|
|
#define MG_EARGC 2 /* wrong number of arguments */ |
| 56 |
|
|
#define MG_ETYPE 3 /* argument type error */ |
| 57 |
|
|
#define MG_EILL 4 /* illegal argument value */ |
| 58 |
|
|
#define MG_EUNDEF 5 /* undefined reference */ |
| 59 |
|
|
#define MG_ENOFILE 6 /* cannot open input file */ |
| 60 |
|
|
#define MG_EINCL 7 /* error in included file */ |
| 61 |
|
|
#define MG_EMEM 8 /* out of memory */ |
| 62 |
|
|
#define MG_ESEEK 9 /* file seek error */ |
| 63 |
|
|
|
| 64 |
|
|
#define MG_NERRS 10 |
| 65 |
|
|
|
| 66 |
|
|
extern char *mg_err[MG_NERRS]; |
| 67 |
|
|
|
| 68 |
|
|
/* |
| 69 |
|
|
* The general process for running the parser is to fill in the mg_ehand |
| 70 |
|
|
* array with handlers for each entity you know how to handle. |
| 71 |
|
|
* Then, call mg_init to fill in the rest. This function will report |
| 72 |
|
|
* an error and quit if you try to support an inconsistent set of entities. |
| 73 |
|
|
* For each file you want to parse, call mg_load with the file name. |
| 74 |
|
|
* To read from standard input, use NULL as the file name. |
| 75 |
|
|
* For additional control over error reporting and file management, |
| 76 |
|
|
* use mg_open, mg_read, mg_parse and mg_close instead of mg_load. |
| 77 |
|
|
* To free any data structures and clear the parser, use mg_clear. |
| 78 |
|
|
* If there is an error, mg_load, mg_open, mg_parse, and mg_rewind |
| 79 |
|
|
* will return an error from the list above. In addition, mg_load |
| 80 |
|
|
* will report the error to stderr. The mg_read routine returns 0 |
| 81 |
|
|
* when the end of file has been reached. |
| 82 |
|
|
*/ |
| 83 |
|
|
|
| 84 |
|
|
#define MG_MAXLINE 512 /* maximum input line length */ |
| 85 |
|
|
#define MG_MAXARGC (MG_MAXLINE/4) /* maximum argument count */ |
| 86 |
|
|
|
| 87 |
|
|
typedef struct mg_fctxt { |
| 88 |
|
|
char *fname; /* file name */ |
| 89 |
|
|
FILE *fp; /* stream pointer */ |
| 90 |
|
|
char inpline[MG_MAXLINE]; /* input line */ |
| 91 |
|
|
int lineno; /* line number */ |
| 92 |
|
|
struct mg_fctxt *prev; /* previous context */ |
| 93 |
|
|
} MG_FCTXT; |
| 94 |
|
|
|
| 95 |
|
|
extern MG_FCTXT *mg_file; /* current file context */ |
| 96 |
|
|
|
| 97 |
|
|
#ifdef NOPROTO |
| 98 |
|
|
extern void mg_init(); /* fill in mg_ehand array */ |
| 99 |
|
|
extern int mg_load(); /* parse a file */ |
| 100 |
|
|
extern int mg_open(); /* open new input file */ |
| 101 |
|
|
extern int mg_read(); /* read next line */ |
| 102 |
|
|
extern int mg_parse(); /* parse current line */ |
| 103 |
|
|
extern int mg_rewind(); /* rewind input file */ |
| 104 |
|
|
extern void mg_close(); /* close input file */ |
| 105 |
|
|
extern void mg_clear(); /* clear parser */ |
| 106 |
|
|
extern int mg_iterate(); |
| 107 |
|
|
#else |
| 108 |
|
|
extern void mg_init(void); /* fill in mg_ehand array */ |
| 109 |
|
|
extern int mg_load(char *); /* parse a file */ |
| 110 |
|
|
extern int mg_open(MG_FCTXT *, char *); /* open new input file */ |
| 111 |
|
|
extern int mg_read(void); /* read next line */ |
| 112 |
|
|
extern int mg_parse(void); /* parse current line */ |
| 113 |
|
|
extern int mg_rewind(void); /* rewind input file */ |
| 114 |
|
|
extern void mg_close(void); /* close input file */ |
| 115 |
|
|
extern void mg_clear(void); /* clear parser */ |
| 116 |
|
|
extern int mg_iterate(int, char **, int (*)(void)); |
| 117 |
|
|
#endif |
| 118 |
|
|
|
| 119 |
|
|
#ifndef MG_NQCD |
| 120 |
|
|
#define MG_NQCD 5 /* default number of divisions */ |
| 121 |
|
|
#endif |
| 122 |
|
|
|
| 123 |
|
|
extern int mg_nqcdivs; /* divisions per quarter circle */ |
| 124 |
|
|
|
| 125 |
|
|
/* |
| 126 |
|
|
* The following library routines are included for your convenience: |
| 127 |
|
|
*/ |
| 128 |
|
|
|
| 129 |
|
|
#ifdef NOPROTO |
| 130 |
|
|
extern int mg_entity(); /* get entity number from its name */ |
| 131 |
|
|
extern int isint(); /* non-zero if integer format */ |
| 132 |
|
|
extern int isflt(); /* non-zero if floating point format */ |
| 133 |
|
|
#else |
| 134 |
|
|
extern int mg_entity(char *); /* get entity number from its name */ |
| 135 |
|
|
extern int isint(char *); /* non-zero if integer format */ |
| 136 |
|
|
extern int isflt(char *); /* non-zero if floating point format */ |
| 137 |
|
|
#endif |
| 138 |
|
|
|
| 139 |
|
|
/************************************************************************ |
| 140 |
|
|
* Definitions for 3-d vector manipulation functions |
| 141 |
|
|
*/ |
| 142 |
|
|
|
| 143 |
|
|
typedef double FVECT[3]; |
| 144 |
|
|
|
| 145 |
|
|
#ifdef NOPROTO |
| 146 |
|
|
extern double normalize(); /* normalize a vector */ |
| 147 |
|
|
#else |
| 148 |
|
|
extern double normalize(FVECT); /* normalize a vector */ |
| 149 |
|
|
#endif |
| 150 |
|
|
|
| 151 |
|
|
/************************************************************************ |
| 152 |
|
|
* Definitions for context handling routines |
| 153 |
|
|
* (materials, colors, vectors) |
| 154 |
|
|
*/ |
| 155 |
|
|
|
| 156 |
|
|
typedef struct { |
| 157 |
|
|
double cx, cy; /* XY chromaticity coordinates */ |
| 158 |
|
|
} C_COLOR; /* color context */ |
| 159 |
|
|
|
| 160 |
|
|
typedef struct { |
| 161 |
|
|
double rd; /* diffuse reflectance */ |
| 162 |
|
|
C_COLOR rd_c; /* diffuse reflectance color */ |
| 163 |
|
|
double td; /* diffuse transmittance */ |
| 164 |
|
|
C_COLOR td_c; /* diffuse transmittance color */ |
| 165 |
|
|
double ed; /* diffuse emittance */ |
| 166 |
|
|
C_COLOR ed_c; /* diffuse emittance color */ |
| 167 |
|
|
double rs; /* specular reflectance */ |
| 168 |
|
|
C_COLOR rs_c; /* specular reflectance color */ |
| 169 |
|
|
double rs_a; /* specular reflectance roughness */ |
| 170 |
|
|
double ts; /* specular transmittance */ |
| 171 |
|
|
C_COLOR ts_c; /* specular transmittance color */ |
| 172 |
|
|
double ts_a; /* specular transmittance roughness */ |
| 173 |
|
|
} C_MATERIAL; /* material context */ |
| 174 |
|
|
|
| 175 |
|
|
typedef struct { |
| 176 |
|
|
FVECT p, n; /* point and normal */ |
| 177 |
|
|
} C_VERTEX; /* vertex context */ |
| 178 |
|
|
|
| 179 |
|
|
#define C_DEFCOLOR {.333,.333} |
| 180 |
|
|
#define C_DEFMATERIAL {0.,C_DEFCOLOR,0.,C_DEFCOLOR,0.,C_DEFCOLOR,\ |
| 181 |
|
|
0.,C_DEFCOLOR,0.,0.,C_DEFCOLOR,0.} |
| 182 |
|
|
#define C_DEFVERTEX {{0.,0.,0.},{0.,0.,0.}} |
| 183 |
|
|
|
| 184 |
|
|
extern C_COLOR *c_ccolor; /* the current color */ |
| 185 |
|
|
extern C_MATERIAL *c_cmaterial; /* the current material */ |
| 186 |
|
|
extern C_VERTEX *c_cvertex; /* the current vertex */ |
| 187 |
|
|
|
| 188 |
|
|
#ifdef NOPROTO |
| 189 |
|
|
extern int c_hcolor(); /* handle color entity */ |
| 190 |
|
|
extern int c_hmaterial(); /* handle material entity */ |
| 191 |
|
|
extern int c_hvertex(); /* handle vertex entity */ |
| 192 |
|
|
extern void c_clearall(); /* clear context tables */ |
| 193 |
|
|
extern C_VERTEX *c_getvert(); /* get a named vertex */ |
| 194 |
|
|
#else |
| 195 |
|
|
extern int c_hcolor(int, char **); /* handle color entity */ |
| 196 |
|
|
extern int c_hmaterial(int, char **); /* handle material entity */ |
| 197 |
|
|
extern int c_hvertex(int, char **); /* handle vertex entity */ |
| 198 |
|
|
extern void c_clearall(void); /* clear context tables */ |
| 199 |
|
|
extern C_VERTEX *c_getvert(char *); /* get a named vertex */ |
| 200 |
|
|
#endif |
| 201 |
|
|
|
| 202 |
|
|
/************************************************************************* |
| 203 |
|
|
* Definitions for hierarchical object name handler |
| 204 |
|
|
*/ |
| 205 |
|
|
|
| 206 |
|
|
extern int obj_nnames; /* depth of name hierarchy */ |
| 207 |
|
|
extern char **obj_name; /* names in hierarchy */ |
| 208 |
|
|
|
| 209 |
|
|
#ifdef NOPROTO |
| 210 |
|
|
extern int obj_handler(); /* handle an object entity */ |
| 211 |
|
|
#else |
| 212 |
|
|
extern int obj_handler(int, char **); /* handle an object entity */ |
| 213 |
|
|
#endif |
| 214 |
|
|
|
| 215 |
|
|
/************************************************************************** |
| 216 |
|
|
* Definitions for hierarchical transformation handler |
| 217 |
|
|
*/ |
| 218 |
|
|
|
| 219 |
|
|
typedef double MAT4[4][4]; |
| 220 |
|
|
|
| 221 |
|
|
#ifdef BSD |
| 222 |
|
|
#define copymat4(m4a,m4b) bcopy((char *)m4b,(char *)m4a,sizeof(MAT4)) |
| 223 |
|
|
#else |
| 224 |
|
|
#define copymat4(m4a,m4b) (void)memcpy((char *)m4a,(char *)m4b,sizeof(MAT4)) |
| 225 |
|
|
extern char *memcpy(); |
| 226 |
|
|
#endif |
| 227 |
|
|
|
| 228 |
|
|
#define MAT4IDENT { {1.,0.,0.,0.}, {0.,1.,0.,0.}, \ |
| 229 |
|
|
{0.,0.,1.,0.}, {0.,0.,0.,1.} } |
| 230 |
|
|
|
| 231 |
|
|
extern MAT4 m4ident; |
| 232 |
|
|
|
| 233 |
|
|
#define setident4(m4) copymat4(m4, m4ident) |
| 234 |
|
|
|
| 235 |
|
|
/* regular transformation */ |
| 236 |
|
|
typedef struct { |
| 237 |
|
|
MAT4 xfm; /* transform matrix */ |
| 238 |
|
|
double sca; /* scalefactor */ |
| 239 |
|
|
} XF; |
| 240 |
|
|
|
| 241 |
|
|
#define identxf(xp) (void)(setident4((xp)->xfm),(xp)->sca=1.0) |
| 242 |
|
|
|
| 243 |
|
|
typedef struct xf_spec { |
| 244 |
|
|
short xac; /* transform argument count */ |
| 245 |
|
|
short xav0; /* zeroeth argument in xf_argv array */ |
| 246 |
|
|
XF xf; /* cumulative transformation */ |
| 247 |
|
|
struct xf_spec *prev; /* previous transformation context */ |
| 248 |
|
|
} XF_SPEC; |
| 249 |
|
|
|
| 250 |
|
|
extern int xf_argc; /* total # transform args. */ |
| 251 |
|
|
extern char **xf_argv; /* transform arguments */ |
| 252 |
|
|
extern XF_SPEC *xf_context; /* current context */ |
| 253 |
|
|
|
| 254 |
|
|
/* |
| 255 |
|
|
* The transformation handler should do most of the work that needs |
| 256 |
|
|
* doing. Just pass it any xf entities, then use the associated |
| 257 |
|
|
* functions to transform and translate points, transform vectors |
| 258 |
|
|
* (without translation), rotate vectors (without scaling) and scale |
| 259 |
|
|
* values appropriately. |
| 260 |
|
|
* |
| 261 |
|
|
* The routines xf_xfmpoint, xf_xfmvect and xf_rotvect take two |
| 262 |
|
|
* 3-D vectors (which may be identical), transforms the second and |
| 263 |
|
|
* puts the result into the first. |
| 264 |
|
|
*/ |
| 265 |
|
|
|
| 266 |
|
|
#ifdef NOPROTO |
| 267 |
|
|
|
| 268 |
|
|
extern int xf_handler(); /* handle xf entity */ |
| 269 |
|
|
extern void xf_xfmpoint(); /* transform point */ |
| 270 |
|
|
extern void xf_xfmvect(); /* transform vector */ |
| 271 |
|
|
extern void xf_rotvect(); /* rotate vector */ |
| 272 |
|
|
extern double xf_scale(); /* scale a value */ |
| 273 |
|
|
|
| 274 |
|
|
/* The following are support routines you probably won't call directly */ |
| 275 |
|
|
|
| 276 |
|
|
extern void multmat4(); /* m4a = m4b X m4c */ |
| 277 |
|
|
extern void multv3(); /* v3a = v3b X m4 (vectors) */ |
| 278 |
|
|
extern void multp3(); /* p3a = p3b X m4 (points) */ |
| 279 |
|
|
extern int xf(); /* interpret transform spec. */ |
| 280 |
|
|
|
| 281 |
|
|
#else |
| 282 |
|
|
|
| 283 |
|
|
extern int xf_handler(); /* handle xf entity */ |
| 284 |
|
|
extern void xf_xfmpoint(); /* transform point */ |
| 285 |
|
|
extern void xf_xfmvect(); /* transform vector */ |
| 286 |
|
|
extern void xf_rotvect(); /* rotate vector */ |
| 287 |
|
|
extern double xf_scale(); /* scale a value */ |
| 288 |
|
|
|
| 289 |
|
|
/* The following are support routines you probably won't call directly */ |
| 290 |
|
|
|
| 291 |
|
|
extern void multmat4(MAT4, MAT4, MAT4); /* m4a = m4b X m4c */ |
| 292 |
|
|
extern void multv3(FVECT, FVECT, MAT4); /* v3a = v3b X m4 (vectors) */ |
| 293 |
|
|
extern void multp3(FVECT, FVECT, MAT4); /* p3a = p3b X m4 (points) */ |
| 294 |
|
|
extern int xf(XF, int, char **); /* interpret transform spec. */ |
| 295 |
|
|
|
| 296 |
|
|
#endif |
| 297 |
|
|
|
| 298 |
|
|
/************************************************************************ |
| 299 |
|
|
* Miscellaneous definitions |
| 300 |
|
|
*/ |
| 301 |
|
|
|
| 302 |
|
|
#ifdef M_PI |
| 303 |
|
|
#define PI M_PI |
| 304 |
|
|
#else |
| 305 |
|
|
#define PI 3.14159265358979323846 |
| 306 |
|
|
#endif |
| 307 |
|
|
|
| 308 |
|
|
#ifdef DCL_ATOF |
| 309 |
|
|
extern double atof(); |
| 310 |
|
|
#endif |
| 311 |
|
|
|
| 312 |
|
|
#ifndef MEM_PTR |
| 313 |
|
|
#define MEM_PTR void * |
| 314 |
|
|
#endif |
| 315 |
|
|
|
| 316 |
|
|
extern MEM_PTR malloc(); |
| 317 |
|
|
extern MEM_PTR calloc(); |
| 318 |
|
|
extern MEM_PTR realloc(); |