diff --git a/libs/gfx/include/psemek/gfx/mesh.hpp b/libs/gfx/include/psemek/gfx/mesh.hpp index bbc024c9..b8d1468e 100644 --- a/libs/gfx/include/psemek/gfx/mesh.hpp +++ b/libs/gfx/include/psemek/gfx/mesh.hpp @@ -96,6 +96,8 @@ namespace psemek::gfx } + struct imported_mesh; + /* A generic mesh class * Supports both indexed & non-indexed rendering * Supports both instanced & non-instanced rendering @@ -154,6 +156,8 @@ namespace psemek::gfx template void load(std::vector const & vertices, std::vector> const & simplices, GLenum usage = gl::STREAM_DRAW); + void load_raw(imported_mesh const & m); + // Index-only vertex data template @@ -337,6 +341,31 @@ namespace psemek::gfx load_instance(instances.data(), instances.size(), usage); } - std::tuple, util::span> load_mesh(std::string_view data); + struct imported_mesh + { + attribs_description attribs; + util::span vertices; + util::span indices; + + struct bone + { + std::uint32_t parent; + + static constexpr std::uint32_t null = std::uint32_t(-1); + }; + + util::span bones; + + struct bone_pose + { + geom::vector translation; + float scale; + geom::quaternion rotation; + }; + + std::unordered_map> poses; + }; + + imported_mesh load_mesh(std::string_view data); } diff --git a/libs/gfx/source/mesh.cpp b/libs/gfx/source/mesh.cpp index 7dbe09a6..06598550 100644 --- a/libs/gfx/source/mesh.cpp +++ b/libs/gfx/source/mesh.cpp @@ -143,6 +143,11 @@ namespace psemek::gfx info_.index_type_ = index_type; } + void mesh::load_raw(imported_mesh const & m) + { + load_raw(m.vertices.data(), m.attribs.vertex_size, m.vertices.size() / m.attribs.vertex_size, m.indices.data(), gl::UNSIGNED_INT, m.indices.size(), gl::TRIANGLES, gl::STATIC_DRAW); + } + void mesh::draw() const { draw(0, is_indexed() ? index_count() : vertex_count(), instance_count()); @@ -198,7 +203,7 @@ namespace psemek::gfx } } - std::tuple, util::span> load_mesh(std::string_view data) + imported_mesh load_mesh(std::string_view data) { // These should be in sync with convert-mesh.py static std::uint32_t const POSITION_MASK = 1; @@ -208,31 +213,32 @@ namespace psemek::gfx util::binary_istream s{data}; + imported_mesh result; + auto vertex_format = s.read(); - attribs_description attrs; if (vertex_format & POSITION_MASK) - attrs += make_attribs_description>(); + result.attribs += make_attribs_description>(); if (vertex_format & NORMALS_MASK) - attrs += make_attribs_description>(); + result.attribs += make_attribs_description>(); if (vertex_format & COLORS_MASK) - attrs += make_attribs_description>>(); + result.attribs += make_attribs_description>>(); if (vertex_format & TEXCOORDS_MASK) - attrs += make_attribs_description>(); + result.attribs += make_attribs_description>(); auto vertex_count = s.read(); - auto vertex_ptr = s.read_raw(vertex_count * attrs.vertex_size); + auto vertex_ptr = s.read_raw(vertex_count * result.attribs.vertex_size); auto index_count = s.read(); auto index_ptr = reinterpret_cast(s.read_raw(index_count * sizeof(std::uint32_t))); - util::span vertices{vertex_ptr, vertex_ptr + vertex_count * attrs.vertex_size}; - util::span indices{index_ptr, index_ptr + index_count}; + result.vertices = {vertex_ptr, vertex_ptr + vertex_count * result.attribs.vertex_size}; + result.indices = {index_ptr, index_ptr + index_count}; - return {attrs, vertices, indices}; + return result; } }