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 | |
zbendefy | 69eeea1 | 2022-09-05 23:54:57 +0200 | [diff] [blame] | 51 | //Auto-detect C++14 standard version |
| 52 | #if !defined(TINYGLTF_USE_CPP14) && defined(__cplusplus) && (__cplusplus >= 201402L) |
| 53 | #define TINYGLTF_USE_CPP14 |
| 54 | #endif |
| 55 | |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 56 | #ifdef __ANDROID__ |
| 57 | #ifdef TINYGLTF_ANDROID_LOAD_FROM_ASSETS |
| 58 | #include <android/asset_manager.h> |
| 59 | #endif |
| 60 | #endif |
| 61 | |
Selmar Kok | 79e3df2 | 2019-10-29 16:22:07 +0100 | [diff] [blame] | 62 | #ifdef __GNUC__ |
| 63 | #if (__GNUC__ < 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ <= 8)) |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 64 | #define TINYGLTF_NOEXCEPT |
Selmar Kok | 79e3df2 | 2019-10-29 16:22:07 +0100 | [diff] [blame] | 65 | #else |
| 66 | #define TINYGLTF_NOEXCEPT noexcept |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 67 | #endif |
Selmar Kok | 79e3df2 | 2019-10-29 16:22:07 +0100 | [diff] [blame] | 68 | #else |
| 69 | #define TINYGLTF_NOEXCEPT noexcept |
| 70 | #endif |
| 71 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 72 | #define DEFAULT_METHODS(x) \ |
| 73 | ~x() = default; \ |
| 74 | x(const x &) = default; \ |
| 75 | x(x &&) TINYGLTF_NOEXCEPT = default; \ |
| 76 | x &operator=(const x &) = default; \ |
| 77 | x &operator=(x &&) TINYGLTF_NOEXCEPT = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 78 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 79 | namespace tinygltf { |
| 80 | |
| 81 | #define TINYGLTF_MODE_POINTS (0) |
| 82 | #define TINYGLTF_MODE_LINE (1) |
| 83 | #define TINYGLTF_MODE_LINE_LOOP (2) |
Thomas Tissot | 6c4a006 | 2019-01-30 13:10:51 +0100 | [diff] [blame] | 84 | #define TINYGLTF_MODE_LINE_STRIP (3) |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 85 | #define TINYGLTF_MODE_TRIANGLES (4) |
| 86 | #define TINYGLTF_MODE_TRIANGLE_STRIP (5) |
| 87 | #define TINYGLTF_MODE_TRIANGLE_FAN (6) |
| 88 | |
| 89 | #define TINYGLTF_COMPONENT_TYPE_BYTE (5120) |
| 90 | #define TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE (5121) |
| 91 | #define TINYGLTF_COMPONENT_TYPE_SHORT (5122) |
| 92 | #define TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT (5123) |
| 93 | #define TINYGLTF_COMPONENT_TYPE_INT (5124) |
| 94 | #define TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT (5125) |
| 95 | #define TINYGLTF_COMPONENT_TYPE_FLOAT (5126) |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 96 | #define TINYGLTF_COMPONENT_TYPE_DOUBLE \ |
| 97 | (5130) // OpenGL double type. Note that some of glTF 2.0 validator does not |
| 98 | // support double type even the schema seems allow any value of |
| 99 | // integer: |
| 100 | // 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] | 101 | |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 102 | #define TINYGLTF_TEXTURE_FILTER_NEAREST (9728) |
| 103 | #define TINYGLTF_TEXTURE_FILTER_LINEAR (9729) |
| 104 | #define TINYGLTF_TEXTURE_FILTER_NEAREST_MIPMAP_NEAREST (9984) |
| 105 | #define TINYGLTF_TEXTURE_FILTER_LINEAR_MIPMAP_NEAREST (9985) |
| 106 | #define TINYGLTF_TEXTURE_FILTER_NEAREST_MIPMAP_LINEAR (9986) |
| 107 | #define TINYGLTF_TEXTURE_FILTER_LINEAR_MIPMAP_LINEAR (9987) |
| 108 | |
Cemalettin Dervis | 246d866 | 2017-12-07 20:29:51 +0100 | [diff] [blame] | 109 | #define TINYGLTF_TEXTURE_WRAP_REPEAT (10497) |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 110 | #define TINYGLTF_TEXTURE_WRAP_CLAMP_TO_EDGE (33071) |
Luke San Antonio Bialecki | 904612c | 2016-08-19 17:28:03 -0400 | [diff] [blame] | 111 | #define TINYGLTF_TEXTURE_WRAP_MIRRORED_REPEAT (33648) |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 112 | |
Luke San Antonio | cdf4cb7 | 2016-06-14 21:32:11 -0400 | [diff] [blame] | 113 | // Redeclarations of the above for technique.parameters. |
| 114 | #define TINYGLTF_PARAMETER_TYPE_BYTE (5120) |
| 115 | #define TINYGLTF_PARAMETER_TYPE_UNSIGNED_BYTE (5121) |
| 116 | #define TINYGLTF_PARAMETER_TYPE_SHORT (5122) |
| 117 | #define TINYGLTF_PARAMETER_TYPE_UNSIGNED_SHORT (5123) |
| 118 | #define TINYGLTF_PARAMETER_TYPE_INT (5124) |
| 119 | #define TINYGLTF_PARAMETER_TYPE_UNSIGNED_INT (5125) |
| 120 | #define TINYGLTF_PARAMETER_TYPE_FLOAT (5126) |
| 121 | |
| 122 | #define TINYGLTF_PARAMETER_TYPE_FLOAT_VEC2 (35664) |
| 123 | #define TINYGLTF_PARAMETER_TYPE_FLOAT_VEC3 (35665) |
| 124 | #define TINYGLTF_PARAMETER_TYPE_FLOAT_VEC4 (35666) |
| 125 | |
| 126 | #define TINYGLTF_PARAMETER_TYPE_INT_VEC2 (35667) |
| 127 | #define TINYGLTF_PARAMETER_TYPE_INT_VEC3 (35668) |
| 128 | #define TINYGLTF_PARAMETER_TYPE_INT_VEC4 (35669) |
| 129 | |
| 130 | #define TINYGLTF_PARAMETER_TYPE_BOOL (35670) |
| 131 | #define TINYGLTF_PARAMETER_TYPE_BOOL_VEC2 (35671) |
| 132 | #define TINYGLTF_PARAMETER_TYPE_BOOL_VEC3 (35672) |
| 133 | #define TINYGLTF_PARAMETER_TYPE_BOOL_VEC4 (35673) |
| 134 | |
| 135 | #define TINYGLTF_PARAMETER_TYPE_FLOAT_MAT2 (35674) |
| 136 | #define TINYGLTF_PARAMETER_TYPE_FLOAT_MAT3 (35675) |
| 137 | #define TINYGLTF_PARAMETER_TYPE_FLOAT_MAT4 (35676) |
| 138 | |
| 139 | #define TINYGLTF_PARAMETER_TYPE_SAMPLER_2D (35678) |
| 140 | |
| 141 | // End parameter types |
| 142 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 143 | #define TINYGLTF_TYPE_VEC2 (2) |
| 144 | #define TINYGLTF_TYPE_VEC3 (3) |
| 145 | #define TINYGLTF_TYPE_VEC4 (4) |
| 146 | #define TINYGLTF_TYPE_MAT2 (32 + 2) |
| 147 | #define TINYGLTF_TYPE_MAT3 (32 + 3) |
| 148 | #define TINYGLTF_TYPE_MAT4 (32 + 4) |
| 149 | #define TINYGLTF_TYPE_SCALAR (64 + 1) |
| 150 | #define TINYGLTF_TYPE_VECTOR (64 + 4) |
| 151 | #define TINYGLTF_TYPE_MATRIX (64 + 16) |
| 152 | |
| 153 | #define TINYGLTF_IMAGE_FORMAT_JPEG (0) |
| 154 | #define TINYGLTF_IMAGE_FORMAT_PNG (1) |
| 155 | #define TINYGLTF_IMAGE_FORMAT_BMP (2) |
| 156 | #define TINYGLTF_IMAGE_FORMAT_GIF (3) |
| 157 | |
Luke San Antonio | 6d616f5 | 2016-06-23 14:09:23 -0400 | [diff] [blame] | 158 | #define TINYGLTF_TEXTURE_FORMAT_ALPHA (6406) |
| 159 | #define TINYGLTF_TEXTURE_FORMAT_RGB (6407) |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 160 | #define TINYGLTF_TEXTURE_FORMAT_RGBA (6408) |
Luke San Antonio | 6d616f5 | 2016-06-23 14:09:23 -0400 | [diff] [blame] | 161 | #define TINYGLTF_TEXTURE_FORMAT_LUMINANCE (6409) |
| 162 | #define TINYGLTF_TEXTURE_FORMAT_LUMINANCE_ALPHA (6410) |
| 163 | |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 164 | #define TINYGLTF_TEXTURE_TARGET_TEXTURE2D (3553) |
| 165 | #define TINYGLTF_TEXTURE_TYPE_UNSIGNED_BYTE (5121) |
| 166 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 167 | #define TINYGLTF_TARGET_ARRAY_BUFFER (34962) |
| 168 | #define TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER (34963) |
| 169 | |
Luke San Antonio | cdf4cb7 | 2016-06-14 21:32:11 -0400 | [diff] [blame] | 170 | #define TINYGLTF_SHADER_TYPE_VERTEX_SHADER (35633) |
| 171 | #define TINYGLTF_SHADER_TYPE_FRAGMENT_SHADER (35632) |
| 172 | |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 173 | #define TINYGLTF_DOUBLE_EPS (1.e-12) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 174 | #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] | 175 | |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 176 | #ifdef __ANDROID__ |
| 177 | #ifdef TINYGLTF_ANDROID_LOAD_FROM_ASSETS |
Serdar Kocdemir | 264ae4c | 2022-10-30 22:32:59 +0000 | [diff] [blame] | 178 | #ifdef TINYGLTF_IMPLEMENTATION |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 179 | AAssetManager *asset_manager = nullptr; |
Serdar Kocdemir | 264ae4c | 2022-10-30 22:32:59 +0000 | [diff] [blame] | 180 | #else |
| 181 | extern AAssetManager *asset_manager; |
| 182 | #endif |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 183 | #endif |
| 184 | #endif |
| 185 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 186 | typedef enum { |
Idriss Chaouch | 425195b | 2021-05-20 12:11:00 +0100 | [diff] [blame] | 187 | NULL_TYPE, |
| 188 | REAL_TYPE, |
| 189 | INT_TYPE, |
| 190 | BOOL_TYPE, |
| 191 | STRING_TYPE, |
| 192 | ARRAY_TYPE, |
| 193 | BINARY_TYPE, |
| 194 | OBJECT_TYPE |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 195 | } Type; |
| 196 | |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 197 | static inline int32_t GetComponentSizeInBytes(uint32_t componentType) { |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 198 | if (componentType == TINYGLTF_COMPONENT_TYPE_BYTE) { |
| 199 | return 1; |
| 200 | } else if (componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE) { |
| 201 | return 1; |
| 202 | } else if (componentType == TINYGLTF_COMPONENT_TYPE_SHORT) { |
| 203 | return 2; |
| 204 | } else if (componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT) { |
| 205 | return 2; |
| 206 | } else if (componentType == TINYGLTF_COMPONENT_TYPE_INT) { |
| 207 | return 4; |
| 208 | } else if (componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT) { |
| 209 | return 4; |
| 210 | } else if (componentType == TINYGLTF_COMPONENT_TYPE_FLOAT) { |
| 211 | return 4; |
| 212 | } else if (componentType == TINYGLTF_COMPONENT_TYPE_DOUBLE) { |
| 213 | return 8; |
| 214 | } else { |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 215 | // Unknown component type |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 216 | return -1; |
| 217 | } |
| 218 | } |
| 219 | |
DingboDingboDingbo | 83ccb9f | 2019-08-29 18:49:15 -0400 | [diff] [blame] | 220 | static inline int32_t GetNumComponentsInType(uint32_t ty) { |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 221 | if (ty == TINYGLTF_TYPE_SCALAR) { |
| 222 | return 1; |
| 223 | } else if (ty == TINYGLTF_TYPE_VEC2) { |
| 224 | return 2; |
| 225 | } else if (ty == TINYGLTF_TYPE_VEC3) { |
| 226 | return 3; |
| 227 | } else if (ty == TINYGLTF_TYPE_VEC4) { |
| 228 | return 4; |
| 229 | } else if (ty == TINYGLTF_TYPE_MAT2) { |
| 230 | return 4; |
| 231 | } else if (ty == TINYGLTF_TYPE_MAT3) { |
| 232 | return 9; |
| 233 | } else if (ty == TINYGLTF_TYPE_MAT4) { |
| 234 | return 16; |
| 235 | } else { |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 236 | // Unknown component type |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 237 | return -1; |
| 238 | } |
| 239 | } |
| 240 | |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 241 | // TODO(syoyo): Move these functions to TinyGLTF class |
Selmar Kok | 0d0e97e | 2018-08-22 14:01:57 +0200 | [diff] [blame] | 242 | bool IsDataURI(const std::string &in); |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 243 | bool DecodeDataURI(std::vector<unsigned char> *out, std::string &mime_type, |
| 244 | const std::string &in, size_t reqBytes, bool checkSize); |
Selmar Kok | 0d0e97e | 2018-08-22 14:01:57 +0200 | [diff] [blame] | 245 | |
Syoyo Fujita | c8d3c76 | 2017-05-25 01:42:38 +0900 | [diff] [blame] | 246 | #ifdef __clang__ |
| 247 | #pragma clang diagnostic push |
| 248 | // Suppress warning for : static Value null_value |
Syoyo Fujita | c8d3c76 | 2017-05-25 01:42:38 +0900 | [diff] [blame] | 249 | #pragma clang diagnostic ignored "-Wexit-time-destructors" |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 250 | #pragma clang diagnostic ignored "-Wpadded" |
Syoyo Fujita | c8d3c76 | 2017-05-25 01:42:38 +0900 | [diff] [blame] | 251 | #endif |
| 252 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 253 | // Simple class to represent JSON object |
| 254 | class Value { |
| 255 | public: |
| 256 | typedef std::vector<Value> Array; |
| 257 | typedef std::map<std::string, Value> Object; |
| 258 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 259 | Value() |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 260 | |
| 261 | {} |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 262 | |
| 263 | explicit Value(bool b) : type_(BOOL_TYPE) { boolean_value_ = b; } |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 264 | explicit Value(int i) : type_(INT_TYPE) { |
| 265 | int_value_ = i; |
| 266 | real_value_ = i; |
| 267 | } |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 268 | explicit Value(double n) : type_(REAL_TYPE) { real_value_ = n; } |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 269 | explicit Value(const std::string &s) : type_(STRING_TYPE) { |
| 270 | string_value_ = s; |
| 271 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 272 | explicit Value(std::string &&s) |
| 273 | : type_(STRING_TYPE), string_value_(std::move(s)) {} |
David Siegel | 49caa65 | 2023-04-08 23:44:53 +0200 | [diff] [blame] | 274 | explicit Value(const char *s) : type_(STRING_TYPE) { |
| 275 | string_value_ = s; |
| 276 | } |
Syoyo Fujita | 7680abf | 2016-10-17 18:02:45 +0900 | [diff] [blame] | 277 | explicit Value(const unsigned char *p, size_t n) : type_(BINARY_TYPE) { |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 278 | binary_value_.resize(n); |
| 279 | memcpy(binary_value_.data(), p, n); |
| 280 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 281 | explicit Value(std::vector<unsigned char> &&v) noexcept |
| 282 | : type_(BINARY_TYPE), |
| 283 | binary_value_(std::move(v)) {} |
| 284 | explicit Value(const Array &a) : type_(ARRAY_TYPE) { array_value_ = a; } |
| 285 | explicit Value(Array &&a) noexcept : type_(ARRAY_TYPE), |
| 286 | array_value_(std::move(a)) {} |
jrkoonce | d1e1472 | 2019-08-27 11:51:02 -0500 | [diff] [blame] | 287 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 288 | explicit Value(const Object &o) : type_(OBJECT_TYPE) { object_value_ = o; } |
| 289 | explicit Value(Object &&o) noexcept : type_(OBJECT_TYPE), |
| 290 | object_value_(std::move(o)) {} |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 291 | |
| 292 | DEFAULT_METHODS(Value) |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 293 | |
Hill Ma | d1e3286 | 2021-02-20 22:30:44 -0800 | [diff] [blame] | 294 | char Type() const { return static_cast<char>(type_); } |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 295 | |
| 296 | bool IsBool() const { return (type_ == BOOL_TYPE); } |
| 297 | |
| 298 | bool IsInt() const { return (type_ == INT_TYPE); } |
| 299 | |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 300 | bool IsNumber() const { return (type_ == REAL_TYPE) || (type_ == INT_TYPE); } |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 301 | |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 302 | bool IsReal() const { return (type_ == REAL_TYPE); } |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 303 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 304 | bool IsString() const { return (type_ == STRING_TYPE); } |
| 305 | |
| 306 | bool IsBinary() const { return (type_ == BINARY_TYPE); } |
| 307 | |
| 308 | bool IsArray() const { return (type_ == ARRAY_TYPE); } |
| 309 | |
| 310 | bool IsObject() const { return (type_ == OBJECT_TYPE); } |
| 311 | |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 312 | // Use this function if you want to have number value as double. |
| 313 | double GetNumberAsDouble() const { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 314 | if (type_ == INT_TYPE) { |
| 315 | return double(int_value_); |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 316 | } else { |
| 317 | return real_value_; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 318 | } |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 319 | } |
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 | // Use this function if you want to have number value as int. |
Syoyo Fujita | ff0a2e9 | 2020-05-11 19:07:00 +0900 | [diff] [blame] | 322 | // TODO(syoyo): Support int value larger than 32 bits |
| 323 | int GetNumberAsInt() const { |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 324 | if (type_ == REAL_TYPE) { |
| 325 | return int(real_value_); |
| 326 | } else { |
| 327 | return int_value_; |
| 328 | } |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 329 | } |
| 330 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 331 | // Accessor |
| 332 | template <typename T> |
| 333 | const T &Get() const; |
| 334 | template <typename T> |
| 335 | T &Get(); |
| 336 | |
| 337 | // Lookup value from an array |
Syoyo Fujita | 7680abf | 2016-10-17 18:02:45 +0900 | [diff] [blame] | 338 | const Value &Get(int idx) const { |
Syoyo Fujita | c8d3c76 | 2017-05-25 01:42:38 +0900 | [diff] [blame] | 339 | static Value null_value; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 340 | assert(IsArray()); |
| 341 | assert(idx >= 0); |
Syoyo Fujita | 7680abf | 2016-10-17 18:02:45 +0900 | [diff] [blame] | 342 | return (static_cast<size_t>(idx) < array_value_.size()) |
| 343 | ? array_value_[static_cast<size_t>(idx)] |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 344 | : null_value; |
| 345 | } |
| 346 | |
| 347 | // Lookup value from a key-value pair |
| 348 | const Value &Get(const std::string &key) const { |
Syoyo Fujita | c8d3c76 | 2017-05-25 01:42:38 +0900 | [diff] [blame] | 349 | static Value null_value; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 350 | assert(IsObject()); |
| 351 | Object::const_iterator it = object_value_.find(key); |
| 352 | return (it != object_value_.end()) ? it->second : null_value; |
| 353 | } |
| 354 | |
| 355 | size_t ArrayLen() const { |
| 356 | if (!IsArray()) return 0; |
| 357 | return array_value_.size(); |
| 358 | } |
| 359 | |
| 360 | // Valid only for object type. |
| 361 | bool Has(const std::string &key) const { |
| 362 | if (!IsObject()) return false; |
| 363 | Object::const_iterator it = object_value_.find(key); |
| 364 | return (it != object_value_.end()) ? true : false; |
| 365 | } |
| 366 | |
| 367 | // List keys |
| 368 | std::vector<std::string> Keys() const { |
| 369 | std::vector<std::string> keys; |
| 370 | if (!IsObject()) return keys; // empty |
| 371 | |
| 372 | for (Object::const_iterator it = object_value_.begin(); |
| 373 | it != object_value_.end(); ++it) { |
| 374 | keys.push_back(it->first); |
| 375 | } |
| 376 | |
| 377 | return keys; |
| 378 | } |
| 379 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 380 | size_t Size() const { return (IsArray() ? ArrayLen() : Keys().size()); } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 381 | |
| 382 | bool operator==(const tinygltf::Value &other) const; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 383 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 384 | protected: |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 385 | int type_ = NULL_TYPE; |
Syoyo Fujita | 7680abf | 2016-10-17 18:02:45 +0900 | [diff] [blame] | 386 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 387 | int int_value_ = 0; |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 388 | double real_value_ = 0.0; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 389 | std::string string_value_; |
Syoyo Fujita | 7680abf | 2016-10-17 18:02:45 +0900 | [diff] [blame] | 390 | std::vector<unsigned char> binary_value_; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 391 | Array array_value_; |
| 392 | Object object_value_; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 393 | bool boolean_value_ = false; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 394 | }; |
| 395 | |
Syoyo Fujita | c8d3c76 | 2017-05-25 01:42:38 +0900 | [diff] [blame] | 396 | #ifdef __clang__ |
| 397 | #pragma clang diagnostic pop |
| 398 | #endif |
| 399 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 400 | #define TINYGLTF_VALUE_GET(ctype, var) \ |
| 401 | template <> \ |
| 402 | inline const ctype &Value::Get<ctype>() const { \ |
| 403 | return var; \ |
| 404 | } \ |
| 405 | template <> \ |
| 406 | inline ctype &Value::Get<ctype>() { \ |
| 407 | return var; \ |
| 408 | } |
| 409 | TINYGLTF_VALUE_GET(bool, boolean_value_) |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 410 | TINYGLTF_VALUE_GET(double, real_value_) |
Syoyo Fujita | 7680abf | 2016-10-17 18:02:45 +0900 | [diff] [blame] | 411 | TINYGLTF_VALUE_GET(int, int_value_) |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 412 | TINYGLTF_VALUE_GET(std::string, string_value_) |
Syoyo Fujita | 7680abf | 2016-10-17 18:02:45 +0900 | [diff] [blame] | 413 | TINYGLTF_VALUE_GET(std::vector<unsigned char>, binary_value_) |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 414 | TINYGLTF_VALUE_GET(Value::Array, array_value_) |
| 415 | TINYGLTF_VALUE_GET(Value::Object, object_value_) |
| 416 | #undef TINYGLTF_VALUE_GET |
| 417 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 418 | #ifdef __clang__ |
| 419 | #pragma clang diagnostic push |
| 420 | #pragma clang diagnostic ignored "-Wc++98-compat" |
| 421 | #pragma clang diagnostic ignored "-Wpadded" |
| 422 | #endif |
| 423 | |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 424 | /// Aggregate object for representing a color |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 425 | using ColorValue = std::array<double, 4>; |
| 426 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 427 | // === legacy interface ==== |
| 428 | // TODO(syoyo): Deprecate `Parameter` class. |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 429 | struct Parameter { |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 430 | bool bool_value = false; |
Ben Buzbee | f6af224 | 2018-04-25 15:13:05 -0700 | [diff] [blame] | 431 | bool has_number_value = false; |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 432 | std::string string_value; |
| 433 | std::vector<double> number_array; |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 434 | std::map<std::string, double> json_double_value; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 435 | double number_value = 0.0; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 436 | |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 437 | // context sensitive methods. depending the type of the Parameter you are |
| 438 | // accessing, these are either valid or not |
| 439 | // If this parameter represent a texture map in a material, will return the |
| 440 | // texture index |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 441 | |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 442 | /// Return the index of a texture if this Parameter is a texture map. |
| 443 | /// Returned value is only valid if the parameter represent a texture from a |
| 444 | /// material |
Arthur Brainville | 41fe772 | 2018-01-08 18:32:48 +0100 | [diff] [blame] | 445 | int TextureIndex() const { |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 446 | const auto it = json_double_value.find("index"); |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 447 | if (it != std::end(json_double_value)) { |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 448 | return int(it->second); |
| 449 | } |
| 450 | return -1; |
| 451 | } |
| 452 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 453 | /// Return the index of a texture coordinate set if this Parameter is a |
| 454 | /// texture map. Returned value is only valid if the parameter represent a |
| 455 | /// texture from a material |
Sascha Willems | eb01106 | 2019-02-23 21:15:45 +0100 | [diff] [blame] | 456 | int TextureTexCoord() const { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 457 | const auto it = json_double_value.find("texCoord"); |
| 458 | if (it != std::end(json_double_value)) { |
| 459 | return int(it->second); |
| 460 | } |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 461 | // 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] | 462 | return 0; |
Sascha Willems | eb01106 | 2019-02-23 21:15:45 +0100 | [diff] [blame] | 463 | } |
| 464 | |
Christophe | 820ede8 | 2019-07-04 15:21:21 +0900 | [diff] [blame] | 465 | /// Return the scale of a texture if this Parameter is a normal texture map. |
| 466 | /// Returned value is only valid if the parameter represent a normal texture |
| 467 | /// from a material |
| 468 | double TextureScale() const { |
| 469 | const auto it = json_double_value.find("scale"); |
| 470 | if (it != std::end(json_double_value)) { |
| 471 | return it->second; |
| 472 | } |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 473 | // As per the spec, if scale is omitted, this parameter is 1 |
Arthur Brainville | 8a98d98 | 2019-07-05 00:26:02 +0200 | [diff] [blame] | 474 | return 1; |
| 475 | } |
Arthur Brianville (Ybalrid) | 2a9d9de | 2019-07-05 00:30:47 +0200 | [diff] [blame] | 476 | |
Arthur Brainville | 8a98d98 | 2019-07-05 00:26:02 +0200 | [diff] [blame] | 477 | /// Return the strength of a texture if this Parameter is a an occlusion map. |
| 478 | /// Returned value is only valid if the parameter represent an occlusion map |
| 479 | /// from a material |
| 480 | double TextureStrength() const { |
| 481 | const auto it = json_double_value.find("strength"); |
| 482 | if (it != std::end(json_double_value)) { |
| 483 | return it->second; |
| 484 | } |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 485 | // As per the spec, if strength is omitted, this parameter is 1 |
Arthur Brainville | 8a98d98 | 2019-07-05 00:26:02 +0200 | [diff] [blame] | 486 | return 1; |
Christophe | 820ede8 | 2019-07-04 15:21:21 +0900 | [diff] [blame] | 487 | } |
| 488 | |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 489 | /// Material factor, like the roughness or metalness of a material |
| 490 | /// Returned value is only valid if the parameter represent a texture from a |
| 491 | /// material |
Ben Buzbee | f6af224 | 2018-04-25 15:13:05 -0700 | [diff] [blame] | 492 | double Factor() const { return number_value; } |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 493 | |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 494 | /// Return the color of a material |
| 495 | /// Returned value is only valid if the parameter represent a texture from a |
| 496 | /// material |
Arthur Brainville | 9585391 | 2018-01-08 18:37:44 +0100 | [diff] [blame] | 497 | ColorValue ColorFactor() const { |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 498 | return { |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 499 | {// this aggregate initialize the std::array object, and uses C++11 RVO. |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 500 | number_array[0], number_array[1], number_array[2], |
| 501 | (number_array.size() > 3 ? number_array[3] : 1.0)}}; |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 502 | } |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 503 | |
Selmar Kok | ff2b1f9 | 2019-10-21 17:58:09 +0200 | [diff] [blame] | 504 | Parameter() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 505 | DEFAULT_METHODS(Parameter) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 506 | bool operator==(const Parameter &) const; |
Arthur Brainville | 5845319 | 2018-01-08 18:25:52 +0100 | [diff] [blame] | 507 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 508 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 509 | #ifdef __clang__ |
| 510 | #pragma clang diagnostic pop |
| 511 | #endif |
| 512 | |
| 513 | #ifdef __clang__ |
| 514 | #pragma clang diagnostic push |
| 515 | #pragma clang diagnostic ignored "-Wpadded" |
| 516 | #endif |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 517 | |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 518 | typedef std::map<std::string, Parameter> ParameterMap; |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 519 | typedef std::map<std::string, Value> ExtensionMap; |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 520 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 521 | struct AnimationChannel { |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 522 | int sampler{-1}; // required |
| 523 | int target_node{-1}; // optional index of the node to target (alternative |
Jack Mousseau | 283b552 | 2023-01-15 11:45:45 -0800 | [diff] [blame] | 524 | // target should be provided by extension) |
| 525 | std::string target_path; // required with standard values of ["translation", |
| 526 | // "rotation", "scale", "weights"] |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 527 | Value extras; |
Selmar Kok | 4e2988e | 2019-08-16 14:08:08 +0200 | [diff] [blame] | 528 | ExtensionMap extensions; |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 529 | Value target_extras; |
Selmar Kok | 973d9b3 | 2020-01-21 18:45:24 +0100 | [diff] [blame] | 530 | ExtensionMap target_extensions; |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 531 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 532 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 533 | std::string extras_json_string; |
| 534 | std::string extensions_json_string; |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 535 | std::string target_extras_json_string; |
Selmar Kok | 973d9b3 | 2020-01-21 18:45:24 +0100 | [diff] [blame] | 536 | std::string target_extensions_json_string; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 537 | |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 538 | AnimationChannel() {} |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 539 | DEFAULT_METHODS(AnimationChannel) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 540 | bool operator==(const AnimationChannel &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 541 | }; |
| 542 | |
| 543 | struct AnimationSampler { |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 544 | int input{-1}; // required |
| 545 | int output{-1}; // required |
Arthur Brianville (Ybalrid) | 2a9d9de | 2019-07-05 00:30:47 +0200 | [diff] [blame] | 546 | std::string interpolation; // "LINEAR", "STEP","CUBICSPLINE" or user defined |
| 547 | // string. default "LINEAR" |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 548 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 549 | ExtensionMap extensions; |
| 550 | |
| 551 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 552 | std::string extras_json_string; |
| 553 | std::string extensions_json_string; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 554 | |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 555 | AnimationSampler() : interpolation("LINEAR") {} |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 556 | DEFAULT_METHODS(AnimationSampler) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 557 | bool operator==(const AnimationSampler &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 558 | }; |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 559 | |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 560 | struct Animation { |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 561 | std::string name; |
| 562 | std::vector<AnimationChannel> channels; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 563 | std::vector<AnimationSampler> samplers; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 564 | Value extras; |
Selmar Kok | 4e2988e | 2019-08-16 14:08:08 +0200 | [diff] [blame] | 565 | ExtensionMap extensions; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 566 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 567 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 568 | std::string extras_json_string; |
| 569 | std::string extensions_json_string; |
| 570 | |
Selmar Kok | ff2b1f9 | 2019-10-21 17:58:09 +0200 | [diff] [blame] | 571 | Animation() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 572 | DEFAULT_METHODS(Animation) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 573 | bool operator==(const Animation &) const; |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 574 | }; |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 575 | |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 576 | struct Skin { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 577 | std::string name; |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 578 | int inverseBindMatrices; // required here but not in the spec |
| 579 | int skeleton; // The index of the node used as a skeleton root |
| 580 | std::vector<int> joints; // Indices of skeleton nodes |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 581 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 582 | Value extras; |
| 583 | ExtensionMap extensions; |
| 584 | |
| 585 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 586 | std::string extras_json_string; |
| 587 | std::string extensions_json_string; |
| 588 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 589 | Skin() { |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 590 | inverseBindMatrices = -1; |
Aurélien Chatelain | 8d5e0a7 | 2017-06-19 13:52:08 +0000 | [diff] [blame] | 591 | skeleton = -1; |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 592 | } |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 593 | DEFAULT_METHODS(Skin) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 594 | bool operator==(const Skin &) const; |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 595 | }; |
| 596 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 597 | struct Sampler { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 598 | std::string name; |
Syoyo Fujita | ee179b2 | 2019-08-16 13:11:30 +0900 | [diff] [blame] | 599 | // glTF 2.0 spec does not define default value for `minFilter` and |
| 600 | // `magFilter`. Set -1 in TinyGLTF(issue #186) |
| 601 | int minFilter = |
| 602 | -1; // optional. -1 = no filter defined. ["NEAREST", "LINEAR", |
zz | 8a35b8f | 2021-05-14 13:49:33 +0800 | [diff] [blame] | 603 | // "NEAREST_MIPMAP_NEAREST", "LINEAR_MIPMAP_NEAREST", |
Syoyo Fujita | ee179b2 | 2019-08-16 13:11:30 +0900 | [diff] [blame] | 604 | // "NEAREST_MIPMAP_LINEAR", "LINEAR_MIPMAP_LINEAR"] |
| 605 | int magFilter = |
| 606 | -1; // optional. -1 = no filter defined. ["NEAREST", "LINEAR"] |
| 607 | int wrapS = |
| 608 | TINYGLTF_TEXTURE_WRAP_REPEAT; // ["CLAMP_TO_EDGE", "MIRRORED_REPEAT", |
| 609 | // "REPEAT"], default "REPEAT" |
| 610 | int wrapT = |
| 611 | TINYGLTF_TEXTURE_WRAP_REPEAT; // ["CLAMP_TO_EDGE", "MIRRORED_REPEAT", |
| 612 | // "REPEAT"], default "REPEAT" |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 613 | // int wrapR = TINYGLTF_TEXTURE_WRAP_REPEAT; // TinyGLTF extension. currently |
| 614 | // not used. |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 615 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 616 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 617 | ExtensionMap extensions; |
| 618 | |
| 619 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 620 | std::string extras_json_string; |
| 621 | std::string extensions_json_string; |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 622 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 623 | Sampler() |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 624 | |
| 625 | {} |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 626 | DEFAULT_METHODS(Sampler) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 627 | bool operator==(const Sampler &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 628 | }; |
| 629 | |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 630 | struct Image { |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 631 | std::string name; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 632 | int width; |
| 633 | int height; |
| 634 | int component; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 635 | int bits; // bit depth per channel. 8(byte), 16 or 32. |
| 636 | int pixel_type; // pixel type(TINYGLTF_COMPONENT_TYPE_***). usually |
| 637 | // UBYTE(bits = 8) or USHORT(bits = 16) |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 638 | std::vector<unsigned char> image; |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 639 | int bufferView; // (required if no uri) |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 640 | std::string mimeType; // (required if no uri) ["image/jpeg", "image/png", |
| 641 | // "image/bmp", "image/gif"] |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 642 | std::string uri; // (required if no mimeType) uri is not decoded(e.g. |
| 643 | // whitespace may be represented as %20) |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 644 | Value extras; |
Syoyo Fujita | 3e53feb | 2018-09-02 15:36:17 +0900 | [diff] [blame] | 645 | ExtensionMap extensions; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 646 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 647 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 648 | std::string extras_json_string; |
| 649 | std::string extensions_json_string; |
| 650 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 651 | // When this flag is true, data is stored to `image` in as-is format(e.g. jpeg |
| 652 | // compressed for "image/jpeg" mime) This feature is good if you use custom |
| 653 | // image loader function. (e.g. delayed decoding of images for faster glTF |
| 654 | // parsing) Default parser for Image does not provide as-is loading feature at |
| 655 | // the moment. (You can manipulate this by providing your own LoadImageData |
| 656 | // function) |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 657 | bool as_is{false}; |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 658 | |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 659 | Image() { |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 660 | bufferView = -1; |
| 661 | width = -1; |
| 662 | height = -1; |
| 663 | component = -1; |
daemyung jang | 1739d02 | 2020-07-03 13:27:39 +0900 | [diff] [blame] | 664 | bits = -1; |
| 665 | pixel_type = -1; |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 666 | } |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 667 | DEFAULT_METHODS(Image) |
jrkoonce | d1e1472 | 2019-08-27 11:51:02 -0500 | [diff] [blame] | 668 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 669 | bool operator==(const Image &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 670 | }; |
| 671 | |
| 672 | struct Texture { |
Vladimír Vondruš | 239be2c | 2018-07-24 23:23:56 +0200 | [diff] [blame] | 673 | std::string name; |
| 674 | |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 675 | int sampler{-1}; |
| 676 | int source{-1}; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 677 | Value extras; |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 678 | ExtensionMap extensions; |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 679 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 680 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 681 | std::string extras_json_string; |
| 682 | std::string extensions_json_string; |
| 683 | |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 684 | Texture() {} |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 685 | DEFAULT_METHODS(Texture) |
jrkoonce | d1e1472 | 2019-08-27 11:51:02 -0500 | [diff] [blame] | 686 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 687 | bool operator==(const Texture &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 688 | }; |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 689 | |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 690 | struct TextureInfo { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 691 | int index = -1; // required. |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 692 | int texCoord{0}; // The set index of texture's TEXCOORD attribute used for |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 693 | // texture coordinate mapping. |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 694 | |
| 695 | Value extras; |
| 696 | ExtensionMap extensions; |
| 697 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 698 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 699 | std::string extras_json_string; |
| 700 | std::string extensions_json_string; |
| 701 | |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 702 | TextureInfo() {} |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 703 | DEFAULT_METHODS(TextureInfo) |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 704 | bool operator==(const TextureInfo &) const; |
| 705 | }; |
| 706 | |
| 707 | struct NormalTextureInfo { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 708 | int index = -1; // required |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 709 | int texCoord{0}; // The set index of texture's TEXCOORD attribute used for |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 710 | // texture coordinate mapping. |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 711 | double scale{1.0}; // scaledNormal = normalize((<sampled normal texture value> |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 712 | // * 2.0 - 1.0) * vec3(<normal scale>, <normal scale>, 1.0)) |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 713 | |
| 714 | Value extras; |
| 715 | ExtensionMap extensions; |
| 716 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 717 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 718 | std::string extras_json_string; |
| 719 | std::string extensions_json_string; |
| 720 | |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 721 | NormalTextureInfo() {} |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 722 | DEFAULT_METHODS(NormalTextureInfo) |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 723 | bool operator==(const NormalTextureInfo &) const; |
| 724 | }; |
| 725 | |
| 726 | struct OcclusionTextureInfo { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 727 | int index = -1; // required |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 728 | 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] | 729 | // texture coordinate mapping. |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 730 | double strength{1.0}; // occludedColor = lerp(color, color * <sampled occlusion |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 731 | // texture value>, <occlusion strength>) |
| 732 | |
| 733 | Value extras; |
| 734 | ExtensionMap extensions; |
| 735 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 736 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 737 | std::string extras_json_string; |
| 738 | std::string extensions_json_string; |
| 739 | |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 740 | OcclusionTextureInfo() {} |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 741 | DEFAULT_METHODS(OcclusionTextureInfo) |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 742 | bool operator==(const OcclusionTextureInfo &) const; |
| 743 | }; |
| 744 | |
| 745 | // pbrMetallicRoughness class defined in glTF 2.0 spec. |
| 746 | struct PbrMetallicRoughness { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 747 | std::vector<double> baseColorFactor; // len = 4. default [1,1,1,1] |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 748 | TextureInfo baseColorTexture; |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 749 | double metallicFactor{1.0}; // default 1 |
| 750 | double roughnessFactor{1.0}; // default 1 |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 751 | TextureInfo metallicRoughnessTexture; |
| 752 | |
| 753 | Value extras; |
| 754 | ExtensionMap extensions; |
| 755 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 756 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 757 | std::string extras_json_string; |
| 758 | std::string extensions_json_string; |
| 759 | |
| 760 | PbrMetallicRoughness() |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 761 | : baseColorFactor(std::vector<double>{1.0, 1.0, 1.0, 1.0}) |
| 762 | {} |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 763 | DEFAULT_METHODS(PbrMetallicRoughness) |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 764 | bool operator==(const PbrMetallicRoughness &) const; |
| 765 | }; |
| 766 | |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 767 | // Each extension should be stored in a ParameterMap. |
| 768 | // members not in the values could be included in the ParameterMap |
| 769 | // to keep a single material model |
| 770 | struct Material { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 771 | std::string name; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 772 | |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 773 | std::vector<double> emissiveFactor; // length 3. default [0, 0, 0] |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 774 | std::string alphaMode; // default "OPAQUE" |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 775 | double alphaCutoff{0.5}; // default 0.5 |
| 776 | bool doubleSided{false}; // default false; |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 777 | |
| 778 | PbrMetallicRoughness pbrMetallicRoughness; |
| 779 | |
| 780 | NormalTextureInfo normalTexture; |
| 781 | OcclusionTextureInfo occlusionTexture; |
| 782 | TextureInfo emissiveTexture; |
| 783 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 784 | // For backward compatibility |
| 785 | // TODO(syoyo): Remove `values` and `additionalValues` in the next release. |
| 786 | ParameterMap values; |
| 787 | ParameterMap additionalValues; |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 788 | |
| 789 | ExtensionMap extensions; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 790 | Value extras; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 791 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 792 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 793 | std::string extras_json_string; |
| 794 | std::string extensions_json_string; |
| 795 | |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 796 | Material() : alphaMode("OPAQUE") {} |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 797 | DEFAULT_METHODS(Material) |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 798 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 799 | bool operator==(const Material &) const; |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 800 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 801 | |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 802 | struct BufferView { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 803 | std::string name; |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 804 | int buffer{-1}; // Required |
Syoyo Fujita | 4e47bc7 | 2020-01-02 22:07:25 +0900 | [diff] [blame] | 805 | size_t byteOffset{0}; // minimum 0, default 0 |
| 806 | size_t byteLength{0}; // required, minimum 1. 0 = invalid |
| 807 | 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] | 808 | // understood to be tightly packed |
| 809 | int target{0}; // ["ARRAY_BUFFER", "ELEMENT_ARRAY_BUFFER"] for vertex indices |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 810 | // or attribs. Could be 0 for other data |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 811 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 812 | ExtensionMap extensions; |
| 813 | |
| 814 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 815 | std::string extras_json_string; |
| 816 | std::string extensions_json_string; |
| 817 | |
Syoyo Fujita | 4e47bc7 | 2020-01-02 22:07:25 +0900 | [diff] [blame] | 818 | bool dracoDecoded{false}; // Flag indicating this has been draco decoded |
Syoyo Fujita | d17ff66 | 2017-05-29 02:53:12 +0900 | [diff] [blame] | 819 | |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 820 | BufferView() |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 821 | |
| 822 | {} |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 823 | DEFAULT_METHODS(BufferView) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 824 | bool operator==(const BufferView &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 825 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 826 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 827 | struct Accessor { |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 828 | int bufferView{-1}; // optional in spec but required here since sparse accessor |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 829 | // are not supported |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 830 | std::string name; |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 831 | size_t byteOffset{0}; |
| 832 | bool normalized{false}; // optional. |
| 833 | int componentType{-1}; // (required) One of TINYGLTF_COMPONENT_TYPE_*** |
| 834 | size_t count{0}; // required |
| 835 | int type{-1}; // (required) One of TINYGLTF_TYPE_*** .. |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 836 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 837 | ExtensionMap extensions; |
| 838 | |
| 839 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 840 | std::string extras_json_string; |
| 841 | std::string extensions_json_string; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 842 | |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 843 | std::vector<double> |
| 844 | minValues; // optional. integer value is promoted to double |
| 845 | std::vector<double> |
| 846 | maxValues; // optional. integer value is promoted to double |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 847 | |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 848 | struct Sparse { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 849 | int count; |
| 850 | bool isSparse; |
| 851 | struct { |
| 852 | int byteOffset; |
| 853 | int bufferView; |
| 854 | int componentType; // a TINYGLTF_COMPONENT_TYPE_ value |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 855 | Value extras; |
| 856 | ExtensionMap extensions; |
| 857 | std::string extras_json_string; |
| 858 | std::string extensions_json_string; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 859 | } indices; |
| 860 | struct { |
| 861 | int bufferView; |
| 862 | int byteOffset; |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 863 | Value extras; |
| 864 | ExtensionMap extensions; |
| 865 | std::string extras_json_string; |
| 866 | std::string extensions_json_string; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 867 | } values; |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 868 | Value extras; |
| 869 | ExtensionMap extensions; |
| 870 | std::string extras_json_string; |
| 871 | std::string extensions_json_string; |
| 872 | }; |
| 873 | |
| 874 | Sparse sparse; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 875 | |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 876 | /// |
| 877 | /// Utility function to compute byteStride for a given bufferView object. |
| 878 | /// Returns -1 upon invalid glTF value or parameter configuration. |
| 879 | /// |
| 880 | int ByteStride(const BufferView &bufferViewObject) const { |
| 881 | if (bufferViewObject.byteStride == 0) { |
| 882 | // Assume data is tightly packed. |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 883 | int componentSizeInBytes = |
| 884 | GetComponentSizeInBytes(static_cast<uint32_t>(componentType)); |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 885 | if (componentSizeInBytes <= 0) { |
| 886 | return -1; |
| 887 | } |
| 888 | |
Syoyo Fujita | 81d75df | 2019-08-30 19:19:52 +0900 | [diff] [blame] | 889 | int numComponents = GetNumComponentsInType(static_cast<uint32_t>(type)); |
| 890 | if (numComponents <= 0) { |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 891 | return -1; |
| 892 | } |
| 893 | |
Syoyo Fujita | 81d75df | 2019-08-30 19:19:52 +0900 | [diff] [blame] | 894 | return componentSizeInBytes * numComponents; |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 895 | } else { |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 896 | // Check if byteStride is a multiple of the size of the accessor's component |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 897 | // type. |
| 898 | int componentSizeInBytes = |
| 899 | GetComponentSizeInBytes(static_cast<uint32_t>(componentType)); |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 900 | if (componentSizeInBytes <= 0) { |
| 901 | return -1; |
| 902 | } |
| 903 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 904 | if ((bufferViewObject.byteStride % uint32_t(componentSizeInBytes)) != 0) { |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 905 | return -1; |
| 906 | } |
Arthur Brainville | 8ce4e54 | 2018-01-10 01:27:12 +0100 | [diff] [blame] | 907 | return static_cast<int>(bufferViewObject.byteStride); |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 908 | } |
| 909 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 910 | // unreachable return 0; |
Syoyo Fujita | 584f8c0 | 2018-01-03 17:47:08 +0900 | [diff] [blame] | 911 | } |
| 912 | |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 913 | Accessor() |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 914 | |
| 915 | { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 916 | sparse.isSparse = false; |
| 917 | } |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 918 | DEFAULT_METHODS(Accessor) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 919 | bool operator==(const tinygltf::Accessor &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 920 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 921 | |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 922 | struct PerspectiveCamera { |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 923 | double aspectRatio{0.0}; // min > 0 |
| 924 | double yfov{0.0}; // required. min > 0 |
| 925 | double zfar{0.0}; // min > 0 |
| 926 | double znear{0.0}; // required. min > 0 |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 927 | |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 928 | PerspectiveCamera() |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 929 | |
| 930 | {} |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 931 | DEFAULT_METHODS(PerspectiveCamera) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 932 | bool operator==(const PerspectiveCamera &) const; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 933 | |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 934 | ExtensionMap extensions; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 935 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 936 | |
| 937 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 938 | std::string extras_json_string; |
| 939 | std::string extensions_json_string; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 940 | }; |
Aurélien Chatelain | a25e495 | 2017-05-24 09:52:28 +0000 | [diff] [blame] | 941 | |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 942 | struct OrthographicCamera { |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 943 | double xmag{0.0}; // required. must not be zero. |
| 944 | double ymag{0.0}; // required. must not be zero. |
| 945 | double zfar{0.0}; // required. `zfar` must be greater than `znear`. |
| 946 | double znear{0.0}; // required |
Aurélien Chatelain | a25e495 | 2017-05-24 09:52:28 +0000 | [diff] [blame] | 947 | |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 948 | OrthographicCamera() {} |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 949 | DEFAULT_METHODS(OrthographicCamera) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 950 | bool operator==(const OrthographicCamera &) const; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 951 | |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 952 | ExtensionMap extensions; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 953 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 954 | |
| 955 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 956 | std::string extras_json_string; |
| 957 | std::string extensions_json_string; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 958 | }; |
| 959 | |
| 960 | struct Camera { |
| 961 | std::string type; // required. "perspective" or "orthographic" |
| 962 | std::string name; |
| 963 | |
| 964 | PerspectiveCamera perspective; |
| 965 | OrthographicCamera orthographic; |
| 966 | |
| 967 | Camera() {} |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 968 | DEFAULT_METHODS(Camera) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 969 | bool operator==(const Camera &) const; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 970 | |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 971 | ExtensionMap extensions; |
Aurélien Chatelain | a25e495 | 2017-05-24 09:52:28 +0000 | [diff] [blame] | 972 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 973 | |
| 974 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 975 | std::string extras_json_string; |
| 976 | std::string extensions_json_string; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 977 | }; |
| 978 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 979 | struct Primitive { |
| 980 | std::map<std::string, int> attributes; // (required) A dictionary object of |
| 981 | // integer, where each integer |
| 982 | // is the index of the accessor |
| 983 | // containing an attribute. |
| 984 | int material; // The index of the material to apply to this primitive |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 985 | // when rendering. |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 986 | int indices; // The index of the accessor that contains the indices. |
| 987 | int mode; // one of TINYGLTF_MODE_*** |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 988 | std::vector<std::map<std::string, int> > targets; // array of morph targets, |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 989 | // where each target is a dict with attributes in ["POSITION, "NORMAL", |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 990 | // "TANGENT"] pointing |
| 991 | // to their corresponding accessors |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 992 | ExtensionMap extensions; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 993 | Value extras; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 994 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 995 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 996 | std::string extras_json_string; |
| 997 | std::string extensions_json_string; |
| 998 | |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 999 | Primitive() { |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1000 | material = -1; |
| 1001 | indices = -1; |
daemyung jang | 1739d02 | 2020-07-03 13:27:39 +0900 | [diff] [blame] | 1002 | mode = -1; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1003 | } |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1004 | DEFAULT_METHODS(Primitive) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1005 | bool operator==(const Primitive &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1006 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1007 | |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 1008 | struct Mesh { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1009 | std::string name; |
| 1010 | std::vector<Primitive> primitives; |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 1011 | std::vector<double> weights; // weights to be applied to the Morph Targets |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 1012 | ExtensionMap extensions; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 1013 | Value extras; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1014 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1015 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1016 | std::string extras_json_string; |
| 1017 | std::string extensions_json_string; |
| 1018 | |
jrkoonce | d1e1472 | 2019-08-27 11:51:02 -0500 | [diff] [blame] | 1019 | Mesh() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1020 | DEFAULT_METHODS(Mesh) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1021 | bool operator==(const Mesh &) const; |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 1022 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1023 | |
| 1024 | class Node { |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 1025 | public: |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 1026 | Node() {} |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1027 | |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1028 | DEFAULT_METHODS(Node) |
Syoyo Fujita | d6b0d0a | 2019-06-29 17:31:13 +0900 | [diff] [blame] | 1029 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1030 | bool operator==(const Node &) const; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1031 | |
Syoyo Fujita | 7a570c8 | 2023-06-19 21:52:13 +0900 | [diff] [blame] | 1032 | int camera{-1}; // the index of the camera referenced by this node |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1033 | |
| 1034 | std::string name; |
Syoyo Fujita | 7a570c8 | 2023-06-19 21:52:13 +0900 | [diff] [blame] | 1035 | int skin{-1}; |
| 1036 | int mesh{-1}; |
| 1037 | int light{-1}; // light source index (KHR_lights_punctual) |
| 1038 | int emitter{-1}; // audio emitter index (KHR_audio) |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1039 | std::vector<int> children; |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 1040 | std::vector<double> rotation; // length must be 0 or 4 |
| 1041 | std::vector<double> scale; // length must be 0 or 3 |
| 1042 | std::vector<double> translation; // length must be 0 or 3 |
| 1043 | std::vector<double> matrix; // length must be 0 or 16 |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 1044 | std::vector<double> weights; // The weights of the instantiated Morph Target |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 1045 | |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 1046 | ExtensionMap extensions; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 1047 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1048 | |
| 1049 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1050 | std::string extras_json_string; |
| 1051 | std::string extensions_json_string; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1052 | }; |
| 1053 | |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 1054 | struct Buffer { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1055 | std::string name; |
| 1056 | std::vector<unsigned char> data; |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 1057 | std::string |
| 1058 | 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] | 1059 | // uri is not decoded(e.g. whitespace may be represented as %20) |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 1060 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1061 | ExtensionMap extensions; |
| 1062 | |
| 1063 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1064 | std::string extras_json_string; |
| 1065 | std::string extensions_json_string; |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1066 | |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1067 | Buffer() = default; |
| 1068 | DEFAULT_METHODS(Buffer) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1069 | bool operator==(const Buffer &) const; |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 1070 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1071 | |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 1072 | struct Asset { |
Syoyo Fujita | b702de7 | 2021-03-02 19:08:29 +0900 | [diff] [blame] | 1073 | std::string version = "2.0"; // required |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1074 | std::string generator; |
Aurélien Chatelain | d48bbdc | 2017-05-24 09:54:18 +0000 | [diff] [blame] | 1075 | std::string minVersion; |
| 1076 | std::string copyright; |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 1077 | ExtensionMap extensions; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 1078 | Value extras; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1079 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1080 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1081 | std::string extras_json_string; |
| 1082 | std::string extensions_json_string; |
| 1083 | |
jrkoonce | d1e1472 | 2019-08-27 11:51:02 -0500 | [diff] [blame] | 1084 | Asset() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1085 | DEFAULT_METHODS(Asset) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1086 | bool operator==(const Asset &) const; |
Syoyo Fujita | 2d17a31 | 2018-04-17 15:45:42 +0900 | [diff] [blame] | 1087 | }; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1088 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1089 | struct Scene { |
| 1090 | std::string name; |
| 1091 | std::vector<int> nodes; |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 1092 | std::vector<int> audioEmitters; // KHR_audio global emitters |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1093 | |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 1094 | ExtensionMap extensions; |
| 1095 | Value extras; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1096 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1097 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1098 | std::string extras_json_string; |
| 1099 | std::string extensions_json_string; |
| 1100 | |
jrkoonce | d1e1472 | 2019-08-27 11:51:02 -0500 | [diff] [blame] | 1101 | Scene() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1102 | DEFAULT_METHODS(Scene) |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1103 | bool operator==(const Scene &) const; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1104 | }; |
| 1105 | |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 1106 | struct SpotLight { |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 1107 | double innerConeAngle{0.0}; |
| 1108 | double outerConeAngle{0.7853981634}; |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 1109 | |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 1110 | SpotLight() {} |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1111 | DEFAULT_METHODS(SpotLight) |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 1112 | bool operator==(const SpotLight &) const; |
| 1113 | |
| 1114 | ExtensionMap extensions; |
| 1115 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1116 | |
| 1117 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1118 | std::string extras_json_string; |
| 1119 | std::string extensions_json_string; |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 1120 | }; |
| 1121 | |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 1122 | struct Light { |
| 1123 | std::string name; |
| 1124 | std::vector<double> color; |
Syoyo Fujita | 3dad303 | 2020-05-13 21:22:23 +0900 | [diff] [blame] | 1125 | double intensity{1.0}; |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 1126 | std::string type; |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 1127 | double range{0.0}; // 0.0 = infinite |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 1128 | SpotLight spot; |
| 1129 | |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 1130 | Light() {} |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1131 | DEFAULT_METHODS(Light) |
Arthur Brainville (Ybalrid) | 9eeaf20 | 2019-09-05 13:02:05 +0200 | [diff] [blame] | 1132 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1133 | bool operator==(const Light &) const; |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 1134 | |
| 1135 | ExtensionMap extensions; |
| 1136 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1137 | |
| 1138 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1139 | std::string extras_json_string; |
| 1140 | std::string extensions_json_string; |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 1141 | }; |
| 1142 | |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1143 | struct PositionalEmitter { |
| 1144 | double coneInnerAngle{6.283185307179586}; |
| 1145 | double coneOuterAngle{6.283185307179586}; |
| 1146 | double coneOuterGain{0.0}; |
| 1147 | double maxDistance{100.0}; |
| 1148 | double refDistance{1.0}; |
| 1149 | double rolloffFactor{1.0}; |
| 1150 | |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 1151 | PositionalEmitter() {} |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1152 | DEFAULT_METHODS(PositionalEmitter) |
| 1153 | bool operator==(const PositionalEmitter &) const; |
| 1154 | |
| 1155 | ExtensionMap extensions; |
| 1156 | Value extras; |
| 1157 | |
| 1158 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1159 | std::string extras_json_string; |
| 1160 | std::string extensions_json_string; |
| 1161 | }; |
| 1162 | |
| 1163 | struct AudioEmitter { |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 1164 | std::string name; |
| 1165 | double gain{1.0}; |
| 1166 | bool loop{false}; |
| 1167 | bool playing{false}; |
| 1168 | std::string |
| 1169 | type; // positional - Positional audio emitters. Using sound cones, the |
| 1170 | // orientation is +Z having the same front side for a glTF asset. |
| 1171 | // global - Global audio emitters are not affected by the position |
| 1172 | // of audio listeners. coneInnerAngle, coneOuterAngle, |
| 1173 | // coneOuterGain, distanceModel, maxDistance, refDistance, and |
| 1174 | // rolloffFactor should all be ignored when set. |
| 1175 | std::string |
| 1176 | distanceModel; // linear - A linear distance model calculating the |
| 1177 | // gain induced by the distance according to: 1.0 |
| 1178 | // - rolloffFactor * (distance - refDistance) / |
| 1179 | // (maxDistance - refDistance) |
| 1180 | // inverse - (default) An inverse distance model |
| 1181 | // calculating the gain induced by the distance according |
| 1182 | // to: refDistance / (refDistance + rolloffFactor * |
| 1183 | // (Math.max(distance, refDistance) - refDistance)) |
| 1184 | // exponential - An exponential distance model calculating |
| 1185 | // the gain induced by the distance according to: |
| 1186 | // pow((Math.max(distance, refDistance) / refDistance, |
| 1187 | // -rolloffFactor)) |
| 1188 | PositionalEmitter positional; |
| 1189 | int source{-1}; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1190 | |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 1191 | AudioEmitter() |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 1192 | : |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 1193 | type("global"), |
| 1194 | distanceModel("inverse") {} |
| 1195 | DEFAULT_METHODS(AudioEmitter) |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1196 | |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 1197 | bool operator==(const AudioEmitter &) const; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1198 | |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 1199 | ExtensionMap extensions; |
| 1200 | Value extras; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1201 | |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 1202 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1203 | std::string extras_json_string; |
| 1204 | std::string extensions_json_string; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1205 | }; |
| 1206 | |
| 1207 | struct AudioSource { |
| 1208 | std::string name; |
| 1209 | std::string uri; |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 1210 | int bufferView{-1}; // (required if no uri) |
| 1211 | std::string mimeType; // (required if no uri) The audio's MIME type. Required if |
| 1212 | // bufferView is defined. Unless specified by another |
| 1213 | // extension, the only supported mimeType is audio/mpeg. |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1214 | |
| 1215 | AudioSource() {} |
| 1216 | DEFAULT_METHODS(AudioSource) |
| 1217 | |
| 1218 | bool operator==(const AudioSource &) const; |
| 1219 | |
| 1220 | Value extras; |
| 1221 | ExtensionMap extensions; |
| 1222 | |
| 1223 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1224 | std::string extras_json_string; |
| 1225 | std::string extensions_json_string; |
| 1226 | }; |
| 1227 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1228 | class Model { |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 1229 | public: |
Selmar Kok | ff2b1f9 | 2019-10-21 17:58:09 +0200 | [diff] [blame] | 1230 | Model() = default; |
Selmar Kok | b74fade | 2019-10-29 16:09:32 +0100 | [diff] [blame] | 1231 | DEFAULT_METHODS(Model) |
Syoyo Fujita | d6b0d0a | 2019-06-29 17:31:13 +0900 | [diff] [blame] | 1232 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1233 | bool operator==(const Model &) const; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1234 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1235 | std::vector<Accessor> accessors; |
| 1236 | std::vector<Animation> animations; |
| 1237 | std::vector<Buffer> buffers; |
| 1238 | std::vector<BufferView> bufferViews; |
| 1239 | std::vector<Material> materials; |
| 1240 | std::vector<Mesh> meshes; |
| 1241 | std::vector<Node> nodes; |
| 1242 | std::vector<Texture> textures; |
| 1243 | std::vector<Image> images; |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 1244 | std::vector<Skin> skins; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1245 | std::vector<Sampler> samplers; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 1246 | std::vector<Camera> cameras; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1247 | std::vector<Scene> scenes; |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 1248 | std::vector<Light> lights; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 1249 | std::vector<AudioEmitter> audioEmitters; |
| 1250 | std::vector<AudioSource> audioSources; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1251 | |
sammyKhan | a0a62bd | 2020-01-17 13:41:16 +0100 | [diff] [blame] | 1252 | int defaultScene = -1; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 1253 | std::vector<std::string> extensionsUsed; |
Aurélien Chatelain | 756ee6b | 2017-06-19 13:52:49 +0000 | [diff] [blame] | 1254 | std::vector<std::string> extensionsRequired; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1255 | |
| 1256 | Asset asset; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 1257 | |
| 1258 | Value extras; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1259 | ExtensionMap extensions; |
| 1260 | |
| 1261 | // Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled. |
| 1262 | std::string extras_json_string; |
| 1263 | std::string extensions_json_string; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1264 | }; |
| 1265 | |
Syoyo Fujita | 0614eb8 | 2016-10-14 18:50:14 +0900 | [diff] [blame] | 1266 | enum SectionCheck { |
Luke San Antonio | 9e36b61 | 2016-06-23 14:10:51 -0400 | [diff] [blame] | 1267 | NO_REQUIRE = 0x00, |
Benjamin Schmithüsen | d02ad0d | 2019-05-02 14:44:20 +0200 | [diff] [blame] | 1268 | REQUIRE_VERSION = 0x01, |
| 1269 | REQUIRE_SCENE = 0x02, |
| 1270 | REQUIRE_SCENES = 0x04, |
| 1271 | REQUIRE_NODES = 0x08, |
| 1272 | REQUIRE_ACCESSORS = 0x10, |
| 1273 | REQUIRE_BUFFERS = 0x20, |
| 1274 | REQUIRE_BUFFER_VIEWS = 0x40, |
| 1275 | REQUIRE_ALL = 0x7f |
Luke San Antonio | 9e36b61 | 2016-06-23 14:10:51 -0400 | [diff] [blame] | 1276 | }; |
| 1277 | |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1278 | /// |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 1279 | /// URIEncodeFunction type. Signature for custom URI encoding of external |
| 1280 | /// resources such as .bin and image files. Used by tinygltf to re-encode the |
| 1281 | /// final location of saved files. object_type may be used to encode buffer and |
| 1282 | /// image URIs differently, for example. See |
| 1283 | /// https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#uris |
| 1284 | /// |
| 1285 | typedef bool (*URIEncodeFunction)(const std::string &in_uri, |
| 1286 | const std::string &object_type, |
| 1287 | std::string *out_uri, void *user_data); |
| 1288 | |
| 1289 | /// |
| 1290 | /// URIDecodeFunction type. Signature for custom URI decoding of external |
| 1291 | /// resources such as .bin and image files. Used by tinygltf when computing |
| 1292 | /// filenames to write resources. |
| 1293 | /// |
| 1294 | typedef bool (*URIDecodeFunction)(const std::string &in_uri, |
| 1295 | std::string *out_uri, void *user_data); |
| 1296 | |
| 1297 | // Declaration of default uri decode function |
| 1298 | bool URIDecode(const std::string &in_uri, std::string *out_uri, |
| 1299 | void *user_data); |
| 1300 | |
| 1301 | /// |
| 1302 | /// A structure containing URI callbacks and a pointer to their user data. |
| 1303 | /// |
| 1304 | struct URICallbacks { |
| 1305 | URIEncodeFunction encode; // Optional encode method |
| 1306 | URIDecodeFunction decode; // Required decode method |
| 1307 | |
| 1308 | void *user_data; // An argument that is passed to all uri callbacks |
| 1309 | }; |
| 1310 | |
| 1311 | /// |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1312 | /// LoadImageDataFunction type. Signature for custom image loading callbacks. |
| 1313 | /// |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 1314 | typedef bool (*LoadImageDataFunction)(Image *, const int, std::string *, |
| 1315 | std::string *, int, int, |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 1316 | const unsigned char *, int, |
| 1317 | void *user_pointer); |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1318 | |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1319 | /// |
| 1320 | /// WriteImageDataFunction type. Signature for custom image writing callbacks. |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 1321 | /// The out_uri parameter becomes the URI written to the gltf and may reference |
| 1322 | /// a file or contain a data URI. |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1323 | /// |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 1324 | typedef bool (*WriteImageDataFunction)(const std::string *basepath, |
| 1325 | const std::string *filename, |
| 1326 | const Image *image, bool embedImages, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 1327 | const URICallbacks *uri_cb, |
| 1328 | std::string *out_uri, |
| 1329 | void *user_pointer); |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1330 | |
Squareys | 2d3594d | 2018-03-13 22:40:53 +0100 | [diff] [blame] | 1331 | #ifndef TINYGLTF_NO_STB_IMAGE |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1332 | // Declaration of default image loader callback |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 1333 | bool LoadImageData(Image *image, const int image_idx, std::string *err, |
| 1334 | std::string *warn, int req_width, int req_height, |
| 1335 | const unsigned char *bytes, int size, void *); |
Squareys | 2d3594d | 2018-03-13 22:40:53 +0100 | [diff] [blame] | 1336 | #endif |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1337 | |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1338 | #ifndef TINYGLTF_NO_STB_IMAGE_WRITE |
| 1339 | // Declaration of default image writer callback |
| 1340 | bool WriteImageData(const std::string *basepath, const std::string *filename, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 1341 | const Image *image, bool embedImages, |
| 1342 | const URICallbacks *uri_cb, std::string *out_uri, void *); |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1343 | #endif |
| 1344 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1345 | /// |
| 1346 | /// FilExistsFunction type. Signature for custom filesystem callbacks. |
| 1347 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1348 | typedef bool (*FileExistsFunction)(const std::string &abs_filename, void *); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1349 | |
| 1350 | /// |
| 1351 | /// ExpandFilePathFunction type. Signature for custom filesystem callbacks. |
| 1352 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1353 | typedef std::string (*ExpandFilePathFunction)(const std::string &, void *); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1354 | |
| 1355 | /// |
| 1356 | /// ReadWholeFileFunction type. Signature for custom filesystem callbacks. |
| 1357 | /// |
| 1358 | typedef bool (*ReadWholeFileFunction)(std::vector<unsigned char> *, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1359 | std::string *, const std::string &, |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1360 | void *); |
| 1361 | |
| 1362 | /// |
| 1363 | /// WriteWholeFileFunction type. Signature for custom filesystem callbacks. |
| 1364 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1365 | typedef bool (*WriteWholeFileFunction)(std::string *, const std::string &, |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1366 | const std::vector<unsigned char> &, |
| 1367 | void *); |
| 1368 | |
| 1369 | /// |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 1370 | /// GetFileSizeFunction type. Signature for custom filesystem callbacks. |
| 1371 | /// |
| 1372 | typedef bool (*GetFileSizeFunction)(size_t *filesize_out, std::string *err, const std::string &abs_filename, |
| 1373 | void *userdata); |
| 1374 | |
| 1375 | /// |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1376 | /// A structure containing all required filesystem callbacks and a pointer to |
| 1377 | /// their user data. |
| 1378 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1379 | struct FsCallbacks { |
| 1380 | FileExistsFunction FileExists; |
| 1381 | ExpandFilePathFunction ExpandFilePath; |
| 1382 | ReadWholeFileFunction ReadWholeFile; |
| 1383 | WriteWholeFileFunction WriteWholeFile; |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 1384 | GetFileSizeFunction GetFileSizeInBytes; // To avoid GetFileSize Win32 API, add `InBytes` suffix. |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1385 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1386 | void *user_data; // An argument that is passed to all fs callbacks |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1387 | }; |
| 1388 | |
| 1389 | #ifndef TINYGLTF_NO_FS |
| 1390 | // Declaration of default filesystem callbacks |
| 1391 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1392 | bool FileExists(const std::string &abs_filename, void *); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1393 | |
Syoyo Fujita | 3d939fd | 2020-06-06 18:13:15 +0900 | [diff] [blame] | 1394 | /// |
Rahul Sheth | 96a8b2c | 2020-07-10 14:27:46 -0400 | [diff] [blame] | 1395 | /// Expand file path(e.g. `~` to home directory on posix, `%APPDATA%` to |
Syoyo Fujita | 2c521b3 | 2020-12-04 00:50:46 +0900 | [diff] [blame] | 1396 | /// `C:\\Users\\tinygltf\\AppData`) |
Syoyo Fujita | 3d939fd | 2020-06-06 18:13:15 +0900 | [diff] [blame] | 1397 | /// |
| 1398 | /// @param[in] filepath File path string. Assume UTF-8 |
| 1399 | /// @param[in] userdata User data. Set to `nullptr` if you don't need it. |
| 1400 | /// |
| 1401 | std::string ExpandFilePath(const std::string &filepath, void *userdata); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1402 | |
| 1403 | bool ReadWholeFile(std::vector<unsigned char> *out, std::string *err, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1404 | const std::string &filepath, void *); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1405 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1406 | bool WriteWholeFile(std::string *err, const std::string &filepath, |
| 1407 | const std::vector<unsigned char> &contents, void *); |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 1408 | |
| 1409 | bool GetFileSizeInBytes(size_t *filesize_out, std::string *err, const std::string &filepath, |
| 1410 | void *); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1411 | #endif |
| 1412 | |
Syoyo Fujita | a472a3f | 2019-08-21 14:23:00 +0900 | [diff] [blame] | 1413 | /// |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 1414 | /// glTF Parser/Serializer context. |
Syoyo Fujita | a472a3f | 2019-08-21 14:23:00 +0900 | [diff] [blame] | 1415 | /// |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1416 | class TinyGLTF { |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 1417 | public: |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1418 | #ifdef __clang__ |
| 1419 | #pragma clang diagnostic push |
| 1420 | #pragma clang diagnostic ignored "-Wc++98-compat" |
| 1421 | #endif |
| 1422 | |
Bryn Lloyd | a64f4b4 | 2023-06-21 18:40:18 +0200 | [diff] [blame^] | 1423 | TinyGLTF() {} |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1424 | |
| 1425 | #ifdef __clang__ |
| 1426 | #pragma clang diagnostic pop |
| 1427 | #endif |
| 1428 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1429 | ~TinyGLTF() {} |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1430 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1431 | /// |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1432 | /// Loads glTF ASCII asset from a file. |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1433 | /// Set warning message to `warn` for example it fails to load asserts. |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 1434 | /// 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] | 1435 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1436 | bool LoadASCIIFromFile(Model *model, std::string *err, std::string *warn, |
Luke San Antonio | 9e36b61 | 2016-06-23 14:10:51 -0400 | [diff] [blame] | 1437 | const std::string &filename, |
Benjamin Schmithüsen | d02ad0d | 2019-05-02 14:44:20 +0200 | [diff] [blame] | 1438 | unsigned int check_sections = REQUIRE_VERSION); |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 1439 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1440 | /// |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1441 | /// Loads glTF ASCII asset from string(memory). |
| 1442 | /// `length` = strlen(str); |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 1443 | /// `base_dir` is a search path of glTF asset(e.g. images). Path Must be an |
| 1444 | /// expanded path (e.g. no tilde(`~`), no environment variables). Set warning |
| 1445 | /// message to `warn` for example it fails to load asserts. Returns false and |
| 1446 | /// set error string to `err` if there's an error. |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1447 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1448 | bool LoadASCIIFromString(Model *model, std::string *err, std::string *warn, |
| 1449 | const char *str, const unsigned int length, |
Luke San Antonio | 9e36b61 | 2016-06-23 14:10:51 -0400 | [diff] [blame] | 1450 | const std::string &base_dir, |
Benjamin Schmithüsen | d02ad0d | 2019-05-02 14:44:20 +0200 | [diff] [blame] | 1451 | unsigned int check_sections = REQUIRE_VERSION); |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1452 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1453 | /// |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1454 | /// Loads glTF binary asset from a file. |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1455 | /// Set warning message to `warn` for example it fails to load asserts. |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1456 | /// 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] | 1457 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1458 | bool LoadBinaryFromFile(Model *model, std::string *err, std::string *warn, |
Luke San Antonio | 9e36b61 | 2016-06-23 14:10:51 -0400 | [diff] [blame] | 1459 | const std::string &filename, |
Benjamin Schmithüsen | d02ad0d | 2019-05-02 14:44:20 +0200 | [diff] [blame] | 1460 | unsigned int check_sections = REQUIRE_VERSION); |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1461 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1462 | /// |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1463 | /// Loads glTF binary asset from memory. |
| 1464 | /// `length` = strlen(str); |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 1465 | /// `base_dir` is a search path of glTF asset(e.g. images). Path Must be an |
| 1466 | /// expanded path (e.g. no tilde(`~`), no environment variables). |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1467 | /// Set warning message to `warn` for example it fails to load asserts. |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1468 | /// 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] | 1469 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1470 | bool LoadBinaryFromMemory(Model *model, std::string *err, std::string *warn, |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 1471 | const unsigned char *bytes, |
| 1472 | const unsigned int length, |
Luke San Antonio | 9e36b61 | 2016-06-23 14:10:51 -0400 | [diff] [blame] | 1473 | const std::string &base_dir = "", |
Benjamin Schmithüsen | d02ad0d | 2019-05-02 14:44:20 +0200 | [diff] [blame] | 1474 | unsigned int check_sections = REQUIRE_VERSION); |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1475 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1476 | /// |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 1477 | /// Write glTF to stream, buffers and images will be embedded |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 1478 | /// |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 1479 | bool WriteGltfSceneToStream(const Model *model, std::ostream &stream, |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 1480 | bool prettyPrint, bool writeBinary); |
| 1481 | |
| 1482 | /// |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1483 | /// Write glTF to file. |
| 1484 | /// |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 1485 | bool WriteGltfSceneToFile(const Model *model, const std::string &filename, |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 1486 | bool embedImages, bool embedBuffers, |
| 1487 | bool prettyPrint, bool writeBinary); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 1488 | |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1489 | /// |
| 1490 | /// Set callback to use for loading image data |
| 1491 | /// |
| 1492 | void SetImageLoader(LoadImageDataFunction LoadImageData, void *user_data); |
| 1493 | |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1494 | /// |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1495 | /// Unset(remove) callback of loading image data |
| 1496 | /// |
| 1497 | void RemoveImageLoader(); |
| 1498 | |
| 1499 | /// |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1500 | /// Set callback to use for writing image data |
| 1501 | /// |
| 1502 | void SetImageWriter(WriteImageDataFunction WriteImageData, void *user_data); |
| 1503 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1504 | /// |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 1505 | /// Set callbacks to use for URI encoding and decoding and their user data |
| 1506 | /// |
| 1507 | void SetURICallbacks(URICallbacks callbacks); |
| 1508 | |
| 1509 | /// |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1510 | /// Set callbacks to use for filesystem (fs) access and their user data |
| 1511 | /// |
| 1512 | void SetFsCallbacks(FsCallbacks callbacks); |
| 1513 | |
Syoyo Fujita | a472a3f | 2019-08-21 14:23:00 +0900 | [diff] [blame] | 1514 | /// |
| 1515 | /// Set serializing default values(default = false). |
| 1516 | /// When true, default values are force serialized to .glTF. |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 1517 | /// 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] | 1518 | /// data. |
Syoyo Fujita | a472a3f | 2019-08-21 14:23:00 +0900 | [diff] [blame] | 1519 | /// |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 1520 | /// TODO(LTE): Supply parsing option as function arguments to |
| 1521 | /// `LoadASCIIFromFile()` and others, not by a class method |
Syoyo Fujita | a472a3f | 2019-08-21 14:23:00 +0900 | [diff] [blame] | 1522 | /// |
| 1523 | void SetSerializeDefaultValues(const bool enabled) { |
| 1524 | serialize_default_values_ = enabled; |
| 1525 | } |
| 1526 | |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 1527 | bool GetSerializeDefaultValues() const { return serialize_default_values_; } |
Syoyo Fujita | a472a3f | 2019-08-21 14:23:00 +0900 | [diff] [blame] | 1528 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1529 | /// |
| 1530 | /// Store original JSON string for `extras` and `extensions`. |
| 1531 | /// This feature will be useful when the user want to reconstruct custom data |
| 1532 | /// structure from JSON string. |
| 1533 | /// |
| 1534 | void SetStoreOriginalJSONForExtrasAndExtensions(const bool enabled) { |
| 1535 | store_original_json_for_extras_and_extensions_ = enabled; |
| 1536 | } |
| 1537 | |
| 1538 | bool GetStoreOriginalJSONForExtrasAndExtensions() const { |
| 1539 | return store_original_json_for_extras_and_extensions_; |
| 1540 | } |
| 1541 | |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1542 | /// |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 1543 | /// Specify whether preserve image channels when loading images or not. |
| 1544 | /// (Not effective when the user supplies their own LoadImageData callbacks) |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1545 | /// |
| 1546 | void SetPreserveImageChannels(bool onoff) { |
| 1547 | preserve_image_channels_ = onoff; |
| 1548 | } |
| 1549 | |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 1550 | /// |
| 1551 | /// Set maximum allowed external file size in bytes. |
| 1552 | /// Default: 2GB |
| 1553 | /// Only effective for built-in ReadWholeFileFunction FS function. |
| 1554 | /// |
| 1555 | void SetMaxExternalFileSize(size_t max_bytes) { |
| 1556 | max_external_file_size_ = max_bytes; |
| 1557 | } |
| 1558 | |
| 1559 | size_t GetMaxExternalFileSize() const { |
| 1560 | return max_external_file_size_; |
| 1561 | } |
| 1562 | |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 1563 | bool GetPreserveImageChannels() const { return preserve_image_channels_; } |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1564 | |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 1565 | private: |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1566 | /// |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 1567 | /// Loads glTF asset from string(memory). |
| 1568 | /// `length` = strlen(str); |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1569 | /// Set warning message to `warn` for example it fails to load asserts |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 1570 | /// 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] | 1571 | /// |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1572 | bool LoadFromString(Model *model, std::string *err, std::string *warn, |
| 1573 | const char *str, const unsigned int length, |
| 1574 | const std::string &base_dir, unsigned int check_sections); |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1575 | |
Syoyo Fujita | a472a3f | 2019-08-21 14:23:00 +0900 | [diff] [blame] | 1576 | const unsigned char *bin_data_ = nullptr; |
| 1577 | size_t bin_size_ = 0; |
| 1578 | bool is_binary_ = false; |
| 1579 | |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 1580 | bool serialize_default_values_ = false; ///< Serialize default values? |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1581 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1582 | bool store_original_json_for_extras_and_extensions_ = false; |
| 1583 | |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 1584 | bool preserve_image_channels_ = false; /// Default false(expand channels to |
| 1585 | /// RGBA) for backward compatibility. |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1586 | |
Syoyo Fujita | 1a5046e | 2023-04-23 23:08:41 +0900 | [diff] [blame] | 1587 | size_t max_external_file_size_{size_t((std::numeric_limits<int32_t>::max)())}; // Default 2GB |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 1588 | |
Syoyo Fujita | 9117abb | 2022-08-16 20:10:26 +0900 | [diff] [blame] | 1589 | // Warning & error messages |
Syoyo Fujita | 81f7dbe | 2022-08-02 01:05:34 +0900 | [diff] [blame] | 1590 | std::string warn_; |
| 1591 | std::string err_; |
| 1592 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1593 | FsCallbacks fs = { |
| 1594 | #ifndef TINYGLTF_NO_FS |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1595 | &tinygltf::FileExists, &tinygltf::ExpandFilePath, |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 1596 | &tinygltf::ReadWholeFile, &tinygltf::WriteWholeFile, &tinygltf::GetFileSizeInBytes, |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1597 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1598 | nullptr // Fs callback user data |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1599 | #else |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 1600 | nullptr, nullptr, nullptr, nullptr, nullptr, |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1601 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1602 | nullptr // Fs callback user data |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1603 | #endif |
| 1604 | }; |
| 1605 | |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 1606 | URICallbacks uri_cb = { |
| 1607 | // Use paths as-is by default. This will use JSON string escaping. |
| 1608 | nullptr, |
| 1609 | // Decode all URIs before using them as paths as the application may have |
| 1610 | // percent encoded them. |
| 1611 | &tinygltf::URIDecode, |
| 1612 | // URI callback user data |
| 1613 | nullptr}; |
| 1614 | |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1615 | LoadImageDataFunction LoadImageData = |
| 1616 | #ifndef TINYGLTF_NO_STB_IMAGE |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1617 | &tinygltf::LoadImageData; |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1618 | #else |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1619 | nullptr; |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 1620 | #endif |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1621 | void *load_image_user_data_{nullptr}; |
| 1622 | bool user_image_loader_{false}; |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1623 | |
| 1624 | WriteImageDataFunction WriteImageData = |
| 1625 | #ifndef TINYGLTF_NO_STB_IMAGE_WRITE |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1626 | &tinygltf::WriteImageData; |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1627 | #else |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 1628 | nullptr; |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1629 | #endif |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1630 | void *write_image_user_data_{nullptr}; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1631 | }; |
| 1632 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1633 | #ifdef __clang__ |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 1634 | #pragma clang diagnostic pop // -Wpadded |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1635 | #endif |
| 1636 | |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 1637 | } // namespace tinygltf |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1638 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 1639 | #endif // TINY_GLTF_H_ |
Syoyo Fujita | c8d3c76 | 2017-05-25 01:42:38 +0900 | [diff] [blame] | 1640 | |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1641 | #if defined(TINYGLTF_IMPLEMENTATION) || defined(__INTELLISENSE__) |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 1642 | #include <algorithm> |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 1643 | //#include <cassert> |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1644 | #ifndef TINYGLTF_NO_FS |
Syoyo Fujita | 125d8e5 | 2019-11-09 20:52:56 +0900 | [diff] [blame] | 1645 | #include <cstdio> |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 1646 | #include <fstream> |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 1647 | #include <sys/stat.h> // for is_directory check |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 1648 | #endif |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 1649 | #include <sstream> |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 1650 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 1651 | #ifdef __clang__ |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 1652 | // Disable some warnings for external files. |
| 1653 | #pragma clang diagnostic push |
| 1654 | #pragma clang diagnostic ignored "-Wfloat-equal" |
| 1655 | #pragma clang diagnostic ignored "-Wexit-time-destructors" |
| 1656 | #pragma clang diagnostic ignored "-Wconversion" |
| 1657 | #pragma clang diagnostic ignored "-Wold-style-cast" |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 1658 | #pragma clang diagnostic ignored "-Wglobal-constructors" |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1659 | #if __has_warning("-Wreserved-id-macro") |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 1660 | #pragma clang diagnostic ignored "-Wreserved-id-macro" |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1661 | #endif |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 1662 | #pragma clang diagnostic ignored "-Wdisabled-macro-expansion" |
| 1663 | #pragma clang diagnostic ignored "-Wpadded" |
Syoyo Fujita | 2e21be7 | 2017-11-05 17:13:01 +0900 | [diff] [blame] | 1664 | #pragma clang diagnostic ignored "-Wc++98-compat" |
Syoyo Fujita | dc4bb86 | 2018-04-02 02:04:24 +0900 | [diff] [blame] | 1665 | #pragma clang diagnostic ignored "-Wc++98-compat-pedantic" |
Syoyo Fujita | 2e21be7 | 2017-11-05 17:13:01 +0900 | [diff] [blame] | 1666 | #pragma clang diagnostic ignored "-Wdocumentation-unknown-command" |
| 1667 | #pragma clang diagnostic ignored "-Wswitch-enum" |
| 1668 | #pragma clang diagnostic ignored "-Wimplicit-fallthrough" |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1669 | #pragma clang diagnostic ignored "-Wweak-vtables" |
| 1670 | #pragma clang diagnostic ignored "-Wcovered-switch-default" |
Syoyo Fujita | a8f0b1c | 2018-08-28 21:33:40 +0900 | [diff] [blame] | 1671 | #if __has_warning("-Wdouble-promotion") |
| 1672 | #pragma clang diagnostic ignored "-Wdouble-promotion" |
| 1673 | #endif |
Syoyo Fujita | 2e21be7 | 2017-11-05 17:13:01 +0900 | [diff] [blame] | 1674 | #if __has_warning("-Wcomma") |
Syoyo Fujita | c8d3c76 | 2017-05-25 01:42:38 +0900 | [diff] [blame] | 1675 | #pragma clang diagnostic ignored "-Wcomma" |
| 1676 | #endif |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1677 | #if __has_warning("-Wzero-as-null-pointer-constant") |
| 1678 | #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" |
| 1679 | #endif |
| 1680 | #if __has_warning("-Wcast-qual") |
| 1681 | #pragma clang diagnostic ignored "-Wcast-qual" |
| 1682 | #endif |
Syoyo Fujita | dc4bb86 | 2018-04-02 02:04:24 +0900 | [diff] [blame] | 1683 | #if __has_warning("-Wmissing-variable-declarations") |
| 1684 | #pragma clang diagnostic ignored "-Wmissing-variable-declarations" |
| 1685 | #endif |
| 1686 | #if __has_warning("-Wmissing-prototypes") |
| 1687 | #pragma clang diagnostic ignored "-Wmissing-prototypes" |
| 1688 | #endif |
| 1689 | #if __has_warning("-Wcast-align") |
| 1690 | #pragma clang diagnostic ignored "-Wcast-align" |
| 1691 | #endif |
| 1692 | #if __has_warning("-Wnewline-eof") |
| 1693 | #pragma clang diagnostic ignored "-Wnewline-eof" |
| 1694 | #endif |
Syoyo Fujita | 7bdfed3 | 2019-03-03 17:04:49 +0900 | [diff] [blame] | 1695 | #if __has_warning("-Wunused-parameter") |
| 1696 | #pragma clang diagnostic ignored "-Wunused-parameter" |
Syoyo Fujita | 6fd9162 | 2016-05-01 20:27:30 +0900 | [diff] [blame] | 1697 | #endif |
Syoyo Fujita | 7bdfed3 | 2019-03-03 17:04:49 +0900 | [diff] [blame] | 1698 | #if __has_warning("-Wmismatched-tags") |
| 1699 | #pragma clang diagnostic ignored "-Wmismatched-tags" |
| 1700 | #endif |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 1701 | #if __has_warning("-Wextra-semi-stmt") |
| 1702 | #pragma clang diagnostic ignored "-Wextra-semi-stmt" |
| 1703 | #endif |
Syoyo Fujita | 7bdfed3 | 2019-03-03 17:04:49 +0900 | [diff] [blame] | 1704 | #endif |
| 1705 | |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 1706 | // Disable GCC warnings |
Syoyo Fujita | 7bdfed3 | 2019-03-03 17:04:49 +0900 | [diff] [blame] | 1707 | #ifdef __GNUC__ |
| 1708 | #pragma GCC diagnostic push |
| 1709 | #pragma GCC diagnostic ignored "-Wtype-limits" |
Syoyo Fujita | ca56f72 | 2019-03-07 21:04:25 +0900 | [diff] [blame] | 1710 | #endif // __GNUC__ |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1711 | |
kroko | fc0116b | 2019-03-03 08:28:49 +0200 | [diff] [blame] | 1712 | #ifndef TINYGLTF_NO_INCLUDE_JSON |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1713 | #ifndef TINYGLTF_USE_RAPIDJSON |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 1714 | #include "json.hpp" |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1715 | #else |
Alvaro Barua | 3ddf830 | 2021-03-18 00:21:23 +0000 | [diff] [blame] | 1716 | #ifndef TINYGLTF_NO_INCLUDE_RAPIDJSON |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1717 | #include "document.h" |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1718 | #include "prettywriter.h" |
| 1719 | #include "rapidjson.h" |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1720 | #include "stringbuffer.h" |
| 1721 | #include "writer.h" |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1722 | #endif |
kroko | f4b6d11 | 2019-03-03 01:11:31 +0200 | [diff] [blame] | 1723 | #endif |
Alvaro Barua | 3ddf830 | 2021-03-18 00:21:23 +0000 | [diff] [blame] | 1724 | #endif |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 1725 | |
| 1726 | #ifdef TINYGLTF_ENABLE_DRACO |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 1727 | #include "draco/compression/decode.h" |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 1728 | #include "draco/core/decoder_buffer.h" |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 1729 | #endif |
Squareys | 2d3594d | 2018-03-13 22:40:53 +0100 | [diff] [blame] | 1730 | |
| 1731 | #ifndef TINYGLTF_NO_STB_IMAGE |
kroko | fc0116b | 2019-03-03 08:28:49 +0200 | [diff] [blame] | 1732 | #ifndef TINYGLTF_NO_INCLUDE_STB_IMAGE |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 1733 | #include "stb_image.h" |
Squareys | 2d3594d | 2018-03-13 22:40:53 +0100 | [diff] [blame] | 1734 | #endif |
kroko | f4b6d11 | 2019-03-03 01:11:31 +0200 | [diff] [blame] | 1735 | #endif |
Squareys | 2d3594d | 2018-03-13 22:40:53 +0100 | [diff] [blame] | 1736 | |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1737 | #ifndef TINYGLTF_NO_STB_IMAGE_WRITE |
kroko | fc0116b | 2019-03-03 08:28:49 +0200 | [diff] [blame] | 1738 | #ifndef TINYGLTF_NO_INCLUDE_STB_IMAGE_WRITE |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 1739 | #include "stb_image_write.h" |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1740 | #endif |
kroko | f4b6d11 | 2019-03-03 01:11:31 +0200 | [diff] [blame] | 1741 | #endif |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 1742 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 1743 | #ifdef __clang__ |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 1744 | #pragma clang diagnostic pop |
Syoyo Fujita | 6fd9162 | 2016-05-01 20:27:30 +0900 | [diff] [blame] | 1745 | #endif |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1746 | |
Syoyo Fujita | 7bdfed3 | 2019-03-03 17:04:49 +0900 | [diff] [blame] | 1747 | #ifdef __GNUC__ |
| 1748 | #pragma GCC diagnostic pop |
| 1749 | #endif |
| 1750 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1751 | #ifdef _WIN32 |
Syoyo Fujita | 7d9a0bd | 2019-02-19 16:03:23 +0900 | [diff] [blame] | 1752 | |
| 1753 | // issue 143. |
| 1754 | // Define NOMINMAX to avoid min/max defines, |
imallett | 3a29588 | 2022-10-07 11:20:39 -0700 | [diff] [blame] | 1755 | // but undef it after included Windows.h |
Syoyo Fujita | 7d9a0bd | 2019-02-19 16:03:23 +0900 | [diff] [blame] | 1756 | #ifndef NOMINMAX |
| 1757 | #define TINYGLTF_INTERNAL_NOMINMAX |
| 1758 | #define NOMINMAX |
| 1759 | #endif |
| 1760 | |
Syoyo Fujita | 87be0ce | 2019-02-19 21:36:32 +0900 | [diff] [blame] | 1761 | #ifndef WIN32_LEAN_AND_MEAN |
| 1762 | #define WIN32_LEAN_AND_MEAN |
| 1763 | #define TINYGLTF_INTERNAL_WIN32_LEAN_AND_MEAN |
| 1764 | #endif |
imallett | 3a29588 | 2022-10-07 11:20:39 -0700 | [diff] [blame] | 1765 | #ifndef __MINGW32__ |
imallett | 56e1098 | 2022-10-07 10:35:16 -0700 | [diff] [blame] | 1766 | #include <Windows.h> // include API for expanding a file path |
imallett | 3a29588 | 2022-10-07 11:20:39 -0700 | [diff] [blame] | 1767 | #else |
| 1768 | #include <windows.h> |
| 1769 | #endif |
Syoyo Fujita | 7d9a0bd | 2019-02-19 16:03:23 +0900 | [diff] [blame] | 1770 | |
Syoyo Fujita | 87be0ce | 2019-02-19 21:36:32 +0900 | [diff] [blame] | 1771 | #ifdef TINYGLTF_INTERNAL_WIN32_LEAN_AND_MEAN |
| 1772 | #undef WIN32_LEAN_AND_MEAN |
| 1773 | #endif |
| 1774 | |
Syoyo Fujita | 7d9a0bd | 2019-02-19 16:03:23 +0900 | [diff] [blame] | 1775 | #if defined(TINYGLTF_INTERNAL_NOMINMAX) |
| 1776 | #undef NOMINMAX |
| 1777 | #endif |
| 1778 | |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 1779 | #if defined(__GLIBCXX__) // mingw |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 1780 | |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 1781 | #include <fcntl.h> // _O_RDONLY |
| 1782 | |
| 1783 | #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] | 1784 | |
| 1785 | #endif |
| 1786 | |
Julian Smith | 0598a20 | 2021-08-25 12:06:08 +0100 | [diff] [blame] | 1787 | #elif !defined(__ANDROID__) && !defined(__OpenBSD__) |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 1788 | //#include <wordexp.h> |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1789 | #endif |
| 1790 | |
Christopher Sean Morrison | 2c9b256 | 2022-05-14 19:00:19 -0400 | [diff] [blame] | 1791 | #if defined(__sparcv9) || defined(__powerpc__) |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 1792 | // Big endian |
| 1793 | #else |
| 1794 | #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU |
| 1795 | #define TINYGLTF_LITTLE_ENDIAN 1 |
| 1796 | #endif |
| 1797 | #endif |
| 1798 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 1799 | namespace tinygltf { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 1800 | namespace detail { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1801 | #ifdef TINYGLTF_USE_RAPIDJSON |
jrkoonce | ce7fa74 | 2019-09-04 13:31:44 -0500 | [diff] [blame] | 1802 | |
| 1803 | #ifdef TINYGLTF_USE_RAPIDJSON_CRTALLOCATOR |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1804 | // This uses the RapidJSON CRTAllocator. It is thread safe and multiple |
| 1805 | // documents may be active at once. |
| 1806 | using json = |
| 1807 | rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAllocator>; |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 1808 | using json_iterator = json::MemberIterator; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1809 | using json_const_iterator = json::ConstMemberIterator; |
| 1810 | using json_const_array_iterator = json const *; |
| 1811 | using JsonDocument = |
jrkoonce | ce7fa74 | 2019-09-04 13:31:44 -0500 | [diff] [blame] | 1812 | rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator>; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1813 | rapidjson::CrtAllocator s_CrtAllocator; // stateless and thread safe |
| 1814 | rapidjson::CrtAllocator &GetAllocator() { return s_CrtAllocator; } |
jrkoonce | ce7fa74 | 2019-09-04 13:31:44 -0500 | [diff] [blame] | 1815 | #else |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1816 | // This uses the default RapidJSON MemoryPoolAllocator. It is very fast, but |
| 1817 | // not thread safe. Only a single JsonDocument may be active at any one time, |
| 1818 | // meaning only a single gltf load/save can be active any one time. |
| 1819 | using json = rapidjson::Value; |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 1820 | using json_iterator = json::MemberIterator; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1821 | using json_const_iterator = json::ConstMemberIterator; |
| 1822 | using json_const_array_iterator = json const *; |
| 1823 | rapidjson::Document *s_pActiveDocument = nullptr; |
| 1824 | rapidjson::Document::AllocatorType &GetAllocator() { |
| 1825 | assert(s_pActiveDocument); // Root json node must be JsonDocument type |
| 1826 | return s_pActiveDocument->GetAllocator(); |
| 1827 | } |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1828 | |
| 1829 | #ifdef __clang__ |
| 1830 | #pragma clang diagnostic push |
| 1831 | // Suppress JsonDocument(JsonDocument &&rhs) noexcept |
| 1832 | #pragma clang diagnostic ignored "-Wunused-member-function" |
| 1833 | #endif |
| 1834 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1835 | struct JsonDocument : public rapidjson::Document { |
| 1836 | JsonDocument() { |
| 1837 | assert(s_pActiveDocument == |
| 1838 | nullptr); // When using default allocator, only one document can be |
| 1839 | // active at a time, if you need multiple active at once, |
| 1840 | // define TINYGLTF_USE_RAPIDJSON_CRTALLOCATOR |
| 1841 | s_pActiveDocument = this; |
jrkoonce | ce7fa74 | 2019-09-04 13:31:44 -0500 | [diff] [blame] | 1842 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1843 | JsonDocument(const JsonDocument &) = delete; |
| 1844 | JsonDocument(JsonDocument &&rhs) noexcept |
jrkoonce | ce7fa74 | 2019-09-04 13:31:44 -0500 | [diff] [blame] | 1845 | : rapidjson::Document(std::move(rhs)) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1846 | s_pActiveDocument = this; |
| 1847 | rhs.isNil = true; |
| 1848 | } |
| 1849 | ~JsonDocument() { |
| 1850 | if (!isNil) { |
| 1851 | s_pActiveDocument = nullptr; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1852 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1853 | } |
jrkoonce | ce7fa74 | 2019-09-04 13:31:44 -0500 | [diff] [blame] | 1854 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1855 | private: |
| 1856 | bool isNil = false; |
| 1857 | }; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1858 | |
| 1859 | #ifdef __clang__ |
| 1860 | #pragma clang diagnostic pop |
| 1861 | #endif |
| 1862 | |
jrkoonce | ce7fa74 | 2019-09-04 13:31:44 -0500 | [diff] [blame] | 1863 | #endif // TINYGLTF_USE_RAPIDJSON_CRTALLOCATOR |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1864 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1865 | #else |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1866 | using nlohmann::json; |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 1867 | using json_iterator = json::iterator; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1868 | using json_const_iterator = json::const_iterator; |
| 1869 | using json_const_array_iterator = json_const_iterator; |
| 1870 | using JsonDocument = json; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1871 | #endif |
| 1872 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1873 | void JsonParse(JsonDocument &doc, const char *str, size_t length, |
| 1874 | bool throwExc = false) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1875 | #ifdef TINYGLTF_USE_RAPIDJSON |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1876 | (void)throwExc; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1877 | doc.Parse(str, length); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1878 | #else |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 1879 | doc = detail::json::parse(str, str + length, nullptr, throwExc); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1880 | #endif |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 1881 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 1882 | } // namespace |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 1883 | } |
Syoyo Fujita | 2e21be7 | 2017-11-05 17:13:01 +0900 | [diff] [blame] | 1884 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1885 | #ifdef __APPLE__ |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 1886 | #include "TargetConditionals.h" |
Syoyo Fujita | b23f6fe | 2017-11-15 01:03:09 +0900 | [diff] [blame] | 1887 | #endif |
| 1888 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 1889 | #ifdef __clang__ |
| 1890 | #pragma clang diagnostic push |
| 1891 | #pragma clang diagnostic ignored "-Wc++98-compat" |
| 1892 | #endif |
| 1893 | |
Syoyo Fujita | e1bef8c | 2016-03-17 21:31:13 +0900 | [diff] [blame] | 1894 | namespace tinygltf { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 1895 | |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1896 | /// |
| 1897 | /// Internal LoadImageDataOption struct. |
| 1898 | /// This struct is passed through `user_pointer` in LoadImageData. |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 1899 | /// The struct is not passed when the user supply their own LoadImageData |
| 1900 | /// callbacks. |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1901 | /// |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 1902 | struct LoadImageDataOption { |
| 1903 | // true: preserve image channels(e.g. load as RGB image if the image has RGB |
| 1904 | // channels) default `false`(channels are expanded to RGBA for backward |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 1905 | // compatibility). |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1906 | bool preserve_channels{false}; |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 1907 | }; |
| 1908 | |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1909 | // Equals function for Value, for recursivity |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1910 | static bool Equals(const tinygltf::Value &one, const tinygltf::Value &other) { |
| 1911 | if (one.Type() != other.Type()) return false; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1912 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1913 | switch (one.Type()) { |
| 1914 | case NULL_TYPE: |
| 1915 | return true; |
| 1916 | case BOOL_TYPE: |
| 1917 | return one.Get<bool>() == other.Get<bool>(); |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 1918 | case REAL_TYPE: |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1919 | return TINYGLTF_DOUBLE_EQUAL(one.Get<double>(), other.Get<double>()); |
| 1920 | case INT_TYPE: |
| 1921 | return one.Get<int>() == other.Get<int>(); |
| 1922 | case OBJECT_TYPE: { |
| 1923 | auto oneObj = one.Get<tinygltf::Value::Object>(); |
| 1924 | auto otherObj = other.Get<tinygltf::Value::Object>(); |
| 1925 | if (oneObj.size() != otherObj.size()) return false; |
| 1926 | for (auto &it : oneObj) { |
| 1927 | auto otherIt = otherObj.find(it.first); |
| 1928 | if (otherIt == otherObj.end()) return false; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1929 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1930 | if (!Equals(it.second, otherIt->second)) return false; |
| 1931 | } |
| 1932 | return true; |
| 1933 | } |
| 1934 | case ARRAY_TYPE: { |
| 1935 | if (one.Size() != other.Size()) return false; |
| 1936 | for (int i = 0; i < int(one.Size()); ++i) |
Selmar | a63cc63 | 2019-04-16 16:57:43 +0200 | [diff] [blame] | 1937 | if (!Equals(one.Get(i), other.Get(i))) return false; |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1938 | return true; |
| 1939 | } |
| 1940 | case STRING_TYPE: |
| 1941 | return one.Get<std::string>() == other.Get<std::string>(); |
| 1942 | case BINARY_TYPE: |
| 1943 | return one.Get<std::vector<unsigned char> >() == |
| 1944 | other.Get<std::vector<unsigned char> >(); |
| 1945 | default: { |
| 1946 | // unhandled type |
| 1947 | return false; |
| 1948 | } |
| 1949 | } |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | // Equals function for std::vector<double> using TINYGLTF_DOUBLE_EPSILON |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1953 | static bool Equals(const std::vector<double> &one, |
| 1954 | const std::vector<double> &other) { |
| 1955 | if (one.size() != other.size()) return false; |
| 1956 | for (int i = 0; i < int(one.size()); ++i) { |
| 1957 | if (!TINYGLTF_DOUBLE_EQUAL(one[size_t(i)], other[size_t(i)])) return false; |
| 1958 | } |
| 1959 | return true; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1960 | } |
| 1961 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1962 | bool Accessor::operator==(const Accessor &other) const { |
| 1963 | return this->bufferView == other.bufferView && |
| 1964 | this->byteOffset == other.byteOffset && |
| 1965 | this->componentType == other.componentType && |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1966 | this->count == other.count && this->extensions == other.extensions && |
| 1967 | this->extras == other.extras && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1968 | Equals(this->maxValues, other.maxValues) && |
| 1969 | Equals(this->minValues, other.minValues) && this->name == other.name && |
| 1970 | this->normalized == other.normalized && this->type == other.type; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1971 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1972 | bool Animation::operator==(const Animation &other) const { |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 1973 | return this->channels == other.channels && |
| 1974 | this->extensions == other.extensions && this->extras == other.extras && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1975 | this->name == other.name && this->samplers == other.samplers; |
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 AnimationChannel::operator==(const AnimationChannel &other) const { |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 1978 | return this->extensions == other.extensions && this->extras == other.extras && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1979 | this->target_node == other.target_node && |
| 1980 | this->target_path == other.target_path && |
| 1981 | this->sampler == other.sampler; |
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 AnimationSampler::operator==(const AnimationSampler &other) const { |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1984 | return this->extras == other.extras && this->extensions == other.extensions && |
| 1985 | this->input == other.input && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1986 | this->interpolation == other.interpolation && |
| 1987 | this->output == other.output; |
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 Asset::operator==(const Asset &other) const { |
| 1990 | return this->copyright == other.copyright && |
| 1991 | this->extensions == other.extensions && this->extras == other.extras && |
| 1992 | this->generator == other.generator && |
| 1993 | this->minVersion == other.minVersion && this->version == other.version; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1994 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 1995 | bool Buffer::operator==(const Buffer &other) const { |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 1996 | return this->data == other.data && this->extensions == other.extensions && |
| 1997 | this->extras == other.extras && this->name == other.name && |
| 1998 | this->uri == other.uri; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 1999 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2000 | bool BufferView::operator==(const BufferView &other) const { |
| 2001 | return this->buffer == other.buffer && this->byteLength == other.byteLength && |
| 2002 | this->byteOffset == other.byteOffset && |
| 2003 | this->byteStride == other.byteStride && this->name == other.name && |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 2004 | this->target == other.target && this->extensions == other.extensions && |
| 2005 | this->extras == other.extras && |
Alexander Wood | 0d77a29 | 2019-01-26 08:58:45 -0500 | [diff] [blame] | 2006 | this->dracoDecoded == other.dracoDecoded; |
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 Camera::operator==(const Camera &other) const { |
| 2009 | return this->name == other.name && this->extensions == other.extensions && |
| 2010 | this->extras == other.extras && |
| 2011 | this->orthographic == other.orthographic && |
| 2012 | this->perspective == other.perspective && this->type == other.type; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2013 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2014 | bool Image::operator==(const Image &other) const { |
| 2015 | return this->bufferView == other.bufferView && |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 2016 | this->component == other.component && |
| 2017 | this->extensions == other.extensions && this->extras == other.extras && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2018 | this->height == other.height && this->image == other.image && |
| 2019 | this->mimeType == other.mimeType && this->name == other.name && |
| 2020 | this->uri == other.uri && this->width == other.width; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2021 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2022 | bool Light::operator==(const Light &other) const { |
| 2023 | return Equals(this->color, other.color) && this->name == other.name && |
| 2024 | this->type == other.type; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2025 | } |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 2026 | bool AudioEmitter::operator==(const AudioEmitter &other) const { |
| 2027 | return this->name == other.name && TINYGLTF_DOUBLE_EQUAL(this->gain, other.gain) && |
| 2028 | this->loop == other.loop && this->playing == other.playing && |
| 2029 | this->type == other.type && this->distanceModel == other.distanceModel && |
| 2030 | this->source == other.source; |
| 2031 | } |
| 2032 | bool AudioSource::operator == (const AudioSource &other) const { |
| 2033 | return this->name == other.name && this->uri == other.uri; |
| 2034 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2035 | bool Material::operator==(const Material &other) const { |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 2036 | return (this->pbrMetallicRoughness == other.pbrMetallicRoughness) && |
| 2037 | (this->normalTexture == other.normalTexture) && |
| 2038 | (this->occlusionTexture == other.occlusionTexture) && |
| 2039 | (this->emissiveTexture == other.emissiveTexture) && |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 2040 | Equals(this->emissiveFactor, other.emissiveFactor) && |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 2041 | (this->alphaMode == other.alphaMode) && |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 2042 | TINYGLTF_DOUBLE_EQUAL(this->alphaCutoff, other.alphaCutoff) && |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 2043 | (this->doubleSided == other.doubleSided) && |
| 2044 | (this->extensions == other.extensions) && |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 2045 | (this->extras == other.extras) && (this->values == other.values) && |
| 2046 | (this->additionalValues == other.additionalValues) && |
| 2047 | (this->name == other.name); |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2048 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2049 | bool Mesh::operator==(const Mesh &other) const { |
| 2050 | return this->extensions == other.extensions && this->extras == other.extras && |
Selmar Kok | 81b672b | 2019-10-18 16:08:44 +0200 | [diff] [blame] | 2051 | this->name == other.name && Equals(this->weights, other.weights) && |
| 2052 | this->primitives == other.primitives; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2053 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2054 | bool Model::operator==(const Model &other) const { |
| 2055 | return this->accessors == other.accessors && |
| 2056 | this->animations == other.animations && this->asset == other.asset && |
| 2057 | this->buffers == other.buffers && |
| 2058 | this->bufferViews == other.bufferViews && |
| 2059 | this->cameras == other.cameras && |
| 2060 | this->defaultScene == other.defaultScene && |
| 2061 | this->extensions == other.extensions && |
| 2062 | this->extensionsRequired == other.extensionsRequired && |
| 2063 | this->extensionsUsed == other.extensionsUsed && |
| 2064 | this->extras == other.extras && this->images == other.images && |
| 2065 | this->lights == other.lights && this->materials == other.materials && |
| 2066 | this->meshes == other.meshes && this->nodes == other.nodes && |
| 2067 | this->samplers == other.samplers && this->scenes == other.scenes && |
| 2068 | this->skins == other.skins && this->textures == other.textures; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2069 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2070 | bool Node::operator==(const Node &other) const { |
| 2071 | return this->camera == other.camera && this->children == other.children && |
| 2072 | this->extensions == other.extensions && this->extras == other.extras && |
| 2073 | Equals(this->matrix, other.matrix) && this->mesh == other.mesh && |
Syoyo Fujita | 7a570c8 | 2023-06-19 21:52:13 +0900 | [diff] [blame] | 2074 | (this->light == other.light) && |
| 2075 | (this->emitter == other.emitter) && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2076 | this->name == other.name && Equals(this->rotation, other.rotation) && |
| 2077 | Equals(this->scale, other.scale) && this->skin == other.skin && |
| 2078 | Equals(this->translation, other.translation) && |
| 2079 | Equals(this->weights, other.weights); |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2080 | } |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 2081 | bool SpotLight::operator==(const SpotLight &other) const { |
| 2082 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2083 | TINYGLTF_DOUBLE_EQUAL(this->innerConeAngle, other.innerConeAngle) && |
| 2084 | TINYGLTF_DOUBLE_EQUAL(this->outerConeAngle, other.outerConeAngle); |
| 2085 | } |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 2086 | bool PositionalEmitter::operator == (const PositionalEmitter& other) const { |
| 2087 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2088 | TINYGLTF_DOUBLE_EQUAL(this->coneInnerAngle, other.coneInnerAngle) && |
| 2089 | TINYGLTF_DOUBLE_EQUAL(this->coneOuterAngle, other.coneOuterAngle) && |
| 2090 | TINYGLTF_DOUBLE_EQUAL(this->coneOuterGain, other.coneOuterGain) && |
| 2091 | TINYGLTF_DOUBLE_EQUAL(this->maxDistance, other.maxDistance) && |
| 2092 | TINYGLTF_DOUBLE_EQUAL(this->refDistance, other.refDistance) && |
| 2093 | TINYGLTF_DOUBLE_EQUAL(this->rolloffFactor, other.rolloffFactor); |
| 2094 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2095 | bool OrthographicCamera::operator==(const OrthographicCamera &other) const { |
| 2096 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2097 | TINYGLTF_DOUBLE_EQUAL(this->xmag, other.xmag) && |
| 2098 | TINYGLTF_DOUBLE_EQUAL(this->ymag, other.ymag) && |
| 2099 | TINYGLTF_DOUBLE_EQUAL(this->zfar, other.zfar) && |
| 2100 | TINYGLTF_DOUBLE_EQUAL(this->znear, other.znear); |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2101 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2102 | bool Parameter::operator==(const Parameter &other) const { |
| 2103 | if (this->bool_value != other.bool_value || |
| 2104 | this->has_number_value != other.has_number_value) |
| 2105 | return false; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2106 | |
Selmar Kok | 2bda71c | 2018-10-05 14:36:05 +0200 | [diff] [blame] | 2107 | if (!TINYGLTF_DOUBLE_EQUAL(this->number_value, other.number_value)) |
| 2108 | return false; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2109 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2110 | if (this->json_double_value.size() != other.json_double_value.size()) |
| 2111 | return false; |
| 2112 | for (auto &it : this->json_double_value) { |
| 2113 | auto otherIt = other.json_double_value.find(it.first); |
| 2114 | if (otherIt == other.json_double_value.end()) return false; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2115 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2116 | if (!TINYGLTF_DOUBLE_EQUAL(it.second, otherIt->second)) return false; |
| 2117 | } |
| 2118 | |
| 2119 | if (!Equals(this->number_array, other.number_array)) return false; |
| 2120 | |
| 2121 | if (this->string_value != other.string_value) return false; |
| 2122 | |
| 2123 | return true; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2124 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2125 | bool PerspectiveCamera::operator==(const PerspectiveCamera &other) const { |
| 2126 | return TINYGLTF_DOUBLE_EQUAL(this->aspectRatio, other.aspectRatio) && |
| 2127 | this->extensions == other.extensions && this->extras == other.extras && |
| 2128 | TINYGLTF_DOUBLE_EQUAL(this->yfov, other.yfov) && |
| 2129 | TINYGLTF_DOUBLE_EQUAL(this->zfar, other.zfar) && |
| 2130 | TINYGLTF_DOUBLE_EQUAL(this->znear, other.znear); |
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 Primitive::operator==(const Primitive &other) const { |
| 2133 | return this->attributes == other.attributes && this->extras == other.extras && |
| 2134 | this->indices == other.indices && this->material == other.material && |
| 2135 | this->mode == other.mode && this->targets == other.targets; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2136 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2137 | bool Sampler::operator==(const Sampler &other) const { |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 2138 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2139 | this->magFilter == other.magFilter && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2140 | this->minFilter == other.minFilter && this->name == other.name && |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 2141 | this->wrapS == other.wrapS && this->wrapT == other.wrapT; |
Syoyo Fujita | 2c521b3 | 2020-12-04 00:50:46 +0900 | [diff] [blame] | 2142 | |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 2143 | // this->wrapR == other.wrapR |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2144 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2145 | bool Scene::operator==(const Scene &other) const { |
| 2146 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2147 | this->name == other.name && this->nodes == other.nodes; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2148 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2149 | bool Skin::operator==(const Skin &other) const { |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 2150 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2151 | this->inverseBindMatrices == other.inverseBindMatrices && |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2152 | this->joints == other.joints && this->name == other.name && |
| 2153 | this->skeleton == other.skeleton; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2154 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2155 | bool Texture::operator==(const Texture &other) const { |
| 2156 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2157 | this->name == other.name && this->sampler == other.sampler && |
| 2158 | this->source == other.source; |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2159 | } |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 2160 | bool TextureInfo::operator==(const TextureInfo &other) const { |
| 2161 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2162 | this->index == other.index && this->texCoord == other.texCoord; |
| 2163 | } |
| 2164 | bool NormalTextureInfo::operator==(const NormalTextureInfo &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->scale, other.scale); |
| 2168 | } |
| 2169 | bool OcclusionTextureInfo::operator==(const OcclusionTextureInfo &other) const { |
| 2170 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2171 | this->index == other.index && this->texCoord == other.texCoord && |
| 2172 | TINYGLTF_DOUBLE_EQUAL(this->strength, other.strength); |
| 2173 | } |
| 2174 | bool PbrMetallicRoughness::operator==(const PbrMetallicRoughness &other) const { |
| 2175 | return this->extensions == other.extensions && this->extras == other.extras && |
| 2176 | (this->baseColorTexture == other.baseColorTexture) && |
| 2177 | (this->metallicRoughnessTexture == other.metallicRoughnessTexture) && |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 2178 | Equals(this->baseColorFactor, other.baseColorFactor) && |
Syoyo Fujita | 89fd93f | 2019-07-23 22:37:06 +0900 | [diff] [blame] | 2179 | TINYGLTF_DOUBLE_EQUAL(this->metallicFactor, other.metallicFactor) && |
| 2180 | TINYGLTF_DOUBLE_EQUAL(this->roughnessFactor, other.roughnessFactor); |
| 2181 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2182 | bool Value::operator==(const Value &other) const { |
| 2183 | return Equals(*this, other); |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 2184 | } |
| 2185 | |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 2186 | static void swap4(unsigned int *val) { |
| 2187 | #ifdef TINYGLTF_LITTLE_ENDIAN |
| 2188 | (void)val; |
| 2189 | #else |
| 2190 | unsigned int tmp = *val; |
| 2191 | unsigned char *dst = reinterpret_cast<unsigned char *>(val); |
| 2192 | unsigned char *src = reinterpret_cast<unsigned char *>(&tmp); |
| 2193 | |
| 2194 | dst[0] = src[3]; |
| 2195 | dst[1] = src[2]; |
| 2196 | dst[2] = src[1]; |
| 2197 | dst[3] = src[0]; |
| 2198 | #endif |
| 2199 | } |
| 2200 | |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2201 | static std::string JoinPath(const std::string &path0, |
| 2202 | const std::string &path1) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2203 | if (path0.empty()) { |
| 2204 | return path1; |
| 2205 | } else { |
| 2206 | // check '/' |
| 2207 | char lastChar = *path0.rbegin(); |
| 2208 | if (lastChar != '/') { |
| 2209 | return path0 + std::string("/") + path1; |
| 2210 | } else { |
| 2211 | return path0 + path1; |
| 2212 | } |
| 2213 | } |
| 2214 | } |
| 2215 | |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 2216 | static std::string FindFile(const std::vector<std::string> &paths, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2217 | const std::string &filepath, FsCallbacks *fs) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2218 | if (fs == nullptr || fs->ExpandFilePath == nullptr || |
| 2219 | fs->FileExists == nullptr) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2220 | // Error, fs callback[s] missing |
| 2221 | return std::string(); |
| 2222 | } |
| 2223 | |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2224 | // https://github.com/syoyo/tinygltf/issues/416 |
| 2225 | // Use strlen() since std::string's size/length reports the number of elements in the buffer, not the length of string(null-terminated) |
| 2226 | // strip null-character in the middle of string. |
| 2227 | size_t slength = strlen(filepath.c_str()); |
| 2228 | if (slength == 0) { |
| 2229 | return std::string(); |
| 2230 | } |
| 2231 | |
| 2232 | std::string cleaned_filepath = std::string(filepath.c_str()); |
| 2233 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2234 | for (size_t i = 0; i < paths.size(); i++) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2235 | std::string absPath = |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2236 | fs->ExpandFilePath(JoinPath(paths[i], cleaned_filepath), fs->user_data); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2237 | if (fs->FileExists(absPath, fs->user_data)) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2238 | return absPath; |
| 2239 | } |
| 2240 | } |
| 2241 | |
| 2242 | return std::string(); |
| 2243 | } |
| 2244 | |
Syoyo Fujita | dc4bb86 | 2018-04-02 02:04:24 +0900 | [diff] [blame] | 2245 | static std::string GetFilePathExtension(const std::string &FileName) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2246 | if (FileName.find_last_of(".") != std::string::npos) |
| 2247 | return FileName.substr(FileName.find_last_of(".") + 1); |
| 2248 | return ""; |
| 2249 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2250 | |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 2251 | static std::string GetBaseDir(const std::string &filepath) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2252 | if (filepath.find_last_of("/\\") != std::string::npos) |
| 2253 | return filepath.substr(0, filepath.find_last_of("/\\")); |
| 2254 | return ""; |
| 2255 | } |
| 2256 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2257 | static std::string GetBaseFilename(const std::string &filepath) { |
Martin Gerhardy | fae6543 | 2022-03-13 18:42:39 +0100 | [diff] [blame] | 2258 | auto idx = filepath.find_last_of("/\\"); |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 2259 | if (idx != std::string::npos) return filepath.substr(idx + 1); |
Alexander Wood | 190382a | 2021-10-08 12:19:13 -0400 | [diff] [blame] | 2260 | return filepath; |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 2261 | } |
| 2262 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2263 | std::string base64_encode(unsigned char const *, unsigned int len); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2264 | std::string base64_decode(std::string const &s); |
| 2265 | |
| 2266 | /* |
| 2267 | base64.cpp and base64.h |
| 2268 | |
| 2269 | Copyright (C) 2004-2008 René Nyffenegger |
| 2270 | |
| 2271 | This source code is provided 'as-is', without any express or implied |
| 2272 | warranty. In no event will the author be held liable for any damages |
| 2273 | arising from the use of this software. |
| 2274 | |
| 2275 | Permission is granted to anyone to use this software for any purpose, |
| 2276 | including commercial applications, and to alter it and redistribute it |
| 2277 | freely, subject to the following restrictions: |
| 2278 | |
| 2279 | 1. The origin of this source code must not be misrepresented; you must not |
| 2280 | claim that you wrote the original source code. If you use this source code |
| 2281 | in a product, an acknowledgment in the product documentation would be |
| 2282 | appreciated but is not required. |
| 2283 | |
| 2284 | 2. Altered source versions must be plainly marked as such, and must not be |
| 2285 | misrepresented as being the original source code. |
| 2286 | |
| 2287 | 3. This notice may not be removed or altered from any source distribution. |
| 2288 | |
| 2289 | René Nyffenegger rene.nyffenegger@adp-gmbh.ch |
| 2290 | |
| 2291 | */ |
| 2292 | |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 2293 | #ifdef __clang__ |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 2294 | #pragma clang diagnostic push |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 2295 | #pragma clang diagnostic ignored "-Wsign-conversion" |
| 2296 | #pragma clang diagnostic ignored "-Wconversion" |
Syoyo Fujita | 6fd9162 | 2016-05-01 20:27:30 +0900 | [diff] [blame] | 2297 | #endif |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2298 | |
| 2299 | static inline bool is_base64(unsigned char c) { |
| 2300 | return (isalnum(c) || (c == '+') || (c == '/')); |
| 2301 | } |
| 2302 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2303 | std::string base64_encode(unsigned char const *bytes_to_encode, |
| 2304 | unsigned int in_len) { |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 2305 | std::string ret; |
| 2306 | int i = 0; |
| 2307 | int j = 0; |
| 2308 | unsigned char char_array_3[3]; |
| 2309 | unsigned char char_array_4[4]; |
| 2310 | |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 2311 | const char *base64_chars = |
| 2312 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 2313 | "abcdefghijklmnopqrstuvwxyz" |
| 2314 | "0123456789+/"; |
| 2315 | |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 2316 | while (in_len--) { |
| 2317 | char_array_3[i++] = *(bytes_to_encode++); |
| 2318 | if (i == 3) { |
| 2319 | char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2320 | char_array_4[1] = |
| 2321 | ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); |
| 2322 | char_array_4[2] = |
| 2323 | ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 2324 | char_array_4[3] = char_array_3[2] & 0x3f; |
| 2325 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2326 | 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] | 2327 | i = 0; |
| 2328 | } |
| 2329 | } |
| 2330 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2331 | if (i) { |
| 2332 | for (j = i; j < 3; j++) char_array_3[j] = '\0'; |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 2333 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2334 | char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; |
| 2335 | char_array_4[1] = |
| 2336 | ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); |
| 2337 | char_array_4[2] = |
| 2338 | ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 2339 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2340 | 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] | 2341 | |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 2342 | while ((i++ < 3)) ret += '='; |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 2343 | } |
| 2344 | |
| 2345 | return ret; |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 2346 | } |
| 2347 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2348 | std::string base64_decode(std::string const &encoded_string) { |
Syoyo Fujita | 8c6774c | 2016-05-01 20:36:29 +0900 | [diff] [blame] | 2349 | int in_len = static_cast<int>(encoded_string.size()); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2350 | int i = 0; |
| 2351 | int j = 0; |
| 2352 | int in_ = 0; |
| 2353 | unsigned char char_array_4[4], char_array_3[3]; |
| 2354 | std::string ret; |
| 2355 | |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 2356 | const std::string base64_chars = |
| 2357 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 2358 | "abcdefghijklmnopqrstuvwxyz" |
| 2359 | "0123456789+/"; |
| 2360 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2361 | while (in_len-- && (encoded_string[in_] != '=') && |
| 2362 | is_base64(encoded_string[in_])) { |
| 2363 | char_array_4[i++] = encoded_string[in_]; |
| 2364 | in_++; |
| 2365 | if (i == 4) { |
| 2366 | for (i = 0; i < 4; i++) |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2367 | char_array_4[i] = |
| 2368 | static_cast<unsigned char>(base64_chars.find(char_array_4[i])); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2369 | |
| 2370 | char_array_3[0] = |
| 2371 | (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); |
| 2372 | char_array_3[1] = |
| 2373 | ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); |
| 2374 | char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; |
| 2375 | |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 2376 | for (i = 0; (i < 3); i++) ret += char_array_3[i]; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2377 | i = 0; |
| 2378 | } |
| 2379 | } |
| 2380 | |
| 2381 | if (i) { |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 2382 | for (j = i; j < 4; j++) char_array_4[j] = 0; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2383 | |
| 2384 | for (j = 0; j < 4; j++) |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2385 | char_array_4[j] = |
| 2386 | static_cast<unsigned char>(base64_chars.find(char_array_4[j])); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2387 | |
| 2388 | char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); |
| 2389 | char_array_3[1] = |
| 2390 | ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); |
| 2391 | char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; |
| 2392 | |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 2393 | for (j = 0; (j < i - 1); j++) ret += char_array_3[j]; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2394 | } |
| 2395 | |
| 2396 | return ret; |
| 2397 | } |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 2398 | #ifdef __clang__ |
Syoyo Fujita | 643ce10 | 2016-05-01 17:19:37 +0900 | [diff] [blame] | 2399 | #pragma clang diagnostic pop |
Syoyo Fujita | 6fd9162 | 2016-05-01 20:27:30 +0900 | [diff] [blame] | 2400 | #endif |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2401 | |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2402 | // https://github.com/syoyo/tinygltf/issues/228 |
| 2403 | // TODO(syoyo): Use uriparser https://uriparser.github.io/ for stricter Uri |
| 2404 | // decoding? |
| 2405 | // |
Alexander Wood | e4bc6c7 | 2021-10-14 08:54:59 -0400 | [diff] [blame] | 2406 | // Uri Decoding from DLIB |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2407 | // http://dlib.net/dlib/server/server_http.cpp.html |
Alexander Wood | e4bc6c7 | 2021-10-14 08:54:59 -0400 | [diff] [blame] | 2408 | // --- dlib begin ------------------------------------------------------------ |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2409 | // Copyright (C) 2003 Davis E. King (davis@dlib.net) |
Alexander Wood | e4bc6c7 | 2021-10-14 08:54:59 -0400 | [diff] [blame] | 2410 | // License: Boost Software License |
| 2411 | // Boost Software License - Version 1.0 - August 17th, 2003 |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2412 | |
Alexander Wood | e4bc6c7 | 2021-10-14 08:54:59 -0400 | [diff] [blame] | 2413 | // Permission is hereby granted, free of charge, to any person or organization |
| 2414 | // obtaining a copy of the software and accompanying documentation covered by |
| 2415 | // this license (the "Software") to use, reproduce, display, distribute, |
| 2416 | // execute, and transmit the Software, and to prepare derivative works of the |
| 2417 | // Software, and to permit third-parties to whom the Software is furnished to |
| 2418 | // do so, all subject to the following: |
| 2419 | // The copyright notices in the Software and this entire statement, including |
| 2420 | // the above license grant, this restriction and the following disclaimer, |
| 2421 | // must be included in all copies of the Software, in whole or in part, and |
| 2422 | // all derivative works of the Software, unless such copies or derivative |
| 2423 | // works are solely in the form of machine-executable object code generated by |
| 2424 | // a source language processor. |
| 2425 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 2426 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 2427 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
| 2428 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
| 2429 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
| 2430 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 2431 | // DEALINGS IN THE SOFTWARE. |
Syoyo Fujita | cbd3885 | 2022-02-26 21:19:15 +0900 | [diff] [blame] | 2432 | // |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2433 | namespace dlib { |
| 2434 | |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2435 | inline unsigned char from_hex(unsigned char ch) { |
| 2436 | if (ch <= '9' && ch >= '0') |
| 2437 | ch -= '0'; |
| 2438 | else if (ch <= 'f' && ch >= 'a') |
| 2439 | ch -= 'a' - 10; |
| 2440 | else if (ch <= 'F' && ch >= 'A') |
| 2441 | ch -= 'A' - 10; |
| 2442 | else |
| 2443 | ch = 0; |
| 2444 | return ch; |
| 2445 | } |
| 2446 | |
| 2447 | static const std::string urldecode(const std::string &str) { |
| 2448 | using namespace std; |
| 2449 | string result; |
| 2450 | string::size_type i; |
| 2451 | for (i = 0; i < str.size(); ++i) { |
| 2452 | if (str[i] == '+') { |
| 2453 | result += ' '; |
| 2454 | } else if (str[i] == '%' && str.size() > i + 2) { |
| 2455 | const unsigned char ch1 = |
| 2456 | from_hex(static_cast<unsigned char>(str[i + 1])); |
| 2457 | const unsigned char ch2 = |
| 2458 | from_hex(static_cast<unsigned char>(str[i + 2])); |
| 2459 | const unsigned char ch = static_cast<unsigned char>((ch1 << 4) | ch2); |
| 2460 | result += static_cast<char>(ch); |
| 2461 | i += 2; |
| 2462 | } else { |
| 2463 | result += str[i]; |
| 2464 | } |
| 2465 | } |
| 2466 | return result; |
| 2467 | } |
| 2468 | |
| 2469 | } // namespace dlib |
| 2470 | // --- dlib end -------------------------------------------------------------- |
| 2471 | |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 2472 | bool URIDecode(const std::string &in_uri, std::string *out_uri, |
| 2473 | void *user_data) { |
| 2474 | (void)user_data; |
| 2475 | *out_uri = dlib::urldecode(in_uri); |
| 2476 | return true; |
| 2477 | } |
| 2478 | |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 2479 | static bool LoadExternalFile(std::vector<unsigned char> *out, std::string *err, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2480 | std::string *warn, const std::string &filename, |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2481 | const std::string &basedir, bool required, |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2482 | size_t reqBytes, bool checkSize, size_t maxFileSize, FsCallbacks *fs) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2483 | if (fs == nullptr || fs->FileExists == nullptr || |
| 2484 | fs->ExpandFilePath == nullptr || fs->ReadWholeFile == nullptr) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2485 | // This is a developer error, assert() ? |
| 2486 | if (err) { |
| 2487 | (*err) += "FS callback[s] not set\n"; |
| 2488 | } |
| 2489 | return false; |
| 2490 | } |
| 2491 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2492 | std::string *failMsgOut = required ? err : warn; |
Selmar Kok | e3b3fa9 | 2018-08-22 19:04:21 +0200 | [diff] [blame] | 2493 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 2494 | out->clear(); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2495 | |
| 2496 | std::vector<std::string> paths; |
| 2497 | paths.push_back(basedir); |
| 2498 | paths.push_back("."); |
| 2499 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2500 | std::string filepath = FindFile(paths, filename, fs); |
jianghaosen | b721fb7 | 2017-08-25 21:01:17 +0800 | [diff] [blame] | 2501 | if (filepath.empty() || filename.empty()) { |
Selmar Kok | e3b3fa9 | 2018-08-22 19:04:21 +0200 | [diff] [blame] | 2502 | if (failMsgOut) { |
| 2503 | (*failMsgOut) += "File not found : " + filename + "\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 2504 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 2505 | return false; |
| 2506 | } |
| 2507 | |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2508 | // Check file size |
| 2509 | if (fs->GetFileSizeInBytes) { |
| 2510 | |
| 2511 | size_t file_size{0}; |
| 2512 | std::string _err; |
David Siegel | cc93e1f | 2023-04-26 12:13:41 +0200 | [diff] [blame] | 2513 | bool ok = fs->GetFileSizeInBytes(&file_size, &_err, filepath, fs->user_data); |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2514 | if (!ok) { |
| 2515 | if (_err.size()) { |
| 2516 | if (failMsgOut) { |
| 2517 | (*failMsgOut) += "Getting file size failed : " + filename + ", err = " + _err + "\n"; |
| 2518 | } |
| 2519 | } |
| 2520 | return false; |
| 2521 | } |
| 2522 | |
| 2523 | if (file_size > maxFileSize) { |
| 2524 | if (failMsgOut) { |
| 2525 | (*failMsgOut) += "File size " + std::to_string(file_size) + " exceeds maximum allowed file size " + std::to_string(maxFileSize) + " : " + filepath + "\n"; |
| 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 | |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 2570 | void TinyGLTF::SetImageLoader(LoadImageDataFunction func, void *user_data) { |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 2571 | LoadImageData = func; |
| 2572 | load_image_user_data_ = user_data; |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 2573 | user_image_loader_ = true; |
| 2574 | } |
| 2575 | |
| 2576 | void TinyGLTF::RemoveImageLoader() { |
| 2577 | LoadImageData = |
| 2578 | #ifndef TINYGLTF_NO_STB_IMAGE |
| 2579 | &tinygltf::LoadImageData; |
| 2580 | #else |
| 2581 | nullptr; |
| 2582 | #endif |
| 2583 | |
| 2584 | load_image_user_data_ = nullptr; |
| 2585 | user_image_loader_ = false; |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 2586 | } |
| 2587 | |
Squareys | 2d3594d | 2018-03-13 22:40:53 +0100 | [diff] [blame] | 2588 | #ifndef TINYGLTF_NO_STB_IMAGE |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2589 | bool LoadImageData(Image *image, const int image_idx, std::string *err, |
| 2590 | std::string *warn, int req_width, int req_height, |
| 2591 | const unsigned char *bytes, int size, void *user_data) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2592 | (void)warn; |
| 2593 | |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 2594 | LoadImageDataOption option; |
| 2595 | if (user_data) { |
| 2596 | option = *reinterpret_cast<LoadImageDataOption *>(user_data); |
| 2597 | } |
| 2598 | |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 2599 | int w = 0, h = 0, comp = 0, req_comp = 0; |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2600 | |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2601 | unsigned char *data = nullptr; |
Arthur Brainville (Ybalrid) | f2addc0 | 2019-03-02 22:00:48 +0100 | [diff] [blame] | 2602 | |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 2603 | // preserve_channels true: Use channels stored in the image file. |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 2604 | // false: force 32-bit textures for common Vulkan compatibility. It appears |
| 2605 | // that some GPU drivers do not support 24-bit images for Vulkan |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 2606 | req_comp = option.preserve_channels ? 0 : 4; |
Arthur Brainville (Ybalrid) | f2addc0 | 2019-03-02 22:00:48 +0100 | [diff] [blame] | 2607 | int bits = 8; |
| 2608 | int pixel_type = TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE; |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2609 | |
Arthur Brainville (Ybalrid) | f2addc0 | 2019-03-02 22:00:48 +0100 | [diff] [blame] | 2610 | // It is possible that the image we want to load is a 16bit per channel image |
| 2611 | // 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] | 2612 | // 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] | 2613 | // unsigned char, because we are representing "bytes". But we are updating |
| 2614 | // the Image metadata to signal that this image uses 2 bytes (16bits) per |
| 2615 | // channel: |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2616 | if (stbi_is_16_bit_from_memory(bytes, size)) { |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 2617 | data = reinterpret_cast<unsigned char *>( |
| 2618 | 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] | 2619 | if (data) { |
| 2620 | bits = 16; |
| 2621 | pixel_type = TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT; |
| 2622 | } |
Arthur Brainville (Ybalrid) | f2addc0 | 2019-03-02 22:00:48 +0100 | [diff] [blame] | 2623 | } |
| 2624 | |
| 2625 | // at this point, if data is still NULL, it means that the image wasn't |
| 2626 | // 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] | 2627 | // image as we used to do: |
Aurélien Chatelain | 5cb4346 | 2017-05-24 09:55:14 +0000 | [diff] [blame] | 2628 | // if image cannot be decoded, ignore parsing and keep it by its path |
| 2629 | // don't break in this case |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 2630 | // FIXME we should only enter this function if the image is embedded. If |
| 2631 | // image->uri references |
| 2632 | // an image file, it should be left as it is. Image loading should not be |
| 2633 | // mandatory (to support other formats) |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2634 | 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] | 2635 | if (!data) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2636 | // NOTE: you can use `warn` instead of `err` |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2637 | if (err) { |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2638 | (*err) += |
| 2639 | "Unknown image format. STB cannot decode image data for image[" + |
| 2640 | std::to_string(image_idx) + "] name = \"" + image->name + "\".\n"; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2641 | } |
Omar C. F | d492efc | 2018-02-10 09:50:35 +0200 | [diff] [blame] | 2642 | return false; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2643 | } |
| 2644 | |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 2645 | if ((w < 1) || (h < 1)) { |
Arthur Brainville (Ybalrid) | f2addc0 | 2019-03-02 22:00:48 +0100 | [diff] [blame] | 2646 | stbi_image_free(data); |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2647 | if (err) { |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2648 | (*err) += "Invalid image data for image[" + std::to_string(image_idx) + |
| 2649 | "] name = \"" + image->name + "\"\n"; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2650 | } |
Omar C. F | d492efc | 2018-02-10 09:50:35 +0200 | [diff] [blame] | 2651 | return false; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | if (req_width > 0) { |
| 2655 | if (req_width != w) { |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2656 | stbi_image_free(data); |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2657 | if (err) { |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2658 | (*err) += "Image width mismatch for image[" + |
| 2659 | std::to_string(image_idx) + "] name = \"" + image->name + |
| 2660 | "\"\n"; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2661 | } |
| 2662 | return false; |
| 2663 | } |
| 2664 | } |
| 2665 | |
| 2666 | if (req_height > 0) { |
| 2667 | if (req_height != h) { |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2668 | stbi_image_free(data); |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2669 | if (err) { |
Arthur Brainville (Ybalrid) | 5a4c898 | 2019-03-02 22:03:34 +0100 | [diff] [blame] | 2670 | (*err) += "Image height mismatch. for image[" + |
| 2671 | std::to_string(image_idx) + "] name = \"" + image->name + |
| 2672 | "\"\n"; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2673 | } |
| 2674 | return false; |
| 2675 | } |
| 2676 | } |
| 2677 | |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 2678 | if (req_comp != 0) { |
| 2679 | // loaded data has `req_comp` channels(components) |
| 2680 | comp = req_comp; |
| 2681 | } |
| 2682 | |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2683 | image->width = w; |
| 2684 | image->height = h; |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 2685 | image->component = comp; |
Arthur Brainville (Ybalrid) | f2addc0 | 2019-03-02 22:00:48 +0100 | [diff] [blame] | 2686 | image->bits = bits; |
| 2687 | image->pixel_type = pixel_type; |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 2688 | image->image.resize(static_cast<size_t>(w * h * comp) * size_t(bits / 8)); |
| 2689 | 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] | 2690 | stbi_image_free(data); |
Syoyo Fujita | 4be2f88 | 2016-11-24 16:20:34 +0900 | [diff] [blame] | 2691 | |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2692 | return true; |
| 2693 | } |
Squareys | 2d3594d | 2018-03-13 22:40:53 +0100 | [diff] [blame] | 2694 | #endif |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 2695 | |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2696 | void TinyGLTF::SetImageWriter(WriteImageDataFunction func, void *user_data) { |
| 2697 | WriteImageData = func; |
| 2698 | write_image_user_data_ = user_data; |
| 2699 | } |
| 2700 | |
| 2701 | #ifndef TINYGLTF_NO_STB_IMAGE_WRITE |
| 2702 | static void WriteToMemory_stbi(void *context, void *data, int size) { |
| 2703 | std::vector<unsigned char> *buffer = |
| 2704 | reinterpret_cast<std::vector<unsigned char> *>(context); |
| 2705 | |
| 2706 | unsigned char *pData = reinterpret_cast<unsigned char *>(data); |
| 2707 | |
| 2708 | buffer->insert(buffer->end(), pData, pData + size); |
| 2709 | } |
| 2710 | |
| 2711 | bool WriteImageData(const std::string *basepath, const std::string *filename, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 2712 | const Image *image, bool embedImages, |
| 2713 | const URICallbacks *uri_cb, std::string *out_uri, |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 2714 | void *fsPtr) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2715 | const std::string ext = GetFilePathExtension(*filename); |
| 2716 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2717 | // Write image to temporary buffer |
| 2718 | std::string header; |
| 2719 | std::vector<unsigned char> data; |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2720 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2721 | if (ext == "png") { |
Syoyo Fujita | ca56f72 | 2019-03-07 21:04:25 +0900 | [diff] [blame] | 2722 | if ((image->bits != 8) || |
| 2723 | (image->pixel_type != TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE)) { |
Syoyo Fujita | e8a46c4 | 2019-03-07 20:50:58 +0900 | [diff] [blame] | 2724 | // Unsupported pixel format |
| 2725 | return false; |
| 2726 | } |
| 2727 | |
Keiji Yoshimi | c0cfc1e | 2018-08-28 21:13:03 +0900 | [diff] [blame] | 2728 | if (!stbi_write_png_to_func(WriteToMemory_stbi, &data, image->width, |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2729 | image->height, image->component, |
| 2730 | &image->image[0], 0)) { |
Keiji Yoshimi | c0cfc1e | 2018-08-28 21:13:03 +0900 | [diff] [blame] | 2731 | return false; |
| 2732 | } |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2733 | header = "data:image/png;base64,"; |
| 2734 | } else if (ext == "jpg") { |
Keiji Yoshimi | c0cfc1e | 2018-08-28 21:13:03 +0900 | [diff] [blame] | 2735 | if (!stbi_write_jpg_to_func(WriteToMemory_stbi, &data, image->width, |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2736 | image->height, image->component, |
| 2737 | &image->image[0], 100)) { |
Keiji Yoshimi | c0cfc1e | 2018-08-28 21:13:03 +0900 | [diff] [blame] | 2738 | return false; |
| 2739 | } |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2740 | header = "data:image/jpeg;base64,"; |
| 2741 | } else if (ext == "bmp") { |
Keiji Yoshimi | c0cfc1e | 2018-08-28 21:13:03 +0900 | [diff] [blame] | 2742 | if (!stbi_write_bmp_to_func(WriteToMemory_stbi, &data, image->width, |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 2743 | image->height, image->component, |
| 2744 | &image->image[0])) { |
Keiji Yoshimi | c0cfc1e | 2018-08-28 21:13:03 +0900 | [diff] [blame] | 2745 | return false; |
| 2746 | } |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2747 | header = "data:image/bmp;base64,"; |
| 2748 | } else if (!embedImages) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2749 | // Error: can't output requested format to file |
| 2750 | return false; |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2751 | } |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2752 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2753 | if (embedImages) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2754 | // Embed base64-encoded image into URI |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2755 | if (data.size()) { |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 2756 | *out_uri = header + |
| 2757 | base64_encode(&data[0], static_cast<unsigned int>(data.size())); |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2758 | } else { |
| 2759 | // Throw error? |
| 2760 | } |
| 2761 | } else { |
| 2762 | // Write image to disc |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2763 | FsCallbacks *fs = reinterpret_cast<FsCallbacks *>(fsPtr); |
Syoyo Fujita | 33514af | 2018-11-05 13:32:43 +0900 | [diff] [blame] | 2764 | if ((fs != nullptr) && (fs->WriteWholeFile != nullptr)) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2765 | const std::string imagefilepath = JoinPath(*basepath, *filename); |
| 2766 | std::string writeError; |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2767 | if (!fs->WriteWholeFile(&writeError, imagefilepath, data, |
| 2768 | fs->user_data)) { |
| 2769 | // Could not write image file to disc; Throw error ? |
Keiji Yoshimi | c0cfc1e | 2018-08-28 21:13:03 +0900 | [diff] [blame] | 2770 | return false; |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2771 | } |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2772 | } else { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2773 | // Throw error? |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2774 | } |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 2775 | if (uri_cb->encode) { |
| 2776 | if (!uri_cb->encode(*filename, "image", out_uri, uri_cb->user_data)) { |
| 2777 | return false; |
| 2778 | } |
| 2779 | } else { |
| 2780 | *out_uri = *filename; |
| 2781 | } |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 2782 | } |
| 2783 | |
| 2784 | return true; |
| 2785 | } |
| 2786 | #endif |
| 2787 | |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 2788 | void TinyGLTF::SetURICallbacks(URICallbacks callbacks) { |
| 2789 | assert(callbacks.decode); |
| 2790 | if (callbacks.decode) { |
| 2791 | uri_cb = callbacks; |
| 2792 | } |
| 2793 | } |
| 2794 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 2795 | void TinyGLTF::SetFsCallbacks(FsCallbacks callbacks) { fs = callbacks; } |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2796 | |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 2797 | #ifdef _WIN32 |
| 2798 | static inline std::wstring UTF8ToWchar(const std::string &str) { |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2799 | int wstr_size = |
| 2800 | MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), nullptr, 0); |
imallett | 56e1098 | 2022-10-07 10:35:16 -0700 | [diff] [blame] | 2801 | std::wstring wstr((size_t)wstr_size, 0); |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 2802 | MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), &wstr[0], |
| 2803 | (int)wstr.size()); |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 2804 | return wstr; |
| 2805 | } |
cwbhhjl | 2f5aa9f | 2020-06-04 10:40:38 +0800 | [diff] [blame] | 2806 | |
| 2807 | static inline std::string WcharToUTF8(const std::wstring &wstr) { |
| 2808 | int str_size = WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), |
imallett | 56e1098 | 2022-10-07 10:35:16 -0700 | [diff] [blame] | 2809 | nullptr, 0, nullptr, nullptr); |
| 2810 | std::string str((size_t)str_size, 0); |
cwbhhjl | 2f5aa9f | 2020-06-04 10:40:38 +0800 | [diff] [blame] | 2811 | WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), &str[0], |
imallett | 56e1098 | 2022-10-07 10:35:16 -0700 | [diff] [blame] | 2812 | (int)str.size(), nullptr, nullptr); |
cwbhhjl | 2f5aa9f | 2020-06-04 10:40:38 +0800 | [diff] [blame] | 2813 | return str; |
| 2814 | } |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 2815 | #endif |
| 2816 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2817 | #ifndef TINYGLTF_NO_FS |
| 2818 | // Default implementations of filesystem functions |
| 2819 | |
| 2820 | bool FileExists(const std::string &abs_filename, void *) { |
| 2821 | bool ret; |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 2822 | #ifdef TINYGLTF_ANDROID_LOAD_FROM_ASSETS |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 2823 | if (asset_manager) { |
| 2824 | AAsset *asset = AAssetManager_open(asset_manager, abs_filename.c_str(), |
| 2825 | AASSET_MODE_STREAMING); |
| 2826 | if (!asset) { |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 2827 | return false; |
| 2828 | } |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 2829 | AAsset_close(asset); |
| 2830 | ret = true; |
| 2831 | } else { |
| 2832 | return false; |
| 2833 | } |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 2834 | #else |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2835 | #ifdef _WIN32 |
operatios | 1668d1e | 2022-09-24 22:37:14 +0300 | [diff] [blame] | 2836 | #if defined(_MSC_VER) || defined(__GLIBCXX__) || defined(_LIBCPP_VERSION) |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2837 | |
| 2838 | // First check if a file is a directory. |
| 2839 | DWORD result = GetFileAttributesW(UTF8ToWchar(abs_filename).c_str()); |
| 2840 | if (result == INVALID_FILE_ATTRIBUTES) { |
| 2841 | return false; |
| 2842 | } |
| 2843 | if (result & FILE_ATTRIBUTE_DIRECTORY) { |
| 2844 | return false; |
| 2845 | } |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2846 | |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 2847 | FILE *fp = nullptr; |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 2848 | 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] | 2849 | if (err != 0) { |
| 2850 | return false; |
| 2851 | } |
| 2852 | #else |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2853 | // TODO: is_directory check |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 2854 | FILE *fp = nullptr; |
| 2855 | errno_t err = fopen_s(&fp, abs_filename.c_str(), "rb"); |
| 2856 | if (err != 0) { |
| 2857 | return false; |
| 2858 | } |
| 2859 | #endif |
| 2860 | |
| 2861 | #else |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2862 | struct stat sb; |
| 2863 | if (stat(abs_filename.c_str(), &sb)) { |
| 2864 | return false; |
| 2865 | } |
| 2866 | if (S_ISDIR(sb.st_mode)) { |
| 2867 | return false; |
| 2868 | } |
| 2869 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2870 | FILE *fp = fopen(abs_filename.c_str(), "rb"); |
| 2871 | #endif |
| 2872 | if (fp) { |
| 2873 | ret = true; |
| 2874 | fclose(fp); |
| 2875 | } else { |
| 2876 | ret = false; |
| 2877 | } |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 2878 | #endif |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2879 | |
| 2880 | return ret; |
| 2881 | } |
| 2882 | |
| 2883 | std::string ExpandFilePath(const std::string &filepath, void *) { |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 2884 | // https://github.com/syoyo/tinygltf/issues/368 |
| 2885 | // |
| 2886 | // No file path expansion in built-in FS function anymore, since glTF URI |
| 2887 | // should not contain tilde('~') and environment variables, and for security |
| 2888 | // reason(`wordexp`). |
| 2889 | // |
| 2890 | // Users need to supply `base_dir`(in `LoadASCIIFromString`, |
| 2891 | // `LoadBinaryFromMemory`) in expanded absolute path. |
| 2892 | |
| 2893 | return filepath; |
| 2894 | |
| 2895 | #if 0 |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2896 | #ifdef _WIN32 |
Syoyo Fujita | fbbeb4d | 2020-06-06 17:11:50 +0900 | [diff] [blame] | 2897 | // Assume input `filepath` is encoded in UTF-8 |
cwbhhjl | 2f5aa9f | 2020-06-04 10:40:38 +0800 | [diff] [blame] | 2898 | std::wstring wfilepath = UTF8ToWchar(filepath); |
Syoyo Fujita | fbbeb4d | 2020-06-06 17:11:50 +0900 | [diff] [blame] | 2899 | DWORD wlen = ExpandEnvironmentStringsW(wfilepath.c_str(), nullptr, 0); |
cwbhhjl | 2f5aa9f | 2020-06-04 10:40:38 +0800 | [diff] [blame] | 2900 | wchar_t *wstr = new wchar_t[wlen]; |
| 2901 | ExpandEnvironmentStringsW(wfilepath.c_str(), wstr, wlen); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2902 | |
cwbhhjl | 2f5aa9f | 2020-06-04 10:40:38 +0800 | [diff] [blame] | 2903 | std::wstring ws(wstr); |
| 2904 | delete[] wstr; |
| 2905 | return WcharToUTF8(ws); |
Syoyo Fujita | fbbeb4d | 2020-06-06 17:11:50 +0900 | [diff] [blame] | 2906 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2907 | #else |
| 2908 | |
| 2909 | #if defined(TARGET_OS_IPHONE) || defined(TARGET_IPHONE_SIMULATOR) || \ |
Julian Smith | 0598a20 | 2021-08-25 12:06:08 +0100 | [diff] [blame] | 2910 | defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__OpenBSD__) |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2911 | // no expansion |
| 2912 | std::string s = filepath; |
| 2913 | #else |
| 2914 | std::string s; |
| 2915 | wordexp_t p; |
| 2916 | |
| 2917 | if (filepath.empty()) { |
| 2918 | return ""; |
| 2919 | } |
| 2920 | |
Frank Galligan | aa3c5a1 | 2020-01-13 15:06:56 -0800 | [diff] [blame] | 2921 | // Quote the string to keep any spaces in filepath intact. |
| 2922 | std::string quoted_path = "\"" + filepath + "\""; |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2923 | // char** w; |
Frank Galligan | aa3c5a1 | 2020-01-13 15:06:56 -0800 | [diff] [blame] | 2924 | int ret = wordexp(quoted_path.c_str(), &p, 0); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2925 | if (ret) { |
| 2926 | // err |
| 2927 | s = filepath; |
| 2928 | return s; |
| 2929 | } |
| 2930 | |
| 2931 | // Use first element only. |
| 2932 | if (p.we_wordv) { |
| 2933 | s = std::string(p.we_wordv[0]); |
| 2934 | wordfree(&p); |
| 2935 | } else { |
| 2936 | s = filepath; |
| 2937 | } |
| 2938 | |
| 2939 | #endif |
| 2940 | |
| 2941 | return s; |
| 2942 | #endif |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 2943 | #endif |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 2944 | } |
| 2945 | |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 2946 | bool GetFileSizeInBytes(size_t *filesize_out, std::string *err, |
| 2947 | const std::string &filepath, void *userdata) { |
| 2948 | (void)userdata; |
| 2949 | |
| 2950 | #ifdef TINYGLTF_ANDROID_LOAD_FROM_ASSETS |
| 2951 | if (asset_manager) { |
| 2952 | AAsset *asset = AAssetManager_open(asset_manager, filepath.c_str(), |
| 2953 | AASSET_MODE_STREAMING); |
| 2954 | if (!asset) { |
| 2955 | if (err) { |
| 2956 | (*err) += "File open error : " + filepath + "\n"; |
| 2957 | } |
| 2958 | return false; |
| 2959 | } |
| 2960 | size_t size = AAsset_getLength(asset); |
| 2961 | |
| 2962 | if (size == 0) { |
| 2963 | if (err) { |
| 2964 | (*err) += "Invalid file size : " + filepath + |
| 2965 | " (does the path point to a directory?)"; |
| 2966 | } |
| 2967 | return false; |
| 2968 | } |
| 2969 | |
| 2970 | return true; |
| 2971 | } else { |
| 2972 | if (err) { |
| 2973 | (*err) += "No asset manager specified : " + filepath + "\n"; |
| 2974 | } |
| 2975 | return false; |
| 2976 | } |
| 2977 | #else |
| 2978 | #ifdef _WIN32 |
| 2979 | #if defined(__GLIBCXX__) // mingw |
| 2980 | int file_descriptor = |
| 2981 | _wopen(UTF8ToWchar(filepath).c_str(), _O_RDONLY | _O_BINARY); |
| 2982 | __gnu_cxx::stdio_filebuf<char> wfile_buf(file_descriptor, std::ios_base::in); |
| 2983 | std::istream f(&wfile_buf); |
| 2984 | #elif defined(_MSC_VER) || defined(_LIBCPP_VERSION) |
| 2985 | // For libcxx, assume _LIBCPP_HAS_OPEN_WITH_WCHAR is defined to accept |
| 2986 | // `wchar_t *` |
| 2987 | std::ifstream f(UTF8ToWchar(filepath).c_str(), std::ifstream::binary); |
| 2988 | #else |
| 2989 | // Unknown compiler/runtime |
| 2990 | std::ifstream f(filepath.c_str(), std::ifstream::binary); |
| 2991 | #endif |
| 2992 | #else |
| 2993 | std::ifstream f(filepath.c_str(), std::ifstream::binary); |
| 2994 | #endif |
| 2995 | if (!f) { |
| 2996 | if (err) { |
| 2997 | (*err) += "File open error : " + filepath + "\n"; |
| 2998 | } |
| 2999 | return false; |
| 3000 | } |
| 3001 | |
| 3002 | // For directory(and pipe?), peek() will fail(Posix gnustl/libc++ only) |
David Siegel | 47208b2 | 2023-06-05 22:14:30 +0200 | [diff] [blame] | 3003 | f.peek(); |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3004 | if (!f) { |
| 3005 | if (err) { |
| 3006 | (*err) += "File read error. Maybe empty file or invalid file : " + filepath + "\n"; |
| 3007 | } |
| 3008 | return false; |
| 3009 | } |
| 3010 | |
| 3011 | f.seekg(0, f.end); |
| 3012 | size_t sz = static_cast<size_t>(f.tellg()); |
| 3013 | |
| 3014 | //std::cout << "sz = " << sz << "\n"; |
| 3015 | f.seekg(0, f.beg); |
| 3016 | |
| 3017 | if (int64_t(sz) < 0) { |
| 3018 | if (err) { |
| 3019 | (*err) += "Invalid file size : " + filepath + |
| 3020 | " (does the path point to a directory?)"; |
| 3021 | } |
| 3022 | return false; |
| 3023 | } else if (sz == 0) { |
| 3024 | if (err) { |
| 3025 | (*err) += "File is empty : " + filepath + "\n"; |
| 3026 | } |
| 3027 | return false; |
| 3028 | } else if (sz >= (std::numeric_limits<std::streamoff>::max)()) { |
| 3029 | if (err) { |
| 3030 | (*err) += "Invalid file size : " + filepath + "\n"; |
| 3031 | } |
| 3032 | return false; |
| 3033 | } |
| 3034 | |
| 3035 | (*filesize_out) = sz; |
| 3036 | return true; |
| 3037 | #endif |
| 3038 | } |
| 3039 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3040 | bool ReadWholeFile(std::vector<unsigned char> *out, std::string *err, |
| 3041 | const std::string &filepath, void *) { |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 3042 | #ifdef TINYGLTF_ANDROID_LOAD_FROM_ASSETS |
| 3043 | if (asset_manager) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 3044 | AAsset *asset = AAssetManager_open(asset_manager, filepath.c_str(), |
| 3045 | AASSET_MODE_STREAMING); |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 3046 | if (!asset) { |
| 3047 | if (err) { |
| 3048 | (*err) += "File open error : " + filepath + "\n"; |
| 3049 | } |
| 3050 | return false; |
| 3051 | } |
| 3052 | size_t size = AAsset_getLength(asset); |
Syoyo Fujita | 73c4cce | 2020-04-28 01:06:34 +0900 | [diff] [blame] | 3053 | if (size == 0) { |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 3054 | if (err) { |
| 3055 | (*err) += "Invalid file size : " + filepath + |
| 3056 | " (does the path point to a directory?)"; |
| 3057 | } |
Syoyo Fujita | 73c4cce | 2020-04-28 01:06:34 +0900 | [diff] [blame] | 3058 | return false; |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 3059 | } |
| 3060 | out->resize(size); |
| 3061 | AAsset_read(asset, reinterpret_cast<char *>(&out->at(0)), size); |
| 3062 | AAsset_close(asset); |
| 3063 | return true; |
| 3064 | } else { |
| 3065 | if (err) { |
| 3066 | (*err) += "No asset manager specified : " + filepath + "\n"; |
| 3067 | } |
| 3068 | return false; |
| 3069 | } |
| 3070 | #else |
Harokyang | 5cecef2 | 2019-10-30 15:16:46 +0800 | [diff] [blame] | 3071 | #ifdef _WIN32 |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 3072 | #if defined(__GLIBCXX__) // mingw |
| 3073 | int file_descriptor = |
| 3074 | _wopen(UTF8ToWchar(filepath).c_str(), _O_RDONLY | _O_BINARY); |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 3075 | __gnu_cxx::stdio_filebuf<char> wfile_buf(file_descriptor, std::ios_base::in); |
| 3076 | std::istream f(&wfile_buf); |
Syoyo Fujita | aba57bb | 2020-05-18 20:50:45 +0900 | [diff] [blame] | 3077 | #elif defined(_MSC_VER) || defined(_LIBCPP_VERSION) |
Rahul Sheth | 96a8b2c | 2020-07-10 14:27:46 -0400 | [diff] [blame] | 3078 | // For libcxx, assume _LIBCPP_HAS_OPEN_WITH_WCHAR is defined to accept |
| 3079 | // `wchar_t *` |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 3080 | std::ifstream f(UTF8ToWchar(filepath).c_str(), std::ifstream::binary); |
Syoyo Fujita | aba57bb | 2020-05-18 20:50:45 +0900 | [diff] [blame] | 3081 | #else |
| 3082 | // Unknown compiler/runtime |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 3083 | std::ifstream f(filepath.c_str(), std::ifstream::binary); |
| 3084 | #endif |
Harokyang | 5cecef2 | 2019-10-30 15:16:46 +0800 | [diff] [blame] | 3085 | #else |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3086 | std::ifstream f(filepath.c_str(), std::ifstream::binary); |
Harokyang | 5cecef2 | 2019-10-30 15:16:46 +0800 | [diff] [blame] | 3087 | #endif |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3088 | if (!f) { |
| 3089 | if (err) { |
| 3090 | (*err) += "File open error : " + filepath + "\n"; |
| 3091 | } |
| 3092 | return false; |
| 3093 | } |
| 3094 | |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3095 | // For directory(and pipe?), peek() will fail(Posix gnustl/libc++ only) |
David Siegel | 47208b2 | 2023-06-05 22:14:30 +0200 | [diff] [blame] | 3096 | f.peek(); |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3097 | if (!f) { |
| 3098 | if (err) { |
| 3099 | (*err) += "File read error. Maybe empty file or invalid file : " + filepath + "\n"; |
| 3100 | } |
| 3101 | return false; |
| 3102 | } |
| 3103 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3104 | f.seekg(0, f.end); |
| 3105 | size_t sz = static_cast<size_t>(f.tellg()); |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3106 | |
| 3107 | //std::cout << "sz = " << sz << "\n"; |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3108 | f.seekg(0, f.beg); |
| 3109 | |
Syoyo Fujita | e886247 | 2019-10-20 17:47:50 +0900 | [diff] [blame] | 3110 | if (int64_t(sz) < 0) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3111 | if (err) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 3112 | (*err) += "Invalid file size : " + filepath + |
| 3113 | " (does the path point to a directory?)"; |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3114 | } |
| 3115 | return false; |
| 3116 | } else if (sz == 0) { |
| 3117 | if (err) { |
| 3118 | (*err) += "File is empty : " + filepath + "\n"; |
| 3119 | } |
| 3120 | return false; |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 3121 | } else if (sz >= (std::numeric_limits<std::streamoff>::max)()) { |
| 3122 | if (err) { |
| 3123 | (*err) += "Invalid file size : " + filepath + "\n"; |
| 3124 | } |
| 3125 | return false; |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3126 | } |
| 3127 | |
| 3128 | out->resize(sz); |
| 3129 | f.read(reinterpret_cast<char *>(&out->at(0)), |
| 3130 | static_cast<std::streamsize>(sz)); |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3131 | |
| 3132 | return true; |
Sascha Willems | 5f9cb24 | 2018-12-28 20:53:41 +0100 | [diff] [blame] | 3133 | #endif |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3134 | } |
| 3135 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 3136 | bool WriteWholeFile(std::string *err, const std::string &filepath, |
| 3137 | const std::vector<unsigned char> &contents, void *) { |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 3138 | #ifdef _WIN32 |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 3139 | #if defined(__GLIBCXX__) // mingw |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 3140 | int file_descriptor = _wopen(UTF8ToWchar(filepath).c_str(), |
| 3141 | _O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY); |
| 3142 | __gnu_cxx::stdio_filebuf<char> wfile_buf( |
| 3143 | file_descriptor, std::ios_base::out | std::ios_base::binary); |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 3144 | std::ostream f(&wfile_buf); |
| 3145 | #elif defined(_MSC_VER) |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 3146 | std::ofstream f(UTF8ToWchar(filepath).c_str(), std::ofstream::binary); |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 3147 | #else // clang? |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 3148 | std::ofstream f(filepath.c_str(), std::ofstream::binary); |
| 3149 | #endif |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 3150 | #else |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3151 | std::ofstream f(filepath.c_str(), std::ofstream::binary); |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 3152 | #endif |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3153 | if (!f) { |
| 3154 | if (err) { |
| 3155 | (*err) += "File open error for writing : " + filepath + "\n"; |
| 3156 | } |
| 3157 | return false; |
| 3158 | } |
| 3159 | |
| 3160 | f.write(reinterpret_cast<const char *>(&contents.at(0)), |
| 3161 | static_cast<std::streamsize>(contents.size())); |
| 3162 | if (!f) { |
| 3163 | if (err) { |
| 3164 | (*err) += "File write error: " + filepath + "\n"; |
| 3165 | } |
| 3166 | return false; |
| 3167 | } |
| 3168 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3169 | return true; |
| 3170 | } |
| 3171 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 3172 | #endif // TINYGLTF_NO_FS |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 3173 | |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3174 | static std::string MimeToExt(const std::string &mimeType) { |
| 3175 | if (mimeType == "image/jpeg") { |
| 3176 | return "jpg"; |
| 3177 | } else if (mimeType == "image/png") { |
| 3178 | return "png"; |
| 3179 | } else if (mimeType == "image/bmp") { |
| 3180 | return "bmp"; |
| 3181 | } else if (mimeType == "image/gif") { |
| 3182 | return "gif"; |
| 3183 | } |
| 3184 | |
| 3185 | return ""; |
| 3186 | } |
| 3187 | |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 3188 | static bool UpdateImageObject(const Image &image, std::string &baseDir, |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 3189 | int index, bool embedImages, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 3190 | const URICallbacks *uri_cb, |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 3191 | WriteImageDataFunction *WriteImageData, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 3192 | void *user_data, std::string *out_uri) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3193 | std::string filename; |
| 3194 | std::string ext; |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 3195 | // If image has uri, use it as a filename |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3196 | if (image.uri.size()) { |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 3197 | std::string decoded_uri; |
| 3198 | if (!uri_cb->decode(image.uri, &decoded_uri, uri_cb->user_data)) { |
| 3199 | // A decode failure results in a failure to write the gltf. |
| 3200 | return false; |
| 3201 | } |
| 3202 | filename = GetBaseFilename(decoded_uri); |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3203 | ext = GetFilePathExtension(filename); |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 3204 | } else if (image.bufferView != -1) { |
| 3205 | // If there's no URI and the data exists in a buffer, |
| 3206 | // don't change properties or write images |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3207 | } else if (image.name.size()) { |
| 3208 | ext = MimeToExt(image.mimeType); |
| 3209 | // Otherwise use name as filename |
| 3210 | filename = image.name + "." + ext; |
| 3211 | } else { |
| 3212 | ext = MimeToExt(image.mimeType); |
| 3213 | // Fallback to index of image as filename |
| 3214 | filename = std::to_string(index) + "." + ext; |
| 3215 | } |
| 3216 | |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 3217 | // If callback is set and image data exists, modify image data object. If |
| 3218 | // image data does not exist, this is not considered a failure and the |
| 3219 | // original uri should be maintained. |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 3220 | bool imageWritten = false; |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 3221 | if (*WriteImageData != nullptr && !filename.empty() && !image.image.empty()) { |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 3222 | imageWritten = (*WriteImageData)(&baseDir, &filename, &image, embedImages, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 3223 | uri_cb, out_uri, user_data); |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 3224 | if (!imageWritten) { |
| 3225 | return false; |
| 3226 | } |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 3227 | } |
| 3228 | |
| 3229 | // Use the original uri if the image was not written. |
| 3230 | if (!imageWritten) { |
| 3231 | *out_uri = image.uri; |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3232 | } |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 3233 | |
| 3234 | return true; |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3235 | } |
| 3236 | |
Selmar Kok | 0d0e97e | 2018-08-22 14:01:57 +0200 | [diff] [blame] | 3237 | bool IsDataURI(const std::string &in) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3238 | std::string header = "data:application/octet-stream;base64,"; |
| 3239 | if (in.find(header) == 0) { |
| 3240 | return true; |
| 3241 | } |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3242 | |
Omar C. F | f8a8d9c | 2018-02-10 09:09:41 +0200 | [diff] [blame] | 3243 | header = "data:image/jpeg;base64,"; |
| 3244 | if (in.find(header) == 0) { |
| 3245 | return true; |
| 3246 | } |
Squareys | 4337463 | 2018-03-13 22:20:48 +0100 | [diff] [blame] | 3247 | |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3248 | header = "data:image/png;base64,"; |
| 3249 | if (in.find(header) == 0) { |
| 3250 | return true; |
| 3251 | } |
Squareys | 4337463 | 2018-03-13 22:20:48 +0100 | [diff] [blame] | 3252 | |
Omar C. F | f8a8d9c | 2018-02-10 09:09:41 +0200 | [diff] [blame] | 3253 | header = "data:image/bmp;base64,"; |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 3254 | if (in.find(header) == 0) { |
Omar C. F | f8a8d9c | 2018-02-10 09:09:41 +0200 | [diff] [blame] | 3255 | return true; |
| 3256 | } |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3257 | |
Omar C. F | f8a8d9c | 2018-02-10 09:09:41 +0200 | [diff] [blame] | 3258 | header = "data:image/gif;base64,"; |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 3259 | if (in.find(header) == 0) { |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3260 | return true; |
| 3261 | } |
| 3262 | |
Luke San Antonio | caa24b0 | 2016-06-15 02:23:09 -0400 | [diff] [blame] | 3263 | header = "data:text/plain;base64,"; |
| 3264 | if (in.find(header) == 0) { |
| 3265 | return true; |
| 3266 | } |
| 3267 | |
Syoyo Fujita | 20244e1 | 2018-03-15 11:01:05 -0500 | [diff] [blame] | 3268 | header = "data:application/gltf-buffer;base64,"; |
| 3269 | if (in.find(header) == 0) { |
| 3270 | return true; |
| 3271 | } |
| 3272 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3273 | return false; |
| 3274 | } |
| 3275 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 3276 | bool DecodeDataURI(std::vector<unsigned char> *out, std::string &mime_type, |
| 3277 | const std::string &in, size_t reqBytes, bool checkSize) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3278 | std::string header = "data:application/octet-stream;base64,"; |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3279 | std::string data; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3280 | if (in.find(header) == 0) { |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 3281 | data = base64_decode(in.substr(header.size())); // cut mime string. |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3282 | } |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3283 | |
| 3284 | if (data.empty()) { |
| 3285 | header = "data:image/jpeg;base64,"; |
| 3286 | if (in.find(header) == 0) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3287 | mime_type = "image/jpeg"; |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 3288 | data = base64_decode(in.substr(header.size())); // cut mime string. |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3289 | } |
| 3290 | } |
| 3291 | |
| 3292 | if (data.empty()) { |
| 3293 | header = "data:image/png;base64,"; |
| 3294 | if (in.find(header) == 0) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3295 | mime_type = "image/png"; |
Syoyo Fujita | 7c87797 | 2016-03-08 01:31:49 +0900 | [diff] [blame] | 3296 | data = base64_decode(in.substr(header.size())); // cut mime string. |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3297 | } |
| 3298 | } |
Squareys | 4337463 | 2018-03-13 22:20:48 +0100 | [diff] [blame] | 3299 | |
Omar C. F | f8a8d9c | 2018-02-10 09:09:41 +0200 | [diff] [blame] | 3300 | if (data.empty()) { |
| 3301 | header = "data:image/bmp;base64,"; |
| 3302 | if (in.find(header) == 0) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3303 | mime_type = "image/bmp"; |
Omar C. F | f8a8d9c | 2018-02-10 09:09:41 +0200 | [diff] [blame] | 3304 | data = base64_decode(in.substr(header.size())); // cut mime string. |
| 3305 | } |
| 3306 | } |
| 3307 | |
| 3308 | if (data.empty()) { |
| 3309 | header = "data:image/gif;base64,"; |
| 3310 | if (in.find(header) == 0) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3311 | mime_type = "image/gif"; |
Omar C. F | f8a8d9c | 2018-02-10 09:09:41 +0200 | [diff] [blame] | 3312 | data = base64_decode(in.substr(header.size())); // cut mime string. |
| 3313 | } |
| 3314 | } |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3315 | |
| 3316 | if (data.empty()) { |
Luke San Antonio | caa24b0 | 2016-06-15 02:23:09 -0400 | [diff] [blame] | 3317 | header = "data:text/plain;base64,"; |
| 3318 | if (in.find(header) == 0) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 3319 | mime_type = "text/plain"; |
Luke San Antonio | caa24b0 | 2016-06-15 02:23:09 -0400 | [diff] [blame] | 3320 | data = base64_decode(in.substr(header.size())); |
| 3321 | } |
| 3322 | } |
| 3323 | |
| 3324 | if (data.empty()) { |
Syoyo Fujita | 20244e1 | 2018-03-15 11:01:05 -0500 | [diff] [blame] | 3325 | header = "data:application/gltf-buffer;base64,"; |
| 3326 | if (in.find(header) == 0) { |
| 3327 | data = base64_decode(in.substr(header.size())); |
| 3328 | } |
| 3329 | } |
| 3330 | |
Syoyo Fujita | a11f6e1 | 2019-12-19 14:16:48 +0900 | [diff] [blame] | 3331 | // TODO(syoyo): Allow empty buffer? #229 |
Syoyo Fujita | 20244e1 | 2018-03-15 11:01:05 -0500 | [diff] [blame] | 3332 | if (data.empty()) { |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3333 | return false; |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 3334 | } |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3335 | |
| 3336 | if (checkSize) { |
| 3337 | if (data.size() != reqBytes) { |
| 3338 | return false; |
| 3339 | } |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3340 | out->resize(reqBytes); |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3341 | } else { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3342 | out->resize(data.size()); |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3343 | } |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3344 | std::copy(data.begin(), data.end(), out->begin()); |
Syoyo Fujita | 620eed1 | 2016-01-02 23:37:12 +0900 | [diff] [blame] | 3345 | return true; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3346 | } |
| 3347 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3348 | namespace detail { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3349 | bool GetInt(const detail::json &o, int &val) { |
jrkoonce | 5cecc41 | 2019-08-29 11:45:04 -0500 | [diff] [blame] | 3350 | #ifdef TINYGLTF_USE_RAPIDJSON |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3351 | if (!o.IsDouble()) { |
| 3352 | if (o.IsInt()) { |
| 3353 | val = o.GetInt(); |
| 3354 | return true; |
| 3355 | } else if (o.IsUint()) { |
| 3356 | val = static_cast<int>(o.GetUint()); |
| 3357 | return true; |
| 3358 | } else if (o.IsInt64()) { |
| 3359 | val = static_cast<int>(o.GetInt64()); |
| 3360 | return true; |
| 3361 | } else if (o.IsUint64()) { |
| 3362 | val = static_cast<int>(o.GetUint64()); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3363 | return true; |
| 3364 | } |
jrkoonce | 5cecc41 | 2019-08-29 11:45:04 -0500 | [diff] [blame] | 3365 | } |
| 3366 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3367 | return false; |
jrkoonce | 5cecc41 | 2019-08-29 11:45:04 -0500 | [diff] [blame] | 3368 | #else |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3369 | auto type = o.type(); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3370 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3371 | if ((type == detail::json::value_t::number_integer) || |
| 3372 | (type == detail::json::value_t::number_unsigned)) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3373 | val = static_cast<int>(o.get<int64_t>()); |
| 3374 | return true; |
jrkoonce | 5cecc41 | 2019-08-29 11:45:04 -0500 | [diff] [blame] | 3375 | } |
| 3376 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3377 | return false; |
jrkoonce | 5cecc41 | 2019-08-29 11:45:04 -0500 | [diff] [blame] | 3378 | #endif |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3379 | } |
| 3380 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3381 | #ifdef TINYGLTF_USE_RAPIDJSON |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3382 | bool GetDouble(const detail::json &o, double &val) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3383 | if (o.IsDouble()) { |
| 3384 | val = o.GetDouble(); |
| 3385 | return true; |
| 3386 | } |
| 3387 | |
| 3388 | return false; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3389 | } |
Syoyo Fujita | 1100f0f | 2019-10-30 15:16:34 +0900 | [diff] [blame] | 3390 | #endif |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3391 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3392 | bool GetNumber(const detail::json &o, double &val) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3393 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3394 | if (o.IsNumber()) { |
| 3395 | val = o.GetDouble(); |
| 3396 | return true; |
| 3397 | } |
| 3398 | |
| 3399 | return false; |
| 3400 | #else |
| 3401 | if (o.is_number()) { |
| 3402 | val = o.get<double>(); |
| 3403 | return true; |
| 3404 | } |
| 3405 | |
| 3406 | return false; |
| 3407 | #endif |
| 3408 | } |
| 3409 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3410 | bool GetString(const detail::json &o, std::string &val) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3411 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3412 | if (o.IsString()) { |
| 3413 | val = o.GetString(); |
| 3414 | return true; |
| 3415 | } |
| 3416 | |
| 3417 | return false; |
| 3418 | #else |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3419 | if (o.type() == detail::json::value_t::string) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3420 | val = o.get<std::string>(); |
| 3421 | return true; |
| 3422 | } |
| 3423 | |
| 3424 | return false; |
| 3425 | #endif |
| 3426 | } |
| 3427 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3428 | bool IsArray(const detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3429 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3430 | return o.IsArray(); |
| 3431 | #else |
| 3432 | return o.is_array(); |
| 3433 | #endif |
| 3434 | } |
| 3435 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3436 | detail::json_const_array_iterator ArrayBegin(const detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3437 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3438 | return o.Begin(); |
| 3439 | #else |
| 3440 | return o.begin(); |
| 3441 | #endif |
| 3442 | } |
| 3443 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3444 | detail::json_const_array_iterator ArrayEnd(const detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3445 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3446 | return o.End(); |
| 3447 | #else |
| 3448 | return o.end(); |
| 3449 | #endif |
| 3450 | } |
| 3451 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3452 | bool IsObject(const detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3453 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3454 | return o.IsObject(); |
| 3455 | #else |
| 3456 | return o.is_object(); |
| 3457 | #endif |
| 3458 | } |
| 3459 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3460 | detail::json_const_iterator ObjectBegin(const detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3461 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3462 | return o.MemberBegin(); |
| 3463 | #else |
| 3464 | return o.begin(); |
| 3465 | #endif |
| 3466 | } |
| 3467 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3468 | detail::json_const_iterator ObjectEnd(const detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3469 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3470 | return o.MemberEnd(); |
| 3471 | #else |
| 3472 | return o.end(); |
| 3473 | #endif |
| 3474 | } |
| 3475 | |
Rahul Sheth | 01d5438 | 2020-07-10 14:27:37 -0400 | [diff] [blame] | 3476 | // Making this a const char* results in a pointer to a temporary when |
| 3477 | // TINYGLTF_USE_RAPIDJSON is off. |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3478 | std::string GetKey(detail::json_const_iterator &it) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3479 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3480 | return it->name.GetString(); |
| 3481 | #else |
| 3482 | return it.key().c_str(); |
| 3483 | #endif |
| 3484 | } |
| 3485 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3486 | bool FindMember(const detail::json &o, const char *member, detail::json_const_iterator &it) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3487 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3488 | if (!o.IsObject()) { |
| 3489 | return false; |
| 3490 | } |
| 3491 | it = o.FindMember(member); |
| 3492 | return it != o.MemberEnd(); |
| 3493 | #else |
| 3494 | it = o.find(member); |
| 3495 | return it != o.end(); |
| 3496 | #endif |
| 3497 | } |
| 3498 | |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 3499 | bool FindMember(detail::json &o, const char *member, detail::json_iterator &it) { |
| 3500 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3501 | if (!o.IsObject()) { |
| 3502 | return false; |
| 3503 | } |
| 3504 | it = o.FindMember(member); |
| 3505 | return it != o.MemberEnd(); |
| 3506 | #else |
| 3507 | it = o.find(member); |
| 3508 | return it != o.end(); |
| 3509 | #endif |
| 3510 | } |
| 3511 | |
| 3512 | void Erase(detail::json & o, detail::json_iterator &it) { |
| 3513 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3514 | o.EraseMember(it); |
| 3515 | #else |
| 3516 | o.erase(it); |
| 3517 | #endif |
| 3518 | } |
| 3519 | |
| 3520 | bool IsEmpty(const detail::json & o) { |
| 3521 | #ifdef TINYGLTF_USE_RAPIDJSON |
David Siegel | a1a34cb | 2023-06-07 15:30:02 +0200 | [diff] [blame] | 3522 | return o.ObjectEmpty(); |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 3523 | #else |
| 3524 | return o.empty(); |
| 3525 | #endif |
| 3526 | } |
| 3527 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3528 | const detail::json &GetValue(detail::json_const_iterator &it) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3529 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3530 | return it->value; |
| 3531 | #else |
| 3532 | return it.value(); |
| 3533 | #endif |
| 3534 | } |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 3535 | |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 3536 | detail::json &GetValue(detail::json_iterator &it) { |
| 3537 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3538 | return it->value; |
| 3539 | #else |
| 3540 | return it.value(); |
| 3541 | #endif |
| 3542 | } |
| 3543 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3544 | std::string JsonToString(const detail::json &o, int spacing = -1) { |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 3545 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3546 | using namespace rapidjson; |
| 3547 | StringBuffer buffer; |
| 3548 | if (spacing == -1) { |
| 3549 | Writer<StringBuffer> writer(buffer); |
Syoyo Fujita | 544969b | 2022-07-13 19:03:33 +0900 | [diff] [blame] | 3550 | // TODO: Better error handling. |
| 3551 | // https://github.com/syoyo/tinygltf/issues/332 |
| 3552 | if (!o.Accept(writer)) { |
| 3553 | return "tiny_gltf::JsonToString() failed rapidjson conversion"; |
| 3554 | } |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 3555 | } else { |
| 3556 | PrettyWriter<StringBuffer> writer(buffer); |
| 3557 | writer.SetIndent(' ', uint32_t(spacing)); |
Syoyo Fujita | 544969b | 2022-07-13 19:03:33 +0900 | [diff] [blame] | 3558 | if (!o.Accept(writer)) { |
| 3559 | return "tiny_gltf::JsonToString() failed rapidjson conversion"; |
| 3560 | } |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 3561 | } |
| 3562 | return buffer.GetString(); |
| 3563 | #else |
| 3564 | return o.dump(spacing); |
| 3565 | #endif |
| 3566 | } |
| 3567 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3568 | } // namespace |
| 3569 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3570 | static bool ParseJsonAsValue(Value *ret, const detail::json &o) { |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3571 | Value val{}; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3572 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3573 | using rapidjson::Type; |
| 3574 | switch (o.GetType()) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3575 | case Type::kObjectType: { |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3576 | Value::Object value_object; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3577 | for (auto it = o.MemberBegin(); it != o.MemberEnd(); ++it) { |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3578 | Value entry; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3579 | ParseJsonAsValue(&entry, it->value); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3580 | if (entry.Type() != NULL_TYPE) |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3581 | value_object.emplace(detail::GetKey(it), std::move(entry)); |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3582 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3583 | if (value_object.size() > 0) val = Value(std::move(value_object)); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 3584 | } break; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3585 | case Type::kArrayType: { |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3586 | Value::Array value_array; |
jrkoonce | 5cecc41 | 2019-08-29 11:45:04 -0500 | [diff] [blame] | 3587 | value_array.reserve(o.Size()); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3588 | for (auto it = o.Begin(); it != o.End(); ++it) { |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3589 | Value entry; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3590 | ParseJsonAsValue(&entry, *it); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3591 | if (entry.Type() != NULL_TYPE) |
| 3592 | value_array.emplace_back(std::move(entry)); |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3593 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3594 | if (value_array.size() > 0) val = Value(std::move(value_array)); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 3595 | } break; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3596 | case Type::kStringType: |
| 3597 | val = Value(std::string(o.GetString())); |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3598 | break; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3599 | case Type::kFalseType: |
| 3600 | case Type::kTrueType: |
| 3601 | val = Value(o.GetBool()); |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3602 | break; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3603 | case Type::kNumberType: |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3604 | if (!o.IsDouble()) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3605 | int i = 0; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3606 | detail::GetInt(o, i); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3607 | val = Value(i); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3608 | } else { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3609 | double d = 0.0; |
David | 3831802 | 2023-02-15 23:21:09 -0600 | [diff] [blame] | 3610 | detail::GetDouble(o, d); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3611 | val = Value(d); |
| 3612 | } |
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::kNullType: |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 3615 | break; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 3616 | // all types are covered, so no `case default` |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 3617 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3618 | #else |
| 3619 | switch (o.type()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3620 | case detail::json::value_t::object: { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3621 | Value::Object value_object; |
| 3622 | for (auto it = o.begin(); it != o.end(); it++) { |
| 3623 | Value entry; |
| 3624 | ParseJsonAsValue(&entry, it.value()); |
| 3625 | if (entry.Type() != NULL_TYPE) |
| 3626 | value_object.emplace(it.key(), std::move(entry)); |
| 3627 | } |
| 3628 | if (value_object.size() > 0) val = Value(std::move(value_object)); |
| 3629 | } break; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3630 | case detail::json::value_t::array: { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3631 | Value::Array value_array; |
| 3632 | value_array.reserve(o.size()); |
| 3633 | for (auto it = o.begin(); it != o.end(); it++) { |
| 3634 | Value entry; |
| 3635 | ParseJsonAsValue(&entry, it.value()); |
| 3636 | if (entry.Type() != NULL_TYPE) |
| 3637 | value_array.emplace_back(std::move(entry)); |
| 3638 | } |
| 3639 | if (value_array.size() > 0) val = Value(std::move(value_array)); |
| 3640 | } break; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3641 | case detail::json::value_t::string: |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3642 | val = Value(o.get<std::string>()); |
| 3643 | break; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3644 | case detail::json::value_t::boolean: |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3645 | val = Value(o.get<bool>()); |
| 3646 | break; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3647 | case detail::json::value_t::number_integer: |
| 3648 | case detail::json::value_t::number_unsigned: |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3649 | val = Value(static_cast<int>(o.get<int64_t>())); |
| 3650 | break; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3651 | case detail::json::value_t::number_float: |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3652 | val = Value(o.get<double>()); |
| 3653 | break; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3654 | case detail::json::value_t::null: |
| 3655 | case detail::json::value_t::discarded: |
| 3656 | case detail::json::value_t::binary: |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3657 | // default: |
| 3658 | break; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3659 | } |
| 3660 | #endif |
Marcin Kacprzak | f4f5c3c | 2022-09-02 16:15:54 +0200 | [diff] [blame] | 3661 | const bool isNotNull = val.Type() != NULL_TYPE; |
| 3662 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3663 | if (ret) *ret = std::move(val); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 3664 | |
Marcin Kacprzak | f4f5c3c | 2022-09-02 16:15:54 +0200 | [diff] [blame] | 3665 | return isNotNull; |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 3666 | } |
| 3667 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3668 | static bool ParseExtrasProperty(Value *ret, const detail::json &o) { |
| 3669 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3670 | if (!detail::FindMember(o, "extras", it)) { |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 3671 | return false; |
| 3672 | } |
| 3673 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3674 | return ParseJsonAsValue(ret, detail::GetValue(it)); |
Syoyo Fujita | f696c6f | 2016-10-17 03:48:55 +0900 | [diff] [blame] | 3675 | } |
| 3676 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3677 | static bool ParseBooleanProperty(bool *ret, std::string *err, const detail::json &o, |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 3678 | const std::string &property, |
| 3679 | const bool required, |
| 3680 | const std::string &parent_node = "") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3681 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3682 | if (!detail::FindMember(o, property.c_str(), it)) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3683 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3684 | if (err) { |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 3685 | (*err) += "'" + property + "' property is missing"; |
| 3686 | if (!parent_node.empty()) { |
| 3687 | (*err) += " in " + parent_node; |
| 3688 | } |
| 3689 | (*err) += ".\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3690 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3691 | } |
| 3692 | return false; |
| 3693 | } |
| 3694 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3695 | auto &value = detail::GetValue(it); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3696 | |
| 3697 | bool isBoolean; |
jrkoonce | 0d2b6ef | 2019-09-04 13:46:45 -0500 | [diff] [blame] | 3698 | bool boolValue = false; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3699 | #ifdef TINYGLTF_USE_RAPIDJSON |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3700 | isBoolean = value.IsBool(); |
| 3701 | if (isBoolean) { |
| 3702 | boolValue = value.GetBool(); |
| 3703 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3704 | #else |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3705 | isBoolean = value.is_boolean(); |
| 3706 | if (isBoolean) { |
| 3707 | boolValue = value.get<bool>(); |
| 3708 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3709 | #endif |
| 3710 | if (!isBoolean) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3711 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3712 | if (err) { |
| 3713 | (*err) += "'" + property + "' property is not a bool type.\n"; |
| 3714 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3715 | } |
| 3716 | return false; |
| 3717 | } |
| 3718 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3719 | if (ret) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3720 | (*ret) = boolValue; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3721 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3722 | |
| 3723 | return true; |
| 3724 | } |
| 3725 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3726 | static bool ParseIntegerProperty(int *ret, std::string *err, const detail::json &o, |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3727 | const std::string &property, |
| 3728 | const bool required, |
| 3729 | const std::string &parent_node = "") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3730 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3731 | if (!detail::FindMember(o, property.c_str(), it)) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3732 | if (required) { |
| 3733 | if (err) { |
| 3734 | (*err) += "'" + property + "' property is missing"; |
| 3735 | if (!parent_node.empty()) { |
| 3736 | (*err) += " in " + parent_node; |
| 3737 | } |
| 3738 | (*err) += ".\n"; |
| 3739 | } |
| 3740 | } |
| 3741 | return false; |
| 3742 | } |
| 3743 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3744 | int intValue; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3745 | bool isInt = detail::GetInt(detail::GetValue(it), intValue); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3746 | if (!isInt) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3747 | if (required) { |
| 3748 | if (err) { |
| 3749 | (*err) += "'" + property + "' property is not an integer type.\n"; |
| 3750 | } |
| 3751 | } |
| 3752 | return false; |
| 3753 | } |
| 3754 | |
| 3755 | if (ret) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3756 | (*ret) = intValue; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3757 | } |
| 3758 | |
| 3759 | return true; |
| 3760 | } |
| 3761 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3762 | static bool ParseUnsignedProperty(size_t *ret, std::string *err, const detail::json &o, |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3763 | const std::string &property, |
| 3764 | const bool required, |
| 3765 | const std::string &parent_node = "") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3766 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3767 | if (!detail::FindMember(o, property.c_str(), it)) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3768 | if (required) { |
| 3769 | if (err) { |
| 3770 | (*err) += "'" + property + "' property is missing"; |
| 3771 | if (!parent_node.empty()) { |
| 3772 | (*err) += " in " + parent_node; |
| 3773 | } |
| 3774 | (*err) += ".\n"; |
| 3775 | } |
| 3776 | } |
| 3777 | return false; |
| 3778 | } |
| 3779 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3780 | auto &value = detail::GetValue(it); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3781 | |
jrkoonce | 0d2b6ef | 2019-09-04 13:46:45 -0500 | [diff] [blame] | 3782 | size_t uValue = 0; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3783 | bool isUValue; |
| 3784 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 3785 | isUValue = false; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3786 | if (value.IsUint()) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3787 | uValue = value.GetUint(); |
| 3788 | isUValue = true; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3789 | } else if (value.IsUint64()) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3790 | uValue = value.GetUint64(); |
| 3791 | isUValue = true; |
| 3792 | } |
| 3793 | #else |
| 3794 | isUValue = value.is_number_unsigned(); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 3795 | if (isUValue) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3796 | uValue = value.get<size_t>(); |
| 3797 | } |
| 3798 | #endif |
| 3799 | if (!isUValue) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3800 | if (required) { |
| 3801 | if (err) { |
| 3802 | (*err) += "'" + property + "' property is not a positive integer.\n"; |
| 3803 | } |
| 3804 | } |
| 3805 | return false; |
| 3806 | } |
| 3807 | |
| 3808 | if (ret) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3809 | (*ret) = uValue; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3810 | } |
| 3811 | |
| 3812 | return true; |
| 3813 | } |
| 3814 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3815 | static bool ParseNumberProperty(double *ret, std::string *err, const detail::json &o, |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 3816 | const std::string &property, |
| 3817 | const bool required, |
| 3818 | const std::string &parent_node = "") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3819 | detail::json_const_iterator it; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3820 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3821 | if (!detail::FindMember(o, property.c_str(), it)) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3822 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3823 | if (err) { |
Syoyo Fujita | 90e1ed2 | 2017-05-30 00:47:37 +0900 | [diff] [blame] | 3824 | (*err) += "'" + property + "' property is missing"; |
| 3825 | if (!parent_node.empty()) { |
| 3826 | (*err) += " in " + parent_node; |
| 3827 | } |
| 3828 | (*err) += ".\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3829 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3830 | } |
| 3831 | return false; |
| 3832 | } |
| 3833 | |
Syoyo Fujita | 1100f0f | 2019-10-30 15:16:34 +0900 | [diff] [blame] | 3834 | double numberValue; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3835 | bool isNumber = detail::GetNumber(detail::GetValue(it), numberValue); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3836 | |
Syoyo Fujita | 1100f0f | 2019-10-30 15:16:34 +0900 | [diff] [blame] | 3837 | if (!isNumber) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3838 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3839 | if (err) { |
| 3840 | (*err) += "'" + property + "' property is not a number type.\n"; |
| 3841 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3842 | } |
| 3843 | return false; |
| 3844 | } |
| 3845 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3846 | if (ret) { |
Syoyo Fujita | 1100f0f | 2019-10-30 15:16:34 +0900 | [diff] [blame] | 3847 | (*ret) = numberValue; |
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 true; |
| 3851 | } |
| 3852 | |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 3853 | static bool ParseNumberArrayProperty(std::vector<double> *ret, std::string *err, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3854 | const detail::json &o, const std::string &property, |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 3855 | bool required, |
Syoyo Fujita | 90e1ed2 | 2017-05-30 00:47:37 +0900 | [diff] [blame] | 3856 | const std::string &parent_node = "") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3857 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3858 | if (!detail::FindMember(o, property.c_str(), it)) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3859 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3860 | if (err) { |
Syoyo Fujita | 90e1ed2 | 2017-05-30 00:47:37 +0900 | [diff] [blame] | 3861 | (*err) += "'" + property + "' property is missing"; |
| 3862 | if (!parent_node.empty()) { |
| 3863 | (*err) += " in " + parent_node; |
| 3864 | } |
| 3865 | (*err) += ".\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3866 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3867 | } |
| 3868 | return false; |
| 3869 | } |
| 3870 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3871 | if (!detail::IsArray(detail::GetValue(it))) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3872 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3873 | if (err) { |
Syoyo Fujita | 90e1ed2 | 2017-05-30 00:47:37 +0900 | [diff] [blame] | 3874 | (*err) += "'" + property + "' property is not an array"; |
| 3875 | if (!parent_node.empty()) { |
| 3876 | (*err) += " in " + parent_node; |
| 3877 | } |
| 3878 | (*err) += ".\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3879 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3880 | } |
| 3881 | return false; |
| 3882 | } |
| 3883 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3884 | ret->clear(); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3885 | auto end = detail::ArrayEnd(detail::GetValue(it)); |
| 3886 | for (auto i = detail::ArrayBegin(detail::GetValue(it)); i != end; ++i) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3887 | double numberValue; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3888 | const bool isNumber = detail::GetNumber(*i, numberValue); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3889 | if (!isNumber) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3890 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3891 | if (err) { |
| 3892 | (*err) += "'" + property + "' property is not a number.\n"; |
Syoyo Fujita | 90e1ed2 | 2017-05-30 00:47:37 +0900 | [diff] [blame] | 3893 | if (!parent_node.empty()) { |
| 3894 | (*err) += " in " + parent_node; |
| 3895 | } |
| 3896 | (*err) += ".\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3897 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3898 | } |
| 3899 | return false; |
| 3900 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3901 | ret->push_back(numberValue); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3902 | } |
| 3903 | |
| 3904 | return true; |
| 3905 | } |
| 3906 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3907 | static bool ParseIntegerArrayProperty(std::vector<int> *ret, std::string *err, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3908 | const detail::json &o, |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3909 | const std::string &property, |
| 3910 | bool required, |
| 3911 | const std::string &parent_node = "") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3912 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3913 | if (!detail::FindMember(o, property.c_str(), it)) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3914 | if (required) { |
| 3915 | if (err) { |
| 3916 | (*err) += "'" + property + "' property is missing"; |
| 3917 | if (!parent_node.empty()) { |
| 3918 | (*err) += " in " + parent_node; |
| 3919 | } |
| 3920 | (*err) += ".\n"; |
| 3921 | } |
| 3922 | } |
| 3923 | return false; |
| 3924 | } |
| 3925 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3926 | if (!detail::IsArray(detail::GetValue(it))) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3927 | if (required) { |
| 3928 | if (err) { |
| 3929 | (*err) += "'" + property + "' property is not an array"; |
| 3930 | if (!parent_node.empty()) { |
| 3931 | (*err) += " in " + parent_node; |
| 3932 | } |
| 3933 | (*err) += ".\n"; |
| 3934 | } |
| 3935 | } |
| 3936 | return false; |
| 3937 | } |
| 3938 | |
| 3939 | ret->clear(); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3940 | auto end = detail::ArrayEnd(detail::GetValue(it)); |
| 3941 | for (auto i = detail::ArrayBegin(detail::GetValue(it)); i != end; ++i) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3942 | int numberValue; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3943 | bool isNumber = detail::GetInt(*i, numberValue); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3944 | if (!isNumber) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3945 | if (required) { |
| 3946 | if (err) { |
| 3947 | (*err) += "'" + property + "' property is not an integer type.\n"; |
| 3948 | if (!parent_node.empty()) { |
| 3949 | (*err) += " in " + parent_node; |
| 3950 | } |
| 3951 | (*err) += ".\n"; |
| 3952 | } |
| 3953 | } |
| 3954 | return false; |
| 3955 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3956 | ret->push_back(numberValue); |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3957 | } |
| 3958 | |
| 3959 | return true; |
| 3960 | } |
| 3961 | |
Syoyo Fujita | 0614eb8 | 2016-10-14 18:50:14 +0900 | [diff] [blame] | 3962 | static bool ParseStringProperty( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3963 | std::string *ret, std::string *err, const detail::json &o, |
Syoyo Fujita | 0614eb8 | 2016-10-14 18:50:14 +0900 | [diff] [blame] | 3964 | const std::string &property, bool required, |
| 3965 | const std::string &parent_node = std::string()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3966 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3967 | if (!detail::FindMember(o, property.c_str(), it)) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3968 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3969 | if (err) { |
Syoyo Fujita | 0614eb8 | 2016-10-14 18:50:14 +0900 | [diff] [blame] | 3970 | (*err) += "'" + property + "' property is missing"; |
| 3971 | if (parent_node.empty()) { |
| 3972 | (*err) += ".\n"; |
| 3973 | } else { |
| 3974 | (*err) += " in `" + parent_node + "'.\n"; |
| 3975 | } |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3976 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3977 | } |
| 3978 | return false; |
| 3979 | } |
| 3980 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3981 | std::string strValue; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 3982 | if (!detail::GetString(detail::GetValue(it), strValue)) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3983 | if (required) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3984 | if (err) { |
| 3985 | (*err) += "'" + property + "' property is not a string type.\n"; |
| 3986 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3987 | } |
| 3988 | return false; |
| 3989 | } |
| 3990 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3991 | if (ret) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 3992 | (*ret) = std::move(strValue); |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 3993 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 3994 | |
| 3995 | return true; |
| 3996 | } |
| 3997 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 3998 | static bool ParseStringIntegerProperty(std::map<std::string, int> *ret, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 3999 | std::string *err, const detail::json &o, |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4000 | const std::string &property, |
| 4001 | bool required, |
| 4002 | const std::string &parent = "") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4003 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4004 | if (!detail::FindMember(o, property.c_str(), it)) { |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4005 | if (required) { |
| 4006 | if (err) { |
Syoyo Fujita | 57c1018 | 2017-10-19 18:48:26 +0900 | [diff] [blame] | 4007 | if (!parent.empty()) { |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 4008 | (*err) += |
| 4009 | "'" + property + "' property is missing in " + parent + ".\n"; |
Syoyo Fujita | 57c1018 | 2017-10-19 18:48:26 +0900 | [diff] [blame] | 4010 | } else { |
| 4011 | (*err) += "'" + property + "' property is missing.\n"; |
| 4012 | } |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4013 | } |
| 4014 | } |
| 4015 | return false; |
| 4016 | } |
| 4017 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4018 | const detail::json &dict = detail::GetValue(it); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4019 | |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4020 | // Make sure we are dealing with an object / dictionary. |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4021 | if (!detail::IsObject(dict)) { |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4022 | if (required) { |
| 4023 | if (err) { |
| 4024 | (*err) += "'" + property + "' property is not an object.\n"; |
| 4025 | } |
| 4026 | } |
| 4027 | return false; |
| 4028 | } |
| 4029 | |
| 4030 | ret->clear(); |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4031 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4032 | detail::json_const_iterator dictIt(detail::ObjectBegin(dict)); |
| 4033 | detail::json_const_iterator dictItEnd(detail::ObjectEnd(dict)); |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4034 | |
Syoyo Fujita | 3cdfb3b | 2016-06-15 20:58:42 +0900 | [diff] [blame] | 4035 | for (; dictIt != dictItEnd; ++dictIt) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4036 | int intVal; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4037 | if (!detail::GetInt(detail::GetValue(dictIt), intVal)) { |
Syoyo Fujita | 3cdfb3b | 2016-06-15 20:58:42 +0900 | [diff] [blame] | 4038 | if (required) { |
| 4039 | if (err) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4040 | (*err) += "'" + property + "' value is not an integer type.\n"; |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4041 | } |
| 4042 | } |
| 4043 | return false; |
| 4044 | } |
| 4045 | |
| 4046 | // Insert into the list. |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4047 | (*ret)[detail::GetKey(dictIt)] = intVal; |
Luke San Antonio | 19894c7 | 2016-06-14 21:19:51 -0400 | [diff] [blame] | 4048 | } |
| 4049 | return true; |
| 4050 | } |
| 4051 | |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 4052 | static bool ParseJSONProperty(std::map<std::string, double> *ret, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4053 | std::string *err, const detail::json &o, |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 4054 | const std::string &property, bool required) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4055 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4056 | if (!detail::FindMember(o, property.c_str(), it)) { |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 4057 | if (required) { |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 4058 | if (err) { |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 4059 | (*err) += "'" + property + "' property is missing. \n'"; |
| 4060 | } |
| 4061 | } |
| 4062 | return false; |
| 4063 | } |
| 4064 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4065 | const detail::json &obj = detail::GetValue(it); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4066 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4067 | if (!detail::IsObject(obj)) { |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 4068 | if (required) { |
| 4069 | if (err) { |
| 4070 | (*err) += "'" + property + "' property is not a JSON object.\n"; |
| 4071 | } |
| 4072 | } |
| 4073 | return false; |
| 4074 | } |
| 4075 | |
| 4076 | ret->clear(); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 4077 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4078 | detail::json_const_iterator it2(detail::ObjectBegin(obj)); |
| 4079 | detail::json_const_iterator itEnd(detail::ObjectEnd(obj)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4080 | for (; it2 != itEnd; ++it2) { |
| 4081 | double numVal; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4082 | if (detail::GetNumber(detail::GetValue(it2), numVal)) |
| 4083 | ret->emplace(std::string(detail::GetKey(it2)), numVal); |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | return true; |
| 4087 | } |
| 4088 | |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 4089 | static bool ParseParameterProperty(Parameter *param, std::string *err, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4090 | const detail::json &o, const std::string &prop, |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 4091 | bool required) { |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 4092 | // A parameter value can either be a string or an array of either a boolean or |
| 4093 | // a number. Booleans of any kind aren't supported here. Granted, it |
| 4094 | // complicates the Parameter structure and breaks it semantically in the sense |
| 4095 | // that the client probably works off the assumption that if the string is |
| 4096 | // empty the vector is used, etc. Would a tagged union work? |
| 4097 | if (ParseStringProperty(¶m->string_value, err, o, prop, false)) { |
| 4098 | // Found string property. |
| 4099 | return true; |
| 4100 | } else if (ParseNumberArrayProperty(¶m->number_array, err, o, prop, |
| 4101 | false)) { |
| 4102 | // Found a number array. |
| 4103 | return true; |
Ben Buzbee | f6af224 | 2018-04-25 15:13:05 -0700 | [diff] [blame] | 4104 | } else if (ParseNumberProperty(¶m->number_value, err, o, prop, false)) { |
Marcin Kacprzak | b12a54e | 2022-09-02 16:13:11 +0200 | [diff] [blame] | 4105 | param->has_number_value = true; |
| 4106 | return true; |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 4107 | } else if (ParseJSONProperty(¶m->json_double_value, err, o, prop, |
| 4108 | false)) { |
| 4109 | return true; |
| 4110 | } else if (ParseBooleanProperty(¶m->bool_value, err, o, prop, false)) { |
| 4111 | return true; |
| 4112 | } else { |
| 4113 | if (required) { |
| 4114 | if (err) { |
| 4115 | (*err) += "parameter must be a string or number / number array.\n"; |
| 4116 | } |
| 4117 | } |
| 4118 | return false; |
| 4119 | } |
| 4120 | } |
| 4121 | |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 4122 | static bool ParseExtensionsProperty(ExtensionMap *ret, std::string *err, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4123 | const detail::json &o) { |
Syoyo Fujita | 48f6db0 | 2018-04-15 18:40:55 +0900 | [diff] [blame] | 4124 | (void)err; |
| 4125 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4126 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4127 | if (!detail::FindMember(o, "extensions", it)) { |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 4128 | return false; |
| 4129 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4130 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4131 | auto &obj = detail::GetValue(it); |
| 4132 | if (!detail::IsObject(obj)) { |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 4133 | return false; |
| 4134 | } |
| 4135 | ExtensionMap extensions; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4136 | detail::json_const_iterator extIt = detail::ObjectBegin(obj); // it.value().begin(); |
| 4137 | detail::json_const_iterator extEnd = detail::ObjectEnd(obj); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4138 | for (; extIt != extEnd; ++extIt) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4139 | auto &itObj = detail::GetValue(extIt); |
| 4140 | if (!detail::IsObject(itObj)) continue; |
| 4141 | std::string key(detail::GetKey(extIt)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4142 | if (!ParseJsonAsValue(&extensions[key], itObj)) { |
jrkoonce | 06c30c4 | 2019-09-03 15:56:48 -0500 | [diff] [blame] | 4143 | if (!key.empty()) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4144 | // create empty object so that an extension object is still of type |
| 4145 | // object |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4146 | extensions[key] = Value{Value::Object{}}; |
Selmar Kok | ee3d066 | 2018-10-08 16:20:43 +0200 | [diff] [blame] | 4147 | } |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4148 | } |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 4149 | } |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 4150 | if (ret) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4151 | (*ret) = std::move(extensions); |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 4152 | } |
| 4153 | return true; |
| 4154 | } |
| 4155 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 4156 | template <typename GltfType> |
| 4157 | static bool ParseExtrasAndExtensions(GltfType * target, std::string *err, |
| 4158 | const detail::json & o, bool store_json_strings) { |
Syoyo Fujita | 7a570c8 | 2023-06-19 21:52:13 +0900 | [diff] [blame] | 4159 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 4160 | ParseExtensionsProperty(&target->extensions, err, o); |
| 4161 | ParseExtrasProperty(&target->extras, o); |
| 4162 | |
| 4163 | if (store_json_strings) { |
| 4164 | { |
| 4165 | detail::json_const_iterator it; |
| 4166 | if (detail::FindMember(o, "extensions", it)) { |
| 4167 | target->extensions_json_string = detail::JsonToString(detail::GetValue(it)); |
| 4168 | } |
| 4169 | } |
| 4170 | { |
| 4171 | detail::json_const_iterator it; |
| 4172 | if (detail::FindMember(o, "extras", it)) { |
| 4173 | target->extras_json_string = detail::JsonToString(detail::GetValue(it)); |
| 4174 | } |
| 4175 | } |
| 4176 | } |
| 4177 | return true; |
| 4178 | } |
| 4179 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4180 | static bool ParseAsset(Asset *asset, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4181 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 57c1018 | 2017-10-19 18:48:26 +0900 | [diff] [blame] | 4182 | ParseStringProperty(&asset->version, err, o, "version", true, "Asset"); |
| 4183 | ParseStringProperty(&asset->generator, err, o, "generator", false, "Asset"); |
| 4184 | ParseStringProperty(&asset->minVersion, err, o, "minVersion", false, "Asset"); |
Miguel Sousa | 22bfc84 | 2020-02-20 14:16:58 +0100 | [diff] [blame] | 4185 | ParseStringProperty(&asset->copyright, err, o, "copyright", false, "Asset"); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4186 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 4187 | ParseExtrasAndExtensions(asset, err, o, store_original_json_for_extras_and_extensions); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4188 | return true; |
| 4189 | } |
| 4190 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4191 | static bool ParseImage(Image *image, const int image_idx, std::string *err, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4192 | std::string *warn, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4193 | bool store_original_json_for_extras_and_extensions, |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 4194 | const std::string &basedir, const size_t max_file_size, FsCallbacks *fs, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 4195 | const URICallbacks *uri_cb, |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 4196 | LoadImageDataFunction *LoadImageData = nullptr, |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 4197 | void *load_image_user_data = nullptr) { |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 4198 | // 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] | 4199 | |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4200 | // schema says oneOf [`bufferView`, `uri`] |
| 4201 | // TODO(syoyo): Check the type of each parameters. |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4202 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4203 | bool hasBufferView = detail::FindMember(o, "bufferView", it); |
| 4204 | bool hasURI = detail::FindMember(o, "uri", it); |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4205 | |
Syoyo Fujita | af3ebb2 | 2019-01-06 18:55:57 +0900 | [diff] [blame] | 4206 | ParseStringProperty(&image->name, err, o, "name", false); |
| 4207 | |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4208 | if (hasBufferView && hasURI) { |
| 4209 | // Should not both defined. |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 4210 | if (err) { |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 4211 | (*err) += |
| 4212 | "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] | 4213 | "defined for image[" + |
| 4214 | std::to_string(image_idx) + "] name = \"" + image->name + "\"\n"; |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4215 | } |
| 4216 | return false; |
| 4217 | } |
| 4218 | |
| 4219 | if (!hasBufferView && !hasURI) { |
| 4220 | if (err) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4221 | (*err) += "Neither required `bufferView` nor `uri` defined for image[" + |
| 4222 | std::to_string(image_idx) + "] name = \"" + image->name + |
| 4223 | "\"\n"; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 4224 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4225 | return false; |
| 4226 | } |
| 4227 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 4228 | ParseExtrasAndExtensions(image, err, o, store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4229 | |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4230 | if (hasBufferView) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4231 | int bufferView = -1; |
| 4232 | if (!ParseIntegerProperty(&bufferView, err, o, "bufferView", true)) { |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4233 | if (err) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4234 | (*err) += "Failed to parse `bufferView` for image[" + |
| 4235 | std::to_string(image_idx) + "] name = \"" + image->name + |
| 4236 | "\"\n"; |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4237 | } |
| 4238 | return false; |
| 4239 | } |
| 4240 | |
| 4241 | std::string mime_type; |
| 4242 | ParseStringProperty(&mime_type, err, o, "mimeType", false); |
| 4243 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4244 | int width = 0; |
| 4245 | ParseIntegerProperty(&width, err, o, "width", false); |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4246 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4247 | int height = 0; |
| 4248 | ParseIntegerProperty(&height, err, o, "height", false); |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4249 | |
| 4250 | // Just only save some information here. Loading actual image data from |
| 4251 | // bufferView is done after this `ParseImage` function. |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4252 | image->bufferView = bufferView; |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4253 | image->mimeType = mime_type; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4254 | image->width = width; |
| 4255 | image->height = height; |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4256 | |
| 4257 | return true; |
| 4258 | } |
| 4259 | |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4260 | // Parse URI & Load image data. |
| 4261 | |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4262 | std::string uri; |
| 4263 | std::string tmp_err; |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4264 | if (!ParseStringProperty(&uri, &tmp_err, o, "uri", true)) { |
| 4265 | if (err) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4266 | (*err) += "Failed to parse `uri` for image[" + std::to_string(image_idx) + |
| 4267 | "] name = \"" + image->name + "\".\n"; |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4268 | } |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4269 | return false; |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4270 | } |
| 4271 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4272 | std::vector<unsigned char> img; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4273 | |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4274 | if (IsDataURI(uri)) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 4275 | if (!DecodeDataURI(&img, image->mimeType, uri, 0, false)) { |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4276 | if (err) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4277 | (*err) += "Failed to decode 'uri' for image[" + |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 4278 | std::to_string(image_idx) + "] name = \"" + image->name + |
| 4279 | "\"\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4280 | } |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4281 | return false; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4282 | } |
| 4283 | } else { |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4284 | // Assume external file |
| 4285 | // Keep texture path (for textures that cannot be decoded) |
| 4286 | image->uri = uri; |
Selmar | 67af3c9 | 2018-03-16 11:48:19 +0100 | [diff] [blame] | 4287 | #ifdef TINYGLTF_NO_EXTERNAL_IMAGE |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4288 | return true; |
AlvaroBarua | 4317223 | 2022-09-11 00:41:43 +0100 | [diff] [blame] | 4289 | #else |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 4290 | std::string decoded_uri; |
| 4291 | if (!uri_cb->decode(uri, &decoded_uri, uri_cb->user_data)) { |
| 4292 | if (warn) { |
| 4293 | (*warn) += "Failed to decode 'uri' for image[" + |
Syoyo Fujita | 877d856 | 2023-04-23 21:47:31 +0900 | [diff] [blame] | 4294 | std::to_string(image_idx) + "] name = \"" + image->name + |
| 4295 | "\"\n"; |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 4296 | } |
| 4297 | |
| 4298 | // Image loading failure is not critical to overall gltf loading. |
| 4299 | return true; |
| 4300 | } |
| 4301 | |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 4302 | if (!LoadExternalFile(&img, err, warn, decoded_uri, basedir, |
| 4303 | /* required */ false, /* required bytes */ 0, |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 4304 | /* checksize */ false, /* max file size */ max_file_size, fs)) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 4305 | if (warn) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4306 | (*warn) += "Failed to load external 'uri' for image[" + |
Syoyo Fujita | 877d856 | 2023-04-23 21:47:31 +0900 | [diff] [blame] | 4307 | std::to_string(image_idx) + "] name = \"" + decoded_uri + |
| 4308 | "\"\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4309 | } |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4310 | // If the image cannot be loaded, keep uri as image->uri. |
| 4311 | return true; |
| 4312 | } |
Syoyo Fujita | 8e2c24f | 2018-03-21 19:54:38 +0900 | [diff] [blame] | 4313 | |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4314 | if (img.empty()) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 4315 | if (warn) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4316 | (*warn) += "Image data is empty 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"; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4319 | } |
Syoyo Fujita | 246654a | 2018-03-21 20:32:22 +0900 | [diff] [blame] | 4320 | return false; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4321 | } |
AlvaroBarua | 4317223 | 2022-09-11 00:41:43 +0100 | [diff] [blame] | 4322 | #endif |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4323 | } |
| 4324 | |
Squareys | ff644d8 | 2018-03-13 22:36:18 +0100 | [diff] [blame] | 4325 | if (*LoadImageData == nullptr) { |
| 4326 | if (err) { |
| 4327 | (*err) += "No LoadImageData callback specified.\n"; |
| 4328 | } |
| 4329 | return false; |
| 4330 | } |
Syoyo Fujita | af3ebb2 | 2019-01-06 18:55:57 +0900 | [diff] [blame] | 4331 | return (*LoadImageData)(image, image_idx, err, warn, 0, 0, &img.at(0), |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 4332 | static_cast<int>(img.size()), load_image_user_data); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4333 | } |
| 4334 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4335 | static bool ParseTexture(Texture *texture, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4336 | bool store_original_json_for_extras_and_extensions, |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4337 | const std::string &basedir) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4338 | (void)basedir; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4339 | int sampler = -1; |
| 4340 | int source = -1; |
| 4341 | ParseIntegerProperty(&sampler, err, o, "sampler", false); |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4342 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4343 | ParseIntegerProperty(&source, err, o, "source", false); |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 4344 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4345 | texture->sampler = sampler; |
| 4346 | texture->source = source; |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 4347 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 4348 | ParseExtrasAndExtensions(texture, err, o, store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4349 | |
Vladimír Vondruš | 239be2c | 2018-07-24 23:23:56 +0200 | [diff] [blame] | 4350 | ParseStringProperty(&texture->name, err, o, "name", false); |
| 4351 | |
Syoyo Fujita | bde7021 | 2016-02-07 17:38:17 +0900 | [diff] [blame] | 4352 | return true; |
| 4353 | } |
| 4354 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4355 | static bool ParseTextureInfo( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4356 | TextureInfo *texinfo, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4357 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 4358 | if (texinfo == nullptr) { |
| 4359 | return false; |
| 4360 | } |
| 4361 | |
| 4362 | if (!ParseIntegerProperty(&texinfo->index, err, o, "index", |
| 4363 | /* required */ true, "TextureInfo")) { |
| 4364 | return false; |
| 4365 | } |
| 4366 | |
| 4367 | ParseIntegerProperty(&texinfo->texCoord, err, o, "texCoord", false); |
| 4368 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 4369 | ParseExtrasAndExtensions(texinfo, err, o, store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4370 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 4371 | return true; |
| 4372 | } |
| 4373 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4374 | static bool ParseNormalTextureInfo( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4375 | NormalTextureInfo *texinfo, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4376 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 4377 | if (texinfo == nullptr) { |
| 4378 | return false; |
| 4379 | } |
| 4380 | |
| 4381 | if (!ParseIntegerProperty(&texinfo->index, err, o, "index", |
| 4382 | /* required */ true, "NormalTextureInfo")) { |
| 4383 | return false; |
| 4384 | } |
| 4385 | |
| 4386 | ParseIntegerProperty(&texinfo->texCoord, err, o, "texCoord", false); |
| 4387 | ParseNumberProperty(&texinfo->scale, err, o, "scale", false); |
| 4388 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 4389 | ParseExtrasAndExtensions(texinfo, err, o, store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4390 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 4391 | return true; |
| 4392 | } |
| 4393 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4394 | static bool ParseOcclusionTextureInfo( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4395 | OcclusionTextureInfo *texinfo, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4396 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 4397 | if (texinfo == nullptr) { |
| 4398 | return false; |
| 4399 | } |
| 4400 | |
| 4401 | if (!ParseIntegerProperty(&texinfo->index, err, o, "index", |
| 4402 | /* required */ true, "NormalTextureInfo")) { |
| 4403 | return false; |
| 4404 | } |
| 4405 | |
| 4406 | ParseIntegerProperty(&texinfo->texCoord, err, o, "texCoord", false); |
| 4407 | ParseNumberProperty(&texinfo->strength, err, o, "strength", false); |
| 4408 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 4409 | ParseExtrasAndExtensions(texinfo, err, o, store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4410 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 4411 | return true; |
| 4412 | } |
| 4413 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4414 | static bool ParseBuffer(Buffer *buffer, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4415 | bool store_original_json_for_extras_and_extensions, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 4416 | FsCallbacks *fs, const URICallbacks *uri_cb, |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 4417 | const std::string &basedir, const size_t max_buffer_size, bool is_binary = false, |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 4418 | const unsigned char *bin_data = nullptr, |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4419 | size_t bin_size = 0) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4420 | size_t byteLength; |
| 4421 | if (!ParseUnsignedProperty(&byteLength, err, o, "byteLength", true, |
| 4422 | "Buffer")) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4423 | return false; |
| 4424 | } |
| 4425 | |
Aurélien Chatelain | 0aab157 | 2017-05-30 15:03:48 +0000 | [diff] [blame] | 4426 | // In glTF 2.0, uri is not mandatory anymore |
Syoyo Fujita | 20244e1 | 2018-03-15 11:01:05 -0500 | [diff] [blame] | 4427 | buffer->uri.clear(); |
| 4428 | ParseStringProperty(&buffer->uri, err, o, "uri", false, "Buffer"); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4429 | |
Aurélien Chatelain | 0aab157 | 2017-05-30 15:03:48 +0000 | [diff] [blame] | 4430 | // 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] | 4431 | if (!is_binary && buffer->uri.empty()) { |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 4432 | if (err) { |
| 4433 | (*err) += "'uri' is missing from non binary glTF file buffer.\n"; |
| 4434 | } |
Aurélien Chatelain | 0aab157 | 2017-05-30 15:03:48 +0000 | [diff] [blame] | 4435 | } |
| 4436 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4437 | detail::json_const_iterator type; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4438 | if (detail::FindMember(o, "type", type)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4439 | std::string typeStr; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4440 | if (detail::GetString(detail::GetValue(type), typeStr)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4441 | if (typeStr.compare("arraybuffer") == 0) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4442 | // buffer.type = "arraybuffer"; |
| 4443 | } |
| 4444 | } |
| 4445 | } |
| 4446 | |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 4447 | if (is_binary) { |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 4448 | // Still binary glTF accepts external dataURI. |
Syoyo Fujita | 20244e1 | 2018-03-15 11:01:05 -0500 | [diff] [blame] | 4449 | if (!buffer->uri.empty()) { |
Syoyo Fujita | 39abfb5 | 2018-07-11 02:46:52 +0900 | [diff] [blame] | 4450 | // First try embedded data URI. |
| 4451 | if (IsDataURI(buffer->uri)) { |
| 4452 | std::string mime_type; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4453 | if (!DecodeDataURI(&buffer->data, mime_type, buffer->uri, byteLength, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 4454 | true)) { |
| 4455 | if (err) { |
| 4456 | (*err) += |
| 4457 | "Failed to decode 'uri' : " + buffer->uri + " in Buffer\n"; |
| 4458 | } |
| 4459 | return false; |
Syoyo Fujita | 39abfb5 | 2018-07-11 02:46:52 +0900 | [diff] [blame] | 4460 | } |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 4461 | } else { |
Syoyo Fujita | 39abfb5 | 2018-07-11 02:46:52 +0900 | [diff] [blame] | 4462 | // External .bin file. |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 4463 | std::string decoded_uri; |
| 4464 | if (!uri_cb->decode(buffer->uri, &decoded_uri, uri_cb->user_data)) { |
| 4465 | return false; |
| 4466 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 4467 | if (!LoadExternalFile(&buffer->data, err, /* warn */ nullptr, |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 4468 | decoded_uri, basedir, /* required */ true, |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 4469 | byteLength, /* checkSize */ true, /* max_file_size */max_buffer_size, fs)) { |
Vladimír Vondruš | fd84ceb | 2018-08-16 21:07:56 +0200 | [diff] [blame] | 4470 | return false; |
| 4471 | } |
Syoyo Fujita | 39abfb5 | 2018-07-11 02:46:52 +0900 | [diff] [blame] | 4472 | } |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 4473 | } else { |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4474 | // load data from (embedded) binary data |
| 4475 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 4476 | if ((bin_size == 0) || (bin_data == nullptr)) { |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4477 | if (err) { |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 4478 | (*err) += "Invalid binary data in `Buffer', or GLB with empty BIN chunk.\n"; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4479 | } |
| 4480 | return false; |
| 4481 | } |
| 4482 | |
| 4483 | if (byteLength > bin_size) { |
| 4484 | if (err) { |
| 4485 | std::stringstream ss; |
| 4486 | ss << "Invalid `byteLength'. Must be equal or less than binary size: " |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 4487 | "`byteLength' = " |
| 4488 | << byteLength << ", binary size = " << bin_size << std::endl; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4489 | (*err) += ss.str(); |
| 4490 | } |
| 4491 | return false; |
| 4492 | } |
| 4493 | |
Aurélien Chatelain | 0aab157 | 2017-05-30 15:03:48 +0000 | [diff] [blame] | 4494 | // Read buffer data |
| 4495 | buffer->data.resize(static_cast<size_t>(byteLength)); |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 4496 | memcpy(&(buffer->data.at(0)), bin_data, static_cast<size_t>(byteLength)); |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 4497 | } |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 4498 | |
| 4499 | } else { |
Syoyo Fujita | 20244e1 | 2018-03-15 11:01:05 -0500 | [diff] [blame] | 4500 | if (IsDataURI(buffer->uri)) { |
johan bowald | ef151a4 | 2018-04-01 13:44:48 +0200 | [diff] [blame] | 4501 | std::string mime_type; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4502 | if (!DecodeDataURI(&buffer->data, mime_type, buffer->uri, byteLength, |
| 4503 | true)) { |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 4504 | if (err) { |
Syoyo Fujita | 20244e1 | 2018-03-15 11:01:05 -0500 | [diff] [blame] | 4505 | (*err) += "Failed to decode 'uri' : " + buffer->uri + " in Buffer\n"; |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 4506 | } |
| 4507 | return false; |
| 4508 | } |
| 4509 | } else { |
| 4510 | // Assume external .bin file. |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 4511 | std::string decoded_uri; |
| 4512 | if (!uri_cb->decode(buffer->uri, &decoded_uri, uri_cb->user_data)) { |
| 4513 | return false; |
| 4514 | } |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 4515 | if (!LoadExternalFile(&buffer->data, err, /* warn */ nullptr, decoded_uri, |
| 4516 | basedir, /* required */ true, byteLength, |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 4517 | /* checkSize */ true, /* max file size */max_buffer_size, fs)) { |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 4518 | return false; |
| 4519 | } |
| 4520 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4521 | } |
| 4522 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4523 | ParseStringProperty(&buffer->name, err, o, "name", false); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4524 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 4525 | ParseExtrasAndExtensions(buffer, err, o, store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4526 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4527 | return true; |
| 4528 | } |
| 4529 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4530 | static bool ParseBufferView( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4531 | BufferView *bufferView, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4532 | bool store_original_json_for_extras_and_extensions) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4533 | int buffer = -1; |
| 4534 | if (!ParseIntegerProperty(&buffer, err, o, "buffer", true, "BufferView")) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4535 | return false; |
| 4536 | } |
| 4537 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4538 | size_t byteOffset = 0; |
| 4539 | ParseUnsignedProperty(&byteOffset, err, o, "byteOffset", false); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4540 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4541 | size_t byteLength = 1; |
| 4542 | if (!ParseUnsignedProperty(&byteLength, err, o, "byteLength", true, |
| 4543 | "BufferView")) { |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 4544 | return false; |
| 4545 | } |
| 4546 | |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 4547 | size_t byteStride = 0; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4548 | if (!ParseUnsignedProperty(&byteStride, err, o, "byteStride", false)) { |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 4549 | // Spec says: When byteStride of referenced bufferView is not defined, it |
| 4550 | // means that accessor elements are tightly packed, i.e., effective stride |
| 4551 | // equals the size of the element. |
| 4552 | // We cannot determine the actual byteStride until Accessor are parsed, thus |
| 4553 | // set 0(= tightly packed) here(as done in OpenGL's VertexAttribPoiner) |
| 4554 | byteStride = 0; |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 4555 | } |
| 4556 | |
| 4557 | if ((byteStride > 252) || ((byteStride % 4) != 0)) { |
| 4558 | if (err) { |
| 4559 | std::stringstream ss; |
| 4560 | ss << "Invalid `byteStride' value. `byteStride' must be the multiple of " |
| 4561 | "4 : " |
| 4562 | << byteStride << std::endl; |
| 4563 | |
| 4564 | (*err) += ss.str(); |
| 4565 | } |
| 4566 | return false; |
| 4567 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4568 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4569 | int target = 0; |
| 4570 | ParseIntegerProperty(&target, err, o, "target", false); |
| 4571 | if ((target == TINYGLTF_TARGET_ARRAY_BUFFER) || |
| 4572 | (target == TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER)) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4573 | // OK |
| 4574 | } else { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4575 | target = 0; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4576 | } |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4577 | bufferView->target = target; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4578 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4579 | ParseStringProperty(&bufferView->name, err, o, "name", false); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4580 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 4581 | ParseExtrasAndExtensions(bufferView, err, o, store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4582 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4583 | bufferView->buffer = buffer; |
| 4584 | bufferView->byteOffset = byteOffset; |
| 4585 | bufferView->byteLength = byteLength; |
| 4586 | bufferView->byteStride = byteStride; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4587 | return true; |
| 4588 | } |
| 4589 | |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 4590 | static bool ParseSparseAccessor(Accessor::Sparse *sparse, std::string *err, |
| 4591 | const detail::json &o, |
| 4592 | bool store_original_json_for_extras_and_extensions) { |
| 4593 | sparse->isSparse = true; |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4594 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4595 | int count = 0; |
Syoyo Fujita | cbd3885 | 2022-02-26 21:19:15 +0900 | [diff] [blame] | 4596 | if (!ParseIntegerProperty(&count, err, o, "count", true, "SparseAccessor")) { |
| 4597 | return false; |
| 4598 | } |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4599 | |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 4600 | ParseExtrasAndExtensions(sparse, err, o, store_original_json_for_extras_and_extensions); |
| 4601 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4602 | detail::json_const_iterator indices_iterator; |
| 4603 | detail::json_const_iterator values_iterator; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4604 | if (!detail::FindMember(o, "indices", indices_iterator)) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4605 | (*err) = "the sparse object of this accessor doesn't have indices"; |
| 4606 | return false; |
| 4607 | } |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4608 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4609 | if (!detail::FindMember(o, "values", values_iterator)) { |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 4610 | (*err) = "the sparse object of this accessor doesn't have values"; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4611 | return false; |
| 4612 | } |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4613 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4614 | const detail::json &indices_obj = detail::GetValue(indices_iterator); |
| 4615 | const detail::json &values_obj = detail::GetValue(values_iterator); |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4616 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4617 | int indices_buffer_view = 0, indices_byte_offset = 0, component_type = 0; |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 4618 | if (!ParseIntegerProperty(&indices_buffer_view, err, indices_obj, |
| 4619 | "bufferView", true, "SparseAccessor")) { |
Syoyo Fujita | cbd3885 | 2022-02-26 21:19:15 +0900 | [diff] [blame] | 4620 | return false; |
| 4621 | } |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4622 | ParseIntegerProperty(&indices_byte_offset, err, indices_obj, "byteOffset", |
Jack Mousseau | 9471517 | 2022-02-24 14:25:37 -0800 | [diff] [blame] | 4623 | false); |
Syoyo Fujita | cbd3885 | 2022-02-26 21:19:15 +0900 | [diff] [blame] | 4624 | if (!ParseIntegerProperty(&component_type, err, indices_obj, "componentType", |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 4625 | true, "SparseAccessor")) { |
Syoyo Fujita | cbd3885 | 2022-02-26 21:19:15 +0900 | [diff] [blame] | 4626 | return false; |
| 4627 | } |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4628 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4629 | int values_buffer_view = 0, values_byte_offset = 0; |
Syoyo Fujita | cbd3885 | 2022-02-26 21:19:15 +0900 | [diff] [blame] | 4630 | if (!ParseIntegerProperty(&values_buffer_view, err, values_obj, "bufferView", |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 4631 | true, "SparseAccessor")) { |
Syoyo Fujita | cbd3885 | 2022-02-26 21:19:15 +0900 | [diff] [blame] | 4632 | return false; |
| 4633 | } |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4634 | ParseIntegerProperty(&values_byte_offset, err, values_obj, "byteOffset", |
Jack Mousseau | 9471517 | 2022-02-24 14:25:37 -0800 | [diff] [blame] | 4635 | false); |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4636 | |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 4637 | sparse->count = count; |
| 4638 | sparse->indices.bufferView = indices_buffer_view; |
| 4639 | sparse->indices.byteOffset = indices_byte_offset; |
| 4640 | sparse->indices.componentType = component_type; |
| 4641 | ParseExtrasAndExtensions(&sparse->indices, err, indices_obj, |
| 4642 | store_original_json_for_extras_and_extensions); |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4643 | |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 4644 | sparse->values.bufferView = values_buffer_view; |
| 4645 | sparse->values.byteOffset = values_byte_offset; |
| 4646 | ParseExtrasAndExtensions(&sparse->values, err, values_obj, |
| 4647 | store_original_json_for_extras_and_extensions); |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4648 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4649 | return true; |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4650 | } |
| 4651 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4652 | static bool ParseAccessor(Accessor *accessor, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4653 | bool store_original_json_for_extras_and_extensions) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4654 | int bufferView = -1; |
| 4655 | ParseIntegerProperty(&bufferView, err, o, "bufferView", false, "Accessor"); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4656 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4657 | size_t byteOffset = 0; |
| 4658 | ParseUnsignedProperty(&byteOffset, err, o, "byteOffset", false, "Accessor"); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4659 | |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 4660 | bool normalized = false; |
| 4661 | ParseBooleanProperty(&normalized, err, o, "normalized", false, "Accessor"); |
| 4662 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4663 | size_t componentType = 0; |
| 4664 | if (!ParseUnsignedProperty(&componentType, err, o, "componentType", true, |
| 4665 | "Accessor")) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4666 | return false; |
| 4667 | } |
| 4668 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4669 | size_t count = 0; |
| 4670 | if (!ParseUnsignedProperty(&count, err, o, "count", true, "Accessor")) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4671 | return false; |
| 4672 | } |
| 4673 | |
| 4674 | std::string type; |
Syoyo Fujita | 90e1ed2 | 2017-05-30 00:47:37 +0900 | [diff] [blame] | 4675 | if (!ParseStringProperty(&type, err, o, "type", true, "Accessor")) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4676 | return false; |
| 4677 | } |
| 4678 | |
| 4679 | if (type.compare("SCALAR") == 0) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4680 | accessor->type = TINYGLTF_TYPE_SCALAR; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4681 | } else if (type.compare("VEC2") == 0) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4682 | accessor->type = TINYGLTF_TYPE_VEC2; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4683 | } else if (type.compare("VEC3") == 0) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4684 | accessor->type = TINYGLTF_TYPE_VEC3; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4685 | } else if (type.compare("VEC4") == 0) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4686 | accessor->type = TINYGLTF_TYPE_VEC4; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4687 | } else if (type.compare("MAT2") == 0) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4688 | accessor->type = TINYGLTF_TYPE_MAT2; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4689 | } else if (type.compare("MAT3") == 0) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4690 | accessor->type = TINYGLTF_TYPE_MAT3; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4691 | } else if (type.compare("MAT4") == 0) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4692 | accessor->type = TINYGLTF_TYPE_MAT4; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4693 | } else { |
| 4694 | std::stringstream ss; |
| 4695 | ss << "Unsupported `type` for accessor object. Got \"" << type << "\"\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4696 | if (err) { |
| 4697 | (*err) += ss.str(); |
| 4698 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4699 | return false; |
| 4700 | } |
| 4701 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4702 | ParseStringProperty(&accessor->name, err, o, "name", false); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4703 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4704 | accessor->minValues.clear(); |
| 4705 | accessor->maxValues.clear(); |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 4706 | ParseNumberArrayProperty(&accessor->minValues, err, o, "min", false, |
| 4707 | "Accessor"); |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 4708 | |
Syoyo Fujita | d9d012a | 2017-07-12 18:29:29 +0900 | [diff] [blame] | 4709 | ParseNumberArrayProperty(&accessor->maxValues, err, o, "max", false, |
| 4710 | "Accessor"); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4711 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4712 | accessor->count = count; |
| 4713 | accessor->bufferView = bufferView; |
| 4714 | accessor->byteOffset = byteOffset; |
jianghaosen | 4748ad6 | 2017-08-29 20:08:37 +0800 | [diff] [blame] | 4715 | accessor->normalized = normalized; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4716 | { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4717 | if (componentType >= TINYGLTF_COMPONENT_TYPE_BYTE && |
| 4718 | componentType <= TINYGLTF_COMPONENT_TYPE_DOUBLE) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4719 | // OK |
Arthur Brainville (Ybalrid) | 811e1d3 | 2019-06-15 07:32:38 +0200 | [diff] [blame] | 4720 | accessor->componentType = int(componentType); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4721 | } else { |
| 4722 | std::stringstream ss; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4723 | ss << "Invalid `componentType` in accessor. Got " << componentType |
| 4724 | << "\n"; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4725 | if (err) { |
| 4726 | (*err) += ss.str(); |
| 4727 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4728 | return false; |
| 4729 | } |
| 4730 | } |
| 4731 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 4732 | ParseExtrasAndExtensions(accessor, err, o, store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4733 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4734 | // check if accessor has a "sparse" object: |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4735 | detail::json_const_iterator iterator; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4736 | if (detail::FindMember(o, "sparse", iterator)) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4737 | // here this accessor has a "sparse" subobject |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 4738 | return ParseSparseAccessor(&accessor->sparse, err, detail::GetValue(iterator), |
| 4739 | store_original_json_for_extras_and_extensions); |
Arthur Brainville (Ybalrid) | 7e9f734 | 2019-03-06 12:27:23 +0000 | [diff] [blame] | 4740 | } |
| 4741 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4742 | return true; |
| 4743 | } |
| 4744 | |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4745 | #ifdef TINYGLTF_ENABLE_DRACO |
| 4746 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4747 | static void DecodeIndexBuffer(draco::Mesh *mesh, size_t componentSize, |
| 4748 | std::vector<uint8_t> &outBuffer) { |
| 4749 | if (componentSize == 4) { |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4750 | assert(sizeof(mesh->face(draco::FaceIndex(0))[0]) == componentSize); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4751 | memcpy(outBuffer.data(), &mesh->face(draco::FaceIndex(0))[0], |
| 4752 | outBuffer.size()); |
| 4753 | } else { |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4754 | size_t faceStride = componentSize * 3; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4755 | for (draco::FaceIndex f(0); f < mesh->num_faces(); ++f) { |
| 4756 | const draco::Mesh::Face &face = mesh->face(f); |
| 4757 | if (componentSize == 2) { |
| 4758 | uint16_t indices[3] = {(uint16_t)face[0].value(), |
| 4759 | (uint16_t)face[1].value(), |
| 4760 | (uint16_t)face[2].value()}; |
| 4761 | memcpy(outBuffer.data() + f.value() * faceStride, &indices[0], |
| 4762 | faceStride); |
| 4763 | } else { |
| 4764 | uint8_t indices[3] = {(uint8_t)face[0].value(), |
| 4765 | (uint8_t)face[1].value(), |
| 4766 | (uint8_t)face[2].value()}; |
| 4767 | memcpy(outBuffer.data() + f.value() * faceStride, &indices[0], |
| 4768 | faceStride); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4769 | } |
| 4770 | } |
| 4771 | } |
| 4772 | } |
| 4773 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4774 | template <typename T> |
| 4775 | static bool GetAttributeForAllPoints(draco::Mesh *mesh, |
| 4776 | const draco::PointAttribute *pAttribute, |
| 4777 | std::vector<uint8_t> &outBuffer) { |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4778 | size_t byteOffset = 0; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4779 | T values[4] = {0, 0, 0, 0}; |
| 4780 | for (draco::PointIndex i(0); i < mesh->num_points(); ++i) { |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4781 | const draco::AttributeValueIndex val_index = pAttribute->mapped_index(i); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4782 | if (!pAttribute->ConvertValue<T>(val_index, pAttribute->num_components(), |
| 4783 | values)) |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4784 | return false; |
| 4785 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4786 | memcpy(outBuffer.data() + byteOffset, &values[0], |
| 4787 | sizeof(T) * pAttribute->num_components()); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4788 | byteOffset += sizeof(T) * pAttribute->num_components(); |
| 4789 | } |
| 4790 | |
| 4791 | return true; |
| 4792 | } |
| 4793 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4794 | static bool GetAttributeForAllPoints(uint32_t componentType, draco::Mesh *mesh, |
| 4795 | const draco::PointAttribute *pAttribute, |
| 4796 | std::vector<uint8_t> &outBuffer) { |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4797 | bool decodeResult = false; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4798 | switch (componentType) { |
| 4799 | case TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE: |
| 4800 | decodeResult = |
| 4801 | GetAttributeForAllPoints<uint8_t>(mesh, pAttribute, outBuffer); |
| 4802 | break; |
| 4803 | case TINYGLTF_COMPONENT_TYPE_BYTE: |
| 4804 | decodeResult = |
| 4805 | GetAttributeForAllPoints<int8_t>(mesh, pAttribute, outBuffer); |
| 4806 | break; |
| 4807 | case TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT: |
| 4808 | decodeResult = |
| 4809 | GetAttributeForAllPoints<uint16_t>(mesh, pAttribute, outBuffer); |
| 4810 | break; |
| 4811 | case TINYGLTF_COMPONENT_TYPE_SHORT: |
| 4812 | decodeResult = |
| 4813 | GetAttributeForAllPoints<int16_t>(mesh, pAttribute, outBuffer); |
| 4814 | break; |
| 4815 | case TINYGLTF_COMPONENT_TYPE_INT: |
| 4816 | decodeResult = |
| 4817 | GetAttributeForAllPoints<int32_t>(mesh, pAttribute, outBuffer); |
| 4818 | break; |
| 4819 | case TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT: |
| 4820 | decodeResult = |
| 4821 | GetAttributeForAllPoints<uint32_t>(mesh, pAttribute, outBuffer); |
| 4822 | break; |
| 4823 | case TINYGLTF_COMPONENT_TYPE_FLOAT: |
| 4824 | decodeResult = |
| 4825 | GetAttributeForAllPoints<float>(mesh, pAttribute, outBuffer); |
| 4826 | break; |
| 4827 | case TINYGLTF_COMPONENT_TYPE_DOUBLE: |
| 4828 | decodeResult = |
| 4829 | GetAttributeForAllPoints<double>(mesh, pAttribute, outBuffer); |
| 4830 | break; |
| 4831 | default: |
| 4832 | return false; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4833 | } |
| 4834 | |
| 4835 | return decodeResult; |
| 4836 | } |
| 4837 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4838 | static bool ParseDracoExtension(Primitive *primitive, Model *model, |
| 4839 | std::string *err, |
| 4840 | const Value &dracoExtensionValue) { |
snowapril | 84e1526 | 2021-03-20 19:25:58 +0900 | [diff] [blame] | 4841 | (void)err; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4842 | auto bufferViewValue = dracoExtensionValue.Get("bufferView"); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4843 | if (!bufferViewValue.IsInt()) return false; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4844 | auto attributesValue = dracoExtensionValue.Get("attributes"); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4845 | if (!attributesValue.IsObject()) return false; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4846 | |
| 4847 | auto attributesObject = attributesValue.Get<Value::Object>(); |
| 4848 | int bufferView = bufferViewValue.Get<int>(); |
| 4849 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4850 | BufferView &view = model->bufferViews[bufferView]; |
| 4851 | Buffer &buffer = model->buffers[view.buffer]; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4852 | // BufferView has already been decoded |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4853 | if (view.dracoDecoded) return true; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4854 | view.dracoDecoded = true; |
| 4855 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4856 | const char *bufferViewData = |
| 4857 | reinterpret_cast<const char *>(buffer.data.data() + view.byteOffset); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4858 | size_t bufferViewSize = view.byteLength; |
| 4859 | |
| 4860 | // decode draco |
| 4861 | draco::DecoderBuffer decoderBuffer; |
| 4862 | decoderBuffer.Init(bufferViewData, bufferViewSize); |
| 4863 | draco::Decoder decoder; |
| 4864 | auto decodeResult = decoder.DecodeMeshFromBuffer(&decoderBuffer); |
| 4865 | if (!decodeResult.ok()) { |
| 4866 | return false; |
| 4867 | } |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4868 | const std::unique_ptr<draco::Mesh> &mesh = decodeResult.value(); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4869 | |
| 4870 | // create new bufferView for indices |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4871 | if (primitive->indices >= 0) { |
| 4872 | int32_t componentSize = GetComponentSizeInBytes( |
| 4873 | model->accessors[primitive->indices].componentType); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4874 | Buffer decodedIndexBuffer; |
| 4875 | decodedIndexBuffer.data.resize(mesh->num_faces() * 3 * componentSize); |
| 4876 | |
| 4877 | DecodeIndexBuffer(mesh.get(), componentSize, decodedIndexBuffer.data); |
| 4878 | |
| 4879 | model->buffers.emplace_back(std::move(decodedIndexBuffer)); |
| 4880 | |
| 4881 | BufferView decodedIndexBufferView; |
| 4882 | decodedIndexBufferView.buffer = int(model->buffers.size() - 1); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4883 | decodedIndexBufferView.byteLength = |
| 4884 | int(mesh->num_faces() * 3 * componentSize); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4885 | decodedIndexBufferView.byteOffset = 0; |
| 4886 | decodedIndexBufferView.byteStride = 0; |
| 4887 | decodedIndexBufferView.target = TINYGLTF_TARGET_ARRAY_BUFFER; |
| 4888 | model->bufferViews.emplace_back(std::move(decodedIndexBufferView)); |
| 4889 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4890 | model->accessors[primitive->indices].bufferView = |
| 4891 | int(model->bufferViews.size() - 1); |
Alexander Wood | 0d77a29 | 2019-01-26 08:58:45 -0500 | [diff] [blame] | 4892 | model->accessors[primitive->indices].count = int(mesh->num_faces() * 3); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4893 | } |
| 4894 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4895 | for (const auto &attribute : attributesObject) { |
| 4896 | if (!attribute.second.IsInt()) return false; |
Alexander Wood | 0d77a29 | 2019-01-26 08:58:45 -0500 | [diff] [blame] | 4897 | auto primitiveAttribute = primitive->attributes.find(attribute.first); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4898 | if (primitiveAttribute == primitive->attributes.end()) return false; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4899 | |
| 4900 | int dracoAttributeIndex = attribute.second.Get<int>(); |
| 4901 | const auto pAttribute = mesh->GetAttributeByUniqueId(dracoAttributeIndex); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4902 | const auto componentType = |
| 4903 | model->accessors[primitiveAttribute->second].componentType; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4904 | |
| 4905 | // Create a new buffer for this decoded buffer |
| 4906 | Buffer decodedBuffer; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4907 | size_t bufferSize = mesh->num_points() * pAttribute->num_components() * |
| 4908 | GetComponentSizeInBytes(componentType); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4909 | decodedBuffer.data.resize(bufferSize); |
| 4910 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4911 | if (!GetAttributeForAllPoints(componentType, mesh.get(), pAttribute, |
| 4912 | decodedBuffer.data)) |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4913 | return false; |
| 4914 | |
| 4915 | model->buffers.emplace_back(std::move(decodedBuffer)); |
| 4916 | |
| 4917 | BufferView decodedBufferView; |
| 4918 | decodedBufferView.buffer = int(model->buffers.size() - 1); |
| 4919 | decodedBufferView.byteLength = bufferSize; |
| 4920 | decodedBufferView.byteOffset = pAttribute->byte_offset(); |
| 4921 | decodedBufferView.byteStride = pAttribute->byte_stride(); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4922 | decodedBufferView.target = primitive->indices >= 0 |
| 4923 | ? TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER |
| 4924 | : TINYGLTF_TARGET_ARRAY_BUFFER; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4925 | model->bufferViews.emplace_back(std::move(decodedBufferView)); |
| 4926 | |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4927 | model->accessors[primitiveAttribute->second].bufferView = |
| 4928 | int(model->bufferViews.size() - 1); |
| 4929 | model->accessors[primitiveAttribute->second].count = |
| 4930 | int(mesh->num_points()); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4931 | } |
| 4932 | |
| 4933 | return true; |
| 4934 | } |
| 4935 | #endif |
| 4936 | |
| 4937 | static bool ParsePrimitive(Primitive *primitive, Model *model, std::string *err, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4938 | const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4939 | bool store_original_json_for_extras_and_extensions) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4940 | int material = -1; |
| 4941 | ParseIntegerProperty(&material, err, o, "material", false); |
| 4942 | primitive->material = material; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4943 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4944 | int mode = TINYGLTF_MODE_TRIANGLES; |
| 4945 | ParseIntegerProperty(&mode, err, o, "mode", false); |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 4946 | primitive->mode = mode; // Why only triangles were supported ? |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4947 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 4948 | int indices = -1; |
| 4949 | ParseIntegerProperty(&indices, err, o, "indices", false); |
| 4950 | primitive->indices = indices; |
| 4951 | if (!ParseStringIntegerProperty(&primitive->attributes, err, o, "attributes", |
| 4952 | true, "Primitive")) { |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 4953 | return false; |
| 4954 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4955 | |
Aurélien Chatelain | 2682c6f | 2017-06-14 09:46:57 +0000 | [diff] [blame] | 4956 | // Look for morph targets |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4957 | detail::json_const_iterator targetsObject; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4958 | if (detail::FindMember(o, "targets", targetsObject) && |
| 4959 | detail::IsArray(detail::GetValue(targetsObject))) { |
| 4960 | auto targetsObjectEnd = detail::ArrayEnd(detail::GetValue(targetsObject)); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4961 | for (detail::json_const_array_iterator i = detail::ArrayBegin(detail::GetValue(targetsObject)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4962 | i != targetsObjectEnd; ++i) { |
Aurélien Chatelain | 2682c6f | 2017-06-14 09:46:57 +0000 | [diff] [blame] | 4963 | std::map<std::string, int> targetAttribues; |
| 4964 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4965 | const detail::json &dict = *i; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4966 | if (detail::IsObject(dict)) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4967 | detail::json_const_iterator dictIt(detail::ObjectBegin(dict)); |
| 4968 | detail::json_const_iterator dictItEnd(detail::ObjectEnd(dict)); |
Aurélien Chatelain | 2682c6f | 2017-06-14 09:46:57 +0000 | [diff] [blame] | 4969 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4970 | for (; dictIt != dictItEnd; ++dictIt) { |
| 4971 | int iVal; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 4972 | if (detail::GetInt(detail::GetValue(dictIt), iVal)) |
| 4973 | targetAttribues[detail::GetKey(dictIt)] = iVal; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 4974 | } |
| 4975 | primitive->targets.emplace_back(std::move(targetAttribues)); |
Aurélien Chatelain | 2682c6f | 2017-06-14 09:46:57 +0000 | [diff] [blame] | 4976 | } |
Aurélien Chatelain | 2682c6f | 2017-06-14 09:46:57 +0000 | [diff] [blame] | 4977 | } |
| 4978 | } |
| 4979 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 4980 | ParseExtrasAndExtensions(primitive, err, o, store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4981 | |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4982 | #ifdef TINYGLTF_ENABLE_DRACO |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 4983 | auto dracoExtension = |
| 4984 | primitive->extensions.find("KHR_draco_mesh_compression"); |
| 4985 | if (dracoExtension != primitive->extensions.end()) { |
| 4986 | ParseDracoExtension(primitive, model, err, dracoExtension->second); |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4987 | } |
Syoyo Fujita | 7bdfed3 | 2019-03-03 17:04:49 +0900 | [diff] [blame] | 4988 | #else |
| 4989 | (void)model; |
Alex Wood | 7319db7 | 2019-01-24 15:38:16 -0500 | [diff] [blame] | 4990 | #endif |
| 4991 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4992 | return true; |
| 4993 | } |
| 4994 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 4995 | static bool ParseMesh(Mesh *mesh, Model *model, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 4996 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4997 | ParseStringProperty(&mesh->name, err, o, "name", false); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 4998 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 4999 | mesh->primitives.clear(); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5000 | detail::json_const_iterator primObject; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5001 | if (detail::FindMember(o, "primitives", primObject) && |
| 5002 | detail::IsArray(detail::GetValue(primObject))) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5003 | detail::json_const_array_iterator primEnd = detail::ArrayEnd(detail::GetValue(primObject)); |
| 5004 | for (detail::json_const_array_iterator i = detail::ArrayBegin(detail::GetValue(primObject)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5005 | i != primEnd; ++i) { |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5006 | Primitive primitive; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5007 | if (ParsePrimitive(&primitive, model, err, *i, |
| 5008 | store_original_json_for_extras_and_extensions)) { |
Luke San Antonio | 10a3e1d | 2016-06-15 00:05:01 -0400 | [diff] [blame] | 5009 | // Only add the primitive if the parsing succeeds. |
jrkoonce | d1e1472 | 2019-08-27 11:51:02 -0500 | [diff] [blame] | 5010 | mesh->primitives.emplace_back(std::move(primitive)); |
Luke San Antonio | 10a3e1d | 2016-06-15 00:05:01 -0400 | [diff] [blame] | 5011 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5012 | } |
| 5013 | } |
| 5014 | |
Aurélien Chatelain | 38a6a75 | 2017-05-24 09:54:55 +0000 | [diff] [blame] | 5015 | // Should probably check if has targets and if dimensions fit |
| 5016 | ParseNumberArrayProperty(&mesh->weights, err, o, "weights", false); |
| 5017 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 5018 | ParseExtrasAndExtensions(mesh, err, o, store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5019 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5020 | return true; |
| 5021 | } |
| 5022 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5023 | static bool ParseNode(Node *node, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5024 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5025 | ParseStringProperty(&node->name, err, o, "name", false); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5026 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5027 | int skin = -1; |
| 5028 | ParseIntegerProperty(&skin, err, o, "skin", false); |
| 5029 | node->skin = skin; |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 5030 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 5031 | // Matrix and T/R/S are exclusive |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 5032 | if (!ParseNumberArrayProperty(&node->matrix, err, o, "matrix", false)) { |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 5033 | ParseNumberArrayProperty(&node->rotation, err, o, "rotation", false); |
| 5034 | ParseNumberArrayProperty(&node->scale, err, o, "scale", false); |
| 5035 | ParseNumberArrayProperty(&node->translation, err, o, "translation", false); |
| 5036 | } |
| 5037 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5038 | int camera = -1; |
| 5039 | ParseIntegerProperty(&camera, err, o, "camera", false); |
| 5040 | node->camera = camera; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 5041 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5042 | int mesh = -1; |
| 5043 | ParseIntegerProperty(&mesh, err, o, "mesh", false); |
| 5044 | node->mesh = mesh; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5045 | |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5046 | node->children.clear(); |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5047 | ParseIntegerArrayProperty(&node->children, err, o, "children", false); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5048 | |
Benjamin Schmithüsen | ad63bf7 | 2019-08-16 14:19:27 +0200 | [diff] [blame] | 5049 | ParseNumberArrayProperty(&node->weights, err, o, "weights", false); |
| 5050 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 5051 | ParseExtrasAndExtensions(node, err, o, store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5052 | |
David Siegel | c164878 | 2023-05-09 01:33:57 +0200 | [diff] [blame] | 5053 | // KHR_lights_punctual: parse light source reference |
| 5054 | int light = -1; |
| 5055 | if (node->extensions.count("KHR_lights_punctual") != 0) { |
| 5056 | auto const& light_ext = node->extensions["KHR_lights_punctual"]; |
| 5057 | if (light_ext.Has("light")) { |
| 5058 | light = light_ext.Get("light").GetNumberAsInt(); |
| 5059 | } else { |
David Siegel | 157063f | 2023-06-06 15:31:58 +0200 | [diff] [blame] | 5060 | if (err) { |
| 5061 | *err += "Node has extension KHR_lights_punctual, but does not reference " |
| 5062 | "a light source.\n"; |
| 5063 | } |
David Siegel | c164878 | 2023-05-09 01:33:57 +0200 | [diff] [blame] | 5064 | return false; |
| 5065 | } |
| 5066 | } |
| 5067 | node->light = light; |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 5068 | |
| 5069 | // KHR_audio: parse audio source reference |
| 5070 | int emitter = -1; |
| 5071 | if (node->extensions.count("KHR_audio") != 0) { |
| 5072 | auto const &audio_ext = node->extensions["KHR_audio"]; |
| 5073 | if (audio_ext.Has("emitter")) { |
| 5074 | emitter = audio_ext.Get("emitter").GetNumberAsInt(); |
| 5075 | } else { |
| 5076 | if (err) { |
| 5077 | *err += |
| 5078 | "Node has extension KHR_audio, but does not reference " |
| 5079 | "a audio emitter.\n"; |
| 5080 | } |
| 5081 | return false; |
| 5082 | } |
| 5083 | } |
| 5084 | node->emitter = emitter; |
Syoyo Fujita | 7a570c8 | 2023-06-19 21:52:13 +0900 | [diff] [blame] | 5085 | |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 5086 | return true; |
Luke San Antonio | cdf4cb7 | 2016-06-14 21:32:11 -0400 | [diff] [blame] | 5087 | } |
| 5088 | |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 5089 | static bool ParseScene(Scene *scene, std::string *err, const detail::json &o, |
| 5090 | bool store_original_json_for_extras_and_extensions) { |
| 5091 | ParseStringProperty(&scene->name, err, o, "name", false); |
| 5092 | ParseIntegerArrayProperty(&scene->nodes, err, o, "nodes", false); |
| 5093 | |
| 5094 | ParseExtrasAndExtensions(scene, err, o, |
| 5095 | store_original_json_for_extras_and_extensions); |
| 5096 | |
| 5097 | // Parse KHR_audio global emitters |
| 5098 | if (scene->extensions.count("KHR_audio") != 0) { |
| 5099 | auto const &audio_ext = scene->extensions["KHR_audio"]; |
| 5100 | if (audio_ext.Has("emitters")) { |
| 5101 | auto emittersArr = audio_ext.Get("emitters"); |
| 5102 | for (size_t i = 0; i < emittersArr.ArrayLen(); ++i) { |
| 5103 | scene->audioEmitters.emplace_back(emittersArr.Get(i).GetNumberAsInt()); |
| 5104 | } |
| 5105 | } else { |
| 5106 | if (err) { |
| 5107 | *err += |
| 5108 | "Node has extension KHR_audio, but does not reference " |
| 5109 | "a audio emitter.\n"; |
| 5110 | } |
| 5111 | return false; |
| 5112 | } |
| 5113 | } |
| 5114 | |
| 5115 | return true; |
| 5116 | } |
| 5117 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5118 | static bool ParsePbrMetallicRoughness( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5119 | PbrMetallicRoughness *pbr, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5120 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5121 | if (pbr == nullptr) { |
| 5122 | return false; |
| 5123 | } |
| 5124 | |
| 5125 | std::vector<double> baseColorFactor; |
| 5126 | if (ParseNumberArrayProperty(&baseColorFactor, err, o, "baseColorFactor", |
| 5127 | /* required */ false)) { |
| 5128 | if (baseColorFactor.size() != 4) { |
| 5129 | if (err) { |
| 5130 | (*err) += |
| 5131 | "Array length of `baseColorFactor` parameter in " |
| 5132 | "pbrMetallicRoughness must be 4, but got " + |
| 5133 | std::to_string(baseColorFactor.size()) + "\n"; |
| 5134 | } |
| 5135 | return false; |
| 5136 | } |
Selmar Kok | c3353e1 | 2019-10-18 18:22:35 +0200 | [diff] [blame] | 5137 | pbr->baseColorFactor = baseColorFactor; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5138 | } |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5139 | |
| 5140 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5141 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5142 | if (detail::FindMember(o, "baseColorTexture", it)) { |
| 5143 | ParseTextureInfo(&pbr->baseColorTexture, err, detail::GetValue(it), |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5144 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5145 | } |
| 5146 | } |
| 5147 | |
| 5148 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5149 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5150 | if (detail::FindMember(o, "metallicRoughnessTexture", it)) { |
| 5151 | ParseTextureInfo(&pbr->metallicRoughnessTexture, err, detail::GetValue(it), |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5152 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5153 | } |
| 5154 | } |
| 5155 | |
| 5156 | ParseNumberProperty(&pbr->metallicFactor, err, o, "metallicFactor", false); |
| 5157 | ParseNumberProperty(&pbr->roughnessFactor, err, o, "roughnessFactor", false); |
| 5158 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 5159 | ParseExtrasAndExtensions(pbr, err, o, store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5160 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5161 | return true; |
| 5162 | } |
| 5163 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5164 | static bool ParseMaterial(Material *material, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5165 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5166 | ParseStringProperty(&material->name, err, o, "name", /* required */ false); |
| 5167 | |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 5168 | if (ParseNumberArrayProperty(&material->emissiveFactor, err, o, |
| 5169 | "emissiveFactor", |
| 5170 | /* required */ false)) { |
Selmar Kok | 6df800d | 2019-08-19 11:05:28 +0200 | [diff] [blame] | 5171 | if (material->emissiveFactor.size() != 3) { |
| 5172 | if (err) { |
| 5173 | (*err) += |
| 5174 | "Array length of `emissiveFactor` parameter in " |
| 5175 | "material must be 3, but got " + |
| 5176 | std::to_string(material->emissiveFactor.size()) + "\n"; |
| 5177 | } |
| 5178 | return false; |
| 5179 | } |
Syoyo Fujita | ff51570 | 2019-08-24 16:29:14 +0900 | [diff] [blame] | 5180 | } else { |
Selmar Kok | 6df800d | 2019-08-19 11:05:28 +0200 | [diff] [blame] | 5181 | // fill with default values |
Selmar Kok | 6dba6c6 | 2019-08-19 11:23:31 +0200 | [diff] [blame] | 5182 | material->emissiveFactor = {0.0, 0.0, 0.0}; |
Selmar Kok | 6df800d | 2019-08-19 11:05:28 +0200 | [diff] [blame] | 5183 | } |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5184 | |
| 5185 | ParseStringProperty(&material->alphaMode, err, o, "alphaMode", |
| 5186 | /* required */ false); |
| 5187 | ParseNumberProperty(&material->alphaCutoff, err, o, "alphaCutoff", |
| 5188 | /* required */ false); |
| 5189 | ParseBooleanProperty(&material->doubleSided, err, o, "doubleSided", |
| 5190 | /* required */ false); |
| 5191 | |
| 5192 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5193 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5194 | if (detail::FindMember(o, "pbrMetallicRoughness", it)) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5195 | ParsePbrMetallicRoughness(&material->pbrMetallicRoughness, err, |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5196 | detail::GetValue(it), |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5197 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5198 | } |
| 5199 | } |
| 5200 | |
| 5201 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5202 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5203 | if (detail::FindMember(o, "normalTexture", it)) { |
| 5204 | ParseNormalTextureInfo(&material->normalTexture, err, detail::GetValue(it), |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5205 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5206 | } |
| 5207 | } |
| 5208 | |
| 5209 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5210 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5211 | if (detail::FindMember(o, "occlusionTexture", it)) { |
| 5212 | ParseOcclusionTextureInfo(&material->occlusionTexture, err, detail::GetValue(it), |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5213 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5214 | } |
| 5215 | } |
| 5216 | |
| 5217 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5218 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5219 | if (detail::FindMember(o, "emissiveTexture", it)) { |
| 5220 | ParseTextureInfo(&material->emissiveTexture, err, detail::GetValue(it), |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5221 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5222 | } |
| 5223 | } |
| 5224 | |
| 5225 | // Old code path. For backward compatibility, we still store material values |
| 5226 | // as Parameter. This will create duplicated information for |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 5227 | // example(pbrMetallicRoughness), but should be negligible in terms of memory |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 5228 | // consumption. |
| 5229 | // TODO(syoyo): Remove in the next major release. |
| 5230 | material->values.clear(); |
| 5231 | material->additionalValues.clear(); |
Luke San Antonio | cdf4cb7 | 2016-06-14 21:32:11 -0400 | [diff] [blame] | 5232 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5233 | detail::json_const_iterator it(detail::ObjectBegin(o)); |
| 5234 | detail::json_const_iterator itEnd(detail::ObjectEnd(o)); |
Luke San Antonio | cdf4cb7 | 2016-06-14 21:32:11 -0400 | [diff] [blame] | 5235 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5236 | for (; it != itEnd; ++it) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5237 | std::string key(detail::GetKey(it)); |
jrkoonce | 06c30c4 | 2019-09-03 15:56:48 -0500 | [diff] [blame] | 5238 | if (key == "pbrMetallicRoughness") { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5239 | if (detail::IsObject(detail::GetValue(it))) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5240 | const detail::json &values_object = detail::GetValue(it); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5241 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5242 | detail::json_const_iterator itVal(detail::ObjectBegin(values_object)); |
| 5243 | detail::json_const_iterator itValEnd(detail::ObjectEnd(values_object)); |
jrkoonce | 06c30c4 | 2019-09-03 15:56:48 -0500 | [diff] [blame] | 5244 | |
| 5245 | for (; itVal != itValEnd; ++itVal) { |
| 5246 | Parameter param; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5247 | if (ParseParameterProperty(¶m, err, values_object, detail::GetKey(itVal), |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5248 | false)) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5249 | material->values.emplace(detail::GetKey(itVal), std::move(param)); |
jrkoonce | 06c30c4 | 2019-09-03 15:56:48 -0500 | [diff] [blame] | 5250 | } |
| 5251 | } |
| 5252 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5253 | } else if (key == "extensions" || key == "extras") { |
| 5254 | // done later, skip, otherwise poorly parsed contents will be saved in the |
| 5255 | // parametermap and serialized again later |
| 5256 | } else { |
jrkoonce | 06c30c4 | 2019-09-03 15:56:48 -0500 | [diff] [blame] | 5257 | Parameter param; |
| 5258 | if (ParseParameterProperty(¶m, err, o, key, false)) { |
| 5259 | // names of materials have already been parsed. Putting it in this map |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 5260 | // doesn't correctly reflect the glTF specification |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5261 | if (key != "name") |
| 5262 | material->additionalValues.emplace(std::move(key), std::move(param)); |
jrkoonce | 06c30c4 | 2019-09-03 15:56:48 -0500 | [diff] [blame] | 5263 | } |
Aurélien Chatelain | ab7b218 | 2017-05-24 09:53:44 +0000 | [diff] [blame] | 5264 | } |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 5265 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5266 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 5267 | material->extensions.clear(); // Note(agnat): Why? |
| 5268 | ParseExtrasAndExtensions(material, err, o, store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5269 | |
Luke San Antonio | cdf4cb7 | 2016-06-14 21:32:11 -0400 | [diff] [blame] | 5270 | return true; |
| 5271 | } |
| 5272 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5273 | static bool ParseAnimationChannel( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5274 | AnimationChannel *channel, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5275 | bool store_original_json_for_extras_and_extensions) { |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5276 | int samplerIndex = -1; |
| 5277 | int targetIndex = -1; |
| 5278 | if (!ParseIntegerProperty(&samplerIndex, err, o, "sampler", true, |
| 5279 | "AnimationChannel")) { |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5280 | if (err) { |
| 5281 | (*err) += "`sampler` field is missing in animation channels\n"; |
| 5282 | } |
| 5283 | return false; |
| 5284 | } |
| 5285 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5286 | detail::json_const_iterator targetIt; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5287 | if (detail::FindMember(o, "target", targetIt) && detail::IsObject(detail::GetValue(targetIt))) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5288 | const detail::json &target_object = detail::GetValue(targetIt); |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5289 | |
Jack Mousseau | 283b552 | 2023-01-15 11:45:45 -0800 | [diff] [blame] | 5290 | ParseIntegerProperty(&targetIndex, err, target_object, "node", false); |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5291 | |
| 5292 | if (!ParseStringProperty(&channel->target_path, err, target_object, "path", |
| 5293 | true)) { |
| 5294 | if (err) { |
| 5295 | (*err) += "`path` field is missing in animation.channels.target\n"; |
| 5296 | } |
| 5297 | return false; |
| 5298 | } |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 5299 | ParseExtensionsProperty(&channel->target_extensions, err, target_object); |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 5300 | ParseExtrasProperty(&channel->target_extras, target_object); |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 5301 | if (store_original_json_for_extras_and_extensions) { |
David Siegel | d852f50 | 2023-06-05 23:28:05 +0200 | [diff] [blame] | 5302 | { |
| 5303 | detail::json_const_iterator it; |
| 5304 | if (detail::FindMember(target_object, "extensions", it)) { |
| 5305 | channel->target_extensions_json_string = detail::JsonToString(detail::GetValue(it)); |
| 5306 | } |
| 5307 | } |
| 5308 | { |
| 5309 | detail::json_const_iterator it; |
| 5310 | if (detail::FindMember(target_object, "extras", it)) { |
| 5311 | channel->target_extras_json_string = detail::JsonToString(detail::GetValue(it)); |
| 5312 | } |
Selmar Kok | 973d9b3 | 2020-01-21 18:45:24 +0100 | [diff] [blame] | 5313 | } |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 5314 | } |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5315 | } |
| 5316 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5317 | channel->sampler = samplerIndex; |
| 5318 | channel->target_node = targetIndex; |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 5319 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 5320 | ParseExtrasAndExtensions(channel, err, o, store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5321 | |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5322 | return true; |
| 5323 | } |
| 5324 | |
| 5325 | static bool ParseAnimation(Animation *animation, std::string *err, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5326 | const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5327 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5328 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5329 | detail::json_const_iterator channelsIt; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5330 | if (detail::FindMember(o, "channels", channelsIt) && |
| 5331 | detail::IsArray(detail::GetValue(channelsIt))) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5332 | detail::json_const_array_iterator channelEnd = detail::ArrayEnd(detail::GetValue(channelsIt)); |
| 5333 | for (detail::json_const_array_iterator i = detail::ArrayBegin(detail::GetValue(channelsIt)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5334 | i != channelEnd; ++i) { |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5335 | AnimationChannel channel; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5336 | if (ParseAnimationChannel( |
| 5337 | &channel, err, *i, |
| 5338 | store_original_json_for_extras_and_extensions)) { |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5339 | // Only add the channel if the parsing succeeds. |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5340 | animation->channels.emplace_back(std::move(channel)); |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5341 | } |
| 5342 | } |
| 5343 | } |
| 5344 | } |
| 5345 | |
| 5346 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5347 | detail::json_const_iterator samplerIt; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5348 | if (detail::FindMember(o, "samplers", samplerIt) && detail::IsArray(detail::GetValue(samplerIt))) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5349 | const detail::json &sampler_array = detail::GetValue(samplerIt); |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5350 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5351 | detail::json_const_array_iterator it = detail::ArrayBegin(sampler_array); |
| 5352 | detail::json_const_array_iterator itEnd = detail::ArrayEnd(sampler_array); |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5353 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5354 | for (; it != itEnd; ++it) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5355 | const detail::json &s = *it; |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5356 | |
| 5357 | AnimationSampler sampler; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5358 | int inputIndex = -1; |
| 5359 | int outputIndex = -1; |
| 5360 | if (!ParseIntegerProperty(&inputIndex, err, s, "input", true)) { |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5361 | if (err) { |
| 5362 | (*err) += "`input` field is missing in animation.sampler\n"; |
| 5363 | } |
| 5364 | return false; |
| 5365 | } |
Evan Birenbaum | 6bdffed | 2019-02-14 13:30:57 -0800 | [diff] [blame] | 5366 | ParseStringProperty(&sampler.interpolation, err, s, "interpolation", |
| 5367 | false); |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5368 | if (!ParseIntegerProperty(&outputIndex, err, s, "output", true)) { |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5369 | if (err) { |
| 5370 | (*err) += "`output` field is missing in animation.sampler\n"; |
| 5371 | } |
| 5372 | return false; |
| 5373 | } |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5374 | sampler.input = inputIndex; |
| 5375 | sampler.output = outputIndex; |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 5376 | ParseExtrasAndExtensions(&sampler, err, o, |
| 5377 | store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5378 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5379 | animation->samplers.emplace_back(std::move(sampler)); |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5380 | } |
| 5381 | } |
| 5382 | } |
| 5383 | |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5384 | ParseStringProperty(&animation->name, err, o, "name", false); |
| 5385 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 5386 | ParseExtrasAndExtensions(animation, err, o, store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5387 | |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5388 | return true; |
| 5389 | } |
| 5390 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5391 | static bool ParseSampler(Sampler *sampler, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5392 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 5393 | ParseStringProperty(&sampler->name, err, o, "name", false); |
| 5394 | |
Syoyo Fujita | ee179b2 | 2019-08-16 13:11:30 +0900 | [diff] [blame] | 5395 | int minFilter = -1; |
| 5396 | int magFilter = -1; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5397 | int wrapS = TINYGLTF_TEXTURE_WRAP_REPEAT; |
| 5398 | int wrapT = TINYGLTF_TEXTURE_WRAP_REPEAT; |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 5399 | // int wrapR = TINYGLTF_TEXTURE_WRAP_REPEAT; |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5400 | ParseIntegerProperty(&minFilter, err, o, "minFilter", false); |
| 5401 | ParseIntegerProperty(&magFilter, err, o, "magFilter", false); |
| 5402 | ParseIntegerProperty(&wrapS, err, o, "wrapS", false); |
| 5403 | ParseIntegerProperty(&wrapT, err, o, "wrapT", false); |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 5404 | // ParseIntegerProperty(&wrapR, err, o, "wrapR", false); // tinygltf |
| 5405 | // extension |
Syoyo Fujita | ee179b2 | 2019-08-16 13:11:30 +0900 | [diff] [blame] | 5406 | |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 5407 | // TODO(syoyo): Check the value is allowed one. |
Syoyo Fujita | ee179b2 | 2019-08-16 13:11:30 +0900 | [diff] [blame] | 5408 | // (e.g. we allow 9728(NEAREST), but don't allow 9727) |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 5409 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5410 | sampler->minFilter = minFilter; |
| 5411 | sampler->magFilter = magFilter; |
| 5412 | sampler->wrapS = wrapS; |
| 5413 | sampler->wrapT = wrapT; |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 5414 | // sampler->wrapR = wrapR; |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 5415 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 5416 | ParseExtrasAndExtensions(sampler, err, o, store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5417 | |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 5418 | return true; |
| 5419 | } |
| 5420 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5421 | static bool ParseSkin(Skin *skin, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5422 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 90e1ed2 | 2017-05-30 00:47:37 +0900 | [diff] [blame] | 5423 | ParseStringProperty(&skin->name, err, o, "name", false, "Skin"); |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 5424 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5425 | std::vector<int> joints; |
| 5426 | if (!ParseIntegerArrayProperty(&joints, err, o, "joints", false, "Skin")) { |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 5427 | return false; |
| 5428 | } |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5429 | skin->joints = std::move(joints); |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 5430 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5431 | int skeleton = -1; |
| 5432 | ParseIntegerProperty(&skeleton, err, o, "skeleton", false, "Skin"); |
| 5433 | skin->skeleton = skeleton; |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 5434 | |
Jeff McGlynn | 19b806e | 2019-04-26 14:49:38 -0700 | [diff] [blame] | 5435 | int invBind = -1; |
| 5436 | ParseIntegerProperty(&invBind, err, o, "inverseBindMatrices", true, "Skin"); |
| 5437 | skin->inverseBindMatrices = invBind; |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 5438 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 5439 | ParseExtrasAndExtensions(skin, err, o, store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5440 | |
Aurélien Chatelain | 8cb9895 | 2017-05-24 12:55:26 +0000 | [diff] [blame] | 5441 | return true; |
| 5442 | } |
| 5443 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5444 | static bool ParsePerspectiveCamera( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5445 | PerspectiveCamera *camera, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5446 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5447 | double yfov = 0.0; |
| 5448 | if (!ParseNumberProperty(&yfov, err, o, "yfov", true, "OrthographicCamera")) { |
| 5449 | return false; |
| 5450 | } |
| 5451 | |
| 5452 | double znear = 0.0; |
| 5453 | if (!ParseNumberProperty(&znear, err, o, "znear", true, |
| 5454 | "PerspectiveCamera")) { |
| 5455 | return false; |
| 5456 | } |
| 5457 | |
| 5458 | double aspectRatio = 0.0; // = invalid |
| 5459 | ParseNumberProperty(&aspectRatio, err, o, "aspectRatio", false, |
| 5460 | "PerspectiveCamera"); |
| 5461 | |
| 5462 | double zfar = 0.0; // = invalid |
| 5463 | ParseNumberProperty(&zfar, err, o, "zfar", false, "PerspectiveCamera"); |
| 5464 | |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 5465 | camera->aspectRatio = aspectRatio; |
| 5466 | camera->zfar = zfar; |
| 5467 | camera->yfov = yfov; |
| 5468 | camera->znear = znear; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5469 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 5470 | ParseExtrasAndExtensions(camera, err, o, store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5471 | |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5472 | // TODO(syoyo): Validate parameter values. |
| 5473 | |
| 5474 | return true; |
| 5475 | } |
| 5476 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5477 | static bool ParseSpotLight(SpotLight *light, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5478 | bool store_original_json_for_extras_and_extensions) { |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5479 | ParseNumberProperty(&light->innerConeAngle, err, o, "innerConeAngle", false); |
| 5480 | ParseNumberProperty(&light->outerConeAngle, err, o, "outerConeAngle", false); |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 5481 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 5482 | ParseExtrasAndExtensions(light, err, o, store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5483 | |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5484 | // TODO(syoyo): Validate parameter values. |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 5485 | |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5486 | return true; |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 5487 | } |
| 5488 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5489 | static bool ParseOrthographicCamera( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5490 | OrthographicCamera *camera, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5491 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5492 | double xmag = 0.0; |
| 5493 | if (!ParseNumberProperty(&xmag, err, o, "xmag", true, "OrthographicCamera")) { |
| 5494 | return false; |
| 5495 | } |
| 5496 | |
| 5497 | double ymag = 0.0; |
| 5498 | if (!ParseNumberProperty(&ymag, err, o, "ymag", true, "OrthographicCamera")) { |
| 5499 | return false; |
| 5500 | } |
| 5501 | |
| 5502 | double zfar = 0.0; |
| 5503 | if (!ParseNumberProperty(&zfar, err, o, "zfar", true, "OrthographicCamera")) { |
| 5504 | return false; |
| 5505 | } |
| 5506 | |
| 5507 | double znear = 0.0; |
| 5508 | if (!ParseNumberProperty(&znear, err, o, "znear", true, |
| 5509 | "OrthographicCamera")) { |
| 5510 | return false; |
| 5511 | } |
| 5512 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 5513 | ParseExtrasAndExtensions(camera, err, o, store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5514 | |
Selmar Kok | 31cb7f9 | 2018-10-03 15:39:05 +0200 | [diff] [blame] | 5515 | camera->xmag = xmag; |
| 5516 | camera->ymag = ymag; |
| 5517 | camera->zfar = zfar; |
| 5518 | camera->znear = znear; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5519 | |
| 5520 | // TODO(syoyo): Validate parameter values. |
| 5521 | |
| 5522 | return true; |
| 5523 | } |
| 5524 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5525 | static bool ParseCamera(Camera *camera, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5526 | bool store_original_json_for_extras_and_extensions) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5527 | if (!ParseStringProperty(&camera->type, err, o, "type", true, "Camera")) { |
| 5528 | return false; |
| 5529 | } |
| 5530 | |
| 5531 | if (camera->type.compare("orthographic") == 0) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5532 | detail::json_const_iterator orthoIt; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5533 | if (!detail::FindMember(o, "orthographic", orthoIt)) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5534 | if (err) { |
| 5535 | std::stringstream ss; |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 5536 | ss << "Orthographic camera description not found." << std::endl; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5537 | (*err) += ss.str(); |
| 5538 | } |
| 5539 | return false; |
| 5540 | } |
| 5541 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5542 | const detail::json &v = detail::GetValue(orthoIt); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5543 | if (!detail::IsObject(v)) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5544 | if (err) { |
| 5545 | std::stringstream ss; |
| 5546 | ss << "\"orthographic\" is not a JSON object." << std::endl; |
| 5547 | (*err) += ss.str(); |
| 5548 | } |
| 5549 | return false; |
| 5550 | } |
| 5551 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5552 | if (!ParseOrthographicCamera( |
| 5553 | &camera->orthographic, err, v, |
| 5554 | store_original_json_for_extras_and_extensions)) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5555 | return false; |
| 5556 | } |
| 5557 | } else if (camera->type.compare("perspective") == 0) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5558 | detail::json_const_iterator perspIt; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5559 | if (!detail::FindMember(o, "perspective", perspIt)) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5560 | if (err) { |
| 5561 | std::stringstream ss; |
| 5562 | ss << "Perspective camera description not found." << std::endl; |
| 5563 | (*err) += ss.str(); |
| 5564 | } |
| 5565 | return false; |
| 5566 | } |
| 5567 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5568 | const detail::json &v = detail::GetValue(perspIt); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5569 | if (!detail::IsObject(v)) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5570 | if (err) { |
| 5571 | std::stringstream ss; |
| 5572 | ss << "\"perspective\" is not a JSON object." << std::endl; |
| 5573 | (*err) += ss.str(); |
| 5574 | } |
| 5575 | return false; |
| 5576 | } |
| 5577 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5578 | if (!ParsePerspectiveCamera( |
| 5579 | &camera->perspective, err, v, |
| 5580 | store_original_json_for_extras_and_extensions)) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5581 | return false; |
| 5582 | } |
| 5583 | } else { |
| 5584 | if (err) { |
| 5585 | std::stringstream ss; |
| 5586 | ss << "Invalid camera type: \"" << camera->type |
| 5587 | << "\". Must be \"perspective\" or \"orthographic\"" << std::endl; |
| 5588 | (*err) += ss.str(); |
| 5589 | } |
| 5590 | return false; |
| 5591 | } |
| 5592 | |
| 5593 | ParseStringProperty(&camera->name, err, o, "name", false); |
| 5594 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 5595 | ParseExtrasAndExtensions(camera, err, o, store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5596 | |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5597 | return true; |
| 5598 | } |
| 5599 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5600 | static bool ParseLight(Light *light, std::string *err, const detail::json &o, |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5601 | bool store_original_json_for_extras_and_extensions) { |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5602 | if (!ParseStringProperty(&light->type, err, o, "type", true)) { |
| 5603 | return false; |
| 5604 | } |
| 5605 | |
| 5606 | if (light->type == "spot") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5607 | detail::json_const_iterator spotIt; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5608 | if (!detail::FindMember(o, "spot", spotIt)) { |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5609 | if (err) { |
| 5610 | std::stringstream ss; |
| 5611 | ss << "Spot light description not found." << std::endl; |
| 5612 | (*err) += ss.str(); |
| 5613 | } |
| 5614 | return false; |
Benjamin Schmithüsen | 4557b6a | 2019-07-09 16:55:55 +0200 | [diff] [blame] | 5615 | } |
| 5616 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5617 | const detail::json &v = detail::GetValue(spotIt); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5618 | if (!detail::IsObject(v)) { |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5619 | if (err) { |
| 5620 | std::stringstream ss; |
| 5621 | ss << "\"spot\" is not a JSON object." << std::endl; |
| 5622 | (*err) += ss.str(); |
| 5623 | } |
| 5624 | return false; |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 5625 | } |
| 5626 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5627 | if (!ParseSpotLight(&light->spot, err, v, |
| 5628 | store_original_json_for_extras_and_extensions)) { |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5629 | return false; |
| 5630 | } |
| 5631 | } |
| 5632 | |
| 5633 | ParseStringProperty(&light->name, err, o, "name", false); |
| 5634 | ParseNumberArrayProperty(&light->color, err, o, "color", false); |
| 5635 | ParseNumberProperty(&light->range, err, o, "range", false); |
| 5636 | ParseNumberProperty(&light->intensity, err, o, "intensity", false); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5637 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 5638 | ParseExtrasAndExtensions(light, err, o, store_original_json_for_extras_and_extensions); |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5639 | |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 5640 | return true; |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 5641 | } |
| 5642 | |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 5643 | static bool ParsePositionalEmitter( |
| 5644 | PositionalEmitter *positional, std::string *err, const detail::json &o, |
| 5645 | bool store_original_json_for_extras_and_extensions) { |
| 5646 | ParseNumberProperty(&positional->coneInnerAngle, err, o, "coneInnerAngle", false); |
| 5647 | ParseNumberProperty(&positional->coneOuterAngle, err, o, "coneOuterAngle", false); |
| 5648 | ParseNumberProperty(&positional->coneOuterGain, err, o, "coneOuterGain", false); |
| 5649 | ParseNumberProperty(&positional->maxDistance, err, o, "maxDistance", false); |
| 5650 | ParseNumberProperty(&positional->refDistance, err, o, "refDistance", false); |
| 5651 | ParseNumberProperty(&positional->rolloffFactor, err, o, "rolloffFactor", false); |
| 5652 | |
| 5653 | ParseExtrasAndExtensions(positional, err, o, store_original_json_for_extras_and_extensions); |
| 5654 | |
| 5655 | return true; |
| 5656 | } |
| 5657 | |
| 5658 | static bool ParseAudioEmitter( |
| 5659 | AudioEmitter *emitter, std::string *err, const detail::json &o, |
| 5660 | bool store_original_json_for_extras_and_extensions) { |
| 5661 | if (!ParseStringProperty(&emitter->type, err, o, "type", true)) { |
| 5662 | return false; |
| 5663 | } |
| 5664 | |
| 5665 | if (emitter->type == "positional") { |
| 5666 | detail::json_const_iterator positionalIt; |
| 5667 | if (!detail::FindMember(o, "positional", positionalIt)) { |
| 5668 | if (err) { |
| 5669 | std::stringstream ss; |
| 5670 | ss << "Positional emitter description not found." << std::endl; |
| 5671 | (*err) += ss.str(); |
| 5672 | } |
| 5673 | return false; |
| 5674 | } |
| 5675 | |
| 5676 | const detail::json &v = detail::GetValue(positionalIt); |
| 5677 | if (!detail::IsObject(v)) { |
| 5678 | if (err) { |
| 5679 | std::stringstream ss; |
| 5680 | ss << "\"positional\" is not a JSON object." << std::endl; |
| 5681 | (*err) += ss.str(); |
| 5682 | } |
| 5683 | return false; |
| 5684 | } |
| 5685 | |
| 5686 | if (!ParsePositionalEmitter(&emitter->positional, err, v, |
| 5687 | store_original_json_for_extras_and_extensions)) { |
| 5688 | return false; |
| 5689 | } |
| 5690 | } |
| 5691 | |
| 5692 | ParseStringProperty(&emitter->name, err, o, "name", false); |
| 5693 | ParseNumberProperty(&emitter->gain, err, o, "gain", false); |
| 5694 | ParseBooleanProperty(&emitter->loop, err, o, "loop", false); |
| 5695 | ParseBooleanProperty(&emitter->playing, err, o, "playing", false); |
| 5696 | ParseStringProperty(&emitter->distanceModel, err, o, "distanceModel", false); |
| 5697 | ParseIntegerProperty(&emitter->source, err, o, "source", true); |
| 5698 | |
| 5699 | ParseExtrasAndExtensions(emitter, err, o, |
| 5700 | store_original_json_for_extras_and_extensions); |
| 5701 | |
| 5702 | return true; |
| 5703 | } |
| 5704 | |
| 5705 | static bool ParseAudioSource( |
| 5706 | AudioSource *source, std::string *err, const detail::json &o, |
| 5707 | bool store_original_json_for_extras_and_extensions) { |
| 5708 | ParseStringProperty(&source->name, err, o, "name", false); |
| 5709 | ParseStringProperty(&source->uri, err, o, "uri", false); |
| 5710 | |
| 5711 | if (source->uri.empty()) { |
| 5712 | ParseIntegerProperty(&source->bufferView, err, o, "bufferView", true); |
| 5713 | ParseStringProperty(&source->mimeType, err, o, "mimeType", true); |
| 5714 | } |
| 5715 | |
| 5716 | ParseExtrasAndExtensions(source, err, o, |
| 5717 | store_original_json_for_extras_and_extensions); |
| 5718 | |
| 5719 | return true; |
| 5720 | } |
| 5721 | |
David Siegel | bec8a6d | 2023-06-06 15:36:07 +0200 | [diff] [blame] | 5722 | namespace detail { |
| 5723 | |
| 5724 | template <typename Callback> |
| 5725 | bool ForEachInArray(const detail::json &_v, const char *member, Callback && cb) { |
| 5726 | detail::json_const_iterator itm; |
| 5727 | if (detail::FindMember(_v, member, itm) && detail::IsArray(detail::GetValue(itm))) { |
| 5728 | const detail::json &root = detail::GetValue(itm); |
| 5729 | auto it = detail::ArrayBegin(root); |
| 5730 | auto end = detail::ArrayEnd(root); |
| 5731 | for (; it != end; ++it) { |
| 5732 | if (!cb(*it)) return false; |
| 5733 | } |
| 5734 | } |
| 5735 | return true; |
| 5736 | }; |
| 5737 | |
| 5738 | } // end of namespace detail |
| 5739 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 5740 | bool TinyGLTF::LoadFromString(Model *model, std::string *err, std::string *warn, |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5741 | const char *json_str, |
| 5742 | unsigned int json_str_length, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 5743 | const std::string &base_dir, |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 5744 | unsigned int check_sections) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5745 | if (json_str_length < 4) { |
Syoyo Fujita | 2b0307f | 2017-08-01 18:15:52 +0900 | [diff] [blame] | 5746 | if (err) { |
| 5747 | (*err) = "JSON string too short.\n"; |
| 5748 | } |
| 5749 | return false; |
| 5750 | } |
| 5751 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5752 | detail::JsonDocument v; |
Syoyo Fujita | 17cfbcc | 2018-01-05 20:13:50 +0900 | [diff] [blame] | 5753 | |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 5754 | #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || \ |
| 5755 | defined(_CPPUNWIND)) && \ |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 5756 | !defined(TINYGLTF_NOEXCEPTION) |
Syoyo Fujita | cdf8490 | 2017-08-01 20:12:08 +0900 | [diff] [blame] | 5757 | try { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5758 | detail::JsonParse(v, json_str, json_str_length, true); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5759 | |
Syoyo Fujita | 17cfbcc | 2018-01-05 20:13:50 +0900 | [diff] [blame] | 5760 | } catch (const std::exception &e) { |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5761 | if (err) { |
Syoyo Fujita | cdf8490 | 2017-08-01 20:12:08 +0900 | [diff] [blame] | 5762 | (*err) = e.what(); |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5763 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5764 | return false; |
| 5765 | } |
Syoyo Fujita | 17cfbcc | 2018-01-05 20:13:50 +0900 | [diff] [blame] | 5766 | #else |
| 5767 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5768 | detail::JsonParse(v, json_str, json_str_length); |
Syoyo Fujita | 17cfbcc | 2018-01-05 20:13:50 +0900 | [diff] [blame] | 5769 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5770 | if (!detail::IsObject(v)) { |
Syoyo Fujita | 17cfbcc | 2018-01-05 20:13:50 +0900 | [diff] [blame] | 5771 | // Assume parsing was failed. |
| 5772 | if (err) { |
| 5773 | (*err) = "Failed to parse JSON object\n"; |
| 5774 | } |
| 5775 | return false; |
| 5776 | } |
| 5777 | } |
| 5778 | #endif |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5779 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5780 | if (!detail::IsObject(v)) { |
Syoyo Fujita | 2b0307f | 2017-08-01 18:15:52 +0900 | [diff] [blame] | 5781 | // root is not an object. |
| 5782 | if (err) { |
| 5783 | (*err) = "Root element is not a JSON object\n"; |
| 5784 | } |
| 5785 | return false; |
| 5786 | } |
| 5787 | |
Benjamin Schmithüsen | d02ad0d | 2019-05-02 14:44:20 +0200 | [diff] [blame] | 5788 | { |
| 5789 | bool version_found = false; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5790 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5791 | if (detail::FindMember(v, "asset", it) && detail::IsObject(detail::GetValue(it))) { |
| 5792 | auto &itObj = detail::GetValue(it); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5793 | detail::json_const_iterator version_it; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5794 | std::string versionStr; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5795 | if (detail::FindMember(itObj, "version", version_it) && |
| 5796 | detail::GetString(detail::GetValue(version_it), versionStr)) { |
Benjamin Schmithüsen | d02ad0d | 2019-05-02 14:44:20 +0200 | [diff] [blame] | 5797 | version_found = true; |
| 5798 | } |
| 5799 | } |
| 5800 | if (version_found) { |
| 5801 | // OK |
| 5802 | } else if (check_sections & REQUIRE_VERSION) { |
| 5803 | if (err) { |
| 5804 | (*err) += "\"asset\" object not found in .gltf or not an object type\n"; |
| 5805 | } |
| 5806 | return false; |
| 5807 | } |
| 5808 | } |
| 5809 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 5810 | // scene is not mandatory. |
Syoyo Fujita | 5b40745 | 2017-06-04 17:42:41 +0900 | [diff] [blame] | 5811 | // FIXME Maybe a better way to handle it than removing the code |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5812 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5813 | auto IsArrayMemberPresent = [](const detail::json &_v, const char *name) -> bool { |
| 5814 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5815 | return detail::FindMember(_v, name, it) && detail::IsArray(detail::GetValue(it)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5816 | }; |
| 5817 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5818 | { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5819 | if ((check_sections & REQUIRE_SCENES) && |
| 5820 | !IsArrayMemberPresent(v, "scenes")) { |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5821 | if (err) { |
| 5822 | (*err) += "\"scenes\" object not found in .gltf or not an array type\n"; |
| 5823 | } |
| 5824 | return false; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5825 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5826 | } |
| 5827 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5828 | { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5829 | if ((check_sections & REQUIRE_NODES) && !IsArrayMemberPresent(v, "nodes")) { |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5830 | if (err) { |
| 5831 | (*err) += "\"nodes\" object not found in .gltf\n"; |
| 5832 | } |
| 5833 | return false; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5834 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5835 | } |
| 5836 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5837 | { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5838 | if ((check_sections & REQUIRE_ACCESSORS) && |
| 5839 | !IsArrayMemberPresent(v, "accessors")) { |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5840 | if (err) { |
| 5841 | (*err) += "\"accessors\" object not found in .gltf\n"; |
| 5842 | } |
| 5843 | return false; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5844 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5845 | } |
| 5846 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5847 | { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5848 | if ((check_sections & REQUIRE_BUFFERS) && |
| 5849 | !IsArrayMemberPresent(v, "buffers")) { |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5850 | if (err) { |
| 5851 | (*err) += "\"buffers\" object not found in .gltf\n"; |
| 5852 | } |
| 5853 | return false; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5854 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5855 | } |
| 5856 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5857 | { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5858 | if ((check_sections & REQUIRE_BUFFER_VIEWS) && |
| 5859 | !IsArrayMemberPresent(v, "bufferViews")) { |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5860 | if (err) { |
| 5861 | (*err) += "\"bufferViews\" object not found in .gltf\n"; |
| 5862 | } |
| 5863 | return false; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 5864 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5865 | } |
| 5866 | |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 5867 | model->buffers.clear(); |
| 5868 | model->bufferViews.clear(); |
| 5869 | model->accessors.clear(); |
| 5870 | model->meshes.clear(); |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5871 | model->cameras.clear(); |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 5872 | model->nodes.clear(); |
| 5873 | model->extensionsUsed.clear(); |
Aurélien Chatelain | 756ee6b | 2017-06-19 13:52:49 +0000 | [diff] [blame] | 5874 | model->extensionsRequired.clear(); |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 5875 | model->extensions.clear(); |
Aurélien Chatelain | ee4693e | 2017-05-24 12:33:36 +0000 | [diff] [blame] | 5876 | model->defaultScene = -1; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5877 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5878 | // 1. Parse Asset |
| 5879 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5880 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5881 | if (detail::FindMember(v, "asset", it) && detail::IsObject(detail::GetValue(it))) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5882 | const detail::json &root = detail::GetValue(it); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5883 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5884 | ParseAsset(&model->asset, err, root, |
| 5885 | store_original_json_for_extras_and_extensions_); |
Aurélien Chatelain | 756ee6b | 2017-06-19 13:52:49 +0000 | [diff] [blame] | 5886 | } |
| 5887 | } |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5888 | |
David Siegel | bec8a6d | 2023-06-06 15:36:07 +0200 | [diff] [blame] | 5889 | using detail::ForEachInArray; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5890 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5891 | // 2. Parse extensionUsed |
| 5892 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5893 | ForEachInArray(v, "extensionsUsed", [&](const detail::json &o) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5894 | std::string str; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5895 | detail::GetString(o, str); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5896 | model->extensionsUsed.emplace_back(std::move(str)); |
| 5897 | return true; |
| 5898 | }); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5899 | } |
| 5900 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5901 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5902 | ForEachInArray(v, "extensionsRequired", [&](const detail::json &o) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5903 | std::string str; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5904 | detail::GetString(o, str); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5905 | model->extensionsRequired.emplace_back(std::move(str)); |
| 5906 | return true; |
| 5907 | }); |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5908 | } |
| 5909 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5910 | // 3. Parse Buffer |
| 5911 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5912 | bool success = ForEachInArray(v, "buffers", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5913 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5914 | if (err) { |
| 5915 | (*err) += "`buffers' does not contain an JSON object."; |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 5916 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5917 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5918 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5919 | Buffer buffer; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5920 | if (!ParseBuffer(&buffer, err, o, |
| 5921 | store_original_json_for_extras_and_extensions_, &fs, |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 5922 | &uri_cb, base_dir, max_external_file_size_, is_binary_, bin_data_, bin_size_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5923 | return false; |
| 5924 | } |
| 5925 | |
| 5926 | model->buffers.emplace_back(std::move(buffer)); |
| 5927 | return true; |
| 5928 | }); |
| 5929 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5930 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5931 | return false; |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 5932 | } |
| 5933 | } |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5934 | // 4. Parse BufferView |
| 5935 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5936 | bool success = ForEachInArray(v, "bufferViews", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5937 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5938 | if (err) { |
| 5939 | (*err) += "`bufferViews' does not contain an JSON object."; |
Syoyo Fujita | 59130d1 | 2017-08-01 20:23:11 +0900 | [diff] [blame] | 5940 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5941 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5942 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5943 | BufferView bufferView; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5944 | if (!ParseBufferView(&bufferView, err, o, |
| 5945 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5946 | return false; |
| 5947 | } |
| 5948 | |
| 5949 | model->bufferViews.emplace_back(std::move(bufferView)); |
| 5950 | return true; |
| 5951 | }); |
| 5952 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5953 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5954 | return false; |
Syoyo Fujita | 8c5ab03 | 2016-06-19 18:15:32 +0900 | [diff] [blame] | 5955 | } |
| 5956 | } |
| 5957 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5958 | // 5. Parse Accessor |
| 5959 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5960 | bool success = ForEachInArray(v, "accessors", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5961 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5962 | if (err) { |
| 5963 | (*err) += "`accessors' does not contain an JSON object."; |
Syoyo Fujita | 59130d1 | 2017-08-01 20:23:11 +0900 | [diff] [blame] | 5964 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5965 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5966 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5967 | Accessor accessor; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5968 | if (!ParseAccessor(&accessor, err, o, |
| 5969 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5970 | return false; |
| 5971 | } |
| 5972 | |
| 5973 | model->accessors.emplace_back(std::move(accessor)); |
| 5974 | return true; |
| 5975 | }); |
| 5976 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 5977 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5978 | return false; |
Syoyo Fujita | c261563 | 2016-06-19 21:56:06 +0900 | [diff] [blame] | 5979 | } |
| 5980 | } |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 5981 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5982 | // 6. Parse Mesh |
| 5983 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 5984 | bool success = ForEachInArray(v, "meshes", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 5985 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5986 | if (err) { |
| 5987 | (*err) += "`meshes' does not contain an JSON object."; |
Syoyo Fujita | 59130d1 | 2017-08-01 20:23:11 +0900 | [diff] [blame] | 5988 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5989 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 5990 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5991 | Mesh mesh; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 5992 | if (!ParseMesh(&mesh, model, err, o, |
| 5993 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 5994 | return false; |
| 5995 | } |
| 5996 | |
| 5997 | model->meshes.emplace_back(std::move(mesh)); |
| 5998 | return true; |
| 5999 | }); |
| 6000 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6001 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6002 | return false; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 6003 | } |
| 6004 | } |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 6005 | |
viperscape | 9df0580 | 2018-12-05 14:11:01 -0500 | [diff] [blame] | 6006 | // Assign missing bufferView target types |
| 6007 | // - Look for missing Mesh indices |
Marcin Kacprzak | d09788d | 2020-01-02 13:00:48 +0100 | [diff] [blame] | 6008 | // - Look for missing Mesh attributes |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 6009 | for (auto &mesh : model->meshes) { |
| 6010 | for (auto &primitive : mesh.primitives) { |
| 6011 | if (primitive.indices > |
| 6012 | -1) // has indices from parsing step, must be Element Array Buffer |
viperscape | 9df0580 | 2018-12-05 14:11:01 -0500 | [diff] [blame] | 6013 | { |
Jeff McGlynn | 8915252 | 2019-04-25 16:33:56 -0700 | [diff] [blame] | 6014 | if (size_t(primitive.indices) >= model->accessors.size()) { |
| 6015 | if (err) { |
| 6016 | (*err) += "primitive indices accessor out of bounds"; |
| 6017 | } |
| 6018 | return false; |
| 6019 | } |
| 6020 | |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 6021 | auto bufferView = |
| 6022 | model->accessors[size_t(primitive.indices)].bufferView; |
Jeff McGlynn | 8915252 | 2019-04-25 16:33:56 -0700 | [diff] [blame] | 6023 | if (bufferView < 0 || size_t(bufferView) >= model->bufferViews.size()) { |
| 6024 | if (err) { |
| 6025 | (*err) += "accessor[" + std::to_string(primitive.indices) + |
| 6026 | "] invalid bufferView"; |
| 6027 | } |
| 6028 | return false; |
| 6029 | } |
| 6030 | |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 6031 | model->bufferViews[size_t(bufferView)].target = |
Jeff McGlynn | 8915252 | 2019-04-25 16:33:56 -0700 | [diff] [blame] | 6032 | TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER; |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 6033 | // we could optionally check if accessors' bufferView type is Scalar, as |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 6034 | // it should be |
viperscape | 9df0580 | 2018-12-05 14:11:01 -0500 | [diff] [blame] | 6035 | } |
Marcin Kacprzak | d09788d | 2020-01-02 13:00:48 +0100 | [diff] [blame] | 6036 | |
| 6037 | for (auto &attribute : primitive.attributes) { |
Nirmal Patel | e413216 | 2022-09-06 09:16:31 -0700 | [diff] [blame] | 6038 | const auto accessorsIndex = size_t(attribute.second); |
| 6039 | if (accessorsIndex < model->accessors.size()) { |
| 6040 | const auto bufferView = model->accessors[accessorsIndex].bufferView; |
| 6041 | // bufferView could be null(-1) for sparse morph target |
imallett | 56e1098 | 2022-10-07 10:35:16 -0700 | [diff] [blame] | 6042 | if (bufferView >= 0 && bufferView < (int)model->bufferViews.size()) { |
Nirmal Patel | e413216 | 2022-09-06 09:16:31 -0700 | [diff] [blame] | 6043 | model->bufferViews[size_t(bufferView)].target = |
| 6044 | TINYGLTF_TARGET_ARRAY_BUFFER; |
| 6045 | } |
| 6046 | } |
Marcin Kacprzak | d09788d | 2020-01-02 13:00:48 +0100 | [diff] [blame] | 6047 | } |
Marcin Kacprzak | c3d6716 | 2020-01-22 13:13:35 +0100 | [diff] [blame] | 6048 | |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 6049 | for (auto &target : primitive.targets) { |
| 6050 | for (auto &attribute : target) { |
Nirmal Patel | e413216 | 2022-09-06 09:16:31 -0700 | [diff] [blame] | 6051 | const auto accessorsIndex = size_t(attribute.second); |
| 6052 | if (accessorsIndex < model->accessors.size()) { |
| 6053 | const auto bufferView = model->accessors[accessorsIndex].bufferView; |
| 6054 | // bufferView could be null(-1) for sparse morph target |
imallett | 56e1098 | 2022-10-07 10:35:16 -0700 | [diff] [blame] | 6055 | if (bufferView >= 0 && bufferView < (int)model->bufferViews.size()) { |
Nirmal Patel | e413216 | 2022-09-06 09:16:31 -0700 | [diff] [blame] | 6056 | model->bufferViews[size_t(bufferView)].target = |
| 6057 | TINYGLTF_TARGET_ARRAY_BUFFER; |
| 6058 | } |
Rahul Sheth | 125e4a2 | 2020-07-13 13:56:50 -0400 | [diff] [blame] | 6059 | } |
Marcin Kacprzak | c3d6716 | 2020-01-22 13:13:35 +0100 | [diff] [blame] | 6060 | } |
| 6061 | } |
viperscape | 9df0580 | 2018-12-05 14:11:01 -0500 | [diff] [blame] | 6062 | } |
| 6063 | } |
| 6064 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6065 | // 7. Parse Node |
| 6066 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6067 | bool success = ForEachInArray(v, "nodes", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6068 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6069 | if (err) { |
| 6070 | (*err) += "`nodes' does not contain an JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6071 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6072 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6073 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6074 | Node node; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6075 | if (!ParseNode(&node, err, o, |
| 6076 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6077 | return false; |
| 6078 | } |
| 6079 | |
| 6080 | model->nodes.emplace_back(std::move(node)); |
| 6081 | return true; |
| 6082 | }); |
| 6083 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6084 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6085 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6086 | } |
| 6087 | } |
| 6088 | |
| 6089 | // 8. Parse scenes. |
| 6090 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6091 | bool success = ForEachInArray(v, "scenes", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6092 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6093 | if (err) { |
| 6094 | (*err) += "`scenes' does not contain an JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6095 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6096 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6097 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6098 | |
| 6099 | Scene scene; |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 6100 | if (!ParseScene(&scene, err, o, |
| 6101 | store_original_json_for_extras_and_extensions_)) { |
| 6102 | return false; |
| 6103 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6104 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6105 | model->scenes.emplace_back(std::move(scene)); |
| 6106 | return true; |
| 6107 | }); |
| 6108 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6109 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6110 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6111 | } |
| 6112 | } |
| 6113 | |
| 6114 | // 9. Parse default scenes. |
| 6115 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6116 | detail::json_const_iterator rootIt; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6117 | int iVal; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6118 | if (detail::FindMember(v, "scene", rootIt) && detail::GetInt(detail::GetValue(rootIt), iVal)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6119 | model->defaultScene = iVal; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6120 | } |
| 6121 | } |
| 6122 | |
| 6123 | // 10. Parse Material |
| 6124 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6125 | bool success = ForEachInArray(v, "materials", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6126 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6127 | if (err) { |
| 6128 | (*err) += "`materials' does not contain an JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6129 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6130 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6131 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6132 | Material material; |
| 6133 | ParseStringProperty(&material.name, err, o, "name", false); |
| 6134 | |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6135 | if (!ParseMaterial(&material, err, o, |
| 6136 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6137 | return false; |
| 6138 | } |
| 6139 | |
| 6140 | model->materials.emplace_back(std::move(material)); |
| 6141 | return true; |
| 6142 | }); |
| 6143 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6144 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6145 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6146 | } |
| 6147 | } |
| 6148 | |
| 6149 | // 11. Parse Image |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 6150 | void *load_image_user_data{nullptr}; |
| 6151 | |
| 6152 | LoadImageDataOption load_image_option; |
| 6153 | |
| 6154 | if (user_image_loader_) { |
| 6155 | // Use user supplied pointer |
| 6156 | load_image_user_data = load_image_user_data_; |
| 6157 | } else { |
| 6158 | load_image_option.preserve_channels = preserve_image_channels_; |
| 6159 | load_image_user_data = reinterpret_cast<void *>(&load_image_option); |
| 6160 | } |
| 6161 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6162 | { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6163 | int idx = 0; |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6164 | bool success = ForEachInArray(v, "images", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6165 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6166 | if (err) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6167 | (*err) += "image[" + std::to_string(idx) + "] is not a JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6168 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6169 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6170 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6171 | Image image; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6172 | if (!ParseImage(&image, idx, err, warn, o, |
| 6173 | store_original_json_for_extras_and_extensions_, base_dir, |
Syoyo Fujita | ecfd37d | 2023-04-23 21:31:30 +0900 | [diff] [blame] | 6174 | max_external_file_size_, &fs, &uri_cb, &this->LoadImageData, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 6175 | load_image_user_data)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6176 | return false; |
| 6177 | } |
| 6178 | |
| 6179 | if (image.bufferView != -1) { |
| 6180 | // Load image from the buffer view. |
| 6181 | if (size_t(image.bufferView) >= model->bufferViews.size()) { |
| 6182 | if (err) { |
| 6183 | std::stringstream ss; |
| 6184 | ss << "image[" << idx << "] bufferView \"" << image.bufferView |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6185 | << "\" not found in the scene." << std::endl; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6186 | (*err) += ss.str(); |
| 6187 | } |
| 6188 | return false; |
| 6189 | } |
| 6190 | |
| 6191 | const BufferView &bufferView = |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6192 | model->bufferViews[size_t(image.bufferView)]; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6193 | if (size_t(bufferView.buffer) >= model->buffers.size()) { |
| 6194 | if (err) { |
| 6195 | std::stringstream ss; |
| 6196 | ss << "image[" << idx << "] buffer \"" << bufferView.buffer |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6197 | << "\" not found in the scene." << std::endl; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6198 | (*err) += ss.str(); |
| 6199 | } |
| 6200 | return false; |
| 6201 | } |
| 6202 | const Buffer &buffer = model->buffers[size_t(bufferView.buffer)]; |
| 6203 | |
| 6204 | if (*LoadImageData == nullptr) { |
| 6205 | if (err) { |
| 6206 | (*err) += "No LoadImageData callback specified.\n"; |
| 6207 | } |
| 6208 | return false; |
| 6209 | } |
| 6210 | bool ret = LoadImageData( |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6211 | &image, idx, err, warn, image.width, image.height, |
| 6212 | &buffer.data[bufferView.byteOffset], |
Syoyo Fujita | 010ee9c | 2020-10-31 19:35:55 +0900 | [diff] [blame] | 6213 | static_cast<int>(bufferView.byteLength), load_image_user_data); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6214 | if (!ret) { |
| 6215 | return false; |
| 6216 | } |
| 6217 | } |
| 6218 | |
| 6219 | model->images.emplace_back(std::move(image)); |
| 6220 | ++idx; |
| 6221 | return true; |
| 6222 | }); |
| 6223 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6224 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6225 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6226 | } |
| 6227 | } |
| 6228 | |
| 6229 | // 12. Parse Texture |
| 6230 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6231 | bool success = ForEachInArray(v, "textures", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6232 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6233 | if (err) { |
| 6234 | (*err) += "`textures' does not contain an JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6235 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6236 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6237 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6238 | Texture texture; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6239 | if (!ParseTexture(&texture, err, o, |
| 6240 | store_original_json_for_extras_and_extensions_, |
| 6241 | base_dir)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6242 | return false; |
| 6243 | } |
| 6244 | |
| 6245 | model->textures.emplace_back(std::move(texture)); |
| 6246 | return true; |
| 6247 | }); |
| 6248 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6249 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6250 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6251 | } |
| 6252 | } |
| 6253 | |
| 6254 | // 13. Parse Animation |
| 6255 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6256 | bool success = ForEachInArray(v, "animations", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6257 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6258 | if (err) { |
| 6259 | (*err) += "`animations' does not contain an JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6260 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6261 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6262 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6263 | Animation animation; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6264 | if (!ParseAnimation(&animation, err, o, |
| 6265 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6266 | return false; |
| 6267 | } |
| 6268 | |
| 6269 | model->animations.emplace_back(std::move(animation)); |
| 6270 | return true; |
| 6271 | }); |
| 6272 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6273 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6274 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6275 | } |
| 6276 | } |
| 6277 | |
| 6278 | // 14. Parse Skin |
| 6279 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6280 | bool success = ForEachInArray(v, "skins", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6281 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6282 | if (err) { |
| 6283 | (*err) += "`skins' does not contain an JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6284 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6285 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6286 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6287 | Skin skin; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6288 | if (!ParseSkin(&skin, err, o, |
| 6289 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6290 | return false; |
| 6291 | } |
| 6292 | |
| 6293 | model->skins.emplace_back(std::move(skin)); |
| 6294 | return true; |
| 6295 | }); |
| 6296 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6297 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6298 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6299 | } |
| 6300 | } |
| 6301 | |
| 6302 | // 15. Parse Sampler |
| 6303 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6304 | bool success = ForEachInArray(v, "samplers", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6305 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6306 | if (err) { |
| 6307 | (*err) += "`samplers' does not contain an JSON object."; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6308 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6309 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6310 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6311 | Sampler sampler; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6312 | if (!ParseSampler(&sampler, err, o, |
| 6313 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6314 | return false; |
| 6315 | } |
| 6316 | |
| 6317 | model->samplers.emplace_back(std::move(sampler)); |
| 6318 | return true; |
| 6319 | }); |
| 6320 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6321 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6322 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6323 | } |
| 6324 | } |
| 6325 | |
| 6326 | // 16. Parse Camera |
| 6327 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6328 | bool success = ForEachInArray(v, "cameras", [&](const detail::json &o) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6329 | if (!detail::IsObject(o)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6330 | if (err) { |
| 6331 | (*err) += "`cameras' does not contain an JSON object."; |
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 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6334 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6335 | Camera camera; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6336 | if (!ParseCamera(&camera, err, o, |
| 6337 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6338 | return false; |
| 6339 | } |
| 6340 | |
| 6341 | model->cameras.emplace_back(std::move(camera)); |
| 6342 | return true; |
| 6343 | }); |
| 6344 | |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6345 | if (!success) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6346 | return false; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6347 | } |
| 6348 | } |
| 6349 | |
David Siegel | 22cafa1 | 2023-06-05 22:18:59 +0200 | [diff] [blame] | 6350 | // 17. Parse Extras & Extensions |
| 6351 | ParseExtrasAndExtensions(model, err, v, store_original_json_for_extras_and_extensions_); |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 6352 | |
| 6353 | // 18. Specific extension implementations |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6354 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6355 | detail::json_const_iterator rootIt; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6356 | if (detail::FindMember(v, "extensions", rootIt) && detail::IsObject(detail::GetValue(rootIt))) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6357 | const detail::json &root = detail::GetValue(rootIt); |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6358 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6359 | detail::json_const_iterator it(detail::ObjectBegin(root)); |
| 6360 | detail::json_const_iterator itEnd(detail::ObjectEnd(root)); |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6361 | for (; it != itEnd; ++it) { |
Benjamin Schmithüsen | b7ca7c9 | 2019-07-08 18:04:24 +0200 | [diff] [blame] | 6362 | // parse KHR_lights_punctual extension |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6363 | std::string key(detail::GetKey(it)); |
| 6364 | if ((key == "KHR_lights_punctual") && detail::IsObject(detail::GetValue(it))) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6365 | const detail::json &object = detail::GetValue(it); |
| 6366 | detail::json_const_iterator itLight; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6367 | if (detail::FindMember(object, "lights", itLight)) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6368 | const detail::json &lights = detail::GetValue(itLight); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6369 | if (!detail::IsArray(lights)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6370 | continue; |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6371 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6372 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6373 | auto arrayIt(detail::ArrayBegin(lights)); |
| 6374 | auto arrayItEnd(detail::ArrayEnd(lights)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6375 | for (; arrayIt != arrayItEnd; ++arrayIt) { |
| 6376 | Light light; |
Syoyo Fujita | 6e08b17 | 2019-10-30 17:25:38 +0900 | [diff] [blame] | 6377 | if (!ParseLight(&light, err, *arrayIt, |
| 6378 | store_original_json_for_extras_and_extensions_)) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6379 | return false; |
| 6380 | } |
| 6381 | model->lights.emplace_back(std::move(light)); |
| 6382 | } |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6383 | } |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 6384 | } |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 6385 | // parse KHR_audio extension |
| 6386 | if ((key == "KHR_audio") && |
| 6387 | detail::IsObject(detail::GetValue(it))) { |
| 6388 | const detail::json &object = detail::GetValue(it); |
| 6389 | detail::json_const_iterator itKhrAudio; |
| 6390 | if (detail::FindMember(object, "emitters", itKhrAudio)) { |
| 6391 | const detail::json &emitters = detail::GetValue(itKhrAudio); |
| 6392 | if (!detail::IsArray(emitters)) { |
| 6393 | continue; |
| 6394 | } |
| 6395 | |
| 6396 | auto arrayIt(detail::ArrayBegin(emitters)); |
| 6397 | auto arrayItEnd(detail::ArrayEnd(emitters)); |
| 6398 | for (; arrayIt != arrayItEnd; ++arrayIt) { |
| 6399 | AudioEmitter emitter; |
| 6400 | if (!ParseAudioEmitter(&emitter, err, *arrayIt, |
| 6401 | store_original_json_for_extras_and_extensions_)) { |
| 6402 | return false; |
| 6403 | } |
| 6404 | model->audioEmitters.emplace_back(std::move(emitter)); |
| 6405 | } |
| 6406 | } |
| 6407 | |
| 6408 | if (detail::FindMember(object, "sources", itKhrAudio)) { |
| 6409 | const detail::json &sources = detail::GetValue(itKhrAudio); |
| 6410 | if (!detail::IsArray(sources)) { |
| 6411 | continue; |
| 6412 | } |
| 6413 | |
| 6414 | auto arrayIt(detail::ArrayBegin(sources)); |
| 6415 | auto arrayItEnd(detail::ArrayEnd(sources)); |
| 6416 | for (; arrayIt != arrayItEnd; ++arrayIt) { |
| 6417 | AudioSource source; |
| 6418 | if (!ParseAudioSource( |
| 6419 | &source, err, *arrayIt, |
| 6420 | store_original_json_for_extras_and_extensions_)) { |
| 6421 | return false; |
| 6422 | } |
| 6423 | model->audioSources.emplace_back(std::move(source)); |
| 6424 | } |
| 6425 | } |
| 6426 | } |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 6427 | } |
| 6428 | } |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 6429 | } |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6430 | |
Syoyo Fujita | a4d2688 | 2015-12-20 20:27:54 +0900 | [diff] [blame] | 6431 | return true; |
| 6432 | } |
| 6433 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6434 | bool TinyGLTF::LoadASCIIFromString(Model *model, std::string *err, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6435 | std::string *warn, const char *str, |
| 6436 | unsigned int length, |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6437 | const std::string &base_dir, |
| 6438 | unsigned int check_sections) { |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6439 | is_binary_ = false; |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 6440 | bin_data_ = nullptr; |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6441 | bin_size_ = 0; |
| 6442 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6443 | return LoadFromString(model, err, warn, str, length, base_dir, |
| 6444 | check_sections); |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6445 | } |
| 6446 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6447 | bool TinyGLTF::LoadASCIIFromFile(Model *model, std::string *err, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6448 | std::string *warn, const std::string &filename, |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6449 | unsigned int check_sections) { |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 6450 | std::stringstream ss; |
| 6451 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 6452 | if (fs.ReadWholeFile == nullptr) { |
| 6453 | // Programmer error, assert() ? |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6454 | ss << "Failed to read file: " << filename |
| 6455 | << ": one or more FS callback not set" << std::endl; |
Syoyo Fujita | 523c9bf | 2016-04-20 14:06:56 +0900 | [diff] [blame] | 6456 | if (err) { |
| 6457 | (*err) = ss.str(); |
| 6458 | } |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 6459 | return false; |
| 6460 | } |
| 6461 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 6462 | std::vector<unsigned char> data; |
| 6463 | std::string fileerr; |
| 6464 | bool fileread = fs.ReadWholeFile(&data, &fileerr, filename, fs.user_data); |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6465 | if (!fileread) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 6466 | ss << "Failed to read file: " << filename << ": " << fileerr << std::endl; |
| 6467 | if (err) { |
| 6468 | (*err) = ss.str(); |
| 6469 | } |
| 6470 | return false; |
| 6471 | } |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 6472 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 6473 | size_t sz = data.size(); |
Luke San Antonio | b310b4a | 2016-06-23 14:17:37 -0400 | [diff] [blame] | 6474 | if (sz == 0) { |
| 6475 | if (err) { |
| 6476 | (*err) = "Empty file."; |
| 6477 | } |
| 6478 | return false; |
| 6479 | } |
| 6480 | |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 6481 | std::string basedir = GetBaseDir(filename); |
| 6482 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6483 | bool ret = LoadASCIIFromString( |
| 6484 | model, err, warn, reinterpret_cast<const char *>(&data.at(0)), |
| 6485 | static_cast<unsigned int>(data.size()), basedir, check_sections); |
Luke San Antonio | 9e36b61 | 2016-06-23 14:10:51 -0400 | [diff] [blame] | 6486 | |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6487 | return ret; |
| 6488 | } |
| 6489 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6490 | bool TinyGLTF::LoadBinaryFromMemory(Model *model, std::string *err, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6491 | std::string *warn, |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6492 | const unsigned char *bytes, |
| 6493 | unsigned int size, |
| 6494 | const std::string &base_dir, |
| 6495 | unsigned int check_sections) { |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6496 | if (size < 20) { |
| 6497 | if (err) { |
| 6498 | (*err) = "Too short data size for glTF Binary."; |
| 6499 | } |
| 6500 | return false; |
| 6501 | } |
| 6502 | |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 6503 | if (bytes[0] == 'g' && bytes[1] == 'l' && bytes[2] == 'T' && |
| 6504 | bytes[3] == 'F') { |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6505 | // ok |
| 6506 | } else { |
| 6507 | if (err) { |
| 6508 | (*err) = "Invalid magic."; |
| 6509 | } |
| 6510 | return false; |
| 6511 | } |
| 6512 | |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 6513 | unsigned int version; // 4 bytes |
| 6514 | unsigned int length; // 4 bytes |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6515 | unsigned int chunk0_length; // 4 bytes |
| 6516 | unsigned int chunk0_format; // 4 bytes; |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6517 | |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 6518 | memcpy(&version, bytes + 4, 4); |
| 6519 | swap4(&version); |
| 6520 | memcpy(&length, bytes + 8, 4); |
| 6521 | swap4(&length); |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6522 | memcpy(&chunk0_length, bytes + 12, 4); // JSON data length |
| 6523 | swap4(&chunk0_length); |
| 6524 | memcpy(&chunk0_format, bytes + 16, 4); |
| 6525 | swap4(&chunk0_format); |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6526 | |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6527 | // https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#binary-gltf-layout |
| 6528 | // |
Syoyo Fujita | e69069d | 2018-03-15 22:01:18 -0500 | [diff] [blame] | 6529 | // In case the Bin buffer is not present, the size is exactly 20 + size of |
| 6530 | // JSON contents, |
| 6531 | // so use "greater than" operator. |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6532 | // |
| 6533 | // https://github.com/syoyo/tinygltf/issues/372 |
| 6534 | // Use 64bit uint to avoid integer overflow. |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6535 | uint64_t header_and_json_size = 20ull + uint64_t(chunk0_length); |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6536 | |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6537 | if (header_and_json_size > std::numeric_limits<uint32_t>::max()) { |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6538 | // Do not allow 4GB or more GLB data. |
| 6539 | (*err) = "Invalid glTF binary. GLB data exceeds 4GB."; |
| 6540 | } |
| 6541 | |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6542 | if ((header_and_json_size > uint64_t(size)) || (chunk0_length < 1) || (length > size) || |
| 6543 | (header_and_json_size > uint64_t(length)) || |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6544 | (chunk0_format != 0x4E4F534A)) { // 0x4E4F534A = JSON format. |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6545 | if (err) { |
| 6546 | (*err) = "Invalid glTF binary."; |
| 6547 | } |
| 6548 | return false; |
| 6549 | } |
| 6550 | |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6551 | // Padding check |
| 6552 | // The start and the end of each chunk must be aligned to a 4-byte boundary. |
| 6553 | // No padding check for chunk0 start since its 4byte-boundary is ensured. |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6554 | if ((header_and_json_size % 4) != 0) { |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6555 | if (err) { |
| 6556 | (*err) = "JSON Chunk end does not aligned to a 4-byte boundary."; |
| 6557 | } |
| 6558 | } |
| 6559 | |
Syoyo Fujita | 612e578 | 2022-09-18 21:01:39 +0900 | [diff] [blame] | 6560 | //std::cout << "header_and_json_size = " << header_and_json_size << "\n"; |
| 6561 | //std::cout << "length = " << length << "\n"; |
| 6562 | |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6563 | // Chunk1(BIN) data |
| 6564 | // The spec says: When the binary buffer is empty or when it is stored by other means, this chunk SHOULD be omitted. |
Syoyo Fujita | 612e578 | 2022-09-18 21:01:39 +0900 | [diff] [blame] | 6565 | // So when header + JSON data == binary size, Chunk1 is omitted. |
| 6566 | if (header_and_json_size == uint64_t(length)) { |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6567 | |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6568 | bin_data_ = nullptr; |
| 6569 | bin_size_ = 0; |
| 6570 | } else { |
| 6571 | // Read Chunk1 info(BIN data) |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 6572 | // At least Chunk1 should have 12 bytes(8 bytes(header) + 4 bytes(bin payload could be 1~3 bytes, but need to be aligned to 4 bytes) |
Syoyo Fujita | 612e578 | 2022-09-18 21:01:39 +0900 | [diff] [blame] | 6573 | if ((header_and_json_size + 12ull) > uint64_t(length)) { |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6574 | if (err) { |
Syoyo Fujita | 612e578 | 2022-09-18 21:01:39 +0900 | [diff] [blame] | 6575 | (*err) = "Insufficient storage space for Chunk1(BIN data). At least Chunk1 Must have 4 bytes or more bytes, but got " + std::to_string((header_and_json_size + 12ull) - uint64_t(length)) + ".\n"; |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6576 | } |
| 6577 | return false; |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6578 | } |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6579 | |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6580 | unsigned int chunk1_length; // 4 bytes |
| 6581 | unsigned int chunk1_format; // 4 bytes; |
| 6582 | memcpy(&chunk1_length, bytes + header_and_json_size, 4); // JSON data length |
| 6583 | swap4(&chunk1_length); |
| 6584 | memcpy(&chunk1_format, bytes + header_and_json_size + 4, 4); |
| 6585 | swap4(&chunk1_format); |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6586 | |
Syoyo Fujita | 612e578 | 2022-09-18 21:01:39 +0900 | [diff] [blame] | 6587 | //std::cout << "chunk1_length = " << chunk1_length << "\n"; |
| 6588 | |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6589 | if (chunk1_length < 4) { |
| 6590 | if (err) { |
| 6591 | (*err) = "Insufficient Chunk1(BIN) data size."; |
| 6592 | } |
| 6593 | return false; |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6594 | } |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6595 | |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6596 | if ((chunk1_length % 4) != 0) { |
| 6597 | if (err) { |
| 6598 | (*err) = "BIN Chunk end does not aligned to a 4-byte boundary."; |
| 6599 | } |
| 6600 | return false; |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6601 | } |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6602 | |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6603 | if (uint64_t(chunk1_length) + header_and_json_size > uint64_t(length)) { |
| 6604 | if (err) { |
| 6605 | (*err) = "BIN Chunk data length exceeds the GLB size."; |
| 6606 | } |
| 6607 | return false; |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6608 | } |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6609 | |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6610 | if (chunk1_format != 0x004e4942) { |
| 6611 | if (err) { |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 6612 | (*err) = "Invalid type for chunk1 data."; |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6613 | } |
| 6614 | return false; |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6615 | } |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6616 | |
Syoyo Fujita | 612e578 | 2022-09-18 21:01:39 +0900 | [diff] [blame] | 6617 | //std::cout << "chunk1_length = " << chunk1_length << "\n"; |
| 6618 | |
Syoyo Fujita | c670f08 | 2022-09-17 19:52:25 +0900 | [diff] [blame] | 6619 | bin_data_ = bytes + header_and_json_size + |
| 6620 | 8; // 4 bytes (bin_buffer_length) + 4 bytes(bin_buffer_format) |
| 6621 | |
| 6622 | bin_size_ = size_t(chunk1_length); |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6623 | } |
| 6624 | |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6625 | // Extract JSON string. |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 6626 | std::string jsonString(reinterpret_cast<const char *>(&bytes[20]), |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6627 | chunk0_length); |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6628 | |
| 6629 | is_binary_ = true; |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6630 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6631 | bool ret = LoadFromString(model, err, warn, |
| 6632 | reinterpret_cast<const char *>(&bytes[20]), |
Syoyo Fujita | 46ee8b4 | 2022-08-26 22:06:53 +0900 | [diff] [blame] | 6633 | chunk0_length, base_dir, check_sections); |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6634 | if (!ret) { |
| 6635 | return ret; |
| 6636 | } |
| 6637 | |
| 6638 | return true; |
| 6639 | } |
| 6640 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6641 | bool TinyGLTF::LoadBinaryFromFile(Model *model, std::string *err, |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6642 | std::string *warn, |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6643 | const std::string &filename, |
| 6644 | unsigned int check_sections) { |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6645 | std::stringstream ss; |
| 6646 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 6647 | if (fs.ReadWholeFile == nullptr) { |
| 6648 | // Programmer error, assert() ? |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6649 | ss << "Failed to read file: " << filename |
| 6650 | << ": one or more FS callback not set" << std::endl; |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6651 | if (err) { |
| 6652 | (*err) = ss.str(); |
| 6653 | } |
| 6654 | return false; |
| 6655 | } |
| 6656 | |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 6657 | std::vector<unsigned char> data; |
| 6658 | std::string fileerr; |
| 6659 | bool fileread = fs.ReadWholeFile(&data, &fileerr, filename, fs.user_data); |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6660 | if (!fileread) { |
Paolo Jovon | e6601bf | 2018-07-07 20:43:33 +0200 | [diff] [blame] | 6661 | ss << "Failed to read file: " << filename << ": " << fileerr << std::endl; |
| 6662 | if (err) { |
| 6663 | (*err) = ss.str(); |
| 6664 | } |
| 6665 | return false; |
| 6666 | } |
Syoyo Fujita | ec39a52 | 2016-05-01 18:33:31 +0900 | [diff] [blame] | 6667 | |
Syoyo Fujita | beded61 | 2016-05-01 20:03:43 +0900 | [diff] [blame] | 6668 | std::string basedir = GetBaseDir(filename); |
| 6669 | |
Syoyo Fujita | 7c8d4ed | 2018-07-27 16:49:10 +0900 | [diff] [blame] | 6670 | bool ret = LoadBinaryFromMemory(model, err, warn, &data.at(0), |
| 6671 | static_cast<unsigned int>(data.size()), |
| 6672 | basedir, check_sections); |
Syoyo Fujita | 61fb52b | 2016-02-06 17:26:44 +0900 | [diff] [blame] | 6673 | |
| 6674 | return ret; |
| 6675 | } |
| 6676 | |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6677 | /////////////////////// |
| 6678 | // GLTF Serialization |
| 6679 | /////////////////////// |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6680 | namespace detail { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6681 | detail::json JsonFromString(const char *s) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6682 | #ifdef TINYGLTF_USE_RAPIDJSON |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6683 | return detail::json(s, detail::GetAllocator()); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6684 | #else |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6685 | return detail::json(s); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6686 | #endif |
jrkoonce | 63419a1 | 2019-09-03 17:06:41 -0500 | [diff] [blame] | 6687 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6688 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6689 | void JsonAssign(detail::json &dest, const detail::json &src) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6690 | #ifdef TINYGLTF_USE_RAPIDJSON |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6691 | dest.CopyFrom(src, detail::GetAllocator()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6692 | #else |
| 6693 | dest = src; |
| 6694 | #endif |
| 6695 | } |
| 6696 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6697 | void JsonAddMember(detail::json &o, const char *key, detail::json &&value) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6698 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 6699 | if (!o.IsObject()) { |
| 6700 | o.SetObject(); |
| 6701 | } |
Syoyo Fujita | 147a00a | 2023-06-04 05:45:24 +0900 | [diff] [blame] | 6702 | |
| 6703 | // Issue 420. |
| 6704 | // AddMember may create duplicated key, so use [] API when a key already exists. |
| 6705 | // https://github.com/Tencent/rapidjson/issues/771#issuecomment-254386863 |
| 6706 | detail::json_const_iterator it; |
| 6707 | if (detail::FindMember(o, key, it)) { |
| 6708 | o[key] = std::move(value); // replace |
| 6709 | } else { |
| 6710 | o.AddMember(detail::json(key, detail::GetAllocator()), std::move(value), detail::GetAllocator()); |
| 6711 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6712 | #else |
| 6713 | o[key] = std::move(value); |
| 6714 | #endif |
| 6715 | } |
| 6716 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6717 | void JsonPushBack(detail::json &o, detail::json &&value) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6718 | #ifdef TINYGLTF_USE_RAPIDJSON |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6719 | o.PushBack(std::move(value), detail::GetAllocator()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6720 | #else |
| 6721 | o.push_back(std::move(value)); |
| 6722 | #endif |
| 6723 | } |
| 6724 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6725 | bool JsonIsNull(const detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6726 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 6727 | return o.IsNull(); |
| 6728 | #else |
| 6729 | return o.is_null(); |
| 6730 | #endif |
| 6731 | } |
| 6732 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6733 | void JsonSetObject(detail::json &o) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6734 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 6735 | o.SetObject(); |
| 6736 | #else |
| 6737 | o = o.object({}); |
| 6738 | #endif |
| 6739 | } |
| 6740 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6741 | void JsonReserveArray(detail::json &o, size_t s) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6742 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 6743 | o.SetArray(); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6744 | o.Reserve(static_cast<rapidjson::SizeType>(s), detail::GetAllocator()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6745 | #endif |
| 6746 | (void)(o); |
| 6747 | (void)(s); |
| 6748 | } |
| 6749 | } // namespace |
| 6750 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6751 | // typedef std::pair<std::string, detail::json> json_object_pair; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6752 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6753 | template <typename T> |
| 6754 | static void SerializeNumberProperty(const std::string &key, T number, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6755 | detail::json &obj) { |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 6756 | // obj.insert( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6757 | // json_object_pair(key, detail::json(static_cast<double>(number)))); |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 6758 | // obj[key] = static_cast<double>(number); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6759 | detail::JsonAddMember(obj, key.c_str(), detail::json(number)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6760 | } |
| 6761 | |
Ivo van Dongen | 272a9df | 2020-05-29 11:24:11 +0300 | [diff] [blame] | 6762 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 6763 | template <> |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6764 | void SerializeNumberProperty(const std::string &key, size_t number, detail::json &obj) { |
| 6765 | detail::JsonAddMember(obj, key.c_str(), detail::json(static_cast<uint64_t>(number))); |
Ivo van Dongen | 272a9df | 2020-05-29 11:24:11 +0300 | [diff] [blame] | 6766 | } |
| 6767 | #endif |
| 6768 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6769 | template <typename T> |
| 6770 | static void SerializeNumberArrayProperty(const std::string &key, |
| 6771 | const std::vector<T> &value, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6772 | detail::json &obj) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6773 | if (value.empty()) return; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6774 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6775 | detail::json ary; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6776 | detail::JsonReserveArray(ary, value.size()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6777 | for (const auto &s : value) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6778 | detail::JsonPushBack(ary, detail::json(s)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6779 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6780 | detail::JsonAddMember(obj, key.c_str(), std::move(ary)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6781 | } |
| 6782 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6783 | static void SerializeStringProperty(const std::string &key, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6784 | const std::string &value, detail::json &obj) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6785 | detail::JsonAddMember(obj, key.c_str(), detail::JsonFromString(value.c_str())); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6786 | } |
| 6787 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6788 | static void SerializeStringArrayProperty(const std::string &key, |
| 6789 | const std::vector<std::string> &value, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6790 | detail::json &obj) { |
| 6791 | detail::json ary; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6792 | detail::JsonReserveArray(ary, value.size()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6793 | for (auto &s : value) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6794 | detail::JsonPushBack(ary, detail::JsonFromString(s.c_str())); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6795 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6796 | detail::JsonAddMember(obj, key.c_str(), std::move(ary)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6797 | } |
| 6798 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6799 | static bool ValueToJson(const Value &value, detail::json *ret) { |
| 6800 | detail::json obj; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6801 | #ifdef TINYGLTF_USE_RAPIDJSON |
| 6802 | switch (value.Type()) { |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6803 | case REAL_TYPE: |
| 6804 | obj.SetDouble(value.Get<double>()); |
| 6805 | break; |
| 6806 | case INT_TYPE: |
| 6807 | obj.SetInt(value.Get<int>()); |
| 6808 | break; |
| 6809 | case BOOL_TYPE: |
| 6810 | obj.SetBool(value.Get<bool>()); |
| 6811 | break; |
| 6812 | case STRING_TYPE: |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6813 | obj.SetString(value.Get<std::string>().c_str(), detail::GetAllocator()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6814 | break; |
| 6815 | case ARRAY_TYPE: { |
| 6816 | obj.SetArray(); |
| 6817 | obj.Reserve(static_cast<rapidjson::SizeType>(value.ArrayLen()), |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6818 | detail::GetAllocator()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6819 | for (unsigned int i = 0; i < value.ArrayLen(); ++i) { |
| 6820 | Value elementValue = value.Get(int(i)); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6821 | detail::json elementJson; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6822 | if (ValueToJson(value.Get(int(i)), &elementJson)) |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6823 | obj.PushBack(std::move(elementJson), detail::GetAllocator()); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6824 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6825 | break; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6826 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6827 | case BINARY_TYPE: |
| 6828 | // TODO |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6829 | // obj = detail::json(value.Get<std::vector<unsigned char>>()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6830 | return false; |
| 6831 | break; |
| 6832 | case OBJECT_TYPE: { |
| 6833 | obj.SetObject(); |
| 6834 | Value::Object objMap = value.Get<Value::Object>(); |
| 6835 | for (auto &it : objMap) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6836 | detail::json elementJson; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6837 | if (ValueToJson(it.second, &elementJson)) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6838 | obj.AddMember(detail::json(it.first.c_str(), detail::GetAllocator()), |
| 6839 | std::move(elementJson), detail::GetAllocator()); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6840 | } |
| 6841 | } |
| 6842 | break; |
| 6843 | } |
| 6844 | case NULL_TYPE: |
| 6845 | default: |
| 6846 | return false; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6847 | } |
| 6848 | #else |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 6849 | switch (value.Type()) { |
Syoyo Fujita | 150f243 | 2019-07-25 19:22:44 +0900 | [diff] [blame] | 6850 | case REAL_TYPE: |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6851 | obj = detail::json(value.Get<double>()); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 6852 | break; |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 6853 | case INT_TYPE: |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6854 | obj = detail::json(value.Get<int>()); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 6855 | break; |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 6856 | case BOOL_TYPE: |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6857 | obj = detail::json(value.Get<bool>()); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 6858 | break; |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 6859 | case STRING_TYPE: |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6860 | obj = detail::json(value.Get<std::string>()); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 6861 | break; |
| 6862 | case ARRAY_TYPE: { |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 6863 | for (unsigned int i = 0; i < value.ArrayLen(); ++i) { |
| 6864 | Value elementValue = value.Get(int(i)); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6865 | detail::json elementJson; |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 6866 | if (ValueToJson(value.Get(int(i)), &elementJson)) |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 6867 | obj.push_back(elementJson); |
| 6868 | } |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 6869 | break; |
| 6870 | } |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 6871 | case BINARY_TYPE: |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 6872 | // TODO |
| 6873 | // obj = json(value.Get<std::vector<unsigned char>>()); |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 6874 | return false; |
| 6875 | break; |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 6876 | case OBJECT_TYPE: { |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 6877 | Value::Object objMap = value.Get<Value::Object>(); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 6878 | for (auto &it : objMap) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6879 | detail::json elementJson; |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 6880 | if (ValueToJson(it.second, &elementJson)) obj[it.first] = elementJson; |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 6881 | } |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 6882 | break; |
| 6883 | } |
| 6884 | case NULL_TYPE: |
| 6885 | default: |
| 6886 | return false; |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 6887 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6888 | #endif |
| 6889 | if (ret) *ret = std::move(obj); |
Selmar Kok | fa7022f | 2018-04-04 18:10:20 +0200 | [diff] [blame] | 6890 | return true; |
| 6891 | } |
| 6892 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6893 | static void SerializeValue(const std::string &key, const Value &value, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6894 | detail::json &obj) { |
| 6895 | detail::json ret; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 6896 | if (ValueToJson(value, &ret)) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6897 | detail::JsonAddMember(obj, key.c_str(), std::move(ret)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6898 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6899 | } |
| 6900 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6901 | static void SerializeGltfBufferData(const std::vector<unsigned char> &data, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6902 | detail::json &o) { |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 6903 | std::string header = "data:application/octet-stream;base64,"; |
Syoyo Fujita | a11f6e1 | 2019-12-19 14:16:48 +0900 | [diff] [blame] | 6904 | if (data.size() > 0) { |
| 6905 | std::string encodedData = |
| 6906 | base64_encode(&data[0], static_cast<unsigned int>(data.size())); |
| 6907 | SerializeStringProperty("uri", header + encodedData, o); |
| 6908 | } else { |
| 6909 | // Issue #229 |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 6910 | // size 0 is allowed. Just emit mime header. |
Syoyo Fujita | a11f6e1 | 2019-12-19 14:16:48 +0900 | [diff] [blame] | 6911 | SerializeStringProperty("uri", header, o); |
| 6912 | } |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 6913 | } |
| 6914 | |
Selmar Kok | e467749 | 2018-10-25 16:45:49 +0200 | [diff] [blame] | 6915 | static bool SerializeGltfBufferData(const std::vector<unsigned char> &data, |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 6916 | const std::string &binFilename) { |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 6917 | #ifdef _WIN32 |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 6918 | #if defined(__GLIBCXX__) // mingw |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 6919 | int file_descriptor = _wopen(UTF8ToWchar(binFilename).c_str(), |
| 6920 | _O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY); |
| 6921 | __gnu_cxx::stdio_filebuf<char> wfile_buf( |
| 6922 | file_descriptor, std::ios_base::out | std::ios_base::binary); |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 6923 | std::ostream output(&wfile_buf); |
| 6924 | if (!wfile_buf.is_open()) return false; |
| 6925 | #elif defined(_MSC_VER) |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 6926 | std::ofstream output(UTF8ToWchar(binFilename).c_str(), std::ofstream::binary); |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 6927 | if (!output.is_open()) return false; |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 6928 | #else |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 6929 | std::ofstream output(binFilename.c_str(), std::ofstream::binary); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 6930 | if (!output.is_open()) return false; |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 6931 | #endif |
| 6932 | #else |
| 6933 | std::ofstream output(binFilename.c_str(), std::ofstream::binary); |
| 6934 | if (!output.is_open()) return false; |
| 6935 | #endif |
Syoyo Fujita | a11f6e1 | 2019-12-19 14:16:48 +0900 | [diff] [blame] | 6936 | if (data.size() > 0) { |
| 6937 | output.write(reinterpret_cast<const char *>(&data[0]), |
| 6938 | std::streamsize(data.size())); |
| 6939 | } else { |
| 6940 | // Issue #229 |
| 6941 | // size 0 will be still valid buffer data. |
| 6942 | // write empty file. |
| 6943 | } |
Selmar Kok | e467749 | 2018-10-25 16:45:49 +0200 | [diff] [blame] | 6944 | return true; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6945 | } |
| 6946 | |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 6947 | #if 0 // FIXME(syoyo): not used. will be removed in the future release. |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6948 | static void SerializeParameterMap(ParameterMap ¶m, detail::json &o) { |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6949 | for (ParameterMap::iterator paramIt = param.begin(); paramIt != param.end(); |
| 6950 | ++paramIt) { |
| 6951 | if (paramIt->second.number_array.size()) { |
| 6952 | SerializeNumberArrayProperty<double>(paramIt->first, |
| 6953 | paramIt->second.number_array, o); |
| 6954 | } else if (paramIt->second.json_double_value.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6955 | detail::json json_double_value; |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6956 | for (std::map<std::string, double>::iterator it = |
| 6957 | paramIt->second.json_double_value.begin(); |
| 6958 | it != paramIt->second.json_double_value.end(); ++it) { |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 6959 | if (it->first == "index") { |
| 6960 | json_double_value[it->first] = paramIt->second.TextureIndex(); |
| 6961 | } else { |
| 6962 | json_double_value[it->first] = it->second; |
| 6963 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6964 | } |
| 6965 | |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6966 | o[paramIt->first] = json_double_value; |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6967 | } else if (!paramIt->second.string_value.empty()) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6968 | SerializeStringProperty(paramIt->first, paramIt->second.string_value, o); |
Ben Buzbee | f6af224 | 2018-04-25 15:13:05 -0700 | [diff] [blame] | 6969 | } else if (paramIt->second.has_number_value) { |
| 6970 | o[paramIt->first] = paramIt->second.number_value; |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 6971 | } else { |
Syoyo Fujita | 8367531 | 2017-12-02 21:14:13 +0900 | [diff] [blame] | 6972 | o[paramIt->first] = paramIt->second.bool_value; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6973 | } |
| 6974 | } |
| 6975 | } |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 6976 | #endif |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 6977 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6978 | static void SerializeExtensionMap(const ExtensionMap &extensions, detail::json &o) { |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 6979 | if (!extensions.size()) return; |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 6980 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6981 | detail::json extMap; |
Selmar Kok | 81b672b | 2019-10-18 16:08:44 +0200 | [diff] [blame] | 6982 | for (ExtensionMap::const_iterator extIt = extensions.begin(); |
Ben Buzbee | 3b735bb | 2018-04-24 11:39:30 -0700 | [diff] [blame] | 6983 | extIt != extensions.end(); ++extIt) { |
Syoyo Fujita | 924d86e | 2018-10-08 21:15:12 +0900 | [diff] [blame] | 6984 | // Allow an empty object for extension(#97) |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6985 | detail::json ret; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6986 | bool isNull = true; |
Syoyo Fujita | 924d86e | 2018-10-08 21:15:12 +0900 | [diff] [blame] | 6987 | if (ValueToJson(extIt->second, &ret)) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6988 | isNull = detail::JsonIsNull(ret); |
| 6989 | detail::JsonAddMember(extMap, extIt->first.c_str(), std::move(ret)); |
Selmar Kok | db7f4e4 | 2018-10-10 18:10:58 +0200 | [diff] [blame] | 6990 | } |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 6991 | if (isNull) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 6992 | if (!(extIt->first.empty())) { // name should not be empty, but for sure |
| 6993 | // create empty object so that an extension name is still included in |
| 6994 | // json. |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 6995 | detail::json empty; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 6996 | detail::JsonSetObject(empty); |
| 6997 | detail::JsonAddMember(extMap, extIt->first.c_str(), std::move(empty)); |
Syoyo Fujita | 924d86e | 2018-10-08 21:15:12 +0900 | [diff] [blame] | 6998 | } |
| 6999 | } |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 7000 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7001 | detail::JsonAddMember(o, "extensions", std::move(extMap)); |
Selmar | 09d2ff1 | 2018-03-15 17:30:42 +0100 | [diff] [blame] | 7002 | } |
| 7003 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7004 | static void SerializeExtras(const Value & extras, detail::json & o) { |
Syoyo Fujita | 7a570c8 | 2023-06-19 21:52:13 +0900 | [diff] [blame] | 7005 | if (extras.Type() != NULL_TYPE) |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7006 | SerializeValue("extras", extras, o); |
| 7007 | } |
| 7008 | |
| 7009 | template <typename GltfType> |
| 7010 | void SerializeExtrasAndExtensions(const GltfType & obj, detail::json & o) { |
| 7011 | SerializeExtensionMap(obj.extensions, o); |
| 7012 | SerializeExtras(obj.extras, o); |
| 7013 | } |
| 7014 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7015 | static void SerializeGltfAccessor(const Accessor &accessor, detail::json &o) { |
Sanjeet Suhag | 5ecede7 | 2020-03-04 17:40:10 -0500 | [diff] [blame] | 7016 | if (accessor.bufferView >= 0) |
| 7017 | SerializeNumberProperty<int>("bufferView", accessor.bufferView, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7018 | |
Syoyo Fujita | 18f0e20 | 2020-04-29 19:16:35 +0900 | [diff] [blame] | 7019 | if (accessor.byteOffset != 0) |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7020 | SerializeNumberProperty<int>("byteOffset", int(accessor.byteOffset), o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7021 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7022 | SerializeNumberProperty<int>("componentType", accessor.componentType, o); |
| 7023 | SerializeNumberProperty<size_t>("count", accessor.count, o); |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 7024 | |
| 7025 | if ((accessor.componentType == TINYGLTF_COMPONENT_TYPE_FLOAT) || |
| 7026 | (accessor.componentType == TINYGLTF_COMPONENT_TYPE_DOUBLE)) { |
| 7027 | SerializeNumberArrayProperty<double>("min", accessor.minValues, o); |
| 7028 | SerializeNumberArrayProperty<double>("max", accessor.maxValues, o); |
| 7029 | } else { |
| 7030 | // Issue #301. Serialize as integer. |
| 7031 | // Assume int value is within [-2**31-1, 2**31-1] |
| 7032 | { |
| 7033 | std::vector<int> values; |
| 7034 | std::transform(accessor.minValues.begin(), accessor.minValues.end(), |
| 7035 | std::back_inserter(values), |
| 7036 | [](double v) { return static_cast<int>(v); }); |
| 7037 | |
| 7038 | SerializeNumberArrayProperty<int>("min", values, o); |
| 7039 | } |
| 7040 | |
| 7041 | { |
| 7042 | std::vector<int> values; |
| 7043 | std::transform(accessor.maxValues.begin(), accessor.maxValues.end(), |
| 7044 | std::back_inserter(values), |
| 7045 | [](double v) { return static_cast<int>(v); }); |
| 7046 | |
| 7047 | SerializeNumberArrayProperty<int>("max", values, o); |
| 7048 | } |
| 7049 | } |
| 7050 | |
Eero Pajarre | 2e8a115 | 2019-11-18 13:09:25 +0200 | [diff] [blame] | 7051 | if (accessor.normalized) |
| 7052 | SerializeValue("normalized", Value(accessor.normalized), o); |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7053 | std::string type; |
| 7054 | switch (accessor.type) { |
| 7055 | case TINYGLTF_TYPE_SCALAR: |
| 7056 | type = "SCALAR"; |
| 7057 | break; |
| 7058 | case TINYGLTF_TYPE_VEC2: |
| 7059 | type = "VEC2"; |
| 7060 | break; |
| 7061 | case TINYGLTF_TYPE_VEC3: |
| 7062 | type = "VEC3"; |
| 7063 | break; |
| 7064 | case TINYGLTF_TYPE_VEC4: |
| 7065 | type = "VEC4"; |
| 7066 | break; |
| 7067 | case TINYGLTF_TYPE_MAT2: |
| 7068 | type = "MAT2"; |
| 7069 | break; |
| 7070 | case TINYGLTF_TYPE_MAT3: |
| 7071 | type = "MAT3"; |
| 7072 | break; |
| 7073 | case TINYGLTF_TYPE_MAT4: |
| 7074 | type = "MAT4"; |
| 7075 | break; |
| 7076 | } |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 7077 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7078 | SerializeStringProperty("type", type, o); |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 7079 | if (!accessor.name.empty()) SerializeStringProperty("name", accessor.name, o); |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7080 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7081 | SerializeExtrasAndExtensions(accessor, o); |
feiy | 0b31543 | 2022-08-13 10:08:17 +0800 | [diff] [blame] | 7082 | |
| 7083 | // sparse |
| 7084 | if (accessor.sparse.isSparse) |
| 7085 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7086 | detail::json sparse; |
feiy | 0b31543 | 2022-08-13 10:08:17 +0800 | [diff] [blame] | 7087 | SerializeNumberProperty<int>("count", accessor.sparse.count, sparse); |
| 7088 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7089 | detail::json indices; |
feiy | 0b31543 | 2022-08-13 10:08:17 +0800 | [diff] [blame] | 7090 | SerializeNumberProperty<int>("bufferView", accessor.sparse.indices.bufferView, indices); |
| 7091 | SerializeNumberProperty<int>("byteOffset", accessor.sparse.indices.byteOffset, indices); |
| 7092 | SerializeNumberProperty<int>("componentType", accessor.sparse.indices.componentType, indices); |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7093 | SerializeExtrasAndExtensions(accessor.sparse.indices, indices); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7094 | detail::JsonAddMember(sparse, "indices", std::move(indices)); |
feiy | 0b31543 | 2022-08-13 10:08:17 +0800 | [diff] [blame] | 7095 | } |
| 7096 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7097 | detail::json values; |
feiy | 0b31543 | 2022-08-13 10:08:17 +0800 | [diff] [blame] | 7098 | SerializeNumberProperty<int>("bufferView", accessor.sparse.values.bufferView, values); |
feiy | 1a8814d | 2022-08-14 19:49:07 +0800 | [diff] [blame] | 7099 | SerializeNumberProperty<int>("byteOffset", accessor.sparse.values.byteOffset, values); |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7100 | SerializeExtrasAndExtensions(accessor.sparse.values, values); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7101 | detail::JsonAddMember(sparse, "values", std::move(values)); |
feiy | 0b31543 | 2022-08-13 10:08:17 +0800 | [diff] [blame] | 7102 | } |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7103 | SerializeExtrasAndExtensions(accessor.sparse, sparse); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7104 | detail::JsonAddMember(o, "sparse", std::move(sparse)); |
feiy | 0b31543 | 2022-08-13 10:08:17 +0800 | [diff] [blame] | 7105 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7106 | } |
| 7107 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7108 | static void SerializeGltfAnimationChannel(const AnimationChannel &channel, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7109 | detail::json &o) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7110 | SerializeNumberProperty("sampler", channel.sampler, o); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7111 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7112 | detail::json target; |
Jack Mousseau | 283b552 | 2023-01-15 11:45:45 -0800 | [diff] [blame] | 7113 | |
Loïc Escales | a75355b | 2023-04-18 21:03:39 +0200 | [diff] [blame] | 7114 | if (channel.target_node >= 0) { |
Jack Mousseau | 283b552 | 2023-01-15 11:45:45 -0800 | [diff] [blame] | 7115 | SerializeNumberProperty("node", channel.target_node, target); |
| 7116 | } |
| 7117 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7118 | SerializeStringProperty("path", channel.target_path, target); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7119 | |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 7120 | SerializeExtensionMap(channel.target_extensions, target); |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7121 | SerializeExtras(channel.target_extras, target); |
Selmar Kok | 973d9b3 | 2020-01-21 18:45:24 +0100 | [diff] [blame] | 7122 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7123 | detail::JsonAddMember(o, "target", std::move(target)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7124 | } |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7125 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7126 | SerializeExtrasAndExtensions(channel, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7127 | } |
| 7128 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7129 | static void SerializeGltfAnimationSampler(const AnimationSampler &sampler, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7130 | detail::json &o) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7131 | SerializeNumberProperty("input", sampler.input, o); |
| 7132 | SerializeNumberProperty("output", sampler.output, o); |
| 7133 | SerializeStringProperty("interpolation", sampler.interpolation, o); |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7134 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7135 | SerializeExtrasAndExtensions(sampler, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7136 | } |
| 7137 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7138 | static void SerializeGltfAnimation(const Animation &animation, detail::json &o) { |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 7139 | if (!animation.name.empty()) |
| 7140 | SerializeStringProperty("name", animation.name, o); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7141 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7142 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7143 | detail::json channels; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7144 | detail::JsonReserveArray(channels, animation.channels.size()); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7145 | for (unsigned int i = 0; i < animation.channels.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7146 | detail::json channel; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7147 | AnimationChannel gltfChannel = animation.channels[i]; |
| 7148 | SerializeGltfAnimationChannel(gltfChannel, channel); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7149 | detail::JsonPushBack(channels, std::move(channel)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7150 | } |
| 7151 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7152 | detail::JsonAddMember(o, "channels", std::move(channels)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7153 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7154 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7155 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7156 | detail::json samplers; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7157 | detail::JsonReserveArray(samplers, animation.samplers.size()); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7158 | for (unsigned int i = 0; i < animation.samplers.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7159 | detail::json sampler; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7160 | AnimationSampler gltfSampler = animation.samplers[i]; |
| 7161 | SerializeGltfAnimationSampler(gltfSampler, sampler); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7162 | detail::JsonPushBack(samplers, std::move(sampler)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7163 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7164 | detail::JsonAddMember(o, "samplers", std::move(samplers)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7165 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7166 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7167 | SerializeExtrasAndExtensions(animation, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7168 | } |
| 7169 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7170 | static void SerializeGltfAsset(const Asset &asset, detail::json &o) { |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7171 | if (!asset.generator.empty()) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7172 | SerializeStringProperty("generator", asset.generator, o); |
| 7173 | } |
| 7174 | |
Christophe | 820ede8 | 2019-07-04 15:21:21 +0900 | [diff] [blame] | 7175 | if (!asset.copyright.empty()) { |
| 7176 | SerializeStringProperty("copyright", asset.copyright, o); |
| 7177 | } |
| 7178 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7179 | auto version = asset.version; |
| 7180 | if (version.empty()) { |
Syoyo Fujita | b702de7 | 2021-03-02 19:08:29 +0900 | [diff] [blame] | 7181 | // Just in case |
| 7182 | // `version` must be defined |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7183 | version = "2.0"; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7184 | } |
| 7185 | |
Syoyo Fujita | b702de7 | 2021-03-02 19:08:29 +0900 | [diff] [blame] | 7186 | // 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] | 7187 | SerializeStringProperty("version", version, o); |
Syoyo Fujita | b702de7 | 2021-03-02 19:08:29 +0900 | [diff] [blame] | 7188 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7189 | SerializeExtrasAndExtensions(asset, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7190 | } |
| 7191 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7192 | static void SerializeGltfBufferBin(const Buffer &buffer, detail::json &o, |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 7193 | std::vector<unsigned char> &binBuffer) { |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 7194 | SerializeNumberProperty("byteLength", buffer.data.size(), o); |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 7195 | binBuffer = buffer.data; |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 7196 | |
| 7197 | if (buffer.name.size()) SerializeStringProperty("name", buffer.name, o); |
| 7198 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7199 | SerializeExtrasAndExtensions(buffer, o); |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 7200 | } |
| 7201 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7202 | static void SerializeGltfBuffer(const Buffer &buffer, detail::json &o) { |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 7203 | SerializeNumberProperty("byteLength", buffer.data.size(), o); |
| 7204 | SerializeGltfBufferData(buffer.data, o); |
| 7205 | |
| 7206 | if (buffer.name.size()) SerializeStringProperty("name", buffer.name, o); |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7207 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7208 | SerializeExtrasAndExtensions(buffer, o); |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 7209 | } |
| 7210 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7211 | static bool SerializeGltfBuffer(const Buffer &buffer, detail::json &o, |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 7212 | const std::string &binFilename, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 7213 | const std::string &binUri) { |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 7214 | if (!SerializeGltfBufferData(buffer.data, binFilename)) return false; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7215 | SerializeNumberProperty("byteLength", buffer.data.size(), o); |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 7216 | SerializeStringProperty("uri", binUri, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7217 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7218 | if (buffer.name.size()) SerializeStringProperty("name", buffer.name, o); |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7219 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7220 | SerializeExtrasAndExtensions(buffer, o); |
Selmar Kok | e467749 | 2018-10-25 16:45:49 +0200 | [diff] [blame] | 7221 | return true; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7222 | } |
| 7223 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7224 | static void SerializeGltfBufferView(const BufferView &bufferView, detail::json &o) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7225 | SerializeNumberProperty("buffer", bufferView.buffer, o); |
| 7226 | SerializeNumberProperty<size_t>("byteLength", bufferView.byteLength, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7227 | |
Johan Bowald | faa2722 | 2018-03-28 14:44:45 +0200 | [diff] [blame] | 7228 | // byteStride is optional, minimum allowed is 4 |
| 7229 | if (bufferView.byteStride >= 4) { |
| 7230 | SerializeNumberProperty<size_t>("byteStride", bufferView.byteStride, o); |
| 7231 | } |
| 7232 | // byteOffset is optional, default is 0 |
| 7233 | if (bufferView.byteOffset > 0) { |
| 7234 | SerializeNumberProperty<size_t>("byteOffset", bufferView.byteOffset, o); |
| 7235 | } |
| 7236 | // Target is optional, check if it contains a valid value |
| 7237 | if (bufferView.target == TINYGLTF_TARGET_ARRAY_BUFFER || |
| 7238 | bufferView.target == TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER) { |
| 7239 | SerializeNumberProperty("target", bufferView.target, o); |
| 7240 | } |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7241 | if (bufferView.name.size()) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7242 | SerializeStringProperty("name", bufferView.name, o); |
| 7243 | } |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7244 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7245 | SerializeExtrasAndExtensions(bufferView, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7246 | } |
| 7247 | |
Pyarelal Knowles | a912155 | 2022-12-29 14:12:29 -0800 | [diff] [blame] | 7248 | static void SerializeGltfImage(const Image &image, const std::string &uri, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7249 | detail::json &o) { |
Syoyo Fujita | 584f1df | 2022-12-29 21:05:53 +0900 | [diff] [blame] | 7250 | // From 2.7.0, we look for `uri` parameter, not `Image.uri` |
| 7251 | // if uri is empty, the mimeType and bufferview should be set |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7252 | if (uri.empty()) { |
Arthur Brianville (Ybalrid) | f29ae1a | 2019-05-25 22:30:55 +0200 | [diff] [blame] | 7253 | SerializeStringProperty("mimeType", image.mimeType, o); |
| 7254 | SerializeNumberProperty<int>("bufferView", image.bufferView, o); |
| 7255 | } else { |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7256 | SerializeStringProperty("uri", uri, o); |
Arthur Brianville (Ybalrid) | f29ae1a | 2019-05-25 22:30:55 +0200 | [diff] [blame] | 7257 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7258 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7259 | if (image.name.size()) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7260 | SerializeStringProperty("name", image.name, o); |
| 7261 | } |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7262 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7263 | SerializeExtrasAndExtensions(image, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7264 | } |
| 7265 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7266 | static void SerializeGltfTextureInfo(const TextureInfo &texinfo, detail::json &o) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7267 | SerializeNumberProperty("index", texinfo.index, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7268 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7269 | if (texinfo.texCoord != 0) { |
| 7270 | SerializeNumberProperty("texCoord", texinfo.texCoord, o); |
| 7271 | } |
| 7272 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7273 | SerializeExtrasAndExtensions(texinfo, o); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7274 | } |
| 7275 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7276 | static void SerializeGltfNormalTextureInfo(const NormalTextureInfo &texinfo, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7277 | detail::json &o) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7278 | SerializeNumberProperty("index", texinfo.index, o); |
| 7279 | |
| 7280 | if (texinfo.texCoord != 0) { |
| 7281 | SerializeNumberProperty("texCoord", texinfo.texCoord, o); |
| 7282 | } |
| 7283 | |
Benjamin Schmithüsen | 1c84fc2 | 2019-07-25 16:07:27 +0200 | [diff] [blame] | 7284 | if (!TINYGLTF_DOUBLE_EQUAL(texinfo.scale, 1.0)) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7285 | SerializeNumberProperty("scale", texinfo.scale, o); |
| 7286 | } |
| 7287 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7288 | SerializeExtrasAndExtensions(texinfo, o); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7289 | } |
| 7290 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7291 | static void SerializeGltfOcclusionTextureInfo( |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7292 | const OcclusionTextureInfo &texinfo, detail::json &o) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7293 | SerializeNumberProperty("index", texinfo.index, o); |
| 7294 | |
| 7295 | if (texinfo.texCoord != 0) { |
| 7296 | SerializeNumberProperty("texCoord", texinfo.texCoord, o); |
| 7297 | } |
| 7298 | |
Benjamin Schmithüsen | 1c84fc2 | 2019-07-25 16:07:27 +0200 | [diff] [blame] | 7299 | if (!TINYGLTF_DOUBLE_EQUAL(texinfo.strength, 1.0)) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7300 | SerializeNumberProperty("strength", texinfo.strength, o); |
| 7301 | } |
| 7302 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7303 | SerializeExtrasAndExtensions(texinfo, o); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7304 | } |
| 7305 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 7306 | static void SerializeGltfPbrMetallicRoughness(const PbrMetallicRoughness &pbr, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7307 | detail::json &o) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7308 | std::vector<double> default_baseColorFactor = {1.0, 1.0, 1.0, 1.0}; |
| 7309 | if (!Equals(pbr.baseColorFactor, default_baseColorFactor)) { |
| 7310 | SerializeNumberArrayProperty<double>("baseColorFactor", pbr.baseColorFactor, |
| 7311 | o); |
| 7312 | } |
| 7313 | |
Benjamin Schmithüsen | 1c84fc2 | 2019-07-25 16:07:27 +0200 | [diff] [blame] | 7314 | if (!TINYGLTF_DOUBLE_EQUAL(pbr.metallicFactor, 1.0)) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7315 | SerializeNumberProperty("metallicFactor", pbr.metallicFactor, o); |
| 7316 | } |
| 7317 | |
Benjamin Schmithüsen | 1c84fc2 | 2019-07-25 16:07:27 +0200 | [diff] [blame] | 7318 | if (!TINYGLTF_DOUBLE_EQUAL(pbr.roughnessFactor, 1.0)) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7319 | SerializeNumberProperty("roughnessFactor", pbr.roughnessFactor, o); |
| 7320 | } |
| 7321 | |
Syoyo Fujita | 4ebd636 | 2019-08-15 12:25:50 +0900 | [diff] [blame] | 7322 | if (pbr.baseColorTexture.index > -1) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7323 | detail::json texinfo; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7324 | SerializeGltfTextureInfo(pbr.baseColorTexture, texinfo); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7325 | detail::JsonAddMember(o, "baseColorTexture", std::move(texinfo)); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7326 | } |
| 7327 | |
Syoyo Fujita | 4ebd636 | 2019-08-15 12:25:50 +0900 | [diff] [blame] | 7328 | if (pbr.metallicRoughnessTexture.index > -1) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7329 | detail::json texinfo; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7330 | SerializeGltfTextureInfo(pbr.metallicRoughnessTexture, texinfo); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7331 | detail::JsonAddMember(o, "metallicRoughnessTexture", std::move(texinfo)); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7332 | } |
| 7333 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7334 | SerializeExtrasAndExtensions(pbr, o); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7335 | } |
| 7336 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7337 | static void SerializeGltfMaterial(const Material &material, detail::json &o) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7338 | if (material.name.size()) { |
| 7339 | SerializeStringProperty("name", material.name, o); |
| 7340 | } |
| 7341 | |
| 7342 | // QUESTION(syoyo): Write material parameters regardless of its default value? |
| 7343 | |
| 7344 | if (!TINYGLTF_DOUBLE_EQUAL(material.alphaCutoff, 0.5)) { |
| 7345 | SerializeNumberProperty("alphaCutoff", material.alphaCutoff, o); |
| 7346 | } |
| 7347 | |
Patrick Härtl | d9a468b | 2019-08-14 14:14:07 +0200 | [diff] [blame] | 7348 | if (material.alphaMode.compare("OPAQUE") != 0) { |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7349 | SerializeStringProperty("alphaMode", material.alphaMode, o); |
| 7350 | } |
| 7351 | |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 7352 | if (material.doubleSided != false) |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7353 | detail::JsonAddMember(o, "doubleSided", detail::json(material.doubleSided)); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7354 | |
Syoyo Fujita | 4ebd636 | 2019-08-15 12:25:50 +0900 | [diff] [blame] | 7355 | if (material.normalTexture.index > -1) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7356 | detail::json texinfo; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7357 | SerializeGltfNormalTextureInfo(material.normalTexture, texinfo); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7358 | detail::JsonAddMember(o, "normalTexture", std::move(texinfo)); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7359 | } |
| 7360 | |
Syoyo Fujita | 4ebd636 | 2019-08-15 12:25:50 +0900 | [diff] [blame] | 7361 | if (material.occlusionTexture.index > -1) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7362 | detail::json texinfo; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7363 | SerializeGltfOcclusionTextureInfo(material.occlusionTexture, texinfo); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7364 | detail::JsonAddMember(o, "occlusionTexture", std::move(texinfo)); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7365 | } |
| 7366 | |
Syoyo Fujita | 4ebd636 | 2019-08-15 12:25:50 +0900 | [diff] [blame] | 7367 | if (material.emissiveTexture.index > -1) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7368 | detail::json texinfo; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7369 | SerializeGltfTextureInfo(material.emissiveTexture, texinfo); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7370 | detail::JsonAddMember(o, "emissiveTexture", std::move(texinfo)); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7371 | } |
| 7372 | |
| 7373 | std::vector<double> default_emissiveFactor = {0.0, 0.0, 0.0}; |
| 7374 | if (!Equals(material.emissiveFactor, default_emissiveFactor)) { |
| 7375 | SerializeNumberArrayProperty<double>("emissiveFactor", |
| 7376 | material.emissiveFactor, o); |
| 7377 | } |
| 7378 | |
| 7379 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7380 | detail::json pbrMetallicRoughness; |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7381 | SerializeGltfPbrMetallicRoughness(material.pbrMetallicRoughness, |
| 7382 | pbrMetallicRoughness); |
Syoyo Fujita | 7e00904 | 2019-09-13 15:32:22 +0900 | [diff] [blame] | 7383 | // Issue 204 |
| 7384 | // Do not serialize `pbrMetallicRoughness` if pbrMetallicRoughness has all |
| 7385 | // default values(json is null). Otherwise it will serialize to |
| 7386 | // `pbrMetallicRoughness : null`, which cannot be read by other glTF |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 7387 | // importers (and validators). |
Syoyo Fujita | 7e00904 | 2019-09-13 15:32:22 +0900 | [diff] [blame] | 7388 | // |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7389 | if (!detail::JsonIsNull(pbrMetallicRoughness)) { |
| 7390 | detail::JsonAddMember(o, "pbrMetallicRoughness", std::move(pbrMetallicRoughness)); |
Syoyo Fujita | 7e00904 | 2019-09-13 15:32:22 +0900 | [diff] [blame] | 7391 | } |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7392 | } |
| 7393 | |
| 7394 | #if 0 // legacy way. just for the record. |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7395 | if (material.values.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7396 | detail::json pbrMetallicRoughness; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7397 | SerializeParameterMap(material.values, pbrMetallicRoughness); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7398 | detail::JsonAddMember(o, "pbrMetallicRoughness", std::move(pbrMetallicRoughness)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7399 | } |
| 7400 | |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7401 | SerializeParameterMap(material.additionalValues, o); |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7402 | #else |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7403 | |
Syoyo Fujita | 046400b | 2019-07-24 19:26:48 +0900 | [diff] [blame] | 7404 | #endif |
| 7405 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7406 | SerializeExtrasAndExtensions(material, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7407 | } |
| 7408 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7409 | static void SerializeGltfMesh(const Mesh &mesh, detail::json &o) { |
| 7410 | detail::json primitives; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7411 | detail::JsonReserveArray(primitives, mesh.primitives.size()); |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7412 | for (unsigned int i = 0; i < mesh.primitives.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7413 | detail::json primitive; |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7414 | const Primitive &gltfPrimitive = mesh.primitives[i]; // don't make a copy |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7415 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7416 | detail::json attributes; |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7417 | for (auto attrIt = gltfPrimitive.attributes.begin(); |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7418 | attrIt != gltfPrimitive.attributes.end(); ++attrIt) { |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7419 | SerializeNumberProperty<int>(attrIt->first, attrIt->second, attributes); |
| 7420 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7421 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7422 | detail::JsonAddMember(primitive, "attributes", std::move(attributes)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7423 | } |
Johan Bowald | faa2722 | 2018-03-28 14:44:45 +0200 | [diff] [blame] | 7424 | |
imallett | d9ce9eb | 2022-10-07 10:37:09 -0700 | [diff] [blame] | 7425 | // Indices is optional |
Johan Bowald | faa2722 | 2018-03-28 14:44:45 +0200 | [diff] [blame] | 7426 | if (gltfPrimitive.indices > -1) { |
| 7427 | SerializeNumberProperty<int>("indices", gltfPrimitive.indices, primitive); |
| 7428 | } |
| 7429 | // Material is optional |
| 7430 | if (gltfPrimitive.material > -1) { |
Syoyo Fujita | 719d7e4 | 2018-03-30 19:26:35 +0900 | [diff] [blame] | 7431 | SerializeNumberProperty<int>("material", gltfPrimitive.material, |
| 7432 | primitive); |
Johan Bowald | faa2722 | 2018-03-28 14:44:45 +0200 | [diff] [blame] | 7433 | } |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7434 | SerializeNumberProperty<int>("mode", gltfPrimitive.mode, primitive); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7435 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7436 | // Morph targets |
| 7437 | if (gltfPrimitive.targets.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7438 | detail::json targets; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7439 | detail::JsonReserveArray(targets, gltfPrimitive.targets.size()); |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7440 | for (unsigned int k = 0; k < gltfPrimitive.targets.size(); ++k) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7441 | detail::json targetAttributes; |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7442 | std::map<std::string, int> targetData = gltfPrimitive.targets[k]; |
| 7443 | for (std::map<std::string, int>::iterator attrIt = targetData.begin(); |
| 7444 | attrIt != targetData.end(); ++attrIt) { |
| 7445 | SerializeNumberProperty<int>(attrIt->first, attrIt->second, |
| 7446 | targetAttributes); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7447 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7448 | detail::JsonPushBack(targets, std::move(targetAttributes)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7449 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7450 | detail::JsonAddMember(primitive, "targets", std::move(targets)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7451 | } |
| 7452 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7453 | SerializeExtrasAndExtensions(gltfPrimitive, o); |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7454 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7455 | detail::JsonPushBack(primitives, std::move(primitive)); |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7456 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7457 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7458 | detail::JsonAddMember(o, "primitives", std::move(primitives)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7459 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7460 | if (mesh.weights.size()) { |
| 7461 | SerializeNumberArrayProperty<double>("weights", mesh.weights, o); |
| 7462 | } |
| 7463 | |
| 7464 | if (mesh.name.size()) { |
| 7465 | SerializeStringProperty("name", mesh.name, o); |
| 7466 | } |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7467 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7468 | SerializeExtrasAndExtensions(mesh, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7469 | } |
| 7470 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7471 | static void SerializeSpotLight(const SpotLight &spot, detail::json &o) { |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 7472 | SerializeNumberProperty("innerConeAngle", spot.innerConeAngle, o); |
| 7473 | SerializeNumberProperty("outerConeAngle", spot.outerConeAngle, o); |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7474 | SerializeExtrasAndExtensions(spot, o); |
Benjamin Schmithüsen | 051f4be | 2019-07-09 17:59:20 +0200 | [diff] [blame] | 7475 | } |
| 7476 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7477 | static void SerializeGltfLight(const Light &light, detail::json &o) { |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 7478 | if (!light.name.empty()) SerializeStringProperty("name", light.name, o); |
Benjamin Schmithüsen | 051f4be | 2019-07-09 17:59:20 +0200 | [diff] [blame] | 7479 | SerializeNumberProperty("intensity", light.intensity, o); |
Syoyo Fujita | 3dad303 | 2020-05-13 21:22:23 +0900 | [diff] [blame] | 7480 | if (light.range > 0.0) { |
| 7481 | SerializeNumberProperty("range", light.range, o); |
| 7482 | } |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 7483 | SerializeNumberArrayProperty("color", light.color, o); |
| 7484 | SerializeStringProperty("type", light.type, o); |
Benjamin Schmithüsen | 051f4be | 2019-07-09 17:59:20 +0200 | [diff] [blame] | 7485 | if (light.type == "spot") { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7486 | detail::json spot; |
Benjamin Schmithüsen | 051f4be | 2019-07-09 17:59:20 +0200 | [diff] [blame] | 7487 | SerializeSpotLight(light.spot, spot); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7488 | detail::JsonAddMember(o, "spot", std::move(spot)); |
Benjamin Schmithüsen | 051f4be | 2019-07-09 17:59:20 +0200 | [diff] [blame] | 7489 | } |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7490 | SerializeExtrasAndExtensions(light, o); |
Emanuel Schrade | 186322b | 2017-11-06 11:14:41 +0100 | [diff] [blame] | 7491 | } |
| 7492 | |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 7493 | static void SerializeGltfPositionalEmitter(const PositionalEmitter &positional, |
| 7494 | detail::json &o) { |
| 7495 | if (!TINYGLTF_DOUBLE_EQUAL(positional.coneInnerAngle, 6.283185307179586)) |
| 7496 | SerializeNumberProperty("coneInnerAngle", positional.coneInnerAngle, o); |
| 7497 | if (!TINYGLTF_DOUBLE_EQUAL(positional.coneOuterAngle, 6.283185307179586)) |
| 7498 | SerializeNumberProperty("coneOuterAngle", positional.coneOuterAngle, o); |
| 7499 | if (positional.coneOuterGain > 0.0) |
| 7500 | SerializeNumberProperty("coneOuterGain", positional.coneOuterGain, o); |
| 7501 | if (!TINYGLTF_DOUBLE_EQUAL(positional.maxDistance, 100.0)) |
| 7502 | SerializeNumberProperty("maxDistance", positional.maxDistance, o); |
| 7503 | if (!TINYGLTF_DOUBLE_EQUAL(positional.refDistance, 1.0)) |
| 7504 | SerializeNumberProperty("refDistance", positional.refDistance, o); |
| 7505 | if (!TINYGLTF_DOUBLE_EQUAL(positional.rolloffFactor, 1.0)) |
| 7506 | SerializeNumberProperty("rolloffFactor", positional.rolloffFactor, o); |
| 7507 | |
| 7508 | SerializeExtrasAndExtensions(positional, o); |
| 7509 | } |
| 7510 | |
| 7511 | static void SerializeGltfAudioEmitter(const AudioEmitter &emitter, |
| 7512 | detail::json &o) { |
| 7513 | if (!emitter.name.empty()) SerializeStringProperty("name", emitter.name, o); |
| 7514 | if (!TINYGLTF_DOUBLE_EQUAL(emitter.gain, 1.0)) |
| 7515 | SerializeNumberProperty("gain", emitter.gain, o); |
| 7516 | if (emitter.loop) SerializeNumberProperty("loop", emitter.loop, o); |
| 7517 | if (emitter.playing) SerializeNumberProperty("playing", emitter.playing, o); |
| 7518 | if (!emitter.type.empty()) SerializeStringProperty("type", emitter.type, o); |
| 7519 | if (!emitter.distanceModel.empty()) |
| 7520 | SerializeStringProperty("distanceModel", emitter.distanceModel, o); |
| 7521 | if (emitter.type == "positional") { |
| 7522 | detail::json positional; |
| 7523 | SerializeGltfPositionalEmitter(emitter.positional, positional); |
| 7524 | detail::JsonAddMember(o, "positional", std::move(positional)); |
| 7525 | } |
| 7526 | SerializeNumberProperty("source", emitter.source, o); |
| 7527 | SerializeExtrasAndExtensions(emitter, o); |
| 7528 | } |
| 7529 | |
| 7530 | static void SerializeGltfAudioSource(const AudioSource& source, detail::json& o) { |
| 7531 | std::string name; |
| 7532 | std::string uri; |
| 7533 | int bufferView; // (required if no uri) |
| 7534 | std::string mimeType; // (required if no uri) ["audio/mp3", "audio/ogg", |
| 7535 | // "audio/wav", "audio/m4a"] |
| 7536 | |
| 7537 | if (!source.name.empty()) SerializeStringProperty("name", source.name, o); |
| 7538 | if (source.uri.empty()) { |
| 7539 | SerializeStringProperty("mimeType", source.mimeType, o); |
| 7540 | SerializeNumberProperty<int>("bufferView", source.bufferView, o); |
| 7541 | } |
| 7542 | else { |
| 7543 | SerializeStringProperty("uri", source.uri, o); |
| 7544 | } |
| 7545 | SerializeExtrasAndExtensions(source, o); |
| 7546 | } |
| 7547 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7548 | static void SerializeGltfNode(const Node &node, detail::json &o) { |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7549 | if (node.translation.size() > 0) { |
| 7550 | SerializeNumberArrayProperty<double>("translation", node.translation, o); |
| 7551 | } |
| 7552 | if (node.rotation.size() > 0) { |
| 7553 | SerializeNumberArrayProperty<double>("rotation", node.rotation, o); |
| 7554 | } |
| 7555 | if (node.scale.size() > 0) { |
| 7556 | SerializeNumberArrayProperty<double>("scale", node.scale, o); |
| 7557 | } |
| 7558 | if (node.matrix.size() > 0) { |
| 7559 | SerializeNumberArrayProperty<double>("matrix", node.matrix, o); |
| 7560 | } |
| 7561 | if (node.mesh != -1) { |
| 7562 | SerializeNumberProperty<int>("mesh", node.mesh, o); |
| 7563 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7564 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7565 | if (node.skin != -1) { |
| 7566 | SerializeNumberProperty<int>("skin", node.skin, o); |
| 7567 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7568 | |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7569 | if (node.camera != -1) { |
| 7570 | SerializeNumberProperty<int>("camera", node.camera, o); |
| 7571 | } |
| 7572 | |
Benjamin Schmithüsen | 74c3c10 | 2019-08-16 14:24:26 +0200 | [diff] [blame] | 7573 | if (node.weights.size() > 0) { |
| 7574 | SerializeNumberArrayProperty<double>("weights", node.weights, o); |
| 7575 | } |
| 7576 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7577 | SerializeExtrasAndExtensions(node, o); |
Jens Olsson | b96f696 | 2018-05-24 15:29:54 +0200 | [diff] [blame] | 7578 | |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 7579 | // Note(agnat): If the asset was loaded from disk, the node may already |
| 7580 | // contain the KHR_lights_punctual extension. If it was constructed in |
| 7581 | // memory it does not. In any case we update the JSON property using |
| 7582 | // the value from the struct. Last, if the node does not have a light |
| 7583 | // reference but the extension is still present, we remove it. |
| 7584 | if (node.light != -1) { |
| 7585 | detail::json_iterator it; |
| 7586 | if (!detail::FindMember(o, "extensions", it)) { |
| 7587 | detail::json extensions; |
| 7588 | detail::JsonSetObject(extensions); |
| 7589 | detail::JsonAddMember(o, "extensions", std::move(extensions)); |
| 7590 | detail::FindMember(o, "extensions", it); |
| 7591 | } |
| 7592 | auto & extensions = detail::GetValue(it); |
| 7593 | if ( ! detail::FindMember(extensions, "KHR_lights_punctual", it)) { |
| 7594 | detail::json lights_punctual; |
| 7595 | detail::JsonSetObject(lights_punctual); |
| 7596 | detail::JsonAddMember(extensions, "KHR_lights_punctual", std::move(lights_punctual)); |
| 7597 | detail::FindMember(o, "KHR_lights_punctual", it); |
| 7598 | } |
David Siegel | 8d5d0b3 | 2023-06-07 15:35:35 +0200 | [diff] [blame] | 7599 | SerializeNumberProperty("light", node.light, detail::GetValue(it)); |
David Siegel | cfe64fb | 2023-06-07 15:18:38 +0200 | [diff] [blame] | 7600 | } else { |
| 7601 | // node has no light ref (any longer)... so we clean up |
| 7602 | detail::json_iterator ext_it; |
| 7603 | if (detail::FindMember(o, "extensions", ext_it)) { |
| 7604 | auto & extensions = detail::GetValue(ext_it); |
| 7605 | detail::json_iterator lp_it; |
| 7606 | if (detail::FindMember(extensions, "KHR_lights_punctual", lp_it)) { |
| 7607 | detail::Erase(extensions, lp_it); |
| 7608 | } |
| 7609 | if (detail::IsEmpty(extensions)) { |
| 7610 | detail::Erase(o, ext_it); |
| 7611 | } |
| 7612 | } |
| 7613 | } |
| 7614 | |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 7615 | // KHR_audio |
| 7616 | if (node.emitter != -1) { |
| 7617 | detail::json_iterator it; |
| 7618 | if (!detail::FindMember(o, "extensions", it)) { |
| 7619 | detail::json extensions; |
| 7620 | detail::JsonSetObject(extensions); |
| 7621 | detail::JsonAddMember(o, "extensions", std::move(extensions)); |
| 7622 | detail::FindMember(o, "extensions", it); |
| 7623 | } |
| 7624 | auto &extensions = detail::GetValue(it); |
| 7625 | if (!detail::FindMember(extensions, "KHR_audio", it)) { |
| 7626 | detail::json audio; |
| 7627 | detail::JsonSetObject(audio); |
| 7628 | detail::JsonAddMember(extensions, "KHR_audio", std::move(audio)); |
| 7629 | detail::FindMember(o, "KHR_audio", it); |
| 7630 | } |
| 7631 | SerializeNumberProperty("emitter", node.emitter, detail::GetValue(it)); |
| 7632 | } else { |
| 7633 | detail::json_iterator ext_it; |
| 7634 | if (detail::FindMember(o, "extensions", ext_it)) { |
| 7635 | auto &extensions = detail::GetValue(ext_it); |
| 7636 | detail::json_iterator lp_it; |
| 7637 | if (detail::FindMember(extensions, "KHR_audio", lp_it)) { |
| 7638 | detail::Erase(extensions, lp_it); |
| 7639 | } |
| 7640 | if (detail::IsEmpty(extensions)) { |
| 7641 | detail::Erase(o, ext_it); |
| 7642 | } |
| 7643 | } |
| 7644 | } |
| 7645 | |
Syoyo Fujita | 641b3cc | 2018-10-04 15:43:33 +0900 | [diff] [blame] | 7646 | if (!node.name.empty()) SerializeStringProperty("name", node.name, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7647 | SerializeNumberArrayProperty<int>("children", node.children, o); |
| 7648 | } |
| 7649 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7650 | static void SerializeGltfSampler(const Sampler &sampler, detail::json &o) { |
s00665032 | 137a7ca | 2023-01-13 12:52:08 +0700 | [diff] [blame] | 7651 | if (!sampler.name.empty()) { |
| 7652 | SerializeStringProperty("name", sampler.name, o); |
| 7653 | } |
Syoyo Fujita | ee179b2 | 2019-08-16 13:11:30 +0900 | [diff] [blame] | 7654 | if (sampler.magFilter != -1) { |
| 7655 | SerializeNumberProperty("magFilter", sampler.magFilter, o); |
| 7656 | } |
| 7657 | if (sampler.minFilter != -1) { |
| 7658 | SerializeNumberProperty("minFilter", sampler.minFilter, o); |
| 7659 | } |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 7660 | // SerializeNumberProperty("wrapR", sampler.wrapR, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7661 | SerializeNumberProperty("wrapS", sampler.wrapS, o); |
| 7662 | SerializeNumberProperty("wrapT", sampler.wrapT, o); |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7663 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7664 | SerializeExtrasAndExtensions(sampler, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7665 | } |
| 7666 | |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7667 | static void SerializeGltfOrthographicCamera(const OrthographicCamera &camera, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7668 | detail::json &o) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7669 | SerializeNumberProperty("zfar", camera.zfar, o); |
| 7670 | SerializeNumberProperty("znear", camera.znear, o); |
| 7671 | SerializeNumberProperty("xmag", camera.xmag, o); |
| 7672 | SerializeNumberProperty("ymag", camera.ymag, o); |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7673 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7674 | SerializeExtrasAndExtensions(camera, o); |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7675 | } |
| 7676 | |
| 7677 | static void SerializeGltfPerspectiveCamera(const PerspectiveCamera &camera, |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7678 | detail::json &o) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7679 | SerializeNumberProperty("zfar", camera.zfar, o); |
| 7680 | SerializeNumberProperty("znear", camera.znear, o); |
| 7681 | if (camera.aspectRatio > 0) { |
| 7682 | SerializeNumberProperty("aspectRatio", camera.aspectRatio, o); |
| 7683 | } |
| 7684 | |
| 7685 | if (camera.yfov > 0) { |
| 7686 | SerializeNumberProperty("yfov", camera.yfov, o); |
| 7687 | } |
Jens Olsson | b3af2f1 | 2018-06-04 10:17:49 +0200 | [diff] [blame] | 7688 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7689 | SerializeExtrasAndExtensions(camera, o); |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7690 | } |
| 7691 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7692 | static void SerializeGltfCamera(const Camera &camera, detail::json &o) { |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7693 | SerializeStringProperty("type", camera.type, o); |
| 7694 | if (!camera.name.empty()) { |
Selmar Kok | ee3d066 | 2018-10-08 16:20:43 +0200 | [diff] [blame] | 7695 | SerializeStringProperty("name", camera.name, o); |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7696 | } |
| 7697 | |
| 7698 | if (camera.type.compare("orthographic") == 0) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7699 | detail::json orthographic; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7700 | SerializeGltfOrthographicCamera(camera.orthographic, orthographic); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7701 | detail::JsonAddMember(o, "orthographic", std::move(orthographic)); |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7702 | } else if (camera.type.compare("perspective") == 0) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7703 | detail::json perspective; |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7704 | SerializeGltfPerspectiveCamera(camera.perspective, perspective); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7705 | detail::JsonAddMember(o, "perspective", std::move(perspective)); |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7706 | } else { |
| 7707 | // ??? |
| 7708 | } |
Syoyo | fe77cc5 | 2020-05-09 02:41:07 +0900 | [diff] [blame] | 7709 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7710 | SerializeExtrasAndExtensions(camera, o); |
Syoyo Fujita | 7b6edab | 2017-07-13 18:20:14 +0900 | [diff] [blame] | 7711 | } |
| 7712 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7713 | static void SerializeGltfScene(const Scene &scene, detail::json &o) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7714 | SerializeNumberArrayProperty<int>("nodes", scene.nodes, o); |
| 7715 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7716 | if (scene.name.size()) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7717 | SerializeStringProperty("name", scene.name, o); |
| 7718 | } |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7719 | SerializeExtrasAndExtensions(scene, o); |
Baranob_Ilya | 879cb47 | 2023-06-12 13:35:05 +0400 | [diff] [blame] | 7720 | |
| 7721 | // KHR_audio |
| 7722 | if (!scene.audioEmitters.empty()) { |
| 7723 | detail::json_iterator it; |
| 7724 | if (!detail::FindMember(o, "extensions", it)) { |
| 7725 | detail::json extensions; |
| 7726 | detail::JsonSetObject(extensions); |
| 7727 | detail::JsonAddMember(o, "extensions", std::move(extensions)); |
| 7728 | detail::FindMember(o, "extensions", it); |
| 7729 | } |
| 7730 | auto &extensions = detail::GetValue(it); |
| 7731 | if (!detail::FindMember(extensions, "KHR_audio", it)) { |
| 7732 | detail::json audio; |
| 7733 | detail::JsonSetObject(audio); |
| 7734 | detail::JsonAddMember(extensions, "KHR_audio", std::move(audio)); |
| 7735 | detail::FindMember(o, "KHR_audio", it); |
| 7736 | } |
| 7737 | SerializeNumberArrayProperty("emitters", scene.audioEmitters, detail::GetValue(it)); |
| 7738 | } else { |
| 7739 | detail::json_iterator ext_it; |
| 7740 | if (detail::FindMember(o, "extensions", ext_it)) { |
| 7741 | auto &extensions = detail::GetValue(ext_it); |
| 7742 | detail::json_iterator lp_it; |
| 7743 | if (detail::FindMember(extensions, "KHR_audio", lp_it)) { |
| 7744 | detail::Erase(extensions, lp_it); |
| 7745 | } |
| 7746 | if (detail::IsEmpty(extensions)) { |
| 7747 | detail::Erase(o, ext_it); |
| 7748 | } |
| 7749 | } |
| 7750 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7751 | } |
| 7752 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7753 | static void SerializeGltfSkin(const Skin &skin, detail::json &o) { |
Syoyo Fujita | d07b976 | 2021-04-28 19:15:16 +0900 | [diff] [blame] | 7754 | // required |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7755 | SerializeNumberArrayProperty<int>("joints", skin.joints, o); |
Syoyo Fujita | d07b976 | 2021-04-28 19:15:16 +0900 | [diff] [blame] | 7756 | |
| 7757 | if (skin.inverseBindMatrices >= 0) { |
| 7758 | SerializeNumberProperty("inverseBindMatrices", skin.inverseBindMatrices, o); |
| 7759 | } |
| 7760 | |
| 7761 | if (skin.skeleton >= 0) { |
| 7762 | SerializeNumberProperty("skeleton", skin.skeleton, o); |
| 7763 | } |
| 7764 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7765 | if (skin.name.size()) { |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7766 | SerializeStringProperty("name", skin.name, o); |
| 7767 | } |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7768 | |
| 7769 | SerializeExtrasAndExtensions(skin, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7770 | } |
| 7771 | |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7772 | static void SerializeGltfTexture(const Texture &texture, detail::json &o) { |
johan bowald | b97d34c | 2018-04-02 07:29:29 +0200 | [diff] [blame] | 7773 | if (texture.sampler > -1) { |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 7774 | SerializeNumberProperty("sampler", texture.sampler, o); |
| 7775 | } |
Selmar Kok | 8eb3904 | 2018-10-05 14:29:35 +0200 | [diff] [blame] | 7776 | if (texture.source > -1) { |
| 7777 | SerializeNumberProperty("source", texture.source, o); |
| 7778 | } |
Christophe | 820ede8 | 2019-07-04 15:21:21 +0900 | [diff] [blame] | 7779 | if (texture.name.size()) { |
| 7780 | SerializeStringProperty("name", texture.name, o); |
| 7781 | } |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7782 | SerializeExtrasAndExtensions(texture, o); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7783 | } |
| 7784 | |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7785 | /// |
| 7786 | /// Serialize all properties except buffers and images. |
| 7787 | /// |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7788 | static void SerializeGltfModel(const Model *model, detail::json &o) { |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7789 | // ACCESSORS |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 7790 | if (model->accessors.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7791 | detail::json accessors; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7792 | detail::JsonReserveArray(accessors, model->accessors.size()); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 7793 | for (unsigned int i = 0; i < model->accessors.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7794 | detail::json accessor; |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 7795 | SerializeGltfAccessor(model->accessors[i], accessor); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7796 | detail::JsonPushBack(accessors, std::move(accessor)); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 7797 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7798 | detail::JsonAddMember(o, "accessors", std::move(accessors)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7799 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7800 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7801 | // ANIMATIONS |
| 7802 | if (model->animations.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7803 | detail::json animations; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7804 | detail::JsonReserveArray(animations, model->animations.size()); |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7805 | for (unsigned int i = 0; i < model->animations.size(); ++i) { |
| 7806 | if (model->animations[i].channels.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7807 | detail::json animation; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7808 | SerializeGltfAnimation(model->animations[i], animation); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7809 | detail::JsonPushBack(animations, std::move(animation)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7810 | } |
| 7811 | } |
Syoyo Fujita | a34aa8e | 2019-09-05 14:40:32 +0900 | [diff] [blame] | 7812 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7813 | detail::JsonAddMember(o, "animations", std::move(animations)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7814 | } |
| 7815 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 7816 | // ASSET |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7817 | detail::json asset; |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7818 | SerializeGltfAsset(model->asset, asset); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7819 | detail::JsonAddMember(o, "asset", std::move(asset)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 7820 | |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7821 | // BUFFERVIEWS |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 7822 | if (model->bufferViews.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7823 | detail::json bufferViews; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7824 | detail::JsonReserveArray(bufferViews, model->bufferViews.size()); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 7825 | for (unsigned int i = 0; i < model->bufferViews.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7826 | detail::json bufferView; |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 7827 | SerializeGltfBufferView(model->bufferViews[i], bufferView); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7828 | detail::JsonPushBack(bufferViews, std::move(bufferView)); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 7829 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7830 | detail::JsonAddMember(o, "bufferViews", std::move(bufferViews)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7831 | } |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7832 | |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7833 | // Extensions required |
| 7834 | if (model->extensionsRequired.size()) { |
| 7835 | SerializeStringArrayProperty("extensionsRequired", |
| 7836 | model->extensionsRequired, o); |
| 7837 | } |
| 7838 | |
| 7839 | // MATERIALS |
| 7840 | if (model->materials.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7841 | detail::json materials; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7842 | detail::JsonReserveArray(materials, model->materials.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7843 | for (unsigned int i = 0; i < model->materials.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7844 | detail::json material; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7845 | SerializeGltfMaterial(model->materials[i], material); |
Syoyo Fujita | 68adc4b | 2020-10-22 22:27:12 +0900 | [diff] [blame] | 7846 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7847 | if (detail::JsonIsNull(material)) { |
Syoyo Fujita | 68adc4b | 2020-10-22 22:27:12 +0900 | [diff] [blame] | 7848 | // Issue 294. |
| 7849 | // `material` does not have any required parameters |
Syoyo Fujita | 7905a5b | 2021-01-21 20:33:11 +0900 | [diff] [blame] | 7850 | // so the result may be null(unmodified) when all material parameters |
| 7851 | // have default value. |
Syoyo Fujita | 68adc4b | 2020-10-22 22:27:12 +0900 | [diff] [blame] | 7852 | // |
| 7853 | // null is not allowed thus we create an empty JSON object. |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7854 | detail::JsonSetObject(material); |
Syoyo Fujita | 68adc4b | 2020-10-22 22:27:12 +0900 | [diff] [blame] | 7855 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7856 | detail::JsonPushBack(materials, std::move(material)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7857 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7858 | detail::JsonAddMember(o, "materials", std::move(materials)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7859 | } |
| 7860 | |
| 7861 | // MESHES |
| 7862 | if (model->meshes.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7863 | detail::json meshes; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7864 | detail::JsonReserveArray(meshes, model->meshes.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7865 | for (unsigned int i = 0; i < model->meshes.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7866 | detail::json mesh; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7867 | SerializeGltfMesh(model->meshes[i], mesh); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7868 | detail::JsonPushBack(meshes, std::move(mesh)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7869 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7870 | detail::JsonAddMember(o, "meshes", std::move(meshes)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7871 | } |
| 7872 | |
| 7873 | // NODES |
| 7874 | if (model->nodes.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7875 | detail::json nodes; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7876 | detail::JsonReserveArray(nodes, model->nodes.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7877 | for (unsigned int i = 0; i < model->nodes.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7878 | detail::json node; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7879 | SerializeGltfNode(model->nodes[i], node); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7880 | detail::JsonPushBack(nodes, std::move(node)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7881 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7882 | detail::JsonAddMember(o, "nodes", std::move(nodes)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7883 | } |
| 7884 | |
| 7885 | // SCENE |
| 7886 | if (model->defaultScene > -1) { |
| 7887 | SerializeNumberProperty<int>("scene", model->defaultScene, o); |
| 7888 | } |
| 7889 | |
| 7890 | // SCENES |
| 7891 | if (model->scenes.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7892 | detail::json scenes; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7893 | detail::JsonReserveArray(scenes, model->scenes.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7894 | for (unsigned int i = 0; i < model->scenes.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7895 | detail::json currentScene; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7896 | SerializeGltfScene(model->scenes[i], currentScene); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7897 | detail::JsonPushBack(scenes, std::move(currentScene)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7898 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7899 | detail::JsonAddMember(o, "scenes", std::move(scenes)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7900 | } |
| 7901 | |
| 7902 | // SKINS |
| 7903 | if (model->skins.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7904 | detail::json skins; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7905 | detail::JsonReserveArray(skins, model->skins.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7906 | for (unsigned int i = 0; i < model->skins.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7907 | detail::json skin; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7908 | SerializeGltfSkin(model->skins[i], skin); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7909 | detail::JsonPushBack(skins, std::move(skin)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7910 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7911 | detail::JsonAddMember(o, "skins", std::move(skins)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7912 | } |
| 7913 | |
| 7914 | // TEXTURES |
| 7915 | if (model->textures.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7916 | detail::json textures; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7917 | detail::JsonReserveArray(textures, model->textures.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7918 | for (unsigned int i = 0; i < model->textures.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7919 | detail::json texture; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7920 | SerializeGltfTexture(model->textures[i], texture); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7921 | detail::JsonPushBack(textures, std::move(texture)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7922 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7923 | detail::JsonAddMember(o, "textures", std::move(textures)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7924 | } |
| 7925 | |
| 7926 | // SAMPLERS |
| 7927 | if (model->samplers.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7928 | detail::json samplers; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7929 | detail::JsonReserveArray(samplers, model->samplers.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7930 | for (unsigned int i = 0; i < model->samplers.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7931 | detail::json sampler; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7932 | SerializeGltfSampler(model->samplers[i], sampler); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7933 | detail::JsonPushBack(samplers, std::move(sampler)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7934 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7935 | detail::JsonAddMember(o, "samplers", std::move(samplers)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7936 | } |
| 7937 | |
| 7938 | // CAMERAS |
| 7939 | if (model->cameras.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7940 | detail::json cameras; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7941 | detail::JsonReserveArray(cameras, model->cameras.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7942 | for (unsigned int i = 0; i < model->cameras.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7943 | detail::json camera; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7944 | SerializeGltfCamera(model->cameras[i], camera); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7945 | detail::JsonPushBack(cameras, std::move(camera)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7946 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7947 | detail::JsonAddMember(o, "cameras", std::move(cameras)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7948 | } |
| 7949 | |
David Siegel | 07616e8 | 2023-06-06 01:24:53 +0200 | [diff] [blame] | 7950 | // EXTRAS & EXTENSIONS |
| 7951 | SerializeExtrasAndExtensions(*model, o); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7952 | |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 7953 | auto extensionsUsed = model->extensionsUsed; |
| 7954 | |
| 7955 | // LIGHTS as KHR_lights_punctual |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7956 | if (model->lights.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7957 | detail::json lights; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7958 | detail::JsonReserveArray(lights, model->lights.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7959 | for (unsigned int i = 0; i < model->lights.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7960 | detail::json light; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7961 | SerializeGltfLight(model->lights[i], light); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7962 | detail::JsonPushBack(lights, std::move(light)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7963 | } |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7964 | detail::json khr_lights_cmn; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7965 | detail::JsonAddMember(khr_lights_cmn, "lights", std::move(lights)); |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7966 | detail::json ext_j; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7967 | |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7968 | { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 7969 | detail::json_const_iterator it; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7970 | if (detail::FindMember(o, "extensions", it)) { |
| 7971 | detail::JsonAssign(ext_j, detail::GetValue(it)); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 7972 | } |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7973 | } |
| 7974 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7975 | detail::JsonAddMember(ext_j, "KHR_lights_punctual", std::move(khr_lights_cmn)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 7976 | |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 7977 | detail::JsonAddMember(o, "extensions", std::move(ext_j)); |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 7978 | |
| 7979 | // Also add "KHR_lights_punctual" to `extensionsUsed` |
| 7980 | { |
Rahul Sheth | 96a8b2c | 2020-07-10 14:27:46 -0400 | [diff] [blame] | 7981 | auto has_khr_lights_punctual = |
| 7982 | std::find_if(extensionsUsed.begin(), extensionsUsed.end(), |
| 7983 | [](const std::string &s) { |
cwbhhjl | 2f5aa9f | 2020-06-04 10:40:38 +0800 | [diff] [blame] | 7984 | return (s.compare("KHR_lights_punctual") == 0); |
| 7985 | }); |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 7986 | |
| 7987 | if (has_khr_lights_punctual == extensionsUsed.end()) { |
| 7988 | extensionsUsed.push_back("KHR_lights_punctual"); |
| 7989 | } |
| 7990 | } |
| 7991 | } |
| 7992 | |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 7993 | // KHR_audio |
| 7994 | if (!model->audioEmitters.empty() || !model->audioSources.empty()) { |
| 7995 | detail::json emitters; |
| 7996 | detail::JsonReserveArray(emitters, model->audioEmitters.size()); |
| 7997 | for (unsigned int i = 0; i < model->audioEmitters.size(); ++i) { |
| 7998 | detail::json emitter; |
| 7999 | SerializeGltfAudioEmitter(model->audioEmitters[i], emitter); |
| 8000 | detail::JsonPushBack(emitters, std::move(emitter)); |
| 8001 | } |
| 8002 | detail::json khr_audio_cmn; |
| 8003 | detail::JsonAddMember(khr_audio_cmn, "emitters", std::move(emitters)); |
| 8004 | |
| 8005 | detail::json sources; |
| 8006 | detail::JsonReserveArray(sources, model->audioSources.size()); |
| 8007 | for (unsigned int i = 0; i < model->audioSources.size(); ++i) { |
| 8008 | detail::json source; |
| 8009 | SerializeGltfAudioSource(model->audioSources[i], source); |
| 8010 | detail::JsonPushBack(sources, std::move(source)); |
| 8011 | } |
| 8012 | detail::JsonAddMember(khr_audio_cmn, "sources", std::move(sources)); |
| 8013 | |
| 8014 | detail::json ext_j; |
| 8015 | { |
| 8016 | detail::json_const_iterator it; |
| 8017 | if (detail::FindMember(o, "extensions", it)) { |
| 8018 | detail::JsonAssign(ext_j, detail::GetValue(it)); |
| 8019 | } |
| 8020 | } |
| 8021 | |
| 8022 | detail::JsonAddMember(ext_j, "KHR_audio", std::move(khr_audio_cmn)); |
| 8023 | |
| 8024 | detail::JsonAddMember(o, "extensions", std::move(ext_j)); |
Baranob_Ilya | c9657be | 2023-06-12 12:34:34 +0400 | [diff] [blame] | 8025 | |
| 8026 | // Also add "KHR_audio" to `extensionsUsed` |
| 8027 | { |
| 8028 | auto has_khr_audio = |
| 8029 | std::find_if(extensionsUsed.begin(), extensionsUsed.end(), |
| 8030 | [](const std::string &s) { |
| 8031 | return (s.compare("KHR_audio") == 0); |
| 8032 | }); |
| 8033 | |
| 8034 | if (has_khr_audio == extensionsUsed.end()) { |
| 8035 | extensionsUsed.push_back("KHR_audio"); |
| 8036 | } |
| 8037 | } |
Baranob_Ilya | 78864c8 | 2023-06-12 10:43:52 +0400 | [diff] [blame] | 8038 | } |
| 8039 | |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8040 | // Extensions used |
Selmar | 1208f05 | 2021-02-01 19:24:41 +0100 | [diff] [blame] | 8041 | if (extensionsUsed.size()) { |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8042 | SerializeStringArrayProperty("extensionsUsed", extensionsUsed, o); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8043 | } |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8044 | } |
| 8045 | |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 8046 | static bool WriteGltfStream(std::ostream &stream, const std::string &content) { |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8047 | stream << content << std::endl; |
Marco Langer | 7658624 | 2023-03-12 19:26:05 +0100 | [diff] [blame] | 8048 | return stream.good(); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8049 | } |
| 8050 | |
| 8051 | static bool WriteGltfFile(const std::string &output, |
| 8052 | const std::string &content) { |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 8053 | #ifdef _WIN32 |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 8054 | #if defined(_MSC_VER) |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 8055 | std::ofstream gltfFile(UTF8ToWchar(output).c_str()); |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 8056 | #elif defined(__GLIBCXX__) |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8057 | int file_descriptor = _wopen(UTF8ToWchar(output).c_str(), |
| 8058 | _O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY); |
| 8059 | __gnu_cxx::stdio_filebuf<char> wfile_buf( |
| 8060 | file_descriptor, std::ios_base::out | std::ios_base::binary); |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 8061 | std::ostream gltfFile(&wfile_buf); |
| 8062 | if (!wfile_buf.is_open()) return false; |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 8063 | #else |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8064 | std::ofstream gltfFile(output.c_str()); |
| 8065 | if (!gltfFile.is_open()) return false; |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 8066 | #endif |
| 8067 | #else |
| 8068 | std::ofstream gltfFile(output.c_str()); |
| 8069 | if (!gltfFile.is_open()) return false; |
| 8070 | #endif |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8071 | return WriteGltfStream(gltfFile, content); |
| 8072 | } |
| 8073 | |
Syoyo Fujita | 81f7dbe | 2022-08-02 01:05:34 +0900 | [diff] [blame] | 8074 | static bool WriteBinaryGltfStream(std::ostream &stream, |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8075 | const std::string &content, |
| 8076 | const std::vector<unsigned char> &binBuffer) { |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8077 | const std::string header = "glTF"; |
| 8078 | const int version = 2; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8079 | |
Alexander Wood | 190382a | 2021-10-08 12:19:13 -0400 | [diff] [blame] | 8080 | const uint32_t content_size = uint32_t(content.size()); |
| 8081 | const uint32_t binBuffer_size = uint32_t(binBuffer.size()); |
| 8082 | // determine number of padding bytes required to ensure 4 byte alignment |
Syoyo Fujita | 52ff00a | 2022-08-16 20:08:45 +0900 | [diff] [blame] | 8083 | const uint32_t content_padding_size = |
| 8084 | content_size % 4 == 0 ? 0 : 4 - content_size % 4; |
| 8085 | const uint32_t bin_padding_size = |
| 8086 | binBuffer_size % 4 == 0 ? 0 : 4 - binBuffer_size % 4; |
Syoyo Fujita | 1d20520 | 2019-11-16 17:00:17 +0900 | [diff] [blame] | 8087 | |
| 8088 | // 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] | 8089 | // 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] | 8090 | const uint32_t length = |
Alexander Wood | 9e3d1f6 | 2021-10-08 16:57:56 -0400 | [diff] [blame] | 8091 | 12 + 8 + content_size + content_padding_size + |
Alexander Wood | 1380365 | 2021-10-08 20:13:07 -0400 | [diff] [blame] | 8092 | (binBuffer_size ? (8 + binBuffer_size + bin_padding_size) : 0); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8093 | |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 8094 | stream.write(header.c_str(), std::streamsize(header.size())); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8095 | stream.write(reinterpret_cast<const char *>(&version), sizeof(version)); |
| 8096 | stream.write(reinterpret_cast<const char *>(&length), sizeof(length)); |
| 8097 | |
| 8098 | // JSON chunk info, then JSON data |
Alexander Wood | 9e3d1f6 | 2021-10-08 16:57:56 -0400 | [diff] [blame] | 8099 | const uint32_t model_length = uint32_t(content.size()) + content_padding_size; |
Syoyo Fujita | 72f4a55 | 2020-01-08 00:40:41 +0900 | [diff] [blame] | 8100 | const uint32_t model_format = 0x4E4F534A; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8101 | stream.write(reinterpret_cast<const char *>(&model_length), |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 8102 | sizeof(model_length)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8103 | stream.write(reinterpret_cast<const char *>(&model_format), |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 8104 | sizeof(model_format)); |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 8105 | stream.write(content.c_str(), std::streamsize(content.size())); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8106 | |
| 8107 | // Chunk must be multiplies of 4, so pad with spaces |
Alexander Wood | 9e3d1f6 | 2021-10-08 16:57:56 -0400 | [diff] [blame] | 8108 | if (content_padding_size > 0) { |
| 8109 | const std::string padding = std::string(size_t(content_padding_size), ' '); |
Syoyo Fujita | cea69e3 | 2019-08-20 17:10:30 +0900 | [diff] [blame] | 8110 | stream.write(padding.c_str(), std::streamsize(padding.size())); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8111 | } |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 8112 | if (binBuffer.size() > 0) { |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8113 | // BIN chunk info, then BIN data |
Syoyo Fujita | 72f4a55 | 2020-01-08 00:40:41 +0900 | [diff] [blame] | 8114 | const uint32_t bin_length = uint32_t(binBuffer.size()) + bin_padding_size; |
| 8115 | const uint32_t bin_format = 0x004e4942; |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8116 | stream.write(reinterpret_cast<const char *>(&bin_length), |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 8117 | sizeof(bin_length)); |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8118 | stream.write(reinterpret_cast<const char *>(&bin_format), |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 8119 | sizeof(bin_format)); |
| 8120 | stream.write(reinterpret_cast<const char *>(binBuffer.data()), |
| 8121 | std::streamsize(binBuffer.size())); |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8122 | // Chunksize must be multiplies of 4, so pad with zeroes |
| 8123 | if (bin_padding_size > 0) { |
Syoyo Fujita | c4166e4 | 2020-01-08 02:38:01 +0900 | [diff] [blame] | 8124 | const std::vector<unsigned char> padding = |
| 8125 | std::vector<unsigned char>(size_t(bin_padding_size), 0); |
| 8126 | stream.write(reinterpret_cast<const char *>(padding.data()), |
| 8127 | std::streamsize(padding.size())); |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8128 | } |
| 8129 | } |
Syoyo Fujita | 81f7dbe | 2022-08-02 01:05:34 +0900 | [diff] [blame] | 8130 | |
Marco Langer | 7658624 | 2023-03-12 19:26:05 +0100 | [diff] [blame] | 8131 | stream.flush(); |
| 8132 | return stream.good(); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8133 | } |
| 8134 | |
Syoyo Fujita | 81f7dbe | 2022-08-02 01:05:34 +0900 | [diff] [blame] | 8135 | static bool WriteBinaryGltfFile(const std::string &output, |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8136 | const std::string &content, |
| 8137 | const std::vector<unsigned char> &binBuffer) { |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 8138 | #ifdef _WIN32 |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 8139 | #if defined(_MSC_VER) |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 8140 | std::ofstream gltfFile(UTF8ToWchar(output).c_str(), std::ios::binary); |
Syoyo Fujita | 45cac78 | 2019-11-09 20:42:55 +0900 | [diff] [blame] | 8141 | #elif defined(__GLIBCXX__) |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8142 | int file_descriptor = _wopen(UTF8ToWchar(output).c_str(), |
| 8143 | _O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY); |
| 8144 | __gnu_cxx::stdio_filebuf<char> wfile_buf( |
| 8145 | file_descriptor, std::ios_base::out | std::ios_base::binary); |
Syoyo Fujita | 4ab0386 | 2019-11-10 15:31:17 +0900 | [diff] [blame] | 8146 | std::ostream gltfFile(&wfile_buf); |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 8147 | #else |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8148 | std::ofstream gltfFile(output.c_str(), std::ios::binary); |
Harokyang | fb25660 | 2019-10-30 16:13:52 +0800 | [diff] [blame] | 8149 | #endif |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8150 | #else |
| 8151 | std::ofstream gltfFile(output.c_str(), std::ios::binary); |
jrkoonce | cba5d6c | 2019-08-29 11:26:22 -0500 | [diff] [blame] | 8152 | #endif |
Syoyo Fujita | 81f7dbe | 2022-08-02 01:05:34 +0900 | [diff] [blame] | 8153 | return WriteBinaryGltfStream(gltfFile, content, binBuffer); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8154 | } |
| 8155 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 8156 | bool TinyGLTF::WriteGltfSceneToStream(const Model *model, std::ostream &stream, |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8157 | bool prettyPrint = true, |
| 8158 | bool writeBinary = false) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8159 | detail::JsonDocument output; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8160 | |
| 8161 | /// Serialize all properties except buffers and images. |
| 8162 | SerializeGltfModel(model, output); |
| 8163 | |
| 8164 | // BUFFERS |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8165 | std::vector<unsigned char> binBuffer; |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8166 | if (model->buffers.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8167 | detail::json buffers; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8168 | detail::JsonReserveArray(buffers, model->buffers.size()); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8169 | for (unsigned int i = 0; i < model->buffers.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8170 | detail::json buffer; |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8171 | if (writeBinary && i == 0 && model->buffers[i].uri.empty()) { |
| 8172 | SerializeGltfBufferBin(model->buffers[i], buffer, binBuffer); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8173 | } else { |
| 8174 | SerializeGltfBuffer(model->buffers[i], buffer); |
| 8175 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8176 | detail::JsonPushBack(buffers, std::move(buffer)); |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8177 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8178 | detail::JsonAddMember(output, "buffers", std::move(buffers)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8179 | } |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8180 | |
| 8181 | // IMAGES |
| 8182 | if (model->images.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8183 | detail::json images; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8184 | detail::JsonReserveArray(images, model->images.size()); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8185 | for (unsigned int i = 0; i < model->images.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8186 | detail::json image; |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8187 | |
| 8188 | std::string dummystring = ""; |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8189 | // UpdateImageObject need baseDir but only uses it if embeddedImages is |
| 8190 | // enabled, since we won't write separate images when writing to a stream |
| 8191 | // we |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 8192 | std::string uri; |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 8193 | if (!UpdateImageObject(model->images[i], dummystring, int(i), true, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8194 | &uri_cb, &this->WriteImageData, |
| 8195 | this->write_image_user_data_, &uri)) { |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 8196 | return false; |
| 8197 | } |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 8198 | SerializeGltfImage(model->images[i], uri, image); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8199 | detail::JsonPushBack(images, std::move(image)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8200 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8201 | detail::JsonAddMember(output, "images", std::move(images)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8202 | } |
| 8203 | |
| 8204 | if (writeBinary) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8205 | return WriteBinaryGltfStream(stream, detail::JsonToString(output), binBuffer); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8206 | } else { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8207 | return WriteGltfStream(stream, detail::JsonToString(output, prettyPrint ? 2 : -1)); |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8208 | } |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8209 | } |
| 8210 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 8211 | bool TinyGLTF::WriteGltfSceneToFile(const Model *model, |
| 8212 | const std::string &filename, |
Johan Bowald | 1af7c1d | 2019-07-16 14:50:46 +0200 | [diff] [blame] | 8213 | bool embedImages = false, |
| 8214 | bool embedBuffers = false, |
| 8215 | bool prettyPrint = true, |
| 8216 | bool writeBinary = false) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8217 | detail::JsonDocument output; |
Selmar Kok | 7cb31e4 | 2018-10-05 16:02:29 +0200 | [diff] [blame] | 8218 | std::string defaultBinFilename = GetBaseFilename(filename); |
| 8219 | std::string defaultBinFileExt = ".bin"; |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 8220 | std::string::size_type pos = |
| 8221 | defaultBinFilename.rfind('.', defaultBinFilename.length()); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8222 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 8223 | if (pos != std::string::npos) { |
Selmar Kok | 7cb31e4 | 2018-10-05 16:02:29 +0200 | [diff] [blame] | 8224 | defaultBinFilename = defaultBinFilename.substr(0, pos); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8225 | } |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 8226 | std::string baseDir = GetBaseDir(filename); |
| 8227 | if (baseDir.empty()) { |
| 8228 | baseDir = "./"; |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 8229 | } |
Johan Bowald | 52936a0 | 2019-07-17 09:06:45 +0200 | [diff] [blame] | 8230 | /// Serialize all properties except buffers and images. |
| 8231 | SerializeGltfModel(model, output); |
Syoyo Fujita | d42767e | 2018-03-15 21:52:00 -0500 | [diff] [blame] | 8232 | |
Selmar Kok | 7cb31e4 | 2018-10-05 16:02:29 +0200 | [diff] [blame] | 8233 | // BUFFERS |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8234 | std::vector<std::string> usedFilenames; |
Eero Pajarre | 7b0d81b | 2019-11-18 14:15:48 +0200 | [diff] [blame] | 8235 | std::vector<unsigned char> binBuffer; |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8236 | if (model->buffers.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8237 | detail::json buffers; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8238 | detail::JsonReserveArray(buffers, model->buffers.size()); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8239 | for (unsigned int i = 0; i < model->buffers.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8240 | detail::json buffer; |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8241 | if (writeBinary && i == 0 && model->buffers[i].uri.empty()) { |
| 8242 | SerializeGltfBufferBin(model->buffers[i], buffer, binBuffer); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8243 | } else if (embedBuffers) { |
| 8244 | SerializeGltfBuffer(model->buffers[i], buffer); |
Arthur Brainville (Ybalrid) | 9223d31 | 2019-03-06 14:00:56 +0000 | [diff] [blame] | 8245 | } else { |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8246 | std::string binSavePath; |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8247 | std::string binFilename; |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8248 | std::string binUri; |
Syoyo Fujita | 6fad7ad | 2020-05-15 21:32:06 +0900 | [diff] [blame] | 8249 | if (!model->buffers[i].uri.empty() && |
| 8250 | !IsDataURI(model->buffers[i].uri)) { |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8251 | binUri = model->buffers[i].uri; |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8252 | if (!uri_cb.decode(binUri, &binFilename, uri_cb.user_data)) { |
| 8253 | return false; |
| 8254 | } |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8255 | } else { |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8256 | binFilename = defaultBinFilename + defaultBinFileExt; |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8257 | bool inUse = true; |
| 8258 | int numUsed = 0; |
| 8259 | while (inUse) { |
| 8260 | inUse = false; |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8261 | for (const std::string &usedName : usedFilenames) { |
| 8262 | if (binFilename.compare(usedName) != 0) continue; |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8263 | inUse = true; |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8264 | binFilename = defaultBinFilename + std::to_string(numUsed++) + |
| 8265 | defaultBinFileExt; |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8266 | break; |
| 8267 | } |
Selmar Kok | c884e58 | 2018-10-05 16:25:54 +0200 | [diff] [blame] | 8268 | } |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8269 | |
| 8270 | if (uri_cb.encode) { |
| 8271 | if (!uri_cb.encode(binFilename, "buffer", &binUri, |
| 8272 | uri_cb.user_data)) { |
| 8273 | return false; |
| 8274 | } |
| 8275 | } else { |
| 8276 | binUri = binFilename; |
| 8277 | } |
Selmar Kok | c884e58 | 2018-10-05 16:25:54 +0200 | [diff] [blame] | 8278 | } |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8279 | usedFilenames.push_back(binFilename); |
| 8280 | binSavePath = JoinPath(baseDir, binFilename); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8281 | if (!SerializeGltfBuffer(model->buffers[i], buffer, binSavePath, |
| 8282 | binUri)) { |
| 8283 | return false; |
| 8284 | } |
Selmar Kok | c884e58 | 2018-10-05 16:25:54 +0200 | [diff] [blame] | 8285 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8286 | detail::JsonPushBack(buffers, std::move(buffer)); |
johan bowald | 30c5347 | 2018-03-30 11:49:36 +0200 | [diff] [blame] | 8287 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8288 | detail::JsonAddMember(output, "buffers", std::move(buffers)); |
Selmar Kok | eb9d29c | 2020-01-28 13:45:38 +0100 | [diff] [blame] | 8289 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8290 | |
Syoyo Fujita | 2840a0e | 2017-06-21 02:52:11 +0900 | [diff] [blame] | 8291 | // IMAGES |
Johan Bowald | faa2722 | 2018-03-28 14:44:45 +0200 | [diff] [blame] | 8292 | if (model->images.size()) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8293 | detail::json images; |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8294 | detail::JsonReserveArray(images, model->images.size()); |
Johan Bowald | faa2722 | 2018-03-28 14:44:45 +0200 | [diff] [blame] | 8295 | for (unsigned int i = 0; i < model->images.size(); ++i) { |
David | 03ad33c | 2023-02-15 23:35:51 -0600 | [diff] [blame] | 8296 | detail::json image; |
johan bowald | 642a343 | 2018-04-01 12:37:18 +0200 | [diff] [blame] | 8297 | |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 8298 | std::string uri; |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 8299 | if (!UpdateImageObject(model->images[i], baseDir, int(i), embedImages, |
Pyarelal Knowles | 385946d | 2023-01-03 18:18:04 -0800 | [diff] [blame] | 8300 | &uri_cb, &this->WriteImageData, |
| 8301 | this->write_image_user_data_, &uri)) { |
Pyarelal Knowles | d2b0af6 | 2022-12-27 16:38:48 -0800 | [diff] [blame] | 8302 | return false; |
| 8303 | } |
Pyarelal Knowles | de75d87 | 2022-12-13 15:50:41 -0800 | [diff] [blame] | 8304 | SerializeGltfImage(model->images[i], uri, image); |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8305 | detail::JsonPushBack(images, std::move(image)); |
Johan Bowald | faa2722 | 2018-03-28 14:44:45 +0200 | [diff] [blame] | 8306 | } |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8307 | detail::JsonAddMember(output, "images", std::move(images)); |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8308 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8309 | |
David Harmon | da9eac2 | 2018-08-30 08:06:05 -0400 | [diff] [blame] | 8310 | if (writeBinary) { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8311 | return WriteBinaryGltfFile(filename, detail::JsonToString(output), binBuffer); |
David Harmon | da9eac2 | 2018-08-30 08:06:05 -0400 | [diff] [blame] | 8312 | } else { |
David | 1f9a4b9 | 2023-02-15 22:56:18 -0600 | [diff] [blame] | 8313 | return WriteGltfFile(filename, detail::JsonToString(output, (prettyPrint ? 2 : -1))); |
David Harmon | da9eac2 | 2018-08-30 08:06:05 -0400 | [diff] [blame] | 8314 | } |
Aurélien Chatelain | cc8ba99 | 2017-06-19 13:53:13 +0000 | [diff] [blame] | 8315 | } |
| 8316 | |
Syoyo Fujita | e1bef8c | 2016-03-17 21:31:13 +0900 | [diff] [blame] | 8317 | } // namespace tinygltf |
| 8318 | |
Syoyo Fujita | 05e0bf1 | 2018-01-16 18:55:13 +0900 | [diff] [blame] | 8319 | #ifdef __clang__ |
| 8320 | #pragma clang diagnostic pop |
| 8321 | #endif |
| 8322 | |
Syoyo Fujita | 612e578 | 2022-09-18 21:01:39 +0900 | [diff] [blame] | 8323 | #endif // TINYGLTF_IMPLEMENTATION |