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 */ |
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 |
|
|
130 |
|
if ((fp = popen(fspec+1, "w")) == NULL) { |
131 |
|
sprintf(errmsg, "%s: cannot execute", fspec); |
132 |
|
error(SYSTEM, errmsg); |
133 |
< |
return(0); |
133 |
> |
return(-1); |
134 |
|
} |
135 |
|
} else |
136 |
|
#endif |
137 |
|
if ((fp = fopen(fspec, "w")) == NULL) { |
138 |
|
sprintf(errmsg, "%s: cannot open for writing", fspec); |
139 |
|
error(SYSTEM, errmsg); |
140 |
< |
return(0); |
140 |
> |
return(-1); |
141 |
|
} |
142 |
|
/* start off header */ |
143 |
|
fprintf(fp, "# Wavefront .OBJ file created by %s\n#\n", progname); |
144 |
|
n = toOBJ(sc, fp); /* write scene */ |
145 |
|
#if POPEN_SUPPORT |
146 |
< |
if (fspec[0] == '!') |
147 |
< |
pclose(fp); |
148 |
< |
else |
146 |
> |
if (fspec[0] == '!') { |
147 |
> |
if (pclose(fp)) { |
148 |
> |
sprintf(errmsg, "%s: error writing to command\n", fspec); |
149 |
> |
error(SYSTEM, errmsg); |
150 |
> |
return(-1); |
151 |
> |
} |
152 |
> |
} else |
153 |
|
#endif |
154 |
< |
fclose(fp); /* close it */ |
154 |
> |
fclose(fp); /* close it (already flushed) */ |
155 |
|
return(n); |
156 |
|
} |