1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
/*
* obj2model.c
*
* Turn Wavefront OBJ files into Thrust3D models
*
* (c) 2008 Thomas White <taw27@cam.ac.uk>
*
* thrust3d - a silly game
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "utils.c"
#define MAX_VERTICES 65536
int main(int argc, char *argv[]) {
FILE *fh;
FILE *out;
float vtmp[3*MAX_VERTICES];
float vntmp[3*MAX_VERTICES];
float textmp[2*MAX_VERTICES];
int v_used[MAX_VERTICES];
int vn_used[MAX_VERTICES];
int tex_used[MAX_VERTICES];
int n_vtmp, n_vntmp, n_textmp;
int nprev;
int i, v_unused, vn_unused, tex_unused;
fh = fopen(argv[1], "r");
if ( fh == NULL ) {
fprintf(stderr, "Couldn't open '%s'\n", argv[1]);
return 1;
}
for ( i=0; i<MAX_VERTICES; i++ ) {
v_used[i] = 5;
vn_used[i] = 5;
tex_used[i] = 5;
}
/* Zip through and find all the vertices */
n_vtmp = 0;
n_textmp = 0;
n_vntmp = 0;
while ( !feof(fh) ) {
char line[1024];
float x, y, z;
size_t s;
fgets(line, 1023, fh);
s = 0;
for ( ; s<strlen(line); s++ ) {
if ( line[s] != ' ' ) break;
}
if ( line[s] == '#' ) {
continue;
}
if ( sscanf(line+s, "v %f %f %f\n", &x, &y, &z) == 3 ) {
vtmp[3*n_vtmp+0] = x;
vtmp[3*n_vtmp+1] = y;
vtmp[3*n_vtmp+2] = z;
v_used[n_vtmp] = 0;
n_vtmp++;
continue;
}
if ( sscanf(line+s, "vn %f %f %f\n", &x, &y, &z) == 3 ) {
vntmp[3*n_vntmp+0] = x;
vntmp[3*n_vntmp+1] = y;
vntmp[3*n_vntmp+2] = z;
vn_used[n_vntmp] = 0;
n_vntmp++;
continue;
}
if ( sscanf(line+s, "vt %f %f %f\n", &x, &y, &z) == 3 ) {
textmp[2*n_textmp+0] = x;
textmp[2*n_textmp+1] = y;
tex_used[n_textmp] = 0;
n_textmp++;
continue;
}
}
/* Go through again and look for faces */
rewind(fh);
out = fopen(argv[2], "w");
fprintf(out, "# %s\n", argv[1]);
fprintf(out, "\n");
nprev = 0;
while ( !feof(fh) ) {
char line[1024];
char **bits;
int n, i;
fgets(line, 1023, fh);
n = assplode(line, " \t\r\n", &bits, ASSPLODE_NONE);
/* Read in a face */
if ( strcmp(bits[0], "f") == 0 ) {
int nthis;
/* Change primitive type if this face has a different number of vertices */
nthis = n-1;
if ( nthis != nprev ) {
if ( nprev != 0 ) fprintf(out, "\n");
if ( nthis == 4 ) {
fprintf(out, "QUADS\n");
} else if ( nthis == 3 ) {
fprintf(out, "TRIANGLES\n");
} else {
fprintf(out, "POLYGONS\n");
}
nprev = nthis;
} else {
//fprintf(out, "#\n");
}
/* For each vertex... */
for ( i=1; i<n; i++ ) {
char **sp;
int np, j, nslash;
nslash = 0;
for ( j=0; j<strlen(bits[i]); j++ ) {
if ( bits[i][j] == '/' ) nslash++;
}
if ( nslash == 2 ) {
int vnum, tnum, nnum;
np = assplode(bits[i], "/", &sp, ASSPLODE_DUPS);
if ( np != 3 ) {
printf("Error!\n");
continue;
}
vnum = atoi(sp[0])-1;
tnum = atoi(sp[1])-1;
nnum = atoi(sp[2])-1;
if ( vnum >= n_vtmp ) {
fprintf(stderr, "Vertex index is too high (%i/%i)\n", vnum, n_vtmp);
continue;
}
if ( nnum >= n_vntmp ) {
fprintf(stderr, "Normal index is too high (%i/%i)\n", nnum, n_vntmp);
continue;
}
fprintf(out, "%+8.3f %+8.3f %+8.3f %8.3f %8.3f %+8.3f %+8.3f %+8.3f\n",
vtmp[3*vnum+0], vtmp[3*vnum+1], vtmp[3*vnum+2],
textmp[2*tnum+0], textmp[2*tnum+1],
vntmp[3*nnum+0], vntmp[3*nnum+1], vntmp[3*nnum+2]);
v_used[vnum] = 1;
vn_used[nnum] = 1;
tex_used[tnum] = 1;
free(sp[0]);
free(sp[1]);
free(sp[2]);
free(sp);
} else if ( nslash == 0 ) {
int vnum;
vnum = atoi(bits[i])-1;
fprintf(out, "%+8.3f %+8.3f %+8.3f %8.3f %8.3f\n",
vtmp[3*vnum+0], vtmp[3*vnum+1], vtmp[3*vnum+2],
0.0, 0.0);
v_used[vnum] = 1;
}
}
}
for ( i=0; i<n; i++ ) free(bits[i]);
free(bits);
}
fprintf(out, "\n");
fclose(fh);
fclose(out);
v_unused = 0;
vn_unused = 0;
tex_unused = 0;
for ( i=0; i<MAX_VERTICES; i++ ) {
if ( v_used[i] == 0 ) {
v_unused++;
}
if ( vn_used[i] == 0 ) {
vn_unused++;
}
if ( tex_used[i] == 0 ) {
tex_unused++;
}
}
printf("%i vertices (%i unused)\n", n_vtmp, v_unused);
printf("%i normals (%i unused)\n", n_vntmp, vn_unused);
printf("%i texcoords (%i unused)\n", n_textmp, tex_unused);
return 0;
}
|