11 |
|
#include "cvmesh.h" |
12 |
|
#include <ctype.h> |
13 |
|
|
14 |
– |
FVECT *vlist; /* our vertex list */ |
15 |
– |
int nvs; /* number of vertices in our list */ |
16 |
– |
FVECT *vnlist; /* vertex normal list */ |
17 |
– |
int nvns; |
18 |
– |
FLOAT (*vtlist)[2]; /* map vertex list */ |
19 |
– |
int nvts; |
20 |
– |
|
14 |
|
typedef int VNDX[3]; /* vertex index (point,map,normal) */ |
15 |
|
|
16 |
|
#define CHUNKSIZ 1024 /* vertex allocation chunk size */ |
17 |
|
|
18 |
< |
#define MAXARG 64 /* maximum # arguments in a statement */ |
18 |
> |
#define MAXARG 512 /* maximum # arguments in a statement */ |
19 |
|
|
20 |
< |
char *inpfile; /* input file name */ |
21 |
< |
int lineno; /* current line number */ |
22 |
< |
int faceno; /* current face number */ |
20 |
> |
static FVECT *vlist; /* our vertex list */ |
21 |
> |
static int nvs; /* number of vertices in our list */ |
22 |
> |
static FVECT *vnlist; /* vertex normal list */ |
23 |
> |
static int nvns; |
24 |
> |
static RREAL (*vtlist)[2]; /* map vertex list */ |
25 |
> |
static int nvts; |
26 |
|
|
27 |
+ |
static char *inpfile; /* input file name */ |
28 |
+ |
static int havemats; /* materials available? */ |
29 |
+ |
static char material[64]; /* current material name */ |
30 |
+ |
static char group[64]; /* current group name */ |
31 |
+ |
static int lineno; /* current line number */ |
32 |
+ |
static int faceno; /* current face number */ |
33 |
|
|
34 |
< |
wfreadobj(objfn) /* read in .OBJ file and convert */ |
35 |
< |
char *objfn; |
34 |
> |
static int getstmt(char *av[MAXARG], FILE *fp); |
35 |
> |
static int cvtndx(VNDX vi, char *vs); |
36 |
> |
static int putface(int ac, char **av); |
37 |
> |
static OBJECT getmod(void); |
38 |
> |
static int puttri(char *v1, char *v2, char *v3); |
39 |
> |
static void freeverts(void); |
40 |
> |
static int newv(double x, double y, double z); |
41 |
> |
static int newvn(double x, double y, double z); |
42 |
> |
static int newvt(double x, double y); |
43 |
> |
static void syntax(char *er); |
44 |
> |
|
45 |
> |
|
46 |
> |
void |
47 |
> |
wfreadobj( /* read in .OBJ file and convert */ |
48 |
> |
char *objfn |
49 |
> |
) |
50 |
|
{ |
51 |
|
FILE *fp; |
52 |
|
char *argv[MAXARG]; |
53 |
|
int argc; |
54 |
|
int nstats, nunknown; |
39 |
– |
int i; |
55 |
|
|
56 |
|
if (objfn == NULL) { |
57 |
|
inpfile = "<stdin>"; |
60 |
|
sprintf(errmsg, "cannot open \"%s\"", inpfile); |
61 |
|
error(USER, errmsg); |
62 |
|
} |
63 |
+ |
havemats = (nobjects > 0); |
64 |
|
nstats = nunknown = 0; |
65 |
+ |
material[0] = '\0'; |
66 |
+ |
group[0] = '\0'; |
67 |
|
lineno = 0; faceno = 0; |
68 |
|
/* scan until EOF */ |
69 |
< |
while (argc = getstmt(argv, fp)) { |
69 |
> |
while ( (argc = getstmt(argv, fp)) ) { |
70 |
|
switch (argv[0][0]) { |
71 |
|
case 'v': /* vertex */ |
72 |
|
switch (argv[0][1]) { |
114 |
|
break; |
115 |
|
} |
116 |
|
break; |
117 |
< |
case 'u': |
118 |
< |
if (strcmp(argv[0], "usemtl") && |
119 |
< |
strcmp(argv[0], "usemap")) |
117 |
> |
case 'u': /* usemtl/usemap */ |
118 |
> |
if (!strcmp(argv[0], "usemap")) |
119 |
> |
break; |
120 |
> |
if (strcmp(argv[0], "usemtl")) |
121 |
|
goto unknown; |
122 |
+ |
if (argc > 1) |
123 |
+ |
strcpy(material, argv[1]); |
124 |
+ |
else |
125 |
+ |
material[0] = '\0'; |
126 |
|
break; |
127 |
|
case 'o': /* object name */ |
128 |
|
if (argv[0][1]) |
129 |
|
goto unknown; |
130 |
|
break; |
131 |
< |
case 'g': /* group name(s) */ |
131 |
> |
case 'g': /* group name */ |
132 |
|
if (argv[0][1]) |
133 |
|
goto unknown; |
134 |
+ |
if (argc > 1) |
135 |
+ |
strcpy(group, argv[1]); |
136 |
+ |
else |
137 |
+ |
group[0] = '\0'; |
138 |
|
break; |
139 |
|
case '#': /* comment */ |
140 |
|
break; |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
< |
int |
160 |
< |
getstmt(av, fp) /* read the next statement from fp */ |
161 |
< |
register char *av[MAXARG]; |
162 |
< |
FILE *fp; |
159 |
> |
static int |
160 |
> |
getstmt( /* read the next statement from fp */ |
161 |
> |
register char *av[MAXARG], |
162 |
> |
FILE *fp |
163 |
> |
) |
164 |
|
{ |
165 |
< |
static char sbuf[MAXARG*10]; |
165 |
> |
static char sbuf[MAXARG*16]; |
166 |
|
register char *cp; |
167 |
|
register int i; |
168 |
|
|
176 |
|
lineno++; |
177 |
|
*cp++ = '\0'; |
178 |
|
} |
179 |
< |
if (!*cp || i >= MAXARG-1) |
179 |
> |
if (!*cp) |
180 |
|
break; |
181 |
+ |
if (i >= MAXARG-1) { |
182 |
+ |
sprintf(errmsg, |
183 |
+ |
"%s: too many arguments near line %d (limit %d)\n", |
184 |
+ |
inpfile, lineno+1, MAXARG-1); |
185 |
+ |
break; |
186 |
+ |
} |
187 |
|
av[i++] = cp; |
188 |
|
while (*++cp && !isspace(*cp)) |
189 |
|
; |
196 |
|
} |
197 |
|
|
198 |
|
|
199 |
< |
cvtndx(vi, vs) /* convert vertex string to index */ |
200 |
< |
register VNDX vi; |
201 |
< |
register char *vs; |
199 |
> |
static int |
200 |
> |
cvtndx( /* convert vertex string to index */ |
201 |
> |
register VNDX vi, |
202 |
> |
register char *vs |
203 |
> |
) |
204 |
|
{ |
205 |
|
/* get point */ |
206 |
|
vi[0] = atoi(vs); |
245 |
|
} |
246 |
|
|
247 |
|
|
248 |
< |
putface(ac, av) /* put out an N-sided polygon */ |
249 |
< |
int ac; |
250 |
< |
register char **av; |
248 |
> |
static int |
249 |
> |
putface( /* put out an N-sided polygon */ |
250 |
> |
int ac, |
251 |
> |
register char **av |
252 |
> |
) |
253 |
|
{ |
254 |
|
char *cp; |
255 |
|
register int i; |
267 |
|
} |
268 |
|
|
269 |
|
|
270 |
< |
puttri(v1, v2, v3) /* convert a triangle */ |
271 |
< |
char *v1, *v2, *v3; |
270 |
> |
static OBJECT |
271 |
> |
getmod(void) /* get current modifier ID */ |
272 |
|
{ |
273 |
+ |
char *mnam; |
274 |
+ |
OBJECT mod; |
275 |
+ |
|
276 |
+ |
if (!havemats) |
277 |
+ |
return(OVOID); |
278 |
+ |
if (!strcmp(material, VOIDID)) |
279 |
+ |
return(OVOID); |
280 |
+ |
if (material[0]) /* prefer usemtl statements */ |
281 |
+ |
mnam = material; |
282 |
+ |
else if (group[0]) /* else use group name */ |
283 |
+ |
mnam = group; |
284 |
+ |
else |
285 |
+ |
return(OVOID); |
286 |
+ |
mod = modifier(mnam); |
287 |
+ |
if (mod == OVOID) { |
288 |
+ |
sprintf(errmsg, "%s: undefined modifier \"%s\"", |
289 |
+ |
inpfile, mnam); |
290 |
+ |
error(USER, errmsg); |
291 |
+ |
} |
292 |
+ |
return(mod); |
293 |
+ |
} |
294 |
+ |
|
295 |
+ |
|
296 |
+ |
static int |
297 |
+ |
puttri( /* convert a triangle */ |
298 |
+ |
char *v1, |
299 |
+ |
char *v2, |
300 |
+ |
char *v3 |
301 |
+ |
) |
302 |
+ |
{ |
303 |
|
VNDX v1i, v2i, v3i; |
304 |
< |
FLOAT *v1c, *v2c, *v3c; |
305 |
< |
FLOAT *v1n, *v2n, *v3n; |
304 |
> |
RREAL *v1c, *v2c, *v3c; |
305 |
> |
RREAL *v1n, *v2n, *v3n; |
306 |
|
|
307 |
< |
if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3)) |
307 |
> |
if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3)) { |
308 |
> |
error(WARNING, "bad vertex reference"); |
309 |
|
return(0); |
310 |
< |
|
310 |
> |
} |
311 |
|
if (v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0) { |
312 |
|
v1c = vtlist[v1i[1]]; |
313 |
|
v2c = vtlist[v2i[1]]; |
322 |
|
} else |
323 |
|
v1n = v2n = v3n = NULL; |
324 |
|
|
325 |
< |
return(cvtri(vlist[v1i[0]], vlist[v2i[0]], vlist[v3i[0]], |
325 |
> |
return(cvtri(getmod(), vlist[v1i[0]], vlist[v2i[0]], vlist[v3i[0]], |
326 |
|
v1n, v2n, v3n, v1c, v2c, v3c) >= 0); |
327 |
|
} |
328 |
|
|
329 |
|
|
330 |
< |
freeverts() /* free all vertices */ |
330 |
> |
static void |
331 |
> |
freeverts(void) /* free all vertices */ |
332 |
|
{ |
333 |
|
if (nvs) { |
334 |
|
free((void *)vlist); |
345 |
|
} |
346 |
|
|
347 |
|
|
348 |
< |
int |
349 |
< |
newv(x, y, z) /* create a new vertex */ |
350 |
< |
double x, y, z; |
348 |
> |
static int |
349 |
> |
newv( /* create a new vertex */ |
350 |
> |
double x, |
351 |
> |
double y, |
352 |
> |
double z |
353 |
> |
) |
354 |
|
{ |
355 |
|
if (!(nvs%CHUNKSIZ)) { /* allocate next block */ |
356 |
|
if (nvs == 0) |
357 |
|
vlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT)); |
358 |
|
else |
359 |
< |
vlist = (FVECT *)realloc((char *)vlist, |
359 |
> |
vlist = (FVECT *)realloc((void *)vlist, |
360 |
|
(nvs+CHUNKSIZ)*sizeof(FVECT)); |
361 |
|
if (vlist == NULL) |
362 |
|
error(SYSTEM, "out of memory in newv"); |
369 |
|
} |
370 |
|
|
371 |
|
|
372 |
< |
int |
373 |
< |
newvn(x, y, z) /* create a new vertex normal */ |
374 |
< |
double x, y, z; |
372 |
> |
static int |
373 |
> |
newvn( /* create a new vertex normal */ |
374 |
> |
double x, |
375 |
> |
double y, |
376 |
> |
double z |
377 |
> |
) |
378 |
|
{ |
379 |
|
if (!(nvns%CHUNKSIZ)) { /* allocate next block */ |
380 |
|
if (nvns == 0) |
381 |
|
vnlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT)); |
382 |
|
else |
383 |
< |
vnlist = (FVECT *)realloc((char *)vnlist, |
383 |
> |
vnlist = (FVECT *)realloc((void *)vnlist, |
384 |
|
(nvns+CHUNKSIZ)*sizeof(FVECT)); |
385 |
|
if (vnlist == NULL) |
386 |
|
error(SYSTEM, "out of memory in newvn"); |
395 |
|
} |
396 |
|
|
397 |
|
|
398 |
< |
int |
399 |
< |
newvt(x, y) /* create a new texture map vertex */ |
400 |
< |
double x, y; |
398 |
> |
static int |
399 |
> |
newvt( /* create a new texture map vertex */ |
400 |
> |
double x, |
401 |
> |
double y |
402 |
> |
) |
403 |
|
{ |
404 |
|
if (!(nvts%CHUNKSIZ)) { /* allocate next block */ |
405 |
|
if (nvts == 0) |
406 |
< |
vtlist = (FLOAT (*)[2])malloc(CHUNKSIZ*2*sizeof(FLOAT)); |
406 |
> |
vtlist = (RREAL (*)[2])malloc(CHUNKSIZ*2*sizeof(RREAL)); |
407 |
|
else |
408 |
< |
vtlist = (FLOAT (*)[2])realloc((char *)vtlist, |
409 |
< |
(nvts+CHUNKSIZ)*2*sizeof(FLOAT)); |
408 |
> |
vtlist = (RREAL (*)[2])realloc((void *)vtlist, |
409 |
> |
(nvts+CHUNKSIZ)*2*sizeof(RREAL)); |
410 |
|
if (vtlist == NULL) |
411 |
|
error(SYSTEM, "out of memory in newvt"); |
412 |
|
} |
417 |
|
} |
418 |
|
|
419 |
|
|
420 |
< |
syntax(er) /* report syntax error and exit */ |
421 |
< |
char *er; |
420 |
> |
static void |
421 |
> |
syntax( /* report syntax error and exit */ |
422 |
> |
char *er |
423 |
> |
) |
424 |
|
{ |
425 |
|
sprintf(errmsg, "%s: Wavefront syntax error near line %d: %s\n", |
426 |
|
inpfile, lineno, er); |