summaryrefslogtreecommitdiff
path: root/gfx/gl
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-01-02 15:10:27 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2023-01-02 15:10:27 +0000
commit07f478ba964464ff6a6f6bc6855acc8353b4594b (patch)
tree796d4383c57ae73e4206dfc9acdf542d5e4e4c46 /gfx/gl
parentAdd a traits struct to lookup sizes/type enums for passing to OpenGL (diff)
downloadilt-07f478ba964464ff6a6f6bc6855acc8353b4594b.tar.bz2
ilt-07f478ba964464ff6a6f6bc6855acc8353b4594b.tar.xz
ilt-07f478ba964464ff6a6f6bc6855acc8353b4594b.zip
Add helper for configuring vertex array objects/buffers of arbitrary things
Diffstat (limited to 'gfx/gl')
-rw-r--r--gfx/gl/vertexArrayObject.hpp76
1 files changed, 76 insertions, 0 deletions
diff --git a/gfx/gl/vertexArrayObject.hpp b/gfx/gl/vertexArrayObject.hpp
new file mode 100644
index 0000000..d90cc59
--- /dev/null
+++ b/gfx/gl/vertexArrayObject.hpp
@@ -0,0 +1,76 @@
+#pragma once
+
+#include "collections.hpp"
+#include "gl_traits.hpp"
+#include <GL/glew.h>
+#include <glm/common.hpp>
+
+template<typename Vertex> class VertexArrayObject {
+public:
+ template<auto Vertex::*... attribs>
+ static void
+ configure(const GLuint arrayObject, const GLuint arrayBuffer, const GLuint indexBuffer,
+ const SequentialCollection<Vertex> auto & vertices, const SequentialCollection<unsigned int> auto & indices)
+ {
+ glBindVertexArray(arrayObject);
+
+ configure_attribs<attribs...>(arrayBuffer);
+ data(vertices, arrayBuffer, GL_ARRAY_BUFFER);
+ data(indices, indexBuffer, GL_ELEMENT_ARRAY_BUFFER);
+
+ glBindVertexArray(0);
+ }
+
+ template<auto Vertex::*... attribs>
+ static void
+ configure(const GLuint arrayObject, const GLuint arrayBuffer, const SequentialCollection<Vertex> auto & vertices)
+ {
+ glBindVertexArray(arrayObject);
+
+ configure_attribs<attribs...>(arrayBuffer);
+ data(vertices, arrayBuffer, GL_ARRAY_BUFFER);
+
+ glBindVertexArray(0);
+ }
+
+ template<auto Vertex::*... attribs>
+ static void
+ configure(const GLuint arrayObject, const GLuint arrayBuffer)
+ {
+ glBindVertexArray(arrayObject);
+
+ configure_attribs<attribs...>(arrayBuffer);
+ glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(sizeof(Vertex)), nullptr, GL_DYNAMIC_DRAW);
+
+ glBindVertexArray(0);
+ }
+
+private:
+ template<typename Data>
+ static void
+ data(const Data & data, const GLuint arrayBuffer, GLenum target)
+ {
+ using Value = typename Data::element_type;
+ glBindBuffer(target, arrayBuffer);
+ glBufferData(target, static_cast<GLsizeiptr>(sizeof(Value) * data.size()), data.data(), GL_STATIC_DRAW);
+ }
+
+ template<auto Vertex::*attrib>
+ static void
+ set_pointer(GLuint & vertexArrayId)
+ {
+ glEnableVertexAttribArray(vertexArrayId);
+ using traits = gl_traits<std::decay_t<decltype(std::declval<Vertex>().*attrib)>>;
+ glVertexAttribPointer(vertexArrayId++, traits::size, traits::type, GL_FALSE, sizeof(Vertex),
+ &(static_cast<const Vertex *>(nullptr)->*attrib));
+ }
+
+ template<auto Vertex::*... attribs>
+ static void
+ configure_attribs(const GLuint arrayBuffer)
+ {
+ glBindBuffer(GL_ARRAY_BUFFER, arrayBuffer);
+ GLuint vertexArrayId {};
+ (set_pointer<attribs>(vertexArrayId), ...);
+ }
+};