diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-08-11 19:32:23 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-08-11 19:32:23 +0100 |
commit | 064864eac61e936af909470fcccc5c67ef6d3169 (patch) | |
tree | 079ebd953589207cf70b87648eedd543013b078a /gfx | |
parent | Use texture alpha in shadow stencil (diff) | |
download | ilt-064864eac61e936af909470fcccc5c67ef6d3169.tar.bz2 ilt-064864eac61e936af909470fcccc5c67ef6d3169.tar.xz ilt-064864eac61e936af909470fcccc5c67ef6d3169.zip |
Calculate centre and size of mesh, wrap it all in a Dimensions object
Diffstat (limited to 'gfx')
-rw-r--r-- | gfx/gl/shadowStenciller.cpp | 4 | ||||
-rw-r--r-- | gfx/models/mesh.cpp | 27 | ||||
-rw-r--r-- | gfx/models/mesh.h | 51 |
3 files changed, 51 insertions, 31 deletions
diff --git a/gfx/gl/shadowStenciller.cpp b/gfx/gl/shadowStenciller.cpp index 2ba519f..dc87d76 100644 --- a/gfx/gl/shadowStenciller.cpp +++ b/gfx/gl/shadowStenciller.cpp @@ -47,8 +47,8 @@ ShadowStenciller::renderStencil(const glTexture & stencil, const MeshBase & mesh glClear(GL_DEPTH_BUFFER_BIT); glViewport(0, 0, 256, 256); glEnable(GL_DEPTH_TEST); - const auto & mins = mesh.minExtent(); - const auto & maxs = mesh.maxExtent(); + const auto & mins = mesh.getDimensions().minExtent; + const auto & maxs = mesh.getDimensions().maxExtent; const auto extents = glm::ortho(mins.x, maxs.x, mins.z, maxs.z, mins.y, maxs.y); const auto lightDir = glm::lookAt({}, north, up); glUniform(viewProjectionLoc, extents * lightDir); diff --git a/gfx/models/mesh.cpp b/gfx/models/mesh.cpp index 1b6ecda..2eae160 100644 --- a/gfx/models/mesh.cpp +++ b/gfx/models/mesh.cpp @@ -1,10 +1,33 @@ #include "mesh.h" -MeshBase::MeshBase(GLsizei m_numIndices, GLenum mode, std::pair<RelativePosition3D, RelativePosition3D> minmax) : - m_numIndices {m_numIndices}, mode {mode}, min {minmax.first}, max {minmax.second} +MeshBase::MeshBase(GLsizei m_numIndices, GLenum mode, const std::vector<RelativePosition3D> & positions) : + m_numIndices {m_numIndices}, mode {mode}, dimensions {positions} { } +MeshBase::Dimensions::Dimensions(const std::span<const RelativePosition3D> positions) : + Dimensions {positions, {extents(positions, 0), extents(positions, 1), extents(positions, 2)}} +{ +} + +MeshBase::Dimensions::Dimensions( + const std::span<const RelativePosition3D> positions, const std::array<Extents1D, 3> & extents1ds) : + minExtent(extents1ds[0].min, extents1ds[1].min, extents1ds[2].min), + maxExtent(extents1ds[0].max, extents1ds[1].max, extents1ds[2].max), centre {(minExtent + maxExtent) / 2.0F}, + size {std::ranges::max(positions | std::views::transform([this](const auto & v) { + return glm::distance(v, centre); + }))} +{ +} + +MeshBase::Dimensions::Extents1D +MeshBase::Dimensions::extents(const std::span<const RelativePosition3D> positions, glm::length_t D) +{ + return std::ranges::minmax(positions | std::views::transform([D](const auto & v) { + return v[D]; + })); +} + void MeshBase::Draw() const { 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 <glArrays.h> #include <glad/gl.h> +#include <ranges> #include <span> #include <stdTypeDefs.h> @@ -11,35 +12,46 @@ class Vertex; class MeshBase { public: + class Dimensions { + public: + using Extents1D = std::ranges::minmax_result<RelativeDistance>; + explicit Dimensions(const 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); + }; + 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<RelativePosition3D, RelativePosition3D> minmax); + MeshBase(GLsizei m_numIndices, GLenum mode, const std::vector<RelativePosition3D> &); glVertexArray m_vertexArrayObject; glBuffers<2> m_vertexArrayBuffers; GLsizei m_numIndices; GLenum mode; - RelativePosition3D min, max; + Dimensions dimensions; }; 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, extent(vertices)} + MeshBase {static_cast<GLsizei>(indices.size()), mode, + materializeRange(vertices | std::views::transform([](const auto & v) { + return static_cast<RelativePosition3D>(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<V>(m_vertexArrayBuffers[0]).addIndices(m_vertexArrayBuffers[1]); } - - static auto - extent(const std::span<const V> vertices) - { - std::pair<decltype(V::pos), decltype(V::pos)> 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<Vertex>; |