From 8eb3904de2ca9e118a2b1a7ed60be635f30497f1 Mon Sep 17 00:00:00 2001 From: Selmar Kok Date: Fri, 5 Oct 2018 14:29:35 +0200 Subject: [PATCH 1/5] - Parse image extras property - Optionally serialize texture.source (it is not required in the current spec) --- tiny_gltf.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tiny_gltf.h b/tiny_gltf.h index ad54543..b11ff5c 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -487,7 +487,7 @@ struct Texture { std::string name; int sampler; - int source; // Required (not specified in the spec ?) + int source; Value extras; ExtensionMap extensions; @@ -2475,6 +2475,7 @@ static bool ParseImage(Image *image, std::string *err, std::string *warn, ParseStringProperty(&image->name, err, o, "name", false); ParseExtensionsProperty(&image->extensions, err, o); + ParseExtrasProperty(&image->extras, o); if (hasBufferView) { double bufferView = -1; @@ -4578,8 +4579,9 @@ static void SerializeGltfTexture(Texture &texture, json &o) { if (texture.sampler > -1) { SerializeNumberProperty("sampler", texture.sampler, o); } - SerializeNumberProperty("source", texture.source, o); - + if (texture.source > -1) { + SerializeNumberProperty("source", texture.source, o); + } if (texture.extras.Type() != NULL_TYPE) { SerializeValue("extras", texture.extras, o); } From 2bda71c8fbb33c048d082c493500f425f469fed9 Mon Sep 17 00:00:00 2001 From: Selmar Kok Date: Fri, 5 Oct 2018 14:36:05 +0200 Subject: [PATCH 2/5] - always check Parameter::number_value to catch user errors, e.g. setting number_value without setting has_number_value, which will cause it to serialize as a bool but read back in as a float anyway --- tiny_gltf.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tiny_gltf.h b/tiny_gltf.h index b0f3912..e56b02d 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -1245,9 +1245,8 @@ bool Parameter::operator==(const Parameter &other) const { this->has_number_value != other.has_number_value) return false; - if (this->has_number_value) - if (!TINYGLTF_DOUBLE_EQUAL(this->number_value, other.number_value)) - return false; + if (!TINYGLTF_DOUBLE_EQUAL(this->number_value, other.number_value)) + return false; if (this->json_double_value.size() != other.json_double_value.size()) return false; From 7cb31e4e23cf9af41e3007496fb84fff9d78e32f Mon Sep 17 00:00:00 2001 From: Selmar Kok Date: Fri, 5 Oct 2018 16:02:29 +0200 Subject: [PATCH 3/5] - support writing multiple bin files - use existing buffer.uri as filename if not empty and not a data uri --- tiny_gltf.h | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/tiny_gltf.h b/tiny_gltf.h index e56b02d..b81dc2f 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -4554,31 +4554,41 @@ bool TinyGLTF::WriteGltfSceneToFile(Model *model, const std::string &filename, SerializeGltfAsset(model->asset, asset); output["asset"] = asset; - std::string binFilename = GetBaseFilename(filename); - std::string ext = ".bin"; - std::string::size_type pos = binFilename.rfind('.', binFilename.length()); + std::string defaultBinFilename = GetBaseFilename(filename); + std::string defaultBinFileExt = ".bin"; + std::string::size_type pos = defaultBinFilename.rfind('.', defaultBinFilename.length()); if (pos != std::string::npos) { - binFilename = binFilename.substr(0, pos) + ext; - } else { - binFilename = binFilename + ".bin"; + defaultBinFilename = defaultBinFilename.substr(0, pos); } std::string baseDir = GetBaseDir(filename); if (baseDir.empty()) { baseDir = "./"; } - std::string binSaveFilePath = JoinPath(baseDir, binFilename); - - // BUFFERS (We expect only one buffer here) + // BUFFERS + int numDefaultBufferNamesUsed = 0; json buffers; for (unsigned int i = 0; i < model->buffers.size(); ++i) { json buffer; if (embedBuffers) { SerializeGltfBuffer(model->buffers[i], buffer); } else { - SerializeGltfBuffer(model->buffers[i], buffer, binSaveFilePath, - binFilename); + std::string binSavePath; + std::string binUri; + if (!model->buffers[i].uri.empty() + && !IsDataURI(model->buffers[i].uri)) { + binUri = model->buffers[i].uri; + } + else { + binUri = defaultBinFilename; + if(numDefaultBufferNamesUsed > 0) + binUri += std::to_string(numDefaultBufferNamesUsed++); + binUri += defaultBinFileExt; + } + binSavePath = JoinPath(baseDir, binUri); + SerializeGltfBuffer(model->buffers[i], buffer, binSavePath, + binUri); } buffers.push_back(buffer); } From c884e5827e3172ec23c27258f492a80fe491cd71 Mon Sep 17 00:00:00 2001 From: Selmar Kok Date: Fri, 5 Oct 2018 16:25:54 +0200 Subject: [PATCH 4/5] better implementation for bin file name checking --- tiny_gltf.h | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/tiny_gltf.h b/tiny_gltf.h index b81dc2f..df29757 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -4567,7 +4567,7 @@ bool TinyGLTF::WriteGltfSceneToFile(Model *model, const std::string &filename, } // BUFFERS - int numDefaultBufferNamesUsed = 0; + std::vector usedUris; json buffers; for (unsigned int i = 0; i < model->buffers.size(); ++i) { json buffer; @@ -4575,17 +4575,26 @@ bool TinyGLTF::WriteGltfSceneToFile(Model *model, const std::string &filename, SerializeGltfBuffer(model->buffers[i], buffer); } else { std::string binSavePath; - std::string binUri; - if (!model->buffers[i].uri.empty() + std::string binUri; + if (!model->buffers[i].uri.empty() && !IsDataURI(model->buffers[i].uri)) { binUri = model->buffers[i].uri; - } - else { - binUri = defaultBinFilename; - if(numDefaultBufferNamesUsed > 0) - binUri += std::to_string(numDefaultBufferNamesUsed++); - binUri += defaultBinFileExt; - } + } + else { + binUri = defaultBinFilename + defaultBinFileExt; + bool inUse = true; + int numUsed = 0; + while(inUse) { + inUse = false; + for (const std::string& usedName : usedUris) { + if (binUri.compare(usedName) != 0) continue; + inUse = true; + binUri = defaultBinFilename + std::to_string(numUsed++) + defaultBinFileExt; + break; + } + } + } + usedUris.push_back(binUri); binSavePath = JoinPath(baseDir, binUri); SerializeGltfBuffer(model->buffers[i], buffer, binSavePath, binUri); From 440cb1e66b0431f014c97e4a8565a38515b923b7 Mon Sep 17 00:00:00 2001 From: Selmar Kok Date: Fri, 5 Oct 2018 16:30:50 +0200 Subject: [PATCH 5/5] tabs to spaces --- tiny_gltf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tiny_gltf.h b/tiny_gltf.h index df29757..8b43da0 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -4583,7 +4583,7 @@ bool TinyGLTF::WriteGltfSceneToFile(Model *model, const std::string &filename, else { binUri = defaultBinFilename + defaultBinFileExt; bool inUse = true; - int numUsed = 0; + int numUsed = 0; while(inUse) { inUse = false; for (const std::string& usedName : usedUris) {