-
Notifications
You must be signed in to change notification settings - Fork 3
/
module.jai
1633 lines (1321 loc) · 47.8 KB
/
module.jai
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
GLTF_Data :: struct {
filepath : string;
asset : GLTF_Asset;
scene : int;
scenes : [..] GLTF_Scene;
skins : [..] GLTF_Skin;
accessors : [..] GLTF_Accessor;
nodes : [..] GLTF_Node;
meshes : [..] GLTF_Mesh;
buffer_views : [..] GLTF_Buffer_View;
buffers : [..] GLTF_Buffer;
textures : [..] GLTF_Texture;
images : [..] GLTF_Image;
samplers : [..] GLTF_Sampler;
materials : [..] GLTF_Material;
cameras : [..] GLTF_Camera;
lights : [..] GLTF_Light;
animations : [..] GLTF_Animation;
using meta : struct {
// Used to prevent loading buffers when it's a glb file
// because it's already loaded in memory.
is_glb : bool;
// Used in order
allocator : Allocator;
};
}
gltf_parse_string :: (gltf_buffer: string) -> GLTF_Data {
data: GLTF_Data;
data.allocator = context.allocator;
if gltf_buffer == "" then return data;
if is_glb(gltf_buffer) {
return parse_glb_string(gltf_buffer);
}
success, root := json_parse_string(gltf_buffer);
defer json_free(root);
if !success then crash("JSON parsing failed.");
//
// asset.
//
asset := get(root, "asset", .OBJECT);
data.asset = .{
version = get_string(asset, "version"),
generator = get_string(asset, "generator"),
copyright = get_string(asset, "copyright"),
};
//
// nodes.
//
nodes := get_array(root, "nodes");
for node_parsed: nodes {
node: GLTF_Node;
defer array_add(*data.nodes, node);
node.name = get_string(node_parsed, "name");
node.skin = get_int(node_parsed, "skin", -1);
node.mesh = get_int(node_parsed, "mesh", -1);
node.camera = get_int(node_parsed, "camera", -1);
matrix, has_matrix := get_array(node_parsed, "matrix");
if has_matrix {
node.has_matrix = true;
for val: matrix node.matrix[it_index] = parse_float(val);
}
rotation, has_rotation := get_array(node_parsed, "rotation");
if has_rotation {
for val: rotation node.rotation[it_index] = parse_float(val);
}
scale, has_scale := get_array(node_parsed, "scale");
if has_scale {
for val: scale node.scale[it_index] = parse_float(val);
}
translation, has_trans := get_array(node_parsed, "translation");
if has_trans {
for val: translation node.translation[it_index] = parse_float(val);
}
node.local_transform = get_local_transform(*node);
children, has_children := get_array(node_parsed, "children");
if has_children {
for val: children array_add(*node.children, parse_int(val));
}
extensions, has_extension := get(node_parsed, "extensions", .OBJECT);
if has_extension {
khr_lights, has_khr_l := get(extensions, "KHR_lights_punctual", .OBJECT);
if has_khr_l then node.light = get_int(khr_lights, "light", -1);
}
}
//
// buffers.
//
buffers := get_array(root, "buffers");
for buffer_parsed: buffers {
buffer: GLTF_Buffer;
defer array_add(*data.buffers, buffer);
buffer.byte_length = get_int(buffer_parsed, "byteLength");
buffer.uri = get_string(buffer_parsed, "uri");
}
//
// buffer_views.
//
buffer_views := get_array(root, "bufferViews");
for buffer_view_parsed: buffer_views {
buffer_view: GLTF_Buffer_View;
defer array_add(*data.buffer_views, buffer_view);
target := get_int(buffer_view_parsed, "target");
buffer_view.target = cast(GLTF_Target) target;
buffer_view.buffer = get_int(buffer_view_parsed, "buffer", -1);
buffer_view.byte_length = get_int(buffer_view_parsed, "byteLength");
buffer_view.byte_offset = get_int(buffer_view_parsed, "byteOffset");
stride, has_stride := get_int(buffer_view_parsed, "byteStride");
if has_stride {
buffer_view.has_stride = true;
buffer_view.stride = stride;
}
}
//
// accessors.
//
accessors := get_array(root, "accessors");
for accessor_parsed: accessors {
accessor: GLTF_Accessor;
defer array_add(*data.accessors, accessor);
normalized, has_norm := get(accessor_parsed, "normalized", .BOOLEAN);
if has_norm then accessor.normalized = normalized.boolean;
accessor.buffer_view = get_int(accessor_parsed, "bufferView", -1);
accessor.byte_offset = get_int(accessor_parsed, "byteOffset");
accessor.count = get_int(accessor_parsed, "count");
comp_type := get_int(accessor_parsed, "componentType");
accessor.component_type = cast(GLTF_Component_Type) comp_type;
if get_string(accessor_parsed, "type") == {
case "SCALAR"; accessor.type = .SCALAR;
case "VEC2"; accessor.type = .VEC2;
case "VEC3"; accessor.type = .VEC3;
case "VEC4"; accessor.type = .VEC4;
case "MAT2"; accessor.type = .MAT2;
case "MAT3"; accessor.type = .MAT3;
case "MAT4"; accessor.type = .MAT4;
}
for value: get_array(accessor_parsed, "min") {
value_to_store := parse_float(value);
union_value := *accessor.min;
set_min_max_value(accessor.type, value_to_store, union_value, it_index);
}
for value: get_array(accessor_parsed, "max") {
value_to_store := parse_float(value);
union_value := *accessor.max;
set_min_max_value(accessor.type, value_to_store, union_value, it_index);
}
if accessor.buffer_view != -1 {
buffer_view := data.buffer_views[accessor.buffer_view];
if buffer_view.has_stride {
accessor.stride = buffer_view.stride;
continue;
}
}
accessor.stride = get_component_info(accessor).stride;
}
//
// scene(s).
//
data.scene = get_int(root, "scene", -1);
scenes := get_array(root, "scenes");
for scene_parsed: scenes {
scene: GLTF_Scene;
defer array_add(*data.scenes, scene);
scene.name = get_string(scene_parsed, "name");
nodes := get_array(scene_parsed, "nodes");
for nodes array_add(*scene.nodes, parse_int(it));
}
//
// skins.
//
skins := get_array(root, "skins");
for skins {
skin: GLTF_Skin;
defer array_add(*data.skins, skin);
skin.name = get_string(it, "name");
skin.skeleton = get_int(it, "skeleton", -1);
skin.inverse_bind_matrices = get_int(it, "inverseBindMatrices", -1);
joints := get_array(it, "joints");
for joints array_add(*skin.joints, parse_int(it));
}
//
// meshes.
//
meshes := get_array(root, "meshes");
for mesh_parsed: meshes {
mesh: GLTF_Mesh;
defer array_add(*data.meshes, mesh);
mesh.name = get_string(mesh_parsed, "name");
primitives := get_array(mesh_parsed, "primitives");
for prim_parsed: primitives {
primitive: GLTF_Primitive;
defer array_add(*mesh.primitives, primitive);
mode, ok := get_int(prim_parsed, "mode");
if !ok {
primitive.mode = .TRIANGLES;
} else {
primitive.mode = cast(GLTF_Mode) mode;
}
primitive.material = get_int(prim_parsed, "material", -1);
primitive.indices_accessor = get_int(prim_parsed, "indices", -1);
attributes := get(prim_parsed, "attributes", .OBJECT);
primitive.position_accessor = get_int(attributes, "POSITION", -1);
primitive.normal_accessor = get_int(attributes, "NORMAL", -1);
primitive.tangent_accessor = get_int(attributes, "TANGENT", -1);
primitive.color_accessor = get_int(attributes, "COLOR", -1);
primitive.joints_accessor = get_int(attributes, "JOINTS_0", -1);
primitive.weights_accessor = get_int(attributes, "WEIGHTS_0", -1);
primitive.texcoord_0_accessor = get_int(attributes, "TEXCOORD_0", -1);
primitive.texcoord_1_accessor = get_int(attributes, "TEXCOORD_1", -1);
primitive.texcoord_2_accessor = get_int(attributes, "TEXCOORD_2", -1);
}
}
//
// textures.
//
textures := get_array(root, "textures");
for texture_parsed : textures {
texture: GLTF_Texture;
defer array_add(*data.textures, texture);
texture.sampler = get_int(texture_parsed, "sampler", -1);
texture.source = get_int(texture_parsed, "source", -1);
}
//
// images.
//
images := get_array(root, "images");
for image_parsed : images {
image: GLTF_Image;
defer array_add(*data.images, image);
image.uri = get_string(image_parsed, "uri");
image.mime_type = get_string(image_parsed, "mimeType");
image.data = get_string(image_parsed, "data");
image.buffer_view = get_int(image_parsed, "bufferView", -1);
}
//
// samplers.
//
samplers := get_array(root, "samplers");
for sampler_parsed: samplers {
sampler: GLTF_Sampler;
defer array_add(*data.samplers, sampler);
wrap_s, has_wrap_s := get(sampler_parsed, "wrapS", .NUMBER);
if has_wrap_s then sampler.wrap_s = cast(GLTF_Wrap_Mode) parse_int(wrap_s);
wrap_t, has_wrap_t := get(sampler_parsed, "wrapT", .NUMBER);
if has_wrap_t then sampler.wrap_t = cast(GLTF_Wrap_Mode) parse_int(wrap_t);
mag_filter, has_mag := get(sampler_parsed, "magFilter", .NUMBER);
if has_mag {
sampler.mag_filter = cast(GLTF_Mag_Filter) parse_int(mag_filter);
}
min_filter, has_min := get(sampler_parsed, "minFilter", .NUMBER);
if has_min {
sampler.min_filter = cast(GLTF_Min_Filter) parse_int(min_filter);
}
}
//
// animations.
//
animations := get_array(root, "animations");
for animation_sampler: animations {
animation: GLTF_Animation;
defer array_add(*data.animations, animation);
animation.name = get_string(animation_sampler, "name");
samplers := get_array(animation_sampler, "samplers");
for sampler_parsed: samplers {
animation_sampler: GLTF_Animation_Sampler;
defer array_add(*animation.samplers, animation_sampler);
animation_sampler.input = get_int(sampler_parsed, "input", -1);
animation_sampler.output = get_int(sampler_parsed, "output", -1);
if get_string(sampler_parsed, "interpolation") == {
case "LINEAR";
animation_sampler.interpolation = .LINEAR;
case "STEP";
animation_sampler.interpolation = .STEP;
case "CUBICSPLINE";
animation_sampler.interpolation = .CUBICSPLINE;
}
}
channels := get_array(animation_sampler, "channels");
for channel_parsed: channels {
channel: GLTF_Channel;
defer array_add(*animation.channels, channel);
channel.sampler = get_int(channel_parsed, "sampler", -1);
target := get(channel_parsed, "target", .OBJECT);
channel.target.node = get_int(target, "node", -1);
if get_string(target, "path") == {
case "translation";
channel.target.property = .TRANSLATION;
case "rotation";
channel.target.property = .ROTATION;
case "scale";
channel.target.property = .SCALE;
case "weights";
channel.target.property = .WEIGHTS;
}
}
}
//
// materials
//
materials := get_array(root, "materials");
for material_parsed: materials {
material: GLTF_Material;
defer array_add(*data.materials, material);
material.name = get_string(material_parsed, "name");
//
// Metallic Roughness.
//
{
using material.metallic_roughness;
metal_rough := get(material_parsed, "pbrMetallicRoughness", .OBJECT);
color_factor := get_array(metal_rough, "baseColorFactor");
for color_factor base_color_factor[it_index] = parse_float(it);
{
texture, has_texture := parse_texture_info(metal_rough, "baseColorTexture");
if has_texture {
has_base_color = true;
base_color_texture = texture;
}
}
{
texture, has_texture := parse_texture_info(metal_rough, "metallicRoughnessTexture");
if has_texture {
has_metallic_roughness = true;
metallic_roughness_texture = texture;
}
}
metallic_factor = get_float(material_parsed, "metallicFactor");
roughness_factor = get_float(material_parsed, "roughnessFactor");
}
normal_texture, has_normal_texture := parse_texture_info(material_parsed, "normalTexture");
if has_normal_texture {
material.has_normal = true;
material.normal_texture = normal_texture;
}
occlu_texture, has_occlu_texture := parse_texture_info(material_parsed, "occlusionTexture");
if has_occlu_texture {
material.has_occlusion = true;
material.occlusion_texture = occlu_texture;
}
emiss_texture, has_emiss_texture := parse_texture_info(material_parsed, "emissiveTexture");
if has_emiss_texture {
material.has_emissive = true;
material.emissive_texture = emiss_texture;
}
if get_string(material_parsed, "alphaMode") == {
case "MASK";
material.alpha_mode = .MASK;
case "BLEND";
material.alpha_mode = .BLEND;
case "OPAQUE"; #through;
case;
material.alpha_mode = .OPAQUE;
}
material.alpha_cutoff = get_float(material_parsed, "alphaCutoff", 0.5);
is_double_sided := get(material_parsed, "doubleSided", .BOOLEAN);
material.is_double_sided = is_double_sided.boolean;
//
// Extensions
//
extensions, has_extension := get(material_parsed, "extensions", .OBJECT);
if has_extension {
// Parse khr_materials_emissive_strength extension.
khr_emissive_strength, has_emissive_str := get(extensions, "KHR_materials_emissive_strength", .OBJECT);
if has_emissive_str {
material.emissive_strength = get_float(khr_emissive_strength, "emissiveStrength");
}
// Parse khr_materials_ior extension.
khr_ior, has_ior := get(extensions, "KHR_materials_ior", .OBJECT);
if has_ior then material.ior = get_float(khr_ior, "ior");
// Parse khr_materials_transmission extension.
khr_transmission, has_transmission := get(extensions, "KHR_materials_transmission", .OBJECT);
if has_transmission {
material.transmission_factor = get_float(khr_transmission, "transmissionFactor");
transmission_texture, has_transmission_texture := parse_texture_info(khr_transmission, "transmissionTexture");
if has_transmission_texture {
material.has_transmission = true;
material.transmission_texture = transmission_texture;
}
}
}
}
//
// cameras.
//
cameras := get_array(root, "cameras");
for camera_parsed: cameras {
camera := GLTF_Camera.{ name = get_string(camera_parsed, "name") };
defer array_add(*data.cameras, camera);
type := get_string(camera_parsed, "type");
if type == {
case "perspective";
parsed := get(camera_parsed, "perspective", .OBJECT);
camera.type = .PERSPECTIVE;
camera.perspective = .{
aspect_ratio = get_float(parsed, "aspectRatio"),
y_fov = get_float(parsed, "yfov"),
z_far = get_float(parsed, "zfar"),
z_near = get_float(parsed, "znear"),
};
case "orthographic";
parsed := get(camera_parsed, "orthographic", .OBJECT);
camera.type = .ORTHOGRAPHIC;
camera.orthographic = .{
x_mag = get_float(parsed, "xmag"),
y_mag = get_float(parsed, "ymag"),
z_far = get_float(parsed, "zfar"),
z_near = get_float(parsed, "znear"),
};
}
}
//
// khr_lights_punctual.
//
extensions, has_extensions := get(root, "extensions", .OBJECT);
if has_extensions {
khr_lights, has_khr_l := get(extensions, "KHR_lights_punctual", .OBJECT);
if has_khr_l {
lights := get_array(khr_lights, "lights");
for light_parsed: lights {
light := GLTF_Light.{ name = get_string(light_parsed, "name") };
defer array_add(*data.lights, light);
color := get_array(light_parsed, "color");
for color light.color[it_index] = parse_float(it);
light.intensity = get_float(light_parsed, "intensity");
light.range = get_float(light_parsed, "range");
if get_string(light_parsed, "type") == {
case "directional";
light.type = .DIRECTIONAL;
case "point";
light.type = .POINT;
case "spot";
light.type = .SPOT;
spot := get(light_parsed, "spot", .OBJECT);
light.spot.inner_cone_angle = get_float(spot, "innerConeAngle");
light.spot.outer_cone_angle = get_float(spot, "outerConeAngle");
}
}
}
}
// Fill parents in nodes, to easily construct a hierarchy.
for scene: data.scenes {
for scene.nodes {
node := *data.nodes[it];
fill_parents(*data, node, it);
}
}
// Once all nodes are set, we could process the world_transform.
for * data.nodes {
it.world_transform = get_world_transform(*data, it);
}
return data;
}
gltf_parse_file :: (filepath: string) -> GLTF_Data {
path, basename, extension := path_decomp(filepath);
gltf_string, success := read_entire_file(filepath);
defer free(gltf_string);
if !success {
crash("Reading % failed. Are you sure that your file exists?", filepath);
}
gltf_data := ifx extension == "glb"
then parse_glb_string(gltf_string)
else gltf_parse_string(gltf_string);
gltf_data.filepath = copy_string(filepath);
return gltf_data;
}
gltf_load_buffers :: (gltf_data: *GLTF_Data) -> bool {
if gltf_data.is_glb return true;
folder_path := gltf_data.filepath;
folder_path.count = find_index_from_right(gltf_data.filepath, #char "/") + 1;
success := true;
for * buffer: gltf_data.buffers {
bin_path := join(folder_path, buffer.uri);
defer free(bin_path);
data, success := read_entire_file(bin_path ,, gltf_data.allocator);
if !success then success = false;
buffer.data = data;
}
return success;
}
// @todo: Should free the string.
gltf_debug_print :: (gltf_data: GLTF_Data) {
builder: String_Builder;
defer free_buffers(*builder);
PRINT_FORMAT :: #string DONE
=========
Debug information for glTF at %.
% scenes(s) was found.
% node(s) was found.
% camera(s) was found.
% buffer(s) was found.
% skin(s) was found.
% meshes(s) was found.
% images(s) was found.
DONE
print_to_builder(*builder, PRINT_FORMAT,
gltf_data.filepath,
gltf_data.scenes.count,
gltf_data.nodes.count,
gltf_data.cameras.count,
gltf_data.buffers.count,
gltf_data.skins.count,
gltf_data.meshes.count,
gltf_data.images.count);
append(*builder, "\n");
print_to_builder(*builder, " % animation(s) was found.\n", gltf_data.animations.count);
for gltf_data.animations {
print_to_builder(*builder, " - '%' (at index %)\n", it.name, it_index);
}
append(*builder, "\n\n");
message := builder_to_string(*builder ,, temp);
log(message);
}
GLTF_Component_Info :: struct {
size : int; // Component's size in bytes.
count : int; // Number of component.
stride : int; // Computed count * size.
}
get_component_info :: (accessor: GLTF_Accessor) -> GLTF_Component_Info {
info : GLTF_Component_Info;
if #complete accessor.component_type == {
case .BYTE; info.size = size_of(s8);
case .UNSIGNED_BYTE; info.size = size_of(u8);
case .SHORT; info.size = size_of(s16);
case .UNSIGNED_SHORT; info.size = size_of(u16);
case .UNSIGNED_INTEGER; info.size = size_of(u32);
case .FLOAT; info.size = size_of(float);
}
if #complete accessor.type == {
case .VEC2;
info.count = 2;
info.stride = 2 * info.size;
case .VEC3;
info.count = 3;
info.stride = 3 * info.size;
case .VEC4; #through;
case .MAT2;
info.count = 4;
info.stride = 4 * info.size;
case .MAT3;
info.count = 9;
info.stride = 9 * info.size;
case .MAT4;
info.count = 16;
info.stride = 16 * info.size;
case .SCALAR;
info.count = 1;
info.stride = info.size;
}
return info;
}
// @note: I would love to find a better way for doing that, if someone
// knows something, let me know!
read_buffer_from_accessor :: (gltf_data: *GLTF_Data, accessor: GLTF_Accessor, list: *[..] $type) {
if #complete accessor.component_type == {
case .BYTE;
temp := get_buffer_data(gltf_data, accessor, s8);
defer array_free(temp);
for temp array_add(list, cast(type) it);
case .UNSIGNED_BYTE;
temp := get_buffer_data(gltf_data, accessor, u8);
defer array_free(temp);
for temp array_add(list, cast(type) it);
case .SHORT;
temp := get_buffer_data(gltf_data, accessor, s16);
defer array_free(temp);
for temp array_add(list, cast(type) it);
case .UNSIGNED_SHORT;
temp := get_buffer_data(gltf_data, accessor, u16);
defer array_free(temp);
for temp array_add(list, cast(type) it);
case .UNSIGNED_INTEGER;
temp := get_buffer_data(gltf_data, accessor, u32);
defer array_free(temp);
for temp array_add(list, cast(type) it);
case .FLOAT;
temp := get_buffer_data(gltf_data, accessor, float);
defer array_free(temp);
for temp array_add(list, cast(type) it);
}
}
/*
*
* All glTF types are here!
* Types are fully documented, mostly copy/pasted from the glTF specs.
* @Note: https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html
*
*/
// A node in the node hierarchy.
//
// When the node contains skin, all mesh.primitives must contain
// JOINTS_0 and WEIGHTS_0 attributes. A node may have either a matrix
// or any combination of translation/rotation/scale (TRS) properties.
// TRS properties are converted to matrices and postmultiplied in
// the T * R * S order to compose the transformation matrix.
// If none are provided, the transform is the identity.
//
// When a node is targeted for animation, referenced by
// an animation.channel.target, matrix must not be present.
GLTF_Node :: struct {
// The user-defined name of this object.
name : string;
// The index of the node's parent.
// A node is called a root node when it doesn’t have a parent.
parent := -1;
world_transform := Matrix4_Identity;
local_transform := Matrix4_Identity;
// The index of the mesh in this node.
mesh := -1;
// The index of the camera referenced by this node.
camera := -1;
// The index of the skin referenced by this node.
skin := -1;
// The indices of this node’s children.
children : [..] int;
// A floating-point 4x4 transformation matrix stored in column-major order.
has_matrix := false;
matrix := float.[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
// The node’s unit quaternion rotation in the order (x, y, z, w),
// where w is the scalar.
has_rotation := false;
rotation := float.[0, 0, 0, 1];
// The node’s non-uniform scale, given as the scaling factors
// along the x, y, and z axes.
has_scale := false;
scale := float.[1, 1, 1];
// The node’s translation along the x, y, and z axes.
has_translation := false;
translation := float.[0, 0, 0];
// The weights of the instantiated morph target.
// The number of array elements must match the number of morph targets
// of the referenced mesh. When defined, mesh mush also be defined.
weights := -1;
//The index of the light referenced by this node.
light := -1;
}
// A buffer points to binary geometry, animation, or skins.
GLTF_Buffer :: struct {
// Relative paths are relative to the current glTF asset.
// It could contains a data:-URI instead of a path.
// Note: data-uri isn't implemented in this library.
uri : string;
// The length of the buffer in bytes.
byte_length : int;
// Buffer's data once gltf_load_data was called.
data : string;
}
// A view into a buffer generally representing a subset of the buffer.
GLTF_Buffer_View :: struct {
// The index of the buffer.
buffer := -1;
// The length of the bufferView in bytes.
byte_length : int;
// The offset into the buffer in bytes.
byte_offset : int;
// The stride, in bytes.
has_stride := false;
stride : int;
// The hint representing the intended GPU buffer type
// to use with this buffer view.
target: GLTF_Target;
}
/// A typed view into a buffer view that contains raw binary data.
GLTF_Accessor :: struct {
// The index of the bufferView.
buffer_view := -1;
// The offset relative to the start of the buffer view in bytes.
byte_offset := 0;
// The datatype of the accessor’s components.
component_type : GLTF_Component_Type;
// Specifies if the accessor’s elements are scalars, vectors, or matrices.
type : GLTF_Accessor_Type;
// Computed stride: size_of(component_type) * type.
// @Note: If buffer_view is set, it's the buffer_view's stride.
stride := 0;
// The number of elements referenced by this accessor.
count := 0;
// Specifies whether integer data values are normalized before usage.
normalized := false;
// Contain per-component minimum and maximum values.
// @todo: Would be better with tagged union for this.
min : GLTF_Min_Max_Value;
max : GLTF_Min_Max_Value;
}
// Refer to a minimum or maximum value.
GLTF_Min_Max_Value :: union {
scalar: float;
vec2: Vector2;
vec3: Vector3;
vec4: Vector4;
mat2: Matrix2;
mat3: Matrix3;
mat4: Matrix4;
}
// The root nodes of a scene.
GLTF_Scene :: struct {
// The user-defined name of this object.
name : string;
// The indices of each root node.
nodes : [..] int;
}
/// Joints and matrices defining a skin.
GLTF_Skin :: struct {
// The user-defined name of this object.
name: string;
// The index of the accessor containing the floating-point
// 4x4 inverse-bind matrices.
inverse_bind_matrices := -1;
// The index of the node used as a skeleton root.
skeleton := -1;
// Indices of skeleton nodes, used as joints in this skin.
joints : [..] int;
}
// Reference to a material texture.
// @Note: It could be a normal/occlusion/color texture!
GLTF_Texture_Info :: struct {
// The index of the texture.
index := -1;
// The set index of texture’s TEXCOORD attribute
// used for texture coordinate mapping.
texcoord := 0;
// The scalar parameter applied to each normal
// vector of the normal texture.
// @Note: Only if normal texture.
normal_scale := 1.0;
// A scalar multiplier controlling the amount of occlusion applied.
// @Note: Only if occlusion texture.
strength := 1.;
has_khr_texture_transform : bool;
khr_texture_transform : struct {
// The offset of the UV coordinate origin as a factor of the texture dimensions.
offset := float.[0, 0];
// Rotate the UVs by this many radians counter-clockwise around the origin.
// This is equivalent to a similar rotation of the image clockwise.
rotation : float;
// The scale factor applied to the components of the UV coordinates.
scale := float.[1., 1.];
// Overrides the textureInfo texCoord value if supplied,
// and if this extension is supported.
texcoord := -1;
};
}
// A set of parameter values that are used to define
// the metallic-roughness material model
// from Physically-Based Rendering methodology.
GLTF_Metallic_Roughness :: struct {
// The factors for the base color of the material.
base_color_factor := float.[1, 1, 1, 1];
// The base color texture.
has_base_color := false;
base_color_texture : GLTF_Texture_Info;
// The factor for the metalness of the material.
metallic_factor := 1.;
// The factor for the roughness of the material.
roughness_factor := 1.;
// The metallic-roughness texture.
has_metallic_roughness := false;
metallic_roughness_texture : GLTF_Texture_Info;
}
// The material appearance of a primitive.
GLTF_Material :: struct {
// The user-defined name of this object.
name: string;
// A set of parameter values that are used to define
// the metallic-roughness material model
// from Physically Based Rendering methodology.
metallic_roughness := GLTF_Metallic_Roughness.{};
// The tangent space normal texture.
has_normal := false;
normal_texture : GLTF_Texture_Info;
// The occlusion texture.
has_occlusion := false;
occlusion_texture : GLTF_Texture_Info;
// The emissive texture.
has_emissive := false;
emissive_texture : GLTF_Texture_Info;
// The factors for the emissive color of the material.
emissive_factor := float.[0, 0, 0];
// The alpha rendering mode of the material.
alpha_mode := GLTF_Alpha_Mode.OPAQUE;
// The alpha cutoff value of the material.
alpha_cutoff := 0.5;
// Specifies whether the material is double sided.
// If it's false, back-face culling is enabled.
// If it's true, back-face culling is disabled and
// double sided lighting is enabled.
is_double_sided := false;
// Emissive strength multiplier for the emissive factor/texture.
// @Note: from khr_materials_emissive_strength extension.
emissive_strength := 1.0;
// Index of refraction of material.
// @Note: from khr_materials_ior extension.
ior := 1.5;
// The factor for the transmission of the material.
// @Note: from khr_materials_transmission extension.