From 514f4006efd42d1578d9fe7fd6f0cf47dc6d0521 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Sat, 14 Jan 2023 03:35:03 +0300 Subject: [PATCH] Parse textures in gltf_parser --- libs/gfx/include/psemek/gfx/gltf_parser.hpp | 9 +++++++- libs/gfx/source/gltf_parser.cpp | 23 ++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/libs/gfx/include/psemek/gfx/gltf_parser.hpp b/libs/gfx/include/psemek/gfx/gltf_parser.hpp index 49244ba2..66c6e882 100644 --- a/libs/gfx/include/psemek/gfx/gltf_parser.hpp +++ b/libs/gfx/include/psemek/gfx/gltf_parser.hpp @@ -34,7 +34,13 @@ namespace psemek::gfx struct material { - color_4f albedo; + std::optional albedo; + std::optional texture; + }; + + struct texture + { + std::string uri; }; struct accessor @@ -62,6 +68,7 @@ namespace psemek::gfx std::vector nodes; std::vector meshes; std::vector materials; + std::vector textures; std::vector accessors; std::vector buffer_views; std::vector buffers; diff --git a/libs/gfx/source/gltf_parser.cpp b/libs/gfx/source/gltf_parser.cpp index 93a7d657..649daba8 100644 --- a/libs/gfx/source/gltf_parser.cpp +++ b/libs/gfx/source/gltf_parser.cpp @@ -102,9 +102,26 @@ namespace psemek::gfx { auto & target = result.materials.emplace_back(); - auto const & color = material["pbrMetallicRoughness"]["baseColorFactor"].GetArray(); - for (std::size_t i = 0; i < 4; ++i) - target.albedo[i] = color[i].GetFloat(); + auto const & pbr = material["pbrMetallicRoughness"]; + + if (pbr.HasMember("baseColorFactor")) + { + auto const & color = pbr["baseColorFactor"].GetArray(); + gfx::color_4f & target_color = target.albedo.emplace(); + for (std::size_t i = 0; i < 4; ++i) + target_color[i] = color[i].GetFloat(); + } + + if (pbr.HasMember("baseColorTexture")) + { + target.texture = document["textures"].GetArray()[pbr["baseColorTexture"]["index"].GetUint64()]["source"].GetInt64(); + } + } + + for (auto const & image : document["images"].GetArray()) + { + auto & target = result.textures.emplace_back(); + target.uri = image["uri"].GetString(); } for (auto const & accessor : document["accessors"].GetArray())