summary refs log tree commit diff
path: root/src/Binder.cpp
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-10-01 16:59:29 +0200
committerMel <einebeere@gmail.com>2022-10-01 16:59:29 +0200
commitaca1326e7e3cc4c06e29b92dca256673b46aa510 (patch)
treec7cb614990afa3140700448ab1c89091e1f6e347 /src/Binder.cpp
parente2234b4afebb266878435d10267e7b162b1fe984 (diff)
downloadmeowcraft-aca1326e7e3cc4c06e29b92dca256673b46aa510.tar.zst
meowcraft-aca1326e7e3cc4c06e29b92dca256673b46aa510.zip
Triangle
Diffstat (limited to 'src/Binder.cpp')
-rw-r--r--src/Binder.cpp50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/Binder.cpp b/src/Binder.cpp
new file mode 100644
index 0000000..8f9b7df
--- /dev/null
+++ b/src/Binder.cpp
@@ -0,0 +1,50 @@
+#include <GL/glew.h>
+#include "Binder.hpp"
+#include "Mesh.hpp"
+
+namespace MC {
+
+BindableMesh Binder::load(Mesh& mesh) {
+    auto vao = create_vao();
+    store_in_attribute_list(0, 3, mesh.flat(), mesh.size() * 3);
+    unbind_vao();
+
+    return {vao, mesh.size()};
+}
+
+uint32_t Binder::create_vao() {
+    GLuint vao;
+    glGenVertexArrays(1, &vao);
+    glBindVertexArray(vao);
+
+    return static_cast<uint32_t>(vao);
+}
+
+uint32_t Binder::unbind_vao() {
+    glBindVertexArray(0);
+}
+
+void Binder::store_in_attribute_list(uint32_t attribute, size_t size, float* data, size_t data_size) {
+    GLuint vbo;
+    glGenBuffers(1, &vbo);
+
+    glBindBuffer(GL_ARRAY_BUFFER, vbo);
+    glBufferData(GL_ARRAY_BUFFER, data_size * sizeof(float), data, GL_STATIC_DRAW);
+    glVertexAttribPointer(attribute, size, GL_FLOAT, GL_FALSE, 3 * sizeof(float), nullptr);
+}
+
+void BindableMesh::bind() const {
+    glBindVertexArray(m_vao);
+    glEnableVertexAttribArray(0);
+}
+
+void BindableMesh::unbind() {
+    glBindVertexArray(0);
+    glDisableVertexAttribArray(0);
+}
+
+size_t BindableMesh::size() const {
+    return m_vertex_count;
+}
+
+}
\ No newline at end of file