summary refs log tree commit diff
path: root/src/GFX/Mesh.hpp
blob: 36a7dac4da7f117a7708d1e3b9307b469ad61fdc (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
#pragma once

#include <utility>
#include <vector>
#include <cstdint>
#include "../Math/Math.hpp"

namespace MC::GFX {

class Mesh {
public:
    struct Attribute {
        template<size_t S = 3, typename T = float>
        Attribute(
            std::vector<Vector<S, T>> v
        ) : data_size(v.size()),
            attribute_size(S),
            type_size(sizeof(T)) {
            data = copy(v.data(), v.size() * S * type_size);
        };

        Attribute(
            const Attribute& other
        ) : data(copy(other.data, other.data_size * other.attribute_size * other.type_size)),
            data_size(other.data_size),
            attribute_size(other.attribute_size),
            type_size(other.type_size) {};

        static void* copy(void* ptr, uint32_t size) {
            auto* buffer = new uint8_t[size];
            std::copy((uint8_t*)ptr, (uint8_t*)ptr + size, buffer);
            return buffer;
        }

        void* data;
        long data_size;
        int attribute_size;
        int type_size;
    };

    Mesh(
        std::vector<Attribute> attributes,
        std::vector<uint32_t> indices
    ) : m_attributes(std::move(attributes)),
        m_indices(std::move(indices)) {};

    Mesh(
        std::vector<Attribute> attributes
    ) : m_attributes(std::move(attributes)),
        m_indices() {};

    std::vector<uint32_t> indices();
    std::vector<Attribute> attributes();

private:
    std::vector<Attribute> m_attributes;
    std::vector<uint32_t> m_indices;
};

}