Make gfx::load_mesh return data instead of loading mesh directly

This commit is contained in:
Nikita Lisitsa 2021-03-07 19:05:13 +03:00
parent 6e49551045
commit 3e4cea25e7
2 changed files with 8 additions and 5 deletions

View file

@ -12,6 +12,7 @@
#include <psemek/geom/matrix.hpp> #include <psemek/geom/matrix.hpp>
#include <psemek/util/assert.hpp> #include <psemek/util/assert.hpp>
#include <psemek/util/span.hpp>
#include <cstddef> #include <cstddef>
#include <type_traits> #include <type_traits>
@ -336,6 +337,6 @@ namespace psemek::gfx
load_instance(instances.data(), instances.size(), usage); load_instance(instances.data(), instances.size(), usage);
} }
void load_mesh(mesh & m, std::string_view data); std::tuple<attribs_description, util::span<char const>, util::span<std::uint32_t const>> load_mesh(std::string_view data);
} }

View file

@ -198,7 +198,7 @@ namespace psemek::gfx
} }
} }
void load_mesh(mesh & m, std::string_view data) std::tuple<attribs_description, util::span<char const>, util::span<std::uint32_t const>> load_mesh(std::string_view data)
{ {
// These should be in sync with convert-mesh.py // These should be in sync with convert-mesh.py
static std::uint32_t const POSITION_MASK = 1; static std::uint32_t const POSITION_MASK = 1;
@ -227,10 +227,12 @@ namespace psemek::gfx
auto vertex_ptr = s.read_raw(vertex_count * attrs.vertex_size); auto vertex_ptr = s.read_raw(vertex_count * attrs.vertex_size);
auto index_count = s.read<std::uint32_t>(); auto index_count = s.read<std::uint32_t>();
auto index_ptr = s.read_raw(index_count * sizeof(std::uint32_t)); auto index_ptr = reinterpret_cast<std::uint32_t const *>(s.read_raw(index_count * sizeof(std::uint32_t)));
m.setup(attrs); util::span<char const> vertices{vertex_ptr, vertex_ptr + vertex_count * attrs.vertex_size};
m.load_raw(vertex_ptr, attrs.vertex_size, vertex_count, index_ptr, gl::UNSIGNED_INT, index_count, gl::TRIANGLES, gl::STATIC_DRAW); util::span<std::uint32_t const> indices{index_ptr, index_ptr + index_count};
return {attrs, vertices, indices};
} }
} }