From 4102debac6fc9bcc58091ead007dbce47b42c712 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 20 Jul 2024 12:19:12 +0100 Subject: Calculate and store the extents of a mesh --- gfx/models/mesh.cpp | 5 ++++- gfx/models/mesh.h | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) (limited to 'gfx/models') diff --git a/gfx/models/mesh.cpp b/gfx/models/mesh.cpp index e7474ca..1b6ecda 100644 --- a/gfx/models/mesh.cpp +++ b/gfx/models/mesh.cpp @@ -1,6 +1,9 @@ #include "mesh.h" -MeshBase::MeshBase(GLsizei m_numIndices, GLenum mode) : m_numIndices {m_numIndices}, mode {mode} { } +MeshBase::MeshBase(GLsizei m_numIndices, GLenum mode, std::pair minmax) : + m_numIndices {m_numIndices}, mode {mode}, min {minmax.first}, max {minmax.second} +{ +} void MeshBase::Draw() const diff --git a/gfx/models/mesh.h b/gfx/models/mesh.h index 248cb8f..f6a1bd2 100644 --- a/gfx/models/mesh.h +++ b/gfx/models/mesh.h @@ -1,5 +1,6 @@ #pragma once +#include "config/types.h" #include "gfx/gl/vertexArrayObject.h" #include #include @@ -13,19 +14,32 @@ public: void Draw() const; void DrawInstanced(GLuint vao, GLsizei count, GLuint base = 0) const; + auto + minExtent() const + { + return min; + } + + auto + maxExtent() const + { + return max; + } + protected: - MeshBase(GLsizei m_numIndices, GLenum mode); + MeshBase(GLsizei m_numIndices, GLenum mode, std::pair minmax); glVertexArray m_vertexArrayObject; glBuffers<2> m_vertexArrayBuffers; GLsizei m_numIndices; GLenum mode; + RelativePosition3D min, max; }; 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} + MeshBase {static_cast(indices.size()), mode, extent(vertices)} { VertexArrayObject::data(vertices, m_vertexArrayBuffers[0], GL_ARRAY_BUFFER); VertexArrayObject::data(indices, m_vertexArrayBuffers[1], GL_ARRAY_BUFFER); @@ -37,6 +51,21 @@ 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 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.cpp | 27 +++++++++++++++++++++++++-- gfx/models/mesh.h | 51 ++++++++++++++++++++++++--------------------------- 2 files changed, 49 insertions(+), 29 deletions(-) (limited to 'gfx/models') 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 minmax) : - m_numIndices {m_numIndices}, mode {mode}, min {minmax.first}, max {minmax.second} +MeshBase::MeshBase(GLsizei m_numIndices, GLenum mode, const std::vector & positions) : + m_numIndices {m_numIndices}, mode {mode}, dimensions {positions} { } +MeshBase::Dimensions::Dimensions(const std::span positions) : + Dimensions {positions, {extents(positions, 0), extents(positions, 1), extents(positions, 2)}} +{ +} + +MeshBase::Dimensions::Dimensions( + const std::span positions, const std::array & 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 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 #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 From 995961c162b4e79c690f722c61c01165ef497e9f Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 2 Sep 2024 20:49:04 +0100 Subject: Update Texture::getSize and ::size to account for the third texture dimension --- gfx/models/texture.cpp | 9 +++++---- gfx/models/texture.h | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'gfx/models') diff --git a/gfx/models/texture.cpp b/gfx/models/texture.cpp index 51223aa..a508421 100644 --- a/gfx/models/texture.cpp +++ b/gfx/models/texture.cpp @@ -59,12 +59,13 @@ Texture::bind(GLenum unit) const glBindTexture(type, m_texture); } -TextureAbsCoord +TextureDimensions Texture::getSize(const glTexture & texture) { - TextureAbsCoord size; + TextureDimensions size {}; glGetTextureLevelParameteriv(texture, 0, GL_TEXTURE_WIDTH, &size.x); glGetTextureLevelParameteriv(texture, 0, GL_TEXTURE_HEIGHT, &size.y); + glGetTextureLevelParameteriv(texture, 0, GL_TEXTURE_DEPTH, &size.z); return size; } @@ -73,7 +74,7 @@ Texture::save( const glTexture & texture, GLenum format, GLenum type, uint8_t channels, const char * path, uint8_t tgaFormat) { const auto size = getSize(texture); - const size_t dataSize = (static_cast(size.x * size.y * channels)); + const size_t dataSize = (static_cast(size.x * size.y * size.z * channels)); const size_t fileSize = dataSize + sizeof(TGAHead); filesystem::fh out {path, O_RDWR | O_CREAT, 0660}; @@ -81,7 +82,7 @@ Texture::save( auto tga = out.mmap(fileSize, 0, PROT_WRITE, MAP_SHARED); *tga.get() = { .format = tgaFormat, - .size = size, + .size = {size.x, size.y * size.z}, .pixelDepth = static_cast(8 * channels), }; glPixelStorei(GL_PACK_ALIGNMENT, 1); diff --git a/gfx/models/texture.h b/gfx/models/texture.h index 8cb8128..3329511 100644 --- a/gfx/models/texture.h +++ b/gfx/models/texture.h @@ -41,7 +41,7 @@ public: protected: static void save(const glTexture &, GLenum, GLenum, uint8_t channels, const char * path, uint8_t tgaFormat); - static TextureAbsCoord getSize(const glTexture &); + static TextureDimensions getSize(const glTexture &); glTexture m_texture; GLenum type; -- cgit v1.2.3