summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gfx/gl/vertexArrayObject.hpp76
-rw-r--r--gfx/models/mesh.cpp27
2 files changed, 80 insertions, 23 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), ...);
+ }
+};
diff --git a/gfx/models/mesh.cpp b/gfx/models/mesh.cpp
index f13e242..85be84c 100644
--- a/gfx/models/mesh.cpp
+++ b/gfx/models/mesh.cpp
@@ -1,33 +1,14 @@
#include "mesh.h"
+#include "gfx/gl/vertexArrayObject.hpp"
#include "glArrays.h"
#include "vertex.hpp"
#include <cstddef>
-#define offset_ptr(T, m) ((reinterpret_cast<char *>(1)) + offsetof(T, m) - 1)
-
Mesh::Mesh(const std::span<const Vertex> vertices, const std::span<const unsigned int> indices, GLenum m) :
m_numIndices {static_cast<GLsizei>(indices.size())}, mode {m}
{
- glBindVertexArray(m_vertexArrayObject);
-
- glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[0]);
- glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(sizeof(Vertex) * vertices.size()), vertices.data(),
- GL_STATIC_DRAW);
-
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), offset_ptr(Vertex, pos));
-
- glEnableVertexAttribArray(1);
- glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), offset_ptr(Vertex, texCoord));
-
- glEnableVertexAttribArray(2);
- glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), offset_ptr(Vertex, normal));
-
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vertexArrayBuffers[1]);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, static_cast<GLsizeiptr>(sizeof(indices[0]) * indices.size()), indices.data(),
- GL_STATIC_DRAW);
-
- glBindVertexArray(0);
+ VertexArrayObject<Vertex>::configure<&Vertex::pos, &Vertex::texCoord, &Vertex::normal>(
+ m_vertexArrayObject, m_vertexArrayBuffers[0], m_vertexArrayBuffers[1], vertices, indices);
}
void
@@ -35,7 +16,7 @@ Mesh::Draw() const
{
glBindVertexArray(m_vertexArrayObject);
- glDrawElementsBaseVertex(mode, m_numIndices, GL_UNSIGNED_INT, nullptr, 0);
+ glDrawElements(mode, m_numIndices, GL_UNSIGNED_INT, nullptr);
glBindVertexArray(0);
}