diff --git a/tiny_gltf.h b/tiny_gltf.h index 7481345..5722ee1 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -27,6 +27,7 @@ // Version: // - v2.6.0 Disable expanding file path for security(no use of awkward `wordexp` anymore). +// Support serializing sparse accessor(Thanks to @fynv). // - v2.5.0 Add SetPreserveImageChannels() option to load image data as is. // - v2.4.3 Fix null object output when when material has all default // parameters. @@ -1434,6 +1435,10 @@ class TinyGLTF { bool preserve_image_channels_ = false; /// Default false(expand channels to /// RGBA) for backward compatibility. + // Warning & error messages + std::string warn_; + std::string err_; + FsCallbacks fs = { #ifndef TINYGLTF_NO_FS &tinygltf::FileExists, &tinygltf::ExpandFilePath, @@ -6711,6 +6716,27 @@ static void SerializeGltfAccessor(Accessor &accessor, json &o) { if (accessor.extras.Type() != NULL_TYPE) { SerializeValue("extras", accessor.extras, o); } + + // sparse + if (accessor.sparse.isSparse) + { + json sparse; + SerializeNumberProperty("count", accessor.sparse.count, sparse); + { + json indices; + SerializeNumberProperty("bufferView", accessor.sparse.indices.bufferView, indices); + SerializeNumberProperty("byteOffset", accessor.sparse.indices.byteOffset, indices); + SerializeNumberProperty("componentType", accessor.sparse.indices.componentType, indices); + JsonAddMember(sparse, "indices", std::move(indices)); + } + { + json values; + SerializeNumberProperty("bufferView", accessor.sparse.values.bufferView, values); + SerializeNumberProperty("byteOffset", accessor.sparse.values.byteOffset, values); + JsonAddMember(sparse, "values", std::move(values)); + } + JsonAddMember(o, "sparse", std::move(sparse)); + } } static void SerializeGltfAnimationChannel(AnimationChannel &channel, json &o) { @@ -7547,7 +7573,7 @@ static bool WriteGltfFile(const std::string &output, return WriteGltfStream(gltfFile, content); } -static void WriteBinaryGltfStream(std::ostream &stream, +static bool WriteBinaryGltfStream(std::ostream &stream, const std::string &content, const std::vector &binBuffer) { const std::string header = "glTF"; @@ -7603,9 +7629,12 @@ static void WriteBinaryGltfStream(std::ostream &stream, std::streamsize(padding.size())); } } + + // TODO: Check error on stream.write + return true; } -static void WriteBinaryGltfFile(const std::string &output, +static bool WriteBinaryGltfFile(const std::string &output, const std::string &content, const std::vector &binBuffer) { #ifdef _WIN32 @@ -7623,7 +7652,7 @@ static void WriteBinaryGltfFile(const std::string &output, #else std::ofstream gltfFile(output.c_str(), std::ios::binary); #endif - WriteBinaryGltfStream(gltfFile, content, binBuffer); + return WriteBinaryGltfStream(gltfFile, content, binBuffer); } bool TinyGLTF::WriteGltfSceneToStream(Model *model, std::ostream &stream, @@ -7671,12 +7700,11 @@ bool TinyGLTF::WriteGltfSceneToStream(Model *model, std::ostream &stream, } if (writeBinary) { - WriteBinaryGltfStream(stream, JsonToString(output), binBuffer); + return WriteBinaryGltfStream(stream, JsonToString(output), binBuffer); } else { - WriteGltfStream(stream, JsonToString(output, prettyPrint ? 2 : -1)); + return WriteGltfStream(stream, JsonToString(output, prettyPrint ? 2 : -1)); } - return true; } bool TinyGLTF::WriteGltfSceneToFile(Model *model, const std::string &filename, @@ -7761,12 +7789,11 @@ bool TinyGLTF::WriteGltfSceneToFile(Model *model, const std::string &filename, } if (writeBinary) { - WriteBinaryGltfFile(filename, JsonToString(output), binBuffer); + return WriteBinaryGltfFile(filename, JsonToString(output), binBuffer); } else { - WriteGltfFile(filename, JsonToString(output, (prettyPrint ? 2 : -1))); + return WriteGltfFile(filename, JsonToString(output, (prettyPrint ? 2 : -1))); } - return true; } } // namespace tinygltf