-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBuffer.inl
executable file
·91 lines (84 loc) · 1.53 KB
/
Buffer.inl
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
84
85
86
87
88
89
90
91
#include <utility>
template <class T>
inline Buffer<T>::Buffer(
VertexArray *vao,
GLenum target,
GLenum usage,
unsigned int itemSize,
unsigned int numItems) :
VAO(vao),
Target(target),
Usage(usage),
ItemSize(itemSize),
NumItems(numItems),
Data(nullptr)
{
glGenBuffers(1, &Name);
}
template <class T>
inline Buffer<T>::~Buffer()
{
glDeleteBuffers(1, &Name);
if (managed_data)
delete[] Data;
}
template <class T>
inline Buffer<T>::Buffer(Buffer<T>&& other) :
Name(0), Data(nullptr)
{
*this = std::move(other);
}
template <class T>
inline Buffer<T>& Buffer<T>::operator=(Buffer<T>&& other)
{
if (this != &other)
{
glDeleteBuffers(1, &Name);
if (managed_data)
delete[] Data;
Name = other.Name;
VAO = other.VAO;
Target = other.Target;
Usage = other.Usage;
ItemSize = other.ItemSize;
NumItems = other.NumItems;
Data = other.Data;
other.Name = 0;
other.VAO = nullptr;
other.Target = 0;
other.Usage = 0;
other.ItemSize = 0;
other.NumItems = 0;
other.Data = nullptr;
}
return *this;
}
template <class T>
inline void Buffer<T>::SetData(T *data, bool copy)
{
if (managed_data)
{
managed_data = false;
delete[] Data;
Data = nullptr;
}
if (copy && data != nullptr)
{
Data = new T[NumItems * ItemSize];
memcpy(Data, data, sizeof(T) * ItemSize * NumItems);
managed_data = true;
}
VAO->SetBufferData(
Name,
Target,
sizeof(T) * ItemSize * NumItems,
Data,
Usage);
}
template <class T>
inline void Buffer<T>::BindBuffer()
{
VAO->Bind([&](){
glBindBuffer(Target, Name);
});
}