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