summary refs log tree commit diff
path: root/src/GFX
diff options
context:
space:
mode:
Diffstat (limited to 'src/GFX')
-rw-r--r--src/GFX/Binder.cpp84
-rw-r--r--src/GFX/Binder.hpp48
-rw-r--r--src/GFX/Mesh.cpp75
-rw-r--r--src/GFX/Mesh.hpp18
4 files changed, 86 insertions, 139 deletions
diff --git a/src/GFX/Binder.cpp b/src/GFX/Binder.cpp
deleted file mode 100644
index 49f82a7..0000000
--- a/src/GFX/Binder.cpp
+++ /dev/null
@@ -1,84 +0,0 @@
-#include <GL/glew.h>
-#include <cassert>
-#include "Binder.hpp"
-#include "Mesh.hpp"
-
-namespace MC::GFX {
-
-BindableMesh Binder::load(const Mesh& mesh) {
-    auto vao = create_vao();
-    if (!mesh.indices().empty()) {
-        store_indices(mesh.indices().data(), mesh.indices().size());
-    }
-
-    Int attribute_index = 0;
-    for (const auto& attribute : mesh.attributes()) {
-        store_in_attribute_list(
-            attribute_index++,
-            attribute.attribute_size,
-            attribute.type_size,
-            attribute.data,
-            attribute.data_size
-        );
-    }
-    unbind_vao();
-
-    return {vao, mesh.indices().size(), mesh.attributes().size()};
-}
-
-U32 Binder::create_vao() {
-    GLuint vao;
-    glGenVertexArrays(1, &vao);
-    glBindVertexArray(vao);
-
-    return vao;
-}
-
-void Binder::unbind_vao() {
-    glBindVertexArray(0);
-}
-
-void Binder::store_indices(const U32* indices, USize indices_size) {
-    GLuint ebo;
-    glGenBuffers(1, &ebo);
-
-    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
-    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices_size * sizeof(U32), indices, GL_STATIC_DRAW);
-}
-
-
-void Binder::store_in_attribute_list(U32 attribute, Int attribute_size, Int type_size, const void* data, long data_size) {
-    assert(type_size == sizeof(F32));
-
-    GLuint vbo;
-    glGenBuffers(1, &vbo);
-
-    glBindBuffer(GL_ARRAY_BUFFER, vbo);
-    glBufferData(GL_ARRAY_BUFFER, data_size * attribute_size * type_size, data, GL_STATIC_DRAW);
-    glVertexAttribPointer(attribute, attribute_size, GL_FLOAT, GL_FALSE, attribute_size * type_size, nullptr);
-    glBindBuffer(GL_ARRAY_BUFFER, 0);
-}
-
-void BindableMesh::bind() const {
-    glBindVertexArray(m_vao);
-    for (Int i = 0; i < m_attribute_count; i++) {
-        glEnableVertexAttribArray(i);
-    }
-}
-
-void BindableMesh::unbind() const {
-    glBindVertexArray(0);
-    for (Int i = 0; i < m_attribute_count; i++) {
-        glDisableVertexAttribArray(i);
-    }
-}
-
-Bool BindableMesh::has_indices() const {
-    return m_has_indices;
-}
-
-USize BindableMesh::size() const {
-    return m_vertex_count;
-}
-
-}
\ No newline at end of file
diff --git a/src/GFX/Binder.hpp b/src/GFX/Binder.hpp
deleted file mode 100644
index 6103e09..0000000
--- a/src/GFX/Binder.hpp
+++ /dev/null
@@ -1,48 +0,0 @@
-#pragma once
-
-#include "../Common/Sizes.hpp"
-#include "Mesh.hpp"
-
-namespace MC::GFX {
-
-class BindableMesh {
-public:
-    void bind() const;
-    void unbind() const;
-
-    Bool has_indices() const;
-    USize size() const;
-
-private:
-    BindableMesh(
-        U32 vao,
-        USize vertex_count,
-        USize attribute_count
-    ) : m_vao(vao),
-        m_vertex_count(vertex_count),
-        m_has_indices(vertex_count > 0),
-        m_attribute_count(attribute_count) {}
-
-    U32 m_vao;
-    USize m_vertex_count;
-    Bool m_has_indices;
-    USize m_attribute_count;
-
-    friend class Binder;
-};
-
-class Binder {
-public:
-    Binder() = default;
-
-    static BindableMesh load(const Mesh& mesh);
-
-private:
-    static U32 create_vao();
-    static void unbind_vao();
-
-    static void store_in_attribute_list(U32 attribute, Int attribute_size, Int type_size, const void* data, long data_size);
-    static void store_indices(const U32* indices, USize indices_size);
-};
-
-}
\ No newline at end of file
diff --git a/src/GFX/Mesh.cpp b/src/GFX/Mesh.cpp
index c45b5c9..0633e11 100644
--- a/src/GFX/Mesh.cpp
+++ b/src/GFX/Mesh.cpp
@@ -2,12 +2,77 @@
 
 namespace MC::GFX {
 
-const std::vector<U32>& Mesh::indices() const {
-    return m_indices;
+USize Mesh::size() const {
+    return m_indices.size();
 }
 
-const std::vector<Mesh::Attribute>& Mesh::attributes() const {
-    return m_attributes;
+void Mesh::bind() {
+    if (!m_vao.has_value()) upload();
+
+    glBindVertexArray(m_vao.value());
+    for (Int i = 0; i < m_attributes.size(); i++) {
+        glEnableVertexAttribArray(i);
+    }
+}
+
+void Mesh::unbind() const {
+    glBindVertexArray(0);
+    for (Int i = 0; i < m_attributes.size(); i++) {
+        glDisableVertexAttribArray(i);
+    }
+}
+
+void Mesh::upload() {
+    auto vao = create_vao();
+    if (!m_indices.empty()) {
+        store_indices(m_indices.data(), m_indices.size());
+    }
+
+    Int attribute_index = 0;
+    for (const auto& attribute : m_attributes) {
+        store_in_attribute_list(
+            attribute_index++,
+            attribute.attribute_size,
+            attribute.type_size,
+            attribute.data,
+            attribute.data_size
+        );
+    }
+    unbind_vao();
+
+    m_vao = {vao};
+}
+
+GLuint Mesh::create_vao() {
+    GLuint vao;
+    glGenVertexArrays(1, &vao);
+    glBindVertexArray(vao);
+
+    return vao;
 }
 
-}
\ No newline at end of file
+void Mesh::unbind_vao() {
+    glBindVertexArray(0);
+}
+
+void Mesh::store_indices(const U32* indices, USize indices_size) {
+    GLuint ebo;
+    glGenBuffers(1, &ebo);
+
+    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
+    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices_size * sizeof(U32), indices, GL_STATIC_DRAW);
+}
+
+void Mesh::store_in_attribute_list(U32 attribute, Int attribute_size, Int type_size, const void* data, long data_size) {
+    assert(type_size == sizeof(F32));
+
+    GLuint vbo;
+    glGenBuffers(1, &vbo);
+
+    glBindBuffer(GL_ARRAY_BUFFER, vbo);
+    glBufferData(GL_ARRAY_BUFFER, data_size * attribute_size * type_size, data, GL_STATIC_DRAW);
+    glVertexAttribPointer(attribute, attribute_size, GL_FLOAT, GL_FALSE, attribute_size * type_size, nullptr);
+    glBindBuffer(GL_ARRAY_BUFFER, 0);
+}
+
+}
diff --git a/src/GFX/Mesh.hpp b/src/GFX/Mesh.hpp
index 69625ff..435afe1 100644
--- a/src/GFX/Mesh.hpp
+++ b/src/GFX/Mesh.hpp
@@ -3,6 +3,8 @@
 #include <utility>
 #include <vector>
 #include <algorithm>
+#include <optional>
+#include <GL/glew.h>
 #include "../Common/Sizes.hpp"
 #include "../Math/Common.hpp"
 
@@ -58,12 +60,24 @@ public:
         std::vector<Attribute> attributes
     ) : m_attributes(std::move(attributes)) {}
 
-    const std::vector<U32>& indices() const;
-    const std::vector<Attribute>& attributes() const;
+    USize size() const;
+
+    void bind();
+    void unbind() const;
 
 private:
+    void upload();
+
+    static GLuint create_vao();
+    static void unbind_vao();
+
+    static void store_in_attribute_list(U32 attribute, Int attribute_size, Int type_size, const void* data, long data_size);
+    static void store_indices(const U32* indices, USize indices_size);
+
     std::vector<Attribute> m_attributes;
     std::vector<U32> m_indices;
+
+    std::optional<GLuint> m_vao;
 };
 
 }
\ No newline at end of file