aboutsummaryrefslogtreecommitdiffstats
path: root/src/graphics/Mesh.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/graphics/Mesh.cpp')
-rw-r--r--src/graphics/Mesh.cpp48
1 files changed, 48 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);
+}