/
Renderables.h
339 lines (276 loc) · 8.27 KB
/
Renderables.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#pragma once
#include "math/AABB.h"
#include "render/RenderableGeometry.h"
#include "render/RenderableBox.h"
#include "render.h"
namespace selection
{
class RenderableCornerPoints :
public render::RenderableGeometry
{
private:
const std::vector<AABB>& _aabbs;
bool _needsUpdate;
Vector4 _colour;
public:
RenderableCornerPoints(const std::vector<AABB>& aabbs) :
_aabbs(aabbs),
_needsUpdate(true),
_colour(1, 1, 1, 1)
{}
void setColour(const Colour4b& colour)
{
_colour.x() = colour.r / 255.0;
_colour.y() = colour.g / 255.0;
_colour.z() = colour.b / 255.0;
_colour.w() = colour.a / 255.0;
}
void queueUpdate()
{
_needsUpdate = true;
}
protected:
void updateGeometry() override
{
if (!_needsUpdate) return;
_needsUpdate = false;
std::vector<MeshVertex> vertices;
std::vector<unsigned int> indices;
// 8 vertices per box
vertices.reserve(_aabbs.size() * 8);
indices.reserve(_aabbs.size() * 8);
unsigned int index = 0;
for (const auto& aabb : _aabbs)
{
// Calculate the corner vertices of this bounding box
Vector3 max(aabb.origin + aabb.extents);
Vector3 min(aabb.origin - aabb.extents);
auto boxVertices = render::detail::getWireframeBoxVertices(min, max, _colour);
for (const auto& vertex : boxVertices)
{
vertices.emplace_back(std::move(vertex));
indices.push_back(index++);
}
}
RenderableGeometry::updateGeometry(render::GeometryType::Points, vertices, indices);
}
};
namespace detail
{
inline void generateQuad(std::vector<MeshVertex>& vertices, std::vector<unsigned int>& indices,
double size, const Vector4& colour)
{
unsigned int indexOffset = static_cast<unsigned int>(vertices.size());
vertices.push_back(MeshVertex({ -size, size, 0 }, { 0,0,0 }, { 0,0 }, colour));
vertices.push_back(MeshVertex({ size, size, 0 }, { 0,0,0 }, { 0,0 }, colour));
vertices.push_back(MeshVertex({ size, -size, 0 }, { 0,0,0 }, { 0,0 }, colour));
vertices.push_back(MeshVertex({ -size, -size, 0 }, { 0,0,0 }, { 0,0 }, colour));
indices.push_back(indexOffset + 0);
indices.push_back(indexOffset + 1);
indices.push_back(indexOffset + 1);
indices.push_back(indexOffset + 2);
indices.push_back(indexOffset + 2);
indices.push_back(indexOffset + 3);
indices.push_back(indexOffset + 3);
indices.push_back(indexOffset + 0);
}
inline Vector4 toVector4(const Colour4b& colour)
{
return
{
colour.r / 255.0,
colour.g / 255.0,
colour.b / 255.0,
colour.a / 255.0,
};
}
}
// Renders a fixed size point array as line strip
class RenderableLineStrip :
public render::RenderableGeometry
{
protected:
const Matrix4& _localToWorld;
bool _needsUpdate;
Vector4 _colour;
std::vector<Vertex3f> _rawPoints;
public:
void queueUpdate()
{
_needsUpdate = true;
}
void setColour(const Colour4b& colour)
{
_colour = detail::toVector4(colour);
queueUpdate();
}
const std::vector<Vertex3f>& getRawPoints() const
{
return _rawPoints;
}
protected:
RenderableLineStrip(std::size_t numPoints, const Matrix4& localToWorld) :
_localToWorld(localToWorld),
_needsUpdate(true),
_rawPoints(numPoints)
{}
void updateGeometry() override
{
if (!_needsUpdate) return;
_needsUpdate = false;
std::vector<MeshVertex> vertices;
std::vector<unsigned int> indices;
unsigned int index = 0;
for (const auto& vertex : _rawPoints)
{
vertices.push_back(MeshVertex(_localToWorld * vertex, { 0,0,0 }, { 0,0 }, _colour));
if (index > 0)
{
indices.push_back(index - 1);
indices.push_back(index);
}
++index;
}
RenderableGeometry::updateGeometry(render::GeometryType::Lines, vertices, indices);
}
};
template<typename RemapPolicy>
class RenderableSemiCircle :
public RenderableLineStrip
{
public:
RenderableSemiCircle(std::size_t segments, double radius, const Matrix4& localToWorld) :
RenderableLineStrip((segments << 2) + 1, localToWorld)
{
draw_semicircle<RemapPolicy>(segments, radius, _rawPoints);
}
};
template<typename RemapPolicy>
class RenderableCircle :
public RenderableLineStrip
{
public:
RenderableCircle(std::size_t segments, double radius, const Matrix4& localToWorld) :
RenderableLineStrip(segments << 3, localToWorld)
{
draw_circle<RemapPolicy>(segments, radius, _rawPoints);
}
};
class RenderableArrowLine :
public RenderableLineStrip
{
public:
RenderableArrowLine(const Vector3& direction, const Matrix4& localToWorld) :
RenderableLineStrip(2, localToWorld)
{
_rawPoints[0] = Vector3(0, 0, 0);
_rawPoints[1] = direction;
}
};
class RenderableQuad :
public RenderableLineStrip
{
public:
RenderableQuad(double edgeLength, const Matrix4& localToWorld) :
RenderableLineStrip(5, localToWorld)
{
_rawPoints[0] = Vector3(edgeLength, edgeLength, 0);
_rawPoints[1] = Vector3(edgeLength, -edgeLength, 0);
_rawPoints[2] = Vector3(-edgeLength, -edgeLength, 0);
_rawPoints[3] = Vector3(-edgeLength, edgeLength, 0);
_rawPoints[4] = _rawPoints[0];
}
};
// Renders a few flat-shaded triangles as arrow head, offset by a given amount
class RenderableArrowHead :
public render::RenderableGeometry
{
protected:
Vector3 _offset;
const Vector3& _screenAxis;
double _width;
double _height;
const Matrix4& _localToWorld;
bool _needsUpdate;
Vector4 _colour;
std::vector<Vertex3f> _rawPoints;
public:
RenderableArrowHead(const Vector3& offset, const Vector3& screenAxis, double width, double height, const Matrix4& localToWorld) :
_offset(offset),
_screenAxis(screenAxis),
_width(width),
_height(height),
_localToWorld(localToWorld),
_needsUpdate(true),
_rawPoints(3)
{}
void queueUpdate()
{
_needsUpdate = true;
}
void setColour(const Colour4b& colour)
{
_colour = detail::toVector4(colour);
queueUpdate();
}
const std::vector<Vertex3f>& getRawPoints() const
{
return _rawPoints;
}
protected:
void updateGeometry() override
{
if (!_needsUpdate) return;
_needsUpdate = false;
std::vector<MeshVertex> vertices;
std::vector<unsigned int> indices;
auto direction = _offset.getNormalised();
auto sideWays = _offset.cross(_screenAxis).getNormalised();
_rawPoints[0] = _offset; // tip
_rawPoints[1] = _offset - direction * _height + sideWays * _width;
_rawPoints[2] = _offset - direction * _height - sideWays * _width;
unsigned int index = 0;
for (const auto& vertex : _rawPoints)
{
vertices.push_back(MeshVertex(_localToWorld * vertex, _screenAxis, { 0,0 }, _colour));
indices.push_back(index++);
}
RenderableGeometry::updateGeometry(render::GeometryType::Triangles, vertices, indices);
}
};
class RenderablePoint :
public render::RenderableGeometry
{
protected:
const Vertex3f& _point;
const Matrix4& _localToWorld;
bool _needsUpdate;
Vector4 _colour;
public:
RenderablePoint(const Vertex3f& point, const Matrix4& localToWorld) :
_point(point),
_localToWorld(localToWorld),
_needsUpdate(true)
{}
void queueUpdate()
{
_needsUpdate = true;
}
void setColour(const Colour4b& colour)
{
_colour = detail::toVector4(colour);
queueUpdate();
}
protected:
void updateGeometry() override
{
if (!_needsUpdate) return;
_needsUpdate = false;
std::vector<MeshVertex> vertices;
std::vector<unsigned int> indices;
vertices.push_back(MeshVertex(_localToWorld * _point, { 0,0,0 }, { 0,0 }, _colour));
indices.push_back(0);
RenderableGeometry::updateGeometry(render::GeometryType::Points, vertices, indices);
}
};
}