diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/GFX/Binder.cpp | 47 | ||||
| -rw-r--r-- | src/GFX/Binder.hpp | 16 | ||||
| -rw-r--r-- | src/GFX/Mesh.cpp | 32 | ||||
| -rw-r--r-- | src/GFX/Mesh.hpp | 49 | ||||
| -rw-r--r-- | src/GFX/Shading/Program.cpp | 2 | ||||
| -rw-r--r-- | src/GFX/Shading/Program.hpp | 2 | ||||
| -rw-r--r-- | src/GFX/Shading/Shader.cpp | 14 | ||||
| -rw-r--r-- | src/GFX/Shading/Shader.hpp | 13 | ||||
| -rw-r--r-- | src/World/Chunk.cpp | 5 | ||||
| -rw-r--r-- | src/main.cpp | 4 |
10 files changed, 99 insertions, 85 deletions
diff --git a/src/GFX/Binder.cpp b/src/GFX/Binder.cpp index 70d045c..0cc844d 100644 --- a/src/GFX/Binder.cpp +++ b/src/GFX/Binder.cpp @@ -6,15 +6,23 @@ namespace MC::GFX { BindableMesh Binder::load(Mesh& mesh) { auto vao = create_vao(); - if (mesh.indices_size() > 0) { - store_indices(mesh.raw_indices(), mesh.indices_size()); + 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 + ); } - store_in_attribute_list(0, 3, mesh.raw(), mesh.size() * 3); - store_in_attribute_list(1, 3, mesh.raw_normals(), mesh.normals_size() * 3); - store_in_attribute_list(2, 2, mesh.raw_tex_coords(), mesh.tex_coords_size() * 2); unbind_vao(); - return {vao, mesh.indices_size()}; + return {vao, mesh.indices().size(), mesh.attributes().size()}; } uint32_t Binder::create_vao() { @@ -37,28 +45,35 @@ void Binder::store_indices(uint32_t* indices, size_t indices_size) { 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) { + +void Binder::store_in_attribute_list(uint32_t attribute, int attribute_size, int type_size, void* data, long data_size) { + assert(type_size == sizeof(float)); + 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, size * sizeof(float), nullptr); + 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); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); + for (int i = 0; i < m_attribute_count; i++) { + glEnableVertexAttribArray(i); + } } -void BindableMesh::unbind() { +void BindableMesh::unbind() const { glBindVertexArray(0); - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); + for (int i = 0; i < m_attribute_count; i++) { + glDisableVertexAttribArray(i); + } +} + +bool BindableMesh::has_indices() const { + return m_has_indices; } size_t BindableMesh::size() const { diff --git a/src/GFX/Binder.hpp b/src/GFX/Binder.hpp index 99f9791..3d3949b 100644 --- a/src/GFX/Binder.hpp +++ b/src/GFX/Binder.hpp @@ -8,15 +8,25 @@ namespace MC::GFX { class BindableMesh { public: void bind() const; - void unbind(); + void unbind() const; + bool has_indices() const; size_t size() const; private: - BindableMesh(uint32_t vao, size_t vertex_count) : m_vao(vao), m_vertex_count(vertex_count) {}; + BindableMesh( + uint32_t vao, + size_t vertex_count, + size_t attribute_count + ) : m_vao(vao), + m_vertex_count(vertex_count), + m_has_indices(vertex_count > 0), + m_attribute_count(attribute_count) {}; uint32_t m_vao; size_t m_vertex_count; + bool m_has_indices; + size_t m_attribute_count; friend class Binder; }; @@ -31,7 +41,7 @@ private: static uint32_t create_vao(); static void unbind_vao(); - static void store_in_attribute_list(uint32_t attribute, size_t size, float* data, size_t data_size); + static void store_in_attribute_list(uint32_t attribute, int attribute_size, int type_size, void* data, long data_size); static void store_indices(uint32_t* indices, size_t indices_size); }; diff --git a/src/GFX/Mesh.cpp b/src/GFX/Mesh.cpp index 1bac9f1..b0271bb 100644 --- a/src/GFX/Mesh.cpp +++ b/src/GFX/Mesh.cpp @@ -2,36 +2,12 @@ namespace MC::GFX { -float* Mesh::raw() { - return (float*) m_positions.data(); +std::vector<uint32_t> Mesh::indices() { + return m_indices; } -size_t Mesh::size() { - return m_positions.size(); -} - -float* Mesh::raw_normals() { - return (float*) m_normals.data(); -} - -size_t Mesh::normals_size() { - return m_normals.size(); -} - -uint32_t* Mesh::raw_indices() { - return m_indices.data(); -} - -size_t Mesh::indices_size() { - return m_indices.size(); -} - -float* Mesh::raw_tex_coords() { - return (float*) m_tex_coords.data(); -} - -size_t Mesh::tex_coords_size() { - return m_tex_coords.size(); +std::vector<Mesh::Attribute> Mesh::attributes() { + return m_attributes; } } \ No newline at end of file diff --git a/src/GFX/Mesh.hpp b/src/GFX/Mesh.hpp index c20b419..9d640b6 100644 --- a/src/GFX/Mesh.hpp +++ b/src/GFX/Mesh.hpp @@ -9,43 +9,38 @@ namespace MC::GFX { class Mesh { public: + struct Attribute { + template<size_t S = 3, typename T = float> + Attribute( + std::vector<Vector<S, T>> data + ) : data((T*) data.data()), + data_size(data.size()), + attribute_size(S), + type_size(sizeof(T)) {}; + + void* data; + long data_size; + int attribute_size; + int type_size; + }; + Mesh( - std::vector<Vector<3>> positions, - std::vector<Vector<3>> normals, - std::vector<Vector<2>> tex_coords, + std::vector<Attribute> attributes, std::vector<uint32_t> indices - ) : m_positions(std::move(positions)), - m_normals(std::move(normals)), - m_tex_coords(std::move(tex_coords)), + ) : m_attributes(std::move(attributes)), m_indices(std::move(indices)) {}; Mesh( - std::vector<Vector<3>> positions, - std::vector<Vector<3>> normals, - std::vector<Vector<2>> tex_coords - ) : m_positions(std::move(positions)), - m_normals(std::move(normals)), - m_tex_coords(std::move(tex_coords)), + std::vector<Attribute> attributes + ) : m_attributes(std::move(attributes)), m_indices() {}; - float* raw(); - size_t size(); - - float* raw_normals(); - size_t normals_size(); - - uint32_t* raw_indices(); - size_t indices_size(); - - float* raw_tex_coords(); - size_t tex_coords_size(); + std::vector<uint32_t> indices(); + std::vector<Attribute> attributes(); private: - std::vector<Vector<3>> m_positions; - std::vector<Vector<3>> m_normals; - std::vector<Vector<2>> m_tex_coords; + std::vector<Attribute> m_attributes; std::vector<uint32_t> m_indices; - }; } \ No newline at end of file diff --git a/src/GFX/Shading/Program.cpp b/src/GFX/Shading/Program.cpp index 39393f8..2b675f8 100644 --- a/src/GFX/Shading/Program.cpp +++ b/src/GFX/Shading/Program.cpp @@ -4,7 +4,7 @@ namespace MC::GFX::Shading { -Program::Program(Shader fragment, Shader vertex) { +Program::Program(Shader vertex, Shader fragment) { m_program = glCreateProgram(); glAttachShader(m_program, fragment.get()); diff --git a/src/GFX/Shading/Program.hpp b/src/GFX/Shading/Program.hpp index 15c9899..b04dfff 100644 --- a/src/GFX/Shading/Program.hpp +++ b/src/GFX/Shading/Program.hpp @@ -10,7 +10,7 @@ namespace MC::GFX::Shading { class Program { public: - Program(Shader fragment, Shader vertex); + Program(Shader vertex, Shader fragment); uint32_t get() const; diff --git a/src/GFX/Shading/Shader.cpp b/src/GFX/Shading/Shader.cpp index ff954a5..f1502c2 100644 --- a/src/GFX/Shading/Shader.cpp +++ b/src/GFX/Shading/Shader.cpp @@ -4,8 +4,18 @@ namespace MC::GFX::Shading { -Shader::Shader(uint32_t type, const char* source) { - m_shader = glCreateShader(type); +Shader::Shader(Shader::Type type, const char* source) { + uint32_t gl_type; + switch (type) { + case Type::Vertex: + gl_type = GL_VERTEX_SHADER; + break; + case Type::Fragment: + gl_type = GL_FRAGMENT_SHADER; + break; + } + + m_shader = glCreateShader(gl_type); glShaderSource(m_shader, 1, &source, nullptr); glCompileShader(m_shader); diff --git a/src/GFX/Shading/Shader.hpp b/src/GFX/Shading/Shader.hpp index 4a3d9cf..15450cb 100644 --- a/src/GFX/Shading/Shader.hpp +++ b/src/GFX/Shading/Shader.hpp @@ -8,21 +8,26 @@ namespace MC::GFX::Shading { class Shader { public: + enum class Type { + Vertex, + Fragment, + }; + + Shader(Type type, const char* source); + uint32_t get() const { return m_shader; } static Shader create_vertex() { - return {GL_VERTEX_SHADER, Assets::Shaders::vertex}; + return {Type::Vertex, Assets::Shaders::vertex}; } static Shader create_fragment() { - return {GL_FRAGMENT_SHADER, Assets::Shaders::fragment}; + return {Type::Fragment, Assets::Shaders::fragment}; } private: - Shader(uint32_t type, const char* source); - uint32_t m_shader; }; diff --git a/src/World/Chunk.cpp b/src/World/Chunk.cpp index ee408ae..cd6d4f6 100644 --- a/src/World/Chunk.cpp +++ b/src/World/Chunk.cpp @@ -45,7 +45,10 @@ GFX::Mesh Chunk::mesh() { } } - return {positions, normals, tex_coords, indices}; + return { + {positions, normals, tex_coords}, + indices, + }; } Vector<3> Chunk::position() { diff --git a/src/main.cpp b/src/main.cpp index 40034e2..eafba6e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -58,8 +58,8 @@ void run() { camera.set_position({0.0f, 50.0f, 0.0f}); MC::GFX::Shading::Program program( - MC::GFX::Shading::Shader::create_fragment(), - MC::GFX::Shading::Shader::create_vertex() + MC::GFX::Shading::Shader::create_vertex(), + MC::GFX::Shading::Shader::create_fragment() ); auto model_uniform = program.uniform("model_matrix"); |
