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); |
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; |
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; |
608 |
|
Scene * |
609 |
|
newScene(void) |
610 |
|
{ |
611 |
< |
Scene *sc = (Scene *)ecalloc(1, sizeof(Scene)); |
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"); |
644 |
|
int |
645 |
|
addNormal(Scene *sc, double xn, double yn, double zn) |
646 |
|
{ |
647 |
< |
FVECT nrm; |
647 |
> |
FVECT nrm; |
648 |
|
|
649 |
|
nrm[0] = xn; nrm[1] = yn; nrm[2] = zn; |
650 |
|
if (normalize(nrm) == .0) |
658 |
|
void |
659 |
|
setGroup(Scene *sc, const char *nm) |
660 |
|
{ |
661 |
< |
sc->lastgrp = findName(nm, (const char **)sc->grpname, sc->ngrps); |
661 |
> |
sc->lastgrp = getGroupID(sc, nm); |
662 |
|
if (sc->lastgrp >= 0) |
663 |
|
return; |
664 |
|
sc->grpname = chunk_alloc(char *, sc->grpname, sc->ngrps); |
669 |
|
void |
670 |
|
setMaterial(Scene *sc, const char *nm) |
671 |
|
{ |
672 |
< |
sc->lastmat = findName(nm, (const char **)sc->matname, sc->nmats); |
672 |
> |
sc->lastmat = getMaterialID(sc, nm); |
673 |
|
if (sc->lastmat >= 0) |
674 |
|
return; |
675 |
|
sc->matname = chunk_alloc(char *, sc->matname, sc->nmats); |
680 |
|
Face * |
681 |
|
addFace(Scene *sc, VNDX vid[], int nv) |
682 |
|
{ |
683 |
< |
Face *f; |
684 |
< |
int i; |
683 |
> |
Face *f; |
684 |
> |
int i; |
685 |
|
|
686 |
|
if (nv < 3) |
687 |
|
return(NULL); |
783 |
|
return(sc); |
784 |
|
} |
785 |
|
|
786 |
+ |
/* Add one scene to another, not checking for redundancies */ |
787 |
+ |
int |
788 |
+ |
addScene(Scene *scdst, const Scene *scsrc) |
789 |
+ |
{ |
790 |
+ |
VNDX my_vlist[4]; |
791 |
+ |
int *vert_map = NULL; |
792 |
+ |
int *norm_map = NULL; |
793 |
+ |
int *tex_map = NULL; |
794 |
+ |
VNDX *vlist = my_vlist; |
795 |
+ |
int vllen = sizeof(my_vlist)/sizeof(VNDX); |
796 |
+ |
int cur_mat = 0; |
797 |
+ |
int cur_grp = 0; |
798 |
+ |
int fcnt = 0; |
799 |
+ |
const Face *f; |
800 |
+ |
int i; |
801 |
+ |
|
802 |
+ |
if ((scdst == NULL) | (scsrc == NULL)) |
803 |
+ |
return(-1); |
804 |
+ |
if (scsrc->nfaces <= 0) |
805 |
+ |
return(0); |
806 |
+ |
/* map vertices */ |
807 |
+ |
vert_map = (int *)emalloc(sizeof(int *)*scsrc->nverts); |
808 |
+ |
for (i = 0; i < scsrc->nverts; i++) { |
809 |
+ |
const Vertex *v = scsrc->vert + i; |
810 |
+ |
if (v->vflist == NULL) { |
811 |
+ |
vert_map[i] = -1; |
812 |
+ |
continue; |
813 |
+ |
} |
814 |
+ |
vert_map[i] = addVertex(scdst, v->p[0], v->p[1], v->p[2]); |
815 |
+ |
} |
816 |
+ |
if (scsrc->ntex > 0) /* map texture coords */ |
817 |
+ |
tex_map = (int *)emalloc(sizeof(int *)*scsrc->ntex); |
818 |
+ |
for (i = 0; i < scsrc->ntex; i++) { |
819 |
+ |
const TexCoord *t = scsrc->tex + i; |
820 |
+ |
tex_map[i] = addTexture(scdst, t->u, t->v); |
821 |
+ |
} |
822 |
+ |
if (scsrc->nnorms > 0) /* map normals */ |
823 |
+ |
norm_map = (int *)emalloc(sizeof(int *)*scsrc->nnorms); |
824 |
+ |
for (i = 0; i < scsrc->nnorms; i++) { |
825 |
+ |
const float *n = scsrc->norm[i]; |
826 |
+ |
norm_map[i] = addNormal(scdst, n[0], n[1], n[2]); |
827 |
+ |
} |
828 |
+ |
/* add faces */ |
829 |
+ |
scdst->lastgrp = scdst->lastmat = 0; |
830 |
+ |
for (f = scsrc->flist; f != NULL; f = f->next) { |
831 |
+ |
if (f->grp != cur_grp) |
832 |
+ |
setGroup(scdst, scsrc->grpname[cur_grp = f->grp]); |
833 |
+ |
if (f->mat != cur_mat) |
834 |
+ |
setMaterial(scdst, scsrc->matname[cur_mat = f->mat]); |
835 |
+ |
if (f->nv > vllen) { |
836 |
+ |
if (vlist == my_vlist) |
837 |
+ |
vlist = (VNDX *)emalloc( |
838 |
+ |
sizeof(VNDX)*(vllen = f->nv)); |
839 |
+ |
else |
840 |
+ |
vlist = (VNDX *)erealloc((char *)vlist, |
841 |
+ |
sizeof(VNDX)*(vllen = f->nv)); |
842 |
+ |
} |
843 |
+ |
memset(vlist, 0xff, sizeof(VNDX)*f->nv); |
844 |
+ |
for (i = f->nv; i-- > 0; ) { |
845 |
+ |
if (f->v[i].vid >= 0) |
846 |
+ |
vlist[i][0] = vert_map[f->v[i].vid]; |
847 |
+ |
if (f->v[i].tid >= 0) |
848 |
+ |
vlist[i][1] = tex_map[f->v[i].tid]; |
849 |
+ |
if (f->v[i].nid >= 0) |
850 |
+ |
vlist[i][2] = norm_map[f->v[i].nid]; |
851 |
+ |
} |
852 |
+ |
fcnt += (addFace(scdst, vlist, f->nv) != NULL); |
853 |
+ |
} |
854 |
+ |
/* clean up */ |
855 |
+ |
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); |
858 |
+ |
efree((char *)vert_map); |
859 |
+ |
return(fcnt); |
860 |
+ |
} |
861 |
+ |
|
862 |
|
#define MAXAC 100 |
863 |
|
|
864 |
|
/* Transform entire scene */ |
915 |
|
if (!*xfm) |
916 |
|
return(0); |
917 |
|
/* parse string into words */ |
918 |
< |
xav[0] = strcpy((char *)malloc(strlen(xfm)+1), xfm); |
918 |
> |
xav[0] = strcpy((char *)emalloc(strlen(xfm)+1), xfm); |
919 |
|
xac = 1; i = 0; |
920 |
|
for ( ; ; ) { |
921 |
|
while (!isspace(xfm[++i])) |
933 |
|
} |
934 |
|
xav[xac] = NULL; |
935 |
|
i = xfScene(sc, xac, xav); |
936 |
< |
free(xav[0]); |
936 |
> |
efree((char *)xav[0]); |
937 |
|
return(i); |
938 |
|
} |
939 |
|
#undef MAXAC |