diff --git a/libs/gfx/include/psemek/gfx/mesh.hpp b/libs/gfx/include/psemek/gfx/mesh.hpp index bbfa59b3..7a40a699 100644 --- a/libs/gfx/include/psemek/gfx/mesh.hpp +++ b/libs/gfx/include/psemek/gfx/mesh.hpp @@ -8,6 +8,8 @@ #include #include +#include + #include #include #include @@ -477,10 +479,17 @@ namespace psemek::gfx void draw() const override { - if (count_ == 0) return; + draw(0, count_); + } + + void draw(GLsizei first, GLsizei count) const + { + assert(first + count <= count_); + + if (count == 0) return; gl::BindVertexArray(array_); - gl::DrawArrays(primitive_type_, 0, count_); + gl::DrawArrays(primitive_type_, first, count); } GLsizei count() const @@ -599,10 +608,16 @@ namespace psemek::gfx void draw() const override { - if (count_ == 0) return; + draw(0, count_); + } + + void draw(GLsizei first, GLsizei count) const + { + if (count == 0) return; + assert(first + count <= count_); gl::BindVertexArray(array_); - gl::DrawElements(primitive_type_, count_, index_type_, nullptr); + gl::DrawElements(primitive_type_, count, index_type_, reinterpret_cast(0) + first * index_size(index_type_)); } GLsizei index_count() const @@ -628,6 +643,18 @@ namespace psemek::gfx GLenum primitive_type_; indexed_mesh(int); + + 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; + } + assert(false); + return 0; + } }; struct instanced_mesh @@ -737,11 +764,19 @@ namespace psemek::gfx void draw() const override { - if (count_ == 0) return; - if (instance_count_ == 0) return; + draw(0, count_, instance_count_); + } + + void draw(GLsizei first, GLsizei count, GLsizei instance_count) const + { + assert(first + count <= count_); + assert(instance_count <= instance_count_); + + if (count == 0) return; + if (instance_count == 0) return; gl::BindVertexArray(array_); - gl::DrawArraysInstanced(primitive_type_, 0, count_, instance_count_); + gl::DrawArraysInstanced(primitive_type_, first, count, instance_count); } GLsizei count() const