diff --git a/libs/gfx/include/psemek/gfx/mesh.hpp b/libs/gfx/include/psemek/gfx/mesh.hpp index b79c20ef..d2691974 100644 --- a/libs/gfx/include/psemek/gfx/mesh.hpp +++ b/libs/gfx/include/psemek/gfx/mesh.hpp @@ -121,6 +121,8 @@ namespace psemek::gfx // Non-indexed vertex data + void load_raw(void const * vertices, std::size_t vertex_size, std::size_t count, GLenum primitive_type, GLenum usage = gl::STREAM_DRAW); + template void load(Vertex const * vertices, std::size_t count, GLenum primitive_type, GLenum usage = gl::STREAM_DRAW); @@ -135,6 +137,10 @@ namespace psemek::gfx // Indexed vertex data + void load_raw(void const * vertices, std::size_t vertex_size, std::size_t vertex_count, + void const * indices, GLenum index_type, std::size_t index_count, + GLenum primitive_type, GLenum usage = gl::STREAM_DRAW); + template void load(Vertex const * vertices, std::size_t vertex_count, Index const * indices, std::size_t index_count, GLenum primitive_type, GLenum usage = gl::STREAM_DRAW); @@ -229,18 +235,7 @@ namespace psemek::gfx template void mesh::load(Vertex const * vertices, std::size_t count, GLenum primitive_type, GLenum usage) { - assert(info_.stride_ == sizeof(Vertex)); - - if (auto n = detail::get_primitive_type_vertex_count(primitive_type); n) - assert((count % (*n)) == 0); - - assert(vertex_buffer_); - - vertex_buffer_.load(vertices, count, usage); - info_.vertex_count_ = count; - info_.index_count_ = 0; - info_.indexed_ = false; - info_.primitive_type_ = primitive_type; + load_raw(vertices, sizeof(Vertex), count, primitive_type, usage); } template @@ -265,26 +260,7 @@ namespace psemek::gfx template void mesh::load(Vertex const * vertices, std::size_t vertex_count, Index const * indices, std::size_t index_count, GLenum primitive_type, GLenum usage) { - assert(info_.stride_ == sizeof(Vertex)); - - if (auto n = detail::get_primitive_type_vertex_count(primitive_type); n) - assert((index_count % (*n)) == 0); - - assert(vertex_buffer_); - - if (!index_buffer_) - { - index_buffer_ = buffer{}; - array_.bind(); - gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, index_buffer_.id()); - } - vertex_buffer_.load(vertices, vertex_count, usage); - index_buffer_.load(indices, index_count, usage); - info_.vertex_count_ = vertex_count; - info_.index_count_ = index_count; - info_.indexed_ = true; - info_.primitive_type_ = primitive_type; - info_.index_type_ = detail::index_type_to_gl_enum_v; + load_raw(vertices, sizeof(Vertex), vertex_count, indices, detail::index_type_to_gl_enum_v, index_count, primitive_type, usage); } template diff --git a/libs/gfx/source/mesh.cpp b/libs/gfx/source/mesh.cpp index 4abaaac0..3347bbc9 100644 --- a/libs/gfx/source/mesh.cpp +++ b/libs/gfx/source/mesh.cpp @@ -89,6 +89,59 @@ namespace psemek::gfx } } + void mesh::load_raw(void const * vertices, std::size_t vertex_size, std::size_t count, GLenum primitive_type, GLenum usage) + { + assert(info_.stride_ == vertex_size); + + if (auto n = detail::get_primitive_type_vertex_count(primitive_type); n) + assert((count % (*n)) == 0); + + assert(vertex_buffer_); + + vertex_buffer_.load(vertices, vertex_size * count, usage); + info_.vertex_count_ = count; + info_.index_count_ = 0; + info_.indexed_ = false; + info_.primitive_type_ = primitive_type; + } + + static std::size_t index_size(GLenum type) + { + switch (type) + { + case gl::UNSIGNED_BYTE: return 1; + case gl::UNSIGNED_SHORT: return 2; + case gl::UNSIGNED_INT: return 4; + default: throw std::runtime_error("Unknown undex type"); + } + } + + void mesh::load_raw(void const * vertices, std::size_t vertex_size, std::size_t vertex_count, + void const * indices, GLenum index_type, std::size_t index_count, + GLenum primitive_type, GLenum usage) + { + assert(info_.stride_ == vertex_size); + + if (auto n = detail::get_primitive_type_vertex_count(primitive_type); n) + assert((index_count % (*n)) == 0); + + assert(vertex_buffer_); + + if (!index_buffer_) + { + index_buffer_ = buffer{}; + array_.bind(); + gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, index_buffer_.id()); + } + vertex_buffer_.load(vertices, vertex_size * vertex_count, usage); + index_buffer_.load(indices, index_size(index_type) * index_count, usage); + info_.vertex_count_ = vertex_count; + info_.index_count_ = index_count; + info_.indexed_ = true; + info_.primitive_type_ = primitive_type; + info_.index_type_ = index_type; + } + void mesh::draw() const { draw(0, is_indexed() ? index_count() : vertex_count(), instance_count());