10 |
|
* Currently, we support only polygonal geometry. Non-planar |
11 |
|
* faces are broken rather haphazardly into triangles. |
12 |
|
* Also, texture map indices only work for triangles, though |
13 |
< |
* I'm not sure they work correctly. |
13 |
> |
* I'm not sure they work correctly. (Taken out -- see TEXMAPS defines.) |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "standard.h" |
37 |
|
FLOAT (*vtlist)[2]; /* map vertex list */ |
38 |
|
int nvts; |
39 |
|
|
40 |
< |
typedef FLOAT BARYCCM[3][4]; /* barycentric coordinate system */ |
40 |
> |
typedef struct { |
41 |
> |
int ax; /* major axis */ |
42 |
> |
FLOAT tm[2][3]; /* transformation */ |
43 |
> |
} BARYCCM; |
44 |
|
|
45 |
|
typedef int VNDX[3]; /* vertex index (point,map,normal) */ |
46 |
|
|
72 |
|
|
73 |
|
char *defmat = DEFMAT; /* default (starting) material name */ |
74 |
|
char *defobj = DEFOBJ; /* default (starting) object name */ |
72 |
– |
int donames = 0; /* only get qualifier names */ |
75 |
|
|
76 |
+ |
int flatten = 0; /* discard surface normal information */ |
77 |
+ |
|
78 |
|
char *getmtl(), *getonm(); |
79 |
|
|
80 |
|
char mapname[128]; /* current picture file */ |
81 |
|
char matname[64]; /* current material name */ |
82 |
|
char group[16][32]; /* current group names */ |
83 |
|
char objname[128]; /* current object name */ |
84 |
+ |
char *inpfile; /* input file name */ |
85 |
|
int lineno; /* current line number */ |
86 |
< |
int faceno; /* number of faces read */ |
86 |
> |
int faceno; /* current face number */ |
87 |
|
|
88 |
|
|
89 |
|
main(argc, argv) /* read in .obj file and convert */ |
90 |
|
int argc; |
91 |
|
char *argv[]; |
92 |
|
{ |
93 |
< |
char *fname; |
93 |
> |
int donames = 0; |
94 |
|
int i; |
95 |
|
|
96 |
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
104 |
|
case 'm': /* use custom mapfile */ |
105 |
|
ourmapping = getmapping(argv[++i], &qlist); |
106 |
|
break; |
107 |
+ |
case 'f': /* flatten surfaces */ |
108 |
+ |
flatten++; |
109 |
+ |
break; |
110 |
|
default: |
111 |
|
goto userr; |
112 |
|
} |
113 |
|
if (i > argc | i < argc-1) |
114 |
|
goto userr; |
115 |
|
if (i == argc) |
116 |
< |
fname = "<stdin>"; |
117 |
< |
else if (freopen(fname=argv[i], "r", stdin) == NULL) { |
118 |
< |
fprintf(stderr, "%s: cannot open\n", fname); |
116 |
> |
inpfile = "<stdin>"; |
117 |
> |
else if (freopen(inpfile=argv[i], "r", stdin) == NULL) { |
118 |
> |
fprintf(stderr, "%s: cannot open\n", inpfile); |
119 |
|
exit(1); |
120 |
|
} |
121 |
|
if (donames) { /* scan for ids */ |
122 |
|
getnames(stdin); |
123 |
< |
printf("filename \"%s\"\n", fname); |
123 |
> |
printf("filename \"%s\"\n", inpfile); |
124 |
|
printf("filetype \"Wavefront\"\n"); |
125 |
|
write_quals(&qlist, qual, stdout); |
126 |
|
printf("qualifier %s begin\n", qlist.qual[Q_FAC]); |
129 |
|
} else { /* translate file */ |
130 |
|
printf("# "); |
131 |
|
printargs(argc, argv, stdout); |
132 |
< |
convert(fname, stdin); |
132 |
> |
convert(stdin); |
133 |
|
} |
134 |
|
exit(0); |
135 |
|
userr: |
136 |
< |
fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n] [file.obj]\n", |
136 |
> |
fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n][-f] [file.obj]\n", |
137 |
|
argv[0]); |
138 |
|
exit(1); |
139 |
|
} |
188 |
|
} |
189 |
|
|
190 |
|
|
191 |
< |
convert(fname, fp) /* convert a T-mesh */ |
184 |
< |
char *fname; |
191 |
> |
convert(fp) /* convert a T-mesh */ |
192 |
|
FILE *fp; |
193 |
|
{ |
194 |
|
char *argv[MAXARG]; |
195 |
|
int argc; |
196 |
|
int nstats, nunknown; |
197 |
|
register int i; |
198 |
< |
/* start fresh */ |
192 |
< |
freeverts(); |
193 |
< |
mapname[0] = '\0'; |
194 |
< |
strcpy(matname, defmat); |
195 |
< |
strcpy(objname, defobj); |
196 |
< |
lineno = 0; |
198 |
> |
|
199 |
|
nstats = nunknown = 0; |
200 |
|
/* scan until EOF */ |
201 |
|
while (argc = getstmt(argv, fp)) { |
204 |
|
switch (argv[0][1]) { |
205 |
|
case '\0': /* point */ |
206 |
|
if (badarg(argc-1,argv+1,"fff")) |
207 |
< |
syntax(fname, lineno, "Bad vertex"); |
207 |
> |
syntax("Bad vertex"); |
208 |
|
newv(atof(argv[1]), atof(argv[2]), |
209 |
|
atof(argv[3])); |
210 |
|
break; |
212 |
|
if (argv[0][2]) |
213 |
|
goto unknown; |
214 |
|
if (badarg(argc-1,argv+1,"fff")) |
215 |
< |
syntax(fname, lineno, "Bad normal"); |
215 |
> |
syntax("Bad normal"); |
216 |
|
if (!newvn(atof(argv[1]), atof(argv[2]), |
217 |
|
atof(argv[3]))) |
218 |
< |
syntax(fname, lineno, "Zero normal"); |
218 |
> |
syntax("Zero normal"); |
219 |
|
break; |
220 |
|
case 't': /* texture map */ |
221 |
|
if (argv[0][2]) |
234 |
|
faceno++; |
235 |
|
switch (argc-1) { |
236 |
|
case 0: case 1: case 2: |
237 |
< |
syntax(fname, lineno, "Too few vertices"); |
237 |
> |
syntax("Too few vertices"); |
238 |
|
break; |
239 |
|
case 3: |
240 |
|
if (!puttri(argv[1], argv[2], argv[3])) |
241 |
< |
syntax(fname, lineno, "Bad triangle"); |
241 |
> |
syntax("Bad triangle"); |
242 |
|
break; |
243 |
|
case 4: |
244 |
|
if (!putquad(argv[1], argv[2], |
245 |
|
argv[3], argv[4])) |
246 |
< |
syntax(fname, lineno, "Bad quad"); |
246 |
> |
syntax("Bad quad"); |
247 |
|
break; |
248 |
|
default: |
249 |
|
if (!putface(argc-1, argv+1)) |
250 |
< |
syntax(fname, lineno, "Bad face"); |
250 |
> |
syntax("Bad face"); |
251 |
|
break; |
252 |
|
} |
253 |
|
break; |
262 |
|
if (!strcmp(argv[1], "off")) |
263 |
|
mapname[0] = '\0'; |
264 |
|
else |
265 |
< |
strcpy(mapname, argv[1]); |
265 |
> |
sprintf(mapname, "%s.pic", argv[1]); |
266 |
|
} else |
267 |
|
goto unknown; |
268 |
|
break; |
281 |
|
group[i-1][0] = '\0'; |
282 |
|
break; |
283 |
|
case '#': /* comment */ |
284 |
+ |
printargs(argc, argv, stdout); |
285 |
|
break; |
286 |
|
default:; /* something we don't deal with */ |
287 |
|
unknown: |
290 |
|
} |
291 |
|
nstats++; |
292 |
|
} |
293 |
< |
printf("\n# Done processing file: %s\n", fname); |
293 |
> |
printf("\n# Done processing file: %s\n", inpfile); |
294 |
|
printf("# %d lines, %d statements, %d unrecognized\n", |
295 |
|
lineno, nstats, nunknown); |
296 |
|
} |
335 |
|
{ |
336 |
|
register RULEHD *rp = ourmapping; |
337 |
|
|
338 |
< |
if (rp == NULL) /* no rule set */ |
339 |
< |
return(matname); |
338 |
> |
if (rp == NULL) { /* no rule set */ |
339 |
> |
if (matname[0]) |
340 |
> |
return(matname); |
341 |
> |
if (group[0][0]) |
342 |
> |
return(group[0]); |
343 |
> |
return(defmat); |
344 |
> |
} |
345 |
|
/* check for match */ |
346 |
|
do { |
347 |
|
if (matchrule(rp)) { |
362 |
|
static char name[64]; |
363 |
|
register char *cp1, *cp2; |
364 |
|
register int i; |
365 |
< |
|
366 |
< |
if (!group[0][0] || strcmp(objname, DEFOBJ)) |
367 |
< |
return(objname); /* good enough for us */ |
368 |
< |
|
365 |
> |
/* check for preset */ |
366 |
> |
if (objname[0]) |
367 |
> |
return(objname); |
368 |
> |
if (!group[0][0]) |
369 |
> |
return(defobj); |
370 |
|
cp1 = name; /* else make name out of groups */ |
371 |
|
for (i = 0; group[i][0]; i++) { |
372 |
|
cp2 = group[i]; |
390 |
|
register int i; |
391 |
|
|
392 |
|
if (rp->qflg & FL(Q_MTL)) { |
393 |
+ |
if (!matname[0]) |
394 |
+ |
return(0); |
395 |
|
tmpid.number = 0; |
396 |
|
tmpid.name = matname; |
397 |
|
if (!matchid(&tmpid, &idm(rp)[Q_MTL])) |
398 |
|
return(0); |
399 |
|
} |
400 |
|
if (rp->qflg & FL(Q_MAP)) { |
401 |
+ |
if (!mapname[0]) |
402 |
+ |
return(0); |
403 |
|
tmpid.number = 0; |
404 |
|
tmpid.name = mapname; |
405 |
|
if (!matchid(&tmpid, &idm(rp)[Q_MAP])) |
416 |
|
return(0); |
417 |
|
} |
418 |
|
if (rp->qflg & FL(Q_OBJ)) { |
419 |
+ |
if (!objname[0]) |
420 |
+ |
return(0); |
421 |
|
tmpid.number = 0; |
422 |
|
tmpid.name = objname; |
423 |
|
if (!matchid(&tmpid, &idm(rp)[Q_OBJ])) |
443 |
|
if (vi[0]-- > nvs) |
444 |
|
return(0); |
445 |
|
} else if (vi[0] < 0) { |
446 |
< |
vi[0] = nvs + vi[0]; |
446 |
> |
vi[0] += nvs; |
447 |
|
if (vi[0] < 0) |
448 |
|
return(0); |
449 |
|
} else |
457 |
|
if (vi[1]-- > nvts) |
458 |
|
return(0); |
459 |
|
} else if (vi[1] < 0) { |
460 |
< |
vi[1] = nvts + vi[1]; |
460 |
> |
vi[1] += nvts; |
461 |
|
if (vi[1] < 0) |
462 |
|
return(0); |
463 |
|
} else |
471 |
|
if (vi[2]-- > nvns) |
472 |
|
return(0); |
473 |
|
} else if (vi[2] < 0) { |
474 |
< |
vi[2] = nvns + vi[2]; |
474 |
> |
vi[2] += nvns; |
475 |
|
if (vi[2] < 0) |
476 |
|
return(0); |
477 |
|
} else |
480 |
|
} |
481 |
|
|
482 |
|
|
483 |
< |
nonplanar(ac, av) /* are vertices are non-planar? */ |
483 |
> |
nonplanar(ac, av) /* are vertices non-planar? */ |
484 |
|
register int ac; |
485 |
|
register char **av; |
486 |
|
{ |
536 |
|
register char **av; |
537 |
|
{ |
538 |
|
VNDX vi; |
539 |
< |
char *mod; |
539 |
> |
char *cp; |
540 |
|
register int i; |
541 |
|
|
542 |
|
if (nonplanar(ac, av)) { /* break into quads and triangles */ |
543 |
|
while (ac > 3) { |
544 |
|
if (!putquad(av[0], av[1], av[2], av[3])) |
545 |
|
return(0); |
546 |
< |
ac -= 2; /* remove two vertices */ |
547 |
< |
for (i = 1; i < ac; i++) |
548 |
< |
av[i] = av[i+2]; |
546 |
> |
ac -= 2; /* remove two vertices & rotate */ |
547 |
> |
cp = av[0]; |
548 |
> |
for (i = 0; i < ac-1; i++) |
549 |
> |
av[i] = av[i+3]; |
550 |
> |
av[i] = cp; |
551 |
|
} |
552 |
|
if (ac == 3 && !puttri(av[0], av[1], av[2])) |
553 |
|
return(0); |
554 |
|
return(1); |
555 |
|
} |
556 |
< |
if ((mod = getmtl()) == NULL) |
556 |
> |
if ((cp = getmtl()) == NULL) |
557 |
|
return(-1); |
558 |
< |
printf("\n%s polygon %s.%d\n", mod, getonm(), faceno); |
558 |
> |
printf("\n%s polygon %s.%d\n", cp, getonm(), faceno); |
559 |
|
printf("0\n0\n%d\n", 3*ac); |
560 |
|
for (i = 0; i < ac; i++) { |
561 |
|
if (!cvtndx(vi, av[i])) |
580 |
|
if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3)) |
581 |
|
return(0); |
582 |
|
/* compute barycentric coordinates */ |
583 |
< |
texOK = (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0); |
583 |
> |
texOK = !flatten && (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0); |
584 |
> |
#ifdef TEXMAPS |
585 |
|
patOK = mapname[0] && (v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0); |
586 |
+ |
#else |
587 |
+ |
patOK = 0; |
588 |
+ |
#endif |
589 |
|
if (texOK | patOK) |
590 |
< |
if (comp_baryc(bvecs, vlist[v1i[0]], vlist[v2i[0]], |
590 |
> |
if (comp_baryc(&bvecs, vlist[v1i[0]], vlist[v2i[0]], |
591 |
|
vlist[v3i[0]]) < 0) |
592 |
|
return(-1); |
593 |
|
/* put out texture (if any) */ |
595 |
|
printf("\n%s texfunc %s\n", mod, TEXNAME); |
596 |
|
mod = TEXNAME; |
597 |
|
printf("4 dx dy dz %s\n", TCALNAME); |
598 |
< |
printf("0\n21\n"); |
599 |
< |
put_baryc(bvecs); |
598 |
> |
printf("0\n16 "); |
599 |
> |
put_baryc(&bvecs); |
600 |
|
printf("\t%14.12g %14.12g %14.12g\n", |
601 |
|
vnlist[v1i[2]][0], vnlist[v2i[2]][0], |
602 |
|
vnlist[v3i[2]][0]); |
607 |
|
vnlist[v1i[2]][2], vnlist[v2i[2]][2], |
608 |
|
vnlist[v3i[2]][2]); |
609 |
|
} |
610 |
+ |
#ifdef TEXMAPS |
611 |
|
/* put out pattern (if any) */ |
612 |
|
if (patOK) { |
613 |
|
printf("\n%s colorpict %s\n", mod, PATNAME); |
614 |
|
mod = PATNAME; |
615 |
|
printf("7 noneg noneg noneg %s %s u v\n", mapname, TCALNAME); |
616 |
< |
printf("0\n18\n"); |
617 |
< |
put_baryc(bvecs); |
616 |
> |
printf("0\n13 "); |
617 |
> |
put_baryc(&bvecs); |
618 |
|
printf("\t%f %f %f\n", vtlist[v1i[1]][0], |
619 |
|
vtlist[v2i[1]][0], vtlist[v3i[1]][0]); |
620 |
|
printf("\t%f %f %f\n", vtlist[v1i[1]][1], |
621 |
|
vtlist[v2i[1]][1], vtlist[v3i[1]][1]); |
622 |
|
} |
623 |
+ |
#endif |
624 |
|
/* put out triangle */ |
625 |
|
printf("\n%s polygon %s.%d\n", mod, getonm(), faceno); |
626 |
|
printf("0\n0\n9\n"); |
634 |
|
|
635 |
|
int |
636 |
|
comp_baryc(bcm, v1, v2, v3) /* compute barycentric vectors */ |
637 |
< |
register BARYCCM bcm; |
637 |
> |
register BARYCCM *bcm; |
638 |
|
FLOAT *v1, *v2, *v3; |
639 |
|
{ |
640 |
|
FLOAT *vt; |
641 |
|
FVECT va, vab, vcb; |
642 |
|
double d; |
643 |
+ |
int ax0, ax1; |
644 |
|
register int i, j; |
645 |
< |
|
646 |
< |
for (j = 0; j < 3; j++) { |
647 |
< |
for (i = 0; i < 3; i++) { |
648 |
< |
vab[i] = v1[i] - v2[i]; |
649 |
< |
vcb[i] = v3[i] - v2[i]; |
650 |
< |
} |
651 |
< |
d = DOT(vcb,vcb); |
645 |
> |
/* compute major axis */ |
646 |
> |
for (i = 0; i < 3; i++) { |
647 |
> |
vab[i] = v1[i] - v2[i]; |
648 |
> |
vcb[i] = v3[i] - v2[i]; |
649 |
> |
} |
650 |
> |
fcross(va, vab, vcb); |
651 |
> |
bcm->ax = ABS(va[0]) > ABS(va[1]) ? 0 : 1; |
652 |
> |
bcm->ax = ABS(va[bcm->ax]) > ABS(va[2]) ? bcm->ax : 2; |
653 |
> |
ax0 = (bcm->ax + 1) % 3; |
654 |
> |
ax1 = (bcm->ax + 2) % 3; |
655 |
> |
for (j = 0; j < 2; j++) { |
656 |
> |
vab[0] = v1[ax0] - v2[ax0]; |
657 |
> |
vcb[0] = v3[ax0] - v2[ax0]; |
658 |
> |
vab[1] = v1[ax1] - v2[ax1]; |
659 |
> |
vcb[1] = v3[ax1] - v2[ax1]; |
660 |
> |
d = vcb[0]*vcb[0] + vcb[1]*vcb[1]; |
661 |
|
if (d <= FTINY) |
662 |
|
return(-1); |
663 |
< |
d = DOT(vcb,vab)/d; |
664 |
< |
for (i = 0; i < 3; i++) |
665 |
< |
va[i] = vab[i] - vcb[i]*d; |
666 |
< |
d = DOT(va,va); |
663 |
> |
d = (vcb[0]*vab[0]+vcb[1]*vab[1])/d; |
664 |
> |
va[0] = vab[0] - vcb[0]*d; |
665 |
> |
va[1] = vab[1] - vcb[1]*d; |
666 |
> |
d = va[0]*va[0] + va[1]*va[1]; |
667 |
|
if (d <= FTINY) |
668 |
|
return(-1); |
669 |
< |
for (i = 0; i < 3; i++) { |
670 |
< |
va[i] /= d; |
671 |
< |
bcm[j][i] = va[i]; |
639 |
< |
} |
640 |
< |
bcm[j][3] = -DOT(v2,va); |
669 |
> |
bcm->tm[j][0] = va[0] /= d; |
670 |
> |
bcm->tm[j][1] = va[1] /= d; |
671 |
> |
bcm->tm[j][2] = -(v2[ax0]*va[0]+v2[ax1]*va[1]); |
672 |
|
/* rotate vertices */ |
673 |
|
vt = v1; |
674 |
|
v1 = v2; |
680 |
|
|
681 |
|
|
682 |
|
put_baryc(bcm) /* put barycentric coord. vectors */ |
683 |
< |
register BARYCCM bcm; |
683 |
> |
register BARYCCM *bcm; |
684 |
|
{ |
685 |
< |
register int i; |
686 |
< |
|
687 |
< |
for (i = 0; i < 3; i++) |
688 |
< |
printf("%14.8f %14.8f %14.8f %14.8f\n", |
689 |
< |
bcm[i][0], bcm[i][1], bcm[i][2], bcm[i][3]); |
685 |
> |
printf("\t%d\n", bcm->ax); |
686 |
> |
printf("%14.8f %14.8f %14.8f\n", |
687 |
> |
bcm->tm[0][0], bcm->tm[0][1], bcm->tm[0][2]); |
688 |
> |
printf("%14.8f %14.8f %14.8f\n", |
689 |
> |
bcm->tm[1][0], bcm->tm[1][1], bcm->tm[1][2]); |
690 |
|
} |
691 |
|
|
692 |
|
|
700 |
|
FVECT v1, v2, vc1, vc2; |
701 |
|
int ok1, ok2; |
702 |
|
|
703 |
+ |
#ifdef TEXMAPS |
704 |
+ |
/* also should output texture index coordinates, |
705 |
+ |
* which will require new .cal file |
706 |
+ |
*/ |
707 |
+ |
#endif |
708 |
|
if ((mod = getmtl()) == NULL) |
709 |
|
return(-1); |
710 |
|
name = getonm(); |
805 |
|
FVECT v1; |
806 |
|
register int i, j; |
807 |
|
|
808 |
< |
if (!(p0i[2]>=0 && p1i[2]>=0 && p2i[2]>=0 && p3i[2]>=0)) |
808 |
> |
#ifdef TEXMAPS |
809 |
> |
/* also check for texture indices */ |
810 |
> |
#endif |
811 |
> |
if (flatten || !(p0i[2]>=0 && p1i[2]>=0 && p2i[2]>=0 && p3i[2]>=0)) |
812 |
|
return(-1); |
813 |
|
/* find dominant axis */ |
814 |
|
VCOPY(v1, vnlist[p0i[2]]); |
844 |
|
eqnmat[j][1]*vnlist[p1i[2]][i] + |
845 |
|
eqnmat[j][2]*vnlist[p2i[2]][i] + |
846 |
|
eqnmat[j][3]*vnlist[p3i[2]][i]; |
847 |
+ |
#ifdef TEXMAPS |
848 |
+ |
/* compute result matrix for texture indices */ |
849 |
+ |
#endif |
850 |
|
return(ax); |
851 |
|
|
852 |
|
#undef u |
945 |
|
} |
946 |
|
|
947 |
|
|
948 |
< |
syntax(fn, ln, er) /* report syntax error and exit */ |
907 |
< |
char *fn; |
908 |
< |
int ln; |
948 |
> |
syntax(er) /* report syntax error and exit */ |
949 |
|
char *er; |
950 |
|
{ |
951 |
|
fprintf(stderr, "%s: Wavefront syntax error near line %d: %s\n", |
952 |
< |
fn, ln, er); |
952 |
> |
inpfile, lineno, er); |
953 |
|
exit(1); |
954 |
|
} |