-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj_loader.h
446 lines (405 loc) · 12.3 KB
/
obj_loader.h
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
OBJ Loader - mojobojo 2016
- Built to load obj files expored by blender
- Built in stb style
- Lets you use your own block of allocated memory to push the data onto
- Outputs a struct that exposes the data as you would see
inside an obj file.
- Currently uses a incomplete obj file spec
- Uses 4 stdlib functions
- malloc (only if you want to use the function that uses it)
- free (onlt if you use the function with malloc)
- atof
- strtoul
- TODO:
- Complete obj file spec
- mtl loading
- load 4-float vertexes
- load single index value faces (right now only loads the format 0/0/0)
*/
#ifndef __OBJ_LOADER_H_
#define __OBJ_LOADER_H_
#include <stdlib.h>
#include <stdint.h>
typedef int8_t objl_i8;
typedef uint8_t objl_u8;
typedef int16_t objl_i16;
typedef uint16_t objl_u16;
typedef int32_t objl_i32;
typedef uint32_t objl_u32;
typedef int64_t objl_i64;
typedef uint64_t objl_u64;
typedef float objl_f32;
typedef double objl_f64;
typedef objl_i32 objl_b32;
#define objl_internal static
#define objl_EatLine() while (*b != '\n') { ++b; } ++b
typedef struct {
size_t Used;
size_t Size;
void *Memory;
} objl_memory;
typedef struct {
objl_f32 x;
objl_f32 y;
objl_f32 z;
} objl_v3;
typedef struct {
objl_f32 x;
objl_f32 y;
} objl_v2;
typedef struct {
objl_i32 vertex;
objl_i32 texture;
objl_i32 normal;
} objl_f_element;
typedef struct {
objl_f_element f0;
objl_f_element f1;
objl_f_element f2;
} objl_f;
typedef struct {
char *o;
objl_i32 v_count;
objl_v3 *v;
objl_i32 vt_count;
objl_v2 *vt;
objl_i32 vn_count;
objl_v3 *vn;
char *s;
objl_i32 f_count;
objl_f *f;
objl_memory Memory;
} objl_obj_file;
#ifdef __cplusplus
extern "C"
{
#endif
// Use this if you wish to use your own allocated block of memory to put the obj data in.
// The obj loader uses an internal arena allocater.
objl_internal void objl_LoadObj(char *ObjData, void *ObjMemory, size_t ObjMemorySize, objl_obj_file *ObjFileOut);
// If you do not care to use your own allocated block of memory
// you can use this version that uses malloc
objl_internal void objl_LoadObjMalloc(char *ObjData, objl_obj_file *ObjFileOut);
// This should only be used if you used objl_LoadObjMalloc
objl_internal void objl_FreeObj(objl_obj_file *ObjFileOut);
#ifdef OBJL_IMPLEMENTATION
objl_internal void *
objl_PushMemory(objl_memory *Memory, size_t Amount) {
void *Result = 0;
if ((Memory->Used + Amount) < Memory->Size) {
Result = (objl_u8 *)Memory->Memory + Memory->Used;
Memory->Used += Amount;
}
return Result;
}
objl_internal void
objl_CopyMemory(void *Destination, void *Source, size_t Length) {
objl_u8 *d = (objl_u8 *)Destination;
objl_u8 *s = (objl_u8 *)Source;
while (Length--) {
*d++ = *s++;
}
}
objl_internal void
objl_ZeroMemory(void *Destination, size_t Length) {
objl_u8 *d = (objl_u8 *)Destination;
while (Length--) {
*d++ = 0;
}
}
objl_internal size_t
objl_strlen(char *String) {
size_t n = 0;
while (*String++) {
++n;
}
return n;
}
objl_internal objl_f32
objl_StringToFloat(char *String) {
// TODO(joey): My own string to float implementation.
return (objl_f32)atof(String);
}
objl_internal objl_u32
objl_StringToUInt(char *String) {
// TODO(joey): My own string to int implementation.
objl_u32 Value = 0;
if (objl_strlen(String)) {
Value = (objl_u32)strtoul(String, 0, 0);
}
return Value;
}
objl_internal void
objl_ParseObj(char *ObjData, objl_obj_file *ObjFileOut, objl_b32 UseMalloc) {
char *b = ObjData;
// NOTE(joey): Count the vertexes, normals, and faces.
while (*b) {
if (*b == 'v') {
if (*(b+1) == 'n') {
++ObjFileOut->vn_count;
}
else if (*(b+1) == 't') {
++ObjFileOut->vt_count;
}
else {
++ObjFileOut->v_count;
}
}
else if (*b == 'f') {
++ObjFileOut->f_count;
}
while (*b != '\n' && *b != '\0') {
++b;
}
// NOTE(joey): In case we reach a null char before a new line
if (*b) {
++b;
}
}
// NOTE(joey): The OBJ file spec says there can be 4 elements in a row for verts. The OBJ files
// blender puts out has not had them so I have not bothered to implement it.
if (UseMalloc) {
ObjFileOut->v = (objl_v3 *)malloc(ObjFileOut->v_count*sizeof(objl_v3));
ObjFileOut->vt = (objl_v2 *)malloc(ObjFileOut->vt_count*sizeof(objl_v2));
ObjFileOut->vn = (objl_v3 *)malloc(ObjFileOut->vn_count*sizeof(objl_v3));
ObjFileOut->f = (objl_f *)malloc(ObjFileOut->f_count*sizeof(objl_f));
}
else {
ObjFileOut->v = (objl_v3 *)objl_PushMemory(&ObjFileOut->Memory, ObjFileOut->v_count*sizeof(objl_v3));
ObjFileOut->vt = (objl_v2 *)objl_PushMemory(&ObjFileOut->Memory, ObjFileOut->vt_count*sizeof(objl_v2));
ObjFileOut->vn = (objl_v3 *)objl_PushMemory(&ObjFileOut->Memory, ObjFileOut->vn_count*sizeof(objl_v3));
ObjFileOut->f = (objl_f *)objl_PushMemory(&ObjFileOut->Memory, ObjFileOut->f_count*sizeof(objl_f));
}
objl_v3 *v = ObjFileOut->v;
objl_v2 *vt = ObjFileOut->vt;
objl_v3 *vn = ObjFileOut->vn;
objl_f_element *f = (objl_f_element *)ObjFileOut->f;
b = ObjData;
while (*b) {
if (*b == '#') {
objl_EatLine();
}
else if (*b == 'm') {
//mtllib
if (*(b+1) == 't' &&
*(b+2) == 'l' &&
*(b+3) == 'l' &&
*(b+4) == 'i' &&
*(b+5) == 'b') {
// TODO(joey): mtl handling
objl_EatLine();
}
}
else if (*b == 'o') {
++b; ++b;
char *s = b;
while (*b != '\n') {
++b;
}
if (UseMalloc) {
ObjFileOut->o = (char *)malloc(b-s+1);
}
else {
ObjFileOut->o = (char *)objl_PushMemory(&ObjFileOut->Memory, b-s+1);
}
objl_CopyMemory(ObjFileOut->o, s, b-s);
ObjFileOut->o[b-s] = 0;
++b;
}
else if (*b == 'v') {
if (*(b+1) == 'n') {
++b; ++b; ++b;
objl_f32 vec[3];
objl_f32 *temvec = vec;
while (*b != '\n') {
char TmpBuf[32];
objl_ZeroMemory(TmpBuf, 32);
char *t = TmpBuf;
while (*b != ' ') {
if (*b == '\n') {
break;
}
else {
*t++ = *b++;
}
}
if (*b == ' ') {
++b;
}
objl_f32 Value = objl_StringToFloat(TmpBuf);
*temvec++ = Value;
}
vn->x = vec[0];
vn->y = vec[1];
vn->z = vec[2];
++vn;
++b;
}
else if (*(b+1) == 't') {
++b; ++b; ++b;
objl_f32 vec[3];
objl_f32 *temvec = vec;
while (*b != '\n') {
char TmpBuf[32];
objl_ZeroMemory(TmpBuf, 32);
char *t = TmpBuf;
while (*b != ' ') {
if (*b == '\n') {
break;
}
else {
*t++ = *b++;
}
}
if (*b == ' ') {
++b;
}
objl_f32 Value = objl_StringToFloat(TmpBuf);
*temvec++ = Value;
}
vt->x = vec[0];
vt->y = vec[1];
++vt;
++b;
}
else {
++b; ++b;
objl_f32 vec[3];
objl_f32 *temvec = vec;
while (*b != '\n') {
char TmpBuf[32];
objl_ZeroMemory(TmpBuf, 32);
char *t = TmpBuf;
while (*b != ' ') {
if (*b == '\n') {
break;
}
else {
*t++ = *b++;
}
}
if (*b == ' ') {
++b;
}
objl_f32 Value = objl_StringToFloat(TmpBuf);
*temvec++ = Value;
}
v->x = vec[0];
v->y = vec[1];
v->z = vec[2];
++v;
++b;
}
}
else if (*b == 'u') {
// usemtl
if (*(b+1) == 's' &&
*(b+2) == 'e' &&
*(b+3) == 'm' &&
*(b+4) == 't' &&
*(b+5) == 'l') {
objl_EatLine();
}
}
else if (*b == 'f') {
++b; ++b;
while (*b != '\n') {
char TmpBuf[32];
objl_ZeroMemory(TmpBuf, 32);
char *t = TmpBuf;
while (*b != ' ') {
if (*b == '\n') {
break;
}
else {
*t++ = *b++;
}
}
if (*b == ' ') {
++b;
}
objl_i32 sc = 0;
char *n = TmpBuf;
char *c = n;
char *c1 = "0";
char *c2 = "0";
char *c3 = "0";
while (*n) {
if (*n == '/') {
if (sc == 0) {
*n = 0;
c1 = c;
c = n+1;
}
else if (sc == 1) {
*n = 0;
c2 = c;
c = n+1;
c3 = c;
}
++sc;
}
++n;
}
f->vertex = objl_StringToUInt(c1);
f->texture = objl_StringToUInt(c2);
f->normal = objl_StringToUInt(c3);
*f++;
}
++b;
}
else if (*b == 's') {
++b; ++b;
char *s = b;
while (*b != '\n') {
++b;
}
if (UseMalloc) {
ObjFileOut->s = (char *)malloc(b-s+1);
}
else {
ObjFileOut->s = (char *)objl_PushMemory(&ObjFileOut->Memory, b-s+1);
}
objl_CopyMemory(ObjFileOut->s, s, b-s);
ObjFileOut->s[b-s] = 0;
++b;
}
else {
++b;
}
}
}
objl_internal void
objl_LoadObjMalloc(char *ObjData, objl_obj_file *ObjFileOut) {
if (ObjData && ObjFileOut) {
objl_ZeroMemory(ObjFileOut, sizeof(objl_obj_file));
objl_ParseObj(ObjData, ObjFileOut, true);
}
}
objl_internal void
objl_FreeObj(objl_obj_file *ObjFile) {
free(ObjFile->o);
free(ObjFile->v);
free(ObjFile->vt);
free(ObjFile->vn);
free(ObjFile->s);
free(ObjFile->f);
objl_ZeroMemory(ObjFile, sizeof(objl_obj_file));
}
// Takes a null terminated buffer with the obj data.
objl_internal void
objl_LoadObj(char *ObjData, void *ObjMemory, size_t ObjMemorySize, objl_obj_file *ObjFileOut) {
if (ObjData && ObjMemory && ObjMemorySize && ObjFileOut) {
objl_ZeroMemory(ObjFileOut, sizeof(objl_obj_file));
ObjFileOut->Memory.Used = 0;
ObjFileOut->Memory.Size = ObjMemorySize;
ObjFileOut->Memory.Memory = ObjMemory;
objl_ParseObj(ObjData, ObjFileOut, false);
}
}
#endif //OBJL_IMPLEMENTATION
#ifdef __cplusplus
}
#endif
#endif