From 45baf48db974c66cfdc51f26cee191abbbd89a0b Mon Sep 17 00:00:00 2001 From: hachem Date: Thu, 20 Aug 2026 22:20:49 +0200 Subject: [feat]: reduce renderer abstraction --- src/rendering/vertex_array.cpp | 67 ++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 19 deletions(-) (limited to 'src/rendering/vertex_array.cpp') 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 -#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& 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(static_cast(element.offset))); + m_vertex_buffer_index++; } + + m_vertex_buffers.push_back(vertex_buffer); + } + + auto VertexArray::set_index_buffer(const Ref& 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(); } }; -- cgit v1.3