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" |
20 |
|
#include <ctype.h> |
21 |
|
|
22 |
|
#define TCALNAME "tmesh.cal" /* triangle interp. file */ |
23 |
– |
#define QCALNAME "surf.cal" /* quad interp. file */ |
23 |
|
#define PATNAME "M-pat" /* mesh pattern name (reused) */ |
24 |
|
#define TEXNAME "M-nor" /* mesh texture name (reused) */ |
25 |
|
#define DEFOBJ "unnamed" /* default object name */ |
36 |
|
FLOAT (*vtlist)[2]; /* map vertex list */ |
37 |
|
int nvts; |
38 |
|
|
39 |
< |
typedef FLOAT BARYCCM[3][4]; /* barycentric coordinate system */ |
39 |
> |
typedef struct { |
40 |
> |
int ax; /* major axis */ |
41 |
> |
FLOAT tm[2][3]; /* transformation */ |
42 |
> |
} BARYCCM; |
43 |
|
|
44 |
|
typedef int VNDX[3]; /* vertex index (point,map,normal) */ |
45 |
|
|
72 |
|
char *defmat = DEFMAT; /* default (starting) material name */ |
73 |
|
char *defobj = DEFOBJ; /* default (starting) object name */ |
74 |
|
|
75 |
+ |
int flatten = 0; /* discard surface normal information */ |
76 |
+ |
|
77 |
|
char *getmtl(), *getonm(); |
78 |
|
|
79 |
|
char mapname[128]; /* current picture file */ |
103 |
|
case 'm': /* use custom mapfile */ |
104 |
|
ourmapping = getmapping(argv[++i], &qlist); |
105 |
|
break; |
106 |
+ |
case 'f': /* flatten surfaces */ |
107 |
+ |
flatten++; |
108 |
+ |
break; |
109 |
|
default: |
110 |
|
goto userr; |
111 |
|
} |
132 |
|
} |
133 |
|
exit(0); |
134 |
|
userr: |
135 |
< |
fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n] [file.obj]\n", |
135 |
> |
fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n][-f] [file.obj]\n", |
136 |
|
argv[0]); |
137 |
|
exit(1); |
138 |
|
} |
239 |
|
if (!puttri(argv[1], argv[2], argv[3])) |
240 |
|
syntax("Bad triangle"); |
241 |
|
break; |
235 |
– |
case 4: |
236 |
– |
if (!putquad(argv[1], argv[2], |
237 |
– |
argv[3], argv[4])) |
238 |
– |
syntax("Bad quad"); |
239 |
– |
break; |
242 |
|
default: |
243 |
|
if (!putface(argc-1, argv+1)) |
244 |
|
syntax("Bad face"); |
256 |
|
if (!strcmp(argv[1], "off")) |
257 |
|
mapname[0] = '\0'; |
258 |
|
else |
259 |
< |
strcpy(mapname, argv[1]); |
259 |
> |
sprintf(mapname, "%s.pic", argv[1]); |
260 |
|
} else |
261 |
|
goto unknown; |
262 |
|
break; |
275 |
|
group[i-1][0] = '\0'; |
276 |
|
break; |
277 |
|
case '#': /* comment */ |
278 |
+ |
printargs(argc, argv, stdout); |
279 |
|
break; |
280 |
|
default:; /* something we don't deal with */ |
281 |
|
unknown: |
437 |
|
if (vi[0]-- > nvs) |
438 |
|
return(0); |
439 |
|
} else if (vi[0] < 0) { |
440 |
< |
vi[0] = nvs + vi[0]; |
440 |
> |
vi[0] += nvs; |
441 |
|
if (vi[0] < 0) |
442 |
|
return(0); |
443 |
|
} else |
451 |
|
if (vi[1]-- > nvts) |
452 |
|
return(0); |
453 |
|
} else if (vi[1] < 0) { |
454 |
< |
vi[1] = nvts + vi[1]; |
454 |
> |
vi[1] += nvts; |
455 |
|
if (vi[1] < 0) |
456 |
|
return(0); |
457 |
|
} else |
465 |
|
if (vi[2]-- > nvns) |
466 |
|
return(0); |
467 |
|
} else if (vi[2] < 0) { |
468 |
< |
vi[2] = nvns + vi[2]; |
468 |
> |
vi[2] += nvns; |
469 |
|
if (vi[2] < 0) |
470 |
|
return(0); |
471 |
|
} else |
474 |
|
} |
475 |
|
|
476 |
|
|
477 |
< |
nonplanar(ac, av) /* are vertices are non-planar? */ |
477 |
> |
nonplanar(ac, av) /* are vertices non-planar? */ |
478 |
|
register int ac; |
479 |
|
register char **av; |
480 |
|
{ |
486 |
|
|
487 |
|
if (!cvtndx(vi, av[0])) |
488 |
|
return(0); |
489 |
< |
if (vi[2] >= 0) |
489 |
> |
if (!flatten && vi[2] >= 0) |
490 |
|
return(1); /* has interpolated normals */ |
491 |
|
if (ac < 4) |
492 |
|
return(0); /* it's a triangle! */ |
530 |
|
register char **av; |
531 |
|
{ |
532 |
|
VNDX vi; |
533 |
< |
char *mod; |
533 |
> |
char *cp; |
534 |
|
register int i; |
535 |
|
|
536 |
< |
if (nonplanar(ac, av)) { /* break into quads and triangles */ |
537 |
< |
while (ac > 3) { |
538 |
< |
if (!putquad(av[0], av[1], av[2], av[3])) |
536 |
> |
if (nonplanar(ac, av)) { /* break into triangles */ |
537 |
> |
while (ac > 2) { |
538 |
> |
if (!puttri(av[0], av[1], av[2])) |
539 |
|
return(0); |
540 |
< |
/* remove two vertices & rotate */ |
541 |
< |
av[ac -= 2] = av[0]; |
542 |
< |
for (i = 0; i <= ac; i++) |
540 |
> |
ac--; /* remove vertex & rotate */ |
541 |
> |
cp = av[0]; |
542 |
> |
for (i = 0; i < ac-1; i++) |
543 |
|
av[i] = av[i+2]; |
544 |
+ |
av[i] = cp; |
545 |
|
} |
542 |
– |
if (ac == 3 && !puttri(av[0], av[1], av[2])) |
543 |
– |
return(0); |
546 |
|
return(1); |
547 |
|
} |
548 |
< |
if ((mod = getmtl()) == NULL) |
548 |
> |
if ((cp = getmtl()) == NULL) |
549 |
|
return(-1); |
550 |
< |
printf("\n%s polygon %s.%d\n", mod, getonm(), faceno); |
550 |
> |
printf("\n%s polygon %s.%d\n", cp, getonm(), faceno); |
551 |
|
printf("0\n0\n%d\n", 3*ac); |
552 |
|
for (i = 0; i < ac; i++) { |
553 |
|
if (!cvtndx(vi, av[i])) |
572 |
|
if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3)) |
573 |
|
return(0); |
574 |
|
/* compute barycentric coordinates */ |
575 |
< |
texOK = (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0); |
575 |
> |
texOK = !flatten && (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0); |
576 |
> |
#ifdef TEXMAPS |
577 |
|
patOK = mapname[0] && (v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0); |
578 |
+ |
#else |
579 |
+ |
patOK = 0; |
580 |
+ |
#endif |
581 |
|
if (texOK | patOK) |
582 |
< |
if (comp_baryc(bvecs, vlist[v1i[0]], vlist[v2i[0]], |
582 |
> |
if (comp_baryc(&bvecs, vlist[v1i[0]], vlist[v2i[0]], |
583 |
|
vlist[v3i[0]]) < 0) |
584 |
|
return(-1); |
585 |
|
/* put out texture (if any) */ |
587 |
|
printf("\n%s texfunc %s\n", mod, TEXNAME); |
588 |
|
mod = TEXNAME; |
589 |
|
printf("4 dx dy dz %s\n", TCALNAME); |
590 |
< |
printf("0\n21\n"); |
591 |
< |
put_baryc(bvecs); |
590 |
> |
printf("0\n16 "); |
591 |
> |
put_baryc(&bvecs); |
592 |
|
printf("\t%14.12g %14.12g %14.12g\n", |
593 |
|
vnlist[v1i[2]][0], vnlist[v2i[2]][0], |
594 |
|
vnlist[v3i[2]][0]); |
599 |
|
vnlist[v1i[2]][2], vnlist[v2i[2]][2], |
600 |
|
vnlist[v3i[2]][2]); |
601 |
|
} |
602 |
+ |
#ifdef TEXMAPS |
603 |
|
/* put out pattern (if any) */ |
604 |
|
if (patOK) { |
605 |
|
printf("\n%s colorpict %s\n", mod, PATNAME); |
606 |
|
mod = PATNAME; |
607 |
|
printf("7 noneg noneg noneg %s %s u v\n", mapname, TCALNAME); |
608 |
< |
printf("0\n18\n"); |
609 |
< |
put_baryc(bvecs); |
608 |
> |
printf("0\n13 "); |
609 |
> |
put_baryc(&bvecs); |
610 |
|
printf("\t%f %f %f\n", vtlist[v1i[1]][0], |
611 |
|
vtlist[v2i[1]][0], vtlist[v3i[1]][0]); |
612 |
|
printf("\t%f %f %f\n", vtlist[v1i[1]][1], |
613 |
|
vtlist[v2i[1]][1], vtlist[v3i[1]][1]); |
614 |
|
} |
615 |
+ |
#endif |
616 |
|
/* put out triangle */ |
617 |
|
printf("\n%s polygon %s.%d\n", mod, getonm(), faceno); |
618 |
|
printf("0\n0\n9\n"); |
626 |
|
|
627 |
|
int |
628 |
|
comp_baryc(bcm, v1, v2, v3) /* compute barycentric vectors */ |
629 |
< |
register BARYCCM bcm; |
629 |
> |
register BARYCCM *bcm; |
630 |
|
FLOAT *v1, *v2, *v3; |
631 |
|
{ |
632 |
|
FLOAT *vt; |
633 |
|
FVECT va, vab, vcb; |
634 |
|
double d; |
635 |
+ |
int ax0, ax1; |
636 |
|
register int i, j; |
637 |
< |
|
638 |
< |
for (j = 0; j < 3; j++) { |
639 |
< |
for (i = 0; i < 3; i++) { |
640 |
< |
vab[i] = v1[i] - v2[i]; |
641 |
< |
vcb[i] = v3[i] - v2[i]; |
642 |
< |
} |
643 |
< |
d = DOT(vcb,vcb); |
637 |
> |
/* compute major axis */ |
638 |
> |
for (i = 0; i < 3; i++) { |
639 |
> |
vab[i] = v1[i] - v2[i]; |
640 |
> |
vcb[i] = v3[i] - v2[i]; |
641 |
> |
} |
642 |
> |
fcross(va, vab, vcb); |
643 |
> |
bcm->ax = ABS(va[0]) > ABS(va[1]) ? 0 : 1; |
644 |
> |
bcm->ax = ABS(va[bcm->ax]) > ABS(va[2]) ? bcm->ax : 2; |
645 |
> |
ax0 = (bcm->ax + 1) % 3; |
646 |
> |
ax1 = (bcm->ax + 2) % 3; |
647 |
> |
for (j = 0; j < 2; j++) { |
648 |
> |
vab[0] = v1[ax0] - v2[ax0]; |
649 |
> |
vcb[0] = v3[ax0] - v2[ax0]; |
650 |
> |
vab[1] = v1[ax1] - v2[ax1]; |
651 |
> |
vcb[1] = v3[ax1] - v2[ax1]; |
652 |
> |
d = vcb[0]*vcb[0] + vcb[1]*vcb[1]; |
653 |
|
if (d <= FTINY) |
654 |
|
return(-1); |
655 |
< |
d = DOT(vcb,vab)/d; |
656 |
< |
for (i = 0; i < 3; i++) |
657 |
< |
va[i] = vab[i] - vcb[i]*d; |
658 |
< |
d = DOT(va,va); |
655 |
> |
d = (vcb[0]*vab[0]+vcb[1]*vab[1])/d; |
656 |
> |
va[0] = vab[0] - vcb[0]*d; |
657 |
> |
va[1] = vab[1] - vcb[1]*d; |
658 |
> |
d = va[0]*va[0] + va[1]*va[1]; |
659 |
|
if (d <= FTINY) |
660 |
|
return(-1); |
661 |
< |
for (i = 0; i < 3; i++) { |
662 |
< |
va[i] /= d; |
663 |
< |
bcm[j][i] = va[i]; |
646 |
< |
} |
647 |
< |
bcm[j][3] = -DOT(v2,va); |
661 |
> |
bcm->tm[j][0] = va[0] /= d; |
662 |
> |
bcm->tm[j][1] = va[1] /= d; |
663 |
> |
bcm->tm[j][2] = -(v2[ax0]*va[0]+v2[ax1]*va[1]); |
664 |
|
/* rotate vertices */ |
665 |
|
vt = v1; |
666 |
|
v1 = v2; |
672 |
|
|
673 |
|
|
674 |
|
put_baryc(bcm) /* put barycentric coord. vectors */ |
675 |
< |
register BARYCCM bcm; |
675 |
> |
register BARYCCM *bcm; |
676 |
|
{ |
677 |
< |
register int i; |
678 |
< |
|
679 |
< |
for (i = 0; i < 3; i++) |
680 |
< |
printf("%14.8f %14.8f %14.8f %14.8f\n", |
681 |
< |
bcm[i][0], bcm[i][1], bcm[i][2], bcm[i][3]); |
666 |
< |
} |
667 |
< |
|
668 |
< |
|
669 |
< |
putquad(p0, p1, p3, p2) /* put out a quadrilateral */ |
670 |
< |
char *p0, *p1, *p3, *p2; /* names correspond to binary pos. */ |
671 |
< |
{ |
672 |
< |
VNDX p0i, p1i, p2i, p3i; |
673 |
< |
FVECT norm[4]; |
674 |
< |
char *mod, *name; |
675 |
< |
int axis; |
676 |
< |
FVECT v1, v2, vc1, vc2; |
677 |
< |
int ok1, ok2; |
678 |
< |
|
679 |
< |
if ((mod = getmtl()) == NULL) |
680 |
< |
return(-1); |
681 |
< |
name = getonm(); |
682 |
< |
/* get actual indices */ |
683 |
< |
if (!cvtndx(p0i,p0) || !cvtndx(p1i,p1) || |
684 |
< |
!cvtndx(p2i,p2) || !cvtndx(p3i,p3)) |
685 |
< |
return(0); |
686 |
< |
/* compute exact normals */ |
687 |
< |
fvsum(v1, vlist[p1i[0]], vlist[p0i[0]], -1.0); |
688 |
< |
fvsum(v2, vlist[p2i[0]], vlist[p0i[0]], -1.0); |
689 |
< |
fcross(vc1, v1, v2); |
690 |
< |
ok1 = normalize(vc1) != 0.0; |
691 |
< |
fvsum(v1, vlist[p2i[0]], vlist[p3i[0]], -1.0); |
692 |
< |
fvsum(v2, vlist[p1i[0]], vlist[p3i[0]], -1.0); |
693 |
< |
fcross(vc2, v1, v2); |
694 |
< |
ok2 = normalize(vc2) != 0.0; |
695 |
< |
if (!(ok1 | ok2)) |
696 |
< |
return(-1); |
697 |
< |
/* compute normal interpolation */ |
698 |
< |
axis = norminterp(norm, p0i, p1i, p2i, p3i); |
699 |
< |
|
700 |
< |
/* put out quadrilateral? */ |
701 |
< |
if (ok1 & ok2 && fabs(fdot(vc1,vc2)) >= 1.0-FTINY) { |
702 |
< |
printf("\n%s ", mod); |
703 |
< |
if (axis != -1) { |
704 |
< |
printf("texfunc %s\n", TEXNAME); |
705 |
< |
printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME); |
706 |
< |
printf("0\n13\t%d\n", axis); |
707 |
< |
pvect(norm[0]); |
708 |
< |
pvect(norm[1]); |
709 |
< |
pvect(norm[2]); |
710 |
< |
fvsum(v1, norm[3], vc1, -0.5); |
711 |
< |
fvsum(v1, v1, vc2, -0.5); |
712 |
< |
pvect(v1); |
713 |
< |
printf("\n%s ", TEXNAME); |
714 |
< |
} |
715 |
< |
printf("polygon %s.%d\n", name, faceno); |
716 |
< |
printf("0\n0\n12\n"); |
717 |
< |
pvect(vlist[p0i[0]]); |
718 |
< |
pvect(vlist[p1i[0]]); |
719 |
< |
pvect(vlist[p3i[0]]); |
720 |
< |
pvect(vlist[p2i[0]]); |
721 |
< |
return(1); |
722 |
< |
} |
723 |
< |
/* put out triangles? */ |
724 |
< |
if (ok1) { |
725 |
< |
printf("\n%s ", mod); |
726 |
< |
if (axis != -1) { |
727 |
< |
printf("texfunc %s\n", TEXNAME); |
728 |
< |
printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME); |
729 |
< |
printf("0\n13\t%d\n", axis); |
730 |
< |
pvect(norm[0]); |
731 |
< |
pvect(norm[1]); |
732 |
< |
pvect(norm[2]); |
733 |
< |
fvsum(v1, norm[3], vc1, -1.0); |
734 |
< |
pvect(v1); |
735 |
< |
printf("\n%s ", TEXNAME); |
736 |
< |
} |
737 |
< |
printf("polygon %s.%da\n", name, faceno); |
738 |
< |
printf("0\n0\n9\n"); |
739 |
< |
pvect(vlist[p0i[0]]); |
740 |
< |
pvect(vlist[p1i[0]]); |
741 |
< |
pvect(vlist[p2i[0]]); |
742 |
< |
} |
743 |
< |
if (ok2) { |
744 |
< |
printf("\n%s ", mod); |
745 |
< |
if (axis != -1) { |
746 |
< |
printf("texfunc %s\n", TEXNAME); |
747 |
< |
printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME); |
748 |
< |
printf("0\n13\t%d\n", axis); |
749 |
< |
pvect(norm[0]); |
750 |
< |
pvect(norm[1]); |
751 |
< |
pvect(norm[2]); |
752 |
< |
fvsum(v2, norm[3], vc2, -1.0); |
753 |
< |
pvect(v2); |
754 |
< |
printf("\n%s ", TEXNAME); |
755 |
< |
} |
756 |
< |
printf("polygon %s.%db\n", name, faceno); |
757 |
< |
printf("0\n0\n9\n"); |
758 |
< |
pvect(vlist[p2i[0]]); |
759 |
< |
pvect(vlist[p1i[0]]); |
760 |
< |
pvect(vlist[p3i[0]]); |
761 |
< |
} |
762 |
< |
return(1); |
763 |
< |
} |
764 |
< |
|
765 |
< |
|
766 |
< |
int |
767 |
< |
norminterp(resmat, p0i, p1i, p2i, p3i) /* compute normal interpolation */ |
768 |
< |
register FVECT resmat[4]; |
769 |
< |
register VNDX p0i, p1i, p2i, p3i; |
770 |
< |
{ |
771 |
< |
#define u ((ax+1)%3) |
772 |
< |
#define v ((ax+2)%3) |
773 |
< |
|
774 |
< |
register int ax; |
775 |
< |
MAT4 eqnmat; |
776 |
< |
FVECT v1; |
777 |
< |
register int i, j; |
778 |
< |
|
779 |
< |
if (!(p0i[2]>=0 && p1i[2]>=0 && p2i[2]>=0 && p3i[2]>=0)) |
780 |
< |
return(-1); |
781 |
< |
/* find dominant axis */ |
782 |
< |
VCOPY(v1, vnlist[p0i[2]]); |
783 |
< |
fvsum(v1, v1, vnlist[p1i[2]], 1.0); |
784 |
< |
fvsum(v1, v1, vnlist[p2i[2]], 1.0); |
785 |
< |
fvsum(v1, v1, vnlist[p3i[2]], 1.0); |
786 |
< |
ax = ABS(v1[0]) > ABS(v1[1]) ? 0 : 1; |
787 |
< |
ax = ABS(v1[ax]) > ABS(v1[2]) ? ax : 2; |
788 |
< |
/* assign equation matrix */ |
789 |
< |
eqnmat[0][0] = vlist[p0i[0]][u]*vlist[p0i[0]][v]; |
790 |
< |
eqnmat[0][1] = vlist[p0i[0]][u]; |
791 |
< |
eqnmat[0][2] = vlist[p0i[0]][v]; |
792 |
< |
eqnmat[0][3] = 1.0; |
793 |
< |
eqnmat[1][0] = vlist[p1i[0]][u]*vlist[p1i[0]][v]; |
794 |
< |
eqnmat[1][1] = vlist[p1i[0]][u]; |
795 |
< |
eqnmat[1][2] = vlist[p1i[0]][v]; |
796 |
< |
eqnmat[1][3] = 1.0; |
797 |
< |
eqnmat[2][0] = vlist[p2i[0]][u]*vlist[p2i[0]][v]; |
798 |
< |
eqnmat[2][1] = vlist[p2i[0]][u]; |
799 |
< |
eqnmat[2][2] = vlist[p2i[0]][v]; |
800 |
< |
eqnmat[2][3] = 1.0; |
801 |
< |
eqnmat[3][0] = vlist[p3i[0]][u]*vlist[p3i[0]][v]; |
802 |
< |
eqnmat[3][1] = vlist[p3i[0]][u]; |
803 |
< |
eqnmat[3][2] = vlist[p3i[0]][v]; |
804 |
< |
eqnmat[3][3] = 1.0; |
805 |
< |
/* invert matrix (solve system) */ |
806 |
< |
if (!invmat4(eqnmat, eqnmat)) |
807 |
< |
return(-1); /* no solution */ |
808 |
< |
/* compute result matrix */ |
809 |
< |
for (j = 0; j < 4; j++) |
810 |
< |
for (i = 0; i < 3; i++) |
811 |
< |
resmat[j][i] = eqnmat[j][0]*vnlist[p0i[2]][i] + |
812 |
< |
eqnmat[j][1]*vnlist[p1i[2]][i] + |
813 |
< |
eqnmat[j][2]*vnlist[p2i[2]][i] + |
814 |
< |
eqnmat[j][3]*vnlist[p3i[2]][i]; |
815 |
< |
return(ax); |
816 |
< |
|
817 |
< |
#undef u |
818 |
< |
#undef v |
677 |
> |
printf("\t%d\n", bcm->ax); |
678 |
> |
printf("%14.8f %14.8f %14.8f\n", |
679 |
> |
bcm->tm[0][0], bcm->tm[0][1], bcm->tm[0][2]); |
680 |
> |
printf("%14.8f %14.8f %14.8f\n", |
681 |
> |
bcm->tm[1][0], bcm->tm[1][1], bcm->tm[1][2]); |
682 |
|
} |
683 |
|
|
684 |
|
|