| 13 |
|
#include "rterror.h" |
| 14 |
|
#include "objutil.h" |
| 15 |
|
|
| 16 |
– |
/* Delete unreferenced vertices */ |
| 17 |
– |
static void |
| 18 |
– |
del_unref_verts(Scene *sc) |
| 19 |
– |
{ |
| 20 |
– |
int *vmap; |
| 21 |
– |
Face *f; |
| 22 |
– |
int nused, i; |
| 23 |
– |
/* allocate index map */ |
| 24 |
– |
if (!sc->nverts) |
| 25 |
– |
return; |
| 26 |
– |
i = sc->nverts; |
| 27 |
– |
if (sc->ntex > i) |
| 28 |
– |
i = sc->ntex; |
| 29 |
– |
if (sc->nnorms > i) |
| 30 |
– |
i = sc->nnorms; |
| 31 |
– |
vmap = (int *)emalloc(sizeof(int)*i); |
| 32 |
– |
/* remap positions */ |
| 33 |
– |
for (i = nused = 0; i < sc->nverts; i++) { |
| 34 |
– |
if (sc->vert[i].vflist == NULL) { |
| 35 |
– |
vmap[i] = -1; |
| 36 |
– |
continue; |
| 37 |
– |
} |
| 38 |
– |
if (nused != i) |
| 39 |
– |
sc->vert[nused] = sc->vert[i]; |
| 40 |
– |
vmap[i] = nused++; |
| 41 |
– |
} |
| 42 |
– |
if (nused == sc->nverts) |
| 43 |
– |
goto skip_pos; |
| 44 |
– |
sc->vert = (Vertex *)erealloc((char *)sc->vert, |
| 45 |
– |
sizeof(Vertex)*(nused+(CHUNKSIZ-1))); |
| 46 |
– |
sc->nverts = nused; |
| 47 |
– |
for (f = sc->flist; f != NULL; f = f->next) |
| 48 |
– |
for (i = f->nv; i--; ) |
| 49 |
– |
if ((f->v[i].vid = vmap[f->v[i].vid]) < 0) |
| 50 |
– |
error(CONSISTENCY, |
| 51 |
– |
"Link error in del_unref_verts()"); |
| 52 |
– |
skip_pos: |
| 53 |
– |
/* remap texture coord's */ |
| 54 |
– |
if (!sc->ntex) |
| 55 |
– |
goto skip_tex; |
| 56 |
– |
memset((void *)vmap, 0, sizeof(int)*sc->ntex); |
| 57 |
– |
for (f = sc->flist; f != NULL; f = f->next) |
| 58 |
– |
for (i = f->nv; i--; ) |
| 59 |
– |
if (f->v[i].tid >= 0) |
| 60 |
– |
vmap[f->v[i].tid] = 1; |
| 61 |
– |
for (i = nused = 0; i < sc->ntex; i++) { |
| 62 |
– |
if (!vmap[i]) |
| 63 |
– |
continue; |
| 64 |
– |
if (nused != i) |
| 65 |
– |
sc->tex[nused] = sc->tex[i]; |
| 66 |
– |
vmap[i] = nused++; |
| 67 |
– |
} |
| 68 |
– |
if (nused == sc->ntex) |
| 69 |
– |
goto skip_tex; |
| 70 |
– |
sc->tex = (TexCoord *)erealloc((char *)sc->tex, |
| 71 |
– |
sizeof(TexCoord)*(nused+(CHUNKSIZ-1))); |
| 72 |
– |
sc->ntex = nused; |
| 73 |
– |
for (f = sc->flist; f != NULL; f = f->next) |
| 74 |
– |
for (i = f->nv; i--; ) |
| 75 |
– |
if (f->v[i].tid >= 0) |
| 76 |
– |
f->v[i].tid = vmap[f->v[i].tid]; |
| 77 |
– |
skip_tex: |
| 78 |
– |
/* remap normals */ |
| 79 |
– |
if (!sc->nnorms) |
| 80 |
– |
goto skip_norms; |
| 81 |
– |
memset((void *)vmap, 0, sizeof(int)*sc->nnorms); |
| 82 |
– |
for (f = sc->flist; f != NULL; f = f->next) |
| 83 |
– |
for (i = f->nv; i--; ) |
| 84 |
– |
if (f->v[i].nid >= 0) |
| 85 |
– |
vmap[f->v[i].nid] = 1; |
| 86 |
– |
for (i = nused = 0; i < sc->nnorms; i++) { |
| 87 |
– |
if (!vmap[i]) |
| 88 |
– |
continue; |
| 89 |
– |
if (nused != i) |
| 90 |
– |
memcpy((void *)sc->norm[nused], |
| 91 |
– |
(void *)sc->norm[i], |
| 92 |
– |
sizeof(Normal)); |
| 93 |
– |
vmap[i] = nused++; |
| 94 |
– |
} |
| 95 |
– |
if (nused == sc->nnorms) |
| 96 |
– |
goto skip_norms; |
| 97 |
– |
sc->norm = (Normal *)erealloc((char *)sc->norm, |
| 98 |
– |
sizeof(Normal)*(nused+(CHUNKSIZ-1))); |
| 99 |
– |
sc->nnorms = nused; |
| 100 |
– |
for (f = sc->flist; f != NULL; f = f->next) |
| 101 |
– |
for (i = f->nv; i--; ) |
| 102 |
– |
if (f->v[i].nid >= 0) |
| 103 |
– |
f->v[i].nid = vmap[f->v[i].nid]; |
| 104 |
– |
skip_norms: |
| 105 |
– |
/* clean up */ |
| 106 |
– |
efree((char *)vmap); |
| 107 |
– |
} |
| 108 |
– |
|
| 16 |
|
/* Write out header comments */ |
| 17 |
|
static void |
| 18 |
|
write_header(Scene *sc, FILE *fp) |
| 93 |
|
return(0); |
| 94 |
|
if (verbose) |
| 95 |
|
fputs(" Removing unreferenced vertices\r", stderr); |
| 96 |
< |
del_unref_verts(sc); /* clean up, first */ |
| 96 |
> |
deleteUnreferenced(sc); /* clean up, first */ |
| 97 |
|
if (verbose) |
| 98 |
|
fputs(" Writing vertices \r", stderr); |
| 99 |
|
write_header(sc, fp); /* write out header */ |
| 119 |
|
int |
| 120 |
|
writeOBJ(Scene *sc, const char *fspec) |
| 121 |
|
{ |
| 215 |
– |
extern char *progname; |
| 122 |
|
FILE *fp; |
| 123 |
|
int n; |
| 124 |
|
|