-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.odin
1230 lines (1064 loc) · 42.2 KB
/
model.odin
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
package zephr
import "base:intrinsics"
import "core:fmt"
import "core:log"
import "core:math"
import m "core:math/linalg/glsl"
import "core:mem"
import "core:mem/virtual"
import "core:strings"
import "core:time"
import gl "vendor:OpenGL"
import "vendor:cgltf"
#assert(size_of(Vertex) == 96)
Vertex :: struct {
position: m.vec3,
normal: m.vec3,
tex_coords: m.vec2,
tangents: m.vec4,
joints: [4]u32,
weights: m.vec4,
color: m.vec4,
}
@(private)
Mesh :: struct {
primitive: cgltf.primitive_type,
vertices: [dynamic]Vertex,
indices: []u32,
parent: ^Node,
material_id: uintptr,
weights: []f32,
morph_targets_tex: TextureId,
morph_normals_offset: int,
morph_tangents_offset: int,
joint_matrices_buf: u32,
joint_matrices_tex: TextureId,
vao: u32,
vbo: u32,
ebo: u32,
morph_weights_buf: u32,
morph_weights_tex: TextureId,
obb: OBB,
}
Node :: struct {
name: string,
parent: ^Node,
is_bone: bool,
joints: []^Node,
skeleton: ^Node,
meshes: []Mesh,
transform: m.mat4,
has_transform: bool,
world_transform: m.mat4,
scale: m.vec3,
translation: m.vec3,
rotation: m.quat,
children: []^Node,
inverse_bind_matrices: []m.mat4,
}
Model :: struct {
nodes: []^Node,
materials: map[uintptr]Material,
arena: virtual.Arena,
animations: []Animation,
active_animation: ^Animation,
obb: OBB,
}
node_name_idx: ^int
bone_name_idx: ^int
// BUG: There seems to be a bug with how we're handling the hierarchy for skins/joints or something, a blender export doesn't move the skin/bones
// when we move the root node. The same model works fine in babylonjs, when I exported from babylon it works fine in the engine
// now, which makes me think there's a chance it's a blender export issue. But also means my importer should do a better job.
// Related: https://github.com/KhronosGroup/glTF-Blender-IO/issues/1626
@(private = "file")
process_sparse_accessor_vec2 :: proc(accessor: ^cgltf.accessor_sparse, data_out: []f32) {
indices_byte_offset := accessor.indices_byte_offset + accessor.indices_buffer_view.offset
values_byte_offset := accessor.values_byte_offset + accessor.values_buffer_view.offset
sparse_values := intrinsics.ptr_offset(
cast([^]f32)accessor.values_buffer_view.buffer.data,
values_byte_offset / size_of(f32),
)
indices := make([]u32, accessor.count)
defer delete(indices)
process_indices(accessor.indices_buffer_view, accessor.indices_component_type, indices_byte_offset, indices)
for idx, i in indices {
data_out[idx * 2] = sparse_values[i * 2]
data_out[idx * 2 + 1] = sparse_values[i * 2 + 1]
}
}
@(private = "file")
process_sparse_accessor_vec3 :: proc(accessor: ^cgltf.accessor_sparse, data_out: []f32) {
indices_byte_offset := accessor.indices_byte_offset + accessor.indices_buffer_view.offset
values_byte_offset := accessor.values_byte_offset + accessor.values_buffer_view.offset
sparse_values := intrinsics.ptr_offset(
cast([^]f32)accessor.values_buffer_view.buffer.data,
values_byte_offset / size_of(f32),
)
indices := make([]u32, accessor.count)
defer delete(indices)
process_indices(accessor.indices_buffer_view, accessor.indices_component_type, indices_byte_offset, indices)
for idx, i in indices {
data_out[idx * 3] = sparse_values[i * 3]
data_out[idx * 3 + 1] = sparse_values[i * 3 + 1]
data_out[idx * 3 + 2] = sparse_values[i * 3 + 2]
}
}
@(private = "file")
process_sparse_accessor_vec4 :: proc(accessor: ^cgltf.accessor_sparse, data_out: []f32) {
indices_byte_offset := accessor.indices_byte_offset + accessor.indices_buffer_view.offset
values_byte_offset := accessor.values_byte_offset + accessor.values_buffer_view.offset
sparse_values := intrinsics.ptr_offset(
cast([^]f32)accessor.values_buffer_view.buffer.data,
values_byte_offset / size_of(f32),
)
indices := make([]u32, accessor.count)
defer delete(indices)
process_indices(accessor.indices_buffer_view, accessor.indices_component_type, indices_byte_offset, indices)
for idx, i in indices {
data_out[idx * 4] = sparse_values[i * 4]
data_out[idx * 4 + 1] = sparse_values[i * 4 + 1]
data_out[idx * 4 + 2] = sparse_values[i * 4 + 2]
data_out[idx * 4 + 3] = sparse_values[i * 4 + 3]
}
}
@(private = "file")
process_accessor_vec2 :: proc(accessor: ^cgltf.accessor, data_out: []f32) {
byte_offset := accessor.offset
if accessor.buffer_view != nil {
byte_offset += accessor.buffer_view.offset
buf := intrinsics.ptr_offset(cast([^]f32)accessor.buffer_view.buffer.data, byte_offset / size_of(f32))
if accessor.buffer_view.stride == 0 {
copy(data_out, buf[:accessor.count * 2])
} else {
for i in 0 ..< accessor.count {
data_out[i * 2] = buf[i * accessor.buffer_view.stride / size_of(f32)]
data_out[i * 2 + 1] = buf[i * (accessor.buffer_view.stride / size_of(f32)) + 1]
}
}
if accessor.is_sparse {
process_sparse_accessor_vec2(&accessor.sparse, data_out)
}
} else {
if accessor.is_sparse {
process_sparse_accessor_vec2(&accessor.sparse, data_out)
} else {
log.error("Got a buffer view that is nil and not sparse. Confused on what to do")
}
}
}
@(private = "file")
process_accessor_vec3 :: proc(accessor: ^cgltf.accessor, data_out: []f32) {
byte_offset := accessor.offset
if accessor.buffer_view != nil {
byte_offset += accessor.buffer_view.offset
buf := intrinsics.ptr_offset(cast([^]f32)accessor.buffer_view.buffer.data, byte_offset / size_of(f32))
if accessor.buffer_view.stride == 0 {
copy(data_out, buf[:accessor.count * 3])
} else {
for i in 0 ..< accessor.count {
data_out[i * 3] = buf[i * accessor.buffer_view.stride / size_of(f32)]
data_out[i * 3 + 1] = buf[i * (accessor.buffer_view.stride / size_of(f32)) + 1]
data_out[i * 3 + 2] = buf[i * (accessor.buffer_view.stride / size_of(f32)) + 2]
}
}
if accessor.is_sparse {
process_sparse_accessor_vec3(&accessor.sparse, data_out)
}
} else {
if accessor.is_sparse {
process_sparse_accessor_vec3(&accessor.sparse, data_out)
} else {
log.error("Got a buffer view that is nil and not sparse. Confused on what to do")
}
}
}
@(private = "file")
process_accessor_vec4 :: proc(accessor: ^cgltf.accessor, data_out: []f32) {
byte_offset := accessor.offset
if accessor.buffer_view != nil {
byte_offset += accessor.buffer_view.offset
buf := intrinsics.ptr_offset(cast([^]f32)accessor.buffer_view.buffer.data, byte_offset / size_of(f32))
if accessor.buffer_view.stride == 0 {
copy(data_out, buf[:accessor.count * 4])
} else {
for i in 0 ..< accessor.count {
data_out[i * 4] = buf[i * accessor.buffer_view.stride / size_of(f32)]
data_out[i * 4 + 1] = buf[i * (accessor.buffer_view.stride / size_of(f32)) + 1]
data_out[i * 4 + 2] = buf[i * (accessor.buffer_view.stride / size_of(f32)) + 2]
data_out[i * 4 + 3] = buf[i * (accessor.buffer_view.stride / size_of(f32)) + 3]
}
}
if accessor.is_sparse {
process_sparse_accessor_vec4(&accessor.sparse, data_out)
}
} else {
if accessor.is_sparse {
process_sparse_accessor_vec4(&accessor.sparse, data_out)
} else {
log.error("Got a buffer view that is nil and not sparse. Confused on what to do")
}
}
}
@(private = "file")
process_joints :: proc(accessor: ^cgltf.accessor, data_out: []u32) {
byte_offset := accessor.offset + accessor.buffer_view.offset
if accessor.is_sparse {
log.error("Sparse joints accessors are not supported yet.")
}
#partial switch accessor.component_type {
case .r_8u:
buf := intrinsics.ptr_offset(cast([^]u8)accessor.buffer_view.buffer.data, byte_offset / size_of(u8))
for i in 0 ..< accessor.count {
offset := accessor.buffer_view.stride == 0 ? i * 4 : i * (accessor.buffer_view.stride / size_of(u8))
data_out[i * 4 + 0] = cast(u32)buf[offset + 0]
data_out[i * 4 + 1] = cast(u32)buf[offset + 1]
data_out[i * 4 + 2] = cast(u32)buf[offset + 2]
data_out[i * 4 + 3] = cast(u32)buf[offset + 3]
}
case .r_16u:
buf := intrinsics.ptr_offset(cast([^]u16)accessor.buffer_view.buffer.data, byte_offset / size_of(u16))
for i in 0 ..< accessor.count {
offset := accessor.buffer_view.stride == 0 ? i * 4 : i * (accessor.buffer_view.stride / size_of(u16))
data_out[i * 4 + 0] = cast(u32)buf[offset + 0]
data_out[i * 4 + 1] = cast(u32)buf[offset + 1]
data_out[i * 4 + 2] = cast(u32)buf[offset + 2]
data_out[i * 4 + 3] = cast(u32)buf[offset + 3]
}
case .r_32u:
buf := intrinsics.ptr_offset(cast([^]u32)accessor.buffer_view.buffer.data, byte_offset / size_of(u32))
for i in 0 ..< accessor.count {
offset := accessor.buffer_view.stride == 0 ? i * 4 : i * (accessor.buffer_view.stride / size_of(u32))
data_out[i * 4 + 0] = buf[offset + 0]
data_out[i * 4 + 1] = buf[offset + 1]
data_out[i * 4 + 2] = buf[offset + 2]
data_out[i * 4 + 3] = buf[offset + 3]
}
}
}
@(private = "file")
process_accessor_mat4 :: proc(accessor: ^cgltf.accessor, data_out: []m.mat4) {
byte_offset := accessor.offset + accessor.buffer_view.offset
buf := intrinsics.ptr_offset(cast([^]f32)accessor.buffer_view.buffer.data, byte_offset / size_of(f32))
stride := accessor.stride / size_of(f32) // 16 floats, 64 bytes always ??
for i in 0 ..< accessor.count {
data_out[i] = m.mat4 {
buf[i * stride + 0],
buf[i * stride + 4],
buf[i * stride + 8],
buf[i * stride + 12],
buf[i * stride + 1],
buf[i * stride + 5],
buf[i * stride + 9],
buf[i * stride + 13],
buf[i * stride + 2],
buf[i * stride + 6],
buf[i * stride + 10],
buf[i * stride + 14],
buf[i * stride + 3],
buf[i * stride + 7],
buf[i * stride + 11],
buf[i * stride + 15],
}
}
if accessor.is_sparse {
log.error("Sparse mat4 accessors are not supported yet.")
}
}
@(private = "file")
process_accessor_scalar_float :: proc(accessor: ^cgltf.accessor, data_out: []f32) {
byte_offset := accessor.offset + accessor.buffer_view.offset
buf := intrinsics.ptr_offset(cast([^]f32)accessor.buffer_view.buffer.data, byte_offset / size_of(f32))
if accessor.buffer_view.stride == 0 {
copy(data_out, buf[:accessor.count])
} else {
for i in 0 ..< accessor.count {
data_out[i] = buf[i * accessor.buffer_view.stride / size_of(f32)]
}
}
if accessor.is_sparse {
log.error("Sparse scalar float accessors are not supported yet.")
}
// TODO: sparse??
}
@(private = "file")
process_indices :: proc(
buffer_view: ^cgltf.buffer_view,
type: cgltf.component_type,
byte_offset: uint,
indices_out: []u32,
) {
if buffer_view.stride != 0 {
// TODO: support stride
log.error("We don't support non zero stride for indices. Faces might not look correct")
}
#partial switch type {
case .r_8u:
start := byte_offset / size_of(u8)
end := start + len(indices_out)
ptr_arr := (cast([^]u8)buffer_view.buffer.data)[start:end]
for i in 0 ..< len(ptr_arr) {
indices_out[i] = cast(u32)ptr_arr[i]
}
case .r_16u:
start := byte_offset / size_of(u16)
end := start + len(indices_out)
ptr_arr := (cast([^]u16)buffer_view.buffer.data)[start:end]
for i in 0 ..< len(ptr_arr) {
indices_out[i] = cast(u32)ptr_arr[i]
}
case .r_32u:
start := byte_offset / size_of(u32)
end := start + len(indices_out)
copy(indices_out, (cast([^]u32)buffer_view.buffer.data)[start:end])
case:
log.error("Unsupported index type")
}
}
@(private = "file")
process_vertex_colors :: proc(accessor: ^cgltf.accessor, color_out: ^[]f32) {
// TODO: there can be multiple sets of vertex colors.
// TODO: support when buffer_view is nil ?? (does this even happen for vert colors?)
color_out := color_out
color_out^ = make([]f32, accessor.count * 4)
byte_offset := accessor.offset + accessor.buffer_view.offset
if accessor.buffer_view == nil {
log.error("Nil buffer views for vertex colors are not supported.")
}
if accessor.type == .vec3 {
#partial switch accessor.component_type {
case .r_32f:
buf := intrinsics.ptr_offset(cast([^]f32)accessor.buffer_view.buffer.data, byte_offset / size_of(f32))
stride := accessor.stride / size_of(f32)
for i in 0..<accessor.count {
color_out[i * 4 + 0] = buf[i * stride + 0]
color_out[i * 4 + 1] = buf[i * stride + 1]
color_out[i * 4 + 2] = buf[i * stride + 2]
color_out[i * 4 + 3] = 1.0
}
case .r_8u:
buf := intrinsics.ptr_offset(cast([^]u8)accessor.buffer_view.buffer.data, byte_offset / size_of(u8))
stride := accessor.stride / size_of(u8)
for i in 0..<accessor.count {
color_out[i * 4 + 0] = cast(f32)buf[i * stride + 0] / 255.0
color_out[i * 4 + 1] = cast(f32)buf[i * stride + 1] / 255.0
color_out[i * 4 + 2] = cast(f32)buf[i * stride + 2] / 255.0
color_out[i * 4 + 3] = 1.0
}
case .r_16u:
buf := intrinsics.ptr_offset(cast([^]u16)accessor.buffer_view.buffer.data, byte_offset / size_of(u16))
stride := accessor.stride / size_of(u16)
for i in 0..<accessor.count {
color_out[i * 4 + 0] = cast(f32)buf[i * stride + 0] / 65535.0
color_out[i * 4 + 1] = cast(f32)buf[i * stride + 1] / 65535.0
color_out[i * 4 + 2] = cast(f32)buf[i * stride + 2] / 65535.0
color_out[i * 4 + 3] = 1.0
}
case:
log.error("Unsupported vertex color type")
}
} else {
#partial switch accessor.component_type {
case .r_32f:
buf := intrinsics.ptr_offset(cast([^]f32)accessor.buffer_view.buffer.data, byte_offset / size_of(f32))
copy(color_out^, buf[:accessor.count * 4])
case .r_8u:
buf := intrinsics.ptr_offset(cast([^]u8)accessor.buffer_view.buffer.data, byte_offset / size_of(u8))
stride := accessor.stride / size_of(u8)
for i in 0..<accessor.count {
color_out[i * 4 + 0] = cast(f32)buf[i * stride + 0] / 255.0
color_out[i * 4 + 1] = cast(f32)buf[i * stride + 1] / 255.0
color_out[i * 4 + 2] = cast(f32)buf[i * stride + 2] / 255.0
color_out[i * 4 + 3] = cast(f32)buf[i * stride + 3] / 255.0
}
case .r_16u:
buf := intrinsics.ptr_offset(cast([^]u16)accessor.buffer_view.buffer.data, byte_offset / size_of(u16))
stride := accessor.stride / size_of(u16)
for i in 0..<accessor.count {
color_out[i * 4 + 0] = cast(f32)buf[i * stride + 0] / 65535.0
color_out[i * 4 + 1] = cast(f32)buf[i * stride + 1] / 65535.0
color_out[i * 4 + 2] = cast(f32)buf[i * stride + 2] / 65535.0
color_out[i * 4 + 3] = cast(f32)buf[i * stride + 3] / 65535.0
}
case:
log.error("Unsupported vertex color type")
}
}
}
@(private = "file")
process_mesh :: proc(
primitive: ^cgltf.primitive,
materials: ^map[uintptr]Material,
model_path: string,
parent: ^Node,
textures: ^map[cstring]TextureId,
weights_len: int,
joints_len: int,
allocator: ^mem.Allocator,
) -> (
Mesh,
bool,
) {
if primitive.indices == nil {
log.error("No indices found")
return Mesh{}, false
}
vertices := make([dynamic]Vertex, 0, 256, allocator^)
material_id: uintptr = 0
primitive_type := primitive.type
if primitive.type != .triangles {
log.warn("Got a primitive that isn't triangles")
}
if primitive.material != nil {
material_id = transmute(uintptr)primitive.material
if !(material_id in materials) {
materials[material_id] = process_material(primitive.material, model_path, textures, allocator)
}
}
positions: []f32
defer delete(positions)
normals: []f32
defer delete(normals)
texcoords: []f32
defer delete(texcoords)
tangents: []f32
defer delete(tangents)
joints: []u32
defer delete(joints)
weights: []f32
defer delete(weights)
colors: []f32
defer delete(colors)
has_obb := false
mesh_obb := OBB{
min = {math.INF_F32, math.INF_F32, math.INF_F32},
max = {math.NEG_INF_F32, math.NEG_INF_F32, math.NEG_INF_F32},
}
for a in 0 ..< len(primitive.attributes) {
attribute := primitive.attributes[a]
accessor := attribute.data
#partial switch attribute.type {
case .position:
positions = make([]f32, accessor.count * 3)
if accessor.has_min && accessor.has_max {
mesh_obb.min = m.min(mesh_obb.min, m.vec3{accessor.min[0], accessor.min[1], accessor.min[2]})
mesh_obb.max = m.max(mesh_obb.max, m.vec3{accessor.max[0], accessor.max[1], accessor.max[2]})
has_obb = true
}
process_accessor_vec3(accessor, positions)
case .normal:
normals = make([]f32, accessor.count * 3)
process_accessor_vec3(accessor, normals)
case .texcoord:
// BUG: we leak memory here for models that have mutiple texcoords.
// Same with the joints and weights attributes.
texcoords = make([]f32, accessor.count * 2)
process_accessor_vec2(accessor, texcoords)
case .tangent:
// This is explicitly defined as a vec4 in the spec
tangents = make([]f32, accessor.count * 4)
process_accessor_vec4(accessor, tangents)
case .joints:
joints = make([]u32, accessor.count * 4)
process_joints(accessor, joints)
case .weights:
weights = make([]f32, accessor.count * 4)
process_accessor_vec4(accessor, weights)
case .color:
process_vertex_colors(accessor, &colors)
}
}
if len(normals) == 0 {
log.warn("No normals found. Falling back to flat shading")
}
if len(tangents) == 0 {
// TODO: calculate tangents using mikkTSpace??
// Not sure if worth because I can just export with tangents from blender.
log.warn("No tangents found. Normal mapping will not be applied")
}
// TODO: different pipeline and mesh shader for static meshes to reduce GPU memory usage
// Instead of a different shader or pipeline we can use preprocessor directives to remove
// any attribute that is not used for the specific mesh. This requires that we implement some sort of
// runtime shader modification stuff or something.
for i in 0 ..< len(positions) / 3 {
pos := m.vec3{positions[i * 3], positions[i * 3 + 1], positions[i * 3 + 2]}
tex_coords := len(texcoords) != 0 ? m.vec2{texcoords[i * 2], texcoords[i * 2 + 1]} : m.vec2{0, 0}
normal := len(normals) != 0 ? m.vec3{normals[i * 3], normals[i * 3 + 1], normals[i * 3 + 2]} : m.vec3{0, 0, 0}
tangents :=
len(tangents) != 0 \
? m.vec4{tangents[i * 4], tangents[i * 4 + 1], tangents[i * 4 + 2], tangents[i * 4 + 3]} \
: m.vec4{0, 0, 0, 0}
joints :=
len(joints) != 0 \
? [4]u32{joints[i * 4], joints[i * 4 + 1], joints[i * 4 + 2], joints[i * 4 + 3]} \
: [4]u32{0, 0, 0, 0}
weights :=
len(weights) != 0 \
? m.vec4{weights[i * 4], weights[i * 4 + 1], weights[i * 4 + 2], weights[i * 4 + 3]} \
: m.vec4{0, 0, 0, 0}
color :=
len(colors) != 0 \
? m.vec4{colors[i * 4], colors[i * 4 + 1], colors[i * 4 + 2], colors[i * 4 + 3]} \
: m.vec4{0, 0, 0, 0}
if !has_obb {
mesh_obb.min = m.min(mesh_obb.min, pos)
mesh_obb.max = m.max(mesh_obb.max, pos)
}
append(
&vertices,
Vertex {
position = pos,
normal = normal,
tex_coords = tex_coords,
tangents = tangents,
joints = joints,
weights = weights,
color = color,
},
)
}
morph_attribute_count := 0
found_pos := false
found_norm := false
found_tan := false
tex_width := math.ceil(math.sqrt(cast(f32)len(vertices)))
single_texture_size := cast(int)math.pow(tex_width, 2) * 3 // 3 for vec3
for target in primitive.targets {
for attr in target.attributes {
if attr.type == .position && !found_pos {
morph_attribute_count += 1
found_pos = true
} else if attr.type == .normal && !found_norm {
morph_attribute_count += 1
found_norm = true
} else if attr.type == .tangent && !found_tan {
morph_attribute_count += 1
found_tan = true
}
}
}
morph_normals_offset := found_pos ? len(primitive.targets) * single_texture_size : 0
// Breaks if there're no positions but there is normals
morph_tangents_offset := found_pos ? len(primitive.targets) * single_texture_size + morph_normals_offset : 0
morph_targets := make([]f32, single_texture_size * len(primitive.targets) * morph_attribute_count)
defer delete(morph_targets)
// The layout for morph_targets is POS, NORM, TAN
for target, i in primitive.targets {
for attr in target.attributes {
accessor := attr.data
#partial switch attr.type {
case .position:
offset := i * single_texture_size
process_accessor_vec3(accessor, morph_targets[offset:cast(uint)offset + accessor.count * 3])
case .normal:
offset := i * single_texture_size + morph_normals_offset
process_accessor_vec3(accessor, morph_targets[offset:cast(uint)offset + accessor.count * 3])
case .tangent:
offset := i * single_texture_size + morph_tangents_offset
// Morph tangents are explicitly defined as a vec3 in the spec
process_accessor_vec3(accessor, morph_targets[offset:cast(uint)offset + accessor.count * 3])
}
}
}
indices := make([]u32, primitive.indices.count, allocator^)
offset_into_buffer := primitive.indices.buffer_view.offset
offset_into_buf_view := primitive.indices.offset
process_indices(
primitive.indices.buffer_view,
primitive.indices.component_type,
offset_into_buffer + offset_into_buf_view,
indices,
)
return new_mesh(
primitive_type,
vertices,
indices,
parent,
material_id,
weights_len,
joints_len,
morph_targets,
morph_attribute_count,
morph_normals_offset,
morph_tangents_offset,
mesh_obb,
allocator,
),
true
}
@(private = "file")
process_node :: proc(
node: ^cgltf.node,
parent: ^Node,
materials: ^map[uintptr]Material,
model_path: string,
textures: ^map[cstring]TextureId,
bones: ^map[uintptr]bool,
nodes_map: ^map[uintptr]^Node,
allocator: ^mem.Allocator,
) -> ^Node {
id := transmute(uintptr)node
if id in nodes_map {
return nodes_map[id]
}
new_node := new(Node, allocator^)
name := strings.clone_from_cstring(node.name, allocator^)
is_bone := id in bones
if node.name == nil {
if is_bone {
name = fmt.aprintf("Bone %d", bone_name_idx^, allocator = allocator^)
bone_name_idx^ += 1
} else {
name = fmt.aprintf("Node %d", node_name_idx^, allocator = allocator^)
node_name_idx^ += 1
}
}
transform := m.identity(m.mat4)
translation := m.vec3{0, 0, 0}
rotation := cast(m.quat)quaternion(x = 0, y = 0, z = 0, w = 1)
scale := m.vec3{1, 1, 1}
if node.has_matrix {
transform = m.mat4 {
node.matrix_[0],
node.matrix_[4],
node.matrix_[8],
node.matrix_[12],
node.matrix_[1],
node.matrix_[5],
node.matrix_[9],
node.matrix_[13],
node.matrix_[2],
node.matrix_[6],
node.matrix_[10],
node.matrix_[14],
node.matrix_[3],
node.matrix_[7],
node.matrix_[11],
node.matrix_[15],
}
} else {
if node.has_scale {
scale = m.vec3(node.scale)
}
if node.has_rotation {
rotation =
cast(m.quat)quaternion(w = node.rotation.w, x = node.rotation.x, y = node.rotation.y, z = node.rotation.z)
}
if node.has_translation {
translation = m.vec3(node.translation)
}
}
inverse_bind_matrices: []m.mat4
joints: []^Node
skeleton: ^Node = nil
if node.skin != nil {
if node.skin.inverse_bind_matrices != nil {
inverse_bind_matrices = make([]m.mat4, len(node.skin.joints), allocator^)
process_accessor_mat4(node.skin.inverse_bind_matrices, inverse_bind_matrices)
} else {
log.warn(
"Found a skin with no inverse bind matrices. We don't support generating the matrices ourselves yet.",
)
// TODO: calculate the inverse bind matrices ourselves
}
joints = make([]^Node, len(node.skin.joints), allocator^)
for joint, i in node.skin.joints {
joints[i] = process_node(joint, nil, materials, model_path, textures, bones, nodes_map, allocator)
}
if node.skin.skeleton != nil {
skeleton = process_node(
node.skin.skeleton,
nil,
materials,
model_path,
textures,
bones,
nodes_map,
allocator,
)
}
}
meshes: []Mesh
if node.mesh != nil {
meshes = make([]Mesh, len(node.mesh.primitives), allocator^)
// we consider primitives to be different meshes
for idx in 0 ..< len(node.mesh.primitives) {
mesh, ok := process_mesh(
&node.mesh.primitives[idx],
materials,
model_path,
new_node,
textures,
len(node.mesh.weights),
len(joints),
allocator,
)
if ok {
copy(mesh.weights, node.mesh.weights)
meshes[idx] = mesh
}
}
}
children := make([]^Node, len(node.children), allocator^)
for idx in 0 ..< len(node.children) {
child := node.children[idx]
children[idx] = process_node(child, new_node, materials, model_path, textures, bones, nodes_map, allocator)
}
new_node.name = name
new_node.parent = parent
new_node.is_bone = is_bone
new_node.joints = joints
new_node.skeleton = skeleton
new_node.meshes = meshes
new_node.transform = transform
new_node.has_transform = cast(bool)node.has_matrix
new_node.world_transform = m.identity(m.mat4)
new_node.scale = scale
new_node.translation = translation
new_node.rotation = rotation
new_node.children = children
new_node.inverse_bind_matrices = inverse_bind_matrices
nodes_map[id] = new_node
return new_node
}
@(private = "file")
new_mesh :: proc(
primitive: cgltf.primitive_type,
vertices: [dynamic]Vertex,
indices: []u32,
parent: ^Node,
material_id: uintptr,
weights_len: int,
joints_len: int,
morph_targets: []f32,
morph_attribute_count: int,
morph_normals_offset: int,
morph_tangents_offset: int,
obb: OBB,
allocator: ^mem.Allocator,
) -> Mesh {
vao, vbo, ebo: u32
morph_texture, morph_weights_texture, morph_weights_buf: u32
joint_matrices_buf, joint_matrices_texture: u32
usage: u32 = gl.STATIC_DRAW
if len(morph_targets) != 0 {
temp_max_tex_size, temp_max_tex_array_size: i32
gl.GetIntegerv(gl.MAX_TEXTURE_SIZE, &temp_max_tex_size)
gl.GetIntegerv(gl.MAX_ARRAY_TEXTURE_LAYERS, &temp_max_tex_array_size)
max_tex_size := cast(u32)math.pow(cast(f32)temp_max_tex_size, 2)
max_tex_array_size := cast(u32)temp_max_tex_array_size
if weights_len * morph_attribute_count > cast(int)max_tex_array_size {
log.warn("Morph targets exceed the maximum texture size limit")
}
width := cast(i32)math.ceil(math.sqrt(cast(f32)len(vertices)))
gl.GenTextures(1, &morph_texture)
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D_ARRAY, morph_texture)
gl.TexImage3D(
gl.TEXTURE_2D_ARRAY,
0,
gl.RGB32F,
width,
width,
cast(i32)weights_len,
0,
gl.RGB,
gl.FLOAT,
raw_data(morph_targets),
)
gl.TexParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
// Morph weights buffer texture's backing buffer
gl.GenBuffers(1, &morph_weights_buf)
gl.BindBuffer(gl.ARRAY_BUFFER, morph_weights_buf)
gl.BufferData(gl.ARRAY_BUFFER, size_of(f32) * weights_len, nil, gl.DYNAMIC_DRAW)
// Morph weights buffer texture
gl.GenTextures(1, &morph_weights_texture)
gl.ActiveTexture(gl.TEXTURE1)
gl.BindTexture(gl.TEXTURE_BUFFER, morph_weights_texture)
gl.TexBuffer(gl.TEXTURE_BUFFER, gl.R32F, morph_weights_buf)
gl.TexParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
usage = gl.DYNAMIC_DRAW
}
if joints_len != 0 {
gl.GenBuffers(1, &joint_matrices_buf)
gl.BindBuffer(gl.ARRAY_BUFFER, joint_matrices_buf)
gl.BufferData(gl.ARRAY_BUFFER, size_of(m.mat4) * joints_len, nil, gl.DYNAMIC_DRAW)
gl.GenTextures(1, &joint_matrices_texture)
gl.ActiveTexture(gl.TEXTURE2)
gl.BindTexture(gl.TEXTURE_BUFFER, joint_matrices_texture)
gl.TexBuffer(gl.TEXTURE_BUFFER, gl.RGBA32F, joint_matrices_buf)
gl.TexParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
}
gl.GenVertexArrays(1, &vao)
gl.GenBuffers(1, &vbo)
gl.GenBuffers(1, &ebo)
gl.BindVertexArray(vao)
gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
gl.BufferData(gl.ARRAY_BUFFER, size_of(Vertex) * len(vertices), raw_data(vertices), usage)
// positions
gl.EnableVertexAttribArray(0)
gl.VertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, size_of(Vertex), 0)
// normals
gl.EnableVertexAttribArray(1)
gl.VertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, size_of(Vertex), 3 * size_of(f32))
// texcoords
gl.EnableVertexAttribArray(2)
gl.VertexAttribPointer(2, 2, gl.FLOAT, gl.FALSE, size_of(Vertex), 6 * size_of(f32))
// tangents
gl.EnableVertexAttribArray(3)
gl.VertexAttribPointer(3, 4, gl.FLOAT, gl.FALSE, size_of(Vertex), 8 * size_of(f32))
// joints
gl.EnableVertexAttribArray(4)
gl.VertexAttribIPointer(4, 4, gl.UNSIGNED_INT, size_of(Vertex), 12 * size_of(f32))
// weights
gl.EnableVertexAttribArray(5)
gl.VertexAttribPointer(5, 4, gl.FLOAT, gl.FALSE, size_of(Vertex), 16 * size_of(u32))
// color
gl.EnableVertexAttribArray(6)
gl.VertexAttribPointer(6, 4, gl.FLOAT, gl.FALSE, size_of(Vertex), 20 * size_of(u32))
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, ebo)
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, size_of(u32) * len(indices), raw_data(indices), gl.STATIC_DRAW)
return(
Mesh {
primitive,
vertices,
indices,
parent,
material_id,
make([]f32, weights_len, allocator^),
morph_texture,
morph_normals_offset,
morph_tangents_offset,
joint_matrices_buf,
joint_matrices_texture,
vao,
vbo,
ebo,
morph_weights_buf,
morph_weights_texture,
obb,
} \
)
}
@(private = "file")
process_material :: proc(
material: ^cgltf.material,
model_path: string,
textures_map: ^map[cstring]TextureId,
allocator: ^mem.Allocator,
) -> Material {
textures := make([dynamic]Texture, 0, 8, allocator^)
name := strings.clone_from_cstring(material.name, allocator^)
if material.name == nil {
delete(name)
name = fmt.aprintf("Material", allocator = allocator^)
}
diffuse := m.vec4(material.pbr_metallic_roughness.base_color_factor)
specular := m.vec3(material.specular.specular_color_factor)
emissive := m.vec3(material.emissive_factor)
shininess := material.specular.specular_factor != 0 ? material.specular.specular_factor : 32.0
metallic := material.pbr_metallic_roughness.metallic_factor
roughness := material.pbr_metallic_roughness.roughness_factor
if material.has_emissive_strength {
emissive *= material.emissive_strength.emissive_strength
}
// TODO: use ior if available to calculate the specular, otherwise default F0 to 0.04
// TODO: specularGlossiness