-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample.cc
More file actions
303 lines (267 loc) · 9.94 KB
/
example.cc
File metadata and controls
303 lines (267 loc) · 9.94 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define FBX_LOADER_IMPLEMENTATION
#include "fbx_loader.h"
#define STB_IMAGE_IMPLEMENTATION
#define STBI_SUPPORT_ZLIB
#include "stb_image.h"
#define internal static
#define THIS_IS_FUCKING_DUMB(x) #x
#define PREPROC_NUM_TO_STRING(x) THIS_IS_FUCKING_DUMB(x)
#define Assert(x) if (!(x)) { fprintf(stderr, "Assert fired in file " __FILE__ " at line " PREPROC_NUM_TO_STRING(__LINE__) " EVAL: " #x "\n"); *(int*)0 = 0; }
#define mfree free
typedef int8_t i8;
typedef uint8_t u8;
typedef int16_t i16;
typedef uint16_t u16;
typedef int32_t i32;
typedef uint32_t u32;
typedef int64_t i64;
typedef uint64_t u64;
typedef float f32;
typedef double f64;
typedef i32 b32;
internal char *fbx_PropertyTypeToString(fbx_property_type Type) {
char *TypeString = "";
switch (Type) {
case FBX_PROPERTY_S16: {
TypeString = "FBX_PROPERTY_S16";
break;
}
case FBX_PROPERTY_BOOL: {
TypeString = "FBX_PROPERTY_BOOL";
break;
}
case FBX_PROPERTY_BOOL_ARRAY: {
TypeString = "FBX_PROPERTY_BOOL_ARRAY";
break;
}
case FBX_PROPERTY_S32: {
TypeString = "FBX_PROPERTY_S32";
break;
}
case FBX_PROPERTY_S32_ARRAY: {
TypeString = "FBX_PROPERTY_S32_ARRAY";
break;
}
case FBX_PROPERTY_S64: {
TypeString = "FBX_PROPERTY_S64";
break;
}
case FBX_PROPERTY_S64_ARRAY: {
TypeString = "FBX_PROPERTY_S64_ARRAY";
break;
}
case FBX_PROPERTY_F32: {
TypeString = "FBX_PROPERTY_F32";
break;
}
case FBX_PROPERTY_F32_ARRAY: {
TypeString = "FBX_PROPERTY_F32_ARRAY";
break;
}
case FBX_PROPERTY_F64: {
TypeString = "FBX_PROPERTY_F64";
break;
}
case FBX_PROPERTY_F64_ARRAY: {
TypeString = "FBX_PROPERTY_F64_ARRAY";
break;
}
case FBX_PROPERTY_STRING: {
TypeString = "FBX_PROPERTY_STRING";
break;
}
case FBX_PROPERTY_RAW: {
TypeString = "FBX_PROPERTY_RAW";
break;
}
default: {
Assert(!"This shouldnt happen");
}
}
return TypeString;
}
i32 RecordIndent = 0;
internal void
fbx_PrintRecords(fbx_record *Record) {
++RecordIndent;
while (Record) {
// NOTE(joey): Right now my fbx parser outputs a blank record at the end of each
// linked list. Im stuck right now on figuring out how to fix that so leaving it
// for later.
if (strlen(Record->Name)) {
char Indent[256] = {};
i32 Written = 0;
for (i32 i = 0; i < RecordIndent; ++i) {
Written = sprintf(Indent+Written, " ");
}
printf("%sName: %s\n", Indent, Record->Name);
for (u32 i = 0; i < Record->PropertiesCount; ++i) {
//printf("%s Data %d: Type %s\n", Indent, i, fbx_PropertyTypeToString(Record->Properties[i].Type));
//printf("%s ", Indent);
fbx_property_type Type = Record->Properties[i].Type;
b32 Compressed = Record->Properties[i].Compressed;
u32 Size = Record->Properties[i].Size;
u32 CompressedSize = Record->Properties[i].CompressedSize;
void *Data = Record->Properties[i].Data;
printf("%s Type: %s\n", Indent, fbx_PropertyTypeToString(Record->Properties[i].Type));
printf("%s Compressed: %d\n", Indent, Compressed);
printf("%s Size: %d\n", Indent, Size);
printf("%s Compressed Size: %d\n", Indent, CompressedSize);
printf("%s Data: ", Indent);
switch (Type) {
case FBX_PROPERTY_S16: {
printf("%d", *(i16 *)Data);
break;
}
case FBX_PROPERTY_BOOL: {
printf("%s", *(u8 *)Data != 0 ? "true" : "false");
break;
}
case FBX_PROPERTY_BOOL_ARRAY: {
u32 Count = Size/1;
u8 *Buffer = (u8 *)Data;
if (Compressed) {
Buffer = (u8 *)malloc(Size);
i32 value = stbi_zlib_decode_buffer((char *)Buffer, Size, (const char *)Data, CompressedSize);
Assert(value == (i32)Size);
}
for (u32 n = 0; n < Count; ++n) {
printf("%s", Buffer[n] != 0 ? "true" : "false");
}
if (Compressed) {
mfree(Buffer);
}
break;
}
case FBX_PROPERTY_S32: {
printf("%d", *(i32 *)Data);
break;
}
case FBX_PROPERTY_S32_ARRAY: {
u32 Count = Size/4;
i32 *Buffer = (i32 *)Data;
if (Compressed) {
Buffer = (i32 *)malloc(Size);
i32 value = stbi_zlib_decode_buffer((char *)Buffer, Size, (const char *)Data, CompressedSize);
Assert(value == (i32)Size);
}
for (u32 n = 0; n < Count; ++n) {
printf("%d ", Buffer[n]);
}
if (Compressed) {
mfree(Buffer);
}
break;
}
case FBX_PROPERTY_S64: {
printf("%lld", *(i64 *)Data);
break;
}
case FBX_PROPERTY_S64_ARRAY: {
u32 Count = Size/8;
i64 *Buffer = (i64 *)Data;
if (Compressed) {
Buffer = (i64 *)malloc(Size);
i32 value = stbi_zlib_decode_buffer((char *)Buffer, Size, (const char *)Data, CompressedSize);
Assert(value == (i32)Size);
}
for (u32 n = 0; n < Count; ++n) {
printf("%lld ", Buffer[n]);
}
if (Compressed) {
mfree(Buffer);
}
break;
}
case FBX_PROPERTY_F32: {
printf("%f", *(f32 *)Data);
break;
}
case FBX_PROPERTY_F32_ARRAY: {
u32 Count = Size/4;
f32 *Buffer = (f32 *)Data;
if (Compressed) {
Buffer = (f32 *)malloc(Size);
i32 value = stbi_zlib_decode_buffer((char *)Buffer, Size, (const char *)Data, CompressedSize);
Assert(value == (i32)Size);
}
for (u32 n = 0; n < Count; ++n) {
printf("%f ", Buffer[n]);
}
if (Compressed) {
mfree(Buffer);
}
break;
}
case FBX_PROPERTY_F64: {
printf("%f", *(f64 *)Data);
break;
}
case FBX_PROPERTY_F64_ARRAY: {
u32 Count = Size/8;
f64 *Buffer = (f64 *)Data;
if (Compressed) {
Buffer = (f64 *)malloc(Size);
i32 value = stbi_zlib_decode_buffer((char *)Buffer, Size, (const char *)Data, CompressedSize);
Assert(value == (i32)Size);
}
for (u32 n = 0; n < Count; ++n) {
printf("%f ", Buffer[n]);
}
if (Compressed) {
mfree(Buffer);
}
break;
}
case FBX_PROPERTY_STRING: {
char str[256] = {};
memcpy(str, Data, Size);
printf("%s", str);
break;
}
case FBX_PROPERTY_RAW: {
u8 *Buffer = (u8 *)Data;
for (u32 n = 0; n < Size; ++n) {
printf("%08X", Buffer[n]);
}
break;
}
default: {
Assert(!"This shouldnt happen");
}
}
printf("\n\n");
}
if (Record->SubRecord && strlen(Record->SubRecord->Name)) {
fbx_PrintRecords(Record->SubRecord);
}
}
Record = Record->Next;
}
--RecordIndent;
}
int
main(int argc, char **argv) {
FILE *f = fopen("cube.fbx", "rb");
u8 *FileBuffer;
size_t FileSize;
if (f) {
fseek(f, 0, SEEK_END);
FileSize = ftell(f);
rewind(f);
FileBuffer = (u8 *)malloc(FileSize);
fread(FileBuffer, 1, FileSize, f);
fclose(f);
fbx_file FbxFile;
if (fbxl_Parse(FileBuffer, FileSize, &FbxFile)) {
printf("Parsed file!\n");
fbx_PrintRecords(FbxFile.Records);
fbxl_Free(&FbxFile);
}
}
return 0;
}