2 * cgltf - a single-file glTF 2.0 parser written in C99.
6 * Website: https://github.com/jkuhlmann/cgltf
8 * Distributed under the MIT License, see notice at the end of this file.
11 * Include this file where you need the struct and function
12 * declarations. Have exactly one source file where you define
13 * `CGLTF_IMPLEMENTATION` before including this file to get the
14 * function definitions.
17 * `cgltf_result cgltf_parse(const cgltf_options*, const void*,
18 * cgltf_size, cgltf_data**)` parses both glTF and GLB data. If
19 * this function returns `cgltf_result_success`, you have to call
20 * `cgltf_free()` on the created `cgltf_data*` variable.
21 * Note that contents of external files for buffers and images are not
22 * automatically loaded. You'll need to read these files yourself using
23 * URIs in the `cgltf_data` structure.
25 * `cgltf_options` is the struct passed to `cgltf_parse()` to control
26 * parts of the parsing process. You can use it to force the file type
27 * and provide memory allocation as well as file operation callbacks.
28 * Should be zero-initialized to trigger default behavior.
30 * `cgltf_data` is the struct allocated and filled by `cgltf_parse()`.
31 * It generally mirrors the glTF format as described by the spec (see
32 * https://github.com/KhronosGroup/glTF/tree/master/specification/2.0).
34 * `void cgltf_free(cgltf_data*)` frees the allocated `cgltf_data`
37 * `cgltf_result cgltf_load_buffers(const cgltf_options*, cgltf_data*,
38 * const char* gltf_path)` can be optionally called to open and read buffer
39 * files using the `FILE*` APIs. The `gltf_path` argument is the path to
40 * the original glTF file, which allows the parser to resolve the path to
43 * `cgltf_result cgltf_load_buffer_base64(const cgltf_options* options,
44 * cgltf_size size, const char* base64, void** out_data)` decodes
45 * base64-encoded data content. Used internally by `cgltf_load_buffers()`.
46 * This is useful when decoding data URIs in images.
48 * `cgltf_result cgltf_parse_file(const cgltf_options* options, const
49 * char* path, cgltf_data** out_data)` can be used to open the given
50 * file using `FILE*` APIs and parse the data using `cgltf_parse()`.
52 * `cgltf_result cgltf_validate(cgltf_data*)` can be used to do additional
53 * checks to make sure the parsed glTF data is valid.
55 * `cgltf_node_transform_local` converts the translation / rotation / scale properties of a node
58 * `cgltf_node_transform_world` calls `cgltf_node_transform_local` on every ancestor in order
59 * to compute the root-to-node transformation.
61 * `cgltf_accessor_unpack_floats` reads in the data from an accessor, applies sparse data (if any),
62 * and converts them to floating point. Assumes that `cgltf_load_buffers` has already been called.
63 * By passing null for the output pointer, users can find out how many floats are required in the
66 * `cgltf_accessor_num_components` is a tiny utility that tells you the dimensionality of
67 * a certain accessor type. This can be used before `cgltf_accessor_unpack_floats` to help allocate
68 * the necessary amount of memory.
70 * `cgltf_accessor_read_float` reads a certain element from a non-sparse accessor and converts it to
71 * floating point, assuming that `cgltf_load_buffers` has already been called. The passed-in element
72 * size is the number of floats in the output buffer, which should be in the range [1, 16]. Returns
73 * false if the passed-in element_size is too small, or if the accessor is sparse.
75 * `cgltf_accessor_read_uint` is similar to its floating-point counterpart, but limited to reading
76 * vector types and does not support matrix types. The passed-in element size is the number of uints
77 * in the output buffer, which should be in the range [1, 4]. Returns false if the passed-in
78 * element_size is too small, or if the accessor is sparse.
80 * `cgltf_accessor_read_index` is similar to its floating-point counterpart, but it returns size_t
81 * and only works with single-component data types.
83 * `cgltf_result cgltf_copy_extras_json(const cgltf_data*, const cgltf_extras*,
84 * char* dest, cgltf_size* dest_size)` allows users to retrieve the "extras" data that
85 * can be attached to many glTF objects (which can be arbitrary JSON data). The
86 * `cgltf_extras` struct stores the offsets of the start and end of the extras JSON data
87 * as it appears in the complete glTF JSON data. This function copies the extras data
88 * into the provided buffer. If `dest` is NULL, the length of the data is written into
89 * `dest_size`. You can then parse this data using your own JSON parser
90 * or, if you've included the cgltf implementation using the integrated JSMN JSON parser.
92 #ifndef CGLTF_H_INCLUDED__
93 #define CGLTF_H_INCLUDED__
101 typedef size_t cgltf_size;
102 typedef float cgltf_float;
103 typedef int cgltf_int;
104 typedef unsigned int cgltf_uint;
105 typedef int cgltf_bool;
107 typedef enum cgltf_file_type
109 cgltf_file_type_invalid,
110 cgltf_file_type_gltf,
114 typedef enum cgltf_result
116 cgltf_result_success,
117 cgltf_result_data_too_short,
118 cgltf_result_unknown_format,
119 cgltf_result_invalid_json,
120 cgltf_result_invalid_gltf,
121 cgltf_result_invalid_options,
122 cgltf_result_file_not_found,
123 cgltf_result_io_error,
124 cgltf_result_out_of_memory,
125 cgltf_result_legacy_gltf,
128 typedef struct cgltf_memory_options
130 void* (*alloc)(void* user, cgltf_size size);
131 void (*free) (void* user, void* ptr);
133 } cgltf_memory_options;
135 typedef struct cgltf_file_options
137 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);
138 void (*release)(const struct cgltf_memory_options* memory_options, const struct cgltf_file_options* file_options, void* data);
140 } cgltf_file_options;
142 typedef struct cgltf_options
144 cgltf_file_type type; /* invalid == auto detect */
145 cgltf_size json_token_count; /* 0 == auto */
146 cgltf_memory_options memory;
147 cgltf_file_options file;
150 typedef enum cgltf_buffer_view_type
152 cgltf_buffer_view_type_invalid,
153 cgltf_buffer_view_type_indices,
154 cgltf_buffer_view_type_vertices,
155 } cgltf_buffer_view_type;
157 typedef enum cgltf_attribute_type
159 cgltf_attribute_type_invalid,
160 cgltf_attribute_type_position,
161 cgltf_attribute_type_normal,
162 cgltf_attribute_type_tangent,
163 cgltf_attribute_type_texcoord,
164 cgltf_attribute_type_color,
165 cgltf_attribute_type_joints,
166 cgltf_attribute_type_weights,
167 } cgltf_attribute_type;
169 typedef enum cgltf_component_type
171 cgltf_component_type_invalid,
172 cgltf_component_type_r_8, /* BYTE */
173 cgltf_component_type_r_8u, /* UNSIGNED_BYTE */
174 cgltf_component_type_r_16, /* SHORT */
175 cgltf_component_type_r_16u, /* UNSIGNED_SHORT */
176 cgltf_component_type_r_32u, /* UNSIGNED_INT */
177 cgltf_component_type_r_32f, /* FLOAT */
178 } cgltf_component_type;
180 typedef enum cgltf_type
192 typedef enum cgltf_primitive_type
194 cgltf_primitive_type_points,
195 cgltf_primitive_type_lines,
196 cgltf_primitive_type_line_loop,
197 cgltf_primitive_type_line_strip,
198 cgltf_primitive_type_triangles,
199 cgltf_primitive_type_triangle_strip,
200 cgltf_primitive_type_triangle_fan,
201 } cgltf_primitive_type;
203 typedef enum cgltf_alpha_mode
205 cgltf_alpha_mode_opaque,
206 cgltf_alpha_mode_mask,
207 cgltf_alpha_mode_blend,
210 typedef enum cgltf_animation_path_type {
211 cgltf_animation_path_type_invalid,
212 cgltf_animation_path_type_translation,
213 cgltf_animation_path_type_rotation,
214 cgltf_animation_path_type_scale,
215 cgltf_animation_path_type_weights,
216 } cgltf_animation_path_type;
218 typedef enum cgltf_interpolation_type {
219 cgltf_interpolation_type_linear,
220 cgltf_interpolation_type_step,
221 cgltf_interpolation_type_cubic_spline,
222 } cgltf_interpolation_type;
224 typedef enum cgltf_camera_type {
225 cgltf_camera_type_invalid,
226 cgltf_camera_type_perspective,
227 cgltf_camera_type_orthographic,
230 typedef enum cgltf_light_type {
231 cgltf_light_type_invalid,
232 cgltf_light_type_directional,
233 cgltf_light_type_point,
234 cgltf_light_type_spot,
237 typedef struct cgltf_extras {
238 cgltf_size start_offset;
239 cgltf_size end_offset;
242 typedef struct cgltf_extension {
247 typedef struct cgltf_buffer
252 void* data; /* loaded by cgltf_load_buffers */
254 cgltf_size extensions_count;
255 cgltf_extension* extensions;
258 typedef enum cgltf_meshopt_compression_mode {
259 cgltf_meshopt_compression_mode_invalid,
260 cgltf_meshopt_compression_mode_attributes,
261 cgltf_meshopt_compression_mode_triangles,
262 cgltf_meshopt_compression_mode_indices,
263 } cgltf_meshopt_compression_mode;
265 typedef enum cgltf_meshopt_compression_filter {
266 cgltf_meshopt_compression_filter_none,
267 cgltf_meshopt_compression_filter_octahedral,
268 cgltf_meshopt_compression_filter_quaternion,
269 cgltf_meshopt_compression_filter_exponential,
270 } cgltf_meshopt_compression_filter;
272 typedef struct cgltf_meshopt_compression
274 cgltf_buffer* buffer;
279 cgltf_meshopt_compression_mode mode;
280 cgltf_meshopt_compression_filter filter;
281 } cgltf_meshopt_compression;
283 typedef struct cgltf_buffer_view
286 cgltf_buffer* buffer;
289 cgltf_size stride; /* 0 == automatically determined by accessor */
290 cgltf_buffer_view_type type;
291 void* data; /* overrides buffer->data if present, filled by extensions */
292 cgltf_bool has_meshopt_compression;
293 cgltf_meshopt_compression meshopt_compression;
295 cgltf_size extensions_count;
296 cgltf_extension* extensions;
299 typedef struct cgltf_accessor_sparse
302 cgltf_buffer_view* indices_buffer_view;
303 cgltf_size indices_byte_offset;
304 cgltf_component_type indices_component_type;
305 cgltf_buffer_view* values_buffer_view;
306 cgltf_size values_byte_offset;
308 cgltf_extras indices_extras;
309 cgltf_extras values_extras;
310 cgltf_size extensions_count;
311 cgltf_extension* extensions;
312 cgltf_size indices_extensions_count;
313 cgltf_extension* indices_extensions;
314 cgltf_size values_extensions_count;
315 cgltf_extension* values_extensions;
316 } cgltf_accessor_sparse;
318 typedef struct cgltf_accessor
321 cgltf_component_type component_type;
322 cgltf_bool normalized;
327 cgltf_buffer_view* buffer_view;
332 cgltf_bool is_sparse;
333 cgltf_accessor_sparse sparse;
335 cgltf_size extensions_count;
336 cgltf_extension* extensions;
339 typedef struct cgltf_attribute
342 cgltf_attribute_type type;
344 cgltf_accessor* data;
347 typedef struct cgltf_image
351 cgltf_buffer_view* buffer_view;
354 cgltf_size extensions_count;
355 cgltf_extension* extensions;
358 typedef struct cgltf_sampler
361 cgltf_int mag_filter;
362 cgltf_int min_filter;
366 cgltf_size extensions_count;
367 cgltf_extension* extensions;
370 typedef struct cgltf_texture
374 cgltf_sampler* sampler;
376 cgltf_size extensions_count;
377 cgltf_extension* extensions;
380 typedef struct cgltf_texture_transform
382 cgltf_float offset[2];
383 cgltf_float rotation;
384 cgltf_float scale[2];
386 } cgltf_texture_transform;
388 typedef struct cgltf_texture_view
390 cgltf_texture* texture;
392 cgltf_float scale; /* equivalent to strength for occlusion_texture */
393 cgltf_bool has_transform;
394 cgltf_texture_transform transform;
396 cgltf_size extensions_count;
397 cgltf_extension* extensions;
398 } cgltf_texture_view;
400 typedef struct cgltf_pbr_metallic_roughness
402 cgltf_texture_view base_color_texture;
403 cgltf_texture_view metallic_roughness_texture;
405 cgltf_float base_color_factor[4];
406 cgltf_float metallic_factor;
407 cgltf_float roughness_factor;
410 } cgltf_pbr_metallic_roughness;
412 typedef struct cgltf_pbr_specular_glossiness
414 cgltf_texture_view diffuse_texture;
415 cgltf_texture_view specular_glossiness_texture;
417 cgltf_float diffuse_factor[4];
418 cgltf_float specular_factor[3];
419 cgltf_float glossiness_factor;
420 } cgltf_pbr_specular_glossiness;
422 typedef struct cgltf_clearcoat
424 cgltf_texture_view clearcoat_texture;
425 cgltf_texture_view clearcoat_roughness_texture;
426 cgltf_texture_view clearcoat_normal_texture;
428 cgltf_float clearcoat_factor;
429 cgltf_float clearcoat_roughness_factor;
432 typedef struct cgltf_transmission
434 cgltf_texture_view transmission_texture;
435 cgltf_float transmission_factor;
436 } cgltf_transmission;
438 typedef struct cgltf_ior
443 typedef struct cgltf_specular
445 cgltf_texture_view specular_texture;
446 cgltf_texture_view specular_color_texture;
447 cgltf_float specular_color_factor[3];
448 cgltf_float specular_factor;
451 typedef struct cgltf_volume
453 cgltf_texture_view thickness_texture;
454 cgltf_float thickness_factor;
455 cgltf_float attenuation_color[3];
456 cgltf_float attenuation_distance;
459 typedef struct cgltf_sheen
461 cgltf_texture_view sheen_color_texture;
462 cgltf_float sheen_color_factor[3];
463 cgltf_texture_view sheen_roughness_texture;
464 cgltf_float sheen_roughness_factor;
467 typedef struct cgltf_material
470 cgltf_bool has_pbr_metallic_roughness;
471 cgltf_bool has_pbr_specular_glossiness;
472 cgltf_bool has_clearcoat;
473 cgltf_bool has_transmission;
474 cgltf_bool has_volume;
476 cgltf_bool has_specular;
477 cgltf_bool has_sheen;
478 cgltf_pbr_metallic_roughness pbr_metallic_roughness;
479 cgltf_pbr_specular_glossiness pbr_specular_glossiness;
480 cgltf_clearcoat clearcoat;
482 cgltf_specular specular;
484 cgltf_transmission transmission;
486 cgltf_texture_view normal_texture;
487 cgltf_texture_view occlusion_texture;
488 cgltf_texture_view emissive_texture;
489 cgltf_float emissive_factor[3];
490 cgltf_alpha_mode alpha_mode;
491 cgltf_float alpha_cutoff;
492 cgltf_bool double_sided;
495 cgltf_size extensions_count;
496 cgltf_extension* extensions;
499 typedef struct cgltf_material_mapping
502 cgltf_material* material;
504 } cgltf_material_mapping;
506 typedef struct cgltf_morph_target {
507 cgltf_attribute* attributes;
508 cgltf_size attributes_count;
509 } cgltf_morph_target;
511 typedef struct cgltf_draco_mesh_compression {
512 cgltf_buffer_view* buffer_view;
513 cgltf_attribute* attributes;
514 cgltf_size attributes_count;
515 } cgltf_draco_mesh_compression;
517 typedef struct cgltf_primitive {
518 cgltf_primitive_type type;
519 cgltf_accessor* indices;
520 cgltf_material* material;
521 cgltf_attribute* attributes;
522 cgltf_size attributes_count;
523 cgltf_morph_target* targets;
524 cgltf_size targets_count;
526 cgltf_bool has_draco_mesh_compression;
527 cgltf_draco_mesh_compression draco_mesh_compression;
528 cgltf_material_mapping* mappings;
529 cgltf_size mappings_count;
530 cgltf_size extensions_count;
531 cgltf_extension* extensions;
534 typedef struct cgltf_mesh {
536 cgltf_primitive* primitives;
537 cgltf_size primitives_count;
538 cgltf_float* weights;
539 cgltf_size weights_count;
541 cgltf_size target_names_count;
543 cgltf_size extensions_count;
544 cgltf_extension* extensions;
547 typedef struct cgltf_node cgltf_node;
549 typedef struct cgltf_skin {
552 cgltf_size joints_count;
553 cgltf_node* skeleton;
554 cgltf_accessor* inverse_bind_matrices;
556 cgltf_size extensions_count;
557 cgltf_extension* extensions;
560 typedef struct cgltf_camera_perspective {
561 cgltf_bool has_aspect_ratio;
562 cgltf_float aspect_ratio;
568 } cgltf_camera_perspective;
570 typedef struct cgltf_camera_orthographic {
576 } cgltf_camera_orthographic;
578 typedef struct cgltf_camera {
580 cgltf_camera_type type;
582 cgltf_camera_perspective perspective;
583 cgltf_camera_orthographic orthographic;
586 cgltf_size extensions_count;
587 cgltf_extension* extensions;
590 typedef struct cgltf_light {
592 cgltf_float color[3];
593 cgltf_float intensity;
594 cgltf_light_type type;
596 cgltf_float spot_inner_cone_angle;
597 cgltf_float spot_outer_cone_angle;
603 cgltf_node** children;
604 cgltf_size children_count;
607 cgltf_camera* camera;
609 cgltf_float* weights;
610 cgltf_size weights_count;
611 cgltf_bool has_translation;
612 cgltf_bool has_rotation;
613 cgltf_bool has_scale;
614 cgltf_bool has_matrix;
615 cgltf_float translation[3];
616 cgltf_float rotation[4];
617 cgltf_float scale[3];
618 cgltf_float matrix[16];
620 cgltf_size extensions_count;
621 cgltf_extension* extensions;
624 typedef struct cgltf_scene {
627 cgltf_size nodes_count;
629 cgltf_size extensions_count;
630 cgltf_extension* extensions;
633 typedef struct cgltf_animation_sampler {
634 cgltf_accessor* input;
635 cgltf_accessor* output;
636 cgltf_interpolation_type interpolation;
638 cgltf_size extensions_count;
639 cgltf_extension* extensions;
640 } cgltf_animation_sampler;
642 typedef struct cgltf_animation_channel {
643 cgltf_animation_sampler* sampler;
644 cgltf_node* target_node;
645 cgltf_animation_path_type target_path;
647 cgltf_size extensions_count;
648 cgltf_extension* extensions;
649 } cgltf_animation_channel;
651 typedef struct cgltf_animation {
653 cgltf_animation_sampler* samplers;
654 cgltf_size samplers_count;
655 cgltf_animation_channel* channels;
656 cgltf_size channels_count;
658 cgltf_size extensions_count;
659 cgltf_extension* extensions;
662 typedef struct cgltf_material_variant
666 } cgltf_material_variant;
668 typedef struct cgltf_asset {
674 cgltf_size extensions_count;
675 cgltf_extension* extensions;
678 typedef struct cgltf_data
680 cgltf_file_type file_type;
686 cgltf_size meshes_count;
688 cgltf_material* materials;
689 cgltf_size materials_count;
691 cgltf_accessor* accessors;
692 cgltf_size accessors_count;
694 cgltf_buffer_view* buffer_views;
695 cgltf_size buffer_views_count;
697 cgltf_buffer* buffers;
698 cgltf_size buffers_count;
701 cgltf_size images_count;
703 cgltf_texture* textures;
704 cgltf_size textures_count;
706 cgltf_sampler* samplers;
707 cgltf_size samplers_count;
710 cgltf_size skins_count;
712 cgltf_camera* cameras;
713 cgltf_size cameras_count;
716 cgltf_size lights_count;
719 cgltf_size nodes_count;
722 cgltf_size scenes_count;
726 cgltf_animation* animations;
727 cgltf_size animations_count;
729 cgltf_material_variant* variants;
730 cgltf_size variants_count;
734 cgltf_size data_extensions_count;
735 cgltf_extension* data_extensions;
737 char** extensions_used;
738 cgltf_size extensions_used_count;
740 char** extensions_required;
741 cgltf_size extensions_required_count;
744 cgltf_size json_size;
749 cgltf_memory_options memory;
750 cgltf_file_options file;
753 cgltf_result cgltf_parse(
754 const cgltf_options* options,
757 cgltf_data** out_data);
759 cgltf_result cgltf_parse_file(
760 const cgltf_options* options,
762 cgltf_data** out_data);
764 cgltf_result cgltf_load_buffers(
765 const cgltf_options* options,
767 const char* gltf_path);
769 cgltf_result cgltf_load_buffer_base64(const cgltf_options* options, cgltf_size size, const char* base64, void** out_data);
771 void cgltf_decode_uri(char* uri);
773 cgltf_result cgltf_validate(cgltf_data* data);
775 void cgltf_free(cgltf_data* data);
777 void cgltf_node_transform_local(const cgltf_node* node, cgltf_float* out_matrix);
778 void cgltf_node_transform_world(const cgltf_node* node, cgltf_float* out_matrix);
780 cgltf_bool cgltf_accessor_read_float(const cgltf_accessor* accessor, cgltf_size index, cgltf_float* out, cgltf_size element_size);
781 cgltf_bool cgltf_accessor_read_uint(const cgltf_accessor* accessor, cgltf_size index, cgltf_uint* out, cgltf_size element_size);
782 cgltf_size cgltf_accessor_read_index(const cgltf_accessor* accessor, cgltf_size index);
784 cgltf_size cgltf_num_components(cgltf_type type);
786 cgltf_size cgltf_accessor_unpack_floats(const cgltf_accessor* accessor, cgltf_float* out, cgltf_size float_count);
788 cgltf_result cgltf_copy_extras_json(const cgltf_data* data, const cgltf_extras* extras, char* dest, cgltf_size* dest_size);
794 #endif /* #ifndef CGLTF_H_INCLUDED__ */
798 * Stop now, if you are only interested in the API.
799 * Below, you find the implementation.
803 #if defined(__INTELLISENSE__) || defined(__JETBRAINS_IDE__)
804 /* This makes MSVC/CLion intellisense work. */
805 #define CGLTF_IMPLEMENTATION
808 #ifdef CGLTF_IMPLEMENTATION
810 #include <stdint.h> /* For uint8_t, uint32_t */
811 #include <string.h> /* For strncpy */
812 #include <stdio.h> /* For fopen */
813 #include <limits.h> /* For UINT_MAX etc */
814 #include <float.h> /* For FLT_MAX */
816 #if !defined(CGLTF_MALLOC) || !defined(CGLTF_FREE) || !defined(CGLTF_ATOI) || !defined(CGLTF_ATOF)
817 #include <stdlib.h> /* For malloc, free, atoi, atof */
820 /* JSMN_PARENT_LINKS is necessary to make parsing large structures linear in input size */
821 #define JSMN_PARENT_LINKS
823 /* JSMN_STRICT is necessary to reject invalid JSON documents */
828 * Source: https://github.com/zserge/jsmn
839 /* Not enough tokens were provided */
840 JSMN_ERROR_NOMEM = -1,
841 /* Invalid character inside JSON string */
842 JSMN_ERROR_INVAL = -2,
843 /* The string is not a full JSON packet, more bytes expected */
851 #ifdef JSMN_PARENT_LINKS
856 unsigned int pos; /* offset in the JSON string */
857 unsigned int toknext; /* next token to allocate */
858 int toksuper; /* superior token node, e.g parent object or array */
860 static void jsmn_init(jsmn_parser *parser);
861 static int jsmn_parse(jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, size_t num_tokens);
867 static const cgltf_size GlbHeaderSize = 12;
868 static const cgltf_size GlbChunkHeaderSize = 8;
869 static const uint32_t GlbVersion = 2;
870 static const uint32_t GlbMagic = 0x46546C67;
871 static const uint32_t GlbMagicJsonChunk = 0x4E4F534A;
872 static const uint32_t GlbMagicBinChunk = 0x004E4942;
875 #define CGLTF_MALLOC(size) malloc(size)
878 #define CGLTF_FREE(ptr) free(ptr)
881 #define CGLTF_ATOI(str) atoi(str)
884 #define CGLTF_ATOF(str) atof(str)
886 #ifndef CGLTF_VALIDATE_ENABLE_ASSERTS
887 #define CGLTF_VALIDATE_ENABLE_ASSERTS 0
890 static void* cgltf_default_alloc(void* user, cgltf_size size)
893 return CGLTF_MALLOC(size);
896 static void cgltf_default_free(void* user, void* ptr)
902 static void* cgltf_calloc(cgltf_options* options, size_t element_size, cgltf_size count)
904 if (SIZE_MAX / element_size < count)
908 void* result = options->memory.alloc(options->memory.user_data, element_size * count);
913 memset(result, 0, element_size * count);
917 static cgltf_result cgltf_default_file_read(const struct cgltf_memory_options* memory_options, const struct cgltf_file_options* file_options, const char* path, cgltf_size* size, void** data)
920 void* (*memory_alloc)(void*, cgltf_size) = memory_options->alloc ? memory_options->alloc : &cgltf_default_alloc;
921 void (*memory_free)(void*, void*) = memory_options->free ? memory_options->free : &cgltf_default_free;
923 FILE* file = fopen(path, "rb");
926 return cgltf_result_file_not_found;
929 cgltf_size file_size = size ? *size : 0;
933 fseek(file, 0, SEEK_END);
935 long length = ftell(file);
939 return cgltf_result_io_error;
942 fseek(file, 0, SEEK_SET);
943 file_size = (cgltf_size)length;
946 char* file_data = (char*)memory_alloc(memory_options->user_data, file_size);
950 return cgltf_result_out_of_memory;
953 cgltf_size read_size = fread(file_data, 1, file_size, file);
957 if (read_size != file_size)
959 memory_free(memory_options->user_data, file_data);
960 return cgltf_result_io_error;
972 return cgltf_result_success;
975 static void cgltf_default_file_release(const struct cgltf_memory_options* memory_options, const struct cgltf_file_options* file_options, void* data)
978 void (*memfree)(void*, void*) = memory_options->free ? memory_options->free : &cgltf_default_free;
979 memfree(memory_options->user_data, data);
982 static cgltf_result cgltf_parse_json(cgltf_options* options, const uint8_t* json_chunk, cgltf_size size, cgltf_data** out_data);
984 cgltf_result cgltf_parse(const cgltf_options* options, const void* data, cgltf_size size, cgltf_data** out_data)
986 if (size < GlbHeaderSize)
988 return cgltf_result_data_too_short;
993 return cgltf_result_invalid_options;
996 cgltf_options fixed_options = *options;
997 if (fixed_options.memory.alloc == NULL)
999 fixed_options.memory.alloc = &cgltf_default_alloc;
1001 if (fixed_options.memory.free == NULL)
1003 fixed_options.memory.free = &cgltf_default_free;
1008 memcpy(&tmp, data, 4);
1009 if (tmp != GlbMagic)
1011 if (fixed_options.type == cgltf_file_type_invalid)
1013 fixed_options.type = cgltf_file_type_gltf;
1015 else if (fixed_options.type == cgltf_file_type_glb)
1017 return cgltf_result_unknown_format;
1021 if (fixed_options.type == cgltf_file_type_gltf)
1023 cgltf_result json_result = cgltf_parse_json(&fixed_options, (const uint8_t*)data, size, out_data);
1024 if (json_result != cgltf_result_success)
1029 (*out_data)->file_type = cgltf_file_type_gltf;
1031 return cgltf_result_success;
1034 const uint8_t* ptr = (const uint8_t*)data;
1036 memcpy(&tmp, ptr + 4, 4);
1037 uint32_t version = tmp;
1038 if (version != GlbVersion)
1040 return version < GlbVersion ? cgltf_result_legacy_gltf : cgltf_result_unknown_format;
1044 memcpy(&tmp, ptr + 8, 4);
1047 return cgltf_result_data_too_short;
1050 const uint8_t* json_chunk = ptr + GlbHeaderSize;
1052 if (GlbHeaderSize + GlbChunkHeaderSize > size)
1054 return cgltf_result_data_too_short;
1057 // JSON chunk: length
1058 uint32_t json_length;
1059 memcpy(&json_length, json_chunk, 4);
1060 if (GlbHeaderSize + GlbChunkHeaderSize + json_length > size)
1062 return cgltf_result_data_too_short;
1065 // JSON chunk: magic
1066 memcpy(&tmp, json_chunk + 4, 4);
1067 if (tmp != GlbMagicJsonChunk)
1069 return cgltf_result_unknown_format;
1072 json_chunk += GlbChunkHeaderSize;
1074 const void* bin = 0;
1075 cgltf_size bin_size = 0;
1077 if (GlbHeaderSize + GlbChunkHeaderSize + json_length + GlbChunkHeaderSize <= size)
1079 // We can read another chunk
1080 const uint8_t* bin_chunk = json_chunk + json_length;
1082 // Bin chunk: length
1083 uint32_t bin_length;
1084 memcpy(&bin_length, bin_chunk, 4);
1085 if (GlbHeaderSize + GlbChunkHeaderSize + json_length + GlbChunkHeaderSize + bin_length > size)
1087 return cgltf_result_data_too_short;
1091 memcpy(&tmp, bin_chunk + 4, 4);
1092 if (tmp != GlbMagicBinChunk)
1094 return cgltf_result_unknown_format;
1097 bin_chunk += GlbChunkHeaderSize;
1100 bin_size = bin_length;
1103 cgltf_result json_result = cgltf_parse_json(&fixed_options, json_chunk, json_length, out_data);
1104 if (json_result != cgltf_result_success)
1109 (*out_data)->file_type = cgltf_file_type_glb;
1110 (*out_data)->bin = bin;
1111 (*out_data)->bin_size = bin_size;
1113 return cgltf_result_success;
1116 cgltf_result cgltf_parse_file(const cgltf_options* options, const char* path, cgltf_data** out_data)
1118 if (options == NULL)
1120 return cgltf_result_invalid_options;
1123 void (*memory_free)(void*, void*) = options->memory.free ? options->memory.free : &cgltf_default_free;
1124 cgltf_result (*file_read)(const struct cgltf_memory_options*, const struct cgltf_file_options*, const char*, cgltf_size*, void**) = options->file.read ? options->file.read : &cgltf_default_file_read;
1126 void* file_data = NULL;
1127 cgltf_size file_size = 0;
1128 cgltf_result result = file_read(&options->memory, &options->file, path, &file_size, &file_data);
1129 if (result != cgltf_result_success)
1134 result = cgltf_parse(options, file_data, file_size, out_data);
1136 if (result != cgltf_result_success)
1138 memory_free(options->memory.user_data, file_data);
1142 (*out_data)->file_data = file_data;
1144 return cgltf_result_success;
1147 static void cgltf_combine_paths(char* path, const char* base, const char* uri)
1149 const char* s0 = strrchr(base, '/');
1150 const char* s1 = strrchr(base, '\\');
1151 const char* slash = s0 ? (s1 && s1 > s0 ? s1 : s0) : s1;
1155 size_t prefix = slash - base + 1;
1157 strncpy(path, base, prefix);
1158 strcpy(path + prefix, uri);
1166 static cgltf_result cgltf_load_buffer_file(const cgltf_options* options, cgltf_size size, const char* uri, const char* gltf_path, void** out_data)
1168 void* (*memory_alloc)(void*, cgltf_size) = options->memory.alloc ? options->memory.alloc : &cgltf_default_alloc;
1169 void (*memory_free)(void*, void*) = options->memory.free ? options->memory.free : &cgltf_default_free;
1170 cgltf_result (*file_read)(const struct cgltf_memory_options*, const struct cgltf_file_options*, const char*, cgltf_size*, void**) = options->file.read ? options->file.read : &cgltf_default_file_read;
1172 char* path = (char*)memory_alloc(options->memory.user_data, strlen(uri) + strlen(gltf_path) + 1);
1175 return cgltf_result_out_of_memory;
1178 cgltf_combine_paths(path, gltf_path, uri);
1180 // after combining, the tail of the resulting path is a uri; decode_uri converts it into path
1181 cgltf_decode_uri(path + strlen(path) - strlen(uri));
1183 void* file_data = NULL;
1184 cgltf_result result = file_read(&options->memory, &options->file, path, &size, &file_data);
1186 memory_free(options->memory.user_data, path);
1188 *out_data = (result == cgltf_result_success) ? file_data : NULL;
1193 cgltf_result cgltf_load_buffer_base64(const cgltf_options* options, cgltf_size size, const char* base64, void** out_data)
1195 void* (*memory_alloc)(void*, cgltf_size) = options->memory.alloc ? options->memory.alloc : &cgltf_default_alloc;
1196 void (*memory_free)(void*, void*) = options->memory.free ? options->memory.free : &cgltf_default_free;
1198 unsigned char* data = (unsigned char*)memory_alloc(options->memory.user_data, size);
1201 return cgltf_result_out_of_memory;
1204 unsigned int buffer = 0;
1205 unsigned int buffer_bits = 0;
1207 for (cgltf_size i = 0; i < size; ++i)
1209 while (buffer_bits < 8)
1211 char ch = *base64++;
1214 (unsigned)(ch - 'A') < 26 ? (ch - 'A') :
1215 (unsigned)(ch - 'a') < 26 ? (ch - 'a') + 26 :
1216 (unsigned)(ch - '0') < 10 ? (ch - '0') + 52 :
1223 memory_free(options->memory.user_data, data);
1224 return cgltf_result_io_error;
1227 buffer = (buffer << 6) | index;
1231 data[i] = (unsigned char)(buffer >> (buffer_bits - 8));
1237 return cgltf_result_success;
1240 static int cgltf_unhex(char ch)
1243 (unsigned)(ch - '0') < 10 ? (ch - '0') :
1244 (unsigned)(ch - 'A') < 6 ? (ch - 'A') + 10 :
1245 (unsigned)(ch - 'a') < 6 ? (ch - 'a') + 10 :
1249 void cgltf_decode_uri(char* uri)
1258 int ch1 = cgltf_unhex(i[1]);
1262 int ch2 = cgltf_unhex(i[2]);
1266 *write++ = (char)(ch1 * 16 + ch2);
1279 cgltf_result cgltf_load_buffers(const cgltf_options* options, cgltf_data* data, const char* gltf_path)
1281 if (options == NULL)
1283 return cgltf_result_invalid_options;
1286 if (data->buffers_count && data->buffers[0].data == NULL && data->buffers[0].uri == NULL && data->bin)
1288 if (data->bin_size < data->buffers[0].size)
1290 return cgltf_result_data_too_short;
1293 data->buffers[0].data = (void*)data->bin;
1296 for (cgltf_size i = 0; i < data->buffers_count; ++i)
1298 if (data->buffers[i].data)
1303 const char* uri = data->buffers[i].uri;
1310 if (strncmp(uri, "data:", 5) == 0)
1312 const char* comma = strchr(uri, ',');
1314 if (comma && comma - uri >= 7 && strncmp(comma - 7, ";base64", 7) == 0)
1316 cgltf_result res = cgltf_load_buffer_base64(options, data->buffers[i].size, comma + 1, &data->buffers[i].data);
1318 if (res != cgltf_result_success)
1325 return cgltf_result_unknown_format;
1328 else if (strstr(uri, "://") == NULL && gltf_path)
1330 cgltf_result res = cgltf_load_buffer_file(options, data->buffers[i].size, uri, gltf_path, &data->buffers[i].data);
1332 if (res != cgltf_result_success)
1339 return cgltf_result_unknown_format;
1343 return cgltf_result_success;
1346 static cgltf_size cgltf_calc_size(cgltf_type type, cgltf_component_type component_type);
1348 static cgltf_size cgltf_calc_index_bound(cgltf_buffer_view* buffer_view, cgltf_size offset, cgltf_component_type component_type, cgltf_size count)
1350 char* data = (char*)buffer_view->buffer->data + offset + buffer_view->offset;
1351 cgltf_size bound = 0;
1353 switch (component_type)
1355 case cgltf_component_type_r_8u:
1356 for (size_t i = 0; i < count; ++i)
1358 cgltf_size v = ((unsigned char*)data)[i];
1359 bound = bound > v ? bound : v;
1363 case cgltf_component_type_r_16u:
1364 for (size_t i = 0; i < count; ++i)
1366 cgltf_size v = ((unsigned short*)data)[i];
1367 bound = bound > v ? bound : v;
1371 case cgltf_component_type_r_32u:
1372 for (size_t i = 0; i < count; ++i)
1374 cgltf_size v = ((unsigned int*)data)[i];
1375 bound = bound > v ? bound : v;
1386 #if CGLTF_VALIDATE_ENABLE_ASSERTS
1387 #define CGLTF_ASSERT_IF(cond, result) assert(!(cond)); if (cond) return result;
1389 #define CGLTF_ASSERT_IF(cond, result) if (cond) return result;
1392 cgltf_result cgltf_validate(cgltf_data* data)
1394 for (cgltf_size i = 0; i < data->accessors_count; ++i)
1396 cgltf_accessor* accessor = &data->accessors[i];
1398 cgltf_size element_size = cgltf_calc_size(accessor->type, accessor->component_type);
1400 if (accessor->buffer_view)
1402 cgltf_size req_size = accessor->offset + accessor->stride * (accessor->count - 1) + element_size;
1404 CGLTF_ASSERT_IF(accessor->buffer_view->size < req_size, cgltf_result_data_too_short);
1407 if (accessor->is_sparse)
1409 cgltf_accessor_sparse* sparse = &accessor->sparse;
1411 cgltf_size indices_component_size = cgltf_calc_size(cgltf_type_scalar, sparse->indices_component_type);
1412 cgltf_size indices_req_size = sparse->indices_byte_offset + indices_component_size * sparse->count;
1413 cgltf_size values_req_size = sparse->values_byte_offset + element_size * sparse->count;
1415 CGLTF_ASSERT_IF(sparse->indices_buffer_view->size < indices_req_size ||
1416 sparse->values_buffer_view->size < values_req_size, cgltf_result_data_too_short);
1418 CGLTF_ASSERT_IF(sparse->indices_component_type != cgltf_component_type_r_8u &&
1419 sparse->indices_component_type != cgltf_component_type_r_16u &&
1420 sparse->indices_component_type != cgltf_component_type_r_32u, cgltf_result_invalid_gltf);
1422 if (sparse->indices_buffer_view->buffer->data)
1424 cgltf_size index_bound = cgltf_calc_index_bound(sparse->indices_buffer_view, sparse->indices_byte_offset, sparse->indices_component_type, sparse->count);
1426 CGLTF_ASSERT_IF(index_bound >= accessor->count, cgltf_result_data_too_short);
1431 for (cgltf_size i = 0; i < data->buffer_views_count; ++i)
1433 cgltf_size req_size = data->buffer_views[i].offset + data->buffer_views[i].size;
1435 CGLTF_ASSERT_IF(data->buffer_views[i].buffer && data->buffer_views[i].buffer->size < req_size, cgltf_result_data_too_short);
1437 if (data->buffer_views[i].has_meshopt_compression)
1439 cgltf_meshopt_compression* mc = &data->buffer_views[i].meshopt_compression;
1441 CGLTF_ASSERT_IF(mc->buffer == NULL || mc->buffer->size < mc->offset + mc->size, cgltf_result_data_too_short);
1443 CGLTF_ASSERT_IF(data->buffer_views[i].stride && mc->stride != data->buffer_views[i].stride, cgltf_result_invalid_gltf);
1445 CGLTF_ASSERT_IF(data->buffer_views[i].size != mc->stride * mc->count, cgltf_result_invalid_gltf);
1447 CGLTF_ASSERT_IF(mc->mode == cgltf_meshopt_compression_mode_invalid, cgltf_result_invalid_gltf);
1449 CGLTF_ASSERT_IF(mc->mode == cgltf_meshopt_compression_mode_attributes && !(mc->stride % 4 == 0 && mc->stride <= 256), cgltf_result_invalid_gltf);
1451 CGLTF_ASSERT_IF(mc->mode == cgltf_meshopt_compression_mode_triangles && mc->count % 3 != 0, cgltf_result_invalid_gltf);
1453 CGLTF_ASSERT_IF((mc->mode == cgltf_meshopt_compression_mode_triangles || mc->mode == cgltf_meshopt_compression_mode_indices) && mc->stride != 2 && mc->stride != 4, cgltf_result_invalid_gltf);
1455 CGLTF_ASSERT_IF((mc->mode == cgltf_meshopt_compression_mode_triangles || mc->mode == cgltf_meshopt_compression_mode_indices) && mc->filter != cgltf_meshopt_compression_filter_none, cgltf_result_invalid_gltf);
1457 CGLTF_ASSERT_IF(mc->filter == cgltf_meshopt_compression_filter_octahedral && mc->stride != 4 && mc->stride != 8, cgltf_result_invalid_gltf);
1459 CGLTF_ASSERT_IF(mc->filter == cgltf_meshopt_compression_filter_quaternion && mc->stride != 8, cgltf_result_invalid_gltf);
1463 for (cgltf_size i = 0; i < data->meshes_count; ++i)
1465 if (data->meshes[i].weights)
1467 CGLTF_ASSERT_IF(data->meshes[i].primitives_count && data->meshes[i].primitives[0].targets_count != data->meshes[i].weights_count, cgltf_result_invalid_gltf);
1470 if (data->meshes[i].target_names)
1472 CGLTF_ASSERT_IF(data->meshes[i].primitives_count && data->meshes[i].primitives[0].targets_count != data->meshes[i].target_names_count, cgltf_result_invalid_gltf);
1475 for (cgltf_size j = 0; j < data->meshes[i].primitives_count; ++j)
1477 CGLTF_ASSERT_IF(data->meshes[i].primitives[j].targets_count != data->meshes[i].primitives[0].targets_count, cgltf_result_invalid_gltf);
1479 if (data->meshes[i].primitives[j].attributes_count)
1481 cgltf_accessor* first = data->meshes[i].primitives[j].attributes[0].data;
1483 for (cgltf_size k = 0; k < data->meshes[i].primitives[j].attributes_count; ++k)
1485 CGLTF_ASSERT_IF(data->meshes[i].primitives[j].attributes[k].data->count != first->count, cgltf_result_invalid_gltf);
1488 for (cgltf_size k = 0; k < data->meshes[i].primitives[j].targets_count; ++k)
1490 for (cgltf_size m = 0; m < data->meshes[i].primitives[j].targets[k].attributes_count; ++m)
1492 CGLTF_ASSERT_IF(data->meshes[i].primitives[j].targets[k].attributes[m].data->count != first->count, cgltf_result_invalid_gltf);
1496 cgltf_accessor* indices = data->meshes[i].primitives[j].indices;
1498 CGLTF_ASSERT_IF(indices &&
1499 indices->component_type != cgltf_component_type_r_8u &&
1500 indices->component_type != cgltf_component_type_r_16u &&
1501 indices->component_type != cgltf_component_type_r_32u, cgltf_result_invalid_gltf);
1503 if (indices && indices->buffer_view && indices->buffer_view->buffer->data)
1505 cgltf_size index_bound = cgltf_calc_index_bound(indices->buffer_view, indices->offset, indices->component_type, indices->count);
1507 CGLTF_ASSERT_IF(index_bound >= first->count, cgltf_result_data_too_short);
1510 for (cgltf_size k = 0; k < data->meshes[i].primitives[j].mappings_count; ++k)
1512 CGLTF_ASSERT_IF(data->meshes[i].primitives[j].mappings[k].variant >= data->variants_count, cgltf_result_invalid_gltf);
1518 for (cgltf_size i = 0; i < data->nodes_count; ++i)
1520 if (data->nodes[i].weights && data->nodes[i].mesh)
1522 CGLTF_ASSERT_IF (data->nodes[i].mesh->primitives_count && data->nodes[i].mesh->primitives[0].targets_count != data->nodes[i].weights_count, cgltf_result_invalid_gltf);
1526 for (cgltf_size i = 0; i < data->nodes_count; ++i)
1528 cgltf_node* p1 = data->nodes[i].parent;
1529 cgltf_node* p2 = p1 ? p1->parent : NULL;
1533 CGLTF_ASSERT_IF(p1 == p2, cgltf_result_invalid_gltf);
1536 p2 = p2->parent ? p2->parent->parent : NULL;
1540 for (cgltf_size i = 0; i < data->scenes_count; ++i)
1542 for (cgltf_size j = 0; j < data->scenes[i].nodes_count; ++j)
1544 CGLTF_ASSERT_IF(data->scenes[i].nodes[j]->parent, cgltf_result_invalid_gltf);
1548 for (cgltf_size i = 0; i < data->animations_count; ++i)
1550 for (cgltf_size j = 0; j < data->animations[i].channels_count; ++j)
1552 cgltf_animation_channel* channel = &data->animations[i].channels[j];
1554 if (!channel->target_node)
1559 cgltf_size components = 1;
1561 if (channel->target_path == cgltf_animation_path_type_weights)
1563 CGLTF_ASSERT_IF(!channel->target_node->mesh || !channel->target_node->mesh->primitives_count, cgltf_result_invalid_gltf);
1565 components = channel->target_node->mesh->primitives[0].targets_count;
1568 cgltf_size values = channel->sampler->interpolation == cgltf_interpolation_type_cubic_spline ? 3 : 1;
1570 CGLTF_ASSERT_IF(channel->sampler->input->count * components * values != channel->sampler->output->count, cgltf_result_data_too_short);
1574 return cgltf_result_success;
1577 cgltf_result cgltf_copy_extras_json(const cgltf_data* data, const cgltf_extras* extras, char* dest, cgltf_size* dest_size)
1579 cgltf_size json_size = extras->end_offset - extras->start_offset;
1585 *dest_size = json_size + 1;
1586 return cgltf_result_success;
1588 return cgltf_result_invalid_options;
1591 if (*dest_size + 1 < json_size)
1593 strncpy(dest, data->json + extras->start_offset, *dest_size - 1);
1594 dest[*dest_size - 1] = 0;
1598 strncpy(dest, data->json + extras->start_offset, json_size);
1599 dest[json_size] = 0;
1602 return cgltf_result_success;
1605 void cgltf_free_extensions(cgltf_data* data, cgltf_extension* extensions, cgltf_size extensions_count)
1607 for (cgltf_size i = 0; i < extensions_count; ++i)
1609 data->memory.free(data->memory.user_data, extensions[i].name);
1610 data->memory.free(data->memory.user_data, extensions[i].data);
1612 data->memory.free(data->memory.user_data, extensions);
1615 void cgltf_free(cgltf_data* data)
1622 void (*file_release)(const struct cgltf_memory_options*, const struct cgltf_file_options*, void* data) = data->file.release ? data->file.release : cgltf_default_file_release;
1624 data->memory.free(data->memory.user_data, data->asset.copyright);
1625 data->memory.free(data->memory.user_data, data->asset.generator);
1626 data->memory.free(data->memory.user_data, data->asset.version);
1627 data->memory.free(data->memory.user_data, data->asset.min_version);
1629 cgltf_free_extensions(data, data->asset.extensions, data->asset.extensions_count);
1631 for (cgltf_size i = 0; i < data->accessors_count; ++i)
1633 data->memory.free(data->memory.user_data, data->accessors[i].name);
1635 if(data->accessors[i].is_sparse)
1637 cgltf_free_extensions(data, data->accessors[i].sparse.extensions, data->accessors[i].sparse.extensions_count);
1638 cgltf_free_extensions(data, data->accessors[i].sparse.indices_extensions, data->accessors[i].sparse.indices_extensions_count);
1639 cgltf_free_extensions(data, data->accessors[i].sparse.values_extensions, data->accessors[i].sparse.values_extensions_count);
1641 cgltf_free_extensions(data, data->accessors[i].extensions, data->accessors[i].extensions_count);
1643 data->memory.free(data->memory.user_data, data->accessors);
1645 for (cgltf_size i = 0; i < data->buffer_views_count; ++i)
1647 data->memory.free(data->memory.user_data, data->buffer_views[i].name);
1648 data->memory.free(data->memory.user_data, data->buffer_views[i].data);
1650 cgltf_free_extensions(data, data->buffer_views[i].extensions, data->buffer_views[i].extensions_count);
1652 data->memory.free(data->memory.user_data, data->buffer_views);
1654 for (cgltf_size i = 0; i < data->buffers_count; ++i)
1656 data->memory.free(data->memory.user_data, data->buffers[i].name);
1658 if (data->buffers[i].data != data->bin)
1660 file_release(&data->memory, &data->file, data->buffers[i].data);
1662 data->memory.free(data->memory.user_data, data->buffers[i].uri);
1664 cgltf_free_extensions(data, data->buffers[i].extensions, data->buffers[i].extensions_count);
1667 data->memory.free(data->memory.user_data, data->buffers);
1669 for (cgltf_size i = 0; i < data->meshes_count; ++i)
1671 data->memory.free(data->memory.user_data, data->meshes[i].name);
1673 for (cgltf_size j = 0; j < data->meshes[i].primitives_count; ++j)
1675 for (cgltf_size k = 0; k < data->meshes[i].primitives[j].attributes_count; ++k)
1677 data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].attributes[k].name);
1680 data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].attributes);
1682 for (cgltf_size k = 0; k < data->meshes[i].primitives[j].targets_count; ++k)
1684 for (cgltf_size m = 0; m < data->meshes[i].primitives[j].targets[k].attributes_count; ++m)
1686 data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].targets[k].attributes[m].name);
1689 data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].targets[k].attributes);
1692 data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].targets);
1694 if (data->meshes[i].primitives[j].has_draco_mesh_compression)
1696 for (cgltf_size k = 0; k < data->meshes[i].primitives[j].draco_mesh_compression.attributes_count; ++k)
1698 data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].draco_mesh_compression.attributes[k].name);
1701 data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].draco_mesh_compression.attributes);
1704 data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].mappings);
1706 cgltf_free_extensions(data, data->meshes[i].primitives[j].extensions, data->meshes[i].primitives[j].extensions_count);
1709 data->memory.free(data->memory.user_data, data->meshes[i].primitives);
1710 data->memory.free(data->memory.user_data, data->meshes[i].weights);
1712 for (cgltf_size j = 0; j < data->meshes[i].target_names_count; ++j)
1714 data->memory.free(data->memory.user_data, data->meshes[i].target_names[j]);
1717 cgltf_free_extensions(data, data->meshes[i].extensions, data->meshes[i].extensions_count);
1719 data->memory.free(data->memory.user_data, data->meshes[i].target_names);
1722 data->memory.free(data->memory.user_data, data->meshes);
1724 for (cgltf_size i = 0; i < data->materials_count; ++i)
1726 data->memory.free(data->memory.user_data, data->materials[i].name);
1728 if(data->materials[i].has_pbr_metallic_roughness)
1730 cgltf_free_extensions(data, data->materials[i].pbr_metallic_roughness.metallic_roughness_texture.extensions, data->materials[i].pbr_metallic_roughness.metallic_roughness_texture.extensions_count);
1731 cgltf_free_extensions(data, data->materials[i].pbr_metallic_roughness.base_color_texture.extensions, data->materials[i].pbr_metallic_roughness.base_color_texture.extensions_count);
1733 if(data->materials[i].has_pbr_specular_glossiness)
1735 cgltf_free_extensions(data, data->materials[i].pbr_specular_glossiness.diffuse_texture.extensions, data->materials[i].pbr_specular_glossiness.diffuse_texture.extensions_count);
1736 cgltf_free_extensions(data, data->materials[i].pbr_specular_glossiness.specular_glossiness_texture.extensions, data->materials[i].pbr_specular_glossiness.specular_glossiness_texture.extensions_count);
1738 if(data->materials[i].has_clearcoat)
1740 cgltf_free_extensions(data, data->materials[i].clearcoat.clearcoat_texture.extensions, data->materials[i].clearcoat.clearcoat_texture.extensions_count);
1741 cgltf_free_extensions(data, data->materials[i].clearcoat.clearcoat_roughness_texture.extensions, data->materials[i].clearcoat.clearcoat_roughness_texture.extensions_count);
1742 cgltf_free_extensions(data, data->materials[i].clearcoat.clearcoat_normal_texture.extensions, data->materials[i].clearcoat.clearcoat_normal_texture.extensions_count);
1744 if(data->materials[i].has_specular)
1746 cgltf_free_extensions(data, data->materials[i].specular.specular_texture.extensions, data->materials[i].specular.specular_texture.extensions_count);
1747 cgltf_free_extensions(data, data->materials[i].specular.specular_color_texture.extensions, data->materials[i].specular.specular_color_texture.extensions_count);
1749 if(data->materials[i].has_transmission)
1751 cgltf_free_extensions(data, data->materials[i].transmission.transmission_texture.extensions, data->materials[i].transmission.transmission_texture.extensions_count);
1753 if (data->materials[i].has_volume)
1755 cgltf_free_extensions(data, data->materials[i].volume.thickness_texture.extensions, data->materials[i].volume.thickness_texture.extensions_count);
1757 if(data->materials[i].has_sheen)
1759 cgltf_free_extensions(data, data->materials[i].sheen.sheen_color_texture.extensions, data->materials[i].sheen.sheen_color_texture.extensions_count);
1760 cgltf_free_extensions(data, data->materials[i].sheen.sheen_roughness_texture.extensions, data->materials[i].sheen.sheen_roughness_texture.extensions_count);
1763 cgltf_free_extensions(data, data->materials[i].normal_texture.extensions, data->materials[i].normal_texture.extensions_count);
1764 cgltf_free_extensions(data, data->materials[i].occlusion_texture.extensions, data->materials[i].occlusion_texture.extensions_count);
1765 cgltf_free_extensions(data, data->materials[i].emissive_texture.extensions, data->materials[i].emissive_texture.extensions_count);
1767 cgltf_free_extensions(data, data->materials[i].extensions, data->materials[i].extensions_count);
1770 data->memory.free(data->memory.user_data, data->materials);
1772 for (cgltf_size i = 0; i < data->images_count; ++i)
1774 data->memory.free(data->memory.user_data, data->images[i].name);
1775 data->memory.free(data->memory.user_data, data->images[i].uri);
1776 data->memory.free(data->memory.user_data, data->images[i].mime_type);
1778 cgltf_free_extensions(data, data->images[i].extensions, data->images[i].extensions_count);
1781 data->memory.free(data->memory.user_data, data->images);
1783 for (cgltf_size i = 0; i < data->textures_count; ++i)
1785 data->memory.free(data->memory.user_data, data->textures[i].name);
1786 cgltf_free_extensions(data, data->textures[i].extensions, data->textures[i].extensions_count);
1789 data->memory.free(data->memory.user_data, data->textures);
1791 for (cgltf_size i = 0; i < data->samplers_count; ++i)
1793 data->memory.free(data->memory.user_data, data->samplers[i].name);
1794 cgltf_free_extensions(data, data->samplers[i].extensions, data->samplers[i].extensions_count);
1797 data->memory.free(data->memory.user_data, data->samplers);
1799 for (cgltf_size i = 0; i < data->skins_count; ++i)
1801 data->memory.free(data->memory.user_data, data->skins[i].name);
1802 data->memory.free(data->memory.user_data, data->skins[i].joints);
1804 cgltf_free_extensions(data, data->skins[i].extensions, data->skins[i].extensions_count);
1807 data->memory.free(data->memory.user_data, data->skins);
1809 for (cgltf_size i = 0; i < data->cameras_count; ++i)
1811 data->memory.free(data->memory.user_data, data->cameras[i].name);
1812 cgltf_free_extensions(data, data->cameras[i].extensions, data->cameras[i].extensions_count);
1815 data->memory.free(data->memory.user_data, data->cameras);
1817 for (cgltf_size i = 0; i < data->lights_count; ++i)
1819 data->memory.free(data->memory.user_data, data->lights[i].name);
1822 data->memory.free(data->memory.user_data, data->lights);
1824 for (cgltf_size i = 0; i < data->nodes_count; ++i)
1826 data->memory.free(data->memory.user_data, data->nodes[i].name);
1827 data->memory.free(data->memory.user_data, data->nodes[i].children);
1828 data->memory.free(data->memory.user_data, data->nodes[i].weights);
1829 cgltf_free_extensions(data, data->nodes[i].extensions, data->nodes[i].extensions_count);
1832 data->memory.free(data->memory.user_data, data->nodes);
1834 for (cgltf_size i = 0; i < data->scenes_count; ++i)
1836 data->memory.free(data->memory.user_data, data->scenes[i].name);
1837 data->memory.free(data->memory.user_data, data->scenes[i].nodes);
1839 cgltf_free_extensions(data, data->scenes[i].extensions, data->scenes[i].extensions_count);
1842 data->memory.free(data->memory.user_data, data->scenes);
1844 for (cgltf_size i = 0; i < data->animations_count; ++i)
1846 data->memory.free(data->memory.user_data, data->animations[i].name);
1847 for (cgltf_size j = 0; j < data->animations[i].samplers_count; ++j)
1849 cgltf_free_extensions(data, data->animations[i].samplers[j].extensions, data->animations[i].samplers[j].extensions_count);
1851 data->memory.free(data->memory.user_data, data->animations[i].samplers);
1853 for (cgltf_size j = 0; j < data->animations[i].channels_count; ++j)
1855 cgltf_free_extensions(data, data->animations[i].channels[j].extensions, data->animations[i].channels[j].extensions_count);
1857 data->memory.free(data->memory.user_data, data->animations[i].channels);
1859 cgltf_free_extensions(data, data->animations[i].extensions, data->animations[i].extensions_count);
1862 data->memory.free(data->memory.user_data, data->animations);
1864 for (cgltf_size i = 0; i < data->variants_count; ++i)
1866 data->memory.free(data->memory.user_data, data->variants[i].name);
1869 data->memory.free(data->memory.user_data, data->variants);
1871 cgltf_free_extensions(data, data->data_extensions, data->data_extensions_count);
1873 for (cgltf_size i = 0; i < data->extensions_used_count; ++i)
1875 data->memory.free(data->memory.user_data, data->extensions_used[i]);
1878 data->memory.free(data->memory.user_data, data->extensions_used);
1880 for (cgltf_size i = 0; i < data->extensions_required_count; ++i)
1882 data->memory.free(data->memory.user_data, data->extensions_required[i]);
1885 data->memory.free(data->memory.user_data, data->extensions_required);
1887 file_release(&data->memory, &data->file, data->file_data);
1889 data->memory.free(data->memory.user_data, data);
1892 void cgltf_node_transform_local(const cgltf_node* node, cgltf_float* out_matrix)
1894 cgltf_float* lm = out_matrix;
1896 if (node->has_matrix)
1898 memcpy(lm, node->matrix, sizeof(float) * 16);
1902 float tx = node->translation[0];
1903 float ty = node->translation[1];
1904 float tz = node->translation[2];
1906 float qx = node->rotation[0];
1907 float qy = node->rotation[1];
1908 float qz = node->rotation[2];
1909 float qw = node->rotation[3];
1911 float sx = node->scale[0];
1912 float sy = node->scale[1];
1913 float sz = node->scale[2];
1915 lm[0] = (1 - 2 * qy*qy - 2 * qz*qz) * sx;
1916 lm[1] = (2 * qx*qy + 2 * qz*qw) * sx;
1917 lm[2] = (2 * qx*qz - 2 * qy*qw) * sx;
1920 lm[4] = (2 * qx*qy - 2 * qz*qw) * sy;
1921 lm[5] = (1 - 2 * qx*qx - 2 * qz*qz) * sy;
1922 lm[6] = (2 * qy*qz + 2 * qx*qw) * sy;
1925 lm[8] = (2 * qx*qz + 2 * qy*qw) * sz;
1926 lm[9] = (2 * qy*qz - 2 * qx*qw) * sz;
1927 lm[10] = (1 - 2 * qx*qx - 2 * qy*qy) * sz;
1937 void cgltf_node_transform_world(const cgltf_node* node, cgltf_float* out_matrix)
1939 cgltf_float* lm = out_matrix;
1940 cgltf_node_transform_local(node, lm);
1942 const cgltf_node* parent = node->parent;
1947 cgltf_node_transform_local(parent, pm);
1949 for (int i = 0; i < 4; ++i)
1951 float l0 = lm[i * 4 + 0];
1952 float l1 = lm[i * 4 + 1];
1953 float l2 = lm[i * 4 + 2];
1955 float r0 = l0 * pm[0] + l1 * pm[4] + l2 * pm[8];
1956 float r1 = l0 * pm[1] + l1 * pm[5] + l2 * pm[9];
1957 float r2 = l0 * pm[2] + l1 * pm[6] + l2 * pm[10];
1968 parent = parent->parent;
1972 static cgltf_size cgltf_component_read_index(const void* in, cgltf_component_type component_type)
1974 switch (component_type)
1976 case cgltf_component_type_r_16:
1977 return *((const int16_t*) in);
1978 case cgltf_component_type_r_16u:
1979 return *((const uint16_t*) in);
1980 case cgltf_component_type_r_32u:
1981 return *((const uint32_t*) in);
1982 case cgltf_component_type_r_32f:
1983 return (cgltf_size)*((const float*) in);
1984 case cgltf_component_type_r_8:
1985 return *((const int8_t*) in);
1986 case cgltf_component_type_r_8u:
1987 return *((const uint8_t*) in);
1993 static cgltf_float cgltf_component_read_float(const void* in, cgltf_component_type component_type, cgltf_bool normalized)
1995 if (component_type == cgltf_component_type_r_32f)
1997 return *((const float*) in);
2002 switch (component_type)
2004 // note: glTF spec doesn't currently define normalized conversions for 32-bit integers
2005 case cgltf_component_type_r_16:
2006 return *((const int16_t*) in) / (cgltf_float)32767;
2007 case cgltf_component_type_r_16u:
2008 return *((const uint16_t*) in) / (cgltf_float)65535;
2009 case cgltf_component_type_r_8:
2010 return *((const int8_t*) in) / (cgltf_float)127;
2011 case cgltf_component_type_r_8u:
2012 return *((const uint8_t*) in) / (cgltf_float)255;
2018 return (cgltf_float)cgltf_component_read_index(in, component_type);
2021 static cgltf_size cgltf_component_size(cgltf_component_type component_type);
2023 static cgltf_bool cgltf_element_read_float(const uint8_t* element, cgltf_type type, cgltf_component_type component_type, cgltf_bool normalized, cgltf_float* out, cgltf_size element_size)
2025 cgltf_size num_components = cgltf_num_components(type);
2027 if (element_size < num_components) {
2031 // There are three special cases for component extraction, see #data-alignment in the 2.0 spec.
2033 cgltf_size component_size = cgltf_component_size(component_type);
2035 if (type == cgltf_type_mat2 && component_size == 1)
2037 out[0] = cgltf_component_read_float(element, component_type, normalized);
2038 out[1] = cgltf_component_read_float(element + 1, component_type, normalized);
2039 out[2] = cgltf_component_read_float(element + 4, component_type, normalized);
2040 out[3] = cgltf_component_read_float(element + 5, component_type, normalized);
2044 if (type == cgltf_type_mat3 && component_size == 1)
2046 out[0] = cgltf_component_read_float(element, component_type, normalized);
2047 out[1] = cgltf_component_read_float(element + 1, component_type, normalized);
2048 out[2] = cgltf_component_read_float(element + 2, component_type, normalized);
2049 out[3] = cgltf_component_read_float(element + 4, component_type, normalized);
2050 out[4] = cgltf_component_read_float(element + 5, component_type, normalized);
2051 out[5] = cgltf_component_read_float(element + 6, component_type, normalized);
2052 out[6] = cgltf_component_read_float(element + 8, component_type, normalized);
2053 out[7] = cgltf_component_read_float(element + 9, component_type, normalized);
2054 out[8] = cgltf_component_read_float(element + 10, component_type, normalized);
2058 if (type == cgltf_type_mat3 && component_size == 2)
2060 out[0] = cgltf_component_read_float(element, component_type, normalized);
2061 out[1] = cgltf_component_read_float(element + 2, component_type, normalized);
2062 out[2] = cgltf_component_read_float(element + 4, component_type, normalized);
2063 out[3] = cgltf_component_read_float(element + 8, component_type, normalized);
2064 out[4] = cgltf_component_read_float(element + 10, component_type, normalized);
2065 out[5] = cgltf_component_read_float(element + 12, component_type, normalized);
2066 out[6] = cgltf_component_read_float(element + 16, component_type, normalized);
2067 out[7] = cgltf_component_read_float(element + 18, component_type, normalized);
2068 out[8] = cgltf_component_read_float(element + 20, component_type, normalized);
2072 for (cgltf_size i = 0; i < num_components; ++i)
2074 out[i] = cgltf_component_read_float(element + component_size * i, component_type, normalized);
2079 const uint8_t* cgltf_buffer_view_data(const cgltf_buffer_view* view)
2082 return (const uint8_t*)view->data;
2084 if (!view->buffer->data)
2087 const uint8_t* result = (const uint8_t*)view->buffer->data;
2088 result += view->offset;
2092 cgltf_bool cgltf_accessor_read_float(const cgltf_accessor* accessor, cgltf_size index, cgltf_float* out, cgltf_size element_size)
2094 if (accessor->is_sparse)
2098 if (accessor->buffer_view == NULL)
2100 memset(out, 0, element_size * sizeof(cgltf_float));
2103 const uint8_t* element = cgltf_buffer_view_data(accessor->buffer_view);
2104 if (element == NULL)
2108 element += accessor->offset + accessor->stride * index;
2109 return cgltf_element_read_float(element, accessor->type, accessor->component_type, accessor->normalized, out, element_size);
2112 cgltf_size cgltf_accessor_unpack_floats(const cgltf_accessor* accessor, cgltf_float* out, cgltf_size float_count)
2114 cgltf_size floats_per_element = cgltf_num_components(accessor->type);
2115 cgltf_size available_floats = accessor->count * floats_per_element;
2118 return available_floats;
2121 float_count = available_floats < float_count ? available_floats : float_count;
2122 cgltf_size element_count = float_count / floats_per_element;
2124 // First pass: convert each element in the base accessor.
2125 cgltf_float* dest = out;
2126 cgltf_accessor dense = *accessor;
2127 dense.is_sparse = 0;
2128 for (cgltf_size index = 0; index < element_count; index++, dest += floats_per_element)
2130 if (!cgltf_accessor_read_float(&dense, index, dest, floats_per_element))
2136 // Second pass: write out each element in the sparse accessor.
2137 if (accessor->is_sparse)
2139 const cgltf_accessor_sparse* sparse = &dense.sparse;
2141 const uint8_t* index_data = cgltf_buffer_view_data(sparse->indices_buffer_view);
2142 const uint8_t* reader_head = cgltf_buffer_view_data(sparse->values_buffer_view);
2144 if (index_data == NULL || reader_head == NULL)
2149 index_data += sparse->indices_byte_offset;
2150 reader_head += sparse->values_byte_offset;
2152 cgltf_size index_stride = cgltf_component_size(sparse->indices_component_type);
2153 for (cgltf_size reader_index = 0; reader_index < sparse->count; reader_index++, index_data += index_stride)
2155 size_t writer_index = cgltf_component_read_index(index_data, sparse->indices_component_type);
2156 float* writer_head = out + writer_index * floats_per_element;
2158 if (!cgltf_element_read_float(reader_head, dense.type, dense.component_type, dense.normalized, writer_head, floats_per_element))
2163 reader_head += dense.stride;
2167 return element_count * floats_per_element;
2170 static cgltf_uint cgltf_component_read_uint(const void* in, cgltf_component_type component_type)
2172 switch (component_type)
2174 case cgltf_component_type_r_8:
2175 return *((const int8_t*) in);
2177 case cgltf_component_type_r_8u:
2178 return *((const uint8_t*) in);
2180 case cgltf_component_type_r_16:
2181 return *((const int16_t*) in);
2183 case cgltf_component_type_r_16u:
2184 return *((const uint16_t*) in);
2186 case cgltf_component_type_r_32u:
2187 return *((const uint32_t*) in);
2194 static cgltf_bool cgltf_element_read_uint(const uint8_t* element, cgltf_type type, cgltf_component_type component_type, cgltf_uint* out, cgltf_size element_size)
2196 cgltf_size num_components = cgltf_num_components(type);
2198 if (element_size < num_components)
2203 // Reading integer matrices is not a valid use case
2204 if (type == cgltf_type_mat2 || type == cgltf_type_mat3 || type == cgltf_type_mat4)
2209 cgltf_size component_size = cgltf_component_size(component_type);
2211 for (cgltf_size i = 0; i < num_components; ++i)
2213 out[i] = cgltf_component_read_uint(element + component_size * i, component_type);
2218 cgltf_bool cgltf_accessor_read_uint(const cgltf_accessor* accessor, cgltf_size index, cgltf_uint* out, cgltf_size element_size)
2220 if (accessor->is_sparse)
2224 if (accessor->buffer_view == NULL)
2226 memset(out, 0, element_size * sizeof( cgltf_uint ));
2229 const uint8_t* element = cgltf_buffer_view_data(accessor->buffer_view);
2230 if (element == NULL)
2234 element += accessor->offset + accessor->stride * index;
2235 return cgltf_element_read_uint(element, accessor->type, accessor->component_type, out, element_size);
2238 cgltf_size cgltf_accessor_read_index(const cgltf_accessor* accessor, cgltf_size index)
2240 if (accessor->is_sparse)
2242 return 0; // This is an error case, but we can't communicate the error with existing interface.
2244 if (accessor->buffer_view == NULL)
2248 const uint8_t* element = cgltf_buffer_view_data(accessor->buffer_view);
2249 if (element == NULL)
2251 return 0; // This is an error case, but we can't communicate the error with existing interface.
2253 element += accessor->offset + accessor->stride * index;
2254 return cgltf_component_read_index(element, accessor->component_type);
2257 #define CGLTF_ERROR_JSON -1
2258 #define CGLTF_ERROR_NOMEM -2
2259 #define CGLTF_ERROR_LEGACY -3
2261 #define CGLTF_CHECK_TOKTYPE(tok_, type_) if ((tok_).type != (type_)) { return CGLTF_ERROR_JSON; }
2262 #define CGLTF_CHECK_KEY(tok_) if ((tok_).type != JSMN_STRING || (tok_).size == 0) { return CGLTF_ERROR_JSON; } /* checking size for 0 verifies that a value follows the key */
2264 #define CGLTF_PTRINDEX(type, idx) (type*)((cgltf_size)idx + 1)
2265 #define CGLTF_PTRFIXUP(var, data, size) if (var) { if ((cgltf_size)var > size) { return CGLTF_ERROR_JSON; } var = &data[(cgltf_size)var-1]; }
2266 #define CGLTF_PTRFIXUP_REQ(var, data, size) if (!var || (cgltf_size)var > size) { return CGLTF_ERROR_JSON; } var = &data[(cgltf_size)var-1];
2268 static int cgltf_json_strcmp(jsmntok_t const* tok, const uint8_t* json_chunk, const char* str)
2270 CGLTF_CHECK_TOKTYPE(*tok, JSMN_STRING);
2271 size_t const str_len = strlen(str);
2272 size_t const name_length = tok->end - tok->start;
2273 return (str_len == name_length) ? strncmp((const char*)json_chunk + tok->start, str, str_len) : 128;
2276 static int cgltf_json_to_int(jsmntok_t const* tok, const uint8_t* json_chunk)
2278 CGLTF_CHECK_TOKTYPE(*tok, JSMN_PRIMITIVE);
2280 int size = (cgltf_size)(tok->end - tok->start) < sizeof(tmp) ? tok->end - tok->start : (int)(sizeof(tmp) - 1);
2281 strncpy(tmp, (const char*)json_chunk + tok->start, size);
2283 return CGLTF_ATOI(tmp);
2286 static cgltf_float cgltf_json_to_float(jsmntok_t const* tok, const uint8_t* json_chunk)
2288 CGLTF_CHECK_TOKTYPE(*tok, JSMN_PRIMITIVE);
2290 int size = (cgltf_size)(tok->end - tok->start) < sizeof(tmp) ? tok->end - tok->start : (int)(sizeof(tmp) - 1);
2291 strncpy(tmp, (const char*)json_chunk + tok->start, size);
2293 return (cgltf_float)CGLTF_ATOF(tmp);
2296 static cgltf_bool cgltf_json_to_bool(jsmntok_t const* tok, const uint8_t* json_chunk)
2298 int size = tok->end - tok->start;
2299 return size == 4 && memcmp(json_chunk + tok->start, "true", 4) == 0;
2302 static int cgltf_skip_json(jsmntok_t const* tokens, int i)
2308 switch (tokens[i].type)
2311 end += tokens[i].size * 2;
2315 end += tokens[i].size;
2318 case JSMN_PRIMITIVE:
2332 static void cgltf_fill_float_array(float* out_array, int size, float value)
2334 for (int j = 0; j < size; ++j)
2336 out_array[j] = value;
2340 static int cgltf_parse_json_float_array(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, float* out_array, int size)
2342 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_ARRAY);
2343 if (tokens[i].size != size)
2345 return CGLTF_ERROR_JSON;
2348 for (int j = 0; j < size; ++j)
2350 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
2351 out_array[j] = cgltf_json_to_float(tokens + i, json_chunk);
2357 static int cgltf_parse_json_string(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, char** out_string)
2359 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_STRING);
2362 return CGLTF_ERROR_JSON;
2364 int size = tokens[i].end - tokens[i].start;
2365 char* result = (char*)options->memory.alloc(options->memory.user_data, size + 1);
2368 return CGLTF_ERROR_NOMEM;
2370 strncpy(result, (const char*)json_chunk + tokens[i].start, size);
2372 *out_string = result;
2376 static int cgltf_parse_json_array(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, size_t element_size, void** out_array, cgltf_size* out_size)
2379 if (tokens[i].type != JSMN_ARRAY)
2381 return tokens[i].type == JSMN_OBJECT ? CGLTF_ERROR_LEGACY : CGLTF_ERROR_JSON;
2385 return CGLTF_ERROR_JSON;
2387 int size = tokens[i].size;
2388 void* result = cgltf_calloc(options, element_size, size);
2391 return CGLTF_ERROR_NOMEM;
2393 *out_array = result;
2398 static int cgltf_parse_json_string_array(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, char*** out_array, cgltf_size* out_size)
2400 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_ARRAY);
2401 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(char*), (void**)out_array, out_size);
2407 for (cgltf_size j = 0; j < *out_size; ++j)
2409 i = cgltf_parse_json_string(options, tokens, i, json_chunk, j + (*out_array));
2418 static void cgltf_parse_attribute_type(const char* name, cgltf_attribute_type* out_type, int* out_index)
2420 const char* us = strchr(name, '_');
2421 size_t len = us ? (size_t)(us - name) : strlen(name);
2423 if (len == 8 && strncmp(name, "POSITION", 8) == 0)
2425 *out_type = cgltf_attribute_type_position;
2427 else if (len == 6 && strncmp(name, "NORMAL", 6) == 0)
2429 *out_type = cgltf_attribute_type_normal;
2431 else if (len == 7 && strncmp(name, "TANGENT", 7) == 0)
2433 *out_type = cgltf_attribute_type_tangent;
2435 else if (len == 8 && strncmp(name, "TEXCOORD", 8) == 0)
2437 *out_type = cgltf_attribute_type_texcoord;
2439 else if (len == 5 && strncmp(name, "COLOR", 5) == 0)
2441 *out_type = cgltf_attribute_type_color;
2443 else if (len == 6 && strncmp(name, "JOINTS", 6) == 0)
2445 *out_type = cgltf_attribute_type_joints;
2447 else if (len == 7 && strncmp(name, "WEIGHTS", 7) == 0)
2449 *out_type = cgltf_attribute_type_weights;
2453 *out_type = cgltf_attribute_type_invalid;
2456 if (us && *out_type != cgltf_attribute_type_invalid)
2458 *out_index = CGLTF_ATOI(us + 1);
2462 static int cgltf_parse_json_attribute_list(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_attribute** out_attributes, cgltf_size* out_attributes_count)
2464 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
2466 if (*out_attributes)
2468 return CGLTF_ERROR_JSON;
2471 *out_attributes_count = tokens[i].size;
2472 *out_attributes = (cgltf_attribute*)cgltf_calloc(options, sizeof(cgltf_attribute), *out_attributes_count);
2475 if (!*out_attributes)
2477 return CGLTF_ERROR_NOMEM;
2480 for (cgltf_size j = 0; j < *out_attributes_count; ++j)
2482 CGLTF_CHECK_KEY(tokens[i]);
2484 i = cgltf_parse_json_string(options, tokens, i, json_chunk, &(*out_attributes)[j].name);
2487 return CGLTF_ERROR_JSON;
2490 cgltf_parse_attribute_type((*out_attributes)[j].name, &(*out_attributes)[j].type, &(*out_attributes)[j].index);
2492 (*out_attributes)[j].data = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
2499 static int cgltf_parse_json_extras(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_extras* out_extras)
2502 out_extras->start_offset = tokens[i].start;
2503 out_extras->end_offset = tokens[i].end;
2504 i = cgltf_skip_json(tokens, i);
2508 static int cgltf_parse_json_unprocessed_extension(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_extension* out_extension)
2510 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_STRING);
2511 CGLTF_CHECK_TOKTYPE(tokens[i+1], JSMN_OBJECT);
2512 if (out_extension->name)
2514 return CGLTF_ERROR_JSON;
2517 cgltf_size name_length = tokens[i].end - tokens[i].start;
2518 out_extension->name = (char*)options->memory.alloc(options->memory.user_data, name_length + 1);
2519 if (!out_extension->name)
2521 return CGLTF_ERROR_NOMEM;
2523 strncpy(out_extension->name, (const char*)json_chunk + tokens[i].start, name_length);
2524 out_extension->name[name_length] = 0;
2527 size_t start = tokens[i].start;
2528 size_t size = tokens[i].end - start;
2529 out_extension->data = (char*)options->memory.alloc(options->memory.user_data, size + 1);
2530 if (!out_extension->data)
2532 return CGLTF_ERROR_NOMEM;
2534 strncpy(out_extension->data, (const char*)json_chunk + start, size);
2535 out_extension->data[size] = '\0';
2537 i = cgltf_skip_json(tokens, i);
2542 static int cgltf_parse_json_unprocessed_extensions(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_size* out_extensions_count, cgltf_extension** out_extensions)
2546 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
2549 return CGLTF_ERROR_JSON;
2552 int extensions_size = tokens[i].size;
2553 *out_extensions_count = 0;
2554 *out_extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
2556 if (!*out_extensions)
2558 return CGLTF_ERROR_NOMEM;
2563 for (int j = 0; j < extensions_size; ++j)
2565 CGLTF_CHECK_KEY(tokens[i]);
2567 cgltf_size extension_index = (*out_extensions_count)++;
2568 cgltf_extension* extension = &((*out_extensions)[extension_index]);
2569 i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, extension);
2579 static int cgltf_parse_json_draco_mesh_compression(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_draco_mesh_compression* out_draco_mesh_compression)
2581 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
2583 int size = tokens[i].size;
2586 for (int j = 0; j < size; ++j)
2588 CGLTF_CHECK_KEY(tokens[i]);
2590 if (cgltf_json_strcmp(tokens + i, json_chunk, "attributes") == 0)
2592 i = cgltf_parse_json_attribute_list(options, tokens, i + 1, json_chunk, &out_draco_mesh_compression->attributes, &out_draco_mesh_compression->attributes_count);
2594 else if (cgltf_json_strcmp(tokens + i, json_chunk, "bufferView") == 0)
2597 out_draco_mesh_compression->buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
2610 static int cgltf_parse_json_material_mapping_data(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_material_mapping* out_mappings, cgltf_size* offset)
2613 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_ARRAY);
2615 int size = tokens[i].size;
2618 for (int j = 0; j < size; ++j)
2620 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
2622 int obj_size = tokens[i].size;
2626 int variants_tok = -1;
2627 cgltf_extras extras = {0, 0};
2629 for (int k = 0; k < obj_size; ++k)
2631 CGLTF_CHECK_KEY(tokens[i]);
2633 if (cgltf_json_strcmp(tokens + i, json_chunk, "material") == 0)
2636 material = cgltf_json_to_int(tokens + i, json_chunk);
2639 else if (cgltf_json_strcmp(tokens + i, json_chunk, "variants") == 0)
2642 CGLTF_CHECK_TOKTYPE(tokens[variants_tok], JSMN_ARRAY);
2644 i = cgltf_skip_json(tokens, i+1);
2646 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
2648 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &extras);
2652 i = cgltf_skip_json(tokens, i+1);
2661 if (material < 0 || variants_tok < 0)
2663 return CGLTF_ERROR_JSON;
2668 for (int k = 0; k < tokens[variants_tok].size; ++k)
2670 int variant = cgltf_json_to_int(&tokens[variants_tok + 1 + k], json_chunk);
2674 out_mappings[*offset].material = CGLTF_PTRINDEX(cgltf_material, material);
2675 out_mappings[*offset].variant = variant;
2676 out_mappings[*offset].extras = extras;
2683 (*offset) += tokens[variants_tok].size;
2690 static int cgltf_parse_json_material_mappings(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_primitive* out_prim)
2692 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
2694 int size = tokens[i].size;
2697 for (int j = 0; j < size; ++j)
2699 CGLTF_CHECK_KEY(tokens[i]);
2701 if (cgltf_json_strcmp(tokens + i, json_chunk, "mappings") == 0)
2703 if (out_prim->mappings)
2705 return CGLTF_ERROR_JSON;
2708 cgltf_size mappings_offset = 0;
2709 int k = cgltf_parse_json_material_mapping_data(options, tokens, i + 1, json_chunk, NULL, &mappings_offset);
2715 out_prim->mappings_count = mappings_offset;
2716 out_prim->mappings = (cgltf_material_mapping*)cgltf_calloc(options, sizeof(cgltf_material_mapping), out_prim->mappings_count);
2718 mappings_offset = 0;
2719 i = cgltf_parse_json_material_mapping_data(options, tokens, i + 1, json_chunk, out_prim->mappings, &mappings_offset);
2723 i = cgltf_skip_json(tokens, i+1);
2735 static int cgltf_parse_json_primitive(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_primitive* out_prim)
2737 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
2739 out_prim->type = cgltf_primitive_type_triangles;
2741 int size = tokens[i].size;
2744 for (int j = 0; j < size; ++j)
2746 CGLTF_CHECK_KEY(tokens[i]);
2748 if (cgltf_json_strcmp(tokens+i, json_chunk, "mode") == 0)
2752 = (cgltf_primitive_type)
2753 cgltf_json_to_int(tokens+i, json_chunk);
2756 else if (cgltf_json_strcmp(tokens+i, json_chunk, "indices") == 0)
2759 out_prim->indices = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
2762 else if (cgltf_json_strcmp(tokens+i, json_chunk, "material") == 0)
2765 out_prim->material = CGLTF_PTRINDEX(cgltf_material, cgltf_json_to_int(tokens + i, json_chunk));
2768 else if (cgltf_json_strcmp(tokens+i, json_chunk, "attributes") == 0)
2770 i = cgltf_parse_json_attribute_list(options, tokens, i + 1, json_chunk, &out_prim->attributes, &out_prim->attributes_count);
2772 else if (cgltf_json_strcmp(tokens+i, json_chunk, "targets") == 0)
2774 i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_morph_target), (void**)&out_prim->targets, &out_prim->targets_count);
2780 for (cgltf_size k = 0; k < out_prim->targets_count; ++k)
2782 i = cgltf_parse_json_attribute_list(options, tokens, i, json_chunk, &out_prim->targets[k].attributes, &out_prim->targets[k].attributes_count);
2789 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
2791 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_prim->extras);
2793 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
2797 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
2798 if(out_prim->extensions)
2800 return CGLTF_ERROR_JSON;
2803 int extensions_size = tokens[i].size;
2804 out_prim->extensions_count = 0;
2805 out_prim->extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
2807 if (!out_prim->extensions)
2809 return CGLTF_ERROR_NOMEM;
2813 for (int k = 0; k < extensions_size; ++k)
2815 CGLTF_CHECK_KEY(tokens[i]);
2817 if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_draco_mesh_compression") == 0)
2819 out_prim->has_draco_mesh_compression = 1;
2820 i = cgltf_parse_json_draco_mesh_compression(options, tokens, i + 1, json_chunk, &out_prim->draco_mesh_compression);
2822 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_variants") == 0)
2824 i = cgltf_parse_json_material_mappings(options, tokens, i + 1, json_chunk, out_prim);
2828 i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_prim->extensions[out_prim->extensions_count++]));
2839 i = cgltf_skip_json(tokens, i+1);
2851 static int cgltf_parse_json_mesh(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_mesh* out_mesh)
2853 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
2855 int size = tokens[i].size;
2858 for (int j = 0; j < size; ++j)
2860 CGLTF_CHECK_KEY(tokens[i]);
2862 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
2864 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_mesh->name);
2866 else if (cgltf_json_strcmp(tokens+i, json_chunk, "primitives") == 0)
2868 i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_primitive), (void**)&out_mesh->primitives, &out_mesh->primitives_count);
2874 for (cgltf_size prim_index = 0; prim_index < out_mesh->primitives_count; ++prim_index)
2876 i = cgltf_parse_json_primitive(options, tokens, i, json_chunk, &out_mesh->primitives[prim_index]);
2883 else if (cgltf_json_strcmp(tokens + i, json_chunk, "weights") == 0)
2885 i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_float), (void**)&out_mesh->weights, &out_mesh->weights_count);
2891 i = cgltf_parse_json_float_array(tokens, i - 1, json_chunk, out_mesh->weights, (int)out_mesh->weights_count);
2893 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
2897 out_mesh->extras.start_offset = tokens[i].start;
2898 out_mesh->extras.end_offset = tokens[i].end;
2900 if (tokens[i].type == JSMN_OBJECT)
2902 int extras_size = tokens[i].size;
2905 for (int k = 0; k < extras_size; ++k)
2907 CGLTF_CHECK_KEY(tokens[i]);
2909 if (cgltf_json_strcmp(tokens+i, json_chunk, "targetNames") == 0 && tokens[i+1].type == JSMN_ARRAY)
2911 i = cgltf_parse_json_string_array(options, tokens, i + 1, json_chunk, &out_mesh->target_names, &out_mesh->target_names_count);
2915 i = cgltf_skip_json(tokens, i+1);
2926 i = cgltf_skip_json(tokens, i);
2929 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
2931 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_mesh->extensions_count, &out_mesh->extensions);
2935 i = cgltf_skip_json(tokens, i+1);
2947 static int cgltf_parse_json_meshes(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
2949 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_mesh), (void**)&out_data->meshes, &out_data->meshes_count);
2955 for (cgltf_size j = 0; j < out_data->meshes_count; ++j)
2957 i = cgltf_parse_json_mesh(options, tokens, i, json_chunk, &out_data->meshes[j]);
2966 static cgltf_component_type cgltf_json_to_component_type(jsmntok_t const* tok, const uint8_t* json_chunk)
2968 int type = cgltf_json_to_int(tok, json_chunk);
2973 return cgltf_component_type_r_8;
2975 return cgltf_component_type_r_8u;
2977 return cgltf_component_type_r_16;
2979 return cgltf_component_type_r_16u;
2981 return cgltf_component_type_r_32u;
2983 return cgltf_component_type_r_32f;
2985 return cgltf_component_type_invalid;
2989 static int cgltf_parse_json_accessor_sparse(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_accessor_sparse* out_sparse)
2991 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
2993 int size = tokens[i].size;
2996 for (int j = 0; j < size; ++j)
2998 CGLTF_CHECK_KEY(tokens[i]);
3000 if (cgltf_json_strcmp(tokens+i, json_chunk, "count") == 0)
3003 out_sparse->count = cgltf_json_to_int(tokens + i, json_chunk);
3006 else if (cgltf_json_strcmp(tokens+i, json_chunk, "indices") == 0)
3009 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3011 int indices_size = tokens[i].size;
3014 for (int k = 0; k < indices_size; ++k)
3016 CGLTF_CHECK_KEY(tokens[i]);
3018 if (cgltf_json_strcmp(tokens+i, json_chunk, "bufferView") == 0)
3021 out_sparse->indices_buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
3024 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
3027 out_sparse->indices_byte_offset = cgltf_json_to_int(tokens + i, json_chunk);
3030 else if (cgltf_json_strcmp(tokens+i, json_chunk, "componentType") == 0)
3033 out_sparse->indices_component_type = cgltf_json_to_component_type(tokens + i, json_chunk);
3036 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3038 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_sparse->indices_extras);
3040 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
3042 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_sparse->indices_extensions_count, &out_sparse->indices_extensions);
3046 i = cgltf_skip_json(tokens, i+1);
3055 else if (cgltf_json_strcmp(tokens+i, json_chunk, "values") == 0)
3058 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3060 int values_size = tokens[i].size;
3063 for (int k = 0; k < values_size; ++k)
3065 CGLTF_CHECK_KEY(tokens[i]);
3067 if (cgltf_json_strcmp(tokens+i, json_chunk, "bufferView") == 0)
3070 out_sparse->values_buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
3073 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
3076 out_sparse->values_byte_offset = cgltf_json_to_int(tokens + i, json_chunk);
3079 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3081 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_sparse->values_extras);
3083 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
3085 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_sparse->values_extensions_count, &out_sparse->values_extensions);
3089 i = cgltf_skip_json(tokens, i+1);
3098 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3100 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_sparse->extras);
3102 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
3104 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_sparse->extensions_count, &out_sparse->extensions);
3108 i = cgltf_skip_json(tokens, i+1);
3120 static int cgltf_parse_json_accessor(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_accessor* out_accessor)
3122 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3124 int size = tokens[i].size;
3127 for (int j = 0; j < size; ++j)
3129 CGLTF_CHECK_KEY(tokens[i]);
3131 if (cgltf_json_strcmp(tokens + i, json_chunk, "name") == 0)
3133 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_accessor->name);
3135 else if (cgltf_json_strcmp(tokens+i, json_chunk, "bufferView") == 0)
3138 out_accessor->buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
3141 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
3144 out_accessor->offset =
3145 cgltf_json_to_int(tokens+i, json_chunk);
3148 else if (cgltf_json_strcmp(tokens+i, json_chunk, "componentType") == 0)
3151 out_accessor->component_type = cgltf_json_to_component_type(tokens + i, json_chunk);
3154 else if (cgltf_json_strcmp(tokens+i, json_chunk, "normalized") == 0)
3157 out_accessor->normalized = cgltf_json_to_bool(tokens+i, json_chunk);
3160 else if (cgltf_json_strcmp(tokens+i, json_chunk, "count") == 0)
3163 out_accessor->count =
3164 cgltf_json_to_int(tokens+i, json_chunk);
3167 else if (cgltf_json_strcmp(tokens+i, json_chunk, "type") == 0)
3170 if (cgltf_json_strcmp(tokens+i, json_chunk, "SCALAR") == 0)
3172 out_accessor->type = cgltf_type_scalar;
3174 else if (cgltf_json_strcmp(tokens+i, json_chunk, "VEC2") == 0)
3176 out_accessor->type = cgltf_type_vec2;
3178 else if (cgltf_json_strcmp(tokens+i, json_chunk, "VEC3") == 0)
3180 out_accessor->type = cgltf_type_vec3;
3182 else if (cgltf_json_strcmp(tokens+i, json_chunk, "VEC4") == 0)
3184 out_accessor->type = cgltf_type_vec4;
3186 else if (cgltf_json_strcmp(tokens+i, json_chunk, "MAT2") == 0)
3188 out_accessor->type = cgltf_type_mat2;
3190 else if (cgltf_json_strcmp(tokens+i, json_chunk, "MAT3") == 0)
3192 out_accessor->type = cgltf_type_mat3;
3194 else if (cgltf_json_strcmp(tokens+i, json_chunk, "MAT4") == 0)
3196 out_accessor->type = cgltf_type_mat4;
3200 else if (cgltf_json_strcmp(tokens + i, json_chunk, "min") == 0)
3203 out_accessor->has_min = 1;
3204 // note: we can't parse the precise number of elements since type may not have been computed yet
3205 int min_size = tokens[i].size > 16 ? 16 : tokens[i].size;
3206 i = cgltf_parse_json_float_array(tokens, i, json_chunk, out_accessor->min, min_size);
3208 else if (cgltf_json_strcmp(tokens + i, json_chunk, "max") == 0)
3211 out_accessor->has_max = 1;
3212 // note: we can't parse the precise number of elements since type may not have been computed yet
3213 int max_size = tokens[i].size > 16 ? 16 : tokens[i].size;
3214 i = cgltf_parse_json_float_array(tokens, i, json_chunk, out_accessor->max, max_size);
3216 else if (cgltf_json_strcmp(tokens + i, json_chunk, "sparse") == 0)
3218 out_accessor->is_sparse = 1;
3219 i = cgltf_parse_json_accessor_sparse(options, tokens, i + 1, json_chunk, &out_accessor->sparse);
3221 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3223 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_accessor->extras);
3225 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
3227 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_accessor->extensions_count, &out_accessor->extensions);
3231 i = cgltf_skip_json(tokens, i+1);
3243 static int cgltf_parse_json_texture_transform(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_texture_transform* out_texture_transform)
3245 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3247 int size = tokens[i].size;
3250 for (int j = 0; j < size; ++j)
3252 CGLTF_CHECK_KEY(tokens[i]);
3254 if (cgltf_json_strcmp(tokens + i, json_chunk, "offset") == 0)
3256 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_texture_transform->offset, 2);
3258 else if (cgltf_json_strcmp(tokens + i, json_chunk, "rotation") == 0)
3261 out_texture_transform->rotation = cgltf_json_to_float(tokens + i, json_chunk);
3264 else if (cgltf_json_strcmp(tokens + i, json_chunk, "scale") == 0)
3266 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_texture_transform->scale, 2);
3268 else if (cgltf_json_strcmp(tokens + i, json_chunk, "texCoord") == 0)
3271 out_texture_transform->texcoord = cgltf_json_to_int(tokens + i, json_chunk);
3276 i = cgltf_skip_json(tokens, i + 1);
3288 static int cgltf_parse_json_texture_view(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_texture_view* out_texture_view)
3290 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3292 out_texture_view->scale = 1.0f;
3293 cgltf_fill_float_array(out_texture_view->transform.scale, 2, 1.0f);
3295 int size = tokens[i].size;
3298 for (int j = 0; j < size; ++j)
3300 CGLTF_CHECK_KEY(tokens[i]);
3302 if (cgltf_json_strcmp(tokens + i, json_chunk, "index") == 0)
3305 out_texture_view->texture = CGLTF_PTRINDEX(cgltf_texture, cgltf_json_to_int(tokens + i, json_chunk));
3308 else if (cgltf_json_strcmp(tokens + i, json_chunk, "texCoord") == 0)
3311 out_texture_view->texcoord = cgltf_json_to_int(tokens + i, json_chunk);
3314 else if (cgltf_json_strcmp(tokens + i, json_chunk, "scale") == 0)
3317 out_texture_view->scale = cgltf_json_to_float(tokens + i, json_chunk);
3320 else if (cgltf_json_strcmp(tokens + i, json_chunk, "strength") == 0)
3323 out_texture_view->scale = cgltf_json_to_float(tokens + i, json_chunk);
3326 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3328 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_texture_view->extras);
3330 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
3334 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3335 if(out_texture_view->extensions)
3337 return CGLTF_ERROR_JSON;
3340 int extensions_size = tokens[i].size;
3341 out_texture_view->extensions_count = 0;
3342 out_texture_view->extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
3344 if (!out_texture_view->extensions)
3346 return CGLTF_ERROR_NOMEM;
3351 for (int k = 0; k < extensions_size; ++k)
3353 CGLTF_CHECK_KEY(tokens[i]);
3355 if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_texture_transform") == 0)
3357 out_texture_view->has_transform = 1;
3358 i = cgltf_parse_json_texture_transform(tokens, i + 1, json_chunk, &out_texture_view->transform);
3362 i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_texture_view->extensions[out_texture_view->extensions_count++]));
3373 i = cgltf_skip_json(tokens, i + 1);
3385 static int cgltf_parse_json_pbr_metallic_roughness(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_pbr_metallic_roughness* out_pbr)
3387 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3389 int size = tokens[i].size;
3392 for (int j = 0; j < size; ++j)
3394 CGLTF_CHECK_KEY(tokens[i]);
3396 if (cgltf_json_strcmp(tokens+i, json_chunk, "metallicFactor") == 0)
3399 out_pbr->metallic_factor =
3400 cgltf_json_to_float(tokens + i, json_chunk);
3403 else if (cgltf_json_strcmp(tokens+i, json_chunk, "roughnessFactor") == 0)
3406 out_pbr->roughness_factor =
3407 cgltf_json_to_float(tokens+i, json_chunk);
3410 else if (cgltf_json_strcmp(tokens+i, json_chunk, "baseColorFactor") == 0)
3412 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_pbr->base_color_factor, 4);
3414 else if (cgltf_json_strcmp(tokens+i, json_chunk, "baseColorTexture") == 0)
3416 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk,
3417 &out_pbr->base_color_texture);
3419 else if (cgltf_json_strcmp(tokens + i, json_chunk, "metallicRoughnessTexture") == 0)
3421 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk,
3422 &out_pbr->metallic_roughness_texture);
3424 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3426 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_pbr->extras);
3430 i = cgltf_skip_json(tokens, i+1);
3442 static int cgltf_parse_json_pbr_specular_glossiness(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_pbr_specular_glossiness* out_pbr)
3444 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3445 int size = tokens[i].size;
3448 for (int j = 0; j < size; ++j)
3450 CGLTF_CHECK_KEY(tokens[i]);
3452 if (cgltf_json_strcmp(tokens+i, json_chunk, "diffuseFactor") == 0)
3454 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_pbr->diffuse_factor, 4);
3456 else if (cgltf_json_strcmp(tokens+i, json_chunk, "specularFactor") == 0)
3458 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_pbr->specular_factor, 3);
3460 else if (cgltf_json_strcmp(tokens+i, json_chunk, "glossinessFactor") == 0)
3463 out_pbr->glossiness_factor = cgltf_json_to_float(tokens + i, json_chunk);
3466 else if (cgltf_json_strcmp(tokens+i, json_chunk, "diffuseTexture") == 0)
3468 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_pbr->diffuse_texture);
3470 else if (cgltf_json_strcmp(tokens+i, json_chunk, "specularGlossinessTexture") == 0)
3472 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_pbr->specular_glossiness_texture);
3476 i = cgltf_skip_json(tokens, i+1);
3488 static int cgltf_parse_json_clearcoat(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_clearcoat* out_clearcoat)
3490 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3491 int size = tokens[i].size;
3494 for (int j = 0; j < size; ++j)
3496 CGLTF_CHECK_KEY(tokens[i]);
3498 if (cgltf_json_strcmp(tokens+i, json_chunk, "clearcoatFactor") == 0)
3501 out_clearcoat->clearcoat_factor = cgltf_json_to_float(tokens + i, json_chunk);
3504 else if (cgltf_json_strcmp(tokens+i, json_chunk, "clearcoatRoughnessFactor") == 0)
3507 out_clearcoat->clearcoat_roughness_factor = cgltf_json_to_float(tokens + i, json_chunk);
3510 else if (cgltf_json_strcmp(tokens+i, json_chunk, "clearcoatTexture") == 0)
3512 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_clearcoat->clearcoat_texture);
3514 else if (cgltf_json_strcmp(tokens+i, json_chunk, "clearcoatRoughnessTexture") == 0)
3516 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_clearcoat->clearcoat_roughness_texture);
3518 else if (cgltf_json_strcmp(tokens+i, json_chunk, "clearcoatNormalTexture") == 0)
3520 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_clearcoat->clearcoat_normal_texture);
3524 i = cgltf_skip_json(tokens, i+1);
3536 static int cgltf_parse_json_ior(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_ior* out_ior)
3538 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3539 int size = tokens[i].size;
3543 out_ior->ior = 1.5f;
3545 for (int j = 0; j < size; ++j)
3547 CGLTF_CHECK_KEY(tokens[i]);
3549 if (cgltf_json_strcmp(tokens+i, json_chunk, "ior") == 0)
3552 out_ior->ior = cgltf_json_to_float(tokens + i, json_chunk);
3557 i = cgltf_skip_json(tokens, i+1);
3569 static int cgltf_parse_json_specular(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_specular* out_specular)
3571 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3572 int size = tokens[i].size;
3576 out_specular->specular_factor = 1.0f;
3577 cgltf_fill_float_array(out_specular->specular_color_factor, 3, 1.0f);
3579 for (int j = 0; j < size; ++j)
3581 CGLTF_CHECK_KEY(tokens[i]);
3583 if (cgltf_json_strcmp(tokens+i, json_chunk, "specularFactor") == 0)
3586 out_specular->specular_factor = cgltf_json_to_float(tokens + i, json_chunk);
3589 else if (cgltf_json_strcmp(tokens+i, json_chunk, "specularColorFactor") == 0)
3591 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_specular->specular_color_factor, 3);
3593 else if (cgltf_json_strcmp(tokens+i, json_chunk, "specularTexture") == 0)
3595 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_specular->specular_texture);
3597 else if (cgltf_json_strcmp(tokens + i, json_chunk, "specularColorTexture") == 0)
3599 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_specular->specular_color_texture);
3603 i = cgltf_skip_json(tokens, i+1);
3615 static int cgltf_parse_json_transmission(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_transmission* out_transmission)
3617 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3618 int size = tokens[i].size;
3621 for (int j = 0; j < size; ++j)
3623 CGLTF_CHECK_KEY(tokens[i]);
3625 if (cgltf_json_strcmp(tokens+i, json_chunk, "transmissionFactor") == 0)
3628 out_transmission->transmission_factor = cgltf_json_to_float(tokens + i, json_chunk);
3631 else if (cgltf_json_strcmp(tokens+i, json_chunk, "transmissionTexture") == 0)
3633 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_transmission->transmission_texture);
3637 i = cgltf_skip_json(tokens, i+1);
3649 static int cgltf_parse_json_volume(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_volume* out_volume)
3651 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3652 int size = tokens[i].size;
3655 for (int j = 0; j < size; ++j)
3657 CGLTF_CHECK_KEY(tokens[i]);
3659 if (cgltf_json_strcmp(tokens + i, json_chunk, "thicknessFactor") == 0)
3662 out_volume->thickness_factor = cgltf_json_to_float(tokens + i, json_chunk);
3665 else if (cgltf_json_strcmp(tokens + i, json_chunk, "thicknessTexture") == 0)
3667 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_volume->thickness_texture);
3669 else if (cgltf_json_strcmp(tokens + i, json_chunk, "attenuationColor") == 0)
3671 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_volume->attenuation_color, 3);
3673 else if (cgltf_json_strcmp(tokens + i, json_chunk, "attenuationDistance") == 0)
3676 out_volume->attenuation_distance = cgltf_json_to_float(tokens + i, json_chunk);
3681 i = cgltf_skip_json(tokens, i + 1);
3693 static int cgltf_parse_json_sheen(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_sheen* out_sheen)
3695 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3696 int size = tokens[i].size;
3699 for (int j = 0; j < size; ++j)
3701 CGLTF_CHECK_KEY(tokens[i]);
3703 if (cgltf_json_strcmp(tokens+i, json_chunk, "sheenColorFactor") == 0)
3705 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_sheen->sheen_color_factor, 3);
3707 else if (cgltf_json_strcmp(tokens+i, json_chunk, "sheenColorTexture") == 0)
3709 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_sheen->sheen_color_texture);
3711 else if (cgltf_json_strcmp(tokens+i, json_chunk, "sheenRoughnessFactor") == 0)
3714 out_sheen->sheen_roughness_factor = cgltf_json_to_float(tokens + i, json_chunk);
3717 else if (cgltf_json_strcmp(tokens+i, json_chunk, "sheenRoughnessTexture") == 0)
3719 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_sheen->sheen_roughness_texture);
3723 i = cgltf_skip_json(tokens, i+1);
3735 static int cgltf_parse_json_image(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_image* out_image)
3737 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3739 int size = tokens[i].size;
3742 for (int j = 0; j < size; ++j)
3744 CGLTF_CHECK_KEY(tokens[i]);
3746 if (cgltf_json_strcmp(tokens + i, json_chunk, "uri") == 0)
3748 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_image->uri);
3750 else if (cgltf_json_strcmp(tokens+i, json_chunk, "bufferView") == 0)
3753 out_image->buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
3756 else if (cgltf_json_strcmp(tokens + i, json_chunk, "mimeType") == 0)
3758 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_image->mime_type);
3760 else if (cgltf_json_strcmp(tokens + i, json_chunk, "name") == 0)
3762 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_image->name);
3764 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3766 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_image->extras);
3768 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
3770 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_image->extensions_count, &out_image->extensions);
3774 i = cgltf_skip_json(tokens, i + 1);
3786 static int cgltf_parse_json_sampler(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_sampler* out_sampler)
3789 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3791 out_sampler->wrap_s = 10497;
3792 out_sampler->wrap_t = 10497;
3794 int size = tokens[i].size;
3797 for (int j = 0; j < size; ++j)
3799 CGLTF_CHECK_KEY(tokens[i]);
3801 if (cgltf_json_strcmp(tokens + i, json_chunk, "name") == 0)
3803 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_sampler->name);
3805 else if (cgltf_json_strcmp(tokens + i, json_chunk, "magFilter") == 0)
3808 out_sampler->mag_filter
3809 = cgltf_json_to_int(tokens + i, json_chunk);
3812 else if (cgltf_json_strcmp(tokens + i, json_chunk, "minFilter") == 0)
3815 out_sampler->min_filter
3816 = cgltf_json_to_int(tokens + i, json_chunk);
3819 else if (cgltf_json_strcmp(tokens + i, json_chunk, "wrapS") == 0)
3823 = cgltf_json_to_int(tokens + i, json_chunk);
3826 else if (cgltf_json_strcmp(tokens + i, json_chunk, "wrapT") == 0)
3830 = cgltf_json_to_int(tokens + i, json_chunk);
3833 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3835 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_sampler->extras);
3837 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
3839 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_sampler->extensions_count, &out_sampler->extensions);
3843 i = cgltf_skip_json(tokens, i + 1);
3855 static int cgltf_parse_json_texture(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_texture* out_texture)
3857 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3859 int size = tokens[i].size;
3862 for (int j = 0; j < size; ++j)
3864 CGLTF_CHECK_KEY(tokens[i]);
3866 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
3868 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_texture->name);
3870 else if (cgltf_json_strcmp(tokens + i, json_chunk, "sampler") == 0)
3873 out_texture->sampler = CGLTF_PTRINDEX(cgltf_sampler, cgltf_json_to_int(tokens + i, json_chunk));
3876 else if (cgltf_json_strcmp(tokens + i, json_chunk, "source") == 0)
3879 out_texture->image = CGLTF_PTRINDEX(cgltf_image, cgltf_json_to_int(tokens + i, json_chunk));
3882 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3884 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_texture->extras);
3886 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
3888 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_texture->extensions_count, &out_texture->extensions);
3892 i = cgltf_skip_json(tokens, i + 1);
3904 static int cgltf_parse_json_material(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_material* out_material)
3906 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3908 cgltf_fill_float_array(out_material->pbr_metallic_roughness.base_color_factor, 4, 1.0f);
3909 out_material->pbr_metallic_roughness.metallic_factor = 1.0f;
3910 out_material->pbr_metallic_roughness.roughness_factor = 1.0f;
3912 cgltf_fill_float_array(out_material->pbr_specular_glossiness.diffuse_factor, 4, 1.0f);
3913 cgltf_fill_float_array(out_material->pbr_specular_glossiness.specular_factor, 3, 1.0f);
3914 out_material->pbr_specular_glossiness.glossiness_factor = 1.0f;
3916 cgltf_fill_float_array(out_material->volume.attenuation_color, 3, 1.0f);
3917 out_material->volume.attenuation_distance = FLT_MAX;
3919 out_material->alpha_cutoff = 0.5f;
3921 int size = tokens[i].size;
3924 for (int j = 0; j < size; ++j)
3926 CGLTF_CHECK_KEY(tokens[i]);
3928 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
3930 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_material->name);
3932 else if (cgltf_json_strcmp(tokens+i, json_chunk, "pbrMetallicRoughness") == 0)
3934 out_material->has_pbr_metallic_roughness = 1;
3935 i = cgltf_parse_json_pbr_metallic_roughness(options, tokens, i + 1, json_chunk, &out_material->pbr_metallic_roughness);
3937 else if (cgltf_json_strcmp(tokens+i, json_chunk, "emissiveFactor") == 0)
3939 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_material->emissive_factor, 3);
3941 else if (cgltf_json_strcmp(tokens + i, json_chunk, "normalTexture") == 0)
3943 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk,
3944 &out_material->normal_texture);
3946 else if (cgltf_json_strcmp(tokens + i, json_chunk, "occlusionTexture") == 0)
3948 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk,
3949 &out_material->occlusion_texture);
3951 else if (cgltf_json_strcmp(tokens + i, json_chunk, "emissiveTexture") == 0)
3953 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk,
3954 &out_material->emissive_texture);
3956 else if (cgltf_json_strcmp(tokens + i, json_chunk, "alphaMode") == 0)
3959 if (cgltf_json_strcmp(tokens + i, json_chunk, "OPAQUE") == 0)
3961 out_material->alpha_mode = cgltf_alpha_mode_opaque;
3963 else if (cgltf_json_strcmp(tokens + i, json_chunk, "MASK") == 0)
3965 out_material->alpha_mode = cgltf_alpha_mode_mask;
3967 else if (cgltf_json_strcmp(tokens + i, json_chunk, "BLEND") == 0)
3969 out_material->alpha_mode = cgltf_alpha_mode_blend;
3973 else if (cgltf_json_strcmp(tokens + i, json_chunk, "alphaCutoff") == 0)
3976 out_material->alpha_cutoff = cgltf_json_to_float(tokens + i, json_chunk);
3979 else if (cgltf_json_strcmp(tokens + i, json_chunk, "doubleSided") == 0)
3982 out_material->double_sided =
3983 cgltf_json_to_bool(tokens + i, json_chunk);
3986 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3988 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_material->extras);
3990 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
3994 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3995 if(out_material->extensions)
3997 return CGLTF_ERROR_JSON;
4000 int extensions_size = tokens[i].size;
4002 out_material->extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
4003 out_material->extensions_count= 0;
4005 if (!out_material->extensions)
4007 return CGLTF_ERROR_NOMEM;
4010 for (int k = 0; k < extensions_size; ++k)
4012 CGLTF_CHECK_KEY(tokens[i]);
4014 if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_pbrSpecularGlossiness") == 0)
4016 out_material->has_pbr_specular_glossiness = 1;
4017 i = cgltf_parse_json_pbr_specular_glossiness(options, tokens, i + 1, json_chunk, &out_material->pbr_specular_glossiness);
4019 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_unlit") == 0)
4021 out_material->unlit = 1;
4022 i = cgltf_skip_json(tokens, i+1);
4024 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_clearcoat") == 0)
4026 out_material->has_clearcoat = 1;
4027 i = cgltf_parse_json_clearcoat(options, tokens, i + 1, json_chunk, &out_material->clearcoat);
4029 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_ior") == 0)
4031 out_material->has_ior = 1;
4032 i = cgltf_parse_json_ior(tokens, i + 1, json_chunk, &out_material->ior);
4034 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_specular") == 0)
4036 out_material->has_specular = 1;
4037 i = cgltf_parse_json_specular(options, tokens, i + 1, json_chunk, &out_material->specular);
4039 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_transmission") == 0)
4041 out_material->has_transmission = 1;
4042 i = cgltf_parse_json_transmission(options, tokens, i + 1, json_chunk, &out_material->transmission);
4044 else if (cgltf_json_strcmp(tokens + i, json_chunk, "KHR_materials_volume") == 0)
4046 out_material->has_volume = 1;
4047 i = cgltf_parse_json_volume(options, tokens, i + 1, json_chunk, &out_material->volume);
4049 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_sheen") == 0)
4051 out_material->has_sheen = 1;
4052 i = cgltf_parse_json_sheen(options, tokens, i + 1, json_chunk, &out_material->sheen);
4056 i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_material->extensions[out_material->extensions_count++]));
4067 i = cgltf_skip_json(tokens, i+1);
4079 static int cgltf_parse_json_accessors(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
4081 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_accessor), (void**)&out_data->accessors, &out_data->accessors_count);
4087 for (cgltf_size j = 0; j < out_data->accessors_count; ++j)
4089 i = cgltf_parse_json_accessor(options, tokens, i, json_chunk, &out_data->accessors[j]);
4098 static int cgltf_parse_json_materials(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
4100 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_material), (void**)&out_data->materials, &out_data->materials_count);
4106 for (cgltf_size j = 0; j < out_data->materials_count; ++j)
4108 i = cgltf_parse_json_material(options, tokens, i, json_chunk, &out_data->materials[j]);
4117 static int cgltf_parse_json_images(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
4119 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_image), (void**)&out_data->images, &out_data->images_count);
4125 for (cgltf_size j = 0; j < out_data->images_count; ++j)
4127 i = cgltf_parse_json_image(options, tokens, i, json_chunk, &out_data->images[j]);
4136 static int cgltf_parse_json_textures(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
4138 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_texture), (void**)&out_data->textures, &out_data->textures_count);
4144 for (cgltf_size j = 0; j < out_data->textures_count; ++j)
4146 i = cgltf_parse_json_texture(options, tokens, i, json_chunk, &out_data->textures[j]);
4155 static int cgltf_parse_json_samplers(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
4157 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_sampler), (void**)&out_data->samplers, &out_data->samplers_count);
4163 for (cgltf_size j = 0; j < out_data->samplers_count; ++j)
4165 i = cgltf_parse_json_sampler(options, tokens, i, json_chunk, &out_data->samplers[j]);
4174 static int cgltf_parse_json_meshopt_compression(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_meshopt_compression* out_meshopt_compression)
4177 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4179 int size = tokens[i].size;
4182 for (int j = 0; j < size; ++j)
4184 CGLTF_CHECK_KEY(tokens[i]);
4186 if (cgltf_json_strcmp(tokens+i, json_chunk, "buffer") == 0)
4189 out_meshopt_compression->buffer = CGLTF_PTRINDEX(cgltf_buffer, cgltf_json_to_int(tokens + i, json_chunk));
4192 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
4195 out_meshopt_compression->offset = cgltf_json_to_int(tokens+i, json_chunk);
4198 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteLength") == 0)
4201 out_meshopt_compression->size = cgltf_json_to_int(tokens+i, json_chunk);
4204 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteStride") == 0)
4207 out_meshopt_compression->stride = cgltf_json_to_int(tokens+i, json_chunk);
4210 else if (cgltf_json_strcmp(tokens+i, json_chunk, "count") == 0)
4213 out_meshopt_compression->count = cgltf_json_to_int(tokens+i, json_chunk);
4216 else if (cgltf_json_strcmp(tokens+i, json_chunk, "mode") == 0)
4219 if (cgltf_json_strcmp(tokens+i, json_chunk, "ATTRIBUTES") == 0)
4221 out_meshopt_compression->mode = cgltf_meshopt_compression_mode_attributes;
4223 else if (cgltf_json_strcmp(tokens+i, json_chunk, "TRIANGLES") == 0)
4225 out_meshopt_compression->mode = cgltf_meshopt_compression_mode_triangles;
4227 else if (cgltf_json_strcmp(tokens+i, json_chunk, "INDICES") == 0)
4229 out_meshopt_compression->mode = cgltf_meshopt_compression_mode_indices;
4233 else if (cgltf_json_strcmp(tokens+i, json_chunk, "filter") == 0)
4236 if (cgltf_json_strcmp(tokens+i, json_chunk, "NONE") == 0)
4238 out_meshopt_compression->filter = cgltf_meshopt_compression_filter_none;
4240 else if (cgltf_json_strcmp(tokens+i, json_chunk, "OCTAHEDRAL") == 0)
4242 out_meshopt_compression->filter = cgltf_meshopt_compression_filter_octahedral;
4244 else if (cgltf_json_strcmp(tokens+i, json_chunk, "QUATERNION") == 0)
4246 out_meshopt_compression->filter = cgltf_meshopt_compression_filter_quaternion;
4248 else if (cgltf_json_strcmp(tokens+i, json_chunk, "EXPONENTIAL") == 0)
4250 out_meshopt_compression->filter = cgltf_meshopt_compression_filter_exponential;
4256 i = cgltf_skip_json(tokens, i+1);
4268 static int cgltf_parse_json_buffer_view(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_buffer_view* out_buffer_view)
4270 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4272 int size = tokens[i].size;
4275 for (int j = 0; j < size; ++j)
4277 CGLTF_CHECK_KEY(tokens[i]);
4279 if (cgltf_json_strcmp(tokens + i, json_chunk, "name") == 0)
4281 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_buffer_view->name);
4283 else if (cgltf_json_strcmp(tokens+i, json_chunk, "buffer") == 0)
4286 out_buffer_view->buffer = CGLTF_PTRINDEX(cgltf_buffer, cgltf_json_to_int(tokens + i, json_chunk));
4289 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
4292 out_buffer_view->offset =
4293 cgltf_json_to_int(tokens+i, json_chunk);
4296 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteLength") == 0)
4299 out_buffer_view->size =
4300 cgltf_json_to_int(tokens+i, json_chunk);
4303 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteStride") == 0)
4306 out_buffer_view->stride =
4307 cgltf_json_to_int(tokens+i, json_chunk);
4310 else if (cgltf_json_strcmp(tokens+i, json_chunk, "target") == 0)
4313 int type = cgltf_json_to_int(tokens+i, json_chunk);
4317 type = cgltf_buffer_view_type_vertices;
4320 type = cgltf_buffer_view_type_indices;
4323 type = cgltf_buffer_view_type_invalid;
4326 out_buffer_view->type = (cgltf_buffer_view_type)type;
4329 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
4331 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_buffer_view->extras);
4333 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
4337 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4338 if(out_buffer_view->extensions)
4340 return CGLTF_ERROR_JSON;
4343 int extensions_size = tokens[i].size;
4344 out_buffer_view->extensions_count = 0;
4345 out_buffer_view->extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
4347 if (!out_buffer_view->extensions)
4349 return CGLTF_ERROR_NOMEM;
4353 for (int k = 0; k < extensions_size; ++k)
4355 CGLTF_CHECK_KEY(tokens[i]);
4357 if (cgltf_json_strcmp(tokens+i, json_chunk, "EXT_meshopt_compression") == 0)
4359 out_buffer_view->has_meshopt_compression = 1;
4360 i = cgltf_parse_json_meshopt_compression(options, tokens, i + 1, json_chunk, &out_buffer_view->meshopt_compression);
4364 i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_buffer_view->extensions[out_buffer_view->extensions_count++]));
4375 i = cgltf_skip_json(tokens, i+1);
4387 static int cgltf_parse_json_buffer_views(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
4389 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_buffer_view), (void**)&out_data->buffer_views, &out_data->buffer_views_count);
4395 for (cgltf_size j = 0; j < out_data->buffer_views_count; ++j)
4397 i = cgltf_parse_json_buffer_view(options, tokens, i, json_chunk, &out_data->buffer_views[j]);
4406 static int cgltf_parse_json_buffer(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_buffer* out_buffer)
4408 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4410 int size = tokens[i].size;
4413 for (int j = 0; j < size; ++j)
4415 CGLTF_CHECK_KEY(tokens[i]);
4417 if (cgltf_json_strcmp(tokens + i, json_chunk, "name") == 0)
4419 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_buffer->name);
4421 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteLength") == 0)
4425 cgltf_json_to_int(tokens+i, json_chunk);
4428 else if (cgltf_json_strcmp(tokens+i, json_chunk, "uri") == 0)
4430 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_buffer->uri);
4432 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
4434 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_buffer->extras);
4436 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
4438 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_buffer->extensions_count, &out_buffer->extensions);
4442 i = cgltf_skip_json(tokens, i+1);
4454 static int cgltf_parse_json_buffers(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
4456 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_buffer), (void**)&out_data->buffers, &out_data->buffers_count);
4462 for (cgltf_size j = 0; j < out_data->buffers_count; ++j)
4464 i = cgltf_parse_json_buffer(options, tokens, i, json_chunk, &out_data->buffers[j]);
4473 static int cgltf_parse_json_skin(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_skin* out_skin)
4475 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4477 int size = tokens[i].size;
4480 for (int j = 0; j < size; ++j)
4482 CGLTF_CHECK_KEY(tokens[i]);
4484 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
4486 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_skin->name);
4488 else if (cgltf_json_strcmp(tokens+i, json_chunk, "joints") == 0)
4490 i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_node*), (void**)&out_skin->joints, &out_skin->joints_count);
4496 for (cgltf_size k = 0; k < out_skin->joints_count; ++k)
4498 out_skin->joints[k] = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
4502 else if (cgltf_json_strcmp(tokens+i, json_chunk, "skeleton") == 0)
4505 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
4506 out_skin->skeleton = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
4509 else if (cgltf_json_strcmp(tokens+i, json_chunk, "inverseBindMatrices") == 0)
4512 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
4513 out_skin->inverse_bind_matrices = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
4516 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
4518 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_skin->extras);
4520 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
4522 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_skin->extensions_count, &out_skin->extensions);
4526 i = cgltf_skip_json(tokens, i+1);
4538 static int cgltf_parse_json_skins(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
4540 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_skin), (void**)&out_data->skins, &out_data->skins_count);
4546 for (cgltf_size j = 0; j < out_data->skins_count; ++j)
4548 i = cgltf_parse_json_skin(options, tokens, i, json_chunk, &out_data->skins[j]);
4557 static int cgltf_parse_json_camera(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_camera* out_camera)
4559 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4561 int size = tokens[i].size;
4564 for (int j = 0; j < size; ++j)
4566 CGLTF_CHECK_KEY(tokens[i]);
4568 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
4570 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_camera->name);
4572 else if (cgltf_json_strcmp(tokens+i, json_chunk, "type") == 0)
4575 if (cgltf_json_strcmp(tokens + i, json_chunk, "perspective") == 0)
4577 out_camera->type = cgltf_camera_type_perspective;
4579 else if (cgltf_json_strcmp(tokens + i, json_chunk, "orthographic") == 0)
4581 out_camera->type = cgltf_camera_type_orthographic;
4585 else if (cgltf_json_strcmp(tokens+i, json_chunk, "perspective") == 0)
4589 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4591 int data_size = tokens[i].size;
4594 out_camera->type = cgltf_camera_type_perspective;
4596 for (int k = 0; k < data_size; ++k)
4598 CGLTF_CHECK_KEY(tokens[i]);
4600 if (cgltf_json_strcmp(tokens+i, json_chunk, "aspectRatio") == 0)
4603 out_camera->data.perspective.has_aspect_ratio = 1;
4604 out_camera->data.perspective.aspect_ratio = cgltf_json_to_float(tokens + i, json_chunk);
4607 else if (cgltf_json_strcmp(tokens+i, json_chunk, "yfov") == 0)
4610 out_camera->data.perspective.yfov = cgltf_json_to_float(tokens + i, json_chunk);
4613 else if (cgltf_json_strcmp(tokens+i, json_chunk, "zfar") == 0)
4616 out_camera->data.perspective.has_zfar = 1;
4617 out_camera->data.perspective.zfar = cgltf_json_to_float(tokens + i, json_chunk);
4620 else if (cgltf_json_strcmp(tokens+i, json_chunk, "znear") == 0)
4623 out_camera->data.perspective.znear = cgltf_json_to_float(tokens + i, json_chunk);
4626 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
4628 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_camera->data.perspective.extras);
4632 i = cgltf_skip_json(tokens, i+1);
4641 else if (cgltf_json_strcmp(tokens+i, json_chunk, "orthographic") == 0)
4645 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4647 int data_size = tokens[i].size;
4650 out_camera->type = cgltf_camera_type_orthographic;
4652 for (int k = 0; k < data_size; ++k)
4654 CGLTF_CHECK_KEY(tokens[i]);
4656 if (cgltf_json_strcmp(tokens+i, json_chunk, "xmag") == 0)
4659 out_camera->data.orthographic.xmag = cgltf_json_to_float(tokens + i, json_chunk);
4662 else if (cgltf_json_strcmp(tokens+i, json_chunk, "ymag") == 0)
4665 out_camera->data.orthographic.ymag = cgltf_json_to_float(tokens + i, json_chunk);
4668 else if (cgltf_json_strcmp(tokens+i, json_chunk, "zfar") == 0)
4671 out_camera->data.orthographic.zfar = cgltf_json_to_float(tokens + i, json_chunk);
4674 else if (cgltf_json_strcmp(tokens+i, json_chunk, "znear") == 0)
4677 out_camera->data.orthographic.znear = cgltf_json_to_float(tokens + i, json_chunk);
4680 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
4682 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_camera->data.orthographic.extras);
4686 i = cgltf_skip_json(tokens, i+1);
4695 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
4697 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_camera->extras);
4699 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
4701 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_camera->extensions_count, &out_camera->extensions);
4705 i = cgltf_skip_json(tokens, i+1);
4717 static int cgltf_parse_json_cameras(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
4719 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_camera), (void**)&out_data->cameras, &out_data->cameras_count);
4725 for (cgltf_size j = 0; j < out_data->cameras_count; ++j)
4727 i = cgltf_parse_json_camera(options, tokens, i, json_chunk, &out_data->cameras[j]);
4736 static int cgltf_parse_json_light(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_light* out_light)
4738 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4740 int size = tokens[i].size;
4743 for (int j = 0; j < size; ++j)
4745 CGLTF_CHECK_KEY(tokens[i]);
4747 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
4749 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_light->name);
4751 else if (cgltf_json_strcmp(tokens + i, json_chunk, "color") == 0)
4753 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_light->color, 3);
4755 else if (cgltf_json_strcmp(tokens + i, json_chunk, "intensity") == 0)
4758 out_light->intensity = cgltf_json_to_float(tokens + i, json_chunk);
4761 else if (cgltf_json_strcmp(tokens+i, json_chunk, "type") == 0)
4764 if (cgltf_json_strcmp(tokens + i, json_chunk, "directional") == 0)
4766 out_light->type = cgltf_light_type_directional;
4768 else if (cgltf_json_strcmp(tokens + i, json_chunk, "point") == 0)
4770 out_light->type = cgltf_light_type_point;
4772 else if (cgltf_json_strcmp(tokens + i, json_chunk, "spot") == 0)
4774 out_light->type = cgltf_light_type_spot;
4778 else if (cgltf_json_strcmp(tokens + i, json_chunk, "range") == 0)
4781 out_light->range = cgltf_json_to_float(tokens + i, json_chunk);
4784 else if (cgltf_json_strcmp(tokens+i, json_chunk, "spot") == 0)
4788 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4790 int data_size = tokens[i].size;
4793 for (int k = 0; k < data_size; ++k)
4795 CGLTF_CHECK_KEY(tokens[i]);
4797 if (cgltf_json_strcmp(tokens+i, json_chunk, "innerConeAngle") == 0)
4800 out_light->spot_inner_cone_angle = cgltf_json_to_float(tokens + i, json_chunk);
4803 else if (cgltf_json_strcmp(tokens+i, json_chunk, "outerConeAngle") == 0)
4806 out_light->spot_outer_cone_angle = cgltf_json_to_float(tokens + i, json_chunk);
4811 i = cgltf_skip_json(tokens, i+1);
4822 i = cgltf_skip_json(tokens, i+1);
4834 static int cgltf_parse_json_lights(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
4836 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_light), (void**)&out_data->lights, &out_data->lights_count);
4842 for (cgltf_size j = 0; j < out_data->lights_count; ++j)
4844 i = cgltf_parse_json_light(options, tokens, i, json_chunk, &out_data->lights[j]);
4853 static int cgltf_parse_json_node(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_node* out_node)
4855 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4857 out_node->rotation[3] = 1.0f;
4858 out_node->scale[0] = 1.0f;
4859 out_node->scale[1] = 1.0f;
4860 out_node->scale[2] = 1.0f;
4861 out_node->matrix[0] = 1.0f;
4862 out_node->matrix[5] = 1.0f;
4863 out_node->matrix[10] = 1.0f;
4864 out_node->matrix[15] = 1.0f;
4866 int size = tokens[i].size;
4869 for (int j = 0; j < size; ++j)
4871 CGLTF_CHECK_KEY(tokens[i]);
4873 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
4875 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_node->name);
4877 else if (cgltf_json_strcmp(tokens+i, json_chunk, "children") == 0)
4879 i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_node*), (void**)&out_node->children, &out_node->children_count);
4885 for (cgltf_size k = 0; k < out_node->children_count; ++k)
4887 out_node->children[k] = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
4891 else if (cgltf_json_strcmp(tokens+i, json_chunk, "mesh") == 0)
4894 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
4895 out_node->mesh = CGLTF_PTRINDEX(cgltf_mesh, cgltf_json_to_int(tokens + i, json_chunk));
4898 else if (cgltf_json_strcmp(tokens+i, json_chunk, "skin") == 0)
4901 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
4902 out_node->skin = CGLTF_PTRINDEX(cgltf_skin, cgltf_json_to_int(tokens + i, json_chunk));
4905 else if (cgltf_json_strcmp(tokens+i, json_chunk, "camera") == 0)
4908 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
4909 out_node->camera = CGLTF_PTRINDEX(cgltf_camera, cgltf_json_to_int(tokens + i, json_chunk));
4912 else if (cgltf_json_strcmp(tokens+i, json_chunk, "translation") == 0)
4914 out_node->has_translation = 1;
4915 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_node->translation, 3);
4917 else if (cgltf_json_strcmp(tokens+i, json_chunk, "rotation") == 0)
4919 out_node->has_rotation = 1;
4920 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_node->rotation, 4);
4922 else if (cgltf_json_strcmp(tokens+i, json_chunk, "scale") == 0)
4924 out_node->has_scale = 1;
4925 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_node->scale, 3);
4927 else if (cgltf_json_strcmp(tokens+i, json_chunk, "matrix") == 0)
4929 out_node->has_matrix = 1;
4930 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_node->matrix, 16);
4932 else if (cgltf_json_strcmp(tokens + i, json_chunk, "weights") == 0)
4934 i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_float), (void**)&out_node->weights, &out_node->weights_count);
4940 i = cgltf_parse_json_float_array(tokens, i - 1, json_chunk, out_node->weights, (int)out_node->weights_count);
4942 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
4944 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_node->extras);
4946 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
4950 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4951 if(out_node->extensions)
4953 return CGLTF_ERROR_JSON;
4956 int extensions_size = tokens[i].size;
4957 out_node->extensions_count= 0;
4958 out_node->extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
4960 if (!out_node->extensions)
4962 return CGLTF_ERROR_NOMEM;
4967 for (int k = 0; k < extensions_size; ++k)
4969 CGLTF_CHECK_KEY(tokens[i]);
4971 if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_lights_punctual") == 0)
4975 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4977 int data_size = tokens[i].size;
4980 for (int m = 0; m < data_size; ++m)
4982 CGLTF_CHECK_KEY(tokens[i]);
4984 if (cgltf_json_strcmp(tokens + i, json_chunk, "light") == 0)
4987 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
4988 out_node->light = CGLTF_PTRINDEX(cgltf_light, cgltf_json_to_int(tokens + i, json_chunk));
4993 i = cgltf_skip_json(tokens, i + 1);
5004 i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_node->extensions[out_node->extensions_count++]));
5015 i = cgltf_skip_json(tokens, i+1);
5027 static int cgltf_parse_json_nodes(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
5029 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_node), (void**)&out_data->nodes, &out_data->nodes_count);
5035 for (cgltf_size j = 0; j < out_data->nodes_count; ++j)
5037 i = cgltf_parse_json_node(options, tokens, i, json_chunk, &out_data->nodes[j]);
5046 static int cgltf_parse_json_scene(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_scene* out_scene)
5048 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5050 int size = tokens[i].size;
5053 for (int j = 0; j < size; ++j)
5055 CGLTF_CHECK_KEY(tokens[i]);
5057 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
5059 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_scene->name);
5061 else if (cgltf_json_strcmp(tokens+i, json_chunk, "nodes") == 0)
5063 i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_node*), (void**)&out_scene->nodes, &out_scene->nodes_count);
5069 for (cgltf_size k = 0; k < out_scene->nodes_count; ++k)
5071 out_scene->nodes[k] = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
5075 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
5077 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_scene->extras);
5079 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
5081 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_scene->extensions_count, &out_scene->extensions);
5085 i = cgltf_skip_json(tokens, i+1);
5097 static int cgltf_parse_json_scenes(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
5099 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_scene), (void**)&out_data->scenes, &out_data->scenes_count);
5105 for (cgltf_size j = 0; j < out_data->scenes_count; ++j)
5107 i = cgltf_parse_json_scene(options, tokens, i, json_chunk, &out_data->scenes[j]);
5116 static int cgltf_parse_json_animation_sampler(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_animation_sampler* out_sampler)
5119 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5121 int size = tokens[i].size;
5124 for (int j = 0; j < size; ++j)
5126 CGLTF_CHECK_KEY(tokens[i]);
5128 if (cgltf_json_strcmp(tokens+i, json_chunk, "input") == 0)
5131 out_sampler->input = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
5134 else if (cgltf_json_strcmp(tokens+i, json_chunk, "output") == 0)
5137 out_sampler->output = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
5140 else if (cgltf_json_strcmp(tokens+i, json_chunk, "interpolation") == 0)
5143 if (cgltf_json_strcmp(tokens + i, json_chunk, "LINEAR") == 0)
5145 out_sampler->interpolation = cgltf_interpolation_type_linear;
5147 else if (cgltf_json_strcmp(tokens + i, json_chunk, "STEP") == 0)
5149 out_sampler->interpolation = cgltf_interpolation_type_step;
5151 else if (cgltf_json_strcmp(tokens + i, json_chunk, "CUBICSPLINE") == 0)
5153 out_sampler->interpolation = cgltf_interpolation_type_cubic_spline;
5157 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
5159 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_sampler->extras);
5161 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
5163 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_sampler->extensions_count, &out_sampler->extensions);
5167 i = cgltf_skip_json(tokens, i+1);
5179 static int cgltf_parse_json_animation_channel(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_animation_channel* out_channel)
5182 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5184 int size = tokens[i].size;
5187 for (int j = 0; j < size; ++j)
5189 CGLTF_CHECK_KEY(tokens[i]);
5191 if (cgltf_json_strcmp(tokens+i, json_chunk, "sampler") == 0)
5194 out_channel->sampler = CGLTF_PTRINDEX(cgltf_animation_sampler, cgltf_json_to_int(tokens + i, json_chunk));
5197 else if (cgltf_json_strcmp(tokens+i, json_chunk, "target") == 0)
5201 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5203 int target_size = tokens[i].size;
5206 for (int k = 0; k < target_size; ++k)
5208 CGLTF_CHECK_KEY(tokens[i]);
5210 if (cgltf_json_strcmp(tokens+i, json_chunk, "node") == 0)
5213 out_channel->target_node = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
5216 else if (cgltf_json_strcmp(tokens+i, json_chunk, "path") == 0)
5219 if (cgltf_json_strcmp(tokens+i, json_chunk, "translation") == 0)
5221 out_channel->target_path = cgltf_animation_path_type_translation;
5223 else if (cgltf_json_strcmp(tokens+i, json_chunk, "rotation") == 0)
5225 out_channel->target_path = cgltf_animation_path_type_rotation;
5227 else if (cgltf_json_strcmp(tokens+i, json_chunk, "scale") == 0)
5229 out_channel->target_path = cgltf_animation_path_type_scale;
5231 else if (cgltf_json_strcmp(tokens+i, json_chunk, "weights") == 0)
5233 out_channel->target_path = cgltf_animation_path_type_weights;
5237 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
5239 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_channel->extras);
5241 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
5243 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_channel->extensions_count, &out_channel->extensions);
5247 i = cgltf_skip_json(tokens, i+1);
5258 i = cgltf_skip_json(tokens, i+1);
5270 static int cgltf_parse_json_animation(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_animation* out_animation)
5272 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5274 int size = tokens[i].size;
5277 for (int j = 0; j < size; ++j)
5279 CGLTF_CHECK_KEY(tokens[i]);
5281 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
5283 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_animation->name);
5285 else if (cgltf_json_strcmp(tokens+i, json_chunk, "samplers") == 0)
5287 i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_animation_sampler), (void**)&out_animation->samplers, &out_animation->samplers_count);
5293 for (cgltf_size k = 0; k < out_animation->samplers_count; ++k)
5295 i = cgltf_parse_json_animation_sampler(options, tokens, i, json_chunk, &out_animation->samplers[k]);
5302 else if (cgltf_json_strcmp(tokens+i, json_chunk, "channels") == 0)
5304 i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_animation_channel), (void**)&out_animation->channels, &out_animation->channels_count);
5310 for (cgltf_size k = 0; k < out_animation->channels_count; ++k)
5312 i = cgltf_parse_json_animation_channel(options, tokens, i, json_chunk, &out_animation->channels[k]);
5319 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
5321 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_animation->extras);
5323 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
5325 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_animation->extensions_count, &out_animation->extensions);
5329 i = cgltf_skip_json(tokens, i+1);
5341 static int cgltf_parse_json_animations(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
5343 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_animation), (void**)&out_data->animations, &out_data->animations_count);
5349 for (cgltf_size j = 0; j < out_data->animations_count; ++j)
5351 i = cgltf_parse_json_animation(options, tokens, i, json_chunk, &out_data->animations[j]);
5360 static int cgltf_parse_json_variant(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_material_variant* out_variant)
5362 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5364 int size = tokens[i].size;
5367 for (int j = 0; j < size; ++j)
5369 CGLTF_CHECK_KEY(tokens[i]);
5371 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
5373 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_variant->name);
5375 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
5377 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_variant->extras);
5381 i = cgltf_skip_json(tokens, i+1);
5393 static int cgltf_parse_json_variants(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
5395 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_material_variant), (void**)&out_data->variants, &out_data->variants_count);
5401 for (cgltf_size j = 0; j < out_data->variants_count; ++j)
5403 i = cgltf_parse_json_variant(options, tokens, i, json_chunk, &out_data->variants[j]);
5412 static int cgltf_parse_json_asset(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_asset* out_asset)
5414 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5416 int size = tokens[i].size;
5419 for (int j = 0; j < size; ++j)
5421 CGLTF_CHECK_KEY(tokens[i]);
5423 if (cgltf_json_strcmp(tokens+i, json_chunk, "copyright") == 0)
5425 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_asset->copyright);
5427 else if (cgltf_json_strcmp(tokens+i, json_chunk, "generator") == 0)
5429 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_asset->generator);
5431 else if (cgltf_json_strcmp(tokens+i, json_chunk, "version") == 0)
5433 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_asset->version);
5435 else if (cgltf_json_strcmp(tokens+i, json_chunk, "minVersion") == 0)
5437 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_asset->min_version);
5439 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
5441 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_asset->extras);
5443 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
5445 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_asset->extensions_count, &out_asset->extensions);
5449 i = cgltf_skip_json(tokens, i+1);
5458 if (out_asset->version && CGLTF_ATOF(out_asset->version) < 2)
5460 return CGLTF_ERROR_LEGACY;
5466 cgltf_size cgltf_num_components(cgltf_type type) {
5469 case cgltf_type_vec2:
5471 case cgltf_type_vec3:
5473 case cgltf_type_vec4:
5475 case cgltf_type_mat2:
5477 case cgltf_type_mat3:
5479 case cgltf_type_mat4:
5481 case cgltf_type_invalid:
5482 case cgltf_type_scalar:
5488 static cgltf_size cgltf_component_size(cgltf_component_type component_type) {
5489 switch (component_type)
5491 case cgltf_component_type_r_8:
5492 case cgltf_component_type_r_8u:
5494 case cgltf_component_type_r_16:
5495 case cgltf_component_type_r_16u:
5497 case cgltf_component_type_r_32u:
5498 case cgltf_component_type_r_32f:
5500 case cgltf_component_type_invalid:
5506 static cgltf_size cgltf_calc_size(cgltf_type type, cgltf_component_type component_type)
5508 cgltf_size component_size = cgltf_component_size(component_type);
5509 if (type == cgltf_type_mat2 && component_size == 1)
5511 return 8 * component_size;
5513 else if (type == cgltf_type_mat3 && (component_size == 1 || component_size == 2))
5515 return 12 * component_size;
5517 return component_size * cgltf_num_components(type);
5520 static int cgltf_fixup_pointers(cgltf_data* out_data);
5522 static int cgltf_parse_json_root(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
5524 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5526 int size = tokens[i].size;
5529 for (int j = 0; j < size; ++j)
5531 CGLTF_CHECK_KEY(tokens[i]);
5533 if (cgltf_json_strcmp(tokens + i, json_chunk, "asset") == 0)
5535 i = cgltf_parse_json_asset(options, tokens, i + 1, json_chunk, &out_data->asset);
5537 else if (cgltf_json_strcmp(tokens + i, json_chunk, "meshes") == 0)
5539 i = cgltf_parse_json_meshes(options, tokens, i + 1, json_chunk, out_data);
5541 else if (cgltf_json_strcmp(tokens + i, json_chunk, "accessors") == 0)
5543 i = cgltf_parse_json_accessors(options, tokens, i + 1, json_chunk, out_data);
5545 else if (cgltf_json_strcmp(tokens + i, json_chunk, "bufferViews") == 0)
5547 i = cgltf_parse_json_buffer_views(options, tokens, i + 1, json_chunk, out_data);
5549 else if (cgltf_json_strcmp(tokens + i, json_chunk, "buffers") == 0)
5551 i = cgltf_parse_json_buffers(options, tokens, i + 1, json_chunk, out_data);
5553 else if (cgltf_json_strcmp(tokens + i, json_chunk, "materials") == 0)
5555 i = cgltf_parse_json_materials(options, tokens, i + 1, json_chunk, out_data);
5557 else if (cgltf_json_strcmp(tokens + i, json_chunk, "images") == 0)
5559 i = cgltf_parse_json_images(options, tokens, i + 1, json_chunk, out_data);
5561 else if (cgltf_json_strcmp(tokens + i, json_chunk, "textures") == 0)
5563 i = cgltf_parse_json_textures(options, tokens, i + 1, json_chunk, out_data);
5565 else if (cgltf_json_strcmp(tokens + i, json_chunk, "samplers") == 0)
5567 i = cgltf_parse_json_samplers(options, tokens, i + 1, json_chunk, out_data);
5569 else if (cgltf_json_strcmp(tokens + i, json_chunk, "skins") == 0)
5571 i = cgltf_parse_json_skins(options, tokens, i + 1, json_chunk, out_data);
5573 else if (cgltf_json_strcmp(tokens + i, json_chunk, "cameras") == 0)
5575 i = cgltf_parse_json_cameras(options, tokens, i + 1, json_chunk, out_data);
5577 else if (cgltf_json_strcmp(tokens + i, json_chunk, "nodes") == 0)
5579 i = cgltf_parse_json_nodes(options, tokens, i + 1, json_chunk, out_data);
5581 else if (cgltf_json_strcmp(tokens + i, json_chunk, "scenes") == 0)
5583 i = cgltf_parse_json_scenes(options, tokens, i + 1, json_chunk, out_data);
5585 else if (cgltf_json_strcmp(tokens + i, json_chunk, "scene") == 0)
5588 out_data->scene = CGLTF_PTRINDEX(cgltf_scene, cgltf_json_to_int(tokens + i, json_chunk));
5591 else if (cgltf_json_strcmp(tokens + i, json_chunk, "animations") == 0)
5593 i = cgltf_parse_json_animations(options, tokens, i + 1, json_chunk, out_data);
5595 else if (cgltf_json_strcmp(tokens+i, json_chunk, "extras") == 0)
5597 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_data->extras);
5599 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
5603 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5604 if(out_data->data_extensions)
5606 return CGLTF_ERROR_JSON;
5609 int extensions_size = tokens[i].size;
5610 out_data->data_extensions_count = 0;
5611 out_data->data_extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
5613 if (!out_data->data_extensions)
5615 return CGLTF_ERROR_NOMEM;
5620 for (int k = 0; k < extensions_size; ++k)
5622 CGLTF_CHECK_KEY(tokens[i]);
5624 if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_lights_punctual") == 0)
5628 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5630 int data_size = tokens[i].size;
5633 for (int m = 0; m < data_size; ++m)
5635 CGLTF_CHECK_KEY(tokens[i]);
5637 if (cgltf_json_strcmp(tokens + i, json_chunk, "lights") == 0)
5639 i = cgltf_parse_json_lights(options, tokens, i + 1, json_chunk, out_data);
5643 i = cgltf_skip_json(tokens, i + 1);
5652 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_variants") == 0)
5656 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5658 int data_size = tokens[i].size;
5661 for (int m = 0; m < data_size; ++m)
5663 CGLTF_CHECK_KEY(tokens[i]);
5665 if (cgltf_json_strcmp(tokens + i, json_chunk, "variants") == 0)
5667 i = cgltf_parse_json_variants(options, tokens, i + 1, json_chunk, out_data);
5671 i = cgltf_skip_json(tokens, i + 1);
5682 i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_data->data_extensions[out_data->data_extensions_count++]));
5691 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensionsUsed") == 0)
5693 i = cgltf_parse_json_string_array(options, tokens, i + 1, json_chunk, &out_data->extensions_used, &out_data->extensions_used_count);
5695 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensionsRequired") == 0)
5697 i = cgltf_parse_json_string_array(options, tokens, i + 1, json_chunk, &out_data->extensions_required, &out_data->extensions_required_count);
5701 i = cgltf_skip_json(tokens, i + 1);
5713 cgltf_result cgltf_parse_json(cgltf_options* options, const uint8_t* json_chunk, cgltf_size size, cgltf_data** out_data)
5715 jsmn_parser parser = { 0, 0, 0 };
5717 if (options->json_token_count == 0)
5719 int token_count = jsmn_parse(&parser, (const char*)json_chunk, size, NULL, 0);
5721 if (token_count <= 0)
5723 return cgltf_result_invalid_json;
5726 options->json_token_count = token_count;
5729 jsmntok_t* tokens = (jsmntok_t*)options->memory.alloc(options->memory.user_data, sizeof(jsmntok_t) * (options->json_token_count + 1));
5733 return cgltf_result_out_of_memory;
5738 int token_count = jsmn_parse(&parser, (const char*)json_chunk, size, tokens, options->json_token_count);
5740 if (token_count <= 0)
5742 options->memory.free(options->memory.user_data, tokens);
5743 return cgltf_result_invalid_json;
5746 // this makes sure that we always have an UNDEFINED token at the end of the stream
5747 // for invalid JSON inputs this makes sure we don't perform out of bound reads of token data
5748 tokens[token_count].type = JSMN_UNDEFINED;
5750 cgltf_data* data = (cgltf_data*)options->memory.alloc(options->memory.user_data, sizeof(cgltf_data));
5754 options->memory.free(options->memory.user_data, tokens);
5755 return cgltf_result_out_of_memory;
5758 memset(data, 0, sizeof(cgltf_data));
5759 data->memory = options->memory;
5760 data->file = options->file;
5762 int i = cgltf_parse_json_root(options, tokens, 0, json_chunk, data);
5764 options->memory.free(options->memory.user_data, tokens);
5772 case CGLTF_ERROR_NOMEM: return cgltf_result_out_of_memory;
5773 case CGLTF_ERROR_LEGACY: return cgltf_result_legacy_gltf;
5774 default: return cgltf_result_invalid_gltf;
5778 if (cgltf_fixup_pointers(data) < 0)
5781 return cgltf_result_invalid_gltf;
5784 data->json = (const char*)json_chunk;
5785 data->json_size = size;
5789 return cgltf_result_success;
5792 static int cgltf_fixup_pointers(cgltf_data* data)
5794 for (cgltf_size i = 0; i < data->meshes_count; ++i)
5796 for (cgltf_size j = 0; j < data->meshes[i].primitives_count; ++j)
5798 CGLTF_PTRFIXUP(data->meshes[i].primitives[j].indices, data->accessors, data->accessors_count);
5799 CGLTF_PTRFIXUP(data->meshes[i].primitives[j].material, data->materials, data->materials_count);
5801 for (cgltf_size k = 0; k < data->meshes[i].primitives[j].attributes_count; ++k)
5803 CGLTF_PTRFIXUP_REQ(data->meshes[i].primitives[j].attributes[k].data, data->accessors, data->accessors_count);
5806 for (cgltf_size k = 0; k < data->meshes[i].primitives[j].targets_count; ++k)
5808 for (cgltf_size m = 0; m < data->meshes[i].primitives[j].targets[k].attributes_count; ++m)
5810 CGLTF_PTRFIXUP_REQ(data->meshes[i].primitives[j].targets[k].attributes[m].data, data->accessors, data->accessors_count);
5814 if (data->meshes[i].primitives[j].has_draco_mesh_compression)
5816 CGLTF_PTRFIXUP_REQ(data->meshes[i].primitives[j].draco_mesh_compression.buffer_view, data->buffer_views, data->buffer_views_count);
5817 for (cgltf_size m = 0; m < data->meshes[i].primitives[j].draco_mesh_compression.attributes_count; ++m)
5819 CGLTF_PTRFIXUP_REQ(data->meshes[i].primitives[j].draco_mesh_compression.attributes[m].data, data->accessors, data->accessors_count);
5823 for (cgltf_size k = 0; k < data->meshes[i].primitives[j].mappings_count; ++k)
5825 CGLTF_PTRFIXUP_REQ(data->meshes[i].primitives[j].mappings[k].material, data->materials, data->materials_count);
5830 for (cgltf_size i = 0; i < data->accessors_count; ++i)
5832 CGLTF_PTRFIXUP(data->accessors[i].buffer_view, data->buffer_views, data->buffer_views_count);
5834 if (data->accessors[i].is_sparse)
5836 CGLTF_PTRFIXUP_REQ(data->accessors[i].sparse.indices_buffer_view, data->buffer_views, data->buffer_views_count);
5837 CGLTF_PTRFIXUP_REQ(data->accessors[i].sparse.values_buffer_view, data->buffer_views, data->buffer_views_count);
5840 if (data->accessors[i].buffer_view)
5842 data->accessors[i].stride = data->accessors[i].buffer_view->stride;
5845 if (data->accessors[i].stride == 0)
5847 data->accessors[i].stride = cgltf_calc_size(data->accessors[i].type, data->accessors[i].component_type);
5851 for (cgltf_size i = 0; i < data->textures_count; ++i)
5853 CGLTF_PTRFIXUP(data->textures[i].image, data->images, data->images_count);
5854 CGLTF_PTRFIXUP(data->textures[i].sampler, data->samplers, data->samplers_count);
5857 for (cgltf_size i = 0; i < data->images_count; ++i)
5859 CGLTF_PTRFIXUP(data->images[i].buffer_view, data->buffer_views, data->buffer_views_count);
5862 for (cgltf_size i = 0; i < data->materials_count; ++i)
5864 CGLTF_PTRFIXUP(data->materials[i].normal_texture.texture, data->textures, data->textures_count);
5865 CGLTF_PTRFIXUP(data->materials[i].emissive_texture.texture, data->textures, data->textures_count);
5866 CGLTF_PTRFIXUP(data->materials[i].occlusion_texture.texture, data->textures, data->textures_count);
5868 CGLTF_PTRFIXUP(data->materials[i].pbr_metallic_roughness.base_color_texture.texture, data->textures, data->textures_count);
5869 CGLTF_PTRFIXUP(data->materials[i].pbr_metallic_roughness.metallic_roughness_texture.texture, data->textures, data->textures_count);
5871 CGLTF_PTRFIXUP(data->materials[i].pbr_specular_glossiness.diffuse_texture.texture, data->textures, data->textures_count);
5872 CGLTF_PTRFIXUP(data->materials[i].pbr_specular_glossiness.specular_glossiness_texture.texture, data->textures, data->textures_count);
5874 CGLTF_PTRFIXUP(data->materials[i].clearcoat.clearcoat_texture.texture, data->textures, data->textures_count);
5875 CGLTF_PTRFIXUP(data->materials[i].clearcoat.clearcoat_roughness_texture.texture, data->textures, data->textures_count);
5876 CGLTF_PTRFIXUP(data->materials[i].clearcoat.clearcoat_normal_texture.texture, data->textures, data->textures_count);
5878 CGLTF_PTRFIXUP(data->materials[i].specular.specular_texture.texture, data->textures, data->textures_count);
5879 CGLTF_PTRFIXUP(data->materials[i].specular.specular_color_texture.texture, data->textures, data->textures_count);
5881 CGLTF_PTRFIXUP(data->materials[i].transmission.transmission_texture.texture, data->textures, data->textures_count);
5883 CGLTF_PTRFIXUP(data->materials[i].volume.thickness_texture.texture, data->textures, data->textures_count);
5885 CGLTF_PTRFIXUP(data->materials[i].sheen.sheen_color_texture.texture, data->textures, data->textures_count);
5886 CGLTF_PTRFIXUP(data->materials[i].sheen.sheen_roughness_texture.texture, data->textures, data->textures_count);
5889 for (cgltf_size i = 0; i < data->buffer_views_count; ++i)
5891 CGLTF_PTRFIXUP_REQ(data->buffer_views[i].buffer, data->buffers, data->buffers_count);
5893 if (data->buffer_views[i].has_meshopt_compression)
5895 CGLTF_PTRFIXUP_REQ(data->buffer_views[i].meshopt_compression.buffer, data->buffers, data->buffers_count);
5899 for (cgltf_size i = 0; i < data->skins_count; ++i)
5901 for (cgltf_size j = 0; j < data->skins[i].joints_count; ++j)
5903 CGLTF_PTRFIXUP_REQ(data->skins[i].joints[j], data->nodes, data->nodes_count);
5906 CGLTF_PTRFIXUP(data->skins[i].skeleton, data->nodes, data->nodes_count);
5907 CGLTF_PTRFIXUP(data->skins[i].inverse_bind_matrices, data->accessors, data->accessors_count);
5910 for (cgltf_size i = 0; i < data->nodes_count; ++i)
5912 for (cgltf_size j = 0; j < data->nodes[i].children_count; ++j)
5914 CGLTF_PTRFIXUP_REQ(data->nodes[i].children[j], data->nodes, data->nodes_count);
5916 if (data->nodes[i].children[j]->parent)
5918 return CGLTF_ERROR_JSON;
5921 data->nodes[i].children[j]->parent = &data->nodes[i];
5924 CGLTF_PTRFIXUP(data->nodes[i].mesh, data->meshes, data->meshes_count);
5925 CGLTF_PTRFIXUP(data->nodes[i].skin, data->skins, data->skins_count);
5926 CGLTF_PTRFIXUP(data->nodes[i].camera, data->cameras, data->cameras_count);
5927 CGLTF_PTRFIXUP(data->nodes[i].light, data->lights, data->lights_count);
5930 for (cgltf_size i = 0; i < data->scenes_count; ++i)
5932 for (cgltf_size j = 0; j < data->scenes[i].nodes_count; ++j)
5934 CGLTF_PTRFIXUP_REQ(data->scenes[i].nodes[j], data->nodes, data->nodes_count);
5936 if (data->scenes[i].nodes[j]->parent)
5938 return CGLTF_ERROR_JSON;
5943 CGLTF_PTRFIXUP(data->scene, data->scenes, data->scenes_count);
5945 for (cgltf_size i = 0; i < data->animations_count; ++i)
5947 for (cgltf_size j = 0; j < data->animations[i].samplers_count; ++j)
5949 CGLTF_PTRFIXUP_REQ(data->animations[i].samplers[j].input, data->accessors, data->accessors_count);
5950 CGLTF_PTRFIXUP_REQ(data->animations[i].samplers[j].output, data->accessors, data->accessors_count);
5953 for (cgltf_size j = 0; j < data->animations[i].channels_count; ++j)
5955 CGLTF_PTRFIXUP_REQ(data->animations[i].channels[j].sampler, data->animations[i].samplers, data->animations[i].samplers_count);
5956 CGLTF_PTRFIXUP(data->animations[i].channels[j].target_node, data->nodes, data->nodes_count);
5964 * -- jsmn.c start --
5965 * Source: https://github.com/zserge/jsmn
5968 * Copyright (c) 2010 Serge A. Zaitsev
5970 * Permission is hereby granted, free of charge, to any person obtaining a copy
5971 * of this software and associated documentation files (the "Software"), to deal
5972 * in the Software without restriction, including without limitation the rights
5973 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
5974 * copies of the Software, and to permit persons to whom the Software is
5975 * furnished to do so, subject to the following conditions:
5977 * The above copyright notice and this permission notice shall be included in
5978 * all copies or substantial portions of the Software.
5980 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5981 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
5982 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
5983 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
5984 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
5985 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
5990 * Allocates a fresh unused token from the token pull.
5992 static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser,
5993 jsmntok_t *tokens, size_t num_tokens) {
5995 if (parser->toknext >= num_tokens) {
5998 tok = &tokens[parser->toknext++];
5999 tok->start = tok->end = -1;
6001 #ifdef JSMN_PARENT_LINKS
6008 * Fills token type and boundaries.
6010 static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type,
6011 int start, int end) {
6013 token->start = start;
6019 * Fills next available token with JSON primitive.
6021 static int jsmn_parse_primitive(jsmn_parser *parser, const char *js,
6022 size_t len, jsmntok_t *tokens, size_t num_tokens) {
6026 start = parser->pos;
6028 for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
6029 switch (js[parser->pos]) {
6031 /* In strict mode primitive must be followed by "," or "}" or "]" */
6034 case '\t' : case '\r' : case '\n' : case ' ' :
6035 case ',' : case ']' : case '}' :
6038 if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
6039 parser->pos = start;
6040 return JSMN_ERROR_INVAL;
6044 /* In strict mode primitive must be followed by a comma/object/array */
6045 parser->pos = start;
6046 return JSMN_ERROR_PART;
6050 if (tokens == NULL) {
6054 token = jsmn_alloc_token(parser, tokens, num_tokens);
6055 if (token == NULL) {
6056 parser->pos = start;
6057 return JSMN_ERROR_NOMEM;
6059 jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
6060 #ifdef JSMN_PARENT_LINKS
6061 token->parent = parser->toksuper;
6068 * Fills next token with JSON string.
6070 static int jsmn_parse_string(jsmn_parser *parser, const char *js,
6071 size_t len, jsmntok_t *tokens, size_t num_tokens) {
6074 int start = parser->pos;
6078 /* Skip starting quote */
6079 for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
6080 char c = js[parser->pos];
6082 /* Quote: end of string */
6084 if (tokens == NULL) {
6087 token = jsmn_alloc_token(parser, tokens, num_tokens);
6088 if (token == NULL) {
6089 parser->pos = start;
6090 return JSMN_ERROR_NOMEM;
6092 jsmn_fill_token(token, JSMN_STRING, start+1, parser->pos);
6093 #ifdef JSMN_PARENT_LINKS
6094 token->parent = parser->toksuper;
6099 /* Backslash: Quoted symbol expected */
6100 if (c == '\\' && parser->pos + 1 < len) {
6103 switch (js[parser->pos]) {
6104 /* Allowed escaped symbols */
6105 case '\"': case '/' : case '\\' : case 'b' :
6106 case 'f' : case 'r' : case 'n' : case 't' :
6108 /* Allows escaped symbol \uXXXX */
6111 for(i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0'; i++) {
6112 /* If it isn't a hex character we have an error */
6113 if(!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */
6114 (js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */
6115 (js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */
6116 parser->pos = start;
6117 return JSMN_ERROR_INVAL;
6123 /* Unexpected symbol */
6125 parser->pos = start;
6126 return JSMN_ERROR_INVAL;
6130 parser->pos = start;
6131 return JSMN_ERROR_PART;
6135 * Parse JSON string and fill tokens.
6137 static int jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
6138 jsmntok_t *tokens, size_t num_tokens) {
6142 int count = parser->toknext;
6144 for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
6148 c = js[parser->pos];
6152 if (tokens == NULL) {
6155 token = jsmn_alloc_token(parser, tokens, num_tokens);
6157 return JSMN_ERROR_NOMEM;
6158 if (parser->toksuper != -1) {
6159 tokens[parser->toksuper].size++;
6160 #ifdef JSMN_PARENT_LINKS
6161 token->parent = parser->toksuper;
6164 token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
6165 token->start = parser->pos;
6166 parser->toksuper = parser->toknext - 1;
6171 type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
6172 #ifdef JSMN_PARENT_LINKS
6173 if (parser->toknext < 1) {
6174 return JSMN_ERROR_INVAL;
6176 token = &tokens[parser->toknext - 1];
6178 if (token->start != -1 && token->end == -1) {
6179 if (token->type != type) {
6180 return JSMN_ERROR_INVAL;
6182 token->end = parser->pos + 1;
6183 parser->toksuper = token->parent;
6186 if (token->parent == -1) {
6187 if(token->type != type || parser->toksuper == -1) {
6188 return JSMN_ERROR_INVAL;
6192 token = &tokens[token->parent];
6195 for (i = parser->toknext - 1; i >= 0; i--) {
6197 if (token->start != -1 && token->end == -1) {
6198 if (token->type != type) {
6199 return JSMN_ERROR_INVAL;
6201 parser->toksuper = -1;
6202 token->end = parser->pos + 1;
6206 /* Error if unmatched closing bracket */
6207 if (i == -1) return JSMN_ERROR_INVAL;
6208 for (; i >= 0; i--) {
6210 if (token->start != -1 && token->end == -1) {
6211 parser->toksuper = i;
6218 r = jsmn_parse_string(parser, js, len, tokens, num_tokens);
6219 if (r < 0) return r;
6221 if (parser->toksuper != -1 && tokens != NULL)
6222 tokens[parser->toksuper].size++;
6224 case '\t' : case '\r' : case '\n' : case ' ':
6227 parser->toksuper = parser->toknext - 1;
6230 if (tokens != NULL && parser->toksuper != -1 &&
6231 tokens[parser->toksuper].type != JSMN_ARRAY &&
6232 tokens[parser->toksuper].type != JSMN_OBJECT) {
6233 #ifdef JSMN_PARENT_LINKS
6234 parser->toksuper = tokens[parser->toksuper].parent;
6236 for (i = parser->toknext - 1; i >= 0; i--) {
6237 if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT) {
6238 if (tokens[i].start != -1 && tokens[i].end == -1) {
6239 parser->toksuper = i;
6248 /* In strict mode primitives are: numbers and booleans */
6249 case '-': case '0': case '1' : case '2': case '3' : case '4':
6250 case '5': case '6': case '7' : case '8': case '9':
6251 case 't': case 'f': case 'n' :
6252 /* And they must not be keys of the object */
6253 if (tokens != NULL && parser->toksuper != -1) {
6254 jsmntok_t *t = &tokens[parser->toksuper];
6255 if (t->type == JSMN_OBJECT ||
6256 (t->type == JSMN_STRING && t->size != 0)) {
6257 return JSMN_ERROR_INVAL;
6261 /* In non-strict mode every unquoted value is a primitive */
6264 r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens);
6265 if (r < 0) return r;
6267 if (parser->toksuper != -1 && tokens != NULL)
6268 tokens[parser->toksuper].size++;
6272 /* Unexpected char in strict mode */
6274 return JSMN_ERROR_INVAL;
6279 if (tokens != NULL) {
6280 for (i = parser->toknext - 1; i >= 0; i--) {
6281 /* Unmatched opened object or array */
6282 if (tokens[i].start != -1 && tokens[i].end == -1) {
6283 return JSMN_ERROR_PART;
6292 * Creates a new parser based over a given buffer with an array of tokens
6295 static void jsmn_init(jsmn_parser *parser) {
6297 parser->toknext = 0;
6298 parser->toksuper = -1;
6304 #endif /* #ifdef CGLTF_IMPLEMENTATION */
6306 /* cgltf is distributed under MIT license:
6308 * Copyright (c) 2018 Johannes Kuhlmann
6310 * Permission is hereby granted, free of charge, to any person obtaining a copy
6311 * of this software and associated documentation files (the "Software"), to deal
6312 * in the Software without restriction, including without limitation the rights
6313 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
6314 * copies of the Software, and to permit persons to whom the Software is
6315 * furnished to do so, subject to the following conditions:
6317 * The above copyright notice and this permission notice shall be included in all
6318 * copies or substantial portions of the Software.
6320 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6321 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6322 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
6323 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
6324 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
6325 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE