/
SurfaceRenderer.h
193 lines (152 loc) · 5.91 KB
/
SurfaceRenderer.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#pragma once
#include "isurfacerenderer.h"
namespace render
{
class SurfaceRenderer :
public ISurfaceRenderer
{
private:
struct VertexBuffer
{
std::vector<ArbitraryMeshVertex> vertices;
std::vector<unsigned int> indices;
};
VertexBuffer _triangleBuffer;
VertexBuffer _quadBuffer;
static constexpr std::size_t InvalidVertexIndex = std::numeric_limits<std::size_t>::max();
struct SlotInfo
{
std::uint8_t bucketIndex;
std::size_t firstVertex;
std::size_t numVertices;
std::size_t firstIndex;
std::size_t numIndices;
};
std::vector<SlotInfo> _slots;
static constexpr std::size_t InvalidSlotMapping = std::numeric_limits<std::size_t>::max();
std::size_t _freeSlotMappingHint;
public:
SurfaceRenderer() :
_freeSlotMappingHint(InvalidSlotMapping)
{}
bool empty() const
{
return _triangleBuffer.vertices.empty() && _quadBuffer.vertices.empty();
}
Slot addSurface(SurfaceIndexingType indexType, const std::vector<ArbitraryMeshVertex>& vertices,
const std::vector<unsigned int>& indices) override
{
auto bucketIndex = GetBucketIndexForIndexType(indexType);
auto& bucket = getBucketByIndex(bucketIndex);
// Allocate a slot
auto oldVertexSize = bucket.vertices.size();
auto oldIndexSize = bucket.indices.size();
auto newSlotIndex = getNextFreeSlotMapping();
auto& slot = _slots.at(newSlotIndex);
slot.bucketIndex = bucketIndex;
slot.firstVertex = oldVertexSize;
slot.numVertices = vertices.size();
slot.firstIndex = oldIndexSize;
slot.numIndices = indices.size();
bucket.vertices.reserve(oldVertexSize + vertices.size()); // reserve() never shrinks
std::copy(vertices.begin(), vertices.end(), std::back_inserter(bucket.vertices));
// Allocate, copy and offset indices
bucket.indices.reserve(oldIndexSize + indices.size());
for (auto index : indices)
{
bucket.indices.push_back(index + static_cast<unsigned int>(oldVertexSize));
}
return newSlotIndex;
}
void removeSurface(Slot slot) override
{
auto& slotInfo = _slots.at(slot);
auto& bucket = getBucketByIndex(slotInfo.bucketIndex);
// Cut out the vertices
auto firstVertexToRemove = bucket.vertices.begin() + slotInfo.firstVertex;
bucket.vertices.erase(firstVertexToRemove, firstVertexToRemove + slotInfo.numVertices);
// Shift all indices to the left, offsetting their values by the number of removed vertices
auto offsetToApply = -static_cast<int>(slotInfo.numVertices);
auto targetIndex = bucket.indices.begin() + slotInfo.firstIndex;
auto indexToMove = targetIndex + slotInfo.numIndices;
auto indexEnd = bucket.indices.end();
while (indexToMove != indexEnd)
{
*targetIndex++ = *indexToMove++ + offsetToApply;
}
// Cut off the tail of the indices
bucket.indices.resize(bucket.indices.size() - slotInfo.numIndices);
// Adjust all offsets in other slots
for (auto& slot : _slots)
{
if (slot.firstVertex > slotInfo.firstVertex)
{
slot.firstVertex -= slotInfo.numVertices;
slot.firstIndex -= slotInfo.numIndices;
}
}
// Invalidate the slot
slotInfo.numVertices = 0;
slotInfo.firstVertex = InvalidVertexIndex;
slotInfo.firstIndex = 0;
slotInfo.numIndices = 0;
if (slot < _freeSlotMappingHint)
{
_freeSlotMappingHint = slot;
}
}
void updateSurface(Slot slot, const std::vector<ArbitraryMeshVertex>& vertices,
const std::vector<unsigned int>& indices) override
{
auto& slotInfo = _slots.at(slot);
auto& bucket = getBucketByIndex(slotInfo.bucketIndex);
// Copy the data to the correct slot in the array
std::copy(vertices.begin(), vertices.end(), bucket.vertices.begin() + slotInfo.firstVertex);
// Before assignment, the indices need to be shifted to match the array offset of the vertices
auto targetIndex = bucket.indices.begin() + slotInfo.firstIndex;
auto indexShift = static_cast<unsigned int>(slotInfo.firstVertex);
for (auto index : indices)
{
*targetIndex++ = index + indexShift;
}
}
void render()
{
renderBuffer(_triangleBuffer, GL_TRIANGLES);
renderBuffer(_quadBuffer, GL_QUADS);
}
private:
void renderBuffer(const VertexBuffer& buffer, GLenum mode)
{
if (!buffer.indices.empty())
{
glVertexPointer(3, GL_DOUBLE, sizeof(ArbitraryMeshVertex), &buffer.vertices.front().vertex);
glTexCoordPointer(2, GL_DOUBLE, sizeof(ArbitraryMeshVertex), &buffer.vertices.front().texcoord);
glNormalPointer(GL_DOUBLE, sizeof(ArbitraryMeshVertex), &buffer.vertices.front().normal);
glDrawElements(mode, static_cast<GLsizei>(buffer.indices.size()), GL_UNSIGNED_INT, &buffer.indices.front());
}
}
constexpr static std::uint8_t GetBucketIndexForIndexType(SurfaceIndexingType indexType)
{
return indexType == SurfaceIndexingType::Triangles ? 0 : 1;
}
VertexBuffer& getBucketByIndex(std::uint8_t bucketIndex)
{
return bucketIndex == 0 ? _triangleBuffer : _quadBuffer;
}
ISurfaceRenderer::Slot getNextFreeSlotMapping()
{
auto numSlots = _slots.size();
for (auto i = _freeSlotMappingHint; i < numSlots; ++i)
{
if (_slots[i].firstVertex == InvalidVertexIndex)
{
_freeSlotMappingHint = i + 1; // start searching here next time
return i;
}
}
_slots.emplace_back();
return numSlots; // == the size before we emplaced the new slot
}
};
}