diff --git a/libs/gfx/include/psemek/gfx/mesh.hpp b/libs/gfx/include/psemek/gfx/mesh.hpp index d2691974..0ede75f9 100644 --- a/libs/gfx/include/psemek/gfx/mesh.hpp +++ b/libs/gfx/include/psemek/gfx/mesh.hpp @@ -336,4 +336,6 @@ namespace psemek::gfx load_instance(instances.data(), instances.size(), usage); } + void load_mesh(mesh & m, std::string_view data); + } diff --git a/libs/gfx/source/mesh.cpp b/libs/gfx/source/mesh.cpp index 3347bbc9..609e7e5b 100644 --- a/libs/gfx/source/mesh.cpp +++ b/libs/gfx/source/mesh.cpp @@ -1,6 +1,7 @@ #include #include +#include namespace psemek::gfx { @@ -197,4 +198,39 @@ namespace psemek::gfx } } + void load_mesh(mesh & m, std::string_view data) + { + // These should be in sync with convert-mesh.py + static std::uint32_t const POSITION_MASK = 1; + static std::uint32_t const NORMALS_MASK = 2; + static std::uint32_t const COLORS_MASK = 4; + static std::uint32_t const TEXCOORDS_MASK = 8; + + util::binary_istream s{data}; + + auto vertex_format = s.read(); + attribs_description attrs; + + if (vertex_format & POSITION_MASK) + attrs += make_attribs_description>(); + + if (vertex_format & NORMALS_MASK) + attrs += make_attribs_description>(); + + if (vertex_format & COLORS_MASK) + attrs += make_attribs_description>>(); + + if (vertex_format & TEXCOORDS_MASK) + attrs += make_attribs_description>(); + + auto vertex_count = s.read(); + auto vertex_ptr = s.read_raw(vertex_count * attrs.vertex_size); + + auto index_count = s.read(); + auto index_ptr = s.read_raw(index_count * sizeof(std::uint32_t)); + + m.setup(attrs); + m.load_raw(vertex_ptr, attrs.vertex_size, vertex_count, index_ptr, gl::UNSIGNED_INT, index_count, gl::TRIANGLES, gl::STATIC_DRAW); + } + }