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 |
Syoyo Fujita | 7680abf | 2016-10-17 18:02:45 +0900 | [diff] [blame] | 340 | const Value &Get(int 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()); |
| 343 | assert(idx >= 0); |
Syoyo Fujita | 7680abf | 2016-10-17 18:02:45 +0900 | [diff] [blame] | 344 | return (static_cast<size_t>(idx) < array_value_.size()) |
| 345 | ? array_value_[static_cast<size_t>(idx)] |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 346 | : null_value; |
| 347 | } |
| 348 | |
| 349 | // Lookup value from a key-value pair |
| 350 | const Value &Get(const std::string &key) const { |
Syoyo Fujita | c8d3c76 | 2017-05-25 01:42:38 +0900 | [diff] [blame] | 351 | static Value null_value; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 352 | assert(IsObject()); |
| 353 | Object::const_iterator it = object_value_.find(key); |
| 354 | return (it != object_value_.end()) ? it->second : null_value; |
| 355 | } |
| 356 | |
| 357 | size_t ArrayLen() const { |
| 358 | if (!IsArray()) return 0; |
| 359 | return array_value_.size(); |
| 360 | } |
| 361 | |
| 362 | // Valid only for object type. |
| 363 | bool Has(const std::string &key) const { |
| 364 | if (!IsObject()) return false; |
| 365 | Object::const_iterator it = object_value_.find(key); |
| 366 | return (it != object_value_.end()) ? true : false; |
| 367 | } |
| 368 | |
| 369 | // List keys |
| 370 | std::vector<std::string> Keys() const { |
| 371 | std::vector<std::string> keys; |
| 372 | if (!IsObject()) return keys; // empty |
| 373 | |
| 374 | for (Object::const_iterator it = object_value_.begin(); |
| 375 | it != object_value_.end(); ++it) { |
| 376 | keys.push_back(it->first); |
| 377 | } |
| 378 | |
| 379 | return keys; |
| 380 | } |
| 381 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 382 | size_t Size() const { return (IsArray() ? ArrayLen() : Keys().size()); } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 383 | |
| 384 | bool operator==(const tinygltf::Value &other) const; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 385 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 386 | protected: |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 387 | int type_ = NULL_TYPE; |
Syoyo Fujita | 7680abf | 2016-10-17 18:02:45 +0900 | [diff] [blame] | 388 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 389 | int int_value_ = 0; |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 390 | double real_value_ = 0.0; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 391 | std::string string_value_; |
Syoyo Fujita | 7680abf | 2016-10-17 18:02:45 +0900 | [diff] [blame] | 392 | std::vector<unsigned char> binary_value_; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 393 | Array array_value_; |
| 394 | Object object_value_; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 395 | bool boolean_value_ = false; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 396 | }; |
| 397 | |
Syoyo Fujita | c8d3c76 | 2017-05-25 01:42:38 +0900 | [diff] [blame] | 398 | #ifdef __clang__ |
| 399 | #pragma clang diagnostic pop |
| 400 | #endif |
| 401 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 402 | #define TINYGLTF_VALUE_GET(ctype, var) \ |
| 403 | template <> \ |
| 404 | inline const ctype &Value::Get<ctype>() const { \ |
| 405 | return var; \ |
| 406 | } \ |
| 407 | template <> \ |
| 408 | inline ctype &Value::Get<ctype>() { \ |
| 409 | return var; \ |
| 410 | } |
| 411 | TINYGLTF_VALUE_GET(bool, boolean_value_) |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 412 | TINYGLTF_VALUE_GET(double, real_value_) |
Syoyo Fujita | 7680abf | 2016-10-17 18:02:45 +0900 | [diff] [blame] | 413 | TINYGLTF_VALUE_GET(int, int_value_) |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 414 | TINYGLTF_VALUE_GET(std::string, string_value_) |
Syoyo Fujita | 7680abf | 2016-10-17 18:02:45 +0900 | [diff] [blame] | 415 | TINYGLTF_VALUE_GET(std::vector<unsigned char>, binary_value_) |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 416 | TINYGLTF_VALUE_GET(Value::Array, array_value_) |
| 417 | TINYGLTF_VALUE_GET(Value::Object, object_value_) |
| 418 | #undef TINYGLTF_VALUE_GET |
| 419 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 420 | #ifdef __clang__ |
| 421 | #pragma clang diagnostic push |
| 422 | #pragma clang diagnostic ignored "-Wc++98-compat" |
| 423 | #pragma clang diagnostic ignored "-Wpadded" |
| 424 | #endif |
| 425 | |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 426 | /// Aggregate object for representing a color |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 427 | using ColorValue = std::array<double, 4>; |
| 428 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 429 | // === legacy interface ==== |
| 430 | // TODO(syoyo): Deprecate `Parameter` class. |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 431 | struct Parameter { |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 432 | bool bool_value = false; |
Ben Buzbee | f6af224 | 2018-04-25 15:13:05 -0700 | [diff] [blame] | 433 | bool has_number_value = false; |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 434 | std::string string_value; |
| 435 | std::vector<double> number_array; |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 436 | std::map<std::string, double> json_double_value; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 437 | double number_value = 0.0; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 438 | |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 439 | // context sensitive methods. depending the type of the Parameter you are |
| 440 | // accessing, these are either valid or not |
| 441 | // If this parameter represent a texture map in a material, will return the |
| 442 | // texture index |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 443 | |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 444 | /// Return the index of a texture if this Parameter is a texture map. |
| 445 | /// Returned value is only valid if the parameter represent a texture from a |
| 446 | /// material |
Arthur Brainville | 41fe772 | 2018-01-08 18:32:48 +0100 | [diff] [blame] | 447 | int TextureIndex() const { |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 448 | const auto it = json_double_value.find("index"); |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 449 | if (it != std::end(json_double_value)) { |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 450 | return int(it->second); |
| 451 | } |
| 452 | return -1; |
| 453 | } |
| 454 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 455 | /// Return the index of a texture coordinate set if this Parameter is a |
| 456 | /// texture map. Returned value is only valid if the parameter represent a |
| 457 | /// texture from a material |
Sascha Willems | eb01106 | 2019-02-23 21:15:45 +0100 | [diff] [blame] | 458 | int TextureTexCoord() const { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 459 | const auto it = json_double_value.find("texCoord"); |
| 460 | if (it != std::end(json_double_value)) { |
| 461 | return int(it->second); |
| 462 | } |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 463 | // 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] | 464 | return 0; |
Sascha Willems | eb01106 | 2019-02-23 21:15:45 +0100 | [diff] [blame] | 465 | } |
| 466 | |
Christophe | 820ede8 | 2019-07-04 15:21:21 +0900 | [diff] [blame] | 467 | /// Return the scale of a texture if this Parameter is a normal texture map. |
| 468 | /// Returned value is only valid if the parameter represent a normal texture |
| 469 | /// from a material |
| 470 | double TextureScale() const { |
| 471 | const auto it = json_double_value.find("scale"); |
| 472 | if (it != std::end(json_double_value)) { |
| 473 | return it->second; |
| 474 | } |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 475 | // As per the spec, if scale is omitted, this parameter is 1 |
Arthur Brainville | 8a98d98 | 2019-07-05 00:26:02 +0200 | [diff] [blame] | 476 | return 1; |
| 477 | } |
Arthur Brianville (Ybalrid) | 2a9d9de | 2019-07-05 00:30:47 +0200 | [diff] [blame] | 478 | |
Arthur Brainville | 8a98d98 | 2019-07-05 00:26:02 +0200 | [diff] [blame] | 479 | /// Return the strength of a texture if this Parameter is a an occlusion map. |
| 480 | /// Returned value is only valid if the parameter represent an occlusion map |
| 481 | /// from a material |
| 482 | double TextureStrength() const { |
| 483 | const auto it = json_double_value.find("strength"); |
| 484 | if (it != std::end(json_double_value)) { |
| 485 | return it->second; |
| 486 | } |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 487 | // As per the spec, if strength is omitted, this parameter is 1 |
Arthur Brainville | 8a98d98 | 2019-07-05 00:26:02 +0200 | [diff] [blame] | 488 | return 1; |
Christophe | 820ede8 | 2019-07-04 15:21:21 +0900 | [diff] [blame] | 489 | } |
| 490 | |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 491 | /// Material factor, like the roughness or metalness of a material |
| 492 | /// Returned value is only valid if the parameter represent a texture from a |
| 493 | /// material |
Ben Buzbee | f6af224 | 2018-04-25 15:13:05 -0700 | [diff] [blame] | 494 | double Factor() const { return number_value; } |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 495 | |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 496 | /// Return the color of a material |
| 497 | /// Returned value is only valid if the parameter represent a texture from a |
| 498 | /// material |
Arthur Brainville | 9585391 | 2018-01-08 18:37:44 +0100 | [diff] [blame] | 499 | ColorValue ColorFactor() const { |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 500 | return { |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 501 | {// this aggregate initialize the std::array object, and uses C++11 RVO. |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 502 | number_array[0], number_array[1], number_array[2], |
| 503 | (number_array.size() > 3 ? number_array[3] : 1.0)}}; |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 504 | } |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 505 | |
Selmar Kok | ff2b1f9 | 2019-10-21 17:58:09 +0200 | [diff] [blame] | 506 | Parameter() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 507 | DEFAULT_METHODS(Parameter) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 508 | bool operator==(const Parameter &) const; |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 509 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 510 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 511 | #ifdef __clang__ |
| 512 | #pragma clang diagnostic pop |
| 513 | #endif |
| 514 | |
| 515 | #ifdef __clang__ |
| 516 | #pragma clang diagnostic push |
| 517 | #pragma clang diagnostic ignored "-Wpadded" |
| 518 | #endif |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 519 | |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 520 | typedef std::map<std::string, Parameter> ParameterMap; |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 521 | typedef std::map<std::string, Value> ExtensionMap; |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 522 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 523 | struct AnimationChannel { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 524 | int sampler{-1}; // required |
| 525 | int target_node{-1}; // optional index of the node to target (alternative |
Jack Mousseau | 283b552 | 2023-01-15 11:45:45 -0800 | [diff] [blame] | 526 | // target should be provided by extension) |
| 527 | std::string target_path; // required with standard values of ["translation", |
| 528 | // "rotation", "scale", "weights"] |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 529 | Value extras; |
Selmar Kok | 4e2988e | 2019-08-16 14:08:08 +0200 | [diff] [blame] | 530 | ExtensionMap extensions; |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 531 | Value target_extras; |
Selmar Kok | 973d9b3 | 2020-01-21 18:45:24 +0100 | [diff] [blame] | 532 | ExtensionMap target_extensions; |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 533 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 534 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 535 | std::string extras_json_string; |
| 536 | std::string extensions_json_string; |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 537 | std::string target_extras_json_string; |
Selmar Kok | 973d9b3 | 2020-01-21 18:45:24 +0100 | [diff] [blame] | 538 | std::string target_extensions_json_string; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 539 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 540 | AnimationChannel() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 541 | DEFAULT_METHODS(AnimationChannel) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 542 | bool operator==(const AnimationChannel &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 543 | }; |
| 544 | |
| 545 | struct AnimationSampler { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 546 | int input{-1}; // required |
| 547 | int output{-1}; // required |
Arthur Brianville (Ybalrid) | 2a9d9de | 2019-07-05 00:30:47 +0200 | [diff] [blame] | 548 | std::string interpolation; // "LINEAR", "STEP","CUBICSPLINE" or user defined |
| 549 | // string. default "LINEAR" |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 550 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 551 | ExtensionMap extensions; |
| 552 | |
| 553 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 554 | std::string extras_json_string; |
| 555 | std::string extensions_json_string; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 556 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 557 | AnimationSampler() : interpolation("LINEAR") {} |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 558 | DEFAULT_METHODS(AnimationSampler) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 559 | bool operator==(const AnimationSampler &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 560 | }; |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 561 | |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 562 | struct Animation { |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 563 | std::string name; |
| 564 | std::vector<AnimationChannel> channels; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 565 | std::vector<AnimationSampler> samplers; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 566 | Value extras; |
Selmar Kok | 4e2988e | 2019-08-16 14:08:08 +0200 | [diff] [blame] | 567 | ExtensionMap extensions; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 568 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 569 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 570 | std::string extras_json_string; |
| 571 | std::string extensions_json_string; |
| 572 | |
Selmar Kok | ff2b1f9 | 2019-10-21 17:58:09 +0200 | [diff] [blame] | 573 | Animation() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 574 | DEFAULT_METHODS(Animation) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 575 | bool operator==(const Animation &) const; |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 576 | }; |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 577 | |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 578 | struct Skin { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 579 | std::string name; |
Bryn Lloyd | cb6a707 | 2023-06-21 22:30:14 +0200 | [diff] [blame] | 580 | int inverseBindMatrices{-1}; // required here but not in the spec |
| 581 | int skeleton{-1}; // The index of the node used as a skeleton root |
| 582 | std::vector<int> joints; // Indices of skeleton nodes |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 583 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 584 | Value extras; |
| 585 | ExtensionMap extensions; |
| 586 | |
| 587 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 588 | std::string extras_json_string; |
| 589 | std::string extensions_json_string; |
| 590 | |
Bryn Lloyd | cb6a707 | 2023-06-21 22:30:14 +0200 | [diff] [blame] | 591 | Skin() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 592 | DEFAULT_METHODS(Skin) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 593 | bool operator==(const Skin &) const; |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 594 | }; |
| 595 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 596 | struct Sampler { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 597 | std::string name; |
Syoyo Fujita | ee179b2 | 2019-08-16 13:11:30 +0900 | [diff] [blame] | 598 | // glTF 2.0 spec does not define default value for `minFilter` and |
| 599 | // `magFilter`. Set -1 in TinyGLTF(issue #186) |
| 600 | int minFilter = |
| 601 | -1; // optional. -1 = no filter defined. ["NEAREST", "LINEAR", |
zz | 8a35b8f | 2021-05-14 13:49:33 +0800 | [diff] [blame] | 602 | // "NEAREST_MIPMAP_NEAREST", "LINEAR_MIPMAP_NEAREST", |
Syoyo Fujita | ee179b2 | 2019-08-16 13:11:30 +0900 | [diff] [blame] | 603 | // "NEAREST_MIPMAP_LINEAR", "LINEAR_MIPMAP_LINEAR"] |
| 604 | int magFilter = |
| 605 | -1; // optional. -1 = no filter defined. ["NEAREST", "LINEAR"] |
| 606 | int wrapS = |
| 607 | TINYGLTF_TEXTURE_WRAP_REPEAT; // ["CLAMP_TO_EDGE", "MIRRORED_REPEAT", |
| 608 | // "REPEAT"], default "REPEAT" |
| 609 | int wrapT = |
| 610 | TINYGLTF_TEXTURE_WRAP_REPEAT; // ["CLAMP_TO_EDGE", "MIRRORED_REPEAT", |
| 611 | // "REPEAT"], default "REPEAT" |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 612 | // int wrapR = TINYGLTF_TEXTURE_WRAP_REPEAT; // TinyGLTF extension. currently |
| 613 | // not used. |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 614 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 615 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 616 | ExtensionMap extensions; |
| 617 | |
| 618 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 619 | std::string extras_json_string; |
| 620 | std::string extensions_json_string; |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 621 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 622 | Sampler() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 623 | DEFAULT_METHODS(Sampler) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 624 | bool operator==(const Sampler &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 625 | }; |
| 626 | |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 627 | struct Image { |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 628 | std::string name; |
Bryn Lloyd | cb6a707 | 2023-06-21 22:30:14 +0200 | [diff] [blame] | 629 | int width{-1}; |
| 630 | int height{-1}; |
| 631 | int component{-1}; |
| 632 | int bits{-1}; // bit depth per channel. 8(byte), 16 or 32. |
| 633 | int pixel_type{-1}; // pixel type(TINYGLTF_COMPONENT_TYPE_***). usually |
| 634 | // UBYTE(bits = 8) or USHORT(bits = 16) |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 635 | std::vector<unsigned char> image; |
Bryn Lloyd | cb6a707 | 2023-06-21 22:30:14 +0200 | [diff] [blame] | 636 | int bufferView{-1}; // (required if no uri) |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 637 | std::string mimeType; // (required if no uri) ["image/jpeg", "image/png", |
| 638 | // "image/bmp", "image/gif"] |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 639 | std::string uri; // (required if no mimeType) uri is not decoded(e.g. |
| 640 | // whitespace may be represented as %20) |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 641 | Value extras; |
Syoyo Fujita | 3e53feb | 2018-09-02 15:36:17 +0900 | [diff] [blame] | 642 | ExtensionMap extensions; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 643 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 644 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 645 | std::string extras_json_string; |
| 646 | std::string extensions_json_string; |
| 647 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 648 | // When this flag is true, data is stored to `image` in as-is format(e.g. jpeg |
| 649 | // compressed for "image/jpeg" mime) This feature is good if you use custom |
| 650 | // image loader function. (e.g. delayed decoding of images for faster glTF |
| 651 | // parsing) Default parser for Image does not provide as-is loading feature at |
| 652 | // the moment. (You can manipulate this by providing your own LoadImageData |
| 653 | // function) |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame] | 654 | bool as_is{false}; |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 655 | |
Bryn Lloyd | cb6a707 | 2023-06-21 22:30:14 +0200 | [diff] [blame] | 656 | Image() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 657 | DEFAULT_METHODS(Image) |
jrkoonce | d1e1472 | 2019-08-27 11:51:02 -0500 | [diff] [blame] | 658 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 659 | bool operator==(const Image &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 660 | }; |
| 661 | |
| 662 | struct Texture { |
Vladimír Vondruš | 239be2c | 2018-07-24 23:23:56 +0200 | [diff] [blame] | 663 | std::string name; |
| 664 | |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame] | 665 | int sampler{-1}; |
| 666 | int source{-1}; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 667 | Value extras; |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 668 | ExtensionMap extensions; |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 669 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 670 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 671 | std::string extras_json_string; |
| 672 | std::string extensions_json_string; |
| 673 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 674 | Texture() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 675 | DEFAULT_METHODS(Texture) |
jrkoonce | d1e1472 | 2019-08-27 11:51:02 -0500 | [diff] [blame] | 676 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 677 | bool operator==(const Texture &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 678 | }; |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 679 | |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 680 | struct TextureInfo { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 681 | int index{-1}; // required. |
| 682 | int texCoord{0}; // The set index of texture's TEXCOORD attribute used for |
| 683 | // texture coordinate mapping. |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 684 | |
| 685 | Value extras; |
| 686 | ExtensionMap extensions; |
| 687 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 688 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 689 | std::string extras_json_string; |
| 690 | std::string extensions_json_string; |
| 691 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 692 | TextureInfo() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 693 | DEFAULT_METHODS(TextureInfo) |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 694 | bool operator==(const TextureInfo &) const; |
| 695 | }; |
| 696 | |
| 697 | struct NormalTextureInfo { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 698 | int index{-1}; // required |
| 699 | int texCoord{0}; // The set index of texture's TEXCOORD attribute used for |
| 700 | // texture coordinate mapping. |
| 701 | double scale{ |
| 702 | 1.0}; // scaledNormal = normalize((<sampled normal texture value> |
| 703 | // * 2.0 - 1.0) * vec3(<normal scale>, <normal scale>, 1.0)) |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 704 | |
| 705 | Value extras; |
| 706 | ExtensionMap extensions; |
| 707 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 708 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 709 | std::string extras_json_string; |
| 710 | std::string extensions_json_string; |
| 711 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 712 | NormalTextureInfo() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 713 | DEFAULT_METHODS(NormalTextureInfo) |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 714 | bool operator==(const NormalTextureInfo &) const; |
| 715 | }; |
| 716 | |
| 717 | struct OcclusionTextureInfo { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 718 | int index{-1}; // required |
| 719 | 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] | 720 | // texture coordinate mapping. |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 721 | double strength{1.0}; // occludedColor = lerp(color, color * <sampled |
| 722 | // occlusion texture value>, <occlusion strength>) |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 723 | |
| 724 | Value extras; |
| 725 | ExtensionMap extensions; |
| 726 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 727 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 728 | std::string extras_json_string; |
| 729 | std::string extensions_json_string; |
| 730 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 731 | OcclusionTextureInfo() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 732 | DEFAULT_METHODS(OcclusionTextureInfo) |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 733 | bool operator==(const OcclusionTextureInfo &) const; |
| 734 | }; |
| 735 | |
| 736 | // pbrMetallicRoughness class defined in glTF 2.0 spec. |
| 737 | struct PbrMetallicRoughness { |
Thomas Gamper | 1f42c96 | 2023-11-22 15:59:13 +0100 | [diff] [blame] | 738 | 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] | 739 | TextureInfo baseColorTexture; |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame] | 740 | double metallicFactor{1.0}; // default 1 |
| 741 | double roughnessFactor{1.0}; // default 1 |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 742 | TextureInfo metallicRoughnessTexture; |
| 743 | |
| 744 | Value extras; |
| 745 | ExtensionMap extensions; |
| 746 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 747 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 748 | std::string extras_json_string; |
| 749 | std::string extensions_json_string; |
| 750 | |
Thomas Gamper | 1f42c96 | 2023-11-22 15:59:13 +0100 | [diff] [blame] | 751 | PbrMetallicRoughness() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 752 | DEFAULT_METHODS(PbrMetallicRoughness) |
Thomas Gamper | 1f42c96 | 2023-11-22 15:59:13 +0100 | [diff] [blame] | 753 | |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 754 | bool operator==(const PbrMetallicRoughness &) const; |
| 755 | }; |
| 756 | |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 757 | // Each extension should be stored in a ParameterMap. |
| 758 | // members not in the values could be included in the ParameterMap |
| 759 | // to keep a single material model |
| 760 | struct Material { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 761 | std::string name; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 762 | |
Thomas Gamper | 1f42c96 | 2023-11-22 15:59:13 +0100 | [diff] [blame] | 763 | std::vector<double> emissiveFactor{0.0, 0.0, 0.0}; // length 3. default [0, 0, 0] |
| 764 | std::string alphaMode{"OPAQUE"}; // default "OPAQUE" |
| 765 | double alphaCutoff{0.5}; // default 0.5 |
| 766 | bool doubleSided{false}; // default false |
Thomas Gamper | a42263b | 2024-02-05 15:42:49 +0100 | [diff] [blame] | 767 | std::vector<int> lods; // level of detail materials (MSFT_lod) |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 768 | |
| 769 | PbrMetallicRoughness pbrMetallicRoughness; |
| 770 | |
| 771 | NormalTextureInfo normalTexture; |
| 772 | OcclusionTextureInfo occlusionTexture; |
| 773 | TextureInfo emissiveTexture; |
| 774 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 775 | // For backward compatibility |
| 776 | // TODO(syoyo): Remove `values` and `additionalValues` in the next release. |
| 777 | ParameterMap values; |
| 778 | ParameterMap additionalValues; |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 779 | |
| 780 | ExtensionMap extensions; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 781 | Value extras; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 782 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 783 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 784 | std::string extras_json_string; |
| 785 | std::string extensions_json_string; |
| 786 | |
Thomas Gamper | 1f42c96 | 2023-11-22 15:59:13 +0100 | [diff] [blame] | 787 | Material() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 788 | DEFAULT_METHODS(Material) |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 789 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 790 | bool operator==(const Material &) const; |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 791 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 792 | |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 793 | struct BufferView { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 794 | std::string name; |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 795 | int buffer{-1}; // Required |
Syoyo Fujita | 4e47bc7 | 2020-01-02 22:07:25 +0900 | [diff] [blame] | 796 | size_t byteOffset{0}; // minimum 0, default 0 |
| 797 | size_t byteLength{0}; // required, minimum 1. 0 = invalid |
| 798 | 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] | 799 | // understood to be tightly packed |
| 800 | int target{0}; // ["ARRAY_BUFFER", "ELEMENT_ARRAY_BUFFER"] for vertex indices |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 801 | // or attribs. Could be 0 for other data |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 802 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 803 | ExtensionMap extensions; |
| 804 | |
| 805 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 806 | std::string extras_json_string; |
| 807 | std::string extensions_json_string; |
| 808 | |
Syoyo Fujita | 4e47bc7 | 2020-01-02 22:07:25 +0900 | [diff] [blame] | 809 | bool dracoDecoded{false}; // Flag indicating this has been draco decoded |
Syoyo Fujita | d17ff66 | 2017-05-29 02:53:12 +0900 | [diff] [blame] | 810 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 811 | BufferView() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 812 | DEFAULT_METHODS(BufferView) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 813 | bool operator==(const BufferView &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 814 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 815 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 816 | struct Accessor { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 817 | int bufferView{-1}; // optional in spec but required here since sparse |
| 818 | // accessor are not supported |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 819 | std::string name; |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame] | 820 | size_t byteOffset{0}; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 821 | bool normalized{false}; // optional. |
| 822 | int componentType{-1}; // (required) One of TINYGLTF_COMPONENT_TYPE_*** |
| 823 | size_t count{0}; // required |
| 824 | int type{-1}; // (required) One of TINYGLTF_TYPE_*** .. |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 825 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 826 | ExtensionMap extensions; |
| 827 | |
| 828 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 829 | std::string extras_json_string; |
| 830 | std::string extensions_json_string; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 831 | |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 832 | std::vector<double> |
| 833 | minValues; // optional. integer value is promoted to double |
| 834 | std::vector<double> |
| 835 | maxValues; // optional. integer value is promoted to double |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 836 | |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 837 | struct Sparse { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 838 | int count; |
| 839 | bool isSparse; |
| 840 | struct { |
emimvi | 759976e | 2023-09-02 09:39:53 +0200 | [diff] [blame] | 841 | size_t byteOffset; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 842 | int bufferView; |
| 843 | int componentType; // a TINYGLTF_COMPONENT_TYPE_ value |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 844 | Value extras; |
| 845 | ExtensionMap extensions; |
| 846 | std::string extras_json_string; |
| 847 | std::string extensions_json_string; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 848 | } indices; |
| 849 | struct { |
| 850 | int bufferView; |
emimvi | 759976e | 2023-09-02 09:39:53 +0200 | [diff] [blame] | 851 | size_t byteOffset; |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 852 | Value extras; |
| 853 | ExtensionMap extensions; |
| 854 | std::string extras_json_string; |
| 855 | std::string extensions_json_string; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 856 | } values; |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 857 | Value extras; |
| 858 | ExtensionMap extensions; |
| 859 | std::string extras_json_string; |
| 860 | std::string extensions_json_string; |
| 861 | }; |
| 862 | |
| 863 | Sparse sparse; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 864 | |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 865 | /// |
| 866 | /// Utility function to compute byteStride for a given bufferView object. |
| 867 | /// Returns -1 upon invalid glTF value or parameter configuration. |
| 868 | /// |
| 869 | int ByteStride(const BufferView &bufferViewObject) const { |
| 870 | if (bufferViewObject.byteStride == 0) { |
| 871 | // Assume data is tightly packed. |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 872 | int componentSizeInBytes = |
| 873 | GetComponentSizeInBytes(static_cast<uint32_t>(componentType)); |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 874 | if (componentSizeInBytes <= 0) { |
| 875 | return -1; |
| 876 | } |
| 877 | |
Syoyo Fujita | 81d75df | 2019-08-30 19:19:52 +0900 | [diff] [blame] | 878 | int numComponents = GetNumComponentsInType(static_cast<uint32_t>(type)); |
| 879 | if (numComponents <= 0) { |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 880 | return -1; |
| 881 | } |
| 882 | |
Syoyo Fujita | 81d75df | 2019-08-30 19:19:52 +0900 | [diff] [blame] | 883 | return componentSizeInBytes * numComponents; |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 884 | } else { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 885 | // Check if byteStride is a multiple of the size of the accessor's |
| 886 | // component type. |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 887 | int componentSizeInBytes = |
| 888 | GetComponentSizeInBytes(static_cast<uint32_t>(componentType)); |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 889 | if (componentSizeInBytes <= 0) { |
| 890 | return -1; |
| 891 | } |
| 892 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 893 | if ((bufferViewObject.byteStride % uint32_t(componentSizeInBytes)) != 0) { |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 894 | return -1; |
| 895 | } |
Arthur Brainville | 8ce4e54 | 2018-01-10 01:27:12 +0100 | [diff] [blame] | 896 | return static_cast<int>(bufferViewObject.byteStride); |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 897 | } |
| 898 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 899 | // unreachable return 0; |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 900 | } |
| 901 | |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 902 | Accessor() |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 903 | |
| 904 | { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 905 | sparse.isSparse = false; |
| 906 | } |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 907 | DEFAULT_METHODS(Accessor) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 908 | bool operator==(const tinygltf::Accessor &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 909 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 910 | |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 911 | struct PerspectiveCamera { |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame] | 912 | double aspectRatio{0.0}; // min > 0 |
| 913 | double yfov{0.0}; // required. min > 0 |
| 914 | double zfar{0.0}; // min > 0 |
| 915 | double znear{0.0}; // required. min > 0 |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 916 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 917 | PerspectiveCamera() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 918 | DEFAULT_METHODS(PerspectiveCamera) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 919 | bool operator==(const PerspectiveCamera &) const; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 920 | |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 921 | ExtensionMap extensions; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 922 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 923 | |
| 924 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 925 | std::string extras_json_string; |
| 926 | std::string extensions_json_string; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 927 | }; |
Aurélien Chatelain | a25e495 | 2017-05-24 09:52:28 +0000 | [diff] [blame] | 928 | |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 929 | struct OrthographicCamera { |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame] | 930 | double xmag{0.0}; // required. must not be zero. |
| 931 | double ymag{0.0}; // required. must not be zero. |
| 932 | double zfar{0.0}; // required. `zfar` must be greater than `znear`. |
| 933 | double znear{0.0}; // required |
Aurélien Chatelain | a25e495 | 2017-05-24 09:52:28 +0000 | [diff] [blame] | 934 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 935 | OrthographicCamera() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 936 | DEFAULT_METHODS(OrthographicCamera) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 937 | bool operator==(const OrthographicCamera &) const; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 938 | |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 939 | ExtensionMap extensions; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 940 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 941 | |
| 942 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 943 | std::string extras_json_string; |
| 944 | std::string extensions_json_string; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 945 | }; |
| 946 | |
| 947 | struct Camera { |
| 948 | std::string type; // required. "perspective" or "orthographic" |
| 949 | std::string name; |
| 950 | |
| 951 | PerspectiveCamera perspective; |
| 952 | OrthographicCamera orthographic; |
| 953 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 954 | Camera() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 955 | DEFAULT_METHODS(Camera) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 956 | bool operator==(const Camera &) const; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 957 | |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 958 | ExtensionMap extensions; |
Aurélien Chatelain | a25e495 | 2017-05-24 09:52:28 +0000 | [diff] [blame] | 959 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 960 | |
| 961 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 962 | std::string extras_json_string; |
| 963 | std::string extensions_json_string; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 964 | }; |
| 965 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 966 | struct Primitive { |
| 967 | std::map<std::string, int> attributes; // (required) A dictionary object of |
| 968 | // integer, where each integer |
| 969 | // is the index of the accessor |
| 970 | // containing an attribute. |
Bryn Lloyd | cb6a707 | 2023-06-21 22:30:14 +0200 | [diff] [blame] | 971 | int material{-1}; // The index of the material to apply to this primitive |
| 972 | // when rendering. |
| 973 | int indices{-1}; // The index of the accessor that contains the indices. |
| 974 | int mode{-1}; // one of TINYGLTF_MODE_*** |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 975 | std::vector<std::map<std::string, int> > targets; // array of morph targets, |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 976 | // where each target is a dict with attributes in ["POSITION, "NORMAL", |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 977 | // "TANGENT"] pointing |
| 978 | // to their corresponding accessors |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 979 | ExtensionMap extensions; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 980 | Value extras; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 981 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 982 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 983 | std::string extras_json_string; |
| 984 | std::string extensions_json_string; |
| 985 | |
Bryn Lloyd | cb6a707 | 2023-06-21 22:30:14 +0200 | [diff] [blame] | 986 | Primitive() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 987 | DEFAULT_METHODS(Primitive) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 988 | bool operator==(const Primitive &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 989 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 990 | |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 991 | struct Mesh { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 992 | std::string name; |
| 993 | std::vector<Primitive> primitives; |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 994 | std::vector<double> weights; // weights to be applied to the Morph Targets |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 995 | ExtensionMap extensions; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 996 | Value extras; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 997 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 998 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 999 | std::string extras_json_string; |
| 1000 | std::string extensions_json_string; |
| 1001 | |
jrkoonce | d1e1472 | 2019-08-27 11:51:02 -0500 | [diff] [blame] | 1002 | Mesh() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1003 | DEFAULT_METHODS(Mesh) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1004 | bool operator==(const Mesh &) const; |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 1005 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1006 | |
| 1007 | class Node { |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 1008 | public: |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1009 | Node() = default; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1010 | |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1011 | DEFAULT_METHODS(Node) |
Syoyo Fujita | d6b0d0a | 2019-06-29 17:31:13 +0900 | [diff] [blame] | 1012 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1013 | bool operator==(const Node &) const; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1014 | |
Syoyo Fujita | 7a570c8 | 2023-06-19 21:52:13 +0900 | [diff] [blame] | 1015 | int camera{-1}; // the index of the camera referenced by this node |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1016 | |
| 1017 | std::string name; |
Syoyo Fujita | 7a570c8 | 2023-06-19 21:52:13 +0900 | [diff] [blame] | 1018 | int skin{-1}; |
| 1019 | int mesh{-1}; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1020 | int light{-1}; // light source index (KHR_lights_punctual) |
| 1021 | int emitter{-1}; // audio emitter index (KHR_audio) |
Thomas Gamper | a42263b | 2024-02-05 15:42:49 +0100 | [diff] [blame] | 1022 | std::vector<int> lods; // level of detail nodes (MSFT_lod) |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1023 | std::vector<int> children; |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 1024 | std::vector<double> rotation; // length must be 0 or 4 |
| 1025 | std::vector<double> scale; // length must be 0 or 3 |
| 1026 | std::vector<double> translation; // length must be 0 or 3 |
| 1027 | std::vector<double> matrix; // length must be 0 or 16 |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 1028 | std::vector<double> weights; // The weights of the instantiated Morph Target |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 1029 | |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 1030 | ExtensionMap extensions; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 1031 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1032 | |
| 1033 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1034 | std::string extras_json_string; |
| 1035 | std::string extensions_json_string; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1036 | }; |
| 1037 | |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 1038 | struct Buffer { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1039 | std::string name; |
| 1040 | std::vector<unsigned char> data; |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 1041 | std::string |
| 1042 | 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] | 1043 | // uri is not decoded(e.g. whitespace may be represented as %20) |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 1044 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1045 | ExtensionMap extensions; |
| 1046 | |
| 1047 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1048 | std::string extras_json_string; |
| 1049 | std::string extensions_json_string; |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1050 | |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1051 | Buffer() = default; |
| 1052 | DEFAULT_METHODS(Buffer) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1053 | bool operator==(const Buffer &) const; |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 1054 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1055 | |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 1056 | struct Asset { |
Syoyo Fujita | b702de7 | 2021-03-02 19:08:29 +0900 | [diff] [blame] | 1057 | std::string version = "2.0"; // required |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1058 | std::string generator; |
Aurélien Chatelain | d48bbdc | 2017-05-24 09:54:18 +0000 | [diff] [blame] | 1059 | std::string minVersion; |
| 1060 | std::string copyright; |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 1061 | ExtensionMap extensions; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 1062 | Value extras; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1063 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1064 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1065 | std::string extras_json_string; |
| 1066 | std::string extensions_json_string; |
| 1067 | |
jrkoonce | d1e1472 | 2019-08-27 11:51:02 -0500 | [diff] [blame] | 1068 | Asset() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1069 | DEFAULT_METHODS(Asset) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1070 | bool operator==(const Asset &) const; |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 1071 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1072 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1073 | struct Scene { |
| 1074 | std::string name; |
| 1075 | std::vector<int> nodes; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1076 | std::vector<int> audioEmitters; // KHR_audio global emitters |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1077 | |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 1078 | ExtensionMap extensions; |
| 1079 | Value extras; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1080 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1081 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1082 | std::string extras_json_string; |
| 1083 | std::string extensions_json_string; |
| 1084 | |
jrkoonce | d1e1472 | 2019-08-27 11:51:02 -0500 | [diff] [blame] | 1085 | Scene() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1086 | DEFAULT_METHODS(Scene) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1087 | bool operator==(const Scene &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1088 | }; |
| 1089 | |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 1090 | struct SpotLight { |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame] | 1091 | double innerConeAngle{0.0}; |
| 1092 | double outerConeAngle{0.7853981634}; |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 1093 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1094 | SpotLight() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1095 | DEFAULT_METHODS(SpotLight) |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 1096 | bool operator==(const SpotLight &) const; |
| 1097 | |
| 1098 | ExtensionMap extensions; |
| 1099 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1100 | |
| 1101 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1102 | std::string extras_json_string; |
| 1103 | std::string extensions_json_string; |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 1104 | }; |
| 1105 | |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 1106 | struct Light { |
| 1107 | std::string name; |
| 1108 | std::vector<double> color; |
Syoyo Fujita | 3dad303 | 2020-05-13 21:22:23 +0900 | [diff] [blame] | 1109 | double intensity{1.0}; |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 1110 | std::string type; |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 1111 | double range{0.0}; // 0.0 = infinite |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 1112 | SpotLight spot; |
| 1113 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1114 | Light() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1115 | DEFAULT_METHODS(Light) |
Arthur Brainville (Ybalrid) | 9eeaf20 | 2019-09-05 13:02:05 +0200 | [diff] [blame] | 1116 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1117 | bool operator==(const Light &) const; |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 1118 | |
| 1119 | ExtensionMap extensions; |
| 1120 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1121 | |
| 1122 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1123 | std::string extras_json_string; |
| 1124 | std::string extensions_json_string; |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 1125 | }; |
| 1126 | |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1127 | struct PositionalEmitter { |
| 1128 | double coneInnerAngle{6.283185307179586}; |
| 1129 | double coneOuterAngle{6.283185307179586}; |
| 1130 | double coneOuterGain{0.0}; |
| 1131 | double maxDistance{100.0}; |
| 1132 | double refDistance{1.0}; |
| 1133 | double rolloffFactor{1.0}; |
| 1134 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1135 | PositionalEmitter() = default; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1136 | DEFAULT_METHODS(PositionalEmitter) |
| 1137 | bool operator==(const PositionalEmitter &) const; |
| 1138 | |
| 1139 | ExtensionMap extensions; |
| 1140 | Value extras; |
| 1141 | |
| 1142 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1143 | std::string extras_json_string; |
| 1144 | std::string extensions_json_string; |
| 1145 | }; |
| 1146 | |
| 1147 | struct AudioEmitter { |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 1148 | std::string name; |
| 1149 | double gain{1.0}; |
| 1150 | bool loop{false}; |
| 1151 | bool playing{false}; |
| 1152 | std::string |
| 1153 | type; // positional - Positional audio emitters. Using sound cones, the |
| 1154 | // orientation is +Z having the same front side for a glTF asset. |
| 1155 | // global - Global audio emitters are not affected by the position |
| 1156 | // of audio listeners. coneInnerAngle, coneOuterAngle, |
| 1157 | // coneOuterGain, distanceModel, maxDistance, refDistance, and |
| 1158 | // rolloffFactor should all be ignored when set. |
| 1159 | std::string |
| 1160 | distanceModel; // linear - A linear distance model calculating the |
| 1161 | // gain induced by the distance according to: 1.0 |
| 1162 | // - rolloffFactor * (distance - refDistance) / |
| 1163 | // (maxDistance - refDistance) |
| 1164 | // inverse - (default) An inverse distance model |
| 1165 | // calculating the gain induced by the distance according |
| 1166 | // to: refDistance / (refDistance + rolloffFactor * |
| 1167 | // (Math.max(distance, refDistance) - refDistance)) |
| 1168 | // exponential - An exponential distance model calculating |
| 1169 | // the gain induced by the distance according to: |
| 1170 | // pow((Math.max(distance, refDistance) / refDistance, |
| 1171 | // -rolloffFactor)) |
| 1172 | PositionalEmitter positional; |
| 1173 | int source{-1}; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1174 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1175 | AudioEmitter() : type("global"), distanceModel("inverse") {} |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 1176 | DEFAULT_METHODS(AudioEmitter) |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1177 | |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 1178 | bool operator==(const AudioEmitter &) const; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1179 | |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 1180 | ExtensionMap extensions; |
| 1181 | Value extras; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1182 | |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 1183 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1184 | std::string extras_json_string; |
| 1185 | std::string extensions_json_string; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1186 | }; |
| 1187 | |
| 1188 | struct AudioSource { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1189 | std::string name; |
| 1190 | std::string uri; |
| 1191 | int bufferView{-1}; // (required if no uri) |
| 1192 | std::string |
| 1193 | mimeType; // (required if no uri) The audio's MIME type. Required if |
| 1194 | // bufferView is defined. Unless specified by another |
| 1195 | // extension, the only supported mimeType is audio/mpeg. |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1196 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1197 | AudioSource() = default; |
| 1198 | DEFAULT_METHODS(AudioSource) |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1199 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1200 | bool operator==(const AudioSource &) const; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1201 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1202 | Value extras; |
| 1203 | ExtensionMap extensions; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1204 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1205 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1206 | std::string extras_json_string; |
| 1207 | std::string extensions_json_string; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1208 | }; |
| 1209 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1210 | class Model { |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 1211 | public: |
Selmar Kok | ff2b1f9 | 2019-10-21 17:58:09 +0200 | [diff] [blame] | 1212 | Model() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1213 | DEFAULT_METHODS(Model) |
Syoyo Fujita | d6b0d0a | 2019-06-29 17:31:13 +0900 | [diff] [blame] | 1214 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1215 | bool operator==(const Model &) const; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1216 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1217 | std::vector<Accessor> accessors; |
| 1218 | std::vector<Animation> animations; |
| 1219 | std::vector<Buffer> buffers; |
| 1220 | std::vector<BufferView> bufferViews; |
| 1221 | std::vector<Material> materials; |
| 1222 | std::vector<Mesh> meshes; |
| 1223 | std::vector<Node> nodes; |
| 1224 | std::vector<Texture> textures; |
| 1225 | std::vector<Image> images; |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 1226 | std::vector<Skin> skins; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1227 | std::vector<Sampler> samplers; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 1228 | std::vector<Camera> cameras; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1229 | std::vector<Scene> scenes; |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 1230 | std::vector<Light> lights; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1231 | std::vector<AudioEmitter> audioEmitters; |
| 1232 | std::vector<AudioSource> audioSources; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1233 | |
Bryn Lloyd | cb6a707 | 2023-06-21 22:30:14 +0200 | [diff] [blame] | 1234 | int defaultScene{-1}; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1235 | std::vector<std::string> extensionsUsed; |
Aurélien Chatelain | 756ee6b | 2017-06-19 13:52:49 +0000 | [diff] [blame] | 1236 | std::vector<std::string> extensionsRequired; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1237 | |
| 1238 | Asset asset; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 1239 | |
| 1240 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1241 | ExtensionMap extensions; |
| 1242 | |
| 1243 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1244 | std::string extras_json_string; |
| 1245 | std::string extensions_json_string; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1246 | }; |
| 1247 | |
Syoyo Fujita | 0614eb8 | 2016-10-14 18:50:14 +0900 | [diff] [blame] | 1248 | enum SectionCheck { |
Luke San Antonio | 9e36b61 | 2016-06-23 14:10:51 -0400 | [diff] [blame] | 1249 | NO_REQUIRE = 0x00, |
Benjamin Schmithüsen | d02ad0d | 2019-05-02 14:44:20 +0200 | [diff] [blame] | 1250 | REQUIRE_VERSION = 0x01, |
| 1251 | REQUIRE_SCENE = 0x02, |
| 1252 | REQUIRE_SCENES = 0x04, |
| 1253 | REQUIRE_NODES = 0x08, |
| 1254 | REQUIRE_ACCESSORS = 0x10, |
| 1255 | REQUIRE_BUFFERS = 0x20, |
| 1256 | REQUIRE_BUFFER_VIEWS = 0x40, |
| 1257 | REQUIRE_ALL = 0x7f |
Luke San Antonio | 9e36b61 | 2016-06-23 14:10:51 -0400 | [diff] [blame] | 1258 | }; |
| 1259 | |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1260 | /// |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 1261 | /// URIEncodeFunction type. Signature for custom URI encoding of external |
| 1262 | /// resources such as .bin and image files. Used by tinygltf to re-encode the |
| 1263 | /// final location of saved files. object_type may be used to encode buffer and |
| 1264 | /// image URIs differently, for example. See |
| 1265 | /// https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#uris |
| 1266 | /// |
| 1267 | typedef bool (*URIEncodeFunction)(const std::string &in_uri, |
| 1268 | const std::string &object_type, |
| 1269 | std::string *out_uri, void *user_data); |
| 1270 | |
| 1271 | /// |
| 1272 | /// URIDecodeFunction type. Signature for custom URI decoding of external |
| 1273 | /// resources such as .bin and image files. Used by tinygltf when computing |
| 1274 | /// filenames to write resources. |
| 1275 | /// |
| 1276 | typedef bool (*URIDecodeFunction)(const std::string &in_uri, |
| 1277 | std::string *out_uri, void *user_data); |
| 1278 | |
| 1279 | // Declaration of default uri decode function |
| 1280 | bool URIDecode(const std::string &in_uri, std::string *out_uri, |
| 1281 | void *user_data); |
| 1282 | |
| 1283 | /// |
| 1284 | /// A structure containing URI callbacks and a pointer to their user data. |
| 1285 | /// |
| 1286 | struct URICallbacks { |
| 1287 | URIEncodeFunction encode; // Optional encode method |
| 1288 | URIDecodeFunction decode; // Required decode method |
| 1289 | |
| 1290 | void *user_data; // An argument that is passed to all uri callbacks |
| 1291 | }; |
| 1292 | |
| 1293 | /// |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1294 | /// LoadImageDataFunction type. Signature for custom image loading callbacks. |
| 1295 | /// |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 1296 | typedef bool (*LoadImageDataFunction)(Image *, const int, std::string *, |
| 1297 | std::string *, int, int, |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 1298 | const unsigned char *, int, |
| 1299 | void *user_pointer); |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1300 | |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1301 | /// |
| 1302 | /// WriteImageDataFunction type. Signature for custom image writing callbacks. |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 1303 | /// The out_uri parameter becomes the URI written to the gltf and may reference |
| 1304 | /// a file or contain a data URI. |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1305 | /// |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 1306 | typedef bool (*WriteImageDataFunction)(const std::string *basepath, |
| 1307 | const std::string *filename, |
| 1308 | const Image *image, bool embedImages, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 1309 | const URICallbacks *uri_cb, |
| 1310 | std::string *out_uri, |
| 1311 | void *user_pointer); |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1312 | |
Squareys | 2d3594d | 2018-03-13 22:40:53 +0100 | [diff] [blame] | 1313 | #ifndef TINYGLTF_NO_STB_IMAGE |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1314 | // Declaration of default image loader callback |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 1315 | bool LoadImageData(Image *image, const int image_idx, std::string *err, |
| 1316 | std::string *warn, int req_width, int req_height, |
| 1317 | const unsigned char *bytes, int size, void *); |
Squareys | 2d3594d | 2018-03-13 22:40:53 +0100 | [diff] [blame] | 1318 | #endif |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1319 | |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1320 | #ifndef TINYGLTF_NO_STB_IMAGE_WRITE |
| 1321 | // Declaration of default image writer callback |
| 1322 | bool WriteImageData(const std::string *basepath, const std::string *filename, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 1323 | const Image *image, bool embedImages, |
| 1324 | const URICallbacks *uri_cb, std::string *out_uri, void *); |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1325 | #endif |
| 1326 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1327 | /// |
| 1328 | /// FilExistsFunction type. Signature for custom filesystem callbacks. |
| 1329 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1330 | typedef bool (*FileExistsFunction)(const std::string &abs_filename, void *); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1331 | |
| 1332 | /// |
| 1333 | /// ExpandFilePathFunction type. Signature for custom filesystem callbacks. |
| 1334 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1335 | typedef std::string (*ExpandFilePathFunction)(const std::string &, void *); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1336 | |
| 1337 | /// |
| 1338 | /// ReadWholeFileFunction type. Signature for custom filesystem callbacks. |
| 1339 | /// |
| 1340 | typedef bool (*ReadWholeFileFunction)(std::vector<unsigned char> *, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1341 | std::string *, const std::string &, |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1342 | void *); |
| 1343 | |
| 1344 | /// |
| 1345 | /// WriteWholeFileFunction type. Signature for custom filesystem callbacks. |
| 1346 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1347 | typedef bool (*WriteWholeFileFunction)(std::string *, const std::string &, |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1348 | const std::vector<unsigned char> &, |
| 1349 | void *); |
| 1350 | |
| 1351 | /// |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 1352 | /// GetFileSizeFunction type. Signature for custom filesystem callbacks. |
| 1353 | /// |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1354 | typedef bool (*GetFileSizeFunction)(size_t *filesize_out, std::string *err, |
| 1355 | const std::string &abs_filename, |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 1356 | void *userdata); |
| 1357 | |
| 1358 | /// |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1359 | /// A structure containing all required filesystem callbacks and a pointer to |
| 1360 | /// their user data. |
| 1361 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1362 | struct FsCallbacks { |
| 1363 | FileExistsFunction FileExists; |
| 1364 | ExpandFilePathFunction ExpandFilePath; |
| 1365 | ReadWholeFileFunction ReadWholeFile; |
| 1366 | WriteWholeFileFunction WriteWholeFile; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1367 | GetFileSizeFunction GetFileSizeInBytes; // To avoid GetFileSize Win32 API, |
| 1368 | // add `InBytes` suffix. |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1369 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1370 | void *user_data; // An argument that is passed to all fs callbacks |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1371 | }; |
| 1372 | |
| 1373 | #ifndef TINYGLTF_NO_FS |
| 1374 | // Declaration of default filesystem callbacks |
| 1375 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1376 | bool FileExists(const std::string &abs_filename, void *); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1377 | |
Syoyo Fujita | 3d939fd | 2020-06-06 18:13:15 +0900 | [diff] [blame] | 1378 | /// |
Rahul Sheth | 96a8b2c | 2020-07-10 14:27:46 -0400 | [diff] [blame] | 1379 | /// Expand file path(e.g. `~` to home directory on posix, `%APPDATA%` to |
Syoyo Fujita | 2c521b3 | 2020-12-04 00:50:46 +0900 | [diff] [blame] | 1380 | /// `C:\\Users\\tinygltf\\AppData`) |
Syoyo Fujita | 3d939fd | 2020-06-06 18:13:15 +0900 | [diff] [blame] | 1381 | /// |
| 1382 | /// @param[in] filepath File path string. Assume UTF-8 |
| 1383 | /// @param[in] userdata User data. Set to `nullptr` if you don't need it. |
| 1384 | /// |
| 1385 | std::string ExpandFilePath(const std::string &filepath, void *userdata); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1386 | |
| 1387 | bool ReadWholeFile(std::vector<unsigned char> *out, std::string *err, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1388 | const std::string &filepath, void *); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1389 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1390 | bool WriteWholeFile(std::string *err, const std::string &filepath, |
| 1391 | const std::vector<unsigned char> &contents, void *); |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 1392 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1393 | bool GetFileSizeInBytes(size_t *filesize_out, std::string *err, |
| 1394 | const std::string &filepath, void *); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1395 | #endif |
| 1396 | |
Syoyo Fujita | a472a3f | 2019-08-21 14:23:00 +0900 | [diff] [blame] | 1397 | /// |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 1398 | /// glTF Parser/Serializer context. |
Syoyo Fujita | a472a3f | 2019-08-21 14:23:00 +0900 | [diff] [blame] | 1399 | /// |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1400 | class TinyGLTF { |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 1401 | public: |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1402 | #ifdef __clang__ |
| 1403 | #pragma clang diagnostic push |
| 1404 | #pragma clang diagnostic ignored "-Wc++98-compat" |
| 1405 | #endif |
| 1406 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1407 | TinyGLTF() = default; |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1408 | |
| 1409 | #ifdef __clang__ |
| 1410 | #pragma clang diagnostic pop |
| 1411 | #endif |
| 1412 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1413 | ~TinyGLTF() = default; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1414 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1415 | /// |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1416 | /// Loads glTF ASCII asset from a file. |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1417 | /// Set warning message to `warn` for example it fails to load asserts. |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 1418 | /// 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] | 1419 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1420 | bool LoadASCIIFromFile(Model *model, std::string *err, std::string *warn, |
Luke San Antonio | 9e36b61 | 2016-06-23 14:10:51 -0400 | [diff] [blame] | 1421 | const std::string &filename, |
Benjamin Schmithüsen | d02ad0d | 2019-05-02 14:44:20 +0200 | [diff] [blame] | 1422 | unsigned int check_sections = REQUIRE_VERSION); |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 1423 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1424 | /// |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1425 | /// Loads glTF ASCII asset from string(memory). |
| 1426 | /// `length` = strlen(str); |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 1427 | /// `base_dir` is a search path of glTF asset(e.g. images). Path Must be an |
| 1428 | /// expanded path (e.g. no tilde(`~`), no environment variables). Set warning |
| 1429 | /// message to `warn` for example it fails to load asserts. Returns false and |
| 1430 | /// set error string to `err` if there's an error. |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1431 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1432 | bool LoadASCIIFromString(Model *model, std::string *err, std::string *warn, |
| 1433 | const char *str, const unsigned int length, |
Luke San Antonio | 9e36b61 | 2016-06-23 14:10:51 -0400 | [diff] [blame] | 1434 | const std::string &base_dir, |
Benjamin Schmithüsen | d02ad0d | 2019-05-02 14:44:20 +0200 | [diff] [blame] | 1435 | unsigned int check_sections = REQUIRE_VERSION); |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1436 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1437 | /// |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1438 | /// Loads glTF binary asset from a file. |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1439 | /// Set warning message to `warn` for example it fails to load asserts. |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1440 | /// 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] | 1441 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1442 | bool LoadBinaryFromFile(Model *model, std::string *err, std::string *warn, |
Luke San Antonio | 9e36b61 | 2016-06-23 14:10:51 -0400 | [diff] [blame] | 1443 | const std::string &filename, |
Benjamin Schmithüsen | d02ad0d | 2019-05-02 14:44:20 +0200 | [diff] [blame] | 1444 | unsigned int check_sections = REQUIRE_VERSION); |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1445 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1446 | /// |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1447 | /// Loads glTF binary asset from memory. |
| 1448 | /// `length` = strlen(str); |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 1449 | /// `base_dir` is a search path of glTF asset(e.g. images). Path Must be an |
| 1450 | /// expanded path (e.g. no tilde(`~`), no environment variables). |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1451 | /// Set warning message to `warn` for example it fails to load asserts. |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1452 | /// 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] | 1453 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1454 | bool LoadBinaryFromMemory(Model *model, std::string *err, std::string *warn, |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 1455 | const unsigned char *bytes, |
| 1456 | const unsigned int length, |
Luke San Antonio | 9e36b61 | 2016-06-23 14:10:51 -0400 | [diff] [blame] | 1457 | const std::string &base_dir = "", |
Benjamin Schmithüsen | d02ad0d | 2019-05-02 14:44:20 +0200 | [diff] [blame] | 1458 | unsigned int check_sections = REQUIRE_VERSION); |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1459 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1460 | /// |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 1461 | /// Write glTF to stream, buffers and images will be embedded |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 1462 | /// |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 1463 | bool WriteGltfSceneToStream(const Model *model, std::ostream &stream, |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 1464 | bool prettyPrint, bool writeBinary); |
| 1465 | |
| 1466 | /// |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1467 | /// Write glTF to file. |
| 1468 | /// |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 1469 | bool WriteGltfSceneToFile(const Model *model, const std::string &filename, |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 1470 | bool embedImages, bool embedBuffers, |
| 1471 | bool prettyPrint, bool writeBinary); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 1472 | |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1473 | /// |
Nyall Dawson | 8c85d5e | 2023-08-28 12:56:09 +1000 | [diff] [blame] | 1474 | /// Sets the parsing strictness. |
| 1475 | /// |
| 1476 | void SetParseStrictness(ParseStrictness strictness); |
| 1477 | |
| 1478 | /// |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1479 | /// Set callback to use for loading image data |
| 1480 | /// |
| 1481 | void SetImageLoader(LoadImageDataFunction LoadImageData, void *user_data); |
| 1482 | |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1483 | /// |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1484 | /// Unset(remove) callback of loading image data |
| 1485 | /// |
| 1486 | void RemoveImageLoader(); |
| 1487 | |
| 1488 | /// |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1489 | /// Set callback to use for writing image data |
| 1490 | /// |
| 1491 | void SetImageWriter(WriteImageDataFunction WriteImageData, void *user_data); |
| 1492 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1493 | /// |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 1494 | /// Set callbacks to use for URI encoding and decoding and their user data |
| 1495 | /// |
| 1496 | void SetURICallbacks(URICallbacks callbacks); |
| 1497 | |
| 1498 | /// |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1499 | /// Set callbacks to use for filesystem (fs) access and their user data |
| 1500 | /// |
| 1501 | void SetFsCallbacks(FsCallbacks callbacks); |
| 1502 | |
Syoyo Fujita | a472a3f | 2019-08-21 14:23:00 +0900 | [diff] [blame] | 1503 | /// |
| 1504 | /// Set serializing default values(default = false). |
| 1505 | /// When true, default values are force serialized to .glTF. |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 1506 | /// 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] | 1507 | /// data. |
Syoyo Fujita | a472a3f | 2019-08-21 14:23:00 +0900 | [diff] [blame] | 1508 | /// |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 1509 | /// TODO(LTE): Supply parsing option as function arguments to |
| 1510 | /// `LoadASCIIFromFile()` and others, not by a class method |
Syoyo Fujita | a472a3f | 2019-08-21 14:23:00 +0900 | [diff] [blame] | 1511 | /// |
| 1512 | void SetSerializeDefaultValues(const bool enabled) { |
| 1513 | serialize_default_values_ = enabled; |
| 1514 | } |
| 1515 | |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 1516 | bool GetSerializeDefaultValues() const { return serialize_default_values_; } |
Syoyo Fujita | a472a3f | 2019-08-21 14:23:00 +0900 | [diff] [blame] | 1517 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1518 | /// |
| 1519 | /// Store original JSON string for `extras` and `extensions`. |
| 1520 | /// This feature will be useful when the user want to reconstruct custom data |
| 1521 | /// structure from JSON string. |
| 1522 | /// |
| 1523 | void SetStoreOriginalJSONForExtrasAndExtensions(const bool enabled) { |
| 1524 | store_original_json_for_extras_and_extensions_ = enabled; |
| 1525 | } |
| 1526 | |
| 1527 | bool GetStoreOriginalJSONForExtrasAndExtensions() const { |
| 1528 | return store_original_json_for_extras_and_extensions_; |
| 1529 | } |
| 1530 | |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1531 | /// |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 1532 | /// Specify whether preserve image channels when loading images or not. |
| 1533 | /// (Not effective when the user supplies their own LoadImageData callbacks) |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1534 | /// |
| 1535 | void SetPreserveImageChannels(bool onoff) { |
| 1536 | preserve_image_channels_ = onoff; |
| 1537 | } |
| 1538 | |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 1539 | /// |
| 1540 | /// Set maximum allowed external file size in bytes. |
| 1541 | /// Default: 2GB |
| 1542 | /// Only effective for built-in ReadWholeFileFunction FS function. |
| 1543 | /// |
| 1544 | void SetMaxExternalFileSize(size_t max_bytes) { |
| 1545 | max_external_file_size_ = max_bytes; |
| 1546 | } |
| 1547 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1548 | size_t GetMaxExternalFileSize() const { return max_external_file_size_; } |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 1549 | |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 1550 | bool GetPreserveImageChannels() const { return preserve_image_channels_; } |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1551 | |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 1552 | private: |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1553 | /// |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 1554 | /// Loads glTF asset from string(memory). |
| 1555 | /// `length` = strlen(str); |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1556 | /// Set warning message to `warn` for example it fails to load asserts |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 1557 | /// 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] | 1558 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1559 | bool LoadFromString(Model *model, std::string *err, std::string *warn, |
| 1560 | const char *str, const unsigned int length, |
| 1561 | const std::string &base_dir, unsigned int check_sections); |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1562 | |
Syoyo Fujita | a472a3f | 2019-08-21 14:23:00 +0900 | [diff] [blame] | 1563 | const unsigned char *bin_data_ = nullptr; |
| 1564 | size_t bin_size_ = 0; |
| 1565 | bool is_binary_ = false; |
| 1566 | |
Nyall Dawson | bbc1eae | 2023-09-03 09:05:32 +1000 | [diff] [blame] | 1567 | ParseStrictness strictness_ = ParseStrictness::Strict; |
Nyall Dawson | 8c85d5e | 2023-08-28 12:56:09 +1000 | [diff] [blame] | 1568 | |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 1569 | bool serialize_default_values_ = false; ///< Serialize default values? |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1570 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1571 | bool store_original_json_for_extras_and_extensions_ = false; |
| 1572 | |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 1573 | bool preserve_image_channels_ = false; /// Default false(expand channels to |
| 1574 | /// RGBA) for backward compatibility. |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1575 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1576 | size_t max_external_file_size_{ |
| 1577 | size_t((std::numeric_limits<int32_t>::max)())}; // Default 2GB |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 1578 | |
Syoyo Fujita | 9117abb | 2022-08-16 20:10:26 +0900 | [diff] [blame] | 1579 | // Warning & error messages |
Syoyo Fujita | 81f7dbe | 2022-08-02 01:05:34 +0900 | [diff] [blame] | 1580 | std::string warn_; |
| 1581 | std::string err_; |
| 1582 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1583 | FsCallbacks fs = { |
| 1584 | #ifndef TINYGLTF_NO_FS |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1585 | &tinygltf::FileExists, |
| 1586 | &tinygltf::ExpandFilePath, |
| 1587 | &tinygltf::ReadWholeFile, |
| 1588 | &tinygltf::WriteWholeFile, |
| 1589 | &tinygltf::GetFileSizeInBytes, |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1590 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1591 | nullptr // Fs callback user data |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1592 | #else |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 1593 | nullptr, nullptr, nullptr, nullptr, nullptr, |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1594 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1595 | nullptr // Fs callback user data |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1596 | #endif |
| 1597 | }; |
| 1598 | |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 1599 | URICallbacks uri_cb = { |
| 1600 | // Use paths as-is by default. This will use JSON string escaping. |
| 1601 | nullptr, |
| 1602 | // Decode all URIs before using them as paths as the application may have |
| 1603 | // percent encoded them. |
| 1604 | &tinygltf::URIDecode, |
| 1605 | // URI callback user data |
| 1606 | nullptr}; |
| 1607 | |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1608 | LoadImageDataFunction LoadImageData = |
| 1609 | #ifndef TINYGLTF_NO_STB_IMAGE |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1610 | &tinygltf::LoadImageData; |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1611 | #else |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1612 | nullptr; |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1613 | #endif |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1614 | void *load_image_user_data_{nullptr}; |
| 1615 | bool user_image_loader_{false}; |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1616 | |
| 1617 | WriteImageDataFunction WriteImageData = |
| 1618 | #ifndef TINYGLTF_NO_STB_IMAGE_WRITE |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1619 | &tinygltf::WriteImageData; |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1620 | #else |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1621 | nullptr; |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1622 | #endif |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1623 | void *write_image_user_data_{nullptr}; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1624 | }; |
| 1625 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1626 | #ifdef __clang__ |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 1627 | #pragma clang diagnostic pop // -Wpadded |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1628 | #endif |
| 1629 | |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 1630 | } // namespace tinygltf |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1631 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1632 | #endif // TINY_GLTF_H_ |
Syoyo Fujita | c8d3c76 | 2017-05-25 01:42:38 +0900 | [diff] [blame] | 1633 | |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1634 | #if defined(TINYGLTF_IMPLEMENTATION) || defined(__INTELLISENSE__) |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 1635 | #include <algorithm> |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1636 | // #include <cassert> |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1637 | #ifndef TINYGLTF_NO_FS |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1638 | #include <sys/stat.h> // for is_directory check |
| 1639 | |
Syoyo Fujita | 125d8e5 | 2019-11-09 20:52:56 +0900 | [diff] [blame] | 1640 | #include <cstdio> |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 1641 | #include <fstream> |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1642 | #endif |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 1643 | #include <sstream> |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 1644 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 1645 | #ifdef __clang__ |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 1646 | // Disable some warnings for external files. |
| 1647 | #pragma clang diagnostic push |
| 1648 | #pragma clang diagnostic ignored "-Wfloat-equal" |
| 1649 | #pragma clang diagnostic ignored "-Wexit-time-destructors" |
| 1650 | #pragma clang diagnostic ignored "-Wconversion" |
| 1651 | #pragma clang diagnostic ignored "-Wold-style-cast" |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 1652 | #pragma clang diagnostic ignored "-Wglobal-constructors" |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1653 | #if __has_warning("-Wreserved-id-macro") |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 1654 | #pragma clang diagnostic ignored "-Wreserved-id-macro" |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1655 | #endif |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 1656 | #pragma clang diagnostic ignored "-Wdisabled-macro-expansion" |
| 1657 | #pragma clang diagnostic ignored "-Wpadded" |
Syoyo Fujita | 2e21be7 | 2017-11-05 17:13:01 +0900 | [diff] [blame] | 1658 | #pragma clang diagnostic ignored "-Wc++98-compat" |
Syoyo Fujita | dc4bb86 | 2018-04-02 02:04:24 +0900 | [diff] [blame] | 1659 | #pragma clang diagnostic ignored "-Wc++98-compat-pedantic" |
Syoyo Fujita | 2e21be7 | 2017-11-05 17:13:01 +0900 | [diff] [blame] | 1660 | #pragma clang diagnostic ignored "-Wdocumentation-unknown-command" |
| 1661 | #pragma clang diagnostic ignored "-Wswitch-enum" |
| 1662 | #pragma clang diagnostic ignored "-Wimplicit-fallthrough" |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1663 | #pragma clang diagnostic ignored "-Wweak-vtables" |
| 1664 | #pragma clang diagnostic ignored "-Wcovered-switch-default" |
Syoyo Fujita | a8f0b1c | 2018-08-28 21:33:40 +0900 | [diff] [blame] | 1665 | #if __has_warning("-Wdouble-promotion") |
| 1666 | #pragma clang diagnostic ignored "-Wdouble-promotion" |
| 1667 | #endif |
Syoyo Fujita | 2e21be7 | 2017-11-05 17:13:01 +0900 | [diff] [blame] | 1668 | #if __has_warning("-Wcomma") |
Syoyo Fujita | c8d3c76 | 2017-05-25 01:42:38 +0900 | [diff] [blame] | 1669 | #pragma clang diagnostic ignored "-Wcomma" |
| 1670 | #endif |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1671 | #if __has_warning("-Wzero-as-null-pointer-constant") |
| 1672 | #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" |
| 1673 | #endif |
| 1674 | #if __has_warning("-Wcast-qual") |
| 1675 | #pragma clang diagnostic ignored "-Wcast-qual" |
| 1676 | #endif |
Syoyo Fujita | dc4bb86 | 2018-04-02 02:04:24 +0900 | [diff] [blame] | 1677 | #if __has_warning("-Wmissing-variable-declarations") |
| 1678 | #pragma clang diagnostic ignored "-Wmissing-variable-declarations" |
| 1679 | #endif |
| 1680 | #if __has_warning("-Wmissing-prototypes") |
| 1681 | #pragma clang diagnostic ignored "-Wmissing-prototypes" |
| 1682 | #endif |
| 1683 | #if __has_warning("-Wcast-align") |
| 1684 | #pragma clang diagnostic ignored "-Wcast-align" |
| 1685 | #endif |
| 1686 | #if __has_warning("-Wnewline-eof") |
| 1687 | #pragma clang diagnostic ignored "-Wnewline-eof" |
| 1688 | #endif |
Syoyo Fujita | 7bdfed3 | 2019-03-03 17:04:49 +0900 | [diff] [blame] | 1689 | #if __has_warning("-Wunused-parameter") |
| 1690 | #pragma clang diagnostic ignored "-Wunused-parameter" |
Syoyo Fujita | 6fd9162 | 2016-05-01 20:27:30 +0900 | [diff] [blame] | 1691 | #endif |
Syoyo Fujita | 7bdfed3 | 2019-03-03 17:04:49 +0900 | [diff] [blame] | 1692 | #if __has_warning("-Wmismatched-tags") |
| 1693 | #pragma clang diagnostic ignored "-Wmismatched-tags" |
| 1694 | #endif |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 1695 | #if __has_warning("-Wextra-semi-stmt") |
| 1696 | #pragma clang diagnostic ignored "-Wextra-semi-stmt" |
| 1697 | #endif |
Syoyo Fujita | 7bdfed3 | 2019-03-03 17:04:49 +0900 | [diff] [blame] | 1698 | #endif |
| 1699 | |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 1700 | // Disable GCC warnings |
Syoyo Fujita | 7bdfed3 | 2019-03-03 17:04:49 +0900 | [diff] [blame] | 1701 | #ifdef __GNUC__ |
| 1702 | #pragma GCC diagnostic push |
| 1703 | #pragma GCC diagnostic ignored "-Wtype-limits" |
Syoyo Fujita | ca56f72 | 2019-03-07 21:04:25 +0900 | [diff] [blame] | 1704 | #endif // __GNUC__ |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1705 | |
kroko | fc0116b | 2019-03-03 08:28:49 +0200 | [diff] [blame] | 1706 | #ifndef TINYGLTF_NO_INCLUDE_JSON |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1707 | #ifndef TINYGLTF_USE_RAPIDJSON |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 1708 | #include "json.hpp" |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1709 | #else |
Alvaro Barua | 3ddf830 | 2021-03-18 00:21:23 +0000 | [diff] [blame] | 1710 | #ifndef TINYGLTF_NO_INCLUDE_RAPIDJSON |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1711 | #include "document.h" |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1712 | #include "prettywriter.h" |
| 1713 | #include "rapidjson.h" |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1714 | #include "stringbuffer.h" |
| 1715 | #include "writer.h" |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1716 | #endif |
kroko | f4b6d11 | 2019-03-03 01:11:31 +0200 | [diff] [blame] | 1717 | #endif |
Alvaro Barua | 3ddf830 | 2021-03-18 00:21:23 +0000 | [diff] [blame] | 1718 | #endif |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 1719 | |
| 1720 | #ifdef TINYGLTF_ENABLE_DRACO |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 1721 | #include "draco/compression/decode.h" |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 1722 | #include "draco/core/decoder_buffer.h" |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 1723 | #endif |
Squareys | 2d3594d | 2018-03-13 22:40:53 +0100 | [diff] [blame] | 1724 | |
| 1725 | #ifndef TINYGLTF_NO_STB_IMAGE |
kroko | fc0116b | 2019-03-03 08:28:49 +0200 | [diff] [blame] | 1726 | #ifndef TINYGLTF_NO_INCLUDE_STB_IMAGE |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 1727 | #include "stb_image.h" |
Squareys | 2d3594d | 2018-03-13 22:40:53 +0100 | [diff] [blame] | 1728 | #endif |
kroko | f4b6d11 | 2019-03-03 01:11:31 +0200 | [diff] [blame] | 1729 | #endif |
Squareys | 2d3594d | 2018-03-13 22:40:53 +0100 | [diff] [blame] | 1730 | |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1731 | #ifndef TINYGLTF_NO_STB_IMAGE_WRITE |
kroko | fc0116b | 2019-03-03 08:28:49 +0200 | [diff] [blame] | 1732 | #ifndef TINYGLTF_NO_INCLUDE_STB_IMAGE_WRITE |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 1733 | #include "stb_image_write.h" |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1734 | #endif |
kroko | f4b6d11 | 2019-03-03 01:11:31 +0200 | [diff] [blame] | 1735 | #endif |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1736 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 1737 | #ifdef __clang__ |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 1738 | #pragma clang diagnostic pop |
Syoyo Fujita | 6fd9162 | 2016-05-01 20:27:30 +0900 | [diff] [blame] | 1739 | #endif |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1740 | |
Syoyo Fujita | 7bdfed3 | 2019-03-03 17:04:49 +0900 | [diff] [blame] | 1741 | #ifdef __GNUC__ |
| 1742 | #pragma GCC diagnostic pop |
| 1743 | #endif |
| 1744 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1745 | #ifdef _WIN32 |
Syoyo Fujita | 7d9a0bd | 2019-02-19 16:03:23 +0900 | [diff] [blame] | 1746 | |
| 1747 | // issue 143. |
| 1748 | // Define NOMINMAX to avoid min/max defines, |
imallett | 3a29588 | 2022-10-07 11:20:39 -0700 | [diff] [blame] | 1749 | // but undef it after included Windows.h |
Syoyo Fujita | 7d9a0bd | 2019-02-19 16:03:23 +0900 | [diff] [blame] | 1750 | #ifndef NOMINMAX |
| 1751 | #define TINYGLTF_INTERNAL_NOMINMAX |
| 1752 | #define NOMINMAX |
| 1753 | #endif |
| 1754 | |
Syoyo Fujita | 87be0ce | 2019-02-19 21:36:32 +0900 | [diff] [blame] | 1755 | #ifndef WIN32_LEAN_AND_MEAN |
| 1756 | #define WIN32_LEAN_AND_MEAN |
| 1757 | #define TINYGLTF_INTERNAL_WIN32_LEAN_AND_MEAN |
| 1758 | #endif |
imallett | 3a29588 | 2022-10-07 11:20:39 -0700 | [diff] [blame] | 1759 | #ifndef __MINGW32__ |
imallett | 56e1098 | 2022-10-07 10:35:16 -0700 | [diff] [blame] | 1760 | #include <Windows.h> // include API for expanding a file path |
imallett | 3a29588 | 2022-10-07 11:20:39 -0700 | [diff] [blame] | 1761 | #else |
| 1762 | #include <windows.h> |
| 1763 | #endif |
Syoyo Fujita | 7d9a0bd | 2019-02-19 16:03:23 +0900 | [diff] [blame] | 1764 | |
Syoyo Fujita | 87be0ce | 2019-02-19 21:36:32 +0900 | [diff] [blame] | 1765 | #ifdef TINYGLTF_INTERNAL_WIN32_LEAN_AND_MEAN |
| 1766 | #undef WIN32_LEAN_AND_MEAN |
| 1767 | #endif |
| 1768 | |
Syoyo Fujita | 7d9a0bd | 2019-02-19 16:03:23 +0900 | [diff] [blame] | 1769 | #if defined(TINYGLTF_INTERNAL_NOMINMAX) |
| 1770 | #undef NOMINMAX |
| 1771 | #endif |
| 1772 | |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 1773 | #if defined(__GLIBCXX__) // mingw |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 1774 | |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 1775 | #include <fcntl.h> // _O_RDONLY |
| 1776 | |
| 1777 | #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] | 1778 | |
| 1779 | #endif |
| 1780 | |
Julian Smith | 0598a20 | 2021-08-25 12:06:08 +0100 | [diff] [blame] | 1781 | #elif !defined(__ANDROID__) && !defined(__OpenBSD__) |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1782 | // #include <wordexp.h> |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1783 | #endif |
| 1784 | |
Christopher Sean Morrison | 2c9b256 | 2022-05-14 19:00:19 -0400 | [diff] [blame] | 1785 | #if defined(__sparcv9) || defined(__powerpc__) |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1786 | // Big endian |
| 1787 | #else |
| 1788 | #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU |
| 1789 | #define TINYGLTF_LITTLE_ENDIAN 1 |
| 1790 | #endif |
| 1791 | #endif |
| 1792 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 1793 | namespace tinygltf { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 1794 | namespace detail { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1795 | #ifdef TINYGLTF_USE_RAPIDJSON |
jrkoonce | ce7fa74 | 2019-09-04 13:31:44 -0500 | [diff] [blame] | 1796 | |
| 1797 | #ifdef TINYGLTF_USE_RAPIDJSON_CRTALLOCATOR |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1798 | // This uses the RapidJSON CRTAllocator. It is thread safe and multiple |
| 1799 | // documents may be active at once. |
| 1800 | using json = |
| 1801 | rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAllocator>; |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 1802 | using json_iterator = json::MemberIterator; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1803 | using json_const_iterator = json::ConstMemberIterator; |
| 1804 | using json_const_array_iterator = json const *; |
| 1805 | using JsonDocument = |
jrkoonce | ce7fa74 | 2019-09-04 13:31:44 -0500 | [diff] [blame] | 1806 | rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator>; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1807 | rapidjson::CrtAllocator s_CrtAllocator; // stateless and thread safe |
| 1808 | rapidjson::CrtAllocator &GetAllocator() { return s_CrtAllocator; } |
jrkoonce | ce7fa74 | 2019-09-04 13:31:44 -0500 | [diff] [blame] | 1809 | #else |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1810 | // This uses the default RapidJSON MemoryPoolAllocator. It is very fast, but |
| 1811 | // not thread safe. Only a single JsonDocument may be active at any one time, |
| 1812 | // meaning only a single gltf load/save can be active any one time. |
| 1813 | using json = rapidjson::Value; |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 1814 | using json_iterator = json::MemberIterator; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1815 | using json_const_iterator = json::ConstMemberIterator; |
| 1816 | using json_const_array_iterator = json const *; |
| 1817 | rapidjson::Document *s_pActiveDocument = nullptr; |
| 1818 | rapidjson::Document::AllocatorType &GetAllocator() { |
| 1819 | assert(s_pActiveDocument); // Root json node must be JsonDocument type |
| 1820 | return s_pActiveDocument->GetAllocator(); |
| 1821 | } |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1822 | |
| 1823 | #ifdef __clang__ |
| 1824 | #pragma clang diagnostic push |
| 1825 | // Suppress JsonDocument(JsonDocument &&rhs) noexcept |
| 1826 | #pragma clang diagnostic ignored "-Wunused-member-function" |
| 1827 | #endif |
| 1828 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1829 | struct JsonDocument : public rapidjson::Document { |
| 1830 | JsonDocument() { |
| 1831 | assert(s_pActiveDocument == |
| 1832 | nullptr); // When using default allocator, only one document can be |
| 1833 | // active at a time, if you need multiple active at once, |
| 1834 | // define TINYGLTF_USE_RAPIDJSON_CRTALLOCATOR |
| 1835 | s_pActiveDocument = this; |
jrkoonce | ce7fa74 | 2019-09-04 13:31:44 -0500 | [diff] [blame] | 1836 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1837 | JsonDocument(const JsonDocument &) = delete; |
| 1838 | JsonDocument(JsonDocument &&rhs) noexcept |
jrkoonce | ce7fa74 | 2019-09-04 13:31:44 -0500 | [diff] [blame] | 1839 | : rapidjson::Document(std::move(rhs)) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1840 | s_pActiveDocument = this; |
| 1841 | rhs.isNil = true; |
| 1842 | } |
| 1843 | ~JsonDocument() { |
| 1844 | if (!isNil) { |
| 1845 | s_pActiveDocument = nullptr; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1846 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1847 | } |
jrkoonce | ce7fa74 | 2019-09-04 13:31:44 -0500 | [diff] [blame] | 1848 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1849 | private: |
| 1850 | bool isNil = false; |
| 1851 | }; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1852 | |
| 1853 | #ifdef __clang__ |
| 1854 | #pragma clang diagnostic pop |
| 1855 | #endif |
| 1856 | |
jrkoonce | ce7fa74 | 2019-09-04 13:31:44 -0500 | [diff] [blame] | 1857 | #endif // TINYGLTF_USE_RAPIDJSON_CRTALLOCATOR |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1858 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1859 | #else |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1860 | using nlohmann::json; |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 1861 | using json_iterator = json::iterator; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1862 | using json_const_iterator = json::const_iterator; |
| 1863 | using json_const_array_iterator = json_const_iterator; |
| 1864 | using JsonDocument = json; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1865 | #endif |
| 1866 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1867 | void JsonParse(JsonDocument &doc, const char *str, size_t length, |
| 1868 | bool throwExc = false) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1869 | #ifdef TINYGLTF_USE_RAPIDJSON |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1870 | (void)throwExc; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1871 | doc.Parse(str, length); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1872 | #else |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 1873 | doc = detail::json::parse(str, str + length, nullptr, throwExc); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1874 | #endif |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1875 | } |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 1876 | } // namespace detail |
| 1877 | } // namespace tinygltf |
Syoyo Fujita | 2e21be7 | 2017-11-05 17:13:01 +0900 | [diff] [blame] | 1878 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1879 | #ifdef __APPLE__ |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 1880 | #include "TargetConditionals.h" |
Syoyo Fujita | b23f6fe | 2017-11-15 01:03:09 +0900 | [diff] [blame] | 1881 | #endif |
| 1882 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1883 | #ifdef __clang__ |
| 1884 | #pragma clang diagnostic push |
| 1885 | #pragma clang diagnostic ignored "-Wc++98-compat" |
| 1886 | #endif |
| 1887 | |
Syoyo Fujita | e1bef8c | 2016-03-17 21:31:13 +0900 | [diff] [blame] | 1888 | namespace tinygltf { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1889 | |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1890 | /// |
| 1891 | /// Internal LoadImageDataOption struct. |
| 1892 | /// This struct is passed through `user_pointer` in LoadImageData. |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 1893 | /// The struct is not passed when the user supply their own LoadImageData |
| 1894 | /// callbacks. |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1895 | /// |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 1896 | struct LoadImageDataOption { |
| 1897 | // true: preserve image channels(e.g. load as RGB image if the image has RGB |
| 1898 | // channels) default `false`(channels are expanded to RGBA for backward |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 1899 | // compatibility). |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1900 | bool preserve_channels{false}; |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1901 | }; |
| 1902 | |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1903 | // Equals function for Value, for recursivity |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1904 | static bool Equals(const tinygltf::Value &one, const tinygltf::Value &other) { |
| 1905 | if (one.Type() != other.Type()) return false; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1906 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1907 | switch (one.Type()) { |
| 1908 | case NULL_TYPE: |
| 1909 | return true; |
| 1910 | case BOOL_TYPE: |
| 1911 | return one.Get<bool>() == other.Get<bool>(); |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 1912 | case REAL_TYPE: |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1913 | return TINYGLTF_DOUBLE_EQUAL(one.Get<double>(), other.Get<double>()); |
| 1914 | case INT_TYPE: |
| 1915 | return one.Get<int>() == other.Get<int>(); |
| 1916 | case OBJECT_TYPE: { |
| 1917 | auto oneObj = one.Get<tinygltf::Value::Object>(); |
| 1918 | auto otherObj = other.Get<tinygltf::Value::Object>(); |
| 1919 | if (oneObj.size() != otherObj.size()) return false; |
| 1920 | for (auto &it : oneObj) { |
| 1921 | auto otherIt = otherObj.find(it.first); |
| 1922 | if (otherIt == otherObj.end()) return false; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1923 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1924 | if (!Equals(it.second, otherIt->second)) return false; |
| 1925 | } |
| 1926 | return true; |
| 1927 | } |
| 1928 | case ARRAY_TYPE: { |
| 1929 | if (one.Size() != other.Size()) return false; |
| 1930 | for (int i = 0; i < int(one.Size()); ++i) |
Selmar | a63cc63 | 2019-04-16 16:57:43 +0200 | [diff] [blame] | 1931 | if (!Equals(one.Get(i), other.Get(i))) return false; |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1932 | return true; |
| 1933 | } |
| 1934 | case STRING_TYPE: |
| 1935 | return one.Get<std::string>() == other.Get<std::string>(); |
| 1936 | case BINARY_TYPE: |
| 1937 | return one.Get<std::vector<unsigned char> >() == |
| 1938 | other.Get<std::vector<unsigned char> >(); |
| 1939 | default: { |
| 1940 | // unhandled type |
| 1941 | return false; |
| 1942 | } |
| 1943 | } |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | // Equals function for std::vector<double> using TINYGLTF_DOUBLE_EPSILON |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1947 | static bool Equals(const std::vector<double> &one, |
| 1948 | const std::vector<double> &other) { |
| 1949 | if (one.size() != other.size()) return false; |
| 1950 | for (int i = 0; i < int(one.size()); ++i) { |
| 1951 | if (!TINYGLTF_DOUBLE_EQUAL(one[size_t(i)], other[size_t(i)])) return false; |
| 1952 | } |
| 1953 | return true; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1954 | } |
| 1955 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1956 | bool Accessor::operator==(const Accessor &other) const { |
| 1957 | return this->bufferView == other.bufferView && |
| 1958 | this->byteOffset == other.byteOffset && |
| 1959 | this->componentType == other.componentType && |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1960 | this->count == other.count && this->extensions == other.extensions && |
| 1961 | this->extras == other.extras && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1962 | Equals(this->maxValues, other.maxValues) && |
| 1963 | Equals(this->minValues, other.minValues) && this->name == other.name && |
| 1964 | this->normalized == other.normalized && this->type == other.type; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1965 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1966 | bool Animation::operator==(const Animation &other) const { |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 1967 | return this->channels == other.channels && |
| 1968 | this->extensions == other.extensions && this->extras == other.extras && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1969 | this->name == other.name && this->samplers == other.samplers; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1970 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1971 | bool AnimationChannel::operator==(const AnimationChannel &other) const { |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 1972 | return this->extensions == other.extensions && this->extras == other.extras && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1973 | this->target_node == other.target_node && |
| 1974 | this->target_path == other.target_path && |
| 1975 | this->sampler == other.sampler; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1976 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1977 | bool AnimationSampler::operator==(const AnimationSampler &other) const { |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1978 | return this->extras == other.extras && this->extensions == other.extensions && |
| 1979 | this->input == other.input && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1980 | this->interpolation == other.interpolation && |
| 1981 | this->output == other.output; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1982 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1983 | bool Asset::operator==(const Asset &other) const { |
| 1984 | return this->copyright == other.copyright && |
| 1985 | this->extensions == other.extensions && this->extras == other.extras && |
| 1986 | this->generator == other.generator && |
| 1987 | this->minVersion == other.minVersion && this->version == other.version; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1988 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1989 | bool Buffer::operator==(const Buffer &other) const { |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1990 | return this->data == other.data && this->extensions == other.extensions && |
| 1991 | this->extras == other.extras && this->name == other.name && |
| 1992 | this->uri == other.uri; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1993 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1994 | bool BufferView::operator==(const BufferView &other) const { |
| 1995 | return this->buffer == other.buffer && this->byteLength == other.byteLength && |
| 1996 | this->byteOffset == other.byteOffset && |
| 1997 | this->byteStride == other.byteStride && this->name == other.name && |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1998 | this->target == other.target && this->extensions == other.extensions && |
| 1999 | this->extras == other.extras && |
Alexander Wood | 0d77a29 | 2019-01-26 08:58:45 -0500 | [diff] [blame] | 2000 | this->dracoDecoded == other.dracoDecoded; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2001 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2002 | bool Camera::operator==(const Camera &other) const { |
| 2003 | return this->name == other.name && this->extensions == other.extensions && |
| 2004 | this->extras == other.extras && |
| 2005 | this->orthographic == other.orthographic && |
| 2006 | this->perspective == other.perspective && this->type == other.type; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2007 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2008 | bool Image::operator==(const Image &other) const { |
| 2009 | return this->bufferView == other.bufferView && |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 2010 | this->component == other.component && |
| 2011 | this->extensions == other.extensions && this->extras == other.extras && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2012 | this->height == other.height && this->image == other.image && |
| 2013 | this->mimeType == other.mimeType && this->name == other.name && |
| 2014 | this->uri == other.uri && this->width == other.width; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2015 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2016 | bool Light::operator==(const Light &other) const { |
| 2017 | return Equals(this->color, other.color) && this->name == other.name && |
| 2018 | this->type == other.type; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2019 | } |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 2020 | bool AudioEmitter::operator==(const AudioEmitter &other) const { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2021 | return this->name == other.name && |
| 2022 | TINYGLTF_DOUBLE_EQUAL(this->gain, other.gain) && |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 2023 | this->loop == other.loop && this->playing == other.playing && |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2024 | this->type == other.type && |
| 2025 | this->distanceModel == other.distanceModel && |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 2026 | this->source == other.source; |
| 2027 | } |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2028 | bool AudioSource::operator==(const AudioSource &other) const { |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 2029 | return this->name == other.name && this->uri == other.uri; |
| 2030 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2031 | bool Material::operator==(const Material &other) const { |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 2032 | return (this->pbrMetallicRoughness == other.pbrMetallicRoughness) && |
| 2033 | (this->normalTexture == other.normalTexture) && |
| 2034 | (this->occlusionTexture == other.occlusionTexture) && |
| 2035 | (this->emissiveTexture == other.emissiveTexture) && |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 2036 | Equals(this->emissiveFactor, other.emissiveFactor) && |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 2037 | (this->alphaMode == other.alphaMode) && |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 2038 | TINYGLTF_DOUBLE_EQUAL(this->alphaCutoff, other.alphaCutoff) && |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 2039 | (this->doubleSided == other.doubleSided) && |
| 2040 | (this->extensions == other.extensions) && |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 2041 | (this->extras == other.extras) && (this->values == other.values) && |
| 2042 | (this->additionalValues == other.additionalValues) && |
| 2043 | (this->name == other.name); |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2044 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2045 | bool Mesh::operator==(const Mesh &other) const { |
| 2046 | return this->extensions == other.extensions && this->extras == other.extras && |
Selmar Kok | 81b672b | 2019-10-18 16:08:44 +0200 | [diff] [blame] | 2047 | this->name == other.name && Equals(this->weights, other.weights) && |
| 2048 | this->primitives == other.primitives; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2049 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2050 | bool Model::operator==(const Model &other) const { |
| 2051 | return this->accessors == other.accessors && |
| 2052 | this->animations == other.animations && this->asset == other.asset && |
| 2053 | this->buffers == other.buffers && |
| 2054 | this->bufferViews == other.bufferViews && |
| 2055 | this->cameras == other.cameras && |
| 2056 | this->defaultScene == other.defaultScene && |
| 2057 | this->extensions == other.extensions && |
| 2058 | this->extensionsRequired == other.extensionsRequired && |
| 2059 | this->extensionsUsed == other.extensionsUsed && |
| 2060 | this->extras == other.extras && this->images == other.images && |
| 2061 | this->lights == other.lights && this->materials == other.materials && |
| 2062 | this->meshes == other.meshes && this->nodes == other.nodes && |
| 2063 | this->samplers == other.samplers && this->scenes == other.scenes && |
| 2064 | this->skins == other.skins && this->textures == other.textures; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2065 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2066 | bool Node::operator==(const Node &other) const { |
| 2067 | return this->camera == other.camera && this->children == other.children && |
| 2068 | this->extensions == other.extensions && this->extras == other.extras && |
| 2069 | Equals(this->matrix, other.matrix) && this->mesh == other.mesh && |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2070 | (this->light == other.light) && (this->emitter == other.emitter) && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2071 | this->name == other.name && Equals(this->rotation, other.rotation) && |
| 2072 | Equals(this->scale, other.scale) && this->skin == other.skin && |
| 2073 | Equals(this->translation, other.translation) && |
| 2074 | Equals(this->weights, other.weights); |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2075 | } |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 2076 | bool SpotLight::operator==(const SpotLight &other) const { |
| 2077 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2078 | TINYGLTF_DOUBLE_EQUAL(this->innerConeAngle, other.innerConeAngle) && |
| 2079 | TINYGLTF_DOUBLE_EQUAL(this->outerConeAngle, other.outerConeAngle); |
| 2080 | } |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2081 | bool PositionalEmitter::operator==(const PositionalEmitter &other) const { |
| 2082 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2083 | TINYGLTF_DOUBLE_EQUAL(this->coneInnerAngle, other.coneInnerAngle) && |
| 2084 | TINYGLTF_DOUBLE_EQUAL(this->coneOuterAngle, other.coneOuterAngle) && |
| 2085 | TINYGLTF_DOUBLE_EQUAL(this->coneOuterGain, other.coneOuterGain) && |
| 2086 | TINYGLTF_DOUBLE_EQUAL(this->maxDistance, other.maxDistance) && |
| 2087 | TINYGLTF_DOUBLE_EQUAL(this->refDistance, other.refDistance) && |
| 2088 | TINYGLTF_DOUBLE_EQUAL(this->rolloffFactor, other.rolloffFactor); |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 2089 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2090 | bool OrthographicCamera::operator==(const OrthographicCamera &other) const { |
| 2091 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2092 | TINYGLTF_DOUBLE_EQUAL(this->xmag, other.xmag) && |
| 2093 | TINYGLTF_DOUBLE_EQUAL(this->ymag, other.ymag) && |
| 2094 | TINYGLTF_DOUBLE_EQUAL(this->zfar, other.zfar) && |
| 2095 | TINYGLTF_DOUBLE_EQUAL(this->znear, other.znear); |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2096 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2097 | bool Parameter::operator==(const Parameter &other) const { |
| 2098 | if (this->bool_value != other.bool_value || |
| 2099 | this->has_number_value != other.has_number_value) |
| 2100 | return false; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2101 | |
Selmar Kok | 2bda71c | 2018-10-05 14:36:05 +0200 | [diff] [blame] | 2102 | if (!TINYGLTF_DOUBLE_EQUAL(this->number_value, other.number_value)) |
| 2103 | return false; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2104 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2105 | if (this->json_double_value.size() != other.json_double_value.size()) |
| 2106 | return false; |
| 2107 | for (auto &it : this->json_double_value) { |
| 2108 | auto otherIt = other.json_double_value.find(it.first); |
| 2109 | if (otherIt == other.json_double_value.end()) return false; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2110 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2111 | if (!TINYGLTF_DOUBLE_EQUAL(it.second, otherIt->second)) return false; |
| 2112 | } |
| 2113 | |
| 2114 | if (!Equals(this->number_array, other.number_array)) return false; |
| 2115 | |
| 2116 | if (this->string_value != other.string_value) return false; |
| 2117 | |
| 2118 | return true; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2119 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2120 | bool PerspectiveCamera::operator==(const PerspectiveCamera &other) const { |
| 2121 | return TINYGLTF_DOUBLE_EQUAL(this->aspectRatio, other.aspectRatio) && |
| 2122 | this->extensions == other.extensions && this->extras == other.extras && |
| 2123 | TINYGLTF_DOUBLE_EQUAL(this->yfov, other.yfov) && |
| 2124 | TINYGLTF_DOUBLE_EQUAL(this->zfar, other.zfar) && |
| 2125 | TINYGLTF_DOUBLE_EQUAL(this->znear, other.znear); |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2126 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2127 | bool Primitive::operator==(const Primitive &other) const { |
| 2128 | return this->attributes == other.attributes && this->extras == other.extras && |
| 2129 | this->indices == other.indices && this->material == other.material && |
| 2130 | this->mode == other.mode && this->targets == other.targets; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2131 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2132 | bool Sampler::operator==(const Sampler &other) const { |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 2133 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2134 | this->magFilter == other.magFilter && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2135 | this->minFilter == other.minFilter && this->name == other.name && |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 2136 | this->wrapS == other.wrapS && this->wrapT == other.wrapT; |
Syoyo Fujita | 2c521b3 | 2020-12-04 00:50:46 +0900 | [diff] [blame] | 2137 | |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 2138 | // this->wrapR == other.wrapR |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2139 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2140 | bool Scene::operator==(const Scene &other) const { |
| 2141 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2142 | this->name == other.name && this->nodes == other.nodes; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2143 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2144 | bool Skin::operator==(const Skin &other) const { |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 2145 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2146 | this->inverseBindMatrices == other.inverseBindMatrices && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2147 | this->joints == other.joints && this->name == other.name && |
| 2148 | this->skeleton == other.skeleton; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2149 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2150 | bool Texture::operator==(const Texture &other) const { |
| 2151 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2152 | this->name == other.name && this->sampler == other.sampler && |
| 2153 | this->source == other.source; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2154 | } |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 2155 | bool TextureInfo::operator==(const TextureInfo &other) const { |
| 2156 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2157 | this->index == other.index && this->texCoord == other.texCoord; |
| 2158 | } |
| 2159 | bool NormalTextureInfo::operator==(const NormalTextureInfo &other) const { |
| 2160 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2161 | this->index == other.index && this->texCoord == other.texCoord && |
| 2162 | TINYGLTF_DOUBLE_EQUAL(this->scale, other.scale); |
| 2163 | } |
| 2164 | bool OcclusionTextureInfo::operator==(const OcclusionTextureInfo &other) const { |
| 2165 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2166 | this->index == other.index && this->texCoord == other.texCoord && |
| 2167 | TINYGLTF_DOUBLE_EQUAL(this->strength, other.strength); |
| 2168 | } |
| 2169 | bool PbrMetallicRoughness::operator==(const PbrMetallicRoughness &other) const { |
| 2170 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2171 | (this->baseColorTexture == other.baseColorTexture) && |
| 2172 | (this->metallicRoughnessTexture == other.metallicRoughnessTexture) && |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 2173 | Equals(this->baseColorFactor, other.baseColorFactor) && |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 2174 | TINYGLTF_DOUBLE_EQUAL(this->metallicFactor, other.metallicFactor) && |
| 2175 | TINYGLTF_DOUBLE_EQUAL(this->roughnessFactor, other.roughnessFactor); |
| 2176 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2177 | bool Value::operator==(const Value &other) const { |
| 2178 | return Equals(*this, other); |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2179 | } |
| 2180 | |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 2181 | static void swap4(unsigned int *val) { |
| 2182 | #ifdef TINYGLTF_LITTLE_ENDIAN |
| 2183 | (void)val; |
| 2184 | #else |
| 2185 | unsigned int tmp = *val; |
| 2186 | unsigned char *dst = reinterpret_cast<unsigned char *>(val); |
| 2187 | unsigned char *src = reinterpret_cast<unsigned char *>(&tmp); |
| 2188 | |
| 2189 | dst[0] = src[3]; |
| 2190 | dst[1] = src[2]; |
| 2191 | dst[2] = src[1]; |
| 2192 | dst[3] = src[0]; |
| 2193 | #endif |
| 2194 | } |
| 2195 | |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2196 | static std::string JoinPath(const std::string &path0, |
| 2197 | const std::string &path1) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2198 | if (path0.empty()) { |
| 2199 | return path1; |
| 2200 | } else { |
| 2201 | // check '/' |
| 2202 | char lastChar = *path0.rbegin(); |
| 2203 | if (lastChar != '/') { |
| 2204 | return path0 + std::string("/") + path1; |
| 2205 | } else { |
| 2206 | return path0 + path1; |
| 2207 | } |
| 2208 | } |
| 2209 | } |
| 2210 | |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 2211 | static std::string FindFile(const std::vector<std::string> &paths, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2212 | const std::string &filepath, FsCallbacks *fs) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2213 | if (fs == nullptr || fs->ExpandFilePath == nullptr || |
| 2214 | fs->FileExists == nullptr) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2215 | // Error, fs callback[s] missing |
| 2216 | return std::string(); |
| 2217 | } |
| 2218 | |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2219 | // https://github.com/syoyo/tinygltf/issues/416 |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2220 | // Use strlen() since std::string's size/length reports the number of elements |
| 2221 | // in the buffer, not the length of string(null-terminated) strip |
| 2222 | // null-character in the middle of string. |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2223 | size_t slength = strlen(filepath.c_str()); |
| 2224 | if (slength == 0) { |
| 2225 | return std::string(); |
| 2226 | } |
| 2227 | |
| 2228 | std::string cleaned_filepath = std::string(filepath.c_str()); |
| 2229 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2230 | for (size_t i = 0; i < paths.size(); i++) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2231 | std::string absPath = |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2232 | fs->ExpandFilePath(JoinPath(paths[i], cleaned_filepath), fs->user_data); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2233 | if (fs->FileExists(absPath, fs->user_data)) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2234 | return absPath; |
| 2235 | } |
| 2236 | } |
| 2237 | |
| 2238 | return std::string(); |
| 2239 | } |
| 2240 | |
Syoyo Fujita | dc4bb86 | 2018-04-02 02:04:24 +0900 | [diff] [blame] | 2241 | static std::string GetFilePathExtension(const std::string &FileName) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2242 | if (FileName.find_last_of(".") != std::string::npos) |
| 2243 | return FileName.substr(FileName.find_last_of(".") + 1); |
| 2244 | return ""; |
| 2245 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2246 | |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 2247 | static std::string GetBaseDir(const std::string &filepath) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2248 | if (filepath.find_last_of("/\\") != std::string::npos) |
| 2249 | return filepath.substr(0, filepath.find_last_of("/\\")); |
| 2250 | return ""; |
| 2251 | } |
| 2252 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2253 | static std::string GetBaseFilename(const std::string &filepath) { |
Martin Gerhardy | fae6543 | 2022-03-13 18:42:39 +0100 | [diff] [blame] | 2254 | auto idx = filepath.find_last_of("/\\"); |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 2255 | if (idx != std::string::npos) return filepath.substr(idx + 1); |
Alexander Wood | 190382a | 2021-10-08 12:19:13 -0400 | [diff] [blame] | 2256 | return filepath; |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 2257 | } |
| 2258 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2259 | std::string base64_encode(unsigned char const *, unsigned int len); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2260 | std::string base64_decode(std::string const &s); |
| 2261 | |
| 2262 | /* |
| 2263 | base64.cpp and base64.h |
| 2264 | |
| 2265 | Copyright (C) 2004-2008 René Nyffenegger |
| 2266 | |
| 2267 | This source code is provided 'as-is', without any express or implied |
| 2268 | warranty. In no event will the author be held liable for any damages |
| 2269 | arising from the use of this software. |
| 2270 | |
| 2271 | Permission is granted to anyone to use this software for any purpose, |
| 2272 | including commercial applications, and to alter it and redistribute it |
| 2273 | freely, subject to the following restrictions: |
| 2274 | |
| 2275 | 1. The origin of this source code must not be misrepresented; you must not |
| 2276 | claim that you wrote the original source code. If you use this source code |
| 2277 | in a product, an acknowledgment in the product documentation would be |
| 2278 | appreciated but is not required. |
| 2279 | |
| 2280 | 2. Altered source versions must be plainly marked as such, and must not be |
| 2281 | misrepresented as being the original source code. |
| 2282 | |
| 2283 | 3. This notice may not be removed or altered from any source distribution. |
| 2284 | |
| 2285 | René Nyffenegger rene.nyffenegger@adp-gmbh.ch |
| 2286 | |
| 2287 | */ |
| 2288 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 2289 | #ifdef __clang__ |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 2290 | #pragma clang diagnostic push |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 2291 | #pragma clang diagnostic ignored "-Wsign-conversion" |
| 2292 | #pragma clang diagnostic ignored "-Wconversion" |
Syoyo Fujita | 6fd9162 | 2016-05-01 20:27:30 +0900 | [diff] [blame] | 2293 | #endif |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2294 | |
| 2295 | static inline bool is_base64(unsigned char c) { |
| 2296 | return (isalnum(c) || (c == '+') || (c == '/')); |
| 2297 | } |
| 2298 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2299 | std::string base64_encode(unsigned char const *bytes_to_encode, |
| 2300 | unsigned int in_len) { |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 2301 | std::string ret; |
| 2302 | int i = 0; |
| 2303 | int j = 0; |
| 2304 | unsigned char char_array_3[3]; |
| 2305 | unsigned char char_array_4[4]; |
| 2306 | |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 2307 | const char *base64_chars = |
| 2308 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 2309 | "abcdefghijklmnopqrstuvwxyz" |
| 2310 | "0123456789+/"; |
| 2311 | |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 2312 | while (in_len--) { |
| 2313 | char_array_3[i++] = *(bytes_to_encode++); |
| 2314 | if (i == 3) { |
| 2315 | char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2316 | char_array_4[1] = |
| 2317 | ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); |
| 2318 | char_array_4[2] = |
| 2319 | ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 2320 | char_array_4[3] = char_array_3[2] & 0x3f; |
| 2321 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2322 | 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] | 2323 | i = 0; |
| 2324 | } |
| 2325 | } |
| 2326 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2327 | if (i) { |
| 2328 | for (j = i; j < 3; j++) char_array_3[j] = '\0'; |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 2329 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2330 | char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; |
| 2331 | char_array_4[1] = |
| 2332 | ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); |
| 2333 | char_array_4[2] = |
| 2334 | ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 2335 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2336 | 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] | 2337 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2338 | while ((i++ < 3)) ret += '='; |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 2339 | } |
| 2340 | |
| 2341 | return ret; |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 2342 | } |
| 2343 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2344 | std::string base64_decode(std::string const &encoded_string) { |
Syoyo Fujita | 8c6774c | 2016-05-01 20:36:29 +0900 | [diff] [blame] | 2345 | int in_len = static_cast<int>(encoded_string.size()); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2346 | int i = 0; |
| 2347 | int j = 0; |
| 2348 | int in_ = 0; |
| 2349 | unsigned char char_array_4[4], char_array_3[3]; |
| 2350 | std::string ret; |
| 2351 | |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 2352 | const std::string base64_chars = |
| 2353 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 2354 | "abcdefghijklmnopqrstuvwxyz" |
| 2355 | "0123456789+/"; |
| 2356 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2357 | while (in_len-- && (encoded_string[in_] != '=') && |
| 2358 | is_base64(encoded_string[in_])) { |
| 2359 | char_array_4[i++] = encoded_string[in_]; |
| 2360 | in_++; |
| 2361 | if (i == 4) { |
| 2362 | for (i = 0; i < 4; i++) |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2363 | char_array_4[i] = |
| 2364 | static_cast<unsigned char>(base64_chars.find(char_array_4[i])); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2365 | |
| 2366 | char_array_3[0] = |
| 2367 | (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); |
| 2368 | char_array_3[1] = |
| 2369 | ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); |
| 2370 | char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; |
| 2371 | |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 2372 | for (i = 0; (i < 3); i++) ret += char_array_3[i]; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2373 | i = 0; |
| 2374 | } |
| 2375 | } |
| 2376 | |
| 2377 | if (i) { |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 2378 | for (j = i; j < 4; j++) char_array_4[j] = 0; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2379 | |
| 2380 | for (j = 0; j < 4; j++) |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2381 | char_array_4[j] = |
| 2382 | static_cast<unsigned char>(base64_chars.find(char_array_4[j])); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2383 | |
| 2384 | char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); |
| 2385 | char_array_3[1] = |
| 2386 | ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); |
| 2387 | char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; |
| 2388 | |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 2389 | for (j = 0; (j < i - 1); j++) ret += char_array_3[j]; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2390 | } |
| 2391 | |
| 2392 | return ret; |
| 2393 | } |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 2394 | #ifdef __clang__ |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 2395 | #pragma clang diagnostic pop |
Syoyo Fujita | 6fd9162 | 2016-05-01 20:27:30 +0900 | [diff] [blame] | 2396 | #endif |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2397 | |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2398 | // https://github.com/syoyo/tinygltf/issues/228 |
| 2399 | // TODO(syoyo): Use uriparser https://uriparser.github.io/ for stricter Uri |
| 2400 | // decoding? |
| 2401 | // |
Alexander Wood | e4bc6c7 | 2021-10-14 08:54:59 -0400 | [diff] [blame] | 2402 | // Uri Decoding from DLIB |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2403 | // http://dlib.net/dlib/server/server_http.cpp.html |
Alexander Wood | e4bc6c7 | 2021-10-14 08:54:59 -0400 | [diff] [blame] | 2404 | // --- dlib begin ------------------------------------------------------------ |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2405 | // Copyright (C) 2003 Davis E. King (davis@dlib.net) |
Alexander Wood | e4bc6c7 | 2021-10-14 08:54:59 -0400 | [diff] [blame] | 2406 | // License: Boost Software License |
| 2407 | // Boost Software License - Version 1.0 - August 17th, 2003 |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2408 | |
Alexander Wood | e4bc6c7 | 2021-10-14 08:54:59 -0400 | [diff] [blame] | 2409 | // Permission is hereby granted, free of charge, to any person or organization |
| 2410 | // obtaining a copy of the software and accompanying documentation covered by |
| 2411 | // this license (the "Software") to use, reproduce, display, distribute, |
| 2412 | // execute, and transmit the Software, and to prepare derivative works of the |
| 2413 | // Software, and to permit third-parties to whom the Software is furnished to |
| 2414 | // do so, all subject to the following: |
| 2415 | // The copyright notices in the Software and this entire statement, including |
| 2416 | // the above license grant, this restriction and the following disclaimer, |
| 2417 | // must be included in all copies of the Software, in whole or in part, and |
| 2418 | // all derivative works of the Software, unless such copies or derivative |
| 2419 | // works are solely in the form of machine-executable object code generated by |
| 2420 | // a source language processor. |
| 2421 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 2422 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 2423 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
| 2424 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
| 2425 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
| 2426 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 2427 | // DEALINGS IN THE SOFTWARE. |
Syoyo Fujita | cbd3885 | 2022-02-26 21:19:15 +0900 | [diff] [blame] | 2428 | // |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2429 | namespace dlib { |
| 2430 | |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2431 | inline unsigned char from_hex(unsigned char ch) { |
| 2432 | if (ch <= '9' && ch >= '0') |
| 2433 | ch -= '0'; |
| 2434 | else if (ch <= 'f' && ch >= 'a') |
| 2435 | ch -= 'a' - 10; |
| 2436 | else if (ch <= 'F' && ch >= 'A') |
| 2437 | ch -= 'A' - 10; |
| 2438 | else |
| 2439 | ch = 0; |
| 2440 | return ch; |
| 2441 | } |
| 2442 | |
| 2443 | static const std::string urldecode(const std::string &str) { |
| 2444 | using namespace std; |
| 2445 | string result; |
| 2446 | string::size_type i; |
| 2447 | for (i = 0; i < str.size(); ++i) { |
| 2448 | if (str[i] == '+') { |
| 2449 | result += ' '; |
| 2450 | } else if (str[i] == '%' && str.size() > i + 2) { |
| 2451 | const unsigned char ch1 = |
| 2452 | from_hex(static_cast<unsigned char>(str[i + 1])); |
| 2453 | const unsigned char ch2 = |
| 2454 | from_hex(static_cast<unsigned char>(str[i + 2])); |
| 2455 | const unsigned char ch = static_cast<unsigned char>((ch1 << 4) | ch2); |
| 2456 | result += static_cast<char>(ch); |
| 2457 | i += 2; |
| 2458 | } else { |
| 2459 | result += str[i]; |
| 2460 | } |
| 2461 | } |
| 2462 | return result; |
| 2463 | } |
| 2464 | |
| 2465 | } // namespace dlib |
| 2466 | // --- dlib end -------------------------------------------------------------- |
| 2467 | |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 2468 | bool URIDecode(const std::string &in_uri, std::string *out_uri, |
| 2469 | void *user_data) { |
| 2470 | (void)user_data; |
| 2471 | *out_uri = dlib::urldecode(in_uri); |
| 2472 | return true; |
| 2473 | } |
| 2474 | |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 2475 | static bool LoadExternalFile(std::vector<unsigned char> *out, std::string *err, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2476 | std::string *warn, const std::string &filename, |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2477 | const std::string &basedir, bool required, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2478 | size_t reqBytes, bool checkSize, |
| 2479 | size_t maxFileSize, FsCallbacks *fs) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2480 | if (fs == nullptr || fs->FileExists == nullptr || |
| 2481 | fs->ExpandFilePath == nullptr || fs->ReadWholeFile == nullptr) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2482 | // This is a developer error, assert() ? |
| 2483 | if (err) { |
| 2484 | (*err) += "FS callback[s] not set\n"; |
| 2485 | } |
| 2486 | return false; |
| 2487 | } |
| 2488 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2489 | std::string *failMsgOut = required ? err : warn; |
Selmar Kok | e3b3fa9 | 2018-08-22 19:04:21 +0200 | [diff] [blame] | 2490 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 2491 | out->clear(); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2492 | |
| 2493 | std::vector<std::string> paths; |
| 2494 | paths.push_back(basedir); |
| 2495 | paths.push_back("."); |
| 2496 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2497 | std::string filepath = FindFile(paths, filename, fs); |
jianghaosen | b721fb7 | 2017-08-25 21:01:17 +0800 | [diff] [blame] | 2498 | if (filepath.empty() || filename.empty()) { |
Selmar Kok | e3b3fa9 | 2018-08-22 19:04:21 +0200 | [diff] [blame] | 2499 | if (failMsgOut) { |
| 2500 | (*failMsgOut) += "File not found : " + filename + "\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 2501 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2502 | return false; |
| 2503 | } |
| 2504 | |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2505 | // Check file size |
| 2506 | if (fs->GetFileSizeInBytes) { |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2507 | size_t file_size{0}; |
| 2508 | std::string _err; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2509 | bool ok = |
| 2510 | fs->GetFileSizeInBytes(&file_size, &_err, filepath, fs->user_data); |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2511 | if (!ok) { |
| 2512 | if (_err.size()) { |
| 2513 | if (failMsgOut) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2514 | (*failMsgOut) += "Getting file size failed : " + filename + |
| 2515 | ", err = " + _err + "\n"; |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2516 | } |
| 2517 | } |
| 2518 | return false; |
| 2519 | } |
| 2520 | |
| 2521 | if (file_size > maxFileSize) { |
| 2522 | if (failMsgOut) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2523 | (*failMsgOut) += "File size " + std::to_string(file_size) + |
| 2524 | " exceeds maximum allowed file size " + |
| 2525 | std::to_string(maxFileSize) + " : " + filepath + "\n"; |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2526 | } |
| 2527 | return false; |
| 2528 | } |
| 2529 | } |
| 2530 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2531 | std::vector<unsigned char> buf; |
| 2532 | std::string fileReadErr; |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2533 | bool fileRead = |
| 2534 | fs->ReadWholeFile(&buf, &fileReadErr, filepath, fs->user_data); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2535 | if (!fileRead) { |
Selmar Kok | e3b3fa9 | 2018-08-22 19:04:21 +0200 | [diff] [blame] | 2536 | if (failMsgOut) { |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2537 | (*failMsgOut) += |
| 2538 | "File read error : " + filepath + " : " + fileReadErr + "\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 2539 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2540 | return false; |
| 2541 | } |
| 2542 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2543 | size_t sz = buf.size(); |
Syoyo Fujita | 2b0307f | 2017-08-01 18:15:52 +0900 | [diff] [blame] | 2544 | if (sz == 0) { |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2545 | if (failMsgOut) { |
Selmar Kok | e3b3fa9 | 2018-08-22 19:04:21 +0200 | [diff] [blame] | 2546 | (*failMsgOut) += "File is empty : " + filepath + "\n"; |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2547 | } |
Syoyo Fujita | 2b0307f | 2017-08-01 18:15:52 +0900 | [diff] [blame] | 2548 | return false; |
| 2549 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2550 | |
| 2551 | if (checkSize) { |
| 2552 | if (reqBytes == sz) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 2553 | out->swap(buf); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2554 | return true; |
| 2555 | } else { |
| 2556 | std::stringstream ss; |
| 2557 | ss << "File size mismatch : " << filepath << ", requestedBytes " |
| 2558 | << reqBytes << ", but got " << sz << std::endl; |
Selmar Kok | e3b3fa9 | 2018-08-22 19:04:21 +0200 | [diff] [blame] | 2559 | if (failMsgOut) { |
| 2560 | (*failMsgOut) += ss.str(); |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 2561 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2562 | return false; |
| 2563 | } |
| 2564 | } |
| 2565 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 2566 | out->swap(buf); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2567 | return true; |
| 2568 | } |
| 2569 | |
Nyall Dawson | 8c85d5e | 2023-08-28 12:56:09 +1000 | [diff] [blame] | 2570 | void TinyGLTF::SetParseStrictness(ParseStrictness strictness) { |
| 2571 | strictness_ = strictness; |
| 2572 | } |
| 2573 | |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 2574 | void TinyGLTF::SetImageLoader(LoadImageDataFunction func, void *user_data) { |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 2575 | LoadImageData = func; |
| 2576 | load_image_user_data_ = user_data; |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 2577 | user_image_loader_ = true; |
| 2578 | } |
| 2579 | |
| 2580 | void TinyGLTF::RemoveImageLoader() { |
| 2581 | LoadImageData = |
| 2582 | #ifndef TINYGLTF_NO_STB_IMAGE |
| 2583 | &tinygltf::LoadImageData; |
| 2584 | #else |
| 2585 | nullptr; |
| 2586 | #endif |
| 2587 | |
| 2588 | load_image_user_data_ = nullptr; |
| 2589 | user_image_loader_ = false; |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 2590 | } |
| 2591 | |
Squareys | 2d3594d | 2018-03-13 22:40:53 +0100 | [diff] [blame] | 2592 | #ifndef TINYGLTF_NO_STB_IMAGE |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2593 | bool LoadImageData(Image *image, const int image_idx, std::string *err, |
| 2594 | std::string *warn, int req_width, int req_height, |
| 2595 | const unsigned char *bytes, int size, void *user_data) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2596 | (void)warn; |
| 2597 | |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 2598 | LoadImageDataOption option; |
| 2599 | if (user_data) { |
| 2600 | option = *reinterpret_cast<LoadImageDataOption *>(user_data); |
| 2601 | } |
| 2602 | |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 2603 | int w = 0, h = 0, comp = 0, req_comp = 0; |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2604 | |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2605 | unsigned char *data = nullptr; |
Arthur Brainville (Ybalrid) | f2addc0 | 2019-03-02 22:00:48 +0100 | [diff] [blame] | 2606 | |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 2607 | // preserve_channels true: Use channels stored in the image file. |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 2608 | // false: force 32-bit textures for common Vulkan compatibility. It appears |
| 2609 | // that some GPU drivers do not support 24-bit images for Vulkan |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 2610 | req_comp = option.preserve_channels ? 0 : 4; |
Arthur Brainville (Ybalrid) | f2addc0 | 2019-03-02 22:00:48 +0100 | [diff] [blame] | 2611 | int bits = 8; |
| 2612 | int pixel_type = TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE; |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2613 | |
Arthur Brainville (Ybalrid) | f2addc0 | 2019-03-02 22:00:48 +0100 | [diff] [blame] | 2614 | // It is possible that the image we want to load is a 16bit per channel image |
| 2615 | // 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] | 2616 | // 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] | 2617 | // unsigned char, because we are representing "bytes". But we are updating |
| 2618 | // the Image metadata to signal that this image uses 2 bytes (16bits) per |
| 2619 | // channel: |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2620 | if (stbi_is_16_bit_from_memory(bytes, size)) { |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 2621 | data = reinterpret_cast<unsigned char *>( |
| 2622 | 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] | 2623 | if (data) { |
| 2624 | bits = 16; |
| 2625 | pixel_type = TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT; |
| 2626 | } |
Arthur Brainville (Ybalrid) | f2addc0 | 2019-03-02 22:00:48 +0100 | [diff] [blame] | 2627 | } |
| 2628 | |
| 2629 | // at this point, if data is still NULL, it means that the image wasn't |
| 2630 | // 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] | 2631 | // image as we used to do: |
Aurélien Chatelain | 5cb4346 | 2017-05-24 09:55:14 +0000 | [diff] [blame] | 2632 | // if image cannot be decoded, ignore parsing and keep it by its path |
| 2633 | // don't break in this case |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 2634 | // FIXME we should only enter this function if the image is embedded. If |
| 2635 | // image->uri references |
| 2636 | // an image file, it should be left as it is. Image loading should not be |
| 2637 | // mandatory (to support other formats) |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2638 | 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] | 2639 | if (!data) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2640 | // NOTE: you can use `warn` instead of `err` |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2641 | if (err) { |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2642 | (*err) += |
| 2643 | "Unknown image format. STB cannot decode image data for image[" + |
| 2644 | std::to_string(image_idx) + "] name = \"" + image->name + "\".\n"; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2645 | } |
Omar C. F | d492efc | 2018-02-10 09:50:35 +0200 | [diff] [blame] | 2646 | return false; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2647 | } |
| 2648 | |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 2649 | if ((w < 1) || (h < 1)) { |
Arthur Brainville (Ybalrid) | f2addc0 | 2019-03-02 22:00:48 +0100 | [diff] [blame] | 2650 | stbi_image_free(data); |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2651 | if (err) { |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2652 | (*err) += "Invalid image data for image[" + std::to_string(image_idx) + |
| 2653 | "] name = \"" + image->name + "\"\n"; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2654 | } |
Omar C. F | d492efc | 2018-02-10 09:50:35 +0200 | [diff] [blame] | 2655 | return false; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2656 | } |
| 2657 | |
| 2658 | if (req_width > 0) { |
| 2659 | if (req_width != w) { |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2660 | stbi_image_free(data); |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2661 | if (err) { |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2662 | (*err) += "Image width mismatch for image[" + |
| 2663 | std::to_string(image_idx) + "] name = \"" + image->name + |
| 2664 | "\"\n"; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2665 | } |
| 2666 | return false; |
| 2667 | } |
| 2668 | } |
| 2669 | |
| 2670 | if (req_height > 0) { |
| 2671 | if (req_height != h) { |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2672 | stbi_image_free(data); |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2673 | if (err) { |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2674 | (*err) += "Image height mismatch. for image[" + |
| 2675 | std::to_string(image_idx) + "] name = \"" + image->name + |
| 2676 | "\"\n"; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2677 | } |
| 2678 | return false; |
| 2679 | } |
| 2680 | } |
| 2681 | |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 2682 | if (req_comp != 0) { |
| 2683 | // loaded data has `req_comp` channels(components) |
| 2684 | comp = req_comp; |
| 2685 | } |
| 2686 | |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2687 | image->width = w; |
| 2688 | image->height = h; |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 2689 | image->component = comp; |
Arthur Brainville (Ybalrid) | f2addc0 | 2019-03-02 22:00:48 +0100 | [diff] [blame] | 2690 | image->bits = bits; |
| 2691 | image->pixel_type = pixel_type; |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 2692 | image->image.resize(static_cast<size_t>(w * h * comp) * size_t(bits / 8)); |
| 2693 | 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] | 2694 | stbi_image_free(data); |
Syoyo Fujita | 4be2f88 | 2016-11-24 16:20:34 +0900 | [diff] [blame] | 2695 | |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2696 | return true; |
| 2697 | } |
Squareys | 2d3594d | 2018-03-13 22:40:53 +0100 | [diff] [blame] | 2698 | #endif |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2699 | |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2700 | void TinyGLTF::SetImageWriter(WriteImageDataFunction func, void *user_data) { |
| 2701 | WriteImageData = func; |
| 2702 | write_image_user_data_ = user_data; |
| 2703 | } |
| 2704 | |
| 2705 | #ifndef TINYGLTF_NO_STB_IMAGE_WRITE |
| 2706 | static void WriteToMemory_stbi(void *context, void *data, int size) { |
| 2707 | std::vector<unsigned char> *buffer = |
| 2708 | reinterpret_cast<std::vector<unsigned char> *>(context); |
| 2709 | |
| 2710 | unsigned char *pData = reinterpret_cast<unsigned char *>(data); |
| 2711 | |
| 2712 | buffer->insert(buffer->end(), pData, pData + size); |
| 2713 | } |
| 2714 | |
| 2715 | bool WriteImageData(const std::string *basepath, const std::string *filename, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 2716 | const Image *image, bool embedImages, |
| 2717 | const URICallbacks *uri_cb, std::string *out_uri, |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 2718 | void *fsPtr) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2719 | const std::string ext = GetFilePathExtension(*filename); |
| 2720 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2721 | // Write image to temporary buffer |
| 2722 | std::string header; |
| 2723 | std::vector<unsigned char> data; |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2724 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2725 | if (ext == "png") { |
Syoyo Fujita | ca56f72 | 2019-03-07 21:04:25 +0900 | [diff] [blame] | 2726 | if ((image->bits != 8) || |
| 2727 | (image->pixel_type != TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE)) { |
Syoyo Fujita | e8a46c4 | 2019-03-07 20:50:58 +0900 | [diff] [blame] | 2728 | // Unsupported pixel format |
| 2729 | return false; |
| 2730 | } |
| 2731 | |
Keiji Yoshimi | c0cfc1e | 2018-08-28 21:13:03 +0900 | [diff] [blame] | 2732 | if (!stbi_write_png_to_func(WriteToMemory_stbi, &data, image->width, |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2733 | image->height, image->component, |
| 2734 | &image->image[0], 0)) { |
Keiji Yoshimi | c0cfc1e | 2018-08-28 21:13:03 +0900 | [diff] [blame] | 2735 | return false; |
| 2736 | } |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2737 | header = "data:image/png;base64,"; |
| 2738 | } else if (ext == "jpg") { |
Keiji Yoshimi | c0cfc1e | 2018-08-28 21:13:03 +0900 | [diff] [blame] | 2739 | if (!stbi_write_jpg_to_func(WriteToMemory_stbi, &data, image->width, |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2740 | image->height, image->component, |
| 2741 | &image->image[0], 100)) { |
Keiji Yoshimi | c0cfc1e | 2018-08-28 21:13:03 +0900 | [diff] [blame] | 2742 | return false; |
| 2743 | } |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2744 | header = "data:image/jpeg;base64,"; |
| 2745 | } else if (ext == "bmp") { |
Keiji Yoshimi | c0cfc1e | 2018-08-28 21:13:03 +0900 | [diff] [blame] | 2746 | if (!stbi_write_bmp_to_func(WriteToMemory_stbi, &data, image->width, |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2747 | image->height, image->component, |
| 2748 | &image->image[0])) { |
Keiji Yoshimi | c0cfc1e | 2018-08-28 21:13:03 +0900 | [diff] [blame] | 2749 | return false; |
| 2750 | } |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2751 | header = "data:image/bmp;base64,"; |
| 2752 | } else if (!embedImages) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2753 | // Error: can't output requested format to file |
| 2754 | return false; |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2755 | } |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2756 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2757 | if (embedImages) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2758 | // Embed base64-encoded image into URI |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2759 | if (data.size()) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2760 | *out_uri = header + base64_encode(&data[0], |
| 2761 | static_cast<unsigned int>(data.size())); |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2762 | } else { |
| 2763 | // Throw error? |
| 2764 | } |
| 2765 | } else { |
| 2766 | // Write image to disc |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2767 | FsCallbacks *fs = reinterpret_cast<FsCallbacks *>(fsPtr); |
Syoyo Fujita | 33514af | 2018-11-05 13:32:43 +0900 | [diff] [blame] | 2768 | if ((fs != nullptr) && (fs->WriteWholeFile != nullptr)) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2769 | const std::string imagefilepath = JoinPath(*basepath, *filename); |
| 2770 | std::string writeError; |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2771 | if (!fs->WriteWholeFile(&writeError, imagefilepath, data, |
| 2772 | fs->user_data)) { |
| 2773 | // Could not write image file to disc; Throw error ? |
Keiji Yoshimi | c0cfc1e | 2018-08-28 21:13:03 +0900 | [diff] [blame] | 2774 | return false; |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2775 | } |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2776 | } else { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2777 | // Throw error? |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2778 | } |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 2779 | if (uri_cb->encode) { |
| 2780 | if (!uri_cb->encode(*filename, "image", out_uri, uri_cb->user_data)) { |
| 2781 | return false; |
| 2782 | } |
| 2783 | } else { |
| 2784 | *out_uri = *filename; |
| 2785 | } |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2786 | } |
| 2787 | |
| 2788 | return true; |
| 2789 | } |
| 2790 | #endif |
| 2791 | |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 2792 | void TinyGLTF::SetURICallbacks(URICallbacks callbacks) { |
| 2793 | assert(callbacks.decode); |
| 2794 | if (callbacks.decode) { |
| 2795 | uri_cb = callbacks; |
| 2796 | } |
| 2797 | } |
| 2798 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2799 | void TinyGLTF::SetFsCallbacks(FsCallbacks callbacks) { fs = callbacks; } |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2800 | |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 2801 | #ifdef _WIN32 |
| 2802 | static inline std::wstring UTF8ToWchar(const std::string &str) { |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2803 | int wstr_size = |
| 2804 | MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), nullptr, 0); |
imallett | 56e1098 | 2022-10-07 10:35:16 -0700 | [diff] [blame] | 2805 | std::wstring wstr((size_t)wstr_size, 0); |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2806 | MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), &wstr[0], |
| 2807 | (int)wstr.size()); |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 2808 | return wstr; |
| 2809 | } |
cwbhhjl | 2f5aa9f | 2020-06-04 10:40:38 +0800 | [diff] [blame] | 2810 | |
| 2811 | static inline std::string WcharToUTF8(const std::wstring &wstr) { |
| 2812 | int str_size = WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), |
imallett | 56e1098 | 2022-10-07 10:35:16 -0700 | [diff] [blame] | 2813 | nullptr, 0, nullptr, nullptr); |
| 2814 | std::string str((size_t)str_size, 0); |
cwbhhjl | 2f5aa9f | 2020-06-04 10:40:38 +0800 | [diff] [blame] | 2815 | WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), &str[0], |
imallett | 56e1098 | 2022-10-07 10:35:16 -0700 | [diff] [blame] | 2816 | (int)str.size(), nullptr, nullptr); |
cwbhhjl | 2f5aa9f | 2020-06-04 10:40:38 +0800 | [diff] [blame] | 2817 | return str; |
| 2818 | } |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 2819 | #endif |
| 2820 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2821 | #ifndef TINYGLTF_NO_FS |
| 2822 | // Default implementations of filesystem functions |
| 2823 | |
| 2824 | bool FileExists(const std::string &abs_filename, void *) { |
| 2825 | bool ret; |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 2826 | #ifdef TINYGLTF_ANDROID_LOAD_FROM_ASSETS |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 2827 | if (asset_manager) { |
| 2828 | AAsset *asset = AAssetManager_open(asset_manager, abs_filename.c_str(), |
| 2829 | AASSET_MODE_STREAMING); |
| 2830 | if (!asset) { |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 2831 | return false; |
| 2832 | } |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 2833 | AAsset_close(asset); |
| 2834 | ret = true; |
| 2835 | } else { |
| 2836 | return false; |
| 2837 | } |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 2838 | #else |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2839 | #ifdef _WIN32 |
R.E. Kovalev | 112e353 | 2023-07-05 09:11:57 +0300 | [diff] [blame] | 2840 | #if defined(_MSC_VER) || defined(_LIBCPP_VERSION) |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2841 | |
| 2842 | // First check if a file is a directory. |
| 2843 | DWORD result = GetFileAttributesW(UTF8ToWchar(abs_filename).c_str()); |
| 2844 | if (result == INVALID_FILE_ATTRIBUTES) { |
| 2845 | return false; |
| 2846 | } |
| 2847 | if (result & FILE_ATTRIBUTE_DIRECTORY) { |
| 2848 | return false; |
| 2849 | } |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2850 | |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 2851 | FILE *fp = nullptr; |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 2852 | 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] | 2853 | if (err != 0) { |
| 2854 | return false; |
| 2855 | } |
R.E. Kovalev | 112e353 | 2023-07-05 09:11:57 +0300 | [diff] [blame] | 2856 | #elif defined(__GLIBCXX__) |
| 2857 | FILE *fp = fopen(abs_filename.c_str(), "rb"); |
| 2858 | if (!fp) { |
| 2859 | return false; |
| 2860 | } |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2861 | #else |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2862 | // TODO: is_directory check |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 2863 | FILE *fp = nullptr; |
| 2864 | errno_t err = fopen_s(&fp, abs_filename.c_str(), "rb"); |
| 2865 | if (err != 0) { |
| 2866 | return false; |
| 2867 | } |
| 2868 | #endif |
| 2869 | |
| 2870 | #else |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2871 | struct stat sb; |
| 2872 | if (stat(abs_filename.c_str(), &sb)) { |
| 2873 | return false; |
| 2874 | } |
| 2875 | if (S_ISDIR(sb.st_mode)) { |
| 2876 | return false; |
| 2877 | } |
| 2878 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2879 | FILE *fp = fopen(abs_filename.c_str(), "rb"); |
| 2880 | #endif |
| 2881 | if (fp) { |
| 2882 | ret = true; |
| 2883 | fclose(fp); |
| 2884 | } else { |
| 2885 | ret = false; |
| 2886 | } |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 2887 | #endif |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2888 | |
| 2889 | return ret; |
| 2890 | } |
| 2891 | |
| 2892 | std::string ExpandFilePath(const std::string &filepath, void *) { |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 2893 | // https://github.com/syoyo/tinygltf/issues/368 |
| 2894 | // |
| 2895 | // No file path expansion in built-in FS function anymore, since glTF URI |
| 2896 | // should not contain tilde('~') and environment variables, and for security |
| 2897 | // reason(`wordexp`). |
| 2898 | // |
| 2899 | // Users need to supply `base_dir`(in `LoadASCIIFromString`, |
| 2900 | // `LoadBinaryFromMemory`) in expanded absolute path. |
| 2901 | |
| 2902 | return filepath; |
| 2903 | |
| 2904 | #if 0 |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2905 | #ifdef _WIN32 |
Syoyo Fujita | fbbeb4d | 2020-06-06 17:11:50 +0900 | [diff] [blame] | 2906 | // Assume input `filepath` is encoded in UTF-8 |
cwbhhjl | 2f5aa9f | 2020-06-04 10:40:38 +0800 | [diff] [blame] | 2907 | std::wstring wfilepath = UTF8ToWchar(filepath); |
Syoyo Fujita | fbbeb4d | 2020-06-06 17:11:50 +0900 | [diff] [blame] | 2908 | DWORD wlen = ExpandEnvironmentStringsW(wfilepath.c_str(), nullptr, 0); |
cwbhhjl | 2f5aa9f | 2020-06-04 10:40:38 +0800 | [diff] [blame] | 2909 | wchar_t *wstr = new wchar_t[wlen]; |
| 2910 | ExpandEnvironmentStringsW(wfilepath.c_str(), wstr, wlen); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2911 | |
cwbhhjl | 2f5aa9f | 2020-06-04 10:40:38 +0800 | [diff] [blame] | 2912 | std::wstring ws(wstr); |
| 2913 | delete[] wstr; |
| 2914 | return WcharToUTF8(ws); |
Syoyo Fujita | fbbeb4d | 2020-06-06 17:11:50 +0900 | [diff] [blame] | 2915 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2916 | #else |
| 2917 | |
| 2918 | #if defined(TARGET_OS_IPHONE) || defined(TARGET_IPHONE_SIMULATOR) || \ |
Julian Smith | 0598a20 | 2021-08-25 12:06:08 +0100 | [diff] [blame] | 2919 | defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__OpenBSD__) |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2920 | // no expansion |
| 2921 | std::string s = filepath; |
| 2922 | #else |
| 2923 | std::string s; |
| 2924 | wordexp_t p; |
| 2925 | |
| 2926 | if (filepath.empty()) { |
| 2927 | return ""; |
| 2928 | } |
| 2929 | |
Frank Galligan | aa3c5a1 | 2020-01-13 15:06:56 -0800 | [diff] [blame] | 2930 | // Quote the string to keep any spaces in filepath intact. |
| 2931 | std::string quoted_path = "\"" + filepath + "\""; |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2932 | // char** w; |
Frank Galligan | aa3c5a1 | 2020-01-13 15:06:56 -0800 | [diff] [blame] | 2933 | int ret = wordexp(quoted_path.c_str(), &p, 0); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2934 | if (ret) { |
| 2935 | // err |
| 2936 | s = filepath; |
| 2937 | return s; |
| 2938 | } |
| 2939 | |
| 2940 | // Use first element only. |
| 2941 | if (p.we_wordv) { |
| 2942 | s = std::string(p.we_wordv[0]); |
| 2943 | wordfree(&p); |
| 2944 | } else { |
| 2945 | s = filepath; |
| 2946 | } |
| 2947 | |
| 2948 | #endif |
| 2949 | |
| 2950 | return s; |
| 2951 | #endif |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 2952 | #endif |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2953 | } |
| 2954 | |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2955 | bool GetFileSizeInBytes(size_t *filesize_out, std::string *err, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 2956 | const std::string &filepath, void *userdata) { |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2957 | (void)userdata; |
| 2958 | |
| 2959 | #ifdef TINYGLTF_ANDROID_LOAD_FROM_ASSETS |
| 2960 | if (asset_manager) { |
| 2961 | AAsset *asset = AAssetManager_open(asset_manager, filepath.c_str(), |
| 2962 | AASSET_MODE_STREAMING); |
| 2963 | if (!asset) { |
| 2964 | if (err) { |
| 2965 | (*err) += "File open error : " + filepath + "\n"; |
| 2966 | } |
| 2967 | return false; |
| 2968 | } |
| 2969 | size_t size = AAsset_getLength(asset); |
| 2970 | |
| 2971 | if (size == 0) { |
| 2972 | if (err) { |
| 2973 | (*err) += "Invalid file size : " + filepath + |
| 2974 | " (does the path point to a directory?)"; |
| 2975 | } |
| 2976 | return false; |
| 2977 | } |
| 2978 | |
| 2979 | return true; |
| 2980 | } else { |
| 2981 | if (err) { |
| 2982 | (*err) += "No asset manager specified : " + filepath + "\n"; |
| 2983 | } |
| 2984 | return false; |
| 2985 | } |
| 2986 | #else |
| 2987 | #ifdef _WIN32 |
| 2988 | #if defined(__GLIBCXX__) // mingw |
| 2989 | int file_descriptor = |
| 2990 | _wopen(UTF8ToWchar(filepath).c_str(), _O_RDONLY | _O_BINARY); |
| 2991 | __gnu_cxx::stdio_filebuf<char> wfile_buf(file_descriptor, std::ios_base::in); |
| 2992 | std::istream f(&wfile_buf); |
| 2993 | #elif defined(_MSC_VER) || defined(_LIBCPP_VERSION) |
| 2994 | // For libcxx, assume _LIBCPP_HAS_OPEN_WITH_WCHAR is defined to accept |
| 2995 | // `wchar_t *` |
| 2996 | std::ifstream f(UTF8ToWchar(filepath).c_str(), std::ifstream::binary); |
| 2997 | #else |
| 2998 | // Unknown compiler/runtime |
| 2999 | std::ifstream f(filepath.c_str(), std::ifstream::binary); |
| 3000 | #endif |
| 3001 | #else |
| 3002 | std::ifstream f(filepath.c_str(), std::ifstream::binary); |
| 3003 | #endif |
| 3004 | if (!f) { |
| 3005 | if (err) { |
| 3006 | (*err) += "File open error : " + filepath + "\n"; |
| 3007 | } |
| 3008 | return false; |
| 3009 | } |
| 3010 | |
| 3011 | // For directory(and pipe?), peek() will fail(Posix gnustl/libc++ only) |
David Siegel | 47208b2 | 2023-06-05 22:14:30 +0200 | [diff] [blame] | 3012 | f.peek(); |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3013 | if (!f) { |
| 3014 | if (err) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3015 | (*err) += |
| 3016 | "File read error. Maybe empty file or invalid file : " + filepath + |
| 3017 | "\n"; |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3018 | } |
| 3019 | return false; |
| 3020 | } |
| 3021 | |
| 3022 | f.seekg(0, f.end); |
| 3023 | size_t sz = static_cast<size_t>(f.tellg()); |
| 3024 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3025 | // std::cout << "sz = " << sz << "\n"; |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3026 | f.seekg(0, f.beg); |
| 3027 | |
| 3028 | if (int64_t(sz) < 0) { |
| 3029 | if (err) { |
| 3030 | (*err) += "Invalid file size : " + filepath + |
| 3031 | " (does the path point to a directory?)"; |
| 3032 | } |
| 3033 | return false; |
| 3034 | } else if (sz == 0) { |
| 3035 | if (err) { |
| 3036 | (*err) += "File is empty : " + filepath + "\n"; |
| 3037 | } |
| 3038 | return false; |
| 3039 | } else if (sz >= (std::numeric_limits<std::streamoff>::max)()) { |
| 3040 | if (err) { |
| 3041 | (*err) += "Invalid file size : " + filepath + "\n"; |
| 3042 | } |
| 3043 | return false; |
| 3044 | } |
| 3045 | |
| 3046 | (*filesize_out) = sz; |
| 3047 | return true; |
| 3048 | #endif |
| 3049 | } |
| 3050 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3051 | bool ReadWholeFile(std::vector<unsigned char> *out, std::string *err, |
| 3052 | const std::string &filepath, void *) { |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 3053 | #ifdef TINYGLTF_ANDROID_LOAD_FROM_ASSETS |
| 3054 | if (asset_manager) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 3055 | AAsset *asset = AAssetManager_open(asset_manager, filepath.c_str(), |
| 3056 | AASSET_MODE_STREAMING); |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 3057 | if (!asset) { |
| 3058 | if (err) { |
| 3059 | (*err) += "File open error : " + filepath + "\n"; |
| 3060 | } |
| 3061 | return false; |
| 3062 | } |
| 3063 | size_t size = AAsset_getLength(asset); |
Syoyo Fujita | 73c4cce | 2020-04-28 01:06:34 +0900 | [diff] [blame] | 3064 | if (size == 0) { |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 3065 | if (err) { |
| 3066 | (*err) += "Invalid file size : " + filepath + |
| 3067 | " (does the path point to a directory?)"; |
| 3068 | } |
Syoyo Fujita | 73c4cce | 2020-04-28 01:06:34 +0900 | [diff] [blame] | 3069 | return false; |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 3070 | } |
| 3071 | out->resize(size); |
| 3072 | AAsset_read(asset, reinterpret_cast<char *>(&out->at(0)), size); |
| 3073 | AAsset_close(asset); |
| 3074 | return true; |
| 3075 | } else { |
| 3076 | if (err) { |
| 3077 | (*err) += "No asset manager specified : " + filepath + "\n"; |
| 3078 | } |
| 3079 | return false; |
| 3080 | } |
| 3081 | #else |
Harokyang | 5cecef2 | 2019-10-30 15:16:46 +0800 | [diff] [blame] | 3082 | #ifdef _WIN32 |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 3083 | #if defined(__GLIBCXX__) // mingw |
| 3084 | int file_descriptor = |
| 3085 | _wopen(UTF8ToWchar(filepath).c_str(), _O_RDONLY | _O_BINARY); |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 3086 | __gnu_cxx::stdio_filebuf<char> wfile_buf(file_descriptor, std::ios_base::in); |
| 3087 | std::istream f(&wfile_buf); |
Syoyo Fujita | aba57bb | 2020-05-18 20:50:45 +0900 | [diff] [blame] | 3088 | #elif defined(_MSC_VER) || defined(_LIBCPP_VERSION) |
Rahul Sheth | 96a8b2c | 2020-07-10 14:27:46 -0400 | [diff] [blame] | 3089 | // For libcxx, assume _LIBCPP_HAS_OPEN_WITH_WCHAR is defined to accept |
| 3090 | // `wchar_t *` |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 3091 | std::ifstream f(UTF8ToWchar(filepath).c_str(), std::ifstream::binary); |
Syoyo Fujita | aba57bb | 2020-05-18 20:50:45 +0900 | [diff] [blame] | 3092 | #else |
| 3093 | // Unknown compiler/runtime |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 3094 | std::ifstream f(filepath.c_str(), std::ifstream::binary); |
| 3095 | #endif |
Harokyang | 5cecef2 | 2019-10-30 15:16:46 +0800 | [diff] [blame] | 3096 | #else |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3097 | std::ifstream f(filepath.c_str(), std::ifstream::binary); |
Harokyang | 5cecef2 | 2019-10-30 15:16:46 +0800 | [diff] [blame] | 3098 | #endif |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3099 | if (!f) { |
| 3100 | if (err) { |
| 3101 | (*err) += "File open error : " + filepath + "\n"; |
| 3102 | } |
| 3103 | return false; |
| 3104 | } |
| 3105 | |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3106 | // For directory(and pipe?), peek() will fail(Posix gnustl/libc++ only) |
David Siegel | 47208b2 | 2023-06-05 22:14:30 +0200 | [diff] [blame] | 3107 | f.peek(); |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3108 | if (!f) { |
| 3109 | if (err) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3110 | (*err) += |
| 3111 | "File read error. Maybe empty file or invalid file : " + filepath + |
| 3112 | "\n"; |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3113 | } |
| 3114 | return false; |
| 3115 | } |
| 3116 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3117 | f.seekg(0, f.end); |
| 3118 | size_t sz = static_cast<size_t>(f.tellg()); |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3119 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3120 | // std::cout << "sz = " << sz << "\n"; |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3121 | f.seekg(0, f.beg); |
| 3122 | |
Syoyo Fujita | e886247 | 2019-10-20 17:47:50 +0900 | [diff] [blame] | 3123 | if (int64_t(sz) < 0) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3124 | if (err) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 3125 | (*err) += "Invalid file size : " + filepath + |
| 3126 | " (does the path point to a directory?)"; |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3127 | } |
| 3128 | return false; |
| 3129 | } else if (sz == 0) { |
| 3130 | if (err) { |
| 3131 | (*err) += "File is empty : " + filepath + "\n"; |
| 3132 | } |
| 3133 | return false; |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3134 | } else if (sz >= (std::numeric_limits<std::streamoff>::max)()) { |
| 3135 | if (err) { |
| 3136 | (*err) += "Invalid file size : " + filepath + "\n"; |
| 3137 | } |
| 3138 | return false; |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3139 | } |
| 3140 | |
| 3141 | out->resize(sz); |
| 3142 | f.read(reinterpret_cast<char *>(&out->at(0)), |
| 3143 | static_cast<std::streamsize>(sz)); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3144 | |
| 3145 | return true; |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 3146 | #endif |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3147 | } |
| 3148 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 3149 | bool WriteWholeFile(std::string *err, const std::string &filepath, |
| 3150 | const std::vector<unsigned char> &contents, void *) { |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 3151 | #ifdef _WIN32 |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 3152 | #if defined(__GLIBCXX__) // mingw |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 3153 | int file_descriptor = _wopen(UTF8ToWchar(filepath).c_str(), |
| 3154 | _O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY); |
| 3155 | __gnu_cxx::stdio_filebuf<char> wfile_buf( |
| 3156 | file_descriptor, std::ios_base::out | std::ios_base::binary); |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 3157 | std::ostream f(&wfile_buf); |
| 3158 | #elif defined(_MSC_VER) |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 3159 | std::ofstream f(UTF8ToWchar(filepath).c_str(), std::ofstream::binary); |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 3160 | #else // clang? |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 3161 | std::ofstream f(filepath.c_str(), std::ofstream::binary); |
| 3162 | #endif |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 3163 | #else |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3164 | std::ofstream f(filepath.c_str(), std::ofstream::binary); |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 3165 | #endif |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3166 | if (!f) { |
| 3167 | if (err) { |
| 3168 | (*err) += "File open error for writing : " + filepath + "\n"; |
| 3169 | } |
| 3170 | return false; |
| 3171 | } |
| 3172 | |
| 3173 | f.write(reinterpret_cast<const char *>(&contents.at(0)), |
| 3174 | static_cast<std::streamsize>(contents.size())); |
| 3175 | if (!f) { |
| 3176 | if (err) { |
| 3177 | (*err) += "File write error: " + filepath + "\n"; |
| 3178 | } |
| 3179 | return false; |
| 3180 | } |
| 3181 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3182 | return true; |
| 3183 | } |
| 3184 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 3185 | #endif // TINYGLTF_NO_FS |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3186 | |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3187 | static std::string MimeToExt(const std::string &mimeType) { |
| 3188 | if (mimeType == "image/jpeg") { |
| 3189 | return "jpg"; |
| 3190 | } else if (mimeType == "image/png") { |
| 3191 | return "png"; |
| 3192 | } else if (mimeType == "image/bmp") { |
| 3193 | return "bmp"; |
| 3194 | } else if (mimeType == "image/gif") { |
| 3195 | return "gif"; |
| 3196 | } |
| 3197 | |
| 3198 | return ""; |
| 3199 | } |
| 3200 | |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 3201 | static bool UpdateImageObject(const Image &image, std::string &baseDir, |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 3202 | int index, bool embedImages, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 3203 | const URICallbacks *uri_cb, |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 3204 | WriteImageDataFunction *WriteImageData, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 3205 | void *user_data, std::string *out_uri) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3206 | std::string filename; |
| 3207 | std::string ext; |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 3208 | // If image has uri, use it as a filename |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3209 | if (image.uri.size()) { |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 3210 | std::string decoded_uri; |
| 3211 | if (!uri_cb->decode(image.uri, &decoded_uri, uri_cb->user_data)) { |
| 3212 | // A decode failure results in a failure to write the gltf. |
| 3213 | return false; |
| 3214 | } |
| 3215 | filename = GetBaseFilename(decoded_uri); |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3216 | ext = GetFilePathExtension(filename); |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 3217 | } else if (image.bufferView != -1) { |
| 3218 | // If there's no URI and the data exists in a buffer, |
| 3219 | // don't change properties or write images |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3220 | } else if (image.name.size()) { |
| 3221 | ext = MimeToExt(image.mimeType); |
| 3222 | // Otherwise use name as filename |
| 3223 | filename = image.name + "." + ext; |
| 3224 | } else { |
| 3225 | ext = MimeToExt(image.mimeType); |
| 3226 | // Fallback to index of image as filename |
| 3227 | filename = std::to_string(index) + "." + ext; |
| 3228 | } |
| 3229 | |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 3230 | // If callback is set and image data exists, modify image data object. If |
| 3231 | // image data does not exist, this is not considered a failure and the |
| 3232 | // original uri should be maintained. |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 3233 | bool imageWritten = false; |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 3234 | if (*WriteImageData != nullptr && !filename.empty() && !image.image.empty()) { |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 3235 | imageWritten = (*WriteImageData)(&baseDir, &filename, &image, embedImages, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 3236 | uri_cb, out_uri, user_data); |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 3237 | if (!imageWritten) { |
| 3238 | return false; |
| 3239 | } |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 3240 | } |
| 3241 | |
| 3242 | // Use the original uri if the image was not written. |
| 3243 | if (!imageWritten) { |
| 3244 | *out_uri = image.uri; |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3245 | } |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 3246 | |
| 3247 | return true; |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3248 | } |
| 3249 | |
Selmar Kok | 0d0e97e | 2018-08-22 14:01:57 +0200 | [diff] [blame] | 3250 | bool IsDataURI(const std::string &in) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3251 | std::string header = "data:application/octet-stream;base64,"; |
| 3252 | if (in.find(header) == 0) { |
| 3253 | return true; |
| 3254 | } |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3255 | |
Omar C. F | f8a8d9c | 2018-02-10 09:09:41 +0200 | [diff] [blame] | 3256 | header = "data:image/jpeg;base64,"; |
| 3257 | if (in.find(header) == 0) { |
| 3258 | return true; |
| 3259 | } |
Squareys | 4337463 | 2018-03-13 22:20:48 +0100 | [diff] [blame] | 3260 | |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3261 | header = "data:image/png;base64,"; |
| 3262 | if (in.find(header) == 0) { |
| 3263 | return true; |
| 3264 | } |
Squareys | 4337463 | 2018-03-13 22:20:48 +0100 | [diff] [blame] | 3265 | |
Omar C. F | f8a8d9c | 2018-02-10 09:09:41 +0200 | [diff] [blame] | 3266 | header = "data:image/bmp;base64,"; |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 3267 | if (in.find(header) == 0) { |
Omar C. F | f8a8d9c | 2018-02-10 09:09:41 +0200 | [diff] [blame] | 3268 | return true; |
| 3269 | } |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3270 | |
Omar C. F | f8a8d9c | 2018-02-10 09:09:41 +0200 | [diff] [blame] | 3271 | header = "data:image/gif;base64,"; |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 3272 | if (in.find(header) == 0) { |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3273 | return true; |
| 3274 | } |
| 3275 | |
Luke San Antonio | caa24b0 | 2016-06-15 02:23:09 -0400 | [diff] [blame] | 3276 | header = "data:text/plain;base64,"; |
| 3277 | if (in.find(header) == 0) { |
| 3278 | return true; |
| 3279 | } |
| 3280 | |
Syoyo Fujita | 20244e1 | 2018-03-15 11:01:05 -0500 | [diff] [blame] | 3281 | header = "data:application/gltf-buffer;base64,"; |
| 3282 | if (in.find(header) == 0) { |
| 3283 | return true; |
| 3284 | } |
| 3285 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3286 | return false; |
| 3287 | } |
| 3288 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 3289 | bool DecodeDataURI(std::vector<unsigned char> *out, std::string &mime_type, |
| 3290 | const std::string &in, size_t reqBytes, bool checkSize) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3291 | std::string header = "data:application/octet-stream;base64,"; |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3292 | std::string data; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3293 | if (in.find(header) == 0) { |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 3294 | data = base64_decode(in.substr(header.size())); // cut mime string. |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3295 | } |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3296 | |
| 3297 | if (data.empty()) { |
| 3298 | header = "data:image/jpeg;base64,"; |
| 3299 | if (in.find(header) == 0) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3300 | mime_type = "image/jpeg"; |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 3301 | data = base64_decode(in.substr(header.size())); // cut mime string. |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3302 | } |
| 3303 | } |
| 3304 | |
| 3305 | if (data.empty()) { |
| 3306 | header = "data:image/png;base64,"; |
| 3307 | if (in.find(header) == 0) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3308 | mime_type = "image/png"; |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 3309 | data = base64_decode(in.substr(header.size())); // cut mime string. |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3310 | } |
| 3311 | } |
Squareys | 4337463 | 2018-03-13 22:20:48 +0100 | [diff] [blame] | 3312 | |
Omar C. F | f8a8d9c | 2018-02-10 09:09:41 +0200 | [diff] [blame] | 3313 | if (data.empty()) { |
| 3314 | header = "data:image/bmp;base64,"; |
| 3315 | if (in.find(header) == 0) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3316 | mime_type = "image/bmp"; |
Omar C. F | f8a8d9c | 2018-02-10 09:09:41 +0200 | [diff] [blame] | 3317 | data = base64_decode(in.substr(header.size())); // cut mime string. |
| 3318 | } |
| 3319 | } |
| 3320 | |
| 3321 | if (data.empty()) { |
| 3322 | header = "data:image/gif;base64,"; |
| 3323 | if (in.find(header) == 0) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3324 | mime_type = "image/gif"; |
Omar C. F | f8a8d9c | 2018-02-10 09:09:41 +0200 | [diff] [blame] | 3325 | data = base64_decode(in.substr(header.size())); // cut mime string. |
| 3326 | } |
| 3327 | } |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3328 | |
| 3329 | if (data.empty()) { |
Luke San Antonio | caa24b0 | 2016-06-15 02:23:09 -0400 | [diff] [blame] | 3330 | header = "data:text/plain;base64,"; |
| 3331 | if (in.find(header) == 0) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3332 | mime_type = "text/plain"; |
Luke San Antonio | caa24b0 | 2016-06-15 02:23:09 -0400 | [diff] [blame] | 3333 | data = base64_decode(in.substr(header.size())); |
| 3334 | } |
| 3335 | } |
| 3336 | |
| 3337 | if (data.empty()) { |
Syoyo Fujita | 20244e1 | 2018-03-15 11:01:05 -0500 | [diff] [blame] | 3338 | header = "data:application/gltf-buffer;base64,"; |
| 3339 | if (in.find(header) == 0) { |
| 3340 | data = base64_decode(in.substr(header.size())); |
| 3341 | } |
| 3342 | } |
| 3343 | |
Syoyo Fujita | a11f6e1 | 2019-12-19 14:16:48 +0900 | [diff] [blame] | 3344 | // TODO(syoyo): Allow empty buffer? #229 |
Syoyo Fujita | 20244e1 | 2018-03-15 11:01:05 -0500 | [diff] [blame] | 3345 | if (data.empty()) { |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3346 | return false; |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 3347 | } |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3348 | |
| 3349 | if (checkSize) { |
| 3350 | if (data.size() != reqBytes) { |
| 3351 | return false; |
| 3352 | } |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3353 | out->resize(reqBytes); |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3354 | } else { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3355 | out->resize(data.size()); |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3356 | } |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3357 | std::copy(data.begin(), data.end(), out->begin()); |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3358 | return true; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3359 | } |
| 3360 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3361 | namespace detail { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3362 | bool GetInt(const detail::json &o, int &val) { |
jrkoonce | 5cecc41 | 2019-08-29 11:45:04 -0500 | [diff] [blame] | 3363 | #ifdef TINYGLTF_USE_RAPIDJSON |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3364 | if (!o.IsDouble()) { |
| 3365 | if (o.IsInt()) { |
| 3366 | val = o.GetInt(); |
| 3367 | return true; |
| 3368 | } else if (o.IsUint()) { |
| 3369 | val = static_cast<int>(o.GetUint()); |
| 3370 | return true; |
| 3371 | } else if (o.IsInt64()) { |
| 3372 | val = static_cast<int>(o.GetInt64()); |
| 3373 | return true; |
| 3374 | } else if (o.IsUint64()) { |
| 3375 | val = static_cast<int>(o.GetUint64()); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3376 | return true; |
| 3377 | } |
jrkoonce | 5cecc41 | 2019-08-29 11:45:04 -0500 | [diff] [blame] | 3378 | } |
| 3379 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3380 | return false; |
jrkoonce | 5cecc41 | 2019-08-29 11:45:04 -0500 | [diff] [blame] | 3381 | #else |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3382 | auto type = o.type(); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3383 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3384 | if ((type == detail::json::value_t::number_integer) || |
| 3385 | (type == detail::json::value_t::number_unsigned)) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3386 | val = static_cast<int>(o.get<int64_t>()); |
| 3387 | return true; |
jrkoonce | 5cecc41 | 2019-08-29 11:45:04 -0500 | [diff] [blame] | 3388 | } |
| 3389 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3390 | return false; |
jrkoonce | 5cecc41 | 2019-08-29 11:45:04 -0500 | [diff] [blame] | 3391 | #endif |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3392 | } |
| 3393 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3394 | #ifdef TINYGLTF_USE_RAPIDJSON |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3395 | bool GetDouble(const detail::json &o, double &val) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3396 | if (o.IsDouble()) { |
| 3397 | val = o.GetDouble(); |
| 3398 | return true; |
| 3399 | } |
| 3400 | |
| 3401 | return false; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3402 | } |
Syoyo Fujita | 1100f0f | 2019-10-30 15:16:34 +0900 | [diff] [blame] | 3403 | #endif |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3404 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3405 | bool GetNumber(const detail::json &o, double &val) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3406 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3407 | if (o.IsNumber()) { |
| 3408 | val = o.GetDouble(); |
| 3409 | return true; |
| 3410 | } |
| 3411 | |
| 3412 | return false; |
| 3413 | #else |
| 3414 | if (o.is_number()) { |
| 3415 | val = o.get<double>(); |
| 3416 | return true; |
| 3417 | } |
| 3418 | |
| 3419 | return false; |
| 3420 | #endif |
| 3421 | } |
| 3422 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3423 | bool GetString(const detail::json &o, std::string &val) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3424 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3425 | if (o.IsString()) { |
| 3426 | val = o.GetString(); |
| 3427 | return true; |
| 3428 | } |
| 3429 | |
| 3430 | return false; |
| 3431 | #else |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3432 | if (o.type() == detail::json::value_t::string) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3433 | val = o.get<std::string>(); |
| 3434 | return true; |
| 3435 | } |
| 3436 | |
| 3437 | return false; |
| 3438 | #endif |
| 3439 | } |
| 3440 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3441 | bool IsArray(const detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3442 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3443 | return o.IsArray(); |
| 3444 | #else |
| 3445 | return o.is_array(); |
| 3446 | #endif |
| 3447 | } |
| 3448 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3449 | detail::json_const_array_iterator ArrayBegin(const detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3450 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3451 | return o.Begin(); |
| 3452 | #else |
| 3453 | return o.begin(); |
| 3454 | #endif |
| 3455 | } |
| 3456 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3457 | detail::json_const_array_iterator ArrayEnd(const detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3458 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3459 | return o.End(); |
| 3460 | #else |
| 3461 | return o.end(); |
| 3462 | #endif |
| 3463 | } |
| 3464 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3465 | bool IsObject(const detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3466 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3467 | return o.IsObject(); |
| 3468 | #else |
| 3469 | return o.is_object(); |
| 3470 | #endif |
| 3471 | } |
| 3472 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3473 | detail::json_const_iterator ObjectBegin(const detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3474 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3475 | return o.MemberBegin(); |
| 3476 | #else |
| 3477 | return o.begin(); |
| 3478 | #endif |
| 3479 | } |
| 3480 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3481 | detail::json_const_iterator ObjectEnd(const detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3482 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3483 | return o.MemberEnd(); |
| 3484 | #else |
| 3485 | return o.end(); |
| 3486 | #endif |
| 3487 | } |
| 3488 | |
Rahul Sheth | 01d5438 | 2020-07-10 14:27:37 -0400 | [diff] [blame] | 3489 | // Making this a const char* results in a pointer to a temporary when |
| 3490 | // TINYGLTF_USE_RAPIDJSON is off. |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3491 | std::string GetKey(detail::json_const_iterator &it) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3492 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3493 | return it->name.GetString(); |
| 3494 | #else |
| 3495 | return it.key().c_str(); |
| 3496 | #endif |
| 3497 | } |
| 3498 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3499 | bool FindMember(const detail::json &o, const char *member, |
| 3500 | detail::json_const_iterator &it) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3501 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3502 | if (!o.IsObject()) { |
| 3503 | return false; |
| 3504 | } |
| 3505 | it = o.FindMember(member); |
| 3506 | return it != o.MemberEnd(); |
| 3507 | #else |
| 3508 | it = o.find(member); |
| 3509 | return it != o.end(); |
| 3510 | #endif |
| 3511 | } |
| 3512 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3513 | bool FindMember(detail::json &o, const char *member, |
| 3514 | detail::json_iterator &it) { |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 3515 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3516 | if (!o.IsObject()) { |
| 3517 | return false; |
| 3518 | } |
| 3519 | it = o.FindMember(member); |
| 3520 | return it != o.MemberEnd(); |
| 3521 | #else |
| 3522 | it = o.find(member); |
| 3523 | return it != o.end(); |
| 3524 | #endif |
| 3525 | } |
| 3526 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3527 | void Erase(detail::json &o, detail::json_iterator &it) { |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 3528 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3529 | o.EraseMember(it); |
| 3530 | #else |
| 3531 | o.erase(it); |
| 3532 | #endif |
| 3533 | } |
| 3534 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3535 | bool IsEmpty(const detail::json &o) { |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 3536 | #ifdef TINYGLTF_USE_RAPIDJSON |
David Siegel | a1a34cb | 2023-06-07 15:30:02 +0200 | [diff] [blame] | 3537 | return o.ObjectEmpty(); |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 3538 | #else |
| 3539 | return o.empty(); |
| 3540 | #endif |
| 3541 | } |
| 3542 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3543 | const detail::json &GetValue(detail::json_const_iterator &it) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3544 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3545 | return it->value; |
| 3546 | #else |
| 3547 | return it.value(); |
| 3548 | #endif |
| 3549 | } |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 3550 | |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 3551 | detail::json &GetValue(detail::json_iterator &it) { |
| 3552 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3553 | return it->value; |
| 3554 | #else |
| 3555 | return it.value(); |
| 3556 | #endif |
| 3557 | } |
| 3558 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3559 | std::string JsonToString(const detail::json &o, int spacing = -1) { |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 3560 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3561 | using namespace rapidjson; |
| 3562 | StringBuffer buffer; |
| 3563 | if (spacing == -1) { |
| 3564 | Writer<StringBuffer> writer(buffer); |
Syoyo Fujita | 544969b | 2022-07-13 19:03:33 +0900 | [diff] [blame] | 3565 | // TODO: Better error handling. |
| 3566 | // https://github.com/syoyo/tinygltf/issues/332 |
| 3567 | if (!o.Accept(writer)) { |
| 3568 | return "tiny_gltf::JsonToString() failed rapidjson conversion"; |
| 3569 | } |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 3570 | } else { |
| 3571 | PrettyWriter<StringBuffer> writer(buffer); |
| 3572 | writer.SetIndent(' ', uint32_t(spacing)); |
Syoyo Fujita | 544969b | 2022-07-13 19:03:33 +0900 | [diff] [blame] | 3573 | if (!o.Accept(writer)) { |
| 3574 | return "tiny_gltf::JsonToString() failed rapidjson conversion"; |
| 3575 | } |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 3576 | } |
| 3577 | return buffer.GetString(); |
| 3578 | #else |
| 3579 | return o.dump(spacing); |
| 3580 | #endif |
| 3581 | } |
| 3582 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3583 | } // namespace detail |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3584 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3585 | static bool ParseJsonAsValue(Value *ret, const detail::json &o) { |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3586 | Value val{}; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3587 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3588 | using rapidjson::Type; |
| 3589 | switch (o.GetType()) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3590 | case Type::kObjectType: { |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3591 | Value::Object value_object; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3592 | for (auto it = o.MemberBegin(); it != o.MemberEnd(); ++it) { |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3593 | Value entry; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3594 | ParseJsonAsValue(&entry, it->value); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3595 | if (entry.Type() != NULL_TYPE) |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3596 | value_object.emplace(detail::GetKey(it), std::move(entry)); |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3597 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3598 | if (value_object.size() > 0) val = Value(std::move(value_object)); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 3599 | } break; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3600 | case Type::kArrayType: { |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3601 | Value::Array value_array; |
jrkoonce | 5cecc41 | 2019-08-29 11:45:04 -0500 | [diff] [blame] | 3602 | value_array.reserve(o.Size()); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3603 | for (auto it = o.Begin(); it != o.End(); ++it) { |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3604 | Value entry; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3605 | ParseJsonAsValue(&entry, *it); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3606 | if (entry.Type() != NULL_TYPE) |
| 3607 | value_array.emplace_back(std::move(entry)); |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3608 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3609 | if (value_array.size() > 0) val = Value(std::move(value_array)); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 3610 | } break; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3611 | case Type::kStringType: |
| 3612 | val = Value(std::string(o.GetString())); |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3613 | break; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3614 | case Type::kFalseType: |
| 3615 | case Type::kTrueType: |
| 3616 | val = Value(o.GetBool()); |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3617 | break; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3618 | case Type::kNumberType: |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3619 | if (!o.IsDouble()) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3620 | int i = 0; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3621 | detail::GetInt(o, i); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3622 | val = Value(i); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3623 | } else { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3624 | double d = 0.0; |
David | 3831802 | 2023-02-15 23:21:09 -0600 | [diff] [blame] | 3625 | detail::GetDouble(o, d); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3626 | val = Value(d); |
| 3627 | } |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3628 | break; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3629 | case Type::kNullType: |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3630 | break; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 3631 | // all types are covered, so no `case default` |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 3632 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3633 | #else |
| 3634 | switch (o.type()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3635 | case detail::json::value_t::object: { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3636 | Value::Object value_object; |
| 3637 | for (auto it = o.begin(); it != o.end(); it++) { |
| 3638 | Value entry; |
| 3639 | ParseJsonAsValue(&entry, it.value()); |
| 3640 | if (entry.Type() != NULL_TYPE) |
| 3641 | value_object.emplace(it.key(), std::move(entry)); |
| 3642 | } |
| 3643 | if (value_object.size() > 0) val = Value(std::move(value_object)); |
| 3644 | } break; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3645 | case detail::json::value_t::array: { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3646 | Value::Array value_array; |
| 3647 | value_array.reserve(o.size()); |
| 3648 | for (auto it = o.begin(); it != o.end(); it++) { |
| 3649 | Value entry; |
| 3650 | ParseJsonAsValue(&entry, it.value()); |
| 3651 | if (entry.Type() != NULL_TYPE) |
| 3652 | value_array.emplace_back(std::move(entry)); |
| 3653 | } |
| 3654 | if (value_array.size() > 0) val = Value(std::move(value_array)); |
| 3655 | } break; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3656 | case detail::json::value_t::string: |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3657 | val = Value(o.get<std::string>()); |
| 3658 | break; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3659 | case detail::json::value_t::boolean: |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3660 | val = Value(o.get<bool>()); |
| 3661 | break; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3662 | case detail::json::value_t::number_integer: |
| 3663 | case detail::json::value_t::number_unsigned: |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3664 | val = Value(static_cast<int>(o.get<int64_t>())); |
| 3665 | break; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3666 | case detail::json::value_t::number_float: |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3667 | val = Value(o.get<double>()); |
| 3668 | break; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3669 | case detail::json::value_t::null: |
| 3670 | case detail::json::value_t::discarded: |
| 3671 | case detail::json::value_t::binary: |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3672 | // default: |
| 3673 | break; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3674 | } |
| 3675 | #endif |
Marcin Kacprzak | f4f5c3c | 2022-09-02 16:15:54 +0200 | [diff] [blame] | 3676 | const bool isNotNull = val.Type() != NULL_TYPE; |
| 3677 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3678 | if (ret) *ret = std::move(val); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 3679 | |
Marcin Kacprzak | f4f5c3c | 2022-09-02 16:15:54 +0200 | [diff] [blame] | 3680 | return isNotNull; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 3681 | } |
| 3682 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3683 | static bool ParseExtrasProperty(Value *ret, const detail::json &o) { |
| 3684 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3685 | if (!detail::FindMember(o, "extras", it)) { |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 3686 | return false; |
| 3687 | } |
| 3688 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3689 | return ParseJsonAsValue(ret, detail::GetValue(it)); |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 3690 | } |
| 3691 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3692 | static bool ParseBooleanProperty(bool *ret, std::string *err, |
| 3693 | const detail::json &o, |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 3694 | const std::string &property, |
| 3695 | const bool required, |
| 3696 | const std::string &parent_node = "") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3697 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3698 | if (!detail::FindMember(o, property.c_str(), it)) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3699 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3700 | if (err) { |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 3701 | (*err) += "'" + property + "' property is missing"; |
| 3702 | if (!parent_node.empty()) { |
| 3703 | (*err) += " in " + parent_node; |
| 3704 | } |
| 3705 | (*err) += ".\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3706 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3707 | } |
| 3708 | return false; |
| 3709 | } |
| 3710 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3711 | auto &value = detail::GetValue(it); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3712 | |
| 3713 | bool isBoolean; |
jrkoonce | 0d2b6ef | 2019-09-04 13:46:45 -0500 | [diff] [blame] | 3714 | bool boolValue = false; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3715 | #ifdef TINYGLTF_USE_RAPIDJSON |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3716 | isBoolean = value.IsBool(); |
| 3717 | if (isBoolean) { |
| 3718 | boolValue = value.GetBool(); |
| 3719 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3720 | #else |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3721 | isBoolean = value.is_boolean(); |
| 3722 | if (isBoolean) { |
| 3723 | boolValue = value.get<bool>(); |
| 3724 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3725 | #endif |
| 3726 | if (!isBoolean) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3727 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3728 | if (err) { |
| 3729 | (*err) += "'" + property + "' property is not a bool type.\n"; |
| 3730 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3731 | } |
| 3732 | return false; |
| 3733 | } |
| 3734 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3735 | if (ret) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3736 | (*ret) = boolValue; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3737 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3738 | |
| 3739 | return true; |
| 3740 | } |
| 3741 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3742 | static bool ParseIntegerProperty(int *ret, std::string *err, |
| 3743 | const detail::json &o, |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3744 | const std::string &property, |
| 3745 | const bool required, |
| 3746 | const std::string &parent_node = "") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3747 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3748 | if (!detail::FindMember(o, property.c_str(), it)) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3749 | if (required) { |
| 3750 | if (err) { |
| 3751 | (*err) += "'" + property + "' property is missing"; |
| 3752 | if (!parent_node.empty()) { |
| 3753 | (*err) += " in " + parent_node; |
| 3754 | } |
| 3755 | (*err) += ".\n"; |
| 3756 | } |
| 3757 | } |
| 3758 | return false; |
| 3759 | } |
| 3760 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3761 | int intValue; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3762 | bool isInt = detail::GetInt(detail::GetValue(it), intValue); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3763 | if (!isInt) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3764 | if (required) { |
| 3765 | if (err) { |
| 3766 | (*err) += "'" + property + "' property is not an integer type.\n"; |
| 3767 | } |
| 3768 | } |
| 3769 | return false; |
| 3770 | } |
| 3771 | |
| 3772 | if (ret) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3773 | (*ret) = intValue; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3774 | } |
| 3775 | |
| 3776 | return true; |
| 3777 | } |
| 3778 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3779 | static bool ParseUnsignedProperty(size_t *ret, std::string *err, |
| 3780 | const detail::json &o, |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3781 | const std::string &property, |
| 3782 | const bool required, |
| 3783 | const std::string &parent_node = "") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3784 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3785 | if (!detail::FindMember(o, property.c_str(), it)) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3786 | if (required) { |
| 3787 | if (err) { |
| 3788 | (*err) += "'" + property + "' property is missing"; |
| 3789 | if (!parent_node.empty()) { |
| 3790 | (*err) += " in " + parent_node; |
| 3791 | } |
| 3792 | (*err) += ".\n"; |
| 3793 | } |
| 3794 | } |
| 3795 | return false; |
| 3796 | } |
| 3797 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3798 | auto &value = detail::GetValue(it); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3799 | |
jrkoonce | 0d2b6ef | 2019-09-04 13:46:45 -0500 | [diff] [blame] | 3800 | size_t uValue = 0; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3801 | bool isUValue; |
| 3802 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3803 | isUValue = false; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3804 | if (value.IsUint()) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3805 | uValue = value.GetUint(); |
| 3806 | isUValue = true; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3807 | } else if (value.IsUint64()) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3808 | uValue = value.GetUint64(); |
| 3809 | isUValue = true; |
| 3810 | } |
| 3811 | #else |
| 3812 | isUValue = value.is_number_unsigned(); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3813 | if (isUValue) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3814 | uValue = value.get<size_t>(); |
| 3815 | } |
| 3816 | #endif |
| 3817 | if (!isUValue) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3818 | if (required) { |
| 3819 | if (err) { |
| 3820 | (*err) += "'" + property + "' property is not a positive integer.\n"; |
| 3821 | } |
| 3822 | } |
| 3823 | return false; |
| 3824 | } |
| 3825 | |
| 3826 | if (ret) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3827 | (*ret) = uValue; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3828 | } |
| 3829 | |
| 3830 | return true; |
| 3831 | } |
| 3832 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3833 | static bool ParseNumberProperty(double *ret, std::string *err, |
| 3834 | const detail::json &o, |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 3835 | const std::string &property, |
| 3836 | const bool required, |
| 3837 | const std::string &parent_node = "") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3838 | detail::json_const_iterator it; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3839 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3840 | if (!detail::FindMember(o, property.c_str(), it)) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3841 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3842 | if (err) { |
Syoyo Fujita | 90e1ed2 | 2017-05-30 00:47:37 +0900 | [diff] [blame] | 3843 | (*err) += "'" + property + "' property is missing"; |
| 3844 | if (!parent_node.empty()) { |
| 3845 | (*err) += " in " + parent_node; |
| 3846 | } |
| 3847 | (*err) += ".\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3848 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3849 | } |
| 3850 | return false; |
| 3851 | } |
| 3852 | |
Syoyo Fujita | 1100f0f | 2019-10-30 15:16:34 +0900 | [diff] [blame] | 3853 | double numberValue; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3854 | bool isNumber = detail::GetNumber(detail::GetValue(it), numberValue); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3855 | |
Syoyo Fujita | 1100f0f | 2019-10-30 15:16:34 +0900 | [diff] [blame] | 3856 | if (!isNumber) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3857 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3858 | if (err) { |
| 3859 | (*err) += "'" + property + "' property is not a number type.\n"; |
| 3860 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3861 | } |
| 3862 | return false; |
| 3863 | } |
| 3864 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3865 | if (ret) { |
Syoyo Fujita | 1100f0f | 2019-10-30 15:16:34 +0900 | [diff] [blame] | 3866 | (*ret) = numberValue; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3867 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3868 | |
| 3869 | return true; |
| 3870 | } |
| 3871 | |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 3872 | static bool ParseNumberArrayProperty(std::vector<double> *ret, std::string *err, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 3873 | const detail::json &o, |
| 3874 | const std::string &property, bool required, |
Syoyo Fujita | 90e1ed2 | 2017-05-30 00:47:37 +0900 | [diff] [blame] | 3875 | const std::string &parent_node = "") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3876 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3877 | if (!detail::FindMember(o, property.c_str(), it)) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3878 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3879 | if (err) { |
Syoyo Fujita | 90e1ed2 | 2017-05-30 00:47:37 +0900 | [diff] [blame] | 3880 | (*err) += "'" + property + "' property is missing"; |
| 3881 | if (!parent_node.empty()) { |
| 3882 | (*err) += " in " + parent_node; |
| 3883 | } |
| 3884 | (*err) += ".\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3885 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3886 | } |
| 3887 | return false; |
| 3888 | } |
| 3889 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3890 | if (!detail::IsArray(detail::GetValue(it))) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3891 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3892 | if (err) { |
Syoyo Fujita | 90e1ed2 | 2017-05-30 00:47:37 +0900 | [diff] [blame] | 3893 | (*err) += "'" + property + "' property is not an array"; |
| 3894 | if (!parent_node.empty()) { |
| 3895 | (*err) += " in " + parent_node; |
| 3896 | } |
| 3897 | (*err) += ".\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3898 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3899 | } |
| 3900 | return false; |
| 3901 | } |
| 3902 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3903 | ret->clear(); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3904 | auto end = detail::ArrayEnd(detail::GetValue(it)); |
| 3905 | for (auto i = detail::ArrayBegin(detail::GetValue(it)); i != end; ++i) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3906 | double numberValue; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3907 | const bool isNumber = detail::GetNumber(*i, numberValue); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3908 | if (!isNumber) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3909 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3910 | if (err) { |
| 3911 | (*err) += "'" + property + "' property is not a number.\n"; |
Syoyo Fujita | 90e1ed2 | 2017-05-30 00:47:37 +0900 | [diff] [blame] | 3912 | if (!parent_node.empty()) { |
| 3913 | (*err) += " in " + parent_node; |
| 3914 | } |
| 3915 | (*err) += ".\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3916 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3917 | } |
| 3918 | return false; |
| 3919 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3920 | ret->push_back(numberValue); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3921 | } |
| 3922 | |
| 3923 | return true; |
| 3924 | } |
| 3925 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3926 | static bool ParseIntegerArrayProperty(std::vector<int> *ret, std::string *err, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3927 | const detail::json &o, |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3928 | const std::string &property, |
| 3929 | bool required, |
| 3930 | const std::string &parent_node = "") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3931 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3932 | if (!detail::FindMember(o, property.c_str(), it)) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3933 | if (required) { |
| 3934 | if (err) { |
| 3935 | (*err) += "'" + property + "' property is missing"; |
| 3936 | if (!parent_node.empty()) { |
| 3937 | (*err) += " in " + parent_node; |
| 3938 | } |
| 3939 | (*err) += ".\n"; |
| 3940 | } |
| 3941 | } |
| 3942 | return false; |
| 3943 | } |
| 3944 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3945 | if (!detail::IsArray(detail::GetValue(it))) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3946 | if (required) { |
| 3947 | if (err) { |
| 3948 | (*err) += "'" + property + "' property is not an array"; |
| 3949 | if (!parent_node.empty()) { |
| 3950 | (*err) += " in " + parent_node; |
| 3951 | } |
| 3952 | (*err) += ".\n"; |
| 3953 | } |
| 3954 | } |
| 3955 | return false; |
| 3956 | } |
| 3957 | |
| 3958 | ret->clear(); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3959 | auto end = detail::ArrayEnd(detail::GetValue(it)); |
| 3960 | for (auto i = detail::ArrayBegin(detail::GetValue(it)); i != end; ++i) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3961 | int numberValue; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3962 | bool isNumber = detail::GetInt(*i, numberValue); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3963 | if (!isNumber) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3964 | if (required) { |
| 3965 | if (err) { |
| 3966 | (*err) += "'" + property + "' property is not an integer type.\n"; |
| 3967 | if (!parent_node.empty()) { |
| 3968 | (*err) += " in " + parent_node; |
| 3969 | } |
| 3970 | (*err) += ".\n"; |
| 3971 | } |
| 3972 | } |
| 3973 | return false; |
| 3974 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3975 | ret->push_back(numberValue); |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3976 | } |
| 3977 | |
| 3978 | return true; |
| 3979 | } |
| 3980 | |
Syoyo Fujita | 0614eb8 | 2016-10-14 18:50:14 +0900 | [diff] [blame] | 3981 | static bool ParseStringProperty( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3982 | std::string *ret, std::string *err, const detail::json &o, |
Syoyo Fujita | 0614eb8 | 2016-10-14 18:50:14 +0900 | [diff] [blame] | 3983 | const std::string &property, bool required, |
| 3984 | const std::string &parent_node = std::string()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3985 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3986 | if (!detail::FindMember(o, property.c_str(), it)) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3987 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3988 | if (err) { |
Syoyo Fujita | 0614eb8 | 2016-10-14 18:50:14 +0900 | [diff] [blame] | 3989 | (*err) += "'" + property + "' property is missing"; |
| 3990 | if (parent_node.empty()) { |
| 3991 | (*err) += ".\n"; |
| 3992 | } else { |
| 3993 | (*err) += " in `" + parent_node + "'.\n"; |
| 3994 | } |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3995 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3996 | } |
| 3997 | return false; |
| 3998 | } |
| 3999 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4000 | std::string strValue; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4001 | if (!detail::GetString(detail::GetValue(it), strValue)) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4002 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4003 | if (err) { |
| 4004 | (*err) += "'" + property + "' property is not a string type.\n"; |
| 4005 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4006 | } |
| 4007 | return false; |
| 4008 | } |
| 4009 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4010 | if (ret) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4011 | (*ret) = std::move(strValue); |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4012 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4013 | |
| 4014 | return true; |
| 4015 | } |
| 4016 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4017 | static bool ParseStringIntegerProperty(std::map<std::string, int> *ret, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4018 | std::string *err, const detail::json &o, |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4019 | const std::string &property, |
| 4020 | bool required, |
| 4021 | const std::string &parent = "") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4022 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4023 | if (!detail::FindMember(o, property.c_str(), it)) { |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4024 | if (required) { |
| 4025 | if (err) { |
Syoyo Fujita | 57c1018 | 2017-10-19 18:48:26 +0900 | [diff] [blame] | 4026 | if (!parent.empty()) { |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 4027 | (*err) += |
| 4028 | "'" + property + "' property is missing in " + parent + ".\n"; |
Syoyo Fujita | 57c1018 | 2017-10-19 18:48:26 +0900 | [diff] [blame] | 4029 | } else { |
| 4030 | (*err) += "'" + property + "' property is missing.\n"; |
| 4031 | } |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4032 | } |
| 4033 | } |
| 4034 | return false; |
| 4035 | } |
| 4036 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4037 | const detail::json &dict = detail::GetValue(it); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4038 | |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4039 | // Make sure we are dealing with an object / dictionary. |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4040 | if (!detail::IsObject(dict)) { |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4041 | if (required) { |
| 4042 | if (err) { |
| 4043 | (*err) += "'" + property + "' property is not an object.\n"; |
| 4044 | } |
| 4045 | } |
| 4046 | return false; |
| 4047 | } |
| 4048 | |
| 4049 | ret->clear(); |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4050 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4051 | detail::json_const_iterator dictIt(detail::ObjectBegin(dict)); |
| 4052 | detail::json_const_iterator dictItEnd(detail::ObjectEnd(dict)); |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4053 | |
Syoyo Fujita | 3cdfb3b | 2016-06-15 20:58:42 +0900 | [diff] [blame] | 4054 | for (; dictIt != dictItEnd; ++dictIt) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4055 | int intVal; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4056 | if (!detail::GetInt(detail::GetValue(dictIt), intVal)) { |
Syoyo Fujita | 3cdfb3b | 2016-06-15 20:58:42 +0900 | [diff] [blame] | 4057 | if (required) { |
| 4058 | if (err) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4059 | (*err) += "'" + property + "' value is not an integer type.\n"; |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4060 | } |
| 4061 | } |
| 4062 | return false; |
| 4063 | } |
| 4064 | |
| 4065 | // Insert into the list. |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4066 | (*ret)[detail::GetKey(dictIt)] = intVal; |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4067 | } |
| 4068 | return true; |
| 4069 | } |
| 4070 | |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 4071 | static bool ParseJSONProperty(std::map<std::string, double> *ret, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4072 | std::string *err, const detail::json &o, |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 4073 | const std::string &property, bool required) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4074 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4075 | if (!detail::FindMember(o, property.c_str(), it)) { |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 4076 | if (required) { |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 4077 | if (err) { |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 4078 | (*err) += "'" + property + "' property is missing. \n'"; |
| 4079 | } |
| 4080 | } |
| 4081 | return false; |
| 4082 | } |
| 4083 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4084 | const detail::json &obj = detail::GetValue(it); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4085 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4086 | if (!detail::IsObject(obj)) { |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 4087 | if (required) { |
| 4088 | if (err) { |
| 4089 | (*err) += "'" + property + "' property is not a JSON object.\n"; |
| 4090 | } |
| 4091 | } |
| 4092 | return false; |
| 4093 | } |
| 4094 | |
| 4095 | ret->clear(); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 4096 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4097 | detail::json_const_iterator it2(detail::ObjectBegin(obj)); |
| 4098 | detail::json_const_iterator itEnd(detail::ObjectEnd(obj)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4099 | for (; it2 != itEnd; ++it2) { |
| 4100 | double numVal; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4101 | if (detail::GetNumber(detail::GetValue(it2), numVal)) |
| 4102 | ret->emplace(std::string(detail::GetKey(it2)), numVal); |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 4103 | } |
| 4104 | |
| 4105 | return true; |
| 4106 | } |
| 4107 | |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 4108 | static bool ParseParameterProperty(Parameter *param, std::string *err, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4109 | const detail::json &o, |
| 4110 | const std::string &prop, bool required) { |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 4111 | // A parameter value can either be a string or an array of either a boolean or |
| 4112 | // a number. Booleans of any kind aren't supported here. Granted, it |
| 4113 | // complicates the Parameter structure and breaks it semantically in the sense |
| 4114 | // that the client probably works off the assumption that if the string is |
| 4115 | // empty the vector is used, etc. Would a tagged union work? |
| 4116 | if (ParseStringProperty(¶m->string_value, err, o, prop, false)) { |
| 4117 | // Found string property. |
| 4118 | return true; |
| 4119 | } else if (ParseNumberArrayProperty(¶m->number_array, err, o, prop, |
| 4120 | false)) { |
| 4121 | // Found a number array. |
| 4122 | return true; |
Ben Buzbee | f6af224 | 2018-04-25 15:13:05 -0700 | [diff] [blame] | 4123 | } else if (ParseNumberProperty(¶m->number_value, err, o, prop, false)) { |
Marcin Kacprzak | b12a54e | 2022-09-02 16:13:11 +0200 | [diff] [blame] | 4124 | param->has_number_value = true; |
| 4125 | return true; |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 4126 | } else if (ParseJSONProperty(¶m->json_double_value, err, o, prop, |
| 4127 | false)) { |
| 4128 | return true; |
| 4129 | } else if (ParseBooleanProperty(¶m->bool_value, err, o, prop, false)) { |
| 4130 | return true; |
| 4131 | } else { |
| 4132 | if (required) { |
| 4133 | if (err) { |
| 4134 | (*err) += "parameter must be a string or number / number array.\n"; |
| 4135 | } |
| 4136 | } |
| 4137 | return false; |
| 4138 | } |
| 4139 | } |
| 4140 | |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 4141 | static bool ParseExtensionsProperty(ExtensionMap *ret, std::string *err, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4142 | const detail::json &o) { |
Syoyo Fujita | 48f6db0 | 2018-04-15 18:40:55 +0900 | [diff] [blame] | 4143 | (void)err; |
| 4144 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4145 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4146 | if (!detail::FindMember(o, "extensions", it)) { |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 4147 | return false; |
| 4148 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4149 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4150 | auto &obj = detail::GetValue(it); |
| 4151 | if (!detail::IsObject(obj)) { |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 4152 | return false; |
| 4153 | } |
| 4154 | ExtensionMap extensions; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4155 | detail::json_const_iterator extIt = |
| 4156 | detail::ObjectBegin(obj); // it.value().begin(); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4157 | detail::json_const_iterator extEnd = detail::ObjectEnd(obj); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4158 | for (; extIt != extEnd; ++extIt) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4159 | auto &itObj = detail::GetValue(extIt); |
| 4160 | if (!detail::IsObject(itObj)) continue; |
| 4161 | std::string key(detail::GetKey(extIt)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4162 | if (!ParseJsonAsValue(&extensions[key], itObj)) { |
jrkoonce | 06c30c4 | 2019-09-03 15:56:48 -0500 | [diff] [blame] | 4163 | if (!key.empty()) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4164 | // create empty object so that an extension object is still of type |
| 4165 | // object |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4166 | extensions[key] = Value{Value::Object{}}; |
Selmar Kok | ee3d066 | 2018-10-08 16:20:43 +0200 | [diff] [blame] | 4167 | } |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4168 | } |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 4169 | } |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 4170 | if (ret) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4171 | (*ret) = std::move(extensions); |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 4172 | } |
| 4173 | return true; |
| 4174 | } |
| 4175 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 4176 | template <typename GltfType> |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4177 | static bool ParseExtrasAndExtensions(GltfType *target, std::string *err, |
| 4178 | const detail::json &o, |
| 4179 | bool store_json_strings) { |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 4180 | ParseExtensionsProperty(&target->extensions, err, o); |
| 4181 | ParseExtrasProperty(&target->extras, o); |
| 4182 | |
| 4183 | if (store_json_strings) { |
| 4184 | { |
| 4185 | detail::json_const_iterator it; |
| 4186 | if (detail::FindMember(o, "extensions", it)) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4187 | target->extensions_json_string = |
| 4188 | detail::JsonToString(detail::GetValue(it)); |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 4189 | } |
| 4190 | } |
| 4191 | { |
| 4192 | detail::json_const_iterator it; |
| 4193 | if (detail::FindMember(o, "extras", it)) { |
| 4194 | target->extras_json_string = detail::JsonToString(detail::GetValue(it)); |
| 4195 | } |
| 4196 | } |
| 4197 | } |
| 4198 | return true; |
| 4199 | } |
| 4200 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4201 | static bool ParseAsset(Asset *asset, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4202 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 57c1018 | 2017-10-19 18:48:26 +0900 | [diff] [blame] | 4203 | ParseStringProperty(&asset->version, err, o, "version", true, "Asset"); |
| 4204 | ParseStringProperty(&asset->generator, err, o, "generator", false, "Asset"); |
| 4205 | ParseStringProperty(&asset->minVersion, err, o, "minVersion", false, "Asset"); |
Miguel Sousa | 22bfc84 | 2020-02-20 14:16:58 +0100 | [diff] [blame] | 4206 | ParseStringProperty(&asset->copyright, err, o, "copyright", false, "Asset"); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4207 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4208 | ParseExtrasAndExtensions(asset, err, o, |
| 4209 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4210 | return true; |
| 4211 | } |
| 4212 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4213 | static bool ParseImage(Image *image, const int image_idx, std::string *err, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4214 | std::string *warn, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4215 | bool store_original_json_for_extras_and_extensions, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4216 | const std::string &basedir, const size_t max_file_size, |
| 4217 | FsCallbacks *fs, const URICallbacks *uri_cb, |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 4218 | LoadImageDataFunction *LoadImageData = nullptr, |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 4219 | void *load_image_user_data = nullptr) { |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 4220 | // 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] | 4221 | |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4222 | // schema says oneOf [`bufferView`, `uri`] |
| 4223 | // TODO(syoyo): Check the type of each parameters. |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4224 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4225 | bool hasBufferView = detail::FindMember(o, "bufferView", it); |
| 4226 | bool hasURI = detail::FindMember(o, "uri", it); |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4227 | |
Syoyo Fujita | af3ebb2 | 2019-01-06 18:55:57 +0900 | [diff] [blame] | 4228 | ParseStringProperty(&image->name, err, o, "name", false); |
| 4229 | |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4230 | if (hasBufferView && hasURI) { |
| 4231 | // Should not both defined. |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 4232 | if (err) { |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 4233 | (*err) += |
| 4234 | "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] | 4235 | "defined for image[" + |
| 4236 | std::to_string(image_idx) + "] name = \"" + image->name + "\"\n"; |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4237 | } |
| 4238 | return false; |
| 4239 | } |
| 4240 | |
| 4241 | if (!hasBufferView && !hasURI) { |
| 4242 | if (err) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4243 | (*err) += "Neither required `bufferView` nor `uri` defined for image[" + |
| 4244 | std::to_string(image_idx) + "] name = \"" + image->name + |
| 4245 | "\"\n"; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 4246 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4247 | return false; |
| 4248 | } |
| 4249 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4250 | ParseExtrasAndExtensions(image, err, o, |
| 4251 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4252 | |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4253 | if (hasBufferView) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4254 | int bufferView = -1; |
| 4255 | if (!ParseIntegerProperty(&bufferView, err, o, "bufferView", true)) { |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4256 | if (err) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4257 | (*err) += "Failed to parse `bufferView` for image[" + |
| 4258 | std::to_string(image_idx) + "] name = \"" + image->name + |
| 4259 | "\"\n"; |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4260 | } |
| 4261 | return false; |
| 4262 | } |
| 4263 | |
| 4264 | std::string mime_type; |
| 4265 | ParseStringProperty(&mime_type, err, o, "mimeType", false); |
| 4266 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4267 | int width = 0; |
| 4268 | ParseIntegerProperty(&width, err, o, "width", false); |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4269 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4270 | int height = 0; |
| 4271 | ParseIntegerProperty(&height, err, o, "height", false); |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4272 | |
| 4273 | // Just only save some information here. Loading actual image data from |
| 4274 | // bufferView is done after this `ParseImage` function. |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4275 | image->bufferView = bufferView; |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4276 | image->mimeType = mime_type; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4277 | image->width = width; |
| 4278 | image->height = height; |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4279 | |
| 4280 | return true; |
| 4281 | } |
| 4282 | |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4283 | // Parse URI & Load image data. |
| 4284 | |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4285 | std::string uri; |
| 4286 | std::string tmp_err; |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4287 | if (!ParseStringProperty(&uri, &tmp_err, o, "uri", true)) { |
| 4288 | if (err) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4289 | (*err) += "Failed to parse `uri` for image[" + std::to_string(image_idx) + |
| 4290 | "] name = \"" + image->name + "\".\n"; |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4291 | } |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4292 | return false; |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4293 | } |
| 4294 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4295 | std::vector<unsigned char> img; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4296 | |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4297 | if (IsDataURI(uri)) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 4298 | if (!DecodeDataURI(&img, image->mimeType, uri, 0, false)) { |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4299 | if (err) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4300 | (*err) += "Failed to decode 'uri' for image[" + |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 4301 | std::to_string(image_idx) + "] name = \"" + image->name + |
| 4302 | "\"\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4303 | } |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4304 | return false; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4305 | } |
| 4306 | } else { |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4307 | // Assume external file |
| 4308 | // Keep texture path (for textures that cannot be decoded) |
| 4309 | image->uri = uri; |
Selmar | 67af3c9 | 2018-03-16 11:48:19 +0100 | [diff] [blame] | 4310 | #ifdef TINYGLTF_NO_EXTERNAL_IMAGE |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4311 | return true; |
AlvaroBarua | 4317223 | 2022-09-11 00:41:43 +0100 | [diff] [blame] | 4312 | #else |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 4313 | std::string decoded_uri; |
| 4314 | if (!uri_cb->decode(uri, &decoded_uri, uri_cb->user_data)) { |
| 4315 | if (warn) { |
| 4316 | (*warn) += "Failed to decode 'uri' for image[" + |
Syoyo Fujita | 877d856 | 2023-04-23 21:47:31 +0900 | [diff] [blame] | 4317 | std::to_string(image_idx) + "] name = \"" + image->name + |
| 4318 | "\"\n"; |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 4319 | } |
| 4320 | |
| 4321 | // Image loading failure is not critical to overall gltf loading. |
| 4322 | return true; |
| 4323 | } |
| 4324 | |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 4325 | if (!LoadExternalFile(&img, err, warn, decoded_uri, basedir, |
| 4326 | /* required */ false, /* required bytes */ 0, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4327 | /* checksize */ false, |
| 4328 | /* max file size */ max_file_size, fs)) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 4329 | if (warn) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4330 | (*warn) += "Failed to load external 'uri' for image[" + |
Syoyo Fujita | 877d856 | 2023-04-23 21:47:31 +0900 | [diff] [blame] | 4331 | std::to_string(image_idx) + "] name = \"" + decoded_uri + |
| 4332 | "\"\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4333 | } |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4334 | // If the image cannot be loaded, keep uri as image->uri. |
| 4335 | return true; |
| 4336 | } |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4337 | |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4338 | if (img.empty()) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 4339 | if (warn) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4340 | (*warn) += "Image data is empty for image[" + |
Syoyo Fujita | 877d856 | 2023-04-23 21:47:31 +0900 | [diff] [blame] | 4341 | std::to_string(image_idx) + "] name = \"" + image->name + |
| 4342 | "\" \n"; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4343 | } |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4344 | return false; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4345 | } |
AlvaroBarua | 4317223 | 2022-09-11 00:41:43 +0100 | [diff] [blame] | 4346 | #endif |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4347 | } |
| 4348 | |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 4349 | if (*LoadImageData == nullptr) { |
| 4350 | if (err) { |
| 4351 | (*err) += "No LoadImageData callback specified.\n"; |
| 4352 | } |
| 4353 | return false; |
| 4354 | } |
Syoyo Fujita | af3ebb2 | 2019-01-06 18:55:57 +0900 | [diff] [blame] | 4355 | return (*LoadImageData)(image, image_idx, err, warn, 0, 0, &img.at(0), |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 4356 | static_cast<int>(img.size()), load_image_user_data); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4357 | } |
| 4358 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4359 | static bool ParseTexture(Texture *texture, std::string *err, |
| 4360 | const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4361 | bool store_original_json_for_extras_and_extensions, |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4362 | const std::string &basedir) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4363 | (void)basedir; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4364 | int sampler = -1; |
| 4365 | int source = -1; |
| 4366 | ParseIntegerProperty(&sampler, err, o, "sampler", false); |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4367 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4368 | ParseIntegerProperty(&source, err, o, "source", false); |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 4369 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4370 | texture->sampler = sampler; |
| 4371 | texture->source = source; |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 4372 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4373 | ParseExtrasAndExtensions(texture, err, o, |
| 4374 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4375 | |
Vladimír Vondruš | 239be2c | 2018-07-24 23:23:56 +0200 | [diff] [blame] | 4376 | ParseStringProperty(&texture->name, err, o, "name", false); |
| 4377 | |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 4378 | return true; |
| 4379 | } |
| 4380 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4381 | static bool ParseTextureInfo( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4382 | TextureInfo *texinfo, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4383 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 4384 | if (texinfo == nullptr) { |
| 4385 | return false; |
| 4386 | } |
| 4387 | |
| 4388 | if (!ParseIntegerProperty(&texinfo->index, err, o, "index", |
| 4389 | /* required */ true, "TextureInfo")) { |
| 4390 | return false; |
| 4391 | } |
| 4392 | |
| 4393 | ParseIntegerProperty(&texinfo->texCoord, err, o, "texCoord", false); |
| 4394 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4395 | ParseExtrasAndExtensions(texinfo, err, o, |
| 4396 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4397 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 4398 | return true; |
| 4399 | } |
| 4400 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4401 | static bool ParseNormalTextureInfo( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4402 | NormalTextureInfo *texinfo, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4403 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 4404 | if (texinfo == nullptr) { |
| 4405 | return false; |
| 4406 | } |
| 4407 | |
| 4408 | if (!ParseIntegerProperty(&texinfo->index, err, o, "index", |
| 4409 | /* required */ true, "NormalTextureInfo")) { |
| 4410 | return false; |
| 4411 | } |
| 4412 | |
| 4413 | ParseIntegerProperty(&texinfo->texCoord, err, o, "texCoord", false); |
| 4414 | ParseNumberProperty(&texinfo->scale, err, o, "scale", false); |
| 4415 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4416 | ParseExtrasAndExtensions(texinfo, err, o, |
| 4417 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4418 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 4419 | return true; |
| 4420 | } |
| 4421 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4422 | static bool ParseOcclusionTextureInfo( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4423 | OcclusionTextureInfo *texinfo, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4424 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 4425 | if (texinfo == nullptr) { |
| 4426 | return false; |
| 4427 | } |
| 4428 | |
| 4429 | if (!ParseIntegerProperty(&texinfo->index, err, o, "index", |
| 4430 | /* required */ true, "NormalTextureInfo")) { |
| 4431 | return false; |
| 4432 | } |
| 4433 | |
| 4434 | ParseIntegerProperty(&texinfo->texCoord, err, o, "texCoord", false); |
| 4435 | ParseNumberProperty(&texinfo->strength, err, o, "strength", false); |
| 4436 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4437 | ParseExtrasAndExtensions(texinfo, err, o, |
| 4438 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4439 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 4440 | return true; |
| 4441 | } |
| 4442 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4443 | static bool ParseBuffer(Buffer *buffer, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4444 | bool store_original_json_for_extras_and_extensions, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 4445 | FsCallbacks *fs, const URICallbacks *uri_cb, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4446 | const std::string &basedir, |
| 4447 | const size_t max_buffer_size, bool is_binary = false, |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 4448 | const unsigned char *bin_data = nullptr, |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4449 | size_t bin_size = 0) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4450 | size_t byteLength; |
| 4451 | if (!ParseUnsignedProperty(&byteLength, err, o, "byteLength", true, |
| 4452 | "Buffer")) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4453 | return false; |
| 4454 | } |
| 4455 | |
Aurélien Chatelain | 0aab157 | 2017-05-30 15:03:48 +0000 | [diff] [blame] | 4456 | // In glTF 2.0, uri is not mandatory anymore |
Syoyo Fujita | 20244e1 | 2018-03-15 11:01:05 -0500 | [diff] [blame] | 4457 | buffer->uri.clear(); |
| 4458 | ParseStringProperty(&buffer->uri, err, o, "uri", false, "Buffer"); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4459 | |
Aurélien Chatelain | 0aab157 | 2017-05-30 15:03:48 +0000 | [diff] [blame] | 4460 | // 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] | 4461 | if (!is_binary && buffer->uri.empty()) { |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 4462 | if (err) { |
| 4463 | (*err) += "'uri' is missing from non binary glTF file buffer.\n"; |
| 4464 | } |
Aurélien Chatelain | 0aab157 | 2017-05-30 15:03:48 +0000 | [diff] [blame] | 4465 | } |
| 4466 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4467 | detail::json_const_iterator type; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4468 | if (detail::FindMember(o, "type", type)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4469 | std::string typeStr; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4470 | if (detail::GetString(detail::GetValue(type), typeStr)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4471 | if (typeStr.compare("arraybuffer") == 0) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4472 | // buffer.type = "arraybuffer"; |
| 4473 | } |
| 4474 | } |
| 4475 | } |
| 4476 | |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 4477 | if (is_binary) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 4478 | // Still binary glTF accepts external dataURI. |
Syoyo Fujita | 20244e1 | 2018-03-15 11:01:05 -0500 | [diff] [blame] | 4479 | if (!buffer->uri.empty()) { |
Syoyo Fujita | 39abfb5 | 2018-07-11 02:46:52 +0900 | [diff] [blame] | 4480 | // First try embedded data URI. |
| 4481 | if (IsDataURI(buffer->uri)) { |
| 4482 | std::string mime_type; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4483 | if (!DecodeDataURI(&buffer->data, mime_type, buffer->uri, byteLength, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 4484 | true)) { |
| 4485 | if (err) { |
| 4486 | (*err) += |
| 4487 | "Failed to decode 'uri' : " + buffer->uri + " in Buffer\n"; |
| 4488 | } |
| 4489 | return false; |
Syoyo Fujita | 39abfb5 | 2018-07-11 02:46:52 +0900 | [diff] [blame] | 4490 | } |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 4491 | } else { |
Syoyo Fujita | 39abfb5 | 2018-07-11 02:46:52 +0900 | [diff] [blame] | 4492 | // External .bin file. |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 4493 | std::string decoded_uri; |
| 4494 | if (!uri_cb->decode(buffer->uri, &decoded_uri, uri_cb->user_data)) { |
| 4495 | return false; |
| 4496 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 4497 | if (!LoadExternalFile(&buffer->data, err, /* warn */ nullptr, |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 4498 | decoded_uri, basedir, /* required */ true, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4499 | byteLength, /* checkSize */ true, |
| 4500 | /* max_file_size */ max_buffer_size, fs)) { |
Vladimír Vondruš | fd84ceb | 2018-08-16 21:07:56 +0200 | [diff] [blame] | 4501 | return false; |
| 4502 | } |
Syoyo Fujita | 39abfb5 | 2018-07-11 02:46:52 +0900 | [diff] [blame] | 4503 | } |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 4504 | } else { |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4505 | // load data from (embedded) binary data |
| 4506 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 4507 | if ((bin_size == 0) || (bin_data == nullptr)) { |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4508 | if (err) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4509 | (*err) += |
| 4510 | "Invalid binary data in `Buffer', or GLB with empty BIN chunk.\n"; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4511 | } |
| 4512 | return false; |
| 4513 | } |
| 4514 | |
| 4515 | if (byteLength > bin_size) { |
| 4516 | if (err) { |
| 4517 | std::stringstream ss; |
| 4518 | ss << "Invalid `byteLength'. Must be equal or less than binary size: " |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 4519 | "`byteLength' = " |
| 4520 | << byteLength << ", binary size = " << bin_size << std::endl; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4521 | (*err) += ss.str(); |
| 4522 | } |
| 4523 | return false; |
| 4524 | } |
| 4525 | |
Aurélien Chatelain | 0aab157 | 2017-05-30 15:03:48 +0000 | [diff] [blame] | 4526 | // Read buffer data |
| 4527 | buffer->data.resize(static_cast<size_t>(byteLength)); |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 4528 | memcpy(&(buffer->data.at(0)), bin_data, static_cast<size_t>(byteLength)); |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4529 | } |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 4530 | |
| 4531 | } else { |
Syoyo Fujita | 20244e1 | 2018-03-15 11:01:05 -0500 | [diff] [blame] | 4532 | if (IsDataURI(buffer->uri)) { |
johan bowald | ef151a4 | 2018-04-01 13:44:48 +0200 | [diff] [blame] | 4533 | std::string mime_type; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4534 | if (!DecodeDataURI(&buffer->data, mime_type, buffer->uri, byteLength, |
| 4535 | true)) { |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 4536 | if (err) { |
Syoyo Fujita | 20244e1 | 2018-03-15 11:01:05 -0500 | [diff] [blame] | 4537 | (*err) += "Failed to decode 'uri' : " + buffer->uri + " in Buffer\n"; |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 4538 | } |
| 4539 | return false; |
| 4540 | } |
| 4541 | } else { |
| 4542 | // Assume external .bin file. |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 4543 | std::string decoded_uri; |
| 4544 | if (!uri_cb->decode(buffer->uri, &decoded_uri, uri_cb->user_data)) { |
| 4545 | return false; |
| 4546 | } |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 4547 | if (!LoadExternalFile(&buffer->data, err, /* warn */ nullptr, decoded_uri, |
| 4548 | basedir, /* required */ true, byteLength, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4549 | /* checkSize */ true, |
| 4550 | /* max file size */ max_buffer_size, fs)) { |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 4551 | return false; |
| 4552 | } |
| 4553 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4554 | } |
| 4555 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4556 | ParseStringProperty(&buffer->name, err, o, "name", false); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4557 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4558 | ParseExtrasAndExtensions(buffer, err, o, |
| 4559 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4560 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4561 | return true; |
| 4562 | } |
| 4563 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4564 | static bool ParseBufferView( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4565 | BufferView *bufferView, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4566 | bool store_original_json_for_extras_and_extensions) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4567 | int buffer = -1; |
| 4568 | if (!ParseIntegerProperty(&buffer, err, o, "buffer", true, "BufferView")) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4569 | return false; |
| 4570 | } |
| 4571 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4572 | size_t byteOffset = 0; |
| 4573 | ParseUnsignedProperty(&byteOffset, err, o, "byteOffset", false); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4574 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4575 | size_t byteLength = 1; |
| 4576 | if (!ParseUnsignedProperty(&byteLength, err, o, "byteLength", true, |
| 4577 | "BufferView")) { |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 4578 | return false; |
| 4579 | } |
| 4580 | |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 4581 | size_t byteStride = 0; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4582 | if (!ParseUnsignedProperty(&byteStride, err, o, "byteStride", false)) { |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 4583 | // Spec says: When byteStride of referenced bufferView is not defined, it |
| 4584 | // means that accessor elements are tightly packed, i.e., effective stride |
| 4585 | // equals the size of the element. |
| 4586 | // We cannot determine the actual byteStride until Accessor are parsed, thus |
| 4587 | // set 0(= tightly packed) here(as done in OpenGL's VertexAttribPoiner) |
| 4588 | byteStride = 0; |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 4589 | } |
| 4590 | |
| 4591 | if ((byteStride > 252) || ((byteStride % 4) != 0)) { |
| 4592 | if (err) { |
| 4593 | std::stringstream ss; |
| 4594 | ss << "Invalid `byteStride' value. `byteStride' must be the multiple of " |
| 4595 | "4 : " |
| 4596 | << byteStride << std::endl; |
| 4597 | |
| 4598 | (*err) += ss.str(); |
| 4599 | } |
| 4600 | return false; |
| 4601 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4602 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4603 | int target = 0; |
| 4604 | ParseIntegerProperty(&target, err, o, "target", false); |
| 4605 | if ((target == TINYGLTF_TARGET_ARRAY_BUFFER) || |
| 4606 | (target == TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER)) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4607 | // OK |
| 4608 | } else { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4609 | target = 0; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4610 | } |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4611 | bufferView->target = target; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4612 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4613 | ParseStringProperty(&bufferView->name, err, o, "name", false); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4614 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4615 | ParseExtrasAndExtensions(bufferView, err, o, |
| 4616 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4617 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4618 | bufferView->buffer = buffer; |
| 4619 | bufferView->byteOffset = byteOffset; |
| 4620 | bufferView->byteLength = byteLength; |
| 4621 | bufferView->byteStride = byteStride; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4622 | return true; |
| 4623 | } |
| 4624 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4625 | static bool ParseSparseAccessor( |
| 4626 | Accessor::Sparse *sparse, std::string *err, const detail::json &o, |
| 4627 | bool store_original_json_for_extras_and_extensions) { |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 4628 | sparse->isSparse = true; |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4629 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4630 | int count = 0; |
Syoyo Fujita | cbd3885 | 2022-02-26 21:19:15 +0900 | [diff] [blame] | 4631 | if (!ParseIntegerProperty(&count, err, o, "count", true, "SparseAccessor")) { |
| 4632 | return false; |
| 4633 | } |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4634 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4635 | ParseExtrasAndExtensions(sparse, err, o, |
| 4636 | store_original_json_for_extras_and_extensions); |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 4637 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4638 | detail::json_const_iterator indices_iterator; |
| 4639 | detail::json_const_iterator values_iterator; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4640 | if (!detail::FindMember(o, "indices", indices_iterator)) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4641 | (*err) = "the sparse object of this accessor doesn't have indices"; |
| 4642 | return false; |
| 4643 | } |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4644 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4645 | if (!detail::FindMember(o, "values", values_iterator)) { |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 4646 | (*err) = "the sparse object of this accessor doesn't have values"; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4647 | return false; |
| 4648 | } |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4649 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4650 | const detail::json &indices_obj = detail::GetValue(indices_iterator); |
| 4651 | const detail::json &values_obj = detail::GetValue(values_iterator); |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4652 | |
emimvi | 759976e | 2023-09-02 09:39:53 +0200 | [diff] [blame] | 4653 | int indices_buffer_view = 0, component_type = 0; |
| 4654 | size_t indices_byte_offset = 0; |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 4655 | if (!ParseIntegerProperty(&indices_buffer_view, err, indices_obj, |
| 4656 | "bufferView", true, "SparseAccessor")) { |
Syoyo Fujita | cbd3885 | 2022-02-26 21:19:15 +0900 | [diff] [blame] | 4657 | return false; |
| 4658 | } |
emimvi | 759976e | 2023-09-02 09:39:53 +0200 | [diff] [blame] | 4659 | ParseUnsignedProperty(&indices_byte_offset, err, indices_obj, "byteOffset", |
Jack Mousseau | 9471517 | 2022-02-24 14:25:37 -0800 | [diff] [blame] | 4660 | false); |
Syoyo Fujita | cbd3885 | 2022-02-26 21:19:15 +0900 | [diff] [blame] | 4661 | if (!ParseIntegerProperty(&component_type, err, indices_obj, "componentType", |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 4662 | true, "SparseAccessor")) { |
Syoyo Fujita | cbd3885 | 2022-02-26 21:19:15 +0900 | [diff] [blame] | 4663 | return false; |
| 4664 | } |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4665 | |
emimvi | 759976e | 2023-09-02 09:39:53 +0200 | [diff] [blame] | 4666 | int values_buffer_view = 0; |
| 4667 | size_t values_byte_offset = 0; |
Syoyo Fujita | cbd3885 | 2022-02-26 21:19:15 +0900 | [diff] [blame] | 4668 | if (!ParseIntegerProperty(&values_buffer_view, err, values_obj, "bufferView", |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 4669 | true, "SparseAccessor")) { |
Syoyo Fujita | cbd3885 | 2022-02-26 21:19:15 +0900 | [diff] [blame] | 4670 | return false; |
| 4671 | } |
emimvi | 759976e | 2023-09-02 09:39:53 +0200 | [diff] [blame] | 4672 | ParseUnsignedProperty(&values_byte_offset, err, values_obj, "byteOffset", |
Jack Mousseau | 9471517 | 2022-02-24 14:25:37 -0800 | [diff] [blame] | 4673 | false); |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4674 | |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 4675 | sparse->count = count; |
| 4676 | sparse->indices.bufferView = indices_buffer_view; |
| 4677 | sparse->indices.byteOffset = indices_byte_offset; |
| 4678 | sparse->indices.componentType = component_type; |
| 4679 | ParseExtrasAndExtensions(&sparse->indices, err, indices_obj, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4680 | store_original_json_for_extras_and_extensions); |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4681 | |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 4682 | sparse->values.bufferView = values_buffer_view; |
| 4683 | sparse->values.byteOffset = values_byte_offset; |
| 4684 | ParseExtrasAndExtensions(&sparse->values, err, values_obj, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4685 | store_original_json_for_extras_and_extensions); |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4686 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4687 | return true; |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4688 | } |
| 4689 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4690 | static bool ParseAccessor(Accessor *accessor, std::string *err, |
| 4691 | const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4692 | bool store_original_json_for_extras_and_extensions) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4693 | int bufferView = -1; |
| 4694 | ParseIntegerProperty(&bufferView, err, o, "bufferView", false, "Accessor"); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4695 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4696 | size_t byteOffset = 0; |
| 4697 | ParseUnsignedProperty(&byteOffset, err, o, "byteOffset", false, "Accessor"); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4698 | |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 4699 | bool normalized = false; |
| 4700 | ParseBooleanProperty(&normalized, err, o, "normalized", false, "Accessor"); |
| 4701 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4702 | size_t componentType = 0; |
| 4703 | if (!ParseUnsignedProperty(&componentType, err, o, "componentType", true, |
| 4704 | "Accessor")) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4705 | return false; |
| 4706 | } |
| 4707 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4708 | size_t count = 0; |
| 4709 | if (!ParseUnsignedProperty(&count, err, o, "count", true, "Accessor")) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4710 | return false; |
| 4711 | } |
| 4712 | |
| 4713 | std::string type; |
Syoyo Fujita | 90e1ed2 | 2017-05-30 00:47:37 +0900 | [diff] [blame] | 4714 | if (!ParseStringProperty(&type, err, o, "type", true, "Accessor")) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4715 | return false; |
| 4716 | } |
| 4717 | |
| 4718 | if (type.compare("SCALAR") == 0) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4719 | accessor->type = TINYGLTF_TYPE_SCALAR; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4720 | } else if (type.compare("VEC2") == 0) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4721 | accessor->type = TINYGLTF_TYPE_VEC2; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4722 | } else if (type.compare("VEC3") == 0) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4723 | accessor->type = TINYGLTF_TYPE_VEC3; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4724 | } else if (type.compare("VEC4") == 0) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4725 | accessor->type = TINYGLTF_TYPE_VEC4; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4726 | } else if (type.compare("MAT2") == 0) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4727 | accessor->type = TINYGLTF_TYPE_MAT2; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4728 | } else if (type.compare("MAT3") == 0) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4729 | accessor->type = TINYGLTF_TYPE_MAT3; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4730 | } else if (type.compare("MAT4") == 0) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4731 | accessor->type = TINYGLTF_TYPE_MAT4; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4732 | } else { |
| 4733 | std::stringstream ss; |
| 4734 | ss << "Unsupported `type` for accessor object. Got \"" << type << "\"\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4735 | if (err) { |
| 4736 | (*err) += ss.str(); |
| 4737 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4738 | return false; |
| 4739 | } |
| 4740 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4741 | ParseStringProperty(&accessor->name, err, o, "name", false); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4742 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4743 | accessor->minValues.clear(); |
| 4744 | accessor->maxValues.clear(); |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 4745 | ParseNumberArrayProperty(&accessor->minValues, err, o, "min", false, |
| 4746 | "Accessor"); |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 4747 | |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 4748 | ParseNumberArrayProperty(&accessor->maxValues, err, o, "max", false, |
| 4749 | "Accessor"); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4750 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4751 | accessor->count = count; |
| 4752 | accessor->bufferView = bufferView; |
| 4753 | accessor->byteOffset = byteOffset; |
jianghaosen | 4748ad6 | 2017-08-29 20:08:37 +0800 | [diff] [blame] | 4754 | accessor->normalized = normalized; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4755 | { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4756 | if (componentType >= TINYGLTF_COMPONENT_TYPE_BYTE && |
| 4757 | componentType <= TINYGLTF_COMPONENT_TYPE_DOUBLE) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4758 | // OK |
Arthur Brainville (Ybalrid) | 811e1d3 | 2019-06-15 07:32:38 +0200 | [diff] [blame] | 4759 | accessor->componentType = int(componentType); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4760 | } else { |
| 4761 | std::stringstream ss; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4762 | ss << "Invalid `componentType` in accessor. Got " << componentType |
| 4763 | << "\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4764 | if (err) { |
| 4765 | (*err) += ss.str(); |
| 4766 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4767 | return false; |
| 4768 | } |
| 4769 | } |
| 4770 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4771 | ParseExtrasAndExtensions(accessor, err, o, |
| 4772 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4773 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4774 | // check if accessor has a "sparse" object: |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4775 | detail::json_const_iterator iterator; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4776 | if (detail::FindMember(o, "sparse", iterator)) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4777 | // here this accessor has a "sparse" subobject |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 4778 | return ParseSparseAccessor(&accessor->sparse, err, |
| 4779 | detail::GetValue(iterator), |
| 4780 | store_original_json_for_extras_and_extensions); |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4781 | } |
| 4782 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4783 | return true; |
| 4784 | } |
| 4785 | |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4786 | #ifdef TINYGLTF_ENABLE_DRACO |
| 4787 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4788 | static void DecodeIndexBuffer(draco::Mesh *mesh, size_t componentSize, |
| 4789 | std::vector<uint8_t> &outBuffer) { |
| 4790 | if (componentSize == 4) { |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4791 | assert(sizeof(mesh->face(draco::FaceIndex(0))[0]) == componentSize); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4792 | memcpy(outBuffer.data(), &mesh->face(draco::FaceIndex(0))[0], |
| 4793 | outBuffer.size()); |
| 4794 | } else { |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4795 | size_t faceStride = componentSize * 3; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4796 | for (draco::FaceIndex f(0); f < mesh->num_faces(); ++f) { |
| 4797 | const draco::Mesh::Face &face = mesh->face(f); |
| 4798 | if (componentSize == 2) { |
| 4799 | uint16_t indices[3] = {(uint16_t)face[0].value(), |
| 4800 | (uint16_t)face[1].value(), |
| 4801 | (uint16_t)face[2].value()}; |
| 4802 | memcpy(outBuffer.data() + f.value() * faceStride, &indices[0], |
| 4803 | faceStride); |
| 4804 | } else { |
| 4805 | uint8_t indices[3] = {(uint8_t)face[0].value(), |
| 4806 | (uint8_t)face[1].value(), |
| 4807 | (uint8_t)face[2].value()}; |
| 4808 | memcpy(outBuffer.data() + f.value() * faceStride, &indices[0], |
| 4809 | faceStride); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4810 | } |
| 4811 | } |
| 4812 | } |
| 4813 | } |
| 4814 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4815 | template <typename T> |
| 4816 | static bool GetAttributeForAllPoints(draco::Mesh *mesh, |
| 4817 | const draco::PointAttribute *pAttribute, |
| 4818 | std::vector<uint8_t> &outBuffer) { |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4819 | size_t byteOffset = 0; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4820 | T values[4] = {0, 0, 0, 0}; |
| 4821 | for (draco::PointIndex i(0); i < mesh->num_points(); ++i) { |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4822 | const draco::AttributeValueIndex val_index = pAttribute->mapped_index(i); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4823 | if (!pAttribute->ConvertValue<T>(val_index, pAttribute->num_components(), |
| 4824 | values)) |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4825 | return false; |
| 4826 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4827 | memcpy(outBuffer.data() + byteOffset, &values[0], |
| 4828 | sizeof(T) * pAttribute->num_components()); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4829 | byteOffset += sizeof(T) * pAttribute->num_components(); |
| 4830 | } |
| 4831 | |
| 4832 | return true; |
| 4833 | } |
| 4834 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4835 | static bool GetAttributeForAllPoints(uint32_t componentType, draco::Mesh *mesh, |
| 4836 | const draco::PointAttribute *pAttribute, |
| 4837 | std::vector<uint8_t> &outBuffer) { |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4838 | bool decodeResult = false; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4839 | switch (componentType) { |
| 4840 | case TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE: |
| 4841 | decodeResult = |
| 4842 | GetAttributeForAllPoints<uint8_t>(mesh, pAttribute, outBuffer); |
| 4843 | break; |
| 4844 | case TINYGLTF_COMPONENT_TYPE_BYTE: |
| 4845 | decodeResult = |
| 4846 | GetAttributeForAllPoints<int8_t>(mesh, pAttribute, outBuffer); |
| 4847 | break; |
| 4848 | case TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT: |
| 4849 | decodeResult = |
| 4850 | GetAttributeForAllPoints<uint16_t>(mesh, pAttribute, outBuffer); |
| 4851 | break; |
| 4852 | case TINYGLTF_COMPONENT_TYPE_SHORT: |
| 4853 | decodeResult = |
| 4854 | GetAttributeForAllPoints<int16_t>(mesh, pAttribute, outBuffer); |
| 4855 | break; |
| 4856 | case TINYGLTF_COMPONENT_TYPE_INT: |
| 4857 | decodeResult = |
| 4858 | GetAttributeForAllPoints<int32_t>(mesh, pAttribute, outBuffer); |
| 4859 | break; |
| 4860 | case TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT: |
| 4861 | decodeResult = |
| 4862 | GetAttributeForAllPoints<uint32_t>(mesh, pAttribute, outBuffer); |
| 4863 | break; |
| 4864 | case TINYGLTF_COMPONENT_TYPE_FLOAT: |
| 4865 | decodeResult = |
| 4866 | GetAttributeForAllPoints<float>(mesh, pAttribute, outBuffer); |
| 4867 | break; |
| 4868 | case TINYGLTF_COMPONENT_TYPE_DOUBLE: |
| 4869 | decodeResult = |
| 4870 | GetAttributeForAllPoints<double>(mesh, pAttribute, outBuffer); |
| 4871 | break; |
| 4872 | default: |
| 4873 | return false; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4874 | } |
| 4875 | |
| 4876 | return decodeResult; |
| 4877 | } |
| 4878 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4879 | static bool ParseDracoExtension(Primitive *primitive, Model *model, |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 4880 | std::string *err, std::string *warn, |
| 4881 | const Value &dracoExtensionValue, |
| 4882 | ParseStrictness strictness) { |
snowapril | 84e1526 | 2021-03-20 19:25:58 +0900 | [diff] [blame] | 4883 | (void)err; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4884 | auto bufferViewValue = dracoExtensionValue.Get("bufferView"); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4885 | if (!bufferViewValue.IsInt()) return false; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4886 | auto attributesValue = dracoExtensionValue.Get("attributes"); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4887 | if (!attributesValue.IsObject()) return false; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4888 | |
| 4889 | auto attributesObject = attributesValue.Get<Value::Object>(); |
| 4890 | int bufferView = bufferViewValue.Get<int>(); |
| 4891 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4892 | BufferView &view = model->bufferViews[bufferView]; |
| 4893 | Buffer &buffer = model->buffers[view.buffer]; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4894 | // BufferView has already been decoded |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4895 | if (view.dracoDecoded) return true; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4896 | view.dracoDecoded = true; |
| 4897 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4898 | const char *bufferViewData = |
| 4899 | reinterpret_cast<const char *>(buffer.data.data() + view.byteOffset); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4900 | size_t bufferViewSize = view.byteLength; |
| 4901 | |
| 4902 | // decode draco |
| 4903 | draco::DecoderBuffer decoderBuffer; |
| 4904 | decoderBuffer.Init(bufferViewData, bufferViewSize); |
| 4905 | draco::Decoder decoder; |
| 4906 | auto decodeResult = decoder.DecodeMeshFromBuffer(&decoderBuffer); |
| 4907 | if (!decodeResult.ok()) { |
| 4908 | return false; |
| 4909 | } |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4910 | const std::unique_ptr<draco::Mesh> &mesh = decodeResult.value(); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4911 | |
| 4912 | // create new bufferView for indices |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4913 | if (primitive->indices >= 0) { |
Nyall Dawson | 0067b4d | 2023-09-07 08:20:28 +1000 | [diff] [blame] | 4914 | if (strictness == ParseStrictness::Permissive) { |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 4915 | const draco::PointIndex::ValueType numPoint = mesh->num_points(); |
| 4916 | // handle the situation where the stored component type does not match the |
| 4917 | // required type for the actual number of stored points |
| 4918 | int supposedComponentType = TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE; |
| 4919 | if (numPoint < static_cast<draco::PointIndex::ValueType>( |
| 4920 | std::numeric_limits<uint8_t>::max())) { |
| 4921 | supposedComponentType = TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE; |
| 4922 | } else if ( |
| 4923 | numPoint < static_cast<draco::PointIndex::ValueType>( |
| 4924 | std::numeric_limits<uint16_t>::max())) { |
| 4925 | supposedComponentType = TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT; |
| 4926 | } else { |
| 4927 | supposedComponentType = TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT; |
| 4928 | } |
| 4929 | |
| 4930 | if (supposedComponentType > model->accessors[primitive->indices].componentType) { |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 4931 | if (warn) { |
| 4932 | (*warn) += |
| 4933 | "GLTF component type " + std::to_string(model->accessors[primitive->indices].componentType) + |
| 4934 | " is not sufficient for number of stored points," |
| 4935 | " treating as " + std::to_string(supposedComponentType) + "\n"; |
| 4936 | } |
Nyall Dawson | c35819f | 2023-09-12 08:46:02 +1000 | [diff] [blame] | 4937 | model->accessors[primitive->indices].componentType = supposedComponentType; |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 4938 | } |
| 4939 | } |
| 4940 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4941 | int32_t componentSize = GetComponentSizeInBytes( |
| 4942 | model->accessors[primitive->indices].componentType); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4943 | Buffer decodedIndexBuffer; |
| 4944 | decodedIndexBuffer.data.resize(mesh->num_faces() * 3 * componentSize); |
| 4945 | |
| 4946 | DecodeIndexBuffer(mesh.get(), componentSize, decodedIndexBuffer.data); |
| 4947 | |
| 4948 | model->buffers.emplace_back(std::move(decodedIndexBuffer)); |
| 4949 | |
| 4950 | BufferView decodedIndexBufferView; |
| 4951 | decodedIndexBufferView.buffer = int(model->buffers.size() - 1); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4952 | decodedIndexBufferView.byteLength = |
| 4953 | int(mesh->num_faces() * 3 * componentSize); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4954 | decodedIndexBufferView.byteOffset = 0; |
| 4955 | decodedIndexBufferView.byteStride = 0; |
| 4956 | decodedIndexBufferView.target = TINYGLTF_TARGET_ARRAY_BUFFER; |
| 4957 | model->bufferViews.emplace_back(std::move(decodedIndexBufferView)); |
| 4958 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4959 | model->accessors[primitive->indices].bufferView = |
| 4960 | int(model->bufferViews.size() - 1); |
Alexander Wood | 0d77a29 | 2019-01-26 08:58:45 -0500 | [diff] [blame] | 4961 | model->accessors[primitive->indices].count = int(mesh->num_faces() * 3); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4962 | } |
| 4963 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4964 | for (const auto &attribute : attributesObject) { |
| 4965 | if (!attribute.second.IsInt()) return false; |
Alexander Wood | 0d77a29 | 2019-01-26 08:58:45 -0500 | [diff] [blame] | 4966 | auto primitiveAttribute = primitive->attributes.find(attribute.first); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4967 | if (primitiveAttribute == primitive->attributes.end()) return false; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4968 | |
| 4969 | int dracoAttributeIndex = attribute.second.Get<int>(); |
| 4970 | const auto pAttribute = mesh->GetAttributeByUniqueId(dracoAttributeIndex); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4971 | const auto componentType = |
| 4972 | model->accessors[primitiveAttribute->second].componentType; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4973 | |
| 4974 | // Create a new buffer for this decoded buffer |
| 4975 | Buffer decodedBuffer; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4976 | size_t bufferSize = mesh->num_points() * pAttribute->num_components() * |
| 4977 | GetComponentSizeInBytes(componentType); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4978 | decodedBuffer.data.resize(bufferSize); |
| 4979 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4980 | if (!GetAttributeForAllPoints(componentType, mesh.get(), pAttribute, |
| 4981 | decodedBuffer.data)) |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4982 | return false; |
| 4983 | |
| 4984 | model->buffers.emplace_back(std::move(decodedBuffer)); |
| 4985 | |
| 4986 | BufferView decodedBufferView; |
| 4987 | decodedBufferView.buffer = int(model->buffers.size() - 1); |
| 4988 | decodedBufferView.byteLength = bufferSize; |
| 4989 | decodedBufferView.byteOffset = pAttribute->byte_offset(); |
| 4990 | decodedBufferView.byteStride = pAttribute->byte_stride(); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4991 | decodedBufferView.target = primitive->indices >= 0 |
| 4992 | ? TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER |
| 4993 | : TINYGLTF_TARGET_ARRAY_BUFFER; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4994 | model->bufferViews.emplace_back(std::move(decodedBufferView)); |
| 4995 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4996 | model->accessors[primitiveAttribute->second].bufferView = |
| 4997 | int(model->bufferViews.size() - 1); |
| 4998 | model->accessors[primitiveAttribute->second].count = |
| 4999 | int(mesh->num_points()); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 5000 | } |
| 5001 | |
| 5002 | return true; |
| 5003 | } |
| 5004 | #endif |
| 5005 | |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 5006 | static bool ParsePrimitive(Primitive *primitive, Model *model, |
| 5007 | std::string *err, std::string *warn, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5008 | const detail::json &o, |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 5009 | bool store_original_json_for_extras_and_extensions, |
| 5010 | ParseStrictness strictness) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5011 | int material = -1; |
| 5012 | ParseIntegerProperty(&material, err, o, "material", false); |
| 5013 | primitive->material = material; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5014 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5015 | int mode = TINYGLTF_MODE_TRIANGLES; |
| 5016 | ParseIntegerProperty(&mode, err, o, "mode", false); |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 5017 | primitive->mode = mode; // Why only triangles were supported ? |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5018 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5019 | int indices = -1; |
| 5020 | ParseIntegerProperty(&indices, err, o, "indices", false); |
| 5021 | primitive->indices = indices; |
| 5022 | if (!ParseStringIntegerProperty(&primitive->attributes, err, o, "attributes", |
| 5023 | true, "Primitive")) { |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 5024 | return false; |
| 5025 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5026 | |
Aurélien Chatelain | 2682c6f | 2017-06-14 09:46:57 +0000 | [diff] [blame] | 5027 | // Look for morph targets |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5028 | detail::json_const_iterator targetsObject; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5029 | if (detail::FindMember(o, "targets", targetsObject) && |
| 5030 | detail::IsArray(detail::GetValue(targetsObject))) { |
| 5031 | auto targetsObjectEnd = detail::ArrayEnd(detail::GetValue(targetsObject)); |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5032 | for (detail::json_const_array_iterator i = |
| 5033 | detail::ArrayBegin(detail::GetValue(targetsObject)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5034 | i != targetsObjectEnd; ++i) { |
Aurélien Chatelain | 2682c6f | 2017-06-14 09:46:57 +0000 | [diff] [blame] | 5035 | std::map<std::string, int> targetAttribues; |
| 5036 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5037 | const detail::json &dict = *i; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5038 | if (detail::IsObject(dict)) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5039 | detail::json_const_iterator dictIt(detail::ObjectBegin(dict)); |
| 5040 | detail::json_const_iterator dictItEnd(detail::ObjectEnd(dict)); |
Aurélien Chatelain | 2682c6f | 2017-06-14 09:46:57 +0000 | [diff] [blame] | 5041 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5042 | for (; dictIt != dictItEnd; ++dictIt) { |
| 5043 | int iVal; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5044 | if (detail::GetInt(detail::GetValue(dictIt), iVal)) |
| 5045 | targetAttribues[detail::GetKey(dictIt)] = iVal; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5046 | } |
| 5047 | primitive->targets.emplace_back(std::move(targetAttribues)); |
Aurélien Chatelain | 2682c6f | 2017-06-14 09:46:57 +0000 | [diff] [blame] | 5048 | } |
Aurélien Chatelain | 2682c6f | 2017-06-14 09:46:57 +0000 | [diff] [blame] | 5049 | } |
| 5050 | } |
| 5051 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5052 | ParseExtrasAndExtensions(primitive, err, o, |
| 5053 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5054 | |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 5055 | #ifdef TINYGLTF_ENABLE_DRACO |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 5056 | auto dracoExtension = |
| 5057 | primitive->extensions.find("KHR_draco_mesh_compression"); |
| 5058 | if (dracoExtension != primitive->extensions.end()) { |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 5059 | ParseDracoExtension(primitive, model, err, warn, dracoExtension->second, strictness); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 5060 | } |
Syoyo Fujita | 7bdfed3 | 2019-03-03 17:04:49 +0900 | [diff] [blame] | 5061 | #else |
| 5062 | (void)model; |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 5063 | (void)warn; |
Nyall Dawson | 5a7b827 | 2023-09-03 09:04:56 +1000 | [diff] [blame] | 5064 | (void)strictness; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 5065 | #endif |
| 5066 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5067 | return true; |
| 5068 | } |
| 5069 | |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 5070 | static bool ParseMesh(Mesh *mesh, Model *model, |
| 5071 | std::string *err, std::string *warn, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5072 | const detail::json &o, |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 5073 | bool store_original_json_for_extras_and_extensions, |
| 5074 | ParseStrictness strictness) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5075 | ParseStringProperty(&mesh->name, err, o, "name", false); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5076 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5077 | mesh->primitives.clear(); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5078 | detail::json_const_iterator primObject; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5079 | if (detail::FindMember(o, "primitives", primObject) && |
| 5080 | detail::IsArray(detail::GetValue(primObject))) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5081 | detail::json_const_array_iterator primEnd = |
| 5082 | detail::ArrayEnd(detail::GetValue(primObject)); |
| 5083 | for (detail::json_const_array_iterator i = |
| 5084 | detail::ArrayBegin(detail::GetValue(primObject)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5085 | i != primEnd; ++i) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5086 | Primitive primitive; |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 5087 | if (ParsePrimitive(&primitive, model, err, warn, *i, |
| 5088 | store_original_json_for_extras_and_extensions, |
| 5089 | strictness)) { |
Luke San Antonio | 10a3e1d | 2016-06-15 00:05:01 -0400 | [diff] [blame] | 5090 | // Only add the primitive if the parsing succeeds. |
jrkoonce | d1e1472 | 2019-08-27 11:51:02 -0500 | [diff] [blame] | 5091 | mesh->primitives.emplace_back(std::move(primitive)); |
Luke San Antonio | 10a3e1d | 2016-06-15 00:05:01 -0400 | [diff] [blame] | 5092 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5093 | } |
| 5094 | } |
| 5095 | |
Aurélien Chatelain | 38a6a75 | 2017-05-24 09:54:55 +0000 | [diff] [blame] | 5096 | // Should probably check if has targets and if dimensions fit |
| 5097 | ParseNumberArrayProperty(&mesh->weights, err, o, "weights", false); |
| 5098 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5099 | ParseExtrasAndExtensions(mesh, err, o, |
| 5100 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5101 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5102 | return true; |
| 5103 | } |
| 5104 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5105 | static bool ParseNode(Node *node, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5106 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5107 | ParseStringProperty(&node->name, err, o, "name", false); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5108 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5109 | int skin = -1; |
| 5110 | ParseIntegerProperty(&skin, err, o, "skin", false); |
| 5111 | node->skin = skin; |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 5112 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 5113 | // Matrix and T/R/S are exclusive |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 5114 | if (!ParseNumberArrayProperty(&node->matrix, err, o, "matrix", false)) { |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 5115 | ParseNumberArrayProperty(&node->rotation, err, o, "rotation", false); |
| 5116 | ParseNumberArrayProperty(&node->scale, err, o, "scale", false); |
| 5117 | ParseNumberArrayProperty(&node->translation, err, o, "translation", false); |
| 5118 | } |
| 5119 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5120 | int camera = -1; |
| 5121 | ParseIntegerProperty(&camera, err, o, "camera", false); |
| 5122 | node->camera = camera; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 5123 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5124 | int mesh = -1; |
| 5125 | ParseIntegerProperty(&mesh, err, o, "mesh", false); |
| 5126 | node->mesh = mesh; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5127 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5128 | node->children.clear(); |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5129 | ParseIntegerArrayProperty(&node->children, err, o, "children", false); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5130 | |
Benjamin Schmithüsen | ad63bf7 | 2019-08-16 14:19:27 +0200 | [diff] [blame] | 5131 | ParseNumberArrayProperty(&node->weights, err, o, "weights", false); |
| 5132 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5133 | ParseExtrasAndExtensions(node, err, o, |
| 5134 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5135 | |
David Siegel | c164878 | 2023-05-09 01:33:57 +0200 | [diff] [blame] | 5136 | // KHR_lights_punctual: parse light source reference |
| 5137 | int light = -1; |
| 5138 | if (node->extensions.count("KHR_lights_punctual") != 0) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5139 | auto const &light_ext = node->extensions["KHR_lights_punctual"]; |
David Siegel | c164878 | 2023-05-09 01:33:57 +0200 | [diff] [blame] | 5140 | if (light_ext.Has("light")) { |
| 5141 | light = light_ext.Get("light").GetNumberAsInt(); |
| 5142 | } else { |
David Siegel | 157063f | 2023-06-06 15:31:58 +0200 | [diff] [blame] | 5143 | if (err) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5144 | *err += |
| 5145 | "Node has extension KHR_lights_punctual, but does not reference " |
| 5146 | "a light source.\n"; |
David Siegel | 157063f | 2023-06-06 15:31:58 +0200 | [diff] [blame] | 5147 | } |
David Siegel | c164878 | 2023-05-09 01:33:57 +0200 | [diff] [blame] | 5148 | return false; |
| 5149 | } |
| 5150 | } |
| 5151 | node->light = light; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 5152 | |
| 5153 | // KHR_audio: parse audio source reference |
| 5154 | int emitter = -1; |
| 5155 | if (node->extensions.count("KHR_audio") != 0) { |
| 5156 | auto const &audio_ext = node->extensions["KHR_audio"]; |
| 5157 | if (audio_ext.Has("emitter")) { |
| 5158 | emitter = audio_ext.Get("emitter").GetNumberAsInt(); |
| 5159 | } else { |
| 5160 | if (err) { |
| 5161 | *err += |
| 5162 | "Node has extension KHR_audio, but does not reference " |
| 5163 | "a audio emitter.\n"; |
| 5164 | } |
| 5165 | return false; |
| 5166 | } |
| 5167 | } |
| 5168 | node->emitter = emitter; |
Syoyo Fujita | 7a570c8 | 2023-06-19 21:52:13 +0900 | [diff] [blame] | 5169 | |
Thomas Gamper | a42263b | 2024-02-05 15:42:49 +0100 | [diff] [blame] | 5170 | node->lods.clear(); |
| 5171 | if (node->extensions.count("MSFT_lod") != 0) { |
| 5172 | auto const &msft_lod_ext = node->extensions["MSFT_lod"]; |
| 5173 | if (msft_lod_ext.Has("ids")) { |
| 5174 | auto idsArr = msft_lod_ext.Get("ids"); |
| 5175 | for (size_t i = 0; i < idsArr.ArrayLen(); ++i) { |
| 5176 | node->lods.emplace_back(idsArr.Get(i).GetNumberAsInt()); |
| 5177 | } |
| 5178 | } else { |
| 5179 | if (err) { |
| 5180 | *err += |
| 5181 | "Node has extension MSFT_lod, but does not reference " |
| 5182 | "other nodes via their ids.\n"; |
| 5183 | } |
| 5184 | return false; |
| 5185 | } |
| 5186 | } |
| 5187 | |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 5188 | return true; |
Luke San Antonio | cdf4cb7 | 2016-06-14 21:32:11 -0400 | [diff] [blame] | 5189 | } |
| 5190 | |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 5191 | static bool ParseScene(Scene *scene, std::string *err, const detail::json &o, |
| 5192 | bool store_original_json_for_extras_and_extensions) { |
| 5193 | ParseStringProperty(&scene->name, err, o, "name", false); |
| 5194 | ParseIntegerArrayProperty(&scene->nodes, err, o, "nodes", false); |
| 5195 | |
| 5196 | ParseExtrasAndExtensions(scene, err, o, |
| 5197 | store_original_json_for_extras_and_extensions); |
| 5198 | |
| 5199 | // Parse KHR_audio global emitters |
| 5200 | if (scene->extensions.count("KHR_audio") != 0) { |
| 5201 | auto const &audio_ext = scene->extensions["KHR_audio"]; |
| 5202 | if (audio_ext.Has("emitters")) { |
| 5203 | auto emittersArr = audio_ext.Get("emitters"); |
| 5204 | for (size_t i = 0; i < emittersArr.ArrayLen(); ++i) { |
| 5205 | scene->audioEmitters.emplace_back(emittersArr.Get(i).GetNumberAsInt()); |
| 5206 | } |
| 5207 | } else { |
| 5208 | if (err) { |
| 5209 | *err += |
| 5210 | "Node has extension KHR_audio, but does not reference " |
| 5211 | "a audio emitter.\n"; |
| 5212 | } |
| 5213 | return false; |
| 5214 | } |
| 5215 | } |
| 5216 | |
| 5217 | return true; |
| 5218 | } |
| 5219 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5220 | static bool ParsePbrMetallicRoughness( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5221 | PbrMetallicRoughness *pbr, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5222 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5223 | if (pbr == nullptr) { |
| 5224 | return false; |
| 5225 | } |
| 5226 | |
| 5227 | std::vector<double> baseColorFactor; |
| 5228 | if (ParseNumberArrayProperty(&baseColorFactor, err, o, "baseColorFactor", |
| 5229 | /* required */ false)) { |
| 5230 | if (baseColorFactor.size() != 4) { |
| 5231 | if (err) { |
| 5232 | (*err) += |
| 5233 | "Array length of `baseColorFactor` parameter in " |
| 5234 | "pbrMetallicRoughness must be 4, but got " + |
| 5235 | std::to_string(baseColorFactor.size()) + "\n"; |
| 5236 | } |
| 5237 | return false; |
| 5238 | } |
Selmar Kok | c3353e1 | 2019-10-18 18:22:35 +0200 | [diff] [blame] | 5239 | pbr->baseColorFactor = baseColorFactor; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5240 | } |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5241 | |
| 5242 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5243 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5244 | if (detail::FindMember(o, "baseColorTexture", it)) { |
| 5245 | ParseTextureInfo(&pbr->baseColorTexture, err, detail::GetValue(it), |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5246 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5247 | } |
| 5248 | } |
| 5249 | |
| 5250 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5251 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5252 | if (detail::FindMember(o, "metallicRoughnessTexture", it)) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5253 | ParseTextureInfo(&pbr->metallicRoughnessTexture, err, |
| 5254 | detail::GetValue(it), |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5255 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5256 | } |
| 5257 | } |
| 5258 | |
| 5259 | ParseNumberProperty(&pbr->metallicFactor, err, o, "metallicFactor", false); |
| 5260 | ParseNumberProperty(&pbr->roughnessFactor, err, o, "roughnessFactor", false); |
| 5261 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5262 | ParseExtrasAndExtensions(pbr, err, o, |
| 5263 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5264 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5265 | return true; |
| 5266 | } |
| 5267 | |
Nyall Dawson | 02e8b8d | 2023-08-24 10:25:20 +1000 | [diff] [blame] | 5268 | static bool ParseMaterial(Material *material, std::string *err, std::string *warn, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5269 | const detail::json &o, |
Nyall Dawson | 8c85d5e | 2023-08-28 12:56:09 +1000 | [diff] [blame] | 5270 | bool store_original_json_for_extras_and_extensions, |
| 5271 | ParseStrictness strictness) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5272 | ParseStringProperty(&material->name, err, o, "name", /* required */ false); |
| 5273 | |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 5274 | if (ParseNumberArrayProperty(&material->emissiveFactor, err, o, |
| 5275 | "emissiveFactor", |
| 5276 | /* required */ false)) { |
Nyall Dawson | bbc1eae | 2023-09-03 09:05:32 +1000 | [diff] [blame] | 5277 | if (strictness==ParseStrictness::Permissive && material->emissiveFactor.size() == 4) { |
Nyall Dawson | 02e8b8d | 2023-08-24 10:25:20 +1000 | [diff] [blame] | 5278 | if (warn) { |
| 5279 | (*warn) += |
| 5280 | "Array length of `emissiveFactor` parameter in " |
| 5281 | "material must be 3, but got 4\n"; |
| 5282 | } |
| 5283 | material->emissiveFactor.resize(3); |
| 5284 | } |
| 5285 | else if (material->emissiveFactor.size() != 3) { |
Selmar Kok | 6df800d | 2019-08-19 11:05:28 +0200 | [diff] [blame] | 5286 | if (err) { |
| 5287 | (*err) += |
| 5288 | "Array length of `emissiveFactor` parameter in " |
| 5289 | "material must be 3, but got " + |
| 5290 | std::to_string(material->emissiveFactor.size()) + "\n"; |
| 5291 | } |
| 5292 | return false; |
| 5293 | } |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 5294 | } else { |
Selmar Kok | 6df800d | 2019-08-19 11:05:28 +0200 | [diff] [blame] | 5295 | // fill with default values |
Selmar Kok | 6dba6c6 | 2019-08-19 11:23:31 +0200 | [diff] [blame] | 5296 | material->emissiveFactor = {0.0, 0.0, 0.0}; |
Selmar Kok | 6df800d | 2019-08-19 11:05:28 +0200 | [diff] [blame] | 5297 | } |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5298 | |
| 5299 | ParseStringProperty(&material->alphaMode, err, o, "alphaMode", |
| 5300 | /* required */ false); |
| 5301 | ParseNumberProperty(&material->alphaCutoff, err, o, "alphaCutoff", |
| 5302 | /* required */ false); |
| 5303 | ParseBooleanProperty(&material->doubleSided, err, o, "doubleSided", |
| 5304 | /* required */ false); |
| 5305 | |
| 5306 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5307 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5308 | if (detail::FindMember(o, "pbrMetallicRoughness", it)) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5309 | ParsePbrMetallicRoughness(&material->pbrMetallicRoughness, err, |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5310 | detail::GetValue(it), |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5311 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5312 | } |
| 5313 | } |
| 5314 | |
| 5315 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5316 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5317 | if (detail::FindMember(o, "normalTexture", it)) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5318 | ParseNormalTextureInfo(&material->normalTexture, err, |
| 5319 | detail::GetValue(it), |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5320 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5321 | } |
| 5322 | } |
| 5323 | |
| 5324 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5325 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5326 | if (detail::FindMember(o, "occlusionTexture", it)) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5327 | ParseOcclusionTextureInfo(&material->occlusionTexture, err, |
| 5328 | detail::GetValue(it), |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5329 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5330 | } |
| 5331 | } |
| 5332 | |
| 5333 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5334 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5335 | if (detail::FindMember(o, "emissiveTexture", it)) { |
| 5336 | ParseTextureInfo(&material->emissiveTexture, err, detail::GetValue(it), |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5337 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5338 | } |
| 5339 | } |
| 5340 | |
| 5341 | // Old code path. For backward compatibility, we still store material values |
| 5342 | // as Parameter. This will create duplicated information for |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 5343 | // example(pbrMetallicRoughness), but should be negligible in terms of memory |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5344 | // consumption. |
| 5345 | // TODO(syoyo): Remove in the next major release. |
| 5346 | material->values.clear(); |
| 5347 | material->additionalValues.clear(); |
Luke San Antonio | cdf4cb7 | 2016-06-14 21:32:11 -0400 | [diff] [blame] | 5348 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5349 | detail::json_const_iterator it(detail::ObjectBegin(o)); |
| 5350 | detail::json_const_iterator itEnd(detail::ObjectEnd(o)); |
Luke San Antonio | cdf4cb7 | 2016-06-14 21:32:11 -0400 | [diff] [blame] | 5351 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5352 | for (; it != itEnd; ++it) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5353 | std::string key(detail::GetKey(it)); |
jrkoonce | 06c30c4 | 2019-09-03 15:56:48 -0500 | [diff] [blame] | 5354 | if (key == "pbrMetallicRoughness") { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5355 | if (detail::IsObject(detail::GetValue(it))) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5356 | const detail::json &values_object = detail::GetValue(it); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5357 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5358 | detail::json_const_iterator itVal(detail::ObjectBegin(values_object)); |
| 5359 | detail::json_const_iterator itValEnd(detail::ObjectEnd(values_object)); |
jrkoonce | 06c30c4 | 2019-09-03 15:56:48 -0500 | [diff] [blame] | 5360 | |
| 5361 | for (; itVal != itValEnd; ++itVal) { |
| 5362 | Parameter param; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5363 | if (ParseParameterProperty(¶m, err, values_object, |
| 5364 | detail::GetKey(itVal), false)) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5365 | material->values.emplace(detail::GetKey(itVal), std::move(param)); |
jrkoonce | 06c30c4 | 2019-09-03 15:56:48 -0500 | [diff] [blame] | 5366 | } |
| 5367 | } |
| 5368 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5369 | } else if (key == "extensions" || key == "extras") { |
| 5370 | // done later, skip, otherwise poorly parsed contents will be saved in the |
| 5371 | // parametermap and serialized again later |
| 5372 | } else { |
jrkoonce | 06c30c4 | 2019-09-03 15:56:48 -0500 | [diff] [blame] | 5373 | Parameter param; |
| 5374 | if (ParseParameterProperty(¶m, err, o, key, false)) { |
| 5375 | // names of materials have already been parsed. Putting it in this map |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 5376 | // doesn't correctly reflect the glTF specification |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5377 | if (key != "name") |
| 5378 | material->additionalValues.emplace(std::move(key), std::move(param)); |
jrkoonce | 06c30c4 | 2019-09-03 15:56:48 -0500 | [diff] [blame] | 5379 | } |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 5380 | } |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 5381 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5382 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5383 | material->extensions.clear(); // Note(agnat): Why? |
| 5384 | ParseExtrasAndExtensions(material, err, o, |
| 5385 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5386 | |
Thomas Gamper | a42263b | 2024-02-05 15:42:49 +0100 | [diff] [blame] | 5387 | material->lods.clear(); |
| 5388 | if (material->extensions.count("MSFT_lod") != 0) { |
| 5389 | auto const &msft_lod_ext = material->extensions["MSFT_lod"]; |
| 5390 | if (msft_lod_ext.Has("ids")) { |
| 5391 | auto idsArr = msft_lod_ext.Get("ids"); |
| 5392 | for (size_t i = 0; i < idsArr.ArrayLen(); ++i) { |
| 5393 | material->lods.emplace_back(idsArr.Get(i).GetNumberAsInt()); |
| 5394 | } |
| 5395 | } else { |
| 5396 | if (err) { |
| 5397 | *err += |
| 5398 | "Material has extension MSFT_lod, but does not reference " |
| 5399 | "other materials via their ids.\n"; |
| 5400 | } |
| 5401 | return false; |
| 5402 | } |
| 5403 | } |
| 5404 | |
Luke San Antonio | cdf4cb7 | 2016-06-14 21:32:11 -0400 | [diff] [blame] | 5405 | return true; |
| 5406 | } |
| 5407 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5408 | static bool ParseAnimationChannel( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5409 | AnimationChannel *channel, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5410 | bool store_original_json_for_extras_and_extensions) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5411 | int samplerIndex = -1; |
| 5412 | int targetIndex = -1; |
| 5413 | if (!ParseIntegerProperty(&samplerIndex, err, o, "sampler", true, |
| 5414 | "AnimationChannel")) { |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5415 | if (err) { |
| 5416 | (*err) += "`sampler` field is missing in animation channels\n"; |
| 5417 | } |
| 5418 | return false; |
| 5419 | } |
| 5420 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5421 | detail::json_const_iterator targetIt; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5422 | if (detail::FindMember(o, "target", targetIt) && |
| 5423 | detail::IsObject(detail::GetValue(targetIt))) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5424 | const detail::json &target_object = detail::GetValue(targetIt); |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5425 | |
Jack Mousseau | 283b552 | 2023-01-15 11:45:45 -0800 | [diff] [blame] | 5426 | ParseIntegerProperty(&targetIndex, err, target_object, "node", false); |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5427 | |
| 5428 | if (!ParseStringProperty(&channel->target_path, err, target_object, "path", |
| 5429 | true)) { |
| 5430 | if (err) { |
| 5431 | (*err) += "`path` field is missing in animation.channels.target\n"; |
| 5432 | } |
| 5433 | return false; |
| 5434 | } |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 5435 | ParseExtensionsProperty(&channel->target_extensions, err, target_object); |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 5436 | ParseExtrasProperty(&channel->target_extras, target_object); |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 5437 | if (store_original_json_for_extras_and_extensions) { |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 5438 | { |
| 5439 | detail::json_const_iterator it; |
| 5440 | if (detail::FindMember(target_object, "extensions", it)) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5441 | channel->target_extensions_json_string = |
| 5442 | detail::JsonToString(detail::GetValue(it)); |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 5443 | } |
| 5444 | } |
| 5445 | { |
| 5446 | detail::json_const_iterator it; |
| 5447 | if (detail::FindMember(target_object, "extras", it)) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5448 | channel->target_extras_json_string = |
| 5449 | detail::JsonToString(detail::GetValue(it)); |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 5450 | } |
Selmar Kok | 973d9b3 | 2020-01-21 18:45:24 +0100 | [diff] [blame] | 5451 | } |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 5452 | } |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5453 | } |
| 5454 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5455 | channel->sampler = samplerIndex; |
| 5456 | channel->target_node = targetIndex; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 5457 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5458 | ParseExtrasAndExtensions(channel, err, o, |
| 5459 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5460 | |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5461 | return true; |
| 5462 | } |
| 5463 | |
| 5464 | static bool ParseAnimation(Animation *animation, std::string *err, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5465 | const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5466 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5467 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5468 | detail::json_const_iterator channelsIt; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5469 | if (detail::FindMember(o, "channels", channelsIt) && |
| 5470 | detail::IsArray(detail::GetValue(channelsIt))) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5471 | detail::json_const_array_iterator channelEnd = |
| 5472 | detail::ArrayEnd(detail::GetValue(channelsIt)); |
| 5473 | for (detail::json_const_array_iterator i = |
| 5474 | detail::ArrayBegin(detail::GetValue(channelsIt)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5475 | i != channelEnd; ++i) { |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5476 | AnimationChannel channel; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5477 | if (ParseAnimationChannel( |
| 5478 | &channel, err, *i, |
| 5479 | store_original_json_for_extras_and_extensions)) { |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5480 | // Only add the channel if the parsing succeeds. |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5481 | animation->channels.emplace_back(std::move(channel)); |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5482 | } |
| 5483 | } |
| 5484 | } |
| 5485 | } |
| 5486 | |
| 5487 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5488 | detail::json_const_iterator samplerIt; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5489 | if (detail::FindMember(o, "samplers", samplerIt) && |
| 5490 | detail::IsArray(detail::GetValue(samplerIt))) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5491 | const detail::json &sampler_array = detail::GetValue(samplerIt); |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5492 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5493 | detail::json_const_array_iterator it = detail::ArrayBegin(sampler_array); |
| 5494 | detail::json_const_array_iterator itEnd = detail::ArrayEnd(sampler_array); |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5495 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5496 | for (; it != itEnd; ++it) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5497 | const detail::json &s = *it; |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5498 | |
| 5499 | AnimationSampler sampler; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5500 | int inputIndex = -1; |
| 5501 | int outputIndex = -1; |
| 5502 | if (!ParseIntegerProperty(&inputIndex, err, s, "input", true)) { |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5503 | if (err) { |
| 5504 | (*err) += "`input` field is missing in animation.sampler\n"; |
| 5505 | } |
| 5506 | return false; |
| 5507 | } |
Evan Birenbaum | 6bdffed | 2019-02-14 13:30:57 -0800 | [diff] [blame] | 5508 | ParseStringProperty(&sampler.interpolation, err, s, "interpolation", |
| 5509 | false); |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5510 | if (!ParseIntegerProperty(&outputIndex, err, s, "output", true)) { |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5511 | if (err) { |
| 5512 | (*err) += "`output` field is missing in animation.sampler\n"; |
| 5513 | } |
| 5514 | return false; |
| 5515 | } |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5516 | sampler.input = inputIndex; |
| 5517 | sampler.output = outputIndex; |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 5518 | ParseExtrasAndExtensions(&sampler, err, o, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5519 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5520 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5521 | animation->samplers.emplace_back(std::move(sampler)); |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5522 | } |
| 5523 | } |
| 5524 | } |
| 5525 | |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5526 | ParseStringProperty(&animation->name, err, o, "name", false); |
| 5527 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5528 | ParseExtrasAndExtensions(animation, err, o, |
| 5529 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5530 | |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5531 | return true; |
| 5532 | } |
| 5533 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5534 | static bool ParseSampler(Sampler *sampler, std::string *err, |
| 5535 | const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5536 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 5537 | ParseStringProperty(&sampler->name, err, o, "name", false); |
| 5538 | |
Syoyo Fujita | ee179b2 | 2019-08-16 13:11:30 +0900 | [diff] [blame] | 5539 | int minFilter = -1; |
| 5540 | int magFilter = -1; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5541 | int wrapS = TINYGLTF_TEXTURE_WRAP_REPEAT; |
| 5542 | int wrapT = TINYGLTF_TEXTURE_WRAP_REPEAT; |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 5543 | // int wrapR = TINYGLTF_TEXTURE_WRAP_REPEAT; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5544 | ParseIntegerProperty(&minFilter, err, o, "minFilter", false); |
| 5545 | ParseIntegerProperty(&magFilter, err, o, "magFilter", false); |
| 5546 | ParseIntegerProperty(&wrapS, err, o, "wrapS", false); |
| 5547 | ParseIntegerProperty(&wrapT, err, o, "wrapT", false); |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 5548 | // ParseIntegerProperty(&wrapR, err, o, "wrapR", false); // tinygltf |
| 5549 | // extension |
Syoyo Fujita | ee179b2 | 2019-08-16 13:11:30 +0900 | [diff] [blame] | 5550 | |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 5551 | // TODO(syoyo): Check the value is allowed one. |
Syoyo Fujita | ee179b2 | 2019-08-16 13:11:30 +0900 | [diff] [blame] | 5552 | // (e.g. we allow 9728(NEAREST), but don't allow 9727) |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 5553 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5554 | sampler->minFilter = minFilter; |
| 5555 | sampler->magFilter = magFilter; |
| 5556 | sampler->wrapS = wrapS; |
| 5557 | sampler->wrapT = wrapT; |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 5558 | // sampler->wrapR = wrapR; |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 5559 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5560 | ParseExtrasAndExtensions(sampler, err, o, |
| 5561 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5562 | |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 5563 | return true; |
| 5564 | } |
| 5565 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5566 | static bool ParseSkin(Skin *skin, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5567 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 90e1ed2 | 2017-05-30 00:47:37 +0900 | [diff] [blame] | 5568 | ParseStringProperty(&skin->name, err, o, "name", false, "Skin"); |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 5569 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5570 | std::vector<int> joints; |
| 5571 | if (!ParseIntegerArrayProperty(&joints, err, o, "joints", false, "Skin")) { |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 5572 | return false; |
| 5573 | } |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5574 | skin->joints = std::move(joints); |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 5575 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5576 | int skeleton = -1; |
| 5577 | ParseIntegerProperty(&skeleton, err, o, "skeleton", false, "Skin"); |
| 5578 | skin->skeleton = skeleton; |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 5579 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5580 | int invBind = -1; |
| 5581 | ParseIntegerProperty(&invBind, err, o, "inverseBindMatrices", true, "Skin"); |
| 5582 | skin->inverseBindMatrices = invBind; |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 5583 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5584 | ParseExtrasAndExtensions(skin, err, o, |
| 5585 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5586 | |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 5587 | return true; |
| 5588 | } |
| 5589 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5590 | static bool ParsePerspectiveCamera( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5591 | PerspectiveCamera *camera, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5592 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5593 | double yfov = 0.0; |
| 5594 | if (!ParseNumberProperty(&yfov, err, o, "yfov", true, "OrthographicCamera")) { |
| 5595 | return false; |
| 5596 | } |
| 5597 | |
| 5598 | double znear = 0.0; |
| 5599 | if (!ParseNumberProperty(&znear, err, o, "znear", true, |
| 5600 | "PerspectiveCamera")) { |
| 5601 | return false; |
| 5602 | } |
| 5603 | |
| 5604 | double aspectRatio = 0.0; // = invalid |
| 5605 | ParseNumberProperty(&aspectRatio, err, o, "aspectRatio", false, |
| 5606 | "PerspectiveCamera"); |
| 5607 | |
| 5608 | double zfar = 0.0; // = invalid |
| 5609 | ParseNumberProperty(&zfar, err, o, "zfar", false, "PerspectiveCamera"); |
| 5610 | |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 5611 | camera->aspectRatio = aspectRatio; |
| 5612 | camera->zfar = zfar; |
| 5613 | camera->yfov = yfov; |
| 5614 | camera->znear = znear; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5615 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5616 | ParseExtrasAndExtensions(camera, err, o, |
| 5617 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5618 | |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5619 | // TODO(syoyo): Validate parameter values. |
| 5620 | |
| 5621 | return true; |
| 5622 | } |
| 5623 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5624 | static bool ParseSpotLight(SpotLight *light, std::string *err, |
| 5625 | const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5626 | bool store_original_json_for_extras_and_extensions) { |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5627 | ParseNumberProperty(&light->innerConeAngle, err, o, "innerConeAngle", false); |
| 5628 | ParseNumberProperty(&light->outerConeAngle, err, o, "outerConeAngle", false); |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 5629 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5630 | ParseExtrasAndExtensions(light, err, o, |
| 5631 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5632 | |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5633 | // TODO(syoyo): Validate parameter values. |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 5634 | |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5635 | return true; |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 5636 | } |
| 5637 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5638 | static bool ParseOrthographicCamera( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5639 | OrthographicCamera *camera, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5640 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5641 | double xmag = 0.0; |
| 5642 | if (!ParseNumberProperty(&xmag, err, o, "xmag", true, "OrthographicCamera")) { |
| 5643 | return false; |
| 5644 | } |
| 5645 | |
| 5646 | double ymag = 0.0; |
| 5647 | if (!ParseNumberProperty(&ymag, err, o, "ymag", true, "OrthographicCamera")) { |
| 5648 | return false; |
| 5649 | } |
| 5650 | |
| 5651 | double zfar = 0.0; |
| 5652 | if (!ParseNumberProperty(&zfar, err, o, "zfar", true, "OrthographicCamera")) { |
| 5653 | return false; |
| 5654 | } |
| 5655 | |
| 5656 | double znear = 0.0; |
| 5657 | if (!ParseNumberProperty(&znear, err, o, "znear", true, |
| 5658 | "OrthographicCamera")) { |
| 5659 | return false; |
| 5660 | } |
| 5661 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5662 | ParseExtrasAndExtensions(camera, err, o, |
| 5663 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5664 | |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 5665 | camera->xmag = xmag; |
| 5666 | camera->ymag = ymag; |
| 5667 | camera->zfar = zfar; |
| 5668 | camera->znear = znear; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5669 | |
| 5670 | // TODO(syoyo): Validate parameter values. |
| 5671 | |
| 5672 | return true; |
| 5673 | } |
| 5674 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5675 | static bool ParseCamera(Camera *camera, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5676 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5677 | if (!ParseStringProperty(&camera->type, err, o, "type", true, "Camera")) { |
| 5678 | return false; |
| 5679 | } |
| 5680 | |
| 5681 | if (camera->type.compare("orthographic") == 0) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5682 | detail::json_const_iterator orthoIt; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5683 | if (!detail::FindMember(o, "orthographic", orthoIt)) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5684 | if (err) { |
| 5685 | std::stringstream ss; |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 5686 | ss << "Orthographic camera description not found." << std::endl; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5687 | (*err) += ss.str(); |
| 5688 | } |
| 5689 | return false; |
| 5690 | } |
| 5691 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5692 | const detail::json &v = detail::GetValue(orthoIt); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5693 | if (!detail::IsObject(v)) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5694 | if (err) { |
| 5695 | std::stringstream ss; |
| 5696 | ss << "\"orthographic\" is not a JSON object." << std::endl; |
| 5697 | (*err) += ss.str(); |
| 5698 | } |
| 5699 | return false; |
| 5700 | } |
| 5701 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5702 | if (!ParseOrthographicCamera( |
| 5703 | &camera->orthographic, err, v, |
| 5704 | store_original_json_for_extras_and_extensions)) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5705 | return false; |
| 5706 | } |
| 5707 | } else if (camera->type.compare("perspective") == 0) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5708 | detail::json_const_iterator perspIt; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5709 | if (!detail::FindMember(o, "perspective", perspIt)) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5710 | if (err) { |
| 5711 | std::stringstream ss; |
| 5712 | ss << "Perspective camera description not found." << std::endl; |
| 5713 | (*err) += ss.str(); |
| 5714 | } |
| 5715 | return false; |
| 5716 | } |
| 5717 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5718 | const detail::json &v = detail::GetValue(perspIt); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5719 | if (!detail::IsObject(v)) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5720 | if (err) { |
| 5721 | std::stringstream ss; |
| 5722 | ss << "\"perspective\" is not a JSON object." << std::endl; |
| 5723 | (*err) += ss.str(); |
| 5724 | } |
| 5725 | return false; |
| 5726 | } |
| 5727 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5728 | if (!ParsePerspectiveCamera( |
| 5729 | &camera->perspective, err, v, |
| 5730 | store_original_json_for_extras_and_extensions)) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5731 | return false; |
| 5732 | } |
| 5733 | } else { |
| 5734 | if (err) { |
| 5735 | std::stringstream ss; |
| 5736 | ss << "Invalid camera type: \"" << camera->type |
| 5737 | << "\". Must be \"perspective\" or \"orthographic\"" << std::endl; |
| 5738 | (*err) += ss.str(); |
| 5739 | } |
| 5740 | return false; |
| 5741 | } |
| 5742 | |
| 5743 | ParseStringProperty(&camera->name, err, o, "name", false); |
| 5744 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5745 | ParseExtrasAndExtensions(camera, err, o, |
| 5746 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5747 | |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5748 | return true; |
| 5749 | } |
| 5750 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5751 | static bool ParseLight(Light *light, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5752 | bool store_original_json_for_extras_and_extensions) { |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5753 | if (!ParseStringProperty(&light->type, err, o, "type", true)) { |
| 5754 | return false; |
| 5755 | } |
| 5756 | |
| 5757 | if (light->type == "spot") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5758 | detail::json_const_iterator spotIt; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5759 | if (!detail::FindMember(o, "spot", spotIt)) { |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5760 | if (err) { |
| 5761 | std::stringstream ss; |
| 5762 | ss << "Spot light description not found." << std::endl; |
| 5763 | (*err) += ss.str(); |
| 5764 | } |
| 5765 | return false; |
Benjamin Schmithüsen | 4557b6a | 2019-07-09 16:55:55 +0200 | [diff] [blame] | 5766 | } |
| 5767 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5768 | const detail::json &v = detail::GetValue(spotIt); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5769 | if (!detail::IsObject(v)) { |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5770 | if (err) { |
| 5771 | std::stringstream ss; |
| 5772 | ss << "\"spot\" is not a JSON object." << std::endl; |
| 5773 | (*err) += ss.str(); |
| 5774 | } |
| 5775 | return false; |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 5776 | } |
| 5777 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5778 | if (!ParseSpotLight(&light->spot, err, v, |
| 5779 | store_original_json_for_extras_and_extensions)) { |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5780 | return false; |
| 5781 | } |
| 5782 | } |
| 5783 | |
| 5784 | ParseStringProperty(&light->name, err, o, "name", false); |
| 5785 | ParseNumberArrayProperty(&light->color, err, o, "color", false); |
| 5786 | ParseNumberProperty(&light->range, err, o, "range", false); |
| 5787 | ParseNumberProperty(&light->intensity, err, o, "intensity", false); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5788 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5789 | ParseExtrasAndExtensions(light, err, o, |
| 5790 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5791 | |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5792 | return true; |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 5793 | } |
| 5794 | |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 5795 | static bool ParsePositionalEmitter( |
| 5796 | PositionalEmitter *positional, std::string *err, const detail::json &o, |
| 5797 | bool store_original_json_for_extras_and_extensions) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5798 | ParseNumberProperty(&positional->coneInnerAngle, err, o, "coneInnerAngle", |
| 5799 | false); |
| 5800 | ParseNumberProperty(&positional->coneOuterAngle, err, o, "coneOuterAngle", |
| 5801 | false); |
| 5802 | ParseNumberProperty(&positional->coneOuterGain, err, o, "coneOuterGain", |
| 5803 | false); |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 5804 | ParseNumberProperty(&positional->maxDistance, err, o, "maxDistance", false); |
| 5805 | ParseNumberProperty(&positional->refDistance, err, o, "refDistance", false); |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5806 | ParseNumberProperty(&positional->rolloffFactor, err, o, "rolloffFactor", |
| 5807 | false); |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 5808 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5809 | ParseExtrasAndExtensions(positional, err, o, |
| 5810 | store_original_json_for_extras_and_extensions); |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 5811 | |
| 5812 | return true; |
| 5813 | } |
| 5814 | |
| 5815 | static bool ParseAudioEmitter( |
| 5816 | AudioEmitter *emitter, std::string *err, const detail::json &o, |
| 5817 | bool store_original_json_for_extras_and_extensions) { |
| 5818 | if (!ParseStringProperty(&emitter->type, err, o, "type", true)) { |
| 5819 | return false; |
| 5820 | } |
| 5821 | |
| 5822 | if (emitter->type == "positional") { |
| 5823 | detail::json_const_iterator positionalIt; |
| 5824 | if (!detail::FindMember(o, "positional", positionalIt)) { |
| 5825 | if (err) { |
| 5826 | std::stringstream ss; |
| 5827 | ss << "Positional emitter description not found." << std::endl; |
| 5828 | (*err) += ss.str(); |
| 5829 | } |
| 5830 | return false; |
| 5831 | } |
| 5832 | |
| 5833 | const detail::json &v = detail::GetValue(positionalIt); |
| 5834 | if (!detail::IsObject(v)) { |
| 5835 | if (err) { |
| 5836 | std::stringstream ss; |
| 5837 | ss << "\"positional\" is not a JSON object." << std::endl; |
| 5838 | (*err) += ss.str(); |
| 5839 | } |
| 5840 | return false; |
| 5841 | } |
| 5842 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5843 | if (!ParsePositionalEmitter( |
| 5844 | &emitter->positional, err, v, |
| 5845 | store_original_json_for_extras_and_extensions)) { |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 5846 | return false; |
| 5847 | } |
| 5848 | } |
| 5849 | |
| 5850 | ParseStringProperty(&emitter->name, err, o, "name", false); |
| 5851 | ParseNumberProperty(&emitter->gain, err, o, "gain", false); |
| 5852 | ParseBooleanProperty(&emitter->loop, err, o, "loop", false); |
| 5853 | ParseBooleanProperty(&emitter->playing, err, o, "playing", false); |
| 5854 | ParseStringProperty(&emitter->distanceModel, err, o, "distanceModel", false); |
| 5855 | ParseIntegerProperty(&emitter->source, err, o, "source", true); |
| 5856 | |
| 5857 | ParseExtrasAndExtensions(emitter, err, o, |
| 5858 | store_original_json_for_extras_and_extensions); |
| 5859 | |
| 5860 | return true; |
| 5861 | } |
| 5862 | |
| 5863 | static bool ParseAudioSource( |
| 5864 | AudioSource *source, std::string *err, const detail::json &o, |
| 5865 | bool store_original_json_for_extras_and_extensions) { |
| 5866 | ParseStringProperty(&source->name, err, o, "name", false); |
| 5867 | ParseStringProperty(&source->uri, err, o, "uri", false); |
| 5868 | |
| 5869 | if (source->uri.empty()) { |
| 5870 | ParseIntegerProperty(&source->bufferView, err, o, "bufferView", true); |
| 5871 | ParseStringProperty(&source->mimeType, err, o, "mimeType", true); |
| 5872 | } |
| 5873 | |
| 5874 | ParseExtrasAndExtensions(source, err, o, |
| 5875 | store_original_json_for_extras_and_extensions); |
| 5876 | |
| 5877 | return true; |
| 5878 | } |
| 5879 | |
David Siegel | bec8a6d | 2023-06-06 15:36:07 +0200 | [diff] [blame] | 5880 | namespace detail { |
| 5881 | |
| 5882 | template <typename Callback> |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5883 | bool ForEachInArray(const detail::json &_v, const char *member, Callback &&cb) { |
David Siegel | bec8a6d | 2023-06-06 15:36:07 +0200 | [diff] [blame] | 5884 | detail::json_const_iterator itm; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5885 | if (detail::FindMember(_v, member, itm) && |
| 5886 | detail::IsArray(detail::GetValue(itm))) { |
David Siegel | bec8a6d | 2023-06-06 15:36:07 +0200 | [diff] [blame] | 5887 | const detail::json &root = detail::GetValue(itm); |
| 5888 | auto it = detail::ArrayBegin(root); |
| 5889 | auto end = detail::ArrayEnd(root); |
| 5890 | for (; it != end; ++it) { |
| 5891 | if (!cb(*it)) return false; |
| 5892 | } |
| 5893 | } |
| 5894 | return true; |
| 5895 | }; |
| 5896 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5897 | } // end of namespace detail |
David Siegel | bec8a6d | 2023-06-06 15:36:07 +0200 | [diff] [blame] | 5898 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 5899 | bool TinyGLTF::LoadFromString(Model *model, std::string *err, std::string *warn, |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5900 | const char *json_str, |
| 5901 | unsigned int json_str_length, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 5902 | const std::string &base_dir, |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 5903 | unsigned int check_sections) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5904 | if (json_str_length < 4) { |
Syoyo Fujita | 2b0307f | 2017-08-01 18:15:52 +0900 | [diff] [blame] | 5905 | if (err) { |
| 5906 | (*err) = "JSON string too short.\n"; |
| 5907 | } |
| 5908 | return false; |
| 5909 | } |
| 5910 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5911 | detail::JsonDocument v; |
Syoyo Fujita | 17cfbcc | 2018-01-05 20:13:50 +0900 | [diff] [blame] | 5912 | |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 5913 | #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || \ |
| 5914 | defined(_CPPUNWIND)) && \ |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 5915 | !defined(TINYGLTF_NOEXCEPTION) |
Syoyo Fujita | cdf8490 | 2017-08-01 20:12:08 +0900 | [diff] [blame] | 5916 | try { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5917 | detail::JsonParse(v, json_str, json_str_length, true); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5918 | |
Syoyo Fujita | 17cfbcc | 2018-01-05 20:13:50 +0900 | [diff] [blame] | 5919 | } catch (const std::exception &e) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5920 | if (err) { |
Syoyo Fujita | cdf8490 | 2017-08-01 20:12:08 +0900 | [diff] [blame] | 5921 | (*err) = e.what(); |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5922 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5923 | return false; |
| 5924 | } |
Syoyo Fujita | 17cfbcc | 2018-01-05 20:13:50 +0900 | [diff] [blame] | 5925 | #else |
| 5926 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5927 | detail::JsonParse(v, json_str, json_str_length); |
Syoyo Fujita | 17cfbcc | 2018-01-05 20:13:50 +0900 | [diff] [blame] | 5928 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5929 | if (!detail::IsObject(v)) { |
Syoyo Fujita | 17cfbcc | 2018-01-05 20:13:50 +0900 | [diff] [blame] | 5930 | // Assume parsing was failed. |
| 5931 | if (err) { |
| 5932 | (*err) = "Failed to parse JSON object\n"; |
| 5933 | } |
| 5934 | return false; |
| 5935 | } |
| 5936 | } |
| 5937 | #endif |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5938 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5939 | if (!detail::IsObject(v)) { |
Syoyo Fujita | 2b0307f | 2017-08-01 18:15:52 +0900 | [diff] [blame] | 5940 | // root is not an object. |
| 5941 | if (err) { |
| 5942 | (*err) = "Root element is not a JSON object\n"; |
| 5943 | } |
| 5944 | return false; |
| 5945 | } |
| 5946 | |
Benjamin Schmithüsen | d02ad0d | 2019-05-02 14:44:20 +0200 | [diff] [blame] | 5947 | { |
| 5948 | bool version_found = false; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5949 | detail::json_const_iterator it; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5950 | if (detail::FindMember(v, "asset", it) && |
| 5951 | detail::IsObject(detail::GetValue(it))) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5952 | auto &itObj = detail::GetValue(it); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5953 | detail::json_const_iterator version_it; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5954 | std::string versionStr; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5955 | if (detail::FindMember(itObj, "version", version_it) && |
| 5956 | detail::GetString(detail::GetValue(version_it), versionStr)) { |
Benjamin Schmithüsen | d02ad0d | 2019-05-02 14:44:20 +0200 | [diff] [blame] | 5957 | version_found = true; |
| 5958 | } |
| 5959 | } |
| 5960 | if (version_found) { |
| 5961 | // OK |
| 5962 | } else if (check_sections & REQUIRE_VERSION) { |
| 5963 | if (err) { |
| 5964 | (*err) += "\"asset\" object not found in .gltf or not an object type\n"; |
| 5965 | } |
| 5966 | return false; |
| 5967 | } |
| 5968 | } |
| 5969 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 5970 | // scene is not mandatory. |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 5971 | // FIXME Maybe a better way to handle it than removing the code |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5972 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5973 | auto IsArrayMemberPresent = [](const detail::json &_v, |
| 5974 | const char *name) -> bool { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5975 | detail::json_const_iterator it; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 5976 | return detail::FindMember(_v, name, it) && |
| 5977 | detail::IsArray(detail::GetValue(it)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5978 | }; |
| 5979 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5980 | { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5981 | if ((check_sections & REQUIRE_SCENES) && |
| 5982 | !IsArrayMemberPresent(v, "scenes")) { |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5983 | if (err) { |
| 5984 | (*err) += "\"scenes\" object not found in .gltf or not an array type\n"; |
| 5985 | } |
| 5986 | return false; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5987 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5988 | } |
| 5989 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5990 | { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5991 | if ((check_sections & REQUIRE_NODES) && !IsArrayMemberPresent(v, "nodes")) { |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5992 | if (err) { |
| 5993 | (*err) += "\"nodes\" object not found in .gltf\n"; |
| 5994 | } |
| 5995 | return false; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5996 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5997 | } |
| 5998 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5999 | { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6000 | if ((check_sections & REQUIRE_ACCESSORS) && |
| 6001 | !IsArrayMemberPresent(v, "accessors")) { |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6002 | if (err) { |
| 6003 | (*err) += "\"accessors\" object not found in .gltf\n"; |
| 6004 | } |
| 6005 | return false; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 6006 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 6007 | } |
| 6008 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6009 | { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6010 | if ((check_sections & REQUIRE_BUFFERS) && |
| 6011 | !IsArrayMemberPresent(v, "buffers")) { |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6012 | if (err) { |
| 6013 | (*err) += "\"buffers\" object not found in .gltf\n"; |
| 6014 | } |
| 6015 | return false; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 6016 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 6017 | } |
| 6018 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6019 | { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6020 | if ((check_sections & REQUIRE_BUFFER_VIEWS) && |
| 6021 | !IsArrayMemberPresent(v, "bufferViews")) { |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6022 | if (err) { |
| 6023 | (*err) += "\"bufferViews\" object not found in .gltf\n"; |
| 6024 | } |
| 6025 | return false; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 6026 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 6027 | } |
| 6028 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 6029 | model->buffers.clear(); |
| 6030 | model->bufferViews.clear(); |
| 6031 | model->accessors.clear(); |
| 6032 | model->meshes.clear(); |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 6033 | model->cameras.clear(); |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 6034 | model->nodes.clear(); |
| 6035 | model->extensionsUsed.clear(); |
Aurélien Chatelain | 756ee6b | 2017-06-19 13:52:49 +0000 | [diff] [blame] | 6036 | model->extensionsRequired.clear(); |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 6037 | model->extensions.clear(); |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 6038 | model->defaultScene = -1; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 6039 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6040 | // 1. Parse Asset |
| 6041 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6042 | detail::json_const_iterator it; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6043 | if (detail::FindMember(v, "asset", it) && |
| 6044 | detail::IsObject(detail::GetValue(it))) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6045 | const detail::json &root = detail::GetValue(it); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 6046 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6047 | ParseAsset(&model->asset, err, root, |
| 6048 | store_original_json_for_extras_and_extensions_); |
Aurélien Chatelain | 756ee6b | 2017-06-19 13:52:49 +0000 | [diff] [blame] | 6049 | } |
| 6050 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 6051 | |
David Siegel | bec8a6d | 2023-06-06 15:36:07 +0200 | [diff] [blame] | 6052 | using detail::ForEachInArray; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6053 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6054 | // 2. Parse extensionUsed |
| 6055 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6056 | ForEachInArray(v, "extensionsUsed", [&](const detail::json &o) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6057 | std::string str; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6058 | detail::GetString(o, str); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6059 | model->extensionsUsed.emplace_back(std::move(str)); |
| 6060 | return true; |
| 6061 | }); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 6062 | } |
| 6063 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6064 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6065 | ForEachInArray(v, "extensionsRequired", [&](const detail::json &o) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6066 | std::string str; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6067 | detail::GetString(o, str); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6068 | model->extensionsRequired.emplace_back(std::move(str)); |
| 6069 | return true; |
| 6070 | }); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 6071 | } |
| 6072 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6073 | // 3. Parse Buffer |
| 6074 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6075 | bool success = ForEachInArray(v, "buffers", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6076 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6077 | if (err) { |
| 6078 | (*err) += "`buffers' does not contain an JSON object."; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 6079 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6080 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6081 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6082 | Buffer buffer; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6083 | if (!ParseBuffer(&buffer, err, o, |
| 6084 | store_original_json_for_extras_and_extensions_, &fs, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6085 | &uri_cb, base_dir, max_external_file_size_, is_binary_, |
| 6086 | bin_data_, bin_size_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6087 | return false; |
| 6088 | } |
| 6089 | |
| 6090 | model->buffers.emplace_back(std::move(buffer)); |
| 6091 | return true; |
| 6092 | }); |
| 6093 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6094 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6095 | return false; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 6096 | } |
| 6097 | } |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6098 | // 4. Parse BufferView |
| 6099 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6100 | bool success = ForEachInArray(v, "bufferViews", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6101 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6102 | if (err) { |
| 6103 | (*err) += "`bufferViews' does not contain an JSON object."; |
Syoyo Fujita | 59130d1 | 2017-08-01 20:23:11 +0900 | [diff] [blame] | 6104 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6105 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6106 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6107 | BufferView bufferView; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6108 | if (!ParseBufferView(&bufferView, err, o, |
| 6109 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6110 | return false; |
| 6111 | } |
| 6112 | |
| 6113 | model->bufferViews.emplace_back(std::move(bufferView)); |
| 6114 | return true; |
| 6115 | }); |
| 6116 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6117 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6118 | return false; |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 6119 | } |
| 6120 | } |
| 6121 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6122 | // 5. Parse Accessor |
| 6123 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6124 | bool success = ForEachInArray(v, "accessors", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6125 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6126 | if (err) { |
| 6127 | (*err) += "`accessors' does not contain an JSON object."; |
Syoyo Fujita | 59130d1 | 2017-08-01 20:23:11 +0900 | [diff] [blame] | 6128 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6129 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6130 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6131 | Accessor accessor; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6132 | if (!ParseAccessor(&accessor, err, o, |
| 6133 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6134 | return false; |
| 6135 | } |
| 6136 | |
| 6137 | model->accessors.emplace_back(std::move(accessor)); |
| 6138 | return true; |
| 6139 | }); |
| 6140 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6141 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6142 | return false; |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 6143 | } |
| 6144 | } |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 6145 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6146 | // 6. Parse Mesh |
| 6147 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6148 | bool success = ForEachInArray(v, "meshes", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6149 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6150 | if (err) { |
| 6151 | (*err) += "`meshes' does not contain an JSON object."; |
Syoyo Fujita | 59130d1 | 2017-08-01 20:23:11 +0900 | [diff] [blame] | 6152 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6153 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6154 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6155 | Mesh mesh; |
Nyall Dawson | 6e3d666 | 2023-08-24 13:25:34 +1000 | [diff] [blame] | 6156 | if (!ParseMesh(&mesh, model, err, warn, o, |
| 6157 | store_original_json_for_extras_and_extensions_, |
| 6158 | strictness_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6159 | return false; |
| 6160 | } |
| 6161 | |
| 6162 | model->meshes.emplace_back(std::move(mesh)); |
| 6163 | return true; |
| 6164 | }); |
| 6165 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6166 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6167 | return false; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 6168 | } |
| 6169 | } |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 6170 | |
viperscape | 9df0580 | 2018-12-05 14:11:01 -0500 | [diff] [blame] | 6171 | // Assign missing bufferView target types |
| 6172 | // - Look for missing Mesh indices |
Marcin Kacprzak | d09788d | 2020-01-02 13:00:48 +0100 | [diff] [blame] | 6173 | // - Look for missing Mesh attributes |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 6174 | for (auto &mesh : model->meshes) { |
| 6175 | for (auto &primitive : mesh.primitives) { |
| 6176 | if (primitive.indices > |
| 6177 | -1) // has indices from parsing step, must be Element Array Buffer |
viperscape | 9df0580 | 2018-12-05 14:11:01 -0500 | [diff] [blame] | 6178 | { |
Jeff McGlynn | 8915252 | 2019-04-25 16:33:56 -0700 | [diff] [blame] | 6179 | if (size_t(primitive.indices) >= model->accessors.size()) { |
| 6180 | if (err) { |
| 6181 | (*err) += "primitive indices accessor out of bounds"; |
| 6182 | } |
| 6183 | return false; |
| 6184 | } |
| 6185 | |
haroonq | 8098a9e | 2023-10-05 11:16:52 +0100 | [diff] [blame] | 6186 | const auto bufferView = |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 6187 | model->accessors[size_t(primitive.indices)].bufferView; |
haroonq | 8098a9e | 2023-10-05 11:16:52 +0100 | [diff] [blame] | 6188 | if (bufferView < 0) { |
| 6189 | // skip, bufferView could be null(-1) for certain extensions |
Syoyo Fujita | 8387fdb | 2023-12-04 22:50:49 +0900 | [diff] [blame] | 6190 | } else if (size_t(bufferView) >= model->bufferViews.size()) { |
Jeff McGlynn | 8915252 | 2019-04-25 16:33:56 -0700 | [diff] [blame] | 6191 | if (err) { |
| 6192 | (*err) += "accessor[" + std::to_string(primitive.indices) + |
| 6193 | "] invalid bufferView"; |
| 6194 | } |
| 6195 | return false; |
haroonq | 8098a9e | 2023-10-05 11:16:52 +0100 | [diff] [blame] | 6196 | } else { |
| 6197 | model->bufferViews[size_t(bufferView)].target = |
| 6198 | TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER; |
| 6199 | // we could optionally check if accessors' bufferView type is Scalar, as |
| 6200 | // it should be |
Jeff McGlynn | 8915252 | 2019-04-25 16:33:56 -0700 | [diff] [blame] | 6201 | } |
viperscape | 9df0580 | 2018-12-05 14:11:01 -0500 | [diff] [blame] | 6202 | } |
Marcin Kacprzak | d09788d | 2020-01-02 13:00:48 +0100 | [diff] [blame] | 6203 | |
| 6204 | for (auto &attribute : primitive.attributes) { |
Nirmal Patel | e413216 | 2022-09-06 09:16:31 -0700 | [diff] [blame] | 6205 | const auto accessorsIndex = size_t(attribute.second); |
| 6206 | if (accessorsIndex < model->accessors.size()) { |
| 6207 | const auto bufferView = model->accessors[accessorsIndex].bufferView; |
| 6208 | // bufferView could be null(-1) for sparse morph target |
imallett | 56e1098 | 2022-10-07 10:35:16 -0700 | [diff] [blame] | 6209 | if (bufferView >= 0 && bufferView < (int)model->bufferViews.size()) { |
Nirmal Patel | e413216 | 2022-09-06 09:16:31 -0700 | [diff] [blame] | 6210 | model->bufferViews[size_t(bufferView)].target = |
| 6211 | TINYGLTF_TARGET_ARRAY_BUFFER; |
| 6212 | } |
| 6213 | } |
Marcin Kacprzak | d09788d | 2020-01-02 13:00:48 +0100 | [diff] [blame] | 6214 | } |
Marcin Kacprzak | c3d6716 | 2020-01-22 13:13:35 +0100 | [diff] [blame] | 6215 | |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 6216 | for (auto &target : primitive.targets) { |
| 6217 | for (auto &attribute : target) { |
Nirmal Patel | e413216 | 2022-09-06 09:16:31 -0700 | [diff] [blame] | 6218 | const auto accessorsIndex = size_t(attribute.second); |
| 6219 | if (accessorsIndex < model->accessors.size()) { |
| 6220 | const auto bufferView = model->accessors[accessorsIndex].bufferView; |
| 6221 | // bufferView could be null(-1) for sparse morph target |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6222 | if (bufferView >= 0 && |
| 6223 | bufferView < (int)model->bufferViews.size()) { |
Nirmal Patel | e413216 | 2022-09-06 09:16:31 -0700 | [diff] [blame] | 6224 | model->bufferViews[size_t(bufferView)].target = |
| 6225 | TINYGLTF_TARGET_ARRAY_BUFFER; |
| 6226 | } |
Rahul Sheth | 125e4a2 | 2020-07-13 13:56:50 -0400 | [diff] [blame] | 6227 | } |
Marcin Kacprzak | c3d6716 | 2020-01-22 13:13:35 +0100 | [diff] [blame] | 6228 | } |
| 6229 | } |
viperscape | 9df0580 | 2018-12-05 14:11:01 -0500 | [diff] [blame] | 6230 | } |
| 6231 | } |
| 6232 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6233 | // 7. Parse Node |
| 6234 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6235 | bool success = ForEachInArray(v, "nodes", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6236 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6237 | if (err) { |
| 6238 | (*err) += "`nodes' does not contain an JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6239 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6240 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6241 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6242 | Node node; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6243 | if (!ParseNode(&node, err, o, |
| 6244 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6245 | return false; |
| 6246 | } |
| 6247 | |
| 6248 | model->nodes.emplace_back(std::move(node)); |
| 6249 | return true; |
| 6250 | }); |
| 6251 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6252 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6253 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6254 | } |
| 6255 | } |
| 6256 | |
| 6257 | // 8. Parse scenes. |
| 6258 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6259 | bool success = ForEachInArray(v, "scenes", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6260 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6261 | if (err) { |
| 6262 | (*err) += "`scenes' does not contain an JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6263 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6264 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6265 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6266 | |
| 6267 | Scene scene; |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 6268 | if (!ParseScene(&scene, err, o, |
| 6269 | store_original_json_for_extras_and_extensions_)) { |
| 6270 | return false; |
| 6271 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6272 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6273 | model->scenes.emplace_back(std::move(scene)); |
| 6274 | return true; |
| 6275 | }); |
| 6276 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6277 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6278 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6279 | } |
| 6280 | } |
| 6281 | |
| 6282 | // 9. Parse default scenes. |
| 6283 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6284 | detail::json_const_iterator rootIt; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6285 | int iVal; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6286 | if (detail::FindMember(v, "scene", rootIt) && |
| 6287 | detail::GetInt(detail::GetValue(rootIt), iVal)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6288 | model->defaultScene = iVal; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6289 | } |
| 6290 | } |
| 6291 | |
| 6292 | // 10. Parse Material |
| 6293 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6294 | bool success = ForEachInArray(v, "materials", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6295 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6296 | if (err) { |
| 6297 | (*err) += "`materials' does not contain an JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6298 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6299 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6300 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6301 | Material material; |
| 6302 | ParseStringProperty(&material.name, err, o, "name", false); |
| 6303 | |
Nyall Dawson | 02e8b8d | 2023-08-24 10:25:20 +1000 | [diff] [blame] | 6304 | if (!ParseMaterial(&material, err, warn, o, |
Nyall Dawson | 8c85d5e | 2023-08-28 12:56:09 +1000 | [diff] [blame] | 6305 | store_original_json_for_extras_and_extensions_, |
| 6306 | strictness_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6307 | return false; |
| 6308 | } |
| 6309 | |
| 6310 | model->materials.emplace_back(std::move(material)); |
| 6311 | return true; |
| 6312 | }); |
| 6313 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6314 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6315 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6316 | } |
| 6317 | } |
| 6318 | |
| 6319 | // 11. Parse Image |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 6320 | void *load_image_user_data{nullptr}; |
| 6321 | |
| 6322 | LoadImageDataOption load_image_option; |
| 6323 | |
| 6324 | if (user_image_loader_) { |
| 6325 | // Use user supplied pointer |
| 6326 | load_image_user_data = load_image_user_data_; |
| 6327 | } else { |
| 6328 | load_image_option.preserve_channels = preserve_image_channels_; |
| 6329 | load_image_user_data = reinterpret_cast<void *>(&load_image_option); |
| 6330 | } |
| 6331 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6332 | { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6333 | int idx = 0; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6334 | bool success = ForEachInArray(v, "images", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6335 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6336 | if (err) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6337 | (*err) += "image[" + std::to_string(idx) + "] is not a JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6338 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6339 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6340 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6341 | Image image; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6342 | if (!ParseImage(&image, idx, err, warn, o, |
| 6343 | store_original_json_for_extras_and_extensions_, base_dir, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6344 | max_external_file_size_, &fs, &uri_cb, |
| 6345 | &this->LoadImageData, load_image_user_data)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6346 | return false; |
| 6347 | } |
| 6348 | |
| 6349 | if (image.bufferView != -1) { |
| 6350 | // Load image from the buffer view. |
| 6351 | if (size_t(image.bufferView) >= model->bufferViews.size()) { |
| 6352 | if (err) { |
| 6353 | std::stringstream ss; |
| 6354 | ss << "image[" << idx << "] bufferView \"" << image.bufferView |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6355 | << "\" not found in the scene." << std::endl; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6356 | (*err) += ss.str(); |
| 6357 | } |
| 6358 | return false; |
| 6359 | } |
| 6360 | |
| 6361 | const BufferView &bufferView = |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6362 | model->bufferViews[size_t(image.bufferView)]; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6363 | if (size_t(bufferView.buffer) >= model->buffers.size()) { |
| 6364 | if (err) { |
| 6365 | std::stringstream ss; |
| 6366 | ss << "image[" << idx << "] buffer \"" << bufferView.buffer |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6367 | << "\" not found in the scene." << std::endl; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6368 | (*err) += ss.str(); |
| 6369 | } |
| 6370 | return false; |
| 6371 | } |
| 6372 | const Buffer &buffer = model->buffers[size_t(bufferView.buffer)]; |
| 6373 | |
| 6374 | if (*LoadImageData == nullptr) { |
| 6375 | if (err) { |
| 6376 | (*err) += "No LoadImageData callback specified.\n"; |
| 6377 | } |
| 6378 | return false; |
| 6379 | } |
| 6380 | bool ret = LoadImageData( |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6381 | &image, idx, err, warn, image.width, image.height, |
| 6382 | &buffer.data[bufferView.byteOffset], |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 6383 | static_cast<int>(bufferView.byteLength), load_image_user_data); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6384 | if (!ret) { |
| 6385 | return false; |
| 6386 | } |
| 6387 | } |
| 6388 | |
| 6389 | model->images.emplace_back(std::move(image)); |
| 6390 | ++idx; |
| 6391 | return true; |
| 6392 | }); |
| 6393 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6394 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6395 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6396 | } |
| 6397 | } |
| 6398 | |
| 6399 | // 12. Parse Texture |
| 6400 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6401 | bool success = ForEachInArray(v, "textures", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6402 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6403 | if (err) { |
| 6404 | (*err) += "`textures' does not contain an JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6405 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6406 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6407 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6408 | Texture texture; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6409 | if (!ParseTexture(&texture, err, o, |
| 6410 | store_original_json_for_extras_and_extensions_, |
| 6411 | base_dir)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6412 | return false; |
| 6413 | } |
| 6414 | |
| 6415 | model->textures.emplace_back(std::move(texture)); |
| 6416 | return true; |
| 6417 | }); |
| 6418 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6419 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6420 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6421 | } |
| 6422 | } |
| 6423 | |
| 6424 | // 13. Parse Animation |
| 6425 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6426 | bool success = ForEachInArray(v, "animations", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6427 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6428 | if (err) { |
| 6429 | (*err) += "`animations' does not contain an JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6430 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6431 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6432 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6433 | Animation animation; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6434 | if (!ParseAnimation(&animation, err, o, |
| 6435 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6436 | return false; |
| 6437 | } |
| 6438 | |
| 6439 | model->animations.emplace_back(std::move(animation)); |
| 6440 | return true; |
| 6441 | }); |
| 6442 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6443 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6444 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6445 | } |
| 6446 | } |
| 6447 | |
| 6448 | // 14. Parse Skin |
| 6449 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6450 | bool success = ForEachInArray(v, "skins", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6451 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6452 | if (err) { |
| 6453 | (*err) += "`skins' does not contain an JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6454 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6455 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6456 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6457 | Skin skin; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6458 | if (!ParseSkin(&skin, err, o, |
| 6459 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6460 | return false; |
| 6461 | } |
| 6462 | |
| 6463 | model->skins.emplace_back(std::move(skin)); |
| 6464 | return true; |
| 6465 | }); |
| 6466 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6467 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6468 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6469 | } |
| 6470 | } |
| 6471 | |
| 6472 | // 15. Parse Sampler |
| 6473 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6474 | bool success = ForEachInArray(v, "samplers", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6475 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6476 | if (err) { |
| 6477 | (*err) += "`samplers' does not contain an JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6478 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6479 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6480 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6481 | Sampler sampler; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6482 | if (!ParseSampler(&sampler, err, o, |
| 6483 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6484 | return false; |
| 6485 | } |
| 6486 | |
| 6487 | model->samplers.emplace_back(std::move(sampler)); |
| 6488 | return true; |
| 6489 | }); |
| 6490 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6491 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6492 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6493 | } |
| 6494 | } |
| 6495 | |
| 6496 | // 16. Parse Camera |
| 6497 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6498 | bool success = ForEachInArray(v, "cameras", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6499 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6500 | if (err) { |
| 6501 | (*err) += "`cameras' does not contain an JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6502 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6503 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6504 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6505 | Camera camera; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6506 | if (!ParseCamera(&camera, err, o, |
| 6507 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6508 | return false; |
| 6509 | } |
| 6510 | |
| 6511 | model->cameras.emplace_back(std::move(camera)); |
| 6512 | return true; |
| 6513 | }); |
| 6514 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6515 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6516 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6517 | } |
| 6518 | } |
| 6519 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 6520 | // 17. Parse Extras & Extensions |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6521 | ParseExtrasAndExtensions(model, err, v, |
| 6522 | store_original_json_for_extras_and_extensions_); |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 6523 | |
| 6524 | // 18. Specific extension implementations |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6525 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6526 | detail::json_const_iterator rootIt; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6527 | if (detail::FindMember(v, "extensions", rootIt) && |
| 6528 | detail::IsObject(detail::GetValue(rootIt))) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6529 | const detail::json &root = detail::GetValue(rootIt); |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6530 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6531 | detail::json_const_iterator it(detail::ObjectBegin(root)); |
| 6532 | detail::json_const_iterator itEnd(detail::ObjectEnd(root)); |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6533 | for (; it != itEnd; ++it) { |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 6534 | // parse KHR_lights_punctual extension |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6535 | std::string key(detail::GetKey(it)); |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6536 | if ((key == "KHR_lights_punctual") && |
| 6537 | detail::IsObject(detail::GetValue(it))) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6538 | const detail::json &object = detail::GetValue(it); |
| 6539 | detail::json_const_iterator itLight; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6540 | if (detail::FindMember(object, "lights", itLight)) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6541 | const detail::json &lights = detail::GetValue(itLight); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6542 | if (!detail::IsArray(lights)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6543 | continue; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6544 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6545 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6546 | auto arrayIt(detail::ArrayBegin(lights)); |
| 6547 | auto arrayItEnd(detail::ArrayEnd(lights)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6548 | for (; arrayIt != arrayItEnd; ++arrayIt) { |
| 6549 | Light light; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6550 | if (!ParseLight(&light, err, *arrayIt, |
| 6551 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6552 | return false; |
| 6553 | } |
| 6554 | model->lights.emplace_back(std::move(light)); |
| 6555 | } |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6556 | } |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 6557 | } |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 6558 | // parse KHR_audio extension |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6559 | if ((key == "KHR_audio") && detail::IsObject(detail::GetValue(it))) { |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 6560 | const detail::json &object = detail::GetValue(it); |
| 6561 | detail::json_const_iterator itKhrAudio; |
| 6562 | if (detail::FindMember(object, "emitters", itKhrAudio)) { |
| 6563 | const detail::json &emitters = detail::GetValue(itKhrAudio); |
| 6564 | if (!detail::IsArray(emitters)) { |
| 6565 | continue; |
| 6566 | } |
| 6567 | |
| 6568 | auto arrayIt(detail::ArrayBegin(emitters)); |
| 6569 | auto arrayItEnd(detail::ArrayEnd(emitters)); |
| 6570 | for (; arrayIt != arrayItEnd; ++arrayIt) { |
| 6571 | AudioEmitter emitter; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6572 | if (!ParseAudioEmitter( |
| 6573 | &emitter, err, *arrayIt, |
| 6574 | store_original_json_for_extras_and_extensions_)) { |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 6575 | return false; |
| 6576 | } |
| 6577 | model->audioEmitters.emplace_back(std::move(emitter)); |
| 6578 | } |
| 6579 | } |
| 6580 | |
| 6581 | if (detail::FindMember(object, "sources", itKhrAudio)) { |
| 6582 | const detail::json &sources = detail::GetValue(itKhrAudio); |
| 6583 | if (!detail::IsArray(sources)) { |
| 6584 | continue; |
| 6585 | } |
| 6586 | |
| 6587 | auto arrayIt(detail::ArrayBegin(sources)); |
| 6588 | auto arrayItEnd(detail::ArrayEnd(sources)); |
| 6589 | for (; arrayIt != arrayItEnd; ++arrayIt) { |
| 6590 | AudioSource source; |
| 6591 | if (!ParseAudioSource( |
| 6592 | &source, err, *arrayIt, |
| 6593 | store_original_json_for_extras_and_extensions_)) { |
| 6594 | return false; |
| 6595 | } |
| 6596 | model->audioSources.emplace_back(std::move(source)); |
| 6597 | } |
| 6598 | } |
| 6599 | } |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 6600 | } |
| 6601 | } |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 6602 | } |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6603 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 6604 | return true; |
| 6605 | } |
| 6606 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6607 | bool TinyGLTF::LoadASCIIFromString(Model *model, std::string *err, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6608 | std::string *warn, const char *str, |
| 6609 | unsigned int length, |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6610 | const std::string &base_dir, |
| 6611 | unsigned int check_sections) { |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6612 | is_binary_ = false; |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 6613 | bin_data_ = nullptr; |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6614 | bin_size_ = 0; |
| 6615 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6616 | return LoadFromString(model, err, warn, str, length, base_dir, |
| 6617 | check_sections); |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6618 | } |
| 6619 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6620 | bool TinyGLTF::LoadASCIIFromFile(Model *model, std::string *err, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6621 | std::string *warn, const std::string &filename, |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6622 | unsigned int check_sections) { |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 6623 | std::stringstream ss; |
| 6624 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 6625 | if (fs.ReadWholeFile == nullptr) { |
| 6626 | // Programmer error, assert() ? |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6627 | ss << "Failed to read file: " << filename |
| 6628 | << ": one or more FS callback not set" << std::endl; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 6629 | if (err) { |
| 6630 | (*err) = ss.str(); |
| 6631 | } |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 6632 | return false; |
| 6633 | } |
| 6634 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 6635 | std::vector<unsigned char> data; |
| 6636 | std::string fileerr; |
| 6637 | bool fileread = fs.ReadWholeFile(&data, &fileerr, filename, fs.user_data); |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6638 | if (!fileread) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 6639 | ss << "Failed to read file: " << filename << ": " << fileerr << std::endl; |
| 6640 | if (err) { |
| 6641 | (*err) = ss.str(); |
| 6642 | } |
| 6643 | return false; |
| 6644 | } |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 6645 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 6646 | size_t sz = data.size(); |
Luke San Antonio | b310b4a | 2016-06-23 14:17:37 -0400 | [diff] [blame] | 6647 | if (sz == 0) { |
| 6648 | if (err) { |
| 6649 | (*err) = "Empty file."; |
| 6650 | } |
| 6651 | return false; |
| 6652 | } |
| 6653 | |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 6654 | std::string basedir = GetBaseDir(filename); |
| 6655 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6656 | bool ret = LoadASCIIFromString( |
| 6657 | model, err, warn, reinterpret_cast<const char *>(&data.at(0)), |
| 6658 | static_cast<unsigned int>(data.size()), basedir, check_sections); |
Luke San Antonio | 9e36b61 | 2016-06-23 14:10:51 -0400 | [diff] [blame] | 6659 | |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6660 | return ret; |
| 6661 | } |
| 6662 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6663 | bool TinyGLTF::LoadBinaryFromMemory(Model *model, std::string *err, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6664 | std::string *warn, |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6665 | const unsigned char *bytes, |
| 6666 | unsigned int size, |
| 6667 | const std::string &base_dir, |
| 6668 | unsigned int check_sections) { |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6669 | if (size < 20) { |
| 6670 | if (err) { |
| 6671 | (*err) = "Too short data size for glTF Binary."; |
| 6672 | } |
| 6673 | return false; |
| 6674 | } |
| 6675 | |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 6676 | if (bytes[0] == 'g' && bytes[1] == 'l' && bytes[2] == 'T' && |
| 6677 | bytes[3] == 'F') { |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6678 | // ok |
| 6679 | } else { |
| 6680 | if (err) { |
| 6681 | (*err) = "Invalid magic."; |
| 6682 | } |
| 6683 | return false; |
| 6684 | } |
| 6685 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6686 | unsigned int version; // 4 bytes |
| 6687 | unsigned int length; // 4 bytes |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6688 | unsigned int chunk0_length; // 4 bytes |
| 6689 | unsigned int chunk0_format; // 4 bytes; |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6690 | |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 6691 | memcpy(&version, bytes + 4, 4); |
| 6692 | swap4(&version); |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6693 | 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] | 6694 | swap4(&length); |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6695 | memcpy(&chunk0_length, bytes + 12, 4); // JSON data length |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6696 | swap4(&chunk0_length); |
| 6697 | memcpy(&chunk0_format, bytes + 16, 4); |
| 6698 | swap4(&chunk0_format); |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6699 | |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6700 | // https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#binary-gltf-layout |
| 6701 | // |
Syoyo Fujita | e69069d | 2018-03-15 22:01:18 -0500 | [diff] [blame] | 6702 | // In case the Bin buffer is not present, the size is exactly 20 + size of |
| 6703 | // JSON contents, |
| 6704 | // so use "greater than" operator. |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6705 | // |
| 6706 | // https://github.com/syoyo/tinygltf/issues/372 |
| 6707 | // Use 64bit uint to avoid integer overflow. |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6708 | uint64_t header_and_json_size = 20ull + uint64_t(chunk0_length); |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6709 | |
rhiskey | 8acf861 | 2023-12-04 17:11:59 +0300 | [diff] [blame] | 6710 | if (header_and_json_size > (std::numeric_limits<uint32_t>::max)()) { |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6711 | // Do not allow 4GB or more GLB data. |
Syoyo Fujita | 8387fdb | 2023-12-04 22:50:49 +0900 | [diff] [blame] | 6712 | if (err) { |
| 6713 | (*err) = "Invalid glTF binary. GLB data exceeds 4GB."; |
| 6714 | } |
| 6715 | return false; |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6716 | } |
| 6717 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6718 | if ((header_and_json_size > uint64_t(size)) || (chunk0_length < 1) || |
| 6719 | (length > size) || (header_and_json_size > uint64_t(length)) || |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6720 | (chunk0_format != 0x4E4F534A)) { // 0x4E4F534A = JSON format. |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6721 | if (err) { |
| 6722 | (*err) = "Invalid glTF binary."; |
| 6723 | } |
| 6724 | return false; |
| 6725 | } |
| 6726 | |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6727 | // Padding check |
| 6728 | // The start and the end of each chunk must be aligned to a 4-byte boundary. |
| 6729 | // No padding check for chunk0 start since its 4byte-boundary is ensured. |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6730 | if ((header_and_json_size % 4) != 0) { |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6731 | if (err) { |
| 6732 | (*err) = "JSON Chunk end does not aligned to a 4-byte boundary."; |
| 6733 | } |
Syoyo Fujita | 8387fdb | 2023-12-04 22:50:49 +0900 | [diff] [blame] | 6734 | return false; |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6735 | } |
| 6736 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6737 | // std::cout << "header_and_json_size = " << header_and_json_size << "\n"; |
| 6738 | // std::cout << "length = " << length << "\n"; |
Syoyo Fujita | 612e578 | 2022-09-18 21:01:39 +0900 | [diff] [blame] | 6739 | |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6740 | // Chunk1(BIN) data |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6741 | // The spec says: When the binary buffer is empty or when it is stored by |
| 6742 | // other means, this chunk SHOULD be omitted. So when header + JSON data == |
| 6743 | // binary size, Chunk1 is omitted. |
Syoyo Fujita | 612e578 | 2022-09-18 21:01:39 +0900 | [diff] [blame] | 6744 | if (header_and_json_size == uint64_t(length)) { |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6745 | bin_data_ = nullptr; |
| 6746 | bin_size_ = 0; |
| 6747 | } else { |
| 6748 | // Read Chunk1 info(BIN data) |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6749 | // |
| 6750 | // issue-440: |
| 6751 | // 'SHOULD' in glTF spec means 'RECOMMENDED', |
| 6752 | // So there is a situation that Chunk1(BIN) is composed of zero-sized BIN data |
| 6753 | // (chunksize(0) + binformat(BIN) = 8bytes). |
| 6754 | // |
| 6755 | if ((header_and_json_size + 8ull) > uint64_t(length)) { |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6756 | if (err) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6757 | (*err) = |
| 6758 | "Insufficient storage space for Chunk1(BIN data). At least Chunk1 " |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6759 | "Must have 8 or more bytes, but got " + |
Nyall Dawson | 8e9aadf | 2023-08-24 11:47:23 +1000 | [diff] [blame] | 6760 | std::to_string((header_and_json_size + 8ull) - uint64_t(length)) + |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6761 | ".\n"; |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6762 | } |
| 6763 | return false; |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6764 | } |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6765 | |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6766 | unsigned int chunk1_length{0}; // 4 bytes |
| 6767 | unsigned int chunk1_format{0}; // 4 bytes; |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6768 | memcpy(&chunk1_length, bytes + header_and_json_size, |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6769 | 4); // Bin data length |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6770 | swap4(&chunk1_length); |
| 6771 | memcpy(&chunk1_format, bytes + header_and_json_size + 4, 4); |
| 6772 | swap4(&chunk1_format); |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6773 | |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6774 | if (chunk1_format != 0x004e4942) { |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6775 | if (err) { |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6776 | (*err) = "Invalid chunkType for Chunk1."; |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6777 | } |
| 6778 | return false; |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6779 | } |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6780 | |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6781 | if (chunk1_length == 0) { |
| 6782 | |
| 6783 | if (header_and_json_size + 8 > uint64_t(length)) { |
Nyall Dawson | 4d119d7 | 2023-09-06 17:35:25 +1000 | [diff] [blame] | 6784 | if (err) { |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6785 | (*err) = "BIN Chunk header location exceeds the GLB size."; |
Nyall Dawson | 4d119d7 | 2023-09-06 17:35:25 +1000 | [diff] [blame] | 6786 | } |
| 6787 | return false; |
| 6788 | } |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6789 | |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6790 | bin_data_ = nullptr; |
| 6791 | |
| 6792 | } else { |
| 6793 | |
| 6794 | // When BIN chunk size is not zero, at least Chunk1 should have 12 bytes(8 bytes(header) + 4 bytes(bin |
| 6795 | // payload could be 1~3 bytes, but need to be aligned to 4 bytes) |
| 6796 | |
| 6797 | if (chunk1_length < 4) { |
| 6798 | if (err) { |
| 6799 | (*err) = "Insufficient Chunk1(BIN) data size."; |
| 6800 | } |
| 6801 | return false; |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6802 | } |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6803 | |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6804 | if ((chunk1_length % 4) != 0) { |
| 6805 | if (strictness_==ParseStrictness::Permissive) { |
| 6806 | if (warn) { |
| 6807 | (*warn) += "BIN Chunk end is not aligned to a 4-byte boundary.\n"; |
| 6808 | } |
| 6809 | } |
| 6810 | else { |
| 6811 | if (err) { |
| 6812 | (*err) = "BIN Chunk end is not aligned to a 4-byte boundary."; |
| 6813 | } |
| 6814 | return false; |
| 6815 | } |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6816 | } |
Syoyo Fujita | 4fea26f | 2024-01-24 05:43:27 +0900 | [diff] [blame] | 6817 | |
| 6818 | // +8 chunk1 header size. |
| 6819 | if (uint64_t(chunk1_length) + header_and_json_size + 8 > uint64_t(length)) { |
| 6820 | if (err) { |
| 6821 | (*err) = "BIN Chunk data length exceeds the GLB size."; |
| 6822 | } |
| 6823 | return false; |
| 6824 | } |
| 6825 | |
| 6826 | bin_data_ = bytes + header_and_json_size + |
| 6827 | 8; // 4 bytes (bin_buffer_length) + 4 bytes(bin_buffer_format) |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6828 | } |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6829 | |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6830 | bin_size_ = size_t(chunk1_length); |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6831 | } |
| 6832 | |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6833 | is_binary_ = true; |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6834 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6835 | bool ret = LoadFromString(model, err, warn, |
| 6836 | reinterpret_cast<const char *>(&bytes[20]), |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6837 | chunk0_length, base_dir, check_sections); |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6838 | if (!ret) { |
| 6839 | return ret; |
| 6840 | } |
| 6841 | |
| 6842 | return true; |
| 6843 | } |
| 6844 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6845 | bool TinyGLTF::LoadBinaryFromFile(Model *model, std::string *err, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6846 | std::string *warn, |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6847 | const std::string &filename, |
| 6848 | unsigned int check_sections) { |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6849 | std::stringstream ss; |
| 6850 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 6851 | if (fs.ReadWholeFile == nullptr) { |
| 6852 | // Programmer error, assert() ? |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6853 | ss << "Failed to read file: " << filename |
| 6854 | << ": one or more FS callback not set" << std::endl; |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6855 | if (err) { |
| 6856 | (*err) = ss.str(); |
| 6857 | } |
| 6858 | return false; |
| 6859 | } |
| 6860 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 6861 | std::vector<unsigned char> data; |
| 6862 | std::string fileerr; |
| 6863 | bool fileread = fs.ReadWholeFile(&data, &fileerr, filename, fs.user_data); |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6864 | if (!fileread) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 6865 | ss << "Failed to read file: " << filename << ": " << fileerr << std::endl; |
| 6866 | if (err) { |
| 6867 | (*err) = ss.str(); |
| 6868 | } |
| 6869 | return false; |
| 6870 | } |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6871 | |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 6872 | std::string basedir = GetBaseDir(filename); |
| 6873 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6874 | bool ret = LoadBinaryFromMemory(model, err, warn, &data.at(0), |
| 6875 | static_cast<unsigned int>(data.size()), |
| 6876 | basedir, check_sections); |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 6877 | |
| 6878 | return ret; |
| 6879 | } |
| 6880 | |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6881 | /////////////////////// |
| 6882 | // GLTF Serialization |
| 6883 | /////////////////////// |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6884 | namespace detail { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6885 | detail::json JsonFromString(const char *s) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6886 | #ifdef TINYGLTF_USE_RAPIDJSON |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6887 | return detail::json(s, detail::GetAllocator()); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6888 | #else |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6889 | return detail::json(s); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6890 | #endif |
jrkoonce | 63419a1 | 2019-09-03 17:06:41 -0500 | [diff] [blame] | 6891 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6892 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6893 | void JsonAssign(detail::json &dest, const detail::json &src) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6894 | #ifdef TINYGLTF_USE_RAPIDJSON |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6895 | dest.CopyFrom(src, detail::GetAllocator()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6896 | #else |
| 6897 | dest = src; |
| 6898 | #endif |
| 6899 | } |
| 6900 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6901 | void JsonAddMember(detail::json &o, const char *key, detail::json &&value) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6902 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 6903 | if (!o.IsObject()) { |
| 6904 | o.SetObject(); |
| 6905 | } |
Syoyo Fujita | 147a00a | 2023-06-04 05:45:24 +0900 | [diff] [blame] | 6906 | |
| 6907 | // Issue 420. |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6908 | // AddMember may create duplicated key, so use [] API when a key already |
| 6909 | // exists. |
Syoyo Fujita | 147a00a | 2023-06-04 05:45:24 +0900 | [diff] [blame] | 6910 | // https://github.com/Tencent/rapidjson/issues/771#issuecomment-254386863 |
| 6911 | detail::json_const_iterator it; |
| 6912 | if (detail::FindMember(o, key, it)) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6913 | o[key] = std::move(value); // replace |
Syoyo Fujita | 147a00a | 2023-06-04 05:45:24 +0900 | [diff] [blame] | 6914 | } else { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6915 | o.AddMember(detail::json(key, detail::GetAllocator()), std::move(value), |
| 6916 | detail::GetAllocator()); |
Syoyo Fujita | 147a00a | 2023-06-04 05:45:24 +0900 | [diff] [blame] | 6917 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6918 | #else |
| 6919 | o[key] = std::move(value); |
| 6920 | #endif |
| 6921 | } |
| 6922 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6923 | void JsonPushBack(detail::json &o, detail::json &&value) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6924 | #ifdef TINYGLTF_USE_RAPIDJSON |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6925 | o.PushBack(std::move(value), detail::GetAllocator()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6926 | #else |
| 6927 | o.push_back(std::move(value)); |
| 6928 | #endif |
| 6929 | } |
| 6930 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6931 | bool JsonIsNull(const detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6932 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 6933 | return o.IsNull(); |
| 6934 | #else |
| 6935 | return o.is_null(); |
| 6936 | #endif |
| 6937 | } |
| 6938 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6939 | void JsonSetObject(detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6940 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 6941 | o.SetObject(); |
| 6942 | #else |
| 6943 | o = o.object({}); |
| 6944 | #endif |
| 6945 | } |
| 6946 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6947 | void JsonReserveArray(detail::json &o, size_t s) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6948 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 6949 | o.SetArray(); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6950 | o.Reserve(static_cast<rapidjson::SizeType>(s), detail::GetAllocator()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6951 | #endif |
| 6952 | (void)(o); |
| 6953 | (void)(s); |
| 6954 | } |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6955 | } // namespace detail |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6956 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6957 | // typedef std::pair<std::string, detail::json> json_object_pair; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6958 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6959 | template <typename T> |
| 6960 | static void SerializeNumberProperty(const std::string &key, T number, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6961 | detail::json &obj) { |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 6962 | // obj.insert( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6963 | // json_object_pair(key, detail::json(static_cast<double>(number)))); |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 6964 | // obj[key] = static_cast<double>(number); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6965 | detail::JsonAddMember(obj, key.c_str(), detail::json(number)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6966 | } |
| 6967 | |
Ivo van Dongen | 272a9df | 2020-05-29 11:24:11 +0300 | [diff] [blame] | 6968 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 6969 | template <> |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6970 | void SerializeNumberProperty(const std::string &key, size_t number, |
| 6971 | detail::json &obj) { |
| 6972 | detail::JsonAddMember(obj, key.c_str(), |
| 6973 | detail::json(static_cast<uint64_t>(number))); |
Ivo van Dongen | 272a9df | 2020-05-29 11:24:11 +0300 | [diff] [blame] | 6974 | } |
| 6975 | #endif |
| 6976 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6977 | template <typename T> |
| 6978 | static void SerializeNumberArrayProperty(const std::string &key, |
| 6979 | const std::vector<T> &value, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6980 | detail::json &obj) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6981 | if (value.empty()) return; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6982 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6983 | detail::json ary; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6984 | detail::JsonReserveArray(ary, value.size()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6985 | for (const auto &s : value) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6986 | detail::JsonPushBack(ary, detail::json(s)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6987 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6988 | detail::JsonAddMember(obj, key.c_str(), std::move(ary)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6989 | } |
| 6990 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6991 | static void SerializeStringProperty(const std::string &key, |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 6992 | const std::string &value, |
| 6993 | detail::json &obj) { |
| 6994 | detail::JsonAddMember(obj, key.c_str(), |
| 6995 | detail::JsonFromString(value.c_str())); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6996 | } |
| 6997 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6998 | static void SerializeStringArrayProperty(const std::string &key, |
| 6999 | const std::vector<std::string> &value, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7000 | detail::json &obj) { |
| 7001 | detail::json ary; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7002 | detail::JsonReserveArray(ary, value.size()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7003 | for (auto &s : value) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7004 | detail::JsonPushBack(ary, detail::JsonFromString(s.c_str())); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7005 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7006 | detail::JsonAddMember(obj, key.c_str(), std::move(ary)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7007 | } |
| 7008 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7009 | static bool ValueToJson(const Value &value, detail::json *ret) { |
| 7010 | detail::json obj; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7011 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 7012 | switch (value.Type()) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7013 | case REAL_TYPE: |
| 7014 | obj.SetDouble(value.Get<double>()); |
| 7015 | break; |
| 7016 | case INT_TYPE: |
| 7017 | obj.SetInt(value.Get<int>()); |
| 7018 | break; |
| 7019 | case BOOL_TYPE: |
| 7020 | obj.SetBool(value.Get<bool>()); |
| 7021 | break; |
| 7022 | case STRING_TYPE: |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7023 | obj.SetString(value.Get<std::string>().c_str(), detail::GetAllocator()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7024 | break; |
| 7025 | case ARRAY_TYPE: { |
| 7026 | obj.SetArray(); |
| 7027 | obj.Reserve(static_cast<rapidjson::SizeType>(value.ArrayLen()), |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7028 | detail::GetAllocator()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7029 | for (unsigned int i = 0; i < value.ArrayLen(); ++i) { |
| 7030 | Value elementValue = value.Get(int(i)); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7031 | detail::json elementJson; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7032 | if (ValueToJson(value.Get(int(i)), &elementJson)) |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7033 | obj.PushBack(std::move(elementJson), detail::GetAllocator()); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7034 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7035 | break; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7036 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7037 | case BINARY_TYPE: |
| 7038 | // TODO |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7039 | // obj = detail::json(value.Get<std::vector<unsigned char>>()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7040 | return false; |
| 7041 | break; |
| 7042 | case OBJECT_TYPE: { |
| 7043 | obj.SetObject(); |
| 7044 | Value::Object objMap = value.Get<Value::Object>(); |
| 7045 | for (auto &it : objMap) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7046 | detail::json elementJson; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7047 | if (ValueToJson(it.second, &elementJson)) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7048 | obj.AddMember(detail::json(it.first.c_str(), detail::GetAllocator()), |
| 7049 | std::move(elementJson), detail::GetAllocator()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7050 | } |
| 7051 | } |
| 7052 | break; |
| 7053 | } |
| 7054 | case NULL_TYPE: |
| 7055 | default: |
| 7056 | return false; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7057 | } |
| 7058 | #else |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7059 | switch (value.Type()) { |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 7060 | case REAL_TYPE: |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7061 | obj = detail::json(value.Get<double>()); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7062 | break; |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 7063 | case INT_TYPE: |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7064 | obj = detail::json(value.Get<int>()); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7065 | break; |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 7066 | case BOOL_TYPE: |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7067 | obj = detail::json(value.Get<bool>()); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7068 | break; |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 7069 | case STRING_TYPE: |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7070 | obj = detail::json(value.Get<std::string>()); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7071 | break; |
| 7072 | case ARRAY_TYPE: { |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 7073 | for (unsigned int i = 0; i < value.ArrayLen(); ++i) { |
| 7074 | Value elementValue = value.Get(int(i)); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7075 | detail::json elementJson; |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7076 | if (ValueToJson(value.Get(int(i)), &elementJson)) |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 7077 | obj.push_back(elementJson); |
| 7078 | } |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7079 | break; |
| 7080 | } |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 7081 | case BINARY_TYPE: |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7082 | // TODO |
| 7083 | // obj = json(value.Get<std::vector<unsigned char>>()); |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 7084 | return false; |
| 7085 | break; |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7086 | case OBJECT_TYPE: { |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 7087 | Value::Object objMap = value.Get<Value::Object>(); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7088 | for (auto &it : objMap) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7089 | detail::json elementJson; |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7090 | if (ValueToJson(it.second, &elementJson)) obj[it.first] = elementJson; |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 7091 | } |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7092 | break; |
| 7093 | } |
| 7094 | case NULL_TYPE: |
| 7095 | default: |
| 7096 | return false; |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 7097 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7098 | #endif |
| 7099 | if (ret) *ret = std::move(obj); |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 7100 | return true; |
| 7101 | } |
| 7102 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7103 | static void SerializeValue(const std::string &key, const Value &value, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7104 | detail::json &obj) { |
| 7105 | detail::json ret; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7106 | if (ValueToJson(value, &ret)) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7107 | detail::JsonAddMember(obj, key.c_str(), std::move(ret)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7108 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7109 | } |
| 7110 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7111 | static void SerializeGltfBufferData(const std::vector<unsigned char> &data, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7112 | detail::json &o) { |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 7113 | std::string header = "data:application/octet-stream;base64,"; |
Syoyo Fujita | a11f6e1 | 2019-12-19 14:16:48 +0900 | [diff] [blame] | 7114 | if (data.size() > 0) { |
| 7115 | std::string encodedData = |
| 7116 | base64_encode(&data[0], static_cast<unsigned int>(data.size())); |
| 7117 | SerializeStringProperty("uri", header + encodedData, o); |
| 7118 | } else { |
| 7119 | // Issue #229 |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 7120 | // size 0 is allowed. Just emit mime header. |
Syoyo Fujita | a11f6e1 | 2019-12-19 14:16:48 +0900 | [diff] [blame] | 7121 | SerializeStringProperty("uri", header, o); |
| 7122 | } |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 7123 | } |
| 7124 | |
Selmar Kok | e467749 | 2018-10-25 16:45:49 +0200 | [diff] [blame] | 7125 | static bool SerializeGltfBufferData(const std::vector<unsigned char> &data, |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 7126 | const std::string &binFilename) { |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 7127 | #ifdef _WIN32 |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 7128 | #if defined(__GLIBCXX__) // mingw |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 7129 | int file_descriptor = _wopen(UTF8ToWchar(binFilename).c_str(), |
| 7130 | _O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY); |
| 7131 | __gnu_cxx::stdio_filebuf<char> wfile_buf( |
| 7132 | file_descriptor, std::ios_base::out | std::ios_base::binary); |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 7133 | std::ostream output(&wfile_buf); |
| 7134 | if (!wfile_buf.is_open()) return false; |
| 7135 | #elif defined(_MSC_VER) |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 7136 | std::ofstream output(UTF8ToWchar(binFilename).c_str(), std::ofstream::binary); |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 7137 | if (!output.is_open()) return false; |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 7138 | #else |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 7139 | std::ofstream output(binFilename.c_str(), std::ofstream::binary); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 7140 | if (!output.is_open()) return false; |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 7141 | #endif |
| 7142 | #else |
| 7143 | std::ofstream output(binFilename.c_str(), std::ofstream::binary); |
| 7144 | if (!output.is_open()) return false; |
| 7145 | #endif |
Syoyo Fujita | a11f6e1 | 2019-12-19 14:16:48 +0900 | [diff] [blame] | 7146 | if (data.size() > 0) { |
| 7147 | output.write(reinterpret_cast<const char *>(&data[0]), |
| 7148 | std::streamsize(data.size())); |
| 7149 | } else { |
| 7150 | // Issue #229 |
| 7151 | // size 0 will be still valid buffer data. |
| 7152 | // write empty file. |
| 7153 | } |
Selmar Kok | e467749 | 2018-10-25 16:45:49 +0200 | [diff] [blame] | 7154 | return true; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7155 | } |
| 7156 | |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 7157 | #if 0 // FIXME(syoyo): not used. will be removed in the future release. |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7158 | static void SerializeParameterMap(ParameterMap ¶m, detail::json &o) { |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7159 | for (ParameterMap::iterator paramIt = param.begin(); paramIt != param.end(); |
| 7160 | ++paramIt) { |
| 7161 | if (paramIt->second.number_array.size()) { |
| 7162 | SerializeNumberArrayProperty<double>(paramIt->first, |
| 7163 | paramIt->second.number_array, o); |
| 7164 | } else if (paramIt->second.json_double_value.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7165 | detail::json json_double_value; |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7166 | for (std::map<std::string, double>::iterator it = |
| 7167 | paramIt->second.json_double_value.begin(); |
| 7168 | it != paramIt->second.json_double_value.end(); ++it) { |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7169 | if (it->first == "index") { |
| 7170 | json_double_value[it->first] = paramIt->second.TextureIndex(); |
| 7171 | } else { |
| 7172 | json_double_value[it->first] = it->second; |
| 7173 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7174 | } |
| 7175 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 7176 | o[paramIt->first] = json_double_value; |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7177 | } else if (!paramIt->second.string_value.empty()) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7178 | SerializeStringProperty(paramIt->first, paramIt->second.string_value, o); |
Ben Buzbee | f6af224 | 2018-04-25 15:13:05 -0700 | [diff] [blame] | 7179 | } else if (paramIt->second.has_number_value) { |
| 7180 | o[paramIt->first] = paramIt->second.number_value; |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7181 | } else { |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 7182 | o[paramIt->first] = paramIt->second.bool_value; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7183 | } |
| 7184 | } |
| 7185 | } |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 7186 | #endif |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7187 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7188 | static void SerializeExtensionMap(const ExtensionMap &extensions, |
| 7189 | detail::json &o) { |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7190 | if (!extensions.size()) return; |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 7191 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7192 | detail::json extMap; |
Selmar Kok | 81b672b | 2019-10-18 16:08:44 +0200 | [diff] [blame] | 7193 | for (ExtensionMap::const_iterator extIt = extensions.begin(); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 7194 | extIt != extensions.end(); ++extIt) { |
Syoyo Fujita | 924d86e | 2018-10-08 21:15:12 +0900 | [diff] [blame] | 7195 | // Allow an empty object for extension(#97) |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7196 | detail::json ret; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7197 | bool isNull = true; |
Syoyo Fujita | 924d86e | 2018-10-08 21:15:12 +0900 | [diff] [blame] | 7198 | if (ValueToJson(extIt->second, &ret)) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7199 | isNull = detail::JsonIsNull(ret); |
| 7200 | detail::JsonAddMember(extMap, extIt->first.c_str(), std::move(ret)); |
Selmar Kok | db7f4e4 | 2018-10-10 18:10:58 +0200 | [diff] [blame] | 7201 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7202 | if (isNull) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 7203 | if (!(extIt->first.empty())) { // name should not be empty, but for sure |
| 7204 | // create empty object so that an extension name is still included in |
| 7205 | // json. |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7206 | detail::json empty; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7207 | detail::JsonSetObject(empty); |
| 7208 | detail::JsonAddMember(extMap, extIt->first.c_str(), std::move(empty)); |
Syoyo Fujita | 924d86e | 2018-10-08 21:15:12 +0900 | [diff] [blame] | 7209 | } |
| 7210 | } |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 7211 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7212 | detail::JsonAddMember(o, "extensions", std::move(extMap)); |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 7213 | } |
| 7214 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7215 | static void SerializeExtras(const Value &extras, detail::json &o) { |
| 7216 | if (extras.Type() != NULL_TYPE) SerializeValue("extras", extras, o); |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7217 | } |
| 7218 | |
| 7219 | template <typename GltfType> |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7220 | void SerializeExtrasAndExtensions(const GltfType &obj, detail::json &o) { |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7221 | SerializeExtensionMap(obj.extensions, o); |
| 7222 | SerializeExtras(obj.extras, o); |
| 7223 | } |
| 7224 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7225 | static void SerializeGltfAccessor(const Accessor &accessor, detail::json &o) { |
Sanjeet Suhag | 5ecede7 | 2020-03-04 17:40:10 -0500 | [diff] [blame] | 7226 | if (accessor.bufferView >= 0) |
| 7227 | SerializeNumberProperty<int>("bufferView", accessor.bufferView, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7228 | |
Syoyo Fujita | 18f0e20 | 2020-04-29 19:16:35 +0900 | [diff] [blame] | 7229 | if (accessor.byteOffset != 0) |
emimvi | bf7120f | 2023-09-02 00:11:41 +0200 | [diff] [blame] | 7230 | SerializeNumberProperty<size_t>("byteOffset", accessor.byteOffset, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7231 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7232 | SerializeNumberProperty<int>("componentType", accessor.componentType, o); |
| 7233 | SerializeNumberProperty<size_t>("count", accessor.count, o); |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 7234 | |
| 7235 | if ((accessor.componentType == TINYGLTF_COMPONENT_TYPE_FLOAT) || |
| 7236 | (accessor.componentType == TINYGLTF_COMPONENT_TYPE_DOUBLE)) { |
| 7237 | SerializeNumberArrayProperty<double>("min", accessor.minValues, o); |
| 7238 | SerializeNumberArrayProperty<double>("max", accessor.maxValues, o); |
| 7239 | } else { |
| 7240 | // Issue #301. Serialize as integer. |
| 7241 | // Assume int value is within [-2**31-1, 2**31-1] |
| 7242 | { |
| 7243 | std::vector<int> values; |
| 7244 | std::transform(accessor.minValues.begin(), accessor.minValues.end(), |
| 7245 | std::back_inserter(values), |
| 7246 | [](double v) { return static_cast<int>(v); }); |
| 7247 | |
| 7248 | SerializeNumberArrayProperty<int>("min", values, o); |
| 7249 | } |
| 7250 | |
| 7251 | { |
| 7252 | std::vector<int> values; |
| 7253 | std::transform(accessor.maxValues.begin(), accessor.maxValues.end(), |
| 7254 | std::back_inserter(values), |
| 7255 | [](double v) { return static_cast<int>(v); }); |
| 7256 | |
| 7257 | SerializeNumberArrayProperty<int>("max", values, o); |
| 7258 | } |
| 7259 | } |
| 7260 | |
Eero Pajarre | 2e8a115 | 2019-11-18 13:09:25 +0200 | [diff] [blame] | 7261 | if (accessor.normalized) |
| 7262 | SerializeValue("normalized", Value(accessor.normalized), o); |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7263 | std::string type; |
| 7264 | switch (accessor.type) { |
| 7265 | case TINYGLTF_TYPE_SCALAR: |
| 7266 | type = "SCALAR"; |
| 7267 | break; |
| 7268 | case TINYGLTF_TYPE_VEC2: |
| 7269 | type = "VEC2"; |
| 7270 | break; |
| 7271 | case TINYGLTF_TYPE_VEC3: |
| 7272 | type = "VEC3"; |
| 7273 | break; |
| 7274 | case TINYGLTF_TYPE_VEC4: |
| 7275 | type = "VEC4"; |
| 7276 | break; |
| 7277 | case TINYGLTF_TYPE_MAT2: |
| 7278 | type = "MAT2"; |
| 7279 | break; |
| 7280 | case TINYGLTF_TYPE_MAT3: |
| 7281 | type = "MAT3"; |
| 7282 | break; |
| 7283 | case TINYGLTF_TYPE_MAT4: |
| 7284 | type = "MAT4"; |
| 7285 | break; |
| 7286 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 7287 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7288 | SerializeStringProperty("type", type, o); |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 7289 | if (!accessor.name.empty()) SerializeStringProperty("name", accessor.name, o); |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7290 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7291 | SerializeExtrasAndExtensions(accessor, o); |
feiy | 0b31543 | 2022-08-13 10:08:17 +0800 | [diff] [blame] | 7292 | |
| 7293 | // sparse |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7294 | if (accessor.sparse.isSparse) { |
| 7295 | detail::json sparse; |
| 7296 | SerializeNumberProperty<int>("count", accessor.sparse.count, sparse); |
| 7297 | { |
| 7298 | detail::json indices; |
| 7299 | SerializeNumberProperty<int>("bufferView", |
| 7300 | accessor.sparse.indices.bufferView, indices); |
emimvi | 759976e | 2023-09-02 09:39:53 +0200 | [diff] [blame] | 7301 | SerializeNumberProperty<size_t>("byteOffset", |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7302 | accessor.sparse.indices.byteOffset, indices); |
| 7303 | SerializeNumberProperty<int>( |
| 7304 | "componentType", accessor.sparse.indices.componentType, indices); |
| 7305 | SerializeExtrasAndExtensions(accessor.sparse.indices, indices); |
| 7306 | detail::JsonAddMember(sparse, "indices", std::move(indices)); |
| 7307 | } |
| 7308 | { |
| 7309 | detail::json values; |
| 7310 | SerializeNumberProperty<int>("bufferView", |
| 7311 | accessor.sparse.values.bufferView, values); |
emimvi | 759976e | 2023-09-02 09:39:53 +0200 | [diff] [blame] | 7312 | SerializeNumberProperty<size_t>("byteOffset", |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7313 | accessor.sparse.values.byteOffset, values); |
| 7314 | SerializeExtrasAndExtensions(accessor.sparse.values, values); |
| 7315 | detail::JsonAddMember(sparse, "values", std::move(values)); |
| 7316 | } |
| 7317 | SerializeExtrasAndExtensions(accessor.sparse, sparse); |
| 7318 | detail::JsonAddMember(o, "sparse", std::move(sparse)); |
feiy | 0b31543 | 2022-08-13 10:08:17 +0800 | [diff] [blame] | 7319 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7320 | } |
| 7321 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7322 | static void SerializeGltfAnimationChannel(const AnimationChannel &channel, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7323 | detail::json &o) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7324 | SerializeNumberProperty("sampler", channel.sampler, o); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7325 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7326 | detail::json target; |
Jack Mousseau | 283b552 | 2023-01-15 11:45:45 -0800 | [diff] [blame] | 7327 | |
Loïc Escales | a75355b | 2023-04-18 21:03:39 +0200 | [diff] [blame] | 7328 | if (channel.target_node >= 0) { |
Jack Mousseau | 283b552 | 2023-01-15 11:45:45 -0800 | [diff] [blame] | 7329 | SerializeNumberProperty("node", channel.target_node, target); |
| 7330 | } |
| 7331 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7332 | SerializeStringProperty("path", channel.target_path, target); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7333 | |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 7334 | SerializeExtensionMap(channel.target_extensions, target); |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7335 | SerializeExtras(channel.target_extras, target); |
Selmar Kok | 973d9b3 | 2020-01-21 18:45:24 +0100 | [diff] [blame] | 7336 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7337 | detail::JsonAddMember(o, "target", std::move(target)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7338 | } |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7339 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7340 | SerializeExtrasAndExtensions(channel, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7341 | } |
| 7342 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7343 | static void SerializeGltfAnimationSampler(const AnimationSampler &sampler, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7344 | detail::json &o) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7345 | SerializeNumberProperty("input", sampler.input, o); |
| 7346 | SerializeNumberProperty("output", sampler.output, o); |
| 7347 | SerializeStringProperty("interpolation", sampler.interpolation, o); |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7348 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7349 | SerializeExtrasAndExtensions(sampler, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7350 | } |
| 7351 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7352 | static void SerializeGltfAnimation(const Animation &animation, |
| 7353 | detail::json &o) { |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 7354 | if (!animation.name.empty()) |
| 7355 | SerializeStringProperty("name", animation.name, o); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7356 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7357 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7358 | detail::json channels; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7359 | detail::JsonReserveArray(channels, animation.channels.size()); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7360 | for (unsigned int i = 0; i < animation.channels.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7361 | detail::json channel; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7362 | AnimationChannel gltfChannel = animation.channels[i]; |
| 7363 | SerializeGltfAnimationChannel(gltfChannel, channel); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7364 | detail::JsonPushBack(channels, std::move(channel)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7365 | } |
| 7366 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7367 | detail::JsonAddMember(o, "channels", std::move(channels)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7368 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7369 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7370 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7371 | detail::json samplers; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7372 | detail::JsonReserveArray(samplers, animation.samplers.size()); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7373 | for (unsigned int i = 0; i < animation.samplers.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7374 | detail::json sampler; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7375 | AnimationSampler gltfSampler = animation.samplers[i]; |
| 7376 | SerializeGltfAnimationSampler(gltfSampler, sampler); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7377 | detail::JsonPushBack(samplers, std::move(sampler)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7378 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7379 | detail::JsonAddMember(o, "samplers", std::move(samplers)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7380 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7381 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7382 | SerializeExtrasAndExtensions(animation, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7383 | } |
| 7384 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7385 | static void SerializeGltfAsset(const Asset &asset, detail::json &o) { |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7386 | if (!asset.generator.empty()) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7387 | SerializeStringProperty("generator", asset.generator, o); |
| 7388 | } |
| 7389 | |
Christophe | 820ede8 | 2019-07-04 15:21:21 +0900 | [diff] [blame] | 7390 | if (!asset.copyright.empty()) { |
| 7391 | SerializeStringProperty("copyright", asset.copyright, o); |
| 7392 | } |
| 7393 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7394 | auto version = asset.version; |
| 7395 | if (version.empty()) { |
Syoyo Fujita | b702de7 | 2021-03-02 19:08:29 +0900 | [diff] [blame] | 7396 | // Just in case |
| 7397 | // `version` must be defined |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7398 | version = "2.0"; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7399 | } |
| 7400 | |
Syoyo Fujita | b702de7 | 2021-03-02 19:08:29 +0900 | [diff] [blame] | 7401 | // 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] | 7402 | SerializeStringProperty("version", version, o); |
Syoyo Fujita | b702de7 | 2021-03-02 19:08:29 +0900 | [diff] [blame] | 7403 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7404 | SerializeExtrasAndExtensions(asset, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7405 | } |
| 7406 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7407 | static void SerializeGltfBufferBin(const Buffer &buffer, detail::json &o, |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 7408 | std::vector<unsigned char> &binBuffer) { |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 7409 | SerializeNumberProperty("byteLength", buffer.data.size(), o); |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 7410 | binBuffer = buffer.data; |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 7411 | |
| 7412 | if (buffer.name.size()) SerializeStringProperty("name", buffer.name, o); |
| 7413 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7414 | SerializeExtrasAndExtensions(buffer, o); |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 7415 | } |
| 7416 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7417 | static void SerializeGltfBuffer(const Buffer &buffer, detail::json &o) { |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 7418 | SerializeNumberProperty("byteLength", buffer.data.size(), o); |
| 7419 | SerializeGltfBufferData(buffer.data, o); |
| 7420 | |
| 7421 | if (buffer.name.size()) SerializeStringProperty("name", buffer.name, o); |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7422 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7423 | SerializeExtrasAndExtensions(buffer, o); |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 7424 | } |
| 7425 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7426 | static bool SerializeGltfBuffer(const Buffer &buffer, detail::json &o, |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 7427 | const std::string &binFilename, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 7428 | const std::string &binUri) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 7429 | if (!SerializeGltfBufferData(buffer.data, binFilename)) return false; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7430 | SerializeNumberProperty("byteLength", buffer.data.size(), o); |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 7431 | SerializeStringProperty("uri", binUri, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7432 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7433 | if (buffer.name.size()) SerializeStringProperty("name", buffer.name, o); |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7434 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7435 | SerializeExtrasAndExtensions(buffer, o); |
Selmar Kok | e467749 | 2018-10-25 16:45:49 +0200 | [diff] [blame] | 7436 | return true; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7437 | } |
| 7438 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7439 | static void SerializeGltfBufferView(const BufferView &bufferView, |
| 7440 | detail::json &o) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7441 | SerializeNumberProperty("buffer", bufferView.buffer, o); |
| 7442 | SerializeNumberProperty<size_t>("byteLength", bufferView.byteLength, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7443 | |
Johan Bowald | faa2722 | 2018-03-28 14:44:45 +0200 | [diff] [blame] | 7444 | // byteStride is optional, minimum allowed is 4 |
| 7445 | if (bufferView.byteStride >= 4) { |
| 7446 | SerializeNumberProperty<size_t>("byteStride", bufferView.byteStride, o); |
| 7447 | } |
| 7448 | // byteOffset is optional, default is 0 |
| 7449 | if (bufferView.byteOffset > 0) { |
| 7450 | SerializeNumberProperty<size_t>("byteOffset", bufferView.byteOffset, o); |
| 7451 | } |
| 7452 | // Target is optional, check if it contains a valid value |
| 7453 | if (bufferView.target == TINYGLTF_TARGET_ARRAY_BUFFER || |
| 7454 | bufferView.target == TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER) { |
| 7455 | SerializeNumberProperty("target", bufferView.target, o); |
| 7456 | } |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7457 | if (bufferView.name.size()) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7458 | SerializeStringProperty("name", bufferView.name, o); |
| 7459 | } |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7460 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7461 | SerializeExtrasAndExtensions(bufferView, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7462 | } |
| 7463 | |
Pyarelal Knowles | a912155 | 2022-12-29 14:12:29 -0800 | [diff] [blame] | 7464 | static void SerializeGltfImage(const Image &image, const std::string &uri, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7465 | detail::json &o) { |
Syoyo Fujita | 584f1df | 2022-12-29 21:05:53 +0900 | [diff] [blame] | 7466 | // From 2.7.0, we look for `uri` parameter, not `Image.uri` |
| 7467 | // if uri is empty, the mimeType and bufferview should be set |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7468 | if (uri.empty()) { |
Arthur Brianville (Ybalrid) | f29ae1a | 2019-05-25 22:30:55 +0200 | [diff] [blame] | 7469 | SerializeStringProperty("mimeType", image.mimeType, o); |
| 7470 | SerializeNumberProperty<int>("bufferView", image.bufferView, o); |
| 7471 | } else { |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7472 | SerializeStringProperty("uri", uri, o); |
Arthur Brianville (Ybalrid) | f29ae1a | 2019-05-25 22:30:55 +0200 | [diff] [blame] | 7473 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7474 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7475 | if (image.name.size()) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7476 | SerializeStringProperty("name", image.name, o); |
| 7477 | } |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7478 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7479 | SerializeExtrasAndExtensions(image, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7480 | } |
| 7481 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7482 | static void SerializeGltfTextureInfo(const TextureInfo &texinfo, |
| 7483 | detail::json &o) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7484 | SerializeNumberProperty("index", texinfo.index, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7485 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7486 | if (texinfo.texCoord != 0) { |
| 7487 | SerializeNumberProperty("texCoord", texinfo.texCoord, o); |
| 7488 | } |
| 7489 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7490 | SerializeExtrasAndExtensions(texinfo, o); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7491 | } |
| 7492 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7493 | static void SerializeGltfNormalTextureInfo(const NormalTextureInfo &texinfo, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7494 | detail::json &o) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7495 | SerializeNumberProperty("index", texinfo.index, o); |
| 7496 | |
| 7497 | if (texinfo.texCoord != 0) { |
| 7498 | SerializeNumberProperty("texCoord", texinfo.texCoord, o); |
| 7499 | } |
| 7500 | |
Benjamin Schmithüsen | 1c84fc2 | 2019-07-25 16:07:27 +0200 | [diff] [blame] | 7501 | if (!TINYGLTF_DOUBLE_EQUAL(texinfo.scale, 1.0)) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7502 | SerializeNumberProperty("scale", texinfo.scale, o); |
| 7503 | } |
| 7504 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7505 | SerializeExtrasAndExtensions(texinfo, o); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7506 | } |
| 7507 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7508 | static void SerializeGltfOcclusionTextureInfo( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7509 | const OcclusionTextureInfo &texinfo, detail::json &o) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7510 | SerializeNumberProperty("index", texinfo.index, o); |
| 7511 | |
| 7512 | if (texinfo.texCoord != 0) { |
| 7513 | SerializeNumberProperty("texCoord", texinfo.texCoord, o); |
| 7514 | } |
| 7515 | |
Benjamin Schmithüsen | 1c84fc2 | 2019-07-25 16:07:27 +0200 | [diff] [blame] | 7516 | if (!TINYGLTF_DOUBLE_EQUAL(texinfo.strength, 1.0)) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7517 | SerializeNumberProperty("strength", texinfo.strength, o); |
| 7518 | } |
| 7519 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7520 | SerializeExtrasAndExtensions(texinfo, o); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7521 | } |
| 7522 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7523 | static void SerializeGltfPbrMetallicRoughness(const PbrMetallicRoughness &pbr, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7524 | detail::json &o) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7525 | std::vector<double> default_baseColorFactor = {1.0, 1.0, 1.0, 1.0}; |
| 7526 | if (!Equals(pbr.baseColorFactor, default_baseColorFactor)) { |
| 7527 | SerializeNumberArrayProperty<double>("baseColorFactor", pbr.baseColorFactor, |
| 7528 | o); |
| 7529 | } |
| 7530 | |
Benjamin Schmithüsen | 1c84fc2 | 2019-07-25 16:07:27 +0200 | [diff] [blame] | 7531 | if (!TINYGLTF_DOUBLE_EQUAL(pbr.metallicFactor, 1.0)) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7532 | SerializeNumberProperty("metallicFactor", pbr.metallicFactor, o); |
| 7533 | } |
| 7534 | |
Benjamin Schmithüsen | 1c84fc2 | 2019-07-25 16:07:27 +0200 | [diff] [blame] | 7535 | if (!TINYGLTF_DOUBLE_EQUAL(pbr.roughnessFactor, 1.0)) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7536 | SerializeNumberProperty("roughnessFactor", pbr.roughnessFactor, o); |
| 7537 | } |
| 7538 | |
Syoyo Fujita | 4ebd636 | 2019-08-15 12:25:50 +0900 | [diff] [blame] | 7539 | if (pbr.baseColorTexture.index > -1) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7540 | detail::json texinfo; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7541 | SerializeGltfTextureInfo(pbr.baseColorTexture, texinfo); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7542 | detail::JsonAddMember(o, "baseColorTexture", std::move(texinfo)); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7543 | } |
| 7544 | |
Syoyo Fujita | 4ebd636 | 2019-08-15 12:25:50 +0900 | [diff] [blame] | 7545 | if (pbr.metallicRoughnessTexture.index > -1) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7546 | detail::json texinfo; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7547 | SerializeGltfTextureInfo(pbr.metallicRoughnessTexture, texinfo); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7548 | detail::JsonAddMember(o, "metallicRoughnessTexture", std::move(texinfo)); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7549 | } |
| 7550 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7551 | SerializeExtrasAndExtensions(pbr, o); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7552 | } |
| 7553 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7554 | static void SerializeGltfMaterial(const Material &material, detail::json &o) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7555 | if (material.name.size()) { |
| 7556 | SerializeStringProperty("name", material.name, o); |
| 7557 | } |
| 7558 | |
| 7559 | // QUESTION(syoyo): Write material parameters regardless of its default value? |
| 7560 | |
| 7561 | if (!TINYGLTF_DOUBLE_EQUAL(material.alphaCutoff, 0.5)) { |
| 7562 | SerializeNumberProperty("alphaCutoff", material.alphaCutoff, o); |
| 7563 | } |
| 7564 | |
Patrick Härtl | d9a468b | 2019-08-14 14:14:07 +0200 | [diff] [blame] | 7565 | if (material.alphaMode.compare("OPAQUE") != 0) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7566 | SerializeStringProperty("alphaMode", material.alphaMode, o); |
| 7567 | } |
| 7568 | |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 7569 | if (material.doubleSided != false) |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7570 | detail::JsonAddMember(o, "doubleSided", detail::json(material.doubleSided)); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7571 | |
Syoyo Fujita | 4ebd636 | 2019-08-15 12:25:50 +0900 | [diff] [blame] | 7572 | if (material.normalTexture.index > -1) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7573 | detail::json texinfo; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7574 | SerializeGltfNormalTextureInfo(material.normalTexture, texinfo); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7575 | detail::JsonAddMember(o, "normalTexture", std::move(texinfo)); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7576 | } |
| 7577 | |
Syoyo Fujita | 4ebd636 | 2019-08-15 12:25:50 +0900 | [diff] [blame] | 7578 | if (material.occlusionTexture.index > -1) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7579 | detail::json texinfo; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7580 | SerializeGltfOcclusionTextureInfo(material.occlusionTexture, texinfo); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7581 | detail::JsonAddMember(o, "occlusionTexture", std::move(texinfo)); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7582 | } |
| 7583 | |
Syoyo Fujita | 4ebd636 | 2019-08-15 12:25:50 +0900 | [diff] [blame] | 7584 | if (material.emissiveTexture.index > -1) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7585 | detail::json texinfo; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7586 | SerializeGltfTextureInfo(material.emissiveTexture, texinfo); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7587 | detail::JsonAddMember(o, "emissiveTexture", std::move(texinfo)); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7588 | } |
| 7589 | |
| 7590 | std::vector<double> default_emissiveFactor = {0.0, 0.0, 0.0}; |
| 7591 | if (!Equals(material.emissiveFactor, default_emissiveFactor)) { |
| 7592 | SerializeNumberArrayProperty<double>("emissiveFactor", |
| 7593 | material.emissiveFactor, o); |
| 7594 | } |
| 7595 | |
| 7596 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7597 | detail::json pbrMetallicRoughness; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7598 | SerializeGltfPbrMetallicRoughness(material.pbrMetallicRoughness, |
| 7599 | pbrMetallicRoughness); |
Syoyo Fujita | 7e00904 | 2019-09-13 15:32:22 +0900 | [diff] [blame] | 7600 | // Issue 204 |
| 7601 | // Do not serialize `pbrMetallicRoughness` if pbrMetallicRoughness has all |
| 7602 | // default values(json is null). Otherwise it will serialize to |
| 7603 | // `pbrMetallicRoughness : null`, which cannot be read by other glTF |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 7604 | // importers (and validators). |
Syoyo Fujita | 7e00904 | 2019-09-13 15:32:22 +0900 | [diff] [blame] | 7605 | // |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7606 | if (!detail::JsonIsNull(pbrMetallicRoughness)) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7607 | detail::JsonAddMember(o, "pbrMetallicRoughness", |
| 7608 | std::move(pbrMetallicRoughness)); |
Syoyo Fujita | 7e00904 | 2019-09-13 15:32:22 +0900 | [diff] [blame] | 7609 | } |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7610 | } |
| 7611 | |
| 7612 | #if 0 // legacy way. just for the record. |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7613 | if (material.values.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7614 | detail::json pbrMetallicRoughness; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7615 | SerializeParameterMap(material.values, pbrMetallicRoughness); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7616 | detail::JsonAddMember(o, "pbrMetallicRoughness", std::move(pbrMetallicRoughness)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7617 | } |
| 7618 | |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7619 | SerializeParameterMap(material.additionalValues, o); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7620 | #endif |
| 7621 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7622 | SerializeExtrasAndExtensions(material, o); |
Thomas Gamper | f1bdf43 | 2024-02-05 16:50:46 +0100 | [diff] [blame] | 7623 | |
| 7624 | // MSFT_lod |
| 7625 | if (!material.lods.empty()) { |
| 7626 | detail::json_iterator it; |
| 7627 | if (!detail::FindMember(o, "extensions", it)) { |
| 7628 | detail::json extensions; |
| 7629 | detail::JsonSetObject(extensions); |
| 7630 | detail::JsonAddMember(o, "extensions", std::move(extensions)); |
| 7631 | detail::FindMember(o, "extensions", it); |
| 7632 | } |
| 7633 | auto &extensions = detail::GetValue(it); |
| 7634 | if (!detail::FindMember(extensions, "MSFT_lod", it)) { |
| 7635 | detail::json lod; |
| 7636 | detail::JsonSetObject(lod); |
| 7637 | detail::JsonAddMember(extensions, "MSFT_lod", std::move(lod)); |
| 7638 | detail::FindMember(extensions, "MSFT_lod", it); |
| 7639 | } |
| 7640 | SerializeNumberArrayProperty<int>("ids", material.lods, detail::GetValue(it)); |
| 7641 | } else { |
| 7642 | detail::json_iterator ext_it; |
| 7643 | if (detail::FindMember(o, "extensions", ext_it)) { |
| 7644 | auto &extensions = detail::GetValue(ext_it); |
| 7645 | detail::json_iterator lp_it; |
| 7646 | if (detail::FindMember(extensions, "MSFT_lod", lp_it)) { |
| 7647 | detail::Erase(extensions, lp_it); |
| 7648 | } |
| 7649 | if (detail::IsEmpty(extensions)) { |
| 7650 | detail::Erase(o, ext_it); |
| 7651 | } |
| 7652 | } |
| 7653 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7654 | } |
| 7655 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7656 | static void SerializeGltfMesh(const Mesh &mesh, detail::json &o) { |
| 7657 | detail::json primitives; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7658 | detail::JsonReserveArray(primitives, mesh.primitives.size()); |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7659 | for (unsigned int i = 0; i < mesh.primitives.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7660 | detail::json primitive; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7661 | const Primitive &gltfPrimitive = mesh.primitives[i]; // don't make a copy |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7662 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7663 | detail::json attributes; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7664 | for (auto attrIt = gltfPrimitive.attributes.begin(); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7665 | attrIt != gltfPrimitive.attributes.end(); ++attrIt) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7666 | SerializeNumberProperty<int>(attrIt->first, attrIt->second, attributes); |
| 7667 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7668 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7669 | detail::JsonAddMember(primitive, "attributes", std::move(attributes)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7670 | } |
Johan Bowald | faa2722 | 2018-03-28 14:44:45 +0200 | [diff] [blame] | 7671 | |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 7672 | // Indices is optional |
Johan Bowald | faa2722 | 2018-03-28 14:44:45 +0200 | [diff] [blame] | 7673 | if (gltfPrimitive.indices > -1) { |
| 7674 | SerializeNumberProperty<int>("indices", gltfPrimitive.indices, primitive); |
| 7675 | } |
| 7676 | // Material is optional |
| 7677 | if (gltfPrimitive.material > -1) { |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 7678 | SerializeNumberProperty<int>("material", gltfPrimitive.material, |
| 7679 | primitive); |
Johan Bowald | faa2722 | 2018-03-28 14:44:45 +0200 | [diff] [blame] | 7680 | } |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7681 | SerializeNumberProperty<int>("mode", gltfPrimitive.mode, primitive); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7682 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7683 | // Morph targets |
| 7684 | if (gltfPrimitive.targets.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7685 | detail::json targets; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7686 | detail::JsonReserveArray(targets, gltfPrimitive.targets.size()); |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7687 | for (unsigned int k = 0; k < gltfPrimitive.targets.size(); ++k) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7688 | detail::json targetAttributes; |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7689 | std::map<std::string, int> targetData = gltfPrimitive.targets[k]; |
| 7690 | for (std::map<std::string, int>::iterator attrIt = targetData.begin(); |
| 7691 | attrIt != targetData.end(); ++attrIt) { |
| 7692 | SerializeNumberProperty<int>(attrIt->first, attrIt->second, |
| 7693 | targetAttributes); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7694 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7695 | detail::JsonPushBack(targets, std::move(targetAttributes)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7696 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7697 | detail::JsonAddMember(primitive, "targets", std::move(targets)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7698 | } |
| 7699 | |
KUDELSKI Dimitri | 69d7557 | 2023-07-10 15:13:09 +0200 | [diff] [blame] | 7700 | SerializeExtrasAndExtensions(gltfPrimitive, primitive); |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7701 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7702 | detail::JsonPushBack(primitives, std::move(primitive)); |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7703 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7704 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7705 | detail::JsonAddMember(o, "primitives", std::move(primitives)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7706 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7707 | if (mesh.weights.size()) { |
| 7708 | SerializeNumberArrayProperty<double>("weights", mesh.weights, o); |
| 7709 | } |
| 7710 | |
| 7711 | if (mesh.name.size()) { |
| 7712 | SerializeStringProperty("name", mesh.name, o); |
| 7713 | } |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7714 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7715 | SerializeExtrasAndExtensions(mesh, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7716 | } |
| 7717 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7718 | static void SerializeSpotLight(const SpotLight &spot, detail::json &o) { |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 7719 | SerializeNumberProperty("innerConeAngle", spot.innerConeAngle, o); |
| 7720 | SerializeNumberProperty("outerConeAngle", spot.outerConeAngle, o); |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7721 | SerializeExtrasAndExtensions(spot, o); |
Benjamin Schmithüsen | 051f4be | 2019-07-09 17:59:20 +0200 | [diff] [blame] | 7722 | } |
| 7723 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7724 | static void SerializeGltfLight(const Light &light, detail::json &o) { |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 7725 | if (!light.name.empty()) SerializeStringProperty("name", light.name, o); |
Benjamin Schmithüsen | 051f4be | 2019-07-09 17:59:20 +0200 | [diff] [blame] | 7726 | SerializeNumberProperty("intensity", light.intensity, o); |
Syoyo Fujita | 3dad303 | 2020-05-13 21:22:23 +0900 | [diff] [blame] | 7727 | if (light.range > 0.0) { |
| 7728 | SerializeNumberProperty("range", light.range, o); |
| 7729 | } |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 7730 | SerializeNumberArrayProperty("color", light.color, o); |
| 7731 | SerializeStringProperty("type", light.type, o); |
Benjamin Schmithüsen | 051f4be | 2019-07-09 17:59:20 +0200 | [diff] [blame] | 7732 | if (light.type == "spot") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7733 | detail::json spot; |
Benjamin Schmithüsen | 051f4be | 2019-07-09 17:59:20 +0200 | [diff] [blame] | 7734 | SerializeSpotLight(light.spot, spot); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7735 | detail::JsonAddMember(o, "spot", std::move(spot)); |
Benjamin Schmithüsen | 051f4be | 2019-07-09 17:59:20 +0200 | [diff] [blame] | 7736 | } |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7737 | SerializeExtrasAndExtensions(light, o); |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 7738 | } |
| 7739 | |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 7740 | static void SerializeGltfPositionalEmitter(const PositionalEmitter &positional, |
| 7741 | detail::json &o) { |
| 7742 | if (!TINYGLTF_DOUBLE_EQUAL(positional.coneInnerAngle, 6.283185307179586)) |
| 7743 | SerializeNumberProperty("coneInnerAngle", positional.coneInnerAngle, o); |
| 7744 | if (!TINYGLTF_DOUBLE_EQUAL(positional.coneOuterAngle, 6.283185307179586)) |
| 7745 | SerializeNumberProperty("coneOuterAngle", positional.coneOuterAngle, o); |
| 7746 | if (positional.coneOuterGain > 0.0) |
| 7747 | SerializeNumberProperty("coneOuterGain", positional.coneOuterGain, o); |
| 7748 | if (!TINYGLTF_DOUBLE_EQUAL(positional.maxDistance, 100.0)) |
| 7749 | SerializeNumberProperty("maxDistance", positional.maxDistance, o); |
| 7750 | if (!TINYGLTF_DOUBLE_EQUAL(positional.refDistance, 1.0)) |
| 7751 | SerializeNumberProperty("refDistance", positional.refDistance, o); |
| 7752 | if (!TINYGLTF_DOUBLE_EQUAL(positional.rolloffFactor, 1.0)) |
| 7753 | SerializeNumberProperty("rolloffFactor", positional.rolloffFactor, o); |
| 7754 | |
| 7755 | SerializeExtrasAndExtensions(positional, o); |
| 7756 | } |
| 7757 | |
| 7758 | static void SerializeGltfAudioEmitter(const AudioEmitter &emitter, |
| 7759 | detail::json &o) { |
| 7760 | if (!emitter.name.empty()) SerializeStringProperty("name", emitter.name, o); |
| 7761 | if (!TINYGLTF_DOUBLE_EQUAL(emitter.gain, 1.0)) |
| 7762 | SerializeNumberProperty("gain", emitter.gain, o); |
| 7763 | if (emitter.loop) SerializeNumberProperty("loop", emitter.loop, o); |
| 7764 | if (emitter.playing) SerializeNumberProperty("playing", emitter.playing, o); |
| 7765 | if (!emitter.type.empty()) SerializeStringProperty("type", emitter.type, o); |
| 7766 | if (!emitter.distanceModel.empty()) |
| 7767 | SerializeStringProperty("distanceModel", emitter.distanceModel, o); |
| 7768 | if (emitter.type == "positional") { |
| 7769 | detail::json positional; |
| 7770 | SerializeGltfPositionalEmitter(emitter.positional, positional); |
| 7771 | detail::JsonAddMember(o, "positional", std::move(positional)); |
| 7772 | } |
| 7773 | SerializeNumberProperty("source", emitter.source, o); |
| 7774 | SerializeExtrasAndExtensions(emitter, o); |
| 7775 | } |
| 7776 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7777 | static void SerializeGltfAudioSource(const AudioSource &source, |
| 7778 | detail::json &o) { |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 7779 | std::string name; |
| 7780 | std::string uri; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 7781 | std::string mimeType; // (required if no uri) ["audio/mp3", "audio/ogg", |
| 7782 | // "audio/wav", "audio/m4a"] |
| 7783 | |
| 7784 | if (!source.name.empty()) SerializeStringProperty("name", source.name, o); |
| 7785 | if (source.uri.empty()) { |
| 7786 | SerializeStringProperty("mimeType", source.mimeType, o); |
| 7787 | SerializeNumberProperty<int>("bufferView", source.bufferView, o); |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7788 | } else { |
| 7789 | SerializeStringProperty("uri", source.uri, o); |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 7790 | } |
| 7791 | SerializeExtrasAndExtensions(source, o); |
| 7792 | } |
| 7793 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7794 | static void SerializeGltfNode(const Node &node, detail::json &o) { |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7795 | if (node.translation.size() > 0) { |
| 7796 | SerializeNumberArrayProperty<double>("translation", node.translation, o); |
| 7797 | } |
| 7798 | if (node.rotation.size() > 0) { |
| 7799 | SerializeNumberArrayProperty<double>("rotation", node.rotation, o); |
| 7800 | } |
| 7801 | if (node.scale.size() > 0) { |
| 7802 | SerializeNumberArrayProperty<double>("scale", node.scale, o); |
| 7803 | } |
| 7804 | if (node.matrix.size() > 0) { |
| 7805 | SerializeNumberArrayProperty<double>("matrix", node.matrix, o); |
| 7806 | } |
| 7807 | if (node.mesh != -1) { |
| 7808 | SerializeNumberProperty<int>("mesh", node.mesh, o); |
| 7809 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7810 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7811 | if (node.skin != -1) { |
| 7812 | SerializeNumberProperty<int>("skin", node.skin, o); |
| 7813 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7814 | |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7815 | if (node.camera != -1) { |
| 7816 | SerializeNumberProperty<int>("camera", node.camera, o); |
| 7817 | } |
| 7818 | |
Benjamin Schmithüsen | 74c3c10 | 2019-08-16 14:24:26 +0200 | [diff] [blame] | 7819 | if (node.weights.size() > 0) { |
| 7820 | SerializeNumberArrayProperty<double>("weights", node.weights, o); |
| 7821 | } |
| 7822 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7823 | SerializeExtrasAndExtensions(node, o); |
Jens Olsson | b96f696 | 2018-05-24 15:29:54 +0200 | [diff] [blame] | 7824 | |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 7825 | // Note(agnat): If the asset was loaded from disk, the node may already |
| 7826 | // contain the KHR_lights_punctual extension. If it was constructed in |
| 7827 | // memory it does not. In any case we update the JSON property using |
| 7828 | // the value from the struct. Last, if the node does not have a light |
| 7829 | // reference but the extension is still present, we remove it. |
| 7830 | if (node.light != -1) { |
| 7831 | detail::json_iterator it; |
| 7832 | if (!detail::FindMember(o, "extensions", it)) { |
| 7833 | detail::json extensions; |
| 7834 | detail::JsonSetObject(extensions); |
| 7835 | detail::JsonAddMember(o, "extensions", std::move(extensions)); |
| 7836 | detail::FindMember(o, "extensions", it); |
| 7837 | } |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7838 | auto &extensions = detail::GetValue(it); |
| 7839 | if (!detail::FindMember(extensions, "KHR_lights_punctual", it)) { |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 7840 | detail::json lights_punctual; |
| 7841 | detail::JsonSetObject(lights_punctual); |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7842 | detail::JsonAddMember(extensions, "KHR_lights_punctual", |
| 7843 | std::move(lights_punctual)); |
Thomas Gamper | afcfb57 | 2023-11-23 14:12:54 +0100 | [diff] [blame] | 7844 | detail::FindMember(extensions, "KHR_lights_punctual", it); |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 7845 | } |
David Siegel | 8d5d0b3 | 2023-06-07 15:35:35 +0200 | [diff] [blame] | 7846 | SerializeNumberProperty("light", node.light, detail::GetValue(it)); |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 7847 | } else { |
| 7848 | // node has no light ref (any longer)... so we clean up |
| 7849 | detail::json_iterator ext_it; |
| 7850 | if (detail::FindMember(o, "extensions", ext_it)) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 7851 | auto &extensions = detail::GetValue(ext_it); |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 7852 | detail::json_iterator lp_it; |
| 7853 | if (detail::FindMember(extensions, "KHR_lights_punctual", lp_it)) { |
| 7854 | detail::Erase(extensions, lp_it); |
| 7855 | } |
| 7856 | if (detail::IsEmpty(extensions)) { |
| 7857 | detail::Erase(o, ext_it); |
| 7858 | } |
| 7859 | } |
| 7860 | } |
| 7861 | |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 7862 | // KHR_audio |
| 7863 | if (node.emitter != -1) { |
| 7864 | detail::json_iterator it; |
| 7865 | if (!detail::FindMember(o, "extensions", it)) { |
| 7866 | detail::json extensions; |
| 7867 | detail::JsonSetObject(extensions); |
| 7868 | detail::JsonAddMember(o, "extensions", std::move(extensions)); |
| 7869 | detail::FindMember(o, "extensions", it); |
| 7870 | } |
| 7871 | auto &extensions = detail::GetValue(it); |
| 7872 | if (!detail::FindMember(extensions, "KHR_audio", it)) { |
| 7873 | detail::json audio; |
| 7874 | detail::JsonSetObject(audio); |
| 7875 | detail::JsonAddMember(extensions, "KHR_audio", std::move(audio)); |
Thomas Gamper | f1bdf43 | 2024-02-05 16:50:46 +0100 | [diff] [blame] | 7876 | detail::FindMember(extensions, "KHR_audio", it); |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 7877 | } |
| 7878 | SerializeNumberProperty("emitter", node.emitter, detail::GetValue(it)); |
| 7879 | } else { |
| 7880 | detail::json_iterator ext_it; |
| 7881 | if (detail::FindMember(o, "extensions", ext_it)) { |
| 7882 | auto &extensions = detail::GetValue(ext_it); |
| 7883 | detail::json_iterator lp_it; |
| 7884 | if (detail::FindMember(extensions, "KHR_audio", lp_it)) { |
| 7885 | detail::Erase(extensions, lp_it); |
| 7886 | } |
| 7887 | if (detail::IsEmpty(extensions)) { |
| 7888 | detail::Erase(o, ext_it); |
| 7889 | } |
| 7890 | } |
| 7891 | } |
| 7892 | |
Thomas Gamper | f1bdf43 | 2024-02-05 16:50:46 +0100 | [diff] [blame] | 7893 | // MSFT_lod |
| 7894 | if (!node.lods.empty()) { |
| 7895 | detail::json_iterator it; |
| 7896 | if (!detail::FindMember(o, "extensions", it)) { |
| 7897 | detail::json extensions; |
| 7898 | detail::JsonSetObject(extensions); |
| 7899 | detail::JsonAddMember(o, "extensions", std::move(extensions)); |
| 7900 | detail::FindMember(o, "extensions", it); |
| 7901 | } |
| 7902 | auto &extensions = detail::GetValue(it); |
| 7903 | if (!detail::FindMember(extensions, "MSFT_lod", it)) { |
| 7904 | detail::json lod; |
| 7905 | detail::JsonSetObject(lod); |
| 7906 | detail::JsonAddMember(extensions, "MSFT_lod", std::move(lod)); |
| 7907 | detail::FindMember(extensions, "MSFT_lod", it); |
| 7908 | } |
| 7909 | SerializeNumberArrayProperty<int>("ids", node.lods, detail::GetValue(it)); |
| 7910 | } else { |
| 7911 | detail::json_iterator ext_it; |
| 7912 | if (detail::FindMember(o, "extensions", ext_it)) { |
| 7913 | auto &extensions = detail::GetValue(ext_it); |
| 7914 | detail::json_iterator lp_it; |
| 7915 | if (detail::FindMember(extensions, "MSFT_lod", lp_it)) { |
| 7916 | detail::Erase(extensions, lp_it); |
| 7917 | } |
| 7918 | if (detail::IsEmpty(extensions)) { |
| 7919 | detail::Erase(o, ext_it); |
| 7920 | } |
| 7921 | } |
| 7922 | } |
| 7923 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 7924 | if (!node.name.empty()) SerializeStringProperty("name", node.name, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7925 | SerializeNumberArrayProperty<int>("children", node.children, o); |
| 7926 | } |
| 7927 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7928 | static void SerializeGltfSampler(const Sampler &sampler, detail::json &o) { |
s00665032 | 137a7ca | 2023-01-13 12:52:08 +0700 | [diff] [blame] | 7929 | if (!sampler.name.empty()) { |
| 7930 | SerializeStringProperty("name", sampler.name, o); |
| 7931 | } |
Syoyo Fujita | ee179b2 | 2019-08-16 13:11:30 +0900 | [diff] [blame] | 7932 | if (sampler.magFilter != -1) { |
| 7933 | SerializeNumberProperty("magFilter", sampler.magFilter, o); |
| 7934 | } |
| 7935 | if (sampler.minFilter != -1) { |
| 7936 | SerializeNumberProperty("minFilter", sampler.minFilter, o); |
| 7937 | } |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 7938 | // SerializeNumberProperty("wrapR", sampler.wrapR, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7939 | SerializeNumberProperty("wrapS", sampler.wrapS, o); |
| 7940 | SerializeNumberProperty("wrapT", sampler.wrapT, o); |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7941 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7942 | SerializeExtrasAndExtensions(sampler, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7943 | } |
| 7944 | |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7945 | static void SerializeGltfOrthographicCamera(const OrthographicCamera &camera, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7946 | detail::json &o) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7947 | SerializeNumberProperty("zfar", camera.zfar, o); |
| 7948 | SerializeNumberProperty("znear", camera.znear, o); |
| 7949 | SerializeNumberProperty("xmag", camera.xmag, o); |
| 7950 | SerializeNumberProperty("ymag", camera.ymag, o); |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7951 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7952 | SerializeExtrasAndExtensions(camera, o); |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7953 | } |
| 7954 | |
| 7955 | static void SerializeGltfPerspectiveCamera(const PerspectiveCamera &camera, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7956 | detail::json &o) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7957 | SerializeNumberProperty("zfar", camera.zfar, o); |
| 7958 | SerializeNumberProperty("znear", camera.znear, o); |
| 7959 | if (camera.aspectRatio > 0) { |
| 7960 | SerializeNumberProperty("aspectRatio", camera.aspectRatio, o); |
| 7961 | } |
| 7962 | |
| 7963 | if (camera.yfov > 0) { |
| 7964 | SerializeNumberProperty("yfov", camera.yfov, o); |
| 7965 | } |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7966 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7967 | SerializeExtrasAndExtensions(camera, o); |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7968 | } |
| 7969 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7970 | static void SerializeGltfCamera(const Camera &camera, detail::json &o) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7971 | SerializeStringProperty("type", camera.type, o); |
| 7972 | if (!camera.name.empty()) { |
Selmar Kok | ee3d066 | 2018-10-08 16:20:43 +0200 | [diff] [blame] | 7973 | SerializeStringProperty("name", camera.name, o); |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7974 | } |
| 7975 | |
| 7976 | if (camera.type.compare("orthographic") == 0) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7977 | detail::json orthographic; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7978 | SerializeGltfOrthographicCamera(camera.orthographic, orthographic); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7979 | detail::JsonAddMember(o, "orthographic", std::move(orthographic)); |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7980 | } else if (camera.type.compare("perspective") == 0) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7981 | detail::json perspective; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7982 | SerializeGltfPerspectiveCamera(camera.perspective, perspective); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7983 | detail::JsonAddMember(o, "perspective", std::move(perspective)); |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7984 | } else { |
| 7985 | // ??? |
| 7986 | } |
Syoyo | fe77cc5 | 2020-05-09 02:41:07 +0900 | [diff] [blame] | 7987 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7988 | SerializeExtrasAndExtensions(camera, o); |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7989 | } |
| 7990 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7991 | static void SerializeGltfScene(const Scene &scene, detail::json &o) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7992 | SerializeNumberArrayProperty<int>("nodes", scene.nodes, o); |
| 7993 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7994 | if (scene.name.size()) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7995 | SerializeStringProperty("name", scene.name, o); |
| 7996 | } |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7997 | SerializeExtrasAndExtensions(scene, o); |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 7998 | |
| 7999 | // KHR_audio |
| 8000 | if (!scene.audioEmitters.empty()) { |
| 8001 | detail::json_iterator it; |
| 8002 | if (!detail::FindMember(o, "extensions", it)) { |
| 8003 | detail::json extensions; |
| 8004 | detail::JsonSetObject(extensions); |
| 8005 | detail::JsonAddMember(o, "extensions", std::move(extensions)); |
| 8006 | detail::FindMember(o, "extensions", it); |
| 8007 | } |
| 8008 | auto &extensions = detail::GetValue(it); |
| 8009 | if (!detail::FindMember(extensions, "KHR_audio", it)) { |
| 8010 | detail::json audio; |
| 8011 | detail::JsonSetObject(audio); |
| 8012 | detail::JsonAddMember(extensions, "KHR_audio", std::move(audio)); |
| 8013 | detail::FindMember(o, "KHR_audio", it); |
| 8014 | } |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 8015 | SerializeNumberArrayProperty("emitters", scene.audioEmitters, |
| 8016 | detail::GetValue(it)); |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 8017 | } else { |
| 8018 | detail::json_iterator ext_it; |
| 8019 | if (detail::FindMember(o, "extensions", ext_it)) { |
| 8020 | auto &extensions = detail::GetValue(ext_it); |
| 8021 | detail::json_iterator lp_it; |
| 8022 | if (detail::FindMember(extensions, "KHR_audio", lp_it)) { |
| 8023 | detail::Erase(extensions, lp_it); |
| 8024 | } |
| 8025 | if (detail::IsEmpty(extensions)) { |
| 8026 | detail::Erase(o, ext_it); |
| 8027 | } |
| 8028 | } |
| 8029 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8030 | } |
| 8031 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8032 | static void SerializeGltfSkin(const Skin &skin, detail::json &o) { |
Syoyo Fujita | d07b976 | 2021-04-28 19:15:16 +0900 | [diff] [blame] | 8033 | // required |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8034 | SerializeNumberArrayProperty<int>("joints", skin.joints, o); |
Syoyo Fujita | d07b976 | 2021-04-28 19:15:16 +0900 | [diff] [blame] | 8035 | |
| 8036 | if (skin.inverseBindMatrices >= 0) { |
| 8037 | SerializeNumberProperty("inverseBindMatrices", skin.inverseBindMatrices, o); |
| 8038 | } |
| 8039 | |
| 8040 | if (skin.skeleton >= 0) { |
| 8041 | SerializeNumberProperty("skeleton", skin.skeleton, o); |
| 8042 | } |
| 8043 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 8044 | if (skin.name.size()) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8045 | SerializeStringProperty("name", skin.name, o); |
| 8046 | } |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 8047 | |
| 8048 | SerializeExtrasAndExtensions(skin, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8049 | } |
| 8050 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8051 | static void SerializeGltfTexture(const Texture &texture, detail::json &o) { |
johan bowald | b97d34c | 2018-04-02 07:29:29 +0200 | [diff] [blame] | 8052 | if (texture.sampler > -1) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 8053 | SerializeNumberProperty("sampler", texture.sampler, o); |
| 8054 | } |
Selmar Kok | 8eb3904 | 2018-10-05 14:29:35 +0200 | [diff] [blame] | 8055 | if (texture.source > -1) { |
| 8056 | SerializeNumberProperty("source", texture.source, o); |
| 8057 | } |
Christophe | 820ede8 | 2019-07-04 15:21:21 +0900 | [diff] [blame] | 8058 | if (texture.name.size()) { |
| 8059 | SerializeStringProperty("name", texture.name, o); |
| 8060 | } |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 8061 | SerializeExtrasAndExtensions(texture, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8062 | } |
| 8063 | |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8064 | /// |
| 8065 | /// Serialize all properties except buffers and images. |
| 8066 | /// |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8067 | static void SerializeGltfModel(const Model *model, detail::json &o) { |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 8068 | // ACCESSORS |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8069 | if (model->accessors.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8070 | detail::json accessors; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8071 | detail::JsonReserveArray(accessors, model->accessors.size()); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8072 | for (unsigned int i = 0; i < model->accessors.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8073 | detail::json accessor; |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8074 | SerializeGltfAccessor(model->accessors[i], accessor); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8075 | detail::JsonPushBack(accessors, std::move(accessor)); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8076 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8077 | detail::JsonAddMember(o, "accessors", std::move(accessors)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8078 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8079 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 8080 | // ANIMATIONS |
| 8081 | if (model->animations.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8082 | detail::json animations; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8083 | detail::JsonReserveArray(animations, model->animations.size()); |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 8084 | for (unsigned int i = 0; i < model->animations.size(); ++i) { |
| 8085 | if (model->animations[i].channels.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8086 | detail::json animation; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8087 | SerializeGltfAnimation(model->animations[i], animation); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8088 | detail::JsonPushBack(animations, std::move(animation)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8089 | } |
| 8090 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 8091 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8092 | detail::JsonAddMember(o, "animations", std::move(animations)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8093 | } |
| 8094 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 8095 | // ASSET |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8096 | detail::json asset; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8097 | SerializeGltfAsset(model->asset, asset); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8098 | detail::JsonAddMember(o, "asset", std::move(asset)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8099 | |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8100 | // BUFFERVIEWS |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8101 | if (model->bufferViews.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8102 | detail::json bufferViews; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8103 | detail::JsonReserveArray(bufferViews, model->bufferViews.size()); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8104 | for (unsigned int i = 0; i < model->bufferViews.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8105 | detail::json bufferView; |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8106 | SerializeGltfBufferView(model->bufferViews[i], bufferView); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8107 | detail::JsonPushBack(bufferViews, std::move(bufferView)); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8108 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8109 | detail::JsonAddMember(o, "bufferViews", std::move(bufferViews)); |
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 | |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8112 | // Extensions required |
| 8113 | if (model->extensionsRequired.size()) { |
| 8114 | SerializeStringArrayProperty("extensionsRequired", |
| 8115 | model->extensionsRequired, o); |
| 8116 | } |
| 8117 | |
| 8118 | // MATERIALS |
| 8119 | if (model->materials.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8120 | detail::json materials; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8121 | detail::JsonReserveArray(materials, model->materials.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8122 | for (unsigned int i = 0; i < model->materials.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8123 | detail::json material; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8124 | SerializeGltfMaterial(model->materials[i], material); |
Syoyo Fujita | 68adc4b | 2020-10-22 22:27:12 +0900 | [diff] [blame] | 8125 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8126 | if (detail::JsonIsNull(material)) { |
Syoyo Fujita | 68adc4b | 2020-10-22 22:27:12 +0900 | [diff] [blame] | 8127 | // Issue 294. |
| 8128 | // `material` does not have any required parameters |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 8129 | // so the result may be null(unmodified) when all material parameters |
| 8130 | // have default value. |
Syoyo Fujita | 68adc4b | 2020-10-22 22:27:12 +0900 | [diff] [blame] | 8131 | // |
| 8132 | // null is not allowed thus we create an empty JSON object. |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8133 | detail::JsonSetObject(material); |
Syoyo Fujita | 68adc4b | 2020-10-22 22:27:12 +0900 | [diff] [blame] | 8134 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8135 | detail::JsonPushBack(materials, std::move(material)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8136 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8137 | detail::JsonAddMember(o, "materials", std::move(materials)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8138 | } |
| 8139 | |
| 8140 | // MESHES |
| 8141 | if (model->meshes.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8142 | detail::json meshes; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8143 | detail::JsonReserveArray(meshes, model->meshes.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8144 | for (unsigned int i = 0; i < model->meshes.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8145 | detail::json mesh; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8146 | SerializeGltfMesh(model->meshes[i], mesh); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8147 | detail::JsonPushBack(meshes, std::move(mesh)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8148 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8149 | detail::JsonAddMember(o, "meshes", std::move(meshes)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8150 | } |
| 8151 | |
| 8152 | // NODES |
| 8153 | if (model->nodes.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8154 | detail::json nodes; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8155 | detail::JsonReserveArray(nodes, model->nodes.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8156 | for (unsigned int i = 0; i < model->nodes.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8157 | detail::json node; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8158 | SerializeGltfNode(model->nodes[i], node); |
Thomas Gamper | d4ea67c | 2023-11-23 11:59:18 +0100 | [diff] [blame] | 8159 | |
| 8160 | if (detail::JsonIsNull(node)) { |
| 8161 | // Issue 457. |
| 8162 | // `node` does not have any required parameters, |
| 8163 | // so the result may be null(unmodified) when all node parameters |
| 8164 | // have default value. |
| 8165 | // |
| 8166 | // null is not allowed thus we create an empty JSON object. |
| 8167 | detail::JsonSetObject(node); |
| 8168 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8169 | detail::JsonPushBack(nodes, std::move(node)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8170 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8171 | detail::JsonAddMember(o, "nodes", std::move(nodes)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8172 | } |
| 8173 | |
| 8174 | // SCENE |
| 8175 | if (model->defaultScene > -1) { |
| 8176 | SerializeNumberProperty<int>("scene", model->defaultScene, o); |
| 8177 | } |
| 8178 | |
| 8179 | // SCENES |
| 8180 | if (model->scenes.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8181 | detail::json scenes; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8182 | detail::JsonReserveArray(scenes, model->scenes.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8183 | for (unsigned int i = 0; i < model->scenes.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8184 | detail::json currentScene; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8185 | SerializeGltfScene(model->scenes[i], currentScene); |
Thomas Gamper | 3203e19 | 2023-11-23 15:14:46 +0100 | [diff] [blame] | 8186 | if (detail::JsonIsNull(currentScene)) { |
| 8187 | // Issue 464. |
| 8188 | // `scene` does not have any required parameters, |
| 8189 | // so the result may be null(unmodified) when all scene parameters |
| 8190 | // have default value. |
| 8191 | // |
| 8192 | // null is not allowed thus we create an empty JSON object. |
| 8193 | detail::JsonSetObject(currentScene); |
| 8194 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8195 | detail::JsonPushBack(scenes, std::move(currentScene)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8196 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8197 | detail::JsonAddMember(o, "scenes", std::move(scenes)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8198 | } |
| 8199 | |
| 8200 | // SKINS |
| 8201 | if (model->skins.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8202 | detail::json skins; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8203 | detail::JsonReserveArray(skins, model->skins.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8204 | for (unsigned int i = 0; i < model->skins.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8205 | detail::json skin; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8206 | SerializeGltfSkin(model->skins[i], skin); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8207 | detail::JsonPushBack(skins, std::move(skin)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8208 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8209 | detail::JsonAddMember(o, "skins", std::move(skins)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8210 | } |
| 8211 | |
| 8212 | // TEXTURES |
| 8213 | if (model->textures.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8214 | detail::json textures; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8215 | detail::JsonReserveArray(textures, model->textures.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8216 | for (unsigned int i = 0; i < model->textures.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8217 | detail::json texture; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8218 | SerializeGltfTexture(model->textures[i], texture); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8219 | detail::JsonPushBack(textures, std::move(texture)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8220 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8221 | detail::JsonAddMember(o, "textures", std::move(textures)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8222 | } |
| 8223 | |
| 8224 | // SAMPLERS |
| 8225 | if (model->samplers.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8226 | detail::json samplers; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8227 | detail::JsonReserveArray(samplers, model->samplers.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8228 | for (unsigned int i = 0; i < model->samplers.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8229 | detail::json sampler; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8230 | SerializeGltfSampler(model->samplers[i], sampler); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8231 | detail::JsonPushBack(samplers, std::move(sampler)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8232 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8233 | detail::JsonAddMember(o, "samplers", std::move(samplers)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8234 | } |
| 8235 | |
| 8236 | // CAMERAS |
| 8237 | if (model->cameras.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8238 | detail::json cameras; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8239 | detail::JsonReserveArray(cameras, model->cameras.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8240 | for (unsigned int i = 0; i < model->cameras.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8241 | detail::json camera; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8242 | SerializeGltfCamera(model->cameras[i], camera); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8243 | detail::JsonPushBack(cameras, std::move(camera)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8244 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8245 | detail::JsonAddMember(o, "cameras", std::move(cameras)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8246 | } |
| 8247 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 8248 | // EXTRAS & EXTENSIONS |
| 8249 | SerializeExtrasAndExtensions(*model, o); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8250 | |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8251 | auto extensionsUsed = model->extensionsUsed; |
| 8252 | |
| 8253 | // LIGHTS as KHR_lights_punctual |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8254 | if (model->lights.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8255 | detail::json lights; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8256 | detail::JsonReserveArray(lights, model->lights.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8257 | for (unsigned int i = 0; i < model->lights.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8258 | detail::json light; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8259 | SerializeGltfLight(model->lights[i], light); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8260 | detail::JsonPushBack(lights, std::move(light)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8261 | } |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8262 | detail::json khr_lights_cmn; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8263 | detail::JsonAddMember(khr_lights_cmn, "lights", std::move(lights)); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8264 | detail::json ext_j; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8265 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 8266 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8267 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8268 | if (detail::FindMember(o, "extensions", it)) { |
| 8269 | detail::JsonAssign(ext_j, detail::GetValue(it)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 8270 | } |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8271 | } |
| 8272 | |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 8273 | detail::JsonAddMember(ext_j, "KHR_lights_punctual", |
| 8274 | std::move(khr_lights_cmn)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8275 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8276 | detail::JsonAddMember(o, "extensions", std::move(ext_j)); |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8277 | |
| 8278 | // Also add "KHR_lights_punctual" to `extensionsUsed` |
| 8279 | { |
Rahul Sheth | 96a8b2c | 2020-07-10 14:27:46 -0400 | [diff] [blame] | 8280 | auto has_khr_lights_punctual = |
| 8281 | std::find_if(extensionsUsed.begin(), extensionsUsed.end(), |
| 8282 | [](const std::string &s) { |
cwbhhjl | 2f5aa9f | 2020-06-04 10:40:38 +0800 | [diff] [blame] | 8283 | return (s.compare("KHR_lights_punctual") == 0); |
| 8284 | }); |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8285 | |
| 8286 | if (has_khr_lights_punctual == extensionsUsed.end()) { |
| 8287 | extensionsUsed.push_back("KHR_lights_punctual"); |
| 8288 | } |
| 8289 | } |
| 8290 | } |
| 8291 | |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 8292 | // KHR_audio |
| 8293 | if (!model->audioEmitters.empty() || !model->audioSources.empty()) { |
| 8294 | detail::json emitters; |
| 8295 | detail::JsonReserveArray(emitters, model->audioEmitters.size()); |
| 8296 | for (unsigned int i = 0; i < model->audioEmitters.size(); ++i) { |
| 8297 | detail::json emitter; |
| 8298 | SerializeGltfAudioEmitter(model->audioEmitters[i], emitter); |
| 8299 | detail::JsonPushBack(emitters, std::move(emitter)); |
| 8300 | } |
| 8301 | detail::json khr_audio_cmn; |
| 8302 | detail::JsonAddMember(khr_audio_cmn, "emitters", std::move(emitters)); |
| 8303 | |
| 8304 | detail::json sources; |
| 8305 | detail::JsonReserveArray(sources, model->audioSources.size()); |
| 8306 | for (unsigned int i = 0; i < model->audioSources.size(); ++i) { |
| 8307 | detail::json source; |
| 8308 | SerializeGltfAudioSource(model->audioSources[i], source); |
| 8309 | detail::JsonPushBack(sources, std::move(source)); |
| 8310 | } |
| 8311 | detail::JsonAddMember(khr_audio_cmn, "sources", std::move(sources)); |
| 8312 | |
| 8313 | detail::json ext_j; |
| 8314 | { |
| 8315 | detail::json_const_iterator it; |
| 8316 | if (detail::FindMember(o, "extensions", it)) { |
| 8317 | detail::JsonAssign(ext_j, detail::GetValue(it)); |
| 8318 | } |
| 8319 | } |
| 8320 | |
| 8321 | detail::JsonAddMember(ext_j, "KHR_audio", std::move(khr_audio_cmn)); |
| 8322 | |
| 8323 | detail::JsonAddMember(o, "extensions", std::move(ext_j)); |
Baranob_Ilya | c9657be | 2023-06-12 12:34:34 +0400 | [diff] [blame] | 8324 | |
| 8325 | // Also add "KHR_audio" to `extensionsUsed` |
| 8326 | { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 8327 | auto has_khr_audio = std::find_if( |
| 8328 | extensionsUsed.begin(), extensionsUsed.end(), |
| 8329 | [](const std::string &s) { return (s.compare("KHR_audio") == 0); }); |
Baranob_Ilya | c9657be | 2023-06-12 12:34:34 +0400 | [diff] [blame] | 8330 | |
| 8331 | if (has_khr_audio == extensionsUsed.end()) { |
| 8332 | extensionsUsed.push_back("KHR_audio"); |
| 8333 | } |
| 8334 | } |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 8335 | } |
| 8336 | |
Thomas Gamper | b274b34 | 2024-03-25 17:00:41 +0100 | [diff] [blame^] | 8337 | // MSFT_lod |
| 8338 | |
| 8339 | // Look if there is a node that employs MSFT_lod |
| 8340 | auto msft_lod_nodes_it = std::find_if( |
| 8341 | model->nodes.begin(), model->nodes.end(), |
| 8342 | [](const Node& node) { return !node.lods.empty(); }); |
| 8343 | |
| 8344 | // Look if there is a material that employs MSFT_lod |
| 8345 | auto msft_lod_materials_it = std::find_if( |
| 8346 | model->materials.begin(), model->materials.end(), |
| 8347 | [](const Material& material) {return !material.lods.empty(); }); |
| 8348 | |
| 8349 | // If either a node or a material employ MSFT_lod, then we need |
| 8350 | // to add MSFT_lod to the list of used extensions. |
| 8351 | if (msft_lod_nodes_it != model->nodes.end() || msft_lod_materials_it != model->materials.end()) { |
| 8352 | // First check if MSFT_lod is already registered as used extension |
| 8353 | auto has_msft_lod = std::find_if( |
| 8354 | extensionsUsed.begin(), extensionsUsed.end(), |
| 8355 | [](const std::string &s) { return (s.compare("MSFT_lod") == 0); }); |
| 8356 | |
| 8357 | // If MSFT_lod is not registered yet, add it |
| 8358 | if (has_msft_lod == extensionsUsed.end()) { |
| 8359 | extensionsUsed.push_back("MSFT_lod"); |
| 8360 | } |
| 8361 | } |
| 8362 | |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8363 | // Extensions used |
Selmar | 1208f05 | 2021-02-01 19:24:41 +0100 | [diff] [blame] | 8364 | if (extensionsUsed.size()) { |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8365 | SerializeStringArrayProperty("extensionsUsed", extensionsUsed, o); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8366 | } |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8367 | } |
| 8368 | |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 8369 | static bool WriteGltfStream(std::ostream &stream, const std::string &content) { |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8370 | stream << content << std::endl; |
Marco Langer | 7658624 | 2023-03-12 19:26:05 +0100 | [diff] [blame] | 8371 | return stream.good(); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8372 | } |
| 8373 | |
| 8374 | static bool WriteGltfFile(const std::string &output, |
| 8375 | const std::string &content) { |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 8376 | #ifdef _WIN32 |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 8377 | #if defined(_MSC_VER) |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 8378 | std::ofstream gltfFile(UTF8ToWchar(output).c_str()); |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 8379 | #elif defined(__GLIBCXX__) |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8380 | int file_descriptor = _wopen(UTF8ToWchar(output).c_str(), |
| 8381 | _O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY); |
| 8382 | __gnu_cxx::stdio_filebuf<char> wfile_buf( |
| 8383 | file_descriptor, std::ios_base::out | std::ios_base::binary); |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 8384 | std::ostream gltfFile(&wfile_buf); |
| 8385 | if (!wfile_buf.is_open()) return false; |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 8386 | #else |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8387 | std::ofstream gltfFile(output.c_str()); |
| 8388 | if (!gltfFile.is_open()) return false; |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 8389 | #endif |
| 8390 | #else |
| 8391 | std::ofstream gltfFile(output.c_str()); |
| 8392 | if (!gltfFile.is_open()) return false; |
| 8393 | #endif |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8394 | return WriteGltfStream(gltfFile, content); |
| 8395 | } |
| 8396 | |
Syoyo Fujita | 81f7dbe | 2022-08-02 01:05:34 +0900 | [diff] [blame] | 8397 | static bool WriteBinaryGltfStream(std::ostream &stream, |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8398 | const std::string &content, |
| 8399 | const std::vector<unsigned char> &binBuffer) { |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8400 | const std::string header = "glTF"; |
| 8401 | const int version = 2; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8402 | |
Alexander Wood | 190382a | 2021-10-08 12:19:13 -0400 | [diff] [blame] | 8403 | const uint32_t content_size = uint32_t(content.size()); |
| 8404 | const uint32_t binBuffer_size = uint32_t(binBuffer.size()); |
| 8405 | // determine number of padding bytes required to ensure 4 byte alignment |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 8406 | const uint32_t content_padding_size = |
| 8407 | content_size % 4 == 0 ? 0 : 4 - content_size % 4; |
| 8408 | const uint32_t bin_padding_size = |
| 8409 | binBuffer_size % 4 == 0 ? 0 : 4 - binBuffer_size % 4; |
Syoyo Fujita | 1d20520 | 2019-11-16 17:00:17 +0900 | [diff] [blame] | 8410 | |
| 8411 | // 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] | 8412 | // 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] | 8413 | const uint32_t length = |
Alexander Wood | 9e3d1f6 | 2021-10-08 16:57:56 -0400 | [diff] [blame] | 8414 | 12 + 8 + content_size + content_padding_size + |
Alexander Wood | 1380365 | 2021-10-08 20:13:07 -0400 | [diff] [blame] | 8415 | (binBuffer_size ? (8 + binBuffer_size + bin_padding_size) : 0); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8416 | |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 8417 | stream.write(header.c_str(), std::streamsize(header.size())); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8418 | stream.write(reinterpret_cast<const char *>(&version), sizeof(version)); |
| 8419 | stream.write(reinterpret_cast<const char *>(&length), sizeof(length)); |
| 8420 | |
| 8421 | // JSON chunk info, then JSON data |
Alexander Wood | 9e3d1f6 | 2021-10-08 16:57:56 -0400 | [diff] [blame] | 8422 | const uint32_t model_length = uint32_t(content.size()) + content_padding_size; |
Syoyo Fujita | 72f4a55 | 2020-01-08 00:40:41 +0900 | [diff] [blame] | 8423 | const uint32_t model_format = 0x4E4F534A; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8424 | stream.write(reinterpret_cast<const char *>(&model_length), |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 8425 | sizeof(model_length)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8426 | stream.write(reinterpret_cast<const char *>(&model_format), |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 8427 | sizeof(model_format)); |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 8428 | stream.write(content.c_str(), std::streamsize(content.size())); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8429 | |
| 8430 | // Chunk must be multiplies of 4, so pad with spaces |
Alexander Wood | 9e3d1f6 | 2021-10-08 16:57:56 -0400 | [diff] [blame] | 8431 | if (content_padding_size > 0) { |
| 8432 | const std::string padding = std::string(size_t(content_padding_size), ' '); |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 8433 | stream.write(padding.c_str(), std::streamsize(padding.size())); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8434 | } |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 8435 | if (binBuffer.size() > 0) { |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8436 | // BIN chunk info, then BIN data |
Syoyo Fujita | 72f4a55 | 2020-01-08 00:40:41 +0900 | [diff] [blame] | 8437 | const uint32_t bin_length = uint32_t(binBuffer.size()) + bin_padding_size; |
| 8438 | const uint32_t bin_format = 0x004e4942; |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8439 | stream.write(reinterpret_cast<const char *>(&bin_length), |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 8440 | sizeof(bin_length)); |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8441 | stream.write(reinterpret_cast<const char *>(&bin_format), |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 8442 | sizeof(bin_format)); |
| 8443 | stream.write(reinterpret_cast<const char *>(binBuffer.data()), |
| 8444 | std::streamsize(binBuffer.size())); |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8445 | // Chunksize must be multiplies of 4, so pad with zeroes |
| 8446 | if (bin_padding_size > 0) { |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 8447 | const std::vector<unsigned char> padding = |
| 8448 | std::vector<unsigned char>(size_t(bin_padding_size), 0); |
| 8449 | stream.write(reinterpret_cast<const char *>(padding.data()), |
| 8450 | std::streamsize(padding.size())); |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8451 | } |
| 8452 | } |
Syoyo Fujita | 81f7dbe | 2022-08-02 01:05:34 +0900 | [diff] [blame] | 8453 | |
Marco Langer | 7658624 | 2023-03-12 19:26:05 +0100 | [diff] [blame] | 8454 | stream.flush(); |
| 8455 | return stream.good(); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8456 | } |
| 8457 | |
Syoyo Fujita | 81f7dbe | 2022-08-02 01:05:34 +0900 | [diff] [blame] | 8458 | static bool WriteBinaryGltfFile(const std::string &output, |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8459 | const std::string &content, |
| 8460 | const std::vector<unsigned char> &binBuffer) { |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 8461 | #ifdef _WIN32 |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 8462 | #if defined(_MSC_VER) |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 8463 | std::ofstream gltfFile(UTF8ToWchar(output).c_str(), std::ios::binary); |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 8464 | #elif defined(__GLIBCXX__) |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8465 | int file_descriptor = _wopen(UTF8ToWchar(output).c_str(), |
| 8466 | _O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY); |
| 8467 | __gnu_cxx::stdio_filebuf<char> wfile_buf( |
| 8468 | file_descriptor, std::ios_base::out | std::ios_base::binary); |
Syoyo Fujita | 4ab0386 | 2019-11-10 15:31:17 +0900 | [diff] [blame] | 8469 | std::ostream gltfFile(&wfile_buf); |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 8470 | #else |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8471 | std::ofstream gltfFile(output.c_str(), std::ios::binary); |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 8472 | #endif |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8473 | #else |
| 8474 | std::ofstream gltfFile(output.c_str(), std::ios::binary); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 8475 | #endif |
Syoyo Fujita | 81f7dbe | 2022-08-02 01:05:34 +0900 | [diff] [blame] | 8476 | return WriteBinaryGltfStream(gltfFile, content, binBuffer); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8477 | } |
| 8478 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 8479 | bool TinyGLTF::WriteGltfSceneToStream(const Model *model, std::ostream &stream, |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8480 | bool prettyPrint = true, |
| 8481 | bool writeBinary = false) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8482 | detail::JsonDocument output; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8483 | |
| 8484 | /// Serialize all properties except buffers and images. |
| 8485 | SerializeGltfModel(model, output); |
| 8486 | |
| 8487 | // BUFFERS |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8488 | std::vector<unsigned char> binBuffer; |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8489 | if (model->buffers.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8490 | detail::json buffers; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8491 | detail::JsonReserveArray(buffers, model->buffers.size()); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8492 | for (unsigned int i = 0; i < model->buffers.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8493 | detail::json buffer; |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8494 | if (writeBinary && i == 0 && model->buffers[i].uri.empty()) { |
| 8495 | SerializeGltfBufferBin(model->buffers[i], buffer, binBuffer); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8496 | } else { |
| 8497 | SerializeGltfBuffer(model->buffers[i], buffer); |
| 8498 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8499 | detail::JsonPushBack(buffers, std::move(buffer)); |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8500 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8501 | detail::JsonAddMember(output, "buffers", std::move(buffers)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8502 | } |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8503 | |
| 8504 | // IMAGES |
| 8505 | if (model->images.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8506 | detail::json images; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8507 | detail::JsonReserveArray(images, model->images.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8508 | for (unsigned int i = 0; i < model->images.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8509 | detail::json image; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8510 | |
Bryn Lloyd | c704d73 | 2023-06-21 18:42:24 +0200 | [diff] [blame] | 8511 | std::string dummystring; |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8512 | // UpdateImageObject need baseDir but only uses it if embeddedImages is |
| 8513 | // enabled, since we won't write separate images when writing to a stream |
| 8514 | // we |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 8515 | std::string uri; |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 8516 | if (!UpdateImageObject(model->images[i], dummystring, int(i), true, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8517 | &uri_cb, &this->WriteImageData, |
| 8518 | this->write_image_user_data_, &uri)) { |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 8519 | return false; |
| 8520 | } |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 8521 | SerializeGltfImage(model->images[i], uri, image); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8522 | detail::JsonPushBack(images, std::move(image)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8523 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8524 | detail::JsonAddMember(output, "images", std::move(images)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8525 | } |
| 8526 | |
| 8527 | if (writeBinary) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 8528 | return WriteBinaryGltfStream(stream, detail::JsonToString(output), |
| 8529 | binBuffer); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8530 | } else { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 8531 | return WriteGltfStream(stream, |
| 8532 | detail::JsonToString(output, prettyPrint ? 2 : -1)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8533 | } |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8534 | } |
| 8535 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 8536 | bool TinyGLTF::WriteGltfSceneToFile(const Model *model, |
| 8537 | const std::string &filename, |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8538 | bool embedImages = false, |
| 8539 | bool embedBuffers = false, |
| 8540 | bool prettyPrint = true, |
| 8541 | bool writeBinary = false) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8542 | detail::JsonDocument output; |
Selmar Kok | 7cb31e4 | 2018-10-05 16:02:29 +0200 | [diff] [blame] | 8543 | std::string defaultBinFilename = GetBaseFilename(filename); |
| 8544 | std::string defaultBinFileExt = ".bin"; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 8545 | std::string::size_type pos = |
| 8546 | defaultBinFilename.rfind('.', defaultBinFilename.length()); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8547 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 8548 | if (pos != std::string::npos) { |
Selmar Kok | 7cb31e4 | 2018-10-05 16:02:29 +0200 | [diff] [blame] | 8549 | defaultBinFilename = defaultBinFilename.substr(0, pos); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8550 | } |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 8551 | std::string baseDir = GetBaseDir(filename); |
| 8552 | if (baseDir.empty()) { |
| 8553 | baseDir = "./"; |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 8554 | } |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 8555 | /// Serialize all properties except buffers and images. |
| 8556 | SerializeGltfModel(model, output); |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 8557 | |
Selmar Kok | 7cb31e4 | 2018-10-05 16:02:29 +0200 | [diff] [blame] | 8558 | // BUFFERS |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8559 | std::vector<std::string> usedFilenames; |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8560 | std::vector<unsigned char> binBuffer; |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8561 | if (model->buffers.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8562 | detail::json buffers; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8563 | detail::JsonReserveArray(buffers, model->buffers.size()); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8564 | for (unsigned int i = 0; i < model->buffers.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8565 | detail::json buffer; |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8566 | if (writeBinary && i == 0 && model->buffers[i].uri.empty()) { |
| 8567 | SerializeGltfBufferBin(model->buffers[i], buffer, binBuffer); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8568 | } else if (embedBuffers) { |
| 8569 | SerializeGltfBuffer(model->buffers[i], buffer); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 8570 | } else { |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8571 | std::string binSavePath; |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8572 | std::string binFilename; |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8573 | std::string binUri; |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8574 | if (!model->buffers[i].uri.empty() && |
| 8575 | !IsDataURI(model->buffers[i].uri)) { |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8576 | binUri = model->buffers[i].uri; |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8577 | if (!uri_cb.decode(binUri, &binFilename, uri_cb.user_data)) { |
| 8578 | return false; |
| 8579 | } |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8580 | } else { |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8581 | binFilename = defaultBinFilename + defaultBinFileExt; |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8582 | bool inUse = true; |
| 8583 | int numUsed = 0; |
| 8584 | while (inUse) { |
| 8585 | inUse = false; |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8586 | for (const std::string &usedName : usedFilenames) { |
| 8587 | if (binFilename.compare(usedName) != 0) continue; |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8588 | inUse = true; |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8589 | binFilename = defaultBinFilename + std::to_string(numUsed++) + |
| 8590 | defaultBinFileExt; |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8591 | break; |
| 8592 | } |
Selmar Kok | c884e58 | 2018-10-05 16:25:54 +0200 | [diff] [blame] | 8593 | } |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8594 | |
| 8595 | if (uri_cb.encode) { |
| 8596 | if (!uri_cb.encode(binFilename, "buffer", &binUri, |
| 8597 | uri_cb.user_data)) { |
| 8598 | return false; |
| 8599 | } |
| 8600 | } else { |
| 8601 | binUri = binFilename; |
| 8602 | } |
Selmar Kok | c884e58 | 2018-10-05 16:25:54 +0200 | [diff] [blame] | 8603 | } |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8604 | usedFilenames.push_back(binFilename); |
| 8605 | binSavePath = JoinPath(baseDir, binFilename); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8606 | if (!SerializeGltfBuffer(model->buffers[i], buffer, binSavePath, |
| 8607 | binUri)) { |
| 8608 | return false; |
| 8609 | } |
Selmar Kok | c884e58 | 2018-10-05 16:25:54 +0200 | [diff] [blame] | 8610 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8611 | detail::JsonPushBack(buffers, std::move(buffer)); |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 8612 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8613 | detail::JsonAddMember(output, "buffers", std::move(buffers)); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8614 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8615 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 8616 | // IMAGES |
Johan Bowald | faa2722 | 2018-03-28 14:44:45 +0200 | [diff] [blame] | 8617 | if (model->images.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8618 | detail::json images; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8619 | detail::JsonReserveArray(images, model->images.size()); |
Johan Bowald | faa2722 | 2018-03-28 14:44:45 +0200 | [diff] [blame] | 8620 | for (unsigned int i = 0; i < model->images.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8621 | detail::json image; |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 8622 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 8623 | std::string uri; |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 8624 | if (!UpdateImageObject(model->images[i], baseDir, int(i), embedImages, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8625 | &uri_cb, &this->WriteImageData, |
| 8626 | this->write_image_user_data_, &uri)) { |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 8627 | return false; |
| 8628 | } |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 8629 | SerializeGltfImage(model->images[i], uri, image); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8630 | detail::JsonPushBack(images, std::move(image)); |
Johan Bowald | faa2722 | 2018-03-28 14:44:45 +0200 | [diff] [blame] | 8631 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8632 | detail::JsonAddMember(output, "images", std::move(images)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8633 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8634 | |
David Harmon | da9eac2 | 2018-08-30 08:06:05 -0400 | [diff] [blame] | 8635 | if (writeBinary) { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 8636 | return WriteBinaryGltfFile(filename, detail::JsonToString(output), |
| 8637 | binBuffer); |
David Harmon | da9eac2 | 2018-08-30 08:06:05 -0400 | [diff] [blame] | 8638 | } else { |
Bryn Lloyd | 3e98ac4 | 2023-06-21 22:15:49 +0200 | [diff] [blame] | 8639 | return WriteGltfFile(filename, |
| 8640 | detail::JsonToString(output, (prettyPrint ? 2 : -1))); |
David Harmon | da9eac2 | 2018-08-30 08:06:05 -0400 | [diff] [blame] | 8641 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8642 | } |
| 8643 | |
Syoyo Fujita | e1bef8c | 2016-03-17 21:31:13 +0900 | [diff] [blame] | 8644 | } // namespace tinygltf |
| 8645 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 8646 | #ifdef __clang__ |
| 8647 | #pragma clang diagnostic pop |
| 8648 | #endif |
| 8649 | |
Syoyo Fujita | 612e578 | 2022-09-18 21:01:39 +0900 | [diff] [blame] | 8650 | #endif // TINYGLTF_IMPLEMENTATION |