From 8d4e29eb355aa5c43ffe41001404cc0a49640188 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Wed, 4 Nov 2020 10:31:28 +0300 Subject: [PATCH] Support loading index-only data in gfx::mesh --- libs/gfx/include/psemek/gfx/mesh.hpp | 52 ++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/libs/gfx/include/psemek/gfx/mesh.hpp b/libs/gfx/include/psemek/gfx/mesh.hpp index c1e21f8d..1291f533 100644 --- a/libs/gfx/include/psemek/gfx/mesh.hpp +++ b/libs/gfx/include/psemek/gfx/mesh.hpp @@ -150,6 +150,20 @@ namespace psemek::gfx template void load(std::vector const & vertices, std::vector> const & simplices, GLenum usage = gl::STREAM_DRAW); + // Index-only vertex data + + template + void load_index(Index const * indices, std::size_t index_count, GLenum primitive_type, GLenum usage = gl::STREAM_DRAW); + + template + void load_index(std::vector const & indices, GLenum primitive_type, GLenum usage = gl::STREAM_DRAW); + + template + void load_index(geom::simplex const * simplices, std::size_t simplex_count, GLenum usage = gl::STREAM_DRAW); + + template + void load_index(std::vector> const & simplices, GLenum usage = gl::STREAM_DRAW); + // Instance data template @@ -291,6 +305,44 @@ namespace psemek::gfx load(vertices.data(), vertices.size(), simplices.data(), simplices.size(), usage); } + template + void mesh::load_index(Index const * indices, std::size_t index_count, GLenum primitive_type, GLenum usage) + { + if (auto n = detail::get_primitive_type_vertex_count(primitive_type); n) + assert((index_count % (*n)) == 0); + + if (!index_buffer_) + { + index_buffer_ = buffer{}; + array_.bind(); + gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, index_buffer_.id()); + } + index_buffer_.load(indices, index_count, usage); + info_.index_count_ = index_count; + info_.indexed_ = true; + info_.primitive_type_ = primitive_type; + info_.index_type_ = detail::index_type_to_gl_enum_v; + } + + template + void mesh::load_index(std::vector const & indices, GLenum primitive_type, GLenum usage) + { + load_index(indices.data(), indices.size(), primitive_type, usage); + } + + template + void mesh::load_index(geom::simplex const * simplices, std::size_t simplex_count, GLenum usage) + { + static_assert(sizeof(Index) * (N + 1) == sizeof(simplices[0])); + load_index(reinterpret_cast(simplices), simplex_count, detail::get_primitive_type(), usage); + } + + template + void mesh::load_index(std::vector> const & simplices, GLenum usage) + { + load_index(simplices.data(), simplices.size(), usage); + } + template void mesh::load_instance(Instance const * instances, std::size_t count, GLenum usage) {