From 2f9bccc348d6ab3d71fb9f126de108a4ae6e97ce Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 8 Jan 2024 21:29:39 -0500 Subject: [PATCH] way of accessing and binding shared references. normal vbo now returns a shared_ptr which can be used in place of a vbo_t --- include/blt/gfx/model.h | 9 +++++++-- src/blt/gfx/model.cpp | 19 +++++++++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/include/blt/gfx/model.h b/include/blt/gfx/model.h index 7036f99..79383bb 100644 --- a/include/blt/gfx/model.h +++ b/include/blt/gfx/model.h @@ -85,9 +85,10 @@ namespace blt::gfx */ class static_dynamic_array { + public: + using vbo_type = std::shared_ptr; private: static constexpr size_t DATA_SIZE = 8; - using vbo_type = std::shared_ptr; using array_t = std::array; std::variant data_; size_t size_ = DATA_SIZE; @@ -130,6 +131,8 @@ namespace blt::gfx static_dynamic_array VBOs; HASHSET used_attributes; vbo_t element; + + void handle_vbo(const vbo_t& vbo, int attribute_number, int coordinate_size, GLenum type, int stride, long offset); public: vertex_array(); @@ -150,8 +153,10 @@ namespace blt::gfx * @param stride how many bytes this data takes (for the entire per-vertex data structure) 0 will assume packed data * This is in effect how many bytes until the next block of data * @param offset offset into the data structure to where the data is stored + * @return a shared pointer to the stored vbo. used for chaining VAOs with multiple shared VBOs */ - void bindVBO(const vbo_t& vbo, int attribute_number, int coordinate_size, GLenum type, int stride, long offset); + static_dynamic_array::vbo_type bindVBO(const vbo_t& vbo, int attribute_number, int coordinate_size, GLenum type, int stride, long offset); + void bindVBO(const static_dynamic_array::vbo_type& vbo, int attribute_number, int coordinate_size, GLenum type, int stride, long offset); inline void bindElement(const vbo_t& vbo) { diff --git a/src/blt/gfx/model.cpp b/src/blt/gfx/model.cpp index 89eacff..c695b32 100644 --- a/src/blt/gfx/model.cpp +++ b/src/blt/gfx/model.cpp @@ -130,7 +130,21 @@ namespace blt::gfx glDeleteVertexArrays(1, &vaoID); } - void vertex_array::bindVBO(const vbo_t& vbo, int attribute_number, int coordinate_size, GLenum type, int stride, long offset) + static_dynamic_array::vbo_type vertex_array::bindVBO(const vbo_t& vbo, int attribute_number, int coordinate_size, GLenum type, int stride, + long offset) + { + handle_vbo(vbo, attribute_number, coordinate_size, type, stride, offset); + VBOs[attribute_number] = std::make_shared(vbo); + return VBOs[attribute_number]; + } + + void vertex_array::bindVBO(const static_dynamic_array::vbo_type& vbo, int attribute_number, int coordinate_size, GLenum type, int stride, long offset) + { + handle_vbo(vbo->vbo, attribute_number, coordinate_size, type, stride, offset); + VBOs[attribute_number] = vbo; + } + + void vertex_array::handle_vbo(const vbo_t& vbo, int attribute_number, int coordinate_size, GLenum type, int stride, long offset) { used_attributes.insert(attribute_number); bind(); @@ -138,9 +152,6 @@ namespace blt::gfx glVertexAttribPointer(attribute_number, coordinate_size, type, GL_FALSE, stride < 0 ? 0 : stride, (void*) offset); glEnableVertexAttribArray(attribute_number); - - VBOs[attribute_number] = std::make_shared(vbo); - unbind(); } } \ No newline at end of file