diff --git a/libs/gfx/include/psemek/gfx/mesh.hpp b/libs/gfx/include/psemek/gfx/mesh.hpp index a539090c..e583d6af 100644 --- a/libs/gfx/include/psemek/gfx/mesh.hpp +++ b/libs/gfx/include/psemek/gfx/mesh.hpp @@ -384,6 +384,9 @@ namespace psemek::gfx template static constexpr GLenum gl_type_v = gl_type::value; + void check_vertex_size(std::size_t vertex_size, std::size_t stride); + void check_instance_size(std::size_t instance_size, std::size_t stride); + } struct mesh @@ -412,8 +415,7 @@ namespace psemek::gfx template void load(Vertex const * vertices, std::size_t count, GLenum primitive_type, GLenum usage = gl::STREAM_DRAW) { - if (sizeof(Vertex) != stride_) - throw std::runtime_error("Vertex size not equal to sum of attribute sizes"); + detail::check_vertex_size(sizeof(Vertex), stride_); switch (primitive_type) { @@ -532,8 +534,7 @@ namespace psemek::gfx 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) { - if (sizeof(Vertex) != stride_) - throw std::runtime_error("Vertex size not equal to sum of attribute sizes"); + detail::check_vertex_size(sizeof(Vertex), stride_); switch (primitive_type) { @@ -658,8 +659,7 @@ namespace psemek::gfx template void load(Vertex const * vertices, std::size_t count, GLenum primitive_type, GLenum usage = gl::STREAM_DRAW) { - if (sizeof(Vertex) != stride_) - throw std::runtime_error("Vertex size not equal to sum of attribute sizes"); + detail::check_vertex_size(sizeof(Vertex), stride_); switch (primitive_type) { @@ -722,8 +722,7 @@ namespace psemek::gfx template void load_instance(Instance const * instances, std::size_t count, GLenum usage = gl::STREAM_DRAW) { - if (sizeof(Instance) != instance_stride_) - throw std::runtime_error("Instance size not equal to sum of instanced attribute sizes"); + detail::check_instance_size(sizeof(Instance), instance_stride_); gl::BindBuffer(gl::ARRAY_BUFFER, instance_buffer_); gl::BufferData(gl::ARRAY_BUFFER, count * sizeof(Instance), instances, usage); diff --git a/libs/gfx/source/mesh.cpp b/libs/gfx/source/mesh.cpp index 843be4f6..02741672 100644 --- a/libs/gfx/source/mesh.cpp +++ b/libs/gfx/source/mesh.cpp @@ -1,8 +1,27 @@ #include +#include + namespace psemek::gfx { + namespace detail + { + + void check_vertex_size(std::size_t vertex_size, std::size_t stride) + { + if (vertex_size != stride) + throw std::runtime_error(util::to_string("Vertex size (", vertex_size, ") not equal to sum of attribute sizes (", stride, ")")); + } + + void check_instance_size(std::size_t instance_size, std::size_t stride) + { + if (instance_size != stride) + throw std::runtime_error(util::to_string("Instance size (", instance_size, ") not equal to sum of instanced attribute sizes (", stride, ")")); + } + + } + mesh mesh::null() { return mesh(0);