From 064864eac61e936af909470fcccc5c67ef6d3169 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 11 Aug 2024 19:32:23 +0100 Subject: Calculate centre and size of mesh, wrap it all in a Dimensions object --- gfx/models/mesh.h | 51 ++++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 27 deletions(-) (limited to 'gfx/models/mesh.h') diff --git a/gfx/models/mesh.h b/gfx/models/mesh.h index f6a1bd2..8791aed 100644 --- a/gfx/models/mesh.h +++ b/gfx/models/mesh.h @@ -4,6 +4,7 @@ #include "gfx/gl/vertexArrayObject.h" #include #include +#include #include #include @@ -11,35 +12,46 @@ class Vertex; class MeshBase { public: + class Dimensions { + public: + using Extents1D = std::ranges::minmax_result; + explicit Dimensions(const std::span); + + RelativePosition3D minExtent, maxExtent; + RelativePosition3D centre; + RelativeDistance size; + + private: + Dimensions(const std::span, const std::array &); + static Extents1D extents(const std::span, glm::length_t D); + }; + void Draw() const; void DrawInstanced(GLuint vao, GLsizei count, GLuint base = 0) const; - auto - minExtent() const + [[nodiscard]] const Dimensions & + getDimensions() const { - return min; - } - - auto - maxExtent() const - { - return max; + return dimensions; } protected: - MeshBase(GLsizei m_numIndices, GLenum mode, std::pair minmax); + MeshBase(GLsizei m_numIndices, GLenum mode, const std::vector &); glVertexArray m_vertexArrayObject; glBuffers<2> m_vertexArrayBuffers; GLsizei m_numIndices; GLenum mode; - RelativePosition3D min, max; + Dimensions dimensions; }; template class MeshT : public MeshBase, public ConstTypeDefs> { public: MeshT(const std::span vertices, const std::span indices, GLenum mode = GL_TRIANGLES) : - MeshBase {static_cast(indices.size()), mode, extent(vertices)} + MeshBase {static_cast(indices.size()), mode, + materializeRange(vertices | std::views::transform([](const auto & v) { + return static_cast(v.pos); + }))} { VertexArrayObject::data(vertices, m_vertexArrayBuffers[0], GL_ARRAY_BUFFER); VertexArrayObject::data(indices, m_vertexArrayBuffers[1], GL_ARRAY_BUFFER); @@ -51,21 +63,6 @@ public: { return vao.addAttribsFor(m_vertexArrayBuffers[0]).addIndices(m_vertexArrayBuffers[1]); } - - static auto - extent(const std::span vertices) - { - std::pair out {}; - for (glm::length_t D {}; D < 3; ++D) { - const auto mm - = std::minmax_element(vertices.begin(), vertices.end(), [D](const auto & va, const auto & vb) { - return va.pos[D] < vb.pos[D]; - }); - out.first[D] = mm.first->pos[D]; - out.second[D] = mm.second->pos[D]; - } - return out; - } }; using Mesh = MeshT; -- cgit v1.2.3