From 0aab1575d5b5f8e4302959d50f97c6387a74564a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Chatelain?= Date: Tue, 30 May 2017 15:03:48 +0000 Subject: [PATCH] Update buffer parsing (2.0) --- tiny_gltf_loader.h | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/tiny_gltf_loader.h b/tiny_gltf_loader.h index a371660..1798b9c 100644 --- a/tiny_gltf_loader.h +++ b/tiny_gltf_loader.h @@ -1558,9 +1558,18 @@ static bool ParseBuffer(Buffer *buffer, std::string *err, return false; } + // In glTF 2.0, uri is not mandatory anymore std::string uri; ParseStringProperty(&uri, err, o, "uri", false, "Buffer"); + // having an empty uri for a non embedded image should not be valid + if(!is_binary && uri.empty()) + { + if (err) { + (*err) += "'uri' is missing from non binary glTF file buffer.\n"; + } + } + picojson::object::const_iterator type = o.find("type"); if (type != o.end()) { if (type->second.is()) { @@ -1574,15 +1583,14 @@ static bool ParseBuffer(Buffer *buffer, std::string *err, size_t bytes = static_cast(byteLength); if (is_binary) { // Still binary glTF accepts external dataURI. First try external resources. - bool loaded = false; - if (IsDataURI(uri)) { - loaded = DecodeDataURI(&buffer->data, uri, bytes, true); - } else { - // Assume external .bin file. - loaded = LoadExternalFile(&buffer->data, err, uri, basedir, bytes, true); - } - if (!loaded) { + if(!uri.empty()) + { + // External .bin file. + LoadExternalFile(&buffer->data, err, uri, basedir, bytes, true); + } + else + { // load data from (embedded) binary data if ((bin_size == 0) || (bin_data == NULL)) { @@ -1603,18 +1611,10 @@ static bool ParseBuffer(Buffer *buffer, std::string *err, return false; } - if (uri.compare("data:,") == 0) { - // @todo { check uri } - buffer->data.resize(static_cast(byteLength)); - memcpy(&(buffer->data.at(0)), bin_data, - static_cast(byteLength)); - - } else { - if (err) { - (*err) += "Invalid URI for binary data in `Buffer'.\n"; - } - return false; - } + // Read buffer data + buffer->data.resize(static_cast(byteLength)); + memcpy(&(buffer->data.at(0)), bin_data, + static_cast(byteLength)); } } else { @@ -2571,7 +2571,7 @@ bool TinyGLTFLoader::LoadBinaryFromMemory(Model *model, std::string *err, model_length); is_binary_ = true; - bin_data_ = bytes + 20 + model_length; + bin_data_ = bytes + 20 + model_length + 8; // 4 bytes (buffer_length) + 4 bytes(buffer_format) bin_size_ = length - (20 + model_length); // extract header + JSON scene data.