9 |
|
* |
10 |
|
* Currently, we support only polygonal geometry, and faces |
11 |
|
* must be either quads or triangles for smoothing to work. |
12 |
< |
* Also, texture indices only work for triangles, though |
12 |
> |
* Also, texture map indices only work for triangles, though |
13 |
|
* I'm not sure they work correctly. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "standard.h" |
17 |
|
|
18 |
+ |
#include "trans.h" |
19 |
+ |
|
20 |
|
#include <ctype.h> |
21 |
|
|
20 |
– |
#define VOIDID "void" /* this is defined in object.h */ |
21 |
– |
|
22 |
|
#define TCALNAME "tmesh.cal" /* triangle interp. file */ |
23 |
|
#define QCALNAME "surf.cal" /* quad interp. file */ |
24 |
|
#define PATNAME "M-pat" /* mesh pattern name (reused) */ |
25 |
|
#define TEXNAME "M-nor" /* mesh texture name (reused) */ |
26 |
+ |
#define DEFOBJ "unnamed" /* default object name */ |
27 |
+ |
#define DEFMAT "white" /* default material name */ |
28 |
|
|
29 |
|
#define ABS(x) ((x)>=0 ? (x) : -(x)) |
30 |
|
|
34 |
|
int nvs; /* number of vertices in our list */ |
35 |
|
FVECT *vnlist; /* vertex normal list */ |
36 |
|
int nvns; |
37 |
< |
FLOAT (*vtlist)[2]; /* texture vertex list */ |
37 |
> |
FLOAT (*vtlist)[2]; /* map vertex list */ |
38 |
|
int nvts; |
39 |
|
|
40 |
|
typedef FLOAT BARYCCM[3][4]; /* barycentric coordinate system */ |
41 |
|
|
42 |
< |
typedef int VNDX[3]; /* vertex index (point,texture,normal) */ |
42 |
> |
typedef int VNDX[3]; /* vertex index (point,map,normal) */ |
43 |
|
|
44 |
< |
#define CHUNKSIZ 128 /* vertex allocation chunk size */ |
44 |
> |
#define CHUNKSIZ 256 /* vertex allocation chunk size */ |
45 |
|
|
46 |
|
#define MAXARG 64 /* maximum # arguments in a statement */ |
47 |
|
|
48 |
< |
char *defmat = VOIDID; /* default (starting) material name */ |
49 |
< |
char *defpat = ""; /* default (starting) picture name */ |
50 |
< |
char *defobj = "F"; /* default (starting) object name */ |
48 |
> |
/* qualifiers */ |
49 |
> |
#define Q_MTL 0 |
50 |
> |
#define Q_MAP 1 |
51 |
> |
#define Q_GRP 2 |
52 |
> |
#define Q_OBJ 3 |
53 |
> |
#define Q_FAC 4 |
54 |
> |
#define NQUALS 5 |
55 |
|
|
56 |
< |
char picfile[128]; /* current picture file */ |
56 |
> |
char *qname[NQUALS] = { |
57 |
> |
"Material", |
58 |
> |
"Map", |
59 |
> |
"Group", |
60 |
> |
"Object", |
61 |
> |
"Face", |
62 |
> |
}; |
63 |
> |
|
64 |
> |
QLIST qlist = {NQUALS, qname}; |
65 |
> |
/* valid qualifier ids */ |
66 |
> |
IDLIST qual[NQUALS]; |
67 |
> |
/* mapping rules */ |
68 |
> |
RULEHD *ourmapping = NULL; |
69 |
> |
|
70 |
> |
char *defmat = DEFMAT; /* default (starting) material name */ |
71 |
> |
char *defobj = DEFOBJ; /* default (starting) object name */ |
72 |
> |
int donames = 0; /* only get qualifier names */ |
73 |
> |
|
74 |
> |
char *getmtl(), *getonm(); |
75 |
> |
|
76 |
> |
char mapname[128]; /* current picture file */ |
77 |
|
char matname[64]; /* current material name */ |
78 |
+ |
char group[16][32]; /* current group names */ |
79 |
|
char objname[128]; /* current object name */ |
80 |
|
int lineno; /* current line number */ |
81 |
+ |
int faceno; /* number of faces read */ |
82 |
|
|
55 |
– |
int nfaces; /* total number of faces output */ |
83 |
|
|
84 |
< |
|
58 |
< |
main(argc, argv) /* read in T-mesh files and convert */ |
84 |
> |
main(argc, argv) /* read in .obj file and convert */ |
85 |
|
int argc; |
86 |
|
char *argv[]; |
87 |
|
{ |
88 |
< |
FILE *fp; |
88 |
> |
char *fname; |
89 |
|
int i; |
90 |
|
|
91 |
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
93 |
|
case 'o': /* object name */ |
94 |
|
defobj = argv[++i]; |
95 |
|
break; |
96 |
< |
case 'm': /* default material */ |
97 |
< |
defmat = argv[++i]; |
96 |
> |
case 'n': /* just produce name list */ |
97 |
> |
donames++; |
98 |
|
break; |
99 |
< |
case 'p': /* default picture */ |
100 |
< |
defpat = argv[++i]; |
99 |
> |
case 'm': /* use custom mapfile */ |
100 |
> |
ourmapping = getmapping(argv[++i], &qlist); |
101 |
|
break; |
102 |
|
default: |
103 |
< |
fprintf(stderr, |
78 |
< |
"Usage: %s [-o obj][-m mat][-p pic] [file ..]\n", |
79 |
< |
argv[0]); |
80 |
< |
exit(1); |
103 |
> |
goto userr; |
104 |
|
} |
105 |
< |
if (i >= argc) |
106 |
< |
convert("<stdin>", stdin); |
107 |
< |
else |
108 |
< |
for ( ; i < argc; i++) { |
109 |
< |
if ((fp = fopen(argv[i], "r")) == NULL) { |
110 |
< |
perror(argv[i]); |
111 |
< |
exit(1); |
105 |
> |
if (i > argc | i < argc-1) |
106 |
> |
goto userr; |
107 |
> |
if (i == argc) |
108 |
> |
fname = "<stdin>"; |
109 |
> |
else if (freopen(fname=argv[i], "r", stdin) == NULL) { |
110 |
> |
fprintf(stderr, "%s: cannot open\n", fname); |
111 |
> |
exit(1); |
112 |
> |
} |
113 |
> |
if (donames) { /* scan for ids */ |
114 |
> |
getnames(stdin); |
115 |
> |
printf("filename \"%s\"\n", fname); |
116 |
> |
printf("filetype \"Wavefront\"\n"); |
117 |
> |
write_quals(&qlist, qual, stdout); |
118 |
> |
printf("qualifier %s begin\n", qlist.qual[Q_FAC]); |
119 |
> |
printf("[%d:%d]\n", 1, faceno); |
120 |
> |
printf("end\n"); |
121 |
> |
} else { /* translate file */ |
122 |
> |
printf("# "); |
123 |
> |
printargs(argc, argv, stdout); |
124 |
> |
convert(fname, stdin); |
125 |
> |
} |
126 |
> |
exit(0); |
127 |
> |
userr: |
128 |
> |
fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n] [file.obj]\n", |
129 |
> |
argv[0]); |
130 |
> |
exit(1); |
131 |
> |
} |
132 |
> |
|
133 |
> |
|
134 |
> |
getnames(fp) /* get valid qualifier names */ |
135 |
> |
FILE *fp; |
136 |
> |
{ |
137 |
> |
char *argv[MAXARG]; |
138 |
> |
int argc; |
139 |
> |
ID tmpid; |
140 |
> |
register int i; |
141 |
> |
|
142 |
> |
while (argc = getstmt(argv, fp)) |
143 |
> |
switch (argv[0][0]) { |
144 |
> |
case 'f': /* face */ |
145 |
> |
if (!argv[0][1]) |
146 |
> |
faceno++; |
147 |
> |
break; |
148 |
> |
case 'u': |
149 |
> |
if (!strcmp(argv[0], "usemtl")) { /* material */ |
150 |
> |
if (argc < 2) |
151 |
> |
break; /* not fatal */ |
152 |
> |
tmpid.number = 0; |
153 |
> |
tmpid.name = argv[1]; |
154 |
> |
findid(&qual[Q_MTL], &tmpid, 1); |
155 |
> |
} else if (!strcmp(argv[0], "usemap")) {/* map */ |
156 |
> |
if (argc < 2 || !strcmp(argv[1], "off")) |
157 |
> |
break; /* not fatal */ |
158 |
> |
tmpid.number = 0; |
159 |
> |
tmpid.name = argv[1]; |
160 |
> |
findid(&qual[Q_MAP], &tmpid, 1); |
161 |
|
} |
162 |
< |
convert(argv[i], fp); |
163 |
< |
fclose(fp); |
162 |
> |
break; |
163 |
> |
case 'o': /* object name */ |
164 |
> |
if (argv[0][1] || argc < 2) |
165 |
> |
break; |
166 |
> |
tmpid.number = 0; |
167 |
> |
tmpid.name = argv[1]; |
168 |
> |
findid(&qual[Q_OBJ], &tmpid, 1); |
169 |
> |
break; |
170 |
> |
case 'g': /* group name(s) */ |
171 |
> |
if (argv[0][1]) |
172 |
> |
break; |
173 |
> |
tmpid.number = 0; |
174 |
> |
for (i = 1; i < argc; i++) { |
175 |
> |
tmpid.name = argv[i]; |
176 |
> |
findid(&qual[Q_GRP], &tmpid, 1); |
177 |
> |
} |
178 |
> |
break; |
179 |
|
} |
93 |
– |
exit(0); |
180 |
|
} |
181 |
|
|
182 |
|
|
190 |
|
register int i; |
191 |
|
/* start fresh */ |
192 |
|
freeverts(); |
193 |
< |
strcpy(picfile, defpat); |
193 |
> |
mapname[0] = '\0'; |
194 |
|
strcpy(matname, defmat); |
195 |
|
strcpy(objname, defobj); |
196 |
|
lineno = 0; |
197 |
|
nstats = nunknown = 0; |
112 |
– |
|
113 |
– |
printf("\n# Wavefront file read from: %s\n", fname); |
198 |
|
/* scan until EOF */ |
199 |
|
while (argc = getstmt(argv, fp)) { |
200 |
|
switch (argv[0][0]) { |
215 |
|
atof(argv[3]))) |
216 |
|
syntax(fname, lineno, "Zero normal"); |
217 |
|
break; |
218 |
< |
case 't': /* texture */ |
218 |
> |
case 't': /* texture map */ |
219 |
|
if (argv[0][2]) |
220 |
|
goto unknown; |
221 |
|
if (badarg(argc-1,argv+1,"ff")) |
229 |
|
case 'f': /* face */ |
230 |
|
if (argv[0][1]) |
231 |
|
goto unknown; |
232 |
+ |
faceno++; |
233 |
|
switch (argc-1) { |
234 |
|
case 0: case 1: case 2: |
235 |
|
syntax(fname, lineno, "Too few vertices"); |
258 |
|
if (argc < 2) |
259 |
|
break; /* not fatal */ |
260 |
|
if (!strcmp(argv[1], "off")) |
261 |
< |
picfile[0] = '\0'; |
261 |
> |
mapname[0] = '\0'; |
262 |
|
else |
263 |
< |
strcpy(picfile, argv[1]); |
263 |
> |
strcpy(mapname, argv[1]); |
264 |
|
} else |
265 |
|
goto unknown; |
266 |
|
break; |
274 |
|
case 'g': /* group name(s) */ |
275 |
|
if (argv[0][1]) |
276 |
|
goto unknown; |
192 |
– |
if (argc < 2) |
193 |
– |
break; /* not fatal */ |
194 |
– |
objname[0] = '\0'; |
277 |
|
for (i = 1; i < argc; i++) |
278 |
< |
if (objname[0]) |
279 |
< |
sprintf(objname+strlen(objname), |
198 |
< |
".%s", argv[i]); |
199 |
< |
else |
200 |
< |
strcpy(objname, argv[i]); |
278 |
> |
strcpy(group[i-1], argv[i]); |
279 |
> |
group[i-1][0] = '\0'; |
280 |
|
break; |
281 |
|
case '#': /* comment */ |
282 |
|
break; |
325 |
|
|
326 |
|
return(i); |
327 |
|
} |
249 |
– |
|
328 |
|
|
329 |
+ |
|
330 |
+ |
char * |
331 |
+ |
getmtl() /* figure material for this face */ |
332 |
+ |
{ |
333 |
+ |
register RULEHD *rp = ourmapping; |
334 |
+ |
|
335 |
+ |
if (rp == NULL) /* no rule set */ |
336 |
+ |
return(matname); |
337 |
+ |
/* check for match */ |
338 |
+ |
do { |
339 |
+ |
if (matchrule(rp)) { |
340 |
+ |
if (!strcmp(rp->mnam, VOIDID)) |
341 |
+ |
return(NULL); /* match is null */ |
342 |
+ |
return(rp->mnam); |
343 |
+ |
} |
344 |
+ |
rp = rp->next; |
345 |
+ |
} while (rp != NULL); |
346 |
+ |
/* no match found */ |
347 |
+ |
return(NULL); |
348 |
+ |
} |
349 |
+ |
|
350 |
+ |
|
351 |
+ |
char * |
352 |
+ |
getonm() /* invent a good name for object */ |
353 |
+ |
{ |
354 |
+ |
static char name[64]; |
355 |
+ |
register char *cp1, *cp2; |
356 |
+ |
register int i; |
357 |
+ |
|
358 |
+ |
if (!group[0][0] || strcmp(objname, DEFOBJ)) |
359 |
+ |
return(objname); /* good enough for us */ |
360 |
+ |
|
361 |
+ |
cp1 = name; /* else make name out of groups */ |
362 |
+ |
for (i = 0; group[i][0]; i++) { |
363 |
+ |
cp2 = group[i]; |
364 |
+ |
if (cp1 > name) |
365 |
+ |
*cp1++ = '.'; |
366 |
+ |
while (*cp1 = *cp2++) |
367 |
+ |
if (++cp1 >= name+sizeof(name)-2) { |
368 |
+ |
*cp1 = '\0'; |
369 |
+ |
return(name); |
370 |
+ |
} |
371 |
+ |
} |
372 |
+ |
return(name); |
373 |
+ |
} |
374 |
+ |
|
375 |
+ |
|
376 |
+ |
matchrule(rp) /* check for a match on this rule */ |
377 |
+ |
register RULEHD *rp; |
378 |
+ |
{ |
379 |
+ |
ID tmpid; |
380 |
+ |
int gotmatch; |
381 |
+ |
register int i; |
382 |
+ |
|
383 |
+ |
if (rp->qflg & FL(Q_MTL)) { |
384 |
+ |
tmpid.number = 0; |
385 |
+ |
tmpid.name = matname; |
386 |
+ |
if (!matchid(&tmpid, &idm(rp)[Q_MTL])) |
387 |
+ |
return(0); |
388 |
+ |
} |
389 |
+ |
if (rp->qflg & FL(Q_MAP)) { |
390 |
+ |
tmpid.number = 0; |
391 |
+ |
tmpid.name = mapname; |
392 |
+ |
if (!matchid(&tmpid, &idm(rp)[Q_MAP])) |
393 |
+ |
return(0); |
394 |
+ |
} |
395 |
+ |
if (rp->qflg & FL(Q_GRP)) { |
396 |
+ |
tmpid.number = 0; |
397 |
+ |
gotmatch = 0; |
398 |
+ |
for (i = 0; group[i][0]; i++) { |
399 |
+ |
tmpid.name = group[i]; |
400 |
+ |
gotmatch |= matchid(&tmpid, &idm(rp)[Q_GRP]); |
401 |
+ |
} |
402 |
+ |
if (!gotmatch) |
403 |
+ |
return(0); |
404 |
+ |
} |
405 |
+ |
if (rp->qflg & FL(Q_OBJ)) { |
406 |
+ |
tmpid.number = 0; |
407 |
+ |
tmpid.name = objname; |
408 |
+ |
if (!matchid(&tmpid, &idm(rp)[Q_OBJ])) |
409 |
+ |
return(0); |
410 |
+ |
} |
411 |
+ |
if (rp->qflg & FL(Q_FAC)) { |
412 |
+ |
tmpid.name = NULL; |
413 |
+ |
tmpid.number = faceno; |
414 |
+ |
if (!matchid(&tmpid, &idm(rp)[Q_FAC])) |
415 |
+ |
return(0); |
416 |
+ |
} |
417 |
+ |
return(1); |
418 |
+ |
} |
419 |
+ |
|
420 |
+ |
|
421 |
|
cvtndx(vi, vs) /* convert vertex string to index */ |
422 |
|
register VNDX vi; |
423 |
|
register char *vs; |
433 |
|
return(0); |
434 |
|
} else |
435 |
|
return(0); |
436 |
< |
/* get texture */ |
436 |
> |
/* get map */ |
437 |
|
while (*vs) |
438 |
|
if (*vs++ == '/') |
439 |
|
break; |
470 |
|
register char **av; |
471 |
|
{ |
472 |
|
VNDX vi; |
473 |
+ |
char *mod; |
474 |
|
|
475 |
< |
printf("\n%s polygon %s.%d\n", matname, objname, ++nfaces); |
475 |
> |
if ((mod = getmtl()) == NULL) |
476 |
> |
return(-1); |
477 |
> |
printf("\n%s polygon %s.%d\n", mod, getonm(), faceno); |
478 |
|
printf("0\n0\n%d\n", 3*ac); |
479 |
|
while (ac--) { |
480 |
|
if (!cvtndx(vi, *av++)) |
488 |
|
puttri(v1, v2, v3) /* put out a triangle */ |
489 |
|
char *v1, *v2, *v3; |
490 |
|
{ |
491 |
< |
char *mod = matname; |
491 |
> |
char *mod; |
492 |
|
VNDX v1i, v2i, v3i; |
493 |
|
BARYCCM bvecs; |
494 |
|
int texOK, patOK; |
495 |
|
|
496 |
+ |
if ((mod = getmtl()) == NULL) |
497 |
+ |
return(-1); |
498 |
+ |
|
499 |
|
if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3)) |
500 |
|
return(0); |
501 |
|
/* compute barycentric coordinates */ |
502 |
|
texOK = (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0); |
503 |
< |
patOK = picfile[0] && (v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0); |
503 |
> |
patOK = mapname[0] && (v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0); |
504 |
|
if (texOK | patOK) |
505 |
|
if (comp_baryc(bvecs, vlist[v1i[0]], vlist[v2i[0]], |
506 |
|
vlist[v3i[0]]) < 0) |
526 |
|
if (patOK) { |
527 |
|
printf("\n%s colorpict %s\n", mod, PATNAME); |
528 |
|
mod = PATNAME; |
529 |
< |
printf("7 noneg noneg noneg %s %s u v\n", picfile, TCALNAME); |
529 |
> |
printf("7 noneg noneg noneg %s %s u v\n", mapname, TCALNAME); |
530 |
|
printf("0\n18\n"); |
531 |
|
put_baryc(bvecs); |
532 |
|
printf("\t%f %f %f\n", vtlist[v1i[1]][0], |
535 |
|
vtlist[v2i[1]][1], vtlist[v3i[1]][1]); |
536 |
|
} |
537 |
|
/* put out triangle */ |
538 |
< |
printf("\n%s polygon %s.%d\n", mod, objname, ++nfaces); |
538 |
> |
printf("\n%s polygon %s.%d\n", mod, getonm(), faceno); |
539 |
|
printf("0\n0\n9\n"); |
540 |
|
pvect(vlist[v1i[0]]); |
541 |
|
pvect(vlist[v2i[0]]); |
595 |
|
} |
596 |
|
|
597 |
|
|
598 |
< |
putquad(p0, p1, p2, p3) /* put out a quadrilateral */ |
599 |
< |
char *p0, *p1, *p2, *p3; |
598 |
> |
putquad(p0, p1, p3, p2) /* put out a quadrilateral */ |
599 |
> |
char *p0, *p1, *p3, *p2; /* names correspond to binary pos. */ |
600 |
|
{ |
601 |
|
VNDX p0i, p1i, p2i, p3i; |
602 |
|
FVECT norm[4]; |
603 |
+ |
char *mod, *name; |
604 |
|
int axis; |
605 |
|
FVECT v1, v2, vc1, vc2; |
606 |
|
int ok1, ok2; |
607 |
+ |
|
608 |
+ |
if ((mod = getmtl()) == NULL) |
609 |
+ |
return(-1); |
610 |
+ |
name = getonm(); |
611 |
|
/* get actual indices */ |
612 |
|
if (!cvtndx(p0i,p0) || !cvtndx(p1i,p1) || |
613 |
|
!cvtndx(p2i,p2) || !cvtndx(p3i,p3)) |
628 |
|
|
629 |
|
/* put out quadrilateral? */ |
630 |
|
if (ok1 & ok2 && fdot(vc1,vc2) >= 1.0-FTINY*FTINY) { |
631 |
< |
printf("\n%s ", matname); |
631 |
> |
printf("\n%s ", mod); |
632 |
|
if (axis != -1) { |
633 |
|
printf("texfunc %s\n", TEXNAME); |
634 |
|
printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME); |
641 |
|
pvect(v1); |
642 |
|
printf("\n%s ", TEXNAME); |
643 |
|
} |
644 |
< |
printf("polygon %s.%d\n", objname, ++nfaces); |
644 |
> |
printf("polygon %s.%d\n", name, faceno); |
645 |
|
printf("0\n0\n12\n"); |
646 |
|
pvect(vlist[p0i[0]]); |
647 |
|
pvect(vlist[p1i[0]]); |
651 |
|
} |
652 |
|
/* put out triangles? */ |
653 |
|
if (ok1) { |
654 |
< |
printf("\n%s ", matname); |
654 |
> |
printf("\n%s ", mod); |
655 |
|
if (axis != -1) { |
656 |
|
printf("texfunc %s\n", TEXNAME); |
657 |
|
printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME); |
663 |
|
pvect(v1); |
664 |
|
printf("\n%s ", TEXNAME); |
665 |
|
} |
666 |
< |
printf("polygon %s.%d\n", objname, ++nfaces); |
666 |
> |
printf("polygon %s.%da\n", name, faceno); |
667 |
|
printf("0\n0\n9\n"); |
668 |
|
pvect(vlist[p0i[0]]); |
669 |
|
pvect(vlist[p1i[0]]); |
670 |
|
pvect(vlist[p2i[0]]); |
671 |
|
} |
672 |
|
if (ok2) { |
673 |
< |
printf("\n%s ", matname); |
673 |
> |
printf("\n%s ", mod); |
674 |
|
if (axis != -1) { |
675 |
|
printf("texfunc %s\n", TEXNAME); |
676 |
|
printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME); |
682 |
|
pvect(v2); |
683 |
|
printf("\n%s ", TEXNAME); |
684 |
|
} |
685 |
< |
printf("polygon %s.%d\n", objname, ++nfaces); |
685 |
> |
printf("polygon %s.%db\n", name, faceno); |
686 |
|
printf("0\n0\n9\n"); |
687 |
|
pvect(vlist[p2i[0]]); |
688 |
|
pvect(vlist[p1i[0]]); |
816 |
|
|
817 |
|
|
818 |
|
int |
819 |
< |
newvt(x, y) /* create a new texture vertex */ |
819 |
> |
newvt(x, y) /* create a new texture map vertex */ |
820 |
|
double x, y; |
821 |
|
{ |
822 |
|
if (!(nvts%CHUNKSIZ)) { /* allocate next block */ |
832 |
|
exit(1); |
833 |
|
} |
834 |
|
} |
835 |
< |
/* assign new texture vertex */ |
835 |
> |
/* assign new vertex */ |
836 |
|
vtlist[nvts][0] = x; |
837 |
|
vtlist[nvts][1] = y; |
838 |
|
return(++nvts); |