/
CompactWindingVertexBuffer.h
126 lines (98 loc) · 4 KB
/
CompactWindingVertexBuffer.h
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#pragma once
#include <vector>
#include <stdexcept>
namespace render
{
template<typename VertexT>
class CompactWindingVertexBuffer
{
private:
std::size_t _size;
std::size_t _numIndicesPerWinding;
std::vector<VertexT> _vertices;
// The indices suitable for rendering triangles
std::vector<unsigned int> _indices;
public:
using Slot = std::uint32_t;
explicit CompactWindingVertexBuffer(std::size_t size) :
_size(size),
_numIndicesPerWinding(3 * (_size - 2))
{}
CompactWindingVertexBuffer(const CompactWindingVertexBuffer& other) = delete;
CompactWindingVertexBuffer& operator=(const CompactWindingVertexBuffer& other) = delete;
// Move ctor
CompactWindingVertexBuffer(CompactWindingVertexBuffer&& other) noexcept :
_size(other._size),
_numIndicesPerWinding(other._numIndicesPerWinding),
_vertices(std::move(other._vertices)),
_indices(std::move(other._indices))
{}
std::size_t getWindingSize() const
{
return _size;
}
std::size_t getNumIndicesPerWinding() const
{
return _numIndicesPerWinding;
}
const std::vector<VertexT>& getVertices() const
{
return _vertices;
}
const std::vector<unsigned int>& getIndices() const
{
return _indices;
}
// Appends the given winding data to the end of the buffer, returns the position in the array
Slot pushWinding(const std::vector<VertexT>& winding)
{
assert(winding.size() == _size);
const auto currentSize = _vertices.size();
auto position = currentSize / _size;
_vertices.reserve(currentSize + _size); // reserve() never shrinks
std::copy(winding.begin(), winding.end(), std::back_inserter(_vertices));
// Allocate and calculate indices
_indices.reserve(_indices.size() + _numIndicesPerWinding);
for (unsigned int n = static_cast<unsigned int>(_size) - 1; n - 1 > 0; --n)
{
_indices.push_back(static_cast<unsigned int>(currentSize) + 0);
_indices.push_back(static_cast<unsigned int>(currentSize) + n);
_indices.push_back(static_cast<unsigned int>(currentSize) + n - 1);
}
return static_cast<Slot>(position);
}
// Replaces the winding in the given slot with the given data
void replaceWinding(Slot slot, const std::vector<VertexT>& winding)
{
assert(winding.size() == _size);
// Copy the incoming data to the target slot
std::copy(winding.begin(), winding.end(), _vertices.begin() + (slot * _size));
// Indices remain unchanged
}
// Removes the winding from the given slot. All slots greater than the given one
// will be shifted towards the left, their values are shifted by -1
// Invalid slot indices will result in a std::logic_error
void removeWinding(Slot slot)
{
const auto currentSize = _vertices.size();
if (slot >= currentSize / _size) throw std::logic_error("Slot index out of bounds");
// Remove _size elements at the given position
auto firstVertexToRemove = _vertices.begin() + (slot * _size);
_vertices.erase(firstVertexToRemove, firstVertexToRemove + _size);
// Since all the windings have the same structure, the index array will always look the same
// after shifting the index values of the remaining windings. So just cut off the last one
#if 0
// Shift all indices after this slot towards the left, applying the -size offset
auto firstIndexToAdjust = (slot + 1) * _numIndicesPerWinding;
auto lastIndexToAdjust = _indices.size() - 1;
auto indexToReassign = slot * _numIndicesPerWinding;
for (auto i = firstIndexToAdjust; i < lastIndexToAdjust; ++i, ++indexToReassign)
{
_indices[indexToReassign] = _indices[i] - static_cast<unsigned int>(_size);
}
#endif
// Cut off one winding from the end of the index array
_indices.resize(_indices.size() - _numIndicesPerWinding);
}
};
}