aboutsummaryrefslogtreecommitdiffstats
path: root/src/graphics
diff options
context:
space:
mode:
Diffstat (limited to 'src/graphics')
-rw-r--r--src/graphics/Mesh.cpp48
-rw-r--r--src/graphics/Mesh.hpp19
2 files changed, 67 insertions, 0 deletions
diff --git a/src/graphics/Mesh.cpp b/src/graphics/Mesh.cpp
new file mode 100644
index 0000000..64c4c7e
--- /dev/null
+++ b/src/graphics/Mesh.cpp
@@ -0,0 +1,48 @@
+#include <GL/glew.h>
+
+#include "Mesh.hpp"
+
+Mesh::Mesh(Vertex *vertices, size_t num_vert, GLuint *indices, size_t num_indices)
+ : number_indices(num_indices)
+{
+ glGenVertexArrays(1, &VAO);
+ glGenBuffers(1, &VBO);
+ glGenBuffers(1, &EBO);
+
+ glBindVertexArray(VAO);
+
+ glBindBuffer(GL_ARRAY_BUFFER, VBO);
+ glBufferData(GL_ARRAY_BUFFER, num_vert * sizeof(Vertex),
+ &vertices[0], GL_STATIC_DRAW);
+
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, num_indices * sizeof(GLuint),
+ &indices[0], GL_STATIC_DRAW);
+
+ // attributes with coords
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
+ (GLvoid *)0);
+ glEnableVertexAttribArray(0);
+
+ // attributes with texture
+ glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
+ (GLvoid *)offsetof(Vertex, tex_coords));
+ glEnableVertexAttribArray(1);
+
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+
+ glBindVertexArray(0);
+}
+
+Mesh::~Mesh()
+{
+ glDeleteBuffers(1, &VBO);
+ glDeleteVertexArrays(1, &VAO);
+}
+
+void Mesh::Draw()
+{
+ glBindVertexArray(VAO);
+ glDrawElements(GL_TRIANGLES, number_indices, GL_UNSIGNED_INT, 0);
+ glBindVertexArray(0);
+}
diff --git a/src/graphics/Mesh.hpp b/src/graphics/Mesh.hpp
new file mode 100644
index 0000000..577a32a
--- /dev/null
+++ b/src/graphics/Mesh.hpp
@@ -0,0 +1,19 @@
+#ifndef ENGINE_MESH_H
+#define ENGINE_MESH_H
+
+#include <GL/glew.h>
+#include "../graphics/Vertex.hpp"
+
+class Mesh {
+ GLuint VAO, VBO, EBO;
+ size_t number_indices;
+
+public:
+ Mesh(Vertex *vertices, size_t num_vert,
+ GLuint *indices, size_t num_indices);
+ ~Mesh();
+
+ void Draw();
+};
+
+#endif /* ENGINE_VERTEX_H */