Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1 | // |
Syoyo Fujita | 9a1ea7e | 2017-06-20 02:17:28 +0900 | [diff] [blame] | 2 | // Header-only tiny glTF 2.0 loader and serializer. |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3 | // |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4 | // |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 5 | // The MIT License (MIT) |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 6 | // |
Syoyo Fujita | b702de7 | 2021-03-02 19:08:29 +0900 | [diff] [blame] | 7 | // Copyright (c) 2015 - Present Syoyo Fujita, Aurélien Chatelain and many |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 8 | // contributors. |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 9 | // |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 10 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | // of this software and associated documentation files (the "Software"), to deal |
| 12 | // in the Software without restriction, including without limitation the rights |
| 13 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | // copies of the Software, and to permit persons to whom the Software is |
| 15 | // furnished to do so, subject to the following conditions: |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 16 | // |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 17 | // The above copyright notice and this permission notice shall be included in |
| 18 | // all copies or substantial portions of the Software. |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 19 | // |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 20 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 26 | // THE SOFTWARE. |
| 27 | |
Syoyo Fujita | 5a6df34 | 2023-06-04 19:07:00 +0900 | [diff] [blame] | 28 | // Version: - v2.8.10 |
| 29 | // See https://github.com/syoyo/tinygltf/releases for release history. |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 30 | // |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 31 | // Tiny glTF loader is using following third party libraries: |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 32 | // |
Syoyo Fujita | 2e21be7 | 2017-11-05 17:13:01 +0900 | [diff] [blame] | 33 | // - jsonhpp: C++ JSON library. |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 34 | // - base64: base64 decode/encode library. |
| 35 | // - stb_image: Image loading library. |
| 36 | // |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 37 | #ifndef TINY_GLTF_H_ |
| 38 | #define TINY_GLTF_H_ |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 39 | |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 40 | #include <array> |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 41 | #include <cassert> |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 42 | #include <cmath> // std::fabs |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 43 | #include <cstdint> |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 44 | #include <cstdlib> |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 45 | #include <cstring> |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 46 | #include <limits> |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 47 | #include <map> |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 48 | #include <string> |
| 49 | #include <vector> |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 50 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 51 | // Auto-detect C++14 standard version |
| 52 | #if !defined(TINYGLTF_USE_CPP14) && defined(__cplusplus) && \ |
| 53 | (__cplusplus >= 201402L) |
zbendefy | 69eeea1 | 2022-09-05 23:54:57 +0200 | [diff] [blame] | 54 | #define TINYGLTF_USE_CPP14 |
| 55 | #endif |
| 56 | |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 57 | #ifdef __ANDROID__ |
| 58 | #ifdef TINYGLTF_ANDROID_LOAD_FROM_ASSETS |
| 59 | #include <android/asset_manager.h> |
| 60 | #endif |
| 61 | #endif |
| 62 | |
Selmar Kok | 79e3df2 | 2019-10-29 16:22:07 +0100 | [diff] [blame] | 63 | #ifdef __GNUC__ |
| 64 | #if (__GNUC__ < 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ <= 8)) |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 65 | #define TINYGLTF_NOEXCEPT |
Selmar Kok | 79e3df2 | 2019-10-29 16:22:07 +0100 | [diff] [blame] | 66 | #else |
| 67 | #define TINYGLTF_NOEXCEPT noexcept |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 68 | #endif |
Selmar Kok | 79e3df2 | 2019-10-29 16:22:07 +0100 | [diff] [blame] | 69 | #else |
| 70 | #define TINYGLTF_NOEXCEPT noexcept |
| 71 | #endif |
| 72 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 73 | #define DEFAULT_METHODS(x) \ |
| 74 | ~x() = default; \ |
| 75 | x(const x &) = default; \ |
| 76 | x(x &&) TINYGLTF_NOEXCEPT = default; \ |
| 77 | x &operator=(const x &) = default; \ |
| 78 | x &operator=(x &&) TINYGLTF_NOEXCEPT = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 79 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 80 | namespace tinygltf { |
| 81 | |
| 82 | #define TINYGLTF_MODE_POINTS (0) |
| 83 | #define TINYGLTF_MODE_LINE (1) |
| 84 | #define TINYGLTF_MODE_LINE_LOOP (2) |
Thomas Tissot | 6c4a006 | 2019-01-30 13:10:51 +0100 | [diff] [blame] | 85 | #define TINYGLTF_MODE_LINE_STRIP (3) |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 86 | #define TINYGLTF_MODE_TRIANGLES (4) |
| 87 | #define TINYGLTF_MODE_TRIANGLE_STRIP (5) |
| 88 | #define TINYGLTF_MODE_TRIANGLE_FAN (6) |
| 89 | |
| 90 | #define TINYGLTF_COMPONENT_TYPE_BYTE (5120) |
| 91 | #define TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE (5121) |
| 92 | #define TINYGLTF_COMPONENT_TYPE_SHORT (5122) |
| 93 | #define TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT (5123) |
| 94 | #define TINYGLTF_COMPONENT_TYPE_INT (5124) |
| 95 | #define TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT (5125) |
| 96 | #define TINYGLTF_COMPONENT_TYPE_FLOAT (5126) |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 97 | #define TINYGLTF_COMPONENT_TYPE_DOUBLE \ |
| 98 | (5130) // OpenGL double type. Note that some of glTF 2.0 validator does not |
| 99 | // support double type even the schema seems allow any value of |
| 100 | // integer: |
| 101 | // https://github.com/KhronosGroup/glTF/blob/b9884a2fd45130b4d673dd6c8a706ee21ee5c5f7/specification/2.0/schema/accessor.schema.json#L22 |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 102 | |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 103 | #define TINYGLTF_TEXTURE_FILTER_NEAREST (9728) |
| 104 | #define TINYGLTF_TEXTURE_FILTER_LINEAR (9729) |
| 105 | #define TINYGLTF_TEXTURE_FILTER_NEAREST_MIPMAP_NEAREST (9984) |
| 106 | #define TINYGLTF_TEXTURE_FILTER_LINEAR_MIPMAP_NEAREST (9985) |
| 107 | #define TINYGLTF_TEXTURE_FILTER_NEAREST_MIPMAP_LINEAR (9986) |
| 108 | #define TINYGLTF_TEXTURE_FILTER_LINEAR_MIPMAP_LINEAR (9987) |
| 109 | |
Cemalettin Dervis | 246d866 | 2017-12-07 20:29:51 +0100 | [diff] [blame] | 110 | #define TINYGLTF_TEXTURE_WRAP_REPEAT (10497) |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 111 | #define TINYGLTF_TEXTURE_WRAP_CLAMP_TO_EDGE (33071) |
Luke San Antonio Bialecki | 904612c | 2016-08-19 17:28:03 -0400 | [diff] [blame] | 112 | #define TINYGLTF_TEXTURE_WRAP_MIRRORED_REPEAT (33648) |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 113 | |
Luke San Antonio | cdf4cb7 | 2016-06-14 21:32:11 -0400 | [diff] [blame] | 114 | // Redeclarations of the above for technique.parameters. |
| 115 | #define TINYGLTF_PARAMETER_TYPE_BYTE (5120) |
| 116 | #define TINYGLTF_PARAMETER_TYPE_UNSIGNED_BYTE (5121) |
| 117 | #define TINYGLTF_PARAMETER_TYPE_SHORT (5122) |
| 118 | #define TINYGLTF_PARAMETER_TYPE_UNSIGNED_SHORT (5123) |
| 119 | #define TINYGLTF_PARAMETER_TYPE_INT (5124) |
| 120 | #define TINYGLTF_PARAMETER_TYPE_UNSIGNED_INT (5125) |
| 121 | #define TINYGLTF_PARAMETER_TYPE_FLOAT (5126) |
| 122 | |
| 123 | #define TINYGLTF_PARAMETER_TYPE_FLOAT_VEC2 (35664) |
| 124 | #define TINYGLTF_PARAMETER_TYPE_FLOAT_VEC3 (35665) |
| 125 | #define TINYGLTF_PARAMETER_TYPE_FLOAT_VEC4 (35666) |
| 126 | |
| 127 | #define TINYGLTF_PARAMETER_TYPE_INT_VEC2 (35667) |
| 128 | #define TINYGLTF_PARAMETER_TYPE_INT_VEC3 (35668) |
| 129 | #define TINYGLTF_PARAMETER_TYPE_INT_VEC4 (35669) |
| 130 | |
| 131 | #define TINYGLTF_PARAMETER_TYPE_BOOL (35670) |
| 132 | #define TINYGLTF_PARAMETER_TYPE_BOOL_VEC2 (35671) |
| 133 | #define TINYGLTF_PARAMETER_TYPE_BOOL_VEC3 (35672) |
| 134 | #define TINYGLTF_PARAMETER_TYPE_BOOL_VEC4 (35673) |
| 135 | |
| 136 | #define TINYGLTF_PARAMETER_TYPE_FLOAT_MAT2 (35674) |
| 137 | #define TINYGLTF_PARAMETER_TYPE_FLOAT_MAT3 (35675) |
| 138 | #define TINYGLTF_PARAMETER_TYPE_FLOAT_MAT4 (35676) |
| 139 | |
| 140 | #define TINYGLTF_PARAMETER_TYPE_SAMPLER_2D (35678) |
| 141 | |
| 142 | // End parameter types |
| 143 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 144 | #define TINYGLTF_TYPE_VEC2 (2) |
| 145 | #define TINYGLTF_TYPE_VEC3 (3) |
| 146 | #define TINYGLTF_TYPE_VEC4 (4) |
| 147 | #define TINYGLTF_TYPE_MAT2 (32 + 2) |
| 148 | #define TINYGLTF_TYPE_MAT3 (32 + 3) |
| 149 | #define TINYGLTF_TYPE_MAT4 (32 + 4) |
| 150 | #define TINYGLTF_TYPE_SCALAR (64 + 1) |
| 151 | #define TINYGLTF_TYPE_VECTOR (64 + 4) |
| 152 | #define TINYGLTF_TYPE_MATRIX (64 + 16) |
| 153 | |
| 154 | #define TINYGLTF_IMAGE_FORMAT_JPEG (0) |
| 155 | #define TINYGLTF_IMAGE_FORMAT_PNG (1) |
| 156 | #define TINYGLTF_IMAGE_FORMAT_BMP (2) |
| 157 | #define TINYGLTF_IMAGE_FORMAT_GIF (3) |
| 158 | |
Luke San Antonio | 6d616f5 | 2016-06-23 14:09:23 -0400 | [diff] [blame] | 159 | #define TINYGLTF_TEXTURE_FORMAT_ALPHA (6406) |
| 160 | #define TINYGLTF_TEXTURE_FORMAT_RGB (6407) |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 161 | #define TINYGLTF_TEXTURE_FORMAT_RGBA (6408) |
Luke San Antonio | 6d616f5 | 2016-06-23 14:09:23 -0400 | [diff] [blame] | 162 | #define TINYGLTF_TEXTURE_FORMAT_LUMINANCE (6409) |
| 163 | #define TINYGLTF_TEXTURE_FORMAT_LUMINANCE_ALPHA (6410) |
| 164 | |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 165 | #define TINYGLTF_TEXTURE_TARGET_TEXTURE2D (3553) |
| 166 | #define TINYGLTF_TEXTURE_TYPE_UNSIGNED_BYTE (5121) |
| 167 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 168 | #define TINYGLTF_TARGET_ARRAY_BUFFER (34962) |
| 169 | #define TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER (34963) |
| 170 | |
Luke San Antonio | cdf4cb7 | 2016-06-14 21:32:11 -0400 | [diff] [blame] | 171 | #define TINYGLTF_SHADER_TYPE_VERTEX_SHADER (35633) |
| 172 | #define TINYGLTF_SHADER_TYPE_FRAGMENT_SHADER (35632) |
| 173 | |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 174 | #define TINYGLTF_DOUBLE_EPS (1.e-12) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 175 | #define TINYGLTF_DOUBLE_EQUAL(a, b) (std::fabs((b) - (a)) < TINYGLTF_DOUBLE_EPS) |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 176 | |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 177 | #ifdef __ANDROID__ |
| 178 | #ifdef TINYGLTF_ANDROID_LOAD_FROM_ASSETS |
Serdar Kocdemir | 264ae4c | 2022-10-30 22:32:59 +0000 | [diff] [blame] | 179 | #ifdef TINYGLTF_IMPLEMENTATION |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 180 | AAssetManager *asset_manager = nullptr; |
Serdar Kocdemir | 264ae4c | 2022-10-30 22:32:59 +0000 | [diff] [blame] | 181 | #else |
| 182 | extern AAssetManager *asset_manager; |
| 183 | #endif |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 184 | #endif |
| 185 | #endif |
| 186 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 187 | typedef enum { |
Idriss Chaouch | 425195b | 2021-05-20 12:11:00 +0100 | [diff] [blame] | 188 | NULL_TYPE, |
| 189 | REAL_TYPE, |
| 190 | INT_TYPE, |
| 191 | BOOL_TYPE, |
| 192 | STRING_TYPE, |
| 193 | ARRAY_TYPE, |
| 194 | BINARY_TYPE, |
| 195 | OBJECT_TYPE |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 196 | } Type; |
| 197 | |
Nyall Dawson | 8c85d5e | 2023-08-28 12:56:09 +1000 | [diff] [blame] | 198 | typedef enum { |
Nyall Dawson | bbc1eae | 2023-09-03 09:05:32 +1000 | [diff] [blame] | 199 | Permissive, |
| 200 | Strict |
Nyall Dawson | 8c85d5e | 2023-08-28 12:56:09 +1000 | [diff] [blame] | 201 | } ParseStrictness; |
| 202 | |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 203 | static inline int32_t GetComponentSizeInBytes(uint32_t componentType) { |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 204 | if (componentType == TINYGLTF_COMPONENT_TYPE_BYTE) { |
| 205 | return 1; |
| 206 | } else if (componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE) { |
| 207 | return 1; |
| 208 | } else if (componentType == TINYGLTF_COMPONENT_TYPE_SHORT) { |
| 209 | return 2; |
| 210 | } else if (componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT) { |
| 211 | return 2; |
| 212 | } else if (componentType == TINYGLTF_COMPONENT_TYPE_INT) { |
| 213 | return 4; |
| 214 | } else if (componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT) { |
| 215 | return 4; |
| 216 | } else if (componentType == TINYGLTF_COMPONENT_TYPE_FLOAT) { |
| 217 | return 4; |
| 218 | } else if (componentType == TINYGLTF_COMPONENT_TYPE_DOUBLE) { |
| 219 | return 8; |
| 220 | } else { |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 221 | // Unknown component type |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 222 | return -1; |
| 223 | } |
| 224 | } |
| 225 | |
DingboDingboDingbo | 83ccb9f | 2019-08-29 18:49:15 -0400 | [diff] [blame] | 226 | static inline int32_t GetNumComponentsInType(uint32_t ty) { |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 227 | if (ty == TINYGLTF_TYPE_SCALAR) { |
| 228 | return 1; |
| 229 | } else if (ty == TINYGLTF_TYPE_VEC2) { |
| 230 | return 2; |
| 231 | } else if (ty == TINYGLTF_TYPE_VEC3) { |
| 232 | return 3; |
| 233 | } else if (ty == TINYGLTF_TYPE_VEC4) { |
| 234 | return 4; |
| 235 | } else if (ty == TINYGLTF_TYPE_MAT2) { |
| 236 | return 4; |
| 237 | } else if (ty == TINYGLTF_TYPE_MAT3) { |
| 238 | return 9; |
| 239 | } else if (ty == TINYGLTF_TYPE_MAT4) { |
| 240 | return 16; |
| 241 | } else { |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 242 | // Unknown component type |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 243 | return -1; |
| 244 | } |
| 245 | } |
| 246 | |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 247 | // TODO(syoyo): Move these functions to TinyGLTF class |
Selmar Kok | 0d0e97e | 2018-08-22 14:01:57 +0200 | [diff] [blame] | 248 | bool IsDataURI(const std::string &in); |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 249 | bool DecodeDataURI(std::vector<unsigned char> *out, std::string &mime_type, |
| 250 | const std::string &in, size_t reqBytes, bool checkSize); |
Selmar Kok | 0d0e97e | 2018-08-22 14:01:57 +0200 | [diff] [blame] | 251 | |
Syoyo Fujita | c8d3c76 | 2017-05-25 01:42:38 +0900 | [diff] [blame] | 252 | #ifdef __clang__ |
| 253 | #pragma clang diagnostic push |
| 254 | // Suppress warning for : static Value null_value |
Syoyo Fujita | c8d3c76 | 2017-05-25 01:42:38 +0900 | [diff] [blame] | 255 | #pragma clang diagnostic ignored "-Wexit-time-destructors" |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 256 | #pragma clang diagnostic ignored "-Wpadded" |
Syoyo Fujita | c8d3c76 | 2017-05-25 01:42:38 +0900 | [diff] [blame] | 257 | #endif |
| 258 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 259 | // Simple class to represent JSON object |
| 260 | class Value { |
| 261 | public: |
| 262 | typedef std::vector<Value> Array; |
| 263 | typedef std::map<std::string, Value> Object; |
| 264 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 265 | Value() = default; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 266 | |
| 267 | explicit Value(bool b) : type_(BOOL_TYPE) { boolean_value_ = b; } |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 268 | explicit Value(int i) : type_(INT_TYPE) { |
| 269 | int_value_ = i; |
| 270 | real_value_ = i; |
| 271 | } |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 272 | explicit Value(double n) : type_(REAL_TYPE) { real_value_ = n; } |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 273 | explicit Value(const std::string &s) : type_(STRING_TYPE) { |
| 274 | string_value_ = s; |
| 275 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 276 | explicit Value(std::string &&s) |
| 277 | : type_(STRING_TYPE), string_value_(std::move(s)) {} |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 278 | explicit Value(const char *s) : type_(STRING_TYPE) { string_value_ = s; } |
Syoyo Fujita | 7680abf | 2016-10-17 18:02:45 +0900 | [diff] [blame] | 279 | explicit Value(const unsigned char *p, size_t n) : type_(BINARY_TYPE) { |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 280 | binary_value_.resize(n); |
| 281 | memcpy(binary_value_.data(), p, n); |
| 282 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 283 | explicit Value(std::vector<unsigned char> &&v) noexcept |
| 284 | : type_(BINARY_TYPE), |
| 285 | binary_value_(std::move(v)) {} |
| 286 | explicit Value(const Array &a) : type_(ARRAY_TYPE) { array_value_ = a; } |
| 287 | explicit Value(Array &&a) noexcept : type_(ARRAY_TYPE), |
| 288 | array_value_(std::move(a)) {} |
jrkoonce | d1e1472 | 2019-08-27 11:51:02 -0500 | [diff] [blame] | 289 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 290 | explicit Value(const Object &o) : type_(OBJECT_TYPE) { object_value_ = o; } |
| 291 | explicit Value(Object &&o) noexcept : type_(OBJECT_TYPE), |
| 292 | object_value_(std::move(o)) {} |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 293 | |
| 294 | DEFAULT_METHODS(Value) |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 295 | |
Hill Ma | d1e3286 | 2021-02-20 22:30:44 -0800 | [diff] [blame] | 296 | char Type() const { return static_cast<char>(type_); } |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 297 | |
| 298 | bool IsBool() const { return (type_ == BOOL_TYPE); } |
| 299 | |
| 300 | bool IsInt() const { return (type_ == INT_TYPE); } |
| 301 | |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 302 | bool IsNumber() const { return (type_ == REAL_TYPE) || (type_ == INT_TYPE); } |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 303 | |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 304 | bool IsReal() const { return (type_ == REAL_TYPE); } |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 305 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 306 | bool IsString() const { return (type_ == STRING_TYPE); } |
| 307 | |
| 308 | bool IsBinary() const { return (type_ == BINARY_TYPE); } |
| 309 | |
| 310 | bool IsArray() const { return (type_ == ARRAY_TYPE); } |
| 311 | |
| 312 | bool IsObject() const { return (type_ == OBJECT_TYPE); } |
| 313 | |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 314 | // Use this function if you want to have number value as double. |
| 315 | double GetNumberAsDouble() const { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 316 | if (type_ == INT_TYPE) { |
| 317 | return double(int_value_); |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 318 | } else { |
| 319 | return real_value_; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 320 | } |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 321 | } |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 322 | |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 323 | // Use this function if you want to have number value as int. |
Syoyo Fujita | ff0a2e9 | 2020-05-11 19:07:00 +0900 | [diff] [blame] | 324 | // TODO(syoyo): Support int value larger than 32 bits |
| 325 | int GetNumberAsInt() const { |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 326 | if (type_ == REAL_TYPE) { |
| 327 | return int(real_value_); |
| 328 | } else { |
| 329 | return int_value_; |
| 330 | } |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 331 | } |
| 332 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 333 | // Accessor |
| 334 | template <typename T> |
| 335 | const T &Get() const; |
| 336 | template <typename T> |
| 337 | T &Get(); |
| 338 | |
| 339 | // Lookup value from an array |
James Ward | 9b4e1ea | 2024-03-25 17:50:33 +0000 | [diff] [blame] | 340 | const Value &Get(size_t idx) const { |
Syoyo Fujita | c8d3c76 | 2017-05-25 01:42:38 +0900 | [diff] [blame] | 341 | static Value null_value; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 342 | assert(IsArray()); |
James Ward | 9b4e1ea | 2024-03-25 17:50:33 +0000 | [diff] [blame] | 343 | return (idx < array_value_.size()) |
| 344 | ? array_value_[idx] |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 345 | : null_value; |
| 346 | } |
| 347 | |
| 348 | // Lookup value from a key-value pair |
| 349 | const Value &Get(const std::string &key) const { |
Syoyo Fujita | c8d3c76 | 2017-05-25 01:42:38 +0900 | [diff] [blame] | 350 | static Value null_value; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 351 | assert(IsObject()); |
| 352 | Object::const_iterator it = object_value_.find(key); |
| 353 | return (it != object_value_.end()) ? it->second : null_value; |
| 354 | } |
| 355 | |
| 356 | size_t ArrayLen() const { |
| 357 | if (!IsArray()) return 0; |
| 358 | return array_value_.size(); |
| 359 | } |
| 360 | |
| 361 | // Valid only for object type. |
| 362 | bool Has(const std::string &key) const { |
| 363 | if (!IsObject()) return false; |
| 364 | Object::const_iterator it = object_value_.find(key); |
| 365 | return (it != object_value_.end()) ? true : false; |
| 366 | } |
| 367 | |
| 368 | // List keys |
| 369 | std::vector<std::string> Keys() const { |
| 370 | std::vector<std::string> keys; |
| 371 | if (!IsObject()) return keys; // empty |
| 372 | |
| 373 | for (Object::const_iterator it = object_value_.begin(); |
| 374 | it != object_value_.end(); ++it) { |
| 375 | keys.push_back(it->first); |
| 376 | } |
| 377 | |
| 378 | return keys; |
| 379 | } |
| 380 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 381 | size_t Size() const { return (IsArray() ? ArrayLen() : Keys().size()); } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 382 | |
| 383 | bool operator==(const tinygltf::Value &other) const; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 384 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 385 | protected: |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 386 | int type_ = NULL_TYPE; |
Syoyo Fujita | 7680abf | 2016-10-17 18:02:45 +0900 | [diff] [blame] | 387 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 388 | int int_value_ = 0; |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 389 | double real_value_ = 0.0; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 390 | std::string string_value_; |
Syoyo Fujita | 7680abf | 2016-10-17 18:02:45 +0900 | [diff] [blame] | 391 | std::vector<unsigned char> binary_value_; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 392 | Array array_value_; |
| 393 | Object object_value_; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 394 | bool boolean_value_ = false; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 395 | }; |
| 396 | |
Syoyo Fujita | c8d3c76 | 2017-05-25 01:42:38 +0900 | [diff] [blame] | 397 | #ifdef __clang__ |
| 398 | #pragma clang diagnostic pop |
| 399 | #endif |
| 400 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 401 | #define TINYGLTF_VALUE_GET(ctype, var) \ |
| 402 | template <> \ |
| 403 | inline const ctype &Value::Get<ctype>() const { \ |
| 404 | return var; \ |
| 405 | } \ |
| 406 | template <> \ |
| 407 | inline ctype &Value::Get<ctype>() { \ |
| 408 | return var; \ |
| 409 | } |
| 410 | TINYGLTF_VALUE_GET(bool, boolean_value_) |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 411 | TINYGLTF_VALUE_GET(double, real_value_) |
Syoyo Fujita | 7680abf | 2016-10-17 18:02:45 +0900 | [diff] [blame] | 412 | TINYGLTF_VALUE_GET(int, int_value_) |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 413 | TINYGLTF_VALUE_GET(std::string, string_value_) |
Syoyo Fujita | 7680abf | 2016-10-17 18:02:45 +0900 | [diff] [blame] | 414 | TINYGLTF_VALUE_GET(std::vector<unsigned char>, binary_value_) |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 415 | TINYGLTF_VALUE_GET(Value::Array, array_value_) |
| 416 | TINYGLTF_VALUE_GET(Value::Object, object_value_) |
| 417 | #undef TINYGLTF_VALUE_GET |
| 418 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 419 | #ifdef __clang__ |
| 420 | #pragma clang diagnostic push |
| 421 | #pragma clang diagnostic ignored "-Wc++98-compat" |
| 422 | #pragma clang diagnostic ignored "-Wpadded" |
| 423 | #endif |
| 424 | |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 425 | /// Aggregate object for representing a color |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 426 | using ColorValue = std::array<double, 4>; |
| 427 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 428 | // === legacy interface ==== |
| 429 | // TODO(syoyo): Deprecate `Parameter` class. |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 430 | struct Parameter { |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 431 | bool bool_value = false; |
Ben Buzbee | f6af224 | 2018-04-25 15:13:05 -0700 | [diff] [blame] | 432 | bool has_number_value = false; |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 433 | std::string string_value; |
| 434 | std::vector<double> number_array; |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 435 | std::map<std::string, double> json_double_value; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 436 | double number_value = 0.0; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 437 | |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 438 | // context sensitive methods. depending the type of the Parameter you are |
| 439 | // accessing, these are either valid or not |
| 440 | // If this parameter represent a texture map in a material, will return the |
| 441 | // texture index |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 442 | |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 443 | /// Return the index of a texture if this Parameter is a texture map. |
| 444 | /// Returned value is only valid if the parameter represent a texture from a |
| 445 | /// material |
Arthur Brainville | 41fe772 | 2018-01-08 18:32:48 +0100 | [diff] [blame] | 446 | int TextureIndex() const { |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 447 | const auto it = json_double_value.find("index"); |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 448 | if (it != std::end(json_double_value)) { |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 449 | return int(it->second); |
| 450 | } |
| 451 | return -1; |
| 452 | } |
| 453 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 454 | /// Return the index of a texture coordinate set if this Parameter is a |
| 455 | /// texture map. Returned value is only valid if the parameter represent a |
| 456 | /// texture from a material |
Sascha Willems | eb01106 | 2019-02-23 21:15:45 +0100 | [diff] [blame] | 457 | int TextureTexCoord() const { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 458 | const auto it = json_double_value.find("texCoord"); |
| 459 | if (it != std::end(json_double_value)) { |
| 460 | return int(it->second); |
| 461 | } |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 462 | // As per the spec, if texCoord is omitted, this parameter is 0 |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 463 | return 0; |
Sascha Willems | eb01106 | 2019-02-23 21:15:45 +0100 | [diff] [blame] | 464 | } |
| 465 | |
Christophe | 820ede8 | 2019-07-04 15:21:21 +0900 | [diff] [blame] | 466 | /// Return the scale of a texture if this Parameter is a normal texture map. |
| 467 | /// Returned value is only valid if the parameter represent a normal texture |
| 468 | /// from a material |
| 469 | double TextureScale() const { |
| 470 | const auto it = json_double_value.find("scale"); |
| 471 | if (it != std::end(json_double_value)) { |
| 472 | return it->second; |
| 473 | } |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 474 | // As per the spec, if scale is omitted, this parameter is 1 |
Arthur Brainville | 8a98d98 | 2019-07-05 00:26:02 +0200 | [diff] [blame] | 475 | return 1; |
| 476 | } |
Arthur Brianville (Ybalrid) | 2a9d9de | 2019-07-05 00:30:47 +0200 | [diff] [blame] | 477 | |
Arthur Brainville | 8a98d98 | 2019-07-05 00:26:02 +0200 | [diff] [blame] | 478 | /// Return the strength of a texture if this Parameter is a an occlusion map. |
| 479 | /// Returned value is only valid if the parameter represent an occlusion map |
| 480 | /// from a material |
| 481 | double TextureStrength() const { |
| 482 | const auto it = json_double_value.find("strength"); |
| 483 | if (it != std::end(json_double_value)) { |
| 484 | return it->second; |
| 485 | } |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 486 | // As per the spec, if strength is omitted, this parameter is 1 |
Arthur Brainville | 8a98d98 | 2019-07-05 00:26:02 +0200 | [diff] [blame] | 487 | return 1; |
Christophe | 820ede8 | 2019-07-04 15:21:21 +0900 | [diff] [blame] | 488 | } |
| 489 | |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 490 | /// Material factor, like the roughness or metalness of a material |
| 491 | /// Returned value is only valid if the parameter represent a texture from a |
| 492 | /// material |
Ben Buzbee | f6af224 | 2018-04-25 15:13:05 -0700 | [diff] [blame] | 493 | double Factor() const { return number_value; } |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 494 | |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 495 | /// Return the color of a material |
| 496 | /// Returned value is only valid if the parameter represent a texture from a |
| 497 | /// material |
Arthur Brainville | 9585391 | 2018-01-08 18:37:44 +0100 | [diff] [blame] | 498 | ColorValue ColorFactor() const { |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 499 | return { |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 500 | {// this aggregate initialize the std::array object, and uses C++11 RVO. |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 501 | number_array[0], number_array[1], number_array[2], |
| 502 | (number_array.size() > 3 ? number_array[3] : 1.0)}}; |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 503 | } |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 504 | |
Selmar Kok | ff2b1f9 | 2019-10-21 17:58:09 +0200 | [diff] [blame] | 505 | Parameter() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 506 | DEFAULT_METHODS(Parameter) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 507 | bool operator==(const Parameter &) const; |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 508 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 509 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 510 | #ifdef __clang__ |
| 511 | #pragma clang diagnostic pop |
| 512 | #endif |
| 513 | |
| 514 | #ifdef __clang__ |
| 515 | #pragma clang diagnostic push |
| 516 | #pragma clang diagnostic ignored "-Wpadded" |
| 517 | #endif |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 518 | |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 519 | typedef std::map<std::string, Parameter> ParameterMap; |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 520 | typedef std::map<std::string, Value> ExtensionMap; |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 521 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 522 | struct AnimationChannel { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 523 | int sampler{-1}; // required |
| 524 | int target_node{-1}; // optional index of the node to target (alternative |
Jack Mousseau | 283b552 | 2023-01-15 11:45:45 -0800 | [diff] [blame] | 525 | // target should be provided by extension) |
| 526 | std::string target_path; // required with standard values of ["translation", |
| 527 | // "rotation", "scale", "weights"] |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 528 | Value extras; |
Selmar Kok | 4e2988e | 2019-08-16 14:08:08 +0200 | [diff] [blame] | 529 | ExtensionMap extensions; |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 530 | Value target_extras; |
Selmar Kok | 973d9b3 | 2020-01-21 18:45:24 +0100 | [diff] [blame] | 531 | ExtensionMap target_extensions; |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 532 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 533 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 534 | std::string extras_json_string; |
| 535 | std::string extensions_json_string; |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 536 | std::string target_extras_json_string; |
Selmar Kok | 973d9b3 | 2020-01-21 18:45:24 +0100 | [diff] [blame] | 537 | std::string target_extensions_json_string; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 538 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 539 | AnimationChannel() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 540 | DEFAULT_METHODS(AnimationChannel) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 541 | bool operator==(const AnimationChannel &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 542 | }; |
| 543 | |
| 544 | struct AnimationSampler { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 545 | int input{-1}; // required |
| 546 | int output{-1}; // required |
Arthur Brianville (Ybalrid) | 2a9d9de | 2019-07-05 00:30:47 +0200 | [diff] [blame] | 547 | std::string interpolation; // "LINEAR", "STEP","CUBICSPLINE" or user defined |
| 548 | // string. default "LINEAR" |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 549 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 550 | ExtensionMap extensions; |
| 551 | |
| 552 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 553 | std::string extras_json_string; |
| 554 | std::string extensions_json_string; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 555 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 556 | AnimationSampler() : interpolation("LINEAR") {} |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 557 | DEFAULT_METHODS(AnimationSampler) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 558 | bool operator==(const AnimationSampler &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 559 | }; |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 560 | |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 561 | struct Animation { |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 562 | std::string name; |
| 563 | std::vector<AnimationChannel> channels; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 564 | std::vector<AnimationSampler> samplers; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 565 | Value extras; |
Selmar Kok | 4e2988e | 2019-08-16 14:08:08 +0200 | [diff] [blame] | 566 | ExtensionMap extensions; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 567 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 568 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 569 | std::string extras_json_string; |
| 570 | std::string extensions_json_string; |
| 571 | |
Selmar Kok | ff2b1f9 | 2019-10-21 17:58:09 +0200 | [diff] [blame] | 572 | Animation() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 573 | DEFAULT_METHODS(Animation) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 574 | bool operator==(const Animation &) const; |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 575 | }; |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 576 | |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 577 | struct Skin { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 578 | std::string name; |
Bryn Lloyd | cb6a707 | 2023-06-21 22:30:14 +0200 | [diff] [blame] | 579 | int inverseBindMatrices{-1}; // required here but not in the spec |
| 580 | int skeleton{-1}; // The index of the node used as a skeleton root |
| 581 | std::vector<int> joints; // Indices of skeleton nodes |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 582 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 583 | Value extras; |
| 584 | ExtensionMap extensions; |
| 585 | |
| 586 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 587 | std::string extras_json_string; |
| 588 | std::string extensions_json_string; |
| 589 | |
Bryn Lloyd | cb6a707 | 2023-06-21 22:30:14 +0200 | [diff] [blame] | 590 | Skin() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 591 | DEFAULT_METHODS(Skin) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 592 | bool operator==(const Skin &) const; |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 593 | }; |
| 594 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 595 | struct Sampler { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 596 | std::string name; |
Syoyo Fujita | ee179b2 | 2019-08-16 13:11:30 +0900 | [diff] [blame] | 597 | // glTF 2.0 spec does not define default value for `minFilter` and |
| 598 | // `magFilter`. Set -1 in TinyGLTF(issue #186) |
| 599 | int minFilter = |
| 600 | -1; // optional. -1 = no filter defined. ["NEAREST", "LINEAR", |
zz | 8a35b8f | 2021-05-14 13:49:33 +0800 | [diff] [blame] | 601 | // "NEAREST_MIPMAP_NEAREST", "LINEAR_MIPMAP_NEAREST", |
Syoyo Fujita | ee179b2 | 2019-08-16 13:11:30 +0900 | [diff] [blame] | 602 | // "NEAREST_MIPMAP_LINEAR", "LINEAR_MIPMAP_LINEAR"] |
| 603 | int magFilter = |
| 604 | -1; // optional. -1 = no filter defined. ["NEAREST", "LINEAR"] |
| 605 | int wrapS = |
| 606 | TINYGLTF_TEXTURE_WRAP_REPEAT; // ["CLAMP_TO_EDGE", "MIRRORED_REPEAT", |
| 607 | // "REPEAT"], default "REPEAT" |
| 608 | int wrapT = |
| 609 | TINYGLTF_TEXTURE_WRAP_REPEAT; // ["CLAMP_TO_EDGE", "MIRRORED_REPEAT", |
| 610 | // "REPEAT"], default "REPEAT" |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 611 | // int wrapR = TINYGLTF_TEXTURE_WRAP_REPEAT; // TinyGLTF extension. currently |
| 612 | // not used. |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 613 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 614 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 615 | ExtensionMap extensions; |
| 616 | |
| 617 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 618 | std::string extras_json_string; |
| 619 | std::string extensions_json_string; |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 620 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 621 | Sampler() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 622 | DEFAULT_METHODS(Sampler) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 623 | bool operator==(const Sampler &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 624 | }; |
| 625 | |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 626 | struct Image { |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 627 | std::string name; |
Bryn Lloyd | cb6a707 | 2023-06-21 22:30:14 +0200 | [diff] [blame] | 628 | int width{-1}; |
| 629 | int height{-1}; |
| 630 | int component{-1}; |
| 631 | int bits{-1}; // bit depth per channel. 8(byte), 16 or 32. |
| 632 | int pixel_type{-1}; // pixel type(TINYGLTF_COMPONENT_TYPE_***). usually |
| 633 | // UBYTE(bits = 8) or USHORT(bits = 16) |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 634 | std::vector<unsigned char> image; |
Bryn Lloyd | cb6a707 | 2023-06-21 22:30:14 +0200 | [diff] [blame] | 635 | int bufferView{-1}; // (required if no uri) |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 636 | std::string mimeType; // (required if no uri) ["image/jpeg", "image/png", |
| 637 | // "image/bmp", "image/gif"] |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 638 | std::string uri; // (required if no mimeType) uri is not decoded(e.g. |
| 639 | // whitespace may be represented as %20) |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 640 | Value extras; |
Syoyo Fujita | 3e53feb | 2018-09-02 15:36:17 +0900 | [diff] [blame] | 641 | ExtensionMap extensions; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 642 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 643 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 644 | std::string extras_json_string; |
| 645 | std::string extensions_json_string; |
| 646 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 647 | // When this flag is true, data is stored to `image` in as-is format(e.g. jpeg |
| 648 | // compressed for "image/jpeg" mime) This feature is good if you use custom |
| 649 | // image loader function. (e.g. delayed decoding of images for faster glTF |
| 650 | // parsing) Default parser for Image does not provide as-is loading feature at |
| 651 | // the moment. (You can manipulate this by providing your own LoadImageData |
| 652 | // function) |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame] | 653 | bool as_is{false}; |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 654 | |
Bryn Lloyd | cb6a707 | 2023-06-21 22:30:14 +0200 | [diff] [blame] | 655 | Image() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 656 | DEFAULT_METHODS(Image) |
jrkoonce | d1e1472 | 2019-08-27 11:51:02 -0500 | [diff] [blame] | 657 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 658 | bool operator==(const Image &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 659 | }; |
| 660 | |
| 661 | struct Texture { |
Vladimír Vondruš | 239be2c | 2018-07-24 23:23:56 +0200 | [diff] [blame] | 662 | std::string name; |
| 663 | |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame] | 664 | int sampler{-1}; |
| 665 | int source{-1}; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 666 | Value extras; |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 667 | ExtensionMap extensions; |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 668 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 669 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 670 | std::string extras_json_string; |
| 671 | std::string extensions_json_string; |
| 672 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 673 | Texture() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 674 | DEFAULT_METHODS(Texture) |
jrkoonce | d1e1472 | 2019-08-27 11:51:02 -0500 | [diff] [blame] | 675 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 676 | bool operator==(const Texture &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 677 | }; |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 678 | |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 679 | struct TextureInfo { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 680 | int index{-1}; // required. |
| 681 | int texCoord{0}; // The set index of texture's TEXCOORD attribute used for |
| 682 | // texture coordinate mapping. |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 683 | |
| 684 | Value extras; |
| 685 | ExtensionMap extensions; |
| 686 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 687 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 688 | std::string extras_json_string; |
| 689 | std::string extensions_json_string; |
| 690 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 691 | TextureInfo() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 692 | DEFAULT_METHODS(TextureInfo) |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 693 | bool operator==(const TextureInfo &) const; |
| 694 | }; |
| 695 | |
| 696 | struct NormalTextureInfo { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 697 | int index{-1}; // required |
| 698 | int texCoord{0}; // The set index of texture's TEXCOORD attribute used for |
| 699 | // texture coordinate mapping. |
| 700 | double scale{ |
| 701 | 1.0}; // scaledNormal = normalize((<sampled normal texture value> |
| 702 | // * 2.0 - 1.0) * vec3(<normal scale>, <normal scale>, 1.0)) |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 703 | |
| 704 | Value extras; |
| 705 | ExtensionMap extensions; |
| 706 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 707 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 708 | std::string extras_json_string; |
| 709 | std::string extensions_json_string; |
| 710 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 711 | NormalTextureInfo() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 712 | DEFAULT_METHODS(NormalTextureInfo) |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 713 | bool operator==(const NormalTextureInfo &) const; |
| 714 | }; |
| 715 | |
| 716 | struct OcclusionTextureInfo { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 717 | int index{-1}; // required |
| 718 | int texCoord{0}; // The set index of texture's TEXCOORD attribute used for |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 719 | // texture coordinate mapping. |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 720 | double strength{1.0}; // occludedColor = lerp(color, color * <sampled |
| 721 | // occlusion texture value>, <occlusion strength>) |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 722 | |
| 723 | Value extras; |
| 724 | ExtensionMap extensions; |
| 725 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 726 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 727 | std::string extras_json_string; |
| 728 | std::string extensions_json_string; |
| 729 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 730 | OcclusionTextureInfo() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 731 | DEFAULT_METHODS(OcclusionTextureInfo) |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 732 | bool operator==(const OcclusionTextureInfo &) const; |
| 733 | }; |
| 734 | |
| 735 | // pbrMetallicRoughness class defined in glTF 2.0 spec. |
| 736 | struct PbrMetallicRoughness { |
Thomas Gamper | 1f42c96 | 2023-11-22 15:59:13 +0100 | [diff] [blame] | 737 | std::vector<double> baseColorFactor{1.0, 1.0, 1.0, 1.0}; // len = 4. default [1,1,1,1] |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 738 | TextureInfo baseColorTexture; |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame] | 739 | double metallicFactor{1.0}; // default 1 |
| 740 | double roughnessFactor{1.0}; // default 1 |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 741 | TextureInfo metallicRoughnessTexture; |
| 742 | |
| 743 | Value extras; |
| 744 | ExtensionMap extensions; |
| 745 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 746 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 747 | std::string extras_json_string; |
| 748 | std::string extensions_json_string; |
| 749 | |
Thomas Gamper | 1f42c96 | 2023-11-22 15:59:13 +0100 | [diff] [blame] | 750 | PbrMetallicRoughness() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 751 | DEFAULT_METHODS(PbrMetallicRoughness) |
Thomas Gamper | 1f42c96 | 2023-11-22 15:59:13 +0100 | [diff] [blame] | 752 | |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 753 | bool operator==(const PbrMetallicRoughness &) const; |
| 754 | }; |
| 755 | |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 756 | // Each extension should be stored in a ParameterMap. |
| 757 | // members not in the values could be included in the ParameterMap |
| 758 | // to keep a single material model |
| 759 | struct Material { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 760 | std::string name; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 761 | |
Thomas Gamper | 1f42c96 | 2023-11-22 15:59:13 +0100 | [diff] [blame] | 762 | std::vector<double> emissiveFactor{0.0, 0.0, 0.0}; // length 3. default [0, 0, 0] |
| 763 | std::string alphaMode{"OPAQUE"}; // default "OPAQUE" |
| 764 | double alphaCutoff{0.5}; // default 0.5 |
| 765 | bool doubleSided{false}; // default false |
Thomas Gamper | a42263b | 2024-02-05 15:42:49 +0100 | [diff] [blame] | 766 | std::vector<int> lods; // level of detail materials (MSFT_lod) |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 767 | |
| 768 | PbrMetallicRoughness pbrMetallicRoughness; |
| 769 | |
| 770 | NormalTextureInfo normalTexture; |
| 771 | OcclusionTextureInfo occlusionTexture; |
| 772 | TextureInfo emissiveTexture; |
| 773 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 774 | // For backward compatibility |
| 775 | // TODO(syoyo): Remove `values` and `additionalValues` in the next release. |
| 776 | ParameterMap values; |
| 777 | ParameterMap additionalValues; |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 778 | |
| 779 | ExtensionMap extensions; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 780 | Value extras; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 781 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 782 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 783 | std::string extras_json_string; |
| 784 | std::string extensions_json_string; |
| 785 | |
Thomas Gamper | 1f42c96 | 2023-11-22 15:59:13 +0100 | [diff] [blame] | 786 | Material() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 787 | DEFAULT_METHODS(Material) |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 788 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 789 | bool operator==(const Material &) const; |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 790 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 791 | |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 792 | struct BufferView { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 793 | std::string name; |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 794 | int buffer{-1}; // Required |
Syoyo Fujita | 4e47bc7 | 2020-01-02 22:07:25 +0900 | [diff] [blame] | 795 | size_t byteOffset{0}; // minimum 0, default 0 |
| 796 | size_t byteLength{0}; // required, minimum 1. 0 = invalid |
| 797 | size_t byteStride{0}; // minimum 4, maximum 252 (multiple of 4), default 0 = |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 798 | // understood to be tightly packed |
| 799 | int target{0}; // ["ARRAY_BUFFER", "ELEMENT_ARRAY_BUFFER"] for vertex indices |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 800 | // or attribs. Could be 0 for other data |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 801 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 802 | ExtensionMap extensions; |
| 803 | |
| 804 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 805 | std::string extras_json_string; |
| 806 | std::string extensions_json_string; |
| 807 | |
Syoyo Fujita | 4e47bc7 | 2020-01-02 22:07:25 +0900 | [diff] [blame] | 808 | bool dracoDecoded{false}; // Flag indicating this has been draco decoded |
Syoyo Fujita | d17ff66 | 2017-05-29 02:53:12 +0900 | [diff] [blame] | 809 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 810 | BufferView() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 811 | DEFAULT_METHODS(BufferView) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 812 | bool operator==(const BufferView &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 813 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 814 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 815 | struct Accessor { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 816 | int bufferView{-1}; // optional in spec but required here since sparse |
| 817 | // accessor are not supported |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 818 | std::string name; |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame] | 819 | size_t byteOffset{0}; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 820 | bool normalized{false}; // optional. |
| 821 | int componentType{-1}; // (required) One of TINYGLTF_COMPONENT_TYPE_*** |
| 822 | size_t count{0}; // required |
| 823 | int type{-1}; // (required) One of TINYGLTF_TYPE_*** .. |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 824 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 825 | ExtensionMap extensions; |
| 826 | |
| 827 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 828 | std::string extras_json_string; |
| 829 | std::string extensions_json_string; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 830 | |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 831 | std::vector<double> |
| 832 | minValues; // optional. integer value is promoted to double |
| 833 | std::vector<double> |
| 834 | maxValues; // optional. integer value is promoted to double |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 835 | |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 836 | struct Sparse { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 837 | int count; |
| 838 | bool isSparse; |
| 839 | struct { |
emimvi | 759976e | 2023-09-02 09:39:53 +0200 | [diff] [blame] | 840 | size_t byteOffset; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 841 | int bufferView; |
| 842 | int componentType; // a TINYGLTF_COMPONENT_TYPE_ value |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 843 | Value extras; |
| 844 | ExtensionMap extensions; |
| 845 | std::string extras_json_string; |
| 846 | std::string extensions_json_string; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 847 | } indices; |
| 848 | struct { |
| 849 | int bufferView; |
emimvi | 759976e | 2023-09-02 09:39:53 +0200 | [diff] [blame] | 850 | size_t byteOffset; |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 851 | Value extras; |
| 852 | ExtensionMap extensions; |
| 853 | std::string extras_json_string; |
| 854 | std::string extensions_json_string; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 855 | } values; |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 856 | Value extras; |
| 857 | ExtensionMap extensions; |
| 858 | std::string extras_json_string; |
| 859 | std::string extensions_json_string; |
| 860 | }; |
| 861 | |
| 862 | Sparse sparse; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 863 | |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 864 | /// |
| 865 | /// Utility function to compute byteStride for a given bufferView object. |
| 866 | /// Returns -1 upon invalid glTF value or parameter configuration. |
| 867 | /// |
| 868 | int ByteStride(const BufferView &bufferViewObject) const { |
| 869 | if (bufferViewObject.byteStride == 0) { |
| 870 | // Assume data is tightly packed. |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 871 | int componentSizeInBytes = |
| 872 | GetComponentSizeInBytes(static_cast<uint32_t>(componentType)); |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 873 | if (componentSizeInBytes <= 0) { |
| 874 | return -1; |
| 875 | } |
| 876 | |
Syoyo Fujita | 81d75df | 2019-08-30 19:19:52 +0900 | [diff] [blame] | 877 | int numComponents = GetNumComponentsInType(static_cast<uint32_t>(type)); |
| 878 | if (numComponents <= 0) { |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 879 | return -1; |
| 880 | } |
| 881 | |
Syoyo Fujita | 81d75df | 2019-08-30 19:19:52 +0900 | [diff] [blame] | 882 | return componentSizeInBytes * numComponents; |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 883 | } else { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 884 | // Check if byteStride is a multiple of the size of the accessor's |
| 885 | // component type. |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 886 | int componentSizeInBytes = |
| 887 | GetComponentSizeInBytes(static_cast<uint32_t>(componentType)); |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 888 | if (componentSizeInBytes <= 0) { |
| 889 | return -1; |
| 890 | } |
| 891 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 892 | if ((bufferViewObject.byteStride % uint32_t(componentSizeInBytes)) != 0) { |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 893 | return -1; |
| 894 | } |
Arthur Brainville | 8ce4e54 | 2018-01-10 01:27:12 +0100 | [diff] [blame] | 895 | return static_cast<int>(bufferViewObject.byteStride); |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 896 | } |
| 897 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 898 | // unreachable return 0; |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 899 | } |
| 900 | |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 901 | Accessor() |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 902 | |
| 903 | { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 904 | sparse.isSparse = false; |
| 905 | } |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 906 | DEFAULT_METHODS(Accessor) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 907 | bool operator==(const tinygltf::Accessor &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 908 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 909 | |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 910 | struct PerspectiveCamera { |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame] | 911 | double aspectRatio{0.0}; // min > 0 |
| 912 | double yfov{0.0}; // required. min > 0 |
| 913 | double zfar{0.0}; // min > 0 |
| 914 | double znear{0.0}; // required. min > 0 |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 915 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 916 | PerspectiveCamera() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 917 | DEFAULT_METHODS(PerspectiveCamera) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 918 | bool operator==(const PerspectiveCamera &) const; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 919 | |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 920 | ExtensionMap extensions; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 921 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 922 | |
| 923 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 924 | std::string extras_json_string; |
| 925 | std::string extensions_json_string; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 926 | }; |
Aurélien Chatelain | a25e495 | 2017-05-24 09:52:28 +0000 | [diff] [blame] | 927 | |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 928 | struct OrthographicCamera { |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame] | 929 | double xmag{0.0}; // required. must not be zero. |
| 930 | double ymag{0.0}; // required. must not be zero. |
| 931 | double zfar{0.0}; // required. `zfar` must be greater than `znear`. |
| 932 | double znear{0.0}; // required |
Aurélien Chatelain | a25e495 | 2017-05-24 09:52:28 +0000 | [diff] [blame] | 933 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 934 | OrthographicCamera() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 935 | DEFAULT_METHODS(OrthographicCamera) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 936 | bool operator==(const OrthographicCamera &) const; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 937 | |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 938 | ExtensionMap extensions; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 939 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 940 | |
| 941 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 942 | std::string extras_json_string; |
| 943 | std::string extensions_json_string; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 944 | }; |
| 945 | |
| 946 | struct Camera { |
| 947 | std::string type; // required. "perspective" or "orthographic" |
| 948 | std::string name; |
| 949 | |
| 950 | PerspectiveCamera perspective; |
| 951 | OrthographicCamera orthographic; |
| 952 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 953 | Camera() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 954 | DEFAULT_METHODS(Camera) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 955 | bool operator==(const Camera &) const; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 956 | |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 957 | ExtensionMap extensions; |
Aurélien Chatelain | a25e495 | 2017-05-24 09:52:28 +0000 | [diff] [blame] | 958 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 959 | |
| 960 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 961 | std::string extras_json_string; |
| 962 | std::string extensions_json_string; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 963 | }; |
| 964 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 965 | struct Primitive { |
| 966 | std::map<std::string, int> attributes; // (required) A dictionary object of |
| 967 | // integer, where each integer |
| 968 | // is the index of the accessor |
| 969 | // containing an attribute. |
Bryn Lloyd | cb6a707 | 2023-06-21 22:30:14 +0200 | [diff] [blame] | 970 | int material{-1}; // The index of the material to apply to this primitive |
| 971 | // when rendering. |
| 972 | int indices{-1}; // The index of the accessor that contains the indices. |
| 973 | int mode{-1}; // one of TINYGLTF_MODE_*** |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 974 | std::vector<std::map<std::string, int> > targets; // array of morph targets, |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 975 | // where each target is a dict with attributes in ["POSITION, "NORMAL", |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 976 | // "TANGENT"] pointing |
| 977 | // to their corresponding accessors |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 978 | ExtensionMap extensions; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 979 | Value extras; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 980 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 981 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 982 | std::string extras_json_string; |
| 983 | std::string extensions_json_string; |
| 984 | |
Bryn Lloyd | cb6a707 | 2023-06-21 22:30:14 +0200 | [diff] [blame] | 985 | Primitive() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 986 | DEFAULT_METHODS(Primitive) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 987 | bool operator==(const Primitive &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 988 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 989 | |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 990 | struct Mesh { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 991 | std::string name; |
| 992 | std::vector<Primitive> primitives; |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 993 | std::vector<double> weights; // weights to be applied to the Morph Targets |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 994 | ExtensionMap extensions; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 995 | Value extras; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 996 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 997 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 998 | std::string extras_json_string; |
| 999 | std::string extensions_json_string; |
| 1000 | |
jrkoonce | d1e1472 | 2019-08-27 11:51:02 -0500 | [diff] [blame] | 1001 | Mesh() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1002 | DEFAULT_METHODS(Mesh) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1003 | bool operator==(const Mesh &) const; |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 1004 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1005 | |
| 1006 | class Node { |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 1007 | public: |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1008 | Node() = default; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1009 | |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1010 | DEFAULT_METHODS(Node) |
Syoyo Fujita | d6b0d0a | 2019-06-29 17:31:13 +0900 | [diff] [blame] | 1011 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1012 | bool operator==(const Node &) const; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1013 | |
Syoyo Fujita | 7a570c8 | 2023-06-19 21:52:13 +0900 | [diff] [blame] | 1014 | int camera{-1}; // the index of the camera referenced by this node |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1015 | |
| 1016 | std::string name; |
Syoyo Fujita | 7a570c8 | 2023-06-19 21:52:13 +0900 | [diff] [blame] | 1017 | int skin{-1}; |
| 1018 | int mesh{-1}; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1019 | int light{-1}; // light source index (KHR_lights_punctual) |
| 1020 | int emitter{-1}; // audio emitter index (KHR_audio) |
Thomas Gamper | a42263b | 2024-02-05 15:42:49 +0100 | [diff] [blame] | 1021 | std::vector<int> lods; // level of detail nodes (MSFT_lod) |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1022 | std::vector<int> children; |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 1023 | std::vector<double> rotation; // length must be 0 or 4 |
| 1024 | std::vector<double> scale; // length must be 0 or 3 |
| 1025 | std::vector<double> translation; // length must be 0 or 3 |
| 1026 | std::vector<double> matrix; // length must be 0 or 16 |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 1027 | std::vector<double> weights; // The weights of the instantiated Morph Target |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 1028 | |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 1029 | ExtensionMap extensions; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 1030 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1031 | |
| 1032 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1033 | std::string extras_json_string; |
| 1034 | std::string extensions_json_string; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1035 | }; |
| 1036 | |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 1037 | struct Buffer { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1038 | std::string name; |
| 1039 | std::vector<unsigned char> data; |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 1040 | std::string |
| 1041 | uri; // considered as required here but not in the spec (need to clarify) |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 1042 | // uri is not decoded(e.g. whitespace may be represented as %20) |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 1043 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1044 | ExtensionMap extensions; |
| 1045 | |
| 1046 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1047 | std::string extras_json_string; |
| 1048 | std::string extensions_json_string; |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1049 | |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1050 | Buffer() = default; |
| 1051 | DEFAULT_METHODS(Buffer) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1052 | bool operator==(const Buffer &) const; |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 1053 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1054 | |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 1055 | struct Asset { |
Syoyo Fujita | b702de7 | 2021-03-02 19:08:29 +0900 | [diff] [blame] | 1056 | std::string version = "2.0"; // required |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1057 | std::string generator; |
Aurélien Chatelain | d48bbdc | 2017-05-24 09:54:18 +0000 | [diff] [blame] | 1058 | std::string minVersion; |
| 1059 | std::string copyright; |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 1060 | ExtensionMap extensions; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 1061 | Value extras; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1062 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1063 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1064 | std::string extras_json_string; |
| 1065 | std::string extensions_json_string; |
| 1066 | |
jrkoonce | d1e1472 | 2019-08-27 11:51:02 -0500 | [diff] [blame] | 1067 | Asset() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1068 | DEFAULT_METHODS(Asset) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1069 | bool operator==(const Asset &) const; |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 1070 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1071 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1072 | struct Scene { |
| 1073 | std::string name; |
| 1074 | std::vector<int> nodes; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1075 | std::vector<int> audioEmitters; // KHR_audio global emitters |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1076 | |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 1077 | ExtensionMap extensions; |
| 1078 | Value extras; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1079 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1080 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1081 | std::string extras_json_string; |
| 1082 | std::string extensions_json_string; |
| 1083 | |
jrkoonce | d1e1472 | 2019-08-27 11:51:02 -0500 | [diff] [blame] | 1084 | Scene() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1085 | DEFAULT_METHODS(Scene) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1086 | bool operator==(const Scene &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1087 | }; |
| 1088 | |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 1089 | struct SpotLight { |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame] | 1090 | double innerConeAngle{0.0}; |
| 1091 | double outerConeAngle{0.7853981634}; |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 1092 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1093 | SpotLight() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1094 | DEFAULT_METHODS(SpotLight) |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 1095 | bool operator==(const SpotLight &) const; |
| 1096 | |
| 1097 | ExtensionMap extensions; |
| 1098 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1099 | |
| 1100 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1101 | std::string extras_json_string; |
| 1102 | std::string extensions_json_string; |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 1103 | }; |
| 1104 | |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 1105 | struct Light { |
| 1106 | std::string name; |
| 1107 | std::vector<double> color; |
Syoyo Fujita | 3dad303 | 2020-05-13 21:22:23 +0900 | [diff] [blame] | 1108 | double intensity{1.0}; |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 1109 | std::string type; |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 1110 | double range{0.0}; // 0.0 = infinite |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 1111 | SpotLight spot; |
| 1112 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1113 | Light() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1114 | DEFAULT_METHODS(Light) |
Arthur Brainville (Ybalrid) | 9eeaf20 | 2019-09-05 13:02:05 +0200 | [diff] [blame] | 1115 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1116 | bool operator==(const Light &) const; |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 1117 | |
| 1118 | ExtensionMap extensions; |
| 1119 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1120 | |
| 1121 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1122 | std::string extras_json_string; |
| 1123 | std::string extensions_json_string; |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 1124 | }; |
| 1125 | |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1126 | struct PositionalEmitter { |
| 1127 | double coneInnerAngle{6.283185307179586}; |
| 1128 | double coneOuterAngle{6.283185307179586}; |
| 1129 | double coneOuterGain{0.0}; |
| 1130 | double maxDistance{100.0}; |
| 1131 | double refDistance{1.0}; |
| 1132 | double rolloffFactor{1.0}; |
| 1133 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1134 | PositionalEmitter() = default; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1135 | DEFAULT_METHODS(PositionalEmitter) |
| 1136 | bool operator==(const PositionalEmitter &) const; |
| 1137 | |
| 1138 | ExtensionMap extensions; |
| 1139 | Value extras; |
| 1140 | |
| 1141 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1142 | std::string extras_json_string; |
| 1143 | std::string extensions_json_string; |
| 1144 | }; |
| 1145 | |
| 1146 | struct AudioEmitter { |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 1147 | std::string name; |
| 1148 | double gain{1.0}; |
| 1149 | bool loop{false}; |
| 1150 | bool playing{false}; |
| 1151 | std::string |
| 1152 | type; // positional - Positional audio emitters. Using sound cones, the |
| 1153 | // orientation is +Z having the same front side for a glTF asset. |
| 1154 | // global - Global audio emitters are not affected by the position |
| 1155 | // of audio listeners. coneInnerAngle, coneOuterAngle, |
| 1156 | // coneOuterGain, distanceModel, maxDistance, refDistance, and |
| 1157 | // rolloffFactor should all be ignored when set. |
| 1158 | std::string |
| 1159 | distanceModel; // linear - A linear distance model calculating the |
| 1160 | // gain induced by the distance according to: 1.0 |
| 1161 | // - rolloffFactor * (distance - refDistance) / |
| 1162 | // (maxDistance - refDistance) |
| 1163 | // inverse - (default) An inverse distance model |
| 1164 | // calculating the gain induced by the distance according |
| 1165 | // to: refDistance / (refDistance + rolloffFactor * |
| 1166 | // (Math.max(distance, refDistance) - refDistance)) |
| 1167 | // exponential - An exponential distance model calculating |
| 1168 | // the gain induced by the distance according to: |
| 1169 | // pow((Math.max(distance, refDistance) / refDistance, |
| 1170 | // -rolloffFactor)) |
| 1171 | PositionalEmitter positional; |
| 1172 | int source{-1}; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1173 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1174 | AudioEmitter() : type("global"), distanceModel("inverse") {} |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 1175 | DEFAULT_METHODS(AudioEmitter) |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1176 | |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 1177 | bool operator==(const AudioEmitter &) const; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1178 | |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 1179 | ExtensionMap extensions; |
| 1180 | Value extras; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1181 | |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 1182 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1183 | std::string extras_json_string; |
| 1184 | std::string extensions_json_string; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1185 | }; |
| 1186 | |
| 1187 | struct AudioSource { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1188 | std::string name; |
| 1189 | std::string uri; |
| 1190 | int bufferView{-1}; // (required if no uri) |
| 1191 | std::string |
| 1192 | mimeType; // (required if no uri) The audio's MIME type. Required if |
| 1193 | // bufferView is defined. Unless specified by another |
| 1194 | // extension, the only supported mimeType is audio/mpeg. |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1195 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1196 | AudioSource() = default; |
| 1197 | DEFAULT_METHODS(AudioSource) |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1198 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1199 | bool operator==(const AudioSource &) const; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1200 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1201 | Value extras; |
| 1202 | ExtensionMap extensions; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1203 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1204 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1205 | std::string extras_json_string; |
| 1206 | std::string extensions_json_string; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1207 | }; |
| 1208 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1209 | class Model { |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 1210 | public: |
Selmar Kok | ff2b1f9 | 2019-10-21 17:58:09 +0200 | [diff] [blame] | 1211 | Model() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1212 | DEFAULT_METHODS(Model) |
Syoyo Fujita | d6b0d0a | 2019-06-29 17:31:13 +0900 | [diff] [blame] | 1213 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1214 | bool operator==(const Model &) const; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1215 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1216 | std::vector<Accessor> accessors; |
| 1217 | std::vector<Animation> animations; |
| 1218 | std::vector<Buffer> buffers; |
| 1219 | std::vector<BufferView> bufferViews; |
| 1220 | std::vector<Material> materials; |
| 1221 | std::vector<Mesh> meshes; |
| 1222 | std::vector<Node> nodes; |
| 1223 | std::vector<Texture> textures; |
| 1224 | std::vector<Image> images; |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 1225 | std::vector<Skin> skins; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1226 | std::vector<Sampler> samplers; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 1227 | std::vector<Camera> cameras; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1228 | std::vector<Scene> scenes; |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 1229 | std::vector<Light> lights; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1230 | std::vector<AudioEmitter> audioEmitters; |
| 1231 | std::vector<AudioSource> audioSources; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1232 | |
Bryn Lloyd | cb6a707 | 2023-06-21 22:30:14 +0200 | [diff] [blame] | 1233 | int defaultScene{-1}; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1234 | std::vector<std::string> extensionsUsed; |
Aurélien Chatelain | 756ee6b | 2017-06-19 13:52:49 +0000 | [diff] [blame] | 1235 | std::vector<std::string> extensionsRequired; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1236 | |
| 1237 | Asset asset; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 1238 | |
| 1239 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1240 | ExtensionMap extensions; |
| 1241 | |
| 1242 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1243 | std::string extras_json_string; |
| 1244 | std::string extensions_json_string; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1245 | }; |
| 1246 | |
Syoyo Fujita | 0614eb8 | 2016-10-14 18:50:14 +0900 | [diff] [blame] | 1247 | enum SectionCheck { |
Luke San Antonio | 9e36b61 | 2016-06-23 14:10:51 -0400 | [diff] [blame] | 1248 | NO_REQUIRE = 0x00, |
Benjamin Schmithüsen | d02ad0d | 2019-05-02 14:44:20 +0200 | [diff] [blame] | 1249 | REQUIRE_VERSION = 0x01, |
| 1250 | REQUIRE_SCENE = 0x02, |
| 1251 | REQUIRE_SCENES = 0x04, |
| 1252 | REQUIRE_NODES = 0x08, |
| 1253 | REQUIRE_ACCESSORS = 0x10, |
| 1254 | REQUIRE_BUFFERS = 0x20, |
| 1255 | REQUIRE_BUFFER_VIEWS = 0x40, |
| 1256 | REQUIRE_ALL = 0x7f |
Luke San Antonio | 9e36b61 | 2016-06-23 14:10:51 -0400 | [diff] [blame] | 1257 | }; |
| 1258 | |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1259 | /// |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 1260 | /// URIEncodeFunction type. Signature for custom URI encoding of external |
| 1261 | /// resources such as .bin and image files. Used by tinygltf to re-encode the |
| 1262 | /// final location of saved files. object_type may be used to encode buffer and |
| 1263 | /// image URIs differently, for example. See |
| 1264 | /// https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#uris |
| 1265 | /// |
| 1266 | typedef bool (*URIEncodeFunction)(const std::string &in_uri, |
| 1267 | const std::string &object_type, |
| 1268 | std::string *out_uri, void *user_data); |
| 1269 | |
| 1270 | /// |
| 1271 | /// URIDecodeFunction type. Signature for custom URI decoding of external |
| 1272 | /// resources such as .bin and image files. Used by tinygltf when computing |
| 1273 | /// filenames to write resources. |
| 1274 | /// |
| 1275 | typedef bool (*URIDecodeFunction)(const std::string &in_uri, |
| 1276 | std::string *out_uri, void *user_data); |
| 1277 | |
| 1278 | // Declaration of default uri decode function |
| 1279 | bool URIDecode(const std::string &in_uri, std::string *out_uri, |
| 1280 | void *user_data); |
| 1281 | |
| 1282 | /// |
| 1283 | /// A structure containing URI callbacks and a pointer to their user data. |
| 1284 | /// |
| 1285 | struct URICallbacks { |
| 1286 | URIEncodeFunction encode; // Optional encode method |
| 1287 | URIDecodeFunction decode; // Required decode method |
| 1288 | |
| 1289 | void *user_data; // An argument that is passed to all uri callbacks |
| 1290 | }; |
| 1291 | |
| 1292 | /// |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1293 | /// LoadImageDataFunction type. Signature for custom image loading callbacks. |
| 1294 | /// |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 1295 | typedef bool (*LoadImageDataFunction)(Image *, const int, std::string *, |
| 1296 | std::string *, int, int, |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 1297 | const unsigned char *, int, |
| 1298 | void *user_pointer); |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1299 | |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1300 | /// |
| 1301 | /// WriteImageDataFunction type. Signature for custom image writing callbacks. |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 1302 | /// The out_uri parameter becomes the URI written to the gltf and may reference |
| 1303 | /// a file or contain a data URI. |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1304 | /// |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 1305 | typedef bool (*WriteImageDataFunction)(const std::string *basepath, |
| 1306 | const std::string *filename, |
| 1307 | const Image *image, bool embedImages, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 1308 | const URICallbacks *uri_cb, |
| 1309 | std::string *out_uri, |
| 1310 | void *user_pointer); |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1311 | |
Squareys | 2d3594d | 2018-03-13 22:40:53 +0100 | [diff] [blame] | 1312 | #ifndef TINYGLTF_NO_STB_IMAGE |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1313 | // Declaration of default image loader callback |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 1314 | bool LoadImageData(Image *image, const int image_idx, std::string *err, |
| 1315 | std::string *warn, int req_width, int req_height, |
| 1316 | const unsigned char *bytes, int size, void *); |
Squareys | 2d3594d | 2018-03-13 22:40:53 +0100 | [diff] [blame] | 1317 | #endif |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1318 | |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1319 | #ifndef TINYGLTF_NO_STB_IMAGE_WRITE |
| 1320 | // Declaration of default image writer callback |
| 1321 | bool WriteImageData(const std::string *basepath, const std::string *filename, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 1322 | const Image *image, bool embedImages, |
| 1323 | const URICallbacks *uri_cb, std::string *out_uri, void *); |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1324 | #endif |
| 1325 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1326 | /// |
| 1327 | /// FilExistsFunction type. Signature for custom filesystem callbacks. |
| 1328 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1329 | typedef bool (*FileExistsFunction)(const std::string &abs_filename, void *); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1330 | |
| 1331 | /// |
| 1332 | /// ExpandFilePathFunction type. Signature for custom filesystem callbacks. |
| 1333 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1334 | typedef std::string (*ExpandFilePathFunction)(const std::string &, void *); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1335 | |
| 1336 | /// |
| 1337 | /// ReadWholeFileFunction type. Signature for custom filesystem callbacks. |
| 1338 | /// |
| 1339 | typedef bool (*ReadWholeFileFunction)(std::vector<unsigned char> *, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1340 | std::string *, const std::string &, |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1341 | void *); |
| 1342 | |
| 1343 | /// |
| 1344 | /// WriteWholeFileFunction type. Signature for custom filesystem callbacks. |
| 1345 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1346 | typedef bool (*WriteWholeFileFunction)(std::string *, const std::string &, |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1347 | const std::vector<unsigned char> &, |
| 1348 | void *); |
| 1349 | |
| 1350 | /// |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 1351 | /// GetFileSizeFunction type. Signature for custom filesystem callbacks. |
| 1352 | /// |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1353 | typedef bool (*GetFileSizeFunction)(size_t *filesize_out, std::string *err, |
| 1354 | const std::string &abs_filename, |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 1355 | void *userdata); |
| 1356 | |
| 1357 | /// |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1358 | /// A structure containing all required filesystem callbacks and a pointer to |
| 1359 | /// their user data. |
| 1360 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1361 | struct FsCallbacks { |
| 1362 | FileExistsFunction FileExists; |
| 1363 | ExpandFilePathFunction ExpandFilePath; |
| 1364 | ReadWholeFileFunction ReadWholeFile; |
| 1365 | WriteWholeFileFunction WriteWholeFile; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1366 | GetFileSizeFunction GetFileSizeInBytes; // To avoid GetFileSize Win32 API, |
| 1367 | // add `InBytes` suffix. |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1368 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1369 | void *user_data; // An argument that is passed to all fs callbacks |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1370 | }; |
| 1371 | |
| 1372 | #ifndef TINYGLTF_NO_FS |
| 1373 | // Declaration of default filesystem callbacks |
| 1374 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1375 | bool FileExists(const std::string &abs_filename, void *); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1376 | |
Syoyo Fujita | 3d939fd | 2020-06-06 18:13:15 +0900 | [diff] [blame] | 1377 | /// |
Rahul Sheth | 96a8b2c | 2020-07-10 14:27:46 -0400 | [diff] [blame] | 1378 | /// Expand file path(e.g. `~` to home directory on posix, `%APPDATA%` to |
Syoyo Fujita | 2c521b3 | 2020-12-04 00:50:46 +0900 | [diff] [blame] | 1379 | /// `C:\\Users\\tinygltf\\AppData`) |
Syoyo Fujita | 3d939fd | 2020-06-06 18:13:15 +0900 | [diff] [blame] | 1380 | /// |
| 1381 | /// @param[in] filepath File path string. Assume UTF-8 |
| 1382 | /// @param[in] userdata User data. Set to `nullptr` if you don't need it. |
| 1383 | /// |
| 1384 | std::string ExpandFilePath(const std::string &filepath, void *userdata); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1385 | |
| 1386 | bool ReadWholeFile(std::vector<unsigned char> *out, std::string *err, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1387 | const std::string &filepath, void *); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1388 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1389 | bool WriteWholeFile(std::string *err, const std::string &filepath, |
| 1390 | const std::vector<unsigned char> &contents, void *); |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 1391 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1392 | bool GetFileSizeInBytes(size_t *filesize_out, std::string *err, |
| 1393 | const std::string &filepath, void *); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1394 | #endif |
| 1395 | |
Syoyo Fujita | a472a3f | 2019-08-21 14:23:00 +0900 | [diff] [blame] | 1396 | /// |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 1397 | /// glTF Parser/Serializer context. |
Syoyo Fujita | a472a3f | 2019-08-21 14:23:00 +0900 | [diff] [blame] | 1398 | /// |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1399 | class TinyGLTF { |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 1400 | public: |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1401 | #ifdef __clang__ |
| 1402 | #pragma clang diagnostic push |
| 1403 | #pragma clang diagnostic ignored "-Wc++98-compat" |
| 1404 | #endif |
| 1405 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1406 | TinyGLTF() = default; |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1407 | |
| 1408 | #ifdef __clang__ |
| 1409 | #pragma clang diagnostic pop |
| 1410 | #endif |
| 1411 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1412 | ~TinyGLTF() = default; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1413 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1414 | /// |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1415 | /// Loads glTF ASCII asset from a file. |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1416 | /// Set warning message to `warn` for example it fails to load asserts. |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 1417 | /// Returns false and set error string to `err` if there's an error. |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1418 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1419 | bool LoadASCIIFromFile(Model *model, std::string *err, std::string *warn, |
Luke San Antonio | 9e36b61 | 2016-06-23 14:10:51 -0400 | [diff] [blame] | 1420 | const std::string &filename, |
Benjamin Schmithüsen | d02ad0d | 2019-05-02 14:44:20 +0200 | [diff] [blame] | 1421 | unsigned int check_sections = REQUIRE_VERSION); |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 1422 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1423 | /// |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1424 | /// Loads glTF ASCII asset from string(memory). |
| 1425 | /// `length` = strlen(str); |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 1426 | /// `base_dir` is a search path of glTF asset(e.g. images). Path Must be an |
| 1427 | /// expanded path (e.g. no tilde(`~`), no environment variables). Set warning |
| 1428 | /// message to `warn` for example it fails to load asserts. Returns false and |
| 1429 | /// set error string to `err` if there's an error. |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1430 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1431 | bool LoadASCIIFromString(Model *model, std::string *err, std::string *warn, |
| 1432 | const char *str, const unsigned int length, |
Luke San Antonio | 9e36b61 | 2016-06-23 14:10:51 -0400 | [diff] [blame] | 1433 | const std::string &base_dir, |
Benjamin Schmithüsen | d02ad0d | 2019-05-02 14:44:20 +0200 | [diff] [blame] | 1434 | unsigned int check_sections = REQUIRE_VERSION); |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1435 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1436 | /// |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1437 | /// Loads glTF binary asset from a file. |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1438 | /// Set warning message to `warn` for example it fails to load asserts. |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1439 | /// Returns false and set error string to `err` if there's an error. |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1440 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1441 | bool LoadBinaryFromFile(Model *model, std::string *err, std::string *warn, |
Luke San Antonio | 9e36b61 | 2016-06-23 14:10:51 -0400 | [diff] [blame] | 1442 | const std::string &filename, |
Benjamin Schmithüsen | d02ad0d | 2019-05-02 14:44:20 +0200 | [diff] [blame] | 1443 | unsigned int check_sections = REQUIRE_VERSION); |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1444 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1445 | /// |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1446 | /// Loads glTF binary asset from memory. |
| 1447 | /// `length` = strlen(str); |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 1448 | /// `base_dir` is a search path of glTF asset(e.g. images). Path Must be an |
| 1449 | /// expanded path (e.g. no tilde(`~`), no environment variables). |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1450 | /// Set warning message to `warn` for example it fails to load asserts. |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1451 | /// Returns false and set error string to `err` if there's an error. |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1452 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1453 | bool LoadBinaryFromMemory(Model *model, std::string *err, std::string *warn, |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 1454 | const unsigned char *bytes, |
| 1455 | const unsigned int length, |
Luke San Antonio | 9e36b61 | 2016-06-23 14:10:51 -0400 | [diff] [blame] | 1456 | const std::string &base_dir = "", |
Benjamin Schmithüsen | d02ad0d | 2019-05-02 14:44:20 +0200 | [diff] [blame] | 1457 | unsigned int check_sections = REQUIRE_VERSION); |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1458 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1459 | /// |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 1460 | /// Write glTF to stream, buffers and images will be embedded |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 1461 | /// |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 1462 | bool WriteGltfSceneToStream(const Model *model, std::ostream &stream, |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 1463 | bool prettyPrint, bool writeBinary); |
| 1464 | |
| 1465 | /// |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1466 | /// Write glTF to file. |
| 1467 | /// |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 1468 | bool WriteGltfSceneToFile(const Model *model, const std::string &filename, |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 1469 | bool embedImages, bool embedBuffers, |
| 1470 | bool prettyPrint, bool writeBinary); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 1471 | |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1472 | /// |
Nyall Dawson | 8c85d5e | 2023-08-28 12:56:09 +1000 | [diff] [blame] | 1473 | /// Sets the parsing strictness. |
| 1474 | /// |
| 1475 | void SetParseStrictness(ParseStrictness strictness); |
| 1476 | |
| 1477 | /// |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1478 | /// Set callback to use for loading image data |
| 1479 | /// |
| 1480 | void SetImageLoader(LoadImageDataFunction LoadImageData, void *user_data); |
| 1481 | |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1482 | /// |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1483 | /// Unset(remove) callback of loading image data |
| 1484 | /// |
| 1485 | void RemoveImageLoader(); |
| 1486 | |
| 1487 | /// |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1488 | /// Set callback to use for writing image data |
| 1489 | /// |
| 1490 | void SetImageWriter(WriteImageDataFunction WriteImageData, void *user_data); |
| 1491 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1492 | /// |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 1493 | /// Set callbacks to use for URI encoding and decoding and their user data |
| 1494 | /// |
| 1495 | void SetURICallbacks(URICallbacks callbacks); |
| 1496 | |
| 1497 | /// |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1498 | /// Set callbacks to use for filesystem (fs) access and their user data |
| 1499 | /// |
| 1500 | void SetFsCallbacks(FsCallbacks callbacks); |
| 1501 | |
Syoyo Fujita | a472a3f | 2019-08-21 14:23:00 +0900 | [diff] [blame] | 1502 | /// |
| 1503 | /// Set serializing default values(default = false). |
| 1504 | /// When true, default values are force serialized to .glTF. |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 1505 | /// This may be helpful if you want to serialize a full description of glTF |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 1506 | /// data. |
Syoyo Fujita | a472a3f | 2019-08-21 14:23:00 +0900 | [diff] [blame] | 1507 | /// |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 1508 | /// TODO(LTE): Supply parsing option as function arguments to |
| 1509 | /// `LoadASCIIFromFile()` and others, not by a class method |
Syoyo Fujita | a472a3f | 2019-08-21 14:23:00 +0900 | [diff] [blame] | 1510 | /// |
| 1511 | void SetSerializeDefaultValues(const bool enabled) { |
| 1512 | serialize_default_values_ = enabled; |
| 1513 | } |
| 1514 | |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 1515 | bool GetSerializeDefaultValues() const { return serialize_default_values_; } |
Syoyo Fujita | a472a3f | 2019-08-21 14:23:00 +0900 | [diff] [blame] | 1516 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1517 | /// |
| 1518 | /// Store original JSON string for `extras` and `extensions`. |
| 1519 | /// This feature will be useful when the user want to reconstruct custom data |
| 1520 | /// structure from JSON string. |
| 1521 | /// |
| 1522 | void SetStoreOriginalJSONForExtrasAndExtensions(const bool enabled) { |
| 1523 | store_original_json_for_extras_and_extensions_ = enabled; |
| 1524 | } |
| 1525 | |
| 1526 | bool GetStoreOriginalJSONForExtrasAndExtensions() const { |
| 1527 | return store_original_json_for_extras_and_extensions_; |
| 1528 | } |
| 1529 | |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1530 | /// |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 1531 | /// Specify whether preserve image channels when loading images or not. |
| 1532 | /// (Not effective when the user supplies their own LoadImageData callbacks) |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1533 | /// |
| 1534 | void SetPreserveImageChannels(bool onoff) { |
| 1535 | preserve_image_channels_ = onoff; |
| 1536 | } |
| 1537 | |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 1538 | /// |
| 1539 | /// Set maximum allowed external file size in bytes. |
| 1540 | /// Default: 2GB |
| 1541 | /// Only effective for built-in ReadWholeFileFunction FS function. |
| 1542 | /// |
| 1543 | void SetMaxExternalFileSize(size_t max_bytes) { |
| 1544 | max_external_file_size_ = max_bytes; |
| 1545 | } |
| 1546 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1547 | size_t GetMaxExternalFileSize() const { return max_external_file_size_; } |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 1548 | |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 1549 | bool GetPreserveImageChannels() const { return preserve_image_channels_; } |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1550 | |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 1551 | private: |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1552 | /// |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 1553 | /// Loads glTF asset from string(memory). |
| 1554 | /// `length` = strlen(str); |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1555 | /// Set warning message to `warn` for example it fails to load asserts |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 1556 | /// Returns false and set error string to `err` if there's an error. |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1557 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1558 | bool LoadFromString(Model *model, std::string *err, std::string *warn, |
| 1559 | const char *str, const unsigned int length, |
| 1560 | const std::string &base_dir, unsigned int check_sections); |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1561 | |
Syoyo Fujita | a472a3f | 2019-08-21 14:23:00 +0900 | [diff] [blame] | 1562 | const unsigned char *bin_data_ = nullptr; |
| 1563 | size_t bin_size_ = 0; |
| 1564 | bool is_binary_ = false; |
| 1565 | |
Nyall Dawson | bbc1eae | 2023-09-03 09:05:32 +1000 | [diff] [blame] | 1566 | ParseStrictness strictness_ = ParseStrictness::Strict; |
Nyall Dawson | 8c85d5e | 2023-08-28 12:56:09 +1000 | [diff] [blame] | 1567 | |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 1568 | bool serialize_default_values_ = false; ///< Serialize default values? |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1569 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1570 | bool store_original_json_for_extras_and_extensions_ = false; |
| 1571 | |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 1572 | bool preserve_image_channels_ = false; /// Default false(expand channels to |
| 1573 | /// RGBA) for backward compatibility. |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1574 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1575 | size_t max_external_file_size_{ |
| 1576 | size_t((std::numeric_limits<int32_t>::max)())}; // Default 2GB |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 1577 | |
Syoyo Fujita | 9117abb | 2022-08-16 20:10:26 +0900 | [diff] [blame] | 1578 | // Warning & error messages |
Syoyo Fujita | 81f7dbe | 2022-08-02 01:05:34 +0900 | [diff] [blame] | 1579 | std::string warn_; |
| 1580 | std::string err_; |
| 1581 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1582 | FsCallbacks fs = { |
| 1583 | #ifndef TINYGLTF_NO_FS |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1584 | &tinygltf::FileExists, |
| 1585 | &tinygltf::ExpandFilePath, |
| 1586 | &tinygltf::ReadWholeFile, |
| 1587 | &tinygltf::WriteWholeFile, |
| 1588 | &tinygltf::GetFileSizeInBytes, |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1589 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1590 | nullptr // Fs callback user data |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1591 | #else |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 1592 | nullptr, nullptr, nullptr, nullptr, nullptr, |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1593 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1594 | nullptr // Fs callback user data |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1595 | #endif |
| 1596 | }; |
| 1597 | |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 1598 | URICallbacks uri_cb = { |
| 1599 | // Use paths as-is by default. This will use JSON string escaping. |
| 1600 | nullptr, |
| 1601 | // Decode all URIs before using them as paths as the application may have |
| 1602 | // percent encoded them. |
| 1603 | &tinygltf::URIDecode, |
| 1604 | // URI callback user data |
| 1605 | nullptr}; |
| 1606 | |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1607 | LoadImageDataFunction LoadImageData = |
| 1608 | #ifndef TINYGLTF_NO_STB_IMAGE |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1609 | &tinygltf::LoadImageData; |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1610 | #else |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1611 | nullptr; |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1612 | #endif |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1613 | void *load_image_user_data_{nullptr}; |
| 1614 | bool user_image_loader_{false}; |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1615 | |
| 1616 | WriteImageDataFunction WriteImageData = |
| 1617 | #ifndef TINYGLTF_NO_STB_IMAGE_WRITE |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1618 | &tinygltf::WriteImageData; |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1619 | #else |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1620 | nullptr; |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1621 | #endif |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1622 | void *write_image_user_data_{nullptr}; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1623 | }; |
| 1624 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1625 | #ifdef __clang__ |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 1626 | #pragma clang diagnostic pop // -Wpadded |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1627 | #endif |
| 1628 | |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 1629 | } // namespace tinygltf |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1630 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1631 | #endif // TINY_GLTF_H_ |
Syoyo Fujita | c8d3c76 | 2017-05-25 01:42:38 +0900 | [diff] [blame] | 1632 | |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1633 | #if defined(TINYGLTF_IMPLEMENTATION) || defined(__INTELLISENSE__) |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 1634 | #include <algorithm> |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1635 | // #include <cassert> |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1636 | #ifndef TINYGLTF_NO_FS |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1637 | #include <sys/stat.h> // for is_directory check |
| 1638 | |
Syoyo Fujita | 125d8e5 | 2019-11-09 20:52:56 +0900 | [diff] [blame] | 1639 | #include <cstdio> |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 1640 | #include <fstream> |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1641 | #endif |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 1642 | #include <sstream> |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 1643 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 1644 | #ifdef __clang__ |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 1645 | // Disable some warnings for external files. |
| 1646 | #pragma clang diagnostic push |
| 1647 | #pragma clang diagnostic ignored "-Wfloat-equal" |
| 1648 | #pragma clang diagnostic ignored "-Wexit-time-destructors" |
| 1649 | #pragma clang diagnostic ignored "-Wconversion" |
| 1650 | #pragma clang diagnostic ignored "-Wold-style-cast" |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 1651 | #pragma clang diagnostic ignored "-Wglobal-constructors" |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1652 | #if __has_warning("-Wreserved-id-macro") |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 1653 | #pragma clang diagnostic ignored "-Wreserved-id-macro" |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1654 | #endif |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 1655 | #pragma clang diagnostic ignored "-Wdisabled-macro-expansion" |
| 1656 | #pragma clang diagnostic ignored "-Wpadded" |
Syoyo Fujita | 2e21be7 | 2017-11-05 17:13:01 +0900 | [diff] [blame] | 1657 | #pragma clang diagnostic ignored "-Wc++98-compat" |
Syoyo Fujita | dc4bb86 | 2018-04-02 02:04:24 +0900 | [diff] [blame] | 1658 | #pragma clang diagnostic ignored "-Wc++98-compat-pedantic" |
Syoyo Fujita | 2e21be7 | 2017-11-05 17:13:01 +0900 | [diff] [blame] | 1659 | #pragma clang diagnostic ignored "-Wdocumentation-unknown-command" |
| 1660 | #pragma clang diagnostic ignored "-Wswitch-enum" |
| 1661 | #pragma clang diagnostic ignored "-Wimplicit-fallthrough" |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1662 | #pragma clang diagnostic ignored "-Wweak-vtables" |
| 1663 | #pragma clang diagnostic ignored "-Wcovered-switch-default" |
Syoyo Fujita | a8f0b1c | 2018-08-28 21:33:40 +0900 | [diff] [blame] | 1664 | #if __has_warning("-Wdouble-promotion") |
| 1665 | #pragma clang diagnostic ignored "-Wdouble-promotion" |
| 1666 | #endif |
Syoyo Fujita | 2e21be7 | 2017-11-05 17:13:01 +0900 | [diff] [blame] | 1667 | #if __has_warning("-Wcomma") |
Syoyo Fujita | c8d3c76 | 2017-05-25 01:42:38 +0900 | [diff] [blame] | 1668 | #pragma clang diagnostic ignored "-Wcomma" |
| 1669 | #endif |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1670 | #if __has_warning("-Wzero-as-null-pointer-constant") |
| 1671 | #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" |
| 1672 | #endif |
| 1673 | #if __has_warning("-Wcast-qual") |
| 1674 | #pragma clang diagnostic ignored "-Wcast-qual" |
| 1675 | #endif |
Syoyo Fujita | dc4bb86 | 2018-04-02 02:04:24 +0900 | [diff] [blame] | 1676 | #if __has_warning("-Wmissing-variable-declarations") |
| 1677 | #pragma clang diagnostic ignored "-Wmissing-variable-declarations" |
| 1678 | #endif |
| 1679 | #if __has_warning("-Wmissing-prototypes") |
| 1680 | #pragma clang diagnostic ignored "-Wmissing-prototypes" |
| 1681 | #endif |
| 1682 | #if __has_warning("-Wcast-align") |
| 1683 | #pragma clang diagnostic ignored "-Wcast-align" |
| 1684 | #endif |
| 1685 | #if __has_warning("-Wnewline-eof") |
| 1686 | #pragma clang diagnostic ignored "-Wnewline-eof" |
| 1687 | #endif |
Syoyo Fujita | 7bdfed3 | 2019-03-03 17:04:49 +0900 | [diff] [blame] | 1688 | #if __has_warning("-Wunused-parameter") |
| 1689 | #pragma clang diagnostic ignored "-Wunused-parameter" |
Syoyo Fujita | 6fd9162 | 2016-05-01 20:27:30 +0900 | [diff] [blame] | 1690 | #endif |
Syoyo Fujita | 7bdfed3 | 2019-03-03 17:04:49 +0900 | [diff] [blame] | 1691 | #if __has_warning("-Wmismatched-tags") |
| 1692 | #pragma clang diagnostic ignored "-Wmismatched-tags" |
| 1693 | #endif |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 1694 | #if __has_warning("-Wextra-semi-stmt") |
| 1695 | #pragma clang diagnostic ignored "-Wextra-semi-stmt" |
| 1696 | #endif |
Syoyo Fujita | 7bdfed3 | 2019-03-03 17:04:49 +0900 | [diff] [blame] | 1697 | #endif |
| 1698 | |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 1699 | // Disable GCC warnings |
Syoyo Fujita | 7bdfed3 | 2019-03-03 17:04:49 +0900 | [diff] [blame] | 1700 | #ifdef __GNUC__ |
| 1701 | #pragma GCC diagnostic push |
| 1702 | #pragma GCC diagnostic ignored "-Wtype-limits" |
Syoyo Fujita | ca56f72 | 2019-03-07 21:04:25 +0900 | [diff] [blame] | 1703 | #endif // __GNUC__ |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1704 | |
kroko | fc0116b | 2019-03-03 08:28:49 +0200 | [diff] [blame] | 1705 | #ifndef TINYGLTF_NO_INCLUDE_JSON |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1706 | #ifndef TINYGLTF_USE_RAPIDJSON |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 1707 | #include "json.hpp" |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1708 | #else |
Alvaro Barua | 3ddf830 | 2021-03-18 00:21:23 +0000 | [diff] [blame] | 1709 | #ifndef TINYGLTF_NO_INCLUDE_RAPIDJSON |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1710 | #include "document.h" |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1711 | #include "prettywriter.h" |
| 1712 | #include "rapidjson.h" |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1713 | #include "stringbuffer.h" |
| 1714 | #include "writer.h" |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1715 | #endif |
kroko | f4b6d11 | 2019-03-03 01:11:31 +0200 | [diff] [blame] | 1716 | #endif |
Alvaro Barua | 3ddf830 | 2021-03-18 00:21:23 +0000 | [diff] [blame] | 1717 | #endif |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 1718 | |
| 1719 | #ifdef TINYGLTF_ENABLE_DRACO |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 1720 | #include "draco/compression/decode.h" |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 1721 | #include "draco/core/decoder_buffer.h" |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 1722 | #endif |
Squareys | 2d3594d | 2018-03-13 22:40:53 +0100 | [diff] [blame] | 1723 | |
| 1724 | #ifndef TINYGLTF_NO_STB_IMAGE |
kroko | fc0116b | 2019-03-03 08:28:49 +0200 | [diff] [blame] | 1725 | #ifndef TINYGLTF_NO_INCLUDE_STB_IMAGE |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 1726 | #include "stb_image.h" |
Squareys | 2d3594d | 2018-03-13 22:40:53 +0100 | [diff] [blame] | 1727 | #endif |
kroko | f4b6d11 | 2019-03-03 01:11:31 +0200 | [diff] [blame] | 1728 | #endif |
Squareys | 2d3594d | 2018-03-13 22:40:53 +0100 | [diff] [blame] | 1729 | |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1730 | #ifndef TINYGLTF_NO_STB_IMAGE_WRITE |
kroko | fc0116b | 2019-03-03 08:28:49 +0200 | [diff] [blame] | 1731 | #ifndef TINYGLTF_NO_INCLUDE_STB_IMAGE_WRITE |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 1732 | #include "stb_image_write.h" |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1733 | #endif |
kroko | f4b6d11 | 2019-03-03 01:11:31 +0200 | [diff] [blame] | 1734 | #endif |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1735 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 1736 | #ifdef __clang__ |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 1737 | #pragma clang diagnostic pop |
Syoyo Fujita | 6fd9162 | 2016-05-01 20:27:30 +0900 | [diff] [blame] | 1738 | #endif |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1739 | |
Syoyo Fujita | 7bdfed3 | 2019-03-03 17:04:49 +0900 | [diff] [blame] | 1740 | #ifdef __GNUC__ |
| 1741 | #pragma GCC diagnostic pop |
| 1742 | #endif |
| 1743 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1744 | #ifdef _WIN32 |
Syoyo Fujita | 7d9a0bd | 2019-02-19 16:03:23 +0900 | [diff] [blame] | 1745 | |
| 1746 | // issue 143. |
| 1747 | // Define NOMINMAX to avoid min/max defines, |
imallett | 3a29588 | 2022-10-07 11:20:39 -0700 | [diff] [blame] | 1748 | // but undef it after included Windows.h |
Syoyo Fujita | 7d9a0bd | 2019-02-19 16:03:23 +0900 | [diff] [blame] | 1749 | #ifndef NOMINMAX |
| 1750 | #define TINYGLTF_INTERNAL_NOMINMAX |
| 1751 | #define NOMINMAX |
| 1752 | #endif |
| 1753 | |
Syoyo Fujita | 87be0ce | 2019-02-19 21:36:32 +0900 | [diff] [blame] | 1754 | #ifndef WIN32_LEAN_AND_MEAN |
| 1755 | #define WIN32_LEAN_AND_MEAN |
| 1756 | #define TINYGLTF_INTERNAL_WIN32_LEAN_AND_MEAN |
| 1757 | #endif |
imallett | 3a29588 | 2022-10-07 11:20:39 -0700 | [diff] [blame] | 1758 | #ifndef __MINGW32__ |
imallett | 56e1098 | 2022-10-07 10:35:16 -0700 | [diff] [blame] | 1759 | #include <Windows.h> // include API for expanding a file path |
imallett | 3a29588 | 2022-10-07 11:20:39 -0700 | [diff] [blame] | 1760 | #else |
| 1761 | #include <windows.h> |
| 1762 | #endif |
Syoyo Fujita | 7d9a0bd | 2019-02-19 16:03:23 +0900 | [diff] [blame] | 1763 | |
Syoyo Fujita | 87be0ce | 2019-02-19 21:36:32 +0900 | [diff] [blame] | 1764 | #ifdef TINYGLTF_INTERNAL_WIN32_LEAN_AND_MEAN |
| 1765 | #undef WIN32_LEAN_AND_MEAN |
| 1766 | #endif |
| 1767 | |
Syoyo Fujita | 7d9a0bd | 2019-02-19 16:03:23 +0900 | [diff] [blame] | 1768 | #if defined(TINYGLTF_INTERNAL_NOMINMAX) |
| 1769 | #undef NOMINMAX |
| 1770 | #endif |
| 1771 | |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 1772 | #if defined(__GLIBCXX__) // mingw |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 1773 | |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 1774 | #include <fcntl.h> // _O_RDONLY |
| 1775 | |
| 1776 | #include <ext/stdio_filebuf.h> // fstream (all sorts of IO stuff) + stdio_filebuf (=streambuf) |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 1777 | |
| 1778 | #endif |
| 1779 | |
Julian Smith | 0598a20 | 2021-08-25 12:06:08 +0100 | [diff] [blame] | 1780 | #elif !defined(__ANDROID__) && !defined(__OpenBSD__) |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1781 | // #include <wordexp.h> |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1782 | #endif |
| 1783 | |
Christopher Sean Morrison | 2c9b256 | 2022-05-14 19:00:19 -0400 | [diff] [blame] | 1784 | #if defined(__sparcv9) || defined(__powerpc__) |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1785 | // Big endian |
| 1786 | #else |
| 1787 | #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU |
| 1788 | #define TINYGLTF_LITTLE_ENDIAN 1 |
| 1789 | #endif |
| 1790 | #endif |
| 1791 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 1792 | namespace tinygltf { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 1793 | namespace detail { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1794 | #ifdef TINYGLTF_USE_RAPIDJSON |
jrkoonce | ce7fa74 | 2019-09-04 13:31:44 -0500 | [diff] [blame] | 1795 | |
| 1796 | #ifdef TINYGLTF_USE_RAPIDJSON_CRTALLOCATOR |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1797 | // This uses the RapidJSON CRTAllocator. It is thread safe and multiple |
| 1798 | // documents may be active at once. |
| 1799 | using json = |
| 1800 | rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAllocator>; |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 1801 | using json_iterator = json::MemberIterator; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1802 | using json_const_iterator = json::ConstMemberIterator; |
| 1803 | using json_const_array_iterator = json const *; |
| 1804 | using JsonDocument = |
jrkoonce | ce7fa74 | 2019-09-04 13:31:44 -0500 | [diff] [blame] | 1805 | rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator>; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1806 | rapidjson::CrtAllocator s_CrtAllocator; // stateless and thread safe |
| 1807 | rapidjson::CrtAllocator &GetAllocator() { return s_CrtAllocator; } |
jrkoonce | ce7fa74 | 2019-09-04 13:31:44 -0500 | [diff] [blame] | 1808 | #else |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1809 | // This uses the default RapidJSON MemoryPoolAllocator. It is very fast, but |
| 1810 | // not thread safe. Only a single JsonDocument may be active at any one time, |
| 1811 | // meaning only a single gltf load/save can be active any one time. |
| 1812 | using json = rapidjson::Value; |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 1813 | using json_iterator = json::MemberIterator; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1814 | using json_const_iterator = json::ConstMemberIterator; |
| 1815 | using json_const_array_iterator = json const *; |
| 1816 | rapidjson::Document *s_pActiveDocument = nullptr; |
| 1817 | rapidjson::Document::AllocatorType &GetAllocator() { |
| 1818 | assert(s_pActiveDocument); // Root json node must be JsonDocument type |
| 1819 | return s_pActiveDocument->GetAllocator(); |
| 1820 | } |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1821 | |
| 1822 | #ifdef __clang__ |
| 1823 | #pragma clang diagnostic push |
| 1824 | // Suppress JsonDocument(JsonDocument &&rhs) noexcept |
| 1825 | #pragma clang diagnostic ignored "-Wunused-member-function" |
| 1826 | #endif |
| 1827 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1828 | struct JsonDocument : public rapidjson::Document { |
| 1829 | JsonDocument() { |
| 1830 | assert(s_pActiveDocument == |
| 1831 | nullptr); // When using default allocator, only one document can be |
| 1832 | // active at a time, if you need multiple active at once, |
| 1833 | // define TINYGLTF_USE_RAPIDJSON_CRTALLOCATOR |
| 1834 | s_pActiveDocument = this; |
jrkoonce | ce7fa74 | 2019-09-04 13:31:44 -0500 | [diff] [blame] | 1835 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1836 | JsonDocument(const JsonDocument &) = delete; |
| 1837 | JsonDocument(JsonDocument &&rhs) noexcept |
jrkoonce | ce7fa74 | 2019-09-04 13:31:44 -0500 | [diff] [blame] | 1838 | : rapidjson::Document(std::move(rhs)) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1839 | s_pActiveDocument = this; |
| 1840 | rhs.isNil = true; |
| 1841 | } |
| 1842 | ~JsonDocument() { |
| 1843 | if (!isNil) { |
| 1844 | s_pActiveDocument = nullptr; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1845 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1846 | } |
jrkoonce | ce7fa74 | 2019-09-04 13:31:44 -0500 | [diff] [blame] | 1847 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1848 | private: |
| 1849 | bool isNil = false; |
| 1850 | }; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1851 | |
| 1852 | #ifdef __clang__ |
| 1853 | #pragma clang diagnostic pop |
| 1854 | #endif |
| 1855 | |
jrkoonce | ce7fa74 | 2019-09-04 13:31:44 -0500 | [diff] [blame] | 1856 | #endif // TINYGLTF_USE_RAPIDJSON_CRTALLOCATOR |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1857 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1858 | #else |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1859 | using nlohmann::json; |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 1860 | using json_iterator = json::iterator; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1861 | using json_const_iterator = json::const_iterator; |
| 1862 | using json_const_array_iterator = json_const_iterator; |
| 1863 | using JsonDocument = json; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1864 | #endif |
| 1865 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1866 | void JsonParse(JsonDocument &doc, const char *str, size_t length, |
| 1867 | bool throwExc = false) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1868 | #ifdef TINYGLTF_USE_RAPIDJSON |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1869 | (void)throwExc; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1870 | doc.Parse(str, length); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1871 | #else |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 1872 | doc = detail::json::parse(str, str + length, nullptr, throwExc); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1873 | #endif |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1874 | } |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1875 | } // namespace detail |
| 1876 | } // namespace tinygltf |
Syoyo Fujita | 2e21be7 | 2017-11-05 17:13:01 +0900 | [diff] [blame] | 1877 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1878 | #ifdef __APPLE__ |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 1879 | #include "TargetConditionals.h" |
Syoyo Fujita | b23f6fe | 2017-11-15 01:03:09 +0900 | [diff] [blame] | 1880 | #endif |
| 1881 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1882 | #ifdef __clang__ |
| 1883 | #pragma clang diagnostic push |
| 1884 | #pragma clang diagnostic ignored "-Wc++98-compat" |
| 1885 | #endif |
| 1886 | |
Syoyo Fujita | e1bef8c | 2016-03-17 21:31:13 +0900 | [diff] [blame] | 1887 | namespace tinygltf { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1888 | |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1889 | /// |
| 1890 | /// Internal LoadImageDataOption struct. |
| 1891 | /// This struct is passed through `user_pointer` in LoadImageData. |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 1892 | /// The struct is not passed when the user supply their own LoadImageData |
| 1893 | /// callbacks. |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1894 | /// |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 1895 | struct LoadImageDataOption { |
| 1896 | // true: preserve image channels(e.g. load as RGB image if the image has RGB |
| 1897 | // channels) default `false`(channels are expanded to RGBA for backward |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 1898 | // compatibility). |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1899 | bool preserve_channels{false}; |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1900 | }; |
| 1901 | |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1902 | // Equals function for Value, for recursivity |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1903 | static bool Equals(const tinygltf::Value &one, const tinygltf::Value &other) { |
| 1904 | if (one.Type() != other.Type()) return false; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1905 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1906 | switch (one.Type()) { |
| 1907 | case NULL_TYPE: |
| 1908 | return true; |
| 1909 | case BOOL_TYPE: |
| 1910 | return one.Get<bool>() == other.Get<bool>(); |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 1911 | case REAL_TYPE: |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1912 | return TINYGLTF_DOUBLE_EQUAL(one.Get<double>(), other.Get<double>()); |
| 1913 | case INT_TYPE: |
| 1914 | return one.Get<int>() == other.Get<int>(); |
| 1915 | case OBJECT_TYPE: { |
| 1916 | auto oneObj = one.Get<tinygltf::Value::Object>(); |
| 1917 | auto otherObj = other.Get<tinygltf::Value::Object>(); |
| 1918 | if (oneObj.size() != otherObj.size()) return false; |
| 1919 | for (auto &it : oneObj) { |
| 1920 | auto otherIt = otherObj.find(it.first); |
| 1921 | if (otherIt == otherObj.end()) return false; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1922 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1923 | if (!Equals(it.second, otherIt->second)) return false; |
| 1924 | } |
| 1925 | return true; |
| 1926 | } |
| 1927 | case ARRAY_TYPE: { |
| 1928 | if (one.Size() != other.Size()) return false; |
James Ward | 9b4e1ea | 2024-03-25 17:50:33 +0000 | [diff] [blame] | 1929 | for (size_t i = 0; i < one.Size(); ++i) |
Selmar | a63cc63 | 2019-04-16 16:57:43 +0200 | [diff] [blame] | 1930 | if (!Equals(one.Get(i), other.Get(i))) return false; |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1931 | return true; |
| 1932 | } |
| 1933 | case STRING_TYPE: |
| 1934 | return one.Get<std::string>() == other.Get<std::string>(); |
| 1935 | case BINARY_TYPE: |
| 1936 | return one.Get<std::vector<unsigned char> >() == |
| 1937 | other.Get<std::vector<unsigned char> >(); |
| 1938 | default: { |
| 1939 | // unhandled type |
| 1940 | return false; |
| 1941 | } |
| 1942 | } |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | // Equals function for std::vector<double> using TINYGLTF_DOUBLE_EPSILON |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1946 | static bool Equals(const std::vector<double> &one, |
| 1947 | const std::vector<double> &other) { |
| 1948 | if (one.size() != other.size()) return false; |
| 1949 | for (int i = 0; i < int(one.size()); ++i) { |
| 1950 | if (!TINYGLTF_DOUBLE_EQUAL(one[size_t(i)], other[size_t(i)])) return false; |
| 1951 | } |
| 1952 | return true; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1953 | } |
| 1954 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1955 | bool Accessor::operator==(const Accessor &other) const { |
| 1956 | return this->bufferView == other.bufferView && |
| 1957 | this->byteOffset == other.byteOffset && |
| 1958 | this->componentType == other.componentType && |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1959 | this->count == other.count && this->extensions == other.extensions && |
| 1960 | this->extras == other.extras && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1961 | Equals(this->maxValues, other.maxValues) && |
| 1962 | Equals(this->minValues, other.minValues) && this->name == other.name && |
| 1963 | this->normalized == other.normalized && this->type == other.type; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1964 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1965 | bool Animation::operator==(const Animation &other) const { |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 1966 | return this->channels == other.channels && |
| 1967 | this->extensions == other.extensions && this->extras == other.extras && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1968 | this->name == other.name && this->samplers == other.samplers; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1969 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1970 | bool AnimationChannel::operator==(const AnimationChannel &other) const { |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 1971 | return this->extensions == other.extensions && this->extras == other.extras && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1972 | this->target_node == other.target_node && |
| 1973 | this->target_path == other.target_path && |
| 1974 | this->sampler == other.sampler; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1975 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1976 | bool AnimationSampler::operator==(const AnimationSampler &other) const { |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1977 | return this->extras == other.extras && this->extensions == other.extensions && |
| 1978 | this->input == other.input && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1979 | this->interpolation == other.interpolation && |
| 1980 | this->output == other.output; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1981 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1982 | bool Asset::operator==(const Asset &other) const { |
| 1983 | return this->copyright == other.copyright && |
| 1984 | this->extensions == other.extensions && this->extras == other.extras && |
| 1985 | this->generator == other.generator && |
| 1986 | this->minVersion == other.minVersion && this->version == other.version; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1987 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1988 | bool Buffer::operator==(const Buffer &other) const { |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1989 | return this->data == other.data && this->extensions == other.extensions && |
| 1990 | this->extras == other.extras && this->name == other.name && |
| 1991 | this->uri == other.uri; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1992 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1993 | bool BufferView::operator==(const BufferView &other) const { |
| 1994 | return this->buffer == other.buffer && this->byteLength == other.byteLength && |
| 1995 | this->byteOffset == other.byteOffset && |
| 1996 | this->byteStride == other.byteStride && this->name == other.name && |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1997 | this->target == other.target && this->extensions == other.extensions && |
| 1998 | this->extras == other.extras && |
Alexander Wood | 0d77a29 | 2019-01-26 08:58:45 -0500 | [diff] [blame] | 1999 | this->dracoDecoded == other.dracoDecoded; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2000 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2001 | bool Camera::operator==(const Camera &other) const { |
| 2002 | return this->name == other.name && this->extensions == other.extensions && |
| 2003 | this->extras == other.extras && |
| 2004 | this->orthographic == other.orthographic && |
| 2005 | this->perspective == other.perspective && this->type == other.type; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2006 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2007 | bool Image::operator==(const Image &other) const { |
| 2008 | return this->bufferView == other.bufferView && |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 2009 | this->component == other.component && |
| 2010 | this->extensions == other.extensions && this->extras == other.extras && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2011 | this->height == other.height && this->image == other.image && |
| 2012 | this->mimeType == other.mimeType && this->name == other.name && |
| 2013 | this->uri == other.uri && this->width == other.width; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2014 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2015 | bool Light::operator==(const Light &other) const { |
| 2016 | return Equals(this->color, other.color) && this->name == other.name && |
| 2017 | this->type == other.type; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2018 | } |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 2019 | bool AudioEmitter::operator==(const AudioEmitter &other) const { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2020 | return this->name == other.name && |
| 2021 | TINYGLTF_DOUBLE_EQUAL(this->gain, other.gain) && |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 2022 | this->loop == other.loop && this->playing == other.playing && |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2023 | this->type == other.type && |
| 2024 | this->distanceModel == other.distanceModel && |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 2025 | this->source == other.source; |
| 2026 | } |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2027 | bool AudioSource::operator==(const AudioSource &other) const { |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 2028 | return this->name == other.name && this->uri == other.uri; |
| 2029 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2030 | bool Material::operator==(const Material &other) const { |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 2031 | return (this->pbrMetallicRoughness == other.pbrMetallicRoughness) && |
| 2032 | (this->normalTexture == other.normalTexture) && |
| 2033 | (this->occlusionTexture == other.occlusionTexture) && |
| 2034 | (this->emissiveTexture == other.emissiveTexture) && |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 2035 | Equals(this->emissiveFactor, other.emissiveFactor) && |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 2036 | (this->alphaMode == other.alphaMode) && |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 2037 | TINYGLTF_DOUBLE_EQUAL(this->alphaCutoff, other.alphaCutoff) && |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 2038 | (this->doubleSided == other.doubleSided) && |
| 2039 | (this->extensions == other.extensions) && |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 2040 | (this->extras == other.extras) && (this->values == other.values) && |
| 2041 | (this->additionalValues == other.additionalValues) && |
| 2042 | (this->name == other.name); |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2043 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2044 | bool Mesh::operator==(const Mesh &other) const { |
| 2045 | return this->extensions == other.extensions && this->extras == other.extras && |
Selmar Kok | 81b672b | 2019-10-18 16:08:44 +0200 | [diff] [blame] | 2046 | this->name == other.name && Equals(this->weights, other.weights) && |
| 2047 | this->primitives == other.primitives; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2048 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2049 | bool Model::operator==(const Model &other) const { |
| 2050 | return this->accessors == other.accessors && |
| 2051 | this->animations == other.animations && this->asset == other.asset && |
| 2052 | this->buffers == other.buffers && |
| 2053 | this->bufferViews == other.bufferViews && |
| 2054 | this->cameras == other.cameras && |
| 2055 | this->defaultScene == other.defaultScene && |
| 2056 | this->extensions == other.extensions && |
| 2057 | this->extensionsRequired == other.extensionsRequired && |
| 2058 | this->extensionsUsed == other.extensionsUsed && |
| 2059 | this->extras == other.extras && this->images == other.images && |
| 2060 | this->lights == other.lights && this->materials == other.materials && |
| 2061 | this->meshes == other.meshes && this->nodes == other.nodes && |
| 2062 | this->samplers == other.samplers && this->scenes == other.scenes && |
| 2063 | this->skins == other.skins && this->textures == other.textures; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2064 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2065 | bool Node::operator==(const Node &other) const { |
| 2066 | return this->camera == other.camera && this->children == other.children && |
| 2067 | this->extensions == other.extensions && this->extras == other.extras && |
| 2068 | Equals(this->matrix, other.matrix) && this->mesh == other.mesh && |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2069 | (this->light == other.light) && (this->emitter == other.emitter) && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2070 | this->name == other.name && Equals(this->rotation, other.rotation) && |
| 2071 | Equals(this->scale, other.scale) && this->skin == other.skin && |
| 2072 | Equals(this->translation, other.translation) && |
| 2073 | Equals(this->weights, other.weights); |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2074 | } |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 2075 | bool SpotLight::operator==(const SpotLight &other) const { |
| 2076 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2077 | TINYGLTF_DOUBLE_EQUAL(this->innerConeAngle, other.innerConeAngle) && |
| 2078 | TINYGLTF_DOUBLE_EQUAL(this->outerConeAngle, other.outerConeAngle); |
| 2079 | } |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2080 | bool PositionalEmitter::operator==(const PositionalEmitter &other) const { |
| 2081 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2082 | TINYGLTF_DOUBLE_EQUAL(this->coneInnerAngle, other.coneInnerAngle) && |
| 2083 | TINYGLTF_DOUBLE_EQUAL(this->coneOuterAngle, other.coneOuterAngle) && |
| 2084 | TINYGLTF_DOUBLE_EQUAL(this->coneOuterGain, other.coneOuterGain) && |
| 2085 | TINYGLTF_DOUBLE_EQUAL(this->maxDistance, other.maxDistance) && |
| 2086 | TINYGLTF_DOUBLE_EQUAL(this->refDistance, other.refDistance) && |
| 2087 | TINYGLTF_DOUBLE_EQUAL(this->rolloffFactor, other.rolloffFactor); |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 2088 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2089 | bool OrthographicCamera::operator==(const OrthographicCamera &other) const { |
| 2090 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2091 | TINYGLTF_DOUBLE_EQUAL(this->xmag, other.xmag) && |
| 2092 | TINYGLTF_DOUBLE_EQUAL(this->ymag, other.ymag) && |
| 2093 | TINYGLTF_DOUBLE_EQUAL(this->zfar, other.zfar) && |
| 2094 | TINYGLTF_DOUBLE_EQUAL(this->znear, other.znear); |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2095 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2096 | bool Parameter::operator==(const Parameter &other) const { |
| 2097 | if (this->bool_value != other.bool_value || |
| 2098 | this->has_number_value != other.has_number_value) |
| 2099 | return false; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2100 | |
Selmar Kok | 2bda71c | 2018-10-05 14:36:05 +0200 | [diff] [blame] | 2101 | if (!TINYGLTF_DOUBLE_EQUAL(this->number_value, other.number_value)) |
| 2102 | return false; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2103 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2104 | if (this->json_double_value.size() != other.json_double_value.size()) |
| 2105 | return false; |
| 2106 | for (auto &it : this->json_double_value) { |
| 2107 | auto otherIt = other.json_double_value.find(it.first); |
| 2108 | if (otherIt == other.json_double_value.end()) return false; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2109 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2110 | if (!TINYGLTF_DOUBLE_EQUAL(it.second, otherIt->second)) return false; |
| 2111 | } |
| 2112 | |
| 2113 | if (!Equals(this->number_array, other.number_array)) return false; |
| 2114 | |
| 2115 | if (this->string_value != other.string_value) return false; |
| 2116 | |
| 2117 | return true; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2118 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2119 | bool PerspectiveCamera::operator==(const PerspectiveCamera &other) const { |
| 2120 | return TINYGLTF_DOUBLE_EQUAL(this->aspectRatio, other.aspectRatio) && |
| 2121 | this->extensions == other.extensions && this->extras == other.extras && |
| 2122 | TINYGLTF_DOUBLE_EQUAL(this->yfov, other.yfov) && |
| 2123 | TINYGLTF_DOUBLE_EQUAL(this->zfar, other.zfar) && |
| 2124 | TINYGLTF_DOUBLE_EQUAL(this->znear, other.znear); |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2125 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2126 | bool Primitive::operator==(const Primitive &other) const { |
| 2127 | return this->attributes == other.attributes && this->extras == other.extras && |
| 2128 | this->indices == other.indices && this->material == other.material && |
| 2129 | this->mode == other.mode && this->targets == other.targets; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2130 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2131 | bool Sampler::operator==(const Sampler &other) const { |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 2132 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2133 | this->magFilter == other.magFilter && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2134 | this->minFilter == other.minFilter && this->name == other.name && |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 2135 | this->wrapS == other.wrapS && this->wrapT == other.wrapT; |
Syoyo Fujita | 2c521b3 | 2020-12-04 00:50:46 +0900 | [diff] [blame] | 2136 | |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 2137 | // this->wrapR == other.wrapR |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2138 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2139 | bool Scene::operator==(const Scene &other) const { |
| 2140 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2141 | this->name == other.name && this->nodes == other.nodes; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2142 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2143 | bool Skin::operator==(const Skin &other) const { |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 2144 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2145 | this->inverseBindMatrices == other.inverseBindMatrices && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2146 | this->joints == other.joints && this->name == other.name && |
| 2147 | this->skeleton == other.skeleton; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2148 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2149 | bool Texture::operator==(const Texture &other) const { |
| 2150 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2151 | this->name == other.name && this->sampler == other.sampler && |
| 2152 | this->source == other.source; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2153 | } |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 2154 | bool TextureInfo::operator==(const TextureInfo &other) const { |
| 2155 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2156 | this->index == other.index && this->texCoord == other.texCoord; |
| 2157 | } |
| 2158 | bool NormalTextureInfo::operator==(const NormalTextureInfo &other) const { |
| 2159 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2160 | this->index == other.index && this->texCoord == other.texCoord && |
| 2161 | TINYGLTF_DOUBLE_EQUAL(this->scale, other.scale); |
| 2162 | } |
| 2163 | bool OcclusionTextureInfo::operator==(const OcclusionTextureInfo &other) const { |
| 2164 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2165 | this->index == other.index && this->texCoord == other.texCoord && |
| 2166 | TINYGLTF_DOUBLE_EQUAL(this->strength, other.strength); |
| 2167 | } |
| 2168 | bool PbrMetallicRoughness::operator==(const PbrMetallicRoughness &other) const { |
| 2169 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2170 | (this->baseColorTexture == other.baseColorTexture) && |
| 2171 | (this->metallicRoughnessTexture == other.metallicRoughnessTexture) && |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 2172 | Equals(this->baseColorFactor, other.baseColorFactor) && |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 2173 | TINYGLTF_DOUBLE_EQUAL(this->metallicFactor, other.metallicFactor) && |
| 2174 | TINYGLTF_DOUBLE_EQUAL(this->roughnessFactor, other.roughnessFactor); |
| 2175 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2176 | bool Value::operator==(const Value &other) const { |
| 2177 | return Equals(*this, other); |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2178 | } |
| 2179 | |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 2180 | static void swap4(unsigned int *val) { |
| 2181 | #ifdef TINYGLTF_LITTLE_ENDIAN |
| 2182 | (void)val; |
| 2183 | #else |
| 2184 | unsigned int tmp = *val; |
| 2185 | unsigned char *dst = reinterpret_cast<unsigned char *>(val); |
| 2186 | unsigned char *src = reinterpret_cast<unsigned char *>(&tmp); |
| 2187 | |
| 2188 | dst[0] = src[3]; |
| 2189 | dst[1] = src[2]; |
| 2190 | dst[2] = src[1]; |
| 2191 | dst[3] = src[0]; |
| 2192 | #endif |
| 2193 | } |
| 2194 | |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2195 | static std::string JoinPath(const std::string &path0, |
| 2196 | const std::string &path1) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2197 | if (path0.empty()) { |
| 2198 | return path1; |
| 2199 | } else { |
| 2200 | // check '/' |
| 2201 | char lastChar = *path0.rbegin(); |
| 2202 | if (lastChar != '/') { |
| 2203 | return path0 + std::string("/") + path1; |
| 2204 | } else { |
| 2205 | return path0 + path1; |
| 2206 | } |
| 2207 | } |
| 2208 | } |
| 2209 | |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 2210 | static std::string FindFile(const std::vector<std::string> &paths, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2211 | const std::string &filepath, FsCallbacks *fs) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2212 | if (fs == nullptr || fs->ExpandFilePath == nullptr || |
| 2213 | fs->FileExists == nullptr) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2214 | // Error, fs callback[s] missing |
| 2215 | return std::string(); |
| 2216 | } |
| 2217 | |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2218 | // https://github.com/syoyo/tinygltf/issues/416 |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2219 | // Use strlen() since std::string's size/length reports the number of elements |
| 2220 | // in the buffer, not the length of string(null-terminated) strip |
| 2221 | // null-character in the middle of string. |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2222 | size_t slength = strlen(filepath.c_str()); |
| 2223 | if (slength == 0) { |
| 2224 | return std::string(); |
| 2225 | } |
| 2226 | |
| 2227 | std::string cleaned_filepath = std::string(filepath.c_str()); |
| 2228 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2229 | for (size_t i = 0; i < paths.size(); i++) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2230 | std::string absPath = |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2231 | fs->ExpandFilePath(JoinPath(paths[i], cleaned_filepath), fs->user_data); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2232 | if (fs->FileExists(absPath, fs->user_data)) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2233 | return absPath; |
| 2234 | } |
| 2235 | } |
| 2236 | |
| 2237 | return std::string(); |
| 2238 | } |
| 2239 | |
Syoyo Fujita | dc4bb86 | 2018-04-02 02:04:24 +0900 | [diff] [blame] | 2240 | static std::string GetFilePathExtension(const std::string &FileName) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2241 | if (FileName.find_last_of(".") != std::string::npos) |
| 2242 | return FileName.substr(FileName.find_last_of(".") + 1); |
| 2243 | return ""; |
| 2244 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2245 | |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 2246 | static std::string GetBaseDir(const std::string &filepath) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2247 | if (filepath.find_last_of("/\\") != std::string::npos) |
Patrick Mc Gartoll | 1bdd404 | 2024-05-16 18:11:26 -0700 | [diff] [blame^] | 2248 | return filepath.substr(0, filepath.find_last_of("/\\") + 1); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2249 | return ""; |
| 2250 | } |
| 2251 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2252 | static std::string GetBaseFilename(const std::string &filepath) { |
Martin Gerhardy | fae6543 | 2022-03-13 18:42:39 +0100 | [diff] [blame] | 2253 | auto idx = filepath.find_last_of("/\\"); |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 2254 | if (idx != std::string::npos) return filepath.substr(idx + 1); |
Alexander Wood | 190382a | 2021-10-08 12:19:13 -0400 | [diff] [blame] | 2255 | return filepath; |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 2256 | } |
| 2257 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2258 | std::string base64_encode(unsigned char const *, unsigned int len); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2259 | std::string base64_decode(std::string const &s); |
| 2260 | |
| 2261 | /* |
| 2262 | base64.cpp and base64.h |
| 2263 | |
| 2264 | Copyright (C) 2004-2008 René Nyffenegger |
| 2265 | |
| 2266 | This source code is provided 'as-is', without any express or implied |
| 2267 | warranty. In no event will the author be held liable for any damages |
| 2268 | arising from the use of this software. |
| 2269 | |
| 2270 | Permission is granted to anyone to use this software for any purpose, |
| 2271 | including commercial applications, and to alter it and redistribute it |
| 2272 | freely, subject to the following restrictions: |
| 2273 | |
| 2274 | 1. The origin of this source code must not be misrepresented; you must not |
| 2275 | claim that you wrote the original source code. If you use this source code |
| 2276 | in a product, an acknowledgment in the product documentation would be |
| 2277 | appreciated but is not required. |
| 2278 | |
| 2279 | 2. Altered source versions must be plainly marked as such, and must not be |
| 2280 | misrepresented as being the original source code. |
| 2281 | |
| 2282 | 3. This notice may not be removed or altered from any source distribution. |
| 2283 | |
| 2284 | René Nyffenegger rene.nyffenegger@adp-gmbh.ch |
| 2285 | |
| 2286 | */ |
| 2287 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 2288 | #ifdef __clang__ |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 2289 | #pragma clang diagnostic push |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 2290 | #pragma clang diagnostic ignored "-Wsign-conversion" |
| 2291 | #pragma clang diagnostic ignored "-Wconversion" |
Syoyo Fujita | 6fd9162 | 2016-05-01 20:27:30 +0900 | [diff] [blame] | 2292 | #endif |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2293 | |
| 2294 | static inline bool is_base64(unsigned char c) { |
| 2295 | return (isalnum(c) || (c == '+') || (c == '/')); |
| 2296 | } |
| 2297 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2298 | std::string base64_encode(unsigned char const *bytes_to_encode, |
| 2299 | unsigned int in_len) { |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 2300 | std::string ret; |
| 2301 | int i = 0; |
| 2302 | int j = 0; |
| 2303 | unsigned char char_array_3[3]; |
| 2304 | unsigned char char_array_4[4]; |
| 2305 | |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 2306 | const char *base64_chars = |
| 2307 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 2308 | "abcdefghijklmnopqrstuvwxyz" |
| 2309 | "0123456789+/"; |
| 2310 | |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 2311 | while (in_len--) { |
| 2312 | char_array_3[i++] = *(bytes_to_encode++); |
| 2313 | if (i == 3) { |
| 2314 | char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2315 | char_array_4[1] = |
| 2316 | ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); |
| 2317 | char_array_4[2] = |
| 2318 | ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 2319 | char_array_4[3] = char_array_3[2] & 0x3f; |
| 2320 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2321 | for (i = 0; (i < 4); i++) ret += base64_chars[char_array_4[i]]; |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 2322 | i = 0; |
| 2323 | } |
| 2324 | } |
| 2325 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2326 | if (i) { |
| 2327 | for (j = i; j < 3; j++) char_array_3[j] = '\0'; |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 2328 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2329 | char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; |
| 2330 | char_array_4[1] = |
| 2331 | ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); |
| 2332 | char_array_4[2] = |
| 2333 | ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 2334 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2335 | for (j = 0; (j < i + 1); j++) ret += base64_chars[char_array_4[j]]; |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 2336 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2337 | while ((i++ < 3)) ret += '='; |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 2338 | } |
| 2339 | |
| 2340 | return ret; |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 2341 | } |
| 2342 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2343 | std::string base64_decode(std::string const &encoded_string) { |
Syoyo Fujita | 8c6774c | 2016-05-01 20:36:29 +0900 | [diff] [blame] | 2344 | int in_len = static_cast<int>(encoded_string.size()); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2345 | int i = 0; |
| 2346 | int j = 0; |
| 2347 | int in_ = 0; |
| 2348 | unsigned char char_array_4[4], char_array_3[3]; |
| 2349 | std::string ret; |
| 2350 | |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 2351 | const std::string base64_chars = |
| 2352 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 2353 | "abcdefghijklmnopqrstuvwxyz" |
| 2354 | "0123456789+/"; |
| 2355 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2356 | while (in_len-- && (encoded_string[in_] != '=') && |
| 2357 | is_base64(encoded_string[in_])) { |
| 2358 | char_array_4[i++] = encoded_string[in_]; |
| 2359 | in_++; |
| 2360 | if (i == 4) { |
| 2361 | for (i = 0; i < 4; i++) |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2362 | char_array_4[i] = |
| 2363 | static_cast<unsigned char>(base64_chars.find(char_array_4[i])); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2364 | |
| 2365 | char_array_3[0] = |
| 2366 | (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); |
| 2367 | char_array_3[1] = |
| 2368 | ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); |
| 2369 | char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; |
| 2370 | |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 2371 | for (i = 0; (i < 3); i++) ret += char_array_3[i]; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2372 | i = 0; |
| 2373 | } |
| 2374 | } |
| 2375 | |
| 2376 | if (i) { |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 2377 | for (j = i; j < 4; j++) char_array_4[j] = 0; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2378 | |
| 2379 | for (j = 0; j < 4; j++) |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2380 | char_array_4[j] = |
| 2381 | static_cast<unsigned char>(base64_chars.find(char_array_4[j])); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2382 | |
| 2383 | char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); |
| 2384 | char_array_3[1] = |
| 2385 | ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); |
| 2386 | char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; |
| 2387 | |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 2388 | for (j = 0; (j < i - 1); j++) ret += char_array_3[j]; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | return ret; |
| 2392 | } |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 2393 | #ifdef __clang__ |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 2394 | #pragma clang diagnostic pop |
Syoyo Fujita | 6fd9162 | 2016-05-01 20:27:30 +0900 | [diff] [blame] | 2395 | #endif |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2396 | |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2397 | // https://github.com/syoyo/tinygltf/issues/228 |
| 2398 | // TODO(syoyo): Use uriparser https://uriparser.github.io/ for stricter Uri |
| 2399 | // decoding? |
| 2400 | // |
Alexander Wood | e4bc6c7 | 2021-10-14 08:54:59 -0400 | [diff] [blame] | 2401 | // Uri Decoding from DLIB |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2402 | // http://dlib.net/dlib/server/server_http.cpp.html |
Alexander Wood | e4bc6c7 | 2021-10-14 08:54:59 -0400 | [diff] [blame] | 2403 | // --- dlib begin ------------------------------------------------------------ |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2404 | // Copyright (C) 2003 Davis E. King (davis@dlib.net) |
Alexander Wood | e4bc6c7 | 2021-10-14 08:54:59 -0400 | [diff] [blame] | 2405 | // License: Boost Software License |
| 2406 | // Boost Software License - Version 1.0 - August 17th, 2003 |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2407 | |
Alexander Wood | e4bc6c7 | 2021-10-14 08:54:59 -0400 | [diff] [blame] | 2408 | // Permission is hereby granted, free of charge, to any person or organization |
| 2409 | // obtaining a copy of the software and accompanying documentation covered by |
| 2410 | // this license (the "Software") to use, reproduce, display, distribute, |
| 2411 | // execute, and transmit the Software, and to prepare derivative works of the |
| 2412 | // Software, and to permit third-parties to whom the Software is furnished to |
| 2413 | // do so, all subject to the following: |
| 2414 | // The copyright notices in the Software and this entire statement, including |
| 2415 | // the above license grant, this restriction and the following disclaimer, |
| 2416 | // must be included in all copies of the Software, in whole or in part, and |
| 2417 | // all derivative works of the Software, unless such copies or derivative |
| 2418 | // works are solely in the form of machine-executable object code generated by |
| 2419 | // a source language processor. |
| 2420 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 2421 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 2422 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
| 2423 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
| 2424 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
| 2425 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 2426 | // DEALINGS IN THE SOFTWARE. |
Syoyo Fujita | cbd3885 | 2022-02-26 21:19:15 +0900 | [diff] [blame] | 2427 | // |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2428 | namespace dlib { |
| 2429 | |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2430 | inline unsigned char from_hex(unsigned char ch) { |
| 2431 | if (ch <= '9' && ch >= '0') |
| 2432 | ch -= '0'; |
| 2433 | else if (ch <= 'f' && ch >= 'a') |
| 2434 | ch -= 'a' - 10; |
| 2435 | else if (ch <= 'F' && ch >= 'A') |
| 2436 | ch -= 'A' - 10; |
| 2437 | else |
| 2438 | ch = 0; |
| 2439 | return ch; |
| 2440 | } |
| 2441 | |
| 2442 | static const std::string urldecode(const std::string &str) { |
| 2443 | using namespace std; |
| 2444 | string result; |
| 2445 | string::size_type i; |
| 2446 | for (i = 0; i < str.size(); ++i) { |
| 2447 | if (str[i] == '+') { |
| 2448 | result += ' '; |
| 2449 | } else if (str[i] == '%' && str.size() > i + 2) { |
| 2450 | const unsigned char ch1 = |
| 2451 | from_hex(static_cast<unsigned char>(str[i + 1])); |
| 2452 | const unsigned char ch2 = |
| 2453 | from_hex(static_cast<unsigned char>(str[i + 2])); |
| 2454 | const unsigned char ch = static_cast<unsigned char>((ch1 << 4) | ch2); |
| 2455 | result += static_cast<char>(ch); |
| 2456 | i += 2; |
| 2457 | } else { |
| 2458 | result += str[i]; |
| 2459 | } |
| 2460 | } |
| 2461 | return result; |
| 2462 | } |
| 2463 | |
| 2464 | } // namespace dlib |
| 2465 | // --- dlib end -------------------------------------------------------------- |
| 2466 | |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 2467 | bool URIDecode(const std::string &in_uri, std::string *out_uri, |
| 2468 | void *user_data) { |
| 2469 | (void)user_data; |
| 2470 | *out_uri = dlib::urldecode(in_uri); |
| 2471 | return true; |
| 2472 | } |
| 2473 | |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 2474 | static bool LoadExternalFile(std::vector<unsigned char> *out, std::string *err, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2475 | std::string *warn, const std::string &filename, |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2476 | const std::string &basedir, bool required, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2477 | size_t reqBytes, bool checkSize, |
| 2478 | size_t maxFileSize, FsCallbacks *fs) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2479 | if (fs == nullptr || fs->FileExists == nullptr || |
| 2480 | fs->ExpandFilePath == nullptr || fs->ReadWholeFile == nullptr) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2481 | // This is a developer error, assert() ? |
| 2482 | if (err) { |
| 2483 | (*err) += "FS callback[s] not set\n"; |
| 2484 | } |
| 2485 | return false; |
| 2486 | } |
| 2487 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2488 | std::string *failMsgOut = required ? err : warn; |
Selmar Kok | e3b3fa9 | 2018-08-22 19:04:21 +0200 | [diff] [blame] | 2489 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 2490 | out->clear(); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2491 | |
| 2492 | std::vector<std::string> paths; |
| 2493 | paths.push_back(basedir); |
| 2494 | paths.push_back("."); |
| 2495 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2496 | std::string filepath = FindFile(paths, filename, fs); |
jianghaosen | b721fb7 | 2017-08-25 21:01:17 +0800 | [diff] [blame] | 2497 | if (filepath.empty() || filename.empty()) { |
Selmar Kok | e3b3fa9 | 2018-08-22 19:04:21 +0200 | [diff] [blame] | 2498 | if (failMsgOut) { |
| 2499 | (*failMsgOut) += "File not found : " + filename + "\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 2500 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2501 | return false; |
| 2502 | } |
| 2503 | |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2504 | // Check file size |
| 2505 | if (fs->GetFileSizeInBytes) { |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2506 | size_t file_size{0}; |
| 2507 | std::string _err; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2508 | bool ok = |
| 2509 | fs->GetFileSizeInBytes(&file_size, &_err, filepath, fs->user_data); |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2510 | if (!ok) { |
| 2511 | if (_err.size()) { |
| 2512 | if (failMsgOut) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2513 | (*failMsgOut) += "Getting file size failed : " + filename + |
| 2514 | ", err = " + _err + "\n"; |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2515 | } |
| 2516 | } |
| 2517 | return false; |
| 2518 | } |
| 2519 | |
| 2520 | if (file_size > maxFileSize) { |
| 2521 | if (failMsgOut) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2522 | (*failMsgOut) += "File size " + std::to_string(file_size) + |
| 2523 | " exceeds maximum allowed file size " + |
| 2524 | std::to_string(maxFileSize) + " : " + filepath + "\n"; |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2525 | } |
| 2526 | return false; |
| 2527 | } |
| 2528 | } |
| 2529 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2530 | std::vector<unsigned char> buf; |
| 2531 | std::string fileReadErr; |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2532 | bool fileRead = |
| 2533 | fs->ReadWholeFile(&buf, &fileReadErr, filepath, fs->user_data); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2534 | if (!fileRead) { |
Selmar Kok | e3b3fa9 | 2018-08-22 19:04:21 +0200 | [diff] [blame] | 2535 | if (failMsgOut) { |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2536 | (*failMsgOut) += |
| 2537 | "File read error : " + filepath + " : " + fileReadErr + "\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 2538 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2539 | return false; |
| 2540 | } |
| 2541 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2542 | size_t sz = buf.size(); |
Syoyo Fujita | 2b0307f | 2017-08-01 18:15:52 +0900 | [diff] [blame] | 2543 | if (sz == 0) { |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2544 | if (failMsgOut) { |
Selmar Kok | e3b3fa9 | 2018-08-22 19:04:21 +0200 | [diff] [blame] | 2545 | (*failMsgOut) += "File is empty : " + filepath + "\n"; |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2546 | } |
Syoyo Fujita | 2b0307f | 2017-08-01 18:15:52 +0900 | [diff] [blame] | 2547 | return false; |
| 2548 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2549 | |
| 2550 | if (checkSize) { |
| 2551 | if (reqBytes == sz) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 2552 | out->swap(buf); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2553 | return true; |
| 2554 | } else { |
| 2555 | std::stringstream ss; |
| 2556 | ss << "File size mismatch : " << filepath << ", requestedBytes " |
| 2557 | << reqBytes << ", but got " << sz << std::endl; |
Selmar Kok | e3b3fa9 | 2018-08-22 19:04:21 +0200 | [diff] [blame] | 2558 | if (failMsgOut) { |
| 2559 | (*failMsgOut) += ss.str(); |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 2560 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2561 | return false; |
| 2562 | } |
| 2563 | } |
| 2564 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 2565 | out->swap(buf); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2566 | return true; |
| 2567 | } |
| 2568 | |
Nyall Dawson | 8c85d5e | 2023-08-28 12:56:09 +1000 | [diff] [blame] | 2569 | void TinyGLTF::SetParseStrictness(ParseStrictness strictness) { |
| 2570 | strictness_ = strictness; |
| 2571 | } |
| 2572 | |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 2573 | void TinyGLTF::SetImageLoader(LoadImageDataFunction func, void *user_data) { |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 2574 | LoadImageData = func; |
| 2575 | load_image_user_data_ = user_data; |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 2576 | user_image_loader_ = true; |
| 2577 | } |
| 2578 | |
| 2579 | void TinyGLTF::RemoveImageLoader() { |
| 2580 | LoadImageData = |
| 2581 | #ifndef TINYGLTF_NO_STB_IMAGE |
| 2582 | &tinygltf::LoadImageData; |
| 2583 | #else |
| 2584 | nullptr; |
| 2585 | #endif |
| 2586 | |
| 2587 | load_image_user_data_ = nullptr; |
| 2588 | user_image_loader_ = false; |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 2589 | } |
| 2590 | |
Squareys | 2d3594d | 2018-03-13 22:40:53 +0100 | [diff] [blame] | 2591 | #ifndef TINYGLTF_NO_STB_IMAGE |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2592 | bool LoadImageData(Image *image, const int image_idx, std::string *err, |
| 2593 | std::string *warn, int req_width, int req_height, |
| 2594 | const unsigned char *bytes, int size, void *user_data) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2595 | (void)warn; |
| 2596 | |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 2597 | LoadImageDataOption option; |
| 2598 | if (user_data) { |
| 2599 | option = *reinterpret_cast<LoadImageDataOption *>(user_data); |
| 2600 | } |
| 2601 | |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 2602 | int w = 0, h = 0, comp = 0, req_comp = 0; |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2603 | |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2604 | unsigned char *data = nullptr; |
Arthur Brainville (Ybalrid) | f2addc0 | 2019-03-02 22:00:48 +0100 | [diff] [blame] | 2605 | |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 2606 | // preserve_channels true: Use channels stored in the image file. |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 2607 | // false: force 32-bit textures for common Vulkan compatibility. It appears |
| 2608 | // that some GPU drivers do not support 24-bit images for Vulkan |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 2609 | req_comp = option.preserve_channels ? 0 : 4; |
Arthur Brainville (Ybalrid) | f2addc0 | 2019-03-02 22:00:48 +0100 | [diff] [blame] | 2610 | int bits = 8; |
| 2611 | int pixel_type = TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE; |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2612 | |
Arthur Brainville (Ybalrid) | f2addc0 | 2019-03-02 22:00:48 +0100 | [diff] [blame] | 2613 | // It is possible that the image we want to load is a 16bit per channel image |
| 2614 | // We are going to attempt to load it as 16bit per channel, and if it worked, |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 2615 | // set the image data accordingly. We are casting the returned pointer into |
Arthur Brainville (Ybalrid) | f2addc0 | 2019-03-02 22:00:48 +0100 | [diff] [blame] | 2616 | // unsigned char, because we are representing "bytes". But we are updating |
| 2617 | // the Image metadata to signal that this image uses 2 bytes (16bits) per |
| 2618 | // channel: |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2619 | if (stbi_is_16_bit_from_memory(bytes, size)) { |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 2620 | data = reinterpret_cast<unsigned char *>( |
| 2621 | stbi_load_16_from_memory(bytes, size, &w, &h, &comp, req_comp)); |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2622 | if (data) { |
| 2623 | bits = 16; |
| 2624 | pixel_type = TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT; |
| 2625 | } |
Arthur Brainville (Ybalrid) | f2addc0 | 2019-03-02 22:00:48 +0100 | [diff] [blame] | 2626 | } |
| 2627 | |
| 2628 | // at this point, if data is still NULL, it means that the image wasn't |
| 2629 | // 16bit per channel, we are going to load it as a normal 8bit per channel |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 2630 | // image as we used to do: |
Aurélien Chatelain | 5cb4346 | 2017-05-24 09:55:14 +0000 | [diff] [blame] | 2631 | // if image cannot be decoded, ignore parsing and keep it by its path |
| 2632 | // don't break in this case |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 2633 | // FIXME we should only enter this function if the image is embedded. If |
| 2634 | // image->uri references |
| 2635 | // an image file, it should be left as it is. Image loading should not be |
| 2636 | // mandatory (to support other formats) |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2637 | if (!data) data = stbi_load_from_memory(bytes, size, &w, &h, &comp, req_comp); |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2638 | if (!data) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2639 | // NOTE: you can use `warn` instead of `err` |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2640 | if (err) { |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2641 | (*err) += |
| 2642 | "Unknown image format. STB cannot decode image data for image[" + |
| 2643 | std::to_string(image_idx) + "] name = \"" + image->name + "\".\n"; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2644 | } |
Omar C. F | d492efc | 2018-02-10 09:50:35 +0200 | [diff] [blame] | 2645 | return false; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2646 | } |
| 2647 | |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 2648 | if ((w < 1) || (h < 1)) { |
Arthur Brainville (Ybalrid) | f2addc0 | 2019-03-02 22:00:48 +0100 | [diff] [blame] | 2649 | stbi_image_free(data); |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2650 | if (err) { |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2651 | (*err) += "Invalid image data for image[" + std::to_string(image_idx) + |
| 2652 | "] name = \"" + image->name + "\"\n"; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2653 | } |
Omar C. F | d492efc | 2018-02-10 09:50:35 +0200 | [diff] [blame] | 2654 | return false; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2655 | } |
| 2656 | |
| 2657 | if (req_width > 0) { |
| 2658 | if (req_width != w) { |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2659 | stbi_image_free(data); |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2660 | if (err) { |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2661 | (*err) += "Image width mismatch for image[" + |
| 2662 | std::to_string(image_idx) + "] name = \"" + image->name + |
| 2663 | "\"\n"; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2664 | } |
| 2665 | return false; |
| 2666 | } |
| 2667 | } |
| 2668 | |
| 2669 | if (req_height > 0) { |
| 2670 | if (req_height != h) { |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2671 | stbi_image_free(data); |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2672 | if (err) { |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2673 | (*err) += "Image height mismatch. for image[" + |
| 2674 | std::to_string(image_idx) + "] name = \"" + image->name + |
| 2675 | "\"\n"; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2676 | } |
| 2677 | return false; |
| 2678 | } |
| 2679 | } |
| 2680 | |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 2681 | if (req_comp != 0) { |
| 2682 | // loaded data has `req_comp` channels(components) |
| 2683 | comp = req_comp; |
| 2684 | } |
| 2685 | |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2686 | image->width = w; |
| 2687 | image->height = h; |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 2688 | image->component = comp; |
Arthur Brainville (Ybalrid) | f2addc0 | 2019-03-02 22:00:48 +0100 | [diff] [blame] | 2689 | image->bits = bits; |
| 2690 | image->pixel_type = pixel_type; |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 2691 | image->image.resize(static_cast<size_t>(w * h * comp) * size_t(bits / 8)); |
| 2692 | std::copy(data, data + w * h * comp * (bits / 8), image->image.begin()); |
Arthur Brainville (Ybalrid) | f2addc0 | 2019-03-02 22:00:48 +0100 | [diff] [blame] | 2693 | stbi_image_free(data); |
Syoyo Fujita | 4be2f88 | 2016-11-24 16:20:34 +0900 | [diff] [blame] | 2694 | |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2695 | return true; |
| 2696 | } |
Squareys | 2d3594d | 2018-03-13 22:40:53 +0100 | [diff] [blame] | 2697 | #endif |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2698 | |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2699 | void TinyGLTF::SetImageWriter(WriteImageDataFunction func, void *user_data) { |
| 2700 | WriteImageData = func; |
| 2701 | write_image_user_data_ = user_data; |
| 2702 | } |
| 2703 | |
| 2704 | #ifndef TINYGLTF_NO_STB_IMAGE_WRITE |
| 2705 | static void WriteToMemory_stbi(void *context, void *data, int size) { |
| 2706 | std::vector<unsigned char> *buffer = |
| 2707 | reinterpret_cast<std::vector<unsigned char> *>(context); |
| 2708 | |
| 2709 | unsigned char *pData = reinterpret_cast<unsigned char *>(data); |
| 2710 | |
| 2711 | buffer->insert(buffer->end(), pData, pData + size); |
| 2712 | } |
| 2713 | |
| 2714 | bool WriteImageData(const std::string *basepath, const std::string *filename, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 2715 | const Image *image, bool embedImages, |
| 2716 | const URICallbacks *uri_cb, std::string *out_uri, |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 2717 | void *fsPtr) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2718 | const std::string ext = GetFilePathExtension(*filename); |
| 2719 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2720 | // Write image to temporary buffer |
| 2721 | std::string header; |
| 2722 | std::vector<unsigned char> data; |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2723 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2724 | if (ext == "png") { |
Syoyo Fujita | ca56f72 | 2019-03-07 21:04:25 +0900 | [diff] [blame] | 2725 | if ((image->bits != 8) || |
| 2726 | (image->pixel_type != TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE)) { |
Syoyo Fujita | e8a46c4 | 2019-03-07 20:50:58 +0900 | [diff] [blame] | 2727 | // Unsupported pixel format |
| 2728 | return false; |
| 2729 | } |
| 2730 | |
Keiji Yoshimi | c0cfc1e | 2018-08-28 21:13:03 +0900 | [diff] [blame] | 2731 | if (!stbi_write_png_to_func(WriteToMemory_stbi, &data, image->width, |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2732 | image->height, image->component, |
| 2733 | &image->image[0], 0)) { |
Keiji Yoshimi | c0cfc1e | 2018-08-28 21:13:03 +0900 | [diff] [blame] | 2734 | return false; |
| 2735 | } |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2736 | header = "data:image/png;base64,"; |
| 2737 | } else if (ext == "jpg") { |
Keiji Yoshimi | c0cfc1e | 2018-08-28 21:13:03 +0900 | [diff] [blame] | 2738 | if (!stbi_write_jpg_to_func(WriteToMemory_stbi, &data, image->width, |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2739 | image->height, image->component, |
| 2740 | &image->image[0], 100)) { |
Keiji Yoshimi | c0cfc1e | 2018-08-28 21:13:03 +0900 | [diff] [blame] | 2741 | return false; |
| 2742 | } |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2743 | header = "data:image/jpeg;base64,"; |
| 2744 | } else if (ext == "bmp") { |
Keiji Yoshimi | c0cfc1e | 2018-08-28 21:13:03 +0900 | [diff] [blame] | 2745 | if (!stbi_write_bmp_to_func(WriteToMemory_stbi, &data, image->width, |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2746 | image->height, image->component, |
| 2747 | &image->image[0])) { |
Keiji Yoshimi | c0cfc1e | 2018-08-28 21:13:03 +0900 | [diff] [blame] | 2748 | return false; |
| 2749 | } |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2750 | header = "data:image/bmp;base64,"; |
| 2751 | } else if (!embedImages) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2752 | // Error: can't output requested format to file |
| 2753 | return false; |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2754 | } |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2755 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2756 | if (embedImages) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2757 | // Embed base64-encoded image into URI |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2758 | if (data.size()) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2759 | *out_uri = header + base64_encode(&data[0], |
| 2760 | static_cast<unsigned int>(data.size())); |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2761 | } else { |
| 2762 | // Throw error? |
| 2763 | } |
| 2764 | } else { |
| 2765 | // Write image to disc |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2766 | FsCallbacks *fs = reinterpret_cast<FsCallbacks *>(fsPtr); |
Syoyo Fujita | 33514af | 2018-11-05 13:32:43 +0900 | [diff] [blame] | 2767 | if ((fs != nullptr) && (fs->WriteWholeFile != nullptr)) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2768 | const std::string imagefilepath = JoinPath(*basepath, *filename); |
| 2769 | std::string writeError; |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2770 | if (!fs->WriteWholeFile(&writeError, imagefilepath, data, |
| 2771 | fs->user_data)) { |
| 2772 | // Could not write image file to disc; Throw error ? |
Keiji Yoshimi | c0cfc1e | 2018-08-28 21:13:03 +0900 | [diff] [blame] | 2773 | return false; |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2774 | } |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2775 | } else { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2776 | // Throw error? |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2777 | } |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 2778 | if (uri_cb->encode) { |
| 2779 | if (!uri_cb->encode(*filename, "image", out_uri, uri_cb->user_data)) { |
| 2780 | return false; |
| 2781 | } |
| 2782 | } else { |
| 2783 | *out_uri = *filename; |
| 2784 | } |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2785 | } |
| 2786 | |
| 2787 | return true; |
| 2788 | } |
| 2789 | #endif |
| 2790 | |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 2791 | void TinyGLTF::SetURICallbacks(URICallbacks callbacks) { |
| 2792 | assert(callbacks.decode); |
| 2793 | if (callbacks.decode) { |
| 2794 | uri_cb = callbacks; |
| 2795 | } |
| 2796 | } |
| 2797 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2798 | void TinyGLTF::SetFsCallbacks(FsCallbacks callbacks) { fs = callbacks; } |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2799 | |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 2800 | #ifdef _WIN32 |
| 2801 | static inline std::wstring UTF8ToWchar(const std::string &str) { |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2802 | int wstr_size = |
| 2803 | MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), nullptr, 0); |
imallett | 56e1098 | 2022-10-07 10:35:16 -0700 | [diff] [blame] | 2804 | std::wstring wstr((size_t)wstr_size, 0); |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2805 | MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), &wstr[0], |
| 2806 | (int)wstr.size()); |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 2807 | return wstr; |
| 2808 | } |
cwbhhjl | 2f5aa9f | 2020-06-04 10:40:38 +0800 | [diff] [blame] | 2809 | |
| 2810 | static inline std::string WcharToUTF8(const std::wstring &wstr) { |
| 2811 | int str_size = WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), |
imallett | 56e1098 | 2022-10-07 10:35:16 -0700 | [diff] [blame] | 2812 | nullptr, 0, nullptr, nullptr); |
| 2813 | std::string str((size_t)str_size, 0); |
cwbhhjl | 2f5aa9f | 2020-06-04 10:40:38 +0800 | [diff] [blame] | 2814 | WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), &str[0], |
imallett | 56e1098 | 2022-10-07 10:35:16 -0700 | [diff] [blame] | 2815 | (int)str.size(), nullptr, nullptr); |
cwbhhjl | 2f5aa9f | 2020-06-04 10:40:38 +0800 | [diff] [blame] | 2816 | return str; |
| 2817 | } |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 2818 | #endif |
| 2819 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2820 | #ifndef TINYGLTF_NO_FS |
| 2821 | // Default implementations of filesystem functions |
| 2822 | |
| 2823 | bool FileExists(const std::string &abs_filename, void *) { |
| 2824 | bool ret; |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 2825 | #ifdef TINYGLTF_ANDROID_LOAD_FROM_ASSETS |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 2826 | if (asset_manager) { |
| 2827 | AAsset *asset = AAssetManager_open(asset_manager, abs_filename.c_str(), |
| 2828 | AASSET_MODE_STREAMING); |
| 2829 | if (!asset) { |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 2830 | return false; |
| 2831 | } |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 2832 | AAsset_close(asset); |
| 2833 | ret = true; |
| 2834 | } else { |
| 2835 | return false; |
| 2836 | } |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 2837 | #else |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2838 | #ifdef _WIN32 |
R.E. Kovalev | 112e353 | 2023-07-05 09:11:57 +0300 | [diff] [blame] | 2839 | #if defined(_MSC_VER) || defined(_LIBCPP_VERSION) |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2840 | |
| 2841 | // First check if a file is a directory. |
| 2842 | DWORD result = GetFileAttributesW(UTF8ToWchar(abs_filename).c_str()); |
| 2843 | if (result == INVALID_FILE_ATTRIBUTES) { |
| 2844 | return false; |
| 2845 | } |
| 2846 | if (result & FILE_ATTRIBUTE_DIRECTORY) { |
| 2847 | return false; |
| 2848 | } |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2849 | |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 2850 | FILE *fp = nullptr; |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 2851 | errno_t err = _wfopen_s(&fp, UTF8ToWchar(abs_filename).c_str(), L"rb"); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2852 | if (err != 0) { |
| 2853 | return false; |
| 2854 | } |
R.E. Kovalev | 112e353 | 2023-07-05 09:11:57 +0300 | [diff] [blame] | 2855 | #elif defined(__GLIBCXX__) |
| 2856 | FILE *fp = fopen(abs_filename.c_str(), "rb"); |
| 2857 | if (!fp) { |
| 2858 | return false; |
| 2859 | } |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2860 | #else |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2861 | // TODO: is_directory check |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 2862 | FILE *fp = nullptr; |
| 2863 | errno_t err = fopen_s(&fp, abs_filename.c_str(), "rb"); |
| 2864 | if (err != 0) { |
| 2865 | return false; |
| 2866 | } |
| 2867 | #endif |
| 2868 | |
| 2869 | #else |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2870 | struct stat sb; |
| 2871 | if (stat(abs_filename.c_str(), &sb)) { |
| 2872 | return false; |
| 2873 | } |
| 2874 | if (S_ISDIR(sb.st_mode)) { |
| 2875 | return false; |
| 2876 | } |
| 2877 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2878 | FILE *fp = fopen(abs_filename.c_str(), "rb"); |
| 2879 | #endif |
| 2880 | if (fp) { |
| 2881 | ret = true; |
| 2882 | fclose(fp); |
| 2883 | } else { |
| 2884 | ret = false; |
| 2885 | } |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 2886 | #endif |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2887 | |
| 2888 | return ret; |
| 2889 | } |
| 2890 | |
| 2891 | std::string ExpandFilePath(const std::string &filepath, void *) { |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 2892 | // https://github.com/syoyo/tinygltf/issues/368 |
| 2893 | // |
| 2894 | // No file path expansion in built-in FS function anymore, since glTF URI |
| 2895 | // should not contain tilde('~') and environment variables, and for security |
| 2896 | // reason(`wordexp`). |
| 2897 | // |
| 2898 | // Users need to supply `base_dir`(in `LoadASCIIFromString`, |
| 2899 | // `LoadBinaryFromMemory`) in expanded absolute path. |
| 2900 | |
| 2901 | return filepath; |
| 2902 | |
| 2903 | #if 0 |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2904 | #ifdef _WIN32 |
Syoyo Fujita | fbbeb4d | 2020-06-06 17:11:50 +0900 | [diff] [blame] | 2905 | // Assume input `filepath` is encoded in UTF-8 |
cwbhhjl | 2f5aa9f | 2020-06-04 10:40:38 +0800 | [diff] [blame] | 2906 | std::wstring wfilepath = UTF8ToWchar(filepath); |
Syoyo Fujita | fbbeb4d | 2020-06-06 17:11:50 +0900 | [diff] [blame] | 2907 | DWORD wlen = ExpandEnvironmentStringsW(wfilepath.c_str(), nullptr, 0); |
cwbhhjl | 2f5aa9f | 2020-06-04 10:40:38 +0800 | [diff] [blame] | 2908 | wchar_t *wstr = new wchar_t[wlen]; |
| 2909 | ExpandEnvironmentStringsW(wfilepath.c_str(), wstr, wlen); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2910 | |
cwbhhjl | 2f5aa9f | 2020-06-04 10:40:38 +0800 | [diff] [blame] | 2911 | std::wstring ws(wstr); |
| 2912 | delete[] wstr; |
| 2913 | return WcharToUTF8(ws); |
Syoyo Fujita | fbbeb4d | 2020-06-06 17:11:50 +0900 | [diff] [blame] | 2914 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2915 | #else |
| 2916 | |
| 2917 | #if defined(TARGET_OS_IPHONE) || defined(TARGET_IPHONE_SIMULATOR) || \ |
Julian Smith | 0598a20 | 2021-08-25 12:06:08 +0100 | [diff] [blame] | 2918 | defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__OpenBSD__) |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2919 | // no expansion |
| 2920 | std::string s = filepath; |
| 2921 | #else |
| 2922 | std::string s; |
| 2923 | wordexp_t p; |
| 2924 | |
| 2925 | if (filepath.empty()) { |
| 2926 | return ""; |
| 2927 | } |
| 2928 | |
Frank Galligan | aa3c5a1 | 2020-01-13 15:06:56 -0800 | [diff] [blame] | 2929 | // Quote the string to keep any spaces in filepath intact. |
| 2930 | std::string quoted_path = "\"" + filepath + "\""; |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2931 | // char** w; |
Frank Galligan | aa3c5a1 | 2020-01-13 15:06:56 -0800 | [diff] [blame] | 2932 | int ret = wordexp(quoted_path.c_str(), &p, 0); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2933 | if (ret) { |
| 2934 | // err |
| 2935 | s = filepath; |
| 2936 | return s; |
| 2937 | } |
| 2938 | |
| 2939 | // Use first element only. |
| 2940 | if (p.we_wordv) { |
| 2941 | s = std::string(p.we_wordv[0]); |
| 2942 | wordfree(&p); |
| 2943 | } else { |
| 2944 | s = filepath; |
| 2945 | } |
| 2946 | |
| 2947 | #endif |
| 2948 | |
| 2949 | return s; |
| 2950 | #endif |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 2951 | #endif |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2952 | } |
| 2953 | |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2954 | bool GetFileSizeInBytes(size_t *filesize_out, std::string *err, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2955 | const std::string &filepath, void *userdata) { |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2956 | (void)userdata; |
| 2957 | |
| 2958 | #ifdef TINYGLTF_ANDROID_LOAD_FROM_ASSETS |
| 2959 | if (asset_manager) { |
| 2960 | AAsset *asset = AAssetManager_open(asset_manager, filepath.c_str(), |
| 2961 | AASSET_MODE_STREAMING); |
| 2962 | if (!asset) { |
| 2963 | if (err) { |
| 2964 | (*err) += "File open error : " + filepath + "\n"; |
| 2965 | } |
| 2966 | return false; |
| 2967 | } |
| 2968 | size_t size = AAsset_getLength(asset); |
| 2969 | |
| 2970 | if (size == 0) { |
| 2971 | if (err) { |
| 2972 | (*err) += "Invalid file size : " + filepath + |
| 2973 | " (does the path point to a directory?)"; |
| 2974 | } |
| 2975 | return false; |
| 2976 | } |
| 2977 | |
| 2978 | return true; |
| 2979 | } else { |
| 2980 | if (err) { |
| 2981 | (*err) += "No asset manager specified : " + filepath + "\n"; |
| 2982 | } |
| 2983 | return false; |
| 2984 | } |
| 2985 | #else |
| 2986 | #ifdef _WIN32 |
| 2987 | #if defined(__GLIBCXX__) // mingw |
| 2988 | int file_descriptor = |
| 2989 | _wopen(UTF8ToWchar(filepath).c_str(), _O_RDONLY | _O_BINARY); |
| 2990 | __gnu_cxx::stdio_filebuf<char> wfile_buf(file_descriptor, std::ios_base::in); |
| 2991 | std::istream f(&wfile_buf); |
| 2992 | #elif defined(_MSC_VER) || defined(_LIBCPP_VERSION) |
| 2993 | // For libcxx, assume _LIBCPP_HAS_OPEN_WITH_WCHAR is defined to accept |
| 2994 | // `wchar_t *` |
| 2995 | std::ifstream f(UTF8ToWchar(filepath).c_str(), std::ifstream::binary); |
| 2996 | #else |
| 2997 | // Unknown compiler/runtime |
| 2998 | std::ifstream f(filepath.c_str(), std::ifstream::binary); |
| 2999 | #endif |
| 3000 | #else |
| 3001 | std::ifstream f(filepath.c_str(), std::ifstream::binary); |
| 3002 | #endif |
| 3003 | if (!f) { |
| 3004 | if (err) { |
| 3005 | (*err) += "File open error : " + filepath + "\n"; |
| 3006 | } |
| 3007 | return false; |
| 3008 | } |
| 3009 | |
| 3010 | // For directory(and pipe?), peek() will fail(Posix gnustl/libc++ only) |
David Siegel | 47208b2 | 2023-06-05 22:14:30 +0200 | [diff] [blame] | 3011 | f.peek(); |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3012 | if (!f) { |
| 3013 | if (err) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3014 | (*err) += |
| 3015 | "File read error. Maybe empty file or invalid file : " + filepath + |
| 3016 | "\n"; |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3017 | } |
| 3018 | return false; |
| 3019 | } |
| 3020 | |
| 3021 | f.seekg(0, f.end); |
jamesvert | f57d18a | 2024-03-26 11:06:16 +0000 | [diff] [blame] | 3022 | const auto sz = f.tellg(); |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3023 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3024 | // std::cout << "sz = " << sz << "\n"; |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3025 | f.seekg(0, f.beg); |
| 3026 | |
jamesvert | f57d18a | 2024-03-26 11:06:16 +0000 | [diff] [blame] | 3027 | if (sz < 0) { |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3028 | if (err) { |
| 3029 | (*err) += "Invalid file size : " + filepath + |
| 3030 | " (does the path point to a directory?)"; |
| 3031 | } |
| 3032 | return false; |
jamesvert | e3f9a7d | 2024-03-26 11:32:43 +0000 | [diff] [blame] | 3033 | } else if (sz == std::streamoff(0)) { |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3034 | if (err) { |
| 3035 | (*err) += "File is empty : " + filepath + "\n"; |
| 3036 | } |
| 3037 | return false; |
| 3038 | } else if (sz >= (std::numeric_limits<std::streamoff>::max)()) { |
| 3039 | if (err) { |
| 3040 | (*err) += "Invalid file size : " + filepath + "\n"; |
| 3041 | } |
| 3042 | return false; |
| 3043 | } |
| 3044 | |
| 3045 | (*filesize_out) = sz; |
| 3046 | return true; |
| 3047 | #endif |
| 3048 | } |
| 3049 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3050 | bool ReadWholeFile(std::vector<unsigned char> *out, std::string *err, |
| 3051 | const std::string &filepath, void *) { |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 3052 | #ifdef TINYGLTF_ANDROID_LOAD_FROM_ASSETS |
| 3053 | if (asset_manager) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 3054 | AAsset *asset = AAssetManager_open(asset_manager, filepath.c_str(), |
| 3055 | AASSET_MODE_STREAMING); |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 3056 | if (!asset) { |
| 3057 | if (err) { |
| 3058 | (*err) += "File open error : " + filepath + "\n"; |
| 3059 | } |
| 3060 | return false; |
| 3061 | } |
| 3062 | size_t size = AAsset_getLength(asset); |
Syoyo Fujita | 73c4cce | 2020-04-28 01:06:34 +0900 | [diff] [blame] | 3063 | if (size == 0) { |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 3064 | if (err) { |
| 3065 | (*err) += "Invalid file size : " + filepath + |
| 3066 | " (does the path point to a directory?)"; |
| 3067 | } |
Syoyo Fujita | 73c4cce | 2020-04-28 01:06:34 +0900 | [diff] [blame] | 3068 | return false; |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 3069 | } |
| 3070 | out->resize(size); |
| 3071 | AAsset_read(asset, reinterpret_cast<char *>(&out->at(0)), size); |
| 3072 | AAsset_close(asset); |
| 3073 | return true; |
| 3074 | } else { |
| 3075 | if (err) { |
| 3076 | (*err) += "No asset manager specified : " + filepath + "\n"; |
| 3077 | } |
| 3078 | return false; |
| 3079 | } |
| 3080 | #else |
Harokyang | 5cecef2 | 2019-10-30 15:16:46 +0800 | [diff] [blame] | 3081 | #ifdef _WIN32 |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 3082 | #if defined(__GLIBCXX__) // mingw |
| 3083 | int file_descriptor = |
| 3084 | _wopen(UTF8ToWchar(filepath).c_str(), _O_RDONLY | _O_BINARY); |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 3085 | __gnu_cxx::stdio_filebuf<char> wfile_buf(file_descriptor, std::ios_base::in); |
| 3086 | std::istream f(&wfile_buf); |
Syoyo Fujita | aba57bb | 2020-05-18 20:50:45 +0900 | [diff] [blame] | 3087 | #elif defined(_MSC_VER) || defined(_LIBCPP_VERSION) |
Rahul Sheth | 96a8b2c | 2020-07-10 14:27:46 -0400 | [diff] [blame] | 3088 | // For libcxx, assume _LIBCPP_HAS_OPEN_WITH_WCHAR is defined to accept |
| 3089 | // `wchar_t *` |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 3090 | std::ifstream f(UTF8ToWchar(filepath).c_str(), std::ifstream::binary); |
Syoyo Fujita | aba57bb | 2020-05-18 20:50:45 +0900 | [diff] [blame] | 3091 | #else |
| 3092 | // Unknown compiler/runtime |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 3093 | std::ifstream f(filepath.c_str(), std::ifstream::binary); |
| 3094 | #endif |
Harokyang | 5cecef2 | 2019-10-30 15:16:46 +0800 | [diff] [blame] | 3095 | #else |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3096 | std::ifstream f(filepath.c_str(), std::ifstream::binary); |
Harokyang | 5cecef2 | 2019-10-30 15:16:46 +0800 | [diff] [blame] | 3097 | #endif |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3098 | if (!f) { |
| 3099 | if (err) { |
| 3100 | (*err) += "File open error : " + filepath + "\n"; |
| 3101 | } |
| 3102 | return false; |
| 3103 | } |
| 3104 | |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3105 | // For directory(and pipe?), peek() will fail(Posix gnustl/libc++ only) |
David Siegel | 47208b2 | 2023-06-05 22:14:30 +0200 | [diff] [blame] | 3106 | f.peek(); |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3107 | if (!f) { |
| 3108 | if (err) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3109 | (*err) += |
| 3110 | "File read error. Maybe empty file or invalid file : " + filepath + |
| 3111 | "\n"; |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3112 | } |
| 3113 | return false; |
| 3114 | } |
| 3115 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3116 | f.seekg(0, f.end); |
jamesvert | f57d18a | 2024-03-26 11:06:16 +0000 | [diff] [blame] | 3117 | const auto sz = f.tellg(); |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3118 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3119 | // std::cout << "sz = " << sz << "\n"; |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3120 | f.seekg(0, f.beg); |
| 3121 | |
jamesvert | f57d18a | 2024-03-26 11:06:16 +0000 | [diff] [blame] | 3122 | if (sz < 0) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3123 | if (err) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 3124 | (*err) += "Invalid file size : " + filepath + |
| 3125 | " (does the path point to a directory?)"; |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3126 | } |
| 3127 | return false; |
jamesvert | e3f9a7d | 2024-03-26 11:32:43 +0000 | [diff] [blame] | 3128 | } else if (sz == std::streamoff(0)) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3129 | if (err) { |
| 3130 | (*err) += "File is empty : " + filepath + "\n"; |
| 3131 | } |
| 3132 | return false; |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3133 | } else if (sz >= (std::numeric_limits<std::streamoff>::max)()) { |
| 3134 | if (err) { |
| 3135 | (*err) += "Invalid file size : " + filepath + "\n"; |
| 3136 | } |
| 3137 | return false; |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3138 | } |
| 3139 | |
| 3140 | out->resize(sz); |
| 3141 | f.read(reinterpret_cast<char *>(&out->at(0)), |
| 3142 | static_cast<std::streamsize>(sz)); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3143 | |
| 3144 | return true; |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 3145 | #endif |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3146 | } |
| 3147 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 3148 | bool WriteWholeFile(std::string *err, const std::string &filepath, |
| 3149 | const std::vector<unsigned char> &contents, void *) { |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 3150 | #ifdef _WIN32 |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 3151 | #if defined(__GLIBCXX__) // mingw |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 3152 | int file_descriptor = _wopen(UTF8ToWchar(filepath).c_str(), |
TheDev | 22dfeab | 2024-03-25 18:21:20 +0300 | [diff] [blame] | 3153 | _O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY, _S_IWRITE); |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 3154 | __gnu_cxx::stdio_filebuf<char> wfile_buf( |
| 3155 | file_descriptor, std::ios_base::out | std::ios_base::binary); |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 3156 | std::ostream f(&wfile_buf); |
| 3157 | #elif defined(_MSC_VER) |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 3158 | std::ofstream f(UTF8ToWchar(filepath).c_str(), std::ofstream::binary); |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 3159 | #else // clang? |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 3160 | std::ofstream f(filepath.c_str(), std::ofstream::binary); |
| 3161 | #endif |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 3162 | #else |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3163 | std::ofstream f(filepath.c_str(), std::ofstream::binary); |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 3164 | #endif |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3165 | if (!f) { |
| 3166 | if (err) { |
| 3167 | (*err) += "File open error for writing : " + filepath + "\n"; |
| 3168 | } |
| 3169 | return false; |
| 3170 | } |
| 3171 | |
| 3172 | f.write(reinterpret_cast<const char *>(&contents.at(0)), |
| 3173 | static_cast<std::streamsize>(contents.size())); |
| 3174 | if (!f) { |
| 3175 | if (err) { |
| 3176 | (*err) += "File write error: " + filepath + "\n"; |
| 3177 | } |
| 3178 | return false; |
| 3179 | } |
| 3180 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3181 | return true; |
| 3182 | } |
| 3183 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 3184 | #endif // TINYGLTF_NO_FS |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3185 | |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3186 | static std::string MimeToExt(const std::string &mimeType) { |
| 3187 | if (mimeType == "image/jpeg") { |
| 3188 | return "jpg"; |
| 3189 | } else if (mimeType == "image/png") { |
| 3190 | return "png"; |
| 3191 | } else if (mimeType == "image/bmp") { |
| 3192 | return "bmp"; |
| 3193 | } else if (mimeType == "image/gif") { |
| 3194 | return "gif"; |
| 3195 | } |
| 3196 | |
| 3197 | return ""; |
| 3198 | } |
| 3199 | |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 3200 | static bool UpdateImageObject(const Image &image, std::string &baseDir, |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 3201 | int index, bool embedImages, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 3202 | const URICallbacks *uri_cb, |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 3203 | WriteImageDataFunction *WriteImageData, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 3204 | void *user_data, std::string *out_uri) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3205 | std::string filename; |
| 3206 | std::string ext; |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 3207 | // If image has uri, use it as a filename |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3208 | if (image.uri.size()) { |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 3209 | std::string decoded_uri; |
| 3210 | if (!uri_cb->decode(image.uri, &decoded_uri, uri_cb->user_data)) { |
| 3211 | // A decode failure results in a failure to write the gltf. |
| 3212 | return false; |
| 3213 | } |
| 3214 | filename = GetBaseFilename(decoded_uri); |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3215 | ext = GetFilePathExtension(filename); |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 3216 | } else if (image.bufferView != -1) { |
| 3217 | // If there's no URI and the data exists in a buffer, |
| 3218 | // don't change properties or write images |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3219 | } else if (image.name.size()) { |
| 3220 | ext = MimeToExt(image.mimeType); |
| 3221 | // Otherwise use name as filename |
| 3222 | filename = image.name + "." + ext; |
| 3223 | } else { |
| 3224 | ext = MimeToExt(image.mimeType); |
| 3225 | // Fallback to index of image as filename |
| 3226 | filename = std::to_string(index) + "." + ext; |
| 3227 | } |
| 3228 | |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 3229 | // If callback is set and image data exists, modify image data object. If |
| 3230 | // image data does not exist, this is not considered a failure and the |
| 3231 | // original uri should be maintained. |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 3232 | bool imageWritten = false; |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 3233 | if (*WriteImageData != nullptr && !filename.empty() && !image.image.empty()) { |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 3234 | imageWritten = (*WriteImageData)(&baseDir, &filename, &image, embedImages, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 3235 | uri_cb, out_uri, user_data); |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 3236 | if (!imageWritten) { |
| 3237 | return false; |
| 3238 | } |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 3239 | } |
| 3240 | |
| 3241 | // Use the original uri if the image was not written. |
| 3242 | if (!imageWritten) { |
| 3243 | *out_uri = image.uri; |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3244 | } |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 3245 | |
| 3246 | return true; |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3247 | } |
| 3248 | |
Selmar Kok | 0d0e97e | 2018-08-22 14:01:57 +0200 | [diff] [blame] | 3249 | bool IsDataURI(const std::string &in) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3250 | std::string header = "data:application/octet-stream;base64,"; |
| 3251 | if (in.find(header) == 0) { |
| 3252 | return true; |
| 3253 | } |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3254 | |
Omar C. F | f8a8d9c | 2018-02-10 09:09:41 +0200 | [diff] [blame] | 3255 | header = "data:image/jpeg;base64,"; |
| 3256 | if (in.find(header) == 0) { |
| 3257 | return true; |
| 3258 | } |
Squareys | 4337463 | 2018-03-13 22:20:48 +0100 | [diff] [blame] | 3259 | |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3260 | header = "data:image/png;base64,"; |
| 3261 | if (in.find(header) == 0) { |
| 3262 | return true; |
| 3263 | } |
Squareys | 4337463 | 2018-03-13 22:20:48 +0100 | [diff] [blame] | 3264 | |
Omar C. F | f8a8d9c | 2018-02-10 09:09:41 +0200 | [diff] [blame] | 3265 | header = "data:image/bmp;base64,"; |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 3266 | if (in.find(header) == 0) { |
Omar C. F | f8a8d9c | 2018-02-10 09:09:41 +0200 | [diff] [blame] | 3267 | return true; |
| 3268 | } |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3269 | |
Omar C. F | f8a8d9c | 2018-02-10 09:09:41 +0200 | [diff] [blame] | 3270 | header = "data:image/gif;base64,"; |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 3271 | if (in.find(header) == 0) { |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3272 | return true; |
| 3273 | } |
| 3274 | |
Luke San Antonio | caa24b0 | 2016-06-15 02:23:09 -0400 | [diff] [blame] | 3275 | header = "data:text/plain;base64,"; |
| 3276 | if (in.find(header) == 0) { |
| 3277 | return true; |
| 3278 | } |
| 3279 | |
Syoyo Fujita | 20244e1 | 2018-03-15 11:01:05 -0500 | [diff] [blame] | 3280 | header = "data:application/gltf-buffer;base64,"; |
| 3281 | if (in.find(header) == 0) { |
| 3282 | return true; |
| 3283 | } |
| 3284 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3285 | return false; |
| 3286 | } |
| 3287 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 3288 | bool DecodeDataURI(std::vector<unsigned char> *out, std::string &mime_type, |
| 3289 | const std::string &in, size_t reqBytes, bool checkSize) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3290 | std::string header = "data:application/octet-stream;base64,"; |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3291 | std::string data; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3292 | if (in.find(header) == 0) { |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 3293 | data = base64_decode(in.substr(header.size())); // cut mime string. |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3294 | } |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3295 | |
| 3296 | if (data.empty()) { |
| 3297 | header = "data:image/jpeg;base64,"; |
| 3298 | if (in.find(header) == 0) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3299 | mime_type = "image/jpeg"; |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 3300 | data = base64_decode(in.substr(header.size())); // cut mime string. |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3301 | } |
| 3302 | } |
| 3303 | |
| 3304 | if (data.empty()) { |
| 3305 | header = "data:image/png;base64,"; |
| 3306 | if (in.find(header) == 0) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3307 | mime_type = "image/png"; |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 3308 | data = base64_decode(in.substr(header.size())); // cut mime string. |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3309 | } |
| 3310 | } |
Squareys | 4337463 | 2018-03-13 22:20:48 +0100 | [diff] [blame] | 3311 | |
Omar C. F | f8a8d9c | 2018-02-10 09:09:41 +0200 | [diff] [blame] | 3312 | if (data.empty()) { |
| 3313 | header = "data:image/bmp;base64,"; |
| 3314 | if (in.find(header) == 0) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3315 | mime_type = "image/bmp"; |
Omar C. F | f8a8d9c | 2018-02-10 09:09:41 +0200 | [diff] [blame] | 3316 | data = base64_decode(in.substr(header.size())); // cut mime string. |
| 3317 | } |
| 3318 | } |
| 3319 | |
| 3320 | if (data.empty()) { |
| 3321 | header = "data:image/gif;base64,"; |
| 3322 | if (in.find(header) == 0) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3323 | mime_type = "image/gif"; |
Omar C. F | f8a8d9c | 2018-02-10 09:09:41 +0200 | [diff] [blame] | 3324 | data = base64_decode(in.substr(header.size())); // cut mime string. |
| 3325 | } |
| 3326 | } |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3327 | |
| 3328 | if (data.empty()) { |
Luke San Antonio | caa24b0 | 2016-06-15 02:23:09 -0400 | [diff] [blame] | 3329 | header = "data:text/plain;base64,"; |
| 3330 | if (in.find(header) == 0) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3331 | mime_type = "text/plain"; |
Luke San Antonio | caa24b0 | 2016-06-15 02:23:09 -0400 | [diff] [blame] | 3332 | data = base64_decode(in.substr(header.size())); |
| 3333 | } |
| 3334 | } |
| 3335 | |
| 3336 | if (data.empty()) { |
Syoyo Fujita | 20244e1 | 2018-03-15 11:01:05 -0500 | [diff] [blame] | 3337 | header = "data:application/gltf-buffer;base64,"; |
| 3338 | if (in.find(header) == 0) { |
| 3339 | data = base64_decode(in.substr(header.size())); |
| 3340 | } |
| 3341 | } |
| 3342 | |
Syoyo Fujita | a11f6e1 | 2019-12-19 14:16:48 +0900 | [diff] [blame] | 3343 | // TODO(syoyo): Allow empty buffer? #229 |
Syoyo Fujita | 20244e1 | 2018-03-15 11:01:05 -0500 | [diff] [blame] | 3344 | if (data.empty()) { |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3345 | return false; |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 3346 | } |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3347 | |
| 3348 | if (checkSize) { |
| 3349 | if (data.size() != reqBytes) { |
| 3350 | return false; |
| 3351 | } |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3352 | out->resize(reqBytes); |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3353 | } else { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3354 | out->resize(data.size()); |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3355 | } |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3356 | std::copy(data.begin(), data.end(), out->begin()); |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3357 | return true; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3358 | } |
| 3359 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3360 | namespace detail { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3361 | bool GetInt(const detail::json &o, int &val) { |
jrkoonce | 5cecc41 | 2019-08-29 11:45:04 -0500 | [diff] [blame] | 3362 | #ifdef TINYGLTF_USE_RAPIDJSON |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3363 | if (!o.IsDouble()) { |
| 3364 | if (o.IsInt()) { |
| 3365 | val = o.GetInt(); |
| 3366 | return true; |
| 3367 | } else if (o.IsUint()) { |
| 3368 | val = static_cast<int>(o.GetUint()); |
| 3369 | return true; |
| 3370 | } else if (o.IsInt64()) { |
| 3371 | val = static_cast<int>(o.GetInt64()); |
| 3372 | return true; |
| 3373 | } else if (o.IsUint64()) { |
| 3374 | val = static_cast<int>(o.GetUint64()); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3375 | return true; |
| 3376 | } |
jrkoonce | 5cecc41 | 2019-08-29 11:45:04 -0500 | [diff] [blame] | 3377 | } |
| 3378 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3379 | return false; |
jrkoonce | 5cecc41 | 2019-08-29 11:45:04 -0500 | [diff] [blame] | 3380 | #else |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3381 | auto type = o.type(); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3382 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3383 | if ((type == detail::json::value_t::number_integer) || |
| 3384 | (type == detail::json::value_t::number_unsigned)) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3385 | val = static_cast<int>(o.get<int64_t>()); |
| 3386 | return true; |
jrkoonce | 5cecc41 | 2019-08-29 11:45:04 -0500 | [diff] [blame] | 3387 | } |
| 3388 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3389 | return false; |
jrkoonce | 5cecc41 | 2019-08-29 11:45:04 -0500 | [diff] [blame] | 3390 | #endif |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3391 | } |
| 3392 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3393 | #ifdef TINYGLTF_USE_RAPIDJSON |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3394 | bool GetDouble(const detail::json &o, double &val) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3395 | if (o.IsDouble()) { |
| 3396 | val = o.GetDouble(); |
| 3397 | return true; |
| 3398 | } |
| 3399 | |
| 3400 | return false; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3401 | } |
Syoyo Fujita | 1100f0f | 2019-10-30 15:16:34 +0900 | [diff] [blame] | 3402 | #endif |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3403 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3404 | bool GetNumber(const detail::json &o, double &val) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3405 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3406 | if (o.IsNumber()) { |
| 3407 | val = o.GetDouble(); |
| 3408 | return true; |
| 3409 | } |
| 3410 | |
| 3411 | return false; |
| 3412 | #else |
| 3413 | if (o.is_number()) { |
| 3414 | val = o.get<double>(); |
| 3415 | return true; |
| 3416 | } |
| 3417 | |
| 3418 | return false; |
| 3419 | #endif |
| 3420 | } |
| 3421 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3422 | bool GetString(const detail::json &o, std::string &val) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3423 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3424 | if (o.IsString()) { |
| 3425 | val = o.GetString(); |
| 3426 | return true; |
| 3427 | } |
| 3428 | |
| 3429 | return false; |
| 3430 | #else |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3431 | if (o.type() == detail::json::value_t::string) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3432 | val = o.get<std::string>(); |
| 3433 | return true; |
| 3434 | } |
| 3435 | |
| 3436 | return false; |
| 3437 | #endif |
| 3438 | } |
| 3439 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3440 | bool IsArray(const detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3441 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3442 | return o.IsArray(); |
| 3443 | #else |
| 3444 | return o.is_array(); |
| 3445 | #endif |
| 3446 | } |
| 3447 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3448 | detail::json_const_array_iterator ArrayBegin(const detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3449 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3450 | return o.Begin(); |
| 3451 | #else |
| 3452 | return o.begin(); |
| 3453 | #endif |
| 3454 | } |
| 3455 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3456 | detail::json_const_array_iterator ArrayEnd(const detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3457 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3458 | return o.End(); |
| 3459 | #else |
| 3460 | return o.end(); |
| 3461 | #endif |
| 3462 | } |
| 3463 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3464 | bool IsObject(const detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3465 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3466 | return o.IsObject(); |
| 3467 | #else |
| 3468 | return o.is_object(); |
| 3469 | #endif |
| 3470 | } |
| 3471 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3472 | detail::json_const_iterator ObjectBegin(const detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3473 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3474 | return o.MemberBegin(); |
| 3475 | #else |
| 3476 | return o.begin(); |
| 3477 | #endif |
| 3478 | } |
| 3479 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3480 | detail::json_const_iterator ObjectEnd(const detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3481 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3482 | return o.MemberEnd(); |
| 3483 | #else |
| 3484 | return o.end(); |
| 3485 | #endif |
| 3486 | } |
| 3487 | |
Rahul Sheth | 01d5438 | 2020-07-10 14:27:37 -0400 | [diff] [blame] | 3488 | // Making this a const char* results in a pointer to a temporary when |
| 3489 | // TINYGLTF_USE_RAPIDJSON is off. |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3490 | std::string GetKey(detail::json_const_iterator &it) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3491 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3492 | return it->name.GetString(); |
| 3493 | #else |
| 3494 | return it.key().c_str(); |
| 3495 | #endif |
| 3496 | } |
| 3497 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3498 | bool FindMember(const detail::json &o, const char *member, |
| 3499 | detail::json_const_iterator &it) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3500 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3501 | if (!o.IsObject()) { |
| 3502 | return false; |
| 3503 | } |
| 3504 | it = o.FindMember(member); |
| 3505 | return it != o.MemberEnd(); |
| 3506 | #else |
| 3507 | it = o.find(member); |
| 3508 | return it != o.end(); |
| 3509 | #endif |
| 3510 | } |
| 3511 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3512 | bool FindMember(detail::json &o, const char *member, |
| 3513 | detail::json_iterator &it) { |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 3514 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3515 | if (!o.IsObject()) { |
| 3516 | return false; |
| 3517 | } |
| 3518 | it = o.FindMember(member); |
| 3519 | return it != o.MemberEnd(); |
| 3520 | #else |
| 3521 | it = o.find(member); |
| 3522 | return it != o.end(); |
| 3523 | #endif |
| 3524 | } |
| 3525 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3526 | void Erase(detail::json &o, detail::json_iterator &it) { |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 3527 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3528 | o.EraseMember(it); |
| 3529 | #else |
| 3530 | o.erase(it); |
| 3531 | #endif |
| 3532 | } |
| 3533 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3534 | bool IsEmpty(const detail::json &o) { |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 3535 | #ifdef TINYGLTF_USE_RAPIDJSON |
David Siegel | a1a34cb | 2023-06-07 15:30:02 +0200 | [diff] [blame] | 3536 | return o.ObjectEmpty(); |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 3537 | #else |
| 3538 | return o.empty(); |
| 3539 | #endif |
| 3540 | } |
| 3541 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3542 | const detail::json &GetValue(detail::json_const_iterator &it) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3543 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3544 | return it->value; |
| 3545 | #else |
| 3546 | return it.value(); |
| 3547 | #endif |
| 3548 | } |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 3549 | |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 3550 | detail::json &GetValue(detail::json_iterator &it) { |
| 3551 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3552 | return it->value; |
| 3553 | #else |
| 3554 | return it.value(); |
| 3555 | #endif |
| 3556 | } |
| 3557 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3558 | std::string JsonToString(const detail::json &o, int spacing = -1) { |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 3559 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3560 | using namespace rapidjson; |
| 3561 | StringBuffer buffer; |
| 3562 | if (spacing == -1) { |
| 3563 | Writer<StringBuffer> writer(buffer); |
Syoyo Fujita | 544969b | 2022-07-13 19:03:33 +0900 | [diff] [blame] | 3564 | // TODO: Better error handling. |
| 3565 | // https://github.com/syoyo/tinygltf/issues/332 |
| 3566 | if (!o.Accept(writer)) { |
| 3567 | return "tiny_gltf::JsonToString() failed rapidjson conversion"; |
| 3568 | } |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 3569 | } else { |
| 3570 | PrettyWriter<StringBuffer> writer(buffer); |
| 3571 | writer.SetIndent(' ', uint32_t(spacing)); |
Syoyo Fujita | 544969b | 2022-07-13 19:03:33 +0900 | [diff] [blame] | 3572 | if (!o.Accept(writer)) { |
| 3573 | return "tiny_gltf::JsonToString() failed rapidjson conversion"; |
| 3574 | } |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 3575 | } |
| 3576 | return buffer.GetString(); |
| 3577 | #else |
| 3578 | return o.dump(spacing); |
| 3579 | #endif |
| 3580 | } |
| 3581 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3582 | } // namespace detail |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3583 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3584 | static bool ParseJsonAsValue(Value *ret, const detail::json &o) { |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3585 | Value val{}; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3586 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3587 | using rapidjson::Type; |
| 3588 | switch (o.GetType()) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3589 | case Type::kObjectType: { |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3590 | Value::Object value_object; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3591 | for (auto it = o.MemberBegin(); it != o.MemberEnd(); ++it) { |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3592 | Value entry; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3593 | ParseJsonAsValue(&entry, it->value); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3594 | if (entry.Type() != NULL_TYPE) |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3595 | value_object.emplace(detail::GetKey(it), std::move(entry)); |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3596 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3597 | if (value_object.size() > 0) val = Value(std::move(value_object)); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 3598 | } break; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3599 | case Type::kArrayType: { |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3600 | Value::Array value_array; |
jrkoonce | 5cecc41 | 2019-08-29 11:45:04 -0500 | [diff] [blame] | 3601 | value_array.reserve(o.Size()); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3602 | for (auto it = o.Begin(); it != o.End(); ++it) { |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3603 | Value entry; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3604 | ParseJsonAsValue(&entry, *it); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3605 | if (entry.Type() != NULL_TYPE) |
| 3606 | value_array.emplace_back(std::move(entry)); |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3607 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3608 | if (value_array.size() > 0) val = Value(std::move(value_array)); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 3609 | } break; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3610 | case Type::kStringType: |
| 3611 | val = Value(std::string(o.GetString())); |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3612 | break; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3613 | case Type::kFalseType: |
| 3614 | case Type::kTrueType: |
| 3615 | val = Value(o.GetBool()); |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3616 | break; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3617 | case Type::kNumberType: |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3618 | if (!o.IsDouble()) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3619 | int i = 0; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3620 | detail::GetInt(o, i); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3621 | val = Value(i); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3622 | } else { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3623 | double d = 0.0; |
David | 3831802 | 2023-02-15 23:21:09 -0600 | [diff] [blame] | 3624 | detail::GetDouble(o, d); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3625 | val = Value(d); |
| 3626 | } |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3627 | break; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3628 | case Type::kNullType: |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3629 | break; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 3630 | // all types are covered, so no `case default` |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 3631 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3632 | #else |
| 3633 | switch (o.type()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3634 | case detail::json::value_t::object: { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3635 | Value::Object value_object; |
| 3636 | for (auto it = o.begin(); it != o.end(); it++) { |
| 3637 | Value entry; |
| 3638 | ParseJsonAsValue(&entry, it.value()); |
| 3639 | if (entry.Type() != NULL_TYPE) |
| 3640 | value_object.emplace(it.key(), std::move(entry)); |
| 3641 | } |
| 3642 | if (value_object.size() > 0) val = Value(std::move(value_object)); |
| 3643 | } break; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3644 | case detail::json::value_t::array: { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3645 | Value::Array value_array; |
| 3646 | value_array.reserve(o.size()); |
| 3647 | for (auto it = o.begin(); it != o.end(); it++) { |
| 3648 | Value entry; |
| 3649 | ParseJsonAsValue(&entry, it.value()); |
| 3650 | if (entry.Type() != NULL_TYPE) |
| 3651 | value_array.emplace_back(std::move(entry)); |
| 3652 | } |
| 3653 | if (value_array.size() > 0) val = Value(std::move(value_array)); |
| 3654 | } break; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3655 | case detail::json::value_t::string: |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3656 | val = Value(o.get<std::string>()); |
| 3657 | break; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3658 | case detail::json::value_t::boolean: |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3659 | val = Value(o.get<bool>()); |
| 3660 | break; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3661 | case detail::json::value_t::number_integer: |
| 3662 | case detail::json::value_t::number_unsigned: |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3663 | val = Value(static_cast<int>(o.get<int64_t>())); |
| 3664 | break; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3665 | case detail::json::value_t::number_float: |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3666 | val = Value(o.get<double>()); |
| 3667 | break; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3668 | case detail::json::value_t::null: |
| 3669 | case detail::json::value_t::discarded: |
| 3670 | case detail::json::value_t::binary: |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3671 | // default: |
| 3672 | break; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3673 | } |
| 3674 | #endif |
Marcin Kacprzak | f4f5c3c | 2022-09-02 16:15:54 +0200 | [diff] [blame] | 3675 | const bool isNotNull = val.Type() != NULL_TYPE; |
| 3676 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3677 | if (ret) *ret = std::move(val); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 3678 | |
Marcin Kacprzak | f4f5c3c | 2022-09-02 16:15:54 +0200 | [diff] [blame] | 3679 | return isNotNull; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 3680 | } |
| 3681 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3682 | static bool ParseExtrasProperty(Value *ret, const detail::json &o) { |
| 3683 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3684 | if (!detail::FindMember(o, "extras", it)) { |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 3685 | return false; |
| 3686 | } |
| 3687 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3688 | return ParseJsonAsValue(ret, detail::GetValue(it)); |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 3689 | } |
| 3690 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3691 | static bool ParseBooleanProperty(bool *ret, std::string *err, |
| 3692 | const detail::json &o, |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 3693 | const std::string &property, |
| 3694 | const bool required, |
| 3695 | const std::string &parent_node = "") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3696 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3697 | if (!detail::FindMember(o, property.c_str(), it)) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3698 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3699 | if (err) { |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 3700 | (*err) += "'" + property + "' property is missing"; |
| 3701 | if (!parent_node.empty()) { |
| 3702 | (*err) += " in " + parent_node; |
| 3703 | } |
| 3704 | (*err) += ".\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3705 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3706 | } |
| 3707 | return false; |
| 3708 | } |
| 3709 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3710 | auto &value = detail::GetValue(it); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3711 | |
| 3712 | bool isBoolean; |
jrkoonce | 0d2b6ef | 2019-09-04 13:46:45 -0500 | [diff] [blame] | 3713 | bool boolValue = false; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3714 | #ifdef TINYGLTF_USE_RAPIDJSON |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3715 | isBoolean = value.IsBool(); |
| 3716 | if (isBoolean) { |
| 3717 | boolValue = value.GetBool(); |
| 3718 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3719 | #else |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3720 | isBoolean = value.is_boolean(); |
| 3721 | if (isBoolean) { |
| 3722 | boolValue = value.get<bool>(); |
| 3723 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3724 | #endif |
| 3725 | if (!isBoolean) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3726 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3727 | if (err) { |
| 3728 | (*err) += "'" + property + "' property is not a bool type.\n"; |
| 3729 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3730 | } |
| 3731 | return false; |
| 3732 | } |
| 3733 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3734 | if (ret) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3735 | (*ret) = boolValue; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3736 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3737 | |
| 3738 | return true; |
| 3739 | } |
| 3740 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3741 | static bool ParseIntegerProperty(int *ret, std::string *err, |
| 3742 | const detail::json &o, |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3743 | const std::string &property, |
| 3744 | const bool required, |
| 3745 | const std::string &parent_node = "") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3746 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3747 | if (!detail::FindMember(o, property.c_str(), it)) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3748 | if (required) { |
| 3749 | if (err) { |
| 3750 | (*err) += "'" + property + "' property is missing"; |
| 3751 | if (!parent_node.empty()) { |
| 3752 | (*err) += " in " + parent_node; |
| 3753 | } |
| 3754 | (*err) += ".\n"; |
| 3755 | } |
| 3756 | } |
| 3757 | return false; |
| 3758 | } |
| 3759 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3760 | int intValue; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3761 | bool isInt = detail::GetInt(detail::GetValue(it), intValue); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3762 | if (!isInt) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3763 | if (required) { |
| 3764 | if (err) { |
| 3765 | (*err) += "'" + property + "' property is not an integer type.\n"; |
| 3766 | } |
| 3767 | } |
| 3768 | return false; |
| 3769 | } |
| 3770 | |
| 3771 | if (ret) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3772 | (*ret) = intValue; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3773 | } |
| 3774 | |
| 3775 | return true; |
| 3776 | } |
| 3777 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3778 | static bool ParseUnsignedProperty(size_t *ret, std::string *err, |
| 3779 | const detail::json &o, |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3780 | const std::string &property, |
| 3781 | const bool required, |
| 3782 | const std::string &parent_node = "") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3783 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3784 | if (!detail::FindMember(o, property.c_str(), it)) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3785 | if (required) { |
| 3786 | if (err) { |
| 3787 | (*err) += "'" + property + "' property is missing"; |
| 3788 | if (!parent_node.empty()) { |
| 3789 | (*err) += " in " + parent_node; |
| 3790 | } |
| 3791 | (*err) += ".\n"; |
| 3792 | } |
| 3793 | } |
| 3794 | return false; |
| 3795 | } |
| 3796 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3797 | auto &value = detail::GetValue(it); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3798 | |
jrkoonce | 0d2b6ef | 2019-09-04 13:46:45 -0500 | [diff] [blame] | 3799 | size_t uValue = 0; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3800 | bool isUValue; |
| 3801 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3802 | isUValue = false; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3803 | if (value.IsUint()) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3804 | uValue = value.GetUint(); |
| 3805 | isUValue = true; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3806 | } else if (value.IsUint64()) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3807 | uValue = value.GetUint64(); |
| 3808 | isUValue = true; |
| 3809 | } |
| 3810 | #else |
| 3811 | isUValue = value.is_number_unsigned(); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3812 | if (isUValue) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3813 | uValue = value.get<size_t>(); |
| 3814 | } |
| 3815 | #endif |
| 3816 | if (!isUValue) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3817 | if (required) { |
| 3818 | if (err) { |
| 3819 | (*err) += "'" + property + "' property is not a positive integer.\n"; |
| 3820 | } |
| 3821 | } |
| 3822 | return false; |
| 3823 | } |
| 3824 | |
| 3825 | if (ret) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3826 | (*ret) = uValue; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3827 | } |
| 3828 | |
| 3829 | return true; |
| 3830 | } |
| 3831 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3832 | static bool ParseNumberProperty(double *ret, std::string *err, |
| 3833 | const detail::json &o, |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 3834 | const std::string &property, |
| 3835 | const bool required, |
| 3836 | const std::string &parent_node = "") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3837 | detail::json_const_iterator it; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3838 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3839 | if (!detail::FindMember(o, property.c_str(), it)) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3840 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3841 | if (err) { |
Syoyo Fujita | 90e1ed2 | 2017-05-30 00:47:37 +0900 | [diff] [blame] | 3842 | (*err) += "'" + property + "' property is missing"; |
| 3843 | if (!parent_node.empty()) { |
| 3844 | (*err) += " in " + parent_node; |
| 3845 | } |
| 3846 | (*err) += ".\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3847 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3848 | } |
| 3849 | return false; |
| 3850 | } |
| 3851 | |
Syoyo Fujita | 1100f0f | 2019-10-30 15:16:34 +0900 | [diff] [blame] | 3852 | double numberValue; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3853 | bool isNumber = detail::GetNumber(detail::GetValue(it), numberValue); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3854 | |
Syoyo Fujita | 1100f0f | 2019-10-30 15:16:34 +0900 | [diff] [blame] | 3855 | if (!isNumber) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3856 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3857 | if (err) { |
| 3858 | (*err) += "'" + property + "' property is not a number type.\n"; |
| 3859 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3860 | } |
| 3861 | return false; |
| 3862 | } |
| 3863 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3864 | if (ret) { |
Syoyo Fujita | 1100f0f | 2019-10-30 15:16:34 +0900 | [diff] [blame] | 3865 | (*ret) = numberValue; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3866 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3867 | |
| 3868 | return true; |
| 3869 | } |
| 3870 | |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 3871 | static bool ParseNumberArrayProperty(std::vector<double> *ret, std::string *err, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3872 | const detail::json &o, |
| 3873 | const std::string &property, bool required, |
Syoyo Fujita | 90e1ed2 | 2017-05-30 00:47:37 +0900 | [diff] [blame] | 3874 | const std::string &parent_node = "") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3875 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3876 | if (!detail::FindMember(o, property.c_str(), it)) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3877 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3878 | if (err) { |
Syoyo Fujita | 90e1ed2 | 2017-05-30 00:47:37 +0900 | [diff] [blame] | 3879 | (*err) += "'" + property + "' property is missing"; |
| 3880 | if (!parent_node.empty()) { |
| 3881 | (*err) += " in " + parent_node; |
| 3882 | } |
| 3883 | (*err) += ".\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3884 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3885 | } |
| 3886 | return false; |
| 3887 | } |
| 3888 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3889 | if (!detail::IsArray(detail::GetValue(it))) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3890 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3891 | if (err) { |
Syoyo Fujita | 90e1ed2 | 2017-05-30 00:47:37 +0900 | [diff] [blame] | 3892 | (*err) += "'" + property + "' property is not an array"; |
| 3893 | if (!parent_node.empty()) { |
| 3894 | (*err) += " in " + parent_node; |
| 3895 | } |
| 3896 | (*err) += ".\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3897 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3898 | } |
| 3899 | return false; |
| 3900 | } |
| 3901 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3902 | ret->clear(); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3903 | auto end = detail::ArrayEnd(detail::GetValue(it)); |
| 3904 | for (auto i = detail::ArrayBegin(detail::GetValue(it)); i != end; ++i) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3905 | double numberValue; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3906 | const bool isNumber = detail::GetNumber(*i, numberValue); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3907 | if (!isNumber) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3908 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3909 | if (err) { |
| 3910 | (*err) += "'" + property + "' property is not a number.\n"; |
Syoyo Fujita | 90e1ed2 | 2017-05-30 00:47:37 +0900 | [diff] [blame] | 3911 | if (!parent_node.empty()) { |
| 3912 | (*err) += " in " + parent_node; |
| 3913 | } |
| 3914 | (*err) += ".\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3915 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3916 | } |
| 3917 | return false; |
| 3918 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3919 | ret->push_back(numberValue); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3920 | } |
| 3921 | |
| 3922 | return true; |
| 3923 | } |
| 3924 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3925 | static bool ParseIntegerArrayProperty(std::vector<int> *ret, std::string *err, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3926 | const detail::json &o, |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3927 | const std::string &property, |
| 3928 | bool required, |
| 3929 | const std::string &parent_node = "") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3930 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3931 | if (!detail::FindMember(o, property.c_str(), it)) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3932 | if (required) { |
| 3933 | if (err) { |
| 3934 | (*err) += "'" + property + "' property is missing"; |
| 3935 | if (!parent_node.empty()) { |
| 3936 | (*err) += " in " + parent_node; |
| 3937 | } |
| 3938 | (*err) += ".\n"; |
| 3939 | } |
| 3940 | } |
| 3941 | return false; |
| 3942 | } |
| 3943 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3944 | if (!detail::IsArray(detail::GetValue(it))) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3945 | if (required) { |
| 3946 | if (err) { |
| 3947 | (*err) += "'" + property + "' property is not an array"; |
| 3948 | if (!parent_node.empty()) { |
| 3949 | (*err) += " in " + parent_node; |
| 3950 | } |
| 3951 | (*err) += ".\n"; |
| 3952 | } |
| 3953 | } |
| 3954 | return false; |
| 3955 | } |
| 3956 | |
| 3957 | ret->clear(); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3958 | auto end = detail::ArrayEnd(detail::GetValue(it)); |
| 3959 | for (auto i = detail::ArrayBegin(detail::GetValue(it)); i != end; ++i) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3960 | int numberValue; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3961 | bool isNumber = detail::GetInt(*i, numberValue); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3962 | if (!isNumber) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3963 | if (required) { |
| 3964 | if (err) { |
| 3965 | (*err) += "'" + property + "' property is not an integer type.\n"; |
| 3966 | if (!parent_node.empty()) { |
| 3967 | (*err) += " in " + parent_node; |
| 3968 | } |
| 3969 | (*err) += ".\n"; |
| 3970 | } |
| 3971 | } |
| 3972 | return false; |
| 3973 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3974 | ret->push_back(numberValue); |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3975 | } |
| 3976 | |
| 3977 | return true; |
| 3978 | } |
| 3979 | |
Syoyo Fujita | 0614eb8 | 2016-10-14 18:50:14 +0900 | [diff] [blame] | 3980 | static bool ParseStringProperty( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3981 | std::string *ret, std::string *err, const detail::json &o, |
Syoyo Fujita | 0614eb8 | 2016-10-14 18:50:14 +0900 | [diff] [blame] | 3982 | const std::string &property, bool required, |
| 3983 | const std::string &parent_node = std::string()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3984 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3985 | if (!detail::FindMember(o, property.c_str(), it)) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3986 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3987 | if (err) { |
Syoyo Fujita | 0614eb8 | 2016-10-14 18:50:14 +0900 | [diff] [blame] | 3988 | (*err) += "'" + property + "' property is missing"; |
| 3989 | if (parent_node.empty()) { |
| 3990 | (*err) += ".\n"; |
| 3991 | } else { |
| 3992 | (*err) += " in `" + parent_node + "'.\n"; |
| 3993 | } |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3994 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3995 | } |
| 3996 | return false; |
| 3997 | } |
| 3998 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3999 | std::string strValue; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4000 | if (!detail::GetString(detail::GetValue(it), strValue)) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4001 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4002 | if (err) { |
| 4003 | (*err) += "'" + property + "' property is not a string type.\n"; |
| 4004 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4005 | } |
| 4006 | return false; |
| 4007 | } |
| 4008 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4009 | if (ret) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4010 | (*ret) = std::move(strValue); |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4011 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4012 | |
| 4013 | return true; |
| 4014 | } |
| 4015 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4016 | static bool ParseStringIntegerProperty(std::map<std::string, int> *ret, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4017 | std::string *err, const detail::json &o, |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4018 | const std::string &property, |
| 4019 | bool required, |
| 4020 | const std::string &parent = "") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4021 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4022 | if (!detail::FindMember(o, property.c_str(), it)) { |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4023 | if (required) { |
| 4024 | if (err) { |
Syoyo Fujita | 57c1018 | 2017-10-19 18:48:26 +0900 | [diff] [blame] | 4025 | if (!parent.empty()) { |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 4026 | (*err) += |
| 4027 | "'" + property + "' property is missing in " + parent + ".\n"; |
Syoyo Fujita | 57c1018 | 2017-10-19 18:48:26 +0900 | [diff] [blame] | 4028 | } else { |
| 4029 | (*err) += "'" + property + "' property is missing.\n"; |
| 4030 | } |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4031 | } |
| 4032 | } |
| 4033 | return false; |
| 4034 | } |
| 4035 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4036 | const detail::json &dict = detail::GetValue(it); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4037 | |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4038 | // Make sure we are dealing with an object / dictionary. |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4039 | if (!detail::IsObject(dict)) { |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4040 | if (required) { |
| 4041 | if (err) { |
| 4042 | (*err) += "'" + property + "' property is not an object.\n"; |
| 4043 | } |
| 4044 | } |
| 4045 | return false; |
| 4046 | } |
| 4047 | |
| 4048 | ret->clear(); |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4049 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4050 | detail::json_const_iterator dictIt(detail::ObjectBegin(dict)); |
| 4051 | detail::json_const_iterator dictItEnd(detail::ObjectEnd(dict)); |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4052 | |
Syoyo Fujita | 3cdfb3b | 2016-06-15 20:58:42 +0900 | [diff] [blame] | 4053 | for (; dictIt != dictItEnd; ++dictIt) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4054 | int intVal; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4055 | if (!detail::GetInt(detail::GetValue(dictIt), intVal)) { |
Syoyo Fujita | 3cdfb3b | 2016-06-15 20:58:42 +0900 | [diff] [blame] | 4056 | if (required) { |
| 4057 | if (err) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4058 | (*err) += "'" + property + "' value is not an integer type.\n"; |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4059 | } |
| 4060 | } |
| 4061 | return false; |
| 4062 | } |
| 4063 | |
| 4064 | // Insert into the list. |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4065 | (*ret)[detail::GetKey(dictIt)] = intVal; |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4066 | } |
| 4067 | return true; |
| 4068 | } |
| 4069 | |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 4070 | static bool ParseJSONProperty(std::map<std::string, double> *ret, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4071 | std::string *err, const detail::json &o, |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 4072 | const std::string &property, bool required) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4073 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4074 | if (!detail::FindMember(o, property.c_str(), it)) { |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 4075 | if (required) { |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 4076 | if (err) { |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 4077 | (*err) += "'" + property + "' property is missing. \n'"; |
| 4078 | } |
| 4079 | } |
| 4080 | return false; |
| 4081 | } |
| 4082 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4083 | const detail::json &obj = detail::GetValue(it); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4084 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4085 | if (!detail::IsObject(obj)) { |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 4086 | if (required) { |
| 4087 | if (err) { |
| 4088 | (*err) += "'" + property + "' property is not a JSON object.\n"; |
| 4089 | } |
| 4090 | } |
| 4091 | return false; |
| 4092 | } |
| 4093 | |
| 4094 | ret->clear(); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 4095 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4096 | detail::json_const_iterator it2(detail::ObjectBegin(obj)); |
| 4097 | detail::json_const_iterator itEnd(detail::ObjectEnd(obj)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4098 | for (; it2 != itEnd; ++it2) { |
| 4099 | double numVal; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4100 | if (detail::GetNumber(detail::GetValue(it2), numVal)) |
| 4101 | ret->emplace(std::string(detail::GetKey(it2)), numVal); |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | return true; |
| 4105 | } |
| 4106 | |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 4107 | static bool ParseParameterProperty(Parameter *param, std::string *err, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4108 | const detail::json &o, |
| 4109 | const std::string &prop, bool required) { |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 4110 | // A parameter value can either be a string or an array of either a boolean or |
| 4111 | // a number. Booleans of any kind aren't supported here. Granted, it |
| 4112 | // complicates the Parameter structure and breaks it semantically in the sense |
| 4113 | // that the client probably works off the assumption that if the string is |
| 4114 | // empty the vector is used, etc. Would a tagged union work? |
| 4115 | if (ParseStringProperty(¶m->string_value, err, o, prop, false)) { |
| 4116 | // Found string property. |
| 4117 | return true; |
| 4118 | } else if (ParseNumberArrayProperty(¶m->number_array, err, o, prop, |
| 4119 | false)) { |
| 4120 | // Found a number array. |
| 4121 | return true; |
Ben Buzbee | f6af224 | 2018-04-25 15:13:05 -0700 | [diff] [blame] | 4122 | } else if (ParseNumberProperty(¶m->number_value, err, o, prop, false)) { |
Marcin Kacprzak | b12a54e | 2022-09-02 16:13:11 +0200 | [diff] [blame] | 4123 | param->has_number_value = true; |
| 4124 | return true; |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 4125 | } else if (ParseJSONProperty(¶m->json_double_value, err, o, prop, |
| 4126 | false)) { |
| 4127 | return true; |
| 4128 | } else if (ParseBooleanProperty(¶m->bool_value, err, o, prop, false)) { |
| 4129 | return true; |
| 4130 | } else { |
| 4131 | if (required) { |
| 4132 | if (err) { |
| 4133 | (*err) += "parameter must be a string or number / number array.\n"; |
| 4134 | } |
| 4135 | } |
| 4136 | return false; |
| 4137 | } |
| 4138 | } |
| 4139 | |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 4140 | static bool ParseExtensionsProperty(ExtensionMap *ret, std::string *err, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4141 | const detail::json &o) { |
Syoyo Fujita | 48f6db0 | 2018-04-15 18:40:55 +0900 | [diff] [blame] | 4142 | (void)err; |
| 4143 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4144 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4145 | if (!detail::FindMember(o, "extensions", it)) { |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 4146 | return false; |
| 4147 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4148 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4149 | auto &obj = detail::GetValue(it); |
| 4150 | if (!detail::IsObject(obj)) { |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 4151 | return false; |
| 4152 | } |
| 4153 | ExtensionMap extensions; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4154 | detail::json_const_iterator extIt = |
| 4155 | detail::ObjectBegin(obj); // it.value().begin(); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4156 | detail::json_const_iterator extEnd = detail::ObjectEnd(obj); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4157 | for (; extIt != extEnd; ++extIt) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4158 | auto &itObj = detail::GetValue(extIt); |
| 4159 | if (!detail::IsObject(itObj)) continue; |
| 4160 | std::string key(detail::GetKey(extIt)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4161 | if (!ParseJsonAsValue(&extensions[key], itObj)) { |
jrkoonce | 06c30c4 | 2019-09-03 15:56:48 -0500 | [diff] [blame] | 4162 | if (!key.empty()) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4163 | // create empty object so that an extension object is still of type |
| 4164 | // object |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4165 | extensions[key] = Value{Value::Object{}}; |
Selmar Kok | ee3d066 | 2018-10-08 16:20:43 +0200 | [diff] [blame] | 4166 | } |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4167 | } |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 4168 | } |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 4169 | if (ret) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4170 | (*ret) = std::move(extensions); |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 4171 | } |
| 4172 | return true; |
| 4173 | } |
| 4174 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 4175 | template <typename GltfType> |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4176 | static bool ParseExtrasAndExtensions(GltfType *target, std::string *err, |
| 4177 | const detail::json &o, |
| 4178 | bool store_json_strings) { |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 4179 | ParseExtensionsProperty(&target->extensions, err, o); |
| 4180 | ParseExtrasProperty(&target->extras, o); |
| 4181 | |
| 4182 | if (store_json_strings) { |
| 4183 | { |
| 4184 | detail::json_const_iterator it; |
| 4185 | if (detail::FindMember(o, "extensions", it)) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4186 | target->extensions_json_string = |
| 4187 | detail::JsonToString(detail::GetValue(it)); |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 4188 | } |
| 4189 | } |
| 4190 | { |
| 4191 | detail::json_const_iterator it; |
| 4192 | if (detail::FindMember(o, "extras", it)) { |
| 4193 | target->extras_json_string = detail::JsonToString(detail::GetValue(it)); |
| 4194 | } |
| 4195 | } |
| 4196 | } |
| 4197 | return true; |
| 4198 | } |
| 4199 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4200 | static bool ParseAsset(Asset *asset, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4201 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 57c1018 | 2017-10-19 18:48:26 +0900 | [diff] [blame] | 4202 | ParseStringProperty(&asset->version, err, o, "version", true, "Asset"); |
| 4203 | ParseStringProperty(&asset->generator, err, o, "generator", false, "Asset"); |
| 4204 | ParseStringProperty(&asset->minVersion, err, o, "minVersion", false, "Asset"); |
Miguel Sousa | 22bfc84 | 2020-02-20 14:16:58 +0100 | [diff] [blame] | 4205 | ParseStringProperty(&asset->copyright, err, o, "copyright", false, "Asset"); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4206 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4207 | ParseExtrasAndExtensions(asset, err, o, |
| 4208 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4209 | return true; |
| 4210 | } |
| 4211 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4212 | static bool ParseImage(Image *image, const int image_idx, std::string *err, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4213 | std::string *warn, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4214 | bool store_original_json_for_extras_and_extensions, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4215 | const std::string &basedir, const size_t max_file_size, |
| 4216 | FsCallbacks *fs, const URICallbacks *uri_cb, |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 4217 | LoadImageDataFunction *LoadImageData = nullptr, |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 4218 | void *load_image_user_data = nullptr) { |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 4219 | // A glTF image must either reference a bufferView or an image uri |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 4220 | |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4221 | // schema says oneOf [`bufferView`, `uri`] |
| 4222 | // TODO(syoyo): Check the type of each parameters. |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4223 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4224 | bool hasBufferView = detail::FindMember(o, "bufferView", it); |
| 4225 | bool hasURI = detail::FindMember(o, "uri", it); |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4226 | |
Syoyo Fujita | af3ebb2 | 2019-01-06 18:55:57 +0900 | [diff] [blame] | 4227 | ParseStringProperty(&image->name, err, o, "name", false); |
| 4228 | |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4229 | if (hasBufferView && hasURI) { |
| 4230 | // Should not both defined. |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 4231 | if (err) { |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 4232 | (*err) += |
| 4233 | "Only one of `bufferView` or `uri` should be defined, but both are " |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4234 | "defined for image[" + |
| 4235 | std::to_string(image_idx) + "] name = \"" + image->name + "\"\n"; |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4236 | } |
| 4237 | return false; |
| 4238 | } |
| 4239 | |
| 4240 | if (!hasBufferView && !hasURI) { |
| 4241 | if (err) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4242 | (*err) += "Neither required `bufferView` nor `uri` defined for image[" + |
| 4243 | std::to_string(image_idx) + "] name = \"" + image->name + |
| 4244 | "\"\n"; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 4245 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4246 | return false; |
| 4247 | } |
| 4248 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4249 | ParseExtrasAndExtensions(image, err, o, |
| 4250 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4251 | |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4252 | if (hasBufferView) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4253 | int bufferView = -1; |
| 4254 | if (!ParseIntegerProperty(&bufferView, err, o, "bufferView", true)) { |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4255 | if (err) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4256 | (*err) += "Failed to parse `bufferView` for image[" + |
| 4257 | std::to_string(image_idx) + "] name = \"" + image->name + |
| 4258 | "\"\n"; |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4259 | } |
| 4260 | return false; |
| 4261 | } |
| 4262 | |
| 4263 | std::string mime_type; |
| 4264 | ParseStringProperty(&mime_type, err, o, "mimeType", false); |
| 4265 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4266 | int width = 0; |
| 4267 | ParseIntegerProperty(&width, err, o, "width", false); |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4268 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4269 | int height = 0; |
| 4270 | ParseIntegerProperty(&height, err, o, "height", false); |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4271 | |
| 4272 | // Just only save some information here. Loading actual image data from |
| 4273 | // bufferView is done after this `ParseImage` function. |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4274 | image->bufferView = bufferView; |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4275 | image->mimeType = mime_type; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4276 | image->width = width; |
| 4277 | image->height = height; |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4278 | |
| 4279 | return true; |
| 4280 | } |
| 4281 | |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4282 | // Parse URI & Load image data. |
| 4283 | |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4284 | std::string uri; |
| 4285 | std::string tmp_err; |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4286 | if (!ParseStringProperty(&uri, &tmp_err, o, "uri", true)) { |
| 4287 | if (err) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4288 | (*err) += "Failed to parse `uri` for image[" + std::to_string(image_idx) + |
| 4289 | "] name = \"" + image->name + "\".\n"; |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4290 | } |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4291 | return false; |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4292 | } |
| 4293 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4294 | std::vector<unsigned char> img; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4295 | |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4296 | if (IsDataURI(uri)) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 4297 | if (!DecodeDataURI(&img, image->mimeType, uri, 0, false)) { |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4298 | if (err) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4299 | (*err) += "Failed to decode 'uri' for image[" + |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 4300 | std::to_string(image_idx) + "] name = \"" + image->name + |
| 4301 | "\"\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4302 | } |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4303 | return false; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4304 | } |
| 4305 | } else { |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4306 | // Assume external file |
| 4307 | // Keep texture path (for textures that cannot be decoded) |
| 4308 | image->uri = uri; |
Selmar | 67af3c9 | 2018-03-16 11:48:19 +0100 | [diff] [blame] | 4309 | #ifdef TINYGLTF_NO_EXTERNAL_IMAGE |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4310 | return true; |
AlvaroBarua | 4317223 | 2022-09-11 00:41:43 +0100 | [diff] [blame] | 4311 | #else |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 4312 | std::string decoded_uri; |
| 4313 | if (!uri_cb->decode(uri, &decoded_uri, uri_cb->user_data)) { |
| 4314 | if (warn) { |
| 4315 | (*warn) += "Failed to decode 'uri' for image[" + |
Syoyo Fujita | 877d856 | 2023-04-23 21:47:31 +0900 | [diff] [blame] | 4316 | std::to_string(image_idx) + "] name = \"" + image->name + |
| 4317 | "\"\n"; |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 4318 | } |
| 4319 | |
| 4320 | // Image loading failure is not critical to overall gltf loading. |
| 4321 | return true; |
| 4322 | } |
| 4323 | |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 4324 | if (!LoadExternalFile(&img, err, warn, decoded_uri, basedir, |
| 4325 | /* required */ false, /* required bytes */ 0, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4326 | /* checksize */ false, |
| 4327 | /* max file size */ max_file_size, fs)) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 4328 | if (warn) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4329 | (*warn) += "Failed to load external 'uri' for image[" + |
Syoyo Fujita | 877d856 | 2023-04-23 21:47:31 +0900 | [diff] [blame] | 4330 | std::to_string(image_idx) + "] name = \"" + decoded_uri + |
| 4331 | "\"\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4332 | } |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4333 | // If the image cannot be loaded, keep uri as image->uri. |
| 4334 | return true; |
| 4335 | } |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4336 | |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4337 | if (img.empty()) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 4338 | if (warn) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4339 | (*warn) += "Image data is empty for image[" + |
Syoyo Fujita | 877d856 | 2023-04-23 21:47:31 +0900 | [diff] [blame] | 4340 | std::to_string(image_idx) + "] name = \"" + image->name + |
| 4341 | "\" \n"; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4342 | } |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4343 | return false; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4344 | } |
AlvaroBarua | 4317223 | 2022-09-11 00:41:43 +0100 | [diff] [blame] | 4345 | #endif |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4346 | } |
| 4347 | |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 4348 | if (*LoadImageData == nullptr) { |
| 4349 | if (err) { |
| 4350 | (*err) += "No LoadImageData callback specified.\n"; |
| 4351 | } |
| 4352 | return false; |
| 4353 | } |
Syoyo Fujita | af3ebb2 | 2019-01-06 18:55:57 +0900 | [diff] [blame] | 4354 | return (*LoadImageData)(image, image_idx, err, warn, 0, 0, &img.at(0), |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 4355 | static_cast<int>(img.size()), load_image_user_data); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4356 | } |
| 4357 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4358 | static bool ParseTexture(Texture *texture, std::string *err, |
| 4359 | const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4360 | bool store_original_json_for_extras_and_extensions, |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4361 | const std::string &basedir) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4362 | (void)basedir; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4363 | int sampler = -1; |
| 4364 | int source = -1; |
| 4365 | ParseIntegerProperty(&sampler, err, o, "sampler", false); |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4366 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4367 | ParseIntegerProperty(&source, err, o, "source", false); |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 4368 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4369 | texture->sampler = sampler; |
| 4370 | texture->source = source; |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 4371 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4372 | ParseExtrasAndExtensions(texture, err, o, |
| 4373 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4374 | |
Vladimír Vondruš | 239be2c | 2018-07-24 23:23:56 +0200 | [diff] [blame] | 4375 | ParseStringProperty(&texture->name, err, o, "name", false); |
| 4376 | |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 4377 | return true; |
| 4378 | } |
| 4379 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4380 | static bool ParseTextureInfo( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4381 | TextureInfo *texinfo, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4382 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 4383 | if (texinfo == nullptr) { |
| 4384 | return false; |
| 4385 | } |
| 4386 | |
| 4387 | if (!ParseIntegerProperty(&texinfo->index, err, o, "index", |
| 4388 | /* required */ true, "TextureInfo")) { |
| 4389 | return false; |
| 4390 | } |
| 4391 | |
| 4392 | ParseIntegerProperty(&texinfo->texCoord, err, o, "texCoord", false); |
| 4393 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4394 | ParseExtrasAndExtensions(texinfo, err, o, |
| 4395 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4396 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 4397 | return true; |
| 4398 | } |
| 4399 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4400 | static bool ParseNormalTextureInfo( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4401 | NormalTextureInfo *texinfo, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4402 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 4403 | if (texinfo == nullptr) { |
| 4404 | return false; |
| 4405 | } |
| 4406 | |
| 4407 | if (!ParseIntegerProperty(&texinfo->index, err, o, "index", |
| 4408 | /* required */ true, "NormalTextureInfo")) { |
| 4409 | return false; |
| 4410 | } |
| 4411 | |
| 4412 | ParseIntegerProperty(&texinfo->texCoord, err, o, "texCoord", false); |
| 4413 | ParseNumberProperty(&texinfo->scale, err, o, "scale", false); |
| 4414 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4415 | ParseExtrasAndExtensions(texinfo, err, o, |
| 4416 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4417 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 4418 | return true; |
| 4419 | } |
| 4420 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4421 | static bool ParseOcclusionTextureInfo( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4422 | OcclusionTextureInfo *texinfo, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4423 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 4424 | if (texinfo == nullptr) { |
| 4425 | return false; |
| 4426 | } |
| 4427 | |
| 4428 | if (!ParseIntegerProperty(&texinfo->index, err, o, "index", |
| 4429 | /* required */ true, "NormalTextureInfo")) { |
| 4430 | return false; |
| 4431 | } |
| 4432 | |
| 4433 | ParseIntegerProperty(&texinfo->texCoord, err, o, "texCoord", false); |
| 4434 | ParseNumberProperty(&texinfo->strength, err, o, "strength", false); |
| 4435 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4436 | ParseExtrasAndExtensions(texinfo, err, o, |
| 4437 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4438 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 4439 | return true; |
| 4440 | } |
| 4441 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4442 | static bool ParseBuffer(Buffer *buffer, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4443 | bool store_original_json_for_extras_and_extensions, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 4444 | FsCallbacks *fs, const URICallbacks *uri_cb, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4445 | const std::string &basedir, |
| 4446 | const size_t max_buffer_size, bool is_binary = false, |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 4447 | const unsigned char *bin_data = nullptr, |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4448 | size_t bin_size = 0) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4449 | size_t byteLength; |
| 4450 | if (!ParseUnsignedProperty(&byteLength, err, o, "byteLength", true, |
| 4451 | "Buffer")) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4452 | return false; |
| 4453 | } |
| 4454 | |
Aurélien Chatelain | 0aab157 | 2017-05-30 15:03:48 +0000 | [diff] [blame] | 4455 | // In glTF 2.0, uri is not mandatory anymore |
Syoyo Fujita | 20244e1 | 2018-03-15 11:01:05 -0500 | [diff] [blame] | 4456 | buffer->uri.clear(); |
| 4457 | ParseStringProperty(&buffer->uri, err, o, "uri", false, "Buffer"); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4458 | |
Aurélien Chatelain | 0aab157 | 2017-05-30 15:03:48 +0000 | [diff] [blame] | 4459 | // having an empty uri for a non embedded image should not be valid |
Syoyo Fujita | 20244e1 | 2018-03-15 11:01:05 -0500 | [diff] [blame] | 4460 | if (!is_binary && buffer->uri.empty()) { |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 4461 | if (err) { |
| 4462 | (*err) += "'uri' is missing from non binary glTF file buffer.\n"; |
| 4463 | } |
Aurélien Chatelain | 0aab157 | 2017-05-30 15:03:48 +0000 | [diff] [blame] | 4464 | } |
| 4465 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4466 | detail::json_const_iterator type; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4467 | if (detail::FindMember(o, "type", type)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4468 | std::string typeStr; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4469 | if (detail::GetString(detail::GetValue(type), typeStr)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4470 | if (typeStr.compare("arraybuffer") == 0) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4471 | // buffer.type = "arraybuffer"; |
| 4472 | } |
| 4473 | } |
| 4474 | } |
| 4475 | |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 4476 | if (is_binary) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 4477 | // Still binary glTF accepts external dataURI. |
Syoyo Fujita | 20244e1 | 2018-03-15 11:01:05 -0500 | [diff] [blame] | 4478 | if (!buffer->uri.empty()) { |
Syoyo Fujita | 39abfb5 | 2018-07-11 02:46:52 +0900 | [diff] [blame] | 4479 | // First try embedded data URI. |
| 4480 | if (IsDataURI(buffer->uri)) { |
| 4481 | std::string mime_type; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4482 | if (!DecodeDataURI(&buffer->data, mime_type, buffer->uri, byteLength, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 4483 | true)) { |
| 4484 | if (err) { |
| 4485 | (*err) += |
| 4486 | "Failed to decode 'uri' : " + buffer->uri + " in Buffer\n"; |
| 4487 | } |
| 4488 | return false; |
Syoyo Fujita | 39abfb5 | 2018-07-11 02:46:52 +0900 | [diff] [blame] | 4489 | } |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 4490 | } else { |
Syoyo Fujita | 39abfb5 | 2018-07-11 02:46:52 +0900 | [diff] [blame] | 4491 | // External .bin file. |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 4492 | std::string decoded_uri; |
| 4493 | if (!uri_cb->decode(buffer->uri, &decoded_uri, uri_cb->user_data)) { |
| 4494 | return false; |
| 4495 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 4496 | if (!LoadExternalFile(&buffer->data, err, /* warn */ nullptr, |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 4497 | decoded_uri, basedir, /* required */ true, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4498 | byteLength, /* checkSize */ true, |
| 4499 | /* max_file_size */ max_buffer_size, fs)) { |
Vladimír Vondruš | fd84ceb | 2018-08-16 21:07:56 +0200 | [diff] [blame] | 4500 | return false; |
| 4501 | } |
Syoyo Fujita | 39abfb5 | 2018-07-11 02:46:52 +0900 | [diff] [blame] | 4502 | } |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 4503 | } else { |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4504 | // load data from (embedded) binary data |
| 4505 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 4506 | if ((bin_size == 0) || (bin_data == nullptr)) { |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4507 | if (err) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4508 | (*err) += |
| 4509 | "Invalid binary data in `Buffer', or GLB with empty BIN chunk.\n"; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4510 | } |
| 4511 | return false; |
| 4512 | } |
| 4513 | |
| 4514 | if (byteLength > bin_size) { |
| 4515 | if (err) { |
| 4516 | std::stringstream ss; |
| 4517 | ss << "Invalid `byteLength'. Must be equal or less than binary size: " |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 4518 | "`byteLength' = " |
| 4519 | << byteLength << ", binary size = " << bin_size << std::endl; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4520 | (*err) += ss.str(); |
| 4521 | } |
| 4522 | return false; |
| 4523 | } |
| 4524 | |
Aurélien Chatelain | 0aab157 | 2017-05-30 15:03:48 +0000 | [diff] [blame] | 4525 | // Read buffer data |
| 4526 | buffer->data.resize(static_cast<size_t>(byteLength)); |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 4527 | memcpy(&(buffer->data.at(0)), bin_data, static_cast<size_t>(byteLength)); |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4528 | } |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 4529 | |
| 4530 | } else { |
Syoyo Fujita | 20244e1 | 2018-03-15 11:01:05 -0500 | [diff] [blame] | 4531 | if (IsDataURI(buffer->uri)) { |
johan bowald | ef151a4 | 2018-04-01 13:44:48 +0200 | [diff] [blame] | 4532 | std::string mime_type; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4533 | if (!DecodeDataURI(&buffer->data, mime_type, buffer->uri, byteLength, |
| 4534 | true)) { |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 4535 | if (err) { |
Syoyo Fujita | 20244e1 | 2018-03-15 11:01:05 -0500 | [diff] [blame] | 4536 | (*err) += "Failed to decode 'uri' : " + buffer->uri + " in Buffer\n"; |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 4537 | } |
| 4538 | return false; |
| 4539 | } |
| 4540 | } else { |
| 4541 | // Assume external .bin file. |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 4542 | std::string decoded_uri; |
| 4543 | if (!uri_cb->decode(buffer->uri, &decoded_uri, uri_cb->user_data)) { |
| 4544 | return false; |
| 4545 | } |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 4546 | if (!LoadExternalFile(&buffer->data, err, /* warn */ nullptr, decoded_uri, |
| 4547 | basedir, /* required */ true, byteLength, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4548 | /* checkSize */ true, |
| 4549 | /* max file size */ max_buffer_size, fs)) { |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 4550 | return false; |
| 4551 | } |
| 4552 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4553 | } |
| 4554 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4555 | ParseStringProperty(&buffer->name, err, o, "name", false); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4556 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4557 | ParseExtrasAndExtensions(buffer, err, o, |
| 4558 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4559 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4560 | return true; |
| 4561 | } |
| 4562 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4563 | static bool ParseBufferView( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4564 | BufferView *bufferView, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4565 | bool store_original_json_for_extras_and_extensions) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4566 | int buffer = -1; |
| 4567 | if (!ParseIntegerProperty(&buffer, err, o, "buffer", true, "BufferView")) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4568 | return false; |
| 4569 | } |
| 4570 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4571 | size_t byteOffset = 0; |
| 4572 | ParseUnsignedProperty(&byteOffset, err, o, "byteOffset", false); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4573 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4574 | size_t byteLength = 1; |
| 4575 | if (!ParseUnsignedProperty(&byteLength, err, o, "byteLength", true, |
| 4576 | "BufferView")) { |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 4577 | return false; |
| 4578 | } |
| 4579 | |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 4580 | size_t byteStride = 0; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4581 | if (!ParseUnsignedProperty(&byteStride, err, o, "byteStride", false)) { |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 4582 | // Spec says: When byteStride of referenced bufferView is not defined, it |
| 4583 | // means that accessor elements are tightly packed, i.e., effective stride |
| 4584 | // equals the size of the element. |
| 4585 | // We cannot determine the actual byteStride until Accessor are parsed, thus |
| 4586 | // set 0(= tightly packed) here(as done in OpenGL's VertexAttribPoiner) |
| 4587 | byteStride = 0; |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 4588 | } |
| 4589 | |
| 4590 | if ((byteStride > 252) || ((byteStride % 4) != 0)) { |
| 4591 | if (err) { |
| 4592 | std::stringstream ss; |
| 4593 | ss << "Invalid `byteStride' value. `byteStride' must be the multiple of " |
| 4594 | "4 : " |
| 4595 | << byteStride << std::endl; |
| 4596 | |
| 4597 | (*err) += ss.str(); |
| 4598 | } |
| 4599 | return false; |
| 4600 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4601 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4602 | int target = 0; |
| 4603 | ParseIntegerProperty(&target, err, o, "target", false); |
| 4604 | if ((target == TINYGLTF_TARGET_ARRAY_BUFFER) || |
| 4605 | (target == TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER)) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4606 | // OK |
| 4607 | } else { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4608 | target = 0; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4609 | } |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4610 | bufferView->target = target; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4611 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4612 | ParseStringProperty(&bufferView->name, err, o, "name", false); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4613 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4614 | ParseExtrasAndExtensions(bufferView, err, o, |
| 4615 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4616 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4617 | bufferView->buffer = buffer; |
| 4618 | bufferView->byteOffset = byteOffset; |
| 4619 | bufferView->byteLength = byteLength; |
| 4620 | bufferView->byteStride = byteStride; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4621 | return true; |
| 4622 | } |
| 4623 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4624 | static bool ParseSparseAccessor( |
| 4625 | Accessor::Sparse *sparse, std::string *err, const detail::json &o, |
| 4626 | bool store_original_json_for_extras_and_extensions) { |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 4627 | sparse->isSparse = true; |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4628 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4629 | int count = 0; |
Syoyo Fujita | cbd3885 | 2022-02-26 21:19:15 +0900 | [diff] [blame] | 4630 | if (!ParseIntegerProperty(&count, err, o, "count", true, "SparseAccessor")) { |
| 4631 | return false; |
| 4632 | } |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4633 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4634 | ParseExtrasAndExtensions(sparse, err, o, |
| 4635 | store_original_json_for_extras_and_extensions); |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 4636 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4637 | detail::json_const_iterator indices_iterator; |
| 4638 | detail::json_const_iterator values_iterator; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4639 | if (!detail::FindMember(o, "indices", indices_iterator)) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4640 | (*err) = "the sparse object of this accessor doesn't have indices"; |
| 4641 | return false; |
| 4642 | } |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4643 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4644 | if (!detail::FindMember(o, "values", values_iterator)) { |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 4645 | (*err) = "the sparse object of this accessor doesn't have values"; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4646 | return false; |
| 4647 | } |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4648 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4649 | const detail::json &indices_obj = detail::GetValue(indices_iterator); |
| 4650 | const detail::json &values_obj = detail::GetValue(values_iterator); |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4651 | |
emimvi | 759976e | 2023-09-02 09:39:53 +0200 | [diff] [blame] | 4652 | int indices_buffer_view = 0, component_type = 0; |
| 4653 | size_t indices_byte_offset = 0; |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 4654 | if (!ParseIntegerProperty(&indices_buffer_view, err, indices_obj, |
| 4655 | "bufferView", true, "SparseAccessor")) { |
Syoyo Fujita | cbd3885 | 2022-02-26 21:19:15 +0900 | [diff] [blame] | 4656 | return false; |
| 4657 | } |
emimvi | 759976e | 2023-09-02 09:39:53 +0200 | [diff] [blame] | 4658 | ParseUnsignedProperty(&indices_byte_offset, err, indices_obj, "byteOffset", |
Jack Mousseau | 9471517 | 2022-02-24 14:25:37 -0800 | [diff] [blame] | 4659 | false); |
Syoyo Fujita | cbd3885 | 2022-02-26 21:19:15 +0900 | [diff] [blame] | 4660 | if (!ParseIntegerProperty(&component_type, err, indices_obj, "componentType", |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 4661 | true, "SparseAccessor")) { |
Syoyo Fujita | cbd3885 | 2022-02-26 21:19:15 +0900 | [diff] [blame] | 4662 | return false; |
| 4663 | } |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4664 | |
emimvi | 759976e | 2023-09-02 09:39:53 +0200 | [diff] [blame] | 4665 | int values_buffer_view = 0; |
| 4666 | size_t values_byte_offset = 0; |
Syoyo Fujita | cbd3885 | 2022-02-26 21:19:15 +0900 | [diff] [blame] | 4667 | if (!ParseIntegerProperty(&values_buffer_view, err, values_obj, "bufferView", |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 4668 | true, "SparseAccessor")) { |
Syoyo Fujita | cbd3885 | 2022-02-26 21:19:15 +0900 | [diff] [blame] | 4669 | return false; |
| 4670 | } |
emimvi | 759976e | 2023-09-02 09:39:53 +0200 | [diff] [blame] | 4671 | ParseUnsignedProperty(&values_byte_offset, err, values_obj, "byteOffset", |
Jack Mousseau | 9471517 | 2022-02-24 14:25:37 -0800 | [diff] [blame] | 4672 | false); |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4673 | |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 4674 | sparse->count = count; |
| 4675 | sparse->indices.bufferView = indices_buffer_view; |
| 4676 | sparse->indices.byteOffset = indices_byte_offset; |
| 4677 | sparse->indices.componentType = component_type; |
| 4678 | ParseExtrasAndExtensions(&sparse->indices, err, indices_obj, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4679 | store_original_json_for_extras_and_extensions); |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4680 | |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 4681 | sparse->values.bufferView = values_buffer_view; |
| 4682 | sparse->values.byteOffset = values_byte_offset; |
| 4683 | ParseExtrasAndExtensions(&sparse->values, err, values_obj, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4684 | store_original_json_for_extras_and_extensions); |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4685 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4686 | return true; |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4687 | } |
| 4688 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4689 | static bool ParseAccessor(Accessor *accessor, std::string *err, |
| 4690 | const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4691 | bool store_original_json_for_extras_and_extensions) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4692 | int bufferView = -1; |
| 4693 | ParseIntegerProperty(&bufferView, err, o, "bufferView", false, "Accessor"); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4694 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4695 | size_t byteOffset = 0; |
| 4696 | ParseUnsignedProperty(&byteOffset, err, o, "byteOffset", false, "Accessor"); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4697 | |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 4698 | bool normalized = false; |
| 4699 | ParseBooleanProperty(&normalized, err, o, "normalized", false, "Accessor"); |
| 4700 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4701 | size_t componentType = 0; |
| 4702 | if (!ParseUnsignedProperty(&componentType, err, o, "componentType", true, |
| 4703 | "Accessor")) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4704 | return false; |
| 4705 | } |
| 4706 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4707 | size_t count = 0; |
| 4708 | if (!ParseUnsignedProperty(&count, err, o, "count", true, "Accessor")) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4709 | return false; |
| 4710 | } |
| 4711 | |
| 4712 | std::string type; |
Syoyo Fujita | 90e1ed2 | 2017-05-30 00:47:37 +0900 | [diff] [blame] | 4713 | if (!ParseStringProperty(&type, err, o, "type", true, "Accessor")) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4714 | return false; |
| 4715 | } |
| 4716 | |
| 4717 | if (type.compare("SCALAR") == 0) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4718 | accessor->type = TINYGLTF_TYPE_SCALAR; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4719 | } else if (type.compare("VEC2") == 0) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4720 | accessor->type = TINYGLTF_TYPE_VEC2; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4721 | } else if (type.compare("VEC3") == 0) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4722 | accessor->type = TINYGLTF_TYPE_VEC3; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4723 | } else if (type.compare("VEC4") == 0) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4724 | accessor->type = TINYGLTF_TYPE_VEC4; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4725 | } else if (type.compare("MAT2") == 0) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4726 | accessor->type = TINYGLTF_TYPE_MAT2; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4727 | } else if (type.compare("MAT3") == 0) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4728 | accessor->type = TINYGLTF_TYPE_MAT3; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4729 | } else if (type.compare("MAT4") == 0) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4730 | accessor->type = TINYGLTF_TYPE_MAT4; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4731 | } else { |
| 4732 | std::stringstream ss; |
| 4733 | ss << "Unsupported `type` for accessor object. Got \"" << type << "\"\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4734 | if (err) { |
| 4735 | (*err) += ss.str(); |
| 4736 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4737 | return false; |
| 4738 | } |
| 4739 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4740 | ParseStringProperty(&accessor->name, err, o, "name", false); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4741 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4742 | accessor->minValues.clear(); |
| 4743 | accessor->maxValues.clear(); |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 4744 | ParseNumberArrayProperty(&accessor->minValues, err, o, "min", false, |
| 4745 | "Accessor"); |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 4746 | |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 4747 | ParseNumberArrayProperty(&accessor->maxValues, err, o, "max", false, |
| 4748 | "Accessor"); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4749 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4750 | accessor->count = count; |
| 4751 | accessor->bufferView = bufferView; |
| 4752 | accessor->byteOffset = byteOffset; |
jianghaosen | 4748ad6 | 2017-08-29 20:08:37 +0800 | [diff] [blame] | 4753 | accessor->normalized = normalized; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4754 | { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4755 | if (componentType >= TINYGLTF_COMPONENT_TYPE_BYTE && |
| 4756 | componentType <= TINYGLTF_COMPONENT_TYPE_DOUBLE) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4757 | // OK |
Arthur Brainville (Ybalrid) | 811e1d3 | 2019-06-15 07:32:38 +0200 | [diff] [blame] | 4758 | accessor->componentType = int(componentType); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4759 | } else { |
| 4760 | std::stringstream ss; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4761 | ss << "Invalid `componentType` in accessor. Got " << componentType |
| 4762 | << "\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4763 | if (err) { |
| 4764 | (*err) += ss.str(); |
| 4765 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4766 | return false; |
| 4767 | } |
| 4768 | } |
| 4769 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4770 | ParseExtrasAndExtensions(accessor, err, o, |
| 4771 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4772 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4773 | // check if accessor has a "sparse" object: |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4774 | detail::json_const_iterator iterator; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4775 | if (detail::FindMember(o, "sparse", iterator)) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4776 | // here this accessor has a "sparse" subobject |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4777 | return ParseSparseAccessor(&accessor->sparse, err, |
| 4778 | detail::GetValue(iterator), |
| 4779 | store_original_json_for_extras_and_extensions); |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4780 | } |
| 4781 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4782 | return true; |
| 4783 | } |
| 4784 | |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4785 | #ifdef TINYGLTF_ENABLE_DRACO |
| 4786 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4787 | static void DecodeIndexBuffer(draco::Mesh *mesh, size_t componentSize, |
| 4788 | std::vector<uint8_t> &outBuffer) { |
| 4789 | if (componentSize == 4) { |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4790 | assert(sizeof(mesh->face(draco::FaceIndex(0))[0]) == componentSize); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4791 | memcpy(outBuffer.data(), &mesh->face(draco::FaceIndex(0))[0], |
| 4792 | outBuffer.size()); |
| 4793 | } else { |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4794 | size_t faceStride = componentSize * 3; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4795 | for (draco::FaceIndex f(0); f < mesh->num_faces(); ++f) { |
| 4796 | const draco::Mesh::Face &face = mesh->face(f); |
| 4797 | if (componentSize == 2) { |
| 4798 | uint16_t indices[3] = {(uint16_t)face[0].value(), |
| 4799 | (uint16_t)face[1].value(), |
| 4800 | (uint16_t)face[2].value()}; |
| 4801 | memcpy(outBuffer.data() + f.value() * faceStride, &indices[0], |
| 4802 | faceStride); |
| 4803 | } else { |
| 4804 | uint8_t indices[3] = {(uint8_t)face[0].value(), |
| 4805 | (uint8_t)face[1].value(), |
| 4806 | (uint8_t)face[2].value()}; |
| 4807 | memcpy(outBuffer.data() + f.value() * faceStride, &indices[0], |
| 4808 | faceStride); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4809 | } |
| 4810 | } |
| 4811 | } |
| 4812 | } |
| 4813 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4814 | template <typename T> |
| 4815 | static bool GetAttributeForAllPoints(draco::Mesh *mesh, |
| 4816 | const draco::PointAttribute *pAttribute, |
| 4817 | std::vector<uint8_t> &outBuffer) { |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4818 | size_t byteOffset = 0; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4819 | T values[4] = {0, 0, 0, 0}; |
| 4820 | for (draco::PointIndex i(0); i < mesh->num_points(); ++i) { |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4821 | const draco::AttributeValueIndex val_index = pAttribute->mapped_index(i); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4822 | if (!pAttribute->ConvertValue<T>(val_index, pAttribute->num_components(), |
| 4823 | values)) |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4824 | return false; |
| 4825 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4826 | memcpy(outBuffer.data() + byteOffset, &values[0], |
| 4827 | sizeof(T) * pAttribute->num_components()); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4828 | byteOffset += sizeof(T) * pAttribute->num_components(); |
| 4829 | } |
| 4830 | |
| 4831 | return true; |
| 4832 | } |
| 4833 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4834 | static bool GetAttributeForAllPoints(uint32_t componentType, draco::Mesh *mesh, |
| 4835 | const draco::PointAttribute *pAttribute, |
| 4836 | std::vector<uint8_t> &outBuffer) { |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4837 | bool decodeResult = false; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4838 | switch (componentType) { |
| 4839 | case TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE: |
| 4840 | decodeResult = |
| 4841 | GetAttributeForAllPoints<uint8_t>(mesh, pAttribute, outBuffer); |
| 4842 | break; |
| 4843 | case TINYGLTF_COMPONENT_TYPE_BYTE: |
| 4844 | decodeResult = |
| 4845 | GetAttributeForAllPoints<int8_t>(mesh, pAttribute, outBuffer); |
| 4846 | break; |
| 4847 | case TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT: |
| 4848 | decodeResult = |
| 4849 | GetAttributeForAllPoints<uint16_t>(mesh, pAttribute, outBuffer); |
| 4850 | break; |
| 4851 | case TINYGLTF_COMPONENT_TYPE_SHORT: |
| 4852 | decodeResult = |
| 4853 | GetAttributeForAllPoints<int16_t>(mesh, pAttribute, outBuffer); |
| 4854 | break; |
| 4855 | case TINYGLTF_COMPONENT_TYPE_INT: |
| 4856 | decodeResult = |
| 4857 | GetAttributeForAllPoints<int32_t>(mesh, pAttribute, outBuffer); |
| 4858 | break; |
| 4859 | case TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT: |
| 4860 | decodeResult = |
| 4861 | GetAttributeForAllPoints<uint32_t>(mesh, pAttribute, outBuffer); |
| 4862 | break; |
| 4863 | case TINYGLTF_COMPONENT_TYPE_FLOAT: |
| 4864 | decodeResult = |
| 4865 | GetAttributeForAllPoints<float>(mesh, pAttribute, outBuffer); |
| 4866 | break; |
| 4867 | case TINYGLTF_COMPONENT_TYPE_DOUBLE: |
| 4868 | decodeResult = |
| 4869 | GetAttributeForAllPoints<double>(mesh, pAttribute, outBuffer); |
| 4870 | break; |
| 4871 | default: |
| 4872 | return false; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4873 | } |
| 4874 | |
| 4875 | return decodeResult; |
| 4876 | } |
| 4877 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4878 | static bool ParseDracoExtension(Primitive *primitive, Model *model, |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 4879 | std::string *err, std::string *warn, |
| 4880 | const Value &dracoExtensionValue, |
| 4881 | ParseStrictness strictness) { |
snowapril | 84e1526 | 2021-03-20 19:25:58 +0900 | [diff] [blame] | 4882 | (void)err; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4883 | auto bufferViewValue = dracoExtensionValue.Get("bufferView"); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4884 | if (!bufferViewValue.IsInt()) return false; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4885 | auto attributesValue = dracoExtensionValue.Get("attributes"); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4886 | if (!attributesValue.IsObject()) return false; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4887 | |
| 4888 | auto attributesObject = attributesValue.Get<Value::Object>(); |
| 4889 | int bufferView = bufferViewValue.Get<int>(); |
| 4890 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4891 | BufferView &view = model->bufferViews[bufferView]; |
| 4892 | Buffer &buffer = model->buffers[view.buffer]; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4893 | // BufferView has already been decoded |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4894 | if (view.dracoDecoded) return true; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4895 | view.dracoDecoded = true; |
| 4896 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4897 | const char *bufferViewData = |
| 4898 | reinterpret_cast<const char *>(buffer.data.data() + view.byteOffset); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4899 | size_t bufferViewSize = view.byteLength; |
| 4900 | |
| 4901 | // decode draco |
| 4902 | draco::DecoderBuffer decoderBuffer; |
| 4903 | decoderBuffer.Init(bufferViewData, bufferViewSize); |
| 4904 | draco::Decoder decoder; |
| 4905 | auto decodeResult = decoder.DecodeMeshFromBuffer(&decoderBuffer); |
| 4906 | if (!decodeResult.ok()) { |
| 4907 | return false; |
| 4908 | } |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4909 | const std::unique_ptr<draco::Mesh> &mesh = decodeResult.value(); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4910 | |
| 4911 | // create new bufferView for indices |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4912 | if (primitive->indices >= 0) { |
Nyall Dawson | 0067b4d | 2023-09-07 08:20:28 +1000 | [diff] [blame] | 4913 | if (strictness == ParseStrictness::Permissive) { |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 4914 | const draco::PointIndex::ValueType numPoint = mesh->num_points(); |
| 4915 | // handle the situation where the stored component type does not match the |
| 4916 | // required type for the actual number of stored points |
| 4917 | int supposedComponentType = TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE; |
| 4918 | if (numPoint < static_cast<draco::PointIndex::ValueType>( |
| 4919 | std::numeric_limits<uint8_t>::max())) { |
| 4920 | supposedComponentType = TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE; |
| 4921 | } else if ( |
| 4922 | numPoint < static_cast<draco::PointIndex::ValueType>( |
| 4923 | std::numeric_limits<uint16_t>::max())) { |
| 4924 | supposedComponentType = TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT; |
| 4925 | } else { |
| 4926 | supposedComponentType = TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT; |
| 4927 | } |
| 4928 | |
| 4929 | if (supposedComponentType > model->accessors[primitive->indices].componentType) { |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 4930 | if (warn) { |
| 4931 | (*warn) += |
| 4932 | "GLTF component type " + std::to_string(model->accessors[primitive->indices].componentType) + |
| 4933 | " is not sufficient for number of stored points," |
| 4934 | " treating as " + std::to_string(supposedComponentType) + "\n"; |
| 4935 | } |
Nyall Dawson | c35819f | 2023-09-12 08:46:02 +1000 | [diff] [blame] | 4936 | model->accessors[primitive->indices].componentType = supposedComponentType; |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 4937 | } |
| 4938 | } |
| 4939 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4940 | int32_t componentSize = GetComponentSizeInBytes( |
| 4941 | model->accessors[primitive->indices].componentType); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4942 | Buffer decodedIndexBuffer; |
| 4943 | decodedIndexBuffer.data.resize(mesh->num_faces() * 3 * componentSize); |
| 4944 | |
| 4945 | DecodeIndexBuffer(mesh.get(), componentSize, decodedIndexBuffer.data); |
| 4946 | |
| 4947 | model->buffers.emplace_back(std::move(decodedIndexBuffer)); |
| 4948 | |
| 4949 | BufferView decodedIndexBufferView; |
| 4950 | decodedIndexBufferView.buffer = int(model->buffers.size() - 1); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4951 | decodedIndexBufferView.byteLength = |
| 4952 | int(mesh->num_faces() * 3 * componentSize); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4953 | decodedIndexBufferView.byteOffset = 0; |
| 4954 | decodedIndexBufferView.byteStride = 0; |
| 4955 | decodedIndexBufferView.target = TINYGLTF_TARGET_ARRAY_BUFFER; |
| 4956 | model->bufferViews.emplace_back(std::move(decodedIndexBufferView)); |
| 4957 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4958 | model->accessors[primitive->indices].bufferView = |
| 4959 | int(model->bufferViews.size() - 1); |
Alexander Wood | 0d77a29 | 2019-01-26 08:58:45 -0500 | [diff] [blame] | 4960 | model->accessors[primitive->indices].count = int(mesh->num_faces() * 3); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4961 | } |
| 4962 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4963 | for (const auto &attribute : attributesObject) { |
| 4964 | if (!attribute.second.IsInt()) return false; |
Alexander Wood | 0d77a29 | 2019-01-26 08:58:45 -0500 | [diff] [blame] | 4965 | auto primitiveAttribute = primitive->attributes.find(attribute.first); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4966 | if (primitiveAttribute == primitive->attributes.end()) return false; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4967 | |
| 4968 | int dracoAttributeIndex = attribute.second.Get<int>(); |
| 4969 | const auto pAttribute = mesh->GetAttributeByUniqueId(dracoAttributeIndex); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4970 | const auto componentType = |
| 4971 | model->accessors[primitiveAttribute->second].componentType; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4972 | |
| 4973 | // Create a new buffer for this decoded buffer |
| 4974 | Buffer decodedBuffer; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4975 | size_t bufferSize = mesh->num_points() * pAttribute->num_components() * |
| 4976 | GetComponentSizeInBytes(componentType); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4977 | decodedBuffer.data.resize(bufferSize); |
| 4978 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4979 | if (!GetAttributeForAllPoints(componentType, mesh.get(), pAttribute, |
| 4980 | decodedBuffer.data)) |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4981 | return false; |
| 4982 | |
| 4983 | model->buffers.emplace_back(std::move(decodedBuffer)); |
| 4984 | |
| 4985 | BufferView decodedBufferView; |
| 4986 | decodedBufferView.buffer = int(model->buffers.size() - 1); |
| 4987 | decodedBufferView.byteLength = bufferSize; |
| 4988 | decodedBufferView.byteOffset = pAttribute->byte_offset(); |
| 4989 | decodedBufferView.byteStride = pAttribute->byte_stride(); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4990 | decodedBufferView.target = primitive->indices >= 0 |
| 4991 | ? TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER |
| 4992 | : TINYGLTF_TARGET_ARRAY_BUFFER; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4993 | model->bufferViews.emplace_back(std::move(decodedBufferView)); |
| 4994 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4995 | model->accessors[primitiveAttribute->second].bufferView = |
| 4996 | int(model->bufferViews.size() - 1); |
| 4997 | model->accessors[primitiveAttribute->second].count = |
| 4998 | int(mesh->num_points()); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4999 | } |
| 5000 | |
| 5001 | return true; |
| 5002 | } |
| 5003 | #endif |
| 5004 | |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 5005 | static bool ParsePrimitive(Primitive *primitive, Model *model, |
| 5006 | std::string *err, std::string *warn, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5007 | const detail::json &o, |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 5008 | bool store_original_json_for_extras_and_extensions, |
| 5009 | ParseStrictness strictness) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5010 | int material = -1; |
| 5011 | ParseIntegerProperty(&material, err, o, "material", false); |
| 5012 | primitive->material = material; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5013 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5014 | int mode = TINYGLTF_MODE_TRIANGLES; |
| 5015 | ParseIntegerProperty(&mode, err, o, "mode", false); |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 5016 | primitive->mode = mode; // Why only triangles were supported ? |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5017 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5018 | int indices = -1; |
| 5019 | ParseIntegerProperty(&indices, err, o, "indices", false); |
| 5020 | primitive->indices = indices; |
| 5021 | if (!ParseStringIntegerProperty(&primitive->attributes, err, o, "attributes", |
| 5022 | true, "Primitive")) { |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 5023 | return false; |
| 5024 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5025 | |
Aurélien Chatelain | 2682c6f | 2017-06-14 09:46:57 +0000 | [diff] [blame] | 5026 | // Look for morph targets |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5027 | detail::json_const_iterator targetsObject; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5028 | if (detail::FindMember(o, "targets", targetsObject) && |
| 5029 | detail::IsArray(detail::GetValue(targetsObject))) { |
| 5030 | auto targetsObjectEnd = detail::ArrayEnd(detail::GetValue(targetsObject)); |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5031 | for (detail::json_const_array_iterator i = |
| 5032 | detail::ArrayBegin(detail::GetValue(targetsObject)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5033 | i != targetsObjectEnd; ++i) { |
Aurélien Chatelain | 2682c6f | 2017-06-14 09:46:57 +0000 | [diff] [blame] | 5034 | std::map<std::string, int> targetAttribues; |
| 5035 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5036 | const detail::json &dict = *i; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5037 | if (detail::IsObject(dict)) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5038 | detail::json_const_iterator dictIt(detail::ObjectBegin(dict)); |
| 5039 | detail::json_const_iterator dictItEnd(detail::ObjectEnd(dict)); |
Aurélien Chatelain | 2682c6f | 2017-06-14 09:46:57 +0000 | [diff] [blame] | 5040 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5041 | for (; dictIt != dictItEnd; ++dictIt) { |
| 5042 | int iVal; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5043 | if (detail::GetInt(detail::GetValue(dictIt), iVal)) |
| 5044 | targetAttribues[detail::GetKey(dictIt)] = iVal; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5045 | } |
| 5046 | primitive->targets.emplace_back(std::move(targetAttribues)); |
Aurélien Chatelain | 2682c6f | 2017-06-14 09:46:57 +0000 | [diff] [blame] | 5047 | } |
Aurélien Chatelain | 2682c6f | 2017-06-14 09:46:57 +0000 | [diff] [blame] | 5048 | } |
| 5049 | } |
| 5050 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5051 | ParseExtrasAndExtensions(primitive, err, o, |
| 5052 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5053 | |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 5054 | #ifdef TINYGLTF_ENABLE_DRACO |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 5055 | auto dracoExtension = |
| 5056 | primitive->extensions.find("KHR_draco_mesh_compression"); |
| 5057 | if (dracoExtension != primitive->extensions.end()) { |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 5058 | ParseDracoExtension(primitive, model, err, warn, dracoExtension->second, strictness); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 5059 | } |
Syoyo Fujita | 7bdfed3 | 2019-03-03 17:04:49 +0900 | [diff] [blame] | 5060 | #else |
| 5061 | (void)model; |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 5062 | (void)warn; |
Nyall Dawson | 5a7b827 | 2023-09-03 09:04:56 +1000 | [diff] [blame] | 5063 | (void)strictness; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 5064 | #endif |
| 5065 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5066 | return true; |
| 5067 | } |
| 5068 | |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 5069 | static bool ParseMesh(Mesh *mesh, Model *model, |
| 5070 | std::string *err, std::string *warn, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5071 | const detail::json &o, |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 5072 | bool store_original_json_for_extras_and_extensions, |
| 5073 | ParseStrictness strictness) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5074 | ParseStringProperty(&mesh->name, err, o, "name", false); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5075 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5076 | mesh->primitives.clear(); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5077 | detail::json_const_iterator primObject; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5078 | if (detail::FindMember(o, "primitives", primObject) && |
| 5079 | detail::IsArray(detail::GetValue(primObject))) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5080 | detail::json_const_array_iterator primEnd = |
| 5081 | detail::ArrayEnd(detail::GetValue(primObject)); |
| 5082 | for (detail::json_const_array_iterator i = |
| 5083 | detail::ArrayBegin(detail::GetValue(primObject)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5084 | i != primEnd; ++i) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5085 | Primitive primitive; |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 5086 | if (ParsePrimitive(&primitive, model, err, warn, *i, |
| 5087 | store_original_json_for_extras_and_extensions, |
| 5088 | strictness)) { |
Luke San Antonio | 10a3e1d | 2016-06-15 00:05:01 -0400 | [diff] [blame] | 5089 | // Only add the primitive if the parsing succeeds. |
jrkoonce | d1e1472 | 2019-08-27 11:51:02 -0500 | [diff] [blame] | 5090 | mesh->primitives.emplace_back(std::move(primitive)); |
Luke San Antonio | 10a3e1d | 2016-06-15 00:05:01 -0400 | [diff] [blame] | 5091 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5092 | } |
| 5093 | } |
| 5094 | |
Aurélien Chatelain | 38a6a75 | 2017-05-24 09:54:55 +0000 | [diff] [blame] | 5095 | // Should probably check if has targets and if dimensions fit |
| 5096 | ParseNumberArrayProperty(&mesh->weights, err, o, "weights", false); |
| 5097 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5098 | ParseExtrasAndExtensions(mesh, err, o, |
| 5099 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5100 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5101 | return true; |
| 5102 | } |
| 5103 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5104 | static bool ParseNode(Node *node, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5105 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5106 | ParseStringProperty(&node->name, err, o, "name", false); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5107 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5108 | int skin = -1; |
| 5109 | ParseIntegerProperty(&skin, err, o, "skin", false); |
| 5110 | node->skin = skin; |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 5111 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 5112 | // Matrix and T/R/S are exclusive |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 5113 | if (!ParseNumberArrayProperty(&node->matrix, err, o, "matrix", false)) { |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 5114 | ParseNumberArrayProperty(&node->rotation, err, o, "rotation", false); |
| 5115 | ParseNumberArrayProperty(&node->scale, err, o, "scale", false); |
| 5116 | ParseNumberArrayProperty(&node->translation, err, o, "translation", false); |
| 5117 | } |
| 5118 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5119 | int camera = -1; |
| 5120 | ParseIntegerProperty(&camera, err, o, "camera", false); |
| 5121 | node->camera = camera; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 5122 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5123 | int mesh = -1; |
| 5124 | ParseIntegerProperty(&mesh, err, o, "mesh", false); |
| 5125 | node->mesh = mesh; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5126 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5127 | node->children.clear(); |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5128 | ParseIntegerArrayProperty(&node->children, err, o, "children", false); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5129 | |
Benjamin Schmithüsen | ad63bf7 | 2019-08-16 14:19:27 +0200 | [diff] [blame] | 5130 | ParseNumberArrayProperty(&node->weights, err, o, "weights", false); |
| 5131 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5132 | ParseExtrasAndExtensions(node, err, o, |
| 5133 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5134 | |
David Siegel | c164878 | 2023-05-09 01:33:57 +0200 | [diff] [blame] | 5135 | // KHR_lights_punctual: parse light source reference |
| 5136 | int light = -1; |
| 5137 | if (node->extensions.count("KHR_lights_punctual") != 0) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5138 | auto const &light_ext = node->extensions["KHR_lights_punctual"]; |
David Siegel | c164878 | 2023-05-09 01:33:57 +0200 | [diff] [blame] | 5139 | if (light_ext.Has("light")) { |
| 5140 | light = light_ext.Get("light").GetNumberAsInt(); |
| 5141 | } else { |
David Siegel | 157063f | 2023-06-06 15:31:58 +0200 | [diff] [blame] | 5142 | if (err) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5143 | *err += |
| 5144 | "Node has extension KHR_lights_punctual, but does not reference " |
| 5145 | "a light source.\n"; |
David Siegel | 157063f | 2023-06-06 15:31:58 +0200 | [diff] [blame] | 5146 | } |
David Siegel | c164878 | 2023-05-09 01:33:57 +0200 | [diff] [blame] | 5147 | return false; |
| 5148 | } |
| 5149 | } |
| 5150 | node->light = light; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 5151 | |
| 5152 | // KHR_audio: parse audio source reference |
| 5153 | int emitter = -1; |
| 5154 | if (node->extensions.count("KHR_audio") != 0) { |
| 5155 | auto const &audio_ext = node->extensions["KHR_audio"]; |
| 5156 | if (audio_ext.Has("emitter")) { |
| 5157 | emitter = audio_ext.Get("emitter").GetNumberAsInt(); |
| 5158 | } else { |
| 5159 | if (err) { |
| 5160 | *err += |
| 5161 | "Node has extension KHR_audio, but does not reference " |
| 5162 | "a audio emitter.\n"; |
| 5163 | } |
| 5164 | return false; |
| 5165 | } |
| 5166 | } |
| 5167 | node->emitter = emitter; |
Syoyo Fujita | 7a570c8 | 2023-06-19 21:52:13 +0900 | [diff] [blame] | 5168 | |
Thomas Gamper | a42263b | 2024-02-05 15:42:49 +0100 | [diff] [blame] | 5169 | node->lods.clear(); |
| 5170 | if (node->extensions.count("MSFT_lod") != 0) { |
| 5171 | auto const &msft_lod_ext = node->extensions["MSFT_lod"]; |
| 5172 | if (msft_lod_ext.Has("ids")) { |
| 5173 | auto idsArr = msft_lod_ext.Get("ids"); |
| 5174 | for (size_t i = 0; i < idsArr.ArrayLen(); ++i) { |
| 5175 | node->lods.emplace_back(idsArr.Get(i).GetNumberAsInt()); |
| 5176 | } |
| 5177 | } else { |
| 5178 | if (err) { |
| 5179 | *err += |
| 5180 | "Node has extension MSFT_lod, but does not reference " |
| 5181 | "other nodes via their ids.\n"; |
| 5182 | } |
| 5183 | return false; |
| 5184 | } |
| 5185 | } |
| 5186 | |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 5187 | return true; |
Luke San Antonio | cdf4cb7 | 2016-06-14 21:32:11 -0400 | [diff] [blame] | 5188 | } |
| 5189 | |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 5190 | static bool ParseScene(Scene *scene, std::string *err, const detail::json &o, |
| 5191 | bool store_original_json_for_extras_and_extensions) { |
| 5192 | ParseStringProperty(&scene->name, err, o, "name", false); |
| 5193 | ParseIntegerArrayProperty(&scene->nodes, err, o, "nodes", false); |
| 5194 | |
| 5195 | ParseExtrasAndExtensions(scene, err, o, |
| 5196 | store_original_json_for_extras_and_extensions); |
| 5197 | |
| 5198 | // Parse KHR_audio global emitters |
| 5199 | if (scene->extensions.count("KHR_audio") != 0) { |
| 5200 | auto const &audio_ext = scene->extensions["KHR_audio"]; |
| 5201 | if (audio_ext.Has("emitters")) { |
| 5202 | auto emittersArr = audio_ext.Get("emitters"); |
| 5203 | for (size_t i = 0; i < emittersArr.ArrayLen(); ++i) { |
| 5204 | scene->audioEmitters.emplace_back(emittersArr.Get(i).GetNumberAsInt()); |
| 5205 | } |
| 5206 | } else { |
| 5207 | if (err) { |
| 5208 | *err += |
| 5209 | "Node has extension KHR_audio, but does not reference " |
| 5210 | "a audio emitter.\n"; |
| 5211 | } |
| 5212 | return false; |
| 5213 | } |
| 5214 | } |
| 5215 | |
| 5216 | return true; |
| 5217 | } |
| 5218 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5219 | static bool ParsePbrMetallicRoughness( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5220 | PbrMetallicRoughness *pbr, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5221 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5222 | if (pbr == nullptr) { |
| 5223 | return false; |
| 5224 | } |
| 5225 | |
| 5226 | std::vector<double> baseColorFactor; |
| 5227 | if (ParseNumberArrayProperty(&baseColorFactor, err, o, "baseColorFactor", |
| 5228 | /* required */ false)) { |
| 5229 | if (baseColorFactor.size() != 4) { |
| 5230 | if (err) { |
| 5231 | (*err) += |
| 5232 | "Array length of `baseColorFactor` parameter in " |
| 5233 | "pbrMetallicRoughness must be 4, but got " + |
| 5234 | std::to_string(baseColorFactor.size()) + "\n"; |
| 5235 | } |
| 5236 | return false; |
| 5237 | } |
Selmar Kok | c3353e1 | 2019-10-18 18:22:35 +0200 | [diff] [blame] | 5238 | pbr->baseColorFactor = baseColorFactor; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5239 | } |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5240 | |
| 5241 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5242 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5243 | if (detail::FindMember(o, "baseColorTexture", it)) { |
| 5244 | ParseTextureInfo(&pbr->baseColorTexture, err, detail::GetValue(it), |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5245 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5246 | } |
| 5247 | } |
| 5248 | |
| 5249 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5250 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5251 | if (detail::FindMember(o, "metallicRoughnessTexture", it)) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5252 | ParseTextureInfo(&pbr->metallicRoughnessTexture, err, |
| 5253 | detail::GetValue(it), |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5254 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5255 | } |
| 5256 | } |
| 5257 | |
| 5258 | ParseNumberProperty(&pbr->metallicFactor, err, o, "metallicFactor", false); |
| 5259 | ParseNumberProperty(&pbr->roughnessFactor, err, o, "roughnessFactor", false); |
| 5260 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5261 | ParseExtrasAndExtensions(pbr, err, o, |
| 5262 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5263 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5264 | return true; |
| 5265 | } |
| 5266 | |
Nyall Dawson | 02e8b8d | 2023-08-24 10:25:20 +1000 | [diff] [blame] | 5267 | static bool ParseMaterial(Material *material, std::string *err, std::string *warn, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5268 | const detail::json &o, |
Nyall Dawson | 8c85d5e | 2023-08-28 12:56:09 +1000 | [diff] [blame] | 5269 | bool store_original_json_for_extras_and_extensions, |
| 5270 | ParseStrictness strictness) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5271 | ParseStringProperty(&material->name, err, o, "name", /* required */ false); |
| 5272 | |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 5273 | if (ParseNumberArrayProperty(&material->emissiveFactor, err, o, |
| 5274 | "emissiveFactor", |
| 5275 | /* required */ false)) { |
Nyall Dawson | bbc1eae | 2023-09-03 09:05:32 +1000 | [diff] [blame] | 5276 | if (strictness==ParseStrictness::Permissive && material->emissiveFactor.size() == 4) { |
Nyall Dawson | 02e8b8d | 2023-08-24 10:25:20 +1000 | [diff] [blame] | 5277 | if (warn) { |
| 5278 | (*warn) += |
| 5279 | "Array length of `emissiveFactor` parameter in " |
| 5280 | "material must be 3, but got 4\n"; |
| 5281 | } |
| 5282 | material->emissiveFactor.resize(3); |
| 5283 | } |
| 5284 | else if (material->emissiveFactor.size() != 3) { |
Selmar Kok | 6df800d | 2019-08-19 11:05:28 +0200 | [diff] [blame] | 5285 | if (err) { |
| 5286 | (*err) += |
| 5287 | "Array length of `emissiveFactor` parameter in " |
| 5288 | "material must be 3, but got " + |
| 5289 | std::to_string(material->emissiveFactor.size()) + "\n"; |
| 5290 | } |
| 5291 | return false; |
| 5292 | } |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 5293 | } else { |
Selmar Kok | 6df800d | 2019-08-19 11:05:28 +0200 | [diff] [blame] | 5294 | // fill with default values |
Selmar Kok | 6dba6c6 | 2019-08-19 11:23:31 +0200 | [diff] [blame] | 5295 | material->emissiveFactor = {0.0, 0.0, 0.0}; |
Selmar Kok | 6df800d | 2019-08-19 11:05:28 +0200 | [diff] [blame] | 5296 | } |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5297 | |
| 5298 | ParseStringProperty(&material->alphaMode, err, o, "alphaMode", |
| 5299 | /* required */ false); |
| 5300 | ParseNumberProperty(&material->alphaCutoff, err, o, "alphaCutoff", |
| 5301 | /* required */ false); |
| 5302 | ParseBooleanProperty(&material->doubleSided, err, o, "doubleSided", |
| 5303 | /* required */ false); |
| 5304 | |
| 5305 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5306 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5307 | if (detail::FindMember(o, "pbrMetallicRoughness", it)) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5308 | ParsePbrMetallicRoughness(&material->pbrMetallicRoughness, err, |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5309 | detail::GetValue(it), |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5310 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5311 | } |
| 5312 | } |
| 5313 | |
| 5314 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5315 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5316 | if (detail::FindMember(o, "normalTexture", it)) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5317 | ParseNormalTextureInfo(&material->normalTexture, err, |
| 5318 | detail::GetValue(it), |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5319 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5320 | } |
| 5321 | } |
| 5322 | |
| 5323 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5324 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5325 | if (detail::FindMember(o, "occlusionTexture", it)) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5326 | ParseOcclusionTextureInfo(&material->occlusionTexture, err, |
| 5327 | detail::GetValue(it), |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5328 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5329 | } |
| 5330 | } |
| 5331 | |
| 5332 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5333 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5334 | if (detail::FindMember(o, "emissiveTexture", it)) { |
| 5335 | ParseTextureInfo(&material->emissiveTexture, err, detail::GetValue(it), |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5336 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5337 | } |
| 5338 | } |
| 5339 | |
| 5340 | // Old code path. For backward compatibility, we still store material values |
| 5341 | // as Parameter. This will create duplicated information for |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 5342 | // example(pbrMetallicRoughness), but should be negligible in terms of memory |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5343 | // consumption. |
| 5344 | // TODO(syoyo): Remove in the next major release. |
| 5345 | material->values.clear(); |
| 5346 | material->additionalValues.clear(); |
Luke San Antonio | cdf4cb7 | 2016-06-14 21:32:11 -0400 | [diff] [blame] | 5347 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5348 | detail::json_const_iterator it(detail::ObjectBegin(o)); |
| 5349 | detail::json_const_iterator itEnd(detail::ObjectEnd(o)); |
Luke San Antonio | cdf4cb7 | 2016-06-14 21:32:11 -0400 | [diff] [blame] | 5350 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5351 | for (; it != itEnd; ++it) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5352 | std::string key(detail::GetKey(it)); |
jrkoonce | 06c30c4 | 2019-09-03 15:56:48 -0500 | [diff] [blame] | 5353 | if (key == "pbrMetallicRoughness") { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5354 | if (detail::IsObject(detail::GetValue(it))) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5355 | const detail::json &values_object = detail::GetValue(it); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5356 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5357 | detail::json_const_iterator itVal(detail::ObjectBegin(values_object)); |
| 5358 | detail::json_const_iterator itValEnd(detail::ObjectEnd(values_object)); |
jrkoonce | 06c30c4 | 2019-09-03 15:56:48 -0500 | [diff] [blame] | 5359 | |
| 5360 | for (; itVal != itValEnd; ++itVal) { |
| 5361 | Parameter param; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5362 | if (ParseParameterProperty(¶m, err, values_object, |
| 5363 | detail::GetKey(itVal), false)) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5364 | material->values.emplace(detail::GetKey(itVal), std::move(param)); |
jrkoonce | 06c30c4 | 2019-09-03 15:56:48 -0500 | [diff] [blame] | 5365 | } |
| 5366 | } |
| 5367 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5368 | } else if (key == "extensions" || key == "extras") { |
| 5369 | // done later, skip, otherwise poorly parsed contents will be saved in the |
| 5370 | // parametermap and serialized again later |
| 5371 | } else { |
jrkoonce | 06c30c4 | 2019-09-03 15:56:48 -0500 | [diff] [blame] | 5372 | Parameter param; |
| 5373 | if (ParseParameterProperty(¶m, err, o, key, false)) { |
| 5374 | // names of materials have already been parsed. Putting it in this map |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 5375 | // doesn't correctly reflect the glTF specification |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5376 | if (key != "name") |
| 5377 | material->additionalValues.emplace(std::move(key), std::move(param)); |
jrkoonce | 06c30c4 | 2019-09-03 15:56:48 -0500 | [diff] [blame] | 5378 | } |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 5379 | } |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 5380 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5381 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5382 | material->extensions.clear(); // Note(agnat): Why? |
| 5383 | ParseExtrasAndExtensions(material, err, o, |
| 5384 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5385 | |
Thomas Gamper | a42263b | 2024-02-05 15:42:49 +0100 | [diff] [blame] | 5386 | material->lods.clear(); |
| 5387 | if (material->extensions.count("MSFT_lod") != 0) { |
| 5388 | auto const &msft_lod_ext = material->extensions["MSFT_lod"]; |
| 5389 | if (msft_lod_ext.Has("ids")) { |
| 5390 | auto idsArr = msft_lod_ext.Get("ids"); |
| 5391 | for (size_t i = 0; i < idsArr.ArrayLen(); ++i) { |
| 5392 | material->lods.emplace_back(idsArr.Get(i).GetNumberAsInt()); |
| 5393 | } |
| 5394 | } else { |
| 5395 | if (err) { |
| 5396 | *err += |
| 5397 | "Material has extension MSFT_lod, but does not reference " |
| 5398 | "other materials via their ids.\n"; |
| 5399 | } |
| 5400 | return false; |
| 5401 | } |
| 5402 | } |
| 5403 | |
Luke San Antonio | cdf4cb7 | 2016-06-14 21:32:11 -0400 | [diff] [blame] | 5404 | return true; |
| 5405 | } |
| 5406 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5407 | static bool ParseAnimationChannel( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5408 | AnimationChannel *channel, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5409 | bool store_original_json_for_extras_and_extensions) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5410 | int samplerIndex = -1; |
| 5411 | int targetIndex = -1; |
| 5412 | if (!ParseIntegerProperty(&samplerIndex, err, o, "sampler", true, |
| 5413 | "AnimationChannel")) { |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5414 | if (err) { |
| 5415 | (*err) += "`sampler` field is missing in animation channels\n"; |
| 5416 | } |
| 5417 | return false; |
| 5418 | } |
| 5419 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5420 | detail::json_const_iterator targetIt; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5421 | if (detail::FindMember(o, "target", targetIt) && |
| 5422 | detail::IsObject(detail::GetValue(targetIt))) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5423 | const detail::json &target_object = detail::GetValue(targetIt); |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5424 | |
Jack Mousseau | 283b552 | 2023-01-15 11:45:45 -0800 | [diff] [blame] | 5425 | ParseIntegerProperty(&targetIndex, err, target_object, "node", false); |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5426 | |
| 5427 | if (!ParseStringProperty(&channel->target_path, err, target_object, "path", |
| 5428 | true)) { |
| 5429 | if (err) { |
| 5430 | (*err) += "`path` field is missing in animation.channels.target\n"; |
| 5431 | } |
| 5432 | return false; |
| 5433 | } |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 5434 | ParseExtensionsProperty(&channel->target_extensions, err, target_object); |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 5435 | ParseExtrasProperty(&channel->target_extras, target_object); |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 5436 | if (store_original_json_for_extras_and_extensions) { |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 5437 | { |
| 5438 | detail::json_const_iterator it; |
| 5439 | if (detail::FindMember(target_object, "extensions", it)) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5440 | channel->target_extensions_json_string = |
| 5441 | detail::JsonToString(detail::GetValue(it)); |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 5442 | } |
| 5443 | } |
| 5444 | { |
| 5445 | detail::json_const_iterator it; |
| 5446 | if (detail::FindMember(target_object, "extras", it)) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5447 | channel->target_extras_json_string = |
| 5448 | detail::JsonToString(detail::GetValue(it)); |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 5449 | } |
Selmar Kok | 973d9b3 | 2020-01-21 18:45:24 +0100 | [diff] [blame] | 5450 | } |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 5451 | } |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5452 | } |
| 5453 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5454 | channel->sampler = samplerIndex; |
| 5455 | channel->target_node = targetIndex; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 5456 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5457 | ParseExtrasAndExtensions(channel, err, o, |
| 5458 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5459 | |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5460 | return true; |
| 5461 | } |
| 5462 | |
| 5463 | static bool ParseAnimation(Animation *animation, std::string *err, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5464 | const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5465 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5466 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5467 | detail::json_const_iterator channelsIt; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5468 | if (detail::FindMember(o, "channels", channelsIt) && |
| 5469 | detail::IsArray(detail::GetValue(channelsIt))) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5470 | detail::json_const_array_iterator channelEnd = |
| 5471 | detail::ArrayEnd(detail::GetValue(channelsIt)); |
| 5472 | for (detail::json_const_array_iterator i = |
| 5473 | detail::ArrayBegin(detail::GetValue(channelsIt)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5474 | i != channelEnd; ++i) { |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5475 | AnimationChannel channel; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5476 | if (ParseAnimationChannel( |
| 5477 | &channel, err, *i, |
| 5478 | store_original_json_for_extras_and_extensions)) { |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5479 | // Only add the channel if the parsing succeeds. |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5480 | animation->channels.emplace_back(std::move(channel)); |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5481 | } |
| 5482 | } |
| 5483 | } |
| 5484 | } |
| 5485 | |
| 5486 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5487 | detail::json_const_iterator samplerIt; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5488 | if (detail::FindMember(o, "samplers", samplerIt) && |
| 5489 | detail::IsArray(detail::GetValue(samplerIt))) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5490 | const detail::json &sampler_array = detail::GetValue(samplerIt); |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5491 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5492 | detail::json_const_array_iterator it = detail::ArrayBegin(sampler_array); |
| 5493 | detail::json_const_array_iterator itEnd = detail::ArrayEnd(sampler_array); |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5494 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5495 | for (; it != itEnd; ++it) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5496 | const detail::json &s = *it; |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5497 | |
| 5498 | AnimationSampler sampler; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5499 | int inputIndex = -1; |
| 5500 | int outputIndex = -1; |
| 5501 | if (!ParseIntegerProperty(&inputIndex, err, s, "input", true)) { |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5502 | if (err) { |
| 5503 | (*err) += "`input` field is missing in animation.sampler\n"; |
| 5504 | } |
| 5505 | return false; |
| 5506 | } |
Evan Birenbaum | 6bdffed | 2019-02-14 13:30:57 -0800 | [diff] [blame] | 5507 | ParseStringProperty(&sampler.interpolation, err, s, "interpolation", |
| 5508 | false); |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5509 | if (!ParseIntegerProperty(&outputIndex, err, s, "output", true)) { |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5510 | if (err) { |
| 5511 | (*err) += "`output` field is missing in animation.sampler\n"; |
| 5512 | } |
| 5513 | return false; |
| 5514 | } |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5515 | sampler.input = inputIndex; |
| 5516 | sampler.output = outputIndex; |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 5517 | ParseExtrasAndExtensions(&sampler, err, o, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5518 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5519 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5520 | animation->samplers.emplace_back(std::move(sampler)); |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5521 | } |
| 5522 | } |
| 5523 | } |
| 5524 | |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5525 | ParseStringProperty(&animation->name, err, o, "name", false); |
| 5526 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5527 | ParseExtrasAndExtensions(animation, err, o, |
| 5528 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5529 | |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5530 | return true; |
| 5531 | } |
| 5532 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5533 | static bool ParseSampler(Sampler *sampler, std::string *err, |
| 5534 | const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5535 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 5536 | ParseStringProperty(&sampler->name, err, o, "name", false); |
| 5537 | |
Syoyo Fujita | ee179b2 | 2019-08-16 13:11:30 +0900 | [diff] [blame] | 5538 | int minFilter = -1; |
| 5539 | int magFilter = -1; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5540 | int wrapS = TINYGLTF_TEXTURE_WRAP_REPEAT; |
| 5541 | int wrapT = TINYGLTF_TEXTURE_WRAP_REPEAT; |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 5542 | // int wrapR = TINYGLTF_TEXTURE_WRAP_REPEAT; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5543 | ParseIntegerProperty(&minFilter, err, o, "minFilter", false); |
| 5544 | ParseIntegerProperty(&magFilter, err, o, "magFilter", false); |
| 5545 | ParseIntegerProperty(&wrapS, err, o, "wrapS", false); |
| 5546 | ParseIntegerProperty(&wrapT, err, o, "wrapT", false); |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 5547 | // ParseIntegerProperty(&wrapR, err, o, "wrapR", false); // tinygltf |
| 5548 | // extension |
Syoyo Fujita | ee179b2 | 2019-08-16 13:11:30 +0900 | [diff] [blame] | 5549 | |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 5550 | // TODO(syoyo): Check the value is allowed one. |
Syoyo Fujita | ee179b2 | 2019-08-16 13:11:30 +0900 | [diff] [blame] | 5551 | // (e.g. we allow 9728(NEAREST), but don't allow 9727) |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 5552 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5553 | sampler->minFilter = minFilter; |
| 5554 | sampler->magFilter = magFilter; |
| 5555 | sampler->wrapS = wrapS; |
| 5556 | sampler->wrapT = wrapT; |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 5557 | // sampler->wrapR = wrapR; |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 5558 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5559 | ParseExtrasAndExtensions(sampler, err, o, |
| 5560 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5561 | |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 5562 | return true; |
| 5563 | } |
| 5564 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5565 | static bool ParseSkin(Skin *skin, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5566 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 90e1ed2 | 2017-05-30 00:47:37 +0900 | [diff] [blame] | 5567 | ParseStringProperty(&skin->name, err, o, "name", false, "Skin"); |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 5568 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5569 | std::vector<int> joints; |
| 5570 | if (!ParseIntegerArrayProperty(&joints, err, o, "joints", false, "Skin")) { |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 5571 | return false; |
| 5572 | } |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5573 | skin->joints = std::move(joints); |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 5574 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5575 | int skeleton = -1; |
| 5576 | ParseIntegerProperty(&skeleton, err, o, "skeleton", false, "Skin"); |
| 5577 | skin->skeleton = skeleton; |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 5578 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5579 | int invBind = -1; |
| 5580 | ParseIntegerProperty(&invBind, err, o, "inverseBindMatrices", true, "Skin"); |
| 5581 | skin->inverseBindMatrices = invBind; |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 5582 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5583 | ParseExtrasAndExtensions(skin, err, o, |
| 5584 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5585 | |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 5586 | return true; |
| 5587 | } |
| 5588 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5589 | static bool ParsePerspectiveCamera( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5590 | PerspectiveCamera *camera, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5591 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5592 | double yfov = 0.0; |
| 5593 | if (!ParseNumberProperty(&yfov, err, o, "yfov", true, "OrthographicCamera")) { |
| 5594 | return false; |
| 5595 | } |
| 5596 | |
| 5597 | double znear = 0.0; |
| 5598 | if (!ParseNumberProperty(&znear, err, o, "znear", true, |
| 5599 | "PerspectiveCamera")) { |
| 5600 | return false; |
| 5601 | } |
| 5602 | |
| 5603 | double aspectRatio = 0.0; // = invalid |
| 5604 | ParseNumberProperty(&aspectRatio, err, o, "aspectRatio", false, |
| 5605 | "PerspectiveCamera"); |
| 5606 | |
| 5607 | double zfar = 0.0; // = invalid |
| 5608 | ParseNumberProperty(&zfar, err, o, "zfar", false, "PerspectiveCamera"); |
| 5609 | |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 5610 | camera->aspectRatio = aspectRatio; |
| 5611 | camera->zfar = zfar; |
| 5612 | camera->yfov = yfov; |
| 5613 | camera->znear = znear; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5614 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5615 | ParseExtrasAndExtensions(camera, err, o, |
| 5616 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5617 | |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5618 | // TODO(syoyo): Validate parameter values. |
| 5619 | |
| 5620 | return true; |
| 5621 | } |
| 5622 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5623 | static bool ParseSpotLight(SpotLight *light, std::string *err, |
| 5624 | const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5625 | bool store_original_json_for_extras_and_extensions) { |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5626 | ParseNumberProperty(&light->innerConeAngle, err, o, "innerConeAngle", false); |
| 5627 | ParseNumberProperty(&light->outerConeAngle, err, o, "outerConeAngle", false); |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 5628 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5629 | ParseExtrasAndExtensions(light, err, o, |
| 5630 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5631 | |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5632 | // TODO(syoyo): Validate parameter values. |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 5633 | |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5634 | return true; |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 5635 | } |
| 5636 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5637 | static bool ParseOrthographicCamera( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5638 | OrthographicCamera *camera, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5639 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5640 | double xmag = 0.0; |
| 5641 | if (!ParseNumberProperty(&xmag, err, o, "xmag", true, "OrthographicCamera")) { |
| 5642 | return false; |
| 5643 | } |
| 5644 | |
| 5645 | double ymag = 0.0; |
| 5646 | if (!ParseNumberProperty(&ymag, err, o, "ymag", true, "OrthographicCamera")) { |
| 5647 | return false; |
| 5648 | } |
| 5649 | |
| 5650 | double zfar = 0.0; |
| 5651 | if (!ParseNumberProperty(&zfar, err, o, "zfar", true, "OrthographicCamera")) { |
| 5652 | return false; |
| 5653 | } |
| 5654 | |
| 5655 | double znear = 0.0; |
| 5656 | if (!ParseNumberProperty(&znear, err, o, "znear", true, |
| 5657 | "OrthographicCamera")) { |
| 5658 | return false; |
| 5659 | } |
| 5660 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5661 | ParseExtrasAndExtensions(camera, err, o, |
| 5662 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5663 | |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 5664 | camera->xmag = xmag; |
| 5665 | camera->ymag = ymag; |
| 5666 | camera->zfar = zfar; |
| 5667 | camera->znear = znear; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5668 | |
| 5669 | // TODO(syoyo): Validate parameter values. |
| 5670 | |
| 5671 | return true; |
| 5672 | } |
| 5673 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5674 | static bool ParseCamera(Camera *camera, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5675 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5676 | if (!ParseStringProperty(&camera->type, err, o, "type", true, "Camera")) { |
| 5677 | return false; |
| 5678 | } |
| 5679 | |
| 5680 | if (camera->type.compare("orthographic") == 0) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5681 | detail::json_const_iterator orthoIt; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5682 | if (!detail::FindMember(o, "orthographic", orthoIt)) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5683 | if (err) { |
| 5684 | std::stringstream ss; |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 5685 | ss << "Orthographic camera description not found." << std::endl; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5686 | (*err) += ss.str(); |
| 5687 | } |
| 5688 | return false; |
| 5689 | } |
| 5690 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5691 | const detail::json &v = detail::GetValue(orthoIt); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5692 | if (!detail::IsObject(v)) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5693 | if (err) { |
| 5694 | std::stringstream ss; |
| 5695 | ss << "\"orthographic\" is not a JSON object." << std::endl; |
| 5696 | (*err) += ss.str(); |
| 5697 | } |
| 5698 | return false; |
| 5699 | } |
| 5700 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5701 | if (!ParseOrthographicCamera( |
| 5702 | &camera->orthographic, err, v, |
| 5703 | store_original_json_for_extras_and_extensions)) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5704 | return false; |
| 5705 | } |
| 5706 | } else if (camera->type.compare("perspective") == 0) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5707 | detail::json_const_iterator perspIt; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5708 | if (!detail::FindMember(o, "perspective", perspIt)) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5709 | if (err) { |
| 5710 | std::stringstream ss; |
| 5711 | ss << "Perspective camera description not found." << std::endl; |
| 5712 | (*err) += ss.str(); |
| 5713 | } |
| 5714 | return false; |
| 5715 | } |
| 5716 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5717 | const detail::json &v = detail::GetValue(perspIt); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5718 | if (!detail::IsObject(v)) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5719 | if (err) { |
| 5720 | std::stringstream ss; |
| 5721 | ss << "\"perspective\" is not a JSON object." << std::endl; |
| 5722 | (*err) += ss.str(); |
| 5723 | } |
| 5724 | return false; |
| 5725 | } |
| 5726 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5727 | if (!ParsePerspectiveCamera( |
| 5728 | &camera->perspective, err, v, |
| 5729 | store_original_json_for_extras_and_extensions)) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5730 | return false; |
| 5731 | } |
| 5732 | } else { |
| 5733 | if (err) { |
| 5734 | std::stringstream ss; |
| 5735 | ss << "Invalid camera type: \"" << camera->type |
| 5736 | << "\". Must be \"perspective\" or \"orthographic\"" << std::endl; |
| 5737 | (*err) += ss.str(); |
| 5738 | } |
| 5739 | return false; |
| 5740 | } |
| 5741 | |
| 5742 | ParseStringProperty(&camera->name, err, o, "name", false); |
| 5743 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5744 | ParseExtrasAndExtensions(camera, err, o, |
| 5745 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5746 | |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5747 | return true; |
| 5748 | } |
| 5749 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5750 | static bool ParseLight(Light *light, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5751 | bool store_original_json_for_extras_and_extensions) { |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5752 | if (!ParseStringProperty(&light->type, err, o, "type", true)) { |
| 5753 | return false; |
| 5754 | } |
| 5755 | |
| 5756 | if (light->type == "spot") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5757 | detail::json_const_iterator spotIt; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5758 | if (!detail::FindMember(o, "spot", spotIt)) { |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5759 | if (err) { |
| 5760 | std::stringstream ss; |
| 5761 | ss << "Spot light description not found." << std::endl; |
| 5762 | (*err) += ss.str(); |
| 5763 | } |
| 5764 | return false; |
Benjamin Schmithüsen | 4557b6a | 2019-07-09 16:55:55 +0200 | [diff] [blame] | 5765 | } |
| 5766 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5767 | const detail::json &v = detail::GetValue(spotIt); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5768 | if (!detail::IsObject(v)) { |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5769 | if (err) { |
| 5770 | std::stringstream ss; |
| 5771 | ss << "\"spot\" is not a JSON object." << std::endl; |
| 5772 | (*err) += ss.str(); |
| 5773 | } |
| 5774 | return false; |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 5775 | } |
| 5776 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5777 | if (!ParseSpotLight(&light->spot, err, v, |
| 5778 | store_original_json_for_extras_and_extensions)) { |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5779 | return false; |
| 5780 | } |
| 5781 | } |
| 5782 | |
| 5783 | ParseStringProperty(&light->name, err, o, "name", false); |
| 5784 | ParseNumberArrayProperty(&light->color, err, o, "color", false); |
| 5785 | ParseNumberProperty(&light->range, err, o, "range", false); |
| 5786 | ParseNumberProperty(&light->intensity, err, o, "intensity", false); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5787 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5788 | ParseExtrasAndExtensions(light, err, o, |
| 5789 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5790 | |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5791 | return true; |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 5792 | } |
| 5793 | |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 5794 | static bool ParsePositionalEmitter( |
| 5795 | PositionalEmitter *positional, std::string *err, const detail::json &o, |
| 5796 | bool store_original_json_for_extras_and_extensions) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5797 | ParseNumberProperty(&positional->coneInnerAngle, err, o, "coneInnerAngle", |
| 5798 | false); |
| 5799 | ParseNumberProperty(&positional->coneOuterAngle, err, o, "coneOuterAngle", |
| 5800 | false); |
| 5801 | ParseNumberProperty(&positional->coneOuterGain, err, o, "coneOuterGain", |
| 5802 | false); |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 5803 | ParseNumberProperty(&positional->maxDistance, err, o, "maxDistance", false); |
| 5804 | ParseNumberProperty(&positional->refDistance, err, o, "refDistance", false); |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5805 | ParseNumberProperty(&positional->rolloffFactor, err, o, "rolloffFactor", |
| 5806 | false); |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 5807 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5808 | ParseExtrasAndExtensions(positional, err, o, |
| 5809 | store_original_json_for_extras_and_extensions); |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 5810 | |
| 5811 | return true; |
| 5812 | } |
| 5813 | |
| 5814 | static bool ParseAudioEmitter( |
| 5815 | AudioEmitter *emitter, std::string *err, const detail::json &o, |
| 5816 | bool store_original_json_for_extras_and_extensions) { |
| 5817 | if (!ParseStringProperty(&emitter->type, err, o, "type", true)) { |
| 5818 | return false; |
| 5819 | } |
| 5820 | |
| 5821 | if (emitter->type == "positional") { |
| 5822 | detail::json_const_iterator positionalIt; |
| 5823 | if (!detail::FindMember(o, "positional", positionalIt)) { |
| 5824 | if (err) { |
| 5825 | std::stringstream ss; |
| 5826 | ss << "Positional emitter description not found." << std::endl; |
| 5827 | (*err) += ss.str(); |
| 5828 | } |
| 5829 | return false; |
| 5830 | } |
| 5831 | |
| 5832 | const detail::json &v = detail::GetValue(positionalIt); |
| 5833 | if (!detail::IsObject(v)) { |
| 5834 | if (err) { |
| 5835 | std::stringstream ss; |
| 5836 | ss << "\"positional\" is not a JSON object." << std::endl; |
| 5837 | (*err) += ss.str(); |
| 5838 | } |
| 5839 | return false; |
| 5840 | } |
| 5841 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5842 | if (!ParsePositionalEmitter( |
| 5843 | &emitter->positional, err, v, |
| 5844 | store_original_json_for_extras_and_extensions)) { |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 5845 | return false; |
| 5846 | } |
| 5847 | } |
| 5848 | |
| 5849 | ParseStringProperty(&emitter->name, err, o, "name", false); |
| 5850 | ParseNumberProperty(&emitter->gain, err, o, "gain", false); |
| 5851 | ParseBooleanProperty(&emitter->loop, err, o, "loop", false); |
| 5852 | ParseBooleanProperty(&emitter->playing, err, o, "playing", false); |
| 5853 | ParseStringProperty(&emitter->distanceModel, err, o, "distanceModel", false); |
| 5854 | ParseIntegerProperty(&emitter->source, err, o, "source", true); |
| 5855 | |
| 5856 | ParseExtrasAndExtensions(emitter, err, o, |
| 5857 | store_original_json_for_extras_and_extensions); |
| 5858 | |
| 5859 | return true; |
| 5860 | } |
| 5861 | |
| 5862 | static bool ParseAudioSource( |
| 5863 | AudioSource *source, std::string *err, const detail::json &o, |
| 5864 | bool store_original_json_for_extras_and_extensions) { |
| 5865 | ParseStringProperty(&source->name, err, o, "name", false); |
| 5866 | ParseStringProperty(&source->uri, err, o, "uri", false); |
| 5867 | |
| 5868 | if (source->uri.empty()) { |
| 5869 | ParseIntegerProperty(&source->bufferView, err, o, "bufferView", true); |
| 5870 | ParseStringProperty(&source->mimeType, err, o, "mimeType", true); |
| 5871 | } |
| 5872 | |
| 5873 | ParseExtrasAndExtensions(source, err, o, |
| 5874 | store_original_json_for_extras_and_extensions); |
| 5875 | |
| 5876 | return true; |
| 5877 | } |
| 5878 | |
David Siegel | bec8a6d | 2023-06-06 15:36:07 +0200 | [diff] [blame] | 5879 | namespace detail { |
| 5880 | |
| 5881 | template <typename Callback> |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5882 | bool ForEachInArray(const detail::json &_v, const char *member, Callback &&cb) { |
David Siegel | bec8a6d | 2023-06-06 15:36:07 +0200 | [diff] [blame] | 5883 | detail::json_const_iterator itm; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5884 | if (detail::FindMember(_v, member, itm) && |
| 5885 | detail::IsArray(detail::GetValue(itm))) { |
David Siegel | bec8a6d | 2023-06-06 15:36:07 +0200 | [diff] [blame] | 5886 | const detail::json &root = detail::GetValue(itm); |
| 5887 | auto it = detail::ArrayBegin(root); |
| 5888 | auto end = detail::ArrayEnd(root); |
| 5889 | for (; it != end; ++it) { |
| 5890 | if (!cb(*it)) return false; |
| 5891 | } |
| 5892 | } |
| 5893 | return true; |
| 5894 | }; |
| 5895 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5896 | } // end of namespace detail |
David Siegel | bec8a6d | 2023-06-06 15:36:07 +0200 | [diff] [blame] | 5897 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 5898 | bool TinyGLTF::LoadFromString(Model *model, std::string *err, std::string *warn, |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5899 | const char *json_str, |
| 5900 | unsigned int json_str_length, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 5901 | const std::string &base_dir, |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 5902 | unsigned int check_sections) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5903 | if (json_str_length < 4) { |
Syoyo Fujita | 2b0307f | 2017-08-01 18:15:52 +0900 | [diff] [blame] | 5904 | if (err) { |
| 5905 | (*err) = "JSON string too short.\n"; |
| 5906 | } |
| 5907 | return false; |
| 5908 | } |
| 5909 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5910 | detail::JsonDocument v; |
Syoyo Fujita | 17cfbcc | 2018-01-05 20:13:50 +0900 | [diff] [blame] | 5911 | |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 5912 | #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || \ |
| 5913 | defined(_CPPUNWIND)) && \ |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 5914 | !defined(TINYGLTF_NOEXCEPTION) |
Syoyo Fujita | cdf8490 | 2017-08-01 20:12:08 +0900 | [diff] [blame] | 5915 | try { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5916 | detail::JsonParse(v, json_str, json_str_length, true); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5917 | |
Syoyo Fujita | 17cfbcc | 2018-01-05 20:13:50 +0900 | [diff] [blame] | 5918 | } catch (const std::exception &e) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5919 | if (err) { |
Syoyo Fujita | cdf8490 | 2017-08-01 20:12:08 +0900 | [diff] [blame] | 5920 | (*err) = e.what(); |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5921 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5922 | return false; |
| 5923 | } |
Syoyo Fujita | 17cfbcc | 2018-01-05 20:13:50 +0900 | [diff] [blame] | 5924 | #else |
| 5925 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5926 | detail::JsonParse(v, json_str, json_str_length); |
Syoyo Fujita | 17cfbcc | 2018-01-05 20:13:50 +0900 | [diff] [blame] | 5927 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5928 | if (!detail::IsObject(v)) { |
Syoyo Fujita | 17cfbcc | 2018-01-05 20:13:50 +0900 | [diff] [blame] | 5929 | // Assume parsing was failed. |
| 5930 | if (err) { |
| 5931 | (*err) = "Failed to parse JSON object\n"; |
| 5932 | } |
| 5933 | return false; |
| 5934 | } |
| 5935 | } |
| 5936 | #endif |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5937 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5938 | if (!detail::IsObject(v)) { |
Syoyo Fujita | 2b0307f | 2017-08-01 18:15:52 +0900 | [diff] [blame] | 5939 | // root is not an object. |
| 5940 | if (err) { |
| 5941 | (*err) = "Root element is not a JSON object\n"; |
| 5942 | } |
| 5943 | return false; |
| 5944 | } |
| 5945 | |
Benjamin Schmithüsen | d02ad0d | 2019-05-02 14:44:20 +0200 | [diff] [blame] | 5946 | { |
| 5947 | bool version_found = false; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5948 | detail::json_const_iterator it; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5949 | if (detail::FindMember(v, "asset", it) && |
| 5950 | detail::IsObject(detail::GetValue(it))) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5951 | auto &itObj = detail::GetValue(it); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5952 | detail::json_const_iterator version_it; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5953 | std::string versionStr; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5954 | if (detail::FindMember(itObj, "version", version_it) && |
| 5955 | detail::GetString(detail::GetValue(version_it), versionStr)) { |
Benjamin Schmithüsen | d02ad0d | 2019-05-02 14:44:20 +0200 | [diff] [blame] | 5956 | version_found = true; |
| 5957 | } |
| 5958 | } |
| 5959 | if (version_found) { |
| 5960 | // OK |
| 5961 | } else if (check_sections & REQUIRE_VERSION) { |
| 5962 | if (err) { |
| 5963 | (*err) += "\"asset\" object not found in .gltf or not an object type\n"; |
| 5964 | } |
| 5965 | return false; |
| 5966 | } |
| 5967 | } |
| 5968 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 5969 | // scene is not mandatory. |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 5970 | // FIXME Maybe a better way to handle it than removing the code |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5971 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5972 | auto IsArrayMemberPresent = [](const detail::json &_v, |
| 5973 | const char *name) -> bool { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5974 | detail::json_const_iterator it; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5975 | return detail::FindMember(_v, name, it) && |
| 5976 | detail::IsArray(detail::GetValue(it)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5977 | }; |
| 5978 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5979 | { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5980 | if ((check_sections & REQUIRE_SCENES) && |
| 5981 | !IsArrayMemberPresent(v, "scenes")) { |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5982 | if (err) { |
| 5983 | (*err) += "\"scenes\" object not found in .gltf or not an array type\n"; |
| 5984 | } |
| 5985 | return false; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5986 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5987 | } |
| 5988 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5989 | { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5990 | if ((check_sections & REQUIRE_NODES) && !IsArrayMemberPresent(v, "nodes")) { |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5991 | if (err) { |
| 5992 | (*err) += "\"nodes\" object not found in .gltf\n"; |
| 5993 | } |
| 5994 | return false; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5995 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5996 | } |
| 5997 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5998 | { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5999 | if ((check_sections & REQUIRE_ACCESSORS) && |
| 6000 | !IsArrayMemberPresent(v, "accessors")) { |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6001 | if (err) { |
| 6002 | (*err) += "\"accessors\" object not found in .gltf\n"; |
| 6003 | } |
| 6004 | return false; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 6005 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 6006 | } |
| 6007 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6008 | { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6009 | if ((check_sections & REQUIRE_BUFFERS) && |
| 6010 | !IsArrayMemberPresent(v, "buffers")) { |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6011 | if (err) { |
| 6012 | (*err) += "\"buffers\" object not found in .gltf\n"; |
| 6013 | } |
| 6014 | return false; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 6015 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 6016 | } |
| 6017 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6018 | { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6019 | if ((check_sections & REQUIRE_BUFFER_VIEWS) && |
| 6020 | !IsArrayMemberPresent(v, "bufferViews")) { |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6021 | if (err) { |
| 6022 | (*err) += "\"bufferViews\" object not found in .gltf\n"; |
| 6023 | } |
| 6024 | return false; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 6025 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 6026 | } |
| 6027 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 6028 | model->buffers.clear(); |
| 6029 | model->bufferViews.clear(); |
| 6030 | model->accessors.clear(); |
| 6031 | model->meshes.clear(); |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 6032 | model->cameras.clear(); |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 6033 | model->nodes.clear(); |
| 6034 | model->extensionsUsed.clear(); |
Aurélien Chatelain | 756ee6b | 2017-06-19 13:52:49 +0000 | [diff] [blame] | 6035 | model->extensionsRequired.clear(); |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 6036 | model->extensions.clear(); |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 6037 | model->defaultScene = -1; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 6038 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6039 | // 1. Parse Asset |
| 6040 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6041 | detail::json_const_iterator it; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6042 | if (detail::FindMember(v, "asset", it) && |
| 6043 | detail::IsObject(detail::GetValue(it))) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6044 | const detail::json &root = detail::GetValue(it); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 6045 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6046 | ParseAsset(&model->asset, err, root, |
| 6047 | store_original_json_for_extras_and_extensions_); |
Aurélien Chatelain | 756ee6b | 2017-06-19 13:52:49 +0000 | [diff] [blame] | 6048 | } |
| 6049 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 6050 | |
David Siegel | bec8a6d | 2023-06-06 15:36:07 +0200 | [diff] [blame] | 6051 | using detail::ForEachInArray; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6052 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6053 | // 2. Parse extensionUsed |
| 6054 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6055 | ForEachInArray(v, "extensionsUsed", [&](const detail::json &o) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6056 | std::string str; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6057 | detail::GetString(o, str); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6058 | model->extensionsUsed.emplace_back(std::move(str)); |
| 6059 | return true; |
| 6060 | }); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 6061 | } |
| 6062 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6063 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6064 | ForEachInArray(v, "extensionsRequired", [&](const detail::json &o) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6065 | std::string str; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6066 | detail::GetString(o, str); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6067 | model->extensionsRequired.emplace_back(std::move(str)); |
| 6068 | return true; |
| 6069 | }); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 6070 | } |
| 6071 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6072 | // 3. Parse Buffer |
| 6073 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6074 | bool success = ForEachInArray(v, "buffers", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6075 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6076 | if (err) { |
| 6077 | (*err) += "`buffers' does not contain an JSON object."; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 6078 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6079 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6080 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6081 | Buffer buffer; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6082 | if (!ParseBuffer(&buffer, err, o, |
| 6083 | store_original_json_for_extras_and_extensions_, &fs, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6084 | &uri_cb, base_dir, max_external_file_size_, is_binary_, |
| 6085 | bin_data_, bin_size_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6086 | return false; |
| 6087 | } |
| 6088 | |
| 6089 | model->buffers.emplace_back(std::move(buffer)); |
| 6090 | return true; |
| 6091 | }); |
| 6092 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6093 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6094 | return false; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 6095 | } |
| 6096 | } |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6097 | // 4. Parse BufferView |
| 6098 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6099 | bool success = ForEachInArray(v, "bufferViews", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6100 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6101 | if (err) { |
| 6102 | (*err) += "`bufferViews' does not contain an JSON object."; |
Syoyo Fujita | 59130d1 | 2017-08-01 20:23:11 +0900 | [diff] [blame] | 6103 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6104 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6105 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6106 | BufferView bufferView; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6107 | if (!ParseBufferView(&bufferView, err, o, |
| 6108 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6109 | return false; |
| 6110 | } |
| 6111 | |
| 6112 | model->bufferViews.emplace_back(std::move(bufferView)); |
| 6113 | return true; |
| 6114 | }); |
| 6115 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6116 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6117 | return false; |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 6118 | } |
| 6119 | } |
| 6120 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6121 | // 5. Parse Accessor |
| 6122 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6123 | bool success = ForEachInArray(v, "accessors", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6124 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6125 | if (err) { |
| 6126 | (*err) += "`accessors' does not contain an JSON object."; |
Syoyo Fujita | 59130d1 | 2017-08-01 20:23:11 +0900 | [diff] [blame] | 6127 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6128 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6129 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6130 | Accessor accessor; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6131 | if (!ParseAccessor(&accessor, err, o, |
| 6132 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6133 | return false; |
| 6134 | } |
| 6135 | |
| 6136 | model->accessors.emplace_back(std::move(accessor)); |
| 6137 | return true; |
| 6138 | }); |
| 6139 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6140 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6141 | return false; |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 6142 | } |
| 6143 | } |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 6144 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6145 | // 6. Parse Mesh |
| 6146 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6147 | bool success = ForEachInArray(v, "meshes", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6148 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6149 | if (err) { |
| 6150 | (*err) += "`meshes' does not contain an JSON object."; |
Syoyo Fujita | 59130d1 | 2017-08-01 20:23:11 +0900 | [diff] [blame] | 6151 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6152 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6153 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6154 | Mesh mesh; |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 6155 | if (!ParseMesh(&mesh, model, err, warn, o, |
| 6156 | store_original_json_for_extras_and_extensions_, |
| 6157 | strictness_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6158 | return false; |
| 6159 | } |
| 6160 | |
| 6161 | model->meshes.emplace_back(std::move(mesh)); |
| 6162 | return true; |
| 6163 | }); |
| 6164 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6165 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6166 | return false; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 6167 | } |
| 6168 | } |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 6169 | |
viperscape | 9df0580 | 2018-12-05 14:11:01 -0500 | [diff] [blame] | 6170 | // Assign missing bufferView target types |
| 6171 | // - Look for missing Mesh indices |
Marcin Kacprzak | d09788d | 2020-01-02 13:00:48 +0100 | [diff] [blame] | 6172 | // - Look for missing Mesh attributes |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 6173 | for (auto &mesh : model->meshes) { |
| 6174 | for (auto &primitive : mesh.primitives) { |
| 6175 | if (primitive.indices > |
| 6176 | -1) // has indices from parsing step, must be Element Array Buffer |
viperscape | 9df0580 | 2018-12-05 14:11:01 -0500 | [diff] [blame] | 6177 | { |
Jeff McGlynn | 8915252 | 2019-04-25 16:33:56 -0700 | [diff] [blame] | 6178 | if (size_t(primitive.indices) >= model->accessors.size()) { |
| 6179 | if (err) { |
| 6180 | (*err) += "primitive indices accessor out of bounds"; |
| 6181 | } |
| 6182 | return false; |
| 6183 | } |
| 6184 | |
haroonq | 8098a9e | 2023-10-05 11:16:52 +0100 | [diff] [blame] | 6185 | const auto bufferView = |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 6186 | model->accessors[size_t(primitive.indices)].bufferView; |
haroonq | 8098a9e | 2023-10-05 11:16:52 +0100 | [diff] [blame] | 6187 | if (bufferView < 0) { |
| 6188 | // skip, bufferView could be null(-1) for certain extensions |
Syoyo Fujita | 8387fdb | 2023-12-04 22:50:49 +0900 | [diff] [blame] | 6189 | } else if (size_t(bufferView) >= model->bufferViews.size()) { |
Jeff McGlynn | 8915252 | 2019-04-25 16:33:56 -0700 | [diff] [blame] | 6190 | if (err) { |
| 6191 | (*err) += "accessor[" + std::to_string(primitive.indices) + |
| 6192 | "] invalid bufferView"; |
| 6193 | } |
| 6194 | return false; |
haroonq | 8098a9e | 2023-10-05 11:16:52 +0100 | [diff] [blame] | 6195 | } else { |
| 6196 | model->bufferViews[size_t(bufferView)].target = |
| 6197 | TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER; |
| 6198 | // we could optionally check if accessors' bufferView type is Scalar, as |
| 6199 | // it should be |
Jeff McGlynn | 8915252 | 2019-04-25 16:33:56 -0700 | [diff] [blame] | 6200 | } |
viperscape | 9df0580 | 2018-12-05 14:11:01 -0500 | [diff] [blame] | 6201 | } |
Marcin Kacprzak | d09788d | 2020-01-02 13:00:48 +0100 | [diff] [blame] | 6202 | |
| 6203 | for (auto &attribute : primitive.attributes) { |
Nirmal Patel | e413216 | 2022-09-06 09:16:31 -0700 | [diff] [blame] | 6204 | const auto accessorsIndex = size_t(attribute.second); |
| 6205 | if (accessorsIndex < model->accessors.size()) { |
| 6206 | const auto bufferView = model->accessors[accessorsIndex].bufferView; |
| 6207 | // bufferView could be null(-1) for sparse morph target |
imallett | 56e1098 | 2022-10-07 10:35:16 -0700 | [diff] [blame] | 6208 | if (bufferView >= 0 && bufferView < (int)model->bufferViews.size()) { |
Nirmal Patel | e413216 | 2022-09-06 09:16:31 -0700 | [diff] [blame] | 6209 | model->bufferViews[size_t(bufferView)].target = |
| 6210 | TINYGLTF_TARGET_ARRAY_BUFFER; |
| 6211 | } |
| 6212 | } |
Marcin Kacprzak | d09788d | 2020-01-02 13:00:48 +0100 | [diff] [blame] | 6213 | } |
Marcin Kacprzak | c3d6716 | 2020-01-22 13:13:35 +0100 | [diff] [blame] | 6214 | |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 6215 | for (auto &target : primitive.targets) { |
| 6216 | for (auto &attribute : target) { |
Nirmal Patel | e413216 | 2022-09-06 09:16:31 -0700 | [diff] [blame] | 6217 | const auto accessorsIndex = size_t(attribute.second); |
| 6218 | if (accessorsIndex < model->accessors.size()) { |
| 6219 | const auto bufferView = model->accessors[accessorsIndex].bufferView; |
| 6220 | // bufferView could be null(-1) for sparse morph target |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6221 | if (bufferView >= 0 && |
| 6222 | bufferView < (int)model->bufferViews.size()) { |
Nirmal Patel | e413216 | 2022-09-06 09:16:31 -0700 | [diff] [blame] | 6223 | model->bufferViews[size_t(bufferView)].target = |
| 6224 | TINYGLTF_TARGET_ARRAY_BUFFER; |
| 6225 | } |
Rahul Sheth | 125e4a2 | 2020-07-13 13:56:50 -0400 | [diff] [blame] | 6226 | } |
Marcin Kacprzak | c3d6716 | 2020-01-22 13:13:35 +0100 | [diff] [blame] | 6227 | } |
| 6228 | } |
viperscape | 9df0580 | 2018-12-05 14:11:01 -0500 | [diff] [blame] | 6229 | } |
| 6230 | } |
| 6231 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6232 | // 7. Parse Node |
| 6233 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6234 | bool success = ForEachInArray(v, "nodes", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6235 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6236 | if (err) { |
| 6237 | (*err) += "`nodes' does not contain an JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6238 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6239 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6240 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6241 | Node node; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6242 | if (!ParseNode(&node, err, o, |
| 6243 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6244 | return false; |
| 6245 | } |
| 6246 | |
| 6247 | model->nodes.emplace_back(std::move(node)); |
| 6248 | return true; |
| 6249 | }); |
| 6250 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6251 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6252 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6253 | } |
| 6254 | } |
| 6255 | |
| 6256 | // 8. Parse scenes. |
| 6257 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6258 | bool success = ForEachInArray(v, "scenes", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6259 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6260 | if (err) { |
| 6261 | (*err) += "`scenes' does not contain an JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6262 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6263 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6264 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6265 | |
| 6266 | Scene scene; |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 6267 | if (!ParseScene(&scene, err, o, |
| 6268 | store_original_json_for_extras_and_extensions_)) { |
| 6269 | return false; |
| 6270 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6271 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6272 | model->scenes.emplace_back(std::move(scene)); |
| 6273 | return true; |
| 6274 | }); |
| 6275 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6276 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6277 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6278 | } |
| 6279 | } |
| 6280 | |
| 6281 | // 9. Parse default scenes. |
| 6282 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6283 | detail::json_const_iterator rootIt; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6284 | int iVal; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6285 | if (detail::FindMember(v, "scene", rootIt) && |
| 6286 | detail::GetInt(detail::GetValue(rootIt), iVal)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6287 | model->defaultScene = iVal; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6288 | } |
| 6289 | } |
| 6290 | |
| 6291 | // 10. Parse Material |
| 6292 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6293 | bool success = ForEachInArray(v, "materials", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6294 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6295 | if (err) { |
| 6296 | (*err) += "`materials' does not contain an JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6297 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6298 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6299 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6300 | Material material; |
| 6301 | ParseStringProperty(&material.name, err, o, "name", false); |
| 6302 | |
Nyall Dawson | 02e8b8d | 2023-08-24 10:25:20 +1000 | [diff] [blame] | 6303 | if (!ParseMaterial(&material, err, warn, o, |
Nyall Dawson | 8c85d5e | 2023-08-28 12:56:09 +1000 | [diff] [blame] | 6304 | store_original_json_for_extras_and_extensions_, |
| 6305 | strictness_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6306 | return false; |
| 6307 | } |
| 6308 | |
| 6309 | model->materials.emplace_back(std::move(material)); |
| 6310 | return true; |
| 6311 | }); |
| 6312 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6313 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6314 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6315 | } |
| 6316 | } |
| 6317 | |
| 6318 | // 11. Parse Image |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 6319 | void *load_image_user_data{nullptr}; |
| 6320 | |
| 6321 | LoadImageDataOption load_image_option; |
| 6322 | |
| 6323 | if (user_image_loader_) { |
| 6324 | // Use user supplied pointer |
| 6325 | load_image_user_data = load_image_user_data_; |
| 6326 | } else { |
| 6327 | load_image_option.preserve_channels = preserve_image_channels_; |
| 6328 | load_image_user_data = reinterpret_cast<void *>(&load_image_option); |
| 6329 | } |
| 6330 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6331 | { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6332 | int idx = 0; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6333 | bool success = ForEachInArray(v, "images", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6334 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6335 | if (err) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6336 | (*err) += "image[" + std::to_string(idx) + "] is not a JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6337 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6338 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6339 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6340 | Image image; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6341 | if (!ParseImage(&image, idx, err, warn, o, |
| 6342 | store_original_json_for_extras_and_extensions_, base_dir, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6343 | max_external_file_size_, &fs, &uri_cb, |
| 6344 | &this->LoadImageData, load_image_user_data)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6345 | return false; |
| 6346 | } |
| 6347 | |
| 6348 | if (image.bufferView != -1) { |
| 6349 | // Load image from the buffer view. |
| 6350 | if (size_t(image.bufferView) >= model->bufferViews.size()) { |
| 6351 | if (err) { |
| 6352 | std::stringstream ss; |
| 6353 | ss << "image[" << idx << "] bufferView \"" << image.bufferView |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6354 | << "\" not found in the scene." << std::endl; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6355 | (*err) += ss.str(); |
| 6356 | } |
| 6357 | return false; |
| 6358 | } |
| 6359 | |
| 6360 | const BufferView &bufferView = |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6361 | model->bufferViews[size_t(image.bufferView)]; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6362 | if (size_t(bufferView.buffer) >= model->buffers.size()) { |
| 6363 | if (err) { |
| 6364 | std::stringstream ss; |
| 6365 | ss << "image[" << idx << "] buffer \"" << bufferView.buffer |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6366 | << "\" not found in the scene." << std::endl; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6367 | (*err) += ss.str(); |
| 6368 | } |
| 6369 | return false; |
| 6370 | } |
| 6371 | const Buffer &buffer = model->buffers[size_t(bufferView.buffer)]; |
| 6372 | |
| 6373 | if (*LoadImageData == nullptr) { |
| 6374 | if (err) { |
| 6375 | (*err) += "No LoadImageData callback specified.\n"; |
| 6376 | } |
| 6377 | return false; |
| 6378 | } |
| 6379 | bool ret = LoadImageData( |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6380 | &image, idx, err, warn, image.width, image.height, |
| 6381 | &buffer.data[bufferView.byteOffset], |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 6382 | static_cast<int>(bufferView.byteLength), load_image_user_data); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6383 | if (!ret) { |
| 6384 | return false; |
| 6385 | } |
| 6386 | } |
| 6387 | |
| 6388 | model->images.emplace_back(std::move(image)); |
| 6389 | ++idx; |
| 6390 | return true; |
| 6391 | }); |
| 6392 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6393 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6394 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6395 | } |
| 6396 | } |
| 6397 | |
| 6398 | // 12. Parse Texture |
| 6399 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6400 | bool success = ForEachInArray(v, "textures", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6401 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6402 | if (err) { |
| 6403 | (*err) += "`textures' does not contain an JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6404 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6405 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6406 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6407 | Texture texture; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6408 | if (!ParseTexture(&texture, err, o, |
| 6409 | store_original_json_for_extras_and_extensions_, |
| 6410 | base_dir)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6411 | return false; |
| 6412 | } |
| 6413 | |
| 6414 | model->textures.emplace_back(std::move(texture)); |
| 6415 | return true; |
| 6416 | }); |
| 6417 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6418 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6419 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6420 | } |
| 6421 | } |
| 6422 | |
| 6423 | // 13. Parse Animation |
| 6424 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6425 | bool success = ForEachInArray(v, "animations", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6426 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6427 | if (err) { |
| 6428 | (*err) += "`animations' does not contain an JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6429 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6430 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6431 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6432 | Animation animation; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6433 | if (!ParseAnimation(&animation, err, o, |
| 6434 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6435 | return false; |
| 6436 | } |
| 6437 | |
| 6438 | model->animations.emplace_back(std::move(animation)); |
| 6439 | return true; |
| 6440 | }); |
| 6441 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6442 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6443 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6444 | } |
| 6445 | } |
| 6446 | |
| 6447 | // 14. Parse Skin |
| 6448 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6449 | bool success = ForEachInArray(v, "skins", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6450 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6451 | if (err) { |
| 6452 | (*err) += "`skins' does not contain an JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6453 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6454 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6455 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6456 | Skin skin; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6457 | if (!ParseSkin(&skin, err, o, |
| 6458 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6459 | return false; |
| 6460 | } |
| 6461 | |
| 6462 | model->skins.emplace_back(std::move(skin)); |
| 6463 | return true; |
| 6464 | }); |
| 6465 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6466 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6467 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6468 | } |
| 6469 | } |
| 6470 | |
| 6471 | // 15. Parse Sampler |
| 6472 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6473 | bool success = ForEachInArray(v, "samplers", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6474 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6475 | if (err) { |
| 6476 | (*err) += "`samplers' does not contain an JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6477 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6478 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6479 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6480 | Sampler sampler; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6481 | if (!ParseSampler(&sampler, err, o, |
| 6482 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6483 | return false; |
| 6484 | } |
| 6485 | |
| 6486 | model->samplers.emplace_back(std::move(sampler)); |
| 6487 | return true; |
| 6488 | }); |
| 6489 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6490 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6491 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6492 | } |
| 6493 | } |
| 6494 | |
| 6495 | // 16. Parse Camera |
| 6496 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6497 | bool success = ForEachInArray(v, "cameras", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6498 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6499 | if (err) { |
| 6500 | (*err) += "`cameras' does not contain an JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6501 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6502 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6503 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6504 | Camera camera; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6505 | if (!ParseCamera(&camera, err, o, |
| 6506 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6507 | return false; |
| 6508 | } |
| 6509 | |
| 6510 | model->cameras.emplace_back(std::move(camera)); |
| 6511 | return true; |
| 6512 | }); |
| 6513 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6514 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6515 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6516 | } |
| 6517 | } |
| 6518 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 6519 | // 17. Parse Extras & Extensions |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6520 | ParseExtrasAndExtensions(model, err, v, |
| 6521 | store_original_json_for_extras_and_extensions_); |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 6522 | |
| 6523 | // 18. Specific extension implementations |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6524 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6525 | detail::json_const_iterator rootIt; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6526 | if (detail::FindMember(v, "extensions", rootIt) && |
| 6527 | detail::IsObject(detail::GetValue(rootIt))) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6528 | const detail::json &root = detail::GetValue(rootIt); |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6529 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6530 | detail::json_const_iterator it(detail::ObjectBegin(root)); |
| 6531 | detail::json_const_iterator itEnd(detail::ObjectEnd(root)); |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6532 | for (; it != itEnd; ++it) { |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 6533 | // parse KHR_lights_punctual extension |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6534 | std::string key(detail::GetKey(it)); |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6535 | if ((key == "KHR_lights_punctual") && |
| 6536 | detail::IsObject(detail::GetValue(it))) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6537 | const detail::json &object = detail::GetValue(it); |
| 6538 | detail::json_const_iterator itLight; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6539 | if (detail::FindMember(object, "lights", itLight)) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6540 | const detail::json &lights = detail::GetValue(itLight); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6541 | if (!detail::IsArray(lights)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6542 | continue; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6543 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6544 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6545 | auto arrayIt(detail::ArrayBegin(lights)); |
| 6546 | auto arrayItEnd(detail::ArrayEnd(lights)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6547 | for (; arrayIt != arrayItEnd; ++arrayIt) { |
| 6548 | Light light; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6549 | if (!ParseLight(&light, err, *arrayIt, |
| 6550 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6551 | return false; |
| 6552 | } |
| 6553 | model->lights.emplace_back(std::move(light)); |
| 6554 | } |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6555 | } |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 6556 | } |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 6557 | // parse KHR_audio extension |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6558 | if ((key == "KHR_audio") && detail::IsObject(detail::GetValue(it))) { |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 6559 | const detail::json &object = detail::GetValue(it); |
| 6560 | detail::json_const_iterator itKhrAudio; |
| 6561 | if (detail::FindMember(object, "emitters", itKhrAudio)) { |
| 6562 | const detail::json &emitters = detail::GetValue(itKhrAudio); |
| 6563 | if (!detail::IsArray(emitters)) { |
| 6564 | continue; |
| 6565 | } |
| 6566 | |
| 6567 | auto arrayIt(detail::ArrayBegin(emitters)); |
| 6568 | auto arrayItEnd(detail::ArrayEnd(emitters)); |
| 6569 | for (; arrayIt != arrayItEnd; ++arrayIt) { |
| 6570 | AudioEmitter emitter; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6571 | if (!ParseAudioEmitter( |
| 6572 | &emitter, err, *arrayIt, |
| 6573 | store_original_json_for_extras_and_extensions_)) { |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 6574 | return false; |
| 6575 | } |
| 6576 | model->audioEmitters.emplace_back(std::move(emitter)); |
| 6577 | } |
| 6578 | } |
| 6579 | |
| 6580 | if (detail::FindMember(object, "sources", itKhrAudio)) { |
| 6581 | const detail::json &sources = detail::GetValue(itKhrAudio); |
| 6582 | if (!detail::IsArray(sources)) { |
| 6583 | continue; |
| 6584 | } |
| 6585 | |
| 6586 | auto arrayIt(detail::ArrayBegin(sources)); |
| 6587 | auto arrayItEnd(detail::ArrayEnd(sources)); |
| 6588 | for (; arrayIt != arrayItEnd; ++arrayIt) { |
| 6589 | AudioSource source; |
| 6590 | if (!ParseAudioSource( |
| 6591 | &source, err, *arrayIt, |
| 6592 | store_original_json_for_extras_and_extensions_)) { |
| 6593 | return false; |
| 6594 | } |
| 6595 | model->audioSources.emplace_back(std::move(source)); |
| 6596 | } |
| 6597 | } |
| 6598 | } |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 6599 | } |
| 6600 | } |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 6601 | } |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6602 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 6603 | return true; |
| 6604 | } |
| 6605 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6606 | bool TinyGLTF::LoadASCIIFromString(Model *model, std::string *err, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6607 | std::string *warn, const char *str, |
| 6608 | unsigned int length, |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6609 | const std::string &base_dir, |
| 6610 | unsigned int check_sections) { |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6611 | is_binary_ = false; |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 6612 | bin_data_ = nullptr; |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6613 | bin_size_ = 0; |
| 6614 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6615 | return LoadFromString(model, err, warn, str, length, base_dir, |
| 6616 | check_sections); |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6617 | } |
| 6618 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6619 | bool TinyGLTF::LoadASCIIFromFile(Model *model, std::string *err, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6620 | std::string *warn, const std::string &filename, |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6621 | unsigned int check_sections) { |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 6622 | std::stringstream ss; |
| 6623 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 6624 | if (fs.ReadWholeFile == nullptr) { |
| 6625 | // Programmer error, assert() ? |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6626 | ss << "Failed to read file: " << filename |
| 6627 | << ": one or more FS callback not set" << std::endl; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 6628 | if (err) { |
| 6629 | (*err) = ss.str(); |
| 6630 | } |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 6631 | return false; |
| 6632 | } |
| 6633 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 6634 | std::vector<unsigned char> data; |
| 6635 | std::string fileerr; |
| 6636 | bool fileread = fs.ReadWholeFile(&data, &fileerr, filename, fs.user_data); |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6637 | if (!fileread) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 6638 | ss << "Failed to read file: " << filename << ": " << fileerr << std::endl; |
| 6639 | if (err) { |
| 6640 | (*err) = ss.str(); |
| 6641 | } |
| 6642 | return false; |
| 6643 | } |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 6644 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 6645 | size_t sz = data.size(); |
Luke San Antonio | b310b4a | 2016-06-23 14:17:37 -0400 | [diff] [blame] | 6646 | if (sz == 0) { |
| 6647 | if (err) { |
| 6648 | (*err) = "Empty file."; |
| 6649 | } |
| 6650 | return false; |
| 6651 | } |
| 6652 | |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 6653 | std::string basedir = GetBaseDir(filename); |
| 6654 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6655 | bool ret = LoadASCIIFromString( |
| 6656 | model, err, warn, reinterpret_cast<const char *>(&data.at(0)), |
| 6657 | static_cast<unsigned int>(data.size()), basedir, check_sections); |
Luke San Antonio | 9e36b61 | 2016-06-23 14:10:51 -0400 | [diff] [blame] | 6658 | |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6659 | return ret; |
| 6660 | } |
| 6661 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6662 | bool TinyGLTF::LoadBinaryFromMemory(Model *model, std::string *err, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6663 | std::string *warn, |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6664 | const unsigned char *bytes, |
| 6665 | unsigned int size, |
| 6666 | const std::string &base_dir, |
| 6667 | unsigned int check_sections) { |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6668 | if (size < 20) { |
| 6669 | if (err) { |
| 6670 | (*err) = "Too short data size for glTF Binary."; |
| 6671 | } |
| 6672 | return false; |
| 6673 | } |
| 6674 | |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 6675 | if (bytes[0] == 'g' && bytes[1] == 'l' && bytes[2] == 'T' && |
| 6676 | bytes[3] == 'F') { |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6677 | // ok |
| 6678 | } else { |
| 6679 | if (err) { |
| 6680 | (*err) = "Invalid magic."; |
| 6681 | } |
| 6682 | return false; |
| 6683 | } |
| 6684 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6685 | unsigned int version; // 4 bytes |
| 6686 | unsigned int length; // 4 bytes |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6687 | unsigned int chunk0_length; // 4 bytes |
| 6688 | unsigned int chunk0_format; // 4 bytes; |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6689 | |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 6690 | memcpy(&version, bytes + 4, 4); |
| 6691 | swap4(&version); |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6692 | memcpy(&length, bytes + 8, 4); // Total glb size, including header and all chunks. |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 6693 | swap4(&length); |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6694 | memcpy(&chunk0_length, bytes + 12, 4); // JSON data length |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6695 | swap4(&chunk0_length); |
| 6696 | memcpy(&chunk0_format, bytes + 16, 4); |
| 6697 | swap4(&chunk0_format); |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6698 | |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6699 | // https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#binary-gltf-layout |
| 6700 | // |
Syoyo Fujita | e69069d | 2018-03-15 22:01:18 -0500 | [diff] [blame] | 6701 | // In case the Bin buffer is not present, the size is exactly 20 + size of |
| 6702 | // JSON contents, |
| 6703 | // so use "greater than" operator. |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6704 | // |
| 6705 | // https://github.com/syoyo/tinygltf/issues/372 |
| 6706 | // Use 64bit uint to avoid integer overflow. |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6707 | uint64_t header_and_json_size = 20ull + uint64_t(chunk0_length); |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6708 | |
rhiskey | 8acf861 | 2023-12-04 17:11:59 +0300 | [diff] [blame] | 6709 | if (header_and_json_size > (std::numeric_limits<uint32_t>::max)()) { |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6710 | // Do not allow 4GB or more GLB data. |
Syoyo Fujita | 8387fdb | 2023-12-04 22:50:49 +0900 | [diff] [blame] | 6711 | if (err) { |
| 6712 | (*err) = "Invalid glTF binary. GLB data exceeds 4GB."; |
| 6713 | } |
| 6714 | return false; |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6715 | } |
| 6716 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6717 | if ((header_and_json_size > uint64_t(size)) || (chunk0_length < 1) || |
| 6718 | (length > size) || (header_and_json_size > uint64_t(length)) || |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6719 | (chunk0_format != 0x4E4F534A)) { // 0x4E4F534A = JSON format. |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6720 | if (err) { |
| 6721 | (*err) = "Invalid glTF binary."; |
| 6722 | } |
| 6723 | return false; |
| 6724 | } |
| 6725 | |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6726 | // Padding check |
| 6727 | // The start and the end of each chunk must be aligned to a 4-byte boundary. |
| 6728 | // No padding check for chunk0 start since its 4byte-boundary is ensured. |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6729 | if ((header_and_json_size % 4) != 0) { |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6730 | if (err) { |
| 6731 | (*err) = "JSON Chunk end does not aligned to a 4-byte boundary."; |
| 6732 | } |
Syoyo Fujita | 8387fdb | 2023-12-04 22:50:49 +0900 | [diff] [blame] | 6733 | return false; |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6734 | } |
| 6735 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6736 | // std::cout << "header_and_json_size = " << header_and_json_size << "\n"; |
| 6737 | // std::cout << "length = " << length << "\n"; |
Syoyo Fujita | 612e578 | 2022-09-18 21:01:39 +0900 | [diff] [blame] | 6738 | |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6739 | // Chunk1(BIN) data |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6740 | // The spec says: When the binary buffer is empty or when it is stored by |
| 6741 | // other means, this chunk SHOULD be omitted. So when header + JSON data == |
| 6742 | // binary size, Chunk1 is omitted. |
Syoyo Fujita | 612e578 | 2022-09-18 21:01:39 +0900 | [diff] [blame] | 6743 | if (header_and_json_size == uint64_t(length)) { |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6744 | bin_data_ = nullptr; |
| 6745 | bin_size_ = 0; |
| 6746 | } else { |
| 6747 | // Read Chunk1 info(BIN data) |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6748 | // |
| 6749 | // issue-440: |
| 6750 | // 'SHOULD' in glTF spec means 'RECOMMENDED', |
| 6751 | // So there is a situation that Chunk1(BIN) is composed of zero-sized BIN data |
| 6752 | // (chunksize(0) + binformat(BIN) = 8bytes). |
| 6753 | // |
| 6754 | if ((header_and_json_size + 8ull) > uint64_t(length)) { |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6755 | if (err) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6756 | (*err) = |
| 6757 | "Insufficient storage space for Chunk1(BIN data). At least Chunk1 " |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6758 | "Must have 8 or more bytes, but got " + |
Nyall Dawson | 8e9aadf | 2023-08-24 11:47:23 +1000 | [diff] [blame] | 6759 | std::to_string((header_and_json_size + 8ull) - uint64_t(length)) + |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6760 | ".\n"; |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6761 | } |
| 6762 | return false; |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6763 | } |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6764 | |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6765 | unsigned int chunk1_length{0}; // 4 bytes |
| 6766 | unsigned int chunk1_format{0}; // 4 bytes; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6767 | memcpy(&chunk1_length, bytes + header_and_json_size, |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6768 | 4); // Bin data length |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6769 | swap4(&chunk1_length); |
| 6770 | memcpy(&chunk1_format, bytes + header_and_json_size + 4, 4); |
| 6771 | swap4(&chunk1_format); |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6772 | |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6773 | if (chunk1_format != 0x004e4942) { |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6774 | if (err) { |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6775 | (*err) = "Invalid chunkType for Chunk1."; |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6776 | } |
| 6777 | return false; |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6778 | } |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6779 | |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6780 | if (chunk1_length == 0) { |
| 6781 | |
| 6782 | if (header_and_json_size + 8 > uint64_t(length)) { |
Nyall Dawson | 4d119d7 | 2023-09-06 17:35:25 +1000 | [diff] [blame] | 6783 | if (err) { |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6784 | (*err) = "BIN Chunk header location exceeds the GLB size."; |
Nyall Dawson | 4d119d7 | 2023-09-06 17:35:25 +1000 | [diff] [blame] | 6785 | } |
| 6786 | return false; |
| 6787 | } |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6788 | |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6789 | bin_data_ = nullptr; |
| 6790 | |
| 6791 | } else { |
| 6792 | |
| 6793 | // When BIN chunk size is not zero, at least Chunk1 should have 12 bytes(8 bytes(header) + 4 bytes(bin |
| 6794 | // payload could be 1~3 bytes, but need to be aligned to 4 bytes) |
| 6795 | |
| 6796 | if (chunk1_length < 4) { |
| 6797 | if (err) { |
| 6798 | (*err) = "Insufficient Chunk1(BIN) data size."; |
| 6799 | } |
| 6800 | return false; |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6801 | } |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6802 | |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6803 | if ((chunk1_length % 4) != 0) { |
| 6804 | if (strictness_==ParseStrictness::Permissive) { |
| 6805 | if (warn) { |
| 6806 | (*warn) += "BIN Chunk end is not aligned to a 4-byte boundary.\n"; |
| 6807 | } |
| 6808 | } |
| 6809 | else { |
| 6810 | if (err) { |
| 6811 | (*err) = "BIN Chunk end is not aligned to a 4-byte boundary."; |
| 6812 | } |
| 6813 | return false; |
| 6814 | } |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6815 | } |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6816 | |
| 6817 | // +8 chunk1 header size. |
| 6818 | if (uint64_t(chunk1_length) + header_and_json_size + 8 > uint64_t(length)) { |
| 6819 | if (err) { |
| 6820 | (*err) = "BIN Chunk data length exceeds the GLB size."; |
| 6821 | } |
| 6822 | return false; |
| 6823 | } |
| 6824 | |
| 6825 | bin_data_ = bytes + header_and_json_size + |
| 6826 | 8; // 4 bytes (bin_buffer_length) + 4 bytes(bin_buffer_format) |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6827 | } |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6828 | |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6829 | bin_size_ = size_t(chunk1_length); |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6830 | } |
| 6831 | |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6832 | is_binary_ = true; |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6833 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6834 | bool ret = LoadFromString(model, err, warn, |
| 6835 | reinterpret_cast<const char *>(&bytes[20]), |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6836 | chunk0_length, base_dir, check_sections); |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6837 | if (!ret) { |
| 6838 | return ret; |
| 6839 | } |
| 6840 | |
| 6841 | return true; |
| 6842 | } |
| 6843 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6844 | bool TinyGLTF::LoadBinaryFromFile(Model *model, std::string *err, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6845 | std::string *warn, |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6846 | const std::string &filename, |
| 6847 | unsigned int check_sections) { |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6848 | std::stringstream ss; |
| 6849 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 6850 | if (fs.ReadWholeFile == nullptr) { |
| 6851 | // Programmer error, assert() ? |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6852 | ss << "Failed to read file: " << filename |
| 6853 | << ": one or more FS callback not set" << std::endl; |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6854 | if (err) { |
| 6855 | (*err) = ss.str(); |
| 6856 | } |
| 6857 | return false; |
| 6858 | } |
| 6859 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 6860 | std::vector<unsigned char> data; |
| 6861 | std::string fileerr; |
| 6862 | bool fileread = fs.ReadWholeFile(&data, &fileerr, filename, fs.user_data); |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6863 | if (!fileread) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 6864 | ss << "Failed to read file: " << filename << ": " << fileerr << std::endl; |
| 6865 | if (err) { |
| 6866 | (*err) = ss.str(); |
| 6867 | } |
| 6868 | return false; |
| 6869 | } |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6870 | |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 6871 | std::string basedir = GetBaseDir(filename); |
| 6872 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6873 | bool ret = LoadBinaryFromMemory(model, err, warn, &data.at(0), |
| 6874 | static_cast<unsigned int>(data.size()), |
| 6875 | basedir, check_sections); |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 6876 | |
| 6877 | return ret; |
| 6878 | } |
| 6879 | |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6880 | /////////////////////// |
| 6881 | // GLTF Serialization |
| 6882 | /////////////////////// |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6883 | namespace detail { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6884 | detail::json JsonFromString(const char *s) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6885 | #ifdef TINYGLTF_USE_RAPIDJSON |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6886 | return detail::json(s, detail::GetAllocator()); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6887 | #else |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6888 | return detail::json(s); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6889 | #endif |
jrkoonce | 63419a1 | 2019-09-03 17:06:41 -0500 | [diff] [blame] | 6890 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6891 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6892 | void JsonAssign(detail::json &dest, const detail::json &src) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6893 | #ifdef TINYGLTF_USE_RAPIDJSON |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6894 | dest.CopyFrom(src, detail::GetAllocator()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6895 | #else |
| 6896 | dest = src; |
| 6897 | #endif |
| 6898 | } |
| 6899 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6900 | void JsonAddMember(detail::json &o, const char *key, detail::json &&value) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6901 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 6902 | if (!o.IsObject()) { |
| 6903 | o.SetObject(); |
| 6904 | } |
Syoyo Fujita | 147a00a | 2023-06-04 05:45:24 +0900 | [diff] [blame] | 6905 | |
| 6906 | // Issue 420. |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6907 | // AddMember may create duplicated key, so use [] API when a key already |
| 6908 | // exists. |
Syoyo Fujita | 147a00a | 2023-06-04 05:45:24 +0900 | [diff] [blame] | 6909 | // https://github.com/Tencent/rapidjson/issues/771#issuecomment-254386863 |
| 6910 | detail::json_const_iterator it; |
| 6911 | if (detail::FindMember(o, key, it)) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6912 | o[key] = std::move(value); // replace |
Syoyo Fujita | 147a00a | 2023-06-04 05:45:24 +0900 | [diff] [blame] | 6913 | } else { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6914 | o.AddMember(detail::json(key, detail::GetAllocator()), std::move(value), |
| 6915 | detail::GetAllocator()); |
Syoyo Fujita | 147a00a | 2023-06-04 05:45:24 +0900 | [diff] [blame] | 6916 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6917 | #else |
| 6918 | o[key] = std::move(value); |
| 6919 | #endif |
| 6920 | } |
| 6921 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6922 | void JsonPushBack(detail::json &o, detail::json &&value) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6923 | #ifdef TINYGLTF_USE_RAPIDJSON |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6924 | o.PushBack(std::move(value), detail::GetAllocator()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6925 | #else |
| 6926 | o.push_back(std::move(value)); |
| 6927 | #endif |
| 6928 | } |
| 6929 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6930 | bool JsonIsNull(const detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6931 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 6932 | return o.IsNull(); |
| 6933 | #else |
| 6934 | return o.is_null(); |
| 6935 | #endif |
| 6936 | } |
| 6937 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6938 | void JsonSetObject(detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6939 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 6940 | o.SetObject(); |
| 6941 | #else |
| 6942 | o = o.object({}); |
| 6943 | #endif |
| 6944 | } |
| 6945 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6946 | void JsonReserveArray(detail::json &o, size_t s) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6947 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 6948 | o.SetArray(); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6949 | o.Reserve(static_cast<rapidjson::SizeType>(s), detail::GetAllocator()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6950 | #endif |
| 6951 | (void)(o); |
| 6952 | (void)(s); |
| 6953 | } |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6954 | } // namespace detail |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6955 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6956 | // typedef std::pair<std::string, detail::json> json_object_pair; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6957 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6958 | template <typename T> |
| 6959 | static void SerializeNumberProperty(const std::string &key, T number, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6960 | detail::json &obj) { |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 6961 | // obj.insert( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6962 | // json_object_pair(key, detail::json(static_cast<double>(number)))); |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 6963 | // obj[key] = static_cast<double>(number); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6964 | detail::JsonAddMember(obj, key.c_str(), detail::json(number)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6965 | } |
| 6966 | |
Ivo van Dongen | 272a9df | 2020-05-29 11:24:11 +0300 | [diff] [blame] | 6967 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 6968 | template <> |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6969 | void SerializeNumberProperty(const std::string &key, size_t number, |
| 6970 | detail::json &obj) { |
| 6971 | detail::JsonAddMember(obj, key.c_str(), |
| 6972 | detail::json(static_cast<uint64_t>(number))); |
Ivo van Dongen | 272a9df | 2020-05-29 11:24:11 +0300 | [diff] [blame] | 6973 | } |
| 6974 | #endif |
| 6975 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6976 | template <typename T> |
| 6977 | static void SerializeNumberArrayProperty(const std::string &key, |
| 6978 | const std::vector<T> &value, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6979 | detail::json &obj) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6980 | if (value.empty()) return; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6981 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6982 | detail::json ary; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6983 | detail::JsonReserveArray(ary, value.size()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6984 | for (const auto &s : value) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6985 | detail::JsonPushBack(ary, detail::json(s)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6986 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6987 | detail::JsonAddMember(obj, key.c_str(), std::move(ary)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6988 | } |
| 6989 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6990 | static void SerializeStringProperty(const std::string &key, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6991 | const std::string &value, |
| 6992 | detail::json &obj) { |
| 6993 | detail::JsonAddMember(obj, key.c_str(), |
| 6994 | detail::JsonFromString(value.c_str())); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6995 | } |
| 6996 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6997 | static void SerializeStringArrayProperty(const std::string &key, |
| 6998 | const std::vector<std::string> &value, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6999 | detail::json &obj) { |
| 7000 | detail::json ary; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7001 | detail::JsonReserveArray(ary, value.size()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7002 | for (auto &s : value) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7003 | detail::JsonPushBack(ary, detail::JsonFromString(s.c_str())); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7004 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7005 | detail::JsonAddMember(obj, key.c_str(), std::move(ary)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7006 | } |
| 7007 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7008 | static bool ValueToJson(const Value &value, detail::json *ret) { |
| 7009 | detail::json obj; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7010 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 7011 | switch (value.Type()) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7012 | case REAL_TYPE: |
| 7013 | obj.SetDouble(value.Get<double>()); |
| 7014 | break; |
| 7015 | case INT_TYPE: |
| 7016 | obj.SetInt(value.Get<int>()); |
| 7017 | break; |
| 7018 | case BOOL_TYPE: |
| 7019 | obj.SetBool(value.Get<bool>()); |
| 7020 | break; |
| 7021 | case STRING_TYPE: |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7022 | obj.SetString(value.Get<std::string>().c_str(), detail::GetAllocator()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7023 | break; |
| 7024 | case ARRAY_TYPE: { |
| 7025 | obj.SetArray(); |
| 7026 | obj.Reserve(static_cast<rapidjson::SizeType>(value.ArrayLen()), |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7027 | detail::GetAllocator()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7028 | for (unsigned int i = 0; i < value.ArrayLen(); ++i) { |
| 7029 | Value elementValue = value.Get(int(i)); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7030 | detail::json elementJson; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7031 | if (ValueToJson(value.Get(int(i)), &elementJson)) |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7032 | obj.PushBack(std::move(elementJson), detail::GetAllocator()); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7033 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7034 | break; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7035 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7036 | case BINARY_TYPE: |
| 7037 | // TODO |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7038 | // obj = detail::json(value.Get<std::vector<unsigned char>>()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7039 | return false; |
| 7040 | break; |
| 7041 | case OBJECT_TYPE: { |
| 7042 | obj.SetObject(); |
| 7043 | Value::Object objMap = value.Get<Value::Object>(); |
| 7044 | for (auto &it : objMap) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7045 | detail::json elementJson; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7046 | if (ValueToJson(it.second, &elementJson)) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7047 | obj.AddMember(detail::json(it.first.c_str(), detail::GetAllocator()), |
| 7048 | std::move(elementJson), detail::GetAllocator()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7049 | } |
| 7050 | } |
| 7051 | break; |
| 7052 | } |
| 7053 | case NULL_TYPE: |
| 7054 | default: |
| 7055 | return false; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7056 | } |
| 7057 | #else |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7058 | switch (value.Type()) { |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 7059 | case REAL_TYPE: |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7060 | obj = detail::json(value.Get<double>()); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7061 | break; |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 7062 | case INT_TYPE: |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7063 | obj = detail::json(value.Get<int>()); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7064 | break; |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 7065 | case BOOL_TYPE: |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7066 | obj = detail::json(value.Get<bool>()); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7067 | break; |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 7068 | case STRING_TYPE: |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7069 | obj = detail::json(value.Get<std::string>()); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7070 | break; |
| 7071 | case ARRAY_TYPE: { |
James Ward | 9b4e1ea | 2024-03-25 17:50:33 +0000 | [diff] [blame] | 7072 | for (size_t i = 0; i < value.ArrayLen(); ++i) { |
| 7073 | Value elementValue = value.Get(i); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7074 | detail::json elementJson; |
James Ward | 9b4e1ea | 2024-03-25 17:50:33 +0000 | [diff] [blame] | 7075 | if (ValueToJson(value.Get(i), &elementJson)) |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 7076 | obj.push_back(elementJson); |
| 7077 | } |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7078 | break; |
| 7079 | } |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 7080 | case BINARY_TYPE: |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7081 | // TODO |
| 7082 | // obj = json(value.Get<std::vector<unsigned char>>()); |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 7083 | return false; |
| 7084 | break; |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7085 | case OBJECT_TYPE: { |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 7086 | Value::Object objMap = value.Get<Value::Object>(); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7087 | for (auto &it : objMap) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7088 | detail::json elementJson; |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7089 | if (ValueToJson(it.second, &elementJson)) obj[it.first] = elementJson; |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 7090 | } |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7091 | break; |
| 7092 | } |
| 7093 | case NULL_TYPE: |
| 7094 | default: |
| 7095 | return false; |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 7096 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7097 | #endif |
| 7098 | if (ret) *ret = std::move(obj); |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 7099 | return true; |
| 7100 | } |
| 7101 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7102 | static void SerializeValue(const std::string &key, const Value &value, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7103 | detail::json &obj) { |
| 7104 | detail::json ret; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7105 | if (ValueToJson(value, &ret)) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7106 | detail::JsonAddMember(obj, key.c_str(), std::move(ret)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7107 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7108 | } |
| 7109 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7110 | static void SerializeGltfBufferData(const std::vector<unsigned char> &data, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7111 | detail::json &o) { |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 7112 | std::string header = "data:application/octet-stream;base64,"; |
Syoyo Fujita | a11f6e1 | 2019-12-19 14:16:48 +0900 | [diff] [blame] | 7113 | if (data.size() > 0) { |
| 7114 | std::string encodedData = |
| 7115 | base64_encode(&data[0], static_cast<unsigned int>(data.size())); |
| 7116 | SerializeStringProperty("uri", header + encodedData, o); |
| 7117 | } else { |
| 7118 | // Issue #229 |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 7119 | // size 0 is allowed. Just emit mime header. |
Syoyo Fujita | a11f6e1 | 2019-12-19 14:16:48 +0900 | [diff] [blame] | 7120 | SerializeStringProperty("uri", header, o); |
| 7121 | } |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 7122 | } |
| 7123 | |
Selmar Kok | e467749 | 2018-10-25 16:45:49 +0200 | [diff] [blame] | 7124 | static bool SerializeGltfBufferData(const std::vector<unsigned char> &data, |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 7125 | const std::string &binFilename) { |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 7126 | #ifdef _WIN32 |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 7127 | #if defined(__GLIBCXX__) // mingw |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 7128 | int file_descriptor = _wopen(UTF8ToWchar(binFilename).c_str(), |
TheDev | 22dfeab | 2024-03-25 18:21:20 +0300 | [diff] [blame] | 7129 | _O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY, _S_IWRITE); |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 7130 | __gnu_cxx::stdio_filebuf<char> wfile_buf( |
| 7131 | file_descriptor, std::ios_base::out | std::ios_base::binary); |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 7132 | std::ostream output(&wfile_buf); |
| 7133 | if (!wfile_buf.is_open()) return false; |
| 7134 | #elif defined(_MSC_VER) |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 7135 | std::ofstream output(UTF8ToWchar(binFilename).c_str(), std::ofstream::binary); |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 7136 | if (!output.is_open()) return false; |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 7137 | #else |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 7138 | std::ofstream output(binFilename.c_str(), std::ofstream::binary); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 7139 | if (!output.is_open()) return false; |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 7140 | #endif |
| 7141 | #else |
| 7142 | std::ofstream output(binFilename.c_str(), std::ofstream::binary); |
| 7143 | if (!output.is_open()) return false; |
| 7144 | #endif |
Syoyo Fujita | a11f6e1 | 2019-12-19 14:16:48 +0900 | [diff] [blame] | 7145 | if (data.size() > 0) { |
| 7146 | output.write(reinterpret_cast<const char *>(&data[0]), |
| 7147 | std::streamsize(data.size())); |
| 7148 | } else { |
| 7149 | // Issue #229 |
| 7150 | // size 0 will be still valid buffer data. |
| 7151 | // write empty file. |
| 7152 | } |
Selmar Kok | e467749 | 2018-10-25 16:45:49 +0200 | [diff] [blame] | 7153 | return true; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7154 | } |
| 7155 | |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 7156 | #if 0 // FIXME(syoyo): not used. will be removed in the future release. |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7157 | static void SerializeParameterMap(ParameterMap ¶m, detail::json &o) { |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7158 | for (ParameterMap::iterator paramIt = param.begin(); paramIt != param.end(); |
| 7159 | ++paramIt) { |
| 7160 | if (paramIt->second.number_array.size()) { |
| 7161 | SerializeNumberArrayProperty<double>(paramIt->first, |
| 7162 | paramIt->second.number_array, o); |
| 7163 | } else if (paramIt->second.json_double_value.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7164 | detail::json json_double_value; |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7165 | for (std::map<std::string, double>::iterator it = |
| 7166 | paramIt->second.json_double_value.begin(); |
| 7167 | it != paramIt->second.json_double_value.end(); ++it) { |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7168 | if (it->first == "index") { |
| 7169 | json_double_value[it->first] = paramIt->second.TextureIndex(); |
| 7170 | } else { |
| 7171 | json_double_value[it->first] = it->second; |
| 7172 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7173 | } |
| 7174 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 7175 | o[paramIt->first] = json_double_value; |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7176 | } else if (!paramIt->second.string_value.empty()) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7177 | SerializeStringProperty(paramIt->first, paramIt->second.string_value, o); |
Ben Buzbee | f6af224 | 2018-04-25 15:13:05 -0700 | [diff] [blame] | 7178 | } else if (paramIt->second.has_number_value) { |
| 7179 | o[paramIt->first] = paramIt->second.number_value; |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7180 | } else { |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 7181 | o[paramIt->first] = paramIt->second.bool_value; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7182 | } |
| 7183 | } |
| 7184 | } |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 7185 | #endif |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7186 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7187 | static void SerializeExtensionMap(const ExtensionMap &extensions, |
| 7188 | detail::json &o) { |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7189 | if (!extensions.size()) return; |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 7190 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7191 | detail::json extMap; |
Selmar Kok | 81b672b | 2019-10-18 16:08:44 +0200 | [diff] [blame] | 7192 | for (ExtensionMap::const_iterator extIt = extensions.begin(); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7193 | extIt != extensions.end(); ++extIt) { |
Syoyo Fujita | 924d86e | 2018-10-08 21:15:12 +0900 | [diff] [blame] | 7194 | // Allow an empty object for extension(#97) |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7195 | detail::json ret; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7196 | bool isNull = true; |
Syoyo Fujita | 924d86e | 2018-10-08 21:15:12 +0900 | [diff] [blame] | 7197 | if (ValueToJson(extIt->second, &ret)) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7198 | isNull = detail::JsonIsNull(ret); |
| 7199 | detail::JsonAddMember(extMap, extIt->first.c_str(), std::move(ret)); |
Selmar Kok | db7f4e4 | 2018-10-10 18:10:58 +0200 | [diff] [blame] | 7200 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7201 | if (isNull) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 7202 | if (!(extIt->first.empty())) { // name should not be empty, but for sure |
| 7203 | // create empty object so that an extension name is still included in |
| 7204 | // json. |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7205 | detail::json empty; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7206 | detail::JsonSetObject(empty); |
| 7207 | detail::JsonAddMember(extMap, extIt->first.c_str(), std::move(empty)); |
Syoyo Fujita | 924d86e | 2018-10-08 21:15:12 +0900 | [diff] [blame] | 7208 | } |
| 7209 | } |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 7210 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7211 | detail::JsonAddMember(o, "extensions", std::move(extMap)); |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 7212 | } |
| 7213 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7214 | static void SerializeExtras(const Value &extras, detail::json &o) { |
| 7215 | if (extras.Type() != NULL_TYPE) SerializeValue("extras", extras, o); |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7216 | } |
| 7217 | |
| 7218 | template <typename GltfType> |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7219 | void SerializeExtrasAndExtensions(const GltfType &obj, detail::json &o) { |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7220 | SerializeExtensionMap(obj.extensions, o); |
| 7221 | SerializeExtras(obj.extras, o); |
| 7222 | } |
| 7223 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7224 | static void SerializeGltfAccessor(const Accessor &accessor, detail::json &o) { |
Sanjeet Suhag | 5ecede7 | 2020-03-04 17:40:10 -0500 | [diff] [blame] | 7225 | if (accessor.bufferView >= 0) |
| 7226 | SerializeNumberProperty<int>("bufferView", accessor.bufferView, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7227 | |
Syoyo Fujita | 18f0e20 | 2020-04-29 19:16:35 +0900 | [diff] [blame] | 7228 | if (accessor.byteOffset != 0) |
emimvi | bf7120f | 2023-09-02 00:11:41 +0200 | [diff] [blame] | 7229 | SerializeNumberProperty<size_t>("byteOffset", accessor.byteOffset, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7230 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7231 | SerializeNumberProperty<int>("componentType", accessor.componentType, o); |
| 7232 | SerializeNumberProperty<size_t>("count", accessor.count, o); |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 7233 | |
| 7234 | if ((accessor.componentType == TINYGLTF_COMPONENT_TYPE_FLOAT) || |
| 7235 | (accessor.componentType == TINYGLTF_COMPONENT_TYPE_DOUBLE)) { |
| 7236 | SerializeNumberArrayProperty<double>("min", accessor.minValues, o); |
| 7237 | SerializeNumberArrayProperty<double>("max", accessor.maxValues, o); |
| 7238 | } else { |
| 7239 | // Issue #301. Serialize as integer. |
| 7240 | // Assume int value is within [-2**31-1, 2**31-1] |
| 7241 | { |
| 7242 | std::vector<int> values; |
| 7243 | std::transform(accessor.minValues.begin(), accessor.minValues.end(), |
| 7244 | std::back_inserter(values), |
| 7245 | [](double v) { return static_cast<int>(v); }); |
| 7246 | |
| 7247 | SerializeNumberArrayProperty<int>("min", values, o); |
| 7248 | } |
| 7249 | |
| 7250 | { |
| 7251 | std::vector<int> values; |
| 7252 | std::transform(accessor.maxValues.begin(), accessor.maxValues.end(), |
| 7253 | std::back_inserter(values), |
| 7254 | [](double v) { return static_cast<int>(v); }); |
| 7255 | |
| 7256 | SerializeNumberArrayProperty<int>("max", values, o); |
| 7257 | } |
| 7258 | } |
| 7259 | |
Eero Pajarre | 2e8a115 | 2019-11-18 13:09:25 +0200 | [diff] [blame] | 7260 | if (accessor.normalized) |
| 7261 | SerializeValue("normalized", Value(accessor.normalized), o); |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7262 | std::string type; |
| 7263 | switch (accessor.type) { |
| 7264 | case TINYGLTF_TYPE_SCALAR: |
| 7265 | type = "SCALAR"; |
| 7266 | break; |
| 7267 | case TINYGLTF_TYPE_VEC2: |
| 7268 | type = "VEC2"; |
| 7269 | break; |
| 7270 | case TINYGLTF_TYPE_VEC3: |
| 7271 | type = "VEC3"; |
| 7272 | break; |
| 7273 | case TINYGLTF_TYPE_VEC4: |
| 7274 | type = "VEC4"; |
| 7275 | break; |
| 7276 | case TINYGLTF_TYPE_MAT2: |
| 7277 | type = "MAT2"; |
| 7278 | break; |
| 7279 | case TINYGLTF_TYPE_MAT3: |
| 7280 | type = "MAT3"; |
| 7281 | break; |
| 7282 | case TINYGLTF_TYPE_MAT4: |
| 7283 | type = "MAT4"; |
| 7284 | break; |
| 7285 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 7286 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7287 | SerializeStringProperty("type", type, o); |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 7288 | if (!accessor.name.empty()) SerializeStringProperty("name", accessor.name, o); |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7289 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7290 | SerializeExtrasAndExtensions(accessor, o); |
feiy | 0b31543 | 2022-08-13 10:08:17 +0800 | [diff] [blame] | 7291 | |
| 7292 | // sparse |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7293 | if (accessor.sparse.isSparse) { |
| 7294 | detail::json sparse; |
| 7295 | SerializeNumberProperty<int>("count", accessor.sparse.count, sparse); |
| 7296 | { |
| 7297 | detail::json indices; |
| 7298 | SerializeNumberProperty<int>("bufferView", |
| 7299 | accessor.sparse.indices.bufferView, indices); |
emimvi | 759976e | 2023-09-02 09:39:53 +0200 | [diff] [blame] | 7300 | SerializeNumberProperty<size_t>("byteOffset", |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7301 | accessor.sparse.indices.byteOffset, indices); |
| 7302 | SerializeNumberProperty<int>( |
| 7303 | "componentType", accessor.sparse.indices.componentType, indices); |
| 7304 | SerializeExtrasAndExtensions(accessor.sparse.indices, indices); |
| 7305 | detail::JsonAddMember(sparse, "indices", std::move(indices)); |
| 7306 | } |
| 7307 | { |
| 7308 | detail::json values; |
| 7309 | SerializeNumberProperty<int>("bufferView", |
| 7310 | accessor.sparse.values.bufferView, values); |
emimvi | 759976e | 2023-09-02 09:39:53 +0200 | [diff] [blame] | 7311 | SerializeNumberProperty<size_t>("byteOffset", |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7312 | accessor.sparse.values.byteOffset, values); |
| 7313 | SerializeExtrasAndExtensions(accessor.sparse.values, values); |
| 7314 | detail::JsonAddMember(sparse, "values", std::move(values)); |
| 7315 | } |
| 7316 | SerializeExtrasAndExtensions(accessor.sparse, sparse); |
| 7317 | detail::JsonAddMember(o, "sparse", std::move(sparse)); |
feiy | 0b31543 | 2022-08-13 10:08:17 +0800 | [diff] [blame] | 7318 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7319 | } |
| 7320 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7321 | static void SerializeGltfAnimationChannel(const AnimationChannel &channel, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7322 | detail::json &o) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7323 | SerializeNumberProperty("sampler", channel.sampler, o); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7324 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7325 | detail::json target; |
Jack Mousseau | 283b552 | 2023-01-15 11:45:45 -0800 | [diff] [blame] | 7326 | |
Loïc Escales | a75355b | 2023-04-18 21:03:39 +0200 | [diff] [blame] | 7327 | if (channel.target_node >= 0) { |
Jack Mousseau | 283b552 | 2023-01-15 11:45:45 -0800 | [diff] [blame] | 7328 | SerializeNumberProperty("node", channel.target_node, target); |
| 7329 | } |
| 7330 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7331 | SerializeStringProperty("path", channel.target_path, target); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7332 | |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 7333 | SerializeExtensionMap(channel.target_extensions, target); |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7334 | SerializeExtras(channel.target_extras, target); |
Selmar Kok | 973d9b3 | 2020-01-21 18:45:24 +0100 | [diff] [blame] | 7335 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7336 | detail::JsonAddMember(o, "target", std::move(target)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7337 | } |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7338 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7339 | SerializeExtrasAndExtensions(channel, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7340 | } |
| 7341 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7342 | static void SerializeGltfAnimationSampler(const AnimationSampler &sampler, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7343 | detail::json &o) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7344 | SerializeNumberProperty("input", sampler.input, o); |
| 7345 | SerializeNumberProperty("output", sampler.output, o); |
| 7346 | SerializeStringProperty("interpolation", sampler.interpolation, o); |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7347 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7348 | SerializeExtrasAndExtensions(sampler, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7349 | } |
| 7350 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7351 | static void SerializeGltfAnimation(const Animation &animation, |
| 7352 | detail::json &o) { |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 7353 | if (!animation.name.empty()) |
| 7354 | SerializeStringProperty("name", animation.name, o); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7355 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7356 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7357 | detail::json channels; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7358 | detail::JsonReserveArray(channels, animation.channels.size()); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7359 | for (unsigned int i = 0; i < animation.channels.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7360 | detail::json channel; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7361 | AnimationChannel gltfChannel = animation.channels[i]; |
| 7362 | SerializeGltfAnimationChannel(gltfChannel, channel); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7363 | detail::JsonPushBack(channels, std::move(channel)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7364 | } |
| 7365 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7366 | detail::JsonAddMember(o, "channels", std::move(channels)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7367 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7368 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7369 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7370 | detail::json samplers; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7371 | detail::JsonReserveArray(samplers, animation.samplers.size()); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7372 | for (unsigned int i = 0; i < animation.samplers.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7373 | detail::json sampler; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7374 | AnimationSampler gltfSampler = animation.samplers[i]; |
| 7375 | SerializeGltfAnimationSampler(gltfSampler, sampler); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7376 | detail::JsonPushBack(samplers, std::move(sampler)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7377 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7378 | detail::JsonAddMember(o, "samplers", std::move(samplers)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7379 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7380 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7381 | SerializeExtrasAndExtensions(animation, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7382 | } |
| 7383 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7384 | static void SerializeGltfAsset(const Asset &asset, detail::json &o) { |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7385 | if (!asset.generator.empty()) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7386 | SerializeStringProperty("generator", asset.generator, o); |
| 7387 | } |
| 7388 | |
Christophe | 820ede8 | 2019-07-04 15:21:21 +0900 | [diff] [blame] | 7389 | if (!asset.copyright.empty()) { |
| 7390 | SerializeStringProperty("copyright", asset.copyright, o); |
| 7391 | } |
| 7392 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7393 | auto version = asset.version; |
| 7394 | if (version.empty()) { |
Syoyo Fujita | b702de7 | 2021-03-02 19:08:29 +0900 | [diff] [blame] | 7395 | // Just in case |
| 7396 | // `version` must be defined |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7397 | version = "2.0"; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7398 | } |
| 7399 | |
Syoyo Fujita | b702de7 | 2021-03-02 19:08:29 +0900 | [diff] [blame] | 7400 | // TODO(syoyo): Do we need to check if `version` is greater or equal to 2.0? |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7401 | SerializeStringProperty("version", version, o); |
Syoyo Fujita | b702de7 | 2021-03-02 19:08:29 +0900 | [diff] [blame] | 7402 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7403 | SerializeExtrasAndExtensions(asset, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7404 | } |
| 7405 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7406 | static void SerializeGltfBufferBin(const Buffer &buffer, detail::json &o, |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 7407 | std::vector<unsigned char> &binBuffer) { |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 7408 | SerializeNumberProperty("byteLength", buffer.data.size(), o); |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 7409 | binBuffer = buffer.data; |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 7410 | |
| 7411 | if (buffer.name.size()) SerializeStringProperty("name", buffer.name, o); |
| 7412 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7413 | SerializeExtrasAndExtensions(buffer, o); |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 7414 | } |
| 7415 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7416 | static void SerializeGltfBuffer(const Buffer &buffer, detail::json &o) { |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 7417 | SerializeNumberProperty("byteLength", buffer.data.size(), o); |
| 7418 | SerializeGltfBufferData(buffer.data, o); |
| 7419 | |
| 7420 | if (buffer.name.size()) SerializeStringProperty("name", buffer.name, o); |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7421 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7422 | SerializeExtrasAndExtensions(buffer, o); |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 7423 | } |
| 7424 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7425 | static bool SerializeGltfBuffer(const Buffer &buffer, detail::json &o, |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 7426 | const std::string &binFilename, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 7427 | const std::string &binUri) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 7428 | if (!SerializeGltfBufferData(buffer.data, binFilename)) return false; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7429 | SerializeNumberProperty("byteLength", buffer.data.size(), o); |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 7430 | SerializeStringProperty("uri", binUri, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7431 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7432 | if (buffer.name.size()) SerializeStringProperty("name", buffer.name, o); |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7433 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7434 | SerializeExtrasAndExtensions(buffer, o); |
Selmar Kok | e467749 | 2018-10-25 16:45:49 +0200 | [diff] [blame] | 7435 | return true; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7436 | } |
| 7437 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7438 | static void SerializeGltfBufferView(const BufferView &bufferView, |
| 7439 | detail::json &o) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7440 | SerializeNumberProperty("buffer", bufferView.buffer, o); |
| 7441 | SerializeNumberProperty<size_t>("byteLength", bufferView.byteLength, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7442 | |
Johan Bowald | faa2722 | 2018-03-28 14:44:45 +0200 | [diff] [blame] | 7443 | // byteStride is optional, minimum allowed is 4 |
| 7444 | if (bufferView.byteStride >= 4) { |
| 7445 | SerializeNumberProperty<size_t>("byteStride", bufferView.byteStride, o); |
| 7446 | } |
| 7447 | // byteOffset is optional, default is 0 |
| 7448 | if (bufferView.byteOffset > 0) { |
| 7449 | SerializeNumberProperty<size_t>("byteOffset", bufferView.byteOffset, o); |
| 7450 | } |
| 7451 | // Target is optional, check if it contains a valid value |
| 7452 | if (bufferView.target == TINYGLTF_TARGET_ARRAY_BUFFER || |
| 7453 | bufferView.target == TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER) { |
| 7454 | SerializeNumberProperty("target", bufferView.target, o); |
| 7455 | } |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7456 | if (bufferView.name.size()) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7457 | SerializeStringProperty("name", bufferView.name, o); |
| 7458 | } |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7459 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7460 | SerializeExtrasAndExtensions(bufferView, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7461 | } |
| 7462 | |
Pyarelal Knowles | a912155 | 2022-12-29 14:12:29 -0800 | [diff] [blame] | 7463 | static void SerializeGltfImage(const Image &image, const std::string &uri, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7464 | detail::json &o) { |
Syoyo Fujita | 584f1df | 2022-12-29 21:05:53 +0900 | [diff] [blame] | 7465 | // From 2.7.0, we look for `uri` parameter, not `Image.uri` |
| 7466 | // if uri is empty, the mimeType and bufferview should be set |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7467 | if (uri.empty()) { |
Arthur Brianville (Ybalrid) | f29ae1a | 2019-05-25 22:30:55 +0200 | [diff] [blame] | 7468 | SerializeStringProperty("mimeType", image.mimeType, o); |
| 7469 | SerializeNumberProperty<int>("bufferView", image.bufferView, o); |
| 7470 | } else { |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7471 | SerializeStringProperty("uri", uri, o); |
Arthur Brianville (Ybalrid) | f29ae1a | 2019-05-25 22:30:55 +0200 | [diff] [blame] | 7472 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7473 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7474 | if (image.name.size()) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7475 | SerializeStringProperty("name", image.name, o); |
| 7476 | } |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7477 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7478 | SerializeExtrasAndExtensions(image, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7479 | } |
| 7480 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7481 | static void SerializeGltfTextureInfo(const TextureInfo &texinfo, |
| 7482 | detail::json &o) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7483 | SerializeNumberProperty("index", texinfo.index, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7484 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7485 | if (texinfo.texCoord != 0) { |
| 7486 | SerializeNumberProperty("texCoord", texinfo.texCoord, o); |
| 7487 | } |
| 7488 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7489 | SerializeExtrasAndExtensions(texinfo, o); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7490 | } |
| 7491 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7492 | static void SerializeGltfNormalTextureInfo(const NormalTextureInfo &texinfo, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7493 | detail::json &o) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7494 | SerializeNumberProperty("index", texinfo.index, o); |
| 7495 | |
| 7496 | if (texinfo.texCoord != 0) { |
| 7497 | SerializeNumberProperty("texCoord", texinfo.texCoord, o); |
| 7498 | } |
| 7499 | |
Benjamin Schmithüsen | 1c84fc2 | 2019-07-25 16:07:27 +0200 | [diff] [blame] | 7500 | if (!TINYGLTF_DOUBLE_EQUAL(texinfo.scale, 1.0)) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7501 | SerializeNumberProperty("scale", texinfo.scale, o); |
| 7502 | } |
| 7503 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7504 | SerializeExtrasAndExtensions(texinfo, o); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7505 | } |
| 7506 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7507 | static void SerializeGltfOcclusionTextureInfo( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7508 | const OcclusionTextureInfo &texinfo, detail::json &o) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7509 | SerializeNumberProperty("index", texinfo.index, o); |
| 7510 | |
| 7511 | if (texinfo.texCoord != 0) { |
| 7512 | SerializeNumberProperty("texCoord", texinfo.texCoord, o); |
| 7513 | } |
| 7514 | |
Benjamin Schmithüsen | 1c84fc2 | 2019-07-25 16:07:27 +0200 | [diff] [blame] | 7515 | if (!TINYGLTF_DOUBLE_EQUAL(texinfo.strength, 1.0)) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7516 | SerializeNumberProperty("strength", texinfo.strength, o); |
| 7517 | } |
| 7518 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7519 | SerializeExtrasAndExtensions(texinfo, o); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7520 | } |
| 7521 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7522 | static void SerializeGltfPbrMetallicRoughness(const PbrMetallicRoughness &pbr, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7523 | detail::json &o) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7524 | std::vector<double> default_baseColorFactor = {1.0, 1.0, 1.0, 1.0}; |
| 7525 | if (!Equals(pbr.baseColorFactor, default_baseColorFactor)) { |
| 7526 | SerializeNumberArrayProperty<double>("baseColorFactor", pbr.baseColorFactor, |
| 7527 | o); |
| 7528 | } |
| 7529 | |
Benjamin Schmithüsen | 1c84fc2 | 2019-07-25 16:07:27 +0200 | [diff] [blame] | 7530 | if (!TINYGLTF_DOUBLE_EQUAL(pbr.metallicFactor, 1.0)) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7531 | SerializeNumberProperty("metallicFactor", pbr.metallicFactor, o); |
| 7532 | } |
| 7533 | |
Benjamin Schmithüsen | 1c84fc2 | 2019-07-25 16:07:27 +0200 | [diff] [blame] | 7534 | if (!TINYGLTF_DOUBLE_EQUAL(pbr.roughnessFactor, 1.0)) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7535 | SerializeNumberProperty("roughnessFactor", pbr.roughnessFactor, o); |
| 7536 | } |
| 7537 | |
Syoyo Fujita | 4ebd636 | 2019-08-15 12:25:50 +0900 | [diff] [blame] | 7538 | if (pbr.baseColorTexture.index > -1) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7539 | detail::json texinfo; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7540 | SerializeGltfTextureInfo(pbr.baseColorTexture, texinfo); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7541 | detail::JsonAddMember(o, "baseColorTexture", std::move(texinfo)); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7542 | } |
| 7543 | |
Syoyo Fujita | 4ebd636 | 2019-08-15 12:25:50 +0900 | [diff] [blame] | 7544 | if (pbr.metallicRoughnessTexture.index > -1) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7545 | detail::json texinfo; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7546 | SerializeGltfTextureInfo(pbr.metallicRoughnessTexture, texinfo); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7547 | detail::JsonAddMember(o, "metallicRoughnessTexture", std::move(texinfo)); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7548 | } |
| 7549 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7550 | SerializeExtrasAndExtensions(pbr, o); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7551 | } |
| 7552 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7553 | static void SerializeGltfMaterial(const Material &material, detail::json &o) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7554 | if (material.name.size()) { |
| 7555 | SerializeStringProperty("name", material.name, o); |
| 7556 | } |
| 7557 | |
| 7558 | // QUESTION(syoyo): Write material parameters regardless of its default value? |
| 7559 | |
| 7560 | if (!TINYGLTF_DOUBLE_EQUAL(material.alphaCutoff, 0.5)) { |
| 7561 | SerializeNumberProperty("alphaCutoff", material.alphaCutoff, o); |
| 7562 | } |
| 7563 | |
Patrick Härtl | d9a468b | 2019-08-14 14:14:07 +0200 | [diff] [blame] | 7564 | if (material.alphaMode.compare("OPAQUE") != 0) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7565 | SerializeStringProperty("alphaMode", material.alphaMode, o); |
| 7566 | } |
| 7567 | |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 7568 | if (material.doubleSided != false) |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7569 | detail::JsonAddMember(o, "doubleSided", detail::json(material.doubleSided)); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7570 | |
Syoyo Fujita | 4ebd636 | 2019-08-15 12:25:50 +0900 | [diff] [blame] | 7571 | if (material.normalTexture.index > -1) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7572 | detail::json texinfo; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7573 | SerializeGltfNormalTextureInfo(material.normalTexture, texinfo); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7574 | detail::JsonAddMember(o, "normalTexture", std::move(texinfo)); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7575 | } |
| 7576 | |
Syoyo Fujita | 4ebd636 | 2019-08-15 12:25:50 +0900 | [diff] [blame] | 7577 | if (material.occlusionTexture.index > -1) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7578 | detail::json texinfo; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7579 | SerializeGltfOcclusionTextureInfo(material.occlusionTexture, texinfo); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7580 | detail::JsonAddMember(o, "occlusionTexture", std::move(texinfo)); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7581 | } |
| 7582 | |
Syoyo Fujita | 4ebd636 | 2019-08-15 12:25:50 +0900 | [diff] [blame] | 7583 | if (material.emissiveTexture.index > -1) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7584 | detail::json texinfo; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7585 | SerializeGltfTextureInfo(material.emissiveTexture, texinfo); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7586 | detail::JsonAddMember(o, "emissiveTexture", std::move(texinfo)); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7587 | } |
| 7588 | |
| 7589 | std::vector<double> default_emissiveFactor = {0.0, 0.0, 0.0}; |
| 7590 | if (!Equals(material.emissiveFactor, default_emissiveFactor)) { |
| 7591 | SerializeNumberArrayProperty<double>("emissiveFactor", |
| 7592 | material.emissiveFactor, o); |
| 7593 | } |
| 7594 | |
| 7595 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7596 | detail::json pbrMetallicRoughness; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7597 | SerializeGltfPbrMetallicRoughness(material.pbrMetallicRoughness, |
| 7598 | pbrMetallicRoughness); |
Syoyo Fujita | 7e00904 | 2019-09-13 15:32:22 +0900 | [diff] [blame] | 7599 | // Issue 204 |
| 7600 | // Do not serialize `pbrMetallicRoughness` if pbrMetallicRoughness has all |
| 7601 | // default values(json is null). Otherwise it will serialize to |
| 7602 | // `pbrMetallicRoughness : null`, which cannot be read by other glTF |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 7603 | // importers (and validators). |
Syoyo Fujita | 7e00904 | 2019-09-13 15:32:22 +0900 | [diff] [blame] | 7604 | // |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7605 | if (!detail::JsonIsNull(pbrMetallicRoughness)) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7606 | detail::JsonAddMember(o, "pbrMetallicRoughness", |
| 7607 | std::move(pbrMetallicRoughness)); |
Syoyo Fujita | 7e00904 | 2019-09-13 15:32:22 +0900 | [diff] [blame] | 7608 | } |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7609 | } |
| 7610 | |
| 7611 | #if 0 // legacy way. just for the record. |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7612 | if (material.values.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7613 | detail::json pbrMetallicRoughness; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7614 | SerializeParameterMap(material.values, pbrMetallicRoughness); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7615 | detail::JsonAddMember(o, "pbrMetallicRoughness", std::move(pbrMetallicRoughness)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7616 | } |
| 7617 | |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7618 | SerializeParameterMap(material.additionalValues, o); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7619 | #endif |
| 7620 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7621 | SerializeExtrasAndExtensions(material, o); |
Thomas Gamper | f1bdf43 | 2024-02-05 16:50:46 +0100 | [diff] [blame] | 7622 | |
| 7623 | // MSFT_lod |
| 7624 | if (!material.lods.empty()) { |
| 7625 | detail::json_iterator it; |
| 7626 | if (!detail::FindMember(o, "extensions", it)) { |
| 7627 | detail::json extensions; |
| 7628 | detail::JsonSetObject(extensions); |
| 7629 | detail::JsonAddMember(o, "extensions", std::move(extensions)); |
| 7630 | detail::FindMember(o, "extensions", it); |
| 7631 | } |
| 7632 | auto &extensions = detail::GetValue(it); |
| 7633 | if (!detail::FindMember(extensions, "MSFT_lod", it)) { |
| 7634 | detail::json lod; |
| 7635 | detail::JsonSetObject(lod); |
| 7636 | detail::JsonAddMember(extensions, "MSFT_lod", std::move(lod)); |
| 7637 | detail::FindMember(extensions, "MSFT_lod", it); |
| 7638 | } |
| 7639 | SerializeNumberArrayProperty<int>("ids", material.lods, detail::GetValue(it)); |
| 7640 | } else { |
| 7641 | detail::json_iterator ext_it; |
| 7642 | if (detail::FindMember(o, "extensions", ext_it)) { |
| 7643 | auto &extensions = detail::GetValue(ext_it); |
| 7644 | detail::json_iterator lp_it; |
| 7645 | if (detail::FindMember(extensions, "MSFT_lod", lp_it)) { |
| 7646 | detail::Erase(extensions, lp_it); |
| 7647 | } |
| 7648 | if (detail::IsEmpty(extensions)) { |
| 7649 | detail::Erase(o, ext_it); |
| 7650 | } |
| 7651 | } |
| 7652 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7653 | } |
| 7654 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7655 | static void SerializeGltfMesh(const Mesh &mesh, detail::json &o) { |
| 7656 | detail::json primitives; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7657 | detail::JsonReserveArray(primitives, mesh.primitives.size()); |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7658 | for (unsigned int i = 0; i < mesh.primitives.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7659 | detail::json primitive; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7660 | const Primitive &gltfPrimitive = mesh.primitives[i]; // don't make a copy |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7661 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7662 | detail::json attributes; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7663 | for (auto attrIt = gltfPrimitive.attributes.begin(); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7664 | attrIt != gltfPrimitive.attributes.end(); ++attrIt) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7665 | SerializeNumberProperty<int>(attrIt->first, attrIt->second, attributes); |
| 7666 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7667 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7668 | detail::JsonAddMember(primitive, "attributes", std::move(attributes)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7669 | } |
Johan Bowald | faa2722 | 2018-03-28 14:44:45 +0200 | [diff] [blame] | 7670 | |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 7671 | // Indices is optional |
Johan Bowald | faa2722 | 2018-03-28 14:44:45 +0200 | [diff] [blame] | 7672 | if (gltfPrimitive.indices > -1) { |
| 7673 | SerializeNumberProperty<int>("indices", gltfPrimitive.indices, primitive); |
| 7674 | } |
| 7675 | // Material is optional |
| 7676 | if (gltfPrimitive.material > -1) { |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 7677 | SerializeNumberProperty<int>("material", gltfPrimitive.material, |
| 7678 | primitive); |
Johan Bowald | faa2722 | 2018-03-28 14:44:45 +0200 | [diff] [blame] | 7679 | } |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7680 | SerializeNumberProperty<int>("mode", gltfPrimitive.mode, primitive); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7681 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7682 | // Morph targets |
| 7683 | if (gltfPrimitive.targets.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7684 | detail::json targets; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7685 | detail::JsonReserveArray(targets, gltfPrimitive.targets.size()); |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7686 | for (unsigned int k = 0; k < gltfPrimitive.targets.size(); ++k) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7687 | detail::json targetAttributes; |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7688 | std::map<std::string, int> targetData = gltfPrimitive.targets[k]; |
| 7689 | for (std::map<std::string, int>::iterator attrIt = targetData.begin(); |
| 7690 | attrIt != targetData.end(); ++attrIt) { |
| 7691 | SerializeNumberProperty<int>(attrIt->first, attrIt->second, |
| 7692 | targetAttributes); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7693 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7694 | detail::JsonPushBack(targets, std::move(targetAttributes)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7695 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7696 | detail::JsonAddMember(primitive, "targets", std::move(targets)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7697 | } |
| 7698 | |
KUDELSKI Dimitri | 69d7557 | 2023-07-10 15:13:09 +0200 | [diff] [blame] | 7699 | SerializeExtrasAndExtensions(gltfPrimitive, primitive); |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7700 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7701 | detail::JsonPushBack(primitives, std::move(primitive)); |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7702 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7703 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7704 | detail::JsonAddMember(o, "primitives", std::move(primitives)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7705 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7706 | if (mesh.weights.size()) { |
| 7707 | SerializeNumberArrayProperty<double>("weights", mesh.weights, o); |
| 7708 | } |
| 7709 | |
| 7710 | if (mesh.name.size()) { |
| 7711 | SerializeStringProperty("name", mesh.name, o); |
| 7712 | } |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7713 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7714 | SerializeExtrasAndExtensions(mesh, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7715 | } |
| 7716 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7717 | static void SerializeSpotLight(const SpotLight &spot, detail::json &o) { |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 7718 | SerializeNumberProperty("innerConeAngle", spot.innerConeAngle, o); |
| 7719 | SerializeNumberProperty("outerConeAngle", spot.outerConeAngle, o); |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7720 | SerializeExtrasAndExtensions(spot, o); |
Benjamin Schmithüsen | 051f4be | 2019-07-09 17:59:20 +0200 | [diff] [blame] | 7721 | } |
| 7722 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7723 | static void SerializeGltfLight(const Light &light, detail::json &o) { |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 7724 | if (!light.name.empty()) SerializeStringProperty("name", light.name, o); |
Benjamin Schmithüsen | 051f4be | 2019-07-09 17:59:20 +0200 | [diff] [blame] | 7725 | SerializeNumberProperty("intensity", light.intensity, o); |
Syoyo Fujita | 3dad303 | 2020-05-13 21:22:23 +0900 | [diff] [blame] | 7726 | if (light.range > 0.0) { |
| 7727 | SerializeNumberProperty("range", light.range, o); |
| 7728 | } |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 7729 | SerializeNumberArrayProperty("color", light.color, o); |
| 7730 | SerializeStringProperty("type", light.type, o); |
Benjamin Schmithüsen | 051f4be | 2019-07-09 17:59:20 +0200 | [diff] [blame] | 7731 | if (light.type == "spot") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7732 | detail::json spot; |
Benjamin Schmithüsen | 051f4be | 2019-07-09 17:59:20 +0200 | [diff] [blame] | 7733 | SerializeSpotLight(light.spot, spot); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7734 | detail::JsonAddMember(o, "spot", std::move(spot)); |
Benjamin Schmithüsen | 051f4be | 2019-07-09 17:59:20 +0200 | [diff] [blame] | 7735 | } |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7736 | SerializeExtrasAndExtensions(light, o); |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 7737 | } |
| 7738 | |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 7739 | static void SerializeGltfPositionalEmitter(const PositionalEmitter &positional, |
| 7740 | detail::json &o) { |
| 7741 | if (!TINYGLTF_DOUBLE_EQUAL(positional.coneInnerAngle, 6.283185307179586)) |
| 7742 | SerializeNumberProperty("coneInnerAngle", positional.coneInnerAngle, o); |
| 7743 | if (!TINYGLTF_DOUBLE_EQUAL(positional.coneOuterAngle, 6.283185307179586)) |
| 7744 | SerializeNumberProperty("coneOuterAngle", positional.coneOuterAngle, o); |
| 7745 | if (positional.coneOuterGain > 0.0) |
| 7746 | SerializeNumberProperty("coneOuterGain", positional.coneOuterGain, o); |
| 7747 | if (!TINYGLTF_DOUBLE_EQUAL(positional.maxDistance, 100.0)) |
| 7748 | SerializeNumberProperty("maxDistance", positional.maxDistance, o); |
| 7749 | if (!TINYGLTF_DOUBLE_EQUAL(positional.refDistance, 1.0)) |
| 7750 | SerializeNumberProperty("refDistance", positional.refDistance, o); |
| 7751 | if (!TINYGLTF_DOUBLE_EQUAL(positional.rolloffFactor, 1.0)) |
| 7752 | SerializeNumberProperty("rolloffFactor", positional.rolloffFactor, o); |
| 7753 | |
| 7754 | SerializeExtrasAndExtensions(positional, o); |
| 7755 | } |
| 7756 | |
| 7757 | static void SerializeGltfAudioEmitter(const AudioEmitter &emitter, |
| 7758 | detail::json &o) { |
| 7759 | if (!emitter.name.empty()) SerializeStringProperty("name", emitter.name, o); |
| 7760 | if (!TINYGLTF_DOUBLE_EQUAL(emitter.gain, 1.0)) |
| 7761 | SerializeNumberProperty("gain", emitter.gain, o); |
| 7762 | if (emitter.loop) SerializeNumberProperty("loop", emitter.loop, o); |
| 7763 | if (emitter.playing) SerializeNumberProperty("playing", emitter.playing, o); |
| 7764 | if (!emitter.type.empty()) SerializeStringProperty("type", emitter.type, o); |
| 7765 | if (!emitter.distanceModel.empty()) |
| 7766 | SerializeStringProperty("distanceModel", emitter.distanceModel, o); |
| 7767 | if (emitter.type == "positional") { |
| 7768 | detail::json positional; |
| 7769 | SerializeGltfPositionalEmitter(emitter.positional, positional); |
| 7770 | detail::JsonAddMember(o, "positional", std::move(positional)); |
| 7771 | } |
| 7772 | SerializeNumberProperty("source", emitter.source, o); |
| 7773 | SerializeExtrasAndExtensions(emitter, o); |
| 7774 | } |
| 7775 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7776 | static void SerializeGltfAudioSource(const AudioSource &source, |
| 7777 | detail::json &o) { |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 7778 | std::string name; |
| 7779 | std::string uri; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 7780 | std::string mimeType; // (required if no uri) ["audio/mp3", "audio/ogg", |
| 7781 | // "audio/wav", "audio/m4a"] |
| 7782 | |
| 7783 | if (!source.name.empty()) SerializeStringProperty("name", source.name, o); |
| 7784 | if (source.uri.empty()) { |
| 7785 | SerializeStringProperty("mimeType", source.mimeType, o); |
| 7786 | SerializeNumberProperty<int>("bufferView", source.bufferView, o); |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7787 | } else { |
| 7788 | SerializeStringProperty("uri", source.uri, o); |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 7789 | } |
| 7790 | SerializeExtrasAndExtensions(source, o); |
| 7791 | } |
| 7792 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7793 | static void SerializeGltfNode(const Node &node, detail::json &o) { |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7794 | if (node.translation.size() > 0) { |
| 7795 | SerializeNumberArrayProperty<double>("translation", node.translation, o); |
| 7796 | } |
| 7797 | if (node.rotation.size() > 0) { |
| 7798 | SerializeNumberArrayProperty<double>("rotation", node.rotation, o); |
| 7799 | } |
| 7800 | if (node.scale.size() > 0) { |
| 7801 | SerializeNumberArrayProperty<double>("scale", node.scale, o); |
| 7802 | } |
| 7803 | if (node.matrix.size() > 0) { |
| 7804 | SerializeNumberArrayProperty<double>("matrix", node.matrix, o); |
| 7805 | } |
| 7806 | if (node.mesh != -1) { |
| 7807 | SerializeNumberProperty<int>("mesh", node.mesh, o); |
| 7808 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7809 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7810 | if (node.skin != -1) { |
| 7811 | SerializeNumberProperty<int>("skin", node.skin, o); |
| 7812 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7813 | |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7814 | if (node.camera != -1) { |
| 7815 | SerializeNumberProperty<int>("camera", node.camera, o); |
| 7816 | } |
| 7817 | |
Benjamin Schmithüsen | 74c3c10 | 2019-08-16 14:24:26 +0200 | [diff] [blame] | 7818 | if (node.weights.size() > 0) { |
| 7819 | SerializeNumberArrayProperty<double>("weights", node.weights, o); |
| 7820 | } |
| 7821 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7822 | SerializeExtrasAndExtensions(node, o); |
Jens Olsson | b96f696 | 2018-05-24 15:29:54 +0200 | [diff] [blame] | 7823 | |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 7824 | // Note(agnat): If the asset was loaded from disk, the node may already |
| 7825 | // contain the KHR_lights_punctual extension. If it was constructed in |
| 7826 | // memory it does not. In any case we update the JSON property using |
| 7827 | // the value from the struct. Last, if the node does not have a light |
| 7828 | // reference but the extension is still present, we remove it. |
| 7829 | if (node.light != -1) { |
| 7830 | detail::json_iterator it; |
| 7831 | if (!detail::FindMember(o, "extensions", it)) { |
| 7832 | detail::json extensions; |
| 7833 | detail::JsonSetObject(extensions); |
| 7834 | detail::JsonAddMember(o, "extensions", std::move(extensions)); |
| 7835 | detail::FindMember(o, "extensions", it); |
| 7836 | } |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7837 | auto &extensions = detail::GetValue(it); |
| 7838 | if (!detail::FindMember(extensions, "KHR_lights_punctual", it)) { |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 7839 | detail::json lights_punctual; |
| 7840 | detail::JsonSetObject(lights_punctual); |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7841 | detail::JsonAddMember(extensions, "KHR_lights_punctual", |
| 7842 | std::move(lights_punctual)); |
Thomas Gamper | afcfb57 | 2023-11-23 14:12:54 +0100 | [diff] [blame] | 7843 | detail::FindMember(extensions, "KHR_lights_punctual", it); |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 7844 | } |
David Siegel | 8d5d0b3 | 2023-06-07 15:35:35 +0200 | [diff] [blame] | 7845 | SerializeNumberProperty("light", node.light, detail::GetValue(it)); |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 7846 | } else { |
| 7847 | // node has no light ref (any longer)... so we clean up |
| 7848 | detail::json_iterator ext_it; |
| 7849 | if (detail::FindMember(o, "extensions", ext_it)) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7850 | auto &extensions = detail::GetValue(ext_it); |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 7851 | detail::json_iterator lp_it; |
| 7852 | if (detail::FindMember(extensions, "KHR_lights_punctual", lp_it)) { |
| 7853 | detail::Erase(extensions, lp_it); |
| 7854 | } |
| 7855 | if (detail::IsEmpty(extensions)) { |
| 7856 | detail::Erase(o, ext_it); |
| 7857 | } |
| 7858 | } |
| 7859 | } |
| 7860 | |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 7861 | // KHR_audio |
| 7862 | if (node.emitter != -1) { |
| 7863 | detail::json_iterator it; |
| 7864 | if (!detail::FindMember(o, "extensions", it)) { |
| 7865 | detail::json extensions; |
| 7866 | detail::JsonSetObject(extensions); |
| 7867 | detail::JsonAddMember(o, "extensions", std::move(extensions)); |
| 7868 | detail::FindMember(o, "extensions", it); |
| 7869 | } |
| 7870 | auto &extensions = detail::GetValue(it); |
| 7871 | if (!detail::FindMember(extensions, "KHR_audio", it)) { |
| 7872 | detail::json audio; |
| 7873 | detail::JsonSetObject(audio); |
| 7874 | detail::JsonAddMember(extensions, "KHR_audio", std::move(audio)); |
Thomas Gamper | f1bdf43 | 2024-02-05 16:50:46 +0100 | [diff] [blame] | 7875 | detail::FindMember(extensions, "KHR_audio", it); |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 7876 | } |
| 7877 | SerializeNumberProperty("emitter", node.emitter, detail::GetValue(it)); |
| 7878 | } else { |
| 7879 | detail::json_iterator ext_it; |
| 7880 | if (detail::FindMember(o, "extensions", ext_it)) { |
| 7881 | auto &extensions = detail::GetValue(ext_it); |
| 7882 | detail::json_iterator lp_it; |
| 7883 | if (detail::FindMember(extensions, "KHR_audio", lp_it)) { |
| 7884 | detail::Erase(extensions, lp_it); |
| 7885 | } |
| 7886 | if (detail::IsEmpty(extensions)) { |
| 7887 | detail::Erase(o, ext_it); |
| 7888 | } |
| 7889 | } |
| 7890 | } |
| 7891 | |
Thomas Gamper | f1bdf43 | 2024-02-05 16:50:46 +0100 | [diff] [blame] | 7892 | // MSFT_lod |
| 7893 | if (!node.lods.empty()) { |
| 7894 | detail::json_iterator it; |
| 7895 | if (!detail::FindMember(o, "extensions", it)) { |
| 7896 | detail::json extensions; |
| 7897 | detail::JsonSetObject(extensions); |
| 7898 | detail::JsonAddMember(o, "extensions", std::move(extensions)); |
| 7899 | detail::FindMember(o, "extensions", it); |
| 7900 | } |
| 7901 | auto &extensions = detail::GetValue(it); |
| 7902 | if (!detail::FindMember(extensions, "MSFT_lod", it)) { |
| 7903 | detail::json lod; |
| 7904 | detail::JsonSetObject(lod); |
| 7905 | detail::JsonAddMember(extensions, "MSFT_lod", std::move(lod)); |
| 7906 | detail::FindMember(extensions, "MSFT_lod", it); |
| 7907 | } |
| 7908 | SerializeNumberArrayProperty<int>("ids", node.lods, detail::GetValue(it)); |
| 7909 | } else { |
| 7910 | detail::json_iterator ext_it; |
| 7911 | if (detail::FindMember(o, "extensions", ext_it)) { |
| 7912 | auto &extensions = detail::GetValue(ext_it); |
| 7913 | detail::json_iterator lp_it; |
| 7914 | if (detail::FindMember(extensions, "MSFT_lod", lp_it)) { |
| 7915 | detail::Erase(extensions, lp_it); |
| 7916 | } |
| 7917 | if (detail::IsEmpty(extensions)) { |
| 7918 | detail::Erase(o, ext_it); |
| 7919 | } |
| 7920 | } |
| 7921 | } |
| 7922 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 7923 | if (!node.name.empty()) SerializeStringProperty("name", node.name, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7924 | SerializeNumberArrayProperty<int>("children", node.children, o); |
| 7925 | } |
| 7926 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7927 | static void SerializeGltfSampler(const Sampler &sampler, detail::json &o) { |
s00665032 | 137a7ca | 2023-01-13 12:52:08 +0700 | [diff] [blame] | 7928 | if (!sampler.name.empty()) { |
| 7929 | SerializeStringProperty("name", sampler.name, o); |
| 7930 | } |
Syoyo Fujita | ee179b2 | 2019-08-16 13:11:30 +0900 | [diff] [blame] | 7931 | if (sampler.magFilter != -1) { |
| 7932 | SerializeNumberProperty("magFilter", sampler.magFilter, o); |
| 7933 | } |
| 7934 | if (sampler.minFilter != -1) { |
| 7935 | SerializeNumberProperty("minFilter", sampler.minFilter, o); |
| 7936 | } |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 7937 | // SerializeNumberProperty("wrapR", sampler.wrapR, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7938 | SerializeNumberProperty("wrapS", sampler.wrapS, o); |
| 7939 | SerializeNumberProperty("wrapT", sampler.wrapT, o); |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7940 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7941 | SerializeExtrasAndExtensions(sampler, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7942 | } |
| 7943 | |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7944 | static void SerializeGltfOrthographicCamera(const OrthographicCamera &camera, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7945 | detail::json &o) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7946 | SerializeNumberProperty("zfar", camera.zfar, o); |
| 7947 | SerializeNumberProperty("znear", camera.znear, o); |
| 7948 | SerializeNumberProperty("xmag", camera.xmag, o); |
| 7949 | SerializeNumberProperty("ymag", camera.ymag, o); |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7950 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7951 | SerializeExtrasAndExtensions(camera, o); |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7952 | } |
| 7953 | |
| 7954 | static void SerializeGltfPerspectiveCamera(const PerspectiveCamera &camera, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7955 | detail::json &o) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7956 | SerializeNumberProperty("zfar", camera.zfar, o); |
| 7957 | SerializeNumberProperty("znear", camera.znear, o); |
| 7958 | if (camera.aspectRatio > 0) { |
| 7959 | SerializeNumberProperty("aspectRatio", camera.aspectRatio, o); |
| 7960 | } |
| 7961 | |
| 7962 | if (camera.yfov > 0) { |
| 7963 | SerializeNumberProperty("yfov", camera.yfov, o); |
| 7964 | } |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7965 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7966 | SerializeExtrasAndExtensions(camera, o); |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7967 | } |
| 7968 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7969 | static void SerializeGltfCamera(const Camera &camera, detail::json &o) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7970 | SerializeStringProperty("type", camera.type, o); |
| 7971 | if (!camera.name.empty()) { |
Selmar Kok | ee3d066 | 2018-10-08 16:20:43 +0200 | [diff] [blame] | 7972 | SerializeStringProperty("name", camera.name, o); |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7973 | } |
| 7974 | |
| 7975 | if (camera.type.compare("orthographic") == 0) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7976 | detail::json orthographic; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7977 | SerializeGltfOrthographicCamera(camera.orthographic, orthographic); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7978 | detail::JsonAddMember(o, "orthographic", std::move(orthographic)); |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7979 | } else if (camera.type.compare("perspective") == 0) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7980 | detail::json perspective; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7981 | SerializeGltfPerspectiveCamera(camera.perspective, perspective); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7982 | detail::JsonAddMember(o, "perspective", std::move(perspective)); |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7983 | } else { |
| 7984 | // ??? |
| 7985 | } |
Syoyo | fe77cc5 | 2020-05-09 02:41:07 +0900 | [diff] [blame] | 7986 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7987 | SerializeExtrasAndExtensions(camera, o); |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7988 | } |
| 7989 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7990 | static void SerializeGltfScene(const Scene &scene, detail::json &o) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7991 | SerializeNumberArrayProperty<int>("nodes", scene.nodes, o); |
| 7992 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7993 | if (scene.name.size()) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7994 | SerializeStringProperty("name", scene.name, o); |
| 7995 | } |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7996 | SerializeExtrasAndExtensions(scene, o); |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 7997 | |
| 7998 | // KHR_audio |
| 7999 | if (!scene.audioEmitters.empty()) { |
| 8000 | detail::json_iterator it; |
| 8001 | if (!detail::FindMember(o, "extensions", it)) { |
| 8002 | detail::json extensions; |
| 8003 | detail::JsonSetObject(extensions); |
| 8004 | detail::JsonAddMember(o, "extensions", std::move(extensions)); |
| 8005 | detail::FindMember(o, "extensions", it); |
| 8006 | } |
| 8007 | auto &extensions = detail::GetValue(it); |
| 8008 | if (!detail::FindMember(extensions, "KHR_audio", it)) { |
| 8009 | detail::json audio; |
| 8010 | detail::JsonSetObject(audio); |
| 8011 | detail::JsonAddMember(extensions, "KHR_audio", std::move(audio)); |
| 8012 | detail::FindMember(o, "KHR_audio", it); |
| 8013 | } |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 8014 | SerializeNumberArrayProperty("emitters", scene.audioEmitters, |
| 8015 | detail::GetValue(it)); |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 8016 | } else { |
| 8017 | detail::json_iterator ext_it; |
| 8018 | if (detail::FindMember(o, "extensions", ext_it)) { |
| 8019 | auto &extensions = detail::GetValue(ext_it); |
| 8020 | detail::json_iterator lp_it; |
| 8021 | if (detail::FindMember(extensions, "KHR_audio", lp_it)) { |
| 8022 | detail::Erase(extensions, lp_it); |
| 8023 | } |
| 8024 | if (detail::IsEmpty(extensions)) { |
| 8025 | detail::Erase(o, ext_it); |
| 8026 | } |
| 8027 | } |
| 8028 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8029 | } |
| 8030 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8031 | static void SerializeGltfSkin(const Skin &skin, detail::json &o) { |
Syoyo Fujita | d07b976 | 2021-04-28 19:15:16 +0900 | [diff] [blame] | 8032 | // required |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8033 | SerializeNumberArrayProperty<int>("joints", skin.joints, o); |
Syoyo Fujita | d07b976 | 2021-04-28 19:15:16 +0900 | [diff] [blame] | 8034 | |
| 8035 | if (skin.inverseBindMatrices >= 0) { |
| 8036 | SerializeNumberProperty("inverseBindMatrices", skin.inverseBindMatrices, o); |
| 8037 | } |
| 8038 | |
| 8039 | if (skin.skeleton >= 0) { |
| 8040 | SerializeNumberProperty("skeleton", skin.skeleton, o); |
| 8041 | } |
| 8042 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 8043 | if (skin.name.size()) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8044 | SerializeStringProperty("name", skin.name, o); |
| 8045 | } |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 8046 | |
| 8047 | SerializeExtrasAndExtensions(skin, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8048 | } |
| 8049 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8050 | static void SerializeGltfTexture(const Texture &texture, detail::json &o) { |
johan bowald | b97d34c | 2018-04-02 07:29:29 +0200 | [diff] [blame] | 8051 | if (texture.sampler > -1) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 8052 | SerializeNumberProperty("sampler", texture.sampler, o); |
| 8053 | } |
Selmar Kok | 8eb3904 | 2018-10-05 14:29:35 +0200 | [diff] [blame] | 8054 | if (texture.source > -1) { |
| 8055 | SerializeNumberProperty("source", texture.source, o); |
| 8056 | } |
Christophe | 820ede8 | 2019-07-04 15:21:21 +0900 | [diff] [blame] | 8057 | if (texture.name.size()) { |
| 8058 | SerializeStringProperty("name", texture.name, o); |
| 8059 | } |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 8060 | SerializeExtrasAndExtensions(texture, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8061 | } |
| 8062 | |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8063 | /// |
| 8064 | /// Serialize all properties except buffers and images. |
| 8065 | /// |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8066 | static void SerializeGltfModel(const Model *model, detail::json &o) { |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 8067 | // ACCESSORS |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8068 | if (model->accessors.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8069 | detail::json accessors; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8070 | detail::JsonReserveArray(accessors, model->accessors.size()); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8071 | for (unsigned int i = 0; i < model->accessors.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8072 | detail::json accessor; |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8073 | SerializeGltfAccessor(model->accessors[i], accessor); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8074 | detail::JsonPushBack(accessors, std::move(accessor)); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8075 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8076 | detail::JsonAddMember(o, "accessors", std::move(accessors)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8077 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8078 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 8079 | // ANIMATIONS |
| 8080 | if (model->animations.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8081 | detail::json animations; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8082 | detail::JsonReserveArray(animations, model->animations.size()); |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 8083 | for (unsigned int i = 0; i < model->animations.size(); ++i) { |
| 8084 | if (model->animations[i].channels.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8085 | detail::json animation; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8086 | SerializeGltfAnimation(model->animations[i], animation); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8087 | detail::JsonPushBack(animations, std::move(animation)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8088 | } |
| 8089 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 8090 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8091 | detail::JsonAddMember(o, "animations", std::move(animations)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8092 | } |
| 8093 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 8094 | // ASSET |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8095 | detail::json asset; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8096 | SerializeGltfAsset(model->asset, asset); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8097 | detail::JsonAddMember(o, "asset", std::move(asset)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8098 | |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8099 | // BUFFERVIEWS |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8100 | if (model->bufferViews.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8101 | detail::json bufferViews; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8102 | detail::JsonReserveArray(bufferViews, model->bufferViews.size()); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8103 | for (unsigned int i = 0; i < model->bufferViews.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8104 | detail::json bufferView; |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8105 | SerializeGltfBufferView(model->bufferViews[i], bufferView); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8106 | detail::JsonPushBack(bufferViews, std::move(bufferView)); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8107 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8108 | detail::JsonAddMember(o, "bufferViews", std::move(bufferViews)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8109 | } |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8110 | |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8111 | // Extensions required |
| 8112 | if (model->extensionsRequired.size()) { |
| 8113 | SerializeStringArrayProperty("extensionsRequired", |
| 8114 | model->extensionsRequired, o); |
| 8115 | } |
| 8116 | |
| 8117 | // MATERIALS |
| 8118 | if (model->materials.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8119 | detail::json materials; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8120 | detail::JsonReserveArray(materials, model->materials.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8121 | for (unsigned int i = 0; i < model->materials.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8122 | detail::json material; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8123 | SerializeGltfMaterial(model->materials[i], material); |
Syoyo Fujita | 68adc4b | 2020-10-22 22:27:12 +0900 | [diff] [blame] | 8124 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8125 | if (detail::JsonIsNull(material)) { |
Syoyo Fujita | 68adc4b | 2020-10-22 22:27:12 +0900 | [diff] [blame] | 8126 | // Issue 294. |
| 8127 | // `material` does not have any required parameters |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 8128 | // so the result may be null(unmodified) when all material parameters |
| 8129 | // have default value. |
Syoyo Fujita | 68adc4b | 2020-10-22 22:27:12 +0900 | [diff] [blame] | 8130 | // |
| 8131 | // null is not allowed thus we create an empty JSON object. |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8132 | detail::JsonSetObject(material); |
Syoyo Fujita | 68adc4b | 2020-10-22 22:27:12 +0900 | [diff] [blame] | 8133 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8134 | detail::JsonPushBack(materials, std::move(material)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8135 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8136 | detail::JsonAddMember(o, "materials", std::move(materials)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8137 | } |
| 8138 | |
| 8139 | // MESHES |
| 8140 | if (model->meshes.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8141 | detail::json meshes; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8142 | detail::JsonReserveArray(meshes, model->meshes.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8143 | for (unsigned int i = 0; i < model->meshes.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8144 | detail::json mesh; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8145 | SerializeGltfMesh(model->meshes[i], mesh); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8146 | detail::JsonPushBack(meshes, std::move(mesh)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8147 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8148 | detail::JsonAddMember(o, "meshes", std::move(meshes)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8149 | } |
| 8150 | |
| 8151 | // NODES |
| 8152 | if (model->nodes.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8153 | detail::json nodes; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8154 | detail::JsonReserveArray(nodes, model->nodes.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8155 | for (unsigned int i = 0; i < model->nodes.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8156 | detail::json node; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8157 | SerializeGltfNode(model->nodes[i], node); |
Thomas Gamper | d4ea67c | 2023-11-23 11:59:18 +0100 | [diff] [blame] | 8158 | |
| 8159 | if (detail::JsonIsNull(node)) { |
| 8160 | // Issue 457. |
| 8161 | // `node` does not have any required parameters, |
| 8162 | // so the result may be null(unmodified) when all node parameters |
| 8163 | // have default value. |
| 8164 | // |
| 8165 | // null is not allowed thus we create an empty JSON object. |
| 8166 | detail::JsonSetObject(node); |
| 8167 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8168 | detail::JsonPushBack(nodes, std::move(node)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8169 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8170 | detail::JsonAddMember(o, "nodes", std::move(nodes)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8171 | } |
| 8172 | |
| 8173 | // SCENE |
| 8174 | if (model->defaultScene > -1) { |
| 8175 | SerializeNumberProperty<int>("scene", model->defaultScene, o); |
| 8176 | } |
| 8177 | |
| 8178 | // SCENES |
| 8179 | if (model->scenes.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8180 | detail::json scenes; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8181 | detail::JsonReserveArray(scenes, model->scenes.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8182 | for (unsigned int i = 0; i < model->scenes.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8183 | detail::json currentScene; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8184 | SerializeGltfScene(model->scenes[i], currentScene); |
Thomas Gamper | 3203e19 | 2023-11-23 15:14:46 +0100 | [diff] [blame] | 8185 | if (detail::JsonIsNull(currentScene)) { |
| 8186 | // Issue 464. |
| 8187 | // `scene` does not have any required parameters, |
| 8188 | // so the result may be null(unmodified) when all scene parameters |
| 8189 | // have default value. |
| 8190 | // |
| 8191 | // null is not allowed thus we create an empty JSON object. |
| 8192 | detail::JsonSetObject(currentScene); |
| 8193 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8194 | detail::JsonPushBack(scenes, std::move(currentScene)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8195 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8196 | detail::JsonAddMember(o, "scenes", std::move(scenes)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8197 | } |
| 8198 | |
| 8199 | // SKINS |
| 8200 | if (model->skins.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8201 | detail::json skins; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8202 | detail::JsonReserveArray(skins, model->skins.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8203 | for (unsigned int i = 0; i < model->skins.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8204 | detail::json skin; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8205 | SerializeGltfSkin(model->skins[i], skin); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8206 | detail::JsonPushBack(skins, std::move(skin)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8207 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8208 | detail::JsonAddMember(o, "skins", std::move(skins)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8209 | } |
| 8210 | |
| 8211 | // TEXTURES |
| 8212 | if (model->textures.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8213 | detail::json textures; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8214 | detail::JsonReserveArray(textures, model->textures.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8215 | for (unsigned int i = 0; i < model->textures.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8216 | detail::json texture; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8217 | SerializeGltfTexture(model->textures[i], texture); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8218 | detail::JsonPushBack(textures, std::move(texture)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8219 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8220 | detail::JsonAddMember(o, "textures", std::move(textures)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8221 | } |
| 8222 | |
| 8223 | // SAMPLERS |
| 8224 | if (model->samplers.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8225 | detail::json samplers; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8226 | detail::JsonReserveArray(samplers, model->samplers.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8227 | for (unsigned int i = 0; i < model->samplers.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8228 | detail::json sampler; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8229 | SerializeGltfSampler(model->samplers[i], sampler); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8230 | detail::JsonPushBack(samplers, std::move(sampler)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8231 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8232 | detail::JsonAddMember(o, "samplers", std::move(samplers)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8233 | } |
| 8234 | |
| 8235 | // CAMERAS |
| 8236 | if (model->cameras.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8237 | detail::json cameras; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8238 | detail::JsonReserveArray(cameras, model->cameras.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8239 | for (unsigned int i = 0; i < model->cameras.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8240 | detail::json camera; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8241 | SerializeGltfCamera(model->cameras[i], camera); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8242 | detail::JsonPushBack(cameras, std::move(camera)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8243 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8244 | detail::JsonAddMember(o, "cameras", std::move(cameras)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8245 | } |
| 8246 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 8247 | // EXTRAS & EXTENSIONS |
| 8248 | SerializeExtrasAndExtensions(*model, o); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8249 | |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8250 | auto extensionsUsed = model->extensionsUsed; |
| 8251 | |
| 8252 | // LIGHTS as KHR_lights_punctual |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8253 | if (model->lights.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8254 | detail::json lights; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8255 | detail::JsonReserveArray(lights, model->lights.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8256 | for (unsigned int i = 0; i < model->lights.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8257 | detail::json light; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8258 | SerializeGltfLight(model->lights[i], light); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8259 | detail::JsonPushBack(lights, std::move(light)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8260 | } |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8261 | detail::json khr_lights_cmn; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8262 | detail::JsonAddMember(khr_lights_cmn, "lights", std::move(lights)); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8263 | detail::json ext_j; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8264 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 8265 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8266 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8267 | if (detail::FindMember(o, "extensions", it)) { |
| 8268 | detail::JsonAssign(ext_j, detail::GetValue(it)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 8269 | } |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8270 | } |
| 8271 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 8272 | detail::JsonAddMember(ext_j, "KHR_lights_punctual", |
| 8273 | std::move(khr_lights_cmn)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8274 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8275 | detail::JsonAddMember(o, "extensions", std::move(ext_j)); |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8276 | |
| 8277 | // Also add "KHR_lights_punctual" to `extensionsUsed` |
| 8278 | { |
Rahul Sheth | 96a8b2c | 2020-07-10 14:27:46 -0400 | [diff] [blame] | 8279 | auto has_khr_lights_punctual = |
| 8280 | std::find_if(extensionsUsed.begin(), extensionsUsed.end(), |
| 8281 | [](const std::string &s) { |
cwbhhjl | 2f5aa9f | 2020-06-04 10:40:38 +0800 | [diff] [blame] | 8282 | return (s.compare("KHR_lights_punctual") == 0); |
| 8283 | }); |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8284 | |
| 8285 | if (has_khr_lights_punctual == extensionsUsed.end()) { |
| 8286 | extensionsUsed.push_back("KHR_lights_punctual"); |
| 8287 | } |
| 8288 | } |
| 8289 | } |
| 8290 | |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 8291 | // KHR_audio |
| 8292 | if (!model->audioEmitters.empty() || !model->audioSources.empty()) { |
| 8293 | detail::json emitters; |
| 8294 | detail::JsonReserveArray(emitters, model->audioEmitters.size()); |
| 8295 | for (unsigned int i = 0; i < model->audioEmitters.size(); ++i) { |
| 8296 | detail::json emitter; |
| 8297 | SerializeGltfAudioEmitter(model->audioEmitters[i], emitter); |
| 8298 | detail::JsonPushBack(emitters, std::move(emitter)); |
| 8299 | } |
| 8300 | detail::json khr_audio_cmn; |
| 8301 | detail::JsonAddMember(khr_audio_cmn, "emitters", std::move(emitters)); |
| 8302 | |
| 8303 | detail::json sources; |
| 8304 | detail::JsonReserveArray(sources, model->audioSources.size()); |
| 8305 | for (unsigned int i = 0; i < model->audioSources.size(); ++i) { |
| 8306 | detail::json source; |
| 8307 | SerializeGltfAudioSource(model->audioSources[i], source); |
| 8308 | detail::JsonPushBack(sources, std::move(source)); |
| 8309 | } |
| 8310 | detail::JsonAddMember(khr_audio_cmn, "sources", std::move(sources)); |
| 8311 | |
| 8312 | detail::json ext_j; |
| 8313 | { |
| 8314 | detail::json_const_iterator it; |
| 8315 | if (detail::FindMember(o, "extensions", it)) { |
| 8316 | detail::JsonAssign(ext_j, detail::GetValue(it)); |
| 8317 | } |
| 8318 | } |
| 8319 | |
| 8320 | detail::JsonAddMember(ext_j, "KHR_audio", std::move(khr_audio_cmn)); |
| 8321 | |
| 8322 | detail::JsonAddMember(o, "extensions", std::move(ext_j)); |
Baranob_Ilya | c9657be | 2023-06-12 12:34:34 +0400 | [diff] [blame] | 8323 | |
| 8324 | // Also add "KHR_audio" to `extensionsUsed` |
| 8325 | { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 8326 | auto has_khr_audio = std::find_if( |
| 8327 | extensionsUsed.begin(), extensionsUsed.end(), |
| 8328 | [](const std::string &s) { return (s.compare("KHR_audio") == 0); }); |
Baranob_Ilya | c9657be | 2023-06-12 12:34:34 +0400 | [diff] [blame] | 8329 | |
| 8330 | if (has_khr_audio == extensionsUsed.end()) { |
| 8331 | extensionsUsed.push_back("KHR_audio"); |
| 8332 | } |
| 8333 | } |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 8334 | } |
| 8335 | |
Thomas Gamper | b274b34 | 2024-03-25 17:00:41 +0100 | [diff] [blame] | 8336 | // MSFT_lod |
| 8337 | |
| 8338 | // Look if there is a node that employs MSFT_lod |
| 8339 | auto msft_lod_nodes_it = std::find_if( |
| 8340 | model->nodes.begin(), model->nodes.end(), |
| 8341 | [](const Node& node) { return !node.lods.empty(); }); |
| 8342 | |
| 8343 | // Look if there is a material that employs MSFT_lod |
| 8344 | auto msft_lod_materials_it = std::find_if( |
| 8345 | model->materials.begin(), model->materials.end(), |
| 8346 | [](const Material& material) {return !material.lods.empty(); }); |
| 8347 | |
| 8348 | // If either a node or a material employ MSFT_lod, then we need |
| 8349 | // to add MSFT_lod to the list of used extensions. |
| 8350 | if (msft_lod_nodes_it != model->nodes.end() || msft_lod_materials_it != model->materials.end()) { |
| 8351 | // First check if MSFT_lod is already registered as used extension |
| 8352 | auto has_msft_lod = std::find_if( |
| 8353 | extensionsUsed.begin(), extensionsUsed.end(), |
| 8354 | [](const std::string &s) { return (s.compare("MSFT_lod") == 0); }); |
| 8355 | |
| 8356 | // If MSFT_lod is not registered yet, add it |
| 8357 | if (has_msft_lod == extensionsUsed.end()) { |
| 8358 | extensionsUsed.push_back("MSFT_lod"); |
| 8359 | } |
| 8360 | } |
| 8361 | |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8362 | // Extensions used |
Selmar | 1208f05 | 2021-02-01 19:24:41 +0100 | [diff] [blame] | 8363 | if (extensionsUsed.size()) { |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8364 | SerializeStringArrayProperty("extensionsUsed", extensionsUsed, o); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8365 | } |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8366 | } |
| 8367 | |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 8368 | static bool WriteGltfStream(std::ostream &stream, const std::string &content) { |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8369 | stream << content << std::endl; |
Marco Langer | 7658624 | 2023-03-12 19:26:05 +0100 | [diff] [blame] | 8370 | return stream.good(); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8371 | } |
| 8372 | |
| 8373 | static bool WriteGltfFile(const std::string &output, |
| 8374 | const std::string &content) { |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 8375 | #ifdef _WIN32 |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 8376 | #if defined(_MSC_VER) |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 8377 | std::ofstream gltfFile(UTF8ToWchar(output).c_str()); |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 8378 | #elif defined(__GLIBCXX__) |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8379 | int file_descriptor = _wopen(UTF8ToWchar(output).c_str(), |
TheDev | 22dfeab | 2024-03-25 18:21:20 +0300 | [diff] [blame] | 8380 | _O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY, _S_IWRITE); |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8381 | __gnu_cxx::stdio_filebuf<char> wfile_buf( |
| 8382 | file_descriptor, std::ios_base::out | std::ios_base::binary); |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 8383 | std::ostream gltfFile(&wfile_buf); |
| 8384 | if (!wfile_buf.is_open()) return false; |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 8385 | #else |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8386 | std::ofstream gltfFile(output.c_str()); |
| 8387 | if (!gltfFile.is_open()) return false; |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 8388 | #endif |
| 8389 | #else |
| 8390 | std::ofstream gltfFile(output.c_str()); |
| 8391 | if (!gltfFile.is_open()) return false; |
| 8392 | #endif |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8393 | return WriteGltfStream(gltfFile, content); |
| 8394 | } |
| 8395 | |
Syoyo Fujita | 81f7dbe | 2022-08-02 01:05:34 +0900 | [diff] [blame] | 8396 | static bool WriteBinaryGltfStream(std::ostream &stream, |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8397 | const std::string &content, |
| 8398 | const std::vector<unsigned char> &binBuffer) { |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8399 | const std::string header = "glTF"; |
| 8400 | const int version = 2; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8401 | |
Alexander Wood | 190382a | 2021-10-08 12:19:13 -0400 | [diff] [blame] | 8402 | const uint32_t content_size = uint32_t(content.size()); |
| 8403 | const uint32_t binBuffer_size = uint32_t(binBuffer.size()); |
| 8404 | // determine number of padding bytes required to ensure 4 byte alignment |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 8405 | const uint32_t content_padding_size = |
| 8406 | content_size % 4 == 0 ? 0 : 4 - content_size % 4; |
| 8407 | const uint32_t bin_padding_size = |
| 8408 | binBuffer_size % 4 == 0 ? 0 : 4 - binBuffer_size % 4; |
Syoyo Fujita | 1d20520 | 2019-11-16 17:00:17 +0900 | [diff] [blame] | 8409 | |
| 8410 | // 12 bytes for header, JSON content length, 8 bytes for JSON chunk info. |
Alexander Wood | 190382a | 2021-10-08 12:19:13 -0400 | [diff] [blame] | 8411 | // Chunk data must be located at 4-byte boundary, which may require padding |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 8412 | const uint32_t length = |
Alexander Wood | 9e3d1f6 | 2021-10-08 16:57:56 -0400 | [diff] [blame] | 8413 | 12 + 8 + content_size + content_padding_size + |
Alexander Wood | 1380365 | 2021-10-08 20:13:07 -0400 | [diff] [blame] | 8414 | (binBuffer_size ? (8 + binBuffer_size + bin_padding_size) : 0); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8415 | |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 8416 | stream.write(header.c_str(), std::streamsize(header.size())); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8417 | stream.write(reinterpret_cast<const char *>(&version), sizeof(version)); |
| 8418 | stream.write(reinterpret_cast<const char *>(&length), sizeof(length)); |
| 8419 | |
| 8420 | // JSON chunk info, then JSON data |
Alexander Wood | 9e3d1f6 | 2021-10-08 16:57:56 -0400 | [diff] [blame] | 8421 | const uint32_t model_length = uint32_t(content.size()) + content_padding_size; |
Syoyo Fujita | 72f4a55 | 2020-01-08 00:40:41 +0900 | [diff] [blame] | 8422 | const uint32_t model_format = 0x4E4F534A; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8423 | stream.write(reinterpret_cast<const char *>(&model_length), |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 8424 | sizeof(model_length)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8425 | stream.write(reinterpret_cast<const char *>(&model_format), |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 8426 | sizeof(model_format)); |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 8427 | stream.write(content.c_str(), std::streamsize(content.size())); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8428 | |
| 8429 | // Chunk must be multiplies of 4, so pad with spaces |
Alexander Wood | 9e3d1f6 | 2021-10-08 16:57:56 -0400 | [diff] [blame] | 8430 | if (content_padding_size > 0) { |
| 8431 | const std::string padding = std::string(size_t(content_padding_size), ' '); |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 8432 | stream.write(padding.c_str(), std::streamsize(padding.size())); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8433 | } |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 8434 | if (binBuffer.size() > 0) { |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8435 | // BIN chunk info, then BIN data |
Syoyo Fujita | 72f4a55 | 2020-01-08 00:40:41 +0900 | [diff] [blame] | 8436 | const uint32_t bin_length = uint32_t(binBuffer.size()) + bin_padding_size; |
| 8437 | const uint32_t bin_format = 0x004e4942; |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8438 | stream.write(reinterpret_cast<const char *>(&bin_length), |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 8439 | sizeof(bin_length)); |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8440 | stream.write(reinterpret_cast<const char *>(&bin_format), |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 8441 | sizeof(bin_format)); |
| 8442 | stream.write(reinterpret_cast<const char *>(binBuffer.data()), |
| 8443 | std::streamsize(binBuffer.size())); |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8444 | // Chunksize must be multiplies of 4, so pad with zeroes |
| 8445 | if (bin_padding_size > 0) { |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 8446 | const std::vector<unsigned char> padding = |
| 8447 | std::vector<unsigned char>(size_t(bin_padding_size), 0); |
| 8448 | stream.write(reinterpret_cast<const char *>(padding.data()), |
| 8449 | std::streamsize(padding.size())); |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8450 | } |
| 8451 | } |
Syoyo Fujita | 81f7dbe | 2022-08-02 01:05:34 +0900 | [diff] [blame] | 8452 | |
Marco Langer | 7658624 | 2023-03-12 19:26:05 +0100 | [diff] [blame] | 8453 | stream.flush(); |
| 8454 | return stream.good(); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8455 | } |
| 8456 | |
Syoyo Fujita | 81f7dbe | 2022-08-02 01:05:34 +0900 | [diff] [blame] | 8457 | static bool WriteBinaryGltfFile(const std::string &output, |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8458 | const std::string &content, |
| 8459 | const std::vector<unsigned char> &binBuffer) { |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 8460 | #ifdef _WIN32 |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 8461 | #if defined(_MSC_VER) |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 8462 | std::ofstream gltfFile(UTF8ToWchar(output).c_str(), std::ios::binary); |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 8463 | #elif defined(__GLIBCXX__) |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8464 | int file_descriptor = _wopen(UTF8ToWchar(output).c_str(), |
TheDev | 22dfeab | 2024-03-25 18:21:20 +0300 | [diff] [blame] | 8465 | _O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY, _S_IWRITE); |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8466 | __gnu_cxx::stdio_filebuf<char> wfile_buf( |
| 8467 | file_descriptor, std::ios_base::out | std::ios_base::binary); |
Syoyo Fujita | 4ab0386 | 2019-11-10 15:31:17 +0900 | [diff] [blame] | 8468 | std::ostream gltfFile(&wfile_buf); |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 8469 | #else |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8470 | std::ofstream gltfFile(output.c_str(), std::ios::binary); |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 8471 | #endif |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8472 | #else |
| 8473 | std::ofstream gltfFile(output.c_str(), std::ios::binary); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 8474 | #endif |
Syoyo Fujita | 81f7dbe | 2022-08-02 01:05:34 +0900 | [diff] [blame] | 8475 | return WriteBinaryGltfStream(gltfFile, content, binBuffer); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8476 | } |
| 8477 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 8478 | bool TinyGLTF::WriteGltfSceneToStream(const Model *model, std::ostream &stream, |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8479 | bool prettyPrint = true, |
| 8480 | bool writeBinary = false) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8481 | detail::JsonDocument output; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8482 | |
| 8483 | /// Serialize all properties except buffers and images. |
| 8484 | SerializeGltfModel(model, output); |
| 8485 | |
| 8486 | // BUFFERS |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8487 | std::vector<unsigned char> binBuffer; |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8488 | if (model->buffers.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8489 | detail::json buffers; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8490 | detail::JsonReserveArray(buffers, model->buffers.size()); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8491 | for (unsigned int i = 0; i < model->buffers.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8492 | detail::json buffer; |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8493 | if (writeBinary && i == 0 && model->buffers[i].uri.empty()) { |
| 8494 | SerializeGltfBufferBin(model->buffers[i], buffer, binBuffer); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8495 | } else { |
| 8496 | SerializeGltfBuffer(model->buffers[i], buffer); |
| 8497 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8498 | detail::JsonPushBack(buffers, std::move(buffer)); |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8499 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8500 | detail::JsonAddMember(output, "buffers", std::move(buffers)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8501 | } |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8502 | |
| 8503 | // IMAGES |
| 8504 | if (model->images.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8505 | detail::json images; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8506 | detail::JsonReserveArray(images, model->images.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8507 | for (unsigned int i = 0; i < model->images.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8508 | detail::json image; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8509 | |
Bryn Lloyd | c704d73 | 2023-06-21 18:42:24 +0200 | [diff] [blame] | 8510 | std::string dummystring; |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8511 | // UpdateImageObject need baseDir but only uses it if embeddedImages is |
| 8512 | // enabled, since we won't write separate images when writing to a stream |
| 8513 | // we |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 8514 | std::string uri; |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 8515 | if (!UpdateImageObject(model->images[i], dummystring, int(i), true, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8516 | &uri_cb, &this->WriteImageData, |
| 8517 | this->write_image_user_data_, &uri)) { |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 8518 | return false; |
| 8519 | } |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 8520 | SerializeGltfImage(model->images[i], uri, image); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8521 | detail::JsonPushBack(images, std::move(image)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8522 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8523 | detail::JsonAddMember(output, "images", std::move(images)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8524 | } |
| 8525 | |
| 8526 | if (writeBinary) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 8527 | return WriteBinaryGltfStream(stream, detail::JsonToString(output), |
| 8528 | binBuffer); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8529 | } else { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 8530 | return WriteGltfStream(stream, |
| 8531 | detail::JsonToString(output, prettyPrint ? 2 : -1)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8532 | } |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8533 | } |
| 8534 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 8535 | bool TinyGLTF::WriteGltfSceneToFile(const Model *model, |
| 8536 | const std::string &filename, |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8537 | bool embedImages = false, |
| 8538 | bool embedBuffers = false, |
| 8539 | bool prettyPrint = true, |
| 8540 | bool writeBinary = false) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8541 | detail::JsonDocument output; |
Selmar Kok | 7cb31e4 | 2018-10-05 16:02:29 +0200 | [diff] [blame] | 8542 | std::string defaultBinFilename = GetBaseFilename(filename); |
| 8543 | std::string defaultBinFileExt = ".bin"; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 8544 | std::string::size_type pos = |
| 8545 | defaultBinFilename.rfind('.', defaultBinFilename.length()); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8546 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 8547 | if (pos != std::string::npos) { |
Selmar Kok | 7cb31e4 | 2018-10-05 16:02:29 +0200 | [diff] [blame] | 8548 | defaultBinFilename = defaultBinFilename.substr(0, pos); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8549 | } |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 8550 | std::string baseDir = GetBaseDir(filename); |
| 8551 | if (baseDir.empty()) { |
| 8552 | baseDir = "./"; |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 8553 | } |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 8554 | /// Serialize all properties except buffers and images. |
| 8555 | SerializeGltfModel(model, output); |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 8556 | |
Selmar Kok | 7cb31e4 | 2018-10-05 16:02:29 +0200 | [diff] [blame] | 8557 | // BUFFERS |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8558 | std::vector<std::string> usedFilenames; |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8559 | std::vector<unsigned char> binBuffer; |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8560 | if (model->buffers.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8561 | detail::json buffers; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8562 | detail::JsonReserveArray(buffers, model->buffers.size()); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8563 | for (unsigned int i = 0; i < model->buffers.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8564 | detail::json buffer; |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8565 | if (writeBinary && i == 0 && model->buffers[i].uri.empty()) { |
| 8566 | SerializeGltfBufferBin(model->buffers[i], buffer, binBuffer); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8567 | } else if (embedBuffers) { |
| 8568 | SerializeGltfBuffer(model->buffers[i], buffer); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 8569 | } else { |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8570 | std::string binSavePath; |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8571 | std::string binFilename; |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8572 | std::string binUri; |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8573 | if (!model->buffers[i].uri.empty() && |
| 8574 | !IsDataURI(model->buffers[i].uri)) { |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8575 | binUri = model->buffers[i].uri; |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8576 | if (!uri_cb.decode(binUri, &binFilename, uri_cb.user_data)) { |
| 8577 | return false; |
| 8578 | } |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8579 | } else { |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8580 | binFilename = defaultBinFilename + defaultBinFileExt; |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8581 | bool inUse = true; |
| 8582 | int numUsed = 0; |
| 8583 | while (inUse) { |
| 8584 | inUse = false; |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8585 | for (const std::string &usedName : usedFilenames) { |
| 8586 | if (binFilename.compare(usedName) != 0) continue; |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8587 | inUse = true; |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8588 | binFilename = defaultBinFilename + std::to_string(numUsed++) + |
| 8589 | defaultBinFileExt; |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8590 | break; |
| 8591 | } |
Selmar Kok | c884e58 | 2018-10-05 16:25:54 +0200 | [diff] [blame] | 8592 | } |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8593 | |
| 8594 | if (uri_cb.encode) { |
| 8595 | if (!uri_cb.encode(binFilename, "buffer", &binUri, |
| 8596 | uri_cb.user_data)) { |
| 8597 | return false; |
| 8598 | } |
| 8599 | } else { |
| 8600 | binUri = binFilename; |
| 8601 | } |
Selmar Kok | c884e58 | 2018-10-05 16:25:54 +0200 | [diff] [blame] | 8602 | } |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8603 | usedFilenames.push_back(binFilename); |
| 8604 | binSavePath = JoinPath(baseDir, binFilename); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8605 | if (!SerializeGltfBuffer(model->buffers[i], buffer, binSavePath, |
| 8606 | binUri)) { |
| 8607 | return false; |
| 8608 | } |
Selmar Kok | c884e58 | 2018-10-05 16:25:54 +0200 | [diff] [blame] | 8609 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8610 | detail::JsonPushBack(buffers, std::move(buffer)); |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 8611 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8612 | detail::JsonAddMember(output, "buffers", std::move(buffers)); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8613 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8614 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 8615 | // IMAGES |
Johan Bowald | faa2722 | 2018-03-28 14:44:45 +0200 | [diff] [blame] | 8616 | if (model->images.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8617 | detail::json images; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8618 | detail::JsonReserveArray(images, model->images.size()); |
Johan Bowald | faa2722 | 2018-03-28 14:44:45 +0200 | [diff] [blame] | 8619 | for (unsigned int i = 0; i < model->images.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8620 | detail::json image; |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 8621 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 8622 | std::string uri; |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 8623 | if (!UpdateImageObject(model->images[i], baseDir, int(i), embedImages, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8624 | &uri_cb, &this->WriteImageData, |
| 8625 | this->write_image_user_data_, &uri)) { |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 8626 | return false; |
| 8627 | } |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 8628 | SerializeGltfImage(model->images[i], uri, image); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8629 | detail::JsonPushBack(images, std::move(image)); |
Johan Bowald | faa2722 | 2018-03-28 14:44:45 +0200 | [diff] [blame] | 8630 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8631 | detail::JsonAddMember(output, "images", std::move(images)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8632 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8633 | |
David Harmon | da9eac2 | 2018-08-30 08:06:05 -0400 | [diff] [blame] | 8634 | if (writeBinary) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 8635 | return WriteBinaryGltfFile(filename, detail::JsonToString(output), |
| 8636 | binBuffer); |
David Harmon | da9eac2 | 2018-08-30 08:06:05 -0400 | [diff] [blame] | 8637 | } else { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 8638 | return WriteGltfFile(filename, |
| 8639 | detail::JsonToString(output, (prettyPrint ? 2 : -1))); |
David Harmon | da9eac2 | 2018-08-30 08:06:05 -0400 | [diff] [blame] | 8640 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8641 | } |
| 8642 | |
Syoyo Fujita | e1bef8c | 2016-03-17 21:31:13 +0900 | [diff] [blame] | 8643 | } // namespace tinygltf |
| 8644 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 8645 | #ifdef __clang__ |
| 8646 | #pragma clang diagnostic pop |
| 8647 | #endif |
| 8648 | |
Syoyo Fujita | 612e578 | 2022-09-18 21:01:39 +0900 | [diff] [blame] | 8649 | #endif // TINYGLTF_IMPLEMENTATION |