summary refs log tree commit diff
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-10-03 07:21:44 +0200
committerMel <einebeere@gmail.com>2022-10-03 07:21:44 +0200
commit75f3941579c756655fc7d4d29e7b92b6eae436b7 (patch)
treef8fab6809cd9525d060cfa62269f00d7ff3ea793
parent3b289a2f75b6e96735519a65d93b6babd1b1759f (diff)
downloadmeowcraft-75f3941579c756655fc7d4d29e7b92b6eae436b7.tar.zst
meowcraft-75f3941579c756655fc7d4d29e7b92b6eae436b7.zip
Indexed Rendering
-rw-r--r--src/Binder.cpp13
-rw-r--r--src/Binder.hpp1
-rw-r--r--src/Mesh.cpp12
-rw-r--r--src/Mesh.hpp13
-rw-r--r--src/main.cpp17
5 files changed, 39 insertions, 17 deletions
diff --git a/src/Binder.cpp b/src/Binder.cpp
index 8f9b7df..409c4ef 100644
--- a/src/Binder.cpp
+++ b/src/Binder.cpp
@@ -6,10 +6,11 @@ namespace MC {
 
 BindableMesh Binder::load(Mesh& mesh) {
     auto vao = create_vao();
-    store_in_attribute_list(0, 3, mesh.flat(), mesh.size() * 3);
+    store_indices(mesh.raw_indices(), mesh.indices_size());
+    store_in_attribute_list(0, 3, mesh.raw(), mesh.size() * 3);
     unbind_vao();
 
-    return {vao, mesh.size()};
+    return {vao, mesh.indices_size()};
 }
 
 uint32_t Binder::create_vao() {
@@ -24,6 +25,14 @@ uint32_t Binder::unbind_vao() {
     glBindVertexArray(0);
 }
 
+void Binder::store_indices(uint32_t* indices, size_t indices_size) {
+    GLuint ebo;
+    glGenBuffers(1, &ebo);
+
+    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
+    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices_size * sizeof(float), indices, GL_STATIC_DRAW);
+}
+
 void Binder::store_in_attribute_list(uint32_t attribute, size_t size, float* data, size_t data_size) {
     GLuint vbo;
     glGenBuffers(1, &vbo);
diff --git a/src/Binder.hpp b/src/Binder.hpp
index 721ec77..9108a65 100644
--- a/src/Binder.hpp
+++ b/src/Binder.hpp
@@ -32,6 +32,7 @@ private:
     static uint32_t unbind_vao();
 
     static void store_in_attribute_list(uint32_t attribute, size_t size, float* data, size_t data_size);
+    static void store_indices(uint32_t* indices, size_t indices_size);
 };
 
 }
\ No newline at end of file
diff --git a/src/Mesh.cpp b/src/Mesh.cpp
index b8c3315..d1f3a88 100644
--- a/src/Mesh.cpp
+++ b/src/Mesh.cpp
@@ -1,9 +1,17 @@
 #include "Mesh.hpp"
 
-float* MC::Mesh::flat() {
+float* MC::Mesh::raw() {
     return (float*)m_positions.data();
 }
 
-std::size_t MC::Mesh::size() {
+size_t MC::Mesh::size() {
     return m_positions.size();
 }
+
+uint32_t* MC::Mesh::raw_indices() {
+    return m_indices.data();
+}
+
+size_t MC::Mesh::indices_size() {
+    return m_indices.size();
+}
diff --git a/src/Mesh.hpp b/src/Mesh.hpp
index a6d80d3..a9dadb7 100644
--- a/src/Mesh.hpp
+++ b/src/Mesh.hpp
@@ -9,12 +9,19 @@ namespace MC {
 
 class Mesh {
 public:
-    Mesh(std::vector<Vector<3>> positions) : m_positions(std::move(positions)) {};
+    Mesh(std::vector<Vector<3>> positions, std::vector<uint32_t> indices)
+        : m_positions(std::move(positions)), m_indices(std::move(indices)) {};
+
+    float* raw();
+    size_t size();
+
+    uint32_t* raw_indices();
+    size_t indices_size();
 
-    std::size_t size();
-    float* flat();
 private:
     std::vector<Vector<3>> m_positions;
+    std::vector<uint32_t> m_indices;
+
 };
 
 }
\ No newline at end of file
diff --git a/src/main.cpp b/src/main.cpp
index 3fc1711..0893849 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -38,15 +38,12 @@ void run() {
 
     glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
 
-    MC::Mesh quad({
-        {-0.5f, -0.5f, 0.0f},
-        {0.5f, -0.5f, 0.0f},
-        {-0.5f, 0.5f, 0.0f},
+    MC::Mesh shape({
+        {-0.5f, -0.5f, 0.0f}, {0.5f, -0.5f, 0.0f},
+        {-0.5f, 0.5f, 0.0f},  {0.5f, 0.5f, 0.0f},
+    }, {0, 1, 2, 3, 2, 1});
 
-        {0.5f, 0.5f, 0.0f},
-        {-0.5f, 0.5f, 0.0f},
-        {0.5f, -0.5f, 0.0f},
-    });
+    auto mesh = MC::Binder::load(shape);
 
     auto mesh = MC::Binder::load(quad);
 
@@ -69,11 +66,11 @@ void run() {
 }
 
 void render(MC::BindableMesh& mesh) {
-    glClearColor(0.65f, 0.8f, 0.8f, 1.0f);
+    glClearColor(0.85f, 0.85f, 0.85f, 1.0f); // #DBDBDB
     glClear(GL_COLOR_BUFFER_BIT);
 
     mesh.bind();
-    glDrawArrays(GL_TRIANGLES, 0, mesh.size());
+    glDrawElements(GL_TRIANGLES, mesh.size(), GL_UNSIGNED_INT, 0);
     mesh.unbind();
 }