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"); |
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! */ |
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 |
< |
ac -= 2; /* remove two vertices & rotate */ |
540 |
> |
ac--; /* remove vertex & rotate */ |
541 |
|
cp = av[0]; |
542 |
|
for (i = 0; i < ac-1; i++) |
543 |
< |
av[i] = av[i+3]; |
543 |
> |
av[i] = av[i+2]; |
544 |
|
av[i] = cp; |
545 |
|
} |
544 |
– |
if (ac == 3 && !puttri(av[0], av[1], av[2])) |
545 |
– |
return(0); |
546 |
|
return(1); |
547 |
|
} |
548 |
|
if ((cp = getmtl()) == NULL) |
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]); |
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], |
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]; |
654 |
< |
} |
655 |
< |
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]); |
674 |
< |
} |
675 |
< |
|
676 |
< |
|
677 |
< |
putquad(p0, p1, p3, p2) /* put out a quadrilateral */ |
678 |
< |
char *p0, *p1, *p3, *p2; /* names correspond to binary pos. */ |
679 |
< |
{ |
680 |
< |
VNDX p0i, p1i, p2i, p3i; |
681 |
< |
FVECT norm[4]; |
682 |
< |
char *mod, *name; |
683 |
< |
int axis; |
684 |
< |
FVECT v1, v2, vc1, vc2; |
685 |
< |
int ok1, ok2; |
686 |
< |
|
687 |
< |
#ifdef TEXMAPS |
688 |
< |
/* also should output texture index coordinates, |
689 |
< |
* which will require new .cal file |
690 |
< |
*/ |
691 |
< |
#endif |
692 |
< |
if ((mod = getmtl()) == NULL) |
693 |
< |
return(-1); |
694 |
< |
name = getonm(); |
695 |
< |
/* get actual indices */ |
696 |
< |
if (!cvtndx(p0i,p0) || !cvtndx(p1i,p1) || |
697 |
< |
!cvtndx(p2i,p2) || !cvtndx(p3i,p3)) |
698 |
< |
return(0); |
699 |
< |
/* compute exact normals */ |
700 |
< |
fvsum(v1, vlist[p1i[0]], vlist[p0i[0]], -1.0); |
701 |
< |
fvsum(v2, vlist[p2i[0]], vlist[p0i[0]], -1.0); |
702 |
< |
fcross(vc1, v1, v2); |
703 |
< |
ok1 = normalize(vc1) != 0.0; |
704 |
< |
fvsum(v1, vlist[p2i[0]], vlist[p3i[0]], -1.0); |
705 |
< |
fvsum(v2, vlist[p1i[0]], vlist[p3i[0]], -1.0); |
706 |
< |
fcross(vc2, v1, v2); |
707 |
< |
ok2 = normalize(vc2) != 0.0; |
708 |
< |
if (!(ok1 | ok2)) |
709 |
< |
return(-1); |
710 |
< |
/* compute normal interpolation */ |
711 |
< |
axis = norminterp(norm, p0i, p1i, p2i, p3i); |
712 |
< |
|
713 |
< |
/* put out quadrilateral? */ |
714 |
< |
if (ok1 & ok2 && fabs(fdot(vc1,vc2)) >= 1.0-FTINY) { |
715 |
< |
printf("\n%s ", mod); |
716 |
< |
if (axis != -1) { |
717 |
< |
printf("texfunc %s\n", TEXNAME); |
718 |
< |
printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME); |
719 |
< |
printf("0\n13\t%d\n", axis); |
720 |
< |
pvect(norm[0]); |
721 |
< |
pvect(norm[1]); |
722 |
< |
pvect(norm[2]); |
723 |
< |
fvsum(v1, norm[3], vc1, -0.5); |
724 |
< |
fvsum(v1, v1, vc2, -0.5); |
725 |
< |
pvect(v1); |
726 |
< |
printf("\n%s ", TEXNAME); |
727 |
< |
} |
728 |
< |
printf("polygon %s.%d\n", name, faceno); |
729 |
< |
printf("0\n0\n12\n"); |
730 |
< |
pvect(vlist[p0i[0]]); |
731 |
< |
pvect(vlist[p1i[0]]); |
732 |
< |
pvect(vlist[p3i[0]]); |
733 |
< |
pvect(vlist[p2i[0]]); |
734 |
< |
return(1); |
735 |
< |
} |
736 |
< |
/* put out triangles? */ |
737 |
< |
if (ok1) { |
738 |
< |
printf("\n%s ", mod); |
739 |
< |
if (axis != -1) { |
740 |
< |
printf("texfunc %s\n", TEXNAME); |
741 |
< |
printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME); |
742 |
< |
printf("0\n13\t%d\n", axis); |
743 |
< |
pvect(norm[0]); |
744 |
< |
pvect(norm[1]); |
745 |
< |
pvect(norm[2]); |
746 |
< |
fvsum(v1, norm[3], vc1, -1.0); |
747 |
< |
pvect(v1); |
748 |
< |
printf("\n%s ", TEXNAME); |
749 |
< |
} |
750 |
< |
printf("polygon %s.%da\n", name, faceno); |
751 |
< |
printf("0\n0\n9\n"); |
752 |
< |
pvect(vlist[p0i[0]]); |
753 |
< |
pvect(vlist[p1i[0]]); |
754 |
< |
pvect(vlist[p2i[0]]); |
755 |
< |
} |
756 |
< |
if (ok2) { |
757 |
< |
printf("\n%s ", mod); |
758 |
< |
if (axis != -1) { |
759 |
< |
printf("texfunc %s\n", TEXNAME); |
760 |
< |
printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME); |
761 |
< |
printf("0\n13\t%d\n", axis); |
762 |
< |
pvect(norm[0]); |
763 |
< |
pvect(norm[1]); |
764 |
< |
pvect(norm[2]); |
765 |
< |
fvsum(v2, norm[3], vc2, -1.0); |
766 |
< |
pvect(v2); |
767 |
< |
printf("\n%s ", TEXNAME); |
768 |
< |
} |
769 |
< |
printf("polygon %s.%db\n", name, faceno); |
770 |
< |
printf("0\n0\n9\n"); |
771 |
< |
pvect(vlist[p2i[0]]); |
772 |
< |
pvect(vlist[p1i[0]]); |
773 |
< |
pvect(vlist[p3i[0]]); |
774 |
< |
} |
775 |
< |
return(1); |
776 |
< |
} |
777 |
< |
|
778 |
< |
|
779 |
< |
int |
780 |
< |
norminterp(resmat, p0i, p1i, p2i, p3i) /* compute normal interpolation */ |
781 |
< |
register FVECT resmat[4]; |
782 |
< |
register VNDX p0i, p1i, p2i, p3i; |
783 |
< |
{ |
784 |
< |
#define u ((ax+1)%3) |
785 |
< |
#define v ((ax+2)%3) |
786 |
< |
|
787 |
< |
register int ax; |
788 |
< |
MAT4 eqnmat; |
789 |
< |
FVECT v1; |
790 |
< |
register int i, j; |
791 |
< |
|
792 |
< |
#ifdef TEXMAPS |
793 |
< |
/* also check for texture indices */ |
794 |
< |
#endif |
795 |
< |
if (!(p0i[2]>=0 && p1i[2]>=0 && p2i[2]>=0 && p3i[2]>=0)) |
796 |
< |
return(-1); |
797 |
< |
/* find dominant axis */ |
798 |
< |
VCOPY(v1, vnlist[p0i[2]]); |
799 |
< |
fvsum(v1, v1, vnlist[p1i[2]], 1.0); |
800 |
< |
fvsum(v1, v1, vnlist[p2i[2]], 1.0); |
801 |
< |
fvsum(v1, v1, vnlist[p3i[2]], 1.0); |
802 |
< |
ax = ABS(v1[0]) > ABS(v1[1]) ? 0 : 1; |
803 |
< |
ax = ABS(v1[ax]) > ABS(v1[2]) ? ax : 2; |
804 |
< |
/* assign equation matrix */ |
805 |
< |
eqnmat[0][0] = vlist[p0i[0]][u]*vlist[p0i[0]][v]; |
806 |
< |
eqnmat[0][1] = vlist[p0i[0]][u]; |
807 |
< |
eqnmat[0][2] = vlist[p0i[0]][v]; |
808 |
< |
eqnmat[0][3] = 1.0; |
809 |
< |
eqnmat[1][0] = vlist[p1i[0]][u]*vlist[p1i[0]][v]; |
810 |
< |
eqnmat[1][1] = vlist[p1i[0]][u]; |
811 |
< |
eqnmat[1][2] = vlist[p1i[0]][v]; |
812 |
< |
eqnmat[1][3] = 1.0; |
813 |
< |
eqnmat[2][0] = vlist[p2i[0]][u]*vlist[p2i[0]][v]; |
814 |
< |
eqnmat[2][1] = vlist[p2i[0]][u]; |
815 |
< |
eqnmat[2][2] = vlist[p2i[0]][v]; |
816 |
< |
eqnmat[2][3] = 1.0; |
817 |
< |
eqnmat[3][0] = vlist[p3i[0]][u]*vlist[p3i[0]][v]; |
818 |
< |
eqnmat[3][1] = vlist[p3i[0]][u]; |
819 |
< |
eqnmat[3][2] = vlist[p3i[0]][v]; |
820 |
< |
eqnmat[3][3] = 1.0; |
821 |
< |
/* invert matrix (solve system) */ |
822 |
< |
if (!invmat4(eqnmat, eqnmat)) |
823 |
< |
return(-1); /* no solution */ |
824 |
< |
/* compute result matrix */ |
825 |
< |
for (j = 0; j < 4; j++) |
826 |
< |
for (i = 0; i < 3; i++) |
827 |
< |
resmat[j][i] = eqnmat[j][0]*vnlist[p0i[2]][i] + |
828 |
< |
eqnmat[j][1]*vnlist[p1i[2]][i] + |
829 |
< |
eqnmat[j][2]*vnlist[p2i[2]][i] + |
830 |
< |
eqnmat[j][3]*vnlist[p3i[2]][i]; |
831 |
< |
#ifdef TEXMAPS |
832 |
< |
/* compute result matrix for texture indices */ |
833 |
< |
#endif |
834 |
< |
return(ax); |
835 |
< |
|
836 |
< |
#undef u |
837 |
< |
#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 |
|
|