-
Notifications
You must be signed in to change notification settings - Fork 0
/
modelParser.c
426 lines (384 loc) · 12 KB
/
modelParser.c
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#include "renderer.h"
// ignores until delimiter
// returns false if end of file was reached
b32 ignore(FILE* file, char delim)
{
char ch = fgetc(file);
while(ch != delim && ch != EOF)
{
ch = fgetc(file);
}
return (ch != EOF);
}
void skip(FILE* file, char delim)
{
char ch = fgetc(file);
while(ch == delim && ch != EOF)
{
ch = fgetc(file);
}
ungetc(ch,file);
}
// TODO: can overflow
int readToken(FILE* file, char* buffer, int size)
{
char ch = fgetc(file);
int charIndex = 0;
if(ch == EOF)
printf("END OF FILE! \n");
while(ch != ' ' && ch != '\n' && ch != EOF && charIndex < size)
{
buffer[charIndex] = ch;
charIndex++;
ch = fgetc(file);
}
ungetc(ch, file);
buffer[charIndex] = '\0';
return charIndex;
}
char readc(FILE* file)
{
long offset = ftell(file);
char c1 = fgetc(file);
fseek ( file , offset , SEEK_SET );
return c1;
}
void getModelStats(char const* fileName, int* verts, int* faces/*, int& normals*/)
{
FILE* file = fopen(fileName, "r");
if(file == NULL)
{
printf("File %s not found! \n", fileName);
return;
}
*verts= 0;
*faces= 0;
int texcoords = 0;
char ch;
while( ( ch = fgetc(file) ) != EOF )
{
int floatIndex = 0;
switch(ch)
{
case '#':
ignore(file,'\n');
break;
case 'v':
ch = fgetc(file);
if(ch == ' ')
{
ignore(file,'\n');
//*verts++;
}
else if(ch == 'n')
{
ignore(file,'\n');
//normals++;
}
else if(ch == 't')
{
ignore(file,'\n');
texcoords++;
}
break;
case 'f':
ch = fgetc(file);
if(ch == ' ')
{
while(ch != '\n' &&
ch != EOF)
{
char index[50];
skip(file,' ');
readToken(file,index,50); // TODO: no need to use the data, so...
floatIndex++;
ch = readc(file);
}
assert(floatIndex == 3 || floatIndex == 4);
*faces+=floatIndex-2;
ch = fgetc(file);
}
break;
default:
ignore(file,'\n');
break;
}
}
printf("tc %d\n",texcoords);
fclose(file);
}
Mesh* loadObjMesh(const char* fileName)
{
printf("starting mesh loading\n");
FILE* file = fopen(fileName, "r");
if(file == NULL)
{
printf("File %s not found!\n", fileName);
return NULL;
}
char ch;
int verts = 0;
int faces = 0;
int normals = 0;
getModelStats(fileName,&verts,&faces/*,normals*/);
printf("%d", verts);
normals = verts;
const void* modelData = malloc(verts*sizeof(Vec4)+verts*sizeof(Vec3)+verts*sizeof(Vec2)+faces*3*sizeof(u16));
const Vec2* texCoordBuffer = (Vec2*)malloc(verts*sizeof(Vec2));
char* dataCursor = (char*)modelData;
int mode = -1;
while( ( ch = fgetc(file) ) != EOF )
{
int floatIndex = 0;
int elCounter = 0;
switch(ch)
{
case '#':
ignore(file,'\n');
break;
case 'v':
switch(fgetc(file))
{
case ' ':
if(mode != 0)
{
mode = 0;
dataCursor = (char*)modelData;
elCounter = 0;
//printf("started reading vertices\n");
}
else
{
elCounter++;
}
while(ch != '\n' &&
ch != EOF)
{
char vert[50];
skip(file,' ');
readToken(file,vert,50);
*(r32*)dataCursor = strtof(vert,NULL);
dataCursor += 4;
floatIndex++;
ch = readc(file);
}
ch = fgetc(file);
while(floatIndex < 4)
{
*(r32*)dataCursor = 1.0f;
dataCursor += 4;
floatIndex++;
}
break;
case 'n':
if(mode != 1)
{
mode = 1;
dataCursor = (char*)modelData+verts*sizeof(Vec4);
elCounter = 0;
//printf("started reading normals/n");
}
else
{
elCounter++;
}
//ignore(file,'\n');
floatIndex = 0;
//float norms[3];
while(ch != '\n' &&
ch != EOF)
{
char vert[50];
skip(file,' ');
readToken(file,vert,50);
//*(float*)dataCursor = strtof(vert,NULL);
*(r32*)dataCursor = 0.0f;
dataCursor += 4;
//norms[floatIndex] = strtof(vert,NULL);
floatIndex++;
ch = readc(file);
}
assert(floatIndex == 3);
//printf("normal %f %f %f\n",norms[0],norms[1],norms[2]);
ch = fgetc(file);
break;
case 't':
if(mode != 3)
{
mode = 3;
dataCursor = (char*)texCoordBuffer;
elCounter = 0;
//printf("started reading normals/n");
}
else
{
elCounter++;
}
floatIndex = 0;
while(ch != '\n' &&
ch != EOF)
{
char vert[50];
skip(file,' ');
readToken(file,vert,50);
//*(r32*)dataCursor = 0.0f;
//dataCursor += sizeof(r32);
floatIndex++;
ch = readc(file);
}
assert(floatIndex == 2);
ch = fgetc(file);
break;
}
break;
case 'f':
if(fgetc(file) == ' ')
{
if(mode != 2)
{
mode = 2;
dataCursor = (char*)modelData+verts*sizeof(Vec4)+verts*sizeof(Vec3)+verts*sizeof(Vec2);
elCounter = 0;
//printf("started reading faces\n");
}
int floatIndex = 0;
u16 indices[4];
//u16 texIndices[4];
while(ch != '\n' &&
ch != EOF)
{
char index[50];
skip(file,' ');
readToken(file,index,50);
indices[floatIndex] = (u16)(atoi(index)-1); // we need 0 based indices
int slashes = 0;
//int locations[2];
for(u32 i=0;i < sizeof(index);++i)
{
if(index[i] == '/')
{
//locations[slashes] = i;
slashes++;
if(slashes >= 2)
break;
}
}
/*if(slashes > 0)
texIndices[floatIndex] = (u16)(atoi(&index[locations[0]]));*/
floatIndex++;
ch = readc(file);
}
switch(floatIndex)
{
case 3:
//printf("face poly %hu %hu %hu\n",indices[0],indices[1],indices[2]);
memcpy(dataCursor,indices,sizeof(u16)*3);
dataCursor += 6;
elCounter++;
break;
case 4:
//printf("face poly %hu %hu %hu %hu\n",indices[0],indices[1],indices[2], indices[3]);
memcpy(dataCursor,indices,sizeof(u16)*3);
memcpy(dataCursor+6,indices,sizeof(u16));
memcpy(dataCursor+8,&indices[2],sizeof(u16)*2);
dataCursor += 12;
elCounter+=2;
break;
default:
assert(false); // invalid number of facesS
}
ch = fgetc(file);
}
break;
case ' ':
break;
default:
ch = fgetc(file);
while(ch != '\n' && ch != EOF)
{
ch = fgetc(file);
}
break;
}
}
#if 1
char* pp = (char*)modelData;
for(int i = 0; i < verts; i++)
{
printf("vert2 %f %f %f %f\n",((r32*)pp)[0],((r32*)pp)[1],((r32*)pp)[2],((r32*)pp)[3]);
pp+=16;
}
for(int i = 0; i < verts; i++)
{
printf("norm2 %f %f %f\n",((r32*)pp)[0],((r32*)pp)[1],((r32*)pp)[2]);
pp+=12;
}
for(int i = 0; i < verts; i++)
{
printf("tex2 %f %f\n",((r32*)pp)[0],((r32*)pp)[1]);
pp+=8;
}
for(int i = 0; i < faces; i++)
{
printf("face2 %hu %hu %hu\n",((u16*)pp)[0],((u16*)pp)[1],((u16*)pp)[2]);
pp+=6;
}
#endif
printf("Model loaded verts: %d faces %d normals %d\n",verts,faces, normals);
fclose(file);
Mesh* ret = malloc(sizeof(Mesh));
meshInit(ret);
ret->vertices = verts;
ret->faces = faces;
ret->normals = normals;
ret->data = (void*)modelData;
return ret;
}
Mesh* loadMesh(const char* fileName)
{
printf("starting mesh loading\n");
FILE* file = fopen(fileName, "r");
if(file == NULL)
{
printf("File %s not found!\n", fileName);
return NULL;
}
u16 verts;
u16 faces;
fread(&verts,sizeof(u16),1,file);
fread(&faces,sizeof(u16),1,file);
printf("verts: %hu, faces: %hu\n",verts,faces);
int byteCount = verts*sizeof(Vec4)+verts*sizeof(Vec3)+verts*sizeof(Vec2)+faces*3*sizeof(u16)+verts*sizeof(Vec3);
void* data = malloc(byteCount);
fread(data, 1, byteCount, file);
Mesh* mesh = malloc(sizeof(Mesh));
meshInit(mesh);
mesh->data = data;
mesh->vertices = verts;
mesh->faces = faces;
meshRecalculateBounds(mesh);
#if 0
char* pp = (char*)mesh->data;
for(int i = 0; i < verts; i++)
{
printf("%d vert2 %f %f %f %f\n",i,((r32*)pp)[0],((r32*)pp)[1],((r32*)pp)[2],((r32*)pp)[3]);
pp+=16;
}
for(int i = 0; i < verts; i++)
{
printf("norm2 %f %f %f\n",((r32*)pp)[0],((r32*)pp)[1],((r32*)pp)[2]);
pp+=12;
}
for(int i = 0; i < verts; i++)
{
printf("tex2 %f %f\n",((r32*)pp)[0],((r32*)pp)[1]);
pp+=8;
}
for(int i = 0; i < faces; i++)
{
printf("face2 %hu %hu %hu\n",((u16*)pp)[0],((u16*)pp)[1],((u16*)pp)[2]);
pp+=6;
}
#endif
return mesh;
}