diff options
Diffstat (limited to 'gfx/models/mesh.h')
| -rw-r--r-- | gfx/models/mesh.h | 53 |
1 files changed, 33 insertions, 20 deletions
diff --git a/gfx/models/mesh.h b/gfx/models/mesh.h index 8791aed..fa12ae8 100644 --- a/gfx/models/mesh.h +++ b/gfx/models/mesh.h @@ -1,8 +1,9 @@ #pragma once #include "config/types.h" -#include "gfx/gl/vertexArrayObject.h" -#include <glArrays.h> +#include "gfx/gl/glBuffer.h" +#include "gfx/gl/gldebug.h" +#include <gfx/gl/glVertexArray.h> #include <glad/gl.h> #include <ranges> #include <span> @@ -15,19 +16,19 @@ public: class Dimensions { public: using Extents1D = std::ranges::minmax_result<RelativeDistance>; - explicit Dimensions(const std::span<const RelativePosition3D>); + explicit Dimensions(std::span<const RelativePosition3D>); RelativePosition3D minExtent, maxExtent; RelativePosition3D centre; RelativeDistance size; private: - Dimensions(const std::span<const RelativePosition3D>, const std::array<Extents1D, 3> &); - static Extents1D extents(const std::span<const RelativePosition3D>, glm::length_t D); + Dimensions(std::span<const RelativePosition3D>, const std::array<Extents1D, 3> &); + static Extents1D extents(std::span<const RelativePosition3D>, glm::length_t); }; - void Draw() const; - void DrawInstanced(GLuint vao, GLsizei count, GLuint base = 0) const; + void draw() const; + void drawInstanced(GLuint vao, GLsizei count, GLuint base = 0) const; [[nodiscard]] const Dimensions & getDimensions() const @@ -36,11 +37,12 @@ public: } protected: - MeshBase(GLsizei m_numIndices, GLenum mode, const std::vector<RelativePosition3D> &); + MeshBase(GLsizei numIndices, GLenum mode, const std::vector<RelativePosition3D> &, GLsizei vertexStride); - glVertexArray m_vertexArrayObject; - glBuffers<2> m_vertexArrayBuffers; - GLsizei m_numIndices; + std::shared_ptr<glVertexArray> vertexArrayObject; + glBuffers<2> vertexArrayBuffers; + GLsizei vertexStride; + GLsizei numIndices; GLenum mode; Dimensions dimensions; }; @@ -49,20 +51,31 @@ template<typename V> class MeshT : public MeshBase, public ConstTypeDefs<MeshT<V public: MeshT(const std::span<const V> vertices, const std::span<const unsigned int> indices, GLenum mode = GL_TRIANGLES) : MeshBase {static_cast<GLsizei>(indices.size()), mode, - materializeRange(vertices | std::views::transform([](const auto & v) { - return static_cast<RelativePosition3D>(v.pos); - }))} + materializeRange(vertices | std::views::transform([](const auto & vertex) { + return static_cast<RelativePosition3D>(vertex.pos); + })), + sizeof(V)} { - VertexArrayObject::data(vertices, m_vertexArrayBuffers[0], GL_ARRAY_BUFFER); - VertexArrayObject::data(indices, m_vertexArrayBuffers[1], GL_ARRAY_BUFFER); - configureVAO(m_vertexArrayObject); + glDebugScope _ {0}; + vertexArrayBuffers[0].storage(vertices, 0); + vertexArrayBuffers[1].storage(indices, 0); + if (!(vertexArrayObject = commonVertexArrayObject.lock())) { + commonVertexArrayObject = vertexArrayObject = std::make_shared<glVertexArray>(); + configureVAO(*vertexArrayObject, 0); + } } - VertexArrayObject & - configureVAO(VertexArrayObject && vao) const + auto + configureVAO(glVertexArray & vao, GLuint divisor) const { - return vao.addAttribsFor<V>(m_vertexArrayBuffers[0]).addIndices(m_vertexArrayBuffers[1]); + glDebugScope _ {0}; + return vao.configure().addAttribsFor<V>(divisor); } + +protected: + static std::weak_ptr<glVertexArray> commonVertexArrayObject; }; +template<typename T> std::weak_ptr<glVertexArray> MeshT<T>::commonVertexArrayObject; + using Mesh = MeshT<Vertex>; |
