From 9b0828ea5bb6cb4e92d6019785b3ceb88e2a58be Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 17 Apr 2023 22:43:51 +0100 Subject: Separate storing of mesh vertex/index data from configuring VAO --- gfx/gl/vertexArrayObject.hpp | 11 +++++++++-- gfx/models/mesh.cpp | 14 +++++++++++--- gfx/models/mesh.h | 2 ++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/gfx/gl/vertexArrayObject.hpp b/gfx/gl/vertexArrayObject.hpp index 3e2a18b..8c828c8 100644 --- a/gfx/gl/vertexArrayObject.hpp +++ b/gfx/gl/vertexArrayObject.hpp @@ -7,7 +7,7 @@ class VertexArrayObject { public: - [[nodiscard]] VertexArrayObject(const GLuint arrayObject) + template [[nodiscard]] VertexArrayObject(const T & arrayObject) { glBindVertexArray(arrayObject); } @@ -54,7 +54,13 @@ public: return *this; } -private: + VertexArrayObject & + addIndices(const GLuint arrayBuffer) + { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, arrayBuffer); + return *this; + } + template static void data(const Data & data, const GLuint arrayBuffer, GLenum target) @@ -64,6 +70,7 @@ private: glBufferData(target, static_cast(sizeof(Value) * data.size()), data.data(), GL_STATIC_DRAW); } +private: template static void set_pointer(const GLuint vertexArrayId, const void * ptr) diff --git a/gfx/models/mesh.cpp b/gfx/models/mesh.cpp index 4200703..2c849e7 100644 --- a/gfx/models/mesh.cpp +++ b/gfx/models/mesh.cpp @@ -7,10 +7,18 @@ Mesh::Mesh(const std::span vertices, const std::span indices, GLenum m) : m_numIndices {static_cast(indices.size())}, mode {m} { - VertexArrayObject {m_vertexArrayObject} + VertexArrayObject::data(vertices, m_vertexArrayBuffers[0], GL_ARRAY_BUFFER); + VertexArrayObject::data(indices, m_vertexArrayBuffers[1], GL_ARRAY_BUFFER); + configureVAO(m_vertexArrayObject); +} + +VertexArrayObject & +Mesh::configureVAO(VertexArrayObject && vao) const +{ + return vao .addAttribs( - m_vertexArrayBuffers[0], vertices) - .addIndices(m_vertexArrayBuffers[1], indices); + m_vertexArrayBuffers[0]) + .addIndices(m_vertexArrayBuffers[1]); } void diff --git a/gfx/models/mesh.h b/gfx/models/mesh.h index 25a9064..0af8d70 100644 --- a/gfx/models/mesh.h +++ b/gfx/models/mesh.h @@ -7,12 +7,14 @@ #include class Vertex; +class VertexArrayObject; class Mesh : public ConstTypeDefs { public: Mesh(const std::span vertices, const std::span indices, GLenum = GL_TRIANGLES); void Draw() const; + VertexArrayObject & configureVAO(VertexArrayObject &&) const; private: glVertexArray m_vertexArrayObject; -- cgit v1.2.3