summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2024-07-20 12:19:12 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2024-08-10 18:11:59 +0100
commit4102debac6fc9bcc58091ead007dbce47b42c712 (patch)
treee26c36635cdf394b5338ce7dfcd5e77394409d9f
parentInitial commit of basic shadow depth map creation (diff)
downloadilt-4102debac6fc9bcc58091ead007dbce47b42c712.tar.bz2
ilt-4102debac6fc9bcc58091ead007dbce47b42c712.tar.xz
ilt-4102debac6fc9bcc58091ead007dbce47b42c712.zip
Calculate and store the extents of a mesh
-rw-r--r--gfx/models/mesh.cpp5
-rw-r--r--gfx/models/mesh.h33
2 files changed, 35 insertions, 3 deletions
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<RelativePosition3D, RelativePosition3D> 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 <glArrays.h>
#include <glad/gl.h>
@@ -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<RelativePosition3D, RelativePosition3D> minmax);
glVertexArray m_vertexArrayObject;
glBuffers<2> m_vertexArrayBuffers;
GLsizei m_numIndices;
GLenum mode;
+ RelativePosition3D min, max;
};
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}
+ MeshBase {static_cast<GLsizei>(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<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>;