19 |
|
int |
20 |
|
findName(const char *nm, const char **nmlist, int n) |
21 |
|
{ |
22 |
< |
int i; |
22 |
> |
int i; |
23 |
|
|
24 |
|
for (i = n; i-- > 0; ) |
25 |
|
if (!strcmp(nmlist[i], nm)) |
31 |
|
void |
32 |
|
clearSelection(Scene *sc, int set) |
33 |
|
{ |
34 |
< |
Face *f; |
34 |
> |
Face *f; |
35 |
|
|
36 |
|
for (f = sc->flist; f != NULL; f = f->next) |
37 |
|
if (set) |
44 |
|
void |
45 |
|
selectGroup(Scene *sc, const char *gname, int invert) |
46 |
|
{ |
47 |
< |
int gid = findName(gname, (const char **)sc->grpname, sc->ngrps); |
48 |
< |
Face *f; |
47 |
> |
int gid = getGroupID(sc, gname); |
48 |
> |
Face *f; |
49 |
|
|
50 |
|
if (gid < 0) |
51 |
|
return; |
62 |
|
void |
63 |
|
selectMaterial(Scene *sc, const char *mname, int invert) |
64 |
|
{ |
65 |
< |
int mid = findName(mname, (const char **)sc->matname, sc->nmats); |
66 |
< |
Face *f; |
65 |
> |
int mid = getMaterialID(sc, mname); |
66 |
> |
Face *f; |
67 |
|
|
68 |
|
if (mid < 0) |
69 |
|
return; |
80 |
|
void |
81 |
|
invertSelection(Scene *sc) |
82 |
|
{ |
83 |
< |
Face *f; |
83 |
> |
Face *f; |
84 |
|
|
85 |
|
for (f = sc->flist; f != NULL; f = f->next) |
86 |
|
f->flags ^= FACE_SELECTED; |
90 |
|
int |
91 |
|
numberSelected(Scene *sc) |
92 |
|
{ |
93 |
< |
int nsel = 0; |
94 |
< |
Face *f; |
93 |
> |
int nsel = 0; |
94 |
> |
Face *f; |
95 |
|
|
96 |
|
for (f = sc->flist; f != NULL; f = f->next) |
97 |
|
nsel += ((f->flags & FACE_SELECTED) != 0); |
103 |
|
foreachFace(Scene *sc, int (*cb)(Scene *, Face *, void *), |
104 |
|
int flreq, int flexc, void *c_data) |
105 |
|
{ |
106 |
< |
int fltest = flreq | flexc; |
107 |
< |
int sum = 0; |
108 |
< |
Face *f; |
106 |
> |
const int fltest = flreq | flexc; |
107 |
> |
int sum = 0; |
108 |
> |
Face *f; |
109 |
|
|
110 |
|
if ((sc == NULL) | (cb == NULL)) |
111 |
|
return(0); |
123 |
|
static int |
124 |
|
remFaceTexture(Scene *sc, Face *f, void *dummy) |
125 |
|
{ |
126 |
< |
int hadTexture = 0; |
127 |
< |
int i; |
126 |
> |
int hadTexture = 0; |
127 |
> |
int i; |
128 |
|
|
129 |
|
for (i = f->nv; i-- > 0; ) { |
130 |
|
if (f->v[i].tid < 0) |
146 |
|
static int |
147 |
|
remFaceNormal(Scene *sc, Face *f, void *dummy) |
148 |
|
{ |
149 |
< |
int hadNormal = 0; |
150 |
< |
int i; |
149 |
> |
int hadNormal = 0; |
150 |
> |
int i; |
151 |
|
|
152 |
|
for (i = f->nv; i-- > 0; ) { |
153 |
|
if (f->v[i].nid < 0) |
169 |
|
static int |
170 |
|
chngFaceGroup(Scene *sc, Face *f, void *ptr) |
171 |
|
{ |
172 |
< |
int grp = *(int *)ptr; |
172 |
> |
int grp = *(int *)ptr; |
173 |
> |
|
174 |
|
if (f->grp == grp) |
175 |
|
return(0); |
176 |
|
f->grp = grp; |
181 |
|
int |
182 |
|
changeGroup(Scene *sc, const char *gname, int flreq, int flexc) |
183 |
|
{ |
184 |
< |
int grp = findName(gname, (const char **)sc->grpname, sc->ngrps); |
184 |
> |
int grp = getGroupID(sc, gname); |
185 |
> |
|
186 |
|
if (grp < 0) { |
187 |
|
sc->grpname = chunk_alloc(char *, sc->grpname, sc->ngrps); |
188 |
|
sc->grpname[grp=sc->ngrps++] = savqstr((char *)gname); |
194 |
|
static int |
195 |
|
chngFaceMaterial(Scene *sc, Face *f, void *ptr) |
196 |
|
{ |
197 |
< |
int mat = *(int *)ptr; |
197 |
> |
int mat = *(int *)ptr; |
198 |
> |
|
199 |
|
if (f->mat == mat) |
200 |
|
return(0); |
201 |
|
f->mat = mat; |
206 |
|
int |
207 |
|
changeMaterial(Scene *sc, const char *mname, int flreq, int flexc) |
208 |
|
{ |
209 |
< |
int mat = findName(mname, (const char **)sc->matname, sc->nmats); |
209 |
> |
int mat = getMaterialID(sc, mname); |
210 |
> |
|
211 |
|
if (mat < 0) { |
212 |
|
sc->matname = chunk_alloc(char *, sc->matname, sc->nmats); |
213 |
|
sc->matname[mat=sc->nmats++] = savqstr((char *)mname); |
252 |
|
static int |
253 |
|
replace_vertex(Scene *sc, int prev, int repl, double eps) |
254 |
|
{ |
255 |
< |
int repl_tex[10]; |
256 |
< |
int repl_norm[10]; |
257 |
< |
Face *f, *flast=NULL; |
258 |
< |
int i, j=0; |
255 |
> |
int repl_tex[10]; |
256 |
> |
int repl_norm[10]; |
257 |
> |
Face *f, *flast=NULL; |
258 |
> |
int i, j=0; |
259 |
|
/* check to see if it's even used */ |
260 |
|
if (sc->vert[prev].vflist == NULL) |
261 |
|
return(0); |
263 |
|
repl_tex[0] = -1; |
264 |
|
for (f = sc->vert[repl].vflist; f != NULL; f = f->v[j].fnext) { |
265 |
|
/* make sure prev isn't in there */ |
266 |
< |
for (j = 0; j < f->nv; j++) |
266 |
> |
for (j = f->nv; j-- > 0; ) |
267 |
|
if (f->v[j].vid == prev) |
268 |
|
return(0); |
269 |
< |
for (j = 0; j < f->nv; j++) |
269 |
> |
for (j = f->nv; j-- > 0; ) |
270 |
|
if (f->v[j].vid == repl) |
271 |
|
break; |
272 |
< |
if (j >= f->nv) |
272 |
> |
if (j < 0) |
273 |
|
goto linkerr; |
274 |
|
if (f->v[j].tid < 0) |
275 |
|
continue; |
294 |
|
/* get replacement normals */ |
295 |
|
repl_norm[0] = -1; |
296 |
|
for (f = sc->vert[repl].vflist; f != NULL; f = f->v[j].fnext) { |
297 |
< |
for (j = 0; j < f->nv; j++) |
297 |
> |
for (j = f->nv; j-- > 0; ) |
298 |
|
if (f->v[j].vid == repl) |
299 |
|
break; |
300 |
|
if (f->v[j].nid < 0) |
319 |
|
} |
320 |
|
/* replace occurrences of vertex */ |
321 |
|
for (f = sc->vert[prev].vflist; f != NULL; f = f->v[j].fnext) { |
322 |
< |
for (j = 0; j < f->nv; j++) |
322 |
> |
for (j = f->nv; j-- > 0; ) |
323 |
|
if (f->v[j].vid == prev) |
324 |
|
break; |
325 |
< |
if (j >= f->nv) |
325 |
> |
if (j < 0) |
326 |
|
goto linkerr; |
327 |
|
/* XXX doesn't allow for multiple references to prev in face */ |
328 |
|
f->v[j].vid = repl; /* replace vertex itself */ |
364 |
|
int |
365 |
|
coalesceVertices(Scene *sc, double eps) |
366 |
|
{ |
367 |
< |
int nelim = 0; |
368 |
< |
LUTAB myLookup; |
369 |
< |
LUENT *le; |
370 |
< |
char vertfmt[32], vertbuf[64]; |
371 |
< |
double d; |
372 |
< |
int i; |
367 |
> |
int nelim = 0; |
368 |
> |
LUTAB myLookup; |
369 |
> |
LUENT *le; |
370 |
> |
char vertfmt[32], vertbuf[64]; |
371 |
> |
double d; |
372 |
> |
int i; |
373 |
|
|
374 |
|
if (eps >= 1.) |
375 |
|
return(0); |
416 |
|
int |
417 |
|
findDuplicateFaces(Scene *sc) |
418 |
|
{ |
419 |
< |
int nchecked = 0; |
420 |
< |
int nfound = 0; |
421 |
< |
Face *f, *f1; |
422 |
< |
int vid; |
423 |
< |
int i, j; |
419 |
> |
int nchecked = 0; |
420 |
> |
int nfound = 0; |
421 |
> |
Face *f, *f1; |
422 |
> |
int vid; |
423 |
> |
int i, j; |
424 |
|
/* start fresh */ |
425 |
|
for (f = sc->flist; f != NULL; f = f->next) |
426 |
|
f->flags &= ~FACE_DUPLICATE; |
436 |
|
/* look for duplicates */ |
437 |
|
for (f1 = sc->vert[vid].vflist; f1 != NULL; |
438 |
|
f1 = f1->v[j].fnext) { |
439 |
< |
for (j = 0; j < f1->nv; j++) |
439 |
> |
for (j = f1->nv; j-- > 0; ) |
440 |
|
if (f1->v[j].vid == vid) |
441 |
|
break; |
442 |
< |
if (j >= f1->nv) |
442 |
> |
if (j < 0) |
443 |
|
break; /* missing link! */ |
444 |
|
if (f1 == f) |
445 |
|
continue; /* shouldn't happen */ |
473 |
|
int |
474 |
|
deleteFaces(Scene *sc, int flreq, int flexc) |
475 |
|
{ |
476 |
< |
int fltest = flreq | flexc; |
477 |
< |
int orig_nfaces = sc->nfaces; |
478 |
< |
Face fhead; |
479 |
< |
Face *f, *ftst; |
476 |
> |
const int fltest = flreq | flexc; |
477 |
> |
int orig_nfaces = sc->nfaces; |
478 |
> |
Face fhead; |
479 |
> |
Face *f, *ftst; |
480 |
|
|
481 |
|
fhead.next = sc->flist; |
482 |
|
f = &fhead; |
492 |
|
continue; |
493 |
|
} |
494 |
|
while (vf != NULL) { |
495 |
< |
for (j = 0; j < vf->nv; j++) |
495 |
> |
for (j = vf->nv; j-- > 0; ) |
496 |
|
if (vf->v[j].vid == vid) |
497 |
|
break; |
498 |
< |
if (j >= vf->nv) |
498 |
> |
if (j < 0) |
499 |
|
break; /* error */ |
500 |
|
if (vf->v[j].fnext == ftst) { |
501 |
|
vf->v[j].fnext = |
592 |
|
for ( ; ; ) { /* else find position */ |
593 |
|
if (fp == f) |
594 |
|
return; /* already in list */ |
595 |
< |
for (j = 0; j < fp->nv; j++) |
595 |
> |
for (j = fp->nv; j-- > 0; ) |
596 |
|
if (fp->v[j].vid == vid) |
597 |
|
break; |
598 |
< |
if (j >= fp->nv) |
598 |
> |
if (j < 0) |
599 |
|
error(CONSISTENCY, "Link error in add2facelist()"); |
600 |
|
if (fp->v[j].fnext == NULL) |
601 |
|
break; /* reached the end */ |
604 |
|
fp->v[j].fnext = f; /* append new face */ |
605 |
|
} |
606 |
|
|
603 |
– |
/* Allocate an empty scene */ |
604 |
– |
Scene * |
605 |
– |
newScene(void) |
606 |
– |
{ |
607 |
– |
Scene *sc = (Scene *)ecalloc(1, sizeof(Scene)); |
608 |
– |
/* default group & material */ |
609 |
– |
sc->grpname = chunk_alloc(char *, sc->grpname, sc->ngrps); |
610 |
– |
sc->grpname[sc->ngrps++] = savqstr("DEFAULT_GROUP"); |
611 |
– |
sc->matname = chunk_alloc(char *, sc->matname, sc->nmats); |
612 |
– |
sc->matname[sc->nmats++] = savqstr("DEFAULT_MATERIAL"); |
613 |
– |
|
614 |
– |
return(sc); |
615 |
– |
} |
616 |
– |
|
607 |
|
/* Add a vertex to a scene */ |
608 |
|
int |
609 |
|
addVertex(Scene *sc, double x, double y, double z) |
630 |
|
int |
631 |
|
addNormal(Scene *sc, double xn, double yn, double zn) |
632 |
|
{ |
633 |
< |
FVECT nrm; |
633 |
> |
FVECT nrm; |
634 |
|
|
635 |
|
nrm[0] = xn; nrm[1] = yn; nrm[2] = zn; |
636 |
|
if (normalize(nrm) == .0) |
644 |
|
void |
645 |
|
setGroup(Scene *sc, const char *nm) |
646 |
|
{ |
647 |
< |
sc->lastgrp = findName(nm, (const char **)sc->grpname, sc->ngrps); |
647 |
> |
sc->lastgrp = getGroupID(sc, nm); |
648 |
|
if (sc->lastgrp >= 0) |
649 |
|
return; |
650 |
|
sc->grpname = chunk_alloc(char *, sc->grpname, sc->ngrps); |
655 |
|
void |
656 |
|
setMaterial(Scene *sc, const char *nm) |
657 |
|
{ |
658 |
< |
sc->lastmat = findName(nm, (const char **)sc->matname, sc->nmats); |
658 |
> |
sc->lastmat = getMaterialID(sc, nm); |
659 |
|
if (sc->lastmat >= 0) |
660 |
|
return; |
661 |
|
sc->matname = chunk_alloc(char *, sc->matname, sc->nmats); |
666 |
|
Face * |
667 |
|
addFace(Scene *sc, VNDX vid[], int nv) |
668 |
|
{ |
669 |
< |
Face *f; |
670 |
< |
int i; |
669 |
> |
Face *f; |
670 |
> |
int i; |
671 |
|
|
672 |
|
if (nv < 3) |
673 |
|
return(NULL); |
700 |
|
return(f); |
701 |
|
} |
702 |
|
|
703 |
< |
/* Duplicate a scene */ |
703 |
> |
/* Allocate an empty scene */ |
704 |
|
Scene * |
705 |
< |
dupScene(const Scene *osc) |
705 |
> |
newScene(void) |
706 |
|
{ |
707 |
+ |
Scene *sc = (Scene *)ecalloc(1, sizeof(Scene)); |
708 |
+ |
/* default group & material */ |
709 |
+ |
sc->grpname = chunk_alloc(char *, sc->grpname, sc->ngrps); |
710 |
+ |
sc->grpname[sc->ngrps++] = savqstr("DEFAULT_GROUP"); |
711 |
+ |
sc->matname = chunk_alloc(char *, sc->matname, sc->nmats); |
712 |
+ |
sc->matname[sc->nmats++] = savqstr("DEFAULT_MATERIAL"); |
713 |
+ |
|
714 |
+ |
return(sc); |
715 |
+ |
} |
716 |
+ |
|
717 |
+ |
/* Duplicate a scene, optionally selecting faces */ |
718 |
+ |
Scene * |
719 |
+ |
dupScene(const Scene *osc, int flreq, int flexc) |
720 |
+ |
{ |
721 |
+ |
int fltest = flreq | flexc; |
722 |
|
Scene *sc; |
723 |
|
const Face *fo; |
724 |
|
Face *f; |
729 |
|
sc = newScene(); |
730 |
|
for (i = 0; i < osc->ndescr; i++) |
731 |
|
addComment(sc, osc->descr[i]); |
732 |
< |
if (osc->ngrps) { |
733 |
< |
sc->grpname = (char **)emalloc(sizeof(char *) * |
734 |
< |
(osc->ngrps+(CHUNKSIZ-1))); |
735 |
< |
for (i = 0; i < osc->ngrps; i++) |
732 |
> |
if (osc->ngrps > 1) { |
733 |
> |
sc->grpname = (char **)erealloc((char *)sc->grpname, |
734 |
> |
sizeof(char *) * (osc->ngrps+(CHUNKSIZ-1))); |
735 |
> |
for (i = 1; i < osc->ngrps; i++) |
736 |
|
sc->grpname[i] = savqstr(osc->grpname[i]); |
737 |
|
sc->ngrps = osc->ngrps; |
738 |
|
} |
739 |
< |
if (osc->nmats) { |
740 |
< |
sc->matname = (char **)emalloc(sizeof(char *) * |
741 |
< |
(osc->nmats+(CHUNKSIZ-1))); |
742 |
< |
for (i = 0; i < osc->nmats; i++) |
739 |
> |
if (osc->nmats > 1) { |
740 |
> |
sc->matname = (char **)erealloc((char *)sc->matname, |
741 |
> |
sizeof(char *) * (osc->nmats+(CHUNKSIZ-1))); |
742 |
> |
for (i = 1; i < osc->nmats; i++) |
743 |
|
sc->matname[i] = savqstr(osc->matname[i]); |
744 |
|
sc->nmats = osc->nmats; |
745 |
|
} |
746 |
|
if (osc->nverts) { |
747 |
|
sc->vert = (Vertex *)emalloc(sizeof(Vertex) * |
748 |
|
(osc->nverts+(CHUNKSIZ-1))); |
749 |
< |
memcpy((void *)sc->vert, (const void *)osc->vert, |
745 |
< |
sizeof(Vertex)*osc->nverts); |
749 |
> |
memcpy(sc->vert, osc->vert, sizeof(Vertex)*osc->nverts); |
750 |
|
sc->nverts = osc->nverts; |
751 |
|
for (i = 0; i < sc->nverts; i++) |
752 |
|
sc->vert[i].vflist = NULL; |
754 |
|
if (osc->ntex) { |
755 |
|
sc->tex = (TexCoord *)emalloc(sizeof(TexCoord) * |
756 |
|
(osc->ntex+(CHUNKSIZ-1))); |
757 |
< |
memcpy((void *)sc->tex, (const void *)osc->tex, |
754 |
< |
sizeof(TexCoord)*osc->ntex); |
757 |
> |
memcpy(sc->tex, osc->tex, sizeof(TexCoord)*osc->ntex); |
758 |
|
sc->ntex = osc->ntex; |
759 |
|
} |
760 |
|
if (osc->nnorms) { |
761 |
|
sc->norm = (Normal *)emalloc(sizeof(Normal) * |
762 |
|
(osc->nnorms+CHUNKSIZ)); |
763 |
< |
memcpy((void *)sc->norm, (const void *)osc->norm, |
761 |
< |
sizeof(Normal)*osc->nnorms); |
763 |
> |
memcpy(sc->norm, osc->norm, sizeof(Normal)*osc->nnorms); |
764 |
|
sc->nnorms = osc->nnorms; |
765 |
|
} |
766 |
|
for (fo = osc->flist; fo != NULL; fo = fo->next) { |
767 |
< |
f = (Face *)emalloc(sizeof(Face) + |
768 |
< |
sizeof(VertEnt)*(fo->nv-3)); |
769 |
< |
memcpy((void *)f, (const void *)fo, sizeof(Face) + |
770 |
< |
sizeof(VertEnt)*(fo->nv-3)); |
767 |
> |
if ((fo->flags & fltest) != flreq) |
768 |
> |
continue; |
769 |
> |
f = (Face *)emalloc(sizeof(Face) + sizeof(VertEnt)*(fo->nv-3)); |
770 |
> |
memcpy(f, fo, sizeof(Face) + sizeof(VertEnt)*(fo->nv-3)); |
771 |
|
for (i = 0; i < f->nv; i++) |
772 |
|
add2facelist(sc, f, i); |
773 |
|
f->next = sc->flist; |
774 |
|
sc->flist = f; |
775 |
|
sc->nfaces++; |
776 |
|
} |
777 |
+ |
deleteUnreferenced(sc); /* jetsam */ |
778 |
|
return(sc); |
779 |
|
} |
780 |
|
|
781 |
+ |
/* Add one scene to another, not checking for redundancies */ |
782 |
+ |
int |
783 |
+ |
addScene(Scene *scdst, const Scene *scsrc) |
784 |
+ |
{ |
785 |
+ |
VNDX my_vlist[4]; |
786 |
+ |
int *vert_map = NULL; |
787 |
+ |
int tex_off = 0; |
788 |
+ |
int norm_off = 0; |
789 |
+ |
VNDX *vlist = my_vlist; |
790 |
+ |
int vllen = sizeof(my_vlist)/sizeof(VNDX); |
791 |
+ |
int cur_mat = 0; |
792 |
+ |
int cur_grp = 0; |
793 |
+ |
int fcnt = 0; |
794 |
+ |
const Face *f; |
795 |
+ |
int i; |
796 |
+ |
|
797 |
+ |
if ((scdst == NULL) | (scsrc == NULL)) |
798 |
+ |
return(-1); |
799 |
+ |
if (scsrc->nfaces <= 0) |
800 |
+ |
return(0); |
801 |
+ |
/* map vertices */ |
802 |
+ |
vert_map = (int *)emalloc(sizeof(int *)*scsrc->nverts); |
803 |
+ |
for (i = 0; i < scsrc->nverts; i++) { |
804 |
+ |
const Vertex *v = scsrc->vert + i; |
805 |
+ |
if (v->vflist == NULL) { |
806 |
+ |
vert_map[i] = -1; |
807 |
+ |
continue; |
808 |
+ |
} |
809 |
+ |
vert_map[i] = addVertex(scdst, v->p[0], v->p[1], v->p[2]); |
810 |
+ |
} |
811 |
+ |
tex_off = scdst->ntex; /* append texture coords */ |
812 |
+ |
if (scsrc->ntex > 0) { |
813 |
+ |
scdst->tex = (TexCoord *)erealloc((char *)scdst->tex, |
814 |
+ |
sizeof(TexCoord)*(tex_off+scsrc->ntex+(CHUNKSIZ-1))); |
815 |
+ |
memcpy(scdst->tex+tex_off, scsrc->tex, |
816 |
+ |
sizeof(TexCoord)*scsrc->ntex); |
817 |
+ |
} |
818 |
+ |
norm_off = scdst->nnorms; /* append normals */ |
819 |
+ |
if (scsrc->nnorms > 0) { |
820 |
+ |
scdst->norm = (Normal *)erealloc((char *)scdst->norm, |
821 |
+ |
sizeof(Normal)*(norm_off+scsrc->nnorms+(CHUNKSIZ-1))); |
822 |
+ |
memcpy(scdst->norm+norm_off, scsrc->norm, |
823 |
+ |
sizeof(Normal)*scsrc->nnorms); |
824 |
+ |
} |
825 |
+ |
/* add faces */ |
826 |
+ |
scdst->lastgrp = scdst->lastmat = 0; |
827 |
+ |
for (f = scsrc->flist; f != NULL; f = f->next) { |
828 |
+ |
if (f->grp != cur_grp) |
829 |
+ |
setGroup(scdst, scsrc->grpname[cur_grp = f->grp]); |
830 |
+ |
if (f->mat != cur_mat) |
831 |
+ |
setMaterial(scdst, scsrc->matname[cur_mat = f->mat]); |
832 |
+ |
if (f->nv > vllen) { |
833 |
+ |
if (vlist == my_vlist) |
834 |
+ |
vlist = (VNDX *)emalloc( |
835 |
+ |
sizeof(VNDX)*(vllen = f->nv)); |
836 |
+ |
else |
837 |
+ |
vlist = (VNDX *)erealloc((char *)vlist, |
838 |
+ |
sizeof(VNDX)*(vllen = f->nv)); |
839 |
+ |
} |
840 |
+ |
memset(vlist, 0xff, sizeof(VNDX)*f->nv); |
841 |
+ |
for (i = f->nv; i-- > 0; ) { |
842 |
+ |
if (f->v[i].vid >= 0) |
843 |
+ |
vlist[i][0] = vert_map[f->v[i].vid]; |
844 |
+ |
if (f->v[i].tid >= 0) |
845 |
+ |
vlist[i][1] = f->v[i].tid + tex_off; |
846 |
+ |
if (f->v[i].nid >= 0) |
847 |
+ |
vlist[i][2] = f->v[i].nid + norm_off; |
848 |
+ |
} |
849 |
+ |
fcnt += (addFace(scdst, vlist, f->nv) != NULL); |
850 |
+ |
} |
851 |
+ |
/* clean up */ |
852 |
+ |
if (vlist != my_vlist) efree((char *)vlist); |
853 |
+ |
efree((char *)vert_map); |
854 |
+ |
return(fcnt); |
855 |
+ |
} |
856 |
+ |
|
857 |
|
#define MAXAC 100 |
858 |
|
|
859 |
|
/* Transform entire scene */ |
910 |
|
if (!*xfm) |
911 |
|
return(0); |
912 |
|
/* parse string into words */ |
913 |
< |
xav[0] = strcpy((char *)malloc(strlen(xfm)+1), xfm); |
913 |
> |
xav[0] = strcpy((char *)emalloc(strlen(xfm)+1), xfm); |
914 |
|
xac = 1; i = 0; |
915 |
|
for ( ; ; ) { |
916 |
|
while (!isspace(xfm[++i])) |
928 |
|
} |
929 |
|
xav[xac] = NULL; |
930 |
|
i = xfScene(sc, xac, xav); |
931 |
< |
free(xav[0]); |
931 |
> |
efree((char *)xav[0]); |
932 |
|
return(i); |
933 |
|
} |
934 |
|
#undef MAXAC |
935 |
+ |
|
936 |
+ |
/* Delete unreferenced vertices, normals, texture coords */ |
937 |
+ |
void |
938 |
+ |
deleteUnreferenced(Scene *sc) |
939 |
+ |
{ |
940 |
+ |
int *vmap; |
941 |
+ |
Face *f; |
942 |
+ |
int nused, i; |
943 |
+ |
/* allocate index map */ |
944 |
+ |
if (!sc->nverts) |
945 |
+ |
return; |
946 |
+ |
i = sc->nverts; |
947 |
+ |
if (sc->ntex > i) |
948 |
+ |
i = sc->ntex; |
949 |
+ |
if (sc->nnorms > i) |
950 |
+ |
i = sc->nnorms; |
951 |
+ |
vmap = (int *)emalloc(sizeof(int)*i); |
952 |
+ |
/* remap positions */ |
953 |
+ |
for (i = nused = 0; i < sc->nverts; i++) { |
954 |
+ |
if (sc->vert[i].vflist == NULL) { |
955 |
+ |
vmap[i] = -1; |
956 |
+ |
continue; |
957 |
+ |
} |
958 |
+ |
if (nused != i) |
959 |
+ |
sc->vert[nused] = sc->vert[i]; |
960 |
+ |
vmap[i] = nused++; |
961 |
+ |
} |
962 |
+ |
if (nused == sc->nverts) |
963 |
+ |
goto skip_pos; |
964 |
+ |
sc->vert = (Vertex *)erealloc((char *)sc->vert, |
965 |
+ |
sizeof(Vertex)*(nused+(CHUNKSIZ-1))); |
966 |
+ |
sc->nverts = nused; |
967 |
+ |
for (f = sc->flist; f != NULL; f = f->next) |
968 |
+ |
for (i = f->nv; i--; ) |
969 |
+ |
if ((f->v[i].vid = vmap[f->v[i].vid]) < 0) |
970 |
+ |
error(CONSISTENCY, |
971 |
+ |
"Link error in del_unref_verts()"); |
972 |
+ |
skip_pos: |
973 |
+ |
/* remap texture coord's */ |
974 |
+ |
if (!sc->ntex) |
975 |
+ |
goto skip_tex; |
976 |
+ |
memset((void *)vmap, 0, sizeof(int)*sc->ntex); |
977 |
+ |
for (f = sc->flist; f != NULL; f = f->next) |
978 |
+ |
for (i = f->nv; i--; ) |
979 |
+ |
if (f->v[i].tid >= 0) |
980 |
+ |
vmap[f->v[i].tid] = 1; |
981 |
+ |
for (i = nused = 0; i < sc->ntex; i++) { |
982 |
+ |
if (!vmap[i]) |
983 |
+ |
continue; |
984 |
+ |
if (nused != i) |
985 |
+ |
sc->tex[nused] = sc->tex[i]; |
986 |
+ |
vmap[i] = nused++; |
987 |
+ |
} |
988 |
+ |
if (nused == sc->ntex) |
989 |
+ |
goto skip_tex; |
990 |
+ |
sc->tex = (TexCoord *)erealloc((char *)sc->tex, |
991 |
+ |
sizeof(TexCoord)*(nused+(CHUNKSIZ-1))); |
992 |
+ |
sc->ntex = nused; |
993 |
+ |
for (f = sc->flist; f != NULL; f = f->next) |
994 |
+ |
for (i = f->nv; i--; ) |
995 |
+ |
if (f->v[i].tid >= 0) |
996 |
+ |
f->v[i].tid = vmap[f->v[i].tid]; |
997 |
+ |
skip_tex: |
998 |
+ |
/* remap normals */ |
999 |
+ |
if (!sc->nnorms) |
1000 |
+ |
goto skip_norms; |
1001 |
+ |
memset((void *)vmap, 0, sizeof(int)*sc->nnorms); |
1002 |
+ |
for (f = sc->flist; f != NULL; f = f->next) |
1003 |
+ |
for (i = f->nv; i--; ) |
1004 |
+ |
if (f->v[i].nid >= 0) |
1005 |
+ |
vmap[f->v[i].nid] = 1; |
1006 |
+ |
for (i = nused = 0; i < sc->nnorms; i++) { |
1007 |
+ |
if (!vmap[i]) |
1008 |
+ |
continue; |
1009 |
+ |
if (nused != i) |
1010 |
+ |
memcpy(sc->norm[nused], sc->norm[i], sizeof(Normal)); |
1011 |
+ |
vmap[i] = nused++; |
1012 |
+ |
} |
1013 |
+ |
if (nused == sc->nnorms) |
1014 |
+ |
goto skip_norms; |
1015 |
+ |
sc->norm = (Normal *)erealloc((char *)sc->norm, |
1016 |
+ |
sizeof(Normal)*(nused+(CHUNKSIZ-1))); |
1017 |
+ |
sc->nnorms = nused; |
1018 |
+ |
for (f = sc->flist; f != NULL; f = f->next) |
1019 |
+ |
for (i = f->nv; i--; ) |
1020 |
+ |
if (f->v[i].nid >= 0) |
1021 |
+ |
f->v[i].nid = vmap[f->v[i].nid]; |
1022 |
+ |
skip_norms: |
1023 |
+ |
/* clean up */ |
1024 |
+ |
efree((char *)vmap); |
1025 |
+ |
} |
1026 |
|
|
1027 |
|
/* Free a scene */ |
1028 |
|
void |