-
Notifications
You must be signed in to change notification settings - Fork 136
/
cgltf.h
7159 lines (6171 loc) · 197 KB
/
cgltf.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
/**
* cgltf - a single-file glTF 2.0 parser written in C99.
*
* Version: 1.14
*
* Website: https://github.com/jkuhlmann/cgltf
*
* Distributed under the MIT License, see notice at the end of this file.
*
* Building:
* Include this file where you need the struct and function
* declarations. Have exactly one source file where you define
* `CGLTF_IMPLEMENTATION` before including this file to get the
* function definitions.
*
* Reference:
* `cgltf_result cgltf_parse(const cgltf_options*, const void*,
* cgltf_size, cgltf_data**)` parses both glTF and GLB data. If
* this function returns `cgltf_result_success`, you have to call
* `cgltf_free()` on the created `cgltf_data*` variable.
* Note that contents of external files for buffers and images are not
* automatically loaded. You'll need to read these files yourself using
* URIs in the `cgltf_data` structure.
*
* `cgltf_options` is the struct passed to `cgltf_parse()` to control
* parts of the parsing process. You can use it to force the file type
* and provide memory allocation as well as file operation callbacks.
* Should be zero-initialized to trigger default behavior.
*
* `cgltf_data` is the struct allocated and filled by `cgltf_parse()`.
* It generally mirrors the glTF format as described by the spec (see
* https://github.com/KhronosGroup/glTF/tree/master/specification/2.0).
*
* `void cgltf_free(cgltf_data*)` frees the allocated `cgltf_data`
* variable.
*
* `cgltf_result cgltf_load_buffers(const cgltf_options*, cgltf_data*,
* const char* gltf_path)` can be optionally called to open and read buffer
* files using the `FILE*` APIs. The `gltf_path` argument is the path to
* the original glTF file, which allows the parser to resolve the path to
* buffer files.
*
* `cgltf_result cgltf_load_buffer_base64(const cgltf_options* options,
* cgltf_size size, const char* base64, void** out_data)` decodes
* base64-encoded data content. Used internally by `cgltf_load_buffers()`.
* This is useful when decoding data URIs in images.
*
* `cgltf_result cgltf_parse_file(const cgltf_options* options, const
* char* path, cgltf_data** out_data)` can be used to open the given
* file using `FILE*` APIs and parse the data using `cgltf_parse()`.
*
* `cgltf_result cgltf_validate(cgltf_data*)` can be used to do additional
* checks to make sure the parsed glTF data is valid.
*
* `cgltf_node_transform_local` converts the translation / rotation / scale properties of a node
* into a mat4.
*
* `cgltf_node_transform_world` calls `cgltf_node_transform_local` on every ancestor in order
* to compute the root-to-node transformation.
*
* `cgltf_accessor_unpack_floats` reads in the data from an accessor, applies sparse data (if any),
* and converts them to floating point. Assumes that `cgltf_load_buffers` has already been called.
* By passing null for the output pointer, users can find out how many floats are required in the
* output buffer.
*
* `cgltf_accessor_unpack_indices` reads in the index data from an accessor. Assumes that
* `cgltf_load_buffers` has already been called. By passing null for the output pointer, users can
* find out how many indices are required in the output buffer. Returns 0 if the accessor is
* sparse or if the output component size is less than the accessor's component size.
*
* `cgltf_num_components` is a tiny utility that tells you the dimensionality of
* a certain accessor type. This can be used before `cgltf_accessor_unpack_floats` to help allocate
* the necessary amount of memory. `cgltf_component_size` and `cgltf_calc_size` exist for
* similar purposes.
*
* `cgltf_accessor_read_float` reads a certain element from a non-sparse accessor and converts it to
* floating point, assuming that `cgltf_load_buffers` has already been called. The passed-in element
* size is the number of floats in the output buffer, which should be in the range [1, 16]. Returns
* false if the passed-in element_size is too small, or if the accessor is sparse.
*
* `cgltf_accessor_read_uint` is similar to its floating-point counterpart, but limited to reading
* vector types and does not support matrix types. The passed-in element size is the number of uints
* in the output buffer, which should be in the range [1, 4]. Returns false if the passed-in
* element_size is too small, or if the accessor is sparse.
*
* `cgltf_accessor_read_index` is similar to its floating-point counterpart, but it returns size_t
* and only works with single-component data types.
*
* `cgltf_copy_extras_json` allows users to retrieve the "extras" data that can be attached to many
* glTF objects (which can be arbitrary JSON data). This is a legacy function, consider using
* cgltf_extras::data directly instead. You can parse this data using your own JSON parser
* or, if you've included the cgltf implementation using the integrated JSMN JSON parser.
*/
#ifndef CGLTF_H_INCLUDED__
#define CGLTF_H_INCLUDED__
#include <stddef.h>
#include <stdint.h> /* For uint8_t, uint32_t */
#ifdef __cplusplus
extern "C" {
#endif
typedef size_t cgltf_size;
typedef long long int cgltf_ssize;
typedef float cgltf_float;
typedef int cgltf_int;
typedef unsigned int cgltf_uint;
typedef int cgltf_bool;
typedef enum cgltf_file_type
{
cgltf_file_type_invalid,
cgltf_file_type_gltf,
cgltf_file_type_glb,
cgltf_file_type_max_enum
} cgltf_file_type;
typedef enum cgltf_result
{
cgltf_result_success,
cgltf_result_data_too_short,
cgltf_result_unknown_format,
cgltf_result_invalid_json,
cgltf_result_invalid_gltf,
cgltf_result_invalid_options,
cgltf_result_file_not_found,
cgltf_result_io_error,
cgltf_result_out_of_memory,
cgltf_result_legacy_gltf,
cgltf_result_max_enum
} cgltf_result;
typedef struct cgltf_memory_options
{
void* (*alloc_func)(void* user, cgltf_size size);
void (*free_func) (void* user, void* ptr);
void* user_data;
} cgltf_memory_options;
typedef struct cgltf_file_options
{
cgltf_result(*read)(const struct cgltf_memory_options* memory_options, const struct cgltf_file_options* file_options, const char* path, cgltf_size* size, void** data);
void (*release)(const struct cgltf_memory_options* memory_options, const struct cgltf_file_options* file_options, void* data);
void* user_data;
} cgltf_file_options;
typedef struct cgltf_options
{
cgltf_file_type type; /* invalid == auto detect */
cgltf_size json_token_count; /* 0 == auto */
cgltf_memory_options memory;
cgltf_file_options file;
} cgltf_options;
typedef enum cgltf_buffer_view_type
{
cgltf_buffer_view_type_invalid,
cgltf_buffer_view_type_indices,
cgltf_buffer_view_type_vertices,
cgltf_buffer_view_type_max_enum
} cgltf_buffer_view_type;
typedef enum cgltf_attribute_type
{
cgltf_attribute_type_invalid,
cgltf_attribute_type_position,
cgltf_attribute_type_normal,
cgltf_attribute_type_tangent,
cgltf_attribute_type_texcoord,
cgltf_attribute_type_color,
cgltf_attribute_type_joints,
cgltf_attribute_type_weights,
cgltf_attribute_type_custom,
cgltf_attribute_type_max_enum
} cgltf_attribute_type;
typedef enum cgltf_component_type
{
cgltf_component_type_invalid,
cgltf_component_type_r_8, /* BYTE */
cgltf_component_type_r_8u, /* UNSIGNED_BYTE */
cgltf_component_type_r_16, /* SHORT */
cgltf_component_type_r_16u, /* UNSIGNED_SHORT */
cgltf_component_type_r_32u, /* UNSIGNED_INT */
cgltf_component_type_r_32f, /* FLOAT */
cgltf_component_type_max_enum
} cgltf_component_type;
typedef enum cgltf_type
{
cgltf_type_invalid,
cgltf_type_scalar,
cgltf_type_vec2,
cgltf_type_vec3,
cgltf_type_vec4,
cgltf_type_mat2,
cgltf_type_mat3,
cgltf_type_mat4,
cgltf_type_max_enum
} cgltf_type;
typedef enum cgltf_primitive_type
{
cgltf_primitive_type_invalid,
cgltf_primitive_type_points,
cgltf_primitive_type_lines,
cgltf_primitive_type_line_loop,
cgltf_primitive_type_line_strip,
cgltf_primitive_type_triangles,
cgltf_primitive_type_triangle_strip,
cgltf_primitive_type_triangle_fan,
cgltf_primitive_type_max_enum
} cgltf_primitive_type;
typedef enum cgltf_alpha_mode
{
cgltf_alpha_mode_opaque,
cgltf_alpha_mode_mask,
cgltf_alpha_mode_blend,
cgltf_alpha_mode_max_enum
} cgltf_alpha_mode;
typedef enum cgltf_animation_path_type {
cgltf_animation_path_type_invalid,
cgltf_animation_path_type_translation,
cgltf_animation_path_type_rotation,
cgltf_animation_path_type_scale,
cgltf_animation_path_type_weights,
cgltf_animation_path_type_max_enum
} cgltf_animation_path_type;
typedef enum cgltf_interpolation_type {
cgltf_interpolation_type_linear,
cgltf_interpolation_type_step,
cgltf_interpolation_type_cubic_spline,
cgltf_interpolation_type_max_enum
} cgltf_interpolation_type;
typedef enum cgltf_camera_type {
cgltf_camera_type_invalid,
cgltf_camera_type_perspective,
cgltf_camera_type_orthographic,
cgltf_camera_type_max_enum
} cgltf_camera_type;
typedef enum cgltf_light_type {
cgltf_light_type_invalid,
cgltf_light_type_directional,
cgltf_light_type_point,
cgltf_light_type_spot,
cgltf_light_type_max_enum
} cgltf_light_type;
typedef enum cgltf_data_free_method {
cgltf_data_free_method_none,
cgltf_data_free_method_file_release,
cgltf_data_free_method_memory_free,
cgltf_data_free_method_max_enum
} cgltf_data_free_method;
typedef struct cgltf_extras {
cgltf_size start_offset; /* this field is deprecated and will be removed in the future; use data instead */
cgltf_size end_offset; /* this field is deprecated and will be removed in the future; use data instead */
char* data;
} cgltf_extras;
typedef struct cgltf_extension {
char* name;
char* data;
} cgltf_extension;
typedef struct cgltf_buffer
{
char* name;
cgltf_size size;
char* uri;
void* data; /* loaded by cgltf_load_buffers */
cgltf_data_free_method data_free_method;
cgltf_extras extras;
cgltf_size extensions_count;
cgltf_extension* extensions;
} cgltf_buffer;
typedef enum cgltf_meshopt_compression_mode {
cgltf_meshopt_compression_mode_invalid,
cgltf_meshopt_compression_mode_attributes,
cgltf_meshopt_compression_mode_triangles,
cgltf_meshopt_compression_mode_indices,
cgltf_meshopt_compression_mode_max_enum
} cgltf_meshopt_compression_mode;
typedef enum cgltf_meshopt_compression_filter {
cgltf_meshopt_compression_filter_none,
cgltf_meshopt_compression_filter_octahedral,
cgltf_meshopt_compression_filter_quaternion,
cgltf_meshopt_compression_filter_exponential,
cgltf_meshopt_compression_filter_max_enum
} cgltf_meshopt_compression_filter;
typedef struct cgltf_meshopt_compression
{
cgltf_buffer* buffer;
cgltf_size offset;
cgltf_size size;
cgltf_size stride;
cgltf_size count;
cgltf_meshopt_compression_mode mode;
cgltf_meshopt_compression_filter filter;
} cgltf_meshopt_compression;
typedef struct cgltf_buffer_view
{
char *name;
cgltf_buffer* buffer;
cgltf_size offset;
cgltf_size size;
cgltf_size stride; /* 0 == automatically determined by accessor */
cgltf_buffer_view_type type;
void* data; /* overrides buffer->data if present, filled by extensions */
cgltf_bool has_meshopt_compression;
cgltf_meshopt_compression meshopt_compression;
cgltf_extras extras;
cgltf_size extensions_count;
cgltf_extension* extensions;
} cgltf_buffer_view;
typedef struct cgltf_accessor_sparse
{
cgltf_size count;
cgltf_buffer_view* indices_buffer_view;
cgltf_size indices_byte_offset;
cgltf_component_type indices_component_type;
cgltf_buffer_view* values_buffer_view;
cgltf_size values_byte_offset;
} cgltf_accessor_sparse;
typedef struct cgltf_accessor
{
char* name;
cgltf_component_type component_type;
cgltf_bool normalized;
cgltf_type type;
cgltf_size offset;
cgltf_size count;
cgltf_size stride;
cgltf_buffer_view* buffer_view;
cgltf_bool has_min;
cgltf_float min[16];
cgltf_bool has_max;
cgltf_float max[16];
cgltf_bool is_sparse;
cgltf_accessor_sparse sparse;
cgltf_extras extras;
cgltf_size extensions_count;
cgltf_extension* extensions;
} cgltf_accessor;
typedef struct cgltf_attribute
{
char* name;
cgltf_attribute_type type;
cgltf_int index;
cgltf_accessor* data;
} cgltf_attribute;
typedef struct cgltf_image
{
char* name;
char* uri;
cgltf_buffer_view* buffer_view;
char* mime_type;
cgltf_extras extras;
cgltf_size extensions_count;
cgltf_extension* extensions;
} cgltf_image;
typedef struct cgltf_sampler
{
char* name;
cgltf_int mag_filter;
cgltf_int min_filter;
cgltf_int wrap_s;
cgltf_int wrap_t;
cgltf_extras extras;
cgltf_size extensions_count;
cgltf_extension* extensions;
} cgltf_sampler;
typedef struct cgltf_texture
{
char* name;
cgltf_image* image;
cgltf_sampler* sampler;
cgltf_bool has_basisu;
cgltf_image* basisu_image;
cgltf_bool has_webp;
cgltf_image* webp_image;
cgltf_extras extras;
cgltf_size extensions_count;
cgltf_extension* extensions;
} cgltf_texture;
typedef struct cgltf_texture_transform
{
cgltf_float offset[2];
cgltf_float rotation;
cgltf_float scale[2];
cgltf_bool has_texcoord;
cgltf_int texcoord;
} cgltf_texture_transform;
typedef struct cgltf_texture_view
{
cgltf_texture* texture;
cgltf_int texcoord;
cgltf_float scale; /* equivalent to strength for occlusion_texture */
cgltf_bool has_transform;
cgltf_texture_transform transform;
} cgltf_texture_view;
typedef struct cgltf_pbr_metallic_roughness
{
cgltf_texture_view base_color_texture;
cgltf_texture_view metallic_roughness_texture;
cgltf_float base_color_factor[4];
cgltf_float metallic_factor;
cgltf_float roughness_factor;
} cgltf_pbr_metallic_roughness;
typedef struct cgltf_pbr_specular_glossiness
{
cgltf_texture_view diffuse_texture;
cgltf_texture_view specular_glossiness_texture;
cgltf_float diffuse_factor[4];
cgltf_float specular_factor[3];
cgltf_float glossiness_factor;
} cgltf_pbr_specular_glossiness;
typedef struct cgltf_clearcoat
{
cgltf_texture_view clearcoat_texture;
cgltf_texture_view clearcoat_roughness_texture;
cgltf_texture_view clearcoat_normal_texture;
cgltf_float clearcoat_factor;
cgltf_float clearcoat_roughness_factor;
} cgltf_clearcoat;
typedef struct cgltf_transmission
{
cgltf_texture_view transmission_texture;
cgltf_float transmission_factor;
} cgltf_transmission;
typedef struct cgltf_ior
{
cgltf_float ior;
} cgltf_ior;
typedef struct cgltf_specular
{
cgltf_texture_view specular_texture;
cgltf_texture_view specular_color_texture;
cgltf_float specular_color_factor[3];
cgltf_float specular_factor;
} cgltf_specular;
typedef struct cgltf_volume
{
cgltf_texture_view thickness_texture;
cgltf_float thickness_factor;
cgltf_float attenuation_color[3];
cgltf_float attenuation_distance;
} cgltf_volume;
typedef struct cgltf_sheen
{
cgltf_texture_view sheen_color_texture;
cgltf_float sheen_color_factor[3];
cgltf_texture_view sheen_roughness_texture;
cgltf_float sheen_roughness_factor;
} cgltf_sheen;
typedef struct cgltf_emissive_strength
{
cgltf_float emissive_strength;
} cgltf_emissive_strength;
typedef struct cgltf_iridescence
{
cgltf_float iridescence_factor;
cgltf_texture_view iridescence_texture;
cgltf_float iridescence_ior;
cgltf_float iridescence_thickness_min;
cgltf_float iridescence_thickness_max;
cgltf_texture_view iridescence_thickness_texture;
} cgltf_iridescence;
typedef struct cgltf_diffuse_transmission
{
cgltf_texture_view diffuse_transmission_texture;
cgltf_float diffuse_transmission_factor;
cgltf_float diffuse_transmission_color_factor[3];
cgltf_texture_view diffuse_transmission_color_texture;
} cgltf_diffuse_transmission;
typedef struct cgltf_anisotropy
{
cgltf_float anisotropy_strength;
cgltf_float anisotropy_rotation;
cgltf_texture_view anisotropy_texture;
} cgltf_anisotropy;
typedef struct cgltf_dispersion
{
cgltf_float dispersion;
} cgltf_dispersion;
typedef struct cgltf_material
{
char* name;
cgltf_bool has_pbr_metallic_roughness;
cgltf_bool has_pbr_specular_glossiness;
cgltf_bool has_clearcoat;
cgltf_bool has_transmission;
cgltf_bool has_volume;
cgltf_bool has_ior;
cgltf_bool has_specular;
cgltf_bool has_sheen;
cgltf_bool has_emissive_strength;
cgltf_bool has_iridescence;
cgltf_bool has_diffuse_transmission;
cgltf_bool has_anisotropy;
cgltf_bool has_dispersion;
cgltf_pbr_metallic_roughness pbr_metallic_roughness;
cgltf_pbr_specular_glossiness pbr_specular_glossiness;
cgltf_clearcoat clearcoat;
cgltf_ior ior;
cgltf_specular specular;
cgltf_sheen sheen;
cgltf_transmission transmission;
cgltf_volume volume;
cgltf_emissive_strength emissive_strength;
cgltf_iridescence iridescence;
cgltf_diffuse_transmission diffuse_transmission;
cgltf_anisotropy anisotropy;
cgltf_dispersion dispersion;
cgltf_texture_view normal_texture;
cgltf_texture_view occlusion_texture;
cgltf_texture_view emissive_texture;
cgltf_float emissive_factor[3];
cgltf_alpha_mode alpha_mode;
cgltf_float alpha_cutoff;
cgltf_bool double_sided;
cgltf_bool unlit;
cgltf_extras extras;
cgltf_size extensions_count;
cgltf_extension* extensions;
} cgltf_material;
typedef struct cgltf_material_mapping
{
cgltf_size variant;
cgltf_material* material;
cgltf_extras extras;
} cgltf_material_mapping;
typedef struct cgltf_morph_target {
cgltf_attribute* attributes;
cgltf_size attributes_count;
} cgltf_morph_target;
typedef struct cgltf_draco_mesh_compression {
cgltf_buffer_view* buffer_view;
cgltf_attribute* attributes;
cgltf_size attributes_count;
} cgltf_draco_mesh_compression;
typedef struct cgltf_mesh_gpu_instancing {
cgltf_attribute* attributes;
cgltf_size attributes_count;
} cgltf_mesh_gpu_instancing;
typedef struct cgltf_primitive {
cgltf_primitive_type type;
cgltf_accessor* indices;
cgltf_material* material;
cgltf_attribute* attributes;
cgltf_size attributes_count;
cgltf_morph_target* targets;
cgltf_size targets_count;
cgltf_extras extras;
cgltf_bool has_draco_mesh_compression;
cgltf_draco_mesh_compression draco_mesh_compression;
cgltf_material_mapping* mappings;
cgltf_size mappings_count;
cgltf_size extensions_count;
cgltf_extension* extensions;
} cgltf_primitive;
typedef struct cgltf_mesh {
char* name;
cgltf_primitive* primitives;
cgltf_size primitives_count;
cgltf_float* weights;
cgltf_size weights_count;
char** target_names;
cgltf_size target_names_count;
cgltf_extras extras;
cgltf_size extensions_count;
cgltf_extension* extensions;
} cgltf_mesh;
typedef struct cgltf_node cgltf_node;
typedef struct cgltf_skin {
char* name;
cgltf_node** joints;
cgltf_size joints_count;
cgltf_node* skeleton;
cgltf_accessor* inverse_bind_matrices;
cgltf_extras extras;
cgltf_size extensions_count;
cgltf_extension* extensions;
} cgltf_skin;
typedef struct cgltf_camera_perspective {
cgltf_bool has_aspect_ratio;
cgltf_float aspect_ratio;
cgltf_float yfov;
cgltf_bool has_zfar;
cgltf_float zfar;
cgltf_float znear;
cgltf_extras extras;
} cgltf_camera_perspective;
typedef struct cgltf_camera_orthographic {
cgltf_float xmag;
cgltf_float ymag;
cgltf_float zfar;
cgltf_float znear;
cgltf_extras extras;
} cgltf_camera_orthographic;
typedef struct cgltf_camera {
char* name;
cgltf_camera_type type;
union {
cgltf_camera_perspective perspective;
cgltf_camera_orthographic orthographic;
} data;
cgltf_extras extras;
cgltf_size extensions_count;
cgltf_extension* extensions;
} cgltf_camera;
typedef struct cgltf_light {
char* name;
cgltf_float color[3];
cgltf_float intensity;
cgltf_light_type type;
cgltf_float range;
cgltf_float spot_inner_cone_angle;
cgltf_float spot_outer_cone_angle;
cgltf_extras extras;
} cgltf_light;
struct cgltf_node {
char* name;
cgltf_node* parent;
cgltf_node** children;
cgltf_size children_count;
cgltf_skin* skin;
cgltf_mesh* mesh;
cgltf_camera* camera;
cgltf_light* light;
cgltf_float* weights;
cgltf_size weights_count;
cgltf_bool has_translation;
cgltf_bool has_rotation;
cgltf_bool has_scale;
cgltf_bool has_matrix;
cgltf_float translation[3];
cgltf_float rotation[4];
cgltf_float scale[3];
cgltf_float matrix[16];
cgltf_extras extras;
cgltf_bool has_mesh_gpu_instancing;
cgltf_mesh_gpu_instancing mesh_gpu_instancing;
cgltf_size extensions_count;
cgltf_extension* extensions;
};
typedef struct cgltf_scene {
char* name;
cgltf_node** nodes;
cgltf_size nodes_count;
cgltf_extras extras;
cgltf_size extensions_count;
cgltf_extension* extensions;
} cgltf_scene;
typedef struct cgltf_animation_sampler {
cgltf_accessor* input;
cgltf_accessor* output;
cgltf_interpolation_type interpolation;
cgltf_extras extras;
cgltf_size extensions_count;
cgltf_extension* extensions;
} cgltf_animation_sampler;
typedef struct cgltf_animation_channel {
cgltf_animation_sampler* sampler;
cgltf_node* target_node;
cgltf_animation_path_type target_path;
cgltf_extras extras;
cgltf_size extensions_count;
cgltf_extension* extensions;
} cgltf_animation_channel;
typedef struct cgltf_animation {
char* name;
cgltf_animation_sampler* samplers;
cgltf_size samplers_count;
cgltf_animation_channel* channels;
cgltf_size channels_count;
cgltf_extras extras;
cgltf_size extensions_count;
cgltf_extension* extensions;
} cgltf_animation;
typedef struct cgltf_material_variant
{
char* name;
cgltf_extras extras;
} cgltf_material_variant;
typedef struct cgltf_asset {
char* copyright;
char* generator;
char* version;
char* min_version;
cgltf_extras extras;
cgltf_size extensions_count;
cgltf_extension* extensions;
} cgltf_asset;
typedef struct cgltf_data
{
cgltf_file_type file_type;
void* file_data;
cgltf_asset asset;
cgltf_mesh* meshes;
cgltf_size meshes_count;
cgltf_material* materials;
cgltf_size materials_count;
cgltf_accessor* accessors;
cgltf_size accessors_count;
cgltf_buffer_view* buffer_views;
cgltf_size buffer_views_count;
cgltf_buffer* buffers;
cgltf_size buffers_count;
cgltf_image* images;
cgltf_size images_count;
cgltf_texture* textures;
cgltf_size textures_count;
cgltf_sampler* samplers;
cgltf_size samplers_count;
cgltf_skin* skins;
cgltf_size skins_count;
cgltf_camera* cameras;
cgltf_size cameras_count;
cgltf_light* lights;
cgltf_size lights_count;
cgltf_node* nodes;
cgltf_size nodes_count;
cgltf_scene* scenes;
cgltf_size scenes_count;
cgltf_scene* scene;
cgltf_animation* animations;
cgltf_size animations_count;
cgltf_material_variant* variants;
cgltf_size variants_count;
cgltf_extras extras;
cgltf_size data_extensions_count;
cgltf_extension* data_extensions;
char** extensions_used;
cgltf_size extensions_used_count;
char** extensions_required;
cgltf_size extensions_required_count;
const char* json;
cgltf_size json_size;
const void* bin;
cgltf_size bin_size;
cgltf_memory_options memory;
cgltf_file_options file;
} cgltf_data;
cgltf_result cgltf_parse(
const cgltf_options* options,
const void* data,
cgltf_size size,
cgltf_data** out_data);
cgltf_result cgltf_parse_file(
const cgltf_options* options,
const char* path,
cgltf_data** out_data);
cgltf_result cgltf_load_buffers(
const cgltf_options* options,
cgltf_data* data,
const char* gltf_path);
cgltf_result cgltf_load_buffer_base64(const cgltf_options* options, cgltf_size size, const char* base64, void** out_data);
cgltf_size cgltf_decode_string(char* string);
cgltf_size cgltf_decode_uri(char* uri);
cgltf_result cgltf_validate(cgltf_data* data);
void cgltf_free(cgltf_data* data);
void cgltf_node_transform_local(const cgltf_node* node, cgltf_float* out_matrix);
void cgltf_node_transform_world(const cgltf_node* node, cgltf_float* out_matrix);
const uint8_t* cgltf_buffer_view_data(const cgltf_buffer_view* view);
const cgltf_accessor* cgltf_find_accessor(const cgltf_primitive* prim, cgltf_attribute_type type, cgltf_int index);
cgltf_bool cgltf_accessor_read_float(const cgltf_accessor* accessor, cgltf_size index, cgltf_float* out, cgltf_size element_size);
cgltf_bool cgltf_accessor_read_uint(const cgltf_accessor* accessor, cgltf_size index, cgltf_uint* out, cgltf_size element_size);
cgltf_size cgltf_accessor_read_index(const cgltf_accessor* accessor, cgltf_size index);
cgltf_size cgltf_num_components(cgltf_type type);
cgltf_size cgltf_component_size(cgltf_component_type component_type);
cgltf_size cgltf_calc_size(cgltf_type type, cgltf_component_type component_type);
cgltf_size cgltf_accessor_unpack_floats(const cgltf_accessor* accessor, cgltf_float* out, cgltf_size float_count);
cgltf_size cgltf_accessor_unpack_indices(const cgltf_accessor* accessor, void* out, cgltf_size out_component_size, cgltf_size index_count);
/* this function is deprecated and will be removed in the future; use cgltf_extras::data instead */
cgltf_result cgltf_copy_extras_json(const cgltf_data* data, const cgltf_extras* extras, char* dest, cgltf_size* dest_size);
cgltf_size cgltf_mesh_index(const cgltf_data* data, const cgltf_mesh* object);
cgltf_size cgltf_material_index(const cgltf_data* data, const cgltf_material* object);
cgltf_size cgltf_accessor_index(const cgltf_data* data, const cgltf_accessor* object);
cgltf_size cgltf_buffer_view_index(const cgltf_data* data, const cgltf_buffer_view* object);
cgltf_size cgltf_buffer_index(const cgltf_data* data, const cgltf_buffer* object);
cgltf_size cgltf_image_index(const cgltf_data* data, const cgltf_image* object);
cgltf_size cgltf_texture_index(const cgltf_data* data, const cgltf_texture* object);
cgltf_size cgltf_sampler_index(const cgltf_data* data, const cgltf_sampler* object);
cgltf_size cgltf_skin_index(const cgltf_data* data, const cgltf_skin* object);
cgltf_size cgltf_camera_index(const cgltf_data* data, const cgltf_camera* object);
cgltf_size cgltf_light_index(const cgltf_data* data, const cgltf_light* object);
cgltf_size cgltf_node_index(const cgltf_data* data, const cgltf_node* object);
cgltf_size cgltf_scene_index(const cgltf_data* data, const cgltf_scene* object);
cgltf_size cgltf_animation_index(const cgltf_data* data, const cgltf_animation* object);
cgltf_size cgltf_animation_sampler_index(const cgltf_animation* animation, const cgltf_animation_sampler* object);
cgltf_size cgltf_animation_channel_index(const cgltf_animation* animation, const cgltf_animation_channel* object);
#ifdef __cplusplus
}
#endif
#endif /* #ifndef CGLTF_H_INCLUDED__ */
/*
*
* Stop now, if you are only interested in the API.
* Below, you find the implementation.
*
*/
#if defined(__INTELLISENSE__) || defined(__JETBRAINS_IDE__)
/* This makes MSVC/CLion intellisense work. */
#define CGLTF_IMPLEMENTATION
#endif
#ifdef CGLTF_IMPLEMENTATION
#include <assert.h> /* For assert */
#include <string.h> /* For strncpy */
#include <stdio.h> /* For fopen */
#include <limits.h> /* For UINT_MAX etc */
#include <float.h> /* For FLT_MAX */
#if !defined(CGLTF_MALLOC) || !defined(CGLTF_FREE) || !defined(CGLTF_ATOI) || !defined(CGLTF_ATOF) || !defined(CGLTF_ATOLL)
#include <stdlib.h> /* For malloc, free, atoi, atof */
#endif
/* JSMN_PARENT_LINKS is necessary to make parsing large structures linear in input size */
#define JSMN_PARENT_LINKS
/* JSMN_STRICT is necessary to reject invalid JSON documents */
#define JSMN_STRICT
/*
* -- jsmn.h start --
* Source: https://github.com/zserge/jsmn
* License: MIT
*/
typedef enum {
JSMN_UNDEFINED = 0,
JSMN_OBJECT = 1,
JSMN_ARRAY = 2,
JSMN_STRING = 3,
JSMN_PRIMITIVE = 4
} jsmntype_t;
enum jsmnerr {
/* Not enough tokens were provided */
JSMN_ERROR_NOMEM = -1,
/* Invalid character inside JSON string */
JSMN_ERROR_INVAL = -2,
/* The string is not a full JSON packet, more bytes expected */
JSMN_ERROR_PART = -3
};
typedef struct {
jsmntype_t type;
ptrdiff_t start;
ptrdiff_t end;
int size;
#ifdef JSMN_PARENT_LINKS
int parent;
#endif
} jsmntok_t;
typedef struct {
size_t pos; /* offset in the JSON string */
unsigned int toknext; /* next token to allocate */
int toksuper; /* superior token node, e.g parent object or array */
} jsmn_parser;
static void jsmn_init(jsmn_parser *parser);
static int jsmn_parse(jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, size_t num_tokens);
/*
* -- jsmn.h end --
*/
#ifndef CGLTF_CONSTS
#define GlbHeaderSize 12
#define GlbChunkHeaderSize 8
static const uint32_t GlbVersion = 2;
static const uint32_t GlbMagic = 0x46546C67;
static const uint32_t GlbMagicJsonChunk = 0x4E4F534A;
static const uint32_t GlbMagicBinChunk = 0x004E4942;
#define CGLTF_CONSTS
#endif
#ifndef CGLTF_MALLOC
#define CGLTF_MALLOC(size) malloc(size)
#endif
#ifndef CGLTF_FREE
#define CGLTF_FREE(ptr) free(ptr)
#endif
#ifndef CGLTF_ATOI
#define CGLTF_ATOI(str) atoi(str)
#endif
#ifndef CGLTF_ATOF
#define CGLTF_ATOF(str) atof(str)
#endif
#ifndef CGLTF_ATOLL
#define CGLTF_ATOLL(str) atoll(str)
#endif
#ifndef CGLTF_VALIDATE_ENABLE_ASSERTS
#define CGLTF_VALIDATE_ENABLE_ASSERTS 0
#endif
static void* cgltf_default_alloc(void* user, cgltf_size size)
{
(void)user;
return CGLTF_MALLOC(size);
}