aboutsummaryrefslogtreecommitdiff
path: root/src/rendering/vertex_buffer.h
blob: 6be7e347d3b34a8e50c6f0d304630515a3114b09 (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
#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:
        virtual ~VertexBuffer() = default;

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

        virtual auto get_layout() const -> const VertexBufferLayout& = 0;
        virtual auto set_layout(const VertexBufferLayout& layout) -> void = 0;

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