aboutsummaryrefslogtreecommitdiff
path: root/src/rendering/vertex_buffer.h
blob: 4e61700ca0634a9e48e9378a1ee2522488773347 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#pragma once

#include <cstdint>
#include <vector>

namespace Donut
{
    struct VertexBufferElement
    {
        uint32_t type;
        uint32_t count;
        uint8_t normalized;
        uint32_t offset;

        static auto get_size_of_type(uint32_t type) -> uint32_t;
    };

    class VertexBufferLayout
    {
    public:
        VertexBufferLayout() = default;
        ~VertexBufferLayout() = default;

        template<typename T>
        auto push(uint32_t count) -> void
        {
            static_assert(false);
        }

        template<>
        void push<float>(uint32_t count)
        {
            m_elements.push_back({ 0x1406, count, 0, m_stride }); // GL_FLOAT
            m_stride += count * VertexBufferElement::get_size_of_type(0x1406);
        }

        template<>
        void push<uint32_t>(uint32_t count)
        {
            m_elements.push_back({ 0x1405, count, 0, m_stride }); // GL_UNSIGNED_INT
            m_stride += count * VertexBufferElement::get_size_of_type(0x1405);
        }

        template<>
        void push<uint8_t>(uint32_t count)
        {
            m_elements.push_back({ 0x1401, count, 1, m_stride }); // GL_UNSIGNED_BYTE
            m_stride += count * VertexBufferElement::get_size_of_type(0x1401);
        }

        inline auto get_elements() const -> const std::vector<VertexBufferElement>& { return m_elements; }
        inline auto get_stride() const -> uint32_t { return m_stride; }

    private:
        std::vector<VertexBufferElement> m_elements;
        uint32_t m_stride = 0;
    };

    class VertexBuffer
    {
    public:
        VertexBuffer(const void* data, uint32_t size);
        ~VertexBuffer();

        auto bind() const -> void;
        auto unbind() const -> void;
        auto set_data(const void* data, uint32_t size) -> void;

        auto get_layout() const -> const VertexBufferLayout& { return m_layout; }
        auto set_layout(const VertexBufferLayout& layout) -> void { m_layout = layout; }

        static auto create(const void* data, uint32_t size) -> VertexBuffer*;

    private:
        uint32_t           m_renderer_id = 0;
        VertexBufferLayout m_layout;
    };
};