/
GeometryRenderer.h
281 lines (224 loc) · 9 KB
/
GeometryRenderer.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#pragma once
#include "igeometryrenderer.h"
namespace render
{
class GeometryRenderer :
public IGeometryRenderer
{
private:
class VertexBuffer
{
private:
GLenum _mode;
std::vector<ArbitraryMeshVertex> _vertices;
std::vector<unsigned int> _indices;
public:
VertexBuffer(GLenum mode) :
_mode(mode)
{}
bool empty() const
{
return _indices.empty();
}
void render() const
{
if (_indices.empty()) return;
glVertexPointer(3, GL_DOUBLE, sizeof(ArbitraryMeshVertex), &_vertices.front().vertex);
glTexCoordPointer(2, GL_DOUBLE, sizeof(ArbitraryMeshVertex), &_vertices.front().texcoord);
glNormalPointer(GL_DOUBLE, sizeof(ArbitraryMeshVertex), &_vertices.front().normal);
glColorPointer(4, GL_DOUBLE, sizeof(ArbitraryMeshVertex), &_vertices.front().colour);
glDrawElements(_mode, static_cast<GLsizei>(_indices.size()), GL_UNSIGNED_INT, &_indices.front());
}
void renderIndexRange(std::size_t firstIndex, std::size_t numIndices) const
{
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
// Render this slot without any vertex colours
glDisableClientState(GL_COLOR_ARRAY);
glFrontFace(GL_CW);
glVertexPointer(3, GL_DOUBLE, sizeof(ArbitraryMeshVertex), &_vertices.front().vertex);
glTexCoordPointer(2, GL_DOUBLE, sizeof(ArbitraryMeshVertex), &_vertices.front().texcoord);
glNormalPointer(GL_DOUBLE, sizeof(ArbitraryMeshVertex), &_vertices.front().normal);
glDrawElements(_mode, static_cast<GLsizei>(numIndices), GL_UNSIGNED_INT, &_indices[firstIndex]);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
// Returns the vertex and index offsets in this buffer
std::pair<std::size_t, std::size_t> addSurface(const std::vector<ArbitraryMeshVertex>& vertices,
const std::vector<unsigned int>& indices)
{
auto vertexOffset = _vertices.size();
auto indexOffset = _indices.size();
std::copy(vertices.begin(), vertices.end(), std::back_inserter(_vertices));
for (auto index : indices)
{
_indices.push_back(index + static_cast<unsigned int>(vertexOffset));
}
return { vertexOffset, indexOffset };
}
void updateSurface(std::size_t firstVertex, std::size_t firstIndex,
const std::vector<ArbitraryMeshVertex>& vertices,
const std::vector<unsigned int>& indices)
{
// Copy the data to the correct slot in the array
std::copy(vertices.begin(), vertices.end(), _vertices.begin() + firstVertex);
// Before assignment, the indices need to be shifted to match the array offset of the vertices
auto targetIndex = _indices.begin() + firstIndex;
auto indexShift = static_cast<unsigned int>(firstVertex);
for (auto index : indices)
{
*targetIndex++ = index + indexShift;
}
}
// Cuts out the vertices and indices, adjusts all indices located to the right of the cut
void removeSurface(std::size_t firstVertex, std::size_t numVertices, std::size_t firstIndex, std::size_t numIndices)
{
// Cut out the vertices
auto firstVertexToRemove = _vertices.begin() + firstVertex;
_vertices.erase(firstVertexToRemove, firstVertexToRemove + numVertices);
// Shift all indices to the left, offsetting their values by the number of removed vertices
auto offsetToApply = -static_cast<int>(numVertices);
auto targetIndex = _indices.begin() + firstIndex;
auto indexToMove = targetIndex + numIndices;
auto indexEnd = _indices.end();
while (indexToMove != indexEnd)
{
*targetIndex++ = *indexToMove++ + offsetToApply;
}
// Cut off the tail of the indices
_indices.resize(_indices.size() - numIndices);
}
};
std::vector<VertexBuffer> _buffers;
static constexpr std::size_t InvalidVertexIndex = std::numeric_limits<std::size_t>::max();
// Internal information about where the chunk of indexed vertex data is located:
// Which buffer they're in, and the data offset and count within the buffer.
// This is enough information to access, replace or remove the data at a later point.
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:
GeometryRenderer() :
_freeSlotMappingHint(InvalidSlotMapping)
{
_buffers.emplace_back(GL_TRIANGLES);
_buffers.emplace_back(GL_QUADS);
_buffers.emplace_back(GL_LINES);
}
bool empty() const
{
for (const auto& buffer : _buffers)
{
if (!buffer.empty()) return false;
}
return true;
}
Slot addGeometry(GeometryType 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 newSlotIndex = getNextFreeSlotMapping();
auto& slot = _slots.at(newSlotIndex);
auto [vertexOffset, indexOffset] = bucket.addSurface(vertices, indices);
slot.bucketIndex = bucketIndex;
slot.firstVertex = vertexOffset;
slot.numVertices = vertices.size();
slot.firstIndex = indexOffset;
slot.numIndices = indices.size();
return newSlotIndex;
}
void removeGeometry(Slot slot) override
{
auto& slotInfo = _slots.at(slot);
auto& bucket = getBucketByIndex(slotInfo.bucketIndex);
bucket.removeSurface(slotInfo.firstVertex, slotInfo.numVertices, slotInfo.firstIndex, slotInfo.numIndices);
// Adjust all offsets in other slots pointing to the same bucket
for (auto& slot : _slots)
{
if (slot.bucketIndex == slotInfo.bucketIndex &&
slot.firstVertex > slotInfo.firstVertex &&
slot.firstVertex != InvalidVertexIndex)
{
assert(slot.firstVertex >= slotInfo.numVertices);
assert(slot.firstIndex >= slotInfo.numIndices);
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 updateGeometry(Slot slot, const std::vector<ArbitraryMeshVertex>& vertices,
const std::vector<unsigned int>& indices) override
{
auto& slotInfo = _slots.at(slot);
if (slotInfo.numVertices != vertices.size() ||
slotInfo.numIndices != indices.size())
{
throw std::logic_error("updateGeometry: Data size mismatch");
}
auto& bucket = getBucketByIndex(slotInfo.bucketIndex);
bucket.updateSurface(slotInfo.firstVertex, slotInfo.firstIndex, vertices, indices);
}
void render()
{
for (auto& buffer : _buffers)
{
buffer.render();
}
}
void renderGeometry(Slot slot) override
{
auto& slotInfo = _slots.at(slot);
auto& buffer = getBucketByIndex(slotInfo.bucketIndex);
buffer.renderIndexRange(slotInfo.firstIndex, slotInfo.numIndices);
}
private:
constexpr static std::uint8_t GetBucketIndexForIndexType(GeometryType indexType)
{
switch (indexType)
{
case GeometryType::Triangles: return 0;
case GeometryType::Quads: return 1;
case GeometryType::Lines: return 2;
}
return 0;
}
VertexBuffer& getBucketByIndex(std::uint8_t bucketIndex)
{
return _buffers[bucketIndex];
}
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
}
};
}