/
RenderableGeometry.h
325 lines (272 loc) · 9.17 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
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
#pragma once
#include <vector>
#include "igeometryrenderer.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._isVisible && _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);
}
bool isShadowCasting() override
{
return _owner._renderEntity->isShadowCasting();
}
};
// Adapater suitable to be attached to an IRenderEntity
std::shared_ptr<RenderAdapter> _renderAdapter;
// The render entity the adapter is attached to
IRenderEntity* _renderEntity;
// Whether the geometry slot is set to active
bool _isVisible;
protected:
RenderableGeometry() :
_surfaceSlot(IGeometryRenderer::InvalidSlot),
_lastVertexSize(0),
_lastIndexSize(0),
_renderEntity(nullptr),
_isVisible(true)
{}
public:
// Noncopyable
RenderableGeometry(const RenderableGeometry& other) = delete;
RenderableGeometry& operator=(const RenderableGeometry& other) = delete;
~RenderableGeometry() override
{
clear();
}
// (Non-virtual) update method handling any possible shader and geometry changes.
// The geometry is withdrawn from any previous shader if the given one
// turns out to be different from the last update.
// If the geometry has been hidden before, it will be automatically
// set to visible again.
void update(const ShaderPtr& shader)
{
if (_shader != shader)
{
clear();
// Update our local shader reference
_shader = shader;
}
if (_shader)
{
// Invoke the virtual method to run needed updates in the subclass
updateGeometry();
}
if (!_isVisible)
{
show();
}
}
// Un-hide the geometry, such that it can be rendered again.
// Also re-enables the renderable object that might be attached to an entity.
// Note that calling update() will implicitly show this geometry if it has been hidden.
void show()
{
if (_isVisible) return;
_isVisible = true;
if (_shader && _surfaceSlot != IGeometryRenderer::InvalidSlot)
{
_shader->activateGeometry(_surfaceSlot);
}
}
// Hide the geometry to skip it during rendering. If the geometry has been
// previously attached to an entity, it will be set to invisible.
void hide()
{
if (!_isVisible) return;
// Set the flag to false, the render adapter will be respecting this
_isVisible = false;
if (_shader && _surfaceSlot != IGeometryRenderer::InvalidSlot)
{
_shader->deactivateGeometry(_surfaceSlot);
}
}
// Removes the geometry and clears the shader reference
// Resets this geometry's visibility to true.
void clear()
{
// Detach from the render entity when being cleared
detachFromEntity();
removeGeometry();
_shader.reset();
_isVisible = true;
}
// 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<RenderVertex>& 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
{
// In case the slot had been deactivated => reactivate automatically
_shader->updateGeometry(_surfaceSlot, vertices, indices);
}
// Fire the bounds changed signal (after submitting the changed vertices)
if (_renderAdapter)
{
_renderAdapter->boundsChanged();
}
}
};
}