From 1582b341d339191129e2ed32cf41867f6f89974f Mon Sep 17 00:00:00 2001 From: lisyarus Date: Wed, 1 Feb 2023 20:35:32 +0300 Subject: [PATCH] Support child nodes & node transforms in gltf parser --- libs/gfx/include/psemek/gfx/gltf_parser.hpp | 9 +++++- libs/gfx/source/gltf_mesh.cpp | 5 +++- libs/gfx/source/gltf_parser.cpp | 33 ++++++++++++++++++++- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/libs/gfx/include/psemek/gfx/gltf_parser.hpp b/libs/gfx/include/psemek/gfx/gltf_parser.hpp index 477c6d10..35147b97 100644 --- a/libs/gfx/include/psemek/gfx/gltf_parser.hpp +++ b/libs/gfx/include/psemek/gfx/gltf_parser.hpp @@ -2,6 +2,8 @@ #include #include +#include +#include #include #include @@ -15,7 +17,12 @@ namespace psemek::gfx struct node { std::string name; - std::size_t mesh; + std::optional mesh; + std::vector children; + + geom::vector translation; + geom::quaternion rotation; + geom::vector scale; }; struct mesh diff --git a/libs/gfx/source/gltf_mesh.cpp b/libs/gfx/source/gltf_mesh.cpp index 6abfb879..b0ddf769 100644 --- a/libs/gfx/source/gltf_mesh.cpp +++ b/libs/gfx/source/gltf_mesh.cpp @@ -80,9 +80,12 @@ namespace psemek::gfx for (auto const & node : asset.nodes) { + if (!node.mesh) + continue; + auto & target_mesh = meshes_[node.name]; - auto const & mesh = asset.meshes[node.mesh]; + auto const & mesh = asset.meshes[*node.mesh]; for (auto const & primitive : mesh.primitives) { diff --git a/libs/gfx/source/gltf_parser.cpp b/libs/gfx/source/gltf_parser.cpp index e41568f8..eeaa24eb 100644 --- a/libs/gfx/source/gltf_parser.cpp +++ b/libs/gfx/source/gltf_parser.cpp @@ -74,8 +74,39 @@ namespace psemek::gfx for (auto const & node : document["nodes"].GetArray()) { auto & target = result.nodes.emplace_back(); - target.mesh = node["mesh"].GetUint64(); target.name = node["name"].GetString(); + if (node.HasMember("mesh")) + target.mesh = node["mesh"].GetUint64(); + + target.translation = {0.f, 0.f, 0.f}; + if (node.HasMember("translation")) + { + auto const & translation = node["translation"].GetArray(); + for (std::size_t i = 0; i < 3; ++i) + target.translation[i] = translation[i].GetFloat(); + } + + target.rotation = geom::quaternion::identity(); + if (node.HasMember("rotation")) + { + auto const & rotation = node["rotation"].GetArray(); + for (std::size_t i = 0; i < 4; ++i) + target.rotation[i] = rotation[i].GetFloat(); + } + + target.scale = {0.f, 0.f, 0.f}; + if (node.HasMember("scale")) + { + auto const & scale = node["scale"].GetArray(); + for (std::size_t i = 0; i < 3; ++i) + target.scale[i] = scale[i].GetFloat(); + } + + if (node.HasMember("children")) + { + for (auto const & child : node["children"].GetArray()) + target.children.push_back(child.GetUint64()); + } } for (auto const & mesh : document["meshes"].GetArray())