Implement loading meshes from binary format

This commit is contained in:
Nikita Lisitsa 2021-03-07 18:31:20 +03:00
parent 697de5c422
commit 5255abd66a
2 changed files with 38 additions and 0 deletions

View file

@ -336,4 +336,6 @@ namespace psemek::gfx
load_instance(instances.data(), instances.size(), usage);
}
void load_mesh(mesh & m, std::string_view data);
}

View file

@ -1,6 +1,7 @@
#include <psemek/gfx/mesh.hpp>
#include <psemek/util/to_string.hpp>
#include <psemek/util/binary_stream.hpp>
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<std::uint32_t>();
attribs_description attrs;
if (vertex_format & POSITION_MASK)
attrs += make_attribs_description<geom::point<float, 3>>();
if (vertex_format & NORMALS_MASK)
attrs += make_attribs_description<geom::vector<float, 3>>();
if (vertex_format & COLORS_MASK)
attrs += make_attribs_description<gfx::normalized<geom::vector<std::uint8_t, 4>>>();
if (vertex_format & TEXCOORDS_MASK)
attrs += make_attribs_description<geom::vector<float, 2>>();
auto vertex_count = s.read<std::uint32_t>();
auto vertex_ptr = s.read_raw(vertex_count * attrs.vertex_size);
auto index_count = s.read<std::uint32_t>();
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);
}
}