aboutsummaryrefslogtreecommitdiff
path: root/src/rendering/vertex_array.cpp
blob: cddcf352bd843fe08db1757a9819432d17883d37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <glad/glad.h>

#include "vertex_array.h"

namespace Donut
{
    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
    {
        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())
        {
            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*
    {
        return new VertexArray();
    }
};