From ee3d06646d4b31f6f65cfeaa488ced7eddb3cd14 Mon Sep 17 00:00:00 2001 From: Selmar Kok Date: Mon, 8 Oct 2018 16:20:43 +0200 Subject: [PATCH 1/7] // pretty print as optional parameter, default true // serialize name instead of type for camera.name // create empy Value::Object if extension value parsing failed --- tiny_gltf.h | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tiny_gltf.h b/tiny_gltf.h index b42020e..9549d78 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -923,7 +923,8 @@ class TinyGLTF { /// bool WriteGltfSceneToFile(Model *model, const std::string &filename, bool embedImages, - bool embedBuffers /*, bool writeBinary*/); + bool embedBuffers, + bool prettyPrint /*, bool writeBinary*/); /// /// Set callback to use for loading image data @@ -2347,7 +2348,12 @@ static bool ParseExtensionsProperty(ExtensionMap *ret, std::string *err, json::const_iterator extIt = it.value().begin(); for (; extIt != it.value().end(); extIt++) { if (!extIt.value().is_object()) continue; - ParseJsonAsValue(&extensions[extIt.key()], extIt.value()); + if (!ParseJsonAsValue(&extensions[extIt.key()], extIt.value())) { + if (!extIt.key().empty()) { + // create empty object so that an extension object is still of type object + extensions[extIt.key()] = Value{ Value::Object{} }; + } + } } if (ret) { (*ret) = extensions; @@ -4471,7 +4477,7 @@ static void SerializeGltfPerspectiveCamera(const PerspectiveCamera &camera, static void SerializeGltfCamera(const Camera &camera, json &o) { SerializeStringProperty("type", camera.type, o); if (!camera.name.empty()) { - SerializeStringProperty("name", camera.type, o); + SerializeStringProperty("name", camera.name, o); } if (camera.type.compare("orthographic") == 0) { @@ -4533,7 +4539,8 @@ static bool WriteGltfFile(const std::string &output, bool TinyGLTF::WriteGltfSceneToFile(Model *model, const std::string &filename, bool embedImages = false, - bool embedBuffers = false + bool embedBuffers = false, + bool prettyPrint = true /*, bool writeBinary*/) { json output; @@ -4771,7 +4778,7 @@ bool TinyGLTF::WriteGltfSceneToFile(Model *model, const std::string &filename, } // pretty printing with spacing 2 - return WriteGltfFile(filename, output.dump(2)); + return WriteGltfFile(filename, output.dump(prettyPrint ? 2 : 0)); } } // namespace tinygltf From db7f4e4d044a02f90e042c5e64dde5c73718a02e Mon Sep 17 00:00:00 2001 From: Selmar Kok Date: Wed, 10 Oct 2018 18:10:58 +0200 Subject: [PATCH 2/7] fix an issue when serializing an empty extension object would serialize as type null (and thus deserialize as type null, causing it to be ignored) --- tiny_gltf.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tiny_gltf.h b/tiny_gltf.h index 9549d78..2a9b3a6 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -4126,7 +4126,8 @@ static void SerializeExtensionMap(ExtensionMap &extensions, json &o) { json ret; if (ValueToJson(extIt->second, &ret)) { extMap[extIt->first] = ret; - } else { + } + if(ret.is_null()) { if (!(extIt->first.empty())) { // name should not be empty, but for sure // create empty object so that an extension name is still included in json. extMap[extIt->first] = json({}); From d0e293fd7dab8f3a55db0f6c0a16a0693d846b29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Kov=C3=A1=C5=99?= Date: Tue, 16 Oct 2018 12:00:24 +0200 Subject: [PATCH 3/7] Fixed typos --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6fe3e3a..32d16d1 100644 --- a/README.md +++ b/README.md @@ -33,10 +33,10 @@ v2.0.0 release(22 Aug, 2018)! * [x] Binary glTF(GLB) * [x] PBR material description * Buffers - * [x] Parse BASE64 encoded embedded buffer fata(DataURI). + * [x] Parse BASE64 encoded embedded buffer data(DataURI). * [x] Load `.bin` file. * Image(Using stb_image) - * [x] Parse BASE64 encoded embedded image fata(DataURI). + * [x] Parse BASE64 encoded embedded image data(DataURI). * [x] Load external image file. * [x] PNG(8bit only) * [x] JPEG(8bit only) From 10365a53ffbc263c4184e11d05d2dbe8772b84c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Kov=C3=A1=C5=99?= Date: Tue, 16 Oct 2018 12:03:45 +0200 Subject: [PATCH 4/7] Fixed make lint --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 1396525..f3c6d3b 100644 --- a/Makefile +++ b/Makefile @@ -6,4 +6,4 @@ all: clang++ $(EXTRA_CXXFLAGS) -std=c++11 -g -O0 -o loader_example loader_example.cc lint: - ./cpplint.py tiny_gltf_loader.h + deps/cpplint.py tiny_gltf.h From 040310e0d39ded3bc5d3aa6597568bf0d2867a12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Kov=C3=A1=C5=99?= Date: Wed, 17 Oct 2018 15:24:59 +0200 Subject: [PATCH 5/7] Added include direectory ../common/ for trackball.h --- examples/glview/premake5.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/glview/premake5.lua b/examples/glview/premake5.lua index 1f41493..938d008 100644 --- a/examples/glview/premake5.lua +++ b/examples/glview/premake5.lua @@ -11,6 +11,7 @@ solution "glview" files { "glview.cc", "../common/trackball.cc" } includedirs { "./" } includedirs { "../../" } + includedirs { "../common/" } configuration { "linux" } linkoptions { "`pkg-config --libs glfw3`" } From a645cfc840d39b2f828436ce4c6b548909013b8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Kov=C3=A1=C5=99?= Date: Wed, 17 Oct 2018 15:24:59 +0200 Subject: [PATCH 6/7] Added include directory ../common/ for trackball.h --- examples/glview/premake5.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/glview/premake5.lua b/examples/glview/premake5.lua index 1f41493..938d008 100644 --- a/examples/glview/premake5.lua +++ b/examples/glview/premake5.lua @@ -11,6 +11,7 @@ solution "glview" files { "glview.cc", "../common/trackball.cc" } includedirs { "./" } includedirs { "../../" } + includedirs { "../common/" } configuration { "linux" } linkoptions { "`pkg-config --libs glfw3`" } From e46774940c0419fd4dc19b5eb04c7f54fe612086 Mon Sep 17 00:00:00 2001 From: Selmar Kok Date: Thu, 25 Oct 2018 16:45:49 +0200 Subject: [PATCH 7/7] fail serialization if a buffer file fails to be written (e.g. due to access rights) --- tiny_gltf.h | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tiny_gltf.h b/tiny_gltf.h index 2a9b3a6..464fc5a 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -4077,12 +4077,14 @@ static void SerializeGltfBufferData(const std::vector &data, SerializeStringProperty("uri", header + encodedData, o); } -static void SerializeGltfBufferData(const std::vector &data, +static bool SerializeGltfBufferData(const std::vector &data, const std::string &binFilename) { std::ofstream output(binFilename.c_str(), std::ofstream::binary); + if(!output.is_open()) return false; output.write(reinterpret_cast(&data[0]), std::streamsize(data.size())); output.close(); + return true; } static void SerializeParameterMap(ParameterMap ¶m, json &o) { @@ -4257,10 +4259,10 @@ static void SerializeGltfBuffer(Buffer &buffer, json &o) { } } -static void SerializeGltfBuffer(Buffer &buffer, json &o, +static bool SerializeGltfBuffer(Buffer &buffer, json &o, const std::string &binFilename, const std::string &binBaseFilename) { - SerializeGltfBufferData(buffer.data, binFilename); + if(!SerializeGltfBufferData(buffer.data, binFilename)) return false; SerializeNumberProperty("byteLength", buffer.data.size(), o); SerializeStringProperty("uri", binBaseFilename, o); @@ -4269,6 +4271,7 @@ static void SerializeGltfBuffer(Buffer &buffer, json &o, if (buffer.extras.Type() != NULL_TYPE) { SerializeValue("extras", buffer.extras, o); } + return true; } static void SerializeGltfBufferView(BufferView &bufferView, json &o) { @@ -4614,8 +4617,10 @@ bool TinyGLTF::WriteGltfSceneToFile(Model *model, const std::string &filename, } usedUris.push_back(binUri); binSavePath = JoinPath(baseDir, binUri); - SerializeGltfBuffer(model->buffers[i], buffer, binSavePath, - binUri); + if(!SerializeGltfBuffer(model->buffers[i], buffer, binSavePath, + binUri)) { + return false; + } } buffers.push_back(buffer); }