/
RenderableGeometry.h
278 lines (234 loc) · 7.73 KB
/
RenderableGeometry.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
#pragma once
#include <vector>
#include "igeometryrenderer.h"
#include "isurfacerenderer.h"
#include "irenderableobject.h"
#include "irender.h"
namespace render
{
/**
* Geometry base type, handling vertex data updates in combination
* with an IGeometryRenderer instance.
*
* It implements the OpenGLRenderable interface which will instruct
* the shader to render just the geometry batch managed by this object.
* This is used to render highlights (such as selection overlays).
*/
class RenderableGeometry :
public OpenGLRenderable
{
private:
ShaderPtr _shader;
IGeometryRenderer::Slot _surfaceSlot;
std::size_t _lastVertexSize; // To detect size changes when updating geometry
std::size_t _lastIndexSize; // To detect size changes when updating geometry
class RenderAdapter final :
public IRenderableObject
{
private:
RenderableGeometry& _owner;
AABB _bounds;
bool _boundsNeedRecalculation;
sigc::signal<void> _sigBoundsChanged;
public:
RenderAdapter(RenderableGeometry& owner) :
_owner(owner),
_boundsNeedRecalculation(true)
{}
bool isVisible() override
{
return _owner._surfaceSlot != IGeometryRenderer::InvalidSlot;
}
bool isOriented() override
{
return false;
}
const Matrix4& getObjectTransform() override
{
static Matrix4 _identity = Matrix4::getIdentity();
return _identity;
}
const AABB& getObjectBounds() override
{
if (_boundsNeedRecalculation)
{
_boundsNeedRecalculation = false;
_bounds = _owner._shader->getGeometryBounds(_owner._surfaceSlot);
}
return _bounds;
}
// Will recalculate the geometry bounds next time getObjectBounds() is called
// Also emits the signal to the observing entity
void boundsChanged()
{
_boundsNeedRecalculation = true;
signal_boundsChanged().emit();
}
sigc::signal<void>& signal_boundsChanged() override
{
return _sigBoundsChanged;
}
IGeometryStore::Slot getStorageLocation() override
{
if (_owner._surfaceSlot == IGeometryRenderer::InvalidSlot)
{
throw std::logic_error("Cannot access storage of unattached RenderableGeometry");
}
return _owner._shader->getGeometryStorageLocation(_owner._surfaceSlot);
}
};
// Adapater suitable to be attached to an IRenderEntity
std::shared_ptr<RenderAdapter> _renderAdapter;
// The render entity the adapter is attached to
IRenderEntity* _renderEntity;
protected:
RenderableGeometry() :
_surfaceSlot(IGeometryRenderer::InvalidSlot),
_lastVertexSize(0),
_lastIndexSize(0),
_renderEntity(nullptr)
{}
public:
// Noncopyable
RenderableGeometry(const RenderableGeometry& other) = delete;
RenderableGeometry& operator=(const RenderableGeometry& other) = delete;
virtual ~RenderableGeometry()
{
clear();
}
// (Non-virtual) update method handling any possible shader change
// The geometry is withdrawn from the given shader if it turns out
// to be different from the last update.
void update(const ShaderPtr& shader)
{
bool shaderChanged = _shader != shader;
if (shaderChanged)
{
clear();
}
// Update our local shader reference
_shader = shader;
if (_shader)
{
// Invoke the virtual method to run needed updates in the subclass
updateGeometry();
}
}
// Removes the geometry and clears the shader reference
void clear()
{
// Detach from the render entity when being cleared
detachFromEntity();
removeGeometry();
_shader.reset();
}
// Renders the geometry stored in our single slot
void render() const override
{
if (_surfaceSlot != IGeometryRenderer::InvalidSlot && _shader)
{
_shader->renderGeometry(_surfaceSlot);
}
}
// Attach this geometry to the given render entity.
// This call is only valid if this Geometry instance has been attached to a shader
// Does nothing if already attached to the given render entity.
void attachToEntity(IRenderEntity* entity)
{
if (_renderEntity == entity) return; // nothing to do
if (!_shader)
{
throw std::logic_error("Cannot attach geometry without any shader");
}
if (_renderEntity && entity != _renderEntity)
{
detachFromEntity();
}
_renderEntity = entity;
ensureRenderAdapter();
_renderEntity->addRenderable(_renderAdapter, _shader.get());
}
void detachFromEntity()
{
if (_renderEntity)
{
_renderEntity->removeRenderable(_renderAdapter);
_renderEntity = nullptr;
}
}
protected:
/**
* Creates the adapter class suitable to attach this geometry as surface to a render entity
* The surface will have an identity transform (all vertices specified in world coords).
* This adapter will be valid as long as this geometry is attached to the IGeometryRenderer,
* otherwise no access to the stored vertices/indices is possible.
*/
void ensureRenderAdapter()
{
if (!_renderAdapter)
{
_renderAdapter = std::make_shared<RenderAdapter>(*this);
}
}
// Removes the geometry from the attached shader. Does nothing if no geometry has been added.
void removeGeometry()
{
if (_shader && _surfaceSlot != IGeometryRenderer::InvalidSlot)
{
_shader->removeGeometry(_surfaceSlot);
}
_lastVertexSize = 0;
_lastIndexSize = 0;
_surfaceSlot = IGeometryRenderer::InvalidSlot;
}
// Sub-class specific geometry update. Should check whether any of the vertex data
// needs to be added or updated to the shader, in which case the implementation
// should invoke the updateGeometry(type, vertices, indices) overload below
virtual void updateGeometry() = 0;
/**
* @brief Submits the given geometry to the known _shader reference.
*
* This method is supposed to be called from within updateGeometry() to ensure that the _shader
* reference is already up to date.
*
* @param type
* Type of primitives to render (quads, lines etc)
*
* @param vertices
* Vector of vertex data.
*
* @param indices
* Indices of vertices to join together into primitives.
*/
void updateGeometryWithData(GeometryType type,
const std::vector<MeshVertex>& vertices,
const std::vector<unsigned int>& indices)
{
// Size changes require removal of the geometry before update
if (_lastVertexSize != vertices.size() || _lastIndexSize != indices.size())
{
removeGeometry();
_lastVertexSize = vertices.size();
_lastIndexSize = indices.size();
}
if (vertices.empty() || indices.empty())
{
clear();
return;
}
if (_surfaceSlot == IGeometryRenderer::InvalidSlot)
{
_surfaceSlot = _shader->addGeometry(type, vertices, indices);
}
else
{
_shader->updateGeometry(_surfaceSlot, vertices, indices);
}
// Fire the bounds changed signal (after submitting the changed vertices)
if (_renderAdapter)
{
_renderAdapter->boundsChanged();
}
}
};
}