68
58
char *resultp1,*resultp2,*resultp3,*resultp4,*resultp5;
71
printf("Msg: Success opening: %s!\n",objfilename);
73
while ( fgets(buf,1024,pFile) != NULL ){
75
// Comment - Do Nothing
61
printf("Msg: Success opening: %s!\n", objfilename);
62
while(fgets(buf, 1024, pFile) != NULL) {
64
// Comment - Do Nothing
77
66
result=strtok(buf," \n\t");
79
if(strcmp(result,"v")==0){
81
// Increase vertex counter
68
if(strcmp(result, "v") ==0) {
70
//Increase vertex counter
84
}else if(strcmp(result,"vt")==0){
73
} else if(strcmp(result, "vt") ==0) {
86
75
// Increase texture coordinate counter
90
}else if(strcmp(result,"vn")==0){
78
} else if(strcmp(result, "vn") ==0) {
92
80
// Increase normal counter
95
}else if(strcmp(result,"f")==0){
83
} else if(strcmp(result, "f") ==0) {
97
85
// Increase face counter
99
resultp1=strtok(NULL," \n\t");
100
resultp2=strtok(NULL," \n\t");
101
resultp3=strtok(NULL," \n\t");
102
resultp4=strtok(NULL," \n\t");
103
resultp5=strtok(NULL," \n\t");
104
if((resultp4!=NULL)&&(resultp5==NULL)){
87
resultp1 = strtok(NULL, " \n\t");
88
resultp2 = strtok(NULL, " \n\t");
89
resultp3 = strtok(NULL, " \n\t");
90
resultp4 = strtok(NULL, " \n\t");
91
resultp5 = strtok(NULL, " \n\t");
92
if((resultp4 != NULL)&&(resultp5 == NULL)) {
106
}else if((resultp3!=NULL)&&(resultp4==NULL)){
94
} else if((resultp3 != NULL)&&(resultp4 == NULL)) {
112
}else if(strcmp(result,"g")==0){
113
if(result=strtok(NULL," \n\t")){
114
if(!strcmp(openobj,result)){
100
} else if(strcmp(result, "g") == 0) {
101
if(result=strtok(NULL, " \n\t")) {
102
if(!strcmp(openobj, result)) {
118
printf("Error: Failed to read object name\n");
106
printf("Error: Failed to read object name\n");
121
// printf("O: %s \n",buf);
109
//printf("O: %s \n",buf);
128
printf("Warning: Object %s in file %s contains faces that will be ignored, with more than 4 vertices.!\n",objfilename,openobj);
116
printf("Warning: Object %s in file %s contains faces that will be ignored, with more than 4 vertices.!\n", objfilename, openobj);
131
if(((trianglecount+quadcount)>0)){
119
if(((trianglecount + quadcount) > 0)) {
133
120
// Set foundobj to true if we are loading all objects
134
if (strcmp(openobj,"ALL")) foundobj=1;
121
if(strcmp(openobj, "ALL")) {
136
125
// Seek to beginning again
137
fseek( pFile, 0, SEEK_SET);
126
fseek(pFile, 0, SEEK_SET);
139
128
// Allocate local buffers
140
float *localvertexlist=(float *)malloc(sizeof(float)*vertcount*3);
141
float *localnormallist=(float *)malloc(sizeof(float)*normalcount*3);
142
float *localuvlist=(float *)malloc(sizeof(float)*texturecount*2);
143
int *localquadlist=(int *)malloc(sizeof(int)*quadcount*4);
144
int *localquadlistuv=(int *)malloc(sizeof(int)*quadcount*4);
145
int *localquadlistnormal=(int *)malloc(sizeof(int)*quadcount*4);
146
int *localtrianglelist=(int *)malloc(sizeof(int)*trianglecount*3);
147
int *localtrianglelistuv=(int *)malloc(sizeof(int)*trianglecount*3);
148
int *localtrianglelistnormal=(int *)malloc(sizeof(int)*trianglecount*3);
129
float *localvertexlist =(float *) malloc (sizeof(float) * vertcount * 3);
130
float *localnormallist =(float *) malloc (sizeof(float) * normalcount * 3);
131
float *localuvlist = (float *) malloc (sizeof(float) * texturecount * 2);
132
int *localquadlist = (int *) malloc (sizeof(int) * quadcount * 4);
133
int *localquadlistuv = (int *) malloc (sizeof(int) * quadcount * 4);
134
int *localquadlistnormal = (int *) malloc (sizeof(int) * quadcount * 4);
135
int *localtrianglelist = (int *) malloc (sizeof(int) * trianglecount * 3);
136
int *localtrianglelistuv = (int *) malloc (sizeof(int) * trianglecount * 3);
137
int *localtrianglelistnormal = (int *) malloc (sizeof(int) * trianglecount * 3);
150
139
// Reset counters for second pass
157
while ( fgets(buf,1024,pFile) != NULL ){
160
// Comment - Do Nothing
163
//-------------------------
164
result = strtok(buf," \n\t");
166
if(strcmp(result,"v")==0){
168
result=strtok(NULL," \n\t");
169
localvertexlist[(vertcount*3)]=atof(result);
170
result=strtok(NULL," \n\t");
171
localvertexlist[(vertcount*3)+1]=atof(result);
172
result=strtok(NULL," \n\t");
173
localvertexlist[(vertcount*3)+2]=atof(result);
146
while(fgets(buf, 1024, pFile) != NULL) {
148
// Comment - Do Nothing
150
result = strtok(buf, " \n\t");
152
if(strcmp(result, "v") ==0) {
154
result = strtok(NULL, " \n\t");
155
localvertexlist[(vertcount * 3)] = atof(result);
156
result = strtok(NULL, " \n\t");
157
localvertexlist[(vertcount * 3) + 1] = atof(result);
158
result = strtok(NULL, " \n\t");
159
localvertexlist[(vertcount * 3) + 2] = atof(result);
176
}else if(strcmp(result,"vt")==0){
178
result=strtok(NULL," \n\t");
179
localuvlist[(texturecount*2)]=atof(result);
180
result=strtok(NULL," \n\t");
181
localuvlist[(texturecount*2)+1]=atof(result);
184
}else if(strcmp(result,"vn")==0){
186
result=strtok(NULL," \n\t");
187
localnormallist[(normalcount*3)]=atof(result);
188
result=strtok(NULL," \n\t");
189
localnormallist[(normalcount*3)+1]=atof(result);
190
result=strtok(NULL," \n\t");
191
localnormallist[(normalcount*3)+2]=atof(result);
162
} else if(strcmp(result, "vt") ==0) {
164
result = strtok(NULL, " \n\t");
165
localuvlist[(texturecount * 2)] = atof(result);
166
result = strtok(NULL, " \n\t");
167
localuvlist[(texturecount * 2) + 1] = atof(result);
170
} else if(strcmp(result, "vn") ==0) {
172
result = strtok(NULL, " \n\t");
173
localnormallist[(normalcount * 3)] = atof(result);
174
result = strtok(NULL, " \n\t");
175
localnormallist[(normalcount * 3) + 1] = atof(result);
176
result = strtok(NULL, " \n\t");
177
localnormallist[(normalcount * 3) + 2] = atof(result);
195
}else if(strcmp(result,"f")==0){
197
resultp1=strtok(NULL," \n\t");
198
resultp2=strtok(NULL," \n\t");
199
resultp3=strtok(NULL," \n\t");
200
resultp4=strtok(NULL," \n\t");
201
resultp5=strtok(NULL," \n\t");
202
if((resultp4!=NULL)&&(resultp5==NULL)){
180
} else if(strcmp(result, "f") ==0) {
182
resultp1 = strtok(NULL, " \n\t");
183
resultp2 = strtok(NULL, " \n\t");
184
resultp3 = strtok(NULL, " \n\t");
185
resultp4 = strtok(NULL, " \n\t");
186
resultp5 = strtok(NULL, " \n\t");
187
if((resultp4 != NULL)&&(resultp5 == NULL)) {
203
188
// Parse all 4 points in quad
204
parseface(resultp1,localquadlist,localquadlistnormal,localquadlistuv,quadcount*4,texturecount,normalcount);
205
parseface(resultp2,localquadlist,localquadlistnormal,localquadlistuv,(quadcount*4)+1,texturecount,normalcount);
206
parseface(resultp3,localquadlist,localquadlistnormal,localquadlistuv,(quadcount*4)+2,texturecount,normalcount);
207
parseface(resultp4,localquadlist,localquadlistnormal,localquadlistuv,(quadcount*4)+3,texturecount,normalcount);
189
parseface(resultp1, localquadlist, localquadlistnormal, localquadlistuv, quadcount * 4, texturecount, normalcount);
190
parseface(resultp2, localquadlist, localquadlistnormal, localquadlistuv, (quadcount * 4) + 1, texturecount, normalcount);
191
parseface(resultp3, localquadlist, localquadlistnormal, localquadlistuv, (quadcount * 4) + 2, texturecount, normalcount);
192
parseface(resultp4, localquadlist, localquadlistnormal, localquadlistuv, (quadcount * 4) + 3, texturecount, normalcount);
211
}else if((resultp3!=NULL)&&(resultp4==NULL)){
194
} else if((resultp3 != NULL)&&(resultp4 == NULL)) {
213
195
// Parse all 3 points in triangle
214
parseface(resultp1,localtrianglelist,localtrianglelistnormal,localtrianglelistuv,trianglecount*3,texturecount,normalcount);
215
parseface(resultp2,localtrianglelist,localtrianglelistnormal,localtrianglelistuv,(trianglecount*3)+1,texturecount,normalcount);
216
parseface(resultp3,localtrianglelist,localtrianglelistnormal,localtrianglelistuv,(trianglecount*3)+2,texturecount,normalcount);
196
parseface(resultp1, localtrianglelist, localtrianglelistnormal, localtrianglelistuv, trianglecount * 3, texturecount, normalcount);
197
parseface(resultp2, localtrianglelist, localtrianglelistnormal, localtrianglelistuv, (trianglecount * 3) + 1, texturecount, normalcount);
198
parseface(resultp3, localtrianglelist, localtrianglelistnormal, localtrianglelistuv, (trianglecount * 3) + 2, texturecount, normalcount);
221
}else if(strcmp(result,"g")==0){
222
if(result=strtok(NULL," \n\t")){
223
if(!strcmp(openobj,result)){
202
} else if(strcmp(result, "g") == 0) {
203
if(result = strtok(NULL, " \n\t")) {
204
if(!strcmp(openobj, result)) {
229
//-------------------------
237
213
// Allocate buffers
238
vertexlist=(float *)malloc(sizeof(float)*vertcount*3);
239
normallist=(float *)malloc(sizeof(float)*vertcount*3);
240
uvlist=(float *)malloc(sizeof(float)*vertcount*2);
241
trianglelist=(int *)malloc(sizeof(int)*trianglecount*3);
242
quadlist=(int *)malloc(sizeof(int)*quadcount*4);
214
vertexlist = (float *) malloc (sizeof(float) * vertcount * 3);
215
normallist = (float *) malloc (sizeof(float) * vertcount * 3);
216
uvlist = (float *) malloc (sizeof(float) * vertcount * 2);
217
trianglelist = (int *) malloc (sizeof(int) * trianglecount * 3);
218
quadlist = (int *) malloc (sizeof(int) * quadcount * 4);
244
220
// Copy data from Per Face model to Per Vertex model first for triangles and then for quads
246
221
// If there are triangle normals, copy triangle normal data
248
printf("Msg: Copying Triangle Normals: %i / %i !\n",normalcount,trianglecount);
249
for(i=0;i<trianglecount*3;i++){
251
normallist[localtrianglelist[i]*3]=localnormallist[localtrianglelistnormal[i]*3];
253
normallist[(localtrianglelist[i]*3)+1]=localnormallist[(localtrianglelistnormal[i]*3)+1];
255
normallist[(localtrianglelist[i]*3)+2]=localnormallist[(localtrianglelistnormal[i]*3)+2];
257
for(i=0;i<quadcount*4;i++){
259
normallist[localquadlist[i]*3]=localnormallist[localquadlistnormal[i]*3];
261
normallist[(localquadlist[i]*3)+1]=localnormallist[(localquadlistnormal[i]*3)+1];
263
normallist[(localquadlist[i]*3)+2]=localnormallist[(localquadlistnormal[i]*3)+2];
222
if(normalcount > 0) {
223
printf("Msg: Copying Triangle Normals: %i / %i !\n", normalcount, trianglecount);
224
for(i = 0; i < trianglecount * 3; i++) {
226
normallist[localtrianglelist[i] * 3] = localnormallist[localtrianglelistnormal[i] * 3];
228
normallist[(localtrianglelist[i] * 3 ) + 1 ] = localnormallist[(localtrianglelistnormal[i] * 3) + 1 ];
230
normallist[(localtrianglelist[i] * 3) + 2] = localnormallist[(localtrianglelistnormal[i] * 3) + 2];
232
for(i = 0; i < quadcount * 4; i++) {
234
normallist[localquadlist[i] * 3] = localnormallist[localquadlistnormal[i] * 3];
236
normallist[(localquadlist[i] * 3) + 1] = localnormallist[(localquadlistnormal[i] * 3) + 1];
238
normallist[(localquadlist[i] * 3) + 2] = localnormallist[(localquadlistnormal[i] * 3) + 2];
266
241
printf("Msg: No Triangle Normals to Copy\n");
269
// If there are triangle uv coordinates, copy uv coordinate data
271
printf("Msg: Copying Triangle UV Coordinates: %i / %i !\n",texturecount,trianglecount);
272
for(i=0;i<trianglecount*3;i++){
274
uvlist[localtrianglelist[i]*3]=localuvlist[localtrianglelistuv[i]*3];
276
uvlist[(localtrianglelist[i]*3)+1]=localuvlist[(localtrianglelistuv[i]*3)+1];
278
for(i=0;i<quadcount*4;i++){
280
uvlist[localquadlist[i]*3]=localuvlist[localquadlistuv[i]*3];
282
uvlist[(localquadlist[i]*3)+1]=localuvlist[(localquadlistuv[i]*3)+1];
244
// If there are triangle uv coordinates, copy uv coordinate data
245
if(texturecount > 0) {
246
printf("Msg: Copying Triangle UV Coordinates: %i / %i !\n", texturecount, trianglecount);
247
for(i = 0; i < trianglecount * 3; i++) {
249
uvlist[localtrianglelist[i] * 3] = localuvlist[localtrianglelistuv[i] * 3];
251
uvlist[(localtrianglelist[i] * 3) + 1] = localuvlist[(localtrianglelistuv[i] * 3) + 1];
253
for(i = 0;i < quadcount * 4; i++) {
255
uvlist[localquadlist[i] * 3] = localuvlist[localquadlistuv[i] * 3];
257
uvlist[(localquadlist[i] * 3) + 1] = localuvlist[(localquadlistuv[i] * 3) + 1];
285
260
printf("Msg: No Triangle UV Coordinates to Copy\n");
291
264
printf("--------------------------------\n");
292
265
printf("Saving JSON Object file: %s\n", outputfile);
293
266
printf("--------------------------------\n");
295
268
// Process data to JSON
298
270
printf("\"vertexPositions\" : [");
299
for(i=0;i<(vertcount*3);i++){
300
if(i<((vertcount*3)-1)) {
301
printf("%f,",localvertexlist[i]);
303
printf("%f",localvertexlist[i]);
271
for(i = 0; i < (vertcount * 3); i++) {
272
if(i < ((vertcount * 3) - 1)) {
273
printf("%f,", localvertexlist[i]);
275
printf("%f", localvertexlist[i]);
308
279
printf("\"vertexNormals\" : [");
310
for(i=0;i<(vertcount*3);i++){
311
if(i<((vertcount*3)-1)) {
312
printf("%f,",normallist[i]);
314
printf("%f",normallist[i]);
280
if(normalcount > 0) {
281
for(i = 0; i < (vertcount * 3); i++) {
282
if(i < ((vertcount * 3) - 1)) {
283
printf("%f,", normallist[i]);
285
printf("%f", normallist[i]);
320
290
printf("\"vertexTextureCoords\" : [");
322
for(i=0;i<(vertcount*2);i++){
323
if(i<((vertcount*2)-1)) {
324
printf("%f,",uvlist[i]);
326
printf("%f",uvlist[i]);
291
if(texturecount > 0) {
292
for(i = 0; i < (vertcount * 2); i++){
293
if(i < ((vertcount * 2) - 1)) {
294
printf("%f,", uvlist[i]);
296
printf("%f", uvlist[i]);
332
301
printf("\"indices\" : [");
334
303
// Triangle indices
335
for(i=0;i<(trianglecount*3);i++){
336
if(i<((trianglecount*3)-1)) {
337
printf("%i,",localtrianglelist[i]);
339
printf("%i",localtrianglelist[i]);
304
for(i = 0; i < (trianglecount * 3); i++) {
305
if(i < ((trianglecount * 3) - 1)) {
306
printf("%i,", localtrianglelist[i]);
308
printf("%i", localtrianglelist[i]);
344
for(i=0;i<quadcount;i++){
345
if(i==(quadcount-1)) {
346
printf("%i,%i,%i,%i,%i,%i,",localquadlist[i],localquadlist[i+1],localquadlist[i+2],localquadlist[i+2],localquadlist[i+3],localquadlist[i]);
348
printf("%i,%i,%i,%i,%i,%i ",localquadlist[i],localquadlist[i+1],localquadlist[i+2],localquadlist[i+2],localquadlist[i+3],localquadlist[i]);
313
for(i = 0; i < quadcount; i++) {
314
if(i == (quadcount - 1)) {
315
printf("%i,%i,%i,%i,%i,%i,", localquadlist[i], localquadlist[i + 1], localquadlist[i + 2], localquadlist[i + 2], localquadlist[i + 3], localquadlist[i]);
317
printf("%i,%i,%i,%i,%i,%i ", localquadlist[i], localquadlist[i + 1], localquadlist[i + 2], localquadlist[i + 2], localquadlist[i + 3], localquadlist[i]);