Debian Codegraph @

src/external/cgltf.h

raw symbols SCIP
1 /**
2 * cgltf - a single-file glTF 2.0 parser written in C99.
3 *
4 * Version: 1.15
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_unpack_indices` reads in the index data from an accessor. Assumes that
67 * `cgltf_load_buffers` has already been called. By passing null for the output pointer, users can
68 * find out how many indices are required in the output buffer. Returns 0 if the accessor is
69 * sparse or if the output component size is less than the accessor's component size.
70 *
71 * `cgltf_num_components` is a tiny utility that tells you the dimensionality of
72 * a certain accessor type. This can be used before `cgltf_accessor_unpack_floats` to help allocate
73 * the necessary amount of memory. `cgltf_component_size` and `cgltf_calc_size` exist for
74 * similar purposes.
75 *
76 * `cgltf_accessor_read_float` reads a certain element from a non-sparse accessor and converts it to
77 * floating point, assuming that `cgltf_load_buffers` has already been called. The passed-in element
78 * size is the number of floats in the output buffer, which should be in the range [1, 16]. Returns
79 * false if the passed-in element_size is too small, or if the accessor is sparse.
80 *
81 * `cgltf_accessor_read_uint` is similar to its floating-point counterpart, but limited to reading
82 * vector types and does not support matrix types. The passed-in element size is the number of uints
83 * in the output buffer, which should be in the range [1, 4]. Returns false if the passed-in
84 * element_size is too small, or if the accessor is sparse.
85 *
86 * `cgltf_accessor_read_index` is similar to its floating-point counterpart, but it returns size_t
87 * and only works with single-component data types.
88 *
89 * `cgltf_copy_extras_json` allows users to retrieve the "extras" data that can be attached to many
90 * glTF objects (which can be arbitrary JSON data). This is a legacy function, consider using
91 * cgltf_extras::data directly instead. You can parse this data using your own JSON parser
92 * or, if you've included the cgltf implementation using the integrated JSMN JSON parser.
93 */
94 #ifndef CGLTF_H_INCLUDED__
95 #define CGLTF_H_INCLUDED__
96
97 #include <stddef.h>
98 #include <stdint.h> /* For uint8_t, uint32_t */
99
100 #ifdef __cplusplus
101 extern "C" {
102 #endif
103
104 typedef size_t cgltf_size;
105 typedef long long int cgltf_ssize;
106 typedef float cgltf_float;
107 typedef int cgltf_int;
108 typedef unsigned int cgltf_uint;
109 typedef int cgltf_bool;
110
111 typedef enum cgltf_file_type
112 {
113 cgltf_file_type_invalid,
114 cgltf_file_type_gltf,
115 cgltf_file_type_glb,
116 cgltf_file_type_max_enum
117 } cgltf_file_type;
118
119 typedef enum cgltf_result
120 {
121 cgltf_result_success,
122 cgltf_result_data_too_short,
123 cgltf_result_unknown_format,
124 cgltf_result_invalid_json,
125 cgltf_result_invalid_gltf,
126 cgltf_result_invalid_options,
127 cgltf_result_file_not_found,
128 cgltf_result_io_error,
129 cgltf_result_out_of_memory,
130 cgltf_result_legacy_gltf,
131 cgltf_result_max_enum
132 } cgltf_result;
133
134 typedef struct cgltf_memory_options
135 {
136 void* (*alloc_func)(void* user, cgltf_size size);
137 void (*free_func) (void* user, void* ptr);
138 void* user_data;
139 } cgltf_memory_options;
140
141 typedef struct cgltf_file_options
142 {
143 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);
144 void (*release)(const struct cgltf_memory_options* memory_options, const struct cgltf_file_options* file_options, void* data, cgltf_size size);
145 void* user_data;
146 } cgltf_file_options;
147
148 typedef struct cgltf_options
149 {
150 cgltf_file_type type; /* invalid == auto detect */
151 cgltf_size json_token_count; /* 0 == auto */
152 cgltf_memory_options memory;
153 cgltf_file_options file;
154 } cgltf_options;
155
156 typedef enum cgltf_buffer_view_type
157 {
158 cgltf_buffer_view_type_invalid,
159 cgltf_buffer_view_type_indices,
160 cgltf_buffer_view_type_vertices,
161 cgltf_buffer_view_type_max_enum
162 } cgltf_buffer_view_type;
163
164 typedef enum cgltf_attribute_type
165 {
166 cgltf_attribute_type_invalid,
167 cgltf_attribute_type_position,
168 cgltf_attribute_type_normal,
169 cgltf_attribute_type_tangent,
170 cgltf_attribute_type_texcoord,
171 cgltf_attribute_type_color,
172 cgltf_attribute_type_joints,
173 cgltf_attribute_type_weights,
174 cgltf_attribute_type_custom,
175 cgltf_attribute_type_max_enum
176 } cgltf_attribute_type;
177
178 typedef enum cgltf_component_type
179 {
180 cgltf_component_type_invalid,
181 cgltf_component_type_r_8, /* BYTE */
182 cgltf_component_type_r_8u, /* UNSIGNED_BYTE */
183 cgltf_component_type_r_16, /* SHORT */
184 cgltf_component_type_r_16u, /* UNSIGNED_SHORT */
185 cgltf_component_type_r_32u, /* UNSIGNED_INT */
186 cgltf_component_type_r_32f, /* FLOAT */
187 cgltf_component_type_max_enum
188 } cgltf_component_type;
189
190 typedef enum cgltf_type
191 {
192 cgltf_type_invalid,
193 cgltf_type_scalar,
194 cgltf_type_vec2,
195 cgltf_type_vec3,
196 cgltf_type_vec4,
197 cgltf_type_mat2,
198 cgltf_type_mat3,
199 cgltf_type_mat4,
200 cgltf_type_max_enum
201 } cgltf_type;
202
203 typedef enum cgltf_primitive_type
204 {
205 cgltf_primitive_type_invalid,
206 cgltf_primitive_type_points,
207 cgltf_primitive_type_lines,
208 cgltf_primitive_type_line_loop,
209 cgltf_primitive_type_line_strip,
210 cgltf_primitive_type_triangles,
211 cgltf_primitive_type_triangle_strip,
212 cgltf_primitive_type_triangle_fan,
213 cgltf_primitive_type_max_enum
214 } cgltf_primitive_type;
215
216 typedef enum cgltf_alpha_mode
217 {
218 cgltf_alpha_mode_opaque,
219 cgltf_alpha_mode_mask,
220 cgltf_alpha_mode_blend,
221 cgltf_alpha_mode_max_enum
222 } cgltf_alpha_mode;
223
224 typedef enum cgltf_animation_path_type {
225 cgltf_animation_path_type_invalid,
226 cgltf_animation_path_type_translation,
227 cgltf_animation_path_type_rotation,
228 cgltf_animation_path_type_scale,
229 cgltf_animation_path_type_weights,
230 cgltf_animation_path_type_max_enum
231 } cgltf_animation_path_type;
232
233 typedef enum cgltf_interpolation_type {
234 cgltf_interpolation_type_linear,
235 cgltf_interpolation_type_step,
236 cgltf_interpolation_type_cubic_spline,
237 cgltf_interpolation_type_max_enum
238 } cgltf_interpolation_type;
239
240 typedef enum cgltf_camera_type {
241 cgltf_camera_type_invalid,
242 cgltf_camera_type_perspective,
243 cgltf_camera_type_orthographic,
244 cgltf_camera_type_max_enum
245 } cgltf_camera_type;
246
247 typedef enum cgltf_light_type {
248 cgltf_light_type_invalid,
249 cgltf_light_type_directional,
250 cgltf_light_type_point,
251 cgltf_light_type_spot,
252 cgltf_light_type_max_enum
253 } cgltf_light_type;
254
255 typedef enum cgltf_data_free_method {
256 cgltf_data_free_method_none,
257 cgltf_data_free_method_file_release,
258 cgltf_data_free_method_memory_free,
259 cgltf_data_free_method_max_enum
260 } cgltf_data_free_method;
261
262 typedef struct cgltf_extras {
263 cgltf_size start_offset; /* this field is deprecated and will be removed in the future; use data instead */
264 cgltf_size end_offset; /* this field is deprecated and will be removed in the future; use data instead */
265
266 char* data;
267 } cgltf_extras;
268
269 typedef struct cgltf_extension {
270 char* name;
271 char* data;
272 } cgltf_extension;
273
274 typedef struct cgltf_buffer
275 {
276 char* name;
277 cgltf_size size;
278 char* uri;
279 void* data; /* loaded by cgltf_load_buffers */
280 cgltf_data_free_method data_free_method;
281 cgltf_extras extras;
282 cgltf_size extensions_count;
283 cgltf_extension* extensions;
284 } cgltf_buffer;
285
286 typedef enum cgltf_meshopt_compression_mode {
287 cgltf_meshopt_compression_mode_invalid,
288 cgltf_meshopt_compression_mode_attributes,
289 cgltf_meshopt_compression_mode_triangles,
290 cgltf_meshopt_compression_mode_indices,
291 cgltf_meshopt_compression_mode_max_enum
292 } cgltf_meshopt_compression_mode;
293
294 typedef enum cgltf_meshopt_compression_filter {
295 cgltf_meshopt_compression_filter_none,
296 cgltf_meshopt_compression_filter_octahedral,
297 cgltf_meshopt_compression_filter_quaternion,
298 cgltf_meshopt_compression_filter_exponential,
299 cgltf_meshopt_compression_filter_color,
300 cgltf_meshopt_compression_filter_max_enum
301 } cgltf_meshopt_compression_filter;
302
303 typedef struct cgltf_meshopt_compression
304 {
305 cgltf_buffer* buffer;
306 cgltf_size offset;
307 cgltf_size size;
308 cgltf_size stride;
309 cgltf_size count;
310 cgltf_meshopt_compression_mode mode;
311 cgltf_meshopt_compression_filter filter;
312 cgltf_bool is_khr;
313 } cgltf_meshopt_compression;
314
315 typedef struct cgltf_buffer_view
316 {
317 char *name;
318 cgltf_buffer* buffer;
319 cgltf_size offset;
320 cgltf_size size;
321 cgltf_size stride; /* 0 == automatically determined by accessor */
322 cgltf_buffer_view_type type;
323 void* data; /* overrides buffer->data if present, filled by extensions */
324 cgltf_bool has_meshopt_compression;
325 cgltf_meshopt_compression meshopt_compression;
326 cgltf_extras extras;
327 cgltf_size extensions_count;
328 cgltf_extension* extensions;
329 } cgltf_buffer_view;
330
331 typedef struct cgltf_accessor_sparse
332 {
333 cgltf_size count;
334 cgltf_buffer_view* indices_buffer_view;
335 cgltf_size indices_byte_offset;
336 cgltf_component_type indices_component_type;
337 cgltf_buffer_view* values_buffer_view;
338 cgltf_size values_byte_offset;
339 } cgltf_accessor_sparse;
340
341 typedef struct cgltf_accessor
342 {
343 char* name;
344 cgltf_component_type component_type;
345 cgltf_bool normalized;
346 cgltf_type type;
347 cgltf_size offset;
348 cgltf_size count;
349 cgltf_size stride;
350 cgltf_buffer_view* buffer_view;
351 cgltf_bool has_min;
352 cgltf_float min[16];
353 cgltf_bool has_max;
354 cgltf_float max[16];
355 cgltf_bool is_sparse;
356 cgltf_accessor_sparse sparse;
357 cgltf_extras extras;
358 cgltf_size extensions_count;
359 cgltf_extension* extensions;
360 } cgltf_accessor;
361
362 typedef struct cgltf_attribute
363 {
364 char* name;
365 cgltf_attribute_type type;
366 cgltf_int index;
367 cgltf_accessor* data;
368 } cgltf_attribute;
369
370 typedef struct cgltf_image
371 {
372 char* name;
373 char* uri;
374 cgltf_buffer_view* buffer_view;
375 char* mime_type;
376 cgltf_extras extras;
377 cgltf_size extensions_count;
378 cgltf_extension* extensions;
379 } cgltf_image;
380
381 typedef enum cgltf_filter_type {
382 cgltf_filter_type_undefined = 0,
383 cgltf_filter_type_nearest = 9728,
384 cgltf_filter_type_linear = 9729,
385 cgltf_filter_type_nearest_mipmap_nearest = 9984,
386 cgltf_filter_type_linear_mipmap_nearest = 9985,
387 cgltf_filter_type_nearest_mipmap_linear = 9986,
388 cgltf_filter_type_linear_mipmap_linear = 9987
389 } cgltf_filter_type;
390
391 typedef enum cgltf_wrap_mode {
392 cgltf_wrap_mode_clamp_to_edge = 33071,
393 cgltf_wrap_mode_mirrored_repeat = 33648,
394 cgltf_wrap_mode_repeat = 10497
395 } cgltf_wrap_mode;
396
397 typedef struct cgltf_sampler
398 {
399 char* name;
400 cgltf_filter_type mag_filter;
401 cgltf_filter_type min_filter;
402 cgltf_wrap_mode wrap_s;
403 cgltf_wrap_mode wrap_t;
404 cgltf_extras extras;
405 cgltf_size extensions_count;
406 cgltf_extension* extensions;
407 } cgltf_sampler;
408
409 typedef struct cgltf_texture
410 {
411 char* name;
412 cgltf_image* image;
413 cgltf_sampler* sampler;
414 cgltf_bool has_basisu;
415 cgltf_image* basisu_image;
416 cgltf_bool has_webp;
417 cgltf_image* webp_image;
418 cgltf_extras extras;
419 cgltf_size extensions_count;
420 cgltf_extension* extensions;
421 } cgltf_texture;
422
423 typedef struct cgltf_texture_transform
424 {
425 cgltf_float offset[2];
426 cgltf_float rotation;
427 cgltf_float scale[2];
428 cgltf_bool has_texcoord;
429 cgltf_int texcoord;
430 } cgltf_texture_transform;
431
432 typedef struct cgltf_texture_view
433 {
434 cgltf_texture* texture;
435 cgltf_int texcoord;
436 cgltf_float scale; /* equivalent to strength for occlusion_texture */
437 cgltf_bool has_transform;
438 cgltf_texture_transform transform;
439 } cgltf_texture_view;
440
441 typedef struct cgltf_pbr_metallic_roughness
442 {
443 cgltf_texture_view base_color_texture;
444 cgltf_texture_view metallic_roughness_texture;
445
446 cgltf_float base_color_factor[4];
447 cgltf_float metallic_factor;
448 cgltf_float roughness_factor;
449 } cgltf_pbr_metallic_roughness;
450
451 typedef struct cgltf_pbr_specular_glossiness
452 {
453 cgltf_texture_view diffuse_texture;
454 cgltf_texture_view specular_glossiness_texture;
455
456 cgltf_float diffuse_factor[4];
457 cgltf_float specular_factor[3];
458 cgltf_float glossiness_factor;
459 } cgltf_pbr_specular_glossiness;
460
461 typedef struct cgltf_clearcoat
462 {
463 cgltf_texture_view clearcoat_texture;
464 cgltf_texture_view clearcoat_roughness_texture;
465 cgltf_texture_view clearcoat_normal_texture;
466
467 cgltf_float clearcoat_factor;
468 cgltf_float clearcoat_roughness_factor;
469 } cgltf_clearcoat;
470
471 typedef struct cgltf_transmission
472 {
473 cgltf_texture_view transmission_texture;
474 cgltf_float transmission_factor;
475 } cgltf_transmission;
476
477 typedef struct cgltf_ior
478 {
479 cgltf_float ior;
480 } cgltf_ior;
481
482 typedef struct cgltf_specular
483 {
484 cgltf_texture_view specular_texture;
485 cgltf_texture_view specular_color_texture;
486 cgltf_float specular_color_factor[3];
487 cgltf_float specular_factor;
488 } cgltf_specular;
489
490 typedef struct cgltf_volume
491 {
492 cgltf_texture_view thickness_texture;
493 cgltf_float thickness_factor;
494 cgltf_float attenuation_color[3];
495 cgltf_float attenuation_distance;
496 } cgltf_volume;
497
498 typedef struct cgltf_sheen
499 {
500 cgltf_texture_view sheen_color_texture;
501 cgltf_float sheen_color_factor[3];
502 cgltf_texture_view sheen_roughness_texture;
503 cgltf_float sheen_roughness_factor;
504 } cgltf_sheen;
505
506 typedef struct cgltf_emissive_strength
507 {
508 cgltf_float emissive_strength;
509 } cgltf_emissive_strength;
510
511 typedef struct cgltf_iridescence
512 {
513 cgltf_float iridescence_factor;
514 cgltf_texture_view iridescence_texture;
515 cgltf_float iridescence_ior;
516 cgltf_float iridescence_thickness_min;
517 cgltf_float iridescence_thickness_max;
518 cgltf_texture_view iridescence_thickness_texture;
519 } cgltf_iridescence;
520
521 typedef struct cgltf_diffuse_transmission
522 {
523 cgltf_texture_view diffuse_transmission_texture;
524 cgltf_float diffuse_transmission_factor;
525 cgltf_float diffuse_transmission_color_factor[3];
526 cgltf_texture_view diffuse_transmission_color_texture;
527 } cgltf_diffuse_transmission;
528
529 typedef struct cgltf_anisotropy
530 {
531 cgltf_float anisotropy_strength;
532 cgltf_float anisotropy_rotation;
533 cgltf_texture_view anisotropy_texture;
534 } cgltf_anisotropy;
535
536 typedef struct cgltf_dispersion
537 {
538 cgltf_float dispersion;
539 } cgltf_dispersion;
540
541 typedef struct cgltf_material
542 {
543 char* name;
544 cgltf_bool has_pbr_metallic_roughness;
545 cgltf_bool has_pbr_specular_glossiness;
546 cgltf_bool has_clearcoat;
547 cgltf_bool has_transmission;
548 cgltf_bool has_volume;
549 cgltf_bool has_ior;
550 cgltf_bool has_specular;
551 cgltf_bool has_sheen;
552 cgltf_bool has_emissive_strength;
553 cgltf_bool has_iridescence;
554 cgltf_bool has_diffuse_transmission;
555 cgltf_bool has_anisotropy;
556 cgltf_bool has_dispersion;
557 cgltf_pbr_metallic_roughness pbr_metallic_roughness;
558 cgltf_pbr_specular_glossiness pbr_specular_glossiness;
559 cgltf_clearcoat clearcoat;
560 cgltf_ior ior;
561 cgltf_specular specular;
562 cgltf_sheen sheen;
563 cgltf_transmission transmission;
564 cgltf_volume volume;
565 cgltf_emissive_strength emissive_strength;
566 cgltf_iridescence iridescence;
567 cgltf_diffuse_transmission diffuse_transmission;
568 cgltf_anisotropy anisotropy;
569 cgltf_dispersion dispersion;
570 cgltf_texture_view normal_texture;
571 cgltf_texture_view occlusion_texture;
572 cgltf_texture_view emissive_texture;
573 cgltf_float emissive_factor[3];
574 cgltf_alpha_mode alpha_mode;
575 cgltf_float alpha_cutoff;
576 cgltf_bool double_sided;
577 cgltf_bool unlit;
578 cgltf_extras extras;
579 cgltf_size extensions_count;
580 cgltf_extension* extensions;
581 } cgltf_material;
582
583 typedef struct cgltf_material_mapping
584 {
585 cgltf_size variant;
586 cgltf_material* material;
587 cgltf_extras extras;
588 } cgltf_material_mapping;
589
590 typedef struct cgltf_morph_target {
591 cgltf_attribute* attributes;
592 cgltf_size attributes_count;
593 } cgltf_morph_target;
594
595 typedef struct cgltf_draco_mesh_compression {
596 cgltf_buffer_view* buffer_view;
597 cgltf_attribute* attributes;
598 cgltf_size attributes_count;
599 } cgltf_draco_mesh_compression;
600
601 typedef struct cgltf_mesh_gpu_instancing {
602 cgltf_attribute* attributes;
603 cgltf_size attributes_count;
604 } cgltf_mesh_gpu_instancing;
605
606 typedef struct cgltf_primitive {
607 cgltf_primitive_type type;
608 cgltf_accessor* indices;
609 cgltf_material* material;
610 cgltf_attribute* attributes;
611 cgltf_size attributes_count;
612 cgltf_morph_target* targets;
613 cgltf_size targets_count;
614 cgltf_extras extras;
615 cgltf_bool has_draco_mesh_compression;
616 cgltf_draco_mesh_compression draco_mesh_compression;
617 cgltf_material_mapping* mappings;
618 cgltf_size mappings_count;
619 cgltf_size extensions_count;
620 cgltf_extension* extensions;
621 } cgltf_primitive;
622
623 typedef struct cgltf_mesh {
624 char* name;
625 cgltf_primitive* primitives;
626 cgltf_size primitives_count;
627 cgltf_float* weights;
628 cgltf_size weights_count;
629 char** target_names;
630 cgltf_size target_names_count;
631 cgltf_extras extras;
632 cgltf_size extensions_count;
633 cgltf_extension* extensions;
634 } cgltf_mesh;
635
636 typedef struct cgltf_node cgltf_node;
637
638 typedef struct cgltf_skin {
639 char* name;
640 cgltf_node** joints;
641 cgltf_size joints_count;
642 cgltf_node* skeleton;
643 cgltf_accessor* inverse_bind_matrices;
644 cgltf_extras extras;
645 cgltf_size extensions_count;
646 cgltf_extension* extensions;
647 } cgltf_skin;
648
649 typedef struct cgltf_camera_perspective {
650 cgltf_bool has_aspect_ratio;
651 cgltf_float aspect_ratio;
652 cgltf_float yfov;
653 cgltf_bool has_zfar;
654 cgltf_float zfar;
655 cgltf_float znear;
656 cgltf_extras extras;
657 } cgltf_camera_perspective;
658
659 typedef struct cgltf_camera_orthographic {
660 cgltf_float xmag;
661 cgltf_float ymag;
662 cgltf_float zfar;
663 cgltf_float znear;
664 cgltf_extras extras;
665 } cgltf_camera_orthographic;
666
667 typedef struct cgltf_camera {
668 char* name;
669 cgltf_camera_type type;
670 union {
671 cgltf_camera_perspective perspective;
672 cgltf_camera_orthographic orthographic;
673 } data;
674 cgltf_extras extras;
675 cgltf_size extensions_count;
676 cgltf_extension* extensions;
677 } cgltf_camera;
678
679 typedef struct cgltf_light {
680 char* name;
681 cgltf_float color[3];
682 cgltf_float intensity;
683 cgltf_light_type type;
684 cgltf_float range;
685 cgltf_float spot_inner_cone_angle;
686 cgltf_float spot_outer_cone_angle;
687 cgltf_extras extras;
688 } cgltf_light;
689
690 struct cgltf_node {
691 char* name;
692 cgltf_node* parent;
693 cgltf_node** children;
694 cgltf_size children_count;
695 cgltf_skin* skin;
696 cgltf_mesh* mesh;
697 cgltf_camera* camera;
698 cgltf_light* light;
699 cgltf_float* weights;
700 cgltf_size weights_count;
701 cgltf_bool has_translation;
702 cgltf_bool has_rotation;
703 cgltf_bool has_scale;
704 cgltf_bool has_matrix;
705 cgltf_float translation[3];
706 cgltf_float rotation[4];
707 cgltf_float scale[3];
708 cgltf_float matrix[16];
709 cgltf_extras extras;
710 cgltf_bool has_mesh_gpu_instancing;
711 cgltf_mesh_gpu_instancing mesh_gpu_instancing;
712 cgltf_size extensions_count;
713 cgltf_extension* extensions;
714 };
715
716 typedef struct cgltf_scene {
717 char* name;
718 cgltf_node** nodes;
719 cgltf_size nodes_count;
720 cgltf_extras extras;
721 cgltf_size extensions_count;
722 cgltf_extension* extensions;
723 } cgltf_scene;
724
725 typedef struct cgltf_animation_sampler {
726 cgltf_accessor* input;
727 cgltf_accessor* output;
728 cgltf_interpolation_type interpolation;
729 cgltf_extras extras;
730 cgltf_size extensions_count;
731 cgltf_extension* extensions;
732 } cgltf_animation_sampler;
733
734 typedef struct cgltf_animation_channel {
735 cgltf_animation_sampler* sampler;
736 cgltf_node* target_node;
737 cgltf_animation_path_type target_path;
738 cgltf_extras extras;
739 cgltf_size extensions_count;
740 cgltf_extension* extensions;
741 } cgltf_animation_channel;
742
743 typedef struct cgltf_animation {
744 char* name;
745 cgltf_animation_sampler* samplers;
746 cgltf_size samplers_count;
747 cgltf_animation_channel* channels;
748 cgltf_size channels_count;
749 cgltf_extras extras;
750 cgltf_size extensions_count;
751 cgltf_extension* extensions;
752 } cgltf_animation;
753
754 typedef struct cgltf_material_variant
755 {
756 char* name;
757 cgltf_extras extras;
758 } cgltf_material_variant;
759
760 typedef struct cgltf_asset {
761 char* copyright;
762 char* generator;
763 char* version;
764 char* min_version;
765 cgltf_extras extras;
766 cgltf_size extensions_count;
767 cgltf_extension* extensions;
768 } cgltf_asset;
769
770 typedef struct cgltf_data
771 {
772 cgltf_file_type file_type;
773 void* file_data;
774 cgltf_size file_size;
775
776 cgltf_asset asset;
777
778 cgltf_mesh* meshes;
779 cgltf_size meshes_count;
780
781 cgltf_material* materials;
782 cgltf_size materials_count;
783
784 cgltf_accessor* accessors;
785 cgltf_size accessors_count;
786
787 cgltf_buffer_view* buffer_views;
788 cgltf_size buffer_views_count;
789
790 cgltf_buffer* buffers;
791 cgltf_size buffers_count;
792
793 cgltf_image* images;
794 cgltf_size images_count;
795
796 cgltf_texture* textures;
797 cgltf_size textures_count;
798
799 cgltf_sampler* samplers;
800 cgltf_size samplers_count;
801
802 cgltf_skin* skins;
803 cgltf_size skins_count;
804
805 cgltf_camera* cameras;
806 cgltf_size cameras_count;
807
808 cgltf_light* lights;
809 cgltf_size lights_count;
810
811 cgltf_node* nodes;
812 cgltf_size nodes_count;
813
814 cgltf_scene* scenes;
815 cgltf_size scenes_count;
816
817 cgltf_scene* scene;
818
819 cgltf_animation* animations;
820 cgltf_size animations_count;
821
822 cgltf_material_variant* variants;
823 cgltf_size variants_count;
824
825 cgltf_extras extras;
826
827 cgltf_size data_extensions_count;
828 cgltf_extension* data_extensions;
829
830 char** extensions_used;
831 cgltf_size extensions_used_count;
832
833 char** extensions_required;
834 cgltf_size extensions_required_count;
835
836 const char* json;
837 cgltf_size json_size;
838
839 const void* bin;
840 cgltf_size bin_size;
841
842 cgltf_memory_options memory;
843 cgltf_file_options file;
844 } cgltf_data;
845
846 cgltf_result cgltf_parse(
847 const cgltf_options* options,
848 const void* data,
849 cgltf_size size,
850 cgltf_data** out_data);
851
852 cgltf_result cgltf_parse_file(
853 const cgltf_options* options,
854 const char* path,
855 cgltf_data** out_data);
856
857 cgltf_result cgltf_load_buffers(
858 const cgltf_options* options,
859 cgltf_data* data,
860 const char* gltf_path);
861
862 cgltf_result cgltf_load_buffer_base64(const cgltf_options* options, cgltf_size size, const char* base64, void** out_data);
863
864 cgltf_size cgltf_decode_string(char* string);
865 cgltf_size cgltf_decode_uri(char* uri);
866
867 cgltf_result cgltf_validate(cgltf_data* data);
868
869 void cgltf_free(cgltf_data* data);
870
871 void cgltf_node_transform_local(const cgltf_node* node, cgltf_float* out_matrix);
872 void cgltf_node_transform_world(const cgltf_node* node, cgltf_float* out_matrix);
873
874 const uint8_t* cgltf_buffer_view_data(const cgltf_buffer_view* view);
875
876 const cgltf_accessor* cgltf_find_accessor(const cgltf_primitive* prim, cgltf_attribute_type type, cgltf_int index);
877
878 cgltf_bool cgltf_accessor_read_float(const cgltf_accessor* accessor, cgltf_size index, cgltf_float* out, cgltf_size element_size);
879 cgltf_bool cgltf_accessor_read_uint(const cgltf_accessor* accessor, cgltf_size index, cgltf_uint* out, cgltf_size element_size);
880 cgltf_size cgltf_accessor_read_index(const cgltf_accessor* accessor, cgltf_size index);
881
882 cgltf_size cgltf_num_components(cgltf_type type);
883 cgltf_size cgltf_component_size(cgltf_component_type component_type);
884 cgltf_size cgltf_calc_size(cgltf_type type, cgltf_component_type component_type);
885
886 cgltf_size cgltf_accessor_unpack_floats(const cgltf_accessor* accessor, cgltf_float* out, cgltf_size float_count);
887 cgltf_size cgltf_accessor_unpack_indices(const cgltf_accessor* accessor, void* out, cgltf_size out_component_size, cgltf_size index_count);
888
889 /* this function is deprecated and will be removed in the future; use cgltf_extras::data instead */
890 cgltf_result cgltf_copy_extras_json(const cgltf_data* data, const cgltf_extras* extras, char* dest, cgltf_size* dest_size);
891
892 cgltf_size cgltf_mesh_index(const cgltf_data* data, const cgltf_mesh* object);
893 cgltf_size cgltf_material_index(const cgltf_data* data, const cgltf_material* object);
894 cgltf_size cgltf_accessor_index(const cgltf_data* data, const cgltf_accessor* object);
895 cgltf_size cgltf_buffer_view_index(const cgltf_data* data, const cgltf_buffer_view* object);
896 cgltf_size cgltf_buffer_index(const cgltf_data* data, const cgltf_buffer* object);
897 cgltf_size cgltf_image_index(const cgltf_data* data, const cgltf_image* object);
898 cgltf_size cgltf_texture_index(const cgltf_data* data, const cgltf_texture* object);
899 cgltf_size cgltf_sampler_index(const cgltf_data* data, const cgltf_sampler* object);
900 cgltf_size cgltf_skin_index(const cgltf_data* data, const cgltf_skin* object);
901 cgltf_size cgltf_camera_index(const cgltf_data* data, const cgltf_camera* object);
902 cgltf_size cgltf_light_index(const cgltf_data* data, const cgltf_light* object);
903 cgltf_size cgltf_node_index(const cgltf_data* data, const cgltf_node* object);
904 cgltf_size cgltf_scene_index(const cgltf_data* data, const cgltf_scene* object);
905 cgltf_size cgltf_animation_index(const cgltf_data* data, const cgltf_animation* object);
906 cgltf_size cgltf_animation_sampler_index(const cgltf_animation* animation, const cgltf_animation_sampler* object);
907 cgltf_size cgltf_animation_channel_index(const cgltf_animation* animation, const cgltf_animation_channel* object);
908
909 #ifdef __cplusplus
910 }
911 #endif
912
913 #endif /* #ifndef CGLTF_H_INCLUDED__ */
914
915 /*
916 *
917 * Stop now, if you are only interested in the API.
918 * Below, you find the implementation.
919 *
920 */
921
922 #if defined(__INTELLISENSE__) || defined(__JETBRAINS_IDE__)
923 /* This makes MSVC/CLion intellisense work. */
924 #define CGLTF_IMPLEMENTATION
925 #endif
926
927 #ifdef CGLTF_IMPLEMENTATION
928
929 #include <assert.h> /* For assert */
930 #include <string.h> /* For strncpy */
931 #include <stdio.h> /* For fopen */
932 #include <limits.h> /* For UINT_MAX etc */
933 #include <float.h> /* For FLT_MAX */
934
935 #if !defined(CGLTF_MALLOC) || !defined(CGLTF_FREE) || !defined(CGLTF_ATOI) || !defined(CGLTF_ATOF) || !defined(CGLTF_ATOLL)
936 #include <stdlib.h> /* For malloc, free, atoi, atof */
937 #endif
938
939 /* JSMN_PARENT_LINKS is necessary to make parsing large structures linear in input size */
940 #define JSMN_PARENT_LINKS
941
942 /* JSMN_STRICT is necessary to reject invalid JSON documents */
943 #define JSMN_STRICT
944
945 /*
946 * -- jsmn.h start --
947 * Source: https://github.com/zserge/jsmn
948 * License: MIT
949 */
950 typedef enum {
951 JSMN_UNDEFINED = 0,
952 JSMN_OBJECT = 1,
953 JSMN_ARRAY = 2,
954 JSMN_STRING = 3,
955 JSMN_PRIMITIVE = 4
956 } jsmntype_t;
957 enum jsmnerr {
958 /* Not enough tokens were provided */
959 JSMN_ERROR_NOMEM = -1,
960 /* Invalid character inside JSON string */
961 JSMN_ERROR_INVAL = -2,
962 /* The string is not a full JSON packet, more bytes expected */
963 JSMN_ERROR_PART = -3
964 };
965 typedef struct {
966 jsmntype_t type;
967 ptrdiff_t start;
968 ptrdiff_t end;
969 int size;
970 #ifdef JSMN_PARENT_LINKS
971 int parent;
972 #endif
973 } jsmntok_t;
974 typedef struct {
975 size_t pos; /* offset in the JSON string */
976 unsigned int toknext; /* next token to allocate */
977 int toksuper; /* superior token node, e.g parent object or array */
978 } jsmn_parser;
979 static void jsmn_init(jsmn_parser *parser);
980 static int jsmn_parse(jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, size_t num_tokens);
981 /*
982 * -- jsmn.h end --
983 */
984
985
986 #ifndef CGLTF_CONSTS
987 #define GlbHeaderSize 12
988 #define GlbChunkHeaderSize 8
989 static const uint32_t GlbVersion = 2;
990 static const uint32_t GlbMagic = 0x46546C67;
991 static const uint32_t GlbMagicJsonChunk = 0x4E4F534A;
992 static const uint32_t GlbMagicBinChunk = 0x004E4942;
993 #define CGLTF_CONSTS
994 #endif
995
996 #ifndef CGLTF_MALLOC
997 #define CGLTF_MALLOC(size) malloc(size)
998 #endif
999 #ifndef CGLTF_FREE
1000 #define CGLTF_FREE(ptr) free(ptr)
1001 #endif
1002 #ifndef CGLTF_ATOI
1003 #define CGLTF_ATOI(str) atoi(str)
1004 #endif
1005 #ifndef CGLTF_ATOF
1006 #define CGLTF_ATOF(str) atof(str)
1007 #endif
1008 #ifndef CGLTF_ATOLL
1009 #define CGLTF_ATOLL(str) atoll(str)
1010 #endif
1011 #ifndef CGLTF_VALIDATE_ENABLE_ASSERTS
1012 #define CGLTF_VALIDATE_ENABLE_ASSERTS 0
1013 #endif
1014
1015 static void* cgltf_default_alloc(void* user, cgltf_size size)
1016 {
1017 (void)user;
1018 return CGLTF_MALLOC(size);
1019 }
1020
1021 static void cgltf_default_free(void* user, void* ptr)
1022 {
1023 (void)user;
1024 CGLTF_FREE(ptr);
1025 }
1026
1027 static void* cgltf_calloc(cgltf_options* options, size_t element_size, cgltf_size count)
1028 {
1029 if (SIZE_MAX / element_size < count)
1030 {
1031 return NULL;
1032 }
1033 void* result = options->memory.alloc_func(options->memory.user_data, element_size * count);
1034 if (!result)
1035 {
1036 return NULL;
1037 }
1038 memset(result, 0, element_size * count);
1039 return result;
1040 }
1041
1042 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)
1043 {
1044 (void)file_options;
1045 void* (*memory_alloc)(void*, cgltf_size) = memory_options->alloc_func ? memory_options->alloc_func : &cgltf_default_alloc;
1046 void (*memory_free)(void*, void*) = memory_options->free_func ? memory_options->free_func : &cgltf_default_free;
1047
1048 FILE* file = fopen(path, "rb");
1049 if (!file)
1050 {
1051 return cgltf_result_file_not_found;
1052 }
1053
1054 cgltf_size file_size = size ? *size : 0;
1055
1056 if (file_size == 0)
1057 {
1058 fseek(file, 0, SEEK_END);
1059
1060 #ifdef _MSC_VER
1061 __int64 length = _ftelli64(file);
1062 #else
1063 long length = ftell(file);
1064 #endif
1065
1066 if (length < 0)
1067 {
1068 fclose(file);
1069 return cgltf_result_io_error;
1070 }
1071
1072 fseek(file, 0, SEEK_SET);
1073 file_size = (cgltf_size)length;
1074 }
1075
1076 char* file_data = (char*)memory_alloc(memory_options->user_data, file_size);
1077 if (!file_data)
1078 {
1079 fclose(file);
1080 return cgltf_result_out_of_memory;
1081 }
1082
1083 cgltf_size read_size = fread(file_data, 1, file_size, file);
1084
1085 fclose(file);
1086
1087 if (read_size != file_size)
1088 {
1089 memory_free(memory_options->user_data, file_data);
1090 return cgltf_result_io_error;
1091 }
1092
1093 if (size)
1094 {
1095 *size = file_size;
1096 }
1097 if (data)
1098 {
1099 *data = file_data;
1100 }
1101
1102 return cgltf_result_success;
1103 }
1104
1105 static void cgltf_default_file_release(const struct cgltf_memory_options* memory_options, const struct cgltf_file_options* file_options, void* data, cgltf_size size)
1106 {
1107 (void)file_options;
1108 (void)size;
1109 void (*memfree)(void*, void*) = memory_options->free_func ? memory_options->free_func : &cgltf_default_free;
1110 memfree(memory_options->user_data, data);
1111 }
1112
1113 static cgltf_result cgltf_parse_json(cgltf_options* options, const uint8_t* json_chunk, cgltf_size size, cgltf_data** out_data);
1114
1115 cgltf_result cgltf_parse(const cgltf_options* options, const void* data, cgltf_size size, cgltf_data** out_data)
1116 {
1117 if (size < GlbHeaderSize)
1118 {
1119 return cgltf_result_data_too_short;
1120 }
1121
1122 if (options == NULL)
1123 {
1124 return cgltf_result_invalid_options;
1125 }
1126
1127 cgltf_options fixed_options = *options;
1128 if (fixed_options.memory.alloc_func == NULL)
1129 {
1130 fixed_options.memory.alloc_func = &cgltf_default_alloc;
1131 }
1132 if (fixed_options.memory.free_func == NULL)
1133 {
1134 fixed_options.memory.free_func = &cgltf_default_free;
1135 }
1136
1137 uint32_t tmp;
1138 // Magic
1139 memcpy(&tmp, data, 4);
1140 if (tmp != GlbMagic)
1141 {
1142 if (fixed_options.type == cgltf_file_type_invalid)
1143 {
1144 fixed_options.type = cgltf_file_type_gltf;
1145 }
1146 else if (fixed_options.type == cgltf_file_type_glb)
1147 {
1148 return cgltf_result_unknown_format;
1149 }
1150 }
1151
1152 if (fixed_options.type == cgltf_file_type_gltf)
1153 {
1154 cgltf_result json_result = cgltf_parse_json(&fixed_options, (const uint8_t*)data, size, out_data);
1155 if (json_result != cgltf_result_success)
1156 {
1157 return json_result;
1158 }
1159
1160 (*out_data)->file_type = cgltf_file_type_gltf;
1161
1162 return cgltf_result_success;
1163 }
1164
1165 const uint8_t* ptr = (const uint8_t*)data;
1166 // Version
1167 memcpy(&tmp, ptr + 4, 4);
1168 uint32_t version = tmp;
1169 if (version != GlbVersion)
1170 {
1171 return version < GlbVersion ? cgltf_result_legacy_gltf : cgltf_result_unknown_format;
1172 }
1173
1174 // Total length
1175 memcpy(&tmp, ptr + 8, 4);
1176 if (tmp > size)
1177 {
1178 return cgltf_result_data_too_short;
1179 }
1180
1181 const uint8_t* json_chunk = ptr + GlbHeaderSize;
1182
1183 if (GlbHeaderSize + GlbChunkHeaderSize > size)
1184 {
1185 return cgltf_result_data_too_short;
1186 }
1187
1188 // JSON chunk: length
1189 uint32_t json_length;
1190 memcpy(&json_length, json_chunk, 4);
1191 if (json_length > size - GlbHeaderSize - GlbChunkHeaderSize)
1192 {
1193 return cgltf_result_data_too_short;
1194 }
1195
1196 // JSON chunk: magic
1197 memcpy(&tmp, json_chunk + 4, 4);
1198 if (tmp != GlbMagicJsonChunk)
1199 {
1200 return cgltf_result_unknown_format;
1201 }
1202
1203 json_chunk += GlbChunkHeaderSize;
1204
1205 const void* bin = NULL;
1206 cgltf_size bin_size = 0;
1207
1208 if (GlbChunkHeaderSize <= size - GlbHeaderSize - GlbChunkHeaderSize - json_length)
1209 {
1210 // We can read another chunk
1211 const uint8_t* bin_chunk = json_chunk + json_length;
1212
1213 // Bin chunk: length
1214 uint32_t bin_length;
1215 memcpy(&bin_length, bin_chunk, 4);
1216 if (bin_length > size - GlbHeaderSize - GlbChunkHeaderSize - json_length - GlbChunkHeaderSize)
1217 {
1218 return cgltf_result_data_too_short;
1219 }
1220
1221 // Bin chunk: magic
1222 memcpy(&tmp, bin_chunk + 4, 4);
1223 if (tmp != GlbMagicBinChunk)
1224 {
1225 return cgltf_result_unknown_format;
1226 }
1227
1228 bin_chunk += GlbChunkHeaderSize;
1229
1230 bin = bin_chunk;
1231 bin_size = bin_length;
1232 }
1233
1234 cgltf_result json_result = cgltf_parse_json(&fixed_options, json_chunk, json_length, out_data);
1235 if (json_result != cgltf_result_success)
1236 {
1237 return json_result;
1238 }
1239
1240 (*out_data)->file_type = cgltf_file_type_glb;
1241 (*out_data)->bin = bin;
1242 (*out_data)->bin_size = bin_size;
1243
1244 return cgltf_result_success;
1245 }
1246
1247 cgltf_result cgltf_parse_file(const cgltf_options* options, const char* path, cgltf_data** out_data)
1248 {
1249 if (options == NULL)
1250 {
1251 return cgltf_result_invalid_options;
1252 }
1253
1254 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;
1255 void (*file_release)(const struct cgltf_memory_options*, const struct cgltf_file_options*, void* data, cgltf_size size) = options->file.release ? options->file.release : cgltf_default_file_release;
1256
1257 void* file_data = NULL;
1258 cgltf_size file_size = 0;
1259 cgltf_result result = file_read(&options->memory, &options->file, path, &file_size, &file_data);
1260 if (result != cgltf_result_success)
1261 {
1262 return result;
1263 }
1264
1265 result = cgltf_parse(options, file_data, file_size, out_data);
1266
1267 if (result != cgltf_result_success)
1268 {
1269 file_release(&options->memory, &options->file, file_data, file_size);
1270 return result;
1271 }
1272
1273 (*out_data)->file_data = file_data;
1274 (*out_data)->file_size = file_size;
1275
1276 return cgltf_result_success;
1277 }
1278
1279 static void cgltf_combine_paths(char* path, const char* base, const char* uri)
1280 {
1281 const char* s0 = strrchr(base, '/');
1282 const char* s1 = strrchr(base, '\\');
1283 const char* slash = s0 ? (s1 && s1 > s0 ? s1 : s0) : s1;
1284
1285 if (slash)
1286 {
1287 size_t prefix = slash - base + 1;
1288
1289 strncpy(path, base, prefix);
1290 strcpy(path + prefix, uri);
1291 }
1292 else
1293 {
1294 strcpy(path, uri);
1295 }
1296 }
1297
1298 static cgltf_result cgltf_load_buffer_file(const cgltf_options* options, cgltf_size size, const char* uri, const char* gltf_path, void** out_data)
1299 {
1300 void* (*memory_alloc)(void*, cgltf_size) = options->memory.alloc_func ? options->memory.alloc_func : &cgltf_default_alloc;
1301 void (*memory_free)(void*, void*) = options->memory.free_func ? options->memory.free_func : &cgltf_default_free;
1302 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;
1303
1304 char* path = (char*)memory_alloc(options->memory.user_data, strlen(uri) + strlen(gltf_path) + 1);
1305 if (!path)
1306 {
1307 return cgltf_result_out_of_memory;
1308 }
1309
1310 cgltf_combine_paths(path, gltf_path, uri);
1311
1312 // after combining, the tail of the resulting path is a uri; decode_uri converts it into path
1313 cgltf_decode_uri(path + strlen(path) - strlen(uri));
1314
1315 void* file_data = NULL;
1316 cgltf_result result = file_read(&options->memory, &options->file, path, &size, &file_data);
1317
1318 memory_free(options->memory.user_data, path);
1319
1320 *out_data = (result == cgltf_result_success) ? file_data : NULL;
1321
1322 return result;
1323 }
1324
1325 cgltf_result cgltf_load_buffer_base64(const cgltf_options* options, cgltf_size size, const char* base64, void** out_data)
1326 {
1327 void* (*memory_alloc)(void*, cgltf_size) = options->memory.alloc_func ? options->memory.alloc_func : &cgltf_default_alloc;
1328 void (*memory_free)(void*, void*) = options->memory.free_func ? options->memory.free_func : &cgltf_default_free;
1329
1330 unsigned char* data = (unsigned char*)memory_alloc(options->memory.user_data, size);
1331 if (!data)
1332 {
1333 return cgltf_result_out_of_memory;
1334 }
1335
1336 unsigned int buffer = 0;
1337 unsigned int buffer_bits = 0;
1338
1339 for (cgltf_size i = 0; i < size; ++i)
1340 {
1341 while (buffer_bits < 8)
1342 {
1343 char ch = *base64++;
1344
1345 int index =
1346 (unsigned)(ch - 'A') < 26 ? (ch - 'A') :
1347 (unsigned)(ch - 'a') < 26 ? (ch - 'a') + 26 :
1348 (unsigned)(ch - '0') < 10 ? (ch - '0') + 52 :
1349 ch == '+' ? 62 :
1350 ch == '/' ? 63 :
1351 -1;
1352
1353 if (index < 0)
1354 {
1355 memory_free(options->memory.user_data, data);
1356 return cgltf_result_io_error;
1357 }
1358
1359 buffer = (buffer << 6) | index;
1360 buffer_bits += 6;
1361 }
1362
1363 data[i] = (unsigned char)(buffer >> (buffer_bits - 8));
1364 buffer_bits -= 8;
1365 }
1366
1367 *out_data = data;
1368
1369 return cgltf_result_success;
1370 }
1371
1372 static int cgltf_unhex(char ch)
1373 {
1374 return
1375 (unsigned)(ch - '0') < 10 ? (ch - '0') :
1376 (unsigned)(ch - 'A') < 6 ? (ch - 'A') + 10 :
1377 (unsigned)(ch - 'a') < 6 ? (ch - 'a') + 10 :
1378 -1;
1379 }
1380
1381 cgltf_size cgltf_decode_string(char* string)
1382 {
1383 char* read = string + strcspn(string, "\\");
1384 if (*read == 0)
1385 {
1386 return read - string;
1387 }
1388 char* write = string;
1389 char* last = string;
1390
1391 for (;;)
1392 {
1393 // Copy characters since last escaped sequence
1394 cgltf_size written = read - last;
1395 memmove(write, last, written);
1396 write += written;
1397
1398 if (*read++ == 0)
1399 {
1400 break;
1401 }
1402
1403 // jsmn already checked that all escape sequences are valid
1404 switch (*read++)
1405 {
1406 case '\"': *write++ = '\"'; break;
1407 case '/': *write++ = '/'; break;
1408 case '\\': *write++ = '\\'; break;
1409 case 'b': *write++ = '\b'; break;
1410 case 'f': *write++ = '\f'; break;
1411 case 'r': *write++ = '\r'; break;
1412 case 'n': *write++ = '\n'; break;
1413 case 't': *write++ = '\t'; break;
1414 case 'u':
1415 {
1416 // UCS-2 codepoint \uXXXX to UTF-8
1417 int character = 0;
1418 for (cgltf_size i = 0; i < 4; ++i)
1419 {
1420 character = (character << 4) + cgltf_unhex(*read++);
1421 }
1422
1423 if (character <= 0x7F)
1424 {
1425 *write++ = character & 0xFF;
1426 }
1427 else if (character <= 0x7FF)
1428 {
1429 *write++ = 0xC0 | ((character >> 6) & 0xFF);
1430 *write++ = 0x80 | (character & 0x3F);
1431 }
1432 else
1433 {
1434 *write++ = 0xE0 | ((character >> 12) & 0xFF);
1435 *write++ = 0x80 | ((character >> 6) & 0x3F);
1436 *write++ = 0x80 | (character & 0x3F);
1437 }
1438 break;
1439 }
1440 default:
1441 break;
1442 }
1443
1444 last = read;
1445 read += strcspn(read, "\\");
1446 }
1447
1448 *write = 0;
1449 return write - string;
1450 }
1451
1452 cgltf_size cgltf_decode_uri(char* uri)
1453 {
1454 char* write = uri;
1455 char* i = uri;
1456
1457 while (*i)
1458 {
1459 if (*i == '%')
1460 {
1461 int ch1 = cgltf_unhex(i[1]);
1462
1463 if (ch1 >= 0)
1464 {
1465 int ch2 = cgltf_unhex(i[2]);
1466
1467 if (ch2 >= 0)
1468 {
1469 *write++ = (char)(ch1 * 16 + ch2);
1470 i += 3;
1471 continue;
1472 }
1473 }
1474 }
1475
1476 *write++ = *i++;
1477 }
1478
1479 *write = 0;
1480 return write - uri;
1481 }
1482
1483 cgltf_result cgltf_load_buffers(const cgltf_options* options, cgltf_data* data, const char* gltf_path)
1484 {
1485 if (options == NULL)
1486 {
1487 return cgltf_result_invalid_options;
1488 }
1489
1490 if (data->buffers_count && data->buffers[0].data == NULL && data->buffers[0].uri == NULL && data->bin)
1491 {
1492 if (data->bin_size < data->buffers[0].size)
1493 {
1494 return cgltf_result_data_too_short;
1495 }
1496
1497 data->buffers[0].data = (void*)data->bin;
1498 data->buffers[0].data_free_method = cgltf_data_free_method_none;
1499 }
1500
1501 for (cgltf_size i = 0; i < data->buffers_count; ++i)
1502 {
1503 if (data->buffers[i].data)
1504 {
1505 continue;
1506 }
1507
1508 const char* uri = data->buffers[i].uri;
1509
1510 if (uri == NULL)
1511 {
1512 continue;
1513 }
1514
1515 if (strncmp(uri, "data:", 5) == 0)
1516 {
1517 const char* comma = strchr(uri, ',');
1518
1519 if (comma && comma - uri >= 7 && strncmp(comma - 7, ";base64", 7) == 0)
1520 {
1521 cgltf_result res = cgltf_load_buffer_base64(options, data->buffers[i].size, comma + 1, &data->buffers[i].data);
1522 data->buffers[i].data_free_method = cgltf_data_free_method_memory_free;
1523
1524 if (res != cgltf_result_success)
1525 {
1526 return res;
1527 }
1528 }
1529 else
1530 {
1531 return cgltf_result_unknown_format;
1532 }
1533 }
1534 else if (strstr(uri, "://") == NULL && gltf_path)
1535 {
1536 cgltf_result res = cgltf_load_buffer_file(options, data->buffers[i].size, uri, gltf_path, &data->buffers[i].data);
1537 data->buffers[i].data_free_method = cgltf_data_free_method_file_release;
1538
1539 if (res != cgltf_result_success)
1540 {
1541 return res;
1542 }
1543 }
1544 else
1545 {
1546 return cgltf_result_unknown_format;
1547 }
1548 }
1549
1550 return cgltf_result_success;
1551 }
1552
1553 static cgltf_size cgltf_calc_index_bound(cgltf_buffer_view* buffer_view, cgltf_size offset, cgltf_component_type component_type, cgltf_size count)
1554 {
1555 char* data = (char*)buffer_view->buffer->data + offset + buffer_view->offset;
1556 cgltf_size bound = 0;
1557
1558 switch (component_type)
1559 {
1560 case cgltf_component_type_r_8u:
1561 for (size_t i = 0; i < count; ++i)
1562 {
1563 cgltf_size v = ((unsigned char*)data)[i];
1564 bound = bound > v ? bound : v;
1565 }
1566 break;
1567
1568 case cgltf_component_type_r_16u:
1569 for (size_t i = 0; i < count; ++i)
1570 {
1571 cgltf_size v = ((unsigned short*)data)[i];
1572 bound = bound > v ? bound : v;
1573 }
1574 break;
1575
1576 case cgltf_component_type_r_32u:
1577 for (size_t i = 0; i < count; ++i)
1578 {
1579 cgltf_size v = ((unsigned int*)data)[i];
1580 bound = bound > v ? bound : v;
1581 }
1582 break;
1583
1584 default:
1585 ;
1586 }
1587
1588 return bound;
1589 }
1590
1591 #if CGLTF_VALIDATE_ENABLE_ASSERTS
1592 #define CGLTF_ASSERT_IF(cond, result) assert(!(cond)); if (cond) return result;
1593 #else
1594 #define CGLTF_ASSERT_IF(cond, result) if (cond) return result;
1595 #endif
1596
1597 cgltf_result cgltf_validate(cgltf_data* data)
1598 {
1599 for (cgltf_size i = 0; i < data->accessors_count; ++i)
1600 {
1601 cgltf_accessor* accessor = &data->accessors[i];
1602
1603 CGLTF_ASSERT_IF(data->accessors[i].component_type == cgltf_component_type_invalid, cgltf_result_invalid_gltf);
1604 CGLTF_ASSERT_IF(data->accessors[i].type == cgltf_type_invalid, cgltf_result_invalid_gltf);
1605
1606 cgltf_size element_size = cgltf_calc_size(accessor->type, accessor->component_type);
1607
1608 if (accessor->buffer_view)
1609 {
1610 cgltf_size req_size = accessor->offset + accessor->stride * (accessor->count - 1) + element_size;
1611
1612 CGLTF_ASSERT_IF(accessor->buffer_view->size < req_size, cgltf_result_data_too_short);
1613 }
1614
1615 if (accessor->is_sparse)
1616 {
1617 cgltf_accessor_sparse* sparse = &accessor->sparse;
1618
1619 cgltf_size indices_component_size = cgltf_component_size(sparse->indices_component_type);
1620 cgltf_size indices_req_size = sparse->indices_byte_offset + indices_component_size * sparse->count;
1621 cgltf_size values_req_size = sparse->values_byte_offset + element_size * sparse->count;
1622
1623 CGLTF_ASSERT_IF(sparse->indices_buffer_view->size < indices_req_size ||
1624 sparse->values_buffer_view->size < values_req_size, cgltf_result_data_too_short);
1625
1626 CGLTF_ASSERT_IF(sparse->indices_component_type != cgltf_component_type_r_8u &&
1627 sparse->indices_component_type != cgltf_component_type_r_16u &&
1628 sparse->indices_component_type != cgltf_component_type_r_32u, cgltf_result_invalid_gltf);
1629
1630 if (sparse->indices_buffer_view->buffer->data)
1631 {
1632 cgltf_size index_bound = cgltf_calc_index_bound(sparse->indices_buffer_view, sparse->indices_byte_offset, sparse->indices_component_type, sparse->count);
1633
1634 CGLTF_ASSERT_IF(index_bound >= accessor->count, cgltf_result_data_too_short);
1635 }
1636 }
1637 }
1638
1639 for (cgltf_size i = 0; i < data->buffer_views_count; ++i)
1640 {
1641 cgltf_size req_size = data->buffer_views[i].offset + data->buffer_views[i].size;
1642
1643 CGLTF_ASSERT_IF(data->buffer_views[i].buffer && data->buffer_views[i].buffer->size < req_size, cgltf_result_data_too_short);
1644
1645 if (data->buffer_views[i].has_meshopt_compression)
1646 {
1647 cgltf_meshopt_compression* mc = &data->buffer_views[i].meshopt_compression;
1648
1649 CGLTF_ASSERT_IF(mc->buffer == NULL || mc->buffer->size < mc->offset + mc->size, cgltf_result_data_too_short);
1650
1651 CGLTF_ASSERT_IF(data->buffer_views[i].stride && mc->stride != data->buffer_views[i].stride, cgltf_result_invalid_gltf);
1652
1653 CGLTF_ASSERT_IF(data->buffer_views[i].size != mc->stride * mc->count, cgltf_result_invalid_gltf);
1654
1655 CGLTF_ASSERT_IF(mc->mode == cgltf_meshopt_compression_mode_invalid, cgltf_result_invalid_gltf);
1656
1657 CGLTF_ASSERT_IF(mc->mode == cgltf_meshopt_compression_mode_attributes && !(mc->stride % 4 == 0 && mc->stride <= 256), cgltf_result_invalid_gltf);
1658
1659 CGLTF_ASSERT_IF(mc->mode == cgltf_meshopt_compression_mode_triangles && mc->count % 3 != 0, cgltf_result_invalid_gltf);
1660
1661 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);
1662
1663 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);
1664
1665 CGLTF_ASSERT_IF(mc->filter == cgltf_meshopt_compression_filter_octahedral && mc->stride != 4 && mc->stride != 8, cgltf_result_invalid_gltf);
1666 CGLTF_ASSERT_IF(mc->filter == cgltf_meshopt_compression_filter_quaternion && mc->stride != 8, cgltf_result_invalid_gltf);
1667 CGLTF_ASSERT_IF(mc->filter == cgltf_meshopt_compression_filter_color && mc->stride != 4 && mc->stride != 8, cgltf_result_invalid_gltf);
1668 }
1669 }
1670
1671 for (cgltf_size i = 0; i < data->meshes_count; ++i)
1672 {
1673 if (data->meshes[i].weights)
1674 {
1675 CGLTF_ASSERT_IF(data->meshes[i].primitives_count && data->meshes[i].primitives[0].targets_count != data->meshes[i].weights_count, cgltf_result_invalid_gltf);
1676 }
1677
1678 if (data->meshes[i].target_names)
1679 {
1680 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);
1681 }
1682
1683 for (cgltf_size j = 0; j < data->meshes[i].primitives_count; ++j)
1684 {
1685 CGLTF_ASSERT_IF(data->meshes[i].primitives[j].type == cgltf_primitive_type_invalid, cgltf_result_invalid_gltf);
1686 CGLTF_ASSERT_IF(data->meshes[i].primitives[j].targets_count != data->meshes[i].primitives[0].targets_count, cgltf_result_invalid_gltf);
1687
1688 CGLTF_ASSERT_IF(data->meshes[i].primitives[j].attributes_count == 0, cgltf_result_invalid_gltf);
1689
1690 cgltf_accessor* first = data->meshes[i].primitives[j].attributes[0].data;
1691
1692 CGLTF_ASSERT_IF(first->count == 0, cgltf_result_invalid_gltf);
1693
1694 for (cgltf_size k = 0; k < data->meshes[i].primitives[j].attributes_count; ++k)
1695 {
1696 CGLTF_ASSERT_IF(data->meshes[i].primitives[j].attributes[k].data->count != first->count, cgltf_result_invalid_gltf);
1697 }
1698
1699 for (cgltf_size k = 0; k < data->meshes[i].primitives[j].targets_count; ++k)
1700 {
1701 for (cgltf_size m = 0; m < data->meshes[i].primitives[j].targets[k].attributes_count; ++m)
1702 {
1703 CGLTF_ASSERT_IF(data->meshes[i].primitives[j].targets[k].attributes[m].data->count != first->count, cgltf_result_invalid_gltf);
1704 }
1705 }
1706
1707 cgltf_accessor* indices = data->meshes[i].primitives[j].indices;
1708
1709 CGLTF_ASSERT_IF(indices &&
1710 indices->component_type != cgltf_component_type_r_8u &&
1711 indices->component_type != cgltf_component_type_r_16u &&
1712 indices->component_type != cgltf_component_type_r_32u, cgltf_result_invalid_gltf);
1713
1714 CGLTF_ASSERT_IF(indices && indices->type != cgltf_type_scalar, cgltf_result_invalid_gltf);
1715 CGLTF_ASSERT_IF(indices && indices->stride != cgltf_component_size(indices->component_type), cgltf_result_invalid_gltf);
1716
1717 if (indices && indices->buffer_view && indices->buffer_view->buffer->data)
1718 {
1719 cgltf_size index_bound = cgltf_calc_index_bound(indices->buffer_view, indices->offset, indices->component_type, indices->count);
1720
1721 CGLTF_ASSERT_IF(index_bound >= first->count, cgltf_result_data_too_short);
1722 }
1723
1724 for (cgltf_size k = 0; k < data->meshes[i].primitives[j].mappings_count; ++k)
1725 {
1726 CGLTF_ASSERT_IF(data->meshes[i].primitives[j].mappings[k].variant >= data->variants_count, cgltf_result_invalid_gltf);
1727 }
1728 }
1729 }
1730
1731 for (cgltf_size i = 0; i < data->nodes_count; ++i)
1732 {
1733 if (data->nodes[i].weights && data->nodes[i].mesh)
1734 {
1735 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);
1736 }
1737
1738 if (data->nodes[i].has_mesh_gpu_instancing)
1739 {
1740 CGLTF_ASSERT_IF(data->nodes[i].mesh == NULL, cgltf_result_invalid_gltf);
1741 CGLTF_ASSERT_IF(data->nodes[i].mesh_gpu_instancing.attributes_count == 0, cgltf_result_invalid_gltf);
1742
1743 cgltf_accessor* first = data->nodes[i].mesh_gpu_instancing.attributes[0].data;
1744
1745 for (cgltf_size k = 0; k < data->nodes[i].mesh_gpu_instancing.attributes_count; ++k)
1746 {
1747 CGLTF_ASSERT_IF(data->nodes[i].mesh_gpu_instancing.attributes[k].data->count != first->count, cgltf_result_invalid_gltf);
1748 }
1749 }
1750 }
1751
1752 for (cgltf_size i = 0; i < data->nodes_count; ++i)
1753 {
1754 cgltf_node* p1 = data->nodes[i].parent;
1755 cgltf_node* p2 = p1 ? p1->parent : NULL;
1756
1757 while (p1 && p2)
1758 {
1759 CGLTF_ASSERT_IF(p1 == p2, cgltf_result_invalid_gltf);
1760
1761 p1 = p1->parent;
1762 p2 = p2->parent ? p2->parent->parent : NULL;
1763 }
1764 }
1765
1766 for (cgltf_size i = 0; i < data->scenes_count; ++i)
1767 {
1768 for (cgltf_size j = 0; j < data->scenes[i].nodes_count; ++j)
1769 {
1770 CGLTF_ASSERT_IF(data->scenes[i].nodes[j]->parent, cgltf_result_invalid_gltf);
1771 }
1772 }
1773
1774 for (cgltf_size i = 0; i < data->animations_count; ++i)
1775 {
1776 for (cgltf_size j = 0; j < data->animations[i].channels_count; ++j)
1777 {
1778 cgltf_animation_channel* channel = &data->animations[i].channels[j];
1779
1780 if (!channel->target_node)
1781 {
1782 continue;
1783 }
1784
1785 cgltf_size components = 1;
1786
1787 if (channel->target_path == cgltf_animation_path_type_weights)
1788 {
1789 CGLTF_ASSERT_IF(!channel->target_node->mesh || !channel->target_node->mesh->primitives_count, cgltf_result_invalid_gltf);
1790
1791 components = channel->target_node->mesh->primitives[0].targets_count;
1792 }
1793
1794 cgltf_size values = channel->sampler->interpolation == cgltf_interpolation_type_cubic_spline ? 3 : 1;
1795
1796 CGLTF_ASSERT_IF(channel->sampler->input->count * components * values != channel->sampler->output->count, cgltf_result_invalid_gltf);
1797 }
1798 }
1799
1800 for (cgltf_size i = 0; i < data->variants_count; ++i)
1801 {
1802 CGLTF_ASSERT_IF(!data->variants[i].name, cgltf_result_invalid_gltf);
1803 }
1804
1805 return cgltf_result_success;
1806 }
1807
1808 cgltf_result cgltf_copy_extras_json(const cgltf_data* data, const cgltf_extras* extras, char* dest, cgltf_size* dest_size)
1809 {
1810 cgltf_size json_size = extras->end_offset - extras->start_offset;
1811
1812 if (!dest)
1813 {
1814 if (dest_size)
1815 {
1816 *dest_size = json_size + 1;
1817 return cgltf_result_success;
1818 }
1819 return cgltf_result_invalid_options;
1820 }
1821
1822 if (*dest_size + 1 < json_size)
1823 {
1824 strncpy(dest, data->json + extras->start_offset, *dest_size - 1);
1825 dest[*dest_size - 1] = 0;
1826 }
1827 else
1828 {
1829 strncpy(dest, data->json + extras->start_offset, json_size);
1830 dest[json_size] = 0;
1831 }
1832
1833 return cgltf_result_success;
1834 }
1835
1836 static void cgltf_free_extras(cgltf_data* data, cgltf_extras* extras)
1837 {
1838 data->memory.free_func(data->memory.user_data, extras->data);
1839 }
1840
1841 static void cgltf_free_extensions(cgltf_data* data, cgltf_extension* extensions, cgltf_size extensions_count)
1842 {
1843 for (cgltf_size i = 0; i < extensions_count; ++i)
1844 {
1845 data->memory.free_func(data->memory.user_data, extensions[i].name);
1846 data->memory.free_func(data->memory.user_data, extensions[i].data);
1847 }
1848 data->memory.free_func(data->memory.user_data, extensions);
1849 }
1850
1851 void cgltf_free(cgltf_data* data)
1852 {
1853 if (!data)
1854 {
1855 return;
1856 }
1857
1858 void (*file_release)(const struct cgltf_memory_options*, const struct cgltf_file_options*, void* data, cgltf_size size) = data->file.release ? data->file.release : cgltf_default_file_release;
1859
1860 data->memory.free_func(data->memory.user_data, data->asset.copyright);
1861 data->memory.free_func(data->memory.user_data, data->asset.generator);
1862 data->memory.free_func(data->memory.user_data, data->asset.version);
1863 data->memory.free_func(data->memory.user_data, data->asset.min_version);
1864
1865 cgltf_free_extensions(data, data->asset.extensions, data->asset.extensions_count);
1866 cgltf_free_extras(data, &data->asset.extras);
1867
1868 for (cgltf_size i = 0; i < data->accessors_count; ++i)
1869 {
1870 data->memory.free_func(data->memory.user_data, data->accessors[i].name);
1871
1872 cgltf_free_extensions(data, data->accessors[i].extensions, data->accessors[i].extensions_count);
1873 cgltf_free_extras(data, &data->accessors[i].extras);
1874 }
1875 data->memory.free_func(data->memory.user_data, data->accessors);
1876
1877 for (cgltf_size i = 0; i < data->buffer_views_count; ++i)
1878 {
1879 data->memory.free_func(data->memory.user_data, data->buffer_views[i].name);
1880 data->memory.free_func(data->memory.user_data, data->buffer_views[i].data);
1881
1882 cgltf_free_extensions(data, data->buffer_views[i].extensions, data->buffer_views[i].extensions_count);
1883 cgltf_free_extras(data, &data->buffer_views[i].extras);
1884 }
1885 data->memory.free_func(data->memory.user_data, data->buffer_views);
1886
1887 for (cgltf_size i = 0; i < data->buffers_count; ++i)
1888 {
1889 data->memory.free_func(data->memory.user_data, data->buffers[i].name);
1890
1891 if (data->buffers[i].data_free_method == cgltf_data_free_method_file_release)
1892 {
1893 file_release(&data->memory, &data->file, data->buffers[i].data, data->buffers[i].size);
1894 }
1895 else if (data->buffers[i].data_free_method == cgltf_data_free_method_memory_free)
1896 {
1897 data->memory.free_func(data->memory.user_data, data->buffers[i].data);
1898 }
1899
1900 data->memory.free_func(data->memory.user_data, data->buffers[i].uri);
1901
1902 cgltf_free_extensions(data, data->buffers[i].extensions, data->buffers[i].extensions_count);
1903 cgltf_free_extras(data, &data->buffers[i].extras);
1904 }
1905 data->memory.free_func(data->memory.user_data, data->buffers);
1906
1907 for (cgltf_size i = 0; i < data->meshes_count; ++i)
1908 {
1909 data->memory.free_func(data->memory.user_data, data->meshes[i].name);
1910
1911 for (cgltf_size j = 0; j < data->meshes[i].primitives_count; ++j)
1912 {
1913 for (cgltf_size k = 0; k < data->meshes[i].primitives[j].attributes_count; ++k)
1914 {
1915 data->memory.free_func(data->memory.user_data, data->meshes[i].primitives[j].attributes[k].name);
1916 }
1917
1918 data->memory.free_func(data->memory.user_data, data->meshes[i].primitives[j].attributes);
1919
1920 for (cgltf_size k = 0; k < data->meshes[i].primitives[j].targets_count; ++k)
1921 {
1922 for (cgltf_size m = 0; m < data->meshes[i].primitives[j].targets[k].attributes_count; ++m)
1923 {
1924 data->memory.free_func(data->memory.user_data, data->meshes[i].primitives[j].targets[k].attributes[m].name);
1925 }
1926
1927 data->memory.free_func(data->memory.user_data, data->meshes[i].primitives[j].targets[k].attributes);
1928 }
1929
1930 data->memory.free_func(data->memory.user_data, data->meshes[i].primitives[j].targets);
1931
1932 if (data->meshes[i].primitives[j].has_draco_mesh_compression)
1933 {
1934 for (cgltf_size k = 0; k < data->meshes[i].primitives[j].draco_mesh_compression.attributes_count; ++k)
1935 {
1936 data->memory.free_func(data->memory.user_data, data->meshes[i].primitives[j].draco_mesh_compression.attributes[k].name);
1937 }
1938
1939 data->memory.free_func(data->memory.user_data, data->meshes[i].primitives[j].draco_mesh_compression.attributes);
1940 }
1941
1942 for (cgltf_size k = 0; k < data->meshes[i].primitives[j].mappings_count; ++k)
1943 {
1944 cgltf_free_extras(data, &data->meshes[i].primitives[j].mappings[k].extras);
1945 }
1946
1947 data->memory.free_func(data->memory.user_data, data->meshes[i].primitives[j].mappings);
1948
1949 cgltf_free_extensions(data, data->meshes[i].primitives[j].extensions, data->meshes[i].primitives[j].extensions_count);
1950 cgltf_free_extras(data, &data->meshes[i].primitives[j].extras);
1951 }
1952
1953 data->memory.free_func(data->memory.user_data, data->meshes[i].primitives);
1954 data->memory.free_func(data->memory.user_data, data->meshes[i].weights);
1955
1956 for (cgltf_size j = 0; j < data->meshes[i].target_names_count; ++j)
1957 {
1958 data->memory.free_func(data->memory.user_data, data->meshes[i].target_names[j]);
1959 }
1960
1961 cgltf_free_extensions(data, data->meshes[i].extensions, data->meshes[i].extensions_count);
1962 cgltf_free_extras(data, &data->meshes[i].extras);
1963
1964 data->memory.free_func(data->memory.user_data, data->meshes[i].target_names);
1965 }
1966
1967 data->memory.free_func(data->memory.user_data, data->meshes);
1968
1969 for (cgltf_size i = 0; i < data->materials_count; ++i)
1970 {
1971 data->memory.free_func(data->memory.user_data, data->materials[i].name);
1972
1973 cgltf_free_extensions(data, data->materials[i].extensions, data->materials[i].extensions_count);
1974 cgltf_free_extras(data, &data->materials[i].extras);
1975 }
1976
1977 data->memory.free_func(data->memory.user_data, data->materials);
1978
1979 for (cgltf_size i = 0; i < data->images_count; ++i)
1980 {
1981 data->memory.free_func(data->memory.user_data, data->images[i].name);
1982 data->memory.free_func(data->memory.user_data, data->images[i].uri);
1983 data->memory.free_func(data->memory.user_data, data->images[i].mime_type);
1984
1985 cgltf_free_extensions(data, data->images[i].extensions, data->images[i].extensions_count);
1986 cgltf_free_extras(data, &data->images[i].extras);
1987 }
1988
1989 data->memory.free_func(data->memory.user_data, data->images);
1990
1991 for (cgltf_size i = 0; i < data->textures_count; ++i)
1992 {
1993 data->memory.free_func(data->memory.user_data, data->textures[i].name);
1994
1995 cgltf_free_extensions(data, data->textures[i].extensions, data->textures[i].extensions_count);
1996 cgltf_free_extras(data, &data->textures[i].extras);
1997 }
1998
1999 data->memory.free_func(data->memory.user_data, data->textures);
2000
2001 for (cgltf_size i = 0; i < data->samplers_count; ++i)
2002 {
2003 data->memory.free_func(data->memory.user_data, data->samplers[i].name);
2004
2005 cgltf_free_extensions(data, data->samplers[i].extensions, data->samplers[i].extensions_count);
2006 cgltf_free_extras(data, &data->samplers[i].extras);
2007 }
2008
2009 data->memory.free_func(data->memory.user_data, data->samplers);
2010
2011 for (cgltf_size i = 0; i < data->skins_count; ++i)
2012 {
2013 data->memory.free_func(data->memory.user_data, data->skins[i].name);
2014 data->memory.free_func(data->memory.user_data, data->skins[i].joints);
2015
2016 cgltf_free_extensions(data, data->skins[i].extensions, data->skins[i].extensions_count);
2017 cgltf_free_extras(data, &data->skins[i].extras);
2018 }
2019
2020 data->memory.free_func(data->memory.user_data, data->skins);
2021
2022 for (cgltf_size i = 0; i < data->cameras_count; ++i)
2023 {
2024 data->memory.free_func(data->memory.user_data, data->cameras[i].name);
2025
2026 if (data->cameras[i].type == cgltf_camera_type_perspective)
2027 {
2028 cgltf_free_extras(data, &data->cameras[i].data.perspective.extras);
2029 }
2030 else if (data->cameras[i].type == cgltf_camera_type_orthographic)
2031 {
2032 cgltf_free_extras(data, &data->cameras[i].data.orthographic.extras);
2033 }
2034
2035 cgltf_free_extensions(data, data->cameras[i].extensions, data->cameras[i].extensions_count);
2036 cgltf_free_extras(data, &data->cameras[i].extras);
2037 }
2038
2039 data->memory.free_func(data->memory.user_data, data->cameras);
2040
2041 for (cgltf_size i = 0; i < data->lights_count; ++i)
2042 {
2043 data->memory.free_func(data->memory.user_data, data->lights[i].name);
2044
2045 cgltf_free_extras(data, &data->lights[i].extras);
2046 }
2047
2048 data->memory.free_func(data->memory.user_data, data->lights);
2049
2050 for (cgltf_size i = 0; i < data->nodes_count; ++i)
2051 {
2052 data->memory.free_func(data->memory.user_data, data->nodes[i].name);
2053 data->memory.free_func(data->memory.user_data, data->nodes[i].children);
2054 data->memory.free_func(data->memory.user_data, data->nodes[i].weights);
2055
2056 if (data->nodes[i].has_mesh_gpu_instancing)
2057 {
2058 for (cgltf_size j = 0; j < data->nodes[i].mesh_gpu_instancing.attributes_count; ++j)
2059 {
2060 data->memory.free_func(data->memory.user_data, data->nodes[i].mesh_gpu_instancing.attributes[j].name);
2061 }
2062
2063 data->memory.free_func(data->memory.user_data, data->nodes[i].mesh_gpu_instancing.attributes);
2064 }
2065
2066 cgltf_free_extensions(data, data->nodes[i].extensions, data->nodes[i].extensions_count);
2067 cgltf_free_extras(data, &data->nodes[i].extras);
2068 }
2069
2070 data->memory.free_func(data->memory.user_data, data->nodes);
2071
2072 for (cgltf_size i = 0; i < data->scenes_count; ++i)
2073 {
2074 data->memory.free_func(data->memory.user_data, data->scenes[i].name);
2075 data->memory.free_func(data->memory.user_data, data->scenes[i].nodes);
2076
2077 cgltf_free_extensions(data, data->scenes[i].extensions, data->scenes[i].extensions_count);
2078 cgltf_free_extras(data, &data->scenes[i].extras);
2079 }
2080
2081 data->memory.free_func(data->memory.user_data, data->scenes);
2082
2083 for (cgltf_size i = 0; i < data->animations_count; ++i)
2084 {
2085 data->memory.free_func(data->memory.user_data, data->animations[i].name);
2086 for (cgltf_size j = 0; j < data->animations[i].samplers_count; ++j)
2087 {
2088 cgltf_free_extensions(data, data->animations[i].samplers[j].extensions, data->animations[i].samplers[j].extensions_count);
2089 cgltf_free_extras(data, &data->animations[i].samplers[j].extras);
2090 }
2091 data->memory.free_func(data->memory.user_data, data->animations[i].samplers);
2092
2093 for (cgltf_size j = 0; j < data->animations[i].channels_count; ++j)
2094 {
2095 cgltf_free_extensions(data, data->animations[i].channels[j].extensions, data->animations[i].channels[j].extensions_count);
2096 cgltf_free_extras(data, &data->animations[i].channels[j].extras);
2097 }
2098 data->memory.free_func(data->memory.user_data, data->animations[i].channels);
2099
2100 cgltf_free_extensions(data, data->animations[i].extensions, data->animations[i].extensions_count);
2101 cgltf_free_extras(data, &data->animations[i].extras);
2102 }
2103
2104 data->memory.free_func(data->memory.user_data, data->animations);
2105
2106 for (cgltf_size i = 0; i < data->variants_count; ++i)
2107 {
2108 data->memory.free_func(data->memory.user_data, data->variants[i].name);
2109
2110 cgltf_free_extras(data, &data->variants[i].extras);
2111 }
2112
2113 data->memory.free_func(data->memory.user_data, data->variants);
2114
2115 cgltf_free_extensions(data, data->data_extensions, data->data_extensions_count);
2116 cgltf_free_extras(data, &data->extras);
2117
2118 for (cgltf_size i = 0; i < data->extensions_used_count; ++i)
2119 {
2120 data->memory.free_func(data->memory.user_data, data->extensions_used[i]);
2121 }
2122
2123 data->memory.free_func(data->memory.user_data, data->extensions_used);
2124
2125 for (cgltf_size i = 0; i < data->extensions_required_count; ++i)
2126 {
2127 data->memory.free_func(data->memory.user_data, data->extensions_required[i]);
2128 }
2129
2130 data->memory.free_func(data->memory.user_data, data->extensions_required);
2131
2132 file_release(&data->memory, &data->file, data->file_data, data->file_size);
2133
2134 data->memory.free_func(data->memory.user_data, data);
2135 }
2136
2137 void cgltf_node_transform_local(const cgltf_node* node, cgltf_float* out_matrix)
2138 {
2139 cgltf_float* lm = out_matrix;
2140
2141 if (node->has_matrix)
2142 {
2143 memcpy(lm, node->matrix, sizeof(float) * 16);
2144 }
2145 else
2146 {
2147 float tx = node->translation[0];
2148 float ty = node->translation[1];
2149 float tz = node->translation[2];
2150
2151 float qx = node->rotation[0];
2152 float qy = node->rotation[1];
2153 float qz = node->rotation[2];
2154 float qw = node->rotation[3];
2155
2156 float sx = node->scale[0];
2157 float sy = node->scale[1];
2158 float sz = node->scale[2];
2159
2160 lm[0] = (1 - 2 * qy*qy - 2 * qz*qz) * sx;
2161 lm[1] = (2 * qx*qy + 2 * qz*qw) * sx;
2162 lm[2] = (2 * qx*qz - 2 * qy*qw) * sx;
2163 lm[3] = 0.f;
2164
2165 lm[4] = (2 * qx*qy - 2 * qz*qw) * sy;
2166 lm[5] = (1 - 2 * qx*qx - 2 * qz*qz) * sy;
2167 lm[6] = (2 * qy*qz + 2 * qx*qw) * sy;
2168 lm[7] = 0.f;
2169
2170 lm[8] = (2 * qx*qz + 2 * qy*qw) * sz;
2171 lm[9] = (2 * qy*qz - 2 * qx*qw) * sz;
2172 lm[10] = (1 - 2 * qx*qx - 2 * qy*qy) * sz;
2173 lm[11] = 0.f;
2174
2175 lm[12] = tx;
2176 lm[13] = ty;
2177 lm[14] = tz;
2178 lm[15] = 1.f;
2179 }
2180 }
2181
2182 void cgltf_node_transform_world(const cgltf_node* node, cgltf_float* out_matrix)
2183 {
2184 cgltf_float* lm = out_matrix;
2185 cgltf_node_transform_local(node, lm);
2186
2187 const cgltf_node* parent = node->parent;
2188
2189 while (parent)
2190 {
2191 float pm[16];
2192 cgltf_node_transform_local(parent, pm);
2193
2194 for (int i = 0; i < 4; ++i)
2195 {
2196 float l0 = lm[i * 4 + 0];
2197 float l1 = lm[i * 4 + 1];
2198 float l2 = lm[i * 4 + 2];
2199
2200 float r0 = l0 * pm[0] + l1 * pm[4] + l2 * pm[8];
2201 float r1 = l0 * pm[1] + l1 * pm[5] + l2 * pm[9];
2202 float r2 = l0 * pm[2] + l1 * pm[6] + l2 * pm[10];
2203
2204 lm[i * 4 + 0] = r0;
2205 lm[i * 4 + 1] = r1;
2206 lm[i * 4 + 2] = r2;
2207 }
2208
2209 lm[12] += pm[12];
2210 lm[13] += pm[13];
2211 lm[14] += pm[14];
2212
2213 parent = parent->parent;
2214 }
2215 }
2216
2217 static cgltf_ssize cgltf_component_read_integer(const void* in, cgltf_component_type component_type)
2218 {
2219 switch (component_type)
2220 {
2221 case cgltf_component_type_r_16:
2222 return *((const int16_t*) in);
2223 case cgltf_component_type_r_16u:
2224 return *((const uint16_t*) in);
2225 case cgltf_component_type_r_32u:
2226 return *((const uint32_t*) in);
2227 case cgltf_component_type_r_8:
2228 return *((const int8_t*) in);
2229 case cgltf_component_type_r_8u:
2230 return *((const uint8_t*) in);
2231 default:
2232 return 0;
2233 }
2234 }
2235
2236 static cgltf_size cgltf_component_read_index(const void* in, cgltf_component_type component_type)
2237 {
2238 switch (component_type)
2239 {
2240 case cgltf_component_type_r_16u:
2241 return *((const uint16_t*) in);
2242 case cgltf_component_type_r_32u:
2243 return *((const uint32_t*) in);
2244 case cgltf_component_type_r_8u:
2245 return *((const uint8_t*) in);
2246 default:
2247 return 0;
2248 }
2249 }
2250
2251 static cgltf_float cgltf_component_read_float(const void* in, cgltf_component_type component_type, cgltf_bool normalized)
2252 {
2253 if (component_type == cgltf_component_type_r_32f)
2254 {
2255 return *((const float*) in);
2256 }
2257
2258 if (normalized)
2259 {
2260 switch (component_type)
2261 {
2262 // note: glTF spec doesn't currently define normalized conversions for 32-bit integers
2263 case cgltf_component_type_r_16:
2264 return *((const int16_t*) in) / (cgltf_float)32767;
2265 case cgltf_component_type_r_16u:
2266 return *((const uint16_t*) in) / (cgltf_float)65535;
2267 case cgltf_component_type_r_8:
2268 return *((const int8_t*) in) / (cgltf_float)127;
2269 case cgltf_component_type_r_8u:
2270 return *((const uint8_t*) in) / (cgltf_float)255;
2271 default:
2272 return 0;
2273 }
2274 }
2275
2276 return (cgltf_float)cgltf_component_read_integer(in, component_type);
2277 }
2278
2279 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)
2280 {
2281 cgltf_size num_components = cgltf_num_components(type);
2282
2283 if (element_size < num_components) {
2284 return 0;
2285 }
2286
2287 // There are three special cases for component extraction, see #data-alignment in the 2.0 spec.
2288
2289 cgltf_size component_size = cgltf_component_size(component_type);
2290
2291 if (type == cgltf_type_mat2 && component_size == 1)
2292 {
2293 out[0] = cgltf_component_read_float(element, component_type, normalized);
2294 out[1] = cgltf_component_read_float(element + 1, component_type, normalized);
2295 out[2] = cgltf_component_read_float(element + 4, component_type, normalized);
2296 out[3] = cgltf_component_read_float(element + 5, component_type, normalized);
2297 return 1;
2298 }
2299
2300 if (type == cgltf_type_mat3 && component_size == 1)
2301 {
2302 out[0] = cgltf_component_read_float(element, component_type, normalized);
2303 out[1] = cgltf_component_read_float(element + 1, component_type, normalized);
2304 out[2] = cgltf_component_read_float(element + 2, component_type, normalized);
2305 out[3] = cgltf_component_read_float(element + 4, component_type, normalized);
2306 out[4] = cgltf_component_read_float(element + 5, component_type, normalized);
2307 out[5] = cgltf_component_read_float(element + 6, component_type, normalized);
2308 out[6] = cgltf_component_read_float(element + 8, component_type, normalized);
2309 out[7] = cgltf_component_read_float(element + 9, component_type, normalized);
2310 out[8] = cgltf_component_read_float(element + 10, component_type, normalized);
2311 return 1;
2312 }
2313
2314 if (type == cgltf_type_mat3 && component_size == 2)
2315 {
2316 out[0] = cgltf_component_read_float(element, component_type, normalized);
2317 out[1] = cgltf_component_read_float(element + 2, component_type, normalized);
2318 out[2] = cgltf_component_read_float(element + 4, component_type, normalized);
2319 out[3] = cgltf_component_read_float(element + 8, component_type, normalized);
2320 out[4] = cgltf_component_read_float(element + 10, component_type, normalized);
2321 out[5] = cgltf_component_read_float(element + 12, component_type, normalized);
2322 out[6] = cgltf_component_read_float(element + 16, component_type, normalized);
2323 out[7] = cgltf_component_read_float(element + 18, component_type, normalized);
2324 out[8] = cgltf_component_read_float(element + 20, component_type, normalized);
2325 return 1;
2326 }
2327
2328 for (cgltf_size i = 0; i < num_components; ++i)
2329 {
2330 out[i] = cgltf_component_read_float(element + component_size * i, component_type, normalized);
2331 }
2332 return 1;
2333 }
2334
2335 const uint8_t* cgltf_buffer_view_data(const cgltf_buffer_view* view)
2336 {
2337 if (view->data)
2338 return (const uint8_t*)view->data;
2339
2340 if (!view->buffer->data)
2341 return NULL;
2342
2343 const uint8_t* result = (const uint8_t*)view->buffer->data;
2344 result += view->offset;
2345 return result;
2346 }
2347
2348 const cgltf_accessor* cgltf_find_accessor(const cgltf_primitive* prim, cgltf_attribute_type type, cgltf_int index)
2349 {
2350 for (cgltf_size i = 0; i < prim->attributes_count; ++i)
2351 {
2352 const cgltf_attribute* attr = &prim->attributes[i];
2353 if (attr->type == type && attr->index == index)
2354 return attr->data;
2355 }
2356
2357 return NULL;
2358 }
2359
2360 static const uint8_t* cgltf_find_sparse_index(const cgltf_accessor* accessor, cgltf_size needle)
2361 {
2362 const cgltf_accessor_sparse* sparse = &accessor->sparse;
2363 const uint8_t* index_data = cgltf_buffer_view_data(sparse->indices_buffer_view);
2364 const uint8_t* value_data = cgltf_buffer_view_data(sparse->values_buffer_view);
2365
2366 if (index_data == NULL || value_data == NULL)
2367 return NULL;
2368
2369 index_data += sparse->indices_byte_offset;
2370 value_data += sparse->values_byte_offset;
2371
2372 cgltf_size index_stride = cgltf_component_size(sparse->indices_component_type);
2373
2374 cgltf_size offset = 0;
2375 cgltf_size length = sparse->count;
2376
2377 while (length)
2378 {
2379 cgltf_size rem = length % 2;
2380 length /= 2;
2381
2382 cgltf_size index = cgltf_component_read_index(index_data + (offset + length) * index_stride, sparse->indices_component_type);
2383 offset += index < needle ? length + rem : 0;
2384 }
2385
2386 if (offset == sparse->count)
2387 return NULL;
2388
2389 cgltf_size index = cgltf_component_read_index(index_data + offset * index_stride, sparse->indices_component_type);
2390 return index == needle ? value_data + offset * accessor->stride : NULL;
2391 }
2392
2393 cgltf_bool cgltf_accessor_read_float(const cgltf_accessor* accessor, cgltf_size index, cgltf_float* out, cgltf_size element_size)
2394 {
2395 if (accessor->is_sparse)
2396 {
2397 const uint8_t* element = cgltf_find_sparse_index(accessor, index);
2398 if (element)
2399 return cgltf_element_read_float(element, accessor->type, accessor->component_type, accessor->normalized, out, element_size);
2400 }
2401 if (accessor->buffer_view == NULL)
2402 {
2403 memset(out, 0, element_size * sizeof(cgltf_float));
2404 return 1;
2405 }
2406 const uint8_t* element = cgltf_buffer_view_data(accessor->buffer_view);
2407 if (element == NULL)
2408 {
2409 return 0;
2410 }
2411 element += accessor->offset + accessor->stride * index;
2412 return cgltf_element_read_float(element, accessor->type, accessor->component_type, accessor->normalized, out, element_size);
2413 }
2414
2415 cgltf_size cgltf_accessor_unpack_floats(const cgltf_accessor* accessor, cgltf_float* out, cgltf_size float_count)
2416 {
2417 cgltf_size floats_per_element = cgltf_num_components(accessor->type);
2418 cgltf_size available_floats = accessor->count * floats_per_element;
2419 if (out == NULL)
2420 {
2421 return available_floats;
2422 }
2423
2424 float_count = available_floats < float_count ? available_floats : float_count;
2425 cgltf_size element_count = float_count / floats_per_element;
2426
2427 // First pass: convert each element in the base accessor.
2428 if (accessor->buffer_view == NULL)
2429 {
2430 memset(out, 0, element_count * floats_per_element * sizeof(cgltf_float));
2431 }
2432 else
2433 {
2434 const uint8_t* element = cgltf_buffer_view_data(accessor->buffer_view);
2435 if (element == NULL)
2436 {
2437 return 0;
2438 }
2439 element += accessor->offset;
2440
2441 if (accessor->component_type == cgltf_component_type_r_32f && accessor->stride == floats_per_element * sizeof(cgltf_float))
2442 {
2443 memcpy(out, element, element_count * floats_per_element * sizeof(cgltf_float));
2444 }
2445 else
2446 {
2447 cgltf_float* dest = out;
2448
2449 for (cgltf_size index = 0; index < element_count; index++, dest += floats_per_element, element += accessor->stride)
2450 {
2451 if (!cgltf_element_read_float(element, accessor->type, accessor->component_type, accessor->normalized, dest, floats_per_element))
2452 {
2453 return 0;
2454 }
2455 }
2456 }
2457 }
2458
2459 // Second pass: write out each element in the sparse accessor.
2460 if (accessor->is_sparse)
2461 {
2462 const cgltf_accessor_sparse* sparse = &accessor->sparse;
2463
2464 const uint8_t* index_data = cgltf_buffer_view_data(sparse->indices_buffer_view);
2465 const uint8_t* reader_head = cgltf_buffer_view_data(sparse->values_buffer_view);
2466
2467 if (index_data == NULL || reader_head == NULL)
2468 {
2469 return 0;
2470 }
2471
2472 index_data += sparse->indices_byte_offset;
2473 reader_head += sparse->values_byte_offset;
2474
2475 cgltf_size index_stride = cgltf_component_size(sparse->indices_component_type);
2476 for (cgltf_size reader_index = 0; reader_index < sparse->count; reader_index++, index_data += index_stride, reader_head += accessor->stride)
2477 {
2478 size_t writer_index = cgltf_component_read_index(index_data, sparse->indices_component_type);
2479 float* writer_head = out + writer_index * floats_per_element;
2480
2481 if (!cgltf_element_read_float(reader_head, accessor->type, accessor->component_type, accessor->normalized, writer_head, floats_per_element))
2482 {
2483 return 0;
2484 }
2485 }
2486 }
2487
2488 return element_count * floats_per_element;
2489 }
2490
2491 static cgltf_uint cgltf_component_read_uint(const void* in, cgltf_component_type component_type)
2492 {
2493 switch (component_type)
2494 {
2495 case cgltf_component_type_r_8:
2496 return *((const int8_t*) in);
2497
2498 case cgltf_component_type_r_8u:
2499 return *((const uint8_t*) in);
2500
2501 case cgltf_component_type_r_16:
2502 return *((const int16_t*) in);
2503
2504 case cgltf_component_type_r_16u:
2505 return *((const uint16_t*) in);
2506
2507 case cgltf_component_type_r_32u:
2508 return *((const uint32_t*) in);
2509
2510 default:
2511 return 0;
2512 }
2513 }
2514
2515 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)
2516 {
2517 cgltf_size num_components = cgltf_num_components(type);
2518
2519 if (element_size < num_components)
2520 {
2521 return 0;
2522 }
2523
2524 // Reading integer matrices is not a valid use case
2525 if (type == cgltf_type_mat2 || type == cgltf_type_mat3 || type == cgltf_type_mat4)
2526 {
2527 return 0;
2528 }
2529
2530 cgltf_size component_size = cgltf_component_size(component_type);
2531
2532 for (cgltf_size i = 0; i < num_components; ++i)
2533 {
2534 out[i] = cgltf_component_read_uint(element + component_size * i, component_type);
2535 }
2536 return 1;
2537 }
2538
2539 cgltf_bool cgltf_accessor_read_uint(const cgltf_accessor* accessor, cgltf_size index, cgltf_uint* out, cgltf_size element_size)
2540 {
2541 if (accessor->is_sparse)
2542 {
2543 const uint8_t* element = cgltf_find_sparse_index(accessor, index);
2544 if (element)
2545 return cgltf_element_read_uint(element, accessor->type, accessor->component_type, out, element_size);
2546 }
2547 if (accessor->buffer_view == NULL)
2548 {
2549 memset(out, 0, element_size * sizeof(cgltf_uint));
2550 return 1;
2551 }
2552 const uint8_t* element = cgltf_buffer_view_data(accessor->buffer_view);
2553 if (element == NULL)
2554 {
2555 return 0;
2556 }
2557 element += accessor->offset + accessor->stride * index;
2558 return cgltf_element_read_uint(element, accessor->type, accessor->component_type, out, element_size);
2559 }
2560
2561 cgltf_size cgltf_accessor_read_index(const cgltf_accessor* accessor, cgltf_size index)
2562 {
2563 if (accessor->is_sparse)
2564 {
2565 const uint8_t* element = cgltf_find_sparse_index(accessor, index);
2566 if (element)
2567 return cgltf_component_read_index(element, accessor->component_type);
2568 }
2569 if (accessor->buffer_view == NULL)
2570 {
2571 return 0;
2572 }
2573 const uint8_t* element = cgltf_buffer_view_data(accessor->buffer_view);
2574 if (element == NULL)
2575 {
2576 return 0; // This is an error case, but we can't communicate the error with existing interface.
2577 }
2578 element += accessor->offset + accessor->stride * index;
2579 return cgltf_component_read_index(element, accessor->component_type);
2580 }
2581
2582 cgltf_size cgltf_mesh_index(const cgltf_data* data, const cgltf_mesh* object)
2583 {
2584 assert(object && (cgltf_size)(object - data->meshes) < data->meshes_count);
2585 return (cgltf_size)(object - data->meshes);
2586 }
2587
2588 cgltf_size cgltf_material_index(const cgltf_data* data, const cgltf_material* object)
2589 {
2590 assert(object && (cgltf_size)(object - data->materials) < data->materials_count);
2591 return (cgltf_size)(object - data->materials);
2592 }
2593
2594 cgltf_size cgltf_accessor_index(const cgltf_data* data, const cgltf_accessor* object)
2595 {
2596 assert(object && (cgltf_size)(object - data->accessors) < data->accessors_count);
2597 return (cgltf_size)(object - data->accessors);
2598 }
2599
2600 cgltf_size cgltf_buffer_view_index(const cgltf_data* data, const cgltf_buffer_view* object)
2601 {
2602 assert(object && (cgltf_size)(object - data->buffer_views) < data->buffer_views_count);
2603 return (cgltf_size)(object - data->buffer_views);
2604 }
2605
2606 cgltf_size cgltf_buffer_index(const cgltf_data* data, const cgltf_buffer* object)
2607 {
2608 assert(object && (cgltf_size)(object - data->buffers) < data->buffers_count);
2609 return (cgltf_size)(object - data->buffers);
2610 }
2611
2612 cgltf_size cgltf_image_index(const cgltf_data* data, const cgltf_image* object)
2613 {
2614 assert(object && (cgltf_size)(object - data->images) < data->images_count);
2615 return (cgltf_size)(object - data->images);
2616 }
2617
2618 cgltf_size cgltf_texture_index(const cgltf_data* data, const cgltf_texture* object)
2619 {
2620 assert(object && (cgltf_size)(object - data->textures) < data->textures_count);
2621 return (cgltf_size)(object - data->textures);
2622 }
2623
2624 cgltf_size cgltf_sampler_index(const cgltf_data* data, const cgltf_sampler* object)
2625 {
2626 assert(object && (cgltf_size)(object - data->samplers) < data->samplers_count);
2627 return (cgltf_size)(object - data->samplers);
2628 }
2629
2630 cgltf_size cgltf_skin_index(const cgltf_data* data, const cgltf_skin* object)
2631 {
2632 assert(object && (cgltf_size)(object - data->skins) < data->skins_count);
2633 return (cgltf_size)(object - data->skins);
2634 }
2635
2636 cgltf_size cgltf_camera_index(const cgltf_data* data, const cgltf_camera* object)
2637 {
2638 assert(object && (cgltf_size)(object - data->cameras) < data->cameras_count);
2639 return (cgltf_size)(object - data->cameras);
2640 }
2641
2642 cgltf_size cgltf_light_index(const cgltf_data* data, const cgltf_light* object)
2643 {
2644 assert(object && (cgltf_size)(object - data->lights) < data->lights_count);
2645 return (cgltf_size)(object - data->lights);
2646 }
2647
2648 cgltf_size cgltf_node_index(const cgltf_data* data, const cgltf_node* object)
2649 {
2650 assert(object && (cgltf_size)(object - data->nodes) < data->nodes_count);
2651 return (cgltf_size)(object - data->nodes);
2652 }
2653
2654 cgltf_size cgltf_scene_index(const cgltf_data* data, const cgltf_scene* object)
2655 {
2656 assert(object && (cgltf_size)(object - data->scenes) < data->scenes_count);
2657 return (cgltf_size)(object - data->scenes);
2658 }
2659
2660 cgltf_size cgltf_animation_index(const cgltf_data* data, const cgltf_animation* object)
2661 {
2662 assert(object && (cgltf_size)(object - data->animations) < data->animations_count);
2663 return (cgltf_size)(object - data->animations);
2664 }
2665
2666 cgltf_size cgltf_animation_sampler_index(const cgltf_animation* animation, const cgltf_animation_sampler* object)
2667 {
2668 assert(object && (cgltf_size)(object - animation->samplers) < animation->samplers_count);
2669 return (cgltf_size)(object - animation->samplers);
2670 }
2671
2672 cgltf_size cgltf_animation_channel_index(const cgltf_animation* animation, const cgltf_animation_channel* object)
2673 {
2674 assert(object && (cgltf_size)(object - animation->channels) < animation->channels_count);
2675 return (cgltf_size)(object - animation->channels);
2676 }
2677
2678 cgltf_size cgltf_accessor_unpack_indices(const cgltf_accessor* accessor, void* out, cgltf_size out_component_size, cgltf_size index_count)
2679 {
2680 if (out == NULL)
2681 {
2682 return accessor->count;
2683 }
2684
2685 cgltf_size numbers_per_element = cgltf_num_components(accessor->type);
2686 cgltf_size available_numbers = accessor->count * numbers_per_element;
2687
2688 index_count = available_numbers < index_count ? available_numbers : index_count;
2689 cgltf_size index_component_size = cgltf_component_size(accessor->component_type);
2690
2691 if (accessor->is_sparse)
2692 {
2693 return 0;
2694 }
2695 if (accessor->buffer_view == NULL)
2696 {
2697 return 0;
2698 }
2699 if (index_component_size > out_component_size)
2700 {
2701 return 0;
2702 }
2703 const uint8_t* element = cgltf_buffer_view_data(accessor->buffer_view);
2704 if (element == NULL)
2705 {
2706 return 0;
2707 }
2708 element += accessor->offset;
2709
2710 if (index_component_size == out_component_size && accessor->stride == out_component_size * numbers_per_element)
2711 {
2712 memcpy(out, element, index_count * index_component_size);
2713 return index_count;
2714 }
2715
2716 // Data couldn't be copied with memcpy due to stride being larger than the component size.
2717 // OR
2718 // The component size of the output array is larger than the component size of the index data, so index data will be padded.
2719 switch (out_component_size)
2720 {
2721 case 1:
2722 for (cgltf_size index = 0; index < index_count; index++, element += accessor->stride)
2723 {
2724 ((uint8_t*)out)[index] = (uint8_t)cgltf_component_read_index(element, accessor->component_type);
2725 }
2726 break;
2727 case 2:
2728 for (cgltf_size index = 0; index < index_count; index++, element += accessor->stride)
2729 {
2730 ((uint16_t*)out)[index] = (uint16_t)cgltf_component_read_index(element, accessor->component_type);
2731 }
2732 break;
2733 case 4:
2734 for (cgltf_size index = 0; index < index_count; index++, element += accessor->stride)
2735 {
2736 ((uint32_t*)out)[index] = (uint32_t)cgltf_component_read_index(element, accessor->component_type);
2737 }
2738 break;
2739 default:
2740 return 0;
2741 }
2742
2743 return index_count;
2744 }
2745
2746 #define CGLTF_ERROR_JSON -1
2747 #define CGLTF_ERROR_NOMEM -2
2748 #define CGLTF_ERROR_LEGACY -3
2749
2750 #define CGLTF_CHECK_TOKTYPE(tok_, type_) if ((tok_).type != (type_)) { return CGLTF_ERROR_JSON; }
2751 #define CGLTF_CHECK_TOKTYPE_RET(tok_, type_, ret_) if ((tok_).type != (type_)) { return ret_; }
2752 #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 */
2753
2754 #define CGLTF_PTRINDEX(type, idx) (type*)((cgltf_size)idx + 1)
2755 #define CGLTF_PTRFIXUP(var, data, size) if (var) { if ((cgltf_size)var > size) { return CGLTF_ERROR_JSON; } var = &data[(cgltf_size)var-1]; }
2756 #define CGLTF_PTRFIXUP_REQ(var, data, size) if (!var || (cgltf_size)var > size) { return CGLTF_ERROR_JSON; } var = &data[(cgltf_size)var-1];
2757
2758 static int cgltf_json_strcmp(jsmntok_t const* tok, const uint8_t* json_chunk, const char* str)
2759 {
2760 CGLTF_CHECK_TOKTYPE(*tok, JSMN_STRING);
2761 size_t const str_len = strlen(str);
2762 size_t const name_length = (size_t)(tok->end - tok->start);
2763 return (str_len == name_length) ? strncmp((const char*)json_chunk + tok->start, str, str_len) : 128;
2764 }
2765
2766 static int cgltf_json_to_int(jsmntok_t const* tok, const uint8_t* json_chunk)
2767 {
2768 CGLTF_CHECK_TOKTYPE(*tok, JSMN_PRIMITIVE);
2769 char tmp[128];
2770 int size = (size_t)(tok->end - tok->start) < sizeof(tmp) ? (int)(tok->end - tok->start) : (int)(sizeof(tmp) - 1);
2771 strncpy(tmp, (const char*)json_chunk + tok->start, size);
2772 tmp[size] = 0;
2773 return CGLTF_ATOI(tmp);
2774 }
2775
2776 static cgltf_size cgltf_json_to_size(jsmntok_t const* tok, const uint8_t* json_chunk)
2777 {
2778 CGLTF_CHECK_TOKTYPE_RET(*tok, JSMN_PRIMITIVE, 0);
2779 char tmp[128];
2780 int size = (size_t)(tok->end - tok->start) < sizeof(tmp) ? (int)(tok->end - tok->start) : (int)(sizeof(tmp) - 1);
2781 strncpy(tmp, (const char*)json_chunk + tok->start, size);
2782 tmp[size] = 0;
2783 long long res = CGLTF_ATOLL(tmp);
2784 return res < 0 ? 0 : (cgltf_size)res;
2785 }
2786
2787 static cgltf_float cgltf_json_to_float(jsmntok_t const* tok, const uint8_t* json_chunk)
2788 {
2789 CGLTF_CHECK_TOKTYPE(*tok, JSMN_PRIMITIVE);
2790 char tmp[128];
2791 int size = (size_t)(tok->end - tok->start) < sizeof(tmp) ? (int)(tok->end - tok->start) : (int)(sizeof(tmp) - 1);
2792 strncpy(tmp, (const char*)json_chunk + tok->start, size);
2793 tmp[size] = 0;
2794 return (cgltf_float)CGLTF_ATOF(tmp);
2795 }
2796
2797 static cgltf_bool cgltf_json_to_bool(jsmntok_t const* tok, const uint8_t* json_chunk)
2798 {
2799 int size = (int)(tok->end - tok->start);
2800 return size == 4 && memcmp(json_chunk + tok->start, "true", 4) == 0;
2801 }
2802
2803 static int cgltf_skip_json(jsmntok_t const* tokens, int i)
2804 {
2805 int end = i + 1;
2806
2807 while (i < end)
2808 {
2809 switch (tokens[i].type)
2810 {
2811 case JSMN_OBJECT:
2812 end += tokens[i].size * 2;
2813 break;
2814
2815 case JSMN_ARRAY:
2816 end += tokens[i].size;
2817 break;
2818
2819 case JSMN_PRIMITIVE:
2820 case JSMN_STRING:
2821 break;
2822
2823 default:
2824 return -1;
2825 }
2826
2827 i++;
2828 }
2829
2830 return i;
2831 }
2832
2833 static void cgltf_fill_float_array(float* out_array, int size, float value)
2834 {
2835 for (int j = 0; j < size; ++j)
2836 {
2837 out_array[j] = value;
2838 }
2839 }
2840
2841 static int cgltf_parse_json_float_array(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, float* out_array, int size)
2842 {
2843 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_ARRAY);
2844 if (tokens[i].size != size)
2845 {
2846 return CGLTF_ERROR_JSON;
2847 }
2848 ++i;
2849 for (int j = 0; j < size; ++j)
2850 {
2851 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
2852 out_array[j] = cgltf_json_to_float(tokens + i, json_chunk);
2853 ++i;
2854 }
2855 return i;
2856 }
2857
2858 static int cgltf_parse_json_string(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, char** out_string)
2859 {
2860 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_STRING);
2861 if (*out_string)
2862 {
2863 return CGLTF_ERROR_JSON;
2864 }
2865 int size = (int)(tokens[i].end - tokens[i].start);
2866 char* result = (char*)options->memory.alloc_func(options->memory.user_data, size + 1);
2867 if (!result)
2868 {
2869 return CGLTF_ERROR_NOMEM;
2870 }
2871 strncpy(result, (const char*)json_chunk + tokens[i].start, size);
2872 result[size] = 0;
2873 *out_string = result;
2874 return i + 1;
2875 }
2876
2877 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)
2878 {
2879 (void)json_chunk;
2880 if (tokens[i].type != JSMN_ARRAY)
2881 {
2882 return tokens[i].type == JSMN_OBJECT ? CGLTF_ERROR_LEGACY : CGLTF_ERROR_JSON;
2883 }
2884 if (*out_array)
2885 {
2886 return CGLTF_ERROR_JSON;
2887 }
2888 int size = tokens[i].size;
2889 void* result = cgltf_calloc(options, element_size, size);
2890 if (!result)
2891 {
2892 return CGLTF_ERROR_NOMEM;
2893 }
2894 *out_array = result;
2895 *out_size = size;
2896 return i + 1;
2897 }
2898
2899 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)
2900 {
2901 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_ARRAY);
2902 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(char*), (void**)out_array, out_size);
2903 if (i < 0)
2904 {
2905 return i;
2906 }
2907
2908 for (cgltf_size j = 0; j < *out_size; ++j)
2909 {
2910 i = cgltf_parse_json_string(options, tokens, i, json_chunk, j + (*out_array));
2911 if (i < 0)
2912 {
2913 return i;
2914 }
2915 }
2916 return i;
2917 }
2918
2919 static void cgltf_parse_attribute_type(const char* name, cgltf_attribute_type* out_type, int* out_index)
2920 {
2921 if (*name == '_')
2922 {
2923 *out_type = cgltf_attribute_type_custom;
2924 return;
2925 }
2926
2927 const char* us = strchr(name, '_');
2928 size_t len = us ? (size_t)(us - name) : strlen(name);
2929
2930 if (len == 8 && strncmp(name, "POSITION", 8) == 0)
2931 {
2932 *out_type = cgltf_attribute_type_position;
2933 }
2934 else if (len == 6 && strncmp(name, "NORMAL", 6) == 0)
2935 {
2936 *out_type = cgltf_attribute_type_normal;
2937 }
2938 else if (len == 7 && strncmp(name, "TANGENT", 7) == 0)
2939 {
2940 *out_type = cgltf_attribute_type_tangent;
2941 }
2942 else if (len == 8 && strncmp(name, "TEXCOORD", 8) == 0)
2943 {
2944 *out_type = cgltf_attribute_type_texcoord;
2945 }
2946 else if (len == 5 && strncmp(name, "COLOR", 5) == 0)
2947 {
2948 *out_type = cgltf_attribute_type_color;
2949 }
2950 else if (len == 6 && strncmp(name, "JOINTS", 6) == 0)
2951 {
2952 *out_type = cgltf_attribute_type_joints;
2953 }
2954 else if (len == 7 && strncmp(name, "WEIGHTS", 7) == 0)
2955 {
2956 *out_type = cgltf_attribute_type_weights;
2957 }
2958 else
2959 {
2960 *out_type = cgltf_attribute_type_invalid;
2961 }
2962
2963 if (us && *out_type != cgltf_attribute_type_invalid)
2964 {
2965 *out_index = CGLTF_ATOI(us + 1);
2966 if (*out_index < 0)
2967 {
2968 *out_type = cgltf_attribute_type_invalid;
2969 *out_index = 0;
2970 }
2971 }
2972 }
2973
2974 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)
2975 {
2976 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
2977
2978 if (*out_attributes)
2979 {
2980 return CGLTF_ERROR_JSON;
2981 }
2982
2983 *out_attributes_count = tokens[i].size;
2984 *out_attributes = (cgltf_attribute*)cgltf_calloc(options, sizeof(cgltf_attribute), *out_attributes_count);
2985 ++i;
2986
2987 if (!*out_attributes)
2988 {
2989 return CGLTF_ERROR_NOMEM;
2990 }
2991
2992 for (cgltf_size j = 0; j < *out_attributes_count; ++j)
2993 {
2994 CGLTF_CHECK_KEY(tokens[i]);
2995
2996 i = cgltf_parse_json_string(options, tokens, i, json_chunk, &(*out_attributes)[j].name);
2997 if (i < 0)
2998 {
2999 return CGLTF_ERROR_JSON;
3000 }
3001
3002 cgltf_parse_attribute_type((*out_attributes)[j].name, &(*out_attributes)[j].type, &(*out_attributes)[j].index);
3003
3004 (*out_attributes)[j].data = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
3005 ++i;
3006 }
3007
3008 return i;
3009 }
3010
3011 static int cgltf_parse_json_extras(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_extras* out_extras)
3012 {
3013 if (out_extras->data)
3014 {
3015 return CGLTF_ERROR_JSON;
3016 }
3017
3018 /* fill deprecated fields for now, this will be removed in the future */
3019 out_extras->start_offset = tokens[i].start;
3020 out_extras->end_offset = tokens[i].end;
3021
3022 size_t start = tokens[i].start;
3023 size_t size = tokens[i].end - start;
3024 out_extras->data = (char*)options->memory.alloc_func(options->memory.user_data, size + 1);
3025 if (!out_extras->data)
3026 {
3027 return CGLTF_ERROR_NOMEM;
3028 }
3029 strncpy(out_extras->data, (const char*)json_chunk + start, size);
3030 out_extras->data[size] = '\0';
3031
3032 i = cgltf_skip_json(tokens, i);
3033 return i;
3034 }
3035
3036 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)
3037 {
3038 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_STRING);
3039 CGLTF_CHECK_TOKTYPE(tokens[i+1], JSMN_OBJECT);
3040 if (out_extension->name)
3041 {
3042 return CGLTF_ERROR_JSON;
3043 }
3044
3045 cgltf_size name_length = tokens[i].end - tokens[i].start;
3046 out_extension->name = (char*)options->memory.alloc_func(options->memory.user_data, name_length + 1);
3047 if (!out_extension->name)
3048 {
3049 return CGLTF_ERROR_NOMEM;
3050 }
3051 strncpy(out_extension->name, (const char*)json_chunk + tokens[i].start, name_length);
3052 out_extension->name[name_length] = 0;
3053 i++;
3054
3055 size_t start = tokens[i].start;
3056 size_t size = tokens[i].end - start;
3057 out_extension->data = (char*)options->memory.alloc_func(options->memory.user_data, size + 1);
3058 if (!out_extension->data)
3059 {
3060 return CGLTF_ERROR_NOMEM;
3061 }
3062 strncpy(out_extension->data, (const char*)json_chunk + start, size);
3063 out_extension->data[size] = '\0';
3064
3065 i = cgltf_skip_json(tokens, i);
3066
3067 return i;
3068 }
3069
3070 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)
3071 {
3072 ++i;
3073
3074 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3075 if(*out_extensions)
3076 {
3077 return CGLTF_ERROR_JSON;
3078 }
3079
3080 int extensions_size = tokens[i].size;
3081 *out_extensions_count = 0;
3082 *out_extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
3083
3084 if (!*out_extensions)
3085 {
3086 return CGLTF_ERROR_NOMEM;
3087 }
3088
3089 ++i;
3090
3091 for (int j = 0; j < extensions_size; ++j)
3092 {
3093 CGLTF_CHECK_KEY(tokens[i]);
3094
3095 cgltf_size extension_index = (*out_extensions_count)++;
3096 cgltf_extension* extension = &((*out_extensions)[extension_index]);
3097 i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, extension);
3098
3099 if (i < 0)
3100 {
3101 return i;
3102 }
3103 }
3104 return i;
3105 }
3106
3107 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)
3108 {
3109 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3110
3111 int size = tokens[i].size;
3112 ++i;
3113
3114 for (int j = 0; j < size; ++j)
3115 {
3116 CGLTF_CHECK_KEY(tokens[i]);
3117
3118 if (cgltf_json_strcmp(tokens + i, json_chunk, "attributes") == 0)
3119 {
3120 i = cgltf_parse_json_attribute_list(options, tokens, i + 1, json_chunk, &out_draco_mesh_compression->attributes, &out_draco_mesh_compression->attributes_count);
3121 }
3122 else if (cgltf_json_strcmp(tokens + i, json_chunk, "bufferView") == 0)
3123 {
3124 ++i;
3125 out_draco_mesh_compression->buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
3126 ++i;
3127 }
3128 else
3129 {
3130 i = cgltf_skip_json(tokens, i+1);
3131 }
3132
3133 if (i < 0)
3134 {
3135 return i;
3136 }
3137 }
3138
3139 return i;
3140 }
3141
3142 static int cgltf_parse_json_mesh_gpu_instancing(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_mesh_gpu_instancing* out_mesh_gpu_instancing)
3143 {
3144 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3145
3146 int size = tokens[i].size;
3147 ++i;
3148
3149 for (int j = 0; j < size; ++j)
3150 {
3151 CGLTF_CHECK_KEY(tokens[i]);
3152
3153 if (cgltf_json_strcmp(tokens + i, json_chunk, "attributes") == 0)
3154 {
3155 i = cgltf_parse_json_attribute_list(options, tokens, i + 1, json_chunk, &out_mesh_gpu_instancing->attributes, &out_mesh_gpu_instancing->attributes_count);
3156 }
3157 else
3158 {
3159 i = cgltf_skip_json(tokens, i+1);
3160 }
3161
3162 if (i < 0)
3163 {
3164 return i;
3165 }
3166 }
3167
3168 return i;
3169 }
3170
3171 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)
3172 {
3173 (void)options;
3174 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_ARRAY);
3175
3176 int size = tokens[i].size;
3177 ++i;
3178
3179 for (int j = 0; j < size; ++j)
3180 {
3181 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3182
3183 int obj_size = tokens[i].size;
3184 ++i;
3185
3186 int material = -1;
3187 int variants_tok = -1;
3188 int extras_tok = -1;
3189
3190 for (int k = 0; k < obj_size; ++k)
3191 {
3192 CGLTF_CHECK_KEY(tokens[i]);
3193
3194 if (cgltf_json_strcmp(tokens + i, json_chunk, "material") == 0)
3195 {
3196 ++i;
3197 material = cgltf_json_to_int(tokens + i, json_chunk);
3198 ++i;
3199 }
3200 else if (cgltf_json_strcmp(tokens + i, json_chunk, "variants") == 0)
3201 {
3202 variants_tok = i+1;
3203 CGLTF_CHECK_TOKTYPE(tokens[variants_tok], JSMN_ARRAY);
3204
3205 i = cgltf_skip_json(tokens, i+1);
3206 }
3207 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3208 {
3209 extras_tok = i + 1;
3210 i = cgltf_skip_json(tokens, extras_tok);
3211 }
3212 else
3213 {
3214 i = cgltf_skip_json(tokens, i+1);
3215 }
3216
3217 if (i < 0)
3218 {
3219 return i;
3220 }
3221 }
3222
3223 if (material < 0 || variants_tok < 0)
3224 {
3225 return CGLTF_ERROR_JSON;
3226 }
3227
3228 if (out_mappings)
3229 {
3230 for (int k = 0; k < tokens[variants_tok].size; ++k)
3231 {
3232 int variant = cgltf_json_to_int(&tokens[variants_tok + 1 + k], json_chunk);
3233 if (variant < 0)
3234 return variant;
3235
3236 out_mappings[*offset].material = CGLTF_PTRINDEX(cgltf_material, material);
3237 out_mappings[*offset].variant = variant;
3238
3239 if (extras_tok >= 0)
3240 {
3241 int e = cgltf_parse_json_extras(options, tokens, extras_tok, json_chunk, &out_mappings[*offset].extras);
3242 if (e < 0)
3243 return e;
3244 }
3245
3246 (*offset)++;
3247 }
3248 }
3249 else
3250 {
3251 (*offset) += tokens[variants_tok].size;
3252 }
3253 }
3254
3255 return i;
3256 }
3257
3258 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)
3259 {
3260 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3261
3262 int size = tokens[i].size;
3263 ++i;
3264
3265 for (int j = 0; j < size; ++j)
3266 {
3267 CGLTF_CHECK_KEY(tokens[i]);
3268
3269 if (cgltf_json_strcmp(tokens + i, json_chunk, "mappings") == 0)
3270 {
3271 if (out_prim->mappings)
3272 {
3273 return CGLTF_ERROR_JSON;
3274 }
3275
3276 cgltf_size mappings_offset = 0;
3277 int k = cgltf_parse_json_material_mapping_data(options, tokens, i + 1, json_chunk, NULL, &mappings_offset);
3278 if (k < 0)
3279 {
3280 return k;
3281 }
3282
3283 out_prim->mappings_count = mappings_offset;
3284 out_prim->mappings = (cgltf_material_mapping*)cgltf_calloc(options, sizeof(cgltf_material_mapping), out_prim->mappings_count);
3285
3286 mappings_offset = 0;
3287 i = cgltf_parse_json_material_mapping_data(options, tokens, i + 1, json_chunk, out_prim->mappings, &mappings_offset);
3288 }
3289 else
3290 {
3291 i = cgltf_skip_json(tokens, i+1);
3292 }
3293
3294 if (i < 0)
3295 {
3296 return i;
3297 }
3298 }
3299
3300 return i;
3301 }
3302
3303 static cgltf_primitive_type cgltf_json_to_primitive_type(jsmntok_t const* tok, const uint8_t* json_chunk)
3304 {
3305 int type = cgltf_json_to_int(tok, json_chunk);
3306
3307 switch (type)
3308 {
3309 case 0:
3310 return cgltf_primitive_type_points;
3311 case 1:
3312 return cgltf_primitive_type_lines;
3313 case 2:
3314 return cgltf_primitive_type_line_loop;
3315 case 3:
3316 return cgltf_primitive_type_line_strip;
3317 case 4:
3318 return cgltf_primitive_type_triangles;
3319 case 5:
3320 return cgltf_primitive_type_triangle_strip;
3321 case 6:
3322 return cgltf_primitive_type_triangle_fan;
3323 default:
3324 return cgltf_primitive_type_invalid;
3325 }
3326 }
3327
3328 static int cgltf_parse_json_primitive(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_primitive* out_prim)
3329 {
3330 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3331
3332 out_prim->type = cgltf_primitive_type_triangles;
3333
3334 int size = tokens[i].size;
3335 ++i;
3336
3337 for (int j = 0; j < size; ++j)
3338 {
3339 CGLTF_CHECK_KEY(tokens[i]);
3340
3341 if (cgltf_json_strcmp(tokens+i, json_chunk, "mode") == 0)
3342 {
3343 ++i;
3344 out_prim->type = cgltf_json_to_primitive_type(tokens+i, json_chunk);
3345 ++i;
3346 }
3347 else if (cgltf_json_strcmp(tokens+i, json_chunk, "indices") == 0)
3348 {
3349 ++i;
3350 out_prim->indices = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
3351 ++i;
3352 }
3353 else if (cgltf_json_strcmp(tokens+i, json_chunk, "material") == 0)
3354 {
3355 ++i;
3356 out_prim->material = CGLTF_PTRINDEX(cgltf_material, cgltf_json_to_int(tokens + i, json_chunk));
3357 ++i;
3358 }
3359 else if (cgltf_json_strcmp(tokens+i, json_chunk, "attributes") == 0)
3360 {
3361 i = cgltf_parse_json_attribute_list(options, tokens, i + 1, json_chunk, &out_prim->attributes, &out_prim->attributes_count);
3362 }
3363 else if (cgltf_json_strcmp(tokens+i, json_chunk, "targets") == 0)
3364 {
3365 i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_morph_target), (void**)&out_prim->targets, &out_prim->targets_count);
3366 if (i < 0)
3367 {
3368 return i;
3369 }
3370
3371 for (cgltf_size k = 0; k < out_prim->targets_count; ++k)
3372 {
3373 i = cgltf_parse_json_attribute_list(options, tokens, i, json_chunk, &out_prim->targets[k].attributes, &out_prim->targets[k].attributes_count);
3374 if (i < 0)
3375 {
3376 return i;
3377 }
3378 }
3379 }
3380 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3381 {
3382 i = cgltf_parse_json_extras(options, tokens, i + 1, json_chunk, &out_prim->extras);
3383 }
3384 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
3385 {
3386 ++i;
3387
3388 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3389 if(out_prim->extensions)
3390 {
3391 return CGLTF_ERROR_JSON;
3392 }
3393
3394 int extensions_size = tokens[i].size;
3395 out_prim->extensions_count = 0;
3396 out_prim->extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
3397
3398 if (!out_prim->extensions)
3399 {
3400 return CGLTF_ERROR_NOMEM;
3401 }
3402
3403 ++i;
3404 for (int k = 0; k < extensions_size; ++k)
3405 {
3406 CGLTF_CHECK_KEY(tokens[i]);
3407
3408 if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_draco_mesh_compression") == 0)
3409 {
3410 out_prim->has_draco_mesh_compression = 1;
3411 i = cgltf_parse_json_draco_mesh_compression(options, tokens, i + 1, json_chunk, &out_prim->draco_mesh_compression);
3412 }
3413 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_variants") == 0)
3414 {
3415 i = cgltf_parse_json_material_mappings(options, tokens, i + 1, json_chunk, out_prim);
3416 }
3417 else
3418 {
3419 i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_prim->extensions[out_prim->extensions_count++]));
3420 }
3421
3422 if (i < 0)
3423 {
3424 return i;
3425 }
3426 }
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_mesh(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_mesh* out_mesh)
3443 {
3444 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3445
3446 int size = tokens[i].size;
3447 ++i;
3448
3449 for (int j = 0; j < size; ++j)
3450 {
3451 CGLTF_CHECK_KEY(tokens[i]);
3452
3453 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
3454 {
3455 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_mesh->name);
3456 }
3457 else if (cgltf_json_strcmp(tokens+i, json_chunk, "primitives") == 0)
3458 {
3459 i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_primitive), (void**)&out_mesh->primitives, &out_mesh->primitives_count);
3460 if (i < 0)
3461 {
3462 return i;
3463 }
3464
3465 for (cgltf_size prim_index = 0; prim_index < out_mesh->primitives_count; ++prim_index)
3466 {
3467 i = cgltf_parse_json_primitive(options, tokens, i, json_chunk, &out_mesh->primitives[prim_index]);
3468 if (i < 0)
3469 {
3470 return i;
3471 }
3472 }
3473 }
3474 else if (cgltf_json_strcmp(tokens + i, json_chunk, "weights") == 0)
3475 {
3476 i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_float), (void**)&out_mesh->weights, &out_mesh->weights_count);
3477 if (i < 0)
3478 {
3479 return i;
3480 }
3481
3482 i = cgltf_parse_json_float_array(tokens, i - 1, json_chunk, out_mesh->weights, (int)out_mesh->weights_count);
3483 }
3484 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3485 {
3486 ++i;
3487
3488 out_mesh->extras.start_offset = tokens[i].start;
3489 out_mesh->extras.end_offset = tokens[i].end;
3490
3491 if (tokens[i].type == JSMN_OBJECT)
3492 {
3493 int extras_size = tokens[i].size;
3494 ++i;
3495
3496 for (int k = 0; k < extras_size; ++k)
3497 {
3498 CGLTF_CHECK_KEY(tokens[i]);
3499
3500 if (cgltf_json_strcmp(tokens+i, json_chunk, "targetNames") == 0 && tokens[i+1].type == JSMN_ARRAY)
3501 {
3502 i = cgltf_parse_json_string_array(options, tokens, i + 1, json_chunk, &out_mesh->target_names, &out_mesh->target_names_count);
3503 }
3504 else
3505 {
3506 i = cgltf_skip_json(tokens, i+1);
3507 }
3508
3509 if (i < 0)
3510 {
3511 return i;
3512 }
3513 }
3514 }
3515 else
3516 {
3517 i = cgltf_skip_json(tokens, i);
3518 }
3519 }
3520 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
3521 {
3522 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_mesh->extensions_count, &out_mesh->extensions);
3523 }
3524 else
3525 {
3526 i = cgltf_skip_json(tokens, i+1);
3527 }
3528
3529 if (i < 0)
3530 {
3531 return i;
3532 }
3533 }
3534
3535 return i;
3536 }
3537
3538 static int cgltf_parse_json_meshes(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
3539 {
3540 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_mesh), (void**)&out_data->meshes, &out_data->meshes_count);
3541 if (i < 0)
3542 {
3543 return i;
3544 }
3545
3546 for (cgltf_size j = 0; j < out_data->meshes_count; ++j)
3547 {
3548 i = cgltf_parse_json_mesh(options, tokens, i, json_chunk, &out_data->meshes[j]);
3549 if (i < 0)
3550 {
3551 return i;
3552 }
3553 }
3554 return i;
3555 }
3556
3557 static cgltf_component_type cgltf_json_to_component_type(jsmntok_t const* tok, const uint8_t* json_chunk)
3558 {
3559 int type = cgltf_json_to_int(tok, json_chunk);
3560
3561 switch (type)
3562 {
3563 case 5120:
3564 return cgltf_component_type_r_8;
3565 case 5121:
3566 return cgltf_component_type_r_8u;
3567 case 5122:
3568 return cgltf_component_type_r_16;
3569 case 5123:
3570 return cgltf_component_type_r_16u;
3571 case 5125:
3572 return cgltf_component_type_r_32u;
3573 case 5126:
3574 return cgltf_component_type_r_32f;
3575 default:
3576 return cgltf_component_type_invalid;
3577 }
3578 }
3579
3580 static int cgltf_parse_json_accessor_sparse(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_accessor_sparse* out_sparse)
3581 {
3582 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3583
3584 int size = tokens[i].size;
3585 ++i;
3586
3587 for (int j = 0; j < size; ++j)
3588 {
3589 CGLTF_CHECK_KEY(tokens[i]);
3590
3591 if (cgltf_json_strcmp(tokens+i, json_chunk, "count") == 0)
3592 {
3593 ++i;
3594 out_sparse->count = cgltf_json_to_size(tokens + i, json_chunk);
3595 ++i;
3596 }
3597 else if (cgltf_json_strcmp(tokens+i, json_chunk, "indices") == 0)
3598 {
3599 ++i;
3600 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3601
3602 int indices_size = tokens[i].size;
3603 ++i;
3604
3605 for (int k = 0; k < indices_size; ++k)
3606 {
3607 CGLTF_CHECK_KEY(tokens[i]);
3608
3609 if (cgltf_json_strcmp(tokens+i, json_chunk, "bufferView") == 0)
3610 {
3611 ++i;
3612 out_sparse->indices_buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
3613 ++i;
3614 }
3615 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
3616 {
3617 ++i;
3618 out_sparse->indices_byte_offset = cgltf_json_to_size(tokens + i, json_chunk);
3619 ++i;
3620 }
3621 else if (cgltf_json_strcmp(tokens+i, json_chunk, "componentType") == 0)
3622 {
3623 ++i;
3624 out_sparse->indices_component_type = cgltf_json_to_component_type(tokens + i, json_chunk);
3625 ++i;
3626 }
3627 else
3628 {
3629 i = cgltf_skip_json(tokens, i+1);
3630 }
3631
3632 if (i < 0)
3633 {
3634 return i;
3635 }
3636 }
3637 }
3638 else if (cgltf_json_strcmp(tokens+i, json_chunk, "values") == 0)
3639 {
3640 ++i;
3641 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3642
3643 int values_size = tokens[i].size;
3644 ++i;
3645
3646 for (int k = 0; k < values_size; ++k)
3647 {
3648 CGLTF_CHECK_KEY(tokens[i]);
3649
3650 if (cgltf_json_strcmp(tokens+i, json_chunk, "bufferView") == 0)
3651 {
3652 ++i;
3653 out_sparse->values_buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
3654 ++i;
3655 }
3656 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
3657 {
3658 ++i;
3659 out_sparse->values_byte_offset = cgltf_json_to_size(tokens + i, json_chunk);
3660 ++i;
3661 }
3662 else
3663 {
3664 i = cgltf_skip_json(tokens, i+1);
3665 }
3666
3667 if (i < 0)
3668 {
3669 return i;
3670 }
3671 }
3672 }
3673 else
3674 {
3675 i = cgltf_skip_json(tokens, i+1);
3676 }
3677
3678 if (i < 0)
3679 {
3680 return i;
3681 }
3682 }
3683
3684 return i;
3685 }
3686
3687 static int cgltf_parse_json_accessor(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_accessor* out_accessor)
3688 {
3689 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3690
3691 int size = tokens[i].size;
3692 ++i;
3693
3694 for (int j = 0; j < size; ++j)
3695 {
3696 CGLTF_CHECK_KEY(tokens[i]);
3697
3698 if (cgltf_json_strcmp(tokens + i, json_chunk, "name") == 0)
3699 {
3700 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_accessor->name);
3701 }
3702 else if (cgltf_json_strcmp(tokens+i, json_chunk, "bufferView") == 0)
3703 {
3704 ++i;
3705 out_accessor->buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
3706 ++i;
3707 }
3708 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
3709 {
3710 ++i;
3711 out_accessor->offset =
3712 cgltf_json_to_size(tokens+i, json_chunk);
3713 ++i;
3714 }
3715 else if (cgltf_json_strcmp(tokens+i, json_chunk, "componentType") == 0)
3716 {
3717 ++i;
3718 out_accessor->component_type = cgltf_json_to_component_type(tokens + i, json_chunk);
3719 ++i;
3720 }
3721 else if (cgltf_json_strcmp(tokens+i, json_chunk, "normalized") == 0)
3722 {
3723 ++i;
3724 out_accessor->normalized = cgltf_json_to_bool(tokens+i, json_chunk);
3725 ++i;
3726 }
3727 else if (cgltf_json_strcmp(tokens+i, json_chunk, "count") == 0)
3728 {
3729 ++i;
3730 out_accessor->count = cgltf_json_to_size(tokens+i, json_chunk);
3731 ++i;
3732 }
3733 else if (cgltf_json_strcmp(tokens+i, json_chunk, "type") == 0)
3734 {
3735 ++i;
3736 if (cgltf_json_strcmp(tokens+i, json_chunk, "SCALAR") == 0)
3737 {
3738 out_accessor->type = cgltf_type_scalar;
3739 }
3740 else if (cgltf_json_strcmp(tokens+i, json_chunk, "VEC2") == 0)
3741 {
3742 out_accessor->type = cgltf_type_vec2;
3743 }
3744 else if (cgltf_json_strcmp(tokens+i, json_chunk, "VEC3") == 0)
3745 {
3746 out_accessor->type = cgltf_type_vec3;
3747 }
3748 else if (cgltf_json_strcmp(tokens+i, json_chunk, "VEC4") == 0)
3749 {
3750 out_accessor->type = cgltf_type_vec4;
3751 }
3752 else if (cgltf_json_strcmp(tokens+i, json_chunk, "MAT2") == 0)
3753 {
3754 out_accessor->type = cgltf_type_mat2;
3755 }
3756 else if (cgltf_json_strcmp(tokens+i, json_chunk, "MAT3") == 0)
3757 {
3758 out_accessor->type = cgltf_type_mat3;
3759 }
3760 else if (cgltf_json_strcmp(tokens+i, json_chunk, "MAT4") == 0)
3761 {
3762 out_accessor->type = cgltf_type_mat4;
3763 }
3764 ++i;
3765 }
3766 else if (cgltf_json_strcmp(tokens + i, json_chunk, "min") == 0)
3767 {
3768 ++i;
3769 out_accessor->has_min = 1;
3770 // note: we can't parse the precise number of elements since type may not have been computed yet
3771 int min_size = tokens[i].size > 16 ? 16 : tokens[i].size;
3772 i = cgltf_parse_json_float_array(tokens, i, json_chunk, out_accessor->min, min_size);
3773 }
3774 else if (cgltf_json_strcmp(tokens + i, json_chunk, "max") == 0)
3775 {
3776 ++i;
3777 out_accessor->has_max = 1;
3778 // note: we can't parse the precise number of elements since type may not have been computed yet
3779 int max_size = tokens[i].size > 16 ? 16 : tokens[i].size;
3780 i = cgltf_parse_json_float_array(tokens, i, json_chunk, out_accessor->max, max_size);
3781 }
3782 else if (cgltf_json_strcmp(tokens + i, json_chunk, "sparse") == 0)
3783 {
3784 out_accessor->is_sparse = 1;
3785 i = cgltf_parse_json_accessor_sparse(tokens, i + 1, json_chunk, &out_accessor->sparse);
3786 }
3787 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
3788 {
3789 i = cgltf_parse_json_extras(options, tokens, i + 1, json_chunk, &out_accessor->extras);
3790 }
3791 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
3792 {
3793 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_accessor->extensions_count, &out_accessor->extensions);
3794 }
3795 else
3796 {
3797 i = cgltf_skip_json(tokens, i+1);
3798 }
3799
3800 if (i < 0)
3801 {
3802 return i;
3803 }
3804 }
3805
3806 return i;
3807 }
3808
3809 static int cgltf_parse_json_texture_transform(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_texture_transform* out_texture_transform)
3810 {
3811 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3812
3813 int size = tokens[i].size;
3814 ++i;
3815
3816 for (int j = 0; j < size; ++j)
3817 {
3818 CGLTF_CHECK_KEY(tokens[i]);
3819
3820 if (cgltf_json_strcmp(tokens + i, json_chunk, "offset") == 0)
3821 {
3822 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_texture_transform->offset, 2);
3823 }
3824 else if (cgltf_json_strcmp(tokens + i, json_chunk, "rotation") == 0)
3825 {
3826 ++i;
3827 out_texture_transform->rotation = cgltf_json_to_float(tokens + i, json_chunk);
3828 ++i;
3829 }
3830 else if (cgltf_json_strcmp(tokens + i, json_chunk, "scale") == 0)
3831 {
3832 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_texture_transform->scale, 2);
3833 }
3834 else if (cgltf_json_strcmp(tokens + i, json_chunk, "texCoord") == 0)
3835 {
3836 ++i;
3837 out_texture_transform->has_texcoord = 1;
3838 out_texture_transform->texcoord = cgltf_json_to_int(tokens + i, json_chunk);
3839 ++i;
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_view(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_texture_view* out_texture_view)
3856 {
3857 (void)options;
3858
3859 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3860
3861 out_texture_view->scale = 1.0f;
3862 cgltf_fill_float_array(out_texture_view->transform.scale, 2, 1.0f);
3863
3864 int size = tokens[i].size;
3865 ++i;
3866
3867 for (int j = 0; j < size; ++j)
3868 {
3869 CGLTF_CHECK_KEY(tokens[i]);
3870
3871 if (cgltf_json_strcmp(tokens + i, json_chunk, "index") == 0)
3872 {
3873 ++i;
3874 out_texture_view->texture = CGLTF_PTRINDEX(cgltf_texture, cgltf_json_to_int(tokens + i, json_chunk));
3875 ++i;
3876 }
3877 else if (cgltf_json_strcmp(tokens + i, json_chunk, "texCoord") == 0)
3878 {
3879 ++i;
3880 out_texture_view->texcoord = cgltf_json_to_int(tokens + i, json_chunk);
3881 ++i;
3882 }
3883 else if (cgltf_json_strcmp(tokens + i, json_chunk, "scale") == 0)
3884 {
3885 ++i;
3886 out_texture_view->scale = cgltf_json_to_float(tokens + i, json_chunk);
3887 ++i;
3888 }
3889 else if (cgltf_json_strcmp(tokens + i, json_chunk, "strength") == 0)
3890 {
3891 ++i;
3892 out_texture_view->scale = cgltf_json_to_float(tokens + i, json_chunk);
3893 ++i;
3894 }
3895 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
3896 {
3897 ++i;
3898
3899 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3900 int extensions_size = tokens[i].size;
3901
3902 ++i;
3903
3904 for (int k = 0; k < extensions_size; ++k)
3905 {
3906 CGLTF_CHECK_KEY(tokens[i]);
3907
3908 if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_texture_transform") == 0)
3909 {
3910 out_texture_view->has_transform = 1;
3911 i = cgltf_parse_json_texture_transform(tokens, i + 1, json_chunk, &out_texture_view->transform);
3912 }
3913 else
3914 {
3915 i = cgltf_skip_json(tokens, i + 1);
3916 }
3917
3918 if (i < 0)
3919 {
3920 return i;
3921 }
3922 }
3923 }
3924 else
3925 {
3926 i = cgltf_skip_json(tokens, i + 1);
3927 }
3928
3929 if (i < 0)
3930 {
3931 return i;
3932 }
3933 }
3934
3935 return i;
3936 }
3937
3938 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)
3939 {
3940 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3941
3942 int size = tokens[i].size;
3943 ++i;
3944
3945 for (int j = 0; j < size; ++j)
3946 {
3947 CGLTF_CHECK_KEY(tokens[i]);
3948
3949 if (cgltf_json_strcmp(tokens+i, json_chunk, "metallicFactor") == 0)
3950 {
3951 ++i;
3952 out_pbr->metallic_factor =
3953 cgltf_json_to_float(tokens + i, json_chunk);
3954 ++i;
3955 }
3956 else if (cgltf_json_strcmp(tokens+i, json_chunk, "roughnessFactor") == 0)
3957 {
3958 ++i;
3959 out_pbr->roughness_factor =
3960 cgltf_json_to_float(tokens+i, json_chunk);
3961 ++i;
3962 }
3963 else if (cgltf_json_strcmp(tokens+i, json_chunk, "baseColorFactor") == 0)
3964 {
3965 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_pbr->base_color_factor, 4);
3966 }
3967 else if (cgltf_json_strcmp(tokens+i, json_chunk, "baseColorTexture") == 0)
3968 {
3969 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_pbr->base_color_texture);
3970 }
3971 else if (cgltf_json_strcmp(tokens + i, json_chunk, "metallicRoughnessTexture") == 0)
3972 {
3973 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_pbr->metallic_roughness_texture);
3974 }
3975 else
3976 {
3977 i = cgltf_skip_json(tokens, i+1);
3978 }
3979
3980 if (i < 0)
3981 {
3982 return i;
3983 }
3984 }
3985
3986 return i;
3987 }
3988
3989 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)
3990 {
3991 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
3992 int size = tokens[i].size;
3993 ++i;
3994
3995 for (int j = 0; j < size; ++j)
3996 {
3997 CGLTF_CHECK_KEY(tokens[i]);
3998
3999 if (cgltf_json_strcmp(tokens+i, json_chunk, "diffuseFactor") == 0)
4000 {
4001 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_pbr->diffuse_factor, 4);
4002 }
4003 else if (cgltf_json_strcmp(tokens+i, json_chunk, "specularFactor") == 0)
4004 {
4005 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_pbr->specular_factor, 3);
4006 }
4007 else if (cgltf_json_strcmp(tokens+i, json_chunk, "glossinessFactor") == 0)
4008 {
4009 ++i;
4010 out_pbr->glossiness_factor = cgltf_json_to_float(tokens + i, json_chunk);
4011 ++i;
4012 }
4013 else if (cgltf_json_strcmp(tokens+i, json_chunk, "diffuseTexture") == 0)
4014 {
4015 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_pbr->diffuse_texture);
4016 }
4017 else if (cgltf_json_strcmp(tokens+i, json_chunk, "specularGlossinessTexture") == 0)
4018 {
4019 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_pbr->specular_glossiness_texture);
4020 }
4021 else
4022 {
4023 i = cgltf_skip_json(tokens, i+1);
4024 }
4025
4026 if (i < 0)
4027 {
4028 return i;
4029 }
4030 }
4031
4032 return i;
4033 }
4034
4035 static int cgltf_parse_json_clearcoat(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_clearcoat* out_clearcoat)
4036 {
4037 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4038 int size = tokens[i].size;
4039 ++i;
4040
4041 for (int j = 0; j < size; ++j)
4042 {
4043 CGLTF_CHECK_KEY(tokens[i]);
4044
4045 if (cgltf_json_strcmp(tokens+i, json_chunk, "clearcoatFactor") == 0)
4046 {
4047 ++i;
4048 out_clearcoat->clearcoat_factor = cgltf_json_to_float(tokens + i, json_chunk);
4049 ++i;
4050 }
4051 else if (cgltf_json_strcmp(tokens+i, json_chunk, "clearcoatRoughnessFactor") == 0)
4052 {
4053 ++i;
4054 out_clearcoat->clearcoat_roughness_factor = cgltf_json_to_float(tokens + i, json_chunk);
4055 ++i;
4056 }
4057 else if (cgltf_json_strcmp(tokens+i, json_chunk, "clearcoatTexture") == 0)
4058 {
4059 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_clearcoat->clearcoat_texture);
4060 }
4061 else if (cgltf_json_strcmp(tokens+i, json_chunk, "clearcoatRoughnessTexture") == 0)
4062 {
4063 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_clearcoat->clearcoat_roughness_texture);
4064 }
4065 else if (cgltf_json_strcmp(tokens+i, json_chunk, "clearcoatNormalTexture") == 0)
4066 {
4067 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_clearcoat->clearcoat_normal_texture);
4068 }
4069 else
4070 {
4071 i = cgltf_skip_json(tokens, i+1);
4072 }
4073
4074 if (i < 0)
4075 {
4076 return i;
4077 }
4078 }
4079
4080 return i;
4081 }
4082
4083 static int cgltf_parse_json_ior(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_ior* out_ior)
4084 {
4085 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4086 int size = tokens[i].size;
4087 ++i;
4088
4089 // Default values
4090 out_ior->ior = 1.5f;
4091
4092 for (int j = 0; j < size; ++j)
4093 {
4094 CGLTF_CHECK_KEY(tokens[i]);
4095
4096 if (cgltf_json_strcmp(tokens+i, json_chunk, "ior") == 0)
4097 {
4098 ++i;
4099 out_ior->ior = cgltf_json_to_float(tokens + i, json_chunk);
4100 ++i;
4101 }
4102 else
4103 {
4104 i = cgltf_skip_json(tokens, i+1);
4105 }
4106
4107 if (i < 0)
4108 {
4109 return i;
4110 }
4111 }
4112
4113 return i;
4114 }
4115
4116 static int cgltf_parse_json_specular(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_specular* out_specular)
4117 {
4118 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4119 int size = tokens[i].size;
4120 ++i;
4121
4122 // Default values
4123 out_specular->specular_factor = 1.0f;
4124 cgltf_fill_float_array(out_specular->specular_color_factor, 3, 1.0f);
4125
4126 for (int j = 0; j < size; ++j)
4127 {
4128 CGLTF_CHECK_KEY(tokens[i]);
4129
4130 if (cgltf_json_strcmp(tokens+i, json_chunk, "specularFactor") == 0)
4131 {
4132 ++i;
4133 out_specular->specular_factor = cgltf_json_to_float(tokens + i, json_chunk);
4134 ++i;
4135 }
4136 else if (cgltf_json_strcmp(tokens+i, json_chunk, "specularColorFactor") == 0)
4137 {
4138 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_specular->specular_color_factor, 3);
4139 }
4140 else if (cgltf_json_strcmp(tokens+i, json_chunk, "specularTexture") == 0)
4141 {
4142 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_specular->specular_texture);
4143 }
4144 else if (cgltf_json_strcmp(tokens + i, json_chunk, "specularColorTexture") == 0)
4145 {
4146 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_specular->specular_color_texture);
4147 }
4148 else
4149 {
4150 i = cgltf_skip_json(tokens, i+1);
4151 }
4152
4153 if (i < 0)
4154 {
4155 return i;
4156 }
4157 }
4158
4159 return i;
4160 }
4161
4162 static int cgltf_parse_json_transmission(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_transmission* out_transmission)
4163 {
4164 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4165 int size = tokens[i].size;
4166 ++i;
4167
4168 for (int j = 0; j < size; ++j)
4169 {
4170 CGLTF_CHECK_KEY(tokens[i]);
4171
4172 if (cgltf_json_strcmp(tokens+i, json_chunk, "transmissionFactor") == 0)
4173 {
4174 ++i;
4175 out_transmission->transmission_factor = cgltf_json_to_float(tokens + i, json_chunk);
4176 ++i;
4177 }
4178 else if (cgltf_json_strcmp(tokens+i, json_chunk, "transmissionTexture") == 0)
4179 {
4180 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_transmission->transmission_texture);
4181 }
4182 else
4183 {
4184 i = cgltf_skip_json(tokens, i+1);
4185 }
4186
4187 if (i < 0)
4188 {
4189 return i;
4190 }
4191 }
4192
4193 return i;
4194 }
4195
4196 static int cgltf_parse_json_volume(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_volume* out_volume)
4197 {
4198 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4199 int size = tokens[i].size;
4200 ++i;
4201
4202 for (int j = 0; j < size; ++j)
4203 {
4204 CGLTF_CHECK_KEY(tokens[i]);
4205
4206 if (cgltf_json_strcmp(tokens + i, json_chunk, "thicknessFactor") == 0)
4207 {
4208 ++i;
4209 out_volume->thickness_factor = cgltf_json_to_float(tokens + i, json_chunk);
4210 ++i;
4211 }
4212 else if (cgltf_json_strcmp(tokens + i, json_chunk, "thicknessTexture") == 0)
4213 {
4214 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_volume->thickness_texture);
4215 }
4216 else if (cgltf_json_strcmp(tokens + i, json_chunk, "attenuationColor") == 0)
4217 {
4218 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_volume->attenuation_color, 3);
4219 }
4220 else if (cgltf_json_strcmp(tokens + i, json_chunk, "attenuationDistance") == 0)
4221 {
4222 ++i;
4223 out_volume->attenuation_distance = cgltf_json_to_float(tokens + i, json_chunk);
4224 ++i;
4225 }
4226 else
4227 {
4228 i = cgltf_skip_json(tokens, i + 1);
4229 }
4230
4231 if (i < 0)
4232 {
4233 return i;
4234 }
4235 }
4236
4237 return i;
4238 }
4239
4240 static int cgltf_parse_json_sheen(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_sheen* out_sheen)
4241 {
4242 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4243 int size = tokens[i].size;
4244 ++i;
4245
4246 for (int j = 0; j < size; ++j)
4247 {
4248 CGLTF_CHECK_KEY(tokens[i]);
4249
4250 if (cgltf_json_strcmp(tokens+i, json_chunk, "sheenColorFactor") == 0)
4251 {
4252 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_sheen->sheen_color_factor, 3);
4253 }
4254 else if (cgltf_json_strcmp(tokens+i, json_chunk, "sheenColorTexture") == 0)
4255 {
4256 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_sheen->sheen_color_texture);
4257 }
4258 else if (cgltf_json_strcmp(tokens+i, json_chunk, "sheenRoughnessFactor") == 0)
4259 {
4260 ++i;
4261 out_sheen->sheen_roughness_factor = cgltf_json_to_float(tokens + i, json_chunk);
4262 ++i;
4263 }
4264 else if (cgltf_json_strcmp(tokens+i, json_chunk, "sheenRoughnessTexture") == 0)
4265 {
4266 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_sheen->sheen_roughness_texture);
4267 }
4268 else
4269 {
4270 i = cgltf_skip_json(tokens, i+1);
4271 }
4272
4273 if (i < 0)
4274 {
4275 return i;
4276 }
4277 }
4278
4279 return i;
4280 }
4281
4282 static int cgltf_parse_json_emissive_strength(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_emissive_strength* out_emissive_strength)
4283 {
4284 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4285 int size = tokens[i].size;
4286 ++i;
4287
4288 // Default
4289 out_emissive_strength->emissive_strength = 1.f;
4290
4291 for (int j = 0; j < size; ++j)
4292 {
4293 CGLTF_CHECK_KEY(tokens[i]);
4294
4295 if (cgltf_json_strcmp(tokens + i, json_chunk, "emissiveStrength") == 0)
4296 {
4297 ++i;
4298 out_emissive_strength->emissive_strength = cgltf_json_to_float(tokens + i, json_chunk);
4299 ++i;
4300 }
4301 else
4302 {
4303 i = cgltf_skip_json(tokens, i + 1);
4304 }
4305
4306 if (i < 0)
4307 {
4308 return i;
4309 }
4310 }
4311
4312 return i;
4313 }
4314
4315 static int cgltf_parse_json_iridescence(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_iridescence* out_iridescence)
4316 {
4317 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4318 int size = tokens[i].size;
4319 ++i;
4320
4321 // Default
4322 out_iridescence->iridescence_ior = 1.3f;
4323 out_iridescence->iridescence_thickness_min = 100.f;
4324 out_iridescence->iridescence_thickness_max = 400.f;
4325
4326 for (int j = 0; j < size; ++j)
4327 {
4328 CGLTF_CHECK_KEY(tokens[i]);
4329
4330 if (cgltf_json_strcmp(tokens + i, json_chunk, "iridescenceFactor") == 0)
4331 {
4332 ++i;
4333 out_iridescence->iridescence_factor = cgltf_json_to_float(tokens + i, json_chunk);
4334 ++i;
4335 }
4336 else if (cgltf_json_strcmp(tokens + i, json_chunk, "iridescenceTexture") == 0)
4337 {
4338 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_iridescence->iridescence_texture);
4339 }
4340 else if (cgltf_json_strcmp(tokens + i, json_chunk, "iridescenceIor") == 0)
4341 {
4342 ++i;
4343 out_iridescence->iridescence_ior = cgltf_json_to_float(tokens + i, json_chunk);
4344 ++i;
4345 }
4346 else if (cgltf_json_strcmp(tokens + i, json_chunk, "iridescenceThicknessMinimum") == 0)
4347 {
4348 ++i;
4349 out_iridescence->iridescence_thickness_min = cgltf_json_to_float(tokens + i, json_chunk);
4350 ++i;
4351 }
4352 else if (cgltf_json_strcmp(tokens + i, json_chunk, "iridescenceThicknessMaximum") == 0)
4353 {
4354 ++i;
4355 out_iridescence->iridescence_thickness_max = cgltf_json_to_float(tokens + i, json_chunk);
4356 ++i;
4357 }
4358 else if (cgltf_json_strcmp(tokens + i, json_chunk, "iridescenceThicknessTexture") == 0)
4359 {
4360 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_iridescence->iridescence_thickness_texture);
4361 }
4362 else
4363 {
4364 i = cgltf_skip_json(tokens, i + 1);
4365 }
4366
4367 if (i < 0)
4368 {
4369 return i;
4370 }
4371 }
4372
4373 return i;
4374 }
4375
4376 static int cgltf_parse_json_diffuse_transmission(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_diffuse_transmission* out_diff_transmission)
4377 {
4378 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4379 int size = tokens[i].size;
4380 ++i;
4381
4382 // Defaults
4383 cgltf_fill_float_array(out_diff_transmission->diffuse_transmission_color_factor, 3, 1.0f);
4384 out_diff_transmission->diffuse_transmission_factor = 0.f;
4385
4386 for (int j = 0; j < size; ++j)
4387 {
4388 CGLTF_CHECK_KEY(tokens[i]);
4389
4390 if (cgltf_json_strcmp(tokens + i, json_chunk, "diffuseTransmissionFactor") == 0)
4391 {
4392 ++i;
4393 out_diff_transmission->diffuse_transmission_factor = cgltf_json_to_float(tokens + i, json_chunk);
4394 ++i;
4395 }
4396 else if (cgltf_json_strcmp(tokens + i, json_chunk, "diffuseTransmissionTexture") == 0)
4397 {
4398 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_diff_transmission->diffuse_transmission_texture);
4399 }
4400 else if (cgltf_json_strcmp(tokens + i, json_chunk, "diffuseTransmissionColorFactor") == 0)
4401 {
4402 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_diff_transmission->diffuse_transmission_color_factor, 3);
4403 }
4404 else if (cgltf_json_strcmp(tokens + i, json_chunk, "diffuseTransmissionColorTexture") == 0)
4405 {
4406 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_diff_transmission->diffuse_transmission_color_texture);
4407 }
4408 else
4409 {
4410 i = cgltf_skip_json(tokens, i + 1);
4411 }
4412
4413 if (i < 0)
4414 {
4415 return i;
4416 }
4417 }
4418
4419 return i;
4420 }
4421
4422 static int cgltf_parse_json_anisotropy(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_anisotropy* out_anisotropy)
4423 {
4424 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4425 int size = tokens[i].size;
4426 ++i;
4427
4428
4429 for (int j = 0; j < size; ++j)
4430 {
4431 CGLTF_CHECK_KEY(tokens[i]);
4432
4433 if (cgltf_json_strcmp(tokens + i, json_chunk, "anisotropyStrength") == 0)
4434 {
4435 ++i;
4436 out_anisotropy->anisotropy_strength = cgltf_json_to_float(tokens + i, json_chunk);
4437 ++i;
4438 }
4439 else if (cgltf_json_strcmp(tokens + i, json_chunk, "anisotropyRotation") == 0)
4440 {
4441 ++i;
4442 out_anisotropy->anisotropy_rotation = cgltf_json_to_float(tokens + i, json_chunk);
4443 ++i;
4444 }
4445 else if (cgltf_json_strcmp(tokens + i, json_chunk, "anisotropyTexture") == 0)
4446 {
4447 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk, &out_anisotropy->anisotropy_texture);
4448 }
4449 else
4450 {
4451 i = cgltf_skip_json(tokens, i + 1);
4452 }
4453
4454 if (i < 0)
4455 {
4456 return i;
4457 }
4458 }
4459
4460 return i;
4461 }
4462
4463 static int cgltf_parse_json_dispersion(jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_dispersion* out_dispersion)
4464 {
4465 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4466 int size = tokens[i].size;
4467 ++i;
4468
4469
4470 for (int j = 0; j < size; ++j)
4471 {
4472 CGLTF_CHECK_KEY(tokens[i]);
4473
4474 if (cgltf_json_strcmp(tokens + i, json_chunk, "dispersion") == 0)
4475 {
4476 ++i;
4477 out_dispersion->dispersion = cgltf_json_to_float(tokens + i, json_chunk);
4478 ++i;
4479 }
4480 else
4481 {
4482 i = cgltf_skip_json(tokens, i + 1);
4483 }
4484
4485 if (i < 0)
4486 {
4487 return i;
4488 }
4489 }
4490
4491 return i;
4492 }
4493
4494 static int cgltf_parse_json_image(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_image* out_image)
4495 {
4496 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4497
4498 int size = tokens[i].size;
4499 ++i;
4500
4501 for (int j = 0; j < size; ++j)
4502 {
4503 CGLTF_CHECK_KEY(tokens[i]);
4504
4505 if (cgltf_json_strcmp(tokens + i, json_chunk, "uri") == 0)
4506 {
4507 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_image->uri);
4508 }
4509 else if (cgltf_json_strcmp(tokens+i, json_chunk, "bufferView") == 0)
4510 {
4511 ++i;
4512 out_image->buffer_view = CGLTF_PTRINDEX(cgltf_buffer_view, cgltf_json_to_int(tokens + i, json_chunk));
4513 ++i;
4514 }
4515 else if (cgltf_json_strcmp(tokens + i, json_chunk, "mimeType") == 0)
4516 {
4517 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_image->mime_type);
4518 }
4519 else if (cgltf_json_strcmp(tokens + i, json_chunk, "name") == 0)
4520 {
4521 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_image->name);
4522 }
4523 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
4524 {
4525 i = cgltf_parse_json_extras(options, tokens, i + 1, json_chunk, &out_image->extras);
4526 }
4527 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
4528 {
4529 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_image->extensions_count, &out_image->extensions);
4530 }
4531 else
4532 {
4533 i = cgltf_skip_json(tokens, i + 1);
4534 }
4535
4536 if (i < 0)
4537 {
4538 return i;
4539 }
4540 }
4541
4542 return i;
4543 }
4544
4545 static int cgltf_parse_json_sampler(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_sampler* out_sampler)
4546 {
4547 (void)options;
4548 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4549
4550 out_sampler->wrap_s = cgltf_wrap_mode_repeat;
4551 out_sampler->wrap_t = cgltf_wrap_mode_repeat;
4552
4553 int size = tokens[i].size;
4554 ++i;
4555
4556 for (int j = 0; j < size; ++j)
4557 {
4558 CGLTF_CHECK_KEY(tokens[i]);
4559
4560 if (cgltf_json_strcmp(tokens + i, json_chunk, "name") == 0)
4561 {
4562 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_sampler->name);
4563 }
4564 else if (cgltf_json_strcmp(tokens + i, json_chunk, "magFilter") == 0)
4565 {
4566 ++i;
4567 out_sampler->mag_filter
4568 = (cgltf_filter_type)cgltf_json_to_int(tokens + i, json_chunk);
4569 ++i;
4570 }
4571 else if (cgltf_json_strcmp(tokens + i, json_chunk, "minFilter") == 0)
4572 {
4573 ++i;
4574 out_sampler->min_filter
4575 = (cgltf_filter_type)cgltf_json_to_int(tokens + i, json_chunk);
4576 ++i;
4577 }
4578 else if (cgltf_json_strcmp(tokens + i, json_chunk, "wrapS") == 0)
4579 {
4580 ++i;
4581 out_sampler->wrap_s
4582 = (cgltf_wrap_mode)cgltf_json_to_int(tokens + i, json_chunk);
4583 ++i;
4584 }
4585 else if (cgltf_json_strcmp(tokens + i, json_chunk, "wrapT") == 0)
4586 {
4587 ++i;
4588 out_sampler->wrap_t
4589 = (cgltf_wrap_mode)cgltf_json_to_int(tokens + i, json_chunk);
4590 ++i;
4591 }
4592 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
4593 {
4594 i = cgltf_parse_json_extras(options, tokens, i + 1, json_chunk, &out_sampler->extras);
4595 }
4596 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
4597 {
4598 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_sampler->extensions_count, &out_sampler->extensions);
4599 }
4600 else
4601 {
4602 i = cgltf_skip_json(tokens, i + 1);
4603 }
4604
4605 if (i < 0)
4606 {
4607 return i;
4608 }
4609 }
4610
4611 return i;
4612 }
4613
4614 static int cgltf_parse_json_texture(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_texture* out_texture)
4615 {
4616 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4617
4618 int size = tokens[i].size;
4619 ++i;
4620
4621 for (int j = 0; j < size; ++j)
4622 {
4623 CGLTF_CHECK_KEY(tokens[i]);
4624
4625 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
4626 {
4627 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_texture->name);
4628 }
4629 else if (cgltf_json_strcmp(tokens + i, json_chunk, "sampler") == 0)
4630 {
4631 ++i;
4632 out_texture->sampler = CGLTF_PTRINDEX(cgltf_sampler, cgltf_json_to_int(tokens + i, json_chunk));
4633 ++i;
4634 }
4635 else if (cgltf_json_strcmp(tokens + i, json_chunk, "source") == 0)
4636 {
4637 ++i;
4638 out_texture->image = CGLTF_PTRINDEX(cgltf_image, cgltf_json_to_int(tokens + i, json_chunk));
4639 ++i;
4640 }
4641 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
4642 {
4643 i = cgltf_parse_json_extras(options, tokens, i + 1, json_chunk, &out_texture->extras);
4644 }
4645 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
4646 {
4647 ++i;
4648
4649 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4650 if (out_texture->extensions)
4651 {
4652 return CGLTF_ERROR_JSON;
4653 }
4654
4655 int extensions_size = tokens[i].size;
4656 ++i;
4657 out_texture->extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
4658 out_texture->extensions_count = 0;
4659
4660 if (!out_texture->extensions)
4661 {
4662 return CGLTF_ERROR_NOMEM;
4663 }
4664
4665 for (int k = 0; k < extensions_size; ++k)
4666 {
4667 CGLTF_CHECK_KEY(tokens[i]);
4668
4669 if (cgltf_json_strcmp(tokens + i, json_chunk, "KHR_texture_basisu") == 0)
4670 {
4671 out_texture->has_basisu = 1;
4672 ++i;
4673 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4674 int num_properties = tokens[i].size;
4675 ++i;
4676
4677 for (int t = 0; t < num_properties; ++t)
4678 {
4679 CGLTF_CHECK_KEY(tokens[i]);
4680
4681 if (cgltf_json_strcmp(tokens + i, json_chunk, "source") == 0)
4682 {
4683 ++i;
4684 out_texture->basisu_image = CGLTF_PTRINDEX(cgltf_image, cgltf_json_to_int(tokens + i, json_chunk));
4685 ++i;
4686 }
4687 else
4688 {
4689 i = cgltf_skip_json(tokens, i + 1);
4690 }
4691 if (i < 0)
4692 {
4693 return i;
4694 }
4695 }
4696 }
4697 else if (cgltf_json_strcmp(tokens + i, json_chunk, "EXT_texture_webp") == 0)
4698 {
4699 out_texture->has_webp = 1;
4700 ++i;
4701 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4702 int num_properties = tokens[i].size;
4703 ++i;
4704
4705 for (int t = 0; t < num_properties; ++t)
4706 {
4707 CGLTF_CHECK_KEY(tokens[i]);
4708
4709 if (cgltf_json_strcmp(tokens + i, json_chunk, "source") == 0)
4710 {
4711 ++i;
4712 out_texture->webp_image = CGLTF_PTRINDEX(cgltf_image, cgltf_json_to_int(tokens + i, json_chunk));
4713 ++i;
4714 }
4715 else
4716 {
4717 i = cgltf_skip_json(tokens, i + 1);
4718 }
4719 if (i < 0)
4720 {
4721 return i;
4722 }
4723 }
4724 }
4725 else
4726 {
4727 i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_texture->extensions[out_texture->extensions_count++]));
4728 }
4729
4730 if (i < 0)
4731 {
4732 return i;
4733 }
4734 }
4735 }
4736 else
4737 {
4738 i = cgltf_skip_json(tokens, i + 1);
4739 }
4740
4741 if (i < 0)
4742 {
4743 return i;
4744 }
4745 }
4746
4747 return i;
4748 }
4749
4750 static int cgltf_parse_json_material(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_material* out_material)
4751 {
4752 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4753
4754 cgltf_fill_float_array(out_material->pbr_metallic_roughness.base_color_factor, 4, 1.0f);
4755 out_material->pbr_metallic_roughness.metallic_factor = 1.0f;
4756 out_material->pbr_metallic_roughness.roughness_factor = 1.0f;
4757
4758 cgltf_fill_float_array(out_material->pbr_specular_glossiness.diffuse_factor, 4, 1.0f);
4759 cgltf_fill_float_array(out_material->pbr_specular_glossiness.specular_factor, 3, 1.0f);
4760 out_material->pbr_specular_glossiness.glossiness_factor = 1.0f;
4761
4762 cgltf_fill_float_array(out_material->volume.attenuation_color, 3, 1.0f);
4763 out_material->volume.attenuation_distance = FLT_MAX;
4764
4765 out_material->alpha_cutoff = 0.5f;
4766
4767 int size = tokens[i].size;
4768 ++i;
4769
4770 for (int j = 0; j < size; ++j)
4771 {
4772 CGLTF_CHECK_KEY(tokens[i]);
4773
4774 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
4775 {
4776 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_material->name);
4777 }
4778 else if (cgltf_json_strcmp(tokens+i, json_chunk, "pbrMetallicRoughness") == 0)
4779 {
4780 out_material->has_pbr_metallic_roughness = 1;
4781 i = cgltf_parse_json_pbr_metallic_roughness(options, tokens, i + 1, json_chunk, &out_material->pbr_metallic_roughness);
4782 }
4783 else if (cgltf_json_strcmp(tokens+i, json_chunk, "emissiveFactor") == 0)
4784 {
4785 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_material->emissive_factor, 3);
4786 }
4787 else if (cgltf_json_strcmp(tokens + i, json_chunk, "normalTexture") == 0)
4788 {
4789 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk,
4790 &out_material->normal_texture);
4791 }
4792 else if (cgltf_json_strcmp(tokens + i, json_chunk, "occlusionTexture") == 0)
4793 {
4794 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk,
4795 &out_material->occlusion_texture);
4796 }
4797 else if (cgltf_json_strcmp(tokens + i, json_chunk, "emissiveTexture") == 0)
4798 {
4799 i = cgltf_parse_json_texture_view(options, tokens, i + 1, json_chunk,
4800 &out_material->emissive_texture);
4801 }
4802 else if (cgltf_json_strcmp(tokens + i, json_chunk, "alphaMode") == 0)
4803 {
4804 ++i;
4805 if (cgltf_json_strcmp(tokens + i, json_chunk, "OPAQUE") == 0)
4806 {
4807 out_material->alpha_mode = cgltf_alpha_mode_opaque;
4808 }
4809 else if (cgltf_json_strcmp(tokens + i, json_chunk, "MASK") == 0)
4810 {
4811 out_material->alpha_mode = cgltf_alpha_mode_mask;
4812 }
4813 else if (cgltf_json_strcmp(tokens + i, json_chunk, "BLEND") == 0)
4814 {
4815 out_material->alpha_mode = cgltf_alpha_mode_blend;
4816 }
4817 ++i;
4818 }
4819 else if (cgltf_json_strcmp(tokens + i, json_chunk, "alphaCutoff") == 0)
4820 {
4821 ++i;
4822 out_material->alpha_cutoff = cgltf_json_to_float(tokens + i, json_chunk);
4823 ++i;
4824 }
4825 else if (cgltf_json_strcmp(tokens + i, json_chunk, "doubleSided") == 0)
4826 {
4827 ++i;
4828 out_material->double_sided =
4829 cgltf_json_to_bool(tokens + i, json_chunk);
4830 ++i;
4831 }
4832 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
4833 {
4834 i = cgltf_parse_json_extras(options, tokens, i + 1, json_chunk, &out_material->extras);
4835 }
4836 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
4837 {
4838 ++i;
4839
4840 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
4841 if(out_material->extensions)
4842 {
4843 return CGLTF_ERROR_JSON;
4844 }
4845
4846 int extensions_size = tokens[i].size;
4847 ++i;
4848 out_material->extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
4849 out_material->extensions_count= 0;
4850
4851 if (!out_material->extensions)
4852 {
4853 return CGLTF_ERROR_NOMEM;
4854 }
4855
4856 for (int k = 0; k < extensions_size; ++k)
4857 {
4858 CGLTF_CHECK_KEY(tokens[i]);
4859
4860 if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_pbrSpecularGlossiness") == 0)
4861 {
4862 out_material->has_pbr_specular_glossiness = 1;
4863 i = cgltf_parse_json_pbr_specular_glossiness(options, tokens, i + 1, json_chunk, &out_material->pbr_specular_glossiness);
4864 }
4865 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_unlit") == 0)
4866 {
4867 out_material->unlit = 1;
4868 i = cgltf_skip_json(tokens, i+1);
4869 }
4870 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_clearcoat") == 0)
4871 {
4872 out_material->has_clearcoat = 1;
4873 i = cgltf_parse_json_clearcoat(options, tokens, i + 1, json_chunk, &out_material->clearcoat);
4874 }
4875 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_ior") == 0)
4876 {
4877 out_material->has_ior = 1;
4878 i = cgltf_parse_json_ior(tokens, i + 1, json_chunk, &out_material->ior);
4879 }
4880 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_specular") == 0)
4881 {
4882 out_material->has_specular = 1;
4883 i = cgltf_parse_json_specular(options, tokens, i + 1, json_chunk, &out_material->specular);
4884 }
4885 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_transmission") == 0)
4886 {
4887 out_material->has_transmission = 1;
4888 i = cgltf_parse_json_transmission(options, tokens, i + 1, json_chunk, &out_material->transmission);
4889 }
4890 else if (cgltf_json_strcmp(tokens + i, json_chunk, "KHR_materials_volume") == 0)
4891 {
4892 out_material->has_volume = 1;
4893 i = cgltf_parse_json_volume(options, tokens, i + 1, json_chunk, &out_material->volume);
4894 }
4895 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_sheen") == 0)
4896 {
4897 out_material->has_sheen = 1;
4898 i = cgltf_parse_json_sheen(options, tokens, i + 1, json_chunk, &out_material->sheen);
4899 }
4900 else if (cgltf_json_strcmp(tokens + i, json_chunk, "KHR_materials_emissive_strength") == 0)
4901 {
4902 out_material->has_emissive_strength = 1;
4903 i = cgltf_parse_json_emissive_strength(tokens, i + 1, json_chunk, &out_material->emissive_strength);
4904 }
4905 else if (cgltf_json_strcmp(tokens + i, json_chunk, "KHR_materials_iridescence") == 0)
4906 {
4907 out_material->has_iridescence = 1;
4908 i = cgltf_parse_json_iridescence(options, tokens, i + 1, json_chunk, &out_material->iridescence);
4909 }
4910 else if (cgltf_json_strcmp(tokens + i, json_chunk, "KHR_materials_diffuse_transmission") == 0)
4911 {
4912 out_material->has_diffuse_transmission = 1;
4913 i = cgltf_parse_json_diffuse_transmission(options, tokens, i + 1, json_chunk, &out_material->diffuse_transmission);
4914 }
4915 else if (cgltf_json_strcmp(tokens + i, json_chunk, "KHR_materials_anisotropy") == 0)
4916 {
4917 out_material->has_anisotropy = 1;
4918 i = cgltf_parse_json_anisotropy(options, tokens, i + 1, json_chunk, &out_material->anisotropy);
4919 }
4920 else if (cgltf_json_strcmp(tokens + i, json_chunk, "KHR_materials_dispersion") == 0)
4921 {
4922 out_material->has_dispersion = 1;
4923 i = cgltf_parse_json_dispersion(tokens, i + 1, json_chunk, &out_material->dispersion);
4924 }
4925 else
4926 {
4927 i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_material->extensions[out_material->extensions_count++]));
4928 }
4929
4930 if (i < 0)
4931 {
4932 return i;
4933 }
4934 }
4935 }
4936 else
4937 {
4938 i = cgltf_skip_json(tokens, i+1);
4939 }
4940
4941 if (i < 0)
4942 {
4943 return i;
4944 }
4945 }
4946
4947 return i;
4948 }
4949
4950 static int cgltf_parse_json_accessors(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
4951 {
4952 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_accessor), (void**)&out_data->accessors, &out_data->accessors_count);
4953 if (i < 0)
4954 {
4955 return i;
4956 }
4957
4958 for (cgltf_size j = 0; j < out_data->accessors_count; ++j)
4959 {
4960 i = cgltf_parse_json_accessor(options, tokens, i, json_chunk, &out_data->accessors[j]);
4961 if (i < 0)
4962 {
4963 return i;
4964 }
4965 }
4966 return i;
4967 }
4968
4969 static int cgltf_parse_json_materials(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
4970 {
4971 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_material), (void**)&out_data->materials, &out_data->materials_count);
4972 if (i < 0)
4973 {
4974 return i;
4975 }
4976
4977 for (cgltf_size j = 0; j < out_data->materials_count; ++j)
4978 {
4979 i = cgltf_parse_json_material(options, tokens, i, json_chunk, &out_data->materials[j]);
4980 if (i < 0)
4981 {
4982 return i;
4983 }
4984 }
4985 return i;
4986 }
4987
4988 static int cgltf_parse_json_images(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
4989 {
4990 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_image), (void**)&out_data->images, &out_data->images_count);
4991 if (i < 0)
4992 {
4993 return i;
4994 }
4995
4996 for (cgltf_size j = 0; j < out_data->images_count; ++j)
4997 {
4998 i = cgltf_parse_json_image(options, tokens, i, json_chunk, &out_data->images[j]);
4999 if (i < 0)
5000 {
5001 return i;
5002 }
5003 }
5004 return i;
5005 }
5006
5007 static int cgltf_parse_json_textures(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
5008 {
5009 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_texture), (void**)&out_data->textures, &out_data->textures_count);
5010 if (i < 0)
5011 {
5012 return i;
5013 }
5014
5015 for (cgltf_size j = 0; j < out_data->textures_count; ++j)
5016 {
5017 i = cgltf_parse_json_texture(options, tokens, i, json_chunk, &out_data->textures[j]);
5018 if (i < 0)
5019 {
5020 return i;
5021 }
5022 }
5023 return i;
5024 }
5025
5026 static int cgltf_parse_json_samplers(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
5027 {
5028 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_sampler), (void**)&out_data->samplers, &out_data->samplers_count);
5029 if (i < 0)
5030 {
5031 return i;
5032 }
5033
5034 for (cgltf_size j = 0; j < out_data->samplers_count; ++j)
5035 {
5036 i = cgltf_parse_json_sampler(options, tokens, i, json_chunk, &out_data->samplers[j]);
5037 if (i < 0)
5038 {
5039 return i;
5040 }
5041 }
5042 return i;
5043 }
5044
5045 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)
5046 {
5047 (void)options;
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, "buffer") == 0)
5058 {
5059 ++i;
5060 out_meshopt_compression->buffer = CGLTF_PTRINDEX(cgltf_buffer, cgltf_json_to_int(tokens + i, json_chunk));
5061 ++i;
5062 }
5063 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
5064 {
5065 ++i;
5066 out_meshopt_compression->offset = cgltf_json_to_size(tokens+i, json_chunk);
5067 ++i;
5068 }
5069 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteLength") == 0)
5070 {
5071 ++i;
5072 out_meshopt_compression->size = cgltf_json_to_size(tokens+i, json_chunk);
5073 ++i;
5074 }
5075 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteStride") == 0)
5076 {
5077 ++i;
5078 out_meshopt_compression->stride = cgltf_json_to_size(tokens+i, json_chunk);
5079 ++i;
5080 }
5081 else if (cgltf_json_strcmp(tokens+i, json_chunk, "count") == 0)
5082 {
5083 ++i;
5084 out_meshopt_compression->count = cgltf_json_to_size(tokens+i, json_chunk);
5085 ++i;
5086 }
5087 else if (cgltf_json_strcmp(tokens+i, json_chunk, "mode") == 0)
5088 {
5089 ++i;
5090 if (cgltf_json_strcmp(tokens+i, json_chunk, "ATTRIBUTES") == 0)
5091 {
5092 out_meshopt_compression->mode = cgltf_meshopt_compression_mode_attributes;
5093 }
5094 else if (cgltf_json_strcmp(tokens+i, json_chunk, "TRIANGLES") == 0)
5095 {
5096 out_meshopt_compression->mode = cgltf_meshopt_compression_mode_triangles;
5097 }
5098 else if (cgltf_json_strcmp(tokens+i, json_chunk, "INDICES") == 0)
5099 {
5100 out_meshopt_compression->mode = cgltf_meshopt_compression_mode_indices;
5101 }
5102 ++i;
5103 }
5104 else if (cgltf_json_strcmp(tokens+i, json_chunk, "filter") == 0)
5105 {
5106 ++i;
5107 if (cgltf_json_strcmp(tokens+i, json_chunk, "NONE") == 0)
5108 {
5109 out_meshopt_compression->filter = cgltf_meshopt_compression_filter_none;
5110 }
5111 else if (cgltf_json_strcmp(tokens+i, json_chunk, "OCTAHEDRAL") == 0)
5112 {
5113 out_meshopt_compression->filter = cgltf_meshopt_compression_filter_octahedral;
5114 }
5115 else if (cgltf_json_strcmp(tokens+i, json_chunk, "QUATERNION") == 0)
5116 {
5117 out_meshopt_compression->filter = cgltf_meshopt_compression_filter_quaternion;
5118 }
5119 else if (cgltf_json_strcmp(tokens+i, json_chunk, "EXPONENTIAL") == 0)
5120 {
5121 out_meshopt_compression->filter = cgltf_meshopt_compression_filter_exponential;
5122 }
5123 else if (cgltf_json_strcmp(tokens+i, json_chunk, "COLOR") == 0)
5124 {
5125 out_meshopt_compression->filter = cgltf_meshopt_compression_filter_color;
5126 }
5127 ++i;
5128 }
5129 else
5130 {
5131 i = cgltf_skip_json(tokens, i+1);
5132 }
5133
5134 if (i < 0)
5135 {
5136 return i;
5137 }
5138 }
5139
5140 return i;
5141 }
5142
5143 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)
5144 {
5145 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5146
5147 int size = tokens[i].size;
5148 ++i;
5149
5150 for (int j = 0; j < size; ++j)
5151 {
5152 CGLTF_CHECK_KEY(tokens[i]);
5153
5154 if (cgltf_json_strcmp(tokens + i, json_chunk, "name") == 0)
5155 {
5156 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_buffer_view->name);
5157 }
5158 else if (cgltf_json_strcmp(tokens+i, json_chunk, "buffer") == 0)
5159 {
5160 ++i;
5161 out_buffer_view->buffer = CGLTF_PTRINDEX(cgltf_buffer, cgltf_json_to_int(tokens + i, json_chunk));
5162 ++i;
5163 }
5164 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
5165 {
5166 ++i;
5167 out_buffer_view->offset =
5168 cgltf_json_to_size(tokens+i, json_chunk);
5169 ++i;
5170 }
5171 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteLength") == 0)
5172 {
5173 ++i;
5174 out_buffer_view->size =
5175 cgltf_json_to_size(tokens+i, json_chunk);
5176 ++i;
5177 }
5178 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteStride") == 0)
5179 {
5180 ++i;
5181 out_buffer_view->stride =
5182 cgltf_json_to_size(tokens+i, json_chunk);
5183 ++i;
5184 }
5185 else if (cgltf_json_strcmp(tokens+i, json_chunk, "target") == 0)
5186 {
5187 ++i;
5188 int type = cgltf_json_to_int(tokens+i, json_chunk);
5189 switch (type)
5190 {
5191 case 34962:
5192 type = cgltf_buffer_view_type_vertices;
5193 break;
5194 case 34963:
5195 type = cgltf_buffer_view_type_indices;
5196 break;
5197 default:
5198 type = cgltf_buffer_view_type_invalid;
5199 break;
5200 }
5201 out_buffer_view->type = (cgltf_buffer_view_type)type;
5202 ++i;
5203 }
5204 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
5205 {
5206 i = cgltf_parse_json_extras(options, tokens, i + 1, json_chunk, &out_buffer_view->extras);
5207 }
5208 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
5209 {
5210 ++i;
5211
5212 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5213 if(out_buffer_view->extensions)
5214 {
5215 return CGLTF_ERROR_JSON;
5216 }
5217
5218 int extensions_size = tokens[i].size;
5219 out_buffer_view->extensions_count = 0;
5220 out_buffer_view->extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
5221
5222 if (!out_buffer_view->extensions)
5223 {
5224 return CGLTF_ERROR_NOMEM;
5225 }
5226
5227 ++i;
5228 for (int k = 0; k < extensions_size; ++k)
5229 {
5230 CGLTF_CHECK_KEY(tokens[i]);
5231
5232 if (cgltf_json_strcmp(tokens+i, json_chunk, "EXT_meshopt_compression") == 0)
5233 {
5234 out_buffer_view->has_meshopt_compression = 1;
5235 i = cgltf_parse_json_meshopt_compression(options, tokens, i + 1, json_chunk, &out_buffer_view->meshopt_compression);
5236 }
5237 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_meshopt_compression") == 0)
5238 {
5239 out_buffer_view->has_meshopt_compression = 1;
5240 out_buffer_view->meshopt_compression.is_khr = 1;
5241 i = cgltf_parse_json_meshopt_compression(options, tokens, i + 1, json_chunk, &out_buffer_view->meshopt_compression);
5242 }
5243 else
5244 {
5245 i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_buffer_view->extensions[out_buffer_view->extensions_count++]));
5246 }
5247
5248 if (i < 0)
5249 {
5250 return i;
5251 }
5252 }
5253 }
5254 else
5255 {
5256 i = cgltf_skip_json(tokens, i+1);
5257 }
5258
5259 if (i < 0)
5260 {
5261 return i;
5262 }
5263 }
5264
5265 return i;
5266 }
5267
5268 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)
5269 {
5270 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_buffer_view), (void**)&out_data->buffer_views, &out_data->buffer_views_count);
5271 if (i < 0)
5272 {
5273 return i;
5274 }
5275
5276 for (cgltf_size j = 0; j < out_data->buffer_views_count; ++j)
5277 {
5278 i = cgltf_parse_json_buffer_view(options, tokens, i, json_chunk, &out_data->buffer_views[j]);
5279 if (i < 0)
5280 {
5281 return i;
5282 }
5283 }
5284 return i;
5285 }
5286
5287 static int cgltf_parse_json_buffer(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_buffer* out_buffer)
5288 {
5289 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5290
5291 int size = tokens[i].size;
5292 ++i;
5293
5294 for (int j = 0; j < size; ++j)
5295 {
5296 CGLTF_CHECK_KEY(tokens[i]);
5297
5298 if (cgltf_json_strcmp(tokens + i, json_chunk, "name") == 0)
5299 {
5300 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_buffer->name);
5301 }
5302 else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteLength") == 0)
5303 {
5304 ++i;
5305 out_buffer->size =
5306 cgltf_json_to_size(tokens+i, json_chunk);
5307 ++i;
5308 }
5309 else if (cgltf_json_strcmp(tokens+i, json_chunk, "uri") == 0)
5310 {
5311 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_buffer->uri);
5312 }
5313 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
5314 {
5315 i = cgltf_parse_json_extras(options, tokens, i + 1, json_chunk, &out_buffer->extras);
5316 }
5317 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
5318 {
5319 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_buffer->extensions_count, &out_buffer->extensions);
5320 }
5321 else
5322 {
5323 i = cgltf_skip_json(tokens, i+1);
5324 }
5325
5326 if (i < 0)
5327 {
5328 return i;
5329 }
5330 }
5331
5332 return i;
5333 }
5334
5335 static int cgltf_parse_json_buffers(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
5336 {
5337 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_buffer), (void**)&out_data->buffers, &out_data->buffers_count);
5338 if (i < 0)
5339 {
5340 return i;
5341 }
5342
5343 for (cgltf_size j = 0; j < out_data->buffers_count; ++j)
5344 {
5345 i = cgltf_parse_json_buffer(options, tokens, i, json_chunk, &out_data->buffers[j]);
5346 if (i < 0)
5347 {
5348 return i;
5349 }
5350 }
5351 return i;
5352 }
5353
5354 static int cgltf_parse_json_skin(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_skin* out_skin)
5355 {
5356 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5357
5358 int size = tokens[i].size;
5359 ++i;
5360
5361 for (int j = 0; j < size; ++j)
5362 {
5363 CGLTF_CHECK_KEY(tokens[i]);
5364
5365 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
5366 {
5367 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_skin->name);
5368 }
5369 else if (cgltf_json_strcmp(tokens+i, json_chunk, "joints") == 0)
5370 {
5371 i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_node*), (void**)&out_skin->joints, &out_skin->joints_count);
5372 if (i < 0)
5373 {
5374 return i;
5375 }
5376
5377 for (cgltf_size k = 0; k < out_skin->joints_count; ++k)
5378 {
5379 out_skin->joints[k] = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
5380 ++i;
5381 }
5382 }
5383 else if (cgltf_json_strcmp(tokens+i, json_chunk, "skeleton") == 0)
5384 {
5385 ++i;
5386 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
5387 out_skin->skeleton = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
5388 ++i;
5389 }
5390 else if (cgltf_json_strcmp(tokens+i, json_chunk, "inverseBindMatrices") == 0)
5391 {
5392 ++i;
5393 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
5394 out_skin->inverse_bind_matrices = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
5395 ++i;
5396 }
5397 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
5398 {
5399 i = cgltf_parse_json_extras(options, tokens, i + 1, json_chunk, &out_skin->extras);
5400 }
5401 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
5402 {
5403 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_skin->extensions_count, &out_skin->extensions);
5404 }
5405 else
5406 {
5407 i = cgltf_skip_json(tokens, i+1);
5408 }
5409
5410 if (i < 0)
5411 {
5412 return i;
5413 }
5414 }
5415
5416 return i;
5417 }
5418
5419 static int cgltf_parse_json_skins(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
5420 {
5421 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_skin), (void**)&out_data->skins, &out_data->skins_count);
5422 if (i < 0)
5423 {
5424 return i;
5425 }
5426
5427 for (cgltf_size j = 0; j < out_data->skins_count; ++j)
5428 {
5429 i = cgltf_parse_json_skin(options, tokens, i, json_chunk, &out_data->skins[j]);
5430 if (i < 0)
5431 {
5432 return i;
5433 }
5434 }
5435 return i;
5436 }
5437
5438 static int cgltf_parse_json_camera(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_camera* out_camera)
5439 {
5440 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5441
5442 int size = tokens[i].size;
5443 ++i;
5444
5445 for (int j = 0; j < size; ++j)
5446 {
5447 CGLTF_CHECK_KEY(tokens[i]);
5448
5449 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
5450 {
5451 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_camera->name);
5452 }
5453 else if (cgltf_json_strcmp(tokens+i, json_chunk, "perspective") == 0)
5454 {
5455 ++i;
5456
5457 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5458
5459 int data_size = tokens[i].size;
5460 ++i;
5461
5462 if (out_camera->type != cgltf_camera_type_invalid)
5463 {
5464 return CGLTF_ERROR_JSON;
5465 }
5466
5467 out_camera->type = cgltf_camera_type_perspective;
5468
5469 for (int k = 0; k < data_size; ++k)
5470 {
5471 CGLTF_CHECK_KEY(tokens[i]);
5472
5473 if (cgltf_json_strcmp(tokens+i, json_chunk, "aspectRatio") == 0)
5474 {
5475 ++i;
5476 out_camera->data.perspective.has_aspect_ratio = 1;
5477 out_camera->data.perspective.aspect_ratio = cgltf_json_to_float(tokens + i, json_chunk);
5478 ++i;
5479 }
5480 else if (cgltf_json_strcmp(tokens+i, json_chunk, "yfov") == 0)
5481 {
5482 ++i;
5483 out_camera->data.perspective.yfov = cgltf_json_to_float(tokens + i, json_chunk);
5484 ++i;
5485 }
5486 else if (cgltf_json_strcmp(tokens+i, json_chunk, "zfar") == 0)
5487 {
5488 ++i;
5489 out_camera->data.perspective.has_zfar = 1;
5490 out_camera->data.perspective.zfar = cgltf_json_to_float(tokens + i, json_chunk);
5491 ++i;
5492 }
5493 else if (cgltf_json_strcmp(tokens+i, json_chunk, "znear") == 0)
5494 {
5495 ++i;
5496 out_camera->data.perspective.znear = cgltf_json_to_float(tokens + i, json_chunk);
5497 ++i;
5498 }
5499 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
5500 {
5501 i = cgltf_parse_json_extras(options, tokens, i + 1, json_chunk, &out_camera->data.perspective.extras);
5502 }
5503 else
5504 {
5505 i = cgltf_skip_json(tokens, i+1);
5506 }
5507
5508 if (i < 0)
5509 {
5510 return i;
5511 }
5512 }
5513 }
5514 else if (cgltf_json_strcmp(tokens+i, json_chunk, "orthographic") == 0)
5515 {
5516 ++i;
5517
5518 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5519
5520 int data_size = tokens[i].size;
5521 ++i;
5522
5523 if (out_camera->type != cgltf_camera_type_invalid)
5524 {
5525 return CGLTF_ERROR_JSON;
5526 }
5527
5528 out_camera->type = cgltf_camera_type_orthographic;
5529
5530 for (int k = 0; k < data_size; ++k)
5531 {
5532 CGLTF_CHECK_KEY(tokens[i]);
5533
5534 if (cgltf_json_strcmp(tokens+i, json_chunk, "xmag") == 0)
5535 {
5536 ++i;
5537 out_camera->data.orthographic.xmag = cgltf_json_to_float(tokens + i, json_chunk);
5538 ++i;
5539 }
5540 else if (cgltf_json_strcmp(tokens+i, json_chunk, "ymag") == 0)
5541 {
5542 ++i;
5543 out_camera->data.orthographic.ymag = cgltf_json_to_float(tokens + i, json_chunk);
5544 ++i;
5545 }
5546 else if (cgltf_json_strcmp(tokens+i, json_chunk, "zfar") == 0)
5547 {
5548 ++i;
5549 out_camera->data.orthographic.zfar = cgltf_json_to_float(tokens + i, json_chunk);
5550 ++i;
5551 }
5552 else if (cgltf_json_strcmp(tokens+i, json_chunk, "znear") == 0)
5553 {
5554 ++i;
5555 out_camera->data.orthographic.znear = cgltf_json_to_float(tokens + i, json_chunk);
5556 ++i;
5557 }
5558 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
5559 {
5560 i = cgltf_parse_json_extras(options, tokens, i + 1, json_chunk, &out_camera->data.orthographic.extras);
5561 }
5562 else
5563 {
5564 i = cgltf_skip_json(tokens, i+1);
5565 }
5566
5567 if (i < 0)
5568 {
5569 return i;
5570 }
5571 }
5572 }
5573 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
5574 {
5575 i = cgltf_parse_json_extras(options, tokens, i + 1, json_chunk, &out_camera->extras);
5576 }
5577 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
5578 {
5579 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_camera->extensions_count, &out_camera->extensions);
5580 }
5581 else
5582 {
5583 i = cgltf_skip_json(tokens, i+1);
5584 }
5585
5586 if (i < 0)
5587 {
5588 return i;
5589 }
5590 }
5591
5592 return i;
5593 }
5594
5595 static int cgltf_parse_json_cameras(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
5596 {
5597 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_camera), (void**)&out_data->cameras, &out_data->cameras_count);
5598 if (i < 0)
5599 {
5600 return i;
5601 }
5602
5603 for (cgltf_size j = 0; j < out_data->cameras_count; ++j)
5604 {
5605 i = cgltf_parse_json_camera(options, tokens, i, json_chunk, &out_data->cameras[j]);
5606 if (i < 0)
5607 {
5608 return i;
5609 }
5610 }
5611 return i;
5612 }
5613
5614 static int cgltf_parse_json_light(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_light* out_light)
5615 {
5616 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5617
5618 out_light->color[0] = 1.f;
5619 out_light->color[1] = 1.f;
5620 out_light->color[2] = 1.f;
5621 out_light->intensity = 1.f;
5622
5623 out_light->spot_inner_cone_angle = 0.f;
5624 out_light->spot_outer_cone_angle = 3.1415926535f / 4.0f;
5625
5626 int size = tokens[i].size;
5627 ++i;
5628
5629 for (int j = 0; j < size; ++j)
5630 {
5631 CGLTF_CHECK_KEY(tokens[i]);
5632
5633 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
5634 {
5635 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_light->name);
5636 }
5637 else if (cgltf_json_strcmp(tokens + i, json_chunk, "color") == 0)
5638 {
5639 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_light->color, 3);
5640 }
5641 else if (cgltf_json_strcmp(tokens + i, json_chunk, "intensity") == 0)
5642 {
5643 ++i;
5644 out_light->intensity = cgltf_json_to_float(tokens + i, json_chunk);
5645 ++i;
5646 }
5647 else if (cgltf_json_strcmp(tokens+i, json_chunk, "type") == 0)
5648 {
5649 ++i;
5650 if (cgltf_json_strcmp(tokens + i, json_chunk, "directional") == 0)
5651 {
5652 out_light->type = cgltf_light_type_directional;
5653 }
5654 else if (cgltf_json_strcmp(tokens + i, json_chunk, "point") == 0)
5655 {
5656 out_light->type = cgltf_light_type_point;
5657 }
5658 else if (cgltf_json_strcmp(tokens + i, json_chunk, "spot") == 0)
5659 {
5660 out_light->type = cgltf_light_type_spot;
5661 }
5662 ++i;
5663 }
5664 else if (cgltf_json_strcmp(tokens + i, json_chunk, "range") == 0)
5665 {
5666 ++i;
5667 out_light->range = cgltf_json_to_float(tokens + i, json_chunk);
5668 ++i;
5669 }
5670 else if (cgltf_json_strcmp(tokens+i, json_chunk, "spot") == 0)
5671 {
5672 ++i;
5673
5674 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5675
5676 int data_size = tokens[i].size;
5677 ++i;
5678
5679 for (int k = 0; k < data_size; ++k)
5680 {
5681 CGLTF_CHECK_KEY(tokens[i]);
5682
5683 if (cgltf_json_strcmp(tokens+i, json_chunk, "innerConeAngle") == 0)
5684 {
5685 ++i;
5686 out_light->spot_inner_cone_angle = cgltf_json_to_float(tokens + i, json_chunk);
5687 ++i;
5688 }
5689 else if (cgltf_json_strcmp(tokens+i, json_chunk, "outerConeAngle") == 0)
5690 {
5691 ++i;
5692 out_light->spot_outer_cone_angle = cgltf_json_to_float(tokens + i, json_chunk);
5693 ++i;
5694 }
5695 else
5696 {
5697 i = cgltf_skip_json(tokens, i+1);
5698 }
5699
5700 if (i < 0)
5701 {
5702 return i;
5703 }
5704 }
5705 }
5706 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
5707 {
5708 i = cgltf_parse_json_extras(options, tokens, i + 1, json_chunk, &out_light->extras);
5709 }
5710 else
5711 {
5712 i = cgltf_skip_json(tokens, i+1);
5713 }
5714
5715 if (i < 0)
5716 {
5717 return i;
5718 }
5719 }
5720
5721 return i;
5722 }
5723
5724 static int cgltf_parse_json_lights(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
5725 {
5726 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_light), (void**)&out_data->lights, &out_data->lights_count);
5727 if (i < 0)
5728 {
5729 return i;
5730 }
5731
5732 for (cgltf_size j = 0; j < out_data->lights_count; ++j)
5733 {
5734 i = cgltf_parse_json_light(options, tokens, i, json_chunk, &out_data->lights[j]);
5735 if (i < 0)
5736 {
5737 return i;
5738 }
5739 }
5740 return i;
5741 }
5742
5743 static int cgltf_parse_json_node(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_node* out_node)
5744 {
5745 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5746
5747 out_node->rotation[3] = 1.0f;
5748 out_node->scale[0] = 1.0f;
5749 out_node->scale[1] = 1.0f;
5750 out_node->scale[2] = 1.0f;
5751 out_node->matrix[0] = 1.0f;
5752 out_node->matrix[5] = 1.0f;
5753 out_node->matrix[10] = 1.0f;
5754 out_node->matrix[15] = 1.0f;
5755
5756 int size = tokens[i].size;
5757 ++i;
5758
5759 for (int j = 0; j < size; ++j)
5760 {
5761 CGLTF_CHECK_KEY(tokens[i]);
5762
5763 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
5764 {
5765 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_node->name);
5766 }
5767 else if (cgltf_json_strcmp(tokens+i, json_chunk, "children") == 0)
5768 {
5769 i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_node*), (void**)&out_node->children, &out_node->children_count);
5770 if (i < 0)
5771 {
5772 return i;
5773 }
5774
5775 for (cgltf_size k = 0; k < out_node->children_count; ++k)
5776 {
5777 out_node->children[k] = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
5778 ++i;
5779 }
5780 }
5781 else if (cgltf_json_strcmp(tokens+i, json_chunk, "mesh") == 0)
5782 {
5783 ++i;
5784 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
5785 out_node->mesh = CGLTF_PTRINDEX(cgltf_mesh, cgltf_json_to_int(tokens + i, json_chunk));
5786 ++i;
5787 }
5788 else if (cgltf_json_strcmp(tokens+i, json_chunk, "skin") == 0)
5789 {
5790 ++i;
5791 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
5792 out_node->skin = CGLTF_PTRINDEX(cgltf_skin, cgltf_json_to_int(tokens + i, json_chunk));
5793 ++i;
5794 }
5795 else if (cgltf_json_strcmp(tokens+i, json_chunk, "camera") == 0)
5796 {
5797 ++i;
5798 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
5799 out_node->camera = CGLTF_PTRINDEX(cgltf_camera, cgltf_json_to_int(tokens + i, json_chunk));
5800 ++i;
5801 }
5802 else if (cgltf_json_strcmp(tokens+i, json_chunk, "translation") == 0)
5803 {
5804 out_node->has_translation = 1;
5805 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_node->translation, 3);
5806 }
5807 else if (cgltf_json_strcmp(tokens+i, json_chunk, "rotation") == 0)
5808 {
5809 out_node->has_rotation = 1;
5810 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_node->rotation, 4);
5811 }
5812 else if (cgltf_json_strcmp(tokens+i, json_chunk, "scale") == 0)
5813 {
5814 out_node->has_scale = 1;
5815 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_node->scale, 3);
5816 }
5817 else if (cgltf_json_strcmp(tokens+i, json_chunk, "matrix") == 0)
5818 {
5819 out_node->has_matrix = 1;
5820 i = cgltf_parse_json_float_array(tokens, i + 1, json_chunk, out_node->matrix, 16);
5821 }
5822 else if (cgltf_json_strcmp(tokens + i, json_chunk, "weights") == 0)
5823 {
5824 i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_float), (void**)&out_node->weights, &out_node->weights_count);
5825 if (i < 0)
5826 {
5827 return i;
5828 }
5829
5830 i = cgltf_parse_json_float_array(tokens, i - 1, json_chunk, out_node->weights, (int)out_node->weights_count);
5831 }
5832 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
5833 {
5834 i = cgltf_parse_json_extras(options, tokens, i + 1, json_chunk, &out_node->extras);
5835 }
5836 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
5837 {
5838 ++i;
5839
5840 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5841 if(out_node->extensions)
5842 {
5843 return CGLTF_ERROR_JSON;
5844 }
5845
5846 int extensions_size = tokens[i].size;
5847 out_node->extensions_count= 0;
5848 out_node->extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
5849
5850 if (!out_node->extensions)
5851 {
5852 return CGLTF_ERROR_NOMEM;
5853 }
5854
5855 ++i;
5856
5857 for (int k = 0; k < extensions_size; ++k)
5858 {
5859 CGLTF_CHECK_KEY(tokens[i]);
5860
5861 if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_lights_punctual") == 0)
5862 {
5863 ++i;
5864
5865 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5866
5867 int data_size = tokens[i].size;
5868 ++i;
5869
5870 for (int m = 0; m < data_size; ++m)
5871 {
5872 CGLTF_CHECK_KEY(tokens[i]);
5873
5874 if (cgltf_json_strcmp(tokens + i, json_chunk, "light") == 0)
5875 {
5876 ++i;
5877 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_PRIMITIVE);
5878 out_node->light = CGLTF_PTRINDEX(cgltf_light, cgltf_json_to_int(tokens + i, json_chunk));
5879 ++i;
5880 }
5881 else
5882 {
5883 i = cgltf_skip_json(tokens, i + 1);
5884 }
5885
5886 if (i < 0)
5887 {
5888 return i;
5889 }
5890 }
5891 }
5892 else if (cgltf_json_strcmp(tokens + i, json_chunk, "EXT_mesh_gpu_instancing") == 0)
5893 {
5894 out_node->has_mesh_gpu_instancing = 1;
5895 i = cgltf_parse_json_mesh_gpu_instancing(options, tokens, i + 1, json_chunk, &out_node->mesh_gpu_instancing);
5896 }
5897 else
5898 {
5899 i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_node->extensions[out_node->extensions_count++]));
5900 }
5901
5902 if (i < 0)
5903 {
5904 return i;
5905 }
5906 }
5907 }
5908 else
5909 {
5910 i = cgltf_skip_json(tokens, i+1);
5911 }
5912
5913 if (i < 0)
5914 {
5915 return i;
5916 }
5917 }
5918
5919 return i;
5920 }
5921
5922 static int cgltf_parse_json_nodes(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
5923 {
5924 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_node), (void**)&out_data->nodes, &out_data->nodes_count);
5925 if (i < 0)
5926 {
5927 return i;
5928 }
5929
5930 for (cgltf_size j = 0; j < out_data->nodes_count; ++j)
5931 {
5932 i = cgltf_parse_json_node(options, tokens, i, json_chunk, &out_data->nodes[j]);
5933 if (i < 0)
5934 {
5935 return i;
5936 }
5937 }
5938 return i;
5939 }
5940
5941 static int cgltf_parse_json_scene(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_scene* out_scene)
5942 {
5943 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
5944
5945 int size = tokens[i].size;
5946 ++i;
5947
5948 for (int j = 0; j < size; ++j)
5949 {
5950 CGLTF_CHECK_KEY(tokens[i]);
5951
5952 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
5953 {
5954 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_scene->name);
5955 }
5956 else if (cgltf_json_strcmp(tokens+i, json_chunk, "nodes") == 0)
5957 {
5958 i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_node*), (void**)&out_scene->nodes, &out_scene->nodes_count);
5959 if (i < 0)
5960 {
5961 return i;
5962 }
5963
5964 for (cgltf_size k = 0; k < out_scene->nodes_count; ++k)
5965 {
5966 out_scene->nodes[k] = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
5967 ++i;
5968 }
5969 }
5970 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
5971 {
5972 i = cgltf_parse_json_extras(options, tokens, i + 1, json_chunk, &out_scene->extras);
5973 }
5974 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
5975 {
5976 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_scene->extensions_count, &out_scene->extensions);
5977 }
5978 else
5979 {
5980 i = cgltf_skip_json(tokens, i+1);
5981 }
5982
5983 if (i < 0)
5984 {
5985 return i;
5986 }
5987 }
5988
5989 return i;
5990 }
5991
5992 static int cgltf_parse_json_scenes(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
5993 {
5994 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_scene), (void**)&out_data->scenes, &out_data->scenes_count);
5995 if (i < 0)
5996 {
5997 return i;
5998 }
5999
6000 for (cgltf_size j = 0; j < out_data->scenes_count; ++j)
6001 {
6002 i = cgltf_parse_json_scene(options, tokens, i, json_chunk, &out_data->scenes[j]);
6003 if (i < 0)
6004 {
6005 return i;
6006 }
6007 }
6008 return i;
6009 }
6010
6011 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)
6012 {
6013 (void)options;
6014 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
6015
6016 int size = tokens[i].size;
6017 ++i;
6018
6019 for (int j = 0; j < size; ++j)
6020 {
6021 CGLTF_CHECK_KEY(tokens[i]);
6022
6023 if (cgltf_json_strcmp(tokens+i, json_chunk, "input") == 0)
6024 {
6025 ++i;
6026 out_sampler->input = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
6027 ++i;
6028 }
6029 else if (cgltf_json_strcmp(tokens+i, json_chunk, "output") == 0)
6030 {
6031 ++i;
6032 out_sampler->output = CGLTF_PTRINDEX(cgltf_accessor, cgltf_json_to_int(tokens + i, json_chunk));
6033 ++i;
6034 }
6035 else if (cgltf_json_strcmp(tokens+i, json_chunk, "interpolation") == 0)
6036 {
6037 ++i;
6038 if (cgltf_json_strcmp(tokens + i, json_chunk, "LINEAR") == 0)
6039 {
6040 out_sampler->interpolation = cgltf_interpolation_type_linear;
6041 }
6042 else if (cgltf_json_strcmp(tokens + i, json_chunk, "STEP") == 0)
6043 {
6044 out_sampler->interpolation = cgltf_interpolation_type_step;
6045 }
6046 else if (cgltf_json_strcmp(tokens + i, json_chunk, "CUBICSPLINE") == 0)
6047 {
6048 out_sampler->interpolation = cgltf_interpolation_type_cubic_spline;
6049 }
6050 ++i;
6051 }
6052 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
6053 {
6054 i = cgltf_parse_json_extras(options, tokens, i + 1, json_chunk, &out_sampler->extras);
6055 }
6056 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
6057 {
6058 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_sampler->extensions_count, &out_sampler->extensions);
6059 }
6060 else
6061 {
6062 i = cgltf_skip_json(tokens, i+1);
6063 }
6064
6065 if (i < 0)
6066 {
6067 return i;
6068 }
6069 }
6070
6071 return i;
6072 }
6073
6074 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)
6075 {
6076 (void)options;
6077 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
6078
6079 int size = tokens[i].size;
6080 ++i;
6081
6082 for (int j = 0; j < size; ++j)
6083 {
6084 CGLTF_CHECK_KEY(tokens[i]);
6085
6086 if (cgltf_json_strcmp(tokens+i, json_chunk, "sampler") == 0)
6087 {
6088 ++i;
6089 out_channel->sampler = CGLTF_PTRINDEX(cgltf_animation_sampler, cgltf_json_to_int(tokens + i, json_chunk));
6090 ++i;
6091 }
6092 else if (cgltf_json_strcmp(tokens+i, json_chunk, "target") == 0)
6093 {
6094 ++i;
6095
6096 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
6097
6098 int target_size = tokens[i].size;
6099 ++i;
6100
6101 for (int k = 0; k < target_size; ++k)
6102 {
6103 CGLTF_CHECK_KEY(tokens[i]);
6104
6105 if (cgltf_json_strcmp(tokens+i, json_chunk, "node") == 0)
6106 {
6107 ++i;
6108 out_channel->target_node = CGLTF_PTRINDEX(cgltf_node, cgltf_json_to_int(tokens + i, json_chunk));
6109 ++i;
6110 }
6111 else if (cgltf_json_strcmp(tokens+i, json_chunk, "path") == 0)
6112 {
6113 ++i;
6114 if (cgltf_json_strcmp(tokens+i, json_chunk, "translation") == 0)
6115 {
6116 out_channel->target_path = cgltf_animation_path_type_translation;
6117 }
6118 else if (cgltf_json_strcmp(tokens+i, json_chunk, "rotation") == 0)
6119 {
6120 out_channel->target_path = cgltf_animation_path_type_rotation;
6121 }
6122 else if (cgltf_json_strcmp(tokens+i, json_chunk, "scale") == 0)
6123 {
6124 out_channel->target_path = cgltf_animation_path_type_scale;
6125 }
6126 else if (cgltf_json_strcmp(tokens+i, json_chunk, "weights") == 0)
6127 {
6128 out_channel->target_path = cgltf_animation_path_type_weights;
6129 }
6130 ++i;
6131 }
6132 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
6133 {
6134 i = cgltf_parse_json_extras(options, tokens, i + 1, json_chunk, &out_channel->extras);
6135 }
6136 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
6137 {
6138 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_channel->extensions_count, &out_channel->extensions);
6139 }
6140 else
6141 {
6142 i = cgltf_skip_json(tokens, i+1);
6143 }
6144
6145 if (i < 0)
6146 {
6147 return i;
6148 }
6149 }
6150 }
6151 else
6152 {
6153 i = cgltf_skip_json(tokens, i+1);
6154 }
6155
6156 if (i < 0)
6157 {
6158 return i;
6159 }
6160 }
6161
6162 return i;
6163 }
6164
6165 static int cgltf_parse_json_animation(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_animation* out_animation)
6166 {
6167 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
6168
6169 int size = tokens[i].size;
6170 ++i;
6171
6172 for (int j = 0; j < size; ++j)
6173 {
6174 CGLTF_CHECK_KEY(tokens[i]);
6175
6176 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
6177 {
6178 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_animation->name);
6179 }
6180 else if (cgltf_json_strcmp(tokens+i, json_chunk, "samplers") == 0)
6181 {
6182 i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_animation_sampler), (void**)&out_animation->samplers, &out_animation->samplers_count);
6183 if (i < 0)
6184 {
6185 return i;
6186 }
6187
6188 for (cgltf_size k = 0; k < out_animation->samplers_count; ++k)
6189 {
6190 i = cgltf_parse_json_animation_sampler(options, tokens, i, json_chunk, &out_animation->samplers[k]);
6191 if (i < 0)
6192 {
6193 return i;
6194 }
6195 }
6196 }
6197 else if (cgltf_json_strcmp(tokens+i, json_chunk, "channels") == 0)
6198 {
6199 i = cgltf_parse_json_array(options, tokens, i + 1, json_chunk, sizeof(cgltf_animation_channel), (void**)&out_animation->channels, &out_animation->channels_count);
6200 if (i < 0)
6201 {
6202 return i;
6203 }
6204
6205 for (cgltf_size k = 0; k < out_animation->channels_count; ++k)
6206 {
6207 i = cgltf_parse_json_animation_channel(options, tokens, i, json_chunk, &out_animation->channels[k]);
6208 if (i < 0)
6209 {
6210 return i;
6211 }
6212 }
6213 }
6214 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
6215 {
6216 i = cgltf_parse_json_extras(options, tokens, i + 1, json_chunk, &out_animation->extras);
6217 }
6218 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
6219 {
6220 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_animation->extensions_count, &out_animation->extensions);
6221 }
6222 else
6223 {
6224 i = cgltf_skip_json(tokens, i+1);
6225 }
6226
6227 if (i < 0)
6228 {
6229 return i;
6230 }
6231 }
6232
6233 return i;
6234 }
6235
6236 static int cgltf_parse_json_animations(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
6237 {
6238 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_animation), (void**)&out_data->animations, &out_data->animations_count);
6239 if (i < 0)
6240 {
6241 return i;
6242 }
6243
6244 for (cgltf_size j = 0; j < out_data->animations_count; ++j)
6245 {
6246 i = cgltf_parse_json_animation(options, tokens, i, json_chunk, &out_data->animations[j]);
6247 if (i < 0)
6248 {
6249 return i;
6250 }
6251 }
6252 return i;
6253 }
6254
6255 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)
6256 {
6257 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
6258
6259 int size = tokens[i].size;
6260 ++i;
6261
6262 for (int j = 0; j < size; ++j)
6263 {
6264 CGLTF_CHECK_KEY(tokens[i]);
6265
6266 if (cgltf_json_strcmp(tokens+i, json_chunk, "name") == 0)
6267 {
6268 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_variant->name);
6269 }
6270 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
6271 {
6272 i = cgltf_parse_json_extras(options, tokens, i + 1, json_chunk, &out_variant->extras);
6273 }
6274 else
6275 {
6276 i = cgltf_skip_json(tokens, i+1);
6277 }
6278
6279 if (i < 0)
6280 {
6281 return i;
6282 }
6283 }
6284
6285 return i;
6286 }
6287
6288 static int cgltf_parse_json_variants(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
6289 {
6290 i = cgltf_parse_json_array(options, tokens, i, json_chunk, sizeof(cgltf_material_variant), (void**)&out_data->variants, &out_data->variants_count);
6291 if (i < 0)
6292 {
6293 return i;
6294 }
6295
6296 for (cgltf_size j = 0; j < out_data->variants_count; ++j)
6297 {
6298 i = cgltf_parse_json_variant(options, tokens, i, json_chunk, &out_data->variants[j]);
6299 if (i < 0)
6300 {
6301 return i;
6302 }
6303 }
6304 return i;
6305 }
6306
6307 static int cgltf_parse_json_asset(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_asset* out_asset)
6308 {
6309 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
6310
6311 int size = tokens[i].size;
6312 ++i;
6313
6314 for (int j = 0; j < size; ++j)
6315 {
6316 CGLTF_CHECK_KEY(tokens[i]);
6317
6318 if (cgltf_json_strcmp(tokens+i, json_chunk, "copyright") == 0)
6319 {
6320 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_asset->copyright);
6321 }
6322 else if (cgltf_json_strcmp(tokens+i, json_chunk, "generator") == 0)
6323 {
6324 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_asset->generator);
6325 }
6326 else if (cgltf_json_strcmp(tokens+i, json_chunk, "version") == 0)
6327 {
6328 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_asset->version);
6329 }
6330 else if (cgltf_json_strcmp(tokens+i, json_chunk, "minVersion") == 0)
6331 {
6332 i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_asset->min_version);
6333 }
6334 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
6335 {
6336 i = cgltf_parse_json_extras(options, tokens, i + 1, json_chunk, &out_asset->extras);
6337 }
6338 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
6339 {
6340 i = cgltf_parse_json_unprocessed_extensions(options, tokens, i, json_chunk, &out_asset->extensions_count, &out_asset->extensions);
6341 }
6342 else
6343 {
6344 i = cgltf_skip_json(tokens, i+1);
6345 }
6346
6347 if (i < 0)
6348 {
6349 return i;
6350 }
6351 }
6352
6353 if (out_asset->version && CGLTF_ATOF(out_asset->version) < 2)
6354 {
6355 return CGLTF_ERROR_LEGACY;
6356 }
6357
6358 return i;
6359 }
6360
6361 cgltf_size cgltf_num_components(cgltf_type type) {
6362 switch (type)
6363 {
6364 case cgltf_type_vec2:
6365 return 2;
6366 case cgltf_type_vec3:
6367 return 3;
6368 case cgltf_type_vec4:
6369 return 4;
6370 case cgltf_type_mat2:
6371 return 4;
6372 case cgltf_type_mat3:
6373 return 9;
6374 case cgltf_type_mat4:
6375 return 16;
6376 case cgltf_type_invalid:
6377 case cgltf_type_scalar:
6378 default:
6379 return 1;
6380 }
6381 }
6382
6383 cgltf_size cgltf_component_size(cgltf_component_type component_type) {
6384 switch (component_type)
6385 {
6386 case cgltf_component_type_r_8:
6387 case cgltf_component_type_r_8u:
6388 return 1;
6389 case cgltf_component_type_r_16:
6390 case cgltf_component_type_r_16u:
6391 return 2;
6392 case cgltf_component_type_r_32u:
6393 case cgltf_component_type_r_32f:
6394 return 4;
6395 case cgltf_component_type_invalid:
6396 default:
6397 return 0;
6398 }
6399 }
6400
6401 cgltf_size cgltf_calc_size(cgltf_type type, cgltf_component_type component_type)
6402 {
6403 cgltf_size component_size = cgltf_component_size(component_type);
6404 if (type == cgltf_type_mat2 && component_size == 1)
6405 {
6406 return 8 * component_size;
6407 }
6408 else if (type == cgltf_type_mat3 && (component_size == 1 || component_size == 2))
6409 {
6410 return 12 * component_size;
6411 }
6412 return component_size * cgltf_num_components(type);
6413 }
6414
6415 static int cgltf_fixup_pointers(cgltf_data* out_data);
6416
6417 static int cgltf_parse_json_root(cgltf_options* options, jsmntok_t const* tokens, int i, const uint8_t* json_chunk, cgltf_data* out_data)
6418 {
6419 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
6420
6421 int size = tokens[i].size;
6422 ++i;
6423
6424 for (int j = 0; j < size; ++j)
6425 {
6426 CGLTF_CHECK_KEY(tokens[i]);
6427
6428 if (cgltf_json_strcmp(tokens + i, json_chunk, "asset") == 0)
6429 {
6430 i = cgltf_parse_json_asset(options, tokens, i + 1, json_chunk, &out_data->asset);
6431 }
6432 else if (cgltf_json_strcmp(tokens + i, json_chunk, "meshes") == 0)
6433 {
6434 i = cgltf_parse_json_meshes(options, tokens, i + 1, json_chunk, out_data);
6435 }
6436 else if (cgltf_json_strcmp(tokens + i, json_chunk, "accessors") == 0)
6437 {
6438 i = cgltf_parse_json_accessors(options, tokens, i + 1, json_chunk, out_data);
6439 }
6440 else if (cgltf_json_strcmp(tokens + i, json_chunk, "bufferViews") == 0)
6441 {
6442 i = cgltf_parse_json_buffer_views(options, tokens, i + 1, json_chunk, out_data);
6443 }
6444 else if (cgltf_json_strcmp(tokens + i, json_chunk, "buffers") == 0)
6445 {
6446 i = cgltf_parse_json_buffers(options, tokens, i + 1, json_chunk, out_data);
6447 }
6448 else if (cgltf_json_strcmp(tokens + i, json_chunk, "materials") == 0)
6449 {
6450 i = cgltf_parse_json_materials(options, tokens, i + 1, json_chunk, out_data);
6451 }
6452 else if (cgltf_json_strcmp(tokens + i, json_chunk, "images") == 0)
6453 {
6454 i = cgltf_parse_json_images(options, tokens, i + 1, json_chunk, out_data);
6455 }
6456 else if (cgltf_json_strcmp(tokens + i, json_chunk, "textures") == 0)
6457 {
6458 i = cgltf_parse_json_textures(options, tokens, i + 1, json_chunk, out_data);
6459 }
6460 else if (cgltf_json_strcmp(tokens + i, json_chunk, "samplers") == 0)
6461 {
6462 i = cgltf_parse_json_samplers(options, tokens, i + 1, json_chunk, out_data);
6463 }
6464 else if (cgltf_json_strcmp(tokens + i, json_chunk, "skins") == 0)
6465 {
6466 i = cgltf_parse_json_skins(options, tokens, i + 1, json_chunk, out_data);
6467 }
6468 else if (cgltf_json_strcmp(tokens + i, json_chunk, "cameras") == 0)
6469 {
6470 i = cgltf_parse_json_cameras(options, tokens, i + 1, json_chunk, out_data);
6471 }
6472 else if (cgltf_json_strcmp(tokens + i, json_chunk, "nodes") == 0)
6473 {
6474 i = cgltf_parse_json_nodes(options, tokens, i + 1, json_chunk, out_data);
6475 }
6476 else if (cgltf_json_strcmp(tokens + i, json_chunk, "scenes") == 0)
6477 {
6478 i = cgltf_parse_json_scenes(options, tokens, i + 1, json_chunk, out_data);
6479 }
6480 else if (cgltf_json_strcmp(tokens + i, json_chunk, "scene") == 0)
6481 {
6482 ++i;
6483 out_data->scene = CGLTF_PTRINDEX(cgltf_scene, cgltf_json_to_int(tokens + i, json_chunk));
6484 ++i;
6485 }
6486 else if (cgltf_json_strcmp(tokens + i, json_chunk, "animations") == 0)
6487 {
6488 i = cgltf_parse_json_animations(options, tokens, i + 1, json_chunk, out_data);
6489 }
6490 else if (cgltf_json_strcmp(tokens+i, json_chunk, "extras") == 0)
6491 {
6492 i = cgltf_parse_json_extras(options, tokens, i + 1, json_chunk, &out_data->extras);
6493 }
6494 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensions") == 0)
6495 {
6496 ++i;
6497
6498 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
6499 if(out_data->data_extensions)
6500 {
6501 return CGLTF_ERROR_JSON;
6502 }
6503
6504 int extensions_size = tokens[i].size;
6505 out_data->data_extensions_count = 0;
6506 out_data->data_extensions = (cgltf_extension*)cgltf_calloc(options, sizeof(cgltf_extension), extensions_size);
6507
6508 if (!out_data->data_extensions)
6509 {
6510 return CGLTF_ERROR_NOMEM;
6511 }
6512
6513 ++i;
6514
6515 for (int k = 0; k < extensions_size; ++k)
6516 {
6517 CGLTF_CHECK_KEY(tokens[i]);
6518
6519 if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_lights_punctual") == 0)
6520 {
6521 ++i;
6522
6523 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
6524
6525 int data_size = tokens[i].size;
6526 ++i;
6527
6528 for (int m = 0; m < data_size; ++m)
6529 {
6530 CGLTF_CHECK_KEY(tokens[i]);
6531
6532 if (cgltf_json_strcmp(tokens + i, json_chunk, "lights") == 0)
6533 {
6534 i = cgltf_parse_json_lights(options, tokens, i + 1, json_chunk, out_data);
6535 }
6536 else
6537 {
6538 i = cgltf_skip_json(tokens, i + 1);
6539 }
6540
6541 if (i < 0)
6542 {
6543 return i;
6544 }
6545 }
6546 }
6547 else if (cgltf_json_strcmp(tokens+i, json_chunk, "KHR_materials_variants") == 0)
6548 {
6549 ++i;
6550
6551 CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT);
6552
6553 int data_size = tokens[i].size;
6554 ++i;
6555
6556 for (int m = 0; m < data_size; ++m)
6557 {
6558 CGLTF_CHECK_KEY(tokens[i]);
6559
6560 if (cgltf_json_strcmp(tokens + i, json_chunk, "variants") == 0)
6561 {
6562 i = cgltf_parse_json_variants(options, tokens, i + 1, json_chunk, out_data);
6563 }
6564 else
6565 {
6566 i = cgltf_skip_json(tokens, i + 1);
6567 }
6568
6569 if (i < 0)
6570 {
6571 return i;
6572 }
6573 }
6574 }
6575 else
6576 {
6577 i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_data->data_extensions[out_data->data_extensions_count++]));
6578 }
6579
6580 if (i < 0)
6581 {
6582 return i;
6583 }
6584 }
6585 }
6586 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensionsUsed") == 0)
6587 {
6588 i = cgltf_parse_json_string_array(options, tokens, i + 1, json_chunk, &out_data->extensions_used, &out_data->extensions_used_count);
6589 }
6590 else if (cgltf_json_strcmp(tokens + i, json_chunk, "extensionsRequired") == 0)
6591 {
6592 i = cgltf_parse_json_string_array(options, tokens, i + 1, json_chunk, &out_data->extensions_required, &out_data->extensions_required_count);
6593 }
6594 else
6595 {
6596 i = cgltf_skip_json(tokens, i + 1);
6597 }
6598
6599 if (i < 0)
6600 {
6601 return i;
6602 }
6603 }
6604
6605 return i;
6606 }
6607
6608 cgltf_result cgltf_parse_json(cgltf_options* options, const uint8_t* json_chunk, cgltf_size size, cgltf_data** out_data)
6609 {
6610 jsmn_parser parser = { 0, 0, 0 };
6611
6612 if (options->json_token_count == 0)
6613 {
6614 int token_count = jsmn_parse(&parser, (const char*)json_chunk, size, NULL, 0);
6615
6616 if (token_count <= 0)
6617 {
6618 return cgltf_result_invalid_json;
6619 }
6620
6621 options->json_token_count = token_count;
6622 }
6623
6624 jsmntok_t* tokens = (jsmntok_t*)options->memory.alloc_func(options->memory.user_data, sizeof(jsmntok_t) * (options->json_token_count + 1));
6625
6626 if (!tokens)
6627 {
6628 return cgltf_result_out_of_memory;
6629 }
6630
6631 jsmn_init(&parser);
6632
6633 int token_count = jsmn_parse(&parser, (const char*)json_chunk, size, tokens, options->json_token_count);
6634
6635 if (token_count <= 0)
6636 {
6637 options->memory.free_func(options->memory.user_data, tokens);
6638 return cgltf_result_invalid_json;
6639 }
6640
6641 // this makes sure that we always have an UNDEFINED token at the end of the stream
6642 // for invalid JSON inputs this makes sure we don't perform out of bound reads of token data
6643 tokens[token_count].type = JSMN_UNDEFINED;
6644
6645 cgltf_data* data = (cgltf_data*)options->memory.alloc_func(options->memory.user_data, sizeof(cgltf_data));
6646
6647 if (!data)
6648 {
6649 options->memory.free_func(options->memory.user_data, tokens);
6650 return cgltf_result_out_of_memory;
6651 }
6652
6653 memset(data, 0, sizeof(cgltf_data));
6654 data->memory = options->memory;
6655 data->file = options->file;
6656
6657 int i = cgltf_parse_json_root(options, tokens, 0, json_chunk, data);
6658
6659 options->memory.free_func(options->memory.user_data, tokens);
6660
6661 if (i < 0)
6662 {
6663 cgltf_free(data);
6664
6665 switch (i)
6666 {
6667 case CGLTF_ERROR_NOMEM: return cgltf_result_out_of_memory;
6668 case CGLTF_ERROR_LEGACY: return cgltf_result_legacy_gltf;
6669 default: return cgltf_result_invalid_gltf;
6670 }
6671 }
6672
6673 if (cgltf_fixup_pointers(data) < 0)
6674 {
6675 cgltf_free(data);
6676 return cgltf_result_invalid_gltf;
6677 }
6678
6679 data->json = (const char*)json_chunk;
6680 data->json_size = size;
6681
6682 *out_data = data;
6683
6684 return cgltf_result_success;
6685 }
6686
6687 static int cgltf_fixup_pointers(cgltf_data* data)
6688 {
6689 for (cgltf_size i = 0; i < data->meshes_count; ++i)
6690 {
6691 for (cgltf_size j = 0; j < data->meshes[i].primitives_count; ++j)
6692 {
6693 CGLTF_PTRFIXUP(data->meshes[i].primitives[j].indices, data->accessors, data->accessors_count);
6694 CGLTF_PTRFIXUP(data->meshes[i].primitives[j].material, data->materials, data->materials_count);
6695
6696 for (cgltf_size k = 0; k < data->meshes[i].primitives[j].attributes_count; ++k)
6697 {
6698 CGLTF_PTRFIXUP_REQ(data->meshes[i].primitives[j].attributes[k].data, data->accessors, data->accessors_count);
6699 }
6700
6701 for (cgltf_size k = 0; k < data->meshes[i].primitives[j].targets_count; ++k)
6702 {
6703 for (cgltf_size m = 0; m < data->meshes[i].primitives[j].targets[k].attributes_count; ++m)
6704 {
6705 CGLTF_PTRFIXUP_REQ(data->meshes[i].primitives[j].targets[k].attributes[m].data, data->accessors, data->accessors_count);
6706 }
6707 }
6708
6709 if (data->meshes[i].primitives[j].has_draco_mesh_compression)
6710 {
6711 CGLTF_PTRFIXUP_REQ(data->meshes[i].primitives[j].draco_mesh_compression.buffer_view, data->buffer_views, data->buffer_views_count);
6712 for (cgltf_size m = 0; m < data->meshes[i].primitives[j].draco_mesh_compression.attributes_count; ++m)
6713 {
6714 CGLTF_PTRFIXUP_REQ(data->meshes[i].primitives[j].draco_mesh_compression.attributes[m].data, data->accessors, data->accessors_count);
6715 }
6716 }
6717
6718 for (cgltf_size k = 0; k < data->meshes[i].primitives[j].mappings_count; ++k)
6719 {
6720 CGLTF_PTRFIXUP_REQ(data->meshes[i].primitives[j].mappings[k].material, data->materials, data->materials_count);
6721 }
6722 }
6723 }
6724
6725 for (cgltf_size i = 0; i < data->accessors_count; ++i)
6726 {
6727 CGLTF_PTRFIXUP(data->accessors[i].buffer_view, data->buffer_views, data->buffer_views_count);
6728
6729 if (data->accessors[i].is_sparse)
6730 {
6731 CGLTF_PTRFIXUP_REQ(data->accessors[i].sparse.indices_buffer_view, data->buffer_views, data->buffer_views_count);
6732 CGLTF_PTRFIXUP_REQ(data->accessors[i].sparse.values_buffer_view, data->buffer_views, data->buffer_views_count);
6733 }
6734
6735 if (data->accessors[i].buffer_view)
6736 {
6737 data->accessors[i].stride = data->accessors[i].buffer_view->stride;
6738 }
6739
6740 if (data->accessors[i].stride == 0)
6741 {
6742 data->accessors[i].stride = cgltf_calc_size(data->accessors[i].type, data->accessors[i].component_type);
6743 }
6744 }
6745
6746 for (cgltf_size i = 0; i < data->textures_count; ++i)
6747 {
6748 CGLTF_PTRFIXUP(data->textures[i].image, data->images, data->images_count);
6749 CGLTF_PTRFIXUP(data->textures[i].basisu_image, data->images, data->images_count);
6750 CGLTF_PTRFIXUP(data->textures[i].webp_image, data->images, data->images_count);
6751 CGLTF_PTRFIXUP(data->textures[i].sampler, data->samplers, data->samplers_count);
6752 }
6753
6754 for (cgltf_size i = 0; i < data->images_count; ++i)
6755 {
6756 CGLTF_PTRFIXUP(data->images[i].buffer_view, data->buffer_views, data->buffer_views_count);
6757 }
6758
6759 for (cgltf_size i = 0; i < data->materials_count; ++i)
6760 {
6761 CGLTF_PTRFIXUP(data->materials[i].normal_texture.texture, data->textures, data->textures_count);
6762 CGLTF_PTRFIXUP(data->materials[i].emissive_texture.texture, data->textures, data->textures_count);
6763 CGLTF_PTRFIXUP(data->materials[i].occlusion_texture.texture, data->textures, data->textures_count);
6764
6765 CGLTF_PTRFIXUP(data->materials[i].pbr_metallic_roughness.base_color_texture.texture, data->textures, data->textures_count);
6766 CGLTF_PTRFIXUP(data->materials[i].pbr_metallic_roughness.metallic_roughness_texture.texture, data->textures, data->textures_count);
6767
6768 CGLTF_PTRFIXUP(data->materials[i].pbr_specular_glossiness.diffuse_texture.texture, data->textures, data->textures_count);
6769 CGLTF_PTRFIXUP(data->materials[i].pbr_specular_glossiness.specular_glossiness_texture.texture, data->textures, data->textures_count);
6770
6771 CGLTF_PTRFIXUP(data->materials[i].clearcoat.clearcoat_texture.texture, data->textures, data->textures_count);
6772 CGLTF_PTRFIXUP(data->materials[i].clearcoat.clearcoat_roughness_texture.texture, data->textures, data->textures_count);
6773 CGLTF_PTRFIXUP(data->materials[i].clearcoat.clearcoat_normal_texture.texture, data->textures, data->textures_count);
6774
6775 CGLTF_PTRFIXUP(data->materials[i].specular.specular_texture.texture, data->textures, data->textures_count);
6776 CGLTF_PTRFIXUP(data->materials[i].specular.specular_color_texture.texture, data->textures, data->textures_count);
6777
6778 CGLTF_PTRFIXUP(data->materials[i].transmission.transmission_texture.texture, data->textures, data->textures_count);
6779
6780 CGLTF_PTRFIXUP(data->materials[i].volume.thickness_texture.texture, data->textures, data->textures_count);
6781
6782 CGLTF_PTRFIXUP(data->materials[i].sheen.sheen_color_texture.texture, data->textures, data->textures_count);
6783 CGLTF_PTRFIXUP(data->materials[i].sheen.sheen_roughness_texture.texture, data->textures, data->textures_count);
6784
6785 CGLTF_PTRFIXUP(data->materials[i].iridescence.iridescence_texture.texture, data->textures, data->textures_count);
6786 CGLTF_PTRFIXUP(data->materials[i].iridescence.iridescence_thickness_texture.texture, data->textures, data->textures_count);
6787
6788 CGLTF_PTRFIXUP(data->materials[i].diffuse_transmission.diffuse_transmission_texture.texture, data->textures, data->textures_count);
6789 CGLTF_PTRFIXUP(data->materials[i].diffuse_transmission.diffuse_transmission_color_texture.texture, data->textures, data->textures_count);
6790
6791 CGLTF_PTRFIXUP(data->materials[i].anisotropy.anisotropy_texture.texture, data->textures, data->textures_count);
6792 }
6793
6794 for (cgltf_size i = 0; i < data->buffer_views_count; ++i)
6795 {
6796 CGLTF_PTRFIXUP_REQ(data->buffer_views[i].buffer, data->buffers, data->buffers_count);
6797
6798 if (data->buffer_views[i].has_meshopt_compression)
6799 {
6800 CGLTF_PTRFIXUP_REQ(data->buffer_views[i].meshopt_compression.buffer, data->buffers, data->buffers_count);
6801 }
6802 }
6803
6804 for (cgltf_size i = 0; i < data->skins_count; ++i)
6805 {
6806 for (cgltf_size j = 0; j < data->skins[i].joints_count; ++j)
6807 {
6808 CGLTF_PTRFIXUP_REQ(data->skins[i].joints[j], data->nodes, data->nodes_count);
6809 }
6810
6811 CGLTF_PTRFIXUP(data->skins[i].skeleton, data->nodes, data->nodes_count);
6812 CGLTF_PTRFIXUP(data->skins[i].inverse_bind_matrices, data->accessors, data->accessors_count);
6813 }
6814
6815 for (cgltf_size i = 0; i < data->nodes_count; ++i)
6816 {
6817 for (cgltf_size j = 0; j < data->nodes[i].children_count; ++j)
6818 {
6819 CGLTF_PTRFIXUP_REQ(data->nodes[i].children[j], data->nodes, data->nodes_count);
6820
6821 if (data->nodes[i].children[j]->parent)
6822 {
6823 return CGLTF_ERROR_JSON;
6824 }
6825
6826 data->nodes[i].children[j]->parent = &data->nodes[i];
6827 }
6828
6829 CGLTF_PTRFIXUP(data->nodes[i].mesh, data->meshes, data->meshes_count);
6830 CGLTF_PTRFIXUP(data->nodes[i].skin, data->skins, data->skins_count);
6831 CGLTF_PTRFIXUP(data->nodes[i].camera, data->cameras, data->cameras_count);
6832 CGLTF_PTRFIXUP(data->nodes[i].light, data->lights, data->lights_count);
6833
6834 if (data->nodes[i].has_mesh_gpu_instancing)
6835 {
6836 for (cgltf_size m = 0; m < data->nodes[i].mesh_gpu_instancing.attributes_count; ++m)
6837 {
6838 CGLTF_PTRFIXUP_REQ(data->nodes[i].mesh_gpu_instancing.attributes[m].data, data->accessors, data->accessors_count);
6839 }
6840 }
6841 }
6842
6843 for (cgltf_size i = 0; i < data->scenes_count; ++i)
6844 {
6845 for (cgltf_size j = 0; j < data->scenes[i].nodes_count; ++j)
6846 {
6847 CGLTF_PTRFIXUP_REQ(data->scenes[i].nodes[j], data->nodes, data->nodes_count);
6848
6849 if (data->scenes[i].nodes[j]->parent)
6850 {
6851 return CGLTF_ERROR_JSON;
6852 }
6853 }
6854 }
6855
6856 CGLTF_PTRFIXUP(data->scene, data->scenes, data->scenes_count);
6857
6858 for (cgltf_size i = 0; i < data->animations_count; ++i)
6859 {
6860 for (cgltf_size j = 0; j < data->animations[i].samplers_count; ++j)
6861 {
6862 CGLTF_PTRFIXUP_REQ(data->animations[i].samplers[j].input, data->accessors, data->accessors_count);
6863 CGLTF_PTRFIXUP_REQ(data->animations[i].samplers[j].output, data->accessors, data->accessors_count);
6864 }
6865
6866 for (cgltf_size j = 0; j < data->animations[i].channels_count; ++j)
6867 {
6868 CGLTF_PTRFIXUP_REQ(data->animations[i].channels[j].sampler, data->animations[i].samplers, data->animations[i].samplers_count);
6869 CGLTF_PTRFIXUP(data->animations[i].channels[j].target_node, data->nodes, data->nodes_count);
6870 }
6871 }
6872
6873 return 0;
6874 }
6875
6876 /*
6877 * -- jsmn.c start --
6878 * Source: https://github.com/zserge/jsmn
6879 * License: MIT
6880 *
6881 * Copyright (c) 2010 Serge A. Zaitsev
6882
6883 * Permission is hereby granted, free of charge, to any person obtaining a copy
6884 * of this software and associated documentation files (the "Software"), to deal
6885 * in the Software without restriction, including without limitation the rights
6886 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
6887 * copies of the Software, and to permit persons to whom the Software is
6888 * furnished to do so, subject to the following conditions:
6889
6890 * The above copyright notice and this permission notice shall be included in
6891 * all copies or substantial portions of the Software.
6892
6893 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6894 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6895 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
6896 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
6897 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
6898 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
6899 * THE SOFTWARE.
6900 */
6901
6902 /**
6903 * Allocates a fresh unused token from the token pull.
6904 */
6905 static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser,
6906 jsmntok_t *tokens, size_t num_tokens) {
6907 jsmntok_t *tok;
6908 if (parser->toknext >= num_tokens) {
6909 return NULL;
6910 }
6911 tok = &tokens[parser->toknext++];
6912 tok->start = tok->end = -1;
6913 tok->size = 0;
6914 #ifdef JSMN_PARENT_LINKS
6915 tok->parent = -1;
6916 #endif
6917 return tok;
6918 }
6919
6920 /**
6921 * Fills token type and boundaries.
6922 */
6923 static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type,
6924 ptrdiff_t start, ptrdiff_t end) {
6925 token->type = type;
6926 token->start = start;
6927 token->end = end;
6928 token->size = 0;
6929 }
6930
6931 /**
6932 * Fills next available token with JSON primitive.
6933 */
6934 static int jsmn_parse_primitive(jsmn_parser *parser, const char *js,
6935 size_t len, jsmntok_t *tokens, size_t num_tokens) {
6936 jsmntok_t *token;
6937 ptrdiff_t start;
6938
6939 start = parser->pos;
6940
6941 for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
6942 switch (js[parser->pos]) {
6943 #ifndef JSMN_STRICT
6944 /* In strict mode primitive must be followed by "," or "}" or "]" */
6945 case ':':
6946 #endif
6947 case '\t' : case '\r' : case '\n' : case ' ' :
6948 case ',' : case ']' : case '}' :
6949 goto found;
6950 }
6951 if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
6952 parser->pos = start;
6953 return JSMN_ERROR_INVAL;
6954 }
6955 }
6956 #ifdef JSMN_STRICT
6957 /* In strict mode primitive must be followed by a comma/object/array */
6958 parser->pos = start;
6959 return JSMN_ERROR_PART;
6960 #endif
6961
6962 found:
6963 if (tokens == NULL) {
6964 parser->pos--;
6965 return 0;
6966 }
6967 token = jsmn_alloc_token(parser, tokens, num_tokens);
6968 if (token == NULL) {
6969 parser->pos = start;
6970 return JSMN_ERROR_NOMEM;
6971 }
6972 jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
6973 #ifdef JSMN_PARENT_LINKS
6974 token->parent = parser->toksuper;
6975 #endif
6976 parser->pos--;
6977 return 0;
6978 }
6979
6980 /**
6981 * Fills next token with JSON string.
6982 */
6983 static int jsmn_parse_string(jsmn_parser *parser, const char *js,
6984 size_t len, jsmntok_t *tokens, size_t num_tokens) {
6985 jsmntok_t *token;
6986
6987 ptrdiff_t start = parser->pos;
6988
6989 parser->pos++;
6990
6991 /* Skip starting quote */
6992 for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
6993 char c = js[parser->pos];
6994
6995 /* Quote: end of string */
6996 if (c == '\"') {
6997 if (tokens == NULL) {
6998 return 0;
6999 }
7000 token = jsmn_alloc_token(parser, tokens, num_tokens);
7001 if (token == NULL) {
7002 parser->pos = start;
7003 return JSMN_ERROR_NOMEM;
7004 }
7005 jsmn_fill_token(token, JSMN_STRING, start+1, parser->pos);
7006 #ifdef JSMN_PARENT_LINKS
7007 token->parent = parser->toksuper;
7008 #endif
7009 return 0;
7010 }
7011
7012 /* Backslash: Quoted symbol expected */
7013 if (c == '\\' && parser->pos + 1 < len) {
7014 int i;
7015 parser->pos++;
7016 switch (js[parser->pos]) {
7017 /* Allowed escaped symbols */
7018 case '\"': case '/' : case '\\' : case 'b' :
7019 case 'f' : case 'r' : case 'n' : case 't' :
7020 break;
7021 /* Allows escaped symbol \uXXXX */
7022 case 'u':
7023 parser->pos++;
7024 for(i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0'; i++) {
7025 /* If it isn't a hex character we have an error */
7026 if(!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */
7027 (js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */
7028 (js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */
7029 parser->pos = start;
7030 return JSMN_ERROR_INVAL;
7031 }
7032 parser->pos++;
7033 }
7034 parser->pos--;
7035 break;
7036 /* Unexpected symbol */
7037 default:
7038 parser->pos = start;
7039 return JSMN_ERROR_INVAL;
7040 }
7041 }
7042 }
7043 parser->pos = start;
7044 return JSMN_ERROR_PART;
7045 }
7046
7047 /**
7048 * Parse JSON string and fill tokens.
7049 */
7050 static int jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
7051 jsmntok_t *tokens, size_t num_tokens) {
7052 int r;
7053 int i;
7054 jsmntok_t *token;
7055 int count = parser->toknext;
7056
7057 for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
7058 char c;
7059 jsmntype_t type;
7060
7061 c = js[parser->pos];
7062 switch (c) {
7063 case '{': case '[':
7064 count++;
7065 if (tokens == NULL) {
7066 break;
7067 }
7068 token = jsmn_alloc_token(parser, tokens, num_tokens);
7069 if (token == NULL)
7070 return JSMN_ERROR_NOMEM;
7071 if (parser->toksuper != -1) {
7072 tokens[parser->toksuper].size++;
7073 #ifdef JSMN_PARENT_LINKS
7074 token->parent = parser->toksuper;
7075 #endif
7076 }
7077 token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
7078 token->start = parser->pos;
7079 parser->toksuper = parser->toknext - 1;
7080 break;
7081 case '}': case ']':
7082 if (tokens == NULL)
7083 break;
7084 type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
7085 #ifdef JSMN_PARENT_LINKS
7086 if (parser->toknext < 1) {
7087 return JSMN_ERROR_INVAL;
7088 }
7089 token = &tokens[parser->toknext - 1];
7090 for (;;) {
7091 if (token->start != -1 && token->end == -1) {
7092 if (token->type != type) {
7093 return JSMN_ERROR_INVAL;
7094 }
7095 token->end = parser->pos + 1;
7096 parser->toksuper = token->parent;
7097 break;
7098 }
7099 if (token->parent == -1) {
7100 if(token->type != type || parser->toksuper == -1) {
7101 return JSMN_ERROR_INVAL;
7102 }
7103 break;
7104 }
7105 token = &tokens[token->parent];
7106 }
7107 #else
7108 for (i = parser->toknext - 1; i >= 0; i--) {
7109 token = &tokens[i];
7110 if (token->start != -1 && token->end == -1) {
7111 if (token->type != type) {
7112 return JSMN_ERROR_INVAL;
7113 }
7114 parser->toksuper = -1;
7115 token->end = parser->pos + 1;
7116 break;
7117 }
7118 }
7119 /* Error if unmatched closing bracket */
7120 if (i == -1) return JSMN_ERROR_INVAL;
7121 for (; i >= 0; i--) {
7122 token = &tokens[i];
7123 if (token->start != -1 && token->end == -1) {
7124 parser->toksuper = i;
7125 break;
7126 }
7127 }
7128 #endif
7129 break;
7130 case '\"':
7131 r = jsmn_parse_string(parser, js, len, tokens, num_tokens);
7132 if (r < 0) return r;
7133 count++;
7134 if (parser->toksuper != -1 && tokens != NULL)
7135 tokens[parser->toksuper].size++;
7136 break;
7137 case '\t' : case '\r' : case '\n' : case ' ':
7138 break;
7139 case ':':
7140 parser->toksuper = parser->toknext - 1;
7141 break;
7142 case ',':
7143 if (tokens != NULL && parser->toksuper != -1 &&
7144 tokens[parser->toksuper].type != JSMN_ARRAY &&
7145 tokens[parser->toksuper].type != JSMN_OBJECT) {
7146 #ifdef JSMN_PARENT_LINKS
7147 parser->toksuper = tokens[parser->toksuper].parent;
7148 #else
7149 for (i = parser->toknext - 1; i >= 0; i--) {
7150 if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT) {
7151 if (tokens[i].start != -1 && tokens[i].end == -1) {
7152 parser->toksuper = i;
7153 break;
7154 }
7155 }
7156 }
7157 #endif
7158 }
7159 break;
7160 #ifdef JSMN_STRICT
7161 /* In strict mode primitives are: numbers and booleans */
7162 case '-': case '0': case '1' : case '2': case '3' : case '4':
7163 case '5': case '6': case '7' : case '8': case '9':
7164 case 't': case 'f': case 'n' :
7165 /* And they must not be keys of the object */
7166 if (tokens != NULL && parser->toksuper != -1) {
7167 jsmntok_t *t = &tokens[parser->toksuper];
7168 if (t->type == JSMN_OBJECT ||
7169 (t->type == JSMN_STRING && t->size != 0)) {
7170 return JSMN_ERROR_INVAL;
7171 }
7172 }
7173 #else
7174 /* In non-strict mode every unquoted value is a primitive */
7175 default:
7176 #endif
7177 r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens);
7178 if (r < 0) return r;
7179 count++;
7180 if (parser->toksuper != -1 && tokens != NULL)
7181 tokens[parser->toksuper].size++;
7182 break;
7183
7184 #ifdef JSMN_STRICT
7185 /* Unexpected char in strict mode */
7186 default:
7187 return JSMN_ERROR_INVAL;
7188 #endif
7189 }
7190 }
7191
7192 if (tokens != NULL) {
7193 for (i = parser->toknext - 1; i >= 0; i--) {
7194 /* Unmatched opened object or array */
7195 if (tokens[i].start != -1 && tokens[i].end == -1) {
7196 return JSMN_ERROR_PART;
7197 }
7198 }
7199 }
7200
7201 return count;
7202 }
7203
7204 /**
7205 * Creates a new parser based over a given buffer with an array of tokens
7206 * available.
7207 */
7208 static void jsmn_init(jsmn_parser *parser) {
7209 parser->pos = 0;
7210 parser->toknext = 0;
7211 parser->toksuper = -1;
7212 }
7213 /*
7214 * -- jsmn.c end --
7215 */
7216
7217 #endif /* #ifdef CGLTF_IMPLEMENTATION */
7218
7219 /* cgltf is distributed under MIT license:
7220 *
7221 * Copyright (c) 2018-2021 Johannes Kuhlmann
7222
7223 * Permission is hereby granted, free of charge, to any person obtaining a copy
7224 * of this software and associated documentation files (the "Software"), to deal
7225 * in the Software without restriction, including without limitation the rights
7226 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7227 * copies of the Software, and to permit persons to whom the Software is
7228 * furnished to do so, subject to the following conditions:
7229
7230 * The above copyright notice and this permission notice shall be included in all
7231 * copies or substantial portions of the Software.
7232
7233 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
7234 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7235 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7236 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
7237 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
7238 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
7239 * SOFTWARE.
7240 */