aboutsummaryrefslogtreecommitdiff
path: root/src/rendering/vertex_array.cpp
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2026-08-20 22:20:49 +0200
committerhachem <im@hachem.wtf>2026-08-20 22:20:49 +0200
commit45baf48db974c66cfdc51f26cee191abbbd89a0b (patch)
treeb60c27506f1e1c37b7238161156c83e7e159fc12 /src/rendering/vertex_array.cpp
parent00bf9a8cce28ab1dbc77ba24428be12391c4a576 (diff)
[feat]: reduce renderer abstraction
Diffstat (limited to 'src/rendering/vertex_array.cpp')
-rw-r--r--src/rendering/vertex_array.cpp67
1 files changed, 48 insertions, 19 deletions
diff --git a/src/rendering/vertex_array.cpp b/src/rendering/vertex_array.cpp
index 8045707..cddcf35 100644
--- a/src/rendering/vertex_array.cpp
+++ b/src/rendering/vertex_array.cpp
@@ -1,32 +1,61 @@
-#include "vertex_array.h"
-#include "renderer.h"
+#include <glad/glad.h>
-#include "platform/opengl/opengl_vertex_array.h"
-#include "platform/vulkan/vulkan_vertex_array.h"
+#include "vertex_array.h"
namespace Donut
{
- auto VertexBufferElement::get_size_of_type(uint32_t type) -> uint32_t
+ VertexArray::VertexArray()
+ {
+ // glCreateVertexArrays is 4.5 DSA; macOS caps at 4.1. glGenVertexArrays
+ // reserves the name and the VAO is created on first bind (done below).
+ glGenVertexArrays(1, &m_renderer_id);
+ }
+
+ VertexArray::~VertexArray()
+ {
+ glDeleteVertexArrays(1, &m_renderer_id);
+ }
+
+ auto VertexArray::bind() const -> void
+ {
+ glBindVertexArray(m_renderer_id);
+ }
+
+ auto VertexArray::unbind() const -> void
{
- switch (type)
+ glBindVertexArray(0);
+ }
+
+ auto VertexArray::add_vertex_buffer(const Ref<VertexBuffer>& vertex_buffer) -> void
+ {
+ glBindVertexArray(m_renderer_id);
+ vertex_buffer->bind();
+
+ const auto& layout = vertex_buffer->get_layout();
+ for (const auto& element : layout.get_elements())
{
- case 0x1406: return 4; // GL_FLOAT
- case 0x1405: return 4; // GL_UNSIGNED_INT
- case 0x1401: return 1; // GL_UNSIGNED_BYTE
- default: return 0;
+ glEnableVertexAttribArray(m_vertex_buffer_index);
+ glVertexAttribPointer(m_vertex_buffer_index,
+ element.count,
+ element.type,
+ element.normalized ? GL_TRUE : GL_FALSE,
+ layout.get_stride(),
+ reinterpret_cast<const void*>(static_cast<uintptr_t>(element.offset)));
+ m_vertex_buffer_index++;
}
+
+ m_vertex_buffers.push_back(vertex_buffer);
+ }
+
+ auto VertexArray::set_index_buffer(const Ref<IndexBuffer>& index_buffer) -> void
+ {
+ glBindVertexArray(m_renderer_id);
+ index_buffer->bind();
+ m_index_buffer = index_buffer;
}
auto VertexArray::create() -> VertexArray*
{
- switch (Renderer::get_api())
- {
- case RendererAPI::API::OpenGL:
- return new OpenGLVertexArray();
- case RendererAPI::API::Vulkan:
- return new VulkanVertexArray();
- default:
- return nullptr;
- }
+ return new VertexArray();
}
};