diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-05-10 00:02:50 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-05-10 00:02:50 +0100 |
commit | ef00fc9ae1323b64d5e167d04a987887c60c6ec0 (patch) | |
tree | a214a04fca7969d5c892422cc462e0bf31e80ee9 /gfx | |
parent | Support for tessellation shaders (diff) | |
download | ilt-ef00fc9ae1323b64d5e167d04a987887c60c6ec0.tar.bz2 ilt-ef00fc9ae1323b64d5e167d04a987887c60c6ec0.tar.xz ilt-ef00fc9ae1323b64d5e167d04a987887c60c6ec0.zip |
Make Mesh into a template to support any vertex type
Customisation point VertexArrayObject to define the layout for the type
Diffstat (limited to 'gfx')
-rw-r--r-- | gfx/gl/vertexArrayObject.h | 3 | ||||
-rw-r--r-- | gfx/models/mesh.cpp | 22 | ||||
-rw-r--r-- | gfx/models/mesh.h | 30 | ||||
-rw-r--r-- | gfx/models/vertex.cpp | 10 |
4 files changed, 40 insertions, 25 deletions
diff --git a/gfx/gl/vertexArrayObject.h b/gfx/gl/vertexArrayObject.h index 085cba6..1d27588 100644 --- a/gfx/gl/vertexArrayObject.h +++ b/gfx/gl/vertexArrayObject.h @@ -46,6 +46,9 @@ public: return *this; } + // Customisation point + template<typename VertexT> VertexArrayObject & addAttribsFor(const GLuint arrayBuffer, const GLuint divisor = 0); + template<typename Indices> VertexArrayObject & addIndices(const GLuint arrayBuffer, const Indices & indices) diff --git a/gfx/models/mesh.cpp b/gfx/models/mesh.cpp index a1ce991..1f4dc09 100644 --- a/gfx/models/mesh.cpp +++ b/gfx/models/mesh.cpp @@ -1,28 +1,12 @@ #include "mesh.h" -#include "gfx/gl/vertexArrayObject.h" #include "glArrays.h" #include "vertex.h" #include <cstddef> -Mesh::Mesh(const std::span<const Vertex> vertices, const std::span<const unsigned int> indices, GLenum m) : - m_numIndices {static_cast<GLsizei>(indices.size())}, mode {m} -{ - VertexArrayObject::data(vertices, m_vertexArrayBuffers[0], GL_ARRAY_BUFFER); - VertexArrayObject::data(indices, m_vertexArrayBuffers[1], GL_ARRAY_BUFFER); - configureVAO(m_vertexArrayObject); -} - -VertexArrayObject & -Mesh::configureVAO(VertexArrayObject && vao) const -{ - return vao - .addAttribs<Vertex, &Vertex::pos, &Vertex::texCoord, &Vertex::normal, &Vertex::colour, &Vertex::material>( - m_vertexArrayBuffers[0]) - .addIndices(m_vertexArrayBuffers[1]); -} +MeshBase::MeshBase(GLsizei m_numIndices, GLenum mode) : m_numIndices {m_numIndices}, mode {mode} { } void -Mesh::Draw() const +MeshBase::Draw() const { glBindVertexArray(m_vertexArrayObject); @@ -32,7 +16,7 @@ Mesh::Draw() const } void -Mesh::DrawInstanced(GLuint vao, GLsizei count) const +MeshBase::DrawInstanced(GLuint vao, GLsizei count) const { glBindVertexArray(vao); diff --git a/gfx/models/mesh.h b/gfx/models/mesh.h index f022c2a..972fe30 100644 --- a/gfx/models/mesh.h +++ b/gfx/models/mesh.h @@ -1,5 +1,6 @@ #pragma once +#include "gfx/gl/vertexArrayObject.h" #include <GL/glew.h> #include <glArrays.h> #include <memory> @@ -7,19 +8,36 @@ #include <stdTypeDefs.h> class Vertex; -class VertexArrayObject; -class Mesh : public ConstTypeDefs<Mesh> { +class MeshBase { public: - Mesh(const std::span<const Vertex> vertices, const std::span<const unsigned int> indices, GLenum = GL_TRIANGLES); - void Draw() const; void DrawInstanced(GLuint vao, GLsizei count) const; - VertexArrayObject & configureVAO(VertexArrayObject &&) const; -private: +protected: + MeshBase(GLsizei m_numIndices, GLenum mode); + glVertexArray m_vertexArrayObject; glBuffers<2> m_vertexArrayBuffers; GLsizei m_numIndices; GLenum mode; }; + +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} + { + VertexArrayObject::data(vertices, m_vertexArrayBuffers[0], GL_ARRAY_BUFFER); + VertexArrayObject::data(indices, m_vertexArrayBuffers[1], GL_ARRAY_BUFFER); + configureVAO(m_vertexArrayObject); + } + + VertexArrayObject & + configureVAO(VertexArrayObject && vao) const + { + return vao.addAttribsFor<V>(m_vertexArrayBuffers[0]).addIndices(m_vertexArrayBuffers[1]); + } +}; + +using Mesh = MeshT<Vertex>; diff --git a/gfx/models/vertex.cpp b/gfx/models/vertex.cpp new file mode 100644 index 0000000..c144db3 --- /dev/null +++ b/gfx/models/vertex.cpp @@ -0,0 +1,10 @@ +#include "vertex.h" +#include "gfx/gl/vertexArrayObject.h" + +template<> +VertexArrayObject & +VertexArrayObject::addAttribsFor<Vertex>(const GLuint arrayBuffer, const GLuint divisor) +{ + return addAttribs<Vertex, &Vertex::pos, &Vertex::texCoord, &Vertex::normal, &Vertex::colour, &Vertex::material>( + arrayBuffer, divisor); +} |