diff --git a/3rdparty/cgltf/.travis.yml b/3rdparty/cgltf/.travis.yml deleted file mode 100644 index 058a45724..000000000 --- a/3rdparty/cgltf/.travis.yml +++ /dev/null @@ -1,13 +0,0 @@ -os: - - linux - - osx - - windows -language: c -compiler: - - gcc - - clang -sudo: false -script: - - cd test/ - - ./test_all.py - diff --git a/3rdparty/cgltf/README.md b/3rdparty/cgltf/README.md deleted file mode 100644 index 52c9ebf3b..000000000 --- a/3rdparty/cgltf/README.md +++ /dev/null @@ -1,141 +0,0 @@ -# cgltf -**Single-file/stb-style C glTF loader and writer** - -[![Build Status](https://travis-ci.org/jkuhlmann/cgltf.svg?branch=master)](https://travis-ci.org/jkuhlmann/cgltf) - -## Usage: Loading -Loading from file: -```c -#include "cgltf.h" - -cgltf_options options = {0}; -cgltf_data* data = NULL; -cgltf_result result = cgltf_parse_file(&options, "scene.gltf", &data); -if (result == cgltf_result_success) -{ - /* TODO make awesome stuff */ - cgltf_free(data); -} -``` - -Loading from memory: -```c -#include "cgltf.h" - -void* buf; /* Pointer to glb or gltf file data */ -size_t size; /* Size of the file data */ - -cgltf_options options = {0}; -cgltf_data* data = NULL; -cgltf_result result = cgltf_parse(&options, buf, size, &data); -if (result == cgltf_result_success) -{ - /* TODO make awesome stuff */ - cgltf_free(data); -} -``` - -Note that cgltf does not load the contents of extra files such as buffers or images into memory by default. You'll need to read these files yourself using URIs from `data.buffers[]` or `data.images[]` respectively. -For buffer data, you can alternatively call `cgltf_load_buffers`, which will use `FILE*` APIs to open and read buffer files. - -**For more in-depth documentation and a description of the public interface refer to the top of the `cgltf.h` file.** - -## Usage: Writing -When writing glTF data, you need a valid `cgltf_data` structure that represents a valid glTF document. You can construct such a structure yourself or load it using the loader functions described above. The writer functions do not deallocate any memory. So, you either have to do it manually or call `cgltf_free()` if you got the data by loading it from a glTF document. - -Writing to file: -```c -#include "cgltf_write.h" - -cgltf_options options = {0}; -cgltf_data* data = /* TODO must be valid data */; -cgltf_result result = cgltf_write_file(&options, "out.gltf", data); -if (result != cgltf_result_success) -{ - /* TODO handle error */ -} -``` - -Writing to memory: -```c -#include "cgltf_write.h" -cgltf_options options = {0}; -cgltf_data* data = /* TODO must be valid data */; - -cgltf_size size = cgltf_write(&options, NULL, 0, data); - -char* buf = malloc(size); - -cgltf_size written = cgltf_write(&options, buf, size, data); -if (written != size) -{ - /* TODO handle error */ -} -``` - -Note that cgltf does not write the contents of extra files such as buffers or images. You'll need to write this data yourself. - -Writing does not yet support "extras" data. - -**For more in-depth documentation and a description of the public interface refer to the top of the `cgltf_write.h` file.** - - -## Features -cgltf supports core glTF 2.0: -- glb (binary files) and gltf (JSON files) -- meshes (including accessors, buffer views, buffers) -- materials (including textures, samplers, images) -- scenes and nodes -- skins -- animations -- cameras -- morph targets -- extras data - -cgltf also supports some glTF extensions: -- KHR_lights_punctual -- KHR_materials_pbrSpecularGlossiness -- KHR_materials_unlit -- KHR_texture_transform - -cgltf does **not** yet support unlisted extensions. - -## Building -The easiest approach is to integrate the `cgltf.h` header file into your project. If you are unfamiliar with single-file C libraries (also known as stb-style libraries), this is how it goes: - -1. Include `cgltf.h` where you need the functionality. -1. Have exactly one source file that defines `CGLTF_IMPLEMENTATION` before including `cgltf.h`. -1. Use the cgltf functions as described above. - -Support for writing can be found in a separate file called `cgltf_write.h` (which includes `cgltf.h`). Building it works analogously using the `CGLTF_WRITE_IMPLEMENTATION` define. - -## Contributing -Everyone is welcome to contribute to the library. If you find any problems, you can submit them using [GitHub's issue system](https://github.com/jkuhlmann/cgltf/issues). If you want to contribute code, you should fork the project and then send a pull request. - - -## Dependencies -None. - -C headers being used by implementation: -``` -#include -#include -#include -#include -#include -#include -``` - -Note, this library has a copy of the [JSMN JSON parser](https://github.com/zserge/jsmn) embedded in its source. - -## Testing -There is a Python script in the `test/` folder that retrieves the glTF 2.0 sample files from the glTF-Sample-Models repository (https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0) and runs the library against all gltf and glb files. - -Here's one way to build and run the test: - - cd test ; mkdir build ; cd build ; cmake .. -DCMAKE_BUILD_TYPE=Debug - make -j - cd .. - ./test_all.py - -There is also a llvm-fuzz test in `fuzz/`. See http://llvm.org/docs/LibFuzzer.html for more information. diff --git a/3rdparty/cgltf/cgltf.h b/3rdparty/cgltf/cgltf.h index 4ad7ef078..3cdfee197 100644 --- a/3rdparty/cgltf/cgltf.h +++ b/3rdparty/cgltf/cgltf.h @@ -1,7 +1,7 @@ /** * cgltf - a single-file glTF 2.0 parser written in C99. * - * Version: 1.3 + * Version: 1.5 * * Website: https://github.com/jkuhlmann/cgltf * @@ -24,8 +24,8 @@ * * `cgltf_options` is the struct passed to `cgltf_parse()` to control * parts of the parsing process. You can use it to force the file type - * and provide memory allocation callbacks. Should be zero-initialized - * to trigger default behavior. + * and provide memory allocation as well as file operation callbacks. + * Should be zero-initialized to trigger default behavior. * * `cgltf_data` is the struct allocated and filled by `cgltf_parse()`. * It generally mirrors the glTF format as described by the spec (see @@ -72,6 +72,11 @@ * size is the number of floats in the output buffer, which should be in the range [1, 16]. Returns * false if the passed-in element_size is too small, or if the accessor is sparse. * + * `cgltf_accessor_read_uint` is similar to its floating-point counterpart, but limited to reading + * vector types and does not support matrix types. The passed-in element size is the number of uints + * in the output buffer, which should be in the range [1, 4]. Returns false if the passed-in + * element_size is too small, or if the accessor is sparse. + * * `cgltf_accessor_read_index` is similar to its floating-point counterpart, but it returns size_t * and only works with single-component data types. * @@ -96,6 +101,7 @@ extern "C" { typedef size_t cgltf_size; typedef float cgltf_float; typedef int cgltf_int; +typedef unsigned int cgltf_uint; typedef int cgltf_bool; typedef enum cgltf_file_type @@ -105,15 +111,6 @@ typedef enum cgltf_file_type cgltf_file_type_glb, } cgltf_file_type; -typedef struct cgltf_options -{ - cgltf_file_type type; /* invalid == auto detect */ - cgltf_size json_token_count; /* 0 == auto */ - void* (*memory_alloc)(void* user, cgltf_size size); - void (*memory_free) (void* user, void* ptr); - void* memory_user_data; -} cgltf_options; - typedef enum cgltf_result { cgltf_result_success, @@ -125,8 +122,31 @@ typedef enum cgltf_result cgltf_result_file_not_found, cgltf_result_io_error, cgltf_result_out_of_memory, + cgltf_result_legacy_gltf, } cgltf_result; +typedef struct cgltf_memory_options +{ + void* (*alloc)(void* user, cgltf_size size); + void (*free) (void* user, void* ptr); + void* user_data; +} cgltf_memory_options; + +typedef struct cgltf_file_options +{ + cgltf_result(*read)(const struct cgltf_memory_options* memory_options, const struct cgltf_file_options* file_options, const char* path, cgltf_size* size, void** data); + void (*release)(const struct cgltf_memory_options* memory_options, const struct cgltf_file_options* file_options, void* data); + void* user_data; +} cgltf_file_options; + +typedef struct cgltf_options +{ + cgltf_file_type type; /* invalid == auto detect */ + cgltf_size json_token_count; /* 0 == auto */ + cgltf_memory_options memory; + cgltf_file_options file; +} cgltf_options; + typedef enum cgltf_buffer_view_type { cgltf_buffer_view_type_invalid, @@ -420,7 +440,7 @@ typedef struct cgltf_camera { union { cgltf_camera_perspective perspective; cgltf_camera_orthographic orthographic; - }; + } data; cgltf_extras extras; } cgltf_camera; @@ -559,8 +579,8 @@ typedef struct cgltf_data const void* bin; cgltf_size bin_size; - void (*memory_free) (void* user, void* ptr); - void* memory_user_data; + cgltf_memory_options memory; + cgltf_file_options file; } cgltf_data; cgltf_result cgltf_parse( @@ -590,6 +610,7 @@ void cgltf_node_transform_local(const cgltf_node* node, cgltf_float* out_matrix) void cgltf_node_transform_world(const cgltf_node* node, cgltf_float* out_matrix); cgltf_bool cgltf_accessor_read_float(const cgltf_accessor* accessor, cgltf_size index, cgltf_float* out, cgltf_size element_size); +cgltf_bool cgltf_accessor_read_uint(const cgltf_accessor* accessor, cgltf_size index, cgltf_uint* out, cgltf_size element_size); cgltf_size cgltf_accessor_read_index(const cgltf_accessor* accessor, cgltf_size index); cgltf_size cgltf_num_components(cgltf_type type); @@ -620,10 +641,13 @@ cgltf_result cgltf_copy_extras_json(const cgltf_data* data, const cgltf_extras* #include /* For uint8_t, uint32_t */ #include /* For strncpy */ -#include /* For malloc, free */ #include /* For fopen */ #include /* For UINT_MAX etc */ +#if !defined(CGLTF_MALLOC) || !defined(CGLTF_FREE) || !defined(CGLTF_ATOI) || !defined(CGLTF_ATOF) +#include /* For malloc, free, atoi, atof */ +#endif + /* JSMN_PARENT_LINKS is necessary to make parsing large structures linear in input size */ #define JSMN_PARENT_LINKS @@ -678,16 +702,29 @@ static const uint32_t GlbMagic = 0x46546C67; static const uint32_t GlbMagicJsonChunk = 0x4E4F534A; static const uint32_t GlbMagicBinChunk = 0x004E4942; +#ifndef CGLTF_MALLOC +#define CGLTF_MALLOC(size) malloc(size) +#endif +#ifndef CGLTF_FREE +#define CGLTF_FREE(ptr) free(ptr) +#endif +#ifndef CGLTF_ATOI +#define CGLTF_ATOI(str) atoi(str) +#endif +#ifndef CGLTF_ATOF +#define CGLTF_ATOF(str) atof(str) +#endif + static void* cgltf_default_alloc(void* user, cgltf_size size) { (void)user; - return malloc(size); + return CGLTF_MALLOC(size); } static void cgltf_default_free(void* user, void* ptr) { (void)user; - free(ptr); + CGLTF_FREE(ptr); } static void* cgltf_calloc(cgltf_options* options, size_t element_size, cgltf_size count) @@ -696,7 +733,7 @@ static void* cgltf_calloc(cgltf_options* options, size_t element_size, cgltf_siz { return NULL; } - void* result = options->memory_alloc(options->memory_user_data, element_size * count); + void* result = options->memory.alloc(options->memory.user_data, element_size * count); if (!result) { return NULL; @@ -705,6 +742,71 @@ static void* cgltf_calloc(cgltf_options* options, size_t element_size, cgltf_siz return result; } +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) +{ + (void)file_options; + void* (*memory_alloc)(void*, cgltf_size) = memory_options->alloc ? memory_options->alloc : &cgltf_default_alloc; + void (*memory_free)(void*, void*) = memory_options->free ? memory_options->free : &cgltf_default_free; + + FILE* file = fopen(path, "rb"); + if (!file) + { + return cgltf_result_file_not_found; + } + + cgltf_size file_size = size ? *size : 0; + + if (file_size == 0) + { + fseek(file, 0, SEEK_END); + + long length = ftell(file); + if (length < 0) + { + fclose(file); + return cgltf_result_io_error; + } + + fseek(file, 0, SEEK_SET); + file_size = (cgltf_size)length; + } + + char* file_data = (char*)memory_alloc(memory_options->user_data, file_size); + if (!file_data) + { + fclose(file); + return cgltf_result_out_of_memory; + } + + cgltf_size read_size = fread(file_data, 1, file_size, file); + + fclose(file); + + if (read_size != file_size) + { + memory_free(memory_options->user_data, file_data); + return cgltf_result_io_error; + } + + if (size) + { + *size = file_size; + } + if (data) + { + *data = file_data; + } + + return cgltf_result_success; +} + +static void cgltf_default_file_release(const struct cgltf_memory_options* memory_options, const struct cgltf_file_options* file_options, void* data) +{ + (void)file_options; + void (*memfree)(void*, void*) = memory_options->free ? memory_options->free : &cgltf_default_free; + memfree(memory_options->user_data, data); +} + static cgltf_result cgltf_parse_json(cgltf_options* options, const uint8_t* json_chunk, cgltf_size size, cgltf_data** out_data); cgltf_result cgltf_parse(const cgltf_options* options, const void* data, cgltf_size size, cgltf_data** out_data) @@ -720,13 +822,13 @@ cgltf_result cgltf_parse(const cgltf_options* options, const void* data, cgltf_s } cgltf_options fixed_options = *options; - if (fixed_options.memory_alloc == NULL) + if (fixed_options.memory.alloc == NULL) { - fixed_options.memory_alloc = &cgltf_default_alloc; + fixed_options.memory.alloc = &cgltf_default_alloc; } - if (fixed_options.memory_free == NULL) + if (fixed_options.memory.free == NULL) { - fixed_options.memory_free = &cgltf_default_free; + fixed_options.memory.free = &cgltf_default_free; } uint32_t tmp; @@ -763,7 +865,7 @@ cgltf_result cgltf_parse(const cgltf_options* options, const void* data, cgltf_s uint32_t version = tmp; if (version != GlbVersion) { - return cgltf_result_unknown_format; + return version < GlbVersion ? cgltf_result_legacy_gltf : cgltf_result_unknown_format; } // Total length @@ -846,49 +948,22 @@ cgltf_result cgltf_parse_file(const cgltf_options* options, const char* path, cg return cgltf_result_invalid_options; } - void* (*memory_alloc)(void*, cgltf_size) = options->memory_alloc ? options->memory_alloc : &cgltf_default_alloc; - void (*memory_free)(void*, void*) = options->memory_free ? options->memory_free : &cgltf_default_free; + void (*memory_free)(void*, void*) = options->memory.free ? options->memory.free : &cgltf_default_free; + 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; - FILE* file = fopen(path, "rb"); - if (!file) + void* file_data = NULL; + cgltf_size file_size = 0; + cgltf_result result = file_read(&options->memory, &options->file, path, &file_size, &file_data); + if (result != cgltf_result_success) { - return cgltf_result_file_not_found; + return result; } - fseek(file, 0, SEEK_END); - - long length = ftell(file); - if (length < 0) - { - fclose(file); - return cgltf_result_io_error; - } - - fseek(file, 0, SEEK_SET); - - char* file_data = (char*)memory_alloc(options->memory_user_data, length); - if (!file_data) - { - fclose(file); - return cgltf_result_out_of_memory; - } - - cgltf_size file_size = (cgltf_size)length; - cgltf_size read_size = fread(file_data, 1, file_size, file); - - fclose(file); - - if (read_size != file_size) - { - memory_free(options->memory_user_data, file_data); - return cgltf_result_io_error; - } - - cgltf_result result = cgltf_parse(options, file_data, file_size, out_data); + result = cgltf_parse(options, file_data, file_size, out_data); if (result != cgltf_result_success) { - memory_free(options->memory_user_data, file_data); + memory_free(options->memory.user_data, file_data); return result; } @@ -918,10 +993,10 @@ static void cgltf_combine_paths(char* path, const char* base, const char* uri) static cgltf_result cgltf_load_buffer_file(const cgltf_options* options, cgltf_size size, const char* uri, const char* gltf_path, void** out_data) { - void* (*memory_alloc)(void*, cgltf_size) = options->memory_alloc ? options->memory_alloc : &cgltf_default_alloc; - void (*memory_free)(void*, void*) = options->memory_free ? options->memory_free : &cgltf_default_free; + void* (*memory_alloc)(void*, cgltf_size) = options->memory.alloc ? options->memory.alloc : &cgltf_default_alloc; + 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; - char* path = (char*)memory_alloc(options->memory_user_data, strlen(uri) + strlen(gltf_path) + 1); + char* path = (char*)memory_alloc(options->memory.user_data, strlen(uri) + strlen(gltf_path) + 1); if (!path) { return cgltf_result_out_of_memory; @@ -929,30 +1004,11 @@ static cgltf_result cgltf_load_buffer_file(const cgltf_options* options, cgltf_s cgltf_combine_paths(path, gltf_path, uri); - FILE* file = fopen(path, "rb"); - - memory_free(options->memory_user_data, path); - - if (!file) + void* file_data = NULL; + cgltf_result result = file_read(&options->memory, &options->file, path, &size, &file_data); + if (result != cgltf_result_success) { - return cgltf_result_file_not_found; - } - - char* file_data = (char*)memory_alloc(options->memory_user_data, size); - if (!file_data) - { - fclose(file); - return cgltf_result_out_of_memory; - } - - cgltf_size read_size = fread(file_data, 1, size, file); - - fclose(file); - - if (read_size != size) - { - memory_free(options->memory_user_data, file_data); - return cgltf_result_io_error; + return result; } *out_data = file_data; @@ -962,10 +1018,10 @@ static cgltf_result cgltf_load_buffer_file(const cgltf_options* options, cgltf_s cgltf_result cgltf_load_buffer_base64(const cgltf_options* options, cgltf_size size, const char* base64, void** out_data) { - void* (*memory_alloc)(void*, cgltf_size) = options->memory_alloc ? options->memory_alloc : &cgltf_default_alloc; - void (*memory_free)(void*, void*) = options->memory_free ? options->memory_free : &cgltf_default_free; + void* (*memory_alloc)(void*, cgltf_size) = options->memory.alloc ? options->memory.alloc : &cgltf_default_alloc; + void (*memory_free)(void*, void*) = options->memory.free ? options->memory.free : &cgltf_default_free; - unsigned char* data = (unsigned char*)memory_alloc(options->memory_user_data, size); + unsigned char* data = (unsigned char*)memory_alloc(options->memory.user_data, size); if (!data) { return cgltf_result_out_of_memory; @@ -990,7 +1046,7 @@ cgltf_result cgltf_load_buffer_base64(const cgltf_options* options, cgltf_size s if (index < 0) { - memory_free(options->memory_user_data, data); + memory_free(options->memory.user_data, data); return cgltf_result_io_error; } @@ -1257,6 +1313,66 @@ cgltf_result cgltf_validate(cgltf_data* data) } } + for (cgltf_size i = 0; i < data->nodes_count; ++i) + { + cgltf_node* p1 = data->nodes[i].parent; + cgltf_node* p2 = p1 ? p1->parent : NULL; + + while (p1 && p2) + { + if (p1 == p2) + { + return cgltf_result_invalid_gltf; + } + + p1 = p1->parent; + p2 = p2->parent ? p2->parent->parent : NULL; + } + } + + for (cgltf_size i = 0; i < data->scenes_count; ++i) + { + for (cgltf_size j = 0; j < data->scenes[i].nodes_count; ++j) + { + if (data->scenes[i].nodes[j]->parent) + { + return cgltf_result_invalid_gltf; + } + } + } + + for (cgltf_size i = 0; i < data->animations_count; ++i) + { + for (cgltf_size j = 0; j < data->animations[i].channels_count; ++j) + { + cgltf_animation_channel* channel = &data->animations[i].channels[j]; + + if (!channel->target_node) + { + continue; + } + + cgltf_size components = 1; + + if (channel->target_path == cgltf_animation_path_type_weights) + { + if (!channel->target_node->mesh || !channel->target_node->mesh->primitives_count) + { + return cgltf_result_invalid_gltf; + } + + components = channel->target_node->mesh->primitives[0].targets_count; + } + + cgltf_size values = channel->sampler->interpolation == cgltf_interpolation_type_cubic_spline ? 3 : 1; + + if (channel->sampler->input->count * components * values != channel->sampler->output->count) + { + return cgltf_result_data_too_short; + } + } + } + return cgltf_result_success; } @@ -1295,155 +1411,157 @@ void cgltf_free(cgltf_data* data) return; } - data->memory_free(data->memory_user_data, data->asset.copyright); - data->memory_free(data->memory_user_data, data->asset.generator); - data->memory_free(data->memory_user_data, data->asset.version); - data->memory_free(data->memory_user_data, data->asset.min_version); + 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; - data->memory_free(data->memory_user_data, data->accessors); - data->memory_free(data->memory_user_data, data->buffer_views); + data->memory.free(data->memory.user_data, data->asset.copyright); + data->memory.free(data->memory.user_data, data->asset.generator); + data->memory.free(data->memory.user_data, data->asset.version); + data->memory.free(data->memory.user_data, data->asset.min_version); + + data->memory.free(data->memory.user_data, data->accessors); + data->memory.free(data->memory.user_data, data->buffer_views); for (cgltf_size i = 0; i < data->buffers_count; ++i) { if (data->buffers[i].data != data->bin) { - data->memory_free(data->memory_user_data, data->buffers[i].data); + file_release(&data->memory, &data->file, data->buffers[i].data); } - data->memory_free(data->memory_user_data, data->buffers[i].uri); + data->memory.free(data->memory.user_data, data->buffers[i].uri); } - data->memory_free(data->memory_user_data, data->buffers); + data->memory.free(data->memory.user_data, data->buffers); for (cgltf_size i = 0; i < data->meshes_count; ++i) { - data->memory_free(data->memory_user_data, data->meshes[i].name); + data->memory.free(data->memory.user_data, data->meshes[i].name); for (cgltf_size j = 0; j < data->meshes[i].primitives_count; ++j) { for (cgltf_size k = 0; k < data->meshes[i].primitives[j].attributes_count; ++k) { - data->memory_free(data->memory_user_data, data->meshes[i].primitives[j].attributes[k].name); + data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].attributes[k].name); } - data->memory_free(data->memory_user_data, data->meshes[i].primitives[j].attributes); + data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].attributes); for (cgltf_size k = 0; k < data->meshes[i].primitives[j].targets_count; ++k) { for (cgltf_size m = 0; m < data->meshes[i].primitives[j].targets[k].attributes_count; ++m) { - data->memory_free(data->memory_user_data, data->meshes[i].primitives[j].targets[k].attributes[m].name); + data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].targets[k].attributes[m].name); } - data->memory_free(data->memory_user_data, data->meshes[i].primitives[j].targets[k].attributes); + data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].targets[k].attributes); } - data->memory_free(data->memory_user_data, data->meshes[i].primitives[j].targets); + data->memory.free(data->memory.user_data, data->meshes[i].primitives[j].targets); } - data->memory_free(data->memory_user_data, data->meshes[i].primitives); - data->memory_free(data->memory_user_data, data->meshes[i].weights); + data->memory.free(data->memory.user_data, data->meshes[i].primitives); + data->memory.free(data->memory.user_data, data->meshes[i].weights); for (cgltf_size j = 0; j < data->meshes[i].target_names_count; ++j) { - data->memory_free(data->memory_user_data, data->meshes[i].target_names[j]); + data->memory.free(data->memory.user_data, data->meshes[i].target_names[j]); } - data->memory_free(data->memory_user_data, data->meshes[i].target_names); + data->memory.free(data->memory.user_data, data->meshes[i].target_names); } - data->memory_free(data->memory_user_data, data->meshes); + data->memory.free(data->memory.user_data, data->meshes); for (cgltf_size i = 0; i < data->materials_count; ++i) { - data->memory_free(data->memory_user_data, data->materials[i].name); + data->memory.free(data->memory.user_data, data->materials[i].name); } - data->memory_free(data->memory_user_data, data->materials); + data->memory.free(data->memory.user_data, data->materials); for (cgltf_size i = 0; i < data->images_count; ++i) { - data->memory_free(data->memory_user_data, data->images[i].name); - data->memory_free(data->memory_user_data, data->images[i].uri); - data->memory_free(data->memory_user_data, data->images[i].mime_type); + data->memory.free(data->memory.user_data, data->images[i].name); + data->memory.free(data->memory.user_data, data->images[i].uri); + data->memory.free(data->memory.user_data, data->images[i].mime_type); } - data->memory_free(data->memory_user_data, data->images); + data->memory.free(data->memory.user_data, data->images); for (cgltf_size i = 0; i < data->textures_count; ++i) { - data->memory_free(data->memory_user_data, data->textures[i].name); + data->memory.free(data->memory.user_data, data->textures[i].name); } - data->memory_free(data->memory_user_data, data->textures); + data->memory.free(data->memory.user_data, data->textures); - data->memory_free(data->memory_user_data, data->samplers); + data->memory.free(data->memory.user_data, data->samplers); for (cgltf_size i = 0; i < data->skins_count; ++i) { - data->memory_free(data->memory_user_data, data->skins[i].name); - data->memory_free(data->memory_user_data, data->skins[i].joints); + data->memory.free(data->memory.user_data, data->skins[i].name); + data->memory.free(data->memory.user_data, data->skins[i].joints); } - data->memory_free(data->memory_user_data, data->skins); + data->memory.free(data->memory.user_data, data->skins); for (cgltf_size i = 0; i < data->cameras_count; ++i) { - data->memory_free(data->memory_user_data, data->cameras[i].name); + data->memory.free(data->memory.user_data, data->cameras[i].name); } - data->memory_free(data->memory_user_data, data->cameras); + data->memory.free(data->memory.user_data, data->cameras); for (cgltf_size i = 0; i < data->lights_count; ++i) { - data->memory_free(data->memory_user_data, data->lights[i].name); + data->memory.free(data->memory.user_data, data->lights[i].name); } - data->memory_free(data->memory_user_data, data->lights); + data->memory.free(data->memory.user_data, data->lights); for (cgltf_size i = 0; i < data->nodes_count; ++i) { - data->memory_free(data->memory_user_data, data->nodes[i].name); - data->memory_free(data->memory_user_data, data->nodes[i].children); - data->memory_free(data->memory_user_data, data->nodes[i].weights); + data->memory.free(data->memory.user_data, data->nodes[i].name); + data->memory.free(data->memory.user_data, data->nodes[i].children); + data->memory.free(data->memory.user_data, data->nodes[i].weights); } - data->memory_free(data->memory_user_data, data->nodes); + data->memory.free(data->memory.user_data, data->nodes); for (cgltf_size i = 0; i < data->scenes_count; ++i) { - data->memory_free(data->memory_user_data, data->scenes[i].name); - data->memory_free(data->memory_user_data, data->scenes[i].nodes); + data->memory.free(data->memory.user_data, data->scenes[i].name); + data->memory.free(data->memory.user_data, data->scenes[i].nodes); } - data->memory_free(data->memory_user_data, data->scenes); + data->memory.free(data->memory.user_data, data->scenes); for (cgltf_size i = 0; i < data->animations_count; ++i) { - data->memory_free(data->memory_user_data, data->animations[i].name); - data->memory_free(data->memory_user_data, data->animations[i].samplers); - data->memory_free(data->memory_user_data, data->animations[i].channels); + data->memory.free(data->memory.user_data, data->animations[i].name); + data->memory.free(data->memory.user_data, data->animations[i].samplers); + data->memory.free(data->memory.user_data, data->animations[i].channels); } - data->memory_free(data->memory_user_data, data->animations); + data->memory.free(data->memory.user_data, data->animations); for (cgltf_size i = 0; i < data->extensions_used_count; ++i) { - data->memory_free(data->memory_user_data, data->extensions_used[i]); + data->memory.free(data->memory.user_data, data->extensions_used[i]); } - data->memory_free(data->memory_user_data, data->extensions_used); + data->memory.free(data->memory.user_data, data->extensions_used); for (cgltf_size i = 0; i < data->extensions_required_count; ++i) { - data->memory_free(data->memory_user_data, data->extensions_required[i]); + data->memory.free(data->memory.user_data, data->extensions_required[i]); } - data->memory_free(data->memory_user_data, data->extensions_required); + data->memory.free(data->memory.user_data, data->extensions_required); - data->memory_free(data->memory_user_data, data->file_data); + file_release(&data->memory, &data->file, data->file_data); - data->memory_free(data->memory_user_data, data); + data->memory.free(data->memory.user_data, data); } void cgltf_node_transform_local(const cgltf_node* node, cgltf_float* out_matrix) @@ -1644,6 +1762,10 @@ cgltf_bool cgltf_accessor_read_float(const cgltf_accessor* accessor, cgltf_size memset(out, 0, element_size * sizeof(cgltf_float)); return 1; } + if (accessor->buffer_view->buffer->data == NULL) + { + return 0; + } cgltf_size offset = accessor->offset + accessor->buffer_view->offset; const uint8_t* element = (const uint8_t*) accessor->buffer_view->buffer->data; element += offset + accessor->stride * index; @@ -1668,13 +1790,22 @@ cgltf_size cgltf_accessor_unpack_floats(const cgltf_accessor* accessor, cgltf_fl dense.is_sparse = 0; for (cgltf_size index = 0; index < element_count; index++, dest += floats_per_element) { - cgltf_accessor_read_float(&dense, index, dest, floats_per_element); + if (!cgltf_accessor_read_float(&dense, index, dest, floats_per_element)) + { + return 0; + } } // Second pass: write out each element in the sparse accessor. if (accessor->is_sparse) { const cgltf_accessor_sparse* sparse = &dense.sparse; + + if (sparse->indices_buffer_view->buffer->data == NULL || sparse->values_buffer_view->buffer->data == NULL) + { + return 0; + } + const uint8_t* index_data = (const uint8_t*) sparse->indices_buffer_view->buffer->data; index_data += sparse->indices_byte_offset + sparse->indices_buffer_view->offset; cgltf_size index_stride = cgltf_component_size(sparse->indices_component_type); @@ -1684,7 +1815,12 @@ cgltf_size cgltf_accessor_unpack_floats(const cgltf_accessor* accessor, cgltf_fl { size_t writer_index = cgltf_component_read_index(index_data, sparse->indices_component_type); float* writer_head = out + writer_index * floats_per_element; - cgltf_element_read_float(reader_head, dense.type, dense.component_type, dense.normalized, writer_head, floats_per_element); + + if (!cgltf_element_read_float(reader_head, dense.type, dense.component_type, dense.normalized, writer_head, floats_per_element)) + { + return 0; + } + reader_head += dense.stride; } } @@ -1692,26 +1828,106 @@ cgltf_size cgltf_accessor_unpack_floats(const cgltf_accessor* accessor, cgltf_fl return element_count * floats_per_element; } -cgltf_size cgltf_accessor_read_index(const cgltf_accessor* accessor, cgltf_size index) +static cgltf_uint cgltf_component_read_uint(const void* in, cgltf_component_type component_type) { - if (accessor->buffer_view) + switch (component_type) { - cgltf_size offset = accessor->offset + accessor->buffer_view->offset; - const uint8_t* element = (const uint8_t*) accessor->buffer_view->buffer->data; - element += offset + accessor->stride * index; - return cgltf_component_read_index(element, accessor->component_type); + case cgltf_component_type_r_8: + return *((const int8_t*) in); + + case cgltf_component_type_r_8u: + return *((const uint8_t*) in); + + case cgltf_component_type_r_16: + return *((const int16_t*) in); + + case cgltf_component_type_r_16u: + return *((const uint16_t*) in); + + case cgltf_component_type_r_32u: + return *((const uint32_t*) in); + + default: + return 0; } return 0; } +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) +{ + cgltf_size num_components = cgltf_num_components(type); + + if (element_size < num_components) + { + return 0; + } + + // Reading integer matrices is not a valid use case + if (type == cgltf_type_mat2 || type == cgltf_type_mat3 || type == cgltf_type_mat4) + { + return 0; + } + + cgltf_size component_size = cgltf_component_size(component_type); + + for (cgltf_size i = 0; i < num_components; ++i) + { + out[i] = cgltf_component_read_uint(element + component_size * i, component_type); + } + return 1; +} + +cgltf_bool cgltf_accessor_read_uint(const cgltf_accessor* accessor, cgltf_size index, cgltf_uint* out, cgltf_size element_size) +{ + if (accessor->is_sparse) + { + return 0; + } + if (accessor->buffer_view == NULL) + { + memset(out, 0, element_size * sizeof( cgltf_uint )); + return 1; + } + if (accessor->buffer_view->buffer->data == NULL) + { + return 0; + } + cgltf_size offset = accessor->offset + accessor->buffer_view->offset; + const uint8_t* element = (const uint8_t*) accessor->buffer_view->buffer->data; + element += offset + accessor->stride * index; + return cgltf_element_read_uint(element, accessor->type, accessor->component_type, out, element_size); +} + +cgltf_size cgltf_accessor_read_index(const cgltf_accessor* accessor, cgltf_size index) +{ + if (accessor->is_sparse) + { + return 0; // This is an error case, but we can't communicate the error with existing interface. + } + if (accessor->buffer_view == NULL) + { + return 0; + } + if (accessor->buffer_view->buffer->data == NULL) + { + return 0; // This is an error case, but we can't communicate the error with existing interface. + } + + cgltf_size offset = accessor->offset + accessor->buffer_view->offset; + const uint8_t* element = (const uint8_t*) accessor->buffer_view->buffer->data; + element += offset + accessor->stride * index; + return cgltf_component_read_index(element, accessor->component_type); +} + #define CGLTF_ERROR_JSON -1 #define CGLTF_ERROR_NOMEM -2 +#define CGLTF_ERROR_LEGACY -3 #define CGLTF_CHECK_TOKTYPE(tok_, type_) if ((tok_).type != (type_)) { return CGLTF_ERROR_JSON; } #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 */ -#define CGLTF_PTRINDEX(type, idx) (type*)(cgltf_size)(idx + 1) +#define CGLTF_PTRINDEX(type, idx) (type*)((cgltf_size)idx + 1) #define CGLTF_PTRFIXUP(var, data, size) if (var) { if ((cgltf_size)var > size) { return CGLTF_ERROR_JSON; } var = &data[(cgltf_size)var-1]; } #define CGLTF_PTRFIXUP_REQ(var, data, size) if (!var || (cgltf_size)var > size) { return CGLTF_ERROR_JSON; } var = &data[(cgltf_size)var-1]; @@ -1727,20 +1943,20 @@ static int cgltf_json_to_int(jsmntok_t const* tok, const uint8_t* json_chunk) { CGLTF_CHECK_TOKTYPE(*tok, JSMN_PRIMITIVE); char tmp[128]; - int size = (cgltf_size)(tok->end - tok->start) < sizeof(tmp) ? tok->end - tok->start : sizeof(tmp) - 1; + int size = (cgltf_size)(tok->end - tok->start) < sizeof(tmp) ? tok->end - tok->start : (int)(sizeof(tmp) - 1); strncpy(tmp, (const char*)json_chunk + tok->start, size); tmp[size] = 0; - return atoi(tmp); + return CGLTF_ATOI(tmp); } static cgltf_float cgltf_json_to_float(jsmntok_t const* tok, const uint8_t* json_chunk) { CGLTF_CHECK_TOKTYPE(*tok, JSMN_PRIMITIVE); char tmp[128]; - int size = (cgltf_size)(tok->end - tok->start) < sizeof(tmp) ? tok->end - tok->start : sizeof(tmp) - 1; + int size = (cgltf_size)(tok->end - tok->start) < sizeof(tmp) ? tok->end - tok->start : (int)(sizeof(tmp) - 1); strncpy(tmp, (const char*)json_chunk + tok->start, size); tmp[size] = 0; - return (cgltf_float)atof(tmp); + return (cgltf_float)CGLTF_ATOF(tmp); } static cgltf_bool cgltf_json_to_bool(jsmntok_t const* tok, const uint8_t* json_chunk) @@ -1812,7 +2028,7 @@ static int cgltf_parse_json_string(cgltf_options* options, jsmntok_t const* toke return CGLTF_ERROR_JSON; } int size = tokens[i].end - tokens[i].start; - char* result = (char*)options->memory_alloc(options->memory_user_data, size + 1); + char* result = (char*)options->memory.alloc(options->memory.user_data, size + 1); if (!result) { return CGLTF_ERROR_NOMEM; @@ -1826,7 +2042,10 @@ static int cgltf_parse_json_string(cgltf_options* options, jsmntok_t const* toke 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) { (void)json_chunk; - CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_ARRAY); + if (tokens[i].type != JSMN_ARRAY) + { + return tokens[i].type == JSMN_OBJECT ? CGLTF_ERROR_LEGACY : CGLTF_ERROR_JSON; + } if (*out_array) { return CGLTF_ERROR_JSON; @@ -1865,7 +2084,7 @@ static int cgltf_parse_json_string_array(cgltf_options* options, jsmntok_t const static void cgltf_parse_attribute_type(const char* name, cgltf_attribute_type* out_type, int* out_index) { const char* us = strchr(name, '_'); - size_t len = us ? us - name : strlen(name); + size_t len = us ? (size_t)(us - name) : strlen(name); if (len == 8 && strncmp(name, "POSITION", 8) == 0) { @@ -1902,7 +2121,7 @@ static void cgltf_parse_attribute_type(const char* name, cgltf_attribute_type* o if (us && *out_type != cgltf_attribute_type_invalid) { - *out_index = atoi(us + 1); + *out_index = CGLTF_ATOI(us + 1); } } @@ -3284,30 +3503,30 @@ static int cgltf_parse_json_camera(cgltf_options* options, jsmntok_t const* toke if (cgltf_json_strcmp(tokens+i, json_chunk, "aspectRatio") == 0) { ++i; - out_camera->perspective.aspect_ratio = cgltf_json_to_float(tokens + i, json_chunk); + out_camera->data.perspective.aspect_ratio = cgltf_json_to_float(tokens + i, json_chunk); ++i; } else if (cgltf_json_strcmp(tokens+i, json_chunk, "yfov") == 0) { ++i; - out_camera->perspective.yfov = cgltf_json_to_float(tokens + i, json_chunk); + out_camera->data.perspective.yfov = cgltf_json_to_float(tokens + i, json_chunk); ++i; } else if (cgltf_json_strcmp(tokens+i, json_chunk, "zfar") == 0) { ++i; - out_camera->perspective.zfar = cgltf_json_to_float(tokens + i, json_chunk); + out_camera->data.perspective.zfar = cgltf_json_to_float(tokens + i, json_chunk); ++i; } else if (cgltf_json_strcmp(tokens+i, json_chunk, "znear") == 0) { ++i; - out_camera->perspective.znear = cgltf_json_to_float(tokens + i, json_chunk); + out_camera->data.perspective.znear = cgltf_json_to_float(tokens + i, json_chunk); ++i; } else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0) { - i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_camera->perspective.extras); + i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_camera->data.perspective.extras); } else { @@ -3338,30 +3557,30 @@ static int cgltf_parse_json_camera(cgltf_options* options, jsmntok_t const* toke if (cgltf_json_strcmp(tokens+i, json_chunk, "xmag") == 0) { ++i; - out_camera->orthographic.xmag = cgltf_json_to_float(tokens + i, json_chunk); + out_camera->data.orthographic.xmag = cgltf_json_to_float(tokens + i, json_chunk); ++i; } else if (cgltf_json_strcmp(tokens+i, json_chunk, "ymag") == 0) { ++i; - out_camera->orthographic.ymag = cgltf_json_to_float(tokens + i, json_chunk); + out_camera->data.orthographic.ymag = cgltf_json_to_float(tokens + i, json_chunk); ++i; } else if (cgltf_json_strcmp(tokens+i, json_chunk, "zfar") == 0) { ++i; - out_camera->orthographic.zfar = cgltf_json_to_float(tokens + i, json_chunk); + out_camera->data.orthographic.zfar = cgltf_json_to_float(tokens + i, json_chunk); ++i; } else if (cgltf_json_strcmp(tokens+i, json_chunk, "znear") == 0) { ++i; - out_camera->orthographic.znear = cgltf_json_to_float(tokens + i, json_chunk); + out_camera->data.orthographic.znear = cgltf_json_to_float(tokens + i, json_chunk); ++i; } else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0) { - i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_camera->orthographic.extras); + i = cgltf_parse_json_extras(tokens, i + 1, json_chunk, &out_camera->data.orthographic.extras); } else { @@ -4049,6 +4268,11 @@ static int cgltf_parse_json_asset(cgltf_options* options, jsmntok_t const* token } } + if (out_asset->version && CGLTF_ATOF(out_asset->version) < 2) + { + return CGLTF_ERROR_LEGACY; + } + return i; } @@ -4275,7 +4499,7 @@ cgltf_result cgltf_parse_json(cgltf_options* options, const uint8_t* json_chunk, options->json_token_count = token_count; } - jsmntok_t* tokens = (jsmntok_t*)options->memory_alloc(options->memory_user_data, sizeof(jsmntok_t) * (options->json_token_count + 1)); + jsmntok_t* tokens = (jsmntok_t*)options->memory.alloc(options->memory.user_data, sizeof(jsmntok_t) * (options->json_token_count + 1)); if (!tokens) { @@ -4288,7 +4512,7 @@ cgltf_result cgltf_parse_json(cgltf_options* options, const uint8_t* json_chunk, if (token_count <= 0) { - options->memory_free(options->memory_user_data, tokens); + options->memory.free(options->memory.user_data, tokens); return cgltf_result_invalid_json; } @@ -4296,26 +4520,32 @@ cgltf_result cgltf_parse_json(cgltf_options* options, const uint8_t* json_chunk, // for invalid JSON inputs this makes sure we don't perform out of bound reads of token data tokens[token_count].type = JSMN_UNDEFINED; - cgltf_data* data = (cgltf_data*)options->memory_alloc(options->memory_user_data, sizeof(cgltf_data)); + cgltf_data* data = (cgltf_data*)options->memory.alloc(options->memory.user_data, sizeof(cgltf_data)); if (!data) { - options->memory_free(options->memory_user_data, tokens); + options->memory.free(options->memory.user_data, tokens); return cgltf_result_out_of_memory; } memset(data, 0, sizeof(cgltf_data)); - data->memory_free = options->memory_free; - data->memory_user_data = options->memory_user_data; + data->memory = options->memory; + data->file = options->file; int i = cgltf_parse_json_root(options, tokens, 0, json_chunk, data); - options->memory_free(options->memory_user_data, tokens); + options->memory.free(options->memory.user_data, tokens); if (i < 0) { cgltf_free(data); - return (i == CGLTF_ERROR_NOMEM) ? cgltf_result_out_of_memory : cgltf_result_invalid_gltf; + + switch (i) + { + case CGLTF_ERROR_NOMEM: return cgltf_result_out_of_memory; + case CGLTF_ERROR_LEGACY: return cgltf_result_legacy_gltf; + default: return cgltf_result_invalid_gltf; + } } if (cgltf_fixup_pointers(data) < 0) diff --git a/3rdparty/cgltf/cgltf_write.h b/3rdparty/cgltf/cgltf_write.h index 33b394154..ce90bcd86 100644 --- a/3rdparty/cgltf/cgltf_write.h +++ b/3rdparty/cgltf/cgltf_write.h @@ -1,7 +1,7 @@ /** * cgltf_write - a single-file glTF 2.0 writer written in C99. * - * Version: 1.3 + * Version: 1.5 * * Website: https://github.com/jkuhlmann/cgltf * @@ -23,6 +23,11 @@ * buffer. Returns the number of bytes written to `buffer`, including a null * terminator. If buffer is null, returns the number of bytes that would have * been written. `data` is not deallocated. + * + * To write custom JSON into the `extras` field, aggregate all the custom JSON + * into a single buffer, then set `file_data` to this buffer. By supplying + * start_offset and end_offset values for various objects, you can select a + * range of characters within the aggregated buffer. */ #ifndef CGLTF_WRITE_H_INCLUDED__ #define CGLTF_WRITE_H_INCLUDED__ @@ -71,11 +76,11 @@ cgltf_size cgltf_write(const cgltf_options* options, char* buffer, cgltf_size si typedef struct { char* buffer; - size_t buffer_size; - size_t remaining; + cgltf_size buffer_size; + cgltf_size remaining; char* cursor; - size_t tmp; - size_t chars_written; + cgltf_size tmp; + cgltf_size chars_written; const cgltf_data* data; int depth; const char* indent; @@ -83,14 +88,24 @@ typedef struct { uint32_t extension_flags; } cgltf_write_context; -#define CGLTF_SPRINTF(fmt, ...) { \ - context->tmp = snprintf ( context->cursor, context->remaining, fmt, ## __VA_ARGS__ ); \ +#define CGLTF_MIN(a, b) (a < b ? a : b) + +#define CGLTF_SPRINTF(...) { \ + context->tmp = snprintf ( context->cursor, context->remaining, __VA_ARGS__ ); \ context->chars_written += context->tmp; \ if (context->cursor) { \ context->cursor += context->tmp; \ context->remaining -= context->tmp; \ } } +#define CGLTF_SNPRINTF(length, ...) { \ + context->tmp = snprintf ( context->cursor, CGLTF_MIN(length + 1, context->remaining), __VA_ARGS__ ); \ + context->chars_written += length; \ + if (context->cursor) { \ + context->cursor += length; \ + context->remaining -= length; \ + } } + #define CGLTF_WRITE_IDXPROP(label, val, start) if (val) { \ cgltf_write_indent(context); \ CGLTF_SPRINTF("\"%s\": %d", label, (int) (val - start)); \ @@ -99,7 +114,7 @@ typedef struct { #define CGLTF_WRITE_IDXARRPROP(label, dim, vals, start) if (vals) { \ cgltf_write_indent(context); \ CGLTF_SPRINTF("\"%s\": [", label); \ - for (int i = 0; i < dim; ++i) { \ + for (int i = 0; i < (int)(dim); ++i) { \ int idx = (int) (vals[i] - start); \ if (i != 0) CGLTF_SPRINTF(","); \ CGLTF_SPRINTF(" %d", idx); \ @@ -116,6 +131,7 @@ typedef struct { context->extension_flags |= CGLTF_EXTENSION_FLAG_TEXTURE_TRANSFORM; \ cgltf_write_texture_transform(context, &info.transform); \ } \ + cgltf_write_extras(context, &info.extras); \ cgltf_write_line(context, "}"); } static void cgltf_write_indent(cgltf_write_context* context) @@ -144,7 +160,7 @@ static void cgltf_write_line(cgltf_write_context* context, const char* line) } cgltf_write_indent(context); CGLTF_SPRINTF("%s", line); - int last = strlen(line) - 1; + cgltf_size last = (cgltf_size)(strlen(line) - 1); if (line[0] == ']' || line[0] == '}') { context->needs_comma = 1; @@ -166,6 +182,19 @@ static void cgltf_write_strprop(cgltf_write_context* context, const char* label, } } +static void cgltf_write_extras(cgltf_write_context* context, const cgltf_extras* extras) +{ + cgltf_size length = extras->end_offset - extras->start_offset; + if (length > 0 && context->data->file_data) + { + char* json_string = ((char*) context->data->file_data) + extras->start_offset; + cgltf_write_indent(context); + CGLTF_SPRINTF("%s", "\"extras\": "); + CGLTF_SNPRINTF(length, "%s", json_string); + context->needs_comma = 1; + } +} + static void cgltf_write_stritem(cgltf_write_context* context, const char* item) { cgltf_write_indent(context); @@ -213,11 +242,11 @@ static void cgltf_write_boolprop_optional(cgltf_write_context* context, const ch } } -static void cgltf_write_floatarrayprop(cgltf_write_context* context, const char* label, const cgltf_float* vals, int dim) +static void cgltf_write_floatarrayprop(cgltf_write_context* context, const char* label, const cgltf_float* vals, cgltf_size dim) { cgltf_write_indent(context); CGLTF_SPRINTF("\"%s\": [", label); - for (int i = 0; i < dim; ++i) + for (cgltf_size i = 0; i < dim; ++i) { if (i != 0) { @@ -282,7 +311,7 @@ static const char* cgltf_str_from_type(cgltf_type type) } } -static int cgltf_dim_from_type(cgltf_type type) +static cgltf_size cgltf_dim_from_type(cgltf_type type) { switch (type) { @@ -343,6 +372,7 @@ static void cgltf_write_asset(cgltf_write_context* context, const cgltf_asset* a cgltf_write_strprop(context, "generator", asset->generator); cgltf_write_strprop(context, "version", asset->version); cgltf_write_strprop(context, "min_version", asset->min_version); + cgltf_write_extras(context, &asset->extras); cgltf_write_line(context, "}"); } @@ -374,6 +404,7 @@ static void cgltf_write_primitive(cgltf_write_context* context, const cgltf_prim } cgltf_write_line(context, "]"); } + cgltf_write_extras(context, &prim->extras); } static void cgltf_write_mesh(cgltf_write_context* context, const cgltf_mesh* mesh) @@ -394,7 +425,7 @@ static void cgltf_write_mesh(cgltf_write_context* context, const cgltf_mesh* mes { cgltf_write_floatarrayprop(context, "weights", mesh->weights, mesh->weights_count); } - + cgltf_write_extras(context, &mesh->extras); cgltf_write_line(context, "}"); } @@ -402,10 +433,11 @@ static void cgltf_write_buffer_view(cgltf_write_context* context, const cgltf_bu { cgltf_write_line(context, "{"); CGLTF_WRITE_IDXPROP("buffer", view->buffer, context->data->buffers); - cgltf_write_intprop(context, "byteLength", view->size, -1); - cgltf_write_intprop(context, "byteOffset", view->offset, 0); - cgltf_write_intprop(context, "byteStride", view->stride, 0); + cgltf_write_intprop(context, "byteLength", (int)view->size, -1); + cgltf_write_intprop(context, "byteOffset", (int)view->offset, 0); + cgltf_write_intprop(context, "byteStride", (int)view->stride, 0); // NOTE: We skip writing "target" because the spec says its usage can be inferred. + cgltf_write_extras(context, &view->extras); cgltf_write_line(context, "}"); } @@ -414,7 +446,8 @@ static void cgltf_write_buffer(cgltf_write_context* context, const cgltf_buffer* { cgltf_write_line(context, "{"); cgltf_write_strprop(context, "uri", buffer->uri); - cgltf_write_intprop(context, "byteLength", buffer->size, -1); + cgltf_write_intprop(context, "byteLength", (int)buffer->size, -1); + cgltf_write_extras(context, &buffer->extras); cgltf_write_line(context, "}"); } @@ -448,6 +481,7 @@ static void cgltf_write_material(cgltf_write_context* context, const cgltf_mater { cgltf_write_floatarrayprop(context, "baseColorFactor", params->base_color_factor, 4); } + cgltf_write_extras(context, ¶ms->extras); cgltf_write_line(context, "}"); } @@ -486,6 +520,7 @@ static void cgltf_write_material(cgltf_write_context* context, const cgltf_mater cgltf_write_floatarrayprop(context, "emissiveFactor", material->emissive_factor, 3); } cgltf_write_strprop(context, "alphaMode", cgltf_str_from_alpha_mode(material->alpha_mode)); + cgltf_write_extras(context, &material->extras); cgltf_write_line(context, "}"); } @@ -495,7 +530,8 @@ static void cgltf_write_image(cgltf_write_context* context, const cgltf_image* i cgltf_write_strprop(context, "name", image->name); cgltf_write_strprop(context, "uri", image->uri); CGLTF_WRITE_IDXPROP("bufferView", image->buffer_view, context->data->buffer_views); - cgltf_write_strprop(context, "mime_type", image->mime_type); + cgltf_write_strprop(context, "mimeType", image->mime_type); + cgltf_write_extras(context, &image->extras); cgltf_write_line(context, "}"); } @@ -505,6 +541,7 @@ static void cgltf_write_texture(cgltf_write_context* context, const cgltf_textur cgltf_write_strprop(context, "name", texture->name); CGLTF_WRITE_IDXPROP("source", texture->image, context->data->images); CGLTF_WRITE_IDXPROP("sampler", texture->sampler, context->data->samplers); + cgltf_write_extras(context, &texture->extras); cgltf_write_line(context, "}"); } @@ -515,6 +552,7 @@ static void cgltf_write_skin(cgltf_write_context* context, const cgltf_skin* ski CGLTF_WRITE_IDXPROP("inverseBindMatrices", skin->inverse_bind_matrices, context->data->accessors); CGLTF_WRITE_IDXARRPROP("joints", skin->joints_count, skin->joints, context->data->nodes); cgltf_write_strprop(context, "name", skin->name); + cgltf_write_extras(context, &skin->extras); cgltf_write_line(context, "}"); } @@ -566,6 +604,7 @@ static void cgltf_write_animation_sampler(cgltf_write_context* context, const cg cgltf_write_interpolation_type(context, "interpolation", animation_sampler->interpolation); CGLTF_WRITE_IDXPROP("input", animation_sampler->input, context->data->accessors); CGLTF_WRITE_IDXPROP("output", animation_sampler->output, context->data->accessors); + cgltf_write_extras(context, &animation_sampler->extras); cgltf_write_line(context, "}"); } @@ -577,6 +616,7 @@ static void cgltf_write_animation_channel(cgltf_write_context* context, const cg CGLTF_WRITE_IDXPROP("node", animation_channel->target_node, context->data->nodes); cgltf_write_path_type(context, "path", animation_channel->target_path); cgltf_write_line(context, "}"); + cgltf_write_extras(context, &animation_channel->extras); cgltf_write_line(context, "}"); } @@ -603,6 +643,7 @@ static void cgltf_write_animation(cgltf_write_context* context, const cgltf_anim } cgltf_write_line(context, "]"); } + cgltf_write_extras(context, &animation->extras); cgltf_write_line(context, "}"); } @@ -613,6 +654,7 @@ static void cgltf_write_sampler(cgltf_write_context* context, const cgltf_sample cgltf_write_intprop(context, "minFilter", sampler->min_filter, 0); cgltf_write_intprop(context, "wrapS", sampler->wrap_s, 10497); cgltf_write_intprop(context, "wrapT", sampler->wrap_t, 10497); + cgltf_write_extras(context, &sampler->extras); cgltf_write_line(context, "}"); } @@ -663,6 +705,7 @@ static void cgltf_write_node(cgltf_write_context* context, const cgltf_node* nod CGLTF_WRITE_IDXPROP("camera", node->camera, context->data->cameras); } + cgltf_write_extras(context, &node->extras); cgltf_write_line(context, "}"); } @@ -671,6 +714,7 @@ static void cgltf_write_scene(cgltf_write_context* context, const cgltf_scene* s cgltf_write_line(context, "{"); cgltf_write_strprop(context, "name", scene->name); CGLTF_WRITE_IDXARRPROP("nodes", scene->nodes_count, scene->nodes, context->data->nodes); + cgltf_write_extras(context, &scene->extras); cgltf_write_line(context, "}"); } @@ -680,10 +724,10 @@ static void cgltf_write_accessor(cgltf_write_context* context, const cgltf_acces CGLTF_WRITE_IDXPROP("bufferView", accessor->buffer_view, context->data->buffer_views); cgltf_write_intprop(context, "componentType", cgltf_int_from_component_type(accessor->component_type), 0); cgltf_write_strprop(context, "type", cgltf_str_from_type(accessor->type)); - int dim = cgltf_dim_from_type(accessor->type); + cgltf_size dim = cgltf_dim_from_type(accessor->type); cgltf_write_boolprop_optional(context, "normalized", accessor->normalized, false); - cgltf_write_intprop(context, "byteOffset", accessor->offset, 0); - cgltf_write_intprop(context, "count", accessor->count, -1); + cgltf_write_intprop(context, "byteOffset", (int)accessor->offset, 0); + cgltf_write_intprop(context, "count", (int)accessor->count, -1); if (accessor->has_min) { cgltf_write_floatarrayprop(context, "min", accessor->min, dim); @@ -695,18 +739,22 @@ static void cgltf_write_accessor(cgltf_write_context* context, const cgltf_acces if (accessor->is_sparse) { cgltf_write_line(context, "\"sparse\": {"); - cgltf_write_intprop(context, "count", accessor->sparse.count, 0); + cgltf_write_intprop(context, "count", (int)accessor->sparse.count, 0); cgltf_write_line(context, "\"indices\": {"); - cgltf_write_intprop(context, "byteOffset", accessor->sparse.indices_byte_offset, 0); + cgltf_write_intprop(context, "byteOffset", (int)accessor->sparse.indices_byte_offset, 0); CGLTF_WRITE_IDXPROP("bufferView", accessor->sparse.indices_buffer_view, context->data->buffer_views); cgltf_write_intprop(context, "componentType", cgltf_int_from_component_type(accessor->sparse.indices_component_type), 0); + cgltf_write_extras(context, &accessor->sparse.indices_extras); cgltf_write_line(context, "}"); cgltf_write_line(context, "\"values\": {"); - cgltf_write_intprop(context, "byteOffset", accessor->sparse.values_byte_offset, 0); + cgltf_write_intprop(context, "byteOffset", (int)accessor->sparse.values_byte_offset, 0); CGLTF_WRITE_IDXPROP("bufferView", accessor->sparse.values_buffer_view, context->data->buffer_views); + cgltf_write_extras(context, &accessor->sparse.values_extras); cgltf_write_line(context, "}"); + cgltf_write_extras(context, &accessor->sparse.extras); cgltf_write_line(context, "}"); } + cgltf_write_extras(context, &accessor->extras); cgltf_write_line(context, "}"); } @@ -722,21 +770,24 @@ static void cgltf_write_camera(cgltf_write_context* context, const cgltf_camera* if (camera->type == cgltf_camera_type_orthographic) { cgltf_write_line(context, "\"orthographic\": {"); - cgltf_write_floatprop(context, "xmag", camera->orthographic.xmag, -1.0f); - cgltf_write_floatprop(context, "ymag", camera->orthographic.ymag, -1.0f); - cgltf_write_floatprop(context, "zfar", camera->orthographic.zfar, -1.0f); - cgltf_write_floatprop(context, "znear", camera->orthographic.znear, -1.0f); + cgltf_write_floatprop(context, "xmag", camera->data.orthographic.xmag, -1.0f); + cgltf_write_floatprop(context, "ymag", camera->data.orthographic.ymag, -1.0f); + cgltf_write_floatprop(context, "zfar", camera->data.orthographic.zfar, -1.0f); + cgltf_write_floatprop(context, "znear", camera->data.orthographic.znear, -1.0f); + cgltf_write_extras(context, &camera->data.orthographic.extras); cgltf_write_line(context, "}"); } else if (camera->type == cgltf_camera_type_perspective) { cgltf_write_line(context, "\"perspective\": {"); - cgltf_write_floatprop(context, "aspectRatio", camera->perspective.aspect_ratio, -1.0f); - cgltf_write_floatprop(context, "yfov", camera->perspective.yfov, -1.0f); - cgltf_write_floatprop(context, "zfar", camera->perspective.zfar, -1.0f); - cgltf_write_floatprop(context, "znear", camera->perspective.znear, -1.0f); + cgltf_write_floatprop(context, "aspectRatio", camera->data.perspective.aspect_ratio, -1.0f); + cgltf_write_floatprop(context, "yfov", camera->data.perspective.yfov, -1.0f); + cgltf_write_floatprop(context, "zfar", camera->data.perspective.zfar, -1.0f); + cgltf_write_floatprop(context, "znear", camera->data.perspective.znear, -1.0f); + cgltf_write_extras(context, &camera->data.perspective.extras); cgltf_write_line(context, "}"); } + cgltf_write_extras(context, &camera->extras); cgltf_write_line(context, "}"); } @@ -750,7 +801,7 @@ static void cgltf_write_light(cgltf_write_context* context, const cgltf_light* l } if (cgltf_check_floatarray(light->color, 3, 1.0f)) { - cgltf_write_floatarrayprop(context, "light", light->color, 3); + cgltf_write_floatarrayprop(context, "color", light->color, 3); } cgltf_write_floatprop(context, "intensity", light->intensity, 1.0f); cgltf_write_floatprop(context, "range", light->range, 0.0f); @@ -767,9 +818,9 @@ static void cgltf_write_light(cgltf_write_context* context, const cgltf_light* l cgltf_result cgltf_write_file(const cgltf_options* options, const char* path, const cgltf_data* data) { - size_t expected = cgltf_write(options, NULL, 0, data); + cgltf_size expected = cgltf_write(options, NULL, 0, data); char* buffer = (char*) malloc(expected); - size_t actual = cgltf_write(options, buffer, expected, data); + cgltf_size actual = cgltf_write(options, buffer, expected, data); if (expected != actual) { fprintf(stderr, "Error: expected %zu bytes but wrote %zu bytes.\n", expected, actual); } @@ -787,6 +838,7 @@ cgltf_result cgltf_write_file(const cgltf_options* options, const char* path, co cgltf_size cgltf_write(const cgltf_options* options, char* buffer, cgltf_size size, const cgltf_data* data) { + (void)options; cgltf_write_context ctx; ctx.buffer = buffer; ctx.buffer_size = size; @@ -885,10 +937,7 @@ cgltf_size cgltf_write(const cgltf_options* options, char* buffer, cgltf_size si cgltf_write_line(context, "]"); } - if (data->scene) - { - cgltf_write_intprop(context, "scene", data->scene - data->scenes, -1); - } + CGLTF_WRITE_IDXPROP("scene", data->scene, data->scenes); if (data->scenes_count > 0) { diff --git a/3rdparty/cgltf/fuzz/data/Box.glb b/3rdparty/cgltf/fuzz/data/Box.glb deleted file mode 100644 index 95ec886b6..000000000 Binary files a/3rdparty/cgltf/fuzz/data/Box.glb and /dev/null differ diff --git a/3rdparty/cgltf/fuzz/data/TriangleWithoutIndices.gltf b/3rdparty/cgltf/fuzz/data/TriangleWithoutIndices.gltf deleted file mode 100644 index 0ac24f412..000000000 --- a/3rdparty/cgltf/fuzz/data/TriangleWithoutIndices.gltf +++ /dev/null @@ -1,53 +0,0 @@ -{ - "scenes" : [ - { - "nodes" : [ 0 ] - } - ], - - "nodes" : [ - { - "mesh" : 0 - } - ], - - "meshes" : [ - { - "primitives" : [ { - "attributes" : { - "POSITION" : 0 - } - } ] - } - ], - - "buffers" : [ - { - "uri" : "data:application/octet-stream;base64,AAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAAAAAAAAAgD8AAAAA", - "byteLength" : 36 - } - ], - "bufferViews" : [ - { - "buffer" : 0, - "byteOffset" : 0, - "byteLength" : 36, - "target" : 34962 - } - ], - "accessors" : [ - { - "bufferView" : 0, - "byteOffset" : 0, - "componentType" : 5126, - "count" : 3, - "type" : "VEC3", - "max" : [ 1.0, 1.0, 0.0 ], - "min" : [ 0.0, 0.0, 0.0 ] - } - ], - - "asset" : { - "version" : "2.0" - } -} \ No newline at end of file diff --git a/3rdparty/cgltf/fuzz/data/WaterBottle.gltf b/3rdparty/cgltf/fuzz/data/WaterBottle.gltf deleted file mode 100644 index 9f23f3add..000000000 --- a/3rdparty/cgltf/fuzz/data/WaterBottle.gltf +++ /dev/null @@ -1,172 +0,0 @@ -{ - "accessors": [ - { - "bufferView": 0, - "componentType": 5126, - "count": 2549, - "type": "VEC2" - }, - { - "bufferView": 1, - "componentType": 5126, - "count": 2549, - "type": "VEC3" - }, - { - "bufferView": 2, - "componentType": 5126, - "count": 2549, - "type": "VEC4" - }, - { - "bufferView": 3, - "componentType": 5126, - "count": 2549, - "type": "VEC3", - "max": [ - 0.05445001, - 0.130220339, - 0.0544500239 - ], - "min": [ - -0.05445001, - -0.130220339, - -0.0544500239 - ] - }, - { - "bufferView": 4, - "componentType": 5123, - "count": 13530, - "type": "SCALAR" - } - ], - "asset": { - "generator": "glTF Tools for Unity", - "version": "2.0" - }, - "bufferViews": [ - { - "buffer": 0, - "byteLength": 20392 - }, - { - "buffer": 0, - "byteOffset": 20392, - "byteLength": 30588 - }, - { - "buffer": 0, - "byteOffset": 50980, - "byteLength": 40784 - }, - { - "buffer": 0, - "byteOffset": 91764, - "byteLength": 30588 - }, - { - "buffer": 0, - "byteOffset": 122352, - "byteLength": 27060 - } - ], - "buffers": [ - { - "uri": "WaterBottle.bin", - "byteLength": 149412 - } - ], - "images": [ - { - "uri": "WaterBottle_baseColor.png" - }, - { - "uri": "WaterBottle_occlusionRoughnessMetallic.png" - }, - { - "uri": "WaterBottle_normal.png" - }, - { - "uri": "WaterBottle_emissive.png" - } - ], - "meshes": [ - { - "primitives": [ - { - "attributes": { - "TEXCOORD_0": 0, - "NORMAL": 1, - "TANGENT": 2, - "POSITION": 3 - }, - "indices": 4, - "material": 0 - } - ], - "name": "WaterBottle" - } - ], - "materials": [ - { - "pbrMetallicRoughness": { - "baseColorTexture": { - "index": 0 - }, - "metallicRoughnessTexture": { - "index": 1 - } - }, - "normalTexture": { - "index": 2 - }, - "occlusionTexture": { - "index": 1 - }, - "emissiveFactor": [ - 1.0, - 1.0, - 1.0 - ], - "emissiveTexture": { - "index": 3 - }, - "name": "BottleMat" - } - ], - "nodes": [ - { - "mesh": 0, - "rotation": [ - 0.0, - 1.0, - 0.0, - 0.0 - ], - "name": "WaterBottle" - } - ], - "scene": 0, - "scenes": [ - { - "nodes": [ - 0 - ] - } - ], - "textures": [ - { - "source": 0 - }, - { - "source": 1 - }, - { - "source": 2 - }, - { - "source": 3 - } - ] -} \ No newline at end of file diff --git a/3rdparty/cgltf/fuzz/gltf.dict b/3rdparty/cgltf/fuzz/gltf.dict deleted file mode 100644 index b6349d2f1..000000000 --- a/3rdparty/cgltf/fuzz/gltf.dict +++ /dev/null @@ -1,201 +0,0 @@ -# -# AFL dictionary for JSON -# ----------------------- -# -# Just the very basics. -# -# Inspired by a dictionary by Jakub Wilk -# - -"0" -",0" -":0" -"0:" -"-1.2e+3" - -"true" -"false" -"null" - -"\"\"" -",\"\"" -":\"\"" -"\"\":" - -"{}" -",{}" -":{}" -"{\"\":0}" -"{{}}" - -"[]" -",[]" -":[]" -"[0]" -"[[]]" - -"''" -"\\" -"\\b" -"\\f" -"\\n" -"\\r" -"\\t" -"\\u0000" -"\\x00" -"\\0" -"\\uD800\\uDC00" -"\\uDBFF\\uDFFF" - -"\"\":0" -"//" -"/**/" - -# -# AFL dictionary for GLTF core -# ----------------------- - -"5120" -"5121" -"5122" -"5123" -"5125" -"5126" -"\"BLEND\"" -"\"CUBICSPLINE\"" -"\"LINEAR\"" -"\"MASK\"" -"\"MAT2\"" -"\"MAT3\"" -"\"MAT4\"" -"\"OPAQUE\"" -"\"SCALAR\"" -"\"STEP\"" -"\"VEC2\"" -"\"VEC3\"" -"\"VEC4\"" -"\"accessor\"" -"\"accessors\"" -"\"alphaCutoff\"" -"\"alphaMode\"" -"\"animations\"" -"\"aspectRatio\"" -"\"asset\"" -"\"attributes\"" -"\"baseColorFactor\"" -"\"baseColorTexture\"" -"\"bufferView\"" -"\"bufferViews\"" -"\"buffer\"" -"\"buffers\"" -"\"byteLength\"" -"\"byteOffset\"" -"\"byteStride\"" -"\"camera\"" -"\"cameras\"" -"\"channel\"" -"\"channels\"" -"\"children\"" -"\"componentType\"" -"\"copyright\"" -"\"count\"" -"\"doubleSided\"" -"\"emissiveFactor\"" -"\"emissiveTexture\"" -"\"extensionsRequired\"" -"\"extensionsUsed\"" -"\"extensions\"" -"\"extras\"" -"\"generator\"" -"\"image\"" -"\"images\"" -"\"index\"" -"\"indices\"" -"\"input\"" -"\"interpolation\"" -"\"inverseBindMatrices\"" -"\"joints\"" -"\"magFilter\"" -"\"material\"" -"\"materials\"" -"\"matrix\"" -"\"max\"" -"\"mesh\"" -"\"meshes\"" -"\"metallicFactor\"" -"\"metallicRoughnessTexture\"" -"\"mimeType\"" -"\"minFilter\"" -"\"minVersion\"" -"\"min\"" -"\"mode\"" -"\"name\"" -"\"node\"" -"\"nodes\"" -"\"normalTextureInfo\"" -"\"normalTexture\"" -"\"normalized\"" -"\"occlusionTextureInfo\"" -"\"occlusionTexture\"" -"\"orthographic\"" -"\"output\"" -"\"path\"" -"\"pbrMetallicRoughness\"" -"\"perspective\"" -"\"primitive\"" -"\"primitives\"" -"\"rotation\"" -"\"roughnessFactor\"" -"\"sampler\"" -"\"samplers\"" -"\"scale\"" -"\"scene\"" -"\"scenes\"" -"\"skeleton\"" -"\"skin\"" -"\"skins\"" -"\"source\"" -"\"sparse\"" -"\"strength\"" -"\"target\"" -"\"targets\"" -"\"texCoord\"" -"\"textureInfo\"" -"\"texture\"" -"\"textures\"" -"\"translation\"" -"\"type\"" -"\"uri\"" -"\"values\"" -"\"version\"" -"\"weights\"" -"\"wrapS\"" -"\"wrapT\"" -"\"xmag\"" -"\"yfov\"" -"\"ymag\"" -"\"zfar\"" -"\"znear\"" - -# -# AFL dictionary for GLTF extensions -# ----------------------- -"\"KHR_materials_pbrSpecularGlossiness\"" -"\"diffuseFactor\"" -"\"diffuseTexture\"" -"\"specularFactor\"" -"\"glossinessFactor\"" -"\"specularGlossinessTexture\"" -"\"KHR_materials_unlit\"" -"\"KHR_texture_transform\"" -"\"offset\"" -"\"rotation\"" -"\"scale\"" -"\"texCoord\"" -"\"KHR_lights_punctual\"" -"\"color\"" -"\"intensity\"" -"\"type\"" -"\"range\"" -"\"innerConeAngle\"" -"\"outerConeAngle\"" diff --git a/3rdparty/cgltf/fuzz/main.c b/3rdparty/cgltf/fuzz/main.c deleted file mode 100644 index 07659411b..000000000 --- a/3rdparty/cgltf/fuzz/main.c +++ /dev/null @@ -1,22 +0,0 @@ -/* How to fuzz: - -clang main.c -O2 -g -fsanitize=address,fuzzer -o fuzz -cp -r data temp -./fuzz temp/ -dict=gltf.dict -jobs=12 -workers=12 - -*/ -#define CGLTF_IMPLEMENTATION -#include "../cgltf.h" - -int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) -{ - cgltf_options options = {0}; - cgltf_data* data = NULL; - cgltf_result res = cgltf_parse(&options, Data, Size, &data); - if (res == cgltf_result_success) - { - cgltf_validate(data); - cgltf_free(data); - } - return 0; -} diff --git a/3rdparty/cgltf/test/CMakeLists.txt b/3rdparty/cgltf/test/CMakeLists.txt deleted file mode 100644 index 805221402..000000000 --- a/3rdparty/cgltf/test/CMakeLists.txt +++ /dev/null @@ -1,23 +0,0 @@ -cmake_minimum_required( VERSION 2.8 ) - -include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ) - -set( EXE_NAME cgltf_test ) -add_executable( ${EXE_NAME} main.c ) -set_property( TARGET ${EXE_NAME} PROPERTY C_STANDARD 99 ) -install( TARGETS ${EXE_NAME} RUNTIME DESTINATION bin ) - -set( EXE_NAME test_conversion ) -add_executable( ${EXE_NAME} test_conversion.cpp ) -set_property( TARGET ${EXE_NAME} PROPERTY CXX_STANDARD 11 ) -install( TARGETS ${EXE_NAME} RUNTIME DESTINATION bin ) - -set( EXE_NAME test_write ) -add_executable( ${EXE_NAME} test_write.cpp ) -set_property( TARGET ${EXE_NAME} PROPERTY CXX_STANDARD 11 ) -install( TARGETS ${EXE_NAME} RUNTIME DESTINATION bin ) - -set( EXE_NAME test_math ) -add_executable( ${EXE_NAME} test_math.cpp ) -set_property( TARGET ${EXE_NAME} PROPERTY CXX_STANDARD 11 ) -install( TARGETS ${EXE_NAME} RUNTIME DESTINATION bin ) diff --git a/3rdparty/cgltf/test/main.c b/3rdparty/cgltf/test/main.c deleted file mode 100644 index a6050d676..000000000 --- a/3rdparty/cgltf/test/main.c +++ /dev/null @@ -1,37 +0,0 @@ - - -#define CGLTF_IMPLEMENTATION -#include "../cgltf.h" - -#include - -int main(int argc, char** argv) -{ - if (argc < 2) - { - printf("err\n"); - return -1; - } - - cgltf_options options = {0}; - cgltf_data* data = NULL; - cgltf_result result = cgltf_parse_file(&options, argv[1], &data); - - if (result == cgltf_result_success) - result = cgltf_load_buffers(&options, data, argv[1]); - - if (result == cgltf_result_success) - result = cgltf_validate(data); - - printf("Result: %d\n", result); - - if (result == cgltf_result_success) - { - printf("Type: %u\n", data->file_type); - printf("Meshes: %lu\n", data->meshes_count); - } - - cgltf_free(data); - - return result; -} diff --git a/3rdparty/cgltf/test/test_all.py b/3rdparty/cgltf/test/test_all.py deleted file mode 100644 index 2cfcf7455..000000000 --- a/3rdparty/cgltf/test/test_all.py +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env python - -import os, sys -from sys import platform - -num_tested = 0 -num_errors = 0 - -def get_executable_path(name): - if platform == "win32": - return "build\\Debug\\" + name - else: - return "build/" + name - -def collect_files(path, type, name): - global num_tested - global num_errors - exe = get_executable_path(name) - for the_file in os.listdir(path): - file_path = os.path.join(os.path.normpath(path), the_file) - if os.path.isfile(file_path): - if the_file.endswith(type): - num_tested = num_tested +1 - print("### " + name + " " + file_path) - result = os.system("{0} \"{1}\"".format(exe, file_path)) - print("### Result: " + str(result) + "\n") - if result != 0: - num_errors = num_errors + 1 - print("Error.") - sys.exit(1) - elif os.path.isdir(file_path): - collect_files(file_path, type, name) - -if __name__ == "__main__": - if not os.path.exists("build/"): - os.makedirs("build/") - os.chdir("build/") - os.system("cmake ..") - if os.system("cmake --build .") != 0: - print("Unable to build.") - exit(1) - os.chdir("..") - if not os.path.exists("glTF-Sample-Models/"): - os.system("git init glTF-Sample-Models") - os.chdir("glTF-Sample-Models") - os.system("git remote add origin https://github.com/KhronosGroup/glTF-Sample-Models.git") - os.system("git config core.sparsecheckout true") - f = open(".git/info/sparse-checkout", "w+") - f.write("2.0/*\n") - f.close() - os.system("git pull --depth=1 origin master") - os.chdir("..") - collect_files("glTF-Sample-Models/2.0/", ".glb", "cgltf_test") - collect_files("glTF-Sample-Models/2.0/", ".gltf", "cgltf_test") - collect_files("glTF-Sample-Models/2.0/", ".glb", "test_conversion") - collect_files("glTF-Sample-Models/2.0/", ".gltf", "test_conversion") - collect_files("glTF-Sample-Models/2.0/", ".gltf", "test_write") - - result = os.system(get_executable_path("test_math")) - if result != 0: - num_errors = num_errors + 1 - print("Error.") - sys.exit(1) - - print("Tested files: " + str(num_tested)) - print("Errors: " + str(num_errors)) diff --git a/3rdparty/cgltf/test/test_conversion.cpp b/3rdparty/cgltf/test/test_conversion.cpp deleted file mode 100644 index b5e06aab2..000000000 --- a/3rdparty/cgltf/test/test_conversion.cpp +++ /dev/null @@ -1,69 +0,0 @@ -#define CGLTF_IMPLEMENTATION -#include "../cgltf.h" - -#include -#include -#include -#include - -static bool is_near(cgltf_float a, cgltf_float b) -{ - return std::abs(a - b) < 10 * std::numeric_limits::min(); -} - -int main(int argc, char** argv) -{ - if (argc < 2) - { - printf("err\n"); - return -1; - } - - cgltf_options options = {}; - cgltf_data* data = NULL; - cgltf_result result = cgltf_parse_file(&options, argv[1], &data); - - if (result == cgltf_result_success) - result = cgltf_load_buffers(&options, data, argv[1]); - - if (result != cgltf_result_success || strstr(argv[1], "Draco")) - return result; - - const cgltf_accessor* blobs = data->accessors; - cgltf_float element[16]; - for (cgltf_size blob_index = 0; blob_index < data->accessors_count; ++blob_index) - { - const cgltf_accessor* blob = data->accessors + blob_index; - if (blob->is_sparse) - { - cgltf_size nfloats = cgltf_num_components(blob->type) * blob->count; - cgltf_float* dense = (cgltf_float*) malloc(nfloats * sizeof(cgltf_float)); - if (cgltf_accessor_unpack_floats(blob, dense, nfloats) < nfloats) { - printf("Unable to completely unpack a sparse accessor.\n"); - return -1; - } - free(dense); - continue; - } - if (blob->has_max && blob->has_min) - { - cgltf_float min0 = std::numeric_limits::max(); - cgltf_float max0 = std::numeric_limits::lowest(); - for (cgltf_size index = 0; index < blob->count; index++) - { - cgltf_accessor_read_float(blob, index, element, 16); - min0 = std::min(min0, element[0]); - max0 = std::max(max0, element[0]); - } - if (!is_near(min0, blob->min[0]) || !is_near(max0, blob->max[0])) - { - printf("Computed [%f, %f] but expected [%f, %f]\n", min0, max0, blob->min[0], blob->max[0]); - return -1; - } - } - } - - cgltf_free(data); - - return result; -} diff --git a/3rdparty/cgltf/test/test_math.cpp b/3rdparty/cgltf/test/test_math.cpp deleted file mode 100644 index 6de608341..000000000 --- a/3rdparty/cgltf/test/test_math.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#define CGLTF_IMPLEMENTATION -#include "../cgltf.h" - -// Performs matrix-vector multiplication, as in (4x4) * (4x1) = (4x1) -static void transform(const cgltf_float matrix[16], const cgltf_float source[4], cgltf_float target[4]) { - target[0] = matrix[0] * source[0] + matrix[4] * source[1] + matrix[ 8] * source[2] + matrix[12] * source[3]; - target[1] = matrix[1] * source[0] + matrix[5] * source[1] + matrix[ 9] * source[2] + matrix[13] * source[3]; - target[2] = matrix[2] * source[0] + matrix[6] * source[1] + matrix[10] * source[2] + matrix[14] * source[3]; - target[3] = matrix[3] * source[0] + matrix[7] * source[1] + matrix[11] * source[2] + matrix[15] * source[3]; -} - -static void set(cgltf_float target[3], float x, float y, float z) { - target[0] = x; - target[1] = y; - target[2] = z; -} - -static void check(cgltf_float target[3], float x, float y, float z) { - if (target[0] != x || target[1] != y || target[2] != z) { - fprintf(stderr, "Mismatch detected.\n"); - exit(1); - } -} - -int main(int argc, char** argv) -{ - cgltf_node node = {}; - - cgltf_float matrix[16]; - cgltf_float source[4] = {1, 2, 3, 1}; - cgltf_float target[4]; - - set(node.scale, 1, 1, 1); - set(node.translation, 1, 0, 0); - cgltf_node_transform_local(&node, matrix); - transform(matrix, source, target); - check(target, 2, 2, 3); - - set(node.scale, 3, 1, 1); - set(node.translation, 0, 0, 0); - cgltf_node_transform_local(&node, matrix); - transform(matrix, source, target); - check(target, 3, 2, 3); - - set(node.scale, 1, 3, 1); - set(node.translation, 1, 0, 0); - cgltf_node_transform_local(&node, matrix); - transform(matrix, source, target); - check(target, 2, 6, 3); - - return 0; -} diff --git a/3rdparty/cgltf/test/test_write.cpp b/3rdparty/cgltf/test/test_write.cpp deleted file mode 100644 index b7ee25dc5..000000000 --- a/3rdparty/cgltf/test/test_write.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#define CGLTF_IMPLEMENTATION -#define CGLTF_WRITE_IMPLEMENTATION -#include "../cgltf_write.h" - -#include -#include -#include -#include - -int main(int argc, char** argv) -{ - if (argc < 2) - { - printf("err\n"); - return -1; - } - - cgltf_options options = {}; - cgltf_data* data0 = NULL; - cgltf_result result = cgltf_parse_file(&options, argv[1], &data0); - - // Silently skip over files that are unreadable since this is a writing test. - if (result != cgltf_result_success) - { - return cgltf_result_success; - } - - result = cgltf_write_file(&options, "out.gltf", data0); - if (result != cgltf_result_success) - { - return result; - } - cgltf_data* data1 = NULL; - result = cgltf_parse_file(&options, "out.gltf", &data1); - if (result != cgltf_result_success) - { - return result; - } - if (data0->meshes_count != data1->meshes_count) { - return -1; - } - cgltf_free(data1); - cgltf_free(data0); - return cgltf_result_success; -}