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 |
|
|
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; |
240 |
– |
case 4: |
241 |
– |
if (!putquad(argv[1], argv[2], |
242 |
– |
argv[3], argv[4])) |
243 |
– |
syntax("Bad quad"); |
244 |
– |
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 |
|
} |
549 |
– |
if (ac == 3 && !puttri(av[0], av[1], av[2])) |
550 |
– |
return(0); |
546 |
|
return(1); |
547 |
|
} |
548 |
|
if ((cp = getmtl()) == NULL) |
564 |
|
char *mod; |
565 |
|
VNDX v1i, v2i, v3i; |
566 |
|
BARYCCM bvecs; |
567 |
+ |
FVECT bcoor[3]; |
568 |
|
int texOK, patOK; |
569 |
+ |
register int i; |
570 |
|
|
571 |
|
if ((mod = getmtl()) == NULL) |
572 |
|
return(-1); |
581 |
|
patOK = 0; |
582 |
|
#endif |
583 |
|
if (texOK | patOK) |
584 |
< |
if (comp_baryc(bvecs, vlist[v1i[0]], vlist[v2i[0]], |
584 |
> |
if (comp_baryc(&bvecs, vlist[v1i[0]], vlist[v2i[0]], |
585 |
|
vlist[v3i[0]]) < 0) |
586 |
|
return(-1); |
587 |
|
/* put out texture (if any) */ |
589 |
|
printf("\n%s texfunc %s\n", mod, TEXNAME); |
590 |
|
mod = TEXNAME; |
591 |
|
printf("4 dx dy dz %s\n", TCALNAME); |
592 |
< |
printf("0\n21\n"); |
593 |
< |
put_baryc(bvecs); |
594 |
< |
printf("\t%14.12g %14.12g %14.12g\n", |
595 |
< |
vnlist[v1i[2]][0], vnlist[v2i[2]][0], |
596 |
< |
vnlist[v3i[2]][0]); |
597 |
< |
printf("\t%14.12g %14.12g %14.12g\n", |
598 |
< |
vnlist[v1i[2]][1], vnlist[v2i[2]][1], |
602 |
< |
vnlist[v3i[2]][1]); |
603 |
< |
printf("\t%14.12g %14.12g %14.12g\n", |
604 |
< |
vnlist[v1i[2]][2], vnlist[v2i[2]][2], |
605 |
< |
vnlist[v3i[2]][2]); |
592 |
> |
printf("0\n"); |
593 |
> |
for (i = 0; i < 3; i++) { |
594 |
> |
bcoor[i][0] = vnlist[v1i[2]][i]; |
595 |
> |
bcoor[i][1] = vnlist[v2i[2]][i]; |
596 |
> |
bcoor[i][2] = vnlist[v3i[2]][i]; |
597 |
> |
} |
598 |
> |
put_baryc(&bvecs, bcoor, 3); |
599 |
|
} |
600 |
|
#ifdef TEXMAPS |
601 |
|
/* put out pattern (if any) */ |
603 |
|
printf("\n%s colorpict %s\n", mod, PATNAME); |
604 |
|
mod = PATNAME; |
605 |
|
printf("7 noneg noneg noneg %s %s u v\n", mapname, TCALNAME); |
606 |
< |
printf("0\n18\n"); |
607 |
< |
put_baryc(bvecs); |
608 |
< |
printf("\t%f %f %f\n", vtlist[v1i[1]][0], |
609 |
< |
vtlist[v2i[1]][0], vtlist[v3i[1]][0]); |
610 |
< |
printf("\t%f %f %f\n", vtlist[v1i[1]][1], |
611 |
< |
vtlist[v2i[1]][1], vtlist[v3i[1]][1]); |
606 |
> |
printf("0\n"); |
607 |
> |
for (i = 0; i < 2; i++) { |
608 |
> |
bcoor[i][0] = vtlist[v1i[1]][i]; |
609 |
> |
bcoor[i][1] = vtlist[v2i[1]][i]; |
610 |
> |
bcoor[i][2] = vtlist[v3i[1]][i]; |
611 |
> |
} |
612 |
> |
put_baryc(&bvecs, bcoor, 2); |
613 |
|
} |
614 |
|
#endif |
615 |
|
/* put out triangle */ |
625 |
|
|
626 |
|
int |
627 |
|
comp_baryc(bcm, v1, v2, v3) /* compute barycentric vectors */ |
628 |
< |
register BARYCCM bcm; |
628 |
> |
register BARYCCM *bcm; |
629 |
|
FLOAT *v1, *v2, *v3; |
630 |
|
{ |
631 |
|
FLOAT *vt; |
632 |
|
FVECT va, vab, vcb; |
633 |
|
double d; |
634 |
+ |
int ax0, ax1; |
635 |
|
register int i, j; |
636 |
< |
|
637 |
< |
for (j = 0; j < 3; j++) { |
638 |
< |
for (i = 0; i < 3; i++) { |
639 |
< |
vab[i] = v1[i] - v2[i]; |
640 |
< |
vcb[i] = v3[i] - v2[i]; |
641 |
< |
} |
642 |
< |
d = DOT(vcb,vcb); |
636 |
> |
/* compute major axis */ |
637 |
> |
for (i = 0; i < 3; i++) { |
638 |
> |
vab[i] = v1[i] - v2[i]; |
639 |
> |
vcb[i] = v3[i] - v2[i]; |
640 |
> |
} |
641 |
> |
fcross(va, vab, vcb); |
642 |
> |
bcm->ax = ABS(va[0]) > ABS(va[1]) ? 0 : 1; |
643 |
> |
bcm->ax = ABS(va[bcm->ax]) > ABS(va[2]) ? bcm->ax : 2; |
644 |
> |
ax0 = (bcm->ax + 1) % 3; |
645 |
> |
ax1 = (bcm->ax + 2) % 3; |
646 |
> |
for (j = 0; j < 2; j++) { |
647 |
> |
vab[0] = v1[ax0] - v2[ax0]; |
648 |
> |
vcb[0] = v3[ax0] - v2[ax0]; |
649 |
> |
vab[1] = v1[ax1] - v2[ax1]; |
650 |
> |
vcb[1] = v3[ax1] - v2[ax1]; |
651 |
> |
d = vcb[0]*vcb[0] + vcb[1]*vcb[1]; |
652 |
|
if (d <= FTINY) |
653 |
|
return(-1); |
654 |
< |
d = DOT(vcb,vab)/d; |
655 |
< |
for (i = 0; i < 3; i++) |
656 |
< |
va[i] = vab[i] - vcb[i]*d; |
657 |
< |
d = DOT(va,va); |
654 |
> |
d = (vcb[0]*vab[0]+vcb[1]*vab[1])/d; |
655 |
> |
va[0] = vab[0] - vcb[0]*d; |
656 |
> |
va[1] = vab[1] - vcb[1]*d; |
657 |
> |
d = va[0]*va[0] + va[1]*va[1]; |
658 |
|
if (d <= FTINY) |
659 |
|
return(-1); |
660 |
< |
for (i = 0; i < 3; i++) { |
661 |
< |
va[i] /= d; |
662 |
< |
bcm[j][i] = va[i]; |
659 |
< |
} |
660 |
< |
bcm[j][3] = -DOT(v2,va); |
660 |
> |
bcm->tm[j][0] = va[0] /= d; |
661 |
> |
bcm->tm[j][1] = va[1] /= d; |
662 |
> |
bcm->tm[j][2] = -(v2[ax0]*va[0]+v2[ax1]*va[1]); |
663 |
|
/* rotate vertices */ |
664 |
|
vt = v1; |
665 |
|
v1 = v2; |
670 |
|
} |
671 |
|
|
672 |
|
|
673 |
< |
put_baryc(bcm) /* put barycentric coord. vectors */ |
674 |
< |
register BARYCCM bcm; |
673 |
> |
put_baryc(bcm, com, n) /* put barycentric coord. vectors */ |
674 |
> |
register BARYCCM *bcm; |
675 |
> |
register FVECT com[]; |
676 |
> |
int n; |
677 |
|
{ |
678 |
< |
register int i; |
678 |
> |
double a, b; |
679 |
> |
register int i, j; |
680 |
|
|
681 |
< |
for (i = 0; i < 3; i++) |
682 |
< |
printf("%14.8f %14.8f %14.8f %14.8f\n", |
683 |
< |
bcm[i][0], bcm[i][1], bcm[i][2], bcm[i][3]); |
684 |
< |
} |
685 |
< |
|
686 |
< |
|
687 |
< |
putquad(p0, p1, p3, p2) /* put out a quadrilateral */ |
688 |
< |
char *p0, *p1, *p3, *p2; /* names correspond to binary pos. */ |
684 |
< |
{ |
685 |
< |
VNDX p0i, p1i, p2i, p3i; |
686 |
< |
FVECT norm[4]; |
687 |
< |
char *mod, *name; |
688 |
< |
int axis; |
689 |
< |
FVECT v1, v2, vc1, vc2; |
690 |
< |
int ok1, ok2; |
691 |
< |
|
692 |
< |
#ifdef TEXMAPS |
693 |
< |
/* also should output texture index coordinates, |
694 |
< |
* which will require new .cal file |
695 |
< |
*/ |
696 |
< |
#endif |
697 |
< |
if ((mod = getmtl()) == NULL) |
698 |
< |
return(-1); |
699 |
< |
name = getonm(); |
700 |
< |
/* get actual indices */ |
701 |
< |
if (!cvtndx(p0i,p0) || !cvtndx(p1i,p1) || |
702 |
< |
!cvtndx(p2i,p2) || !cvtndx(p3i,p3)) |
703 |
< |
return(0); |
704 |
< |
/* compute exact normals */ |
705 |
< |
fvsum(v1, vlist[p1i[0]], vlist[p0i[0]], -1.0); |
706 |
< |
fvsum(v2, vlist[p2i[0]], vlist[p0i[0]], -1.0); |
707 |
< |
fcross(vc1, v1, v2); |
708 |
< |
ok1 = normalize(vc1) != 0.0; |
709 |
< |
fvsum(v1, vlist[p2i[0]], vlist[p3i[0]], -1.0); |
710 |
< |
fvsum(v2, vlist[p1i[0]], vlist[p3i[0]], -1.0); |
711 |
< |
fcross(vc2, v1, v2); |
712 |
< |
ok2 = normalize(vc2) != 0.0; |
713 |
< |
if (!(ok1 | ok2)) |
714 |
< |
return(-1); |
715 |
< |
/* compute normal interpolation */ |
716 |
< |
axis = norminterp(norm, p0i, p1i, p2i, p3i); |
717 |
< |
|
718 |
< |
/* put out quadrilateral? */ |
719 |
< |
if (ok1 & ok2 && fabs(fdot(vc1,vc2)) >= 1.0-FTINY) { |
720 |
< |
printf("\n%s ", mod); |
721 |
< |
if (axis != -1) { |
722 |
< |
printf("texfunc %s\n", TEXNAME); |
723 |
< |
printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME); |
724 |
< |
printf("0\n13\t%d\n", axis); |
725 |
< |
pvect(norm[0]); |
726 |
< |
pvect(norm[1]); |
727 |
< |
pvect(norm[2]); |
728 |
< |
fvsum(v1, norm[3], vc1, -0.5); |
729 |
< |
fvsum(v1, v1, vc2, -0.5); |
730 |
< |
pvect(v1); |
731 |
< |
printf("\n%s ", TEXNAME); |
732 |
< |
} |
733 |
< |
printf("polygon %s.%d\n", name, faceno); |
734 |
< |
printf("0\n0\n12\n"); |
735 |
< |
pvect(vlist[p0i[0]]); |
736 |
< |
pvect(vlist[p1i[0]]); |
737 |
< |
pvect(vlist[p3i[0]]); |
738 |
< |
pvect(vlist[p2i[0]]); |
739 |
< |
return(1); |
681 |
> |
printf("%d\t%d\n", 1+3*n, bcm->ax); |
682 |
> |
for (i = 0; i < n; i++) { |
683 |
> |
a = com[i][0] - com[i][2]; |
684 |
> |
b = com[i][1] - com[i][2]; |
685 |
> |
printf("%14.8f %14.8f %14.8f\n", |
686 |
> |
bcm->tm[0][0]*a + bcm->tm[1][0]*b, |
687 |
> |
bcm->tm[0][1]*a + bcm->tm[1][1]*b, |
688 |
> |
bcm->tm[0][2]*a + bcm->tm[1][2]*b + com[i][2]); |
689 |
|
} |
741 |
– |
/* put out triangles? */ |
742 |
– |
if (ok1) { |
743 |
– |
printf("\n%s ", mod); |
744 |
– |
if (axis != -1) { |
745 |
– |
printf("texfunc %s\n", TEXNAME); |
746 |
– |
printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME); |
747 |
– |
printf("0\n13\t%d\n", axis); |
748 |
– |
pvect(norm[0]); |
749 |
– |
pvect(norm[1]); |
750 |
– |
pvect(norm[2]); |
751 |
– |
fvsum(v1, norm[3], vc1, -1.0); |
752 |
– |
pvect(v1); |
753 |
– |
printf("\n%s ", TEXNAME); |
754 |
– |
} |
755 |
– |
printf("polygon %s.%da\n", name, faceno); |
756 |
– |
printf("0\n0\n9\n"); |
757 |
– |
pvect(vlist[p0i[0]]); |
758 |
– |
pvect(vlist[p1i[0]]); |
759 |
– |
pvect(vlist[p2i[0]]); |
760 |
– |
} |
761 |
– |
if (ok2) { |
762 |
– |
printf("\n%s ", mod); |
763 |
– |
if (axis != -1) { |
764 |
– |
printf("texfunc %s\n", TEXNAME); |
765 |
– |
printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME); |
766 |
– |
printf("0\n13\t%d\n", axis); |
767 |
– |
pvect(norm[0]); |
768 |
– |
pvect(norm[1]); |
769 |
– |
pvect(norm[2]); |
770 |
– |
fvsum(v2, norm[3], vc2, -1.0); |
771 |
– |
pvect(v2); |
772 |
– |
printf("\n%s ", TEXNAME); |
773 |
– |
} |
774 |
– |
printf("polygon %s.%db\n", name, faceno); |
775 |
– |
printf("0\n0\n9\n"); |
776 |
– |
pvect(vlist[p2i[0]]); |
777 |
– |
pvect(vlist[p1i[0]]); |
778 |
– |
pvect(vlist[p3i[0]]); |
779 |
– |
} |
780 |
– |
return(1); |
781 |
– |
} |
782 |
– |
|
783 |
– |
|
784 |
– |
int |
785 |
– |
norminterp(resmat, p0i, p1i, p2i, p3i) /* compute normal interpolation */ |
786 |
– |
register FVECT resmat[4]; |
787 |
– |
register VNDX p0i, p1i, p2i, p3i; |
788 |
– |
{ |
789 |
– |
#define u ((ax+1)%3) |
790 |
– |
#define v ((ax+2)%3) |
791 |
– |
|
792 |
– |
register int ax; |
793 |
– |
MAT4 eqnmat; |
794 |
– |
FVECT v1; |
795 |
– |
register int i, j; |
796 |
– |
|
797 |
– |
#ifdef TEXMAPS |
798 |
– |
/* also check for texture indices */ |
799 |
– |
#endif |
800 |
– |
if (flatten || !(p0i[2]>=0 && p1i[2]>=0 && p2i[2]>=0 && p3i[2]>=0)) |
801 |
– |
return(-1); |
802 |
– |
/* find dominant axis */ |
803 |
– |
VCOPY(v1, vnlist[p0i[2]]); |
804 |
– |
fvsum(v1, v1, vnlist[p1i[2]], 1.0); |
805 |
– |
fvsum(v1, v1, vnlist[p2i[2]], 1.0); |
806 |
– |
fvsum(v1, v1, vnlist[p3i[2]], 1.0); |
807 |
– |
ax = ABS(v1[0]) > ABS(v1[1]) ? 0 : 1; |
808 |
– |
ax = ABS(v1[ax]) > ABS(v1[2]) ? ax : 2; |
809 |
– |
/* assign equation matrix */ |
810 |
– |
eqnmat[0][0] = vlist[p0i[0]][u]*vlist[p0i[0]][v]; |
811 |
– |
eqnmat[0][1] = vlist[p0i[0]][u]; |
812 |
– |
eqnmat[0][2] = vlist[p0i[0]][v]; |
813 |
– |
eqnmat[0][3] = 1.0; |
814 |
– |
eqnmat[1][0] = vlist[p1i[0]][u]*vlist[p1i[0]][v]; |
815 |
– |
eqnmat[1][1] = vlist[p1i[0]][u]; |
816 |
– |
eqnmat[1][2] = vlist[p1i[0]][v]; |
817 |
– |
eqnmat[1][3] = 1.0; |
818 |
– |
eqnmat[2][0] = vlist[p2i[0]][u]*vlist[p2i[0]][v]; |
819 |
– |
eqnmat[2][1] = vlist[p2i[0]][u]; |
820 |
– |
eqnmat[2][2] = vlist[p2i[0]][v]; |
821 |
– |
eqnmat[2][3] = 1.0; |
822 |
– |
eqnmat[3][0] = vlist[p3i[0]][u]*vlist[p3i[0]][v]; |
823 |
– |
eqnmat[3][1] = vlist[p3i[0]][u]; |
824 |
– |
eqnmat[3][2] = vlist[p3i[0]][v]; |
825 |
– |
eqnmat[3][3] = 1.0; |
826 |
– |
/* invert matrix (solve system) */ |
827 |
– |
if (!invmat4(eqnmat, eqnmat)) |
828 |
– |
return(-1); /* no solution */ |
829 |
– |
/* compute result matrix */ |
830 |
– |
for (j = 0; j < 4; j++) |
831 |
– |
for (i = 0; i < 3; i++) |
832 |
– |
resmat[j][i] = eqnmat[j][0]*vnlist[p0i[2]][i] + |
833 |
– |
eqnmat[j][1]*vnlist[p1i[2]][i] + |
834 |
– |
eqnmat[j][2]*vnlist[p2i[2]][i] + |
835 |
– |
eqnmat[j][3]*vnlist[p3i[2]][i]; |
836 |
– |
#ifdef TEXMAPS |
837 |
– |
/* compute result matrix for texture indices */ |
838 |
– |
#endif |
839 |
– |
return(ax); |
840 |
– |
|
841 |
– |
#undef u |
842 |
– |
#undef v |
690 |
|
} |
691 |
|
|
692 |
|
|