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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void parseface(char *facedata,int *varray,int *narray,int *tarray,int position, int hasuv, int hasnormals) {
char workstr[10];
int readpos = 0;
int i;
for(i = 0; i < 3; i++) {
int pos = 0;
while(readpos < strlen(facedata) && facedata[readpos] != '/') {
workstr[pos] = facedata[readpos];
pos++;
readpos++;
}
workstr[pos] = 0;
pos = 0;
readpos++;
// -1 to compensate for wavefront file starting on 1 and not 0
if(i == 0) {
varray[position] = atoi(workstr) - 1;
} else if((i == 1) && (hasuv > 0)) {
tarray[position] = atoi(workstr) - 1;
} else if((i == 2) && (hasnormals > 0)) {
narray[position] = atoi(workstr) - 1;
}
}
}
void loadobj(char *objfilename, char *openobj, char *outputfile) {
FILE * pFile;
pFile = fopen(objfilename, "r");
char *buf;
buf = (char*)malloc(1024);
char *result;
// foundobj is true if an object with a certain name is found
// Set foundobj to true if we are loading all objects
int foundobj = 0;
if(strcmp(openobj, "ALL") == 0) {
foundobj = 1;
}
int vertcount = 0;
int normalcount = 0;
int texturecount = 0;
int facecount = 0;
int polycount = 0;
int quadcount = 0;
int trianglecount = 0;
// Global Variables for Object
float *vertexlist;
float *uvlist;
float *normallist;
int *trianglelist;
int *quadlist;
char *resultp1, *resultp2, *resultp3, *resultp4, *resultp5;
if(pFile) {
printf("Msg: Success opening: %s!\n", objfilename);
while(fgets(buf, 1024, pFile) != NULL) {
if(buf[0] == '#') {
// Comment - Do Nothing
} else {
result = strtok(buf, " \n\t");
if(result != NULL) {
if(strcmp(result, "v") == 0) {
if(foundobj) {
//Increase vertex counter
vertcount++;
}
} else if(strcmp(result, "vt") == 0) {
if(foundobj) {
// Increase texture coordinate counter
texturecount++;
}
} else if(strcmp(result, "vn") == 0) {
if(foundobj) {
// Increase normal counter
normalcount++;
}
} else if(strcmp(result, "f") == 0) {
if(foundobj) {
// Increase face counter
facecount++;
resultp1 = strtok(NULL, " \n\t");
resultp2 = strtok(NULL, " \n\t");
resultp3 = strtok(NULL, " \n\t");
resultp4 = strtok(NULL, " \n\t");
resultp5 = strtok(NULL, " \n\t");
if((resultp4 != NULL)&&(resultp5 == NULL)) {
quadcount++;
} else if((resultp3 != NULL)&&(resultp4 == NULL)) {
trianglecount++;
} else {
polycount++;
}
}
} else if(strcmp(result, "g") == 0) {
if(result=strtok(NULL, " \n\t")) {
if(!strcmp(openobj, result)) {
foundobj = 1;
}
} else {
printf("Error: Failed to read object name\n");
}
} else {
//printf("O: %s \n",buf);
}
}
}
}
if(polycount > 0) {
printf("Warning: Object %s in file %s contains faces that will be ignored, with more than 4 vertices.!\n", objfilename, openobj);
}
if(((trianglecount + quadcount) > 0)) {
// Set foundobj to true if we are loading all objects
if(strcmp(openobj, "ALL")) {
foundobj = 1;
}
// Seek to beginning again
fseek(pFile, 0, SEEK_SET);
// Allocate local buffers
float *localvertexlist = (float *) malloc (sizeof(float) * vertcount * 3);
float *localnormallist = (float *) malloc (sizeof(float) * normalcount * 3);
float *localuvlist = (float *) malloc (sizeof(float) * texturecount * 2);
int *localquadlist = (int *) malloc (sizeof(int) * quadcount * 4);
int *localquadlistuv = (int *) malloc (sizeof(int) * quadcount * 4);
int *localquadlistnormal = (int *) malloc (sizeof(int) * quadcount * 4);
int *localtrianglelist = (int *) malloc (sizeof(int) * trianglecount * 3);
int *localtrianglelistuv = (int *) malloc (sizeof(int) * trianglecount * 3);
int *localtrianglelistnormal = (int *) malloc (sizeof(int) * trianglecount * 3);
// Reset counters for second pass
trianglecount = 0;
quadcount = 0;
normalcount = 0;
vertcount = 0;
texturecount = 0;
while(fgets(buf, 1024, pFile) != NULL) {
if(buf[0] == '#') {
// Comment - Do Nothing
} else {
result = strtok(buf, " \n\t");
if(result != NULL) {
if(strcmp(result, "v") == 0) {
if(foundobj) {
result = strtok(NULL, " \n\t");
localvertexlist[(vertcount * 3)] = atof(result);
result = strtok(NULL, " \n\t");
localvertexlist[(vertcount * 3) + 1] = atof(result);
result = strtok(NULL, " \n\t");
localvertexlist[(vertcount * 3) + 2] = atof(result);
vertcount++;
}
} else if(strcmp(result, "vt") == 0) {
if(foundobj) {
result = strtok(NULL, " \n\t");
localuvlist[(texturecount * 2)] = atof(result);
result = strtok(NULL, " \n\t");
localuvlist[(texturecount * 2) + 1] = atof(result);
texturecount++;
}
} else if(strcmp(result, "vn") == 0) {
if(foundobj) {
result = strtok(NULL, " \n\t");
localnormallist[(normalcount * 3)] = atof(result);
result = strtok(NULL, " \n\t");
localnormallist[(normalcount * 3) + 1] = atof(result);
result = strtok(NULL, " \n\t");
localnormallist[(normalcount * 3) + 2] = atof(result);
normalcount++;
}
} else if(strcmp(result, "f") == 0) {
if(foundobj) {
resultp1 = strtok(NULL, " \n\t");
resultp2 = strtok(NULL, " \n\t");
resultp3 = strtok(NULL, " \n\t");
resultp4 = strtok(NULL, " \n\t");
resultp5 = strtok(NULL, " \n\t");
if((resultp4 != NULL)&&(resultp5 == NULL)) {
// Parse all 4 points in quad
parseface(resultp1, localquadlist, localquadlistnormal, localquadlistuv, quadcount * 4, texturecount, normalcount);
parseface(resultp2, localquadlist, localquadlistnormal, localquadlistuv, (quadcount * 4) + 1, texturecount, normalcount);
parseface(resultp3, localquadlist, localquadlistnormal, localquadlistuv, (quadcount * 4) + 2, texturecount, normalcount);
parseface(resultp4, localquadlist, localquadlistnormal, localquadlistuv, (quadcount * 4) + 3, texturecount, normalcount);
quadcount++;
} else if((resultp3 != NULL)&&(resultp4 == NULL)) {
// Parse all 3 points in triangle
parseface(resultp1, localtrianglelist, localtrianglelistnormal, localtrianglelistuv, trianglecount * 3, texturecount, normalcount);
parseface(resultp2, localtrianglelist, localtrianglelistnormal, localtrianglelistuv, (trianglecount * 3) + 1, texturecount, normalcount);
parseface(resultp3, localtrianglelist, localtrianglelistnormal, localtrianglelistuv, (trianglecount * 3) + 2, texturecount, normalcount);
trianglecount++;
}
}
} else if(strcmp(result, "g") == 0) {
if(result = strtok(NULL, " \n\t")) {
if(!strcmp(openobj, result)) {
foundobj = 1;
}
}
}
}
}
}
int i = 0;
// Allocate buffers
vertexlist = (float *) malloc (sizeof(float) * vertcount * 3);
normallist = (float *) malloc (sizeof(float) * vertcount * 3);
uvlist = (float *) malloc (sizeof(float) * vertcount * 2);
trianglelist = (int *) malloc (sizeof(int) * trianglecount * 3);
quadlist = (int *) malloc (sizeof(int) * quadcount * 4);
// Copy data from Per Face model to Per Vertex model first for triangles and then for quads
// If there are triangle normals, copy triangle normal data
if(normalcount > 0) {
printf("Msg: Copying Triangle Normals: %i / %i !\n", normalcount, trianglecount);
for(i = 0; i < trianglecount * 3; i++) {
// Normal X
normallist[localtrianglelist[i] * 3] = localnormallist[localtrianglelistnormal[i] * 3];
// Normal Y
normallist[(localtrianglelist[i] * 3 ) + 1 ] = localnormallist[(localtrianglelistnormal[i] * 3) + 1 ];
// Normal Z
normallist[(localtrianglelist[i] * 3) + 2] = localnormallist[(localtrianglelistnormal[i] * 3) + 2];
}
for(i = 0; i < quadcount * 4; i++) {
// Normal X
normallist[localquadlist[i] * 3] = localnormallist[localquadlistnormal[i] * 3];
// Normal Y
normallist[(localquadlist[i] * 3) + 1] = localnormallist[(localquadlistnormal[i] * 3) + 1];
// Normal Z
normallist[(localquadlist[i] * 3) + 2] = localnormallist[(localquadlistnormal[i] * 3) + 2];
}
} else {
printf("Msg: No Triangle Normals to Copy\n");
}
// If there are triangle uv coordinates, copy uv coordinate data
if(texturecount > 0) {
printf("Msg: Copying Triangle UV Coordinates: %i / %i !\n", texturecount, trianglecount);
for(i = 0; i < trianglecount * 3; i++) {
// U Coordinate
uvlist[localtrianglelist[i] * 3] = localuvlist[localtrianglelistuv[i] * 3];
// V Coordinate
uvlist[(localtrianglelist[i] * 3) + 1] = localuvlist[(localtrianglelistuv[i] * 3) + 1];
}
for(i = 0;i < quadcount * 4; i++) {
// U Coordinate
uvlist[localquadlist[i] * 3] = localuvlist[localquadlistuv[i] * 3];
// V Coordinate
uvlist[(localquadlist[i] * 3) + 1] = localuvlist[(localquadlistuv[i] * 3) + 1];
}
} else {
printf("Msg: No Triangle UV Coordinates to Copy\n");
}
// Same for Quads
printf("--------------------------------\n");
printf("Saving JSON Object file: %s\n", outputfile);
printf("--------------------------------\n");
// Process data to JSON
printf("{\n");
printf("\"vertexPositions\" : [");
for(i = 0; i < (vertcount * 3); i++) {
if(i < ((vertcount * 3) - 1)) {
printf("%f,", localvertexlist[i]);
} else {
printf("%f", localvertexlist[i]);
}
}
printf("],\n");
printf("\"vertexNormals\" : [");
if(normalcount > 0) {
for(i = 0; i < (vertcount * 3); i++) {
if(i < ((vertcount * 3) - 1)) {
printf("%f,", normallist[i]);
} else {
printf("%f", normallist[i]);
}
}
}
printf("],\n");
printf("\"vertexTextureCoords\" : [");
if(texturecount > 0) {
for(i = 0; i < (vertcount * 2); i++){
if(i < ((vertcount * 2) - 1)) {
printf("%f,", uvlist[i]);
} else {
printf("%f", uvlist[i]);
}
}
}
printf("],\n");
printf("\"indices\" : [");
// Triangle indices
for(i = 0; i < (trianglecount * 3); i++) {
if(i < ((trianglecount * 3) - 1)) {
printf("%i,", localtrianglelist[i]);
} else {
printf("%i", localtrianglelist[i]);
}
}
// Quad indices
for(i = 0; i < quadcount; i++) {
if(i == (quadcount - 1)) {
printf("%i,%i,%i,%i,%i,%i,", localquadlist[i], localquadlist[i + 1], localquadlist[i + 2], localquadlist[i + 2], localquadlist[i + 3], localquadlist[i]);
} else {
printf("%i,%i,%i,%i,%i,%i ", localquadlist[i], localquadlist[i + 1], localquadlist[i + 2], localquadlist[i + 2], localquadlist[i + 3], localquadlist[i]);
}
}
printf("]\n");
printf("}\n");
// Free buffers
free(localvertexlist);
free(localnormallist);
free(localuvlist);
free(localtrianglelist);
free(localtrianglelistuv);
free(localtrianglelistnormal);
free(localquadlist);
free(localquadlistuv);
free(localquadlistnormal);
} else {
// Multigon
printf("Error: Object did not contain any triangles or quads!\n");
}
fclose(pFile);
if(!foundobj) {
printf("Error: Failed to find object %s!\n", openobj);
} else {
printf("Msg: Sucessful Parsing of Object %s in File %s\nMsg: %i vertices %i normals %i texture coordinates %i faces (%i triangles %i quads %i polys)\n", openobj, objfilename, vertcount, normalcount, texturecount, facecount, trianglecount, quadcount, polycount);
}
} else {
printf("Error: Failed to open %s!\n", objfilename);
}
free(buf);
}
int main(int argc, char** argv) {
if(argc < 3) {
printf("ERROR: not enough arguments. must have LOADFILE and SAVEFILE and if you want to process only a specific object, the name of object to process\n");
return 1;
}
printf("--------------------------------\n");
printf("Loading Wavefront Object file: %s\n", argv[1]);
printf("--------------------------------\n");
if(argc > 3) {
printf("Msg: Loading Object: %s\n", argv[3]);
loadobj(argv[1], argv[3], argv[2]);
} else {
printf("Msg: Loading all Objects\n");
loadobj(argv[1], "ALL", argv[2]);
}
return 0;
}
|