Debian Codegraph @

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