diff options
Diffstat (limited to 'src/GFX/Binder.cpp')
| -rw-r--r-- | src/GFX/Binder.cpp | 47 |
1 files changed, 31 insertions, 16 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 { |
