From 2bd4e5790b79704bad648b6b125b6d47568ba553 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Mon, 26 Aug 2024 23:42:29 +0300 Subject: [PATCH] Support embedded textures in glTF --- libs/gfx/include/psemek/gfx/gltf_parser.hpp | 3 ++- libs/gfx/source/gltf_mesh.cpp | 12 ++++++++++-- libs/gfx/source/gltf_parser.cpp | 5 ++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/libs/gfx/include/psemek/gfx/gltf_parser.hpp b/libs/gfx/include/psemek/gfx/gltf_parser.hpp index 7ced7476..8bdc37d9 100644 --- a/libs/gfx/include/psemek/gfx/gltf_parser.hpp +++ b/libs/gfx/include/psemek/gfx/gltf_parser.hpp @@ -73,7 +73,8 @@ namespace psemek::gfx struct texture { - std::string uri; + std::optional uri; + std::optional buffer_view; }; struct skin diff --git a/libs/gfx/source/gltf_mesh.cpp b/libs/gfx/source/gltf_mesh.cpp index e52fa77e..035929b9 100644 --- a/libs/gfx/source/gltf_mesh.cpp +++ b/libs/gfx/source/gltf_mesh.cpp @@ -95,9 +95,17 @@ namespace psemek::gfx for (auto const & texture : asset.textures) { - auto data = uri_loader(texture.uri); auto & target = textures_.emplace_back(); - target.load_srgb(gfx::read_image(io::memory_istream(data.string_view()))); + + if (texture.uri) + { + auto data = uri_loader(*texture.uri); + target.load_srgb(gfx::read_image(io::memory_istream(data.string_view()))); + } + else + { + target.load_srgb(gfx::pixmap_rgba({2, 2}, gfx::color_rgba{255, 0, 255, 255})); + } target.linear_mipmap_filter(); target.anisotropy(); target.generate_mipmap(); diff --git a/libs/gfx/source/gltf_parser.cpp b/libs/gfx/source/gltf_parser.cpp index a4658b4c..6f233dfe 100644 --- a/libs/gfx/source/gltf_parser.cpp +++ b/libs/gfx/source/gltf_parser.cpp @@ -293,7 +293,10 @@ namespace psemek::gfx if (document.HasMember("images")) for (auto const & image : document["images"].GetArray()) { auto & target = result.textures.emplace_back(); - target.uri = image["uri"].GetString(); + if (image.HasMember("uri")) + target.uri = image["uri"].GetString(); + if (image.HasMember("bufferView")) + target.buffer_view = image["bufferView"].GetUint64(); } if (document.HasMember("skins")) for (auto const & skin : document["skins"].GetArray())