aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2021-03-28 22:26:39 +0000
committerJoursoir <chat@joursoir.net>2021-03-28 22:26:39 +0000
commit47d648b85edb14a18d4645861fb2f08a5b52a33b (patch)
tree109c002a63a75922f3e4c1286c2ad6003b2b36b2
parent7f7123ef4ca5e37da45907eeb7f29ed15e535dce (diff)
downloadspace-simulator-47d648b85edb14a18d4645861fb2f08a5b52a33b.tar.gz
space-simulator-47d648b85edb14a18d4645861fb2f08a5b52a33b.tar.bz2
space-simulator-47d648b85edb14a18d4645861fb2f08a5b52a33b.zip
add mesh routines
-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 */