Support retrieving extra properties & raw mesh from gltf mesh

This commit is contained in:
Nikita Lisitsa 2024-03-16 17:37:57 +03:00
parent ff1c144f25
commit 663f7bc5f4
3 changed files with 48 additions and 5 deletions

View file

@ -3,6 +3,7 @@
#include <psemek/gfx/gltf_parser.hpp>
#include <psemek/gfx/drawable.hpp>
#include <psemek/gfx/texture.hpp>
#include <psemek/geom/simplex.hpp>
#include <psemek/util/span.hpp>
#include <psemek/util/blob.hpp>
@ -17,11 +18,15 @@ namespace psemek::gfx
{
gfx::drawable * drawable;
std::optional<std::size_t> material;
util::span<geom::point<float, 3> const> vertices;
util::span<geom::triangle<std::uint32_t> const> triangles;
};
virtual gltf_asset::material const & material(std::size_t index) const = 0;
virtual gfx::texture_2d const & texture(std::size_t index) const = 0;
virtual util::span<primitive const> mesh(std::string_view name) const = 0;
virtual gltf_asset::extra const * mesh_property(std::string_view mesh_name, std::string_view property_name) const = 0;
virtual ~gltf_mesh() {}
};

View file

@ -6,6 +6,7 @@
#include <psemek/geom/quaternion.hpp>
#include <psemek/geom/affine_transform.hpp>
#include <psemek/geom/easing.hpp>
#include <psemek/util/hstring.hpp>
#include <vector>
#include <string>
@ -19,7 +20,7 @@ namespace psemek::gfx
struct gltf_asset
{
using extra = std::variant<float, std::vector<float>, std::string>;
using extras_map = std::unordered_map<std::string, extra>;
using extras_map = std::unordered_map<util::hstring, extra>;
struct node
{

View file

@ -1,4 +1,5 @@
#include <psemek/gfx/gltf_mesh.hpp>
#include <psemek/gfx/gltf_accessor_iterator.hpp>
#include <psemek/gfx/array.hpp>
#include <psemek/gfx/buffer.hpp>
#include <psemek/util/hstring.hpp>
@ -21,6 +22,9 @@ namespace psemek::gfx
std::size_t index_offset;
GLenum index_type;
std::vector<geom::point<float, 3>> vertices;
std::vector<geom::triangle<std::uint32_t>> triangles;
void draw() const override
{
vao.bind();
@ -46,16 +50,32 @@ namespace psemek::gfx
util::span<primitive const> mesh(std::string_view name) const override
{
if (auto it = meshes_.find(name); it != meshes_.end())
return it->second;
return it->second.primitives;
return {};
}
gltf_asset::extra const * mesh_property(std::string_view mesh_name, std::string_view property_name) const override
{
if (auto it = meshes_.find(mesh_name); it != meshes_.end())
if (auto jt = it->second.extras.find(property_name); jt != it->second.extras.end())
return &(jt->second);
return nullptr;
}
private:
struct mesh_data
{
std::vector<primitive> primitives;
gltf_asset::extras_map extras;
std::vector<geom::point<float, 3>> vertices;
std::vector<geom::triangle<std::uint32_t>> triangles;
};
std::vector<gltf_asset::material> materials_;
std::vector<gfx::texture_2d> textures_;
std::vector<gfx::buffer> buffers_;
std::vector<std::unique_ptr<drawable_impl>> drawables_;
std::unordered_map<util::hstring, std::vector<primitive>> meshes_;
std::unordered_map<util::hstring, mesh_data> meshes_;
};
gltf_mesh_impl::gltf_mesh_impl(gltf_asset const & asset, std::function<util::blob(std::string const &)> uri_loader)
@ -91,6 +111,7 @@ namespace psemek::gfx
continue;
auto & target_mesh = meshes_[node.name];
target_mesh.extras = node.extras;
auto const & mesh = asset.meshes[*node.mesh];
@ -111,11 +132,12 @@ namespace psemek::gfx
drawable.index_count = indices_accessor.count;
}
std::pair<GLuint, std::optional<std::size_t>> attributes[3] =
std::pair<GLuint, std::optional<std::size_t>> attributes[] =
{
{0, primitive.position},
{1, primitive.normal},
{2, primitive.texcoord},
{3, primitive.color},
};
for (auto const & attribute : attributes)
@ -130,7 +152,22 @@ namespace psemek::gfx
gl::VertexAttribPointer(attribute.first, attribute_size(accessor.type), accessor.component_type, accessor.normalized, 0, reinterpret_cast<void const *>(view.offset));
}
target_mesh.push_back({&drawable, primitive.material});
{
auto indices = accessor_range<std::uint32_t>(asset, primitive.indices);
for (auto it = indices.it_begin; it != indices.it_end;)
{
auto i0 = *it++;
auto i1 = *it++;
auto i2 = *it++;
drawable.triangles.push_back({i0, i1, i2});
}
}
if (primitive.position)
for (auto const & p : accessor_range<geom::point<float, 3>>(asset, *primitive.position))
drawable.vertices.push_back(p);
target_mesh.primitives.push_back({&drawable, primitive.material, drawable.vertices, drawable.triangles});
}
}
}