diff --git a/models/box01.glb b/models/box01.glb new file mode 100644 index 0000000..7477e89 Binary files /dev/null and b/models/box01.glb differ diff --git a/test_runner.py b/test_runner.py index 26345db..fd96ad9 100644 --- a/test_runner.py +++ b/test_runner.py @@ -28,7 +28,7 @@ def run(filename): p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = p.communicate() except: - print "Failed to execute: ", cmd + print("Failed to execute: ", cmd) raise if p.returncode != 0: diff --git a/tests/tester.cc b/tests/tester.cc index 1cee2b4..377ea0e 100644 --- a/tests/tester.cc +++ b/tests/tester.cc @@ -1,5 +1,6 @@ #define TINYGLTF_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION +#define STB_IMAGE_WRITE_IMPLEMENTATION #include "tiny_gltf.h" #define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file @@ -24,3 +25,18 @@ TEST_CASE("parse-error", "[parse]") { } +TEST_CASE("datauri-in-glb", "[issue-79]") { + + tinygltf::Model model; + tinygltf::TinyGLTF ctx; + std::string err; + + bool ret = ctx.LoadBinaryFromFile(&model, &err, "../models/box01.glb"); + if (!err.empty()) { + std::cerr << err << std::endl; + } + + REQUIRE(true == ret); +} + + diff --git a/tiny_gltf.h b/tiny_gltf.h index 40f1d38..4fab08d 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -2260,11 +2260,21 @@ static bool ParseBuffer(Buffer *buffer, std::string *err, const json &o, size_t bytes = static_cast(byteLength); if (is_binary) { - // Still binary glTF accepts external dataURI. First try external resources. - + // Still binary glTF accepts external dataURI. if (!buffer->uri.empty()) { - // External .bin file. - LoadExternalFile(&buffer->data, err, buffer->uri, basedir, bytes, true, fs); + // First try embedded data URI. + if (IsDataURI(buffer->uri)) { + std::string mime_type; + if (!DecodeDataURI(&buffer->data, mime_type, buffer->uri, bytes, true)) { + if (err) { + (*err) += "Failed to decode 'uri' : " + buffer->uri + " in Buffer\n"; + } + return false; + } + } else { + // External .bin file. + LoadExternalFile(&buffer->data, err, buffer->uri, basedir, bytes, true, fs); + } } else { // load data from (embedded) binary data