diff --git a/include/blt/gfx/model.h b/include/blt/gfx/model.h index 96db136..70a20e2 100644 --- a/include/blt/gfx/model.h +++ b/include/blt/gfx/model.h @@ -22,6 +22,7 @@ #include #include #include +#include namespace blt::gfx { @@ -34,6 +35,57 @@ namespace blt::gfx std::vector normals; }; + class static_dynamic_array + { + private: + static constexpr size_t DATA_SIZE = 8; + std::variant data_; + size_t size_ = DATA_SIZE; + public: + static_dynamic_array() = default; + + static_dynamic_array(const static_dynamic_array& copy) = delete; + + static_dynamic_array(static_dynamic_array&& move) noexcept; + + static_dynamic_array& operator=(const static_dynamic_array& copy) = delete; + + static_dynamic_array& operator=(static_dynamic_array&& move) = delete; + + ~static_dynamic_array() + { + if (std::holds_alternative(data_)) + delete[] std::get(data_); + } + }; + + /** + * basic VAO class. + */ + class basic_vertex_array + { + private: + struct vbo_t + { + GLuint bufferID_ = 0; + GLsizeiptr size_ = 0; + + vbo_t(); + + void bind(GLint buffer_type = GL_ARRAY_BUFFER) const; + + void allocate(GLsizeiptr size, GLint buffer_type = GL_ARRAY_BUFFER, GLint memory_type = GL_STATIC_DRAW, void* data = nullptr); + + static void sub_update(GLsizeiptr offset, GLsizeiptr size, void* data, GLint buffer_type = GL_ARRAY_BUFFER); + + void update(GLsizeiptr size, void* data, GLint buffer_type = GL_ARRAY_BUFFER, GLint memory_type = GL_STATIC_DRAW); + + ~vbo_t(); + }; + GLuint vaoID; + public: + basic_vertex_array(); + }; } diff --git a/src/blt/gfx/model.cpp b/src/blt/gfx/model.cpp index 86da900..7e14ddf 100644 --- a/src/blt/gfx/model.cpp +++ b/src/blt/gfx/model.cpp @@ -16,8 +16,70 @@ * along with this program. If not, see . */ #include +#include -blt::gfx::vertex_array::vertex_array() +namespace blt::gfx { - glGenVertexArrays(1, &vaoID); -} + static_dynamic_array::static_dynamic_array(static_dynamic_array&& move) noexcept + { + if (std::holds_alternative(move.data_)){ + data_ = std::move(std::get(move.data_)); + } else { + data_ = std::get(move.data_); + size_ = move.size_; + } + move.data_ = nullptr; + move.size_ = 0; + } + + /* + * ----------------------------------- + * basic_vertex_array::vbo_t + * ----------------------------------- + */ + basic_vertex_array::vbo_t::vbo_t() + { + glGenBuffers(1, &bufferID_); + } + + basic_vertex_array::vbo_t::~vbo_t() + { + glDeleteBuffers(1, &bufferID_); + } + + void basic_vertex_array::vbo_t::allocate(GLsizeiptr size, GLint buffer_type, GLint memory_type, void* data) + { + size_ = size; + glBufferData(buffer_type, size, data, memory_type); + } + + void blt::gfx::basic_vertex_array::vbo_t::update(GLsizeiptr size, void* data, GLint buffer_type, GLint memory_type) + { + if (size <= size_) + sub_update(0, size, data, buffer_type); + else + allocate(size, buffer_type, memory_type, data); + size_ = size; + } + + void basic_vertex_array::vbo_t::sub_update(GLsizeiptr offset, GLsizeiptr size, void* data, GLint buffer_type) + { + glBufferSubData(buffer_type, offset, size, data); + } + + void basic_vertex_array::vbo_t::bind(GLint buffer_type) const + { + glBindBuffer(buffer_type, bufferID_); + } + + /* + * ---------------------------- + * basic_vertex_array + * ---------------------------- + */ + + blt::gfx::basic_vertex_array::basic_vertex_array() + { + glGenVertexArrays(1, &vaoID); + } +} \ No newline at end of file