summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gfx/models/mesh.cpp13
-rw-r--r--gfx/models/mesh.h16
-rw-r--r--lib/glBuffers.cpp13
-rw-r--r--lib/glBuffers.h48
-rw-r--r--lib/glVertexArrays.cpp13
-rw-r--r--lib/glVertexArrays.h48
-rw-r--r--ui/iconButton.cpp9
-rw-r--r--ui/iconButton.h6
8 files changed, 137 insertions, 29 deletions
diff --git a/gfx/models/mesh.cpp b/gfx/models/mesh.cpp
index 82eafbc..28cd021 100644
--- a/gfx/models/mesh.cpp
+++ b/gfx/models/mesh.cpp
@@ -1,17 +1,16 @@
#include "mesh.h"
+#include "glBuffers.h"
+#include "glVertexArrays.h"
#include "vertex.hpp"
#include <cstddef>
#define offset_ptr(T, m) ((reinterpret_cast<char *>(1)) + offsetof(T, m) - 1)
Mesh::Mesh(const std::span<const Vertex> vertices, const std::span<const unsigned int> indices, GLenum m) :
- m_vertexArrayObject {}, m_vertexArrayBuffers {}, m_numIndices {static_cast<GLsizei>(indices.size())}, mode {m}
+ m_vertexArrayBuffers {}, m_numIndices {static_cast<GLsizei>(indices.size())}, mode {m}
{
- glGenVertexArrays(1, &m_vertexArrayObject);
glBindVertexArray(m_vertexArrayObject);
- glGenBuffers(2, m_vertexArrayBuffers.data());
-
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[0]);
glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(sizeof(Vertex) * vertices.size()), vertices.data(),
GL_STATIC_DRAW);
@@ -32,12 +31,6 @@ Mesh::Mesh(const std::span<const Vertex> vertices, const std::span<const unsigne
glBindVertexArray(0);
}
-Mesh::~Mesh()
-{
- glDeleteBuffers(NUM_BUFFERS, m_vertexArrayBuffers.data());
- glDeleteVertexArrays(1, &m_vertexArrayObject);
-}
-
void
Mesh::Draw() const
{
diff --git a/gfx/models/mesh.h b/gfx/models/mesh.h
index c9f1204..4e6cec8 100644
--- a/gfx/models/mesh.h
+++ b/gfx/models/mesh.h
@@ -2,30 +2,22 @@
#define MESH_INCLUDED_H
#include <GL/glew.h>
-#include <array>
+#include <glBuffers.h>
+#include <glVertexArrays.h>
#include <memory>
#include <span>
-#include <special_members.hpp>
class Vertex;
-enum MeshBufferPositions { POSITION_VB, TEXCOORD_VB, NORMAL_VB, INDEX_VB };
-
class Mesh {
public:
Mesh(const std::span<const Vertex> vertices, const std::span<const unsigned int> indices, GLenum = GL_TRIANGLES);
- virtual ~Mesh();
-
- NO_COPY(Mesh);
- NO_MOVE(Mesh);
void Draw() const;
private:
- static constexpr unsigned int NUM_BUFFERS {4};
-
- GLuint m_vertexArrayObject;
- std::array<GLuint, NUM_BUFFERS> m_vertexArrayBuffers;
+ glVertexArray m_vertexArrayObject;
+ glBuffers<2> m_vertexArrayBuffers;
GLsizei m_numIndices;
GLenum mode;
};
diff --git a/lib/glBuffers.cpp b/lib/glBuffers.cpp
new file mode 100644
index 0000000..cb5ab2a
--- /dev/null
+++ b/lib/glBuffers.cpp
@@ -0,0 +1,13 @@
+#include "glBuffers.h"
+
+void
+glBuffersBase::gen(GLsizei n, GLuint * ids)
+{
+ glGenBuffers(n, ids);
+}
+
+void
+glBuffersBase::del(GLsizei n, const GLuint * ids)
+{
+ glDeleteBuffers(n, ids);
+}
diff --git a/lib/glBuffers.h b/lib/glBuffers.h
new file mode 100644
index 0000000..e91293c
--- /dev/null
+++ b/lib/glBuffers.h
@@ -0,0 +1,48 @@
+#ifndef GLBUFFERS_H
+#define GLBUFFERS_H
+
+#include <GL/glew.h>
+#include <array>
+#include <cstddef>
+#include <special_members.hpp>
+
+class glBuffersBase {
+protected:
+ static void gen(GLsizei, GLuint *);
+ static void del(GLsizei, const GLuint *);
+};
+
+template<size_t N> class glBuffers : glBuffersBase {
+public:
+ glBuffers()
+ {
+ gen(N, ids.data());
+ }
+
+ ~glBuffers()
+ {
+ del(N, ids.data());
+ }
+
+ NO_COPY(glBuffers);
+ NO_MOVE(glBuffers);
+
+ // NOLINTNEXTLINE(hicpp-explicit-conversions)
+ operator GLuint() const
+ {
+ static_assert(N == 1, "Implicit cast only if N == 1");
+ return ids.front();
+ }
+
+ auto
+ operator[](size_t n) const
+ {
+ return ids[n];
+ }
+
+private:
+ std::array<GLuint, N> ids {};
+};
+using glBuffer = glBuffers<1>;
+
+#endif
diff --git a/lib/glVertexArrays.cpp b/lib/glVertexArrays.cpp
new file mode 100644
index 0000000..372b49b
--- /dev/null
+++ b/lib/glVertexArrays.cpp
@@ -0,0 +1,13 @@
+#include "glVertexArrays.h"
+
+void
+glVertexArraysBase::gen(GLsizei n, GLuint * ids)
+{
+ glGenVertexArrays(n, ids);
+}
+
+void
+glVertexArraysBase::del(GLsizei n, const GLuint * ids)
+{
+ glDeleteVertexArrays(n, ids);
+}
diff --git a/lib/glVertexArrays.h b/lib/glVertexArrays.h
new file mode 100644
index 0000000..ab31751
--- /dev/null
+++ b/lib/glVertexArrays.h
@@ -0,0 +1,48 @@
+#ifndef GLVERTEXARRAYS_H
+#define GLVERTEXARRAYS_H
+
+#include <GL/glew.h>
+#include <array>
+#include <cstddef>
+#include <special_members.hpp>
+
+class glVertexArraysBase {
+protected:
+ static void gen(GLsizei, GLuint *);
+ static void del(GLsizei, const GLuint *);
+};
+
+template<size_t N> class glVertexArrays : glVertexArraysBase {
+public:
+ glVertexArrays()
+ {
+ gen(N, ids.data());
+ }
+
+ ~glVertexArrays()
+ {
+ del(N, ids.data());
+ }
+
+ NO_COPY(glVertexArrays);
+ NO_MOVE(glVertexArrays);
+
+ // NOLINTNEXTLINE(hicpp-explicit-conversions)
+ operator GLuint() const
+ {
+ static_assert(N == 1, "Implicit cast only if N == 1");
+ return ids.front();
+ }
+
+ auto
+ operator[](size_t n) const
+ {
+ return ids[n];
+ }
+
+private:
+ std::array<GLuint, N> ids {};
+};
+using glVertexArray = glVertexArrays<1>;
+
+#endif
diff --git a/ui/iconButton.cpp b/ui/iconButton.cpp
index c35303f..c97896f 100644
--- a/ui/iconButton.cpp
+++ b/ui/iconButton.cpp
@@ -1,6 +1,9 @@
#include "iconButton.h"
+#include "glBuffers.h"
+#include "glVertexArrays.h"
#include "ui/icon.h"
#include "ui/uiComponent.h"
+#include <GL/glew.h>
#include <SDL2/SDL.h>
#include <array>
#include <filesystem>
@@ -9,14 +12,10 @@
#include <utility>
IconButton::IconButton(const std::string & icon_, glm::vec2 position_, UIEvent click_) :
- UIComponent {{position_, ICON_SIZE}}, icon {icon_}, click {std::move(click_)}, m_vertexArrayObject {},
- m_vertexArrayBuffer {}
+ UIComponent {{position_, ICON_SIZE}}, icon {icon_}, click {std::move(click_)}
{
- glGenVertexArrays(1, &m_vertexArrayObject);
glBindVertexArray(m_vertexArrayObject);
- glGenBuffers(1, &m_vertexArrayBuffer);
-
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffer);
glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(sizeof(glm::vec4) * 4), nullptr, GL_DYNAMIC_DRAW);
diff --git a/ui/iconButton.h b/ui/iconButton.h
index ec3f357..76e3f1b 100644
--- a/ui/iconButton.h
+++ b/ui/iconButton.h
@@ -3,7 +3,8 @@
#include "icon.h"
#include "uiComponent.h"
-#include <GL/glew.h>
+#include <glBuffers.h>
+#include <glVertexArrays.h>
#include <glm/glm.hpp>
#include <string>
@@ -21,7 +22,8 @@ public:
Icon icon;
UIEvent click;
- GLuint m_vertexArrayObject, m_vertexArrayBuffer;
+ glVertexArray m_vertexArrayObject;
+ glBuffer m_vertexArrayBuffer;
};
#endif