-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssetLoading.h
2715 lines (2154 loc) · 75.4 KB
/
AssetLoading.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
struct AssetHandler;
static AssetInfo GetAssetInfo(AssetHandler *handler, u32 id);
struct UVList
{
u16 uvIndex;
u16 flattendIndex;
u16 normalIndex;
UVList *next;
};
typedef UVList * UVListPtr;
DefineArray(UVListPtr);
static MaterialDynamicArray LoadMTL(String path, String fileName, Arena *arena)
{
MaterialDynamicArray ret = MaterialCreateDynamicArray(globalAlloc);
DeferRestore(frameArena);
File file = LoadFile(FormatCString("%s%s\0", path, fileName), frameArena);
Assert(file.fileSize);
String string = CreateString((Char *)file.memory, file.fileSize);
b32 success = true;
while (string.length)
{
Material cur = {};
String line = ConsumeNextLineSanitize(&string);
while (string.length && line.length == 0 || line[0] == '#') { line = ConsumeNextLineSanitize(&string); }
// newmtl
{
String ident = EatToNextSpaceReturnHead(&line);
Assert(ident == "newmtl");
EatSpaces(&line);
cur.name = CopyString(line, arena);
}
line = ConsumeNextLineSanitize(&string);
while (string.length && line.length == 0 || line[0] == '#') { line = ConsumeNextLineSanitize(&string); }
// Ns
{
String ident = EatToNextSpaceReturnHead(&line);
Assert(ident == "Ns");
EatSpaces(&line);
cur.specularExponent = StoF(line, &success);
}
line = ConsumeNextLineSanitize(&string);
while (string.length && line.length == 0 || line[0] == '#') { line = ConsumeNextLineSanitize(&string); }
// Ka
{
String ident = EatToNextSpaceReturnHead(&line);
Assert(ident == "Ka");
EatSpaces(&line);
String head = EatToNextSpaceReturnHead(&line);
f32 a1 = StoF(head, &success);
EatSpaces(&line);
head = EatToNextSpaceReturnHead(&line);
f32 a2 = StoF(head, &success);
EatSpaces(&line);
head = EatToNextSpaceReturnHead(&line);
f32 a3 = StoF(head, &success);
cur.ka = V3(a1, a2, a3);
}
line = ConsumeNextLineSanitize(&string);
while (string.length && line.length == 0 || line[0] == '#') { line = ConsumeNextLineSanitize(&string); }
// Kd
{
String ident = EatToNextSpaceReturnHead(&line);
Assert(ident == "Kd");
EatSpaces(&line);
String head = EatToNextSpaceReturnHead(&line);
f32 a1 = StoF(head, &success);
EatSpaces(&line);
head = EatToNextSpaceReturnHead(&line);
f32 a2 = StoF(head, &success);
EatSpaces(&line);
head = EatToNextSpaceReturnHead(&line);
f32 a3 = StoF(head, &success);
cur.kd = V3(a1, a2, a3);
}
line = ConsumeNextLineSanitize(&string);
while (string.length && line.length == 0 || line[0] == '#') { line = ConsumeNextLineSanitize(&string); }
// Ks
{
String ident = EatToNextSpaceReturnHead(&line);
Assert(ident == "Ks");
EatSpaces(&line);
String head = EatToNextSpaceReturnHead(&line);
f32 a1 = StoF(head, &success);
EatSpaces(&line);
head = EatToNextSpaceReturnHead(&line);
f32 a2 = StoF(head, &success);
EatSpaces(&line);
head = EatToNextSpaceReturnHead(&line);
f32 a3 = StoF(head, &success);
cur.ks = V3(a1, a2, a3);
}
line = ConsumeNextLineSanitize(&string);
while (string.length && line.length == 0 || line[0] == '#') { line = ConsumeNextLineSanitize(&string); }
// Ke
{
String ident = EatToNextSpaceReturnHead(&line);
Assert(ident == "Ke");
EatSpaces(&line);
String head = EatToNextSpaceReturnHead(&line);
f32 a1 = StoF(head, &success);
EatSpaces(&line);
head = EatToNextSpaceReturnHead(&line);
f32 a2 = StoF(head, &success);
EatSpaces(&line);
head = EatToNextSpaceReturnHead(&line);
f32 a3 = StoF(head, &success);
cur.ke = V3(a1, a2, a3);
}
line = ConsumeNextLineSanitize(&string);
while (string.length && line.length == 0 || line[0] == '#') { line = ConsumeNextLineSanitize(&string); }
// Ni
{
String ident = EatToNextSpaceReturnHead(&line);
Assert(ident == "Ni");
EatSpaces(&line);
String head = EatToNextSpaceReturnHead(&line);
f32 a1 = StoF(head, &success);
cur.indexOfRefraction = a1;
}
line = ConsumeNextLineSanitize(&string);
while (string.length && line.length == 0 || line[0] == '#') { line = ConsumeNextLineSanitize(&string); }
// d
{
String ident = EatToNextSpaceReturnHead(&line);
Assert(ident == "d");
EatSpaces(&line);
String head = EatToNextSpaceReturnHead(&line);
f32 a1 = StoF(head, &success);
cur.dissolved = a1;
}
line = ConsumeNextLineSanitize(&string);
while (string.length && line.length == 0 || line[0] == '#') { line = ConsumeNextLineSanitize(&string); }
// illum
{
String ident = EatToNextSpaceReturnHead(&line);
Assert(ident == "illum");
EatSpaces(&line);
String head = EatToNextSpaceReturnHead(&line);
u32 a1 = StoU(head, &success);
cur.illuminationModel = a1;
}
line = ConsumeNextLineSanitize(&string);
while (string.length && line.length == 0 || line[0] == '#') { line = ConsumeNextLineSanitize(&string); }
// map_Kd
{
String ident = EatToNextSpaceReturnHead(&line);
Assert(ident == "map_Kd");
EatSpaces(&line);
String tail = EatToCharFromBackReturnTail(&line, '\\');
cur.texturePath = CopyString(CreateString(FormatCString("%s.texture", GetToChar(tail, '.'))), arena);
//if(assetHandler) cur.bitmapID = RegisterAsset(assetHandler, Asset_Texture, (char *)cur.texturePath.data); ????
}
ArrayAdd(&ret, cur);
}
Assert(success);
return ret;
}
static String GetDirectioryFromFilePath(String fileName)
{
u32 positionOfLastSlash = 0;
for (u32 i = fileName.length - 1; i < MAXU32; i--)
{
if (fileName[i] == '/')
{
positionOfLastSlash = i;
break;
}
}
if (!positionOfLastSlash)
{
return {};
}
String ret;
ret.data = fileName.data;
ret.length = positionOfLastSlash + 1;
return ret;
}
struct ReadOBJIndex
{
u16 posIndex;
u16 uvIndex;
u16 normalIndex;
};
DefineArray(ReadOBJIndex)
struct ReadOBJIterator
{
Material mat;
v3Array p;
v2Array uv;
v3Array n;
ReadOBJIndexArray indices;
};
DefineDFArray(ReadOBJIterator);
static TriangleMesh ReadObj(char *fileName, Arena *arena)
{
String filename = CreateString(fileName);
String path = GetDirectioryFromFilePath(filename);
//DeferReset(frameArena); caller should do that if he wants, so he could also call with arena == frameArena.
File file = LoadFile(fileName, frameArena);
if (!file.fileSize) return {};
String string = CreateString((Char *)file.memory, file.fileSize);
// here mtllib "bla.mtl"
String mtllib = ConsumeNextLineSanitize(&string);
while (string.length && mtllib.length == 0 || mtllib[0] == '#') { mtllib = ConsumeNextLineSanitize(&string); }
Assert(string.length);
String ident = EatToNextSpaceReturnHead(&mtllib);
Assert(ident == "mtllib");
EatSpaces(&mtllib);
MaterialDynamicArray materials = LoadMTL(path, mtllib, arena);
// todo make sure that multiples get handled, i.e make this assets.
b32 success = true;
String line = ConsumeNextLineSanitize(&string);
ReadOBJIteratorDFArray stuff = ReadOBJIteratorCreateDFArray();
//u16PtrDynamicArray indexPointerArray = u16PtrCreateDynamicArray();
while (string.length)
{
ReadOBJIterator cur;
while (string.length && line.length == 0 || line[0] == '#') { line = ConsumeNextLineSanitize(&string); }
// o
{
String head = EatToNextSpaceReturnHead(&line);
Assert(head == "o");
EatSpaces(&line);
String o = line; // ignored for now
}
line = ConsumeNextLineSanitize(&string);
while (string.length && line.length == 0 || line[0] == '#') { line = ConsumeNextLineSanitize(&string); }
// todo how broken of a file do we not crash on? We could just crash if the file ends in something we do not expect.
BeginArray(frameArena, v3, tempVertexArray);
while (string.length && line[0] == 'v' && line[1] == ' ')
{
Eat1(&line);
EatSpaces(&line);
f32 f1 = Eatf32(&line, &success);
EatSpaces(&line);
f32 f2 = Eatf32(&line, &success);
EatSpaces(&line);
f32 f3 = Eatf32(&line, &success);
*PushStruct(frameArena, v3) = V3(f1, f2, f3);
line = ConsumeNextLineSanitize(&string);
}
EndArray(frameArena, v3, tempVertexArray);
while (string.length && line.length == 0 || line[0] == '#') { line = ConsumeNextLineSanitize(&string); }
BeginArray(frameArena, v2, textureCoordinates);
while (string.length && line[0] == 'v' && line[1] == 't')
{
Eat1(&line);
Eat1(&line);
EatSpaces(&line);
f32 f1 = Eatf32(&line, &success);
EatSpaces(&line);
f32 f2 = Eatf32(&line, &success);
*PushStruct(frameArena, v2) = V2(f1, f2);
line = ConsumeNextLineSanitize(&string);
}
EndArray(frameArena, v2, textureCoordinates);
while (string.length && line.length == 0 || line[0] == '#') { line = ConsumeNextLineSanitize(&string); }
BeginArray(frameArena, v3, normals);
while (string.length && line[0] == 'v' && line[1] == 'n')
{
Eat1(&line);
Eat1(&line);
EatSpaces(&line);
f32 f1 = Eatf32(&line, &success);
EatSpaces(&line);
f32 f2 = Eatf32(&line, &success);
EatSpaces(&line);
f32 f3 = Eatf32(&line, &success);
*PushStruct(frameArena, v3) = V3(f1, f2, f3);;
line = ConsumeNextLineSanitize(&string);
}
EndArray(frameArena, v3, normals);
while (string.length && line.length == 0 || line[0] == '#') { line = ConsumeNextLineSanitize(&string); }
// usemtl
{
String head = EatToNextSpaceReturnHead(&line);
Assert(head == "usemtl");
EatSpaces(&line);
String name = line;
For(materials)
{
if (it->name == name)
{
cur.mat = *it;
break;
}
}
}
line = ConsumeNextLineSanitize(&string);
while (string.length && line.length == 0 || line[0] == '#') { line = ConsumeNextLineSanitize(&string); }
// s -smoothening, what ever that means
{
String head = EatToNextSpaceReturnHead(&line);
Assert(head == "s");
EatSpaces(&line);
//u32 s = StoU(line, &success); // ignored for now
}
line = ConsumeNextLineSanitize(&string);
while (string.length && line.length == 0 || line[0] == '#') { line = ConsumeNextLineSanitize(&string); }
BeginArray(frameArena, ReadOBJIndex, indices);
while (line.length && line[0] == 'f')
{
Eat1(&line);
// we assume they are all here
for (u32 parseIndex = 0; parseIndex < 3; parseIndex++)
{
EatSpaces(&line);
String s1 = EatToCharReturnHead(&line, '/');
Eat1(&line);
String s2 = EatToCharReturnHead(&line, '/');
Eat1(&line);
String s3 = EatToCharReturnHead(&line, ' ');
#if 1
// these come in "absolute".
u32 pIndex = StoU(s1, &success);
u32 uvIndex = StoU(s2, &success);
u32 nIndex = StoU(s3, &success);
#else
u32 u1 = StoU(s1, &success) - amountOfVertsBefore; // this has to be relative to be an index, but also saved absolute wrt the flattend array
u32 u2 = StoU(s2, &success) - amountOfUVsBefore; // this is relative as we just need it to build our array
u32 u3 = StoU(s3, &success) - amountOfNormalsBefore; // this is also relative
#endif
ReadOBJIndex *fill = PushStruct(frameArena, ReadOBJIndex);
fill->normalIndex = (u16)nIndex;
fill->uvIndex = (u16)uvIndex;
fill->posIndex = (u16)pIndex;
}
line = ConsumeNextLineSanitize(&string);
}
EndArray(frameArena, ReadOBJIndex, indices);
cur.p = tempVertexArray;
cur.n = normals;
cur.uv = textureCoordinates;
cur.indices = indices;
ArrayAdd(&stuff, cur);
#if 0
amountOfIndeciesBefore += indecies.amount;
amountOfNormalsBefore += normals.amount;
amountOfUVsBefore += textrueCoordinates.amount;
amountOfVertsBefore += tempVertexArray.amount;
#endif
}
// we cant do this in the loop, as arena might be frameArena
BeginArray(arena, IndexSet, indexSets);
u32 amountOfVerts = 0;
u32 amountOfIndices = 0;
For(stuff)
{
IndexSet *indexSet = PushStruct(arena, IndexSet);
indexSet->offset = amountOfIndices;
indexSet->amount = it->indices.amount;
indexSet->mat = it->mat;
amountOfVerts += it->p.amount;
amountOfIndices += it->indices.amount;
}
EndArray(arena, IndexSet, indexSets);
u16Array indices = PushArray(arena, u16, amountOfIndices);
UVListPtrArray flatten = PushArray(frameArena, UVListPtr, amountOfVerts);
u32 flattener = 0;
u32 iterator = 0;
For(s, stuff)
{
For(base, s->indices)
{
u16 pI = base->posIndex - 1;
u16 nI = base->normalIndex - 1;
u16 uvI = base->uvIndex - 1;
b32 found = false;
u16 index = 0;
for (UVList *it = flatten[pI]; it; it = it->next)
{
if (it->normalIndex == nI && it->uvIndex == uvI)
{
found = true;
index = it->flattendIndex;
break;
}
}
if (!found)
{
UVList *append = PushStruct(frameArena, UVList);
append->flattendIndex = (u16)flattener++;
append->next = flatten[pI];
append->normalIndex = (u16)nI;
append->uvIndex = (u16)uvI;
flatten[pI] = append;
index = append->flattendIndex;
}
indices[iterator++] = index;
}
}
Assert(flattener < 0xFFFF);
u32 amountOfFlattendVertices = flattener;
//Assert(flattener == (u32)((UVList *)frameArena->current - (UVList *)flatten.data));
v3Array positions = PushArray(arena, v3, amountOfFlattendVertices);
v3Array normals = PushArray(arena, v3, amountOfFlattendVertices);
v2Array uvs = PushArray(arena, v2, amountOfFlattendVertices);
u32Array colors = PushArray(arena, u32, amountOfFlattendVertices);
For(base, flatten)
{
u32 base_it_index = (u32)(base - flatten.data);
for(UVList *list = *base; list; list = list->next)
{
u32 i = list->flattendIndex;
ReadOBJIterator *s = NULL;
u32 pSub = 0;
u32 nSub = 0;
u32 uvSub = 0;
For(stuff)
{
if(base_it_index - pSub < it->p.amount)
{
s = it;
break;
}
else
{
pSub += it->p.amount;
nSub += it->n.amount;
uvSub += it->uv.amount;
}
}
positions[i] = s->p[base_it_index - pSub];
normals [i] = s->n[list->normalIndex - nSub];
uvs [i] = s->uv[list->uvIndex - uvSub];
colors [i] = 0xFFFFFFFF;
}
}
AABB aabb = InvertedInfinityAABB();
For(positions)
{
aabb.maxDim.x = Max(it->x, aabb.maxDim.x);
aabb.maxDim.y = Max(it->y, aabb.maxDim.y);
aabb.maxDim.z = Max(it->z, aabb.maxDim.z);
aabb.minDim.x = Min(it->x, aabb.minDim.x);
aabb.minDim.y = Min(it->y, aabb.minDim.y);
aabb.minDim.z = Min(it->z, aabb.minDim.z);
}
TriangleMesh ret;
ret.skeleton = {}; //not neccesary, as ret has {}
ret.indexSets = indexSets;
ret.positions = positions;
ret.normals = normals;
ret.uvs = uvs;
ret.colors = colors;
ret.indices = indices;
ret.type = TriangleMeshType_List;
ret.aabb = aabb;
{
String name = filename;
name = EatToCharFromBackReturnTail(&name, '/');
name = EatToCharReturnHead(&name, '.');
ret.name = name;
}
Assert(success);
return ret;
}
struct DAEReturn
{
TriangleMesh mesh;
KeyFramedAnimation animation;
b32 success;
};
struct DAEParam
{
String name;
String type;
};
DefineDFArray(DAEParam);
struct DAETechnique
{
u32 count;
u32 stride;
DAEParamDFArray params;
};
enum DAEArrayType
{
DAE_None,
DAE_Float,
DAE_Int,
DAE_Name,
DAE_Amount,
};
struct DAESource
{
String id;
//String name; // optional so no megusta
String arr; // string, we will work on it later
u32 elementCount;
DAEArrayType type;
DAETechnique technique;
};
DefineDFArray(DAESource);
static bool CStringsAreEqual(char *a, char *b)
{
while (*a)
{
if (*a++ != *b++) return false;
}
return *a == *b;
}
//also eats spaces at the beginning
static String DAEEatAttAndReturnIt(String *line, char *att)
{
EatSpaces(line);
if (!BeginsWithEat(line, att)) { Die; return {}; };
if (!BeginsWithEat(line, "=\"")) { Die; return {}; };
String ret = EatToCharReturnHead(line, '\"');
Eat1(line);
if (CStringsAreEqual(att, "source"))
{
Eat1(&ret);
}
return ret;
}
#if 0
enum DAEGeometryType
{
DAE_Mesh,
DAE_ConvexMesh,
DAE_Spline,
};
#endif
struct DAEImage
{
String id;
String name;
String file;
};
DefineArray(DAEImage);
struct DAEInputShared
{
u32 offset;
String semantic;
String source;
//u32 set; // optional
};
struct DAEInputUnshared
{
String semantic;
String source;
};
DefineDFArray(DAEInputShared);
DefineDFArray(DAEInputUnshared);
struct DAEVertices
{
String id;
DAEInputUnsharedDFArray inputs;
};
struct DAETriangles
{
//String name; //optional
u32 count;
String material;
DAEInputSharedDFArray inputs;
String indexArray;
};
struct DAEMesh
{
String id;
String name;
DAESourceDFArray sources;
DAEVertices verts;
DAETriangles triangles;
};
DefineDFArray(DAEMesh);
struct DAESampler
{
String id;
DAEInputUnsharedDFArray inputs;
};
struct DAEChannel
{
String source;
String target;
};
struct DAEAnimation
{
String id;
DAESourceDFArray sources;
DAESampler sampler;
DAEChannel channel;
};
DefineDFArray(DAEAnimation);
struct DAEJoints
{
DAEInputUnsharedDFArray inputs;
};
struct DAEVertexWeights
{
u32 count;
DAEInputSharedDFArray inputs;
String vcount;
String v;
};
struct DAESkin
{
String skinSource;
String bind_shape_matrix;
DAESourceDFArray sources;
DAEJoints joints;
DAEVertexWeights vertex_weights;
};
struct DAEController
{
String id;
String name;
DAESkin skin;
};
DefineDFArray(DAEController);
struct DAENode;
typedef DAENode* DAENodePtr;
DefineDFArray(DAENodePtr);
enum DAETransformationType
{
DAE_Scale,
DAE_Rotate,
DAE_Translate,
DAE_Matrix,
DAE_Skew,
DAE_lookAt,
};
struct DAETransformation
{
DAETransformationType type;
String sid;
String val;
};
DefineDFArray(DAETransformation);
struct DAENode
{
String id;
String name;
String sid; // optional
String type;
//String layer; not in my file
DAETransformationDFArray transformations;
DAENodePtrDFArray children;
};
struct DAEVisualScene
{
String id;
String name;
DAENodePtrDFArray nodes;
};
DefineDFArray(DAEVisualScene);
struct DAEdata
{
DAEImageArray images;
DAEAnimationDFArray animations;
DAEVisualSceneDFArray scenes;
DAEControllerDFArray controllers;
DAEMeshDFArray meshes;
MaterialDFArray materials;
};
static DAENode *DAEParseNode(String *line, String *remaining)
{
DAENode *ret = PushStruct(frameArena, DAENode);
if (!BeginsWithEat(line, "<node")) { Die; return NULL; }
ret->id = DAEEatAttAndReturnIt(line, "id");
ret->name = DAEEatAttAndReturnIt(line, "name");
EatSpaces(line);
if (BeginsWith(*line, "sid"))
{
ret->sid = DAEEatAttAndReturnIt(line, "sid");
}
else
{
ret->sid = {};
}
ret->type = DAEEatAttAndReturnIt(line, "type");
ret->transformations = DAETransformationCreateDFArray(1);
while (remaining->amount)
{
*line = ConsumeNextLineSanitizeEatSpaces(remaining);
DAETransformation trans;
if (BeginsWithEat(line, "<translate"))
{
trans.type = DAE_Translate;
trans.sid = DAEEatAttAndReturnIt(line, "sid");
EatToCharReturnHead(line, '>'); Eat1(line);
trans.val = EatToCharReturnHead(line, '<');
}
else if (BeginsWithEat(line, "<rotate"))
{
trans.type = DAE_Rotate;
trans.sid = DAEEatAttAndReturnIt(line, "sid");
EatToCharReturnHead(line, '>'); Eat1(line);
trans.val = EatToCharReturnHead(line, '<');
}
else if (BeginsWithEat(line, "<scale"))
{
trans.type = DAE_Scale;
trans.sid = DAEEatAttAndReturnIt(line, "sid");
EatToCharReturnHead(line, '>'); Eat1(line);
trans.val = EatToCharReturnHead(line, '<');
}
else if (BeginsWithEat(line, "<matrix"))
{
trans.type = DAE_Matrix;
trans.sid = DAEEatAttAndReturnIt(line, "sid");
EatToCharReturnHead(line, '>'); Eat1(line);
trans.val = EatToCharReturnHead(line, '<');
}
else
{
break;
}
ArrayAdd(&ret->transformations, trans);
}
if (BeginsWithEat(line, "<instance_controller")) // dono ignoring this for now todo
{
while (*line != "</instance_controller>")
{
*line = ConsumeNextLineSanitizeEatSpaces(remaining);
}
*line = ConsumeNextLineSanitizeEatSpaces(remaining);
}
ret->children = DAENodePtrCreateDFArray(5);
while (BeginsWith(*line, "<node"))
{
ArrayAdd(&ret->children, DAEParseNode(line, remaining));
}
if (*line == "<extra>") // skip extra for now, as i do not know it it is neccessary
{
while (remaining->length)
{
*line = ConsumeNextLineSanitizeEatSpaces(remaining);
if (*line == "</extra>")
{
break;
}
}
*line = ConsumeNextLineSanitizeEatSpaces(remaining);
}
Assert(*line == "</node>");
*line = ConsumeNextLineSanitizeEatSpaces(remaining);
return ret;
}
// this assume that we just ate "<source"
static DAESource DAEParseSource(String line, String *remaining)
{
DAESource ret = {};
ret.id = DAEEatAttAndReturnIt(&line, "id");
b32 hadArrAllready = false;
b32 hadTechAllready = false;
b32 success = true;
while (remaining->amount)
{
line = ConsumeNextLineSanitizeEatSpaces(remaining);
if (line == "</source>")
{
break;
}
if (BeginsWithEat(&line, "<float_array"))
{
Assert(!hadArrAllready);
hadArrAllready = true;
String id = DAEEatAttAndReturnIt(&line, "id"); // not sure, this does not seem to make a lot of sense, as they can only be one array per source?
String countS = DAEEatAttAndReturnIt(&line, "count");
u32 count = StoU(countS, &success);
Assert(success);
ret.elementCount = count;
ret.type = DAE_Float;
EatToCharReturnHead(&line, '>'); // this should be nothing?
Eat1(&line);
ret.arr = EatToCharReturnHead(&line, '<'); // we assume that arrays are in one line allways
}
else if (BeginsWithEat(&line, "<Name_array"))
{
Assert(!hadArrAllready);
hadArrAllready = true;
String id = DAEEatAttAndReturnIt(&line, "id"); // not sure, this does not seem to make a lot of sense, as they can only be one array per source?
String countS = DAEEatAttAndReturnIt(&line, "count");
u32 count = StoU(countS, &success);
Assert(success);
ret.elementCount = count;
ret.type = DAE_Name;
EatToCharReturnHead(&line, '>'); // this should be nothing?
Eat1(&line);
ret.arr = EatToCharReturnHead(&line, '<'); // we assume that arrays are in one line allways
}
else if (line == "<technique_common>")
{
Assert(!hadTechAllready);
hadTechAllready = true;
line = ConsumeNextLineSanitizeEatSpaces(remaining);
DAETechnique t;
if (!BeginsWithEat(&line, "<accessor")) { Die; return {}; };
String source = DAEEatAttAndReturnIt(&line, "source");
String countS = DAEEatAttAndReturnIt(&line, "count");
String strideS = DAEEatAttAndReturnIt(&line, "stride");