]> git.sesse.net Git - pistorm/blob - raylib/external/cgltf.h
Update raylib files and Makefile for Pi 4 testing
[pistorm] / raylib / external / cgltf.h
1 /**
2  * cgltf - a single-file glTF 2.0 parser written in C99.
3  *
4  * Version: 1.10
5  *
6  * Website: https://github.com/jkuhlmann/cgltf
7  *
8  * Distributed under the MIT License, see notice at the end of this file.
9  *
10  * Building:
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.
15  *
16  * Reference:
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.
24  *
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.
29  *
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).
33  *
34  * `void cgltf_free(cgltf_data*)` frees the allocated `cgltf_data`
35  * variable.
36  *
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
41  * buffer files.
42  *
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.
47  *
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()`.
51  *
52  * `cgltf_result cgltf_validate(cgltf_data*)` can be used to do additional
53  * checks to make sure the parsed glTF data is valid.
54  *
55  * `cgltf_node_transform_local` converts the translation / rotation / scale properties of a node
56  * into a mat4.
57  *
58  * `cgltf_node_transform_world` calls `cgltf_node_transform_local` on every ancestor in order
59  * to compute the root-to-node transformation.
60  *
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
64  * output buffer.
65  *
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.
69  *
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.
74  *
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.
79  *
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.
82  *
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.
91  */
92 #ifndef CGLTF_H_INCLUDED__
93 #define CGLTF_H_INCLUDED__
94
95 #include <stddef.h>
96
97 #ifdef __cplusplus
98 extern "C" {
99 #endif
100
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;
106
107 typedef enum cgltf_file_type
108 {
109         cgltf_file_type_invalid,
110         cgltf_file_type_gltf,
111         cgltf_file_type_glb,
112 } cgltf_file_type;
113
114 typedef enum cgltf_result
115 {
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,
126 } cgltf_result;
127
128 typedef struct cgltf_memory_options
129 {
130         void* (*alloc)(void* user, cgltf_size size);
131         void (*free) (void* user, void* ptr);
132         void* user_data;
133 } cgltf_memory_options;
134
135 typedef struct cgltf_file_options
136 {
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);
139         void* user_data;
140 } cgltf_file_options;
141
142 typedef struct cgltf_options
143 {
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;
148 } cgltf_options;
149
150 typedef enum cgltf_buffer_view_type
151 {
152         cgltf_buffer_view_type_invalid,
153         cgltf_buffer_view_type_indices,
154         cgltf_buffer_view_type_vertices,
155 } cgltf_buffer_view_type;
156
157 typedef enum cgltf_attribute_type
158 {
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;
168
169 typedef enum cgltf_component_type
170 {
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;
179
180 typedef enum cgltf_type
181 {
182         cgltf_type_invalid,
183         cgltf_type_scalar,
184         cgltf_type_vec2,
185         cgltf_type_vec3,
186         cgltf_type_vec4,
187         cgltf_type_mat2,
188         cgltf_type_mat3,
189         cgltf_type_mat4,
190 } cgltf_type;
191
192 typedef enum cgltf_primitive_type
193 {
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;
202
203 typedef enum cgltf_alpha_mode
204 {
205         cgltf_alpha_mode_opaque,
206         cgltf_alpha_mode_mask,
207         cgltf_alpha_mode_blend,
208 } cgltf_alpha_mode;
209
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;
217
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;
223
224 typedef enum cgltf_camera_type {
225         cgltf_camera_type_invalid,
226         cgltf_camera_type_perspective,
227         cgltf_camera_type_orthographic,
228 } cgltf_camera_type;
229
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,
235 } cgltf_light_type;
236
237 typedef struct cgltf_extras {
238         cgltf_size start_offset;
239         cgltf_size end_offset;
240 } cgltf_extras;
241
242 typedef struct cgltf_extension {
243         char* name;
244         char* data;
245 } cgltf_extension;
246
247 typedef struct cgltf_buffer
248 {
249         char* name;
250         cgltf_size size;
251         char* uri;
252         void* data; /* loaded by cgltf_load_buffers */
253         cgltf_extras extras;
254         cgltf_size extensions_count;
255         cgltf_extension* extensions;
256 } cgltf_buffer;
257
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;
264
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;
271
272 typedef struct cgltf_meshopt_compression
273 {
274         cgltf_buffer* buffer;
275         cgltf_size offset;
276         cgltf_size size;
277         cgltf_size stride;
278         cgltf_size count;
279         cgltf_meshopt_compression_mode mode;
280         cgltf_meshopt_compression_filter filter;
281 } cgltf_meshopt_compression;
282
283 typedef struct cgltf_buffer_view
284 {
285         char *name;
286         cgltf_buffer* buffer;
287         cgltf_size offset;
288         cgltf_size size;
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;
294         cgltf_extras extras;
295         cgltf_size extensions_count;
296         cgltf_extension* extensions;
297 } cgltf_buffer_view;
298
299 typedef struct cgltf_accessor_sparse
300 {
301         cgltf_size count;
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;
307         cgltf_extras extras;
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;
317
318 typedef struct cgltf_accessor
319 {
320         char* name;
321         cgltf_component_type component_type;
322         cgltf_bool normalized;
323         cgltf_type type;
324         cgltf_size offset;
325         cgltf_size count;
326         cgltf_size stride;
327         cgltf_buffer_view* buffer_view;
328         cgltf_bool has_min;
329         cgltf_float min[16];
330         cgltf_bool has_max;
331         cgltf_float max[16];
332         cgltf_bool is_sparse;
333         cgltf_accessor_sparse sparse;
334         cgltf_extras extras;
335         cgltf_size extensions_count;
336         cgltf_extension* extensions;
337 } cgltf_accessor;
338
339 typedef struct cgltf_attribute
340 {
341         char* name;
342         cgltf_attribute_type type;
343         cgltf_int index;
344         cgltf_accessor* data;
345 } cgltf_attribute;
346
347 typedef struct cgltf_image
348 {
349         char* name;
350         char* uri;
351         cgltf_buffer_view* buffer_view;
352         char* mime_type;
353         cgltf_extras extras;
354         cgltf_size extensions_count;
355         cgltf_extension* extensions;
356 } cgltf_image;
357
358 typedef struct cgltf_sampler
359 {
360         char* name;
361         cgltf_int mag_filter;
362         cgltf_int min_filter;
363         cgltf_int wrap_s;
364         cgltf_int wrap_t;
365         cgltf_extras extras;
366         cgltf_size extensions_count;
367         cgltf_extension* extensions;
368 } cgltf_sampler;
369
370 typedef struct cgltf_texture
371 {
372         char* name;
373         cgltf_image* image;
374         cgltf_sampler* sampler;
375         cgltf_extras extras;
376         cgltf_size extensions_count;
377         cgltf_extension* extensions;
378 } cgltf_texture;
379
380 typedef struct cgltf_texture_transform
381 {
382         cgltf_float offset[2];
383         cgltf_float rotation;
384         cgltf_float scale[2];
385         cgltf_int texcoord;
386 } cgltf_texture_transform;
387
388 typedef struct cgltf_texture_view
389 {
390         cgltf_texture* texture;
391         cgltf_int texcoord;
392         cgltf_float scale; /* equivalent to strength for occlusion_texture */
393         cgltf_bool has_transform;
394         cgltf_texture_transform transform;
395         cgltf_extras extras;
396         cgltf_size extensions_count;
397         cgltf_extension* extensions;
398 } cgltf_texture_view;
399
400 typedef struct cgltf_pbr_metallic_roughness
401 {
402         cgltf_texture_view base_color_texture;
403         cgltf_texture_view metallic_roughness_texture;
404
405         cgltf_float base_color_factor[4];
406         cgltf_float metallic_factor;
407         cgltf_float roughness_factor;
408
409         cgltf_extras extras;
410 } cgltf_pbr_metallic_roughness;
411
412 typedef struct cgltf_pbr_specular_glossiness
413 {
414         cgltf_texture_view diffuse_texture;
415         cgltf_texture_view specular_glossiness_texture;
416
417         cgltf_float diffuse_factor[4];
418         cgltf_float specular_factor[3];
419         cgltf_float glossiness_factor;
420 } cgltf_pbr_specular_glossiness;
421
422 typedef struct cgltf_clearcoat
423 {
424         cgltf_texture_view clearcoat_texture;
425         cgltf_texture_view clearcoat_roughness_texture;
426         cgltf_texture_view clearcoat_normal_texture;
427
428         cgltf_float clearcoat_factor;
429         cgltf_float clearcoat_roughness_factor;
430 } cgltf_clearcoat;
431
432 typedef struct cgltf_transmission
433 {
434         cgltf_texture_view transmission_texture;
435         cgltf_float transmission_factor;
436 } cgltf_transmission;
437
438 typedef struct cgltf_ior
439 {
440         cgltf_float ior;
441 } cgltf_ior;
442
443 typedef struct cgltf_specular
444 {
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;
449 } cgltf_specular;
450
451 typedef struct cgltf_volume
452 {
453         cgltf_texture_view thickness_texture;
454         cgltf_float thickness_factor;
455         cgltf_float attenuation_color[3];
456         cgltf_float attenuation_distance;
457 } cgltf_volume;
458
459 typedef struct cgltf_sheen
460 {
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;
465 } cgltf_sheen;
466
467 typedef struct cgltf_material
468 {
469         char* name;
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;
475         cgltf_bool has_ior;
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;
481         cgltf_ior ior;
482         cgltf_specular specular;
483         cgltf_sheen sheen;
484         cgltf_transmission transmission;
485         cgltf_volume volume;
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;
493         cgltf_bool unlit;
494         cgltf_extras extras;
495         cgltf_size extensions_count;
496         cgltf_extension* extensions;
497 } cgltf_material;
498
499 typedef struct cgltf_material_mapping
500 {
501         cgltf_size variant;
502         cgltf_material* material;
503         cgltf_extras extras;
504 } cgltf_material_mapping;
505
506 typedef struct cgltf_morph_target {
507         cgltf_attribute* attributes;
508         cgltf_size attributes_count;
509 } cgltf_morph_target;
510
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;
516
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;
525         cgltf_extras extras;
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;
532 } cgltf_primitive;
533
534 typedef struct cgltf_mesh {
535         char* name;
536         cgltf_primitive* primitives;
537         cgltf_size primitives_count;
538         cgltf_float* weights;
539         cgltf_size weights_count;
540         char** target_names;
541         cgltf_size target_names_count;
542         cgltf_extras extras;
543         cgltf_size extensions_count;
544         cgltf_extension* extensions;
545 } cgltf_mesh;
546
547 typedef struct cgltf_node cgltf_node;
548
549 typedef struct cgltf_skin {
550         char* name;
551         cgltf_node** joints;
552         cgltf_size joints_count;
553         cgltf_node* skeleton;
554         cgltf_accessor* inverse_bind_matrices;
555         cgltf_extras extras;
556         cgltf_size extensions_count;
557         cgltf_extension* extensions;
558 } cgltf_skin;
559
560 typedef struct cgltf_camera_perspective {
561         cgltf_bool has_aspect_ratio;
562         cgltf_float aspect_ratio;
563         cgltf_float yfov;
564         cgltf_bool has_zfar;
565         cgltf_float zfar;
566         cgltf_float znear;
567         cgltf_extras extras;
568 } cgltf_camera_perspective;
569
570 typedef struct cgltf_camera_orthographic {
571         cgltf_float xmag;
572         cgltf_float ymag;
573         cgltf_float zfar;
574         cgltf_float znear;
575         cgltf_extras extras;
576 } cgltf_camera_orthographic;
577
578 typedef struct cgltf_camera {
579         char* name;
580         cgltf_camera_type type;
581         union {
582                 cgltf_camera_perspective perspective;
583                 cgltf_camera_orthographic orthographic;
584         } data;
585         cgltf_extras extras;
586         cgltf_size extensions_count;
587         cgltf_extension* extensions;
588 } cgltf_camera;
589
590 typedef struct cgltf_light {
591         char* name;
592         cgltf_float color[3];
593         cgltf_float intensity;
594         cgltf_light_type type;
595         cgltf_float range;
596         cgltf_float spot_inner_cone_angle;
597         cgltf_float spot_outer_cone_angle;
598 } cgltf_light;
599
600 struct cgltf_node {
601         char* name;
602         cgltf_node* parent;
603         cgltf_node** children;
604         cgltf_size children_count;
605         cgltf_skin* skin;
606         cgltf_mesh* mesh;
607         cgltf_camera* camera;
608         cgltf_light* light;
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];
619         cgltf_extras extras;
620         cgltf_size extensions_count;
621         cgltf_extension* extensions;
622 };
623
624 typedef struct cgltf_scene {
625         char* name;
626         cgltf_node** nodes;
627         cgltf_size nodes_count;
628         cgltf_extras extras;
629         cgltf_size extensions_count;
630         cgltf_extension* extensions;
631 } cgltf_scene;
632
633 typedef struct cgltf_animation_sampler {
634         cgltf_accessor* input;
635         cgltf_accessor* output;
636         cgltf_interpolation_type interpolation;
637         cgltf_extras extras;
638         cgltf_size extensions_count;
639         cgltf_extension* extensions;
640 } cgltf_animation_sampler;
641
642 typedef struct cgltf_animation_channel {
643         cgltf_animation_sampler* sampler;
644         cgltf_node* target_node;
645         cgltf_animation_path_type target_path;
646         cgltf_extras extras;
647         cgltf_size extensions_count;
648         cgltf_extension* extensions;
649 } cgltf_animation_channel;
650
651 typedef struct cgltf_animation {
652         char* name;
653         cgltf_animation_sampler* samplers;
654         cgltf_size samplers_count;
655         cgltf_animation_channel* channels;
656         cgltf_size channels_count;
657         cgltf_extras extras;
658         cgltf_size extensions_count;
659         cgltf_extension* extensions;
660 } cgltf_animation;
661
662 typedef struct cgltf_material_variant
663 {
664         char* name;
665         cgltf_extras extras;
666 } cgltf_material_variant;
667
668 typedef struct cgltf_asset {
669         char* copyright;
670         char* generator;
671         char* version;
672         char* min_version;
673         cgltf_extras extras;
674         cgltf_size extensions_count;
675         cgltf_extension* extensions;
676 } cgltf_asset;
677
678 typedef struct cgltf_data
679 {
680         cgltf_file_type file_type;
681         void* file_data;
682
683         cgltf_asset asset;
684
685         cgltf_mesh* meshes;
686         cgltf_size meshes_count;
687
688         cgltf_material* materials;
689         cgltf_size materials_count;
690
691         cgltf_accessor* accessors;
692         cgltf_size accessors_count;
693
694         cgltf_buffer_view* buffer_views;
695         cgltf_size buffer_views_count;
696
697         cgltf_buffer* buffers;
698         cgltf_size buffers_count;
699
700         cgltf_image* images;
701         cgltf_size images_count;
702
703         cgltf_texture* textures;
704         cgltf_size textures_count;
705
706         cgltf_sampler* samplers;
707         cgltf_size samplers_count;
708
709         cgltf_skin* skins;
710         cgltf_size skins_count;
711
712         cgltf_camera* cameras;
713         cgltf_size cameras_count;
714
715         cgltf_light* lights;
716         cgltf_size lights_count;
717
718         cgltf_node* nodes;
719         cgltf_size nodes_count;
720
721         cgltf_scene* scenes;
722         cgltf_size scenes_count;
723
724         cgltf_scene* scene;
725
726         cgltf_animation* animations;
727         cgltf_size animations_count;
728
729         cgltf_material_variant* variants;
730         cgltf_size variants_count;
731
732         cgltf_extras extras;
733
734         cgltf_size data_extensions_count;
735         cgltf_extension* data_extensions;
736
737         char** extensions_used;
738         cgltf_size extensions_used_count;
739
740         char** extensions_required;
741         cgltf_size extensions_required_count;
742
743         const char* json;
744         cgltf_size json_size;
745
746         const void* bin;
747         cgltf_size bin_size;
748
749         cgltf_memory_options memory;
750         cgltf_file_options file;
751 } cgltf_data;
752
753 cgltf_result cgltf_parse(
754                 const cgltf_options* options,
755                 const void* data,
756                 cgltf_size size,
757                 cgltf_data** out_data);
758
759 cgltf_result cgltf_parse_file(
760                 const cgltf_options* options,
761                 const char* path,
762                 cgltf_data** out_data);
763
764 cgltf_result cgltf_load_buffers(
765                 const cgltf_options* options,
766                 cgltf_data* data,
767                 const char* gltf_path);
768
769 cgltf_result cgltf_load_buffer_base64(const cgltf_options* options, cgltf_size size, const char* base64, void** out_data);
770
771 void cgltf_decode_uri(char* uri);
772
773 cgltf_result cgltf_validate(cgltf_data* data);
774
775 void cgltf_free(cgltf_data* data);
776
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);
779
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);
783
784 cgltf_size cgltf_num_components(cgltf_type type);
785
786 cgltf_size cgltf_accessor_unpack_floats(const cgltf_accessor* accessor, cgltf_float* out, cgltf_size float_count);
787
788 cgltf_result cgltf_copy_extras_json(const cgltf_data* data, const cgltf_extras* extras, char* dest, cgltf_size* dest_size);
789
790 #ifdef __cplusplus
791 }
792 #endif
793
794 #endif /* #ifndef CGLTF_H_INCLUDED__ */
795
796 /*
797  *
798  * Stop now, if you are only interested in the API.
799  * Below, you find the implementation.
800  *
801  */
802
803 #if defined(__INTELLISENSE__) || defined(__JETBRAINS_IDE__)
804 /* This makes MSVC/CLion intellisense work. */
805 #define CGLTF_IMPLEMENTATION
806 #endif
807
808 #ifdef CGLTF_IMPLEMENTATION
809
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 */
815
816 #if !defined(CGLTF_MALLOC) || !defined(CGLTF_FREE) || !defined(CGLTF_ATOI) || !defined(CGLTF_ATOF)
817 #include <stdlib.h> /* For malloc, free, atoi, atof */
818 #endif
819
820 /* JSMN_PARENT_LINKS is necessary to make parsing large structures linear in input size */
821 #define JSMN_PARENT_LINKS
822
823 /* JSMN_STRICT is necessary to reject invalid JSON documents */
824 #define JSMN_STRICT
825
826 /*
827  * -- jsmn.h start --
828  * Source: https://github.com/zserge/jsmn
829  * License: MIT
830  */
831 typedef enum {
832         JSMN_UNDEFINED = 0,
833         JSMN_OBJECT = 1,
834         JSMN_ARRAY = 2,
835         JSMN_STRING = 3,
836         JSMN_PRIMITIVE = 4
837 } jsmntype_t;
838 enum jsmnerr {
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 */
844         JSMN_ERROR_PART = -3
845 };
846 typedef struct {
847         jsmntype_t type;
848         int start;
849         int end;
850         int size;
851 #ifdef JSMN_PARENT_LINKS
852         int parent;
853 #endif
854 } jsmntok_t;
855 typedef struct {
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 */
859 } jsmn_parser;
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);
862 /*
863  * -- jsmn.h end --
864  */
865
866
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;
873
874 #ifndef CGLTF_MALLOC
875 #define CGLTF_MALLOC(size) malloc(size)
876 #endif
877 #ifndef CGLTF_FREE
878 #define CGLTF_FREE(ptr) free(ptr)
879 #endif
880 #ifndef CGLTF_ATOI
881 #define CGLTF_ATOI(str) atoi(str)
882 #endif
883 #ifndef CGLTF_ATOF
884 #define CGLTF_ATOF(str) atof(str)
885 #endif
886 #ifndef CGLTF_VALIDATE_ENABLE_ASSERTS
887 #define CGLTF_VALIDATE_ENABLE_ASSERTS 0
888 #endif
889
890 static void* cgltf_default_alloc(void* user, cgltf_size size)
891 {
892         (void)user;
893         return CGLTF_MALLOC(size);
894 }
895
896 static void cgltf_default_free(void* user, void* ptr)
897 {
898         (void)user;
899         CGLTF_FREE(ptr);
900 }
901
902 static void* cgltf_calloc(cgltf_options* options, size_t element_size, cgltf_size count)
903 {
904         if (SIZE_MAX / element_size < count)
905         {
906                 return NULL;
907         }
908         void* result = options->memory.alloc(options->memory.user_data, element_size * count);
909         if (!result)
910         {
911                 return NULL;
912         }
913         memset(result, 0, element_size * count);
914         return result;
915 }
916
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)
918 {
919         (void)file_options;
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;
922
923         FILE* file = fopen(path, "rb");
924         if (!file)
925         {
926                 return cgltf_result_file_not_found;
927         }
928
929         cgltf_size file_size = size ? *size : 0;
930
931         if (file_size == 0)
932         {
933                 fseek(file, 0, SEEK_END);
934
935                 long length = ftell(file);
936                 if (length < 0)
937                 {
938                         fclose(file);
939                         return cgltf_result_io_error;
940                 }
941
942                 fseek(file, 0, SEEK_SET);
943                 file_size = (cgltf_size)length;
944         }
945
946         char* file_data = (char*)memory_alloc(memory_options->user_data, file_size);
947         if (!file_data)
948         {
949                 fclose(file);
950                 return cgltf_result_out_of_memory;
951         }
952         
953         cgltf_size read_size = fread(file_data, 1, file_size, file);
954
955         fclose(file);
956
957         if (read_size != file_size)
958         {
959                 memory_free(memory_options->user_data, file_data);
960                 return cgltf_result_io_error;
961         }
962
963         if (size)
964         {
965                 *size = file_size;
966         }
967         if (data)
968         {
969                 *data = file_data;
970         }
971
972         return cgltf_result_success;
973 }
974
975 static void cgltf_default_file_release(const struct cgltf_memory_options* memory_options, const struct cgltf_file_options* file_options, void* data)
976 {
977         (void)file_options;
978         void (*memfree)(void*, void*) = memory_options->free ? memory_options->free : &cgltf_default_free;
979         memfree(memory_options->user_data, data);
980 }
981
982 static cgltf_result cgltf_parse_json(cgltf_options* options, const uint8_t* json_chunk, cgltf_size size, cgltf_data** out_data);
983
984 cgltf_result cgltf_parse(const cgltf_options* options, const void* data, cgltf_size size, cgltf_data** out_data)
985 {
986         if (size < GlbHeaderSize)
987         {
988                 return cgltf_result_data_too_short;
989         }
990
991         if (options == NULL)
992         {
993                 return cgltf_result_invalid_options;
994         }
995
996         cgltf_options fixed_options = *options;
997         if (fixed_options.memory.alloc == NULL)
998         {
999                 fixed_options.memory.alloc = &cgltf_default_alloc;
1000         }
1001         if (fixed_options.memory.free == NULL)
1002         {
1003                 fixed_options.memory.free = &cgltf_default_free;
1004         }
1005
1006         uint32_t tmp;
1007         // Magic
1008         memcpy(&tmp, data, 4);
1009         if (tmp != GlbMagic)
1010         {
1011                 if (fixed_options.type == cgltf_file_type_invalid)
1012                 {
1013                         fixed_options.type = cgltf_file_type_gltf;
1014                 }
1015                 else if (fixed_options.type == cgltf_file_type_glb)
1016                 {
1017                         return cgltf_result_unknown_format;
1018                 }
1019         }
1020
1021         if (fixed_options.type == cgltf_file_type_gltf)
1022         {
1023                 cgltf_result json_result = cgltf_parse_json(&fixed_options, (const uint8_t*)data, size, out_data);
1024                 if (json_result != cgltf_result_success)
1025                 {
1026                         return json_result;
1027                 }
1028
1029                 (*out_data)->file_type = cgltf_file_type_gltf;
1030
1031                 return cgltf_result_success;
1032         }
1033
1034         const uint8_t* ptr = (const uint8_t*)data;
1035         // Version
1036         memcpy(&tmp, ptr + 4, 4);
1037         uint32_t version = tmp;
1038         if (version != GlbVersion)
1039         {
1040                 return version < GlbVersion ? cgltf_result_legacy_gltf : cgltf_result_unknown_format;
1041         }
1042
1043         // Total length
1044         memcpy(&tmp, ptr + 8, 4);
1045         if (tmp > size)
1046         {
1047                 return cgltf_result_data_too_short;
1048         }
1049
1050         const uint8_t* json_chunk = ptr + GlbHeaderSize;
1051
1052         if (GlbHeaderSize + GlbChunkHeaderSize > size)
1053         {
1054                 return cgltf_result_data_too_short;
1055         }
1056
1057         // JSON chunk: length
1058         uint32_t json_length;
1059         memcpy(&json_length, json_chunk, 4);
1060         if (GlbHeaderSize + GlbChunkHeaderSize + json_length > size)
1061         {
1062                 return cgltf_result_data_too_short;
1063         }
1064
1065         // JSON chunk: magic
1066         memcpy(&tmp, json_chunk + 4, 4);
1067         if (tmp != GlbMagicJsonChunk)
1068         {
1069                 return cgltf_result_unknown_format;
1070         }
1071
1072         json_chunk += GlbChunkHeaderSize;
1073
1074         const void* bin = 0;
1075         cgltf_size bin_size = 0;
1076
1077         if (GlbHeaderSize + GlbChunkHeaderSize + json_length + GlbChunkHeaderSize <= size)
1078         {
1079                 // We can read another chunk
1080                 const uint8_t* bin_chunk = json_chunk + json_length;
1081
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)
1086                 {
1087                         return cgltf_result_data_too_short;
1088                 }
1089
1090                 // Bin chunk: magic
1091                 memcpy(&tmp, bin_chunk + 4, 4);
1092                 if (tmp != GlbMagicBinChunk)
1093                 {
1094                         return cgltf_result_unknown_format;
1095                 }
1096
1097                 bin_chunk += GlbChunkHeaderSize;
1098
1099                 bin = bin_chunk;
1100                 bin_size = bin_length;
1101         }
1102
1103         cgltf_result json_result = cgltf_parse_json(&fixed_options, json_chunk, json_length, out_data);
1104         if (json_result != cgltf_result_success)
1105         {
1106                 return json_result;
1107         }
1108
1109         (*out_data)->file_type = cgltf_file_type_glb;
1110         (*out_data)->bin = bin;
1111         (*out_data)->bin_size = bin_size;
1112
1113         return cgltf_result_success;
1114 }
1115
1116 cgltf_result cgltf_parse_file(const cgltf_options* options, const char* path, cgltf_data** out_data)
1117 {
1118         if (options == NULL)
1119         {
1120                 return cgltf_result_invalid_options;
1121         }
1122
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;
1125
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)
1130         {
1131                 return result;
1132         }
1133
1134         result = cgltf_parse(options, file_data, file_size, out_data);
1135
1136         if (result != cgltf_result_success)
1137         {
1138                 memory_free(options->memory.user_data, file_data);
1139                 return result;
1140         }
1141
1142         (*out_data)->file_data = file_data;
1143
1144         return cgltf_result_success;
1145 }
1146
1147 static void cgltf_combine_paths(char* path, const char* base, const char* uri)
1148 {
1149         const char* s0 = strrchr(base, '/');
1150         const char* s1 = strrchr(base, '\\');
1151         const char* slash = s0 ? (s1 && s1 > s0 ? s1 : s0) : s1;
1152
1153         if (slash)
1154         {
1155                 size_t prefix = slash - base + 1;
1156
1157                 strncpy(path, base, prefix);
1158                 strcpy(path + prefix, uri);
1159         }
1160         else
1161         {
1162                 strcpy(path, uri);
1163         }
1164 }
1165
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)
1167 {
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;
1171
1172         char* path = (char*)memory_alloc(options->memory.user_data, strlen(uri) + strlen(gltf_path) + 1);
1173         if (!path)
1174         {
1175                 return cgltf_result_out_of_memory;
1176         }
1177
1178         cgltf_combine_paths(path, gltf_path, uri);
1179
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));
1182
1183         void* file_data = NULL;
1184         cgltf_result result = file_read(&options->memory, &options->file, path, &size, &file_data);
1185
1186         memory_free(options->memory.user_data, path);
1187
1188         *out_data = (result == cgltf_result_success) ? file_data : NULL;
1189
1190         return result;
1191 }
1192
1193 cgltf_result cgltf_load_buffer_base64(const cgltf_options* options, cgltf_size size, const char* base64, void** out_data)
1194 {
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;
1197
1198         unsigned char* data = (unsigned char*)memory_alloc(options->memory.user_data, size);
1199         if (!data)
1200         {
1201                 return cgltf_result_out_of_memory;
1202         }
1203
1204         unsigned int buffer = 0;
1205         unsigned int buffer_bits = 0;
1206
1207         for (cgltf_size i = 0; i < size; ++i)
1208         {
1209                 while (buffer_bits < 8)
1210                 {
1211                         char ch = *base64++;
1212
1213                         int index =
1214                                 (unsigned)(ch - 'A') < 26 ? (ch - 'A') :
1215                                 (unsigned)(ch - 'a') < 26 ? (ch - 'a') + 26 :
1216                                 (unsigned)(ch - '0') < 10 ? (ch - '0') + 52 :
1217                                 ch == '+' ? 62 :
1218                                 ch == '/' ? 63 :
1219                                 -1;
1220
1221                         if (index < 0)
1222                         {
1223                                 memory_free(options->memory.user_data, data);
1224                                 return cgltf_result_io_error;
1225                         }
1226
1227                         buffer = (buffer << 6) | index;
1228                         buffer_bits += 6;
1229                 }
1230
1231                 data[i] = (unsigned char)(buffer >> (buffer_bits - 8));
1232                 buffer_bits -= 8;
1233         }
1234
1235         *out_data = data;
1236
1237         return cgltf_result_success;
1238 }
1239
1240 static int cgltf_unhex(char ch)
1241 {
1242         return
1243                 (unsigned)(ch - '0') < 10 ? (ch - '0') :
1244                 (unsigned)(ch - 'A') < 6 ? (ch - 'A') + 10 :
1245                 (unsigned)(ch - 'a') < 6 ? (ch - 'a') + 10 :
1246                 -1;
1247 }
1248
1249 void cgltf_decode_uri(char* uri)
1250 {
1251         char* write = uri;
1252         char* i = uri;
1253
1254         while (*i)
1255         {
1256                 if (*i == '%')
1257                 {
1258                         int ch1 = cgltf_unhex(i[1]);
1259
1260                         if (ch1 >= 0)
1261                         {
1262                                 int ch2 = cgltf_unhex(i[2]);
1263
1264                                 if (ch2 >= 0)
1265                                 {
1266                                         *write++ = (char)(ch1 * 16 + ch2);
1267                                         i += 3;
1268                                         continue;
1269                                 }
1270                         }
1271                 }
1272
1273                 *write++ = *i++;
1274         }
1275
1276         *write = 0;
1277 }
1278
1279 cgltf_result cgltf_load_buffers(const cgltf_options* options, cgltf_data* data, const char* gltf_path)
1280 {
1281         if (options == NULL)
1282         {
1283                 return cgltf_result_invalid_options;
1284         }
1285
1286         if (data->buffers_count && data->buffers[0].data == NULL && data->buffers[0].uri == NULL && data->bin)
1287         {
1288                 if (data->bin_size < data->buffers[0].size)
1289                 {
1290                         return cgltf_result_data_too_short;
1291                 }
1292
1293                 data->buffers[0].data = (void*)data->bin;
1294         }
1295
1296         for (cgltf_size i = 0; i < data->buffers_count; ++i)
1297         {
1298                 if (data->buffers[i].data)
1299                 {
1300                         continue;
1301                 }
1302
1303                 const char* uri = data->buffers[i].uri;
1304
1305                 if (uri == NULL)
1306                 {
1307                         continue;
1308                 }
1309
1310                 if (strncmp(uri, "data:", 5) == 0)
1311                 {
1312                         const char* comma = strchr(uri, ',');
1313
1314                         if (comma && comma - uri >= 7 && strncmp(comma - 7, ";base64", 7) == 0)
1315                         {
1316                                 cgltf_result res = cgltf_load_buffer_base64(options, data->buffers[i].size, comma + 1, &data->buffers[i].data);
1317
1318                                 if (res != cgltf_result_success)
1319                                 {
1320                                         return res;
1321                                 }
1322                         }
1323                         else
1324                         {
1325                                 return cgltf_result_unknown_format;
1326                         }
1327                 }
1328                 else if (strstr(uri, "://") == NULL && gltf_path)
1329                 {
1330                         cgltf_result res = cgltf_load_buffer_file(options, data->buffers[i].size, uri, gltf_path, &data->buffers[i].data);
1331
1332                         if (res != cgltf_result_success)
1333                         {
1334                                 return res;
1335                         }
1336                 }
1337                 else
1338                 {
1339                         return cgltf_result_unknown_format;
1340                 }
1341         }
1342
1343         return cgltf_result_success;
1344 }
1345
1346 static cgltf_size cgltf_calc_size(cgltf_type type, cgltf_component_type component_type);
1347
1348 static cgltf_size cgltf_calc_index_bound(cgltf_buffer_view* buffer_view, cgltf_size offset, cgltf_component_type component_type, cgltf_size count)
1349 {
1350         char* data = (char*)buffer_view->buffer->data + offset + buffer_view->offset;
1351         cgltf_size bound = 0;
1352
1353         switch (component_type)
1354         {
1355         case cgltf_component_type_r_8u:
1356                 for (size_t i = 0; i < count; ++i)
1357                 {
1358                         cgltf_size v = ((unsigned char*)data)[i];
1359                         bound = bound > v ? bound : v;
1360                 }
1361                 break;
1362
1363         case cgltf_component_type_r_16u:
1364                 for (size_t i = 0; i < count; ++i)
1365                 {
1366                         cgltf_size v = ((unsigned short*)data)[i];
1367                         bound = bound > v ? bound : v;
1368                 }
1369                 break;
1370
1371         case cgltf_component_type_r_32u:
1372                 for (size_t i = 0; i < count; ++i)
1373                 {
1374                         cgltf_size v = ((unsigned int*)data)[i];
1375                         bound = bound > v ? bound : v;
1376                 }
1377                 break;
1378
1379         default:
1380                 ;
1381         }
1382
1383         return bound;
1384 }
1385
1386 #if CGLTF_VALIDATE_ENABLE_ASSERTS
1387 #define CGLTF_ASSERT_IF(cond, result) assert(!(cond)); if (cond) return result;
1388 #else
1389 #define CGLTF_ASSERT_IF(cond, result) if (cond) return result;
1390 #endif
1391
1392 cgltf_result cgltf_validate(cgltf_data* data)
1393 {
1394         for (cgltf_size i = 0; i < data->accessors_count; ++i)
1395         {
1396                 cgltf_accessor* accessor = &data->accessors[i];
1397
1398                 cgltf_size element_size = cgltf_calc_size(accessor->type, accessor->component_type);
1399
1400                 if (accessor->buffer_view)
1401                 {
1402                         cgltf_size req_size = accessor->offset + accessor->stride * (accessor->count - 1) + element_size;
1403
1404                         CGLTF_ASSERT_IF(accessor->buffer_view->size < req_size, cgltf_result_data_too_short);
1405                 }
1406
1407                 if (accessor->is_sparse)
1408                 {
1409                         cgltf_accessor_sparse* sparse = &accessor->sparse;
1410
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;
1414
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);
1417
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);
1421
1422                         if (sparse->indices_buffer_view->buffer->data)
1423                         {
1424                                 cgltf_size index_bound = cgltf_calc_index_bound(sparse->indices_buffer_view, sparse->indices_byte_offset, sparse->indices_component_type, sparse->count);
1425
1426                                 CGLTF_ASSERT_IF(index_bound >= accessor->count, cgltf_result_data_too_short);
1427                         }
1428                 }
1429         }
1430
1431         for (cgltf_size i = 0; i < data->buffer_views_count; ++i)
1432         {
1433                 cgltf_size req_size = data->buffer_views[i].offset + data->buffer_views[i].size;
1434
1435                 CGLTF_ASSERT_IF(data->buffer_views[i].buffer && data->buffer_views[i].buffer->size < req_size, cgltf_result_data_too_short);
1436
1437                 if (data->buffer_views[i].has_meshopt_compression)
1438                 {
1439                         cgltf_meshopt_compression* mc = &data->buffer_views[i].meshopt_compression;
1440
1441                         CGLTF_ASSERT_IF(mc->buffer == NULL || mc->buffer->size < mc->offset + mc->size, cgltf_result_data_too_short);
1442
1443                         CGLTF_ASSERT_IF(data->buffer_views[i].stride && mc->stride != data->buffer_views[i].stride, cgltf_result_invalid_gltf);
1444
1445                         CGLTF_ASSERT_IF(data->buffer_views[i].size != mc->stride * mc->count, cgltf_result_invalid_gltf);
1446
1447                         CGLTF_ASSERT_IF(mc->mode == cgltf_meshopt_compression_mode_invalid, cgltf_result_invalid_gltf);
1448
1449                         CGLTF_ASSERT_IF(mc->mode == cgltf_meshopt_compression_mode_attributes && !(mc->stride % 4 == 0 && mc->stride <= 256), cgltf_result_invalid_gltf);
1450
1451                         CGLTF_ASSERT_IF(mc->mode == cgltf_meshopt_compression_mode_triangles && mc->count % 3 != 0, cgltf_result_invalid_gltf);
1452
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);
1454
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);
1456
1457                         CGLTF_ASSERT_IF(mc->filter == cgltf_meshopt_compression_filter_octahedral && mc->stride != 4 && mc->stride != 8, cgltf_result_invalid_gltf);
1458
1459                         CGLTF_ASSERT_IF(mc->filter == cgltf_meshopt_compression_filter_quaternion && mc->stride != 8, cgltf_result_invalid_gltf);
1460                 }
1461         }
1462
1463         for (cgltf_size i = 0; i < data->meshes_count; ++i)
1464         {
1465                 if (data->meshes[i].weights)
1466                 {
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);
1468                 }
1469
1470                 if (data->meshes[i].target_names)
1471                 {
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);
1473                 }
1474
1475                 for (cgltf_size j = 0; j < data->meshes[i].primitives_count; ++j)
1476                 {
1477                         CGLTF_ASSERT_IF(data->meshes[i].primitives[j].targets_count != data->meshes[i].primitives[0].targets_count, cgltf_result_invalid_gltf);
1478
1479                         if (data->meshes[i].primitives[j].attributes_count)
1480                         {
1481                                 cgltf_accessor* first = data->meshes[i].primitives[j].attributes[0].data;
1482
1483                                 for (cgltf_size k = 0; k < data->meshes[i].primitives[j].attributes_count; ++k)
1484                                 {
1485                                         CGLTF_ASSERT_IF(data->meshes[i].primitives[j].attributes[k].data->count != first->count, cgltf_result_invalid_gltf);
1486                                 }
1487
1488                                 for (cgltf_size k = 0; k < data->meshes[i].primitives[j].targets_count; ++k)
1489                                 {
1490                                         for (cgltf_size m = 0; m < data->meshes[i].primitives[j].targets[k].attributes_count; ++m)
1491                                         {
1492                                                 CGLTF_ASSERT_IF(data->meshes[i].primitives[j].targets[k].attributes[m].data->count != first->count, cgltf_result_invalid_gltf);
1493                                         }
1494                                 }
1495
1496                                 cgltf_accessor* indices = data->meshes[i].primitives[j].indices;
1497
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);
1502
1503                                 if (indices && indices->buffer_view && indices->buffer_view->buffer->data)
1504                                 {
1505                                         cgltf_size index_bound = cgltf_calc_index_bound(indices->buffer_view, indices->offset, indices->component_type, indices->count);
1506
1507                                         CGLTF_ASSERT_IF(index_bound >= first->count, cgltf_result_data_too_short);
1508                                 }
1509
1510                                 for (cgltf_size k = 0; k < data->meshes[i].primitives[j].mappings_count; ++k)
1511                                 {
1512                                         CGLTF_ASSERT_IF(data->meshes[i].primitives[j].mappings[k].variant >= data->variants_count, cgltf_result_invalid_gltf);
1513                                 }
1514                         }
1515                 }
1516         }
1517
1518         for (cgltf_size i = 0; i < data->nodes_count; ++i)
1519         {
1520                 if (data->nodes[i].weights && data->nodes[i].mesh)
1521                 {
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);
1523                 }
1524         }
1525
1526         for (cgltf_size i = 0; i < data->nodes_count; ++i)
1527         {
1528                 cgltf_node* p1 = data->nodes[i].parent;
1529                 cgltf_node* p2 = p1 ? p1->parent : NULL;
1530
1531                 while (p1 && p2)
1532                 {
1533                         CGLTF_ASSERT_IF(p1 == p2, cgltf_result_invalid_gltf);
1534
1535                         p1 = p1->parent;
1536                         p2 = p2->parent ? p2->parent->parent : NULL;
1537                 }
1538         }
1539
1540         for (cgltf_size i = 0; i < data->scenes_count; ++i)
1541         {
1542                 for (cgltf_size j = 0; j < data->scenes[i].nodes_count; ++j)
1543                 {
1544                         CGLTF_ASSERT_IF(data->scenes[i].nodes[j]->parent, cgltf_result_invalid_gltf);
1545                 }
1546         }
1547
1548         for (cgltf_size i = 0; i < data->animations_count; ++i)
1549         {
1550                 for (cgltf_size j = 0; j < data->animations[i].channels_count; ++j)
1551                 {
1552                         cgltf_animation_channel* channel = &data->animations[i].channels[j];
1553
1554                         if (!channel->target_node)
1555                         {
1556                                 continue;
1557                         }
1558
1559                         cgltf_size components = 1;
1560
1561                         if (channel->target_path == cgltf_animation_path_type_weights)
1562                         {
1563                                 CGLTF_ASSERT_IF(!channel->target_node->mesh || !channel->target_node->mesh->primitives_count, cgltf_result_invalid_gltf);
1564
1565                                 components = channel->target_node->mesh->primitives[0].targets_count;
1566                         }
1567
1568                         cgltf_size values = channel->sampler->interpolation == cgltf_interpolation_type_cubic_spline ? 3 : 1;
1569
1570                         CGLTF_ASSERT_IF(channel->sampler->input->count * components * values != channel->sampler->output->count, cgltf_result_data_too_short);
1571                 }
1572         }
1573
1574         return cgltf_result_success;
1575 }
1576
1577 cgltf_result cgltf_copy_extras_json(const cgltf_data* data, const cgltf_extras* extras, char* dest, cgltf_size* dest_size)
1578 {
1579         cgltf_size json_size = extras->end_offset - extras->start_offset;
1580
1581         if (!dest)
1582         {
1583                 if (dest_size)
1584                 {
1585                         *dest_size = json_size + 1;
1586                         return cgltf_result_success;
1587                 }
1588                 return cgltf_result_invalid_options;
1589         }
1590
1591         if (*dest_size + 1 < json_size)
1592         {
1593                 strncpy(dest, data->json + extras->start_offset, *dest_size - 1);
1594                 dest[*dest_size - 1] = 0;
1595         }
1596         else
1597         {
1598                 strncpy(dest, data->json + extras->start_offset, json_size);
1599                 dest[json_size] = 0;
1600         }
1601
1602         return cgltf_result_success;
1603 }
1604
1605 void cgltf_free_extensions(cgltf_data* data, cgltf_extension* extensions, cgltf_size extensions_count)
1606 {
1607         for (cgltf_size i = 0; i < extensions_count; ++i)
1608         {
1609                 data->memory.free(data->memory.user_data, extensions[i].name);
1610                 data->memory.free(data->memory.user_data, extensions[i].data);
1611         }
1612         data->memory.free(data->memory.user_data, extensions);
1613 }
1614
1615 void cgltf_free(cgltf_data* data)
1616 {
1617         if (!data)
1618         {
1619                 return;
1620         }
1621
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;
1623
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);
1628
1629         cgltf_free_extensions(data, data->asset.extensions, data->asset.extensions_count);
1630
1631         for (cgltf_size i = 0; i < data->accessors_count; ++i)
1632         {
1633                 data->memory.free(data->memory.user_data, data->accessors[i].name);
1634
1635                 if(data->accessors[i].is_sparse)
1636                 {
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);
1640                 }
1641                 cgltf_free_extensions(data, data->accessors[i].extensions, data->accessors[i].extensions_count);
1642         }
1643         data->memory.free(data->memory.user_data, data->accessors);
1644
1645         for (cgltf_size i = 0; i < data->buffer_views_count; ++i)
1646         {
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);
1649
1650                 cgltf_free_extensions(data, data->buffer_views[i].extensions, data->buffer_views[i].extensions_count);
1651         }
1652         data->memory.free(data->memory.user_data, data->buffer_views);
1653
1654         for (cgltf_size i = 0; i < data->buffers_count; ++i)
1655         {
1656                 data->memory.free(data->memory.user_data, data->buffers[i].name);
1657
1658                 if (data->buffers[i].data != data->bin)
1659                 {
1660                         file_release(&data->memory, &data->file, data->buffers[i].data);
1661                 }
1662                 data->memory.free(data->memory.user_data, data->buffers[i].uri);
1663
1664                 cgltf_free_extensions(data, data->buffers[i].extensions, data->buffers[i].extensions_count);
1665         }
1666
1667         data->memory.free(data->memory.user_data, data->buffers);
1668
1669         for (cgltf_size i = 0; i < data->meshes_count; ++i)
1670         {
1671                 data->memory.free(data->memory.user_data, data->meshes[i].name);
1672
1673                 for (cgltf_size j = 0; j < data->meshes[i].primitives_count; ++j)
1674                 {
1675                         for (cgltf_size k = 0; k < data->meshes[i].primitives[j].attributes_count; ++k)
1676                         {
1677                                 data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].attributes[k].name);
1678                         }
1679
1680                         data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].attributes);
1681
1682                         for (cgltf_size k = 0; k < data->meshes[i].primitives[j].targets_count; ++k)
1683                         {
1684                                 for (cgltf_size m = 0; m < data->meshes[i].primitives[j].targets[k].attributes_count; ++m)
1685                                 {
1686                                         data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].targets[k].attributes[m].name);
1687                                 }
1688
1689                                 data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].targets[k].attributes);
1690                         }
1691
1692                         data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].targets);
1693
1694                         if (data->meshes[i].primitives[j].has_draco_mesh_compression)
1695                         {
1696                                 for (cgltf_size k = 0; k < data->meshes[i].primitives[j].draco_mesh_compression.attributes_count; ++k)
1697                                 {
1698                                         data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].draco_mesh_compression.attributes[k].name);
1699                                 }
1700
1701                                 data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].draco_mesh_compression.attributes);
1702                         }
1703
1704                         data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].mappings);
1705
1706                         cgltf_free_extensions(data, data->meshes[i].primitives[j].extensions, data->meshes[i].primitives[j].extensions_count);
1707                 }
1708
1709                 data->memory.free(data->memory.user_data, data->meshes[i].primitives);
1710                 data->memory.free(data->memory.user_data, data->meshes[i].weights);
1711
1712                 for (cgltf_size j = 0; j < data->meshes[i].target_names_count; ++j)
1713                 {
1714                         data->memory.free(data->memory.user_data, data->meshes[i].target_names[j]);
1715                 }
1716
1717                 cgltf_free_extensions(data, data->meshes[i].extensions, data->meshes[i].extensions_count);
1718
1719                 data->memory.free(data->memory.user_data, data->meshes[i].target_names);
1720         }
1721
1722         data->memory.free(data->memory.user_data, data->meshes);
1723
1724         for (cgltf_size i = 0; i < data->materials_count; ++i)
1725         {
1726                 data->memory.free(data->memory.user_data, data->materials[i].name);
1727
1728                 if(data->materials[i].has_pbr_metallic_roughness)
1729                 {
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);
1732                 }
1733                 if(data->materials[i].has_pbr_specular_glossiness)
1734                 {
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);
1737                 }
1738                 if(data->materials[i].has_clearcoat)
1739                 {
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);
1743                 }
1744                 if(data->materials[i].has_specular)
1745                 {
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);
1748                 }
1749                 if(data->materials[i].has_transmission)
1750                 {
1751                         cgltf_free_extensions(data, data->materials[i].transmission.transmission_texture.extensions, data->materials[i].transmission.transmission_texture.extensions_count);
1752                 }
1753                 if (data->materials[i].has_volume)
1754                 {
1755                         cgltf_free_extensions(data, data->materials[i].volume.thickness_texture.extensions, data->materials[i].volume.thickness_texture.extensions_count);
1756                 }
1757                 if(data->materials[i].has_sheen)
1758                 {
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);
1761                 }
1762
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);
1766
1767                 cgltf_free_extensions(data, data->materials[i].extensions, data->materials[i].extensions_count);
1768         }
1769
1770         data->memory.free(data->memory.user_data, data->materials);
1771
1772         for (cgltf_size i = 0; i < data->images_count; ++i) 
1773         {
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);
1777
1778                 cgltf_free_extensions(data, data->images[i].extensions, data->images[i].extensions_count);
1779         }
1780
1781         data->memory.free(data->memory.user_data, data->images);
1782
1783         for (cgltf_size i = 0; i < data->textures_count; ++i)
1784         {
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);
1787         }
1788
1789         data->memory.free(data->memory.user_data, data->textures);
1790
1791         for (cgltf_size i = 0; i < data->samplers_count; ++i)
1792         {
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);
1795         }
1796
1797         data->memory.free(data->memory.user_data, data->samplers);
1798
1799         for (cgltf_size i = 0; i < data->skins_count; ++i)
1800         {
1801                 data->memory.free(data->memory.user_data, data->skins[i].name);
1802                 data->memory.free(data->memory.user_data, data->skins[i].joints);
1803
1804                 cgltf_free_extensions(data, data->skins[i].extensions, data->skins[i].extensions_count);
1805         }
1806
1807         data->memory.free(data->memory.user_data, data->skins);
1808
1809         for (cgltf_size i = 0; i < data->cameras_count; ++i)
1810         {
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);
1813         }
1814
1815         data->memory.free(data->memory.user_data, data->cameras);
1816
1817         for (cgltf_size i = 0; i < data->lights_count; ++i)
1818         {
1819                 data->memory.free(data->memory.user_data, data->lights[i].name);
1820         }
1821
1822         data->memory.free(data->memory.user_data, data->lights);
1823
1824         for (cgltf_size i = 0; i < data->nodes_count; ++i)
1825         {
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);
1830         }
1831
1832         data->memory.free(data->memory.user_data, data->nodes);
1833
1834         for (cgltf_size i = 0; i < data->scenes_count; ++i)
1835         {
1836                 data->memory.free(data->memory.user_data, data->scenes[i].name);
1837                 data->memory.free(data->memory.user_data, data->scenes[i].nodes);
1838
1839                 cgltf_free_extensions(data, data->scenes[i].extensions, data->scenes[i].extensions_count);
1840         }
1841
1842         data->memory.free(data->memory.user_data, data->scenes);
1843
1844         for (cgltf_size i = 0; i < data->animations_count; ++i)
1845         {
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)
1848                 {
1849                         cgltf_free_extensions(data, data->animations[i].samplers[j].extensions, data->animations[i].samplers[j].extensions_count);
1850                 }
1851                 data->memory.free(data->memory.user_data, data->animations[i].samplers);
1852
1853                 for (cgltf_size j = 0; j <  data->animations[i].channels_count; ++j)
1854                 {
1855                         cgltf_free_extensions(data, data->animations[i].channels[j].extensions, data->animations[i].channels[j].extensions_count);
1856                 }
1857                 data->memory.free(data->memory.user_data, data->animations[i].channels);
1858
1859                 cgltf_free_extensions(data, data->animations[i].extensions, data->animations[i].extensions_count);
1860         }
1861
1862         data->memory.free(data->memory.user_data, data->animations);
1863
1864         for (cgltf_size i = 0; i < data->variants_count; ++i)
1865         {
1866                 data->memory.free(data->memory.user_data, data->variants[i].name);
1867         }
1868
1869         data->memory.free(data->memory.user_data, data->variants);
1870
1871         cgltf_free_extensions(data, data->data_extensions, data->data_extensions_count);
1872
1873         for (cgltf_size i = 0; i < data->extensions_used_count; ++i)
1874         {
1875                 data->memory.free(data->memory.user_data, data->extensions_used[i]);
1876         }
1877
1878         data->memory.free(data->memory.user_data, data->extensions_used);
1879
1880         for (cgltf_size i = 0; i < data->extensions_required_count; ++i)
1881         {
1882                 data->memory.free(data->memory.user_data, data->extensions_required[i]);
1883         }
1884
1885         data->memory.free(data->memory.user_data, data->extensions_required);
1886
1887         file_release(&data->memory, &data->file, data->file_data);
1888
1889         data->memory.free(data->memory.user_data, data);
1890 }
1891
1892 void cgltf_node_transform_local(const cgltf_node* node, cgltf_float* out_matrix)
1893 {
1894         cgltf_float* lm = out_matrix;
1895
1896         if (node->has_matrix)
1897         {
1898                 memcpy(lm, node->matrix, sizeof(float) * 16);
1899         }
1900         else
1901         {
1902                 float tx = node->translation[0];
1903                 float ty = node->translation[1];
1904                 float tz = node->translation[2];
1905
1906                 float qx = node->rotation[0];
1907                 float qy = node->rotation[1];
1908                 float qz = node->rotation[2];
1909                 float qw = node->rotation[3];
1910
1911                 float sx = node->scale[0];
1912                 float sy = node->scale[1];
1913                 float sz = node->scale[2];
1914
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;
1918                 lm[3] = 0.f;
1919
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;
1923                 lm[7] = 0.f;
1924
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;
1928                 lm[11] = 0.f;
1929
1930                 lm[12] = tx;
1931                 lm[13] = ty;
1932                 lm[14] = tz;
1933                 lm[15] = 1.f;
1934         }
1935 }
1936
1937 void cgltf_node_transform_world(const cgltf_node* node, cgltf_float* out_matrix)
1938 {
1939         cgltf_float* lm = out_matrix;
1940         cgltf_node_transform_local(node, lm);
1941
1942         const cgltf_node* parent = node->parent;
1943
1944         while (parent)
1945         {
1946                 float pm[16];
1947                 cgltf_node_transform_local(parent, pm);
1948
1949                 for (int i = 0; i < 4; ++i)
1950                 {
1951                         float l0 = lm[i * 4 + 0];
1952                         float l1 = lm[i * 4 + 1];
1953                         float l2 = lm[i * 4 + 2];
1954
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];
1958
1959                         lm[i * 4 + 0] = r0;
1960                         lm[i * 4 + 1] = r1;
1961                         lm[i * 4 + 2] = r2;
1962                 }
1963
1964                 lm[12] += pm[12];
1965                 lm[13] += pm[13];
1966                 lm[14] += pm[14];
1967
1968                 parent = parent->parent;
1969         }
1970 }
1971
1972 static cgltf_size cgltf_component_read_index(const void* in, cgltf_component_type component_type)
1973 {
1974         switch (component_type)
1975         {
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);
1988                 default:
1989                         return 0;
1990         }
1991 }
1992
1993 static cgltf_float cgltf_component_read_float(const void* in, cgltf_component_type component_type, cgltf_bool normalized)
1994 {
1995         if (component_type == cgltf_component_type_r_32f)
1996         {
1997                 return *((const float*) in);
1998         }
1999
2000         if (normalized)
2001         {
2002                 switch (component_type)
2003                 {
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;
2013                         default:
2014                                 return 0;
2015                 }
2016         }
2017
2018         return (cgltf_float)cgltf_component_read_index(in, component_type);
2019 }
2020
2021 static cgltf_size cgltf_component_size(cgltf_component_type component_type);
2022
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)
2024 {
2025         cgltf_size num_components = cgltf_num_components(type);
2026
2027         if (element_size < num_components) {
2028                 return 0;
2029         }
2030
2031         // There are three special cases for component extraction, see #data-alignment in the 2.0 spec.
2032
2033         cgltf_size component_size = cgltf_component_size(component_type);
2034
2035         if (type == cgltf_type_mat2 && component_size == 1)
2036         {
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);
2041                 return 1;
2042         }
2043
2044         if (type == cgltf_type_mat3 && component_size == 1)
2045         {
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);
2055                 return 1;
2056         }
2057
2058         if (type == cgltf_type_mat3 && component_size == 2)
2059         {
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);
2069                 return 1;
2070         }
2071
2072         for (cgltf_size i = 0; i < num_components; ++i)
2073         {
2074                 out[i] = cgltf_component_read_float(element + component_size * i, component_type, normalized);
2075         }
2076         return 1;
2077 }
2078
2079 const uint8_t* cgltf_buffer_view_data(const cgltf_buffer_view* view)
2080 {
2081         if (view->data)
2082                 return (const uint8_t*)view->data;
2083
2084         if (!view->buffer->data)
2085                 return NULL;
2086
2087         const uint8_t* result = (const uint8_t*)view->buffer->data;
2088         result += view->offset;
2089         return result;
2090 }
2091
2092 cgltf_bool cgltf_accessor_read_float(const cgltf_accessor* accessor, cgltf_size index, cgltf_float* out, cgltf_size element_size)
2093 {
2094         if (accessor->is_sparse)
2095         {
2096                 return 0;
2097         }
2098         if (accessor->buffer_view == NULL)
2099         {
2100                 memset(out, 0, element_size * sizeof(cgltf_float));
2101                 return 1;
2102         }
2103         const uint8_t* element = cgltf_buffer_view_data(accessor->buffer_view);
2104         if (element == NULL)
2105         {
2106                 return 0;
2107         }
2108         element += accessor->offset + accessor->stride * index;
2109         return cgltf_element_read_float(element, accessor->type, accessor->component_type, accessor->normalized, out, element_size);
2110 }
2111
2112 cgltf_size cgltf_accessor_unpack_floats(const cgltf_accessor* accessor, cgltf_float* out, cgltf_size float_count)
2113 {
2114         cgltf_size floats_per_element = cgltf_num_components(accessor->type);
2115         cgltf_size available_floats = accessor->count * floats_per_element;
2116         if (out == NULL)
2117         {
2118                 return available_floats;
2119         }
2120
2121         float_count = available_floats < float_count ? available_floats : float_count;
2122         cgltf_size element_count = float_count / floats_per_element;
2123
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)
2129         {
2130                 if (!cgltf_accessor_read_float(&dense, index, dest, floats_per_element))
2131                 {
2132                         return 0;
2133                 }
2134         }
2135
2136         // Second pass: write out each element in the sparse accessor.
2137         if (accessor->is_sparse)
2138         {
2139                 const cgltf_accessor_sparse* sparse = &dense.sparse;
2140
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);
2143
2144                 if (index_data == NULL || reader_head == NULL)
2145                 {
2146                         return 0;
2147                 }
2148
2149                 index_data += sparse->indices_byte_offset;
2150                 reader_head += sparse->values_byte_offset;
2151
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)
2154                 {
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;
2157
2158                         if (!cgltf_element_read_float(reader_head, dense.type, dense.component_type, dense.normalized, writer_head, floats_per_element))
2159                         {
2160                                 return 0;
2161                         }
2162
2163                         reader_head += dense.stride;
2164                 }
2165         }
2166
2167         return element_count * floats_per_element;
2168 }
2169
2170 static cgltf_uint cgltf_component_read_uint(const void* in, cgltf_component_type component_type)
2171 {
2172         switch (component_type)
2173         {
2174                 case cgltf_component_type_r_8:
2175                         return *((const int8_t*) in);
2176
2177                 case cgltf_component_type_r_8u:
2178                         return *((const uint8_t*) in);
2179
2180                 case cgltf_component_type_r_16:
2181                         return *((const int16_t*) in);
2182
2183                 case cgltf_component_type_r_16u:
2184                         return *((const uint16_t*) in);
2185
2186                 case cgltf_component_type_r_32u:
2187                         return *((const uint32_t*) in);
2188
2189                 default:
2190                         return 0;
2191         }
2192 }
2193
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)
2195 {
2196         cgltf_size num_components = cgltf_num_components(type);
2197
2198         if (element_size < num_components)
2199         {
2200                 return 0;
2201         }
2202
2203         // Reading integer matrices is not a valid use case
2204         if (type == cgltf_type_mat2 || type == cgltf_type_mat3 || type == cgltf_type_mat4)
2205         {
2206                 return 0;
2207         }
2208
2209         cgltf_size component_size = cgltf_component_size(component_type);
2210
2211         for (cgltf_size i = 0; i < num_components; ++i)
2212         {
2213                 out[i] = cgltf_component_read_uint(element + component_size * i, component_type);
2214         }
2215         return 1;
2216 }
2217
2218 cgltf_bool cgltf_accessor_read_uint(const cgltf_accessor* accessor, cgltf_size index, cgltf_uint* out, cgltf_size element_size)
2219 {
2220         if (accessor->is_sparse)
2221         {
2222                 return 0;
2223         }
2224         if (accessor->buffer_view == NULL)
2225         {
2226                 memset(out, 0, element_size * sizeof( cgltf_uint ));
2227                 return 1;
2228         }
2229         const uint8_t* element = cgltf_buffer_view_data(accessor->buffer_view);
2230         if (element == NULL)
2231         {
2232                 return 0;
2233         }
2234         element += accessor->offset + accessor->stride * index;
2235         return cgltf_element_read_uint(element, accessor->type, accessor->component_type, out, element_size);
2236 }
2237
2238 cgltf_size cgltf_accessor_read_index(const cgltf_accessor* accessor, cgltf_size index)
2239 {
2240         if (accessor->is_sparse)
2241         {
2242                 return 0; // This is an error case, but we can't communicate the error with existing interface.
2243         }
2244         if (accessor->buffer_view == NULL)
2245         {
2246                 return 0;
2247         }
2248         const uint8_t* element = cgltf_buffer_view_data(accessor->buffer_view);
2249         if (element == NULL)
2250         {
2251                 return 0; // This is an error case, but we can't communicate the error with existing interface.
2252         }
2253         element += accessor->offset + accessor->stride * index;
2254         return cgltf_component_read_index(element, accessor->component_type);
2255 }
2256
2257 #define CGLTF_ERROR_JSON -1
2258 #define CGLTF_ERROR_NOMEM -2
2259 #define CGLTF_ERROR_LEGACY -3
2260
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 */
2263
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];
2267
2268 static int cgltf_json_strcmp(jsmntok_t const* tok, const uint8_t* json_chunk, const char* str)
2269 {
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;
2274 }
2275
2276 static int cgltf_json_to_int(jsmntok_t const* tok, const uint8_t* json_chunk)
2277 {
2278         CGLTF_CHECK_TOKTYPE(*tok, JSMN_PRIMITIVE);
2279         char tmp[128];
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);
2282         tmp[size] = 0;
2283         return CGLTF_ATOI(tmp);
2284 }
2285
2286 static cgltf_float cgltf_json_to_float(jsmntok_t const* tok, const uint8_t* json_chunk)
2287 {
2288         CGLTF_CHECK_TOKTYPE(*tok, JSMN_PRIMITIVE);
2289         char tmp[128];
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);
2292         tmp[size] = 0;
2293         return (cgltf_float)CGLTF_ATOF(tmp);
2294 }
2295
2296 static cgltf_bool cgltf_json_to_bool(jsmntok_t const* tok, const uint8_t* json_chunk)
2297 {
2298         int size = tok->end - tok->start;
2299         return size == 4 && memcmp(json_chunk + tok->start, "true", 4) == 0;
2300 }
2301
2302 static int cgltf_skip_json(jsmntok_t const* tokens, int i)
2303 {
2304         int end = i + 1;
2305
2306         while (i < end)
2307         {
2308                 switch (tokens[i].type)
2309                 {
2310                 case JSMN_OBJECT:
2311                         end += tokens[i].size * 2;
2312                         break;
2313
2314                 case JSMN_ARRAY:
2315                         end += tokens[i].size;
2316                         break;
2317
2318                 case JSMN_PRIMITIVE:
2319                 case JSMN_STRING:
2320                         break;
2321
2322                 default:
2323                         return -1;
2324                 }
2325
2326                 i++;
2327         }
2328
2329         return i;
2330 }
2331
2332 static void cgltf_fill_float_array(float* out_array, int size, float value)
2333 {
2334         for (int j = 0; j < size; ++j)
2335         {
2336                 out_array[j] = value;
2337         }
2338 }
2339
2340 static int cgltf_parse_json_float_array(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, float* out_array, int size)
2341 {
2342         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_ARRAY);
2343         if (tokens[i].size != size)
2344         {
2345                 return CGLTF_ERROR_JSON;
2346         }
2347         ++i;
2348         for (int j = 0; j < size; ++j)
2349         {
2350                 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
2351                 out_array[j] = cgltf_json_to_float(tokens + i, json_chunk);
2352                 ++i;
2353         }
2354         return i;
2355 }
2356
2357 static int cgltf_parse_json_string(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, char** out_string)
2358 {
2359         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_STRING);
2360         if (*out_string)
2361         {
2362                 return CGLTF_ERROR_JSON;
2363         }
2364         int size = tokens[i].end - tokens[i].start;
2365         char* result = (char*)options->memory.alloc(options->memory.user_data, size + 1);
2366         if (!result)
2367         {
2368                 return CGLTF_ERROR_NOMEM;
2369         }
2370         strncpy(result, (const char*)json_chunk + tokens[i].start, size);
2371         result[size] = 0;
2372         *out_string = result;
2373         return i + 1;
2374 }
2375
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)
2377 {
2378         (void)json_chunk;
2379         if (tokens[i].type != JSMN_ARRAY)
2380         {
2381                 return tokens[i].type == JSMN_OBJECT ? CGLTF_ERROR_LEGACY : CGLTF_ERROR_JSON;
2382         }
2383         if (*out_array)
2384         {
2385                 return CGLTF_ERROR_JSON;
2386         }
2387         int size = tokens[i].size;
2388         void* result = cgltf_calloc(options, element_size, size);
2389         if (!result)
2390         {
2391                 return CGLTF_ERROR_NOMEM;
2392         }
2393         *out_array = result;
2394         *out_size = size;
2395         return i + 1;
2396 }
2397
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)
2399 {
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);
2402         if (i < 0)
2403         {
2404                 return i;
2405         }
2406
2407         for (cgltf_size j = 0; j < *out_size; ++j)
2408         {
2409                 i = cgltf_parse_json_string(options, tokens, i, json_chunk, j + (*out_array));
2410                 if (i < 0)
2411                 {
2412                         return i;
2413                 }
2414         }
2415         return i;
2416 }
2417
2418 static void cgltf_parse_attribute_type(const char* name, cgltf_attribute_type* out_type, int* out_index)
2419 {
2420         const char* us = strchr(name, '_');
2421         size_t len = us ? (size_t)(us - name) : strlen(name);
2422
2423         if (len == 8 && strncmp(name, "POSITION", 8) == 0)
2424         {
2425                 *out_type = cgltf_attribute_type_position;
2426         }
2427         else if (len == 6 && strncmp(name, "NORMAL", 6) == 0)
2428         {
2429                 *out_type = cgltf_attribute_type_normal;
2430         }
2431         else if (len == 7 && strncmp(name, "TANGENT", 7) == 0)
2432         {
2433                 *out_type = cgltf_attribute_type_tangent;
2434         }
2435         else if (len == 8 && strncmp(name, "TEXCOORD", 8) == 0)
2436         {
2437                 *out_type = cgltf_attribute_type_texcoord;
2438         }
2439         else if (len == 5 && strncmp(name, "COLOR", 5) == 0)
2440         {
2441                 *out_type = cgltf_attribute_type_color;
2442         }
2443         else if (len == 6 && strncmp(name, "JOINTS", 6) == 0)
2444         {
2445                 *out_type = cgltf_attribute_type_joints;
2446         }
2447         else if (len == 7 && strncmp(name, "WEIGHTS", 7) == 0)
2448         {
2449                 *out_type = cgltf_attribute_type_weights;
2450         }
2451         else
2452         {
2453                 *out_type = cgltf_attribute_type_invalid;
2454         }
2455
2456         if (us && *out_type != cgltf_attribute_type_invalid)
2457         {
2458                 *out_index = CGLTF_ATOI(us + 1);
2459         }
2460 }
2461
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)
2463 {
2464         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
2465
2466         if (*out_attributes)
2467         {
2468                 return CGLTF_ERROR_JSON;
2469         }
2470
2471         *out_attributes_count = tokens[i].size;
2472         *out_attributes = (cgltf_attribute*)cgltf_calloc(options, sizeof(cgltf_attribute), *out_attributes_count);
2473         ++i;
2474
2475         if (!*out_attributes)
2476         {
2477                 return CGLTF_ERROR_NOMEM;
2478         }
2479
2480         for (cgltf_size j = 0; j < *out_attributes_count; ++j)
2481         {
2482                 CGLTF_CHECK_KEY(tokens[i]);
2483
2484                 i = cgltf_parse_json_string(options, tokens, i, json_chunk, &(*out_attributes)[j].name);
2485                 if (i < 0)
2486                 {
2487                         return CGLTF_ERROR_JSON;
2488                 }
2489
2490                 cgltf_parse_attribute_type((*out_attributes)[j].name, &(*out_attributes)[j].type, &(*out_attributes)[j].index);
2491
2492                 (*out_attributes)[j].data = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
2493                 ++i;
2494         }
2495
2496         return i;
2497 }
2498
2499 static int cgltf_parse_json_extras(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_extras* out_extras)
2500 {
2501         (void)json_chunk;
2502         out_extras->start_offset = tokens[i].start;
2503         out_extras->end_offset = tokens[i].end;
2504         i = cgltf_skip_json(tokens, i);
2505         return i;
2506 }
2507
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)
2509 {
2510         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_STRING);
2511         CGLTF_CHECK_TOKTYPE(tokens[i+1], JSMN_OBJECT);
2512         if (out_extension->name)
2513         {
2514                 return CGLTF_ERROR_JSON;
2515         }
2516
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)
2520         {
2521                 return CGLTF_ERROR_NOMEM;
2522         }
2523         strncpy(out_extension->name, (const char*)json_chunk + tokens[i].start, name_length);
2524         out_extension->name[name_length] = 0;
2525         i++;
2526
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)
2531         {
2532                 return CGLTF_ERROR_NOMEM;
2533         }
2534         strncpy(out_extension->data, (const char*)json_chunk + start, size);
2535         out_extension->data[size] = '\0';
2536
2537         i = cgltf_skip_json(tokens, i);
2538
2539         return i;
2540 }
2541
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)
2543 {
2544         ++i;
2545
2546         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
2547         if(*out_extensions)
2548         {
2549                 return CGLTF_ERROR_JSON;
2550         }
2551
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);
2555
2556         if (!*out_extensions)
2557         {
2558                 return CGLTF_ERROR_NOMEM;
2559         }
2560
2561         ++i;
2562
2563         for (int j = 0; j < extensions_size; ++j)
2564         {
2565                 CGLTF_CHECK_KEY(tokens[i]);
2566
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);
2570
2571                 if (i < 0)
2572                 {
2573                         return i;
2574                 }
2575         }
2576         return i;
2577 }
2578
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)
2580 {
2581         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
2582
2583         int size = tokens[i].size;
2584         ++i;
2585
2586         for (int j = 0; j < size; ++j)
2587         {
2588                 CGLTF_CHECK_KEY(tokens[i]);
2589
2590                 if (cgltf_json_strcmp(tokens + i, json_chunk, "attributes") == 0)
2591                 {
2592                         i = cgltf_parse_json_attribute_list(options, tokens, i + 1, json_chunk, &out_draco_mesh_compression->attributes, &out_draco_mesh_compression->attributes_count);
2593                 }
2594                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "bufferView") == 0)
2595                 {
2596                         ++i;
2597                         out_draco_mesh_compression->buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
2598                         ++i;
2599                 }
2600
2601                 if (i < 0)
2602                 {
2603                         return i;
2604                 }
2605         }
2606
2607         return i;
2608 }
2609
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)
2611 {
2612         (void)options;
2613         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_ARRAY);
2614
2615         int size = tokens[i].size;
2616         ++i;
2617
2618         for (int j = 0; j < size; ++j)
2619         {
2620                 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
2621
2622                 int obj_size = tokens[i].size;
2623                 ++i;
2624
2625                 int material = -1;
2626                 int variants_tok = -1;
2627                 cgltf_extras extras = {0, 0};
2628
2629                 for (int k = 0; k < obj_size; ++k)
2630                 {
2631                         CGLTF_CHECK_KEY(tokens[i]);
2632
2633                         if (cgltf_json_strcmp(tokens + i, json_chunk, "material") == 0)
2634                         {
2635                                 ++i;
2636                                 material = cgltf_json_to_int(tokens + i, json_chunk);
2637                                 ++i;
2638                         }
2639                         else if (cgltf_json_strcmp(tokens + i, json_chunk, "variants") == 0)
2640                         {
2641                                 variants_tok = i+1;
2642                                 CGLTF_CHECK_TOKTYPE(tokens[variants_tok], JSMN_ARRAY);
2643
2644                                 i = cgltf_skip_json(tokens, i+1);
2645                         }
2646                         else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
2647                         {
2648                                 i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &extras);
2649                         }
2650                         else
2651                         {
2652                                 i = cgltf_skip_json(tokens, i+1);
2653                         }
2654
2655                         if (i < 0)
2656                         {
2657                                 return i;
2658                         }
2659                 }
2660
2661                 if (material < 0 || variants_tok < 0)
2662                 {
2663                         return CGLTF_ERROR_JSON;
2664                 }
2665
2666                 if (out_mappings)
2667                 {
2668                         for (int k = 0; k < tokens[variants_tok].size; ++k)
2669                         {
2670                                 int variant = cgltf_json_to_int(&tokens[variants_tok + 1 + k], json_chunk);
2671                                 if (variant < 0)
2672                                         return variant;
2673
2674                                 out_mappings[*offset].material = CGLTF_PTRINDEX(cgltf_material, material);
2675                                 out_mappings[*offset].variant = variant;
2676                                 out_mappings[*offset].extras = extras;
2677
2678                                 (*offset)++;
2679                         }
2680                 }
2681                 else
2682                 {
2683                         (*offset) += tokens[variants_tok].size;
2684                 }
2685         }
2686
2687         return i;
2688 }
2689
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)
2691 {
2692         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
2693
2694         int size = tokens[i].size;
2695         ++i;
2696
2697         for (int j = 0; j < size; ++j)
2698         {
2699                 CGLTF_CHECK_KEY(tokens[i]);
2700
2701                 if (cgltf_json_strcmp(tokens + i, json_chunk, "mappings") == 0)
2702                 {
2703                         if (out_prim->mappings)
2704                         {
2705                                 return CGLTF_ERROR_JSON;
2706                         }
2707
2708                         cgltf_size mappings_offset = 0;
2709                         int k = cgltf_parse_json_material_mapping_data(options, tokens, i + 1, json_chunk, NULL, &mappings_offset);
2710                         if (k < 0)
2711                         {
2712                                 return k;
2713                         }
2714
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);
2717
2718                         mappings_offset = 0;
2719                         i = cgltf_parse_json_material_mapping_data(options, tokens, i + 1, json_chunk, out_prim->mappings, &mappings_offset);
2720                 }
2721                 else
2722                 {
2723                         i = cgltf_skip_json(tokens, i+1);
2724                 }
2725
2726                 if (i < 0)
2727                 {
2728                         return i;
2729                 }
2730         }
2731
2732         return i;
2733 }
2734
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)
2736 {
2737         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
2738
2739         out_prim->type = cgltf_primitive_type_triangles;
2740
2741         int size = tokens[i].size;
2742         ++i;
2743
2744         for (int j = 0; j < size; ++j)
2745         {
2746                 CGLTF_CHECK_KEY(tokens[i]);
2747
2748                 if (cgltf_json_strcmp(tokens+i, json_chunk, "mode") == 0)
2749                 {
2750                         ++i;
2751                         out_prim->type
2752                                         = (cgltf_primitive_type)
2753                                         cgltf_json_to_int(tokens+i, json_chunk);
2754                         ++i;
2755                 }
2756                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "indices") == 0)
2757                 {
2758                         ++i;
2759                         out_prim->indices = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
2760                         ++i;
2761                 }
2762                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "material") == 0)
2763                 {
2764                         ++i;
2765                         out_prim->material = CGLTF_PTRINDEX(cgltf_material, cgltf_json_to_int(tokens + i, json_chunk));
2766                         ++i;
2767                 }
2768                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "attributes") == 0)
2769                 {
2770                         i = cgltf_parse_json_attribute_list(options, tokens, i + 1, json_chunk, &out_prim->attributes, &out_prim->attributes_count);
2771                 }
2772                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "targets") == 0)
2773                 {
2774                         i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_morph_target), (void**)&out_prim->targets, &out_prim->targets_count);
2775                         if (i < 0)
2776                         {
2777                                 return i;
2778                         }
2779
2780                         for (cgltf_size k = 0; k < out_prim->targets_count; ++k)
2781                         {
2782                                 i = cgltf_parse_json_attribute_list(options, tokens, i, json_chunk, &out_prim->targets[k].attributes, &out_prim->targets[k].attributes_count);
2783                                 if (i < 0)
2784                                 {
2785                                         return i;
2786                                 }
2787                         }
2788                 }
2789                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
2790                 {
2791                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_prim->extras);
2792                 }
2793                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
2794                 {
2795                         ++i;
2796
2797                         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
2798                         if(out_prim->extensions)
2799                         {
2800                                 return CGLTF_ERROR_JSON;
2801                         }
2802
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);
2806
2807                         if (!out_prim->extensions)
2808                         {
2809                                 return CGLTF_ERROR_NOMEM;
2810                         }
2811
2812                         ++i;
2813                         for (int k = 0; k < extensions_size; ++k)
2814                         {
2815                                 CGLTF_CHECK_KEY(tokens[i]);
2816
2817                                 if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_draco_mesh_compression") == 0)
2818                                 {
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);
2821                                 }
2822                                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_variants") == 0)
2823                                 {
2824                                         i = cgltf_parse_json_material_mappings(options, tokens, i + 1, json_chunk, out_prim);
2825                                 }
2826                                 else
2827                                 {
2828                                         i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_prim->extensions[out_prim->extensions_count++]));
2829                                 }
2830
2831                                 if (i < 0)
2832                                 {
2833                                         return i;
2834                                 }
2835                         }
2836                 }
2837                 else
2838                 {
2839                         i = cgltf_skip_json(tokens, i+1);
2840                 }
2841
2842                 if (i < 0)
2843                 {
2844                         return i;
2845                 }
2846         }
2847
2848         return i;
2849 }
2850
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)
2852 {
2853         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
2854
2855         int size = tokens[i].size;
2856         ++i;
2857
2858         for (int j = 0; j < size; ++j)
2859         {
2860                 CGLTF_CHECK_KEY(tokens[i]);
2861
2862                 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
2863                 {
2864                         i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_mesh->name);
2865                 }
2866                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "primitives") == 0)
2867                 {
2868                         i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_primitive), (void**)&out_mesh->primitives, &out_mesh->primitives_count);
2869                         if (i < 0)
2870                         {
2871                                 return i;
2872                         }
2873
2874                         for (cgltf_size prim_index = 0; prim_index < out_mesh->primitives_count; ++prim_index)
2875                         {
2876                                 i = cgltf_parse_json_primitive(options, tokens, i, json_chunk, &out_mesh->primitives[prim_index]);
2877                                 if (i < 0)
2878                                 {
2879                                         return i;
2880                                 }
2881                         }
2882                 }
2883                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "weights") == 0)
2884                 {
2885                         i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_float), (void**)&out_mesh->weights, &out_mesh->weights_count);
2886                         if (i < 0)
2887                         {
2888                                 return i;
2889                         }
2890
2891                         i = cgltf_parse_json_float_array(tokens, i - 1, json_chunk, out_mesh->weights, (int)out_mesh->weights_count);
2892                 }
2893                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
2894                 {
2895                         ++i;
2896
2897                         out_mesh->extras.start_offset = tokens[i].start;
2898                         out_mesh->extras.end_offset = tokens[i].end;
2899
2900                         if (tokens[i].type == JSMN_OBJECT)
2901                         {
2902                                 int extras_size = tokens[i].size;
2903                                 ++i;
2904
2905                                 for (int k = 0; k < extras_size; ++k)
2906                                 {
2907                                         CGLTF_CHECK_KEY(tokens[i]);
2908
2909                                         if (cgltf_json_strcmp(tokens+i, json_chunk, "targetNames") == 0 && tokens[i+1].type == JSMN_ARRAY)
2910                                         {
2911                                                 i = cgltf_parse_json_string_array(options, tokens, i + 1, json_chunk, &out_mesh->target_names, &out_mesh->target_names_count);
2912                                         }
2913                                         else
2914                                         {
2915                                                 i = cgltf_skip_json(tokens, i+1);
2916                                         }
2917
2918                                         if (i < 0)
2919                                         {
2920                                                 return i;
2921                                         }
2922                                 }
2923                         }
2924                         else
2925                         {
2926                                 i = cgltf_skip_json(tokens, i);
2927                         }
2928                 }
2929                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
2930                 {
2931                         i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_mesh->extensions_count, &out_mesh->extensions);
2932                 }
2933                 else
2934                 {
2935                         i = cgltf_skip_json(tokens, i+1);
2936                 }
2937
2938                 if (i < 0)
2939                 {
2940                         return i;
2941                 }
2942         }
2943
2944         return i;
2945 }
2946
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)
2948 {
2949         i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_mesh), (void**)&out_data->meshes, &out_data->meshes_count);
2950         if (i < 0)
2951         {
2952                 return i;
2953         }
2954
2955         for (cgltf_size j = 0; j < out_data->meshes_count; ++j)
2956         {
2957                 i = cgltf_parse_json_mesh(options, tokens, i, json_chunk, &out_data->meshes[j]);
2958                 if (i < 0)
2959                 {
2960                         return i;
2961                 }
2962         }
2963         return i;
2964 }
2965
2966 static cgltf_component_type cgltf_json_to_component_type(jsmntok_t const* tok, const uint8_t* json_chunk)
2967 {
2968         int type = cgltf_json_to_int(tok, json_chunk);
2969
2970         switch (type)
2971         {
2972         case 5120:
2973                 return cgltf_component_type_r_8;
2974         case 5121:
2975                 return cgltf_component_type_r_8u;
2976         case 5122:
2977                 return cgltf_component_type_r_16;
2978         case 5123:
2979                 return cgltf_component_type_r_16u;
2980         case 5125:
2981                 return cgltf_component_type_r_32u;
2982         case 5126:
2983                 return cgltf_component_type_r_32f;
2984         default:
2985                 return cgltf_component_type_invalid;
2986         }
2987 }
2988
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)
2990 {
2991         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
2992
2993         int size = tokens[i].size;
2994         ++i;
2995
2996         for (int j = 0; j < size; ++j)
2997         {
2998                 CGLTF_CHECK_KEY(tokens[i]);
2999
3000                 if (cgltf_json_strcmp(tokens+i, json_chunk, "count") == 0)
3001                 {
3002                         ++i;
3003                         out_sparse->count = cgltf_json_to_int(tokens + i, json_chunk);
3004                         ++i;
3005                 }
3006                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "indices") == 0)
3007                 {
3008                         ++i;
3009                         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3010
3011                         int indices_size = tokens[i].size;
3012                         ++i;
3013
3014                         for (int k = 0; k < indices_size; ++k)
3015                         {
3016                                 CGLTF_CHECK_KEY(tokens[i]);
3017
3018                                 if (cgltf_json_strcmp(tokens+i, json_chunk, "bufferView") == 0)
3019                                 {
3020                                         ++i;
3021                                         out_sparse->indices_buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
3022                                         ++i;
3023                                 }
3024                                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
3025                                 {
3026                                         ++i;
3027                                         out_sparse->indices_byte_offset = cgltf_json_to_int(tokens + i, json_chunk);
3028                                         ++i;
3029                                 }
3030                                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "componentType") == 0)
3031                                 {
3032                                         ++i;
3033                                         out_sparse->indices_component_type = cgltf_json_to_component_type(tokens + i, json_chunk);
3034                                         ++i;
3035                                 }
3036                                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3037                                 {
3038                                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_sparse->indices_extras);
3039                                 }
3040                                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
3041                                 {
3042                                         i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_sparse->indices_extensions_count, &out_sparse->indices_extensions);
3043                                 }
3044                                 else
3045                                 {
3046                                         i = cgltf_skip_json(tokens, i+1);
3047                                 }
3048
3049                                 if (i < 0)
3050                                 {
3051                                         return i;
3052                                 }
3053                         }
3054                 }
3055                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "values") == 0)
3056                 {
3057                         ++i;
3058                         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3059
3060                         int values_size = tokens[i].size;
3061                         ++i;
3062
3063                         for (int k = 0; k < values_size; ++k)
3064                         {
3065                                 CGLTF_CHECK_KEY(tokens[i]);
3066
3067                                 if (cgltf_json_strcmp(tokens+i, json_chunk, "bufferView") == 0)
3068                                 {
3069                                         ++i;
3070                                         out_sparse->values_buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
3071                                         ++i;
3072                                 }
3073                                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
3074                                 {
3075                                         ++i;
3076                                         out_sparse->values_byte_offset = cgltf_json_to_int(tokens + i, json_chunk);
3077                                         ++i;
3078                                 }
3079                                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3080                                 {
3081                                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_sparse->values_extras);
3082                                 }
3083                                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
3084                                 {
3085                                         i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_sparse->values_extensions_count, &out_sparse->values_extensions);
3086                                 }
3087                                 else
3088                                 {
3089                                         i = cgltf_skip_json(tokens, i+1);
3090                                 }
3091
3092                                 if (i < 0)
3093                                 {
3094                                         return i;
3095                                 }
3096                         }
3097                 }
3098                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3099                 {
3100                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_sparse->extras);
3101                 }
3102                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
3103                 {
3104                         i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_sparse->extensions_count, &out_sparse->extensions);
3105                 }
3106                 else
3107                 {
3108                         i = cgltf_skip_json(tokens, i+1);
3109                 }
3110
3111                 if (i < 0)
3112                 {
3113                         return i;
3114                 }
3115         }
3116
3117         return i;
3118 }
3119
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)
3121 {
3122         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3123
3124         int size = tokens[i].size;
3125         ++i;
3126
3127         for (int j = 0; j < size; ++j)
3128         {
3129                 CGLTF_CHECK_KEY(tokens[i]);
3130
3131                 if (cgltf_json_strcmp(tokens + i, json_chunk, "name") == 0)
3132                 {
3133                         i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_accessor->name);
3134                 }
3135                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "bufferView") == 0)
3136                 {
3137                         ++i;
3138                         out_accessor->buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
3139                         ++i;
3140                 }
3141                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
3142                 {
3143                         ++i;
3144                         out_accessor->offset =
3145                                         cgltf_json_to_int(tokens+i, json_chunk);
3146                         ++i;
3147                 }
3148                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "componentType") == 0)
3149                 {
3150                         ++i;
3151                         out_accessor->component_type = cgltf_json_to_component_type(tokens + i, json_chunk);
3152                         ++i;
3153                 }
3154                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "normalized") == 0)
3155                 {
3156                         ++i;
3157                         out_accessor->normalized = cgltf_json_to_bool(tokens+i, json_chunk);
3158                         ++i;
3159                 }
3160                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "count") == 0)
3161                 {
3162                         ++i;
3163                         out_accessor->count =
3164                                         cgltf_json_to_int(tokens+i, json_chunk);
3165                         ++i;
3166                 }
3167                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "type") == 0)
3168                 {
3169                         ++i;
3170                         if (cgltf_json_strcmp(tokens+i, json_chunk, "SCALAR") == 0)
3171                         {
3172                                 out_accessor->type = cgltf_type_scalar;
3173                         }
3174                         else if (cgltf_json_strcmp(tokens+i, json_chunk, "VEC2") == 0)
3175                         {
3176                                 out_accessor->type = cgltf_type_vec2;
3177                         }
3178                         else if (cgltf_json_strcmp(tokens+i, json_chunk, "VEC3") == 0)
3179                         {
3180                                 out_accessor->type = cgltf_type_vec3;
3181                         }
3182                         else if (cgltf_json_strcmp(tokens+i, json_chunk, "VEC4") == 0)
3183                         {
3184                                 out_accessor->type = cgltf_type_vec4;
3185                         }
3186                         else if (cgltf_json_strcmp(tokens+i, json_chunk, "MAT2") == 0)
3187                         {
3188                                 out_accessor->type = cgltf_type_mat2;
3189                         }
3190                         else if (cgltf_json_strcmp(tokens+i, json_chunk, "MAT3") == 0)
3191                         {
3192                                 out_accessor->type = cgltf_type_mat3;
3193                         }
3194                         else if (cgltf_json_strcmp(tokens+i, json_chunk, "MAT4") == 0)
3195                         {
3196                                 out_accessor->type = cgltf_type_mat4;
3197                         }
3198                         ++i;
3199                 }
3200                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "min") == 0)
3201                 {
3202                         ++i;
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);
3207                 }
3208                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "max") == 0)
3209                 {
3210                         ++i;
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);
3215                 }
3216                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "sparse") == 0)
3217                 {
3218                         out_accessor->is_sparse = 1;
3219                         i = cgltf_parse_json_accessor_sparse(options, tokens, i + 1, json_chunk, &out_accessor->sparse);
3220                 }
3221                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3222                 {
3223                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_accessor->extras);
3224                 }
3225                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
3226                 {
3227                         i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_accessor->extensions_count, &out_accessor->extensions);
3228                 }
3229                 else
3230                 {
3231                         i = cgltf_skip_json(tokens, i+1);
3232                 }
3233
3234                 if (i < 0)
3235                 {
3236                         return i;
3237                 }
3238         }
3239
3240         return i;
3241 }
3242
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)
3244 {
3245         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3246
3247         int size = tokens[i].size;
3248         ++i;
3249
3250         for (int j = 0; j < size; ++j)
3251         {
3252                 CGLTF_CHECK_KEY(tokens[i]);
3253
3254                 if (cgltf_json_strcmp(tokens + i, json_chunk, "offset") == 0)
3255                 {
3256                         i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_texture_transform->offset, 2);
3257                 }
3258                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "rotation") == 0)
3259                 {
3260                         ++i;
3261                         out_texture_transform->rotation = cgltf_json_to_float(tokens + i, json_chunk);
3262                         ++i;
3263                 }
3264                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "scale") == 0)
3265                 {
3266                         i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_texture_transform->scale, 2);
3267                 }
3268                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "texCoord") == 0)
3269                 {
3270                         ++i;
3271                         out_texture_transform->texcoord = cgltf_json_to_int(tokens + i, json_chunk);
3272                         ++i;
3273                 }
3274                 else
3275                 {
3276                         i = cgltf_skip_json(tokens, i + 1);
3277                 }
3278
3279                 if (i < 0)
3280                 {
3281                         return i;
3282                 }
3283         }
3284
3285         return i;
3286 }
3287
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)
3289 {
3290         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3291
3292         out_texture_view->scale = 1.0f;
3293         cgltf_fill_float_array(out_texture_view->transform.scale, 2, 1.0f);
3294
3295         int size = tokens[i].size;
3296         ++i;
3297
3298         for (int j = 0; j < size; ++j)
3299         {
3300                 CGLTF_CHECK_KEY(tokens[i]);
3301
3302                 if (cgltf_json_strcmp(tokens + i, json_chunk, "index") == 0)
3303                 {
3304                         ++i;
3305                         out_texture_view->texture = CGLTF_PTRINDEX(cgltf_texture, cgltf_json_to_int(tokens + i, json_chunk));
3306                         ++i;
3307                 }
3308                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "texCoord") == 0)
3309                 {
3310                         ++i;
3311                         out_texture_view->texcoord = cgltf_json_to_int(tokens + i, json_chunk);
3312                         ++i;
3313                 }
3314                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "scale") == 0) 
3315                 {
3316                         ++i;
3317                         out_texture_view->scale = cgltf_json_to_float(tokens + i, json_chunk);
3318                         ++i;
3319                 }
3320                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "strength") == 0)
3321                 {
3322                         ++i;
3323                         out_texture_view->scale = cgltf_json_to_float(tokens + i, json_chunk);
3324                         ++i;
3325                 }
3326                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3327                 {
3328                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_texture_view->extras);
3329                 }
3330                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
3331                 {
3332                         ++i;
3333
3334                         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3335                         if(out_texture_view->extensions)
3336                         {
3337                                 return CGLTF_ERROR_JSON;
3338                         }
3339
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);
3343
3344                         if (!out_texture_view->extensions)
3345                         {
3346                                 return CGLTF_ERROR_NOMEM;
3347                         }
3348
3349                         ++i;
3350
3351                         for (int k = 0; k < extensions_size; ++k)
3352                         {
3353                                 CGLTF_CHECK_KEY(tokens[i]);
3354
3355                                 if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_texture_transform") == 0)
3356                                 {
3357                                         out_texture_view->has_transform = 1;
3358                                         i = cgltf_parse_json_texture_transform(tokens, i + 1, json_chunk, &out_texture_view->transform);
3359                                 }
3360                                 else
3361                                 {
3362                                         i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_texture_view->extensions[out_texture_view->extensions_count++]));
3363                                 }
3364
3365                                 if (i < 0)
3366                                 {
3367                                         return i;
3368                                 }
3369                         }
3370                 }
3371                 else
3372                 {
3373                         i = cgltf_skip_json(tokens, i + 1);
3374                 }
3375
3376                 if (i < 0)
3377                 {
3378                         return i;
3379                 }
3380         }
3381
3382         return i;
3383 }
3384
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)
3386 {
3387         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3388
3389         int size = tokens[i].size;
3390         ++i;
3391
3392         for (int j = 0; j < size; ++j)
3393         {
3394                 CGLTF_CHECK_KEY(tokens[i]);
3395
3396                 if (cgltf_json_strcmp(tokens+i, json_chunk, "metallicFactor") == 0)
3397                 {
3398                         ++i;
3399                         out_pbr->metallic_factor = 
3400                                 cgltf_json_to_float(tokens + i, json_chunk);
3401                         ++i;
3402                 }
3403                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "roughnessFactor") == 0) 
3404                 {
3405                         ++i;
3406                         out_pbr->roughness_factor =
3407                                 cgltf_json_to_float(tokens+i, json_chunk);
3408                         ++i;
3409                 }
3410                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "baseColorFactor") == 0)
3411                 {
3412                         i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_pbr->base_color_factor, 4);
3413                 }
3414                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "baseColorTexture") == 0)
3415                 {
3416                         i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk,
3417                                 &out_pbr->base_color_texture);
3418                 }
3419                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "metallicRoughnessTexture") == 0)
3420                 {
3421                         i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk,
3422                                 &out_pbr->metallic_roughness_texture);
3423                 }
3424                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3425                 {
3426                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_pbr->extras);
3427                 }
3428                 else
3429                 {
3430                         i = cgltf_skip_json(tokens, i+1);
3431                 }
3432
3433                 if (i < 0)
3434                 {
3435                         return i;
3436                 }
3437         }
3438
3439         return i;
3440 }
3441
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)
3443 {
3444         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3445         int size = tokens[i].size;
3446         ++i;
3447
3448         for (int j = 0; j < size; ++j)
3449         {
3450                 CGLTF_CHECK_KEY(tokens[i]);
3451
3452                 if (cgltf_json_strcmp(tokens+i, json_chunk, "diffuseFactor") == 0)
3453                 {
3454                         i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_pbr->diffuse_factor, 4);
3455                 }
3456                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "specularFactor") == 0)
3457                 {
3458                         i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_pbr->specular_factor, 3);
3459                 }
3460                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "glossinessFactor") == 0)
3461                 {
3462                         ++i;
3463                         out_pbr->glossiness_factor = cgltf_json_to_float(tokens + i, json_chunk);
3464                         ++i;
3465                 }
3466                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "diffuseTexture") == 0)
3467                 {
3468                         i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_pbr->diffuse_texture);
3469                 }
3470                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "specularGlossinessTexture") == 0)
3471                 {
3472                         i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_pbr->specular_glossiness_texture);
3473                 }
3474                 else
3475                 {
3476                         i = cgltf_skip_json(tokens, i+1);
3477                 }
3478
3479                 if (i < 0)
3480                 {
3481                         return i;
3482                 }
3483         }
3484
3485         return i;
3486 }
3487
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)
3489 {
3490         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3491         int size = tokens[i].size;
3492         ++i;
3493
3494         for (int j = 0; j < size; ++j)
3495         {
3496                 CGLTF_CHECK_KEY(tokens[i]);
3497
3498                 if (cgltf_json_strcmp(tokens+i, json_chunk, "clearcoatFactor") == 0)
3499                 {
3500                         ++i;
3501                         out_clearcoat->clearcoat_factor = cgltf_json_to_float(tokens + i, json_chunk);
3502                         ++i;
3503                 }
3504                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "clearcoatRoughnessFactor") == 0)
3505                 {
3506                         ++i;
3507                         out_clearcoat->clearcoat_roughness_factor = cgltf_json_to_float(tokens + i, json_chunk);
3508                         ++i;
3509                 }
3510                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "clearcoatTexture") == 0)
3511                 {
3512                         i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_clearcoat->clearcoat_texture);
3513                 }
3514                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "clearcoatRoughnessTexture") == 0)
3515                 {
3516                         i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_clearcoat->clearcoat_roughness_texture);
3517                 }
3518                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "clearcoatNormalTexture") == 0)
3519                 {
3520                         i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_clearcoat->clearcoat_normal_texture);
3521                 }
3522                 else
3523                 {
3524                         i = cgltf_skip_json(tokens, i+1);
3525                 }
3526
3527                 if (i < 0)
3528                 {
3529                         return i;
3530                 }
3531         }
3532
3533         return i;
3534 }
3535
3536 static int cgltf_parse_json_ior(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_ior* out_ior)
3537 {
3538         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3539         int size = tokens[i].size;
3540         ++i;
3541
3542         // Default values
3543         out_ior->ior = 1.5f;
3544
3545         for (int j = 0; j < size; ++j)
3546         {
3547                 CGLTF_CHECK_KEY(tokens[i]);
3548
3549                 if (cgltf_json_strcmp(tokens+i, json_chunk, "ior") == 0)
3550                 {
3551                         ++i;
3552                         out_ior->ior = cgltf_json_to_float(tokens + i, json_chunk);
3553                         ++i;
3554                 }
3555                 else
3556                 {
3557                         i = cgltf_skip_json(tokens, i+1);
3558                 }
3559
3560                 if (i < 0)
3561                 {
3562                         return i;
3563                 }
3564         }
3565
3566         return i;
3567 }
3568
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)
3570 {
3571         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3572         int size = tokens[i].size;
3573         ++i;
3574
3575         // Default values
3576         out_specular->specular_factor = 1.0f;
3577         cgltf_fill_float_array(out_specular->specular_color_factor, 3, 1.0f);
3578
3579         for (int j = 0; j < size; ++j)
3580         {
3581                 CGLTF_CHECK_KEY(tokens[i]);
3582
3583                 if (cgltf_json_strcmp(tokens+i, json_chunk, "specularFactor") == 0)
3584                 {
3585                         ++i;
3586                         out_specular->specular_factor = cgltf_json_to_float(tokens + i, json_chunk);
3587                         ++i;
3588                 }
3589                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "specularColorFactor") == 0)
3590                 {
3591                         i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_specular->specular_color_factor, 3);
3592                 }
3593                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "specularTexture") == 0)
3594                 {
3595                         i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_specular->specular_texture);
3596                 }
3597                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "specularColorTexture") == 0)
3598                 {
3599                         i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_specular->specular_color_texture);
3600                 }
3601                 else
3602                 {
3603                         i = cgltf_skip_json(tokens, i+1);
3604                 }
3605
3606                 if (i < 0)
3607                 {
3608                         return i;
3609                 }
3610         }
3611
3612         return i;
3613 }
3614
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)
3616 {
3617         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3618         int size = tokens[i].size;
3619         ++i;
3620
3621         for (int j = 0; j < size; ++j)
3622         {
3623                 CGLTF_CHECK_KEY(tokens[i]);
3624
3625                 if (cgltf_json_strcmp(tokens+i, json_chunk, "transmissionFactor") == 0)
3626                 {
3627                         ++i;
3628                         out_transmission->transmission_factor = cgltf_json_to_float(tokens + i, json_chunk);
3629                         ++i;
3630                 }
3631                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "transmissionTexture") == 0)
3632                 {
3633                         i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_transmission->transmission_texture);
3634                 }
3635                 else
3636                 {
3637                         i = cgltf_skip_json(tokens, i+1);
3638                 }
3639
3640                 if (i < 0)
3641                 {
3642                         return i;
3643                 }
3644         }
3645
3646         return i;
3647 }
3648
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)
3650 {
3651         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3652         int size = tokens[i].size;
3653         ++i;
3654
3655         for (int j = 0; j < size; ++j)
3656         {
3657                 CGLTF_CHECK_KEY(tokens[i]);
3658
3659                 if (cgltf_json_strcmp(tokens + i, json_chunk, "thicknessFactor") == 0)
3660                 {
3661                         ++i;
3662                         out_volume->thickness_factor = cgltf_json_to_float(tokens + i, json_chunk);
3663                         ++i;
3664                 }
3665                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "thicknessTexture") == 0)
3666                 {
3667                         i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_volume->thickness_texture);
3668                 }
3669                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "attenuationColor") == 0)
3670                 {
3671                         i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_volume->attenuation_color, 3);
3672                 }
3673                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "attenuationDistance") == 0)
3674                 {
3675                         ++i;
3676                         out_volume->attenuation_distance = cgltf_json_to_float(tokens + i, json_chunk);
3677                         ++i;
3678                 }
3679                 else
3680                 {
3681                         i = cgltf_skip_json(tokens, i + 1);
3682                 }
3683
3684                 if (i < 0)
3685                 {
3686                         return i;
3687                 }
3688         }
3689
3690         return i;
3691 }
3692
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)
3694 {
3695         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3696         int size = tokens[i].size;
3697         ++i;
3698
3699         for (int j = 0; j < size; ++j)
3700         {
3701                 CGLTF_CHECK_KEY(tokens[i]);
3702
3703                 if (cgltf_json_strcmp(tokens+i, json_chunk, "sheenColorFactor") == 0)
3704                 {
3705                         i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_sheen->sheen_color_factor, 3);
3706                 }
3707                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "sheenColorTexture") == 0)
3708                 {
3709                         i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_sheen->sheen_color_texture);
3710                 }
3711                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "sheenRoughnessFactor") == 0)
3712                 {
3713                         ++i;
3714                         out_sheen->sheen_roughness_factor = cgltf_json_to_float(tokens + i, json_chunk);
3715                         ++i;
3716                 }
3717                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "sheenRoughnessTexture") == 0)
3718                 {
3719                         i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_sheen->sheen_roughness_texture);
3720                 }
3721                 else
3722                 {
3723                         i = cgltf_skip_json(tokens, i+1);
3724                 }
3725
3726                 if (i < 0)
3727                 {
3728                         return i;
3729                 }
3730         }
3731
3732         return i;
3733 }
3734
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)
3736 {
3737         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3738
3739         int size = tokens[i].size;
3740         ++i;
3741
3742         for (int j = 0; j < size; ++j) 
3743         {
3744                 CGLTF_CHECK_KEY(tokens[i]);
3745
3746                 if (cgltf_json_strcmp(tokens + i, json_chunk, "uri") == 0) 
3747                 {
3748                         i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_image->uri);
3749                 }
3750                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "bufferView") == 0)
3751                 {
3752                         ++i;
3753                         out_image->buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
3754                         ++i;
3755                 }
3756                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "mimeType") == 0)
3757                 {
3758                         i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_image->mime_type);
3759                 }
3760                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "name") == 0)
3761                 {
3762                         i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_image->name);
3763                 }
3764                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3765                 {
3766                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_image->extras);
3767                 }
3768                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
3769                 {
3770                         i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_image->extensions_count, &out_image->extensions);
3771                 }
3772                 else
3773                 {
3774                         i = cgltf_skip_json(tokens, i + 1);
3775                 }
3776
3777                 if (i < 0)
3778                 {
3779                         return i;
3780                 }
3781         }
3782
3783         return i;
3784 }
3785
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)
3787 {
3788         (void)options;
3789         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3790
3791         out_sampler->wrap_s = 10497;
3792         out_sampler->wrap_t = 10497;
3793
3794         int size = tokens[i].size;
3795         ++i;
3796
3797         for (int j = 0; j < size; ++j)
3798         {
3799                 CGLTF_CHECK_KEY(tokens[i]);
3800
3801                 if (cgltf_json_strcmp(tokens + i, json_chunk, "name") == 0)
3802                 {
3803                         i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_sampler->name);
3804                 }
3805                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "magFilter") == 0)
3806                 {
3807                         ++i;
3808                         out_sampler->mag_filter
3809                                 = cgltf_json_to_int(tokens + i, json_chunk);
3810                         ++i;
3811                 }
3812                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "minFilter") == 0)
3813                 {
3814                         ++i;
3815                         out_sampler->min_filter
3816                                 = cgltf_json_to_int(tokens + i, json_chunk);
3817                         ++i;
3818                 }
3819                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "wrapS") == 0)
3820                 {
3821                         ++i;
3822                         out_sampler->wrap_s
3823                                 = cgltf_json_to_int(tokens + i, json_chunk);
3824                         ++i;
3825                 }
3826                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "wrapT") == 0) 
3827                 {
3828                         ++i;
3829                         out_sampler->wrap_t
3830                                 = cgltf_json_to_int(tokens + i, json_chunk);
3831                         ++i;
3832                 }
3833                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3834                 {
3835                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_sampler->extras);
3836                 }
3837                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
3838                 {
3839                         i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_sampler->extensions_count, &out_sampler->extensions);
3840                 }
3841                 else
3842                 {
3843                         i = cgltf_skip_json(tokens, i + 1);
3844                 }
3845
3846                 if (i < 0)
3847                 {
3848                         return i;
3849                 }
3850         }
3851
3852         return i;
3853 }
3854
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)
3856 {
3857         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3858
3859         int size = tokens[i].size;
3860         ++i;
3861
3862         for (int j = 0; j < size; ++j)
3863         {
3864                 CGLTF_CHECK_KEY(tokens[i]);
3865
3866                 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
3867                 {
3868                         i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_texture->name);
3869                 }
3870                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "sampler") == 0)
3871                 {
3872                         ++i;
3873                         out_texture->sampler = CGLTF_PTRINDEX(cgltf_sampler, cgltf_json_to_int(tokens + i, json_chunk));
3874                         ++i;
3875                 }
3876                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "source") == 0) 
3877                 {
3878                         ++i;
3879                         out_texture->image = CGLTF_PTRINDEX(cgltf_image, cgltf_json_to_int(tokens + i, json_chunk));
3880                         ++i;
3881                 }
3882                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3883                 {
3884                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_texture->extras);
3885                 }
3886                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
3887                 {
3888                         i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_texture->extensions_count, &out_texture->extensions);
3889                 }
3890                 else
3891                 {
3892                         i = cgltf_skip_json(tokens, i + 1);
3893                 }
3894
3895                 if (i < 0)
3896                 {
3897                         return i;
3898                 }
3899         }
3900
3901         return i;
3902 }
3903
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)
3905 {
3906         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3907
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;
3911
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;
3915
3916         cgltf_fill_float_array(out_material->volume.attenuation_color, 3, 1.0f);
3917         out_material->volume.attenuation_distance = FLT_MAX;
3918
3919         out_material->alpha_cutoff = 0.5f;
3920
3921         int size = tokens[i].size;
3922         ++i;
3923
3924         for (int j = 0; j < size; ++j)
3925         {
3926                 CGLTF_CHECK_KEY(tokens[i]);
3927
3928                 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
3929                 {
3930                         i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_material->name);
3931                 }
3932                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "pbrMetallicRoughness") == 0)
3933                 {
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);
3936                 }
3937                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "emissiveFactor") == 0)
3938                 {
3939                         i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_material->emissive_factor, 3);
3940                 }
3941                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "normalTexture") == 0)
3942                 {
3943                         i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk,
3944                                 &out_material->normal_texture);
3945                 }
3946                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "occlusionTexture") == 0)
3947                 {
3948                         i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk,
3949                                 &out_material->occlusion_texture);
3950                 }
3951                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "emissiveTexture") == 0)
3952                 {
3953                         i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk,
3954                                 &out_material->emissive_texture);
3955                 }
3956                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "alphaMode") == 0)
3957                 {
3958                         ++i;
3959                         if (cgltf_json_strcmp(tokens + i, json_chunk, "OPAQUE") == 0)
3960                         {
3961                                 out_material->alpha_mode = cgltf_alpha_mode_opaque;
3962                         }
3963                         else if (cgltf_json_strcmp(tokens + i, json_chunk, "MASK") == 0)
3964                         {
3965                                 out_material->alpha_mode = cgltf_alpha_mode_mask;
3966                         }
3967                         else if (cgltf_json_strcmp(tokens + i, json_chunk, "BLEND") == 0)
3968                         {
3969                                 out_material->alpha_mode = cgltf_alpha_mode_blend;
3970                         }
3971                         ++i;
3972                 }
3973                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "alphaCutoff") == 0)
3974                 {
3975                         ++i;
3976                         out_material->alpha_cutoff = cgltf_json_to_float(tokens + i, json_chunk);
3977                         ++i;
3978                 }
3979                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "doubleSided") == 0)
3980                 {
3981                         ++i;
3982                         out_material->double_sided =
3983                                 cgltf_json_to_bool(tokens + i, json_chunk);
3984                         ++i;
3985                 }
3986                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3987                 {
3988                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_material->extras);
3989                 }
3990                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
3991                 {
3992                         ++i;
3993
3994                         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3995                         if(out_material->extensions)
3996                         {
3997                                 return CGLTF_ERROR_JSON;
3998                         }
3999
4000                         int extensions_size = tokens[i].size;
4001                         ++i;
4002                         out_material->extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
4003                         out_material->extensions_count= 0;
4004
4005                         if (!out_material->extensions)
4006                         {
4007                                 return CGLTF_ERROR_NOMEM;
4008                         }
4009
4010                         for (int k = 0; k < extensions_size; ++k)
4011                         {
4012                                 CGLTF_CHECK_KEY(tokens[i]);
4013
4014                                 if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_pbrSpecularGlossiness") == 0)
4015                                 {
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);
4018                                 }
4019                                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_unlit") == 0)
4020                                 {
4021                                         out_material->unlit = 1;
4022                                         i = cgltf_skip_json(tokens, i+1);
4023                                 }
4024                                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_clearcoat") == 0)
4025                                 {
4026                                         out_material->has_clearcoat = 1;
4027                                         i = cgltf_parse_json_clearcoat(options, tokens, i + 1, json_chunk, &out_material->clearcoat);
4028                                 }
4029                                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_ior") == 0)
4030                                 {
4031                                         out_material->has_ior = 1;
4032                                         i = cgltf_parse_json_ior(tokens, i + 1, json_chunk, &out_material->ior);
4033                                 }
4034                                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_specular") == 0)
4035                                 {
4036                                         out_material->has_specular = 1;
4037                                         i = cgltf_parse_json_specular(options, tokens, i + 1, json_chunk, &out_material->specular);
4038                                 }
4039                                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_transmission") == 0)
4040                                 {
4041                                         out_material->has_transmission = 1;
4042                                         i = cgltf_parse_json_transmission(options, tokens, i + 1, json_chunk, &out_material->transmission);
4043                                 }
4044                                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "KHR_materials_volume") == 0)
4045                                 {
4046                                         out_material->has_volume = 1;
4047                                         i = cgltf_parse_json_volume(options, tokens, i + 1, json_chunk, &out_material->volume);
4048                                 }
4049                                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_sheen") == 0)
4050                                 {
4051                                         out_material->has_sheen = 1;
4052                                         i = cgltf_parse_json_sheen(options, tokens, i + 1, json_chunk, &out_material->sheen);
4053                                 }
4054                                 else
4055                                 {
4056                                         i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_material->extensions[out_material->extensions_count++]));
4057                                 }
4058
4059                                 if (i < 0)
4060                                 {
4061                                         return i;
4062                                 }
4063                         }
4064                 }
4065                 else
4066                 {
4067                         i = cgltf_skip_json(tokens, i+1);
4068                 }
4069
4070                 if (i < 0)
4071                 {
4072                         return i;
4073                 }
4074         }
4075
4076         return i;
4077 }
4078
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)
4080 {
4081         i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_accessor), (void**)&out_data->accessors, &out_data->accessors_count);
4082         if (i < 0)
4083         {
4084                 return i;
4085         }
4086
4087         for (cgltf_size j = 0; j < out_data->accessors_count; ++j)
4088         {
4089                 i = cgltf_parse_json_accessor(options, tokens, i, json_chunk, &out_data->accessors[j]);
4090                 if (i < 0)
4091                 {
4092                         return i;
4093                 }
4094         }
4095         return i;
4096 }
4097
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)
4099 {
4100         i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_material), (void**)&out_data->materials, &out_data->materials_count);
4101         if (i < 0)
4102         {
4103                 return i;
4104         }
4105
4106         for (cgltf_size j = 0; j < out_data->materials_count; ++j)
4107         {
4108                 i = cgltf_parse_json_material(options, tokens, i, json_chunk, &out_data->materials[j]);
4109                 if (i < 0)
4110                 {
4111                         return i;
4112                 }
4113         }
4114         return i;
4115 }
4116
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)
4118 {
4119         i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_image), (void**)&out_data->images, &out_data->images_count);
4120         if (i < 0)
4121         {
4122                 return i;
4123         }
4124
4125         for (cgltf_size j = 0; j < out_data->images_count; ++j)
4126         {
4127                 i = cgltf_parse_json_image(options, tokens, i, json_chunk, &out_data->images[j]);
4128                 if (i < 0)
4129                 {
4130                         return i;
4131                 }
4132         }
4133         return i;
4134 }
4135
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)
4137 {
4138         i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_texture), (void**)&out_data->textures, &out_data->textures_count);
4139         if (i < 0)
4140         {
4141                 return i;
4142         }
4143
4144         for (cgltf_size j = 0; j < out_data->textures_count; ++j)
4145         {
4146                 i = cgltf_parse_json_texture(options, tokens, i, json_chunk, &out_data->textures[j]);
4147                 if (i < 0)
4148                 {
4149                         return i;
4150                 }
4151         }
4152         return i;
4153 }
4154
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)
4156 {
4157         i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_sampler), (void**)&out_data->samplers, &out_data->samplers_count);
4158         if (i < 0)
4159         {
4160                 return i;
4161         }
4162
4163         for (cgltf_size j = 0; j < out_data->samplers_count; ++j)
4164         {
4165                 i = cgltf_parse_json_sampler(options, tokens, i, json_chunk, &out_data->samplers[j]);
4166                 if (i < 0)
4167                 {
4168                         return i;
4169                 }
4170         }
4171         return i;
4172 }
4173
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)
4175 {
4176         (void)options;
4177         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4178
4179         int size = tokens[i].size;
4180         ++i;
4181
4182         for (int j = 0; j < size; ++j)
4183         {
4184                 CGLTF_CHECK_KEY(tokens[i]);
4185
4186                 if (cgltf_json_strcmp(tokens+i, json_chunk, "buffer") == 0)
4187                 {
4188                         ++i;
4189                         out_meshopt_compression->buffer = CGLTF_PTRINDEX(cgltf_buffer, cgltf_json_to_int(tokens + i, json_chunk));
4190                         ++i;
4191                 }
4192                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
4193                 {
4194                         ++i;
4195                         out_meshopt_compression->offset = cgltf_json_to_int(tokens+i, json_chunk);
4196                         ++i;
4197                 }
4198                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteLength") == 0)
4199                 {
4200                         ++i;
4201                         out_meshopt_compression->size = cgltf_json_to_int(tokens+i, json_chunk);
4202                         ++i;
4203                 }
4204                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteStride") == 0)
4205                 {
4206                         ++i;
4207                         out_meshopt_compression->stride = cgltf_json_to_int(tokens+i, json_chunk);
4208                         ++i;
4209                 }
4210                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "count") == 0)
4211                 {
4212                         ++i;
4213                         out_meshopt_compression->count = cgltf_json_to_int(tokens+i, json_chunk);
4214                         ++i;
4215                 }
4216                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "mode") == 0)
4217                 {
4218                         ++i;
4219                         if (cgltf_json_strcmp(tokens+i, json_chunk, "ATTRIBUTES") == 0)
4220                         {
4221                                 out_meshopt_compression->mode = cgltf_meshopt_compression_mode_attributes;
4222                         }
4223                         else if (cgltf_json_strcmp(tokens+i, json_chunk, "TRIANGLES") == 0)
4224                         {
4225                                 out_meshopt_compression->mode = cgltf_meshopt_compression_mode_triangles;
4226                         }
4227                         else if (cgltf_json_strcmp(tokens+i, json_chunk, "INDICES") == 0)
4228                         {
4229                                 out_meshopt_compression->mode = cgltf_meshopt_compression_mode_indices;
4230                         }
4231                         ++i;
4232                 }
4233                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "filter") == 0)
4234                 {
4235                         ++i;
4236                         if (cgltf_json_strcmp(tokens+i, json_chunk, "NONE") == 0)
4237                         {
4238                                 out_meshopt_compression->filter = cgltf_meshopt_compression_filter_none;
4239                         }
4240                         else if (cgltf_json_strcmp(tokens+i, json_chunk, "OCTAHEDRAL") == 0)
4241                         {
4242                                 out_meshopt_compression->filter = cgltf_meshopt_compression_filter_octahedral;
4243                         }
4244                         else if (cgltf_json_strcmp(tokens+i, json_chunk, "QUATERNION") == 0)
4245                         {
4246                                 out_meshopt_compression->filter = cgltf_meshopt_compression_filter_quaternion;
4247                         }
4248                         else if (cgltf_json_strcmp(tokens+i, json_chunk, "EXPONENTIAL") == 0)
4249                         {
4250                                 out_meshopt_compression->filter = cgltf_meshopt_compression_filter_exponential;
4251                         }
4252                         ++i;
4253                 }
4254                 else
4255                 {
4256                         i = cgltf_skip_json(tokens, i+1);
4257                 }
4258
4259                 if (i < 0)
4260                 {
4261                         return i;
4262                 }
4263         }
4264
4265         return i;
4266 }
4267
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)
4269 {
4270         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4271
4272         int size = tokens[i].size;
4273         ++i;
4274
4275         for (int j = 0; j < size; ++j)
4276         {
4277                 CGLTF_CHECK_KEY(tokens[i]);
4278
4279                 if (cgltf_json_strcmp(tokens + i, json_chunk, "name") == 0)
4280                 {
4281                         i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_buffer_view->name);
4282                 }
4283                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "buffer") == 0)
4284                 {
4285                         ++i;
4286                         out_buffer_view->buffer = CGLTF_PTRINDEX(cgltf_buffer, cgltf_json_to_int(tokens + i, json_chunk));
4287                         ++i;
4288                 }
4289                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
4290                 {
4291                         ++i;
4292                         out_buffer_view->offset =
4293                                         cgltf_json_to_int(tokens+i, json_chunk);
4294                         ++i;
4295                 }
4296                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteLength") == 0)
4297                 {
4298                         ++i;
4299                         out_buffer_view->size =
4300                                         cgltf_json_to_int(tokens+i, json_chunk);
4301                         ++i;
4302                 }
4303                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteStride") == 0)
4304                 {
4305                         ++i;
4306                         out_buffer_view->stride =
4307                                         cgltf_json_to_int(tokens+i, json_chunk);
4308                         ++i;
4309                 }
4310                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "target") == 0)
4311                 {
4312                         ++i;
4313                         int type = cgltf_json_to_int(tokens+i, json_chunk);
4314                         switch (type)
4315                         {
4316                         case 34962:
4317                                 type = cgltf_buffer_view_type_vertices;
4318                                 break;
4319                         case 34963:
4320                                 type = cgltf_buffer_view_type_indices;
4321                                 break;
4322                         default:
4323                                 type = cgltf_buffer_view_type_invalid;
4324                                 break;
4325                         }
4326                         out_buffer_view->type = (cgltf_buffer_view_type)type;
4327                         ++i;
4328                 }
4329                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
4330                 {
4331                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_buffer_view->extras);
4332                 }
4333                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
4334                 {
4335                         ++i;
4336
4337                         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4338                         if(out_buffer_view->extensions)
4339                         {
4340                                 return CGLTF_ERROR_JSON;
4341                         }
4342
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);
4346
4347                         if (!out_buffer_view->extensions)
4348                         {
4349                                 return CGLTF_ERROR_NOMEM;
4350                         }
4351
4352                         ++i;
4353                         for (int k = 0; k < extensions_size; ++k)
4354                         {
4355                                 CGLTF_CHECK_KEY(tokens[i]);
4356
4357                                 if (cgltf_json_strcmp(tokens+i, json_chunk, "EXT_meshopt_compression") == 0)
4358                                 {
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);
4361                                 }
4362                                 else
4363                                 {
4364                                         i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_buffer_view->extensions[out_buffer_view->extensions_count++]));
4365                                 }
4366
4367                                 if (i < 0)
4368                                 {
4369                                         return i;
4370                                 }
4371                         }
4372                 }
4373                 else
4374                 {
4375                         i = cgltf_skip_json(tokens, i+1);
4376                 }
4377
4378                 if (i < 0)
4379                 {
4380                         return i;
4381                 }
4382         }
4383
4384         return i;
4385 }
4386
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)
4388 {
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);
4390         if (i < 0)
4391         {
4392                 return i;
4393         }
4394
4395         for (cgltf_size j = 0; j < out_data->buffer_views_count; ++j)
4396         {
4397                 i = cgltf_parse_json_buffer_view(options, tokens, i, json_chunk, &out_data->buffer_views[j]);
4398                 if (i < 0)
4399                 {
4400                         return i;
4401                 }
4402         }
4403         return i;
4404 }
4405
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)
4407 {
4408         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4409
4410         int size = tokens[i].size;
4411         ++i;
4412
4413         for (int j = 0; j < size; ++j)
4414         {
4415                 CGLTF_CHECK_KEY(tokens[i]);
4416
4417                 if (cgltf_json_strcmp(tokens + i, json_chunk, "name") == 0)
4418                 {
4419                         i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_buffer->name);
4420                 }
4421                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteLength") == 0)
4422                 {
4423                         ++i;
4424                         out_buffer->size =
4425                                         cgltf_json_to_int(tokens+i, json_chunk);
4426                         ++i;
4427                 }
4428                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "uri") == 0)
4429                 {
4430                         i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_buffer->uri);
4431                 }
4432                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
4433                 {
4434                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_buffer->extras);
4435                 }
4436                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
4437                 {
4438                         i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_buffer->extensions_count, &out_buffer->extensions);
4439                 }
4440                 else
4441                 {
4442                         i = cgltf_skip_json(tokens, i+1);
4443                 }
4444
4445                 if (i < 0)
4446                 {
4447                         return i;
4448                 }
4449         }
4450
4451         return i;
4452 }
4453
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)
4455 {
4456         i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_buffer), (void**)&out_data->buffers, &out_data->buffers_count);
4457         if (i < 0)
4458         {
4459                 return i;
4460         }
4461
4462         for (cgltf_size j = 0; j < out_data->buffers_count; ++j)
4463         {
4464                 i = cgltf_parse_json_buffer(options, tokens, i, json_chunk, &out_data->buffers[j]);
4465                 if (i < 0)
4466                 {
4467                         return i;
4468                 }
4469         }
4470         return i;
4471 }
4472
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)
4474 {
4475         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4476
4477         int size = tokens[i].size;
4478         ++i;
4479
4480         for (int j = 0; j < size; ++j)
4481         {
4482                 CGLTF_CHECK_KEY(tokens[i]);
4483
4484                 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
4485                 {
4486                         i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_skin->name);
4487                 }
4488                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "joints") == 0)
4489                 {
4490                         i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_node*), (void**)&out_skin->joints, &out_skin->joints_count);
4491                         if (i < 0)
4492                         {
4493                                 return i;
4494                         }
4495
4496                         for (cgltf_size k = 0; k < out_skin->joints_count; ++k)
4497                         {
4498                                 out_skin->joints[k] = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
4499                                 ++i;
4500                         }
4501                 }
4502                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "skeleton") == 0)
4503                 {
4504                         ++i;
4505                         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
4506                         out_skin->skeleton = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
4507                         ++i;
4508                 }
4509                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "inverseBindMatrices") == 0)
4510                 {
4511                         ++i;
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));
4514                         ++i;
4515                 }
4516                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
4517                 {
4518                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_skin->extras);
4519                 }
4520                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
4521                 {
4522                         i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_skin->extensions_count, &out_skin->extensions);
4523                 }
4524                 else
4525                 {
4526                         i = cgltf_skip_json(tokens, i+1);
4527                 }
4528
4529                 if (i < 0)
4530                 {
4531                         return i;
4532                 }
4533         }
4534
4535         return i;
4536 }
4537
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)
4539 {
4540         i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_skin), (void**)&out_data->skins, &out_data->skins_count);
4541         if (i < 0)
4542         {
4543                 return i;
4544         }
4545
4546         for (cgltf_size j = 0; j < out_data->skins_count; ++j)
4547         {
4548                 i = cgltf_parse_json_skin(options, tokens, i, json_chunk, &out_data->skins[j]);
4549                 if (i < 0)
4550                 {
4551                         return i;
4552                 }
4553         }
4554         return i;
4555 }
4556
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)
4558 {
4559         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4560
4561         int size = tokens[i].size;
4562         ++i;
4563
4564         for (int j = 0; j < size; ++j)
4565         {
4566                 CGLTF_CHECK_KEY(tokens[i]);
4567
4568                 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
4569                 {
4570                         i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_camera->name);
4571                 }
4572                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "type") == 0)
4573                 {
4574                         ++i;
4575                         if (cgltf_json_strcmp(tokens + i, json_chunk, "perspective") == 0)
4576                         {
4577                                 out_camera->type = cgltf_camera_type_perspective;
4578                         }
4579                         else if (cgltf_json_strcmp(tokens + i, json_chunk, "orthographic") == 0)
4580                         {
4581                                 out_camera->type = cgltf_camera_type_orthographic;
4582                         }
4583                         ++i;
4584                 }
4585                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "perspective") == 0)
4586                 {
4587                         ++i;
4588
4589                         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4590
4591                         int data_size = tokens[i].size;
4592                         ++i;
4593
4594                         out_camera->type = cgltf_camera_type_perspective;
4595
4596                         for (int k = 0; k < data_size; ++k)
4597                         {
4598                                 CGLTF_CHECK_KEY(tokens[i]);
4599
4600                                 if (cgltf_json_strcmp(tokens+i, json_chunk, "aspectRatio") == 0)
4601                                 {
4602                                         ++i;
4603                                         out_camera->data.perspective.has_aspect_ratio = 1;
4604                                         out_camera->data.perspective.aspect_ratio = cgltf_json_to_float(tokens + i, json_chunk);
4605                                         ++i;
4606                                 }
4607                                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "yfov") == 0)
4608                                 {
4609                                         ++i;
4610                                         out_camera->data.perspective.yfov = cgltf_json_to_float(tokens + i, json_chunk);
4611                                         ++i;
4612                                 }
4613                                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "zfar") == 0)
4614                                 {
4615                                         ++i;
4616                                         out_camera->data.perspective.has_zfar = 1;
4617                                         out_camera->data.perspective.zfar = cgltf_json_to_float(tokens + i, json_chunk);
4618                                         ++i;
4619                                 }
4620                                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "znear") == 0)
4621                                 {
4622                                         ++i;
4623                                         out_camera->data.perspective.znear = cgltf_json_to_float(tokens + i, json_chunk);
4624                                         ++i;
4625                                 }
4626                                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
4627                                 {
4628                                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_camera->data.perspective.extras);
4629                                 }
4630                                 else
4631                                 {
4632                                         i = cgltf_skip_json(tokens, i+1);
4633                                 }
4634
4635                                 if (i < 0)
4636                                 {
4637                                         return i;
4638                                 }
4639                         }
4640                 }
4641                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "orthographic") == 0)
4642                 {
4643                         ++i;
4644
4645                         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4646
4647                         int data_size = tokens[i].size;
4648                         ++i;
4649
4650                         out_camera->type = cgltf_camera_type_orthographic;
4651
4652                         for (int k = 0; k < data_size; ++k)
4653                         {
4654                                 CGLTF_CHECK_KEY(tokens[i]);
4655
4656                                 if (cgltf_json_strcmp(tokens+i, json_chunk, "xmag") == 0)
4657                                 {
4658                                         ++i;
4659                                         out_camera->data.orthographic.xmag = cgltf_json_to_float(tokens + i, json_chunk);
4660                                         ++i;
4661                                 }
4662                                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "ymag") == 0)
4663                                 {
4664                                         ++i;
4665                                         out_camera->data.orthographic.ymag = cgltf_json_to_float(tokens + i, json_chunk);
4666                                         ++i;
4667                                 }
4668                                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "zfar") == 0)
4669                                 {
4670                                         ++i;
4671                                         out_camera->data.orthographic.zfar = cgltf_json_to_float(tokens + i, json_chunk);
4672                                         ++i;
4673                                 }
4674                                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "znear") == 0)
4675                                 {
4676                                         ++i;
4677                                         out_camera->data.orthographic.znear = cgltf_json_to_float(tokens + i, json_chunk);
4678                                         ++i;
4679                                 }
4680                                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
4681                                 {
4682                                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_camera->data.orthographic.extras);
4683                                 }
4684                                 else
4685                                 {
4686                                         i = cgltf_skip_json(tokens, i+1);
4687                                 }
4688
4689                                 if (i < 0)
4690                                 {
4691                                         return i;
4692                                 }
4693                         }
4694                 }
4695                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
4696                 {
4697                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_camera->extras);
4698                 }
4699                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
4700                 {
4701                         i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_camera->extensions_count, &out_camera->extensions);
4702                 }
4703                 else
4704                 {
4705                         i = cgltf_skip_json(tokens, i+1);
4706                 }
4707
4708                 if (i < 0)
4709                 {
4710                         return i;
4711                 }
4712         }
4713
4714         return i;
4715 }
4716
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)
4718 {
4719         i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_camera), (void**)&out_data->cameras, &out_data->cameras_count);
4720         if (i < 0)
4721         {
4722                 return i;
4723         }
4724
4725         for (cgltf_size j = 0; j < out_data->cameras_count; ++j)
4726         {
4727                 i = cgltf_parse_json_camera(options, tokens, i, json_chunk, &out_data->cameras[j]);
4728                 if (i < 0)
4729                 {
4730                         return i;
4731                 }
4732         }
4733         return i;
4734 }
4735
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)
4737 {
4738         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4739
4740         int size = tokens[i].size;
4741         ++i;
4742
4743         for (int j = 0; j < size; ++j)
4744         {
4745                 CGLTF_CHECK_KEY(tokens[i]);
4746
4747                 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
4748                 {
4749                         i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_light->name);
4750                 }
4751                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "color") == 0)
4752                 {
4753                         i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_light->color, 3);
4754                 }
4755                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "intensity") == 0)
4756                 {
4757                         ++i;
4758                         out_light->intensity = cgltf_json_to_float(tokens + i, json_chunk);
4759                         ++i;
4760                 }
4761                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "type") == 0)
4762                 {
4763                         ++i;
4764                         if (cgltf_json_strcmp(tokens + i, json_chunk, "directional") == 0)
4765                         {
4766                                 out_light->type = cgltf_light_type_directional;
4767                         }
4768                         else if (cgltf_json_strcmp(tokens + i, json_chunk, "point") == 0)
4769                         {
4770                                 out_light->type = cgltf_light_type_point;
4771                         }
4772                         else if (cgltf_json_strcmp(tokens + i, json_chunk, "spot") == 0)
4773                         {
4774                                 out_light->type = cgltf_light_type_spot;
4775                         }
4776                         ++i;
4777                 }
4778                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "range") == 0)
4779                 {
4780                         ++i;
4781                         out_light->range = cgltf_json_to_float(tokens + i, json_chunk);
4782                         ++i;
4783                 }
4784                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "spot") == 0)
4785                 {
4786                         ++i;
4787
4788                         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4789
4790                         int data_size = tokens[i].size;
4791                         ++i;
4792
4793                         for (int k = 0; k < data_size; ++k)
4794                         {
4795                                 CGLTF_CHECK_KEY(tokens[i]);
4796
4797                                 if (cgltf_json_strcmp(tokens+i, json_chunk, "innerConeAngle") == 0)
4798                                 {
4799                                         ++i;
4800                                         out_light->spot_inner_cone_angle = cgltf_json_to_float(tokens + i, json_chunk);
4801                                         ++i;
4802                                 }
4803                                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "outerConeAngle") == 0)
4804                                 {
4805                                         ++i;
4806                                         out_light->spot_outer_cone_angle = cgltf_json_to_float(tokens + i, json_chunk);
4807                                         ++i;
4808                                 }
4809                                 else
4810                                 {
4811                                         i = cgltf_skip_json(tokens, i+1);
4812                                 }
4813
4814                                 if (i < 0)
4815                                 {
4816                                         return i;
4817                                 }
4818                         }
4819                 }
4820                 else
4821                 {
4822                         i = cgltf_skip_json(tokens, i+1);
4823                 }
4824
4825                 if (i < 0)
4826                 {
4827                         return i;
4828                 }
4829         }
4830
4831         return i;
4832 }
4833
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)
4835 {
4836         i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_light), (void**)&out_data->lights, &out_data->lights_count);
4837         if (i < 0)
4838         {
4839                 return i;
4840         }
4841
4842         for (cgltf_size j = 0; j < out_data->lights_count; ++j)
4843         {
4844                 i = cgltf_parse_json_light(options, tokens, i, json_chunk, &out_data->lights[j]);
4845                 if (i < 0)
4846                 {
4847                         return i;
4848                 }
4849         }
4850         return i;
4851 }
4852
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)
4854 {
4855         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4856
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;
4865
4866         int size = tokens[i].size;
4867         ++i;
4868
4869         for (int j = 0; j < size; ++j)
4870         {
4871                 CGLTF_CHECK_KEY(tokens[i]);
4872
4873                 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
4874                 {
4875                         i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_node->name);
4876                 }
4877                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "children") == 0)
4878                 {
4879                         i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_node*), (void**)&out_node->children, &out_node->children_count);
4880                         if (i < 0)
4881                         {
4882                                 return i;
4883                         }
4884
4885                         for (cgltf_size k = 0; k < out_node->children_count; ++k)
4886                         {
4887                                 out_node->children[k] = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
4888                                 ++i;
4889                         }
4890                 }
4891                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "mesh") == 0)
4892                 {
4893                         ++i;
4894                         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
4895                         out_node->mesh = CGLTF_PTRINDEX(cgltf_mesh, cgltf_json_to_int(tokens + i, json_chunk));
4896                         ++i;
4897                 }
4898                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "skin") == 0)
4899                 {
4900                         ++i;
4901                         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
4902                         out_node->skin = CGLTF_PTRINDEX(cgltf_skin, cgltf_json_to_int(tokens + i, json_chunk));
4903                         ++i;
4904                 }
4905                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "camera") == 0)
4906                 {
4907                         ++i;
4908                         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
4909                         out_node->camera = CGLTF_PTRINDEX(cgltf_camera, cgltf_json_to_int(tokens + i, json_chunk));
4910                         ++i;
4911                 }
4912                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "translation") == 0)
4913                 {
4914                         out_node->has_translation = 1;
4915                         i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_node->translation, 3);
4916                 }
4917                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "rotation") == 0)
4918                 {
4919                         out_node->has_rotation = 1;
4920                         i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_node->rotation, 4);
4921                 }
4922                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "scale") == 0)
4923                 {
4924                         out_node->has_scale = 1;
4925                         i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_node->scale, 3);
4926                 }
4927                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "matrix") == 0)
4928                 {
4929                         out_node->has_matrix = 1;
4930                         i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_node->matrix, 16);
4931                 }
4932                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "weights") == 0)
4933                 {
4934                         i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_float), (void**)&out_node->weights, &out_node->weights_count);
4935                         if (i < 0)
4936                         {
4937                                 return i;
4938                         }
4939
4940                         i = cgltf_parse_json_float_array(tokens, i - 1, json_chunk, out_node->weights, (int)out_node->weights_count);
4941                 }
4942                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
4943                 {
4944                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_node->extras);
4945                 }
4946                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
4947                 {
4948                         ++i;
4949
4950                         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4951                         if(out_node->extensions)
4952                         {
4953                                 return CGLTF_ERROR_JSON;
4954                         }
4955
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);
4959
4960                         if (!out_node->extensions)
4961                         {
4962                                 return CGLTF_ERROR_NOMEM;
4963                         }
4964
4965                         ++i;
4966
4967                         for (int k = 0; k < extensions_size; ++k)
4968                         {
4969                                 CGLTF_CHECK_KEY(tokens[i]);
4970
4971                                 if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_lights_punctual") == 0)
4972                                 {
4973                                         ++i;
4974
4975                                         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4976
4977                                         int data_size = tokens[i].size;
4978                                         ++i;
4979
4980                                         for (int m = 0; m < data_size; ++m)
4981                                         {
4982                                                 CGLTF_CHECK_KEY(tokens[i]);
4983
4984                                                 if (cgltf_json_strcmp(tokens + i, json_chunk, "light") == 0)
4985                                                 {
4986                                                         ++i;
4987                                                         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
4988                                                         out_node->light = CGLTF_PTRINDEX(cgltf_light, cgltf_json_to_int(tokens + i, json_chunk));
4989                                                         ++i;
4990                                                 }
4991                                                 else
4992                                                 {
4993                                                         i = cgltf_skip_json(tokens, i + 1);
4994                                                 }
4995
4996                                                 if (i < 0)
4997                                                 {
4998                                                         return i;
4999                                                 }
5000                                         }
5001                                 }
5002                                 else
5003                                 {
5004                                         i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_node->extensions[out_node->extensions_count++]));
5005                                 }
5006
5007                                 if (i < 0)
5008                                 {
5009                                         return i;
5010                                 }
5011                         }
5012                 }
5013                 else
5014                 {
5015                         i = cgltf_skip_json(tokens, i+1);
5016                 }
5017
5018                 if (i < 0)
5019                 {
5020                         return i;
5021                 }
5022         }
5023
5024         return i;
5025 }
5026
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)
5028 {
5029         i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_node), (void**)&out_data->nodes, &out_data->nodes_count);
5030         if (i < 0)
5031         {
5032                 return i;
5033         }
5034
5035         for (cgltf_size j = 0; j < out_data->nodes_count; ++j)
5036         {
5037                 i = cgltf_parse_json_node(options, tokens, i, json_chunk, &out_data->nodes[j]);
5038                 if (i < 0)
5039                 {
5040                         return i;
5041                 }
5042         }
5043         return i;
5044 }
5045
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)
5047 {
5048         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5049
5050         int size = tokens[i].size;
5051         ++i;
5052
5053         for (int j = 0; j < size; ++j)
5054         {
5055                 CGLTF_CHECK_KEY(tokens[i]);
5056
5057                 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
5058                 {
5059                         i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_scene->name);
5060                 }
5061                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "nodes") == 0)
5062                 {
5063                         i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_node*), (void**)&out_scene->nodes, &out_scene->nodes_count);
5064                         if (i < 0)
5065                         {
5066                                 return i;
5067                         }
5068
5069                         for (cgltf_size k = 0; k < out_scene->nodes_count; ++k)
5070                         {
5071                                 out_scene->nodes[k] = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
5072                                 ++i;
5073                         }
5074                 }
5075                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
5076                 {
5077                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_scene->extras);
5078                 }
5079                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
5080                 {
5081                         i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_scene->extensions_count, &out_scene->extensions);
5082                 }
5083                 else
5084                 {
5085                         i = cgltf_skip_json(tokens, i+1);
5086                 }
5087
5088                 if (i < 0)
5089                 {
5090                         return i;
5091                 }
5092         }
5093
5094         return i;
5095 }
5096
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)
5098 {
5099         i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_scene), (void**)&out_data->scenes, &out_data->scenes_count);
5100         if (i < 0)
5101         {
5102                 return i;
5103         }
5104
5105         for (cgltf_size j = 0; j < out_data->scenes_count; ++j)
5106         {
5107                 i = cgltf_parse_json_scene(options, tokens, i, json_chunk, &out_data->scenes[j]);
5108                 if (i < 0)
5109                 {
5110                         return i;
5111                 }
5112         }
5113         return i;
5114 }
5115
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)
5117 {
5118         (void)options;
5119         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5120
5121         int size = tokens[i].size;
5122         ++i;
5123
5124         for (int j = 0; j < size; ++j)
5125         {
5126                 CGLTF_CHECK_KEY(tokens[i]);
5127
5128                 if (cgltf_json_strcmp(tokens+i, json_chunk, "input") == 0)
5129                 {
5130                         ++i;
5131                         out_sampler->input = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
5132                         ++i;
5133                 }
5134                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "output") == 0)
5135                 {
5136                         ++i;
5137                         out_sampler->output = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
5138                         ++i;
5139                 }
5140                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "interpolation") == 0)
5141                 {
5142                         ++i;
5143                         if (cgltf_json_strcmp(tokens + i, json_chunk, "LINEAR") == 0)
5144                         {
5145                                 out_sampler->interpolation = cgltf_interpolation_type_linear;
5146                         }
5147                         else if (cgltf_json_strcmp(tokens + i, json_chunk, "STEP") == 0)
5148                         {
5149                                 out_sampler->interpolation = cgltf_interpolation_type_step;
5150                         }
5151                         else if (cgltf_json_strcmp(tokens + i, json_chunk, "CUBICSPLINE") == 0)
5152                         {
5153                                 out_sampler->interpolation = cgltf_interpolation_type_cubic_spline;
5154                         }
5155                         ++i;
5156                 }
5157                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
5158                 {
5159                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_sampler->extras);
5160                 }
5161                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
5162                 {
5163                         i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_sampler->extensions_count, &out_sampler->extensions);
5164                 }
5165                 else
5166                 {
5167                         i = cgltf_skip_json(tokens, i+1);
5168                 }
5169
5170                 if (i < 0)
5171                 {
5172                         return i;
5173                 }
5174         }
5175
5176         return i;
5177 }
5178
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)
5180 {
5181         (void)options;
5182         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5183
5184         int size = tokens[i].size;
5185         ++i;
5186
5187         for (int j = 0; j < size; ++j)
5188         {
5189                 CGLTF_CHECK_KEY(tokens[i]);
5190
5191                 if (cgltf_json_strcmp(tokens+i, json_chunk, "sampler") == 0)
5192                 {
5193                         ++i;
5194                         out_channel->sampler = CGLTF_PTRINDEX(cgltf_animation_sampler, cgltf_json_to_int(tokens + i, json_chunk));
5195                         ++i;
5196                 }
5197                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "target") == 0)
5198                 {
5199                         ++i;
5200
5201                         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5202
5203                         int target_size = tokens[i].size;
5204                         ++i;
5205
5206                         for (int k = 0; k < target_size; ++k)
5207                         {
5208                                 CGLTF_CHECK_KEY(tokens[i]);
5209
5210                                 if (cgltf_json_strcmp(tokens+i, json_chunk, "node") == 0)
5211                                 {
5212                                         ++i;
5213                                         out_channel->target_node = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
5214                                         ++i;
5215                                 }
5216                                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "path") == 0)
5217                                 {
5218                                         ++i;
5219                                         if (cgltf_json_strcmp(tokens+i, json_chunk, "translation") == 0)
5220                                         {
5221                                                 out_channel->target_path = cgltf_animation_path_type_translation;
5222                                         }
5223                                         else if (cgltf_json_strcmp(tokens+i, json_chunk, "rotation") == 0)
5224                                         {
5225                                                 out_channel->target_path = cgltf_animation_path_type_rotation;
5226                                         }
5227                                         else if (cgltf_json_strcmp(tokens+i, json_chunk, "scale") == 0)
5228                                         {
5229                                                 out_channel->target_path = cgltf_animation_path_type_scale;
5230                                         }
5231                                         else if (cgltf_json_strcmp(tokens+i, json_chunk, "weights") == 0)
5232                                         {
5233                                                 out_channel->target_path = cgltf_animation_path_type_weights;
5234                                         }
5235                                         ++i;
5236                                 }
5237                                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
5238                                 {
5239                                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_channel->extras);
5240                                 }
5241                                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
5242                                 {
5243                                         i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_channel->extensions_count, &out_channel->extensions);
5244                                 }
5245                                 else
5246                                 {
5247                                         i = cgltf_skip_json(tokens, i+1);
5248                                 }
5249
5250                                 if (i < 0)
5251                                 {
5252                                         return i;
5253                                 }
5254                         }
5255                 }
5256                 else
5257                 {
5258                         i = cgltf_skip_json(tokens, i+1);
5259                 }
5260
5261                 if (i < 0)
5262                 {
5263                         return i;
5264                 }
5265         }
5266
5267         return i;
5268 }
5269
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)
5271 {
5272         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5273
5274         int size = tokens[i].size;
5275         ++i;
5276
5277         for (int j = 0; j < size; ++j)
5278         {
5279                 CGLTF_CHECK_KEY(tokens[i]);
5280
5281                 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
5282                 {
5283                         i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_animation->name);
5284                 }
5285                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "samplers") == 0)
5286                 {
5287                         i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_animation_sampler), (void**)&out_animation->samplers, &out_animation->samplers_count);
5288                         if (i < 0)
5289                         {
5290                                 return i;
5291                         }
5292
5293                         for (cgltf_size k = 0; k < out_animation->samplers_count; ++k)
5294                         {
5295                                 i = cgltf_parse_json_animation_sampler(options, tokens, i, json_chunk, &out_animation->samplers[k]);
5296                                 if (i < 0)
5297                                 {
5298                                         return i;
5299                                 }
5300                         }
5301                 }
5302                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "channels") == 0)
5303                 {
5304                         i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_animation_channel), (void**)&out_animation->channels, &out_animation->channels_count);
5305                         if (i < 0)
5306                         {
5307                                 return i;
5308                         }
5309
5310                         for (cgltf_size k = 0; k < out_animation->channels_count; ++k)
5311                         {
5312                                 i = cgltf_parse_json_animation_channel(options, tokens, i, json_chunk, &out_animation->channels[k]);
5313                                 if (i < 0)
5314                                 {
5315                                         return i;
5316                                 }
5317                         }
5318                 }
5319                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
5320                 {
5321                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_animation->extras);
5322                 }
5323                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
5324                 {
5325                         i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_animation->extensions_count, &out_animation->extensions);
5326                 }
5327                 else
5328                 {
5329                         i = cgltf_skip_json(tokens, i+1);
5330                 }
5331
5332                 if (i < 0)
5333                 {
5334                         return i;
5335                 }
5336         }
5337
5338         return i;
5339 }
5340
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)
5342 {
5343         i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_animation), (void**)&out_data->animations, &out_data->animations_count);
5344         if (i < 0)
5345         {
5346                 return i;
5347         }
5348
5349         for (cgltf_size j = 0; j < out_data->animations_count; ++j)
5350         {
5351                 i = cgltf_parse_json_animation(options, tokens, i, json_chunk, &out_data->animations[j]);
5352                 if (i < 0)
5353                 {
5354                         return i;
5355                 }
5356         }
5357         return i;
5358 }
5359
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)
5361 {
5362         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5363
5364         int size = tokens[i].size;
5365         ++i;
5366
5367         for (int j = 0; j < size; ++j)
5368         {
5369                 CGLTF_CHECK_KEY(tokens[i]);
5370
5371                 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
5372                 {
5373                         i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_variant->name);
5374                 }
5375                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
5376                 {
5377                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_variant->extras);
5378                 }
5379                 else
5380                 {
5381                         i = cgltf_skip_json(tokens, i+1);
5382                 }
5383
5384                 if (i < 0)
5385                 {
5386                         return i;
5387                 }
5388         }
5389
5390         return i;
5391 }
5392
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)
5394 {
5395         i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_material_variant), (void**)&out_data->variants, &out_data->variants_count);
5396         if (i < 0)
5397         {
5398                 return i;
5399         }
5400
5401         for (cgltf_size j = 0; j < out_data->variants_count; ++j)
5402         {
5403                 i = cgltf_parse_json_variant(options, tokens, i, json_chunk, &out_data->variants[j]);
5404                 if (i < 0)
5405                 {
5406                         return i;
5407                 }
5408         }
5409         return i;
5410 }
5411
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)
5413 {
5414         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5415
5416         int size = tokens[i].size;
5417         ++i;
5418
5419         for (int j = 0; j < size; ++j)
5420         {
5421                 CGLTF_CHECK_KEY(tokens[i]);
5422
5423                 if (cgltf_json_strcmp(tokens+i, json_chunk, "copyright") == 0)
5424                 {
5425                         i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_asset->copyright);
5426                 }
5427                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "generator") == 0)
5428                 {
5429                         i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_asset->generator);
5430                 }
5431                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "version") == 0)
5432                 {
5433                         i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_asset->version);
5434                 }
5435                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "minVersion") == 0)
5436                 {
5437                         i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_asset->min_version);
5438                 }
5439                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
5440                 {
5441                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_asset->extras);
5442                 }
5443                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
5444                 {
5445                         i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_asset->extensions_count, &out_asset->extensions);
5446                 }
5447                 else
5448                 {
5449                         i = cgltf_skip_json(tokens, i+1);
5450                 }
5451
5452                 if (i < 0)
5453                 {
5454                         return i;
5455                 }
5456         }
5457
5458         if (out_asset->version && CGLTF_ATOF(out_asset->version) < 2)
5459         {
5460                 return CGLTF_ERROR_LEGACY;
5461         }
5462
5463         return i;
5464 }
5465
5466 cgltf_size cgltf_num_components(cgltf_type type) {
5467         switch (type)
5468         {
5469         case cgltf_type_vec2:
5470                 return 2;
5471         case cgltf_type_vec3:
5472                 return 3;
5473         case cgltf_type_vec4:
5474                 return 4;
5475         case cgltf_type_mat2:
5476                 return 4;
5477         case cgltf_type_mat3:
5478                 return 9;
5479         case cgltf_type_mat4:
5480                 return 16;
5481         case cgltf_type_invalid:
5482         case cgltf_type_scalar:
5483         default:
5484                 return 1;
5485         }
5486 }
5487
5488 static cgltf_size cgltf_component_size(cgltf_component_type component_type) {
5489         switch (component_type)
5490         {
5491         case cgltf_component_type_r_8:
5492         case cgltf_component_type_r_8u:
5493                 return 1;
5494         case cgltf_component_type_r_16:
5495         case cgltf_component_type_r_16u:
5496                 return 2;
5497         case cgltf_component_type_r_32u:
5498         case cgltf_component_type_r_32f:
5499                 return 4;
5500         case cgltf_component_type_invalid:
5501         default:
5502                 return 0;
5503         }
5504 }
5505
5506 static cgltf_size cgltf_calc_size(cgltf_type type, cgltf_component_type component_type)
5507 {
5508         cgltf_size component_size = cgltf_component_size(component_type);
5509         if (type == cgltf_type_mat2 && component_size == 1)
5510         {
5511                 return 8 * component_size;
5512         }
5513         else if (type == cgltf_type_mat3 && (component_size == 1 || component_size == 2))
5514         {
5515                 return 12 * component_size;
5516         }
5517         return component_size * cgltf_num_components(type);
5518 }
5519
5520 static int cgltf_fixup_pointers(cgltf_data* out_data);
5521
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)
5523 {
5524         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5525
5526         int size = tokens[i].size;
5527         ++i;
5528
5529         for (int j = 0; j < size; ++j)
5530         {
5531                 CGLTF_CHECK_KEY(tokens[i]);
5532
5533                 if (cgltf_json_strcmp(tokens + i, json_chunk, "asset") == 0)
5534                 {
5535                         i = cgltf_parse_json_asset(options, tokens, i + 1, json_chunk, &out_data->asset);
5536                 }
5537                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "meshes") == 0)
5538                 {
5539                         i = cgltf_parse_json_meshes(options, tokens, i + 1, json_chunk, out_data);
5540                 }
5541                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "accessors") == 0)
5542                 {
5543                         i = cgltf_parse_json_accessors(options, tokens, i + 1, json_chunk, out_data);
5544                 }
5545                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "bufferViews") == 0)
5546                 {
5547                         i = cgltf_parse_json_buffer_views(options, tokens, i + 1, json_chunk, out_data);
5548                 }
5549                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "buffers") == 0)
5550                 {
5551                         i = cgltf_parse_json_buffers(options, tokens, i + 1, json_chunk, out_data);
5552                 }
5553                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "materials") == 0)
5554                 {
5555                         i = cgltf_parse_json_materials(options, tokens, i + 1, json_chunk, out_data);
5556                 }
5557                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "images") == 0)
5558                 {
5559                         i = cgltf_parse_json_images(options, tokens, i + 1, json_chunk, out_data);
5560                 }
5561                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "textures") == 0)
5562                 {
5563                         i = cgltf_parse_json_textures(options, tokens, i + 1, json_chunk, out_data);
5564                 }
5565                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "samplers") == 0)
5566                 {
5567                         i = cgltf_parse_json_samplers(options, tokens, i + 1, json_chunk, out_data);
5568                 }
5569                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "skins") == 0)
5570                 {
5571                         i = cgltf_parse_json_skins(options, tokens, i + 1, json_chunk, out_data);
5572                 }
5573                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "cameras") == 0)
5574                 {
5575                         i = cgltf_parse_json_cameras(options, tokens, i + 1, json_chunk, out_data);
5576                 }
5577                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "nodes") == 0)
5578                 {
5579                         i = cgltf_parse_json_nodes(options, tokens, i + 1, json_chunk, out_data);
5580                 }
5581                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "scenes") == 0)
5582                 {
5583                         i = cgltf_parse_json_scenes(options, tokens, i + 1, json_chunk, out_data);
5584                 }
5585                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "scene") == 0)
5586                 {
5587                         ++i;
5588                         out_data->scene = CGLTF_PTRINDEX(cgltf_scene, cgltf_json_to_int(tokens + i, json_chunk));
5589                         ++i;
5590                 }
5591                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "animations") == 0)
5592                 {
5593                         i = cgltf_parse_json_animations(options, tokens, i + 1, json_chunk, out_data);
5594                 }
5595                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "extras") == 0)
5596                 {
5597                         i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_data->extras);
5598                 }
5599                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
5600                 {
5601                         ++i;
5602
5603                         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5604                         if(out_data->data_extensions)
5605                         {
5606                                 return CGLTF_ERROR_JSON;
5607                         }
5608
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);
5612
5613                         if (!out_data->data_extensions)
5614                         {
5615                                 return CGLTF_ERROR_NOMEM;
5616                         }
5617
5618                         ++i;
5619
5620                         for (int k = 0; k < extensions_size; ++k)
5621                         {
5622                                 CGLTF_CHECK_KEY(tokens[i]);
5623
5624                                 if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_lights_punctual") == 0)
5625                                 {
5626                                         ++i;
5627
5628                                         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5629
5630                                         int data_size = tokens[i].size;
5631                                         ++i;
5632
5633                                         for (int m = 0; m < data_size; ++m)
5634                                         {
5635                                                 CGLTF_CHECK_KEY(tokens[i]);
5636
5637                                                 if (cgltf_json_strcmp(tokens + i, json_chunk, "lights") == 0)
5638                                                 {
5639                                                         i = cgltf_parse_json_lights(options, tokens, i + 1, json_chunk, out_data);
5640                                                 }
5641                                                 else
5642                                                 {
5643                                                         i = cgltf_skip_json(tokens, i + 1);
5644                                                 }
5645
5646                                                 if (i < 0)
5647                                                 {
5648                                                         return i;
5649                                                 }
5650                                         }
5651                                 }
5652                                 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_variants") == 0)
5653                                 {
5654                                         ++i;
5655
5656                                         CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5657
5658                                         int data_size = tokens[i].size;
5659                                         ++i;
5660
5661                                         for (int m = 0; m < data_size; ++m)
5662                                         {
5663                                                 CGLTF_CHECK_KEY(tokens[i]);
5664
5665                                                 if (cgltf_json_strcmp(tokens + i, json_chunk, "variants") == 0)
5666                                                 {
5667                                                         i = cgltf_parse_json_variants(options, tokens, i + 1, json_chunk, out_data);
5668                                                 }
5669                                                 else
5670                                                 {
5671                                                         i = cgltf_skip_json(tokens, i + 1);
5672                                                 }
5673
5674                                                 if (i < 0)
5675                                                 {
5676                                                         return i;
5677                                                 }
5678                                         }
5679                                 }
5680                                 else
5681                                 {
5682                                         i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_data->data_extensions[out_data->data_extensions_count++]));
5683                                 }
5684
5685                                 if (i < 0)
5686                                 {
5687                                         return i;
5688                                 }
5689                         }
5690                 }
5691                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensionsUsed") == 0)
5692                 {
5693                         i = cgltf_parse_json_string_array(options, tokens, i + 1, json_chunk, &out_data->extensions_used, &out_data->extensions_used_count);
5694                 }
5695                 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensionsRequired") == 0)
5696                 {
5697                         i = cgltf_parse_json_string_array(options, tokens, i + 1, json_chunk, &out_data->extensions_required, &out_data->extensions_required_count);
5698                 }
5699                 else
5700                 {
5701                         i = cgltf_skip_json(tokens, i + 1);
5702                 }
5703
5704                 if (i < 0)
5705                 {
5706                         return i;
5707                 }
5708         }
5709
5710         return i;
5711 }
5712
5713 cgltf_result cgltf_parse_json(cgltf_options* options, const uint8_t* json_chunk, cgltf_size size, cgltf_data** out_data)
5714 {
5715         jsmn_parser parser = { 0, 0, 0 };
5716
5717         if (options->json_token_count == 0)
5718         {
5719                 int token_count = jsmn_parse(&parser, (const char*)json_chunk, size, NULL, 0);
5720
5721                 if (token_count <= 0)
5722                 {
5723                         return cgltf_result_invalid_json;
5724                 }
5725
5726                 options->json_token_count = token_count;
5727         }
5728
5729         jsmntok_t* tokens = (jsmntok_t*)options->memory.alloc(options->memory.user_data, sizeof(jsmntok_t) * (options->json_token_count + 1));
5730
5731         if (!tokens)
5732         {
5733                 return cgltf_result_out_of_memory;
5734         }
5735
5736         jsmn_init(&parser);
5737
5738         int token_count = jsmn_parse(&parser, (const char*)json_chunk, size, tokens, options->json_token_count);
5739
5740         if (token_count <= 0)
5741         {
5742                 options->memory.free(options->memory.user_data, tokens);
5743                 return cgltf_result_invalid_json;
5744         }
5745
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;
5749
5750         cgltf_data* data = (cgltf_data*)options->memory.alloc(options->memory.user_data, sizeof(cgltf_data));
5751
5752         if (!data)
5753         {
5754                 options->memory.free(options->memory.user_data, tokens);
5755                 return cgltf_result_out_of_memory;
5756         }
5757
5758         memset(data, 0, sizeof(cgltf_data));
5759         data->memory = options->memory;
5760         data->file = options->file;
5761
5762         int i = cgltf_parse_json_root(options, tokens, 0, json_chunk, data);
5763
5764         options->memory.free(options->memory.user_data, tokens);
5765
5766         if (i < 0)
5767         {
5768                 cgltf_free(data);
5769
5770                 switch (i)
5771                 {
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;
5775                 }
5776         }
5777
5778         if (cgltf_fixup_pointers(data) < 0)
5779         {
5780                 cgltf_free(data);
5781                 return cgltf_result_invalid_gltf;
5782         }
5783
5784         data->json = (const char*)json_chunk;
5785         data->json_size = size;
5786
5787         *out_data = data;
5788
5789         return cgltf_result_success;
5790 }
5791
5792 static int cgltf_fixup_pointers(cgltf_data* data)
5793 {
5794         for (cgltf_size i = 0; i < data->meshes_count; ++i)
5795         {
5796                 for (cgltf_size j = 0; j < data->meshes[i].primitives_count; ++j)
5797                 {
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);
5800
5801                         for (cgltf_size k = 0; k < data->meshes[i].primitives[j].attributes_count; ++k)
5802                         {
5803                                 CGLTF_PTRFIXUP_REQ(data->meshes[i].primitives[j].attributes[k].data, data->accessors, data->accessors_count);
5804                         }
5805
5806                         for (cgltf_size k = 0; k < data->meshes[i].primitives[j].targets_count; ++k)
5807                         {
5808                                 for (cgltf_size m = 0; m < data->meshes[i].primitives[j].targets[k].attributes_count; ++m)
5809                                 {
5810                                         CGLTF_PTRFIXUP_REQ(data->meshes[i].primitives[j].targets[k].attributes[m].data, data->accessors, data->accessors_count);
5811                                 }
5812                         }
5813
5814                         if (data->meshes[i].primitives[j].has_draco_mesh_compression)
5815                         {
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)
5818                                 {
5819                                         CGLTF_PTRFIXUP_REQ(data->meshes[i].primitives[j].draco_mesh_compression.attributes[m].data, data->accessors, data->accessors_count);
5820                                 }
5821                         }
5822
5823                         for (cgltf_size k = 0; k < data->meshes[i].primitives[j].mappings_count; ++k)
5824                         {
5825                                 CGLTF_PTRFIXUP_REQ(data->meshes[i].primitives[j].mappings[k].material, data->materials, data->materials_count);
5826                         }
5827                 }
5828         }
5829
5830         for (cgltf_size i = 0; i < data->accessors_count; ++i)
5831         {
5832                 CGLTF_PTRFIXUP(data->accessors[i].buffer_view, data->buffer_views, data->buffer_views_count);
5833
5834                 if (data->accessors[i].is_sparse)
5835                 {
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);
5838                 }
5839
5840                 if (data->accessors[i].buffer_view)
5841                 {
5842                         data->accessors[i].stride = data->accessors[i].buffer_view->stride;
5843                 }
5844
5845                 if (data->accessors[i].stride == 0)
5846                 {
5847                         data->accessors[i].stride = cgltf_calc_size(data->accessors[i].type, data->accessors[i].component_type);
5848                 }
5849         }
5850
5851         for (cgltf_size i = 0; i < data->textures_count; ++i)
5852         {
5853                 CGLTF_PTRFIXUP(data->textures[i].image, data->images, data->images_count);
5854                 CGLTF_PTRFIXUP(data->textures[i].sampler, data->samplers, data->samplers_count);
5855         }
5856
5857         for (cgltf_size i = 0; i < data->images_count; ++i)
5858         {
5859                 CGLTF_PTRFIXUP(data->images[i].buffer_view, data->buffer_views, data->buffer_views_count);
5860         }
5861
5862         for (cgltf_size i = 0; i < data->materials_count; ++i)
5863         {
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);
5867
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);
5870
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);
5873
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);
5877
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);
5880
5881                 CGLTF_PTRFIXUP(data->materials[i].transmission.transmission_texture.texture, data->textures, data->textures_count);
5882
5883                 CGLTF_PTRFIXUP(data->materials[i].volume.thickness_texture.texture, data->textures, data->textures_count);
5884
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);
5887         }
5888
5889         for (cgltf_size i = 0; i < data->buffer_views_count; ++i)
5890         {
5891                 CGLTF_PTRFIXUP_REQ(data->buffer_views[i].buffer, data->buffers, data->buffers_count);
5892
5893                 if (data->buffer_views[i].has_meshopt_compression)
5894                 {
5895                         CGLTF_PTRFIXUP_REQ(data->buffer_views[i].meshopt_compression.buffer, data->buffers, data->buffers_count);
5896                 }
5897         }
5898
5899         for (cgltf_size i = 0; i < data->skins_count; ++i)
5900         {
5901                 for (cgltf_size j = 0; j < data->skins[i].joints_count; ++j)
5902                 {
5903                         CGLTF_PTRFIXUP_REQ(data->skins[i].joints[j], data->nodes, data->nodes_count);
5904                 }
5905
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);
5908         }
5909
5910         for (cgltf_size i = 0; i < data->nodes_count; ++i)
5911         {
5912                 for (cgltf_size j = 0; j < data->nodes[i].children_count; ++j)
5913                 {
5914                         CGLTF_PTRFIXUP_REQ(data->nodes[i].children[j], data->nodes, data->nodes_count);
5915
5916                         if (data->nodes[i].children[j]->parent)
5917                         {
5918                                 return CGLTF_ERROR_JSON;
5919                         }
5920
5921                         data->nodes[i].children[j]->parent = &data->nodes[i];
5922                 }
5923
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);
5928         }
5929
5930         for (cgltf_size i = 0; i < data->scenes_count; ++i)
5931         {
5932                 for (cgltf_size j = 0; j < data->scenes[i].nodes_count; ++j)
5933                 {
5934                         CGLTF_PTRFIXUP_REQ(data->scenes[i].nodes[j], data->nodes, data->nodes_count);
5935
5936                         if (data->scenes[i].nodes[j]->parent)
5937                         {
5938                                 return CGLTF_ERROR_JSON;
5939                         }
5940                 }
5941         }
5942
5943         CGLTF_PTRFIXUP(data->scene, data->scenes, data->scenes_count);
5944
5945         for (cgltf_size i = 0; i < data->animations_count; ++i)
5946         {
5947                 for (cgltf_size j = 0; j < data->animations[i].samplers_count; ++j)
5948                 {
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);
5951                 }
5952
5953                 for (cgltf_size j = 0; j < data->animations[i].channels_count; ++j)
5954                 {
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);
5957                 }
5958         }
5959
5960         return 0;
5961 }
5962
5963 /*
5964  * -- jsmn.c start --
5965  * Source: https://github.com/zserge/jsmn
5966  * License: MIT
5967  *
5968  * Copyright (c) 2010 Serge A. Zaitsev
5969
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:
5976
5977  * The above copyright notice and this permission notice shall be included in
5978  * all copies or substantial portions of the Software.
5979
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
5986  * THE SOFTWARE.
5987  */
5988
5989 /**
5990  * Allocates a fresh unused token from the token pull.
5991  */
5992 static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser,
5993                                    jsmntok_t *tokens, size_t num_tokens) {
5994         jsmntok_t *tok;
5995         if (parser->toknext >= num_tokens) {
5996                 return NULL;
5997         }
5998         tok = &tokens[parser->toknext++];
5999         tok->start = tok->end = -1;
6000         tok->size = 0;
6001 #ifdef JSMN_PARENT_LINKS
6002         tok->parent = -1;
6003 #endif
6004         return tok;
6005 }
6006
6007 /**
6008  * Fills token type and boundaries.
6009  */
6010 static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type,
6011                                 int start, int end) {
6012         token->type = type;
6013         token->start = start;
6014         token->end = end;
6015         token->size = 0;
6016 }
6017
6018 /**
6019  * Fills next available token with JSON primitive.
6020  */
6021 static int jsmn_parse_primitive(jsmn_parser *parser, const char *js,
6022                                 size_t len, jsmntok_t *tokens, size_t num_tokens) {
6023         jsmntok_t *token;
6024         int start;
6025
6026         start = parser->pos;
6027
6028         for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
6029                 switch (js[parser->pos]) {
6030 #ifndef JSMN_STRICT
6031                 /* In strict mode primitive must be followed by "," or "}" or "]" */
6032                 case ':':
6033 #endif
6034                 case '\t' : case '\r' : case '\n' : case ' ' :
6035                 case ','  : case ']'  : case '}' :
6036                         goto found;
6037                 }
6038                 if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
6039                         parser->pos = start;
6040                         return JSMN_ERROR_INVAL;
6041                 }
6042         }
6043 #ifdef JSMN_STRICT
6044         /* In strict mode primitive must be followed by a comma/object/array */
6045         parser->pos = start;
6046         return JSMN_ERROR_PART;
6047 #endif
6048
6049 found:
6050         if (tokens == NULL) {
6051                 parser->pos--;
6052                 return 0;
6053         }
6054         token = jsmn_alloc_token(parser, tokens, num_tokens);
6055         if (token == NULL) {
6056                 parser->pos = start;
6057                 return JSMN_ERROR_NOMEM;
6058         }
6059         jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
6060 #ifdef JSMN_PARENT_LINKS
6061         token->parent = parser->toksuper;
6062 #endif
6063         parser->pos--;
6064         return 0;
6065 }
6066
6067 /**
6068  * Fills next token with JSON string.
6069  */
6070 static int jsmn_parse_string(jsmn_parser *parser, const char *js,
6071                                  size_t len, jsmntok_t *tokens, size_t num_tokens) {
6072         jsmntok_t *token;
6073
6074         int start = parser->pos;
6075
6076         parser->pos++;
6077
6078         /* Skip starting quote */
6079         for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
6080                 char c = js[parser->pos];
6081
6082                 /* Quote: end of string */
6083                 if (c == '\"') {
6084                         if (tokens == NULL) {
6085                                 return 0;
6086                         }
6087                         token = jsmn_alloc_token(parser, tokens, num_tokens);
6088                         if (token == NULL) {
6089                                 parser->pos = start;
6090                                 return JSMN_ERROR_NOMEM;
6091                         }
6092                         jsmn_fill_token(token, JSMN_STRING, start+1, parser->pos);
6093 #ifdef JSMN_PARENT_LINKS
6094                         token->parent = parser->toksuper;
6095 #endif
6096                         return 0;
6097                 }
6098
6099                 /* Backslash: Quoted symbol expected */
6100                 if (c == '\\' && parser->pos + 1 < len) {
6101                         int i;
6102                         parser->pos++;
6103                         switch (js[parser->pos]) {
6104                         /* Allowed escaped symbols */
6105                         case '\"': case '/' : case '\\' : case 'b' :
6106                         case 'f' : case 'r' : case 'n'  : case 't' :
6107                                 break;
6108                                 /* Allows escaped symbol \uXXXX */
6109                         case 'u':
6110                                 parser->pos++;
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;
6118                                         }
6119                                         parser->pos++;
6120                                 }
6121                                 parser->pos--;
6122                                 break;
6123                                 /* Unexpected symbol */
6124                         default:
6125                                 parser->pos = start;
6126                                 return JSMN_ERROR_INVAL;
6127                         }
6128                 }
6129         }
6130         parser->pos = start;
6131         return JSMN_ERROR_PART;
6132 }
6133
6134 /**
6135  * Parse JSON string and fill tokens.
6136  */
6137 static int jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
6138                    jsmntok_t *tokens, size_t num_tokens) {
6139         int r;
6140         int i;
6141         jsmntok_t *token;
6142         int count = parser->toknext;
6143
6144         for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
6145                 char c;
6146                 jsmntype_t type;
6147
6148                 c = js[parser->pos];
6149                 switch (c) {
6150                 case '{': case '[':
6151                         count++;
6152                         if (tokens == NULL) {
6153                                 break;
6154                         }
6155                         token = jsmn_alloc_token(parser, tokens, num_tokens);
6156                         if (token == NULL)
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;
6162 #endif
6163                         }
6164                         token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
6165                         token->start = parser->pos;
6166                         parser->toksuper = parser->toknext - 1;
6167                         break;
6168                 case '}': case ']':
6169                         if (tokens == NULL)
6170                                 break;
6171                         type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
6172 #ifdef JSMN_PARENT_LINKS
6173                         if (parser->toknext < 1) {
6174                                 return JSMN_ERROR_INVAL;
6175                         }
6176                         token = &tokens[parser->toknext - 1];
6177                         for (;;) {
6178                                 if (token->start != -1 && token->end == -1) {
6179                                         if (token->type != type) {
6180                                                 return JSMN_ERROR_INVAL;
6181                                         }
6182                                         token->end = parser->pos + 1;
6183                                         parser->toksuper = token->parent;
6184                                         break;
6185                                 }
6186                                 if (token->parent == -1) {
6187                                         if(token->type != type || parser->toksuper == -1) {
6188                                                 return JSMN_ERROR_INVAL;
6189                                         }
6190                                         break;
6191                                 }
6192                                 token = &tokens[token->parent];
6193                         }
6194 #else
6195                         for (i = parser->toknext - 1; i >= 0; i--) {
6196                                 token = &tokens[i];
6197                                 if (token->start != -1 && token->end == -1) {
6198                                         if (token->type != type) {
6199                                                 return JSMN_ERROR_INVAL;
6200                                         }
6201                                         parser->toksuper = -1;
6202                                         token->end = parser->pos + 1;
6203                                         break;
6204                                 }
6205                         }
6206                         /* Error if unmatched closing bracket */
6207                         if (i == -1) return JSMN_ERROR_INVAL;
6208                         for (; i >= 0; i--) {
6209                                 token = &tokens[i];
6210                                 if (token->start != -1 && token->end == -1) {
6211                                         parser->toksuper = i;
6212                                         break;
6213                                 }
6214                         }
6215 #endif
6216                         break;
6217                 case '\"':
6218                         r = jsmn_parse_string(parser, js, len, tokens, num_tokens);
6219                         if (r < 0) return r;
6220                         count++;
6221                         if (parser->toksuper != -1 && tokens != NULL)
6222                                 tokens[parser->toksuper].size++;
6223                         break;
6224                 case '\t' : case '\r' : case '\n' : case ' ':
6225                         break;
6226                 case ':':
6227                         parser->toksuper = parser->toknext - 1;
6228                         break;
6229                 case ',':
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;
6235 #else
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;
6240                                                         break;
6241                                                 }
6242                                         }
6243                                 }
6244 #endif
6245                         }
6246                         break;
6247 #ifdef JSMN_STRICT
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;
6258                                 }
6259                         }
6260 #else
6261                         /* In non-strict mode every unquoted value is a primitive */
6262                 default:
6263 #endif
6264                         r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens);
6265                         if (r < 0) return r;
6266                         count++;
6267                         if (parser->toksuper != -1 && tokens != NULL)
6268                                 tokens[parser->toksuper].size++;
6269                         break;
6270
6271 #ifdef JSMN_STRICT
6272                         /* Unexpected char in strict mode */
6273                 default:
6274                         return JSMN_ERROR_INVAL;
6275 #endif
6276                 }
6277         }
6278
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;
6284                         }
6285                 }
6286         }
6287
6288         return count;
6289 }
6290
6291 /**
6292  * Creates a new parser based over a given  buffer with an array of tokens
6293  * available.
6294  */
6295 static void jsmn_init(jsmn_parser *parser) {
6296         parser->pos = 0;
6297         parser->toknext = 0;
6298         parser->toksuper = -1;
6299 }
6300 /*
6301  * -- jsmn.c end --
6302  */
6303
6304 #endif /* #ifdef CGLTF_IMPLEMENTATION */
6305
6306 /* cgltf is distributed under MIT license:
6307  *
6308  * Copyright (c) 2018 Johannes Kuhlmann
6309
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:
6316
6317  * The above copyright notice and this permission notice shall be included in all
6318  * copies or substantial portions of the Software.
6319
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
6326  * SOFTWARE.
6327  */