From da1c1336596d361678b3f641a1d23ab89e078789 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 17 Apr 2023 17:09:40 +0100 Subject: Revamp how VertexArrayObject configures attributes and data --- gfx/gl/vertexArrayObject.hpp | 78 ++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 35 deletions(-) (limited to 'gfx/gl/vertexArrayObject.hpp') diff --git a/gfx/gl/vertexArrayObject.hpp b/gfx/gl/vertexArrayObject.hpp index 5b9fc60..3e2a18b 100644 --- a/gfx/gl/vertexArrayObject.hpp +++ b/gfx/gl/vertexArrayObject.hpp @@ -2,47 +2,56 @@ #include "collections.hpp" #include "gl_traits.hpp" +#include "special_members.hpp" #include -#include -template class VertexArrayObject { +class VertexArrayObject { public: - template - static void - configure(const GLuint arrayObject, const GLuint arrayBuffer, const GLuint indexBuffer, - const SequentialCollection auto & vertices, const SequentialCollection auto & indices) + [[nodiscard]] VertexArrayObject(const GLuint arrayObject) { glBindVertexArray(arrayObject); - - configure_attribs(arrayBuffer); - data(vertices, arrayBuffer, GL_ARRAY_BUFFER); - data(indices, indexBuffer, GL_ELEMENT_ARRAY_BUFFER); - + } + ~VertexArrayObject() + { glBindVertexArray(0); } + NO_MOVE(VertexArrayObject); + NO_COPY(VertexArrayObject); - template - static void - configure(const GLuint arrayObject, const GLuint arrayBuffer, const SequentialCollection auto & vertices) - { - glBindVertexArray(arrayObject); + template struct MP { + constexpr MP(m T::*p) : P {p} { } + operator void *() const + { + return &(static_cast(nullptr)->*P); + } + m T::*P; + using value_type = m; + }; + template MP(m T::*) -> MP; - configure_attribs(arrayBuffer); + template + VertexArrayObject & + addAttribs(const GLuint arrayBuffer, const SequentialCollection auto & vertices) + { + addAttribs(arrayBuffer); data(vertices, arrayBuffer, GL_ARRAY_BUFFER); - - glBindVertexArray(0); + return *this; } - template - static void - configure(const GLuint arrayObject, const GLuint arrayBuffer) + template + VertexArrayObject & + addAttribs(const GLuint arrayBuffer) { - glBindVertexArray(arrayObject); - - configure_attribs(arrayBuffer); - glBufferData(GL_ARRAY_BUFFER, static_cast(sizeof(Vertex)), nullptr, GL_DYNAMIC_DRAW); + configure_attribs(arrayBuffer); + return *this; + } - glBindVertexArray(0); + template + VertexArrayObject & + addIndices(const GLuint arrayBuffer, const Indices & indices) + { + data(indices, arrayBuffer, GL_ELEMENT_ARRAY_BUFFER); + return *this; } private: @@ -55,34 +64,33 @@ private: glBufferData(target, static_cast(sizeof(Value) * data.size()), data.data(), GL_STATIC_DRAW); } - template + template static void set_pointer(const GLuint vertexArrayId, const void * ptr) { glEnableVertexAttribArray(vertexArrayId); using traits = gl_traits; - traits::vertexAttribFunc(vertexArrayId, traits::size, traits::type, sizeof(Vertex), ptr); + traits::vertexAttribFunc(vertexArrayId, traits::size, traits::type, sizeof(VertexT), ptr); } - template + template static void set_pointer(const GLuint vertexArrayId) { - set_pointer().*attrib)>>( - vertexArrayId, &(static_cast(nullptr)->*attrib)); + set_pointer(vertexArrayId, attrib); } - template + template static void configure_attribs(const GLuint arrayBuffer) { glBindBuffer(GL_ARRAY_BUFFER, arrayBuffer); if constexpr (sizeof...(attribs) == 0) { - set_pointer(0, nullptr); + set_pointer(0, nullptr); } else { GLuint vertexArrayId {}; - (set_pointer(vertexArrayId++), ...); + (set_pointer(vertexArrayId++), ...); } } }; -- cgit v1.2.3