summaryrefslogtreecommitdiff
path: root/gfx/models
diff options
context:
space:
mode:
Diffstat (limited to 'gfx/models')
-rw-r--r--gfx/models/mesh.cpp11
-rw-r--r--gfx/models/mesh.h26
2 files changed, 28 insertions, 9 deletions
diff --git a/gfx/models/mesh.cpp b/gfx/models/mesh.cpp
index 88314ae..6a53f52 100644
--- a/gfx/models/mesh.cpp
+++ b/gfx/models/mesh.cpp
@@ -1,7 +1,8 @@
#include "mesh.h"
-MeshBase::MeshBase(GLsizei m_numIndices, GLenum mode, const std::vector<RelativePosition3D> & positions) :
- numIndices {m_numIndices}, mode {mode}, dimensions {positions}
+MeshBase::MeshBase(
+ GLsizei m_numIndices, GLenum mode, const std::vector<RelativePosition3D> & positions, GLsizei vertexStride) :
+ vertexStride {vertexStride}, numIndices {m_numIndices}, mode {mode}, dimensions {positions}
{
}
@@ -31,7 +32,9 @@ MeshBase::Dimensions::extents(const std::span<const RelativePosition3D> position
void
MeshBase::draw() const
{
- glBindVertexArray(vertexArrayObject);
+ glBindVertexArray(*vertexArrayObject);
+ glVertexArrayVertexBuffer(*vertexArrayObject, 0, vertexArrayBuffers[0], 0, vertexStride);
+ glVertexArrayElementBuffer(*vertexArrayObject, vertexArrayBuffers[1]);
glDrawElements(mode, numIndices, GL_UNSIGNED_INT, nullptr);
@@ -42,6 +45,8 @@ void
MeshBase::drawInstanced(GLuint vao, GLsizei count, GLuint base) const
{
glBindVertexArray(vao);
+ glVertexArrayVertexBuffer(vao, 0, vertexArrayBuffers[0], 0, vertexStride);
+ glVertexArrayElementBuffer(vao, vertexArrayBuffers[1]);
glDrawElementsInstancedBaseInstance(mode, numIndices, GL_UNSIGNED_INT, nullptr, count, base);
diff --git a/gfx/models/mesh.h b/gfx/models/mesh.h
index d4dbd68..fa12ae8 100644
--- a/gfx/models/mesh.h
+++ b/gfx/models/mesh.h
@@ -2,13 +2,15 @@
#include "config/types.h"
#include "gfx/gl/glBuffer.h"
-#include "gfx/models/vertex.h"
+#include "gfx/gl/gldebug.h"
#include <gfx/gl/glVertexArray.h>
#include <glad/gl.h>
#include <ranges>
#include <span>
#include <stdTypeDefs.h>
+class Vertex;
+
class MeshBase {
public:
class Dimensions {
@@ -35,10 +37,11 @@ public:
}
protected:
- MeshBase(GLsizei numIndices, GLenum mode, const std::vector<RelativePosition3D> &);
+ MeshBase(GLsizei numIndices, GLenum mode, const std::vector<RelativePosition3D> &, GLsizei vertexStride);
- glVertexArray vertexArrayObject;
+ std::shared_ptr<glVertexArray> vertexArrayObject;
glBuffers<2> vertexArrayBuffers;
+ GLsizei vertexStride;
GLsizei numIndices;
GLenum mode;
Dimensions dimensions;
@@ -50,18 +53,29 @@ public:
MeshBase {static_cast<GLsizei>(indices.size()), mode,
materializeRange(vertices | std::views::transform([](const auto & vertex) {
return static_cast<RelativePosition3D>(vertex.pos);
- }))}
+ })),
+ sizeof(V)}
{
+ glDebugScope _ {0};
vertexArrayBuffers[0].storage(vertices, 0);
vertexArrayBuffers[1].storage(indices, 0);
- configureVAO(vertexArrayObject, 0);
+ if (!(vertexArrayObject = commonVertexArrayObject.lock())) {
+ commonVertexArrayObject = vertexArrayObject = std::make_shared<glVertexArray>();
+ configureVAO(*vertexArrayObject, 0);
+ }
}
auto
configureVAO(glVertexArray & vao, GLuint divisor) const
{
- return vao.configure().addAttribsFor<V>(divisor, vertexArrayBuffers[0]).addIndices(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>;