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
79
80
81
82
83
|
#pragma once
#include <utility>
#include <vector>
#include <algorithm>
#include <optional>
#include <GL/glew.h>
#include "../Common/Sizes.hpp"
#include "../Math/Common.hpp"
namespace MC::GFX {
class Mesh {
public:
struct Attribute {
template<typename T = F32>
Attribute(
std::vector<T> v
) : data_size(v.size()),
attribute_size(1),
type_size(sizeof(T)) {
data = copy(v.data(), v.size() * type_size);
}
template<uint S = 3, typename T = F32>
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, U32 size) {
auto* buffer = new U8[size];
std::copy_n((U8*)ptr, size, buffer);
return buffer;
}
void* data;
USize data_size;
USize attribute_size;
USize type_size;
};
Mesh(
std::vector<Attribute> attributes,
std::vector<U32> indices
) : m_attributes(std::move(attributes)),
m_indices(std::move(indices)) {}
explicit Mesh(
std::vector<Attribute> attributes
) : m_attributes(std::move(attributes)) {}
USize size() const;
void bind();
void unbind() const;
private:
void upload();
static GLuint create_vao();
static void unbind_vao();
static void store_in_attribute_list(U32 attribute, Int attribute_size, Int type_size, const void* data, long data_size);
static void store_indices(const U32* indices, USize indices_size);
std::vector<Attribute> m_attributes;
std::vector<U32> m_indices;
std::optional<GLuint> m_vao;
};
}
|