604 |
|
fp->v[j].fnext = f; /* append new face */ |
605 |
|
} |
606 |
|
|
607 |
– |
/* Allocate an empty scene */ |
608 |
– |
Scene * |
609 |
– |
newScene(void) |
610 |
– |
{ |
611 |
– |
Scene *sc = (Scene *)ecalloc(1, sizeof(Scene)); |
612 |
– |
/* default group & material */ |
613 |
– |
sc->grpname = chunk_alloc(char *, sc->grpname, sc->ngrps); |
614 |
– |
sc->grpname[sc->ngrps++] = savqstr("DEFAULT_GROUP"); |
615 |
– |
sc->matname = chunk_alloc(char *, sc->matname, sc->nmats); |
616 |
– |
sc->matname[sc->nmats++] = savqstr("DEFAULT_MATERIAL"); |
617 |
– |
|
618 |
– |
return(sc); |
619 |
– |
} |
620 |
– |
|
607 |
|
/* Add a vertex to a scene */ |
608 |
|
int |
609 |
|
addVertex(Scene *sc, double x, double y, double z) |
700 |
|
return(f); |
701 |
|
} |
702 |
|
|
703 |
+ |
/* Get neighbor vertices: malloc array with valence in index[0] */ |
704 |
+ |
int * |
705 |
+ |
getVertexNeighbors(Scene *sc, int vid) |
706 |
+ |
{ |
707 |
+ |
int *varr; |
708 |
+ |
Face *f; |
709 |
+ |
int j=0; |
710 |
+ |
|
711 |
+ |
if (sc == NULL || (vid < 0) | (vid >= sc->nverts) || |
712 |
+ |
(f = sc->vert[vid].vflist) == NULL) |
713 |
+ |
return(NULL); |
714 |
+ |
|
715 |
+ |
varr = (int *)emalloc(sizeof(int)*CHUNKSIZ); |
716 |
+ |
varr[0] = 0; /* add unique neighbors */ |
717 |
+ |
for ( ; f != NULL; f = f->v[j].fnext) { |
718 |
+ |
int i, cvi; |
719 |
+ |
for (j = f->nv; j--; ) /* find ourself in poly */ |
720 |
+ |
if (f->v[j].vid == vid) |
721 |
+ |
break; |
722 |
+ |
if (j < 0) /* this is an error */ |
723 |
+ |
break; |
724 |
+ |
if (f->nv < 3) /* also never happens */ |
725 |
+ |
continue; |
726 |
+ |
/* check previous neighbor */ |
727 |
+ |
cvi = f->v[ (j > 0) ? j-1 : f->nv-1 ].vid; |
728 |
+ |
for (i = varr[0]+1; --i; ) /* making sure not in list */ |
729 |
+ |
if (varr[i] == cvi) |
730 |
+ |
break; |
731 |
+ |
if (!i) { /* new neighbor? */ |
732 |
+ |
varr = chunk_alloc(int, varr, varr[0]+1); |
733 |
+ |
varr[++varr[0]] = cvi; |
734 |
+ |
} |
735 |
+ |
/* check next neighbor */ |
736 |
+ |
cvi = f->v[ (j < f->nv-1) ? j+1 : 0 ].vid; |
737 |
+ |
for (i = varr[0]+1; --i; ) |
738 |
+ |
if (varr[i] == cvi) |
739 |
+ |
break; |
740 |
+ |
if (!i) { |
741 |
+ |
varr = chunk_alloc(int, varr, varr[0]+1); |
742 |
+ |
varr[++varr[0]] = cvi; |
743 |
+ |
} |
744 |
+ |
} |
745 |
+ |
if (!varr[0]) { |
746 |
+ |
efree((char *)varr); /* something went awry */ |
747 |
+ |
return(NULL); |
748 |
+ |
} |
749 |
+ |
/* tighten array & return */ |
750 |
+ |
return((int *)erealloc((char *)varr, sizeof(int)*(varr[0]+1))); |
751 |
+ |
} |
752 |
+ |
|
753 |
+ |
/* Callback for growBoundingBox() */ |
754 |
+ |
static int |
755 |
+ |
addBBox(Scene *sc, Face *f, void *p) |
756 |
+ |
{ |
757 |
+ |
double (*bbox)[3] = (double (*)[3])p; |
758 |
+ |
int i, j; |
759 |
+ |
|
760 |
+ |
for (i = f->nv; i-- > 0; ) { |
761 |
+ |
double *p3 = sc->vert[f->v[i].vid].p; |
762 |
+ |
for (j = 3; j--; ) { |
763 |
+ |
if (p3[j] < bbox[0][j]) |
764 |
+ |
bbox[0][j] = p3[j]; |
765 |
+ |
if (p3[j] > bbox[1][j]) |
766 |
+ |
bbox[1][j] = p3[j]; |
767 |
+ |
} |
768 |
+ |
} |
769 |
+ |
return(1); |
770 |
+ |
} |
771 |
+ |
|
772 |
+ |
/* Expand bounding box min & max (initialize bbox to all zeroes) */ |
773 |
+ |
int |
774 |
+ |
growBoundingBox(Scene *sc, double bbox[2][3], int flreq, int flexc) |
775 |
+ |
{ |
776 |
+ |
if (sc == NULL || sc->nfaces <= 0 || bbox == NULL) |
777 |
+ |
return(0); |
778 |
+ |
|
779 |
+ |
if (VABSEQ(bbox[0], bbox[1])) { /* first run */ |
780 |
+ |
bbox[0][0] = bbox[0][1] = bbox[0][2] = FHUGE; |
781 |
+ |
bbox[1][0] = bbox[1][1] = bbox[1][2] = -FHUGE; |
782 |
+ |
} |
783 |
+ |
return(foreachFace(sc, addBBox, flreq, flexc, bbox)); |
784 |
+ |
} |
785 |
+ |
|
786 |
+ |
/* Allocate an empty scene */ |
787 |
+ |
Scene * |
788 |
+ |
newScene(void) |
789 |
+ |
{ |
790 |
+ |
Scene *sc = (Scene *)ecalloc(1, sizeof(Scene)); |
791 |
+ |
/* default group & material */ |
792 |
+ |
sc->grpname = chunk_alloc(char *, sc->grpname, sc->ngrps); |
793 |
+ |
sc->grpname[sc->ngrps++] = savqstr("DEFAULT_GROUP"); |
794 |
+ |
sc->matname = chunk_alloc(char *, sc->matname, sc->nmats); |
795 |
+ |
sc->matname[sc->nmats++] = savqstr("DEFAULT_MATERIAL"); |
796 |
+ |
|
797 |
+ |
return(sc); |
798 |
+ |
} |
799 |
+ |
|
800 |
|
/* Duplicate a scene, optionally selecting faces */ |
801 |
|
Scene * |
802 |
|
dupScene(const Scene *osc, int flreq, int flexc) |
812 |
|
sc = newScene(); |
813 |
|
for (i = 0; i < osc->ndescr; i++) |
814 |
|
addComment(sc, osc->descr[i]); |
815 |
< |
if (osc->ngrps) { |
816 |
< |
sc->grpname = (char **)emalloc(sizeof(char *) * |
817 |
< |
(osc->ngrps+(CHUNKSIZ-1))); |
818 |
< |
for (i = 0; i < osc->ngrps; i++) |
815 |
> |
if (osc->ngrps > 1) { |
816 |
> |
sc->grpname = (char **)erealloc((char *)sc->grpname, |
817 |
> |
sizeof(char *) * (osc->ngrps+(CHUNKSIZ-1))); |
818 |
> |
for (i = 1; i < osc->ngrps; i++) |
819 |
|
sc->grpname[i] = savqstr(osc->grpname[i]); |
820 |
|
sc->ngrps = osc->ngrps; |
821 |
|
} |
822 |
< |
if (osc->nmats) { |
823 |
< |
sc->matname = (char **)emalloc(sizeof(char *) * |
824 |
< |
(osc->nmats+(CHUNKSIZ-1))); |
825 |
< |
for (i = 0; i < osc->nmats; i++) |
822 |
> |
if (osc->nmats > 1) { |
823 |
> |
sc->matname = (char **)erealloc((char *)sc->matname, |
824 |
> |
sizeof(char *) * (osc->nmats+(CHUNKSIZ-1))); |
825 |
> |
for (i = 1; i < osc->nmats; i++) |
826 |
|
sc->matname[i] = savqstr(osc->matname[i]); |
827 |
|
sc->nmats = osc->nmats; |
828 |
|
} |
829 |
|
if (osc->nverts) { |
830 |
|
sc->vert = (Vertex *)emalloc(sizeof(Vertex) * |
831 |
|
(osc->nverts+(CHUNKSIZ-1))); |
832 |
< |
memcpy((void *)sc->vert, (const void *)osc->vert, |
750 |
< |
sizeof(Vertex)*osc->nverts); |
832 |
> |
memcpy(sc->vert, osc->vert, sizeof(Vertex)*osc->nverts); |
833 |
|
sc->nverts = osc->nverts; |
834 |
|
for (i = 0; i < sc->nverts; i++) |
835 |
|
sc->vert[i].vflist = NULL; |
837 |
|
if (osc->ntex) { |
838 |
|
sc->tex = (TexCoord *)emalloc(sizeof(TexCoord) * |
839 |
|
(osc->ntex+(CHUNKSIZ-1))); |
840 |
< |
memcpy((void *)sc->tex, (const void *)osc->tex, |
759 |
< |
sizeof(TexCoord)*osc->ntex); |
840 |
> |
memcpy(sc->tex, osc->tex, sizeof(TexCoord)*osc->ntex); |
841 |
|
sc->ntex = osc->ntex; |
842 |
|
} |
843 |
|
if (osc->nnorms) { |
844 |
|
sc->norm = (Normal *)emalloc(sizeof(Normal) * |
845 |
|
(osc->nnorms+CHUNKSIZ)); |
846 |
< |
memcpy((void *)sc->norm, (const void *)osc->norm, |
766 |
< |
sizeof(Normal)*osc->nnorms); |
846 |
> |
memcpy(sc->norm, osc->norm, sizeof(Normal)*osc->nnorms); |
847 |
|
sc->nnorms = osc->nnorms; |
848 |
|
} |
849 |
|
for (fo = osc->flist; fo != NULL; fo = fo->next) { |
850 |
|
if ((fo->flags & fltest) != flreq) |
851 |
|
continue; |
852 |
< |
f = (Face *)emalloc(sizeof(Face) + |
853 |
< |
sizeof(VertEnt)*(fo->nv-3)); |
774 |
< |
memcpy((void *)f, (const void *)fo, sizeof(Face) + |
775 |
< |
sizeof(VertEnt)*(fo->nv-3)); |
852 |
> |
f = (Face *)emalloc(sizeof(Face) + sizeof(VertEnt)*(fo->nv-3)); |
853 |
> |
memcpy(f, fo, sizeof(Face) + sizeof(VertEnt)*(fo->nv-3)); |
854 |
|
for (i = 0; i < f->nv; i++) |
855 |
|
add2facelist(sc, f, i); |
856 |
|
f->next = sc->flist; |
867 |
|
{ |
868 |
|
VNDX my_vlist[4]; |
869 |
|
int *vert_map = NULL; |
870 |
< |
int *norm_map = NULL; |
871 |
< |
int *tex_map = NULL; |
870 |
> |
int tex_off = 0; |
871 |
> |
int norm_off = 0; |
872 |
|
VNDX *vlist = my_vlist; |
873 |
|
int vllen = sizeof(my_vlist)/sizeof(VNDX); |
874 |
|
int cur_mat = 0; |
882 |
|
if (scsrc->nfaces <= 0) |
883 |
|
return(0); |
884 |
|
/* map vertices */ |
885 |
< |
vert_map = (int *)emalloc(sizeof(int *)*scsrc->nverts); |
885 |
> |
vert_map = (int *)emalloc(sizeof(int)*scsrc->nverts); |
886 |
|
for (i = 0; i < scsrc->nverts; i++) { |
887 |
|
const Vertex *v = scsrc->vert + i; |
888 |
|
if (v->vflist == NULL) { |
891 |
|
} |
892 |
|
vert_map[i] = addVertex(scdst, v->p[0], v->p[1], v->p[2]); |
893 |
|
} |
894 |
< |
if (scsrc->ntex > 0) /* map texture coords */ |
895 |
< |
tex_map = (int *)emalloc(sizeof(int *)*scsrc->ntex); |
896 |
< |
for (i = 0; i < scsrc->ntex; i++) { |
897 |
< |
const TexCoord *t = scsrc->tex + i; |
898 |
< |
tex_map[i] = addTexture(scdst, t->u, t->v); |
894 |
> |
tex_off = scdst->ntex; /* append texture coords */ |
895 |
> |
if (scsrc->ntex > 0) { |
896 |
> |
scdst->tex = (TexCoord *)erealloc((char *)scdst->tex, |
897 |
> |
sizeof(TexCoord)*(tex_off+scsrc->ntex+(CHUNKSIZ-1))); |
898 |
> |
memcpy(scdst->tex+tex_off, scsrc->tex, |
899 |
> |
sizeof(TexCoord)*scsrc->ntex); |
900 |
|
} |
901 |
< |
if (scsrc->nnorms > 0) /* map normals */ |
902 |
< |
norm_map = (int *)emalloc(sizeof(int *)*scsrc->nnorms); |
903 |
< |
for (i = 0; i < scsrc->nnorms; i++) { |
904 |
< |
const float *n = scsrc->norm[i]; |
905 |
< |
norm_map[i] = addNormal(scdst, n[0], n[1], n[2]); |
901 |
> |
norm_off = scdst->nnorms; /* append normals */ |
902 |
> |
if (scsrc->nnorms > 0) { |
903 |
> |
scdst->norm = (Normal *)erealloc((char *)scdst->norm, |
904 |
> |
sizeof(Normal)*(norm_off+scsrc->nnorms+(CHUNKSIZ-1))); |
905 |
> |
memcpy(scdst->norm+norm_off, scsrc->norm, |
906 |
> |
sizeof(Normal)*scsrc->nnorms); |
907 |
|
} |
908 |
|
/* add faces */ |
909 |
|
scdst->lastgrp = scdst->lastmat = 0; |
913 |
|
if (f->mat != cur_mat) |
914 |
|
setMaterial(scdst, scsrc->matname[cur_mat = f->mat]); |
915 |
|
if (f->nv > vllen) { |
916 |
< |
if (vlist == my_vlist) |
917 |
< |
vlist = (VNDX *)emalloc( |
918 |
< |
sizeof(VNDX)*(vllen = f->nv)); |
919 |
< |
else |
840 |
< |
vlist = (VNDX *)erealloc((char *)vlist, |
841 |
< |
sizeof(VNDX)*(vllen = f->nv)); |
916 |
> |
vlist = (VNDX *)( vlist == my_vlist ? |
917 |
> |
emalloc(sizeof(VNDX)*f->nv) : |
918 |
> |
erealloc((char *)vlist, sizeof(VNDX)*f->nv) ); |
919 |
> |
vllen = f->nv; |
920 |
|
} |
921 |
|
memset(vlist, 0xff, sizeof(VNDX)*f->nv); |
922 |
|
for (i = f->nv; i-- > 0; ) { |
923 |
|
if (f->v[i].vid >= 0) |
924 |
|
vlist[i][0] = vert_map[f->v[i].vid]; |
925 |
|
if (f->v[i].tid >= 0) |
926 |
< |
vlist[i][1] = tex_map[f->v[i].tid]; |
926 |
> |
vlist[i][1] = f->v[i].tid + tex_off; |
927 |
|
if (f->v[i].nid >= 0) |
928 |
< |
vlist[i][2] = norm_map[f->v[i].nid]; |
928 |
> |
vlist[i][2] = f->v[i].nid + norm_off; |
929 |
|
} |
930 |
|
fcnt += (addFace(scdst, vlist, f->nv) != NULL); |
931 |
|
} |
932 |
|
/* clean up */ |
933 |
|
if (vlist != my_vlist) efree((char *)vlist); |
856 |
– |
if (norm_map != NULL) efree((char *)norm_map); |
857 |
– |
if (tex_map != NULL) efree((char *)tex_map); |
934 |
|
efree((char *)vert_map); |
935 |
|
return(fcnt); |
936 |
|
} |
991 |
|
if (!*xfm) |
992 |
|
return(0); |
993 |
|
/* parse string into words */ |
994 |
< |
xav[0] = strcpy((char *)emalloc(strlen(xfm)+1), xfm); |
994 |
> |
xav[0] = savqstr((char *)xfm); |
995 |
|
xac = 1; i = 0; |
996 |
|
for ( ; ; ) { |
997 |
|
while (!isspace(xfm[++i])) |
1002 |
|
if (!xfm[i]) |
1003 |
|
break; |
1004 |
|
if (xac >= MAXAC-1) { |
1005 |
< |
free(xav[0]); |
1005 |
> |
freeqstr(xav[0]); |
1006 |
|
return(0); |
1007 |
|
} |
1008 |
|
xav[xac++] = xav[0] + i; |
1009 |
|
} |
1010 |
|
xav[xac] = NULL; |
1011 |
|
i = xfScene(sc, xac, xav); |
1012 |
< |
efree((char *)xav[0]); |
1012 |
> |
freeqstr(xav[0]); |
1013 |
|
return(i); |
1014 |
|
} |
1015 |
|
#undef MAXAC |
1088 |
|
if (!vmap[i]) |
1089 |
|
continue; |
1090 |
|
if (nused != i) |
1091 |
< |
memcpy((void *)sc->norm[nused], |
1016 |
< |
(void *)sc->norm[i], |
1017 |
< |
sizeof(Normal)); |
1091 |
> |
memcpy(sc->norm[nused], sc->norm[i], sizeof(Normal)); |
1092 |
|
vmap[i] = nused++; |
1093 |
|
} |
1094 |
|
if (nused == sc->nnorms) |