Support child nodes & node transforms in gltf parser

This commit is contained in:
Nikita Lisitsa 2023-02-01 20:35:32 +03:00
parent 0e84751ac9
commit 1582b341d3
3 changed files with 44 additions and 3 deletions

View file

@ -2,6 +2,8 @@
#include <psemek/io/stream.hpp>
#include <psemek/gfx/color.hpp>
#include <psemek/geom/vector.hpp>
#include <psemek/geom/quaternion.hpp>
#include <vector>
#include <string>
@ -15,7 +17,12 @@ namespace psemek::gfx
struct node
{
std::string name;
std::size_t mesh;
std::optional<std::size_t> mesh;
std::vector<std::size_t> children;
geom::vector<float, 3> translation;
geom::quaternion<float> rotation;
geom::vector<float, 3> scale;
};
struct mesh

View file

@ -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)
{

View file

@ -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<float>::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())