| 9 |
|
* Created by Greg Ward on Thu Feb 12 2004. |
| 10 |
|
*/ |
| 11 |
|
|
| 12 |
< |
#include <stdio.h> |
| 13 |
< |
#include <stdlib.h> |
| 14 |
< |
#include <string.h> |
| 12 |
> |
#include "paths.h" |
| 13 |
|
#include "rterror.h" |
| 14 |
|
#include "objutil.h" |
| 15 |
|
|
| 18 |
– |
/* Delete unreferenced vertices */ |
| 19 |
– |
static void |
| 20 |
– |
del_unref_verts(Scene *sc) |
| 21 |
– |
{ |
| 22 |
– |
int *vmap; |
| 23 |
– |
Face *f; |
| 24 |
– |
int nused, i; |
| 25 |
– |
/* allocate index map */ |
| 26 |
– |
if (!sc->nverts) |
| 27 |
– |
return; |
| 28 |
– |
i = sc->nverts; |
| 29 |
– |
if (sc->ntex > i) |
| 30 |
– |
i = sc->ntex; |
| 31 |
– |
if (sc->nnorms > i) |
| 32 |
– |
i = sc->nnorms; |
| 33 |
– |
vmap = (int *)emalloc(sizeof(int)*i); |
| 34 |
– |
/* remap positions */ |
| 35 |
– |
for (i = nused = 0; i < sc->nverts; i++) { |
| 36 |
– |
if (sc->vert[i].vflist == NULL) { |
| 37 |
– |
vmap[i] = -1; |
| 38 |
– |
continue; |
| 39 |
– |
} |
| 40 |
– |
if (nused != i) |
| 41 |
– |
sc->vert[nused] = sc->vert[i]; |
| 42 |
– |
vmap[i] = nused++; |
| 43 |
– |
} |
| 44 |
– |
if (nused == sc->nverts) |
| 45 |
– |
goto skip_pos; |
| 46 |
– |
sc->vert = (Vertex *)erealloc((char *)sc->vert, |
| 47 |
– |
sizeof(Vertex)*(nused+(CHUNKSIZ-1))); |
| 48 |
– |
sc->nverts = nused; |
| 49 |
– |
for (f = sc->flist; f != NULL; f = f->next) |
| 50 |
– |
for (i = f->nv; i--; ) |
| 51 |
– |
if ((f->v[i].vid = vmap[f->v[i].vid]) < 0) |
| 52 |
– |
error(CONSISTENCY, |
| 53 |
– |
"Link error in del_unref_verts()"); |
| 54 |
– |
skip_pos: |
| 55 |
– |
/* remap texture coord's */ |
| 56 |
– |
if (!sc->ntex) |
| 57 |
– |
goto skip_tex; |
| 58 |
– |
memset((void *)vmap, 0, sizeof(int)*sc->ntex); |
| 59 |
– |
for (f = sc->flist; f != NULL; f = f->next) |
| 60 |
– |
for (i = f->nv; i--; ) |
| 61 |
– |
if (f->v[i].tid >= 0) |
| 62 |
– |
vmap[f->v[i].tid] = 1; |
| 63 |
– |
for (i = nused = 0; i < sc->ntex; i++) { |
| 64 |
– |
if (!vmap[i]) |
| 65 |
– |
continue; |
| 66 |
– |
if (nused != i) |
| 67 |
– |
sc->tex[nused] = sc->tex[i]; |
| 68 |
– |
vmap[i] = nused++; |
| 69 |
– |
} |
| 70 |
– |
if (nused == sc->ntex) |
| 71 |
– |
goto skip_tex; |
| 72 |
– |
sc->tex = (TexCoord *)erealloc((char *)sc->tex, |
| 73 |
– |
sizeof(TexCoord)*(nused+(CHUNKSIZ-1))); |
| 74 |
– |
sc->ntex = nused; |
| 75 |
– |
for (f = sc->flist; f != NULL; f = f->next) |
| 76 |
– |
for (i = f->nv; i--; ) |
| 77 |
– |
if (f->v[i].tid >= 0) |
| 78 |
– |
f->v[i].tid = vmap[f->v[i].tid]; |
| 79 |
– |
skip_tex: |
| 80 |
– |
/* remap normals */ |
| 81 |
– |
if (!sc->nnorms) |
| 82 |
– |
goto skip_norms; |
| 83 |
– |
memset((void *)vmap, 0, sizeof(int)*sc->nnorms); |
| 84 |
– |
for (f = sc->flist; f != NULL; f = f->next) |
| 85 |
– |
for (i = f->nv; i--; ) |
| 86 |
– |
if (f->v[i].nid >= 0) |
| 87 |
– |
vmap[f->v[i].nid] = 1; |
| 88 |
– |
for (i = nused = 0; i < sc->nnorms; i++) { |
| 89 |
– |
if (!vmap[i]) |
| 90 |
– |
continue; |
| 91 |
– |
if (nused != i) |
| 92 |
– |
memcpy((void *)sc->norm[nused], |
| 93 |
– |
(void *)sc->norm[i], |
| 94 |
– |
sizeof(Normal)); |
| 95 |
– |
vmap[i] = nused++; |
| 96 |
– |
} |
| 97 |
– |
if (nused == sc->nnorms) |
| 98 |
– |
goto skip_norms; |
| 99 |
– |
sc->norm = (Normal *)erealloc((char *)sc->norm, |
| 100 |
– |
sizeof(Normal)*(nused+(CHUNKSIZ-1))); |
| 101 |
– |
sc->nnorms = nused; |
| 102 |
– |
for (f = sc->flist; f != NULL; f = f->next) |
| 103 |
– |
for (i = f->nv; i--; ) |
| 104 |
– |
if (f->v[i].nid >= 0) |
| 105 |
– |
f->v[i].nid = vmap[f->v[i].nid]; |
| 106 |
– |
skip_norms: |
| 107 |
– |
/* clean up */ |
| 108 |
– |
efree((char *)vmap); |
| 109 |
– |
} |
| 110 |
– |
|
| 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 */ |
| 101 |
|
/* write out each object group */ |
| 102 |
|
for (i = 0; i < sc->ngrps; i++) { |
| 103 |
|
if (verbose) |
| 104 |
< |
fprintf(stderr, " Writing faces %s \r", |
| 104 |
> |
fprintf(stderr, " Writing faces %-50s\r", |
| 105 |
|
sc->grpname[i]); |
| 106 |
|
write_group(sc, i, fp); |
| 107 |
|
} |
| 109 |
|
error(SYSTEM, "Error flushing .OBJ output"); |
| 110 |
|
return(-1); |
| 111 |
|
} |
| 112 |
+ |
if (verbose) |
| 113 |
+ |
fprintf(stderr, "Wrote %d faces \n", |
| 114 |
+ |
sc->nfaces); |
| 115 |
|
return(sc->nfaces); |
| 116 |
|
} |
| 117 |
|
|
| 119 |
|
int |
| 120 |
|
writeOBJ(Scene *sc, const char *fspec) |
| 121 |
|
{ |
| 214 |
– |
extern char *progname; |
| 122 |
|
FILE *fp; |
| 123 |
|
int n; |
| 124 |
|
|
| 129 |
|
if ((fp = popen(fspec+1, "w")) == NULL) { |
| 130 |
|
sprintf(errmsg, "%s: cannot execute", fspec); |
| 131 |
|
error(SYSTEM, errmsg); |
| 132 |
< |
return(0); |
| 132 |
> |
return(-1); |
| 133 |
|
} |
| 134 |
|
} else |
| 135 |
|
#endif |
| 136 |
|
if ((fp = fopen(fspec, "w")) == NULL) { |
| 137 |
|
sprintf(errmsg, "%s: cannot open for writing", fspec); |
| 138 |
|
error(SYSTEM, errmsg); |
| 139 |
< |
return(0); |
| 139 |
> |
return(-1); |
| 140 |
|
} |
| 141 |
|
/* start off header */ |
| 142 |
|
fprintf(fp, "# Wavefront .OBJ file created by %s\n#\n", progname); |
| 143 |
|
n = toOBJ(sc, fp); /* write scene */ |
| 144 |
|
#if POPEN_SUPPORT |
| 145 |
< |
if (fspec[0] == '!') |
| 146 |
< |
pclose(fp); |
| 147 |
< |
else |
| 145 |
> |
if (fspec[0] == '!') { |
| 146 |
> |
if (pclose(fp)) { |
| 147 |
> |
sprintf(errmsg, "%s: error writing to command\n", fspec); |
| 148 |
> |
error(SYSTEM, errmsg); |
| 149 |
> |
return(-1); |
| 150 |
> |
} |
| 151 |
> |
} else |
| 152 |
|
#endif |
| 153 |
< |
fclose(fp); /* close it */ |
| 153 |
> |
fclose(fp); /* close it (already flushed) */ |
| 154 |
|
return(n); |
| 155 |
|
} |