/
RenderableTargetLines.h
185 lines (149 loc) · 5.72 KB
/
RenderableTargetLines.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
#pragma once
#include "TargetKeyCollection.h"
#include "render.h"
#include "irenderable.h"
#include "ivolumetest.h"
#include "math/Segment.h"
namespace entity
{
namespace
{
constexpr const double TARGET_MAX_ARROW_LENGTH = 10;
}
/**
* greebo: This is a helper object owned by the TargetableInstance.
* It represents a RenderablePointVector which repopulates
* itself each frame with the coordinates of the targeted
* instances. It provides a render() method.
*
* The render() method is invoked by the TargetableNode during the
* frontend render pass.
*/
class RenderableTargetLines :
public OpenGLRenderable
{
private:
const TargetKeyCollection& _targetKeys;
bool _needsUpdate;
ShaderPtr _shader;
render::IGeometryRenderer::Slot _surfaceSlot;
std::size_t _numVisibleLines;
public:
RenderableTargetLines(const TargetKeyCollection& targetKeys) :
_targetKeys(targetKeys),
_needsUpdate(true),
_surfaceSlot(render::IGeometryRenderer::InvalidSlot),
_numVisibleLines(0)
{}
bool hasTargets() const
{
return !_targetKeys.empty();
}
void queueUpdate()
{
_needsUpdate = true;
}
void clear()
{
if (_shader && _surfaceSlot != render::IGeometryRenderer::InvalidSlot)
{
_shader->removeGeometry(_surfaceSlot);
}
_shader.reset();
_surfaceSlot = render::IGeometryRenderer::InvalidSlot;
_numVisibleLines = 0;
}
void update(const ShaderPtr& shader, const Vector3& worldPosition)
{
// Target lines are visible if both their start and end entities are visible
// This is hard to track in the scope of this class, so we fall back to doing
// an update on the renderable geometry every frame
auto shaderChanged = _shader != shader;
_needsUpdate = false;
// Collect vertex and index data
std::vector<ArbitraryMeshVertex> vertices;
std::vector<unsigned int> indices;
auto maxTargets = _targetKeys.getNumTargets();
auto numVisibleLines = 0;
vertices.reserve(6 * maxTargets);
indices.reserve(6 * maxTargets);
_targetKeys.forEachTarget([&](const TargetPtr& target)
{
if (!target || target->isEmpty() || !target->isVisible())
{
return;
}
numVisibleLines++;
auto targetPosition = target->getPosition();
addTargetLine(worldPosition, targetPosition, vertices, indices);
});
// Size or shader changes both require detaching our geometry first
if (_shader && _surfaceSlot != render::IGeometryRenderer::InvalidSlot && (shaderChanged || numVisibleLines != _numVisibleLines))
{
clear();
}
_shader = shader;
_numVisibleLines = numVisibleLines;
if (_surfaceSlot == render::IGeometryRenderer::InvalidSlot)
{
_surfaceSlot = shader->addGeometry(render::GeometryType::Lines, vertices, indices);
}
else
{
shader->updateGeometry(_surfaceSlot, vertices, indices);
}
}
void render(const RenderInfo& info) const override
{
if (_surfaceSlot != render::IGeometryRenderer::InvalidSlot && _shader)
{
_shader->renderGeometry(_surfaceSlot);
}
}
private:
// Adds points to the vector, defining a line from start to end, with arrow indicators
// in the XY plane (located at the midpoint between start/end).
void addTargetLine(const Vector3& startPosition, const Vector3& endPosition,
std::vector<ArbitraryMeshVertex>& vertices, std::vector<unsigned int>& indices)
{
// Take the mid-point
Vector3 mid((startPosition + endPosition) * 0.5f);
// Get the normalised target direction
Vector3 targetDir = (endPosition - startPosition);
// Normalise the length manually to get the scale for the arrows
double length = targetDir.getLength();
targetDir *= 1 / length;
// Get the orthogonal direction (in the xy plane)
Vector3 xyDir(endPosition.y() - startPosition.y(), startPosition.x() - endPosition.x(), 0);
xyDir.normalise();
// Let the target arrow not be any longer than one tenth of the total distance
double targetArrowLength = length * 0.10f;
// Clamp the length to a few units anyway
if (targetArrowLength > TARGET_MAX_ARROW_LENGTH) {
targetArrowLength = TARGET_MAX_ARROW_LENGTH;
}
targetDir *= targetArrowLength;
xyDir *= targetArrowLength;
// Get a point slightly away from the target
Vector3 arrowBase(mid - targetDir);
// The arrow points for the XY plane
Vector3 xyPoint1 = arrowBase + xyDir;
Vector3 xyPoint2 = arrowBase - xyDir;
auto indexOffset = static_cast<unsigned int>(vertices.size());
// The line from this to the other entity
vertices.push_back(ArbitraryMeshVertex(startPosition, { 1,0,0 }, { 0, 0 }));
vertices.push_back(ArbitraryMeshVertex(endPosition, { 1,0,0 }, { 0, 0 }));
// The "arrow indicators" in the xy plane
vertices.push_back(ArbitraryMeshVertex(mid, { 1,0,0 }, { 0, 0 }));
vertices.push_back(ArbitraryMeshVertex(xyPoint1, { 1,0,0 }, { 0, 0 }));
vertices.push_back(ArbitraryMeshVertex(mid, { 1,0,0 }, { 0, 0 }));
vertices.push_back(ArbitraryMeshVertex(xyPoint2, { 1,0,0 }, { 0, 0 }));
indices.push_back(indexOffset + 0);
indices.push_back(indexOffset + 1);
indices.push_back(indexOffset + 2);
indices.push_back(indexOffset + 3);
indices.push_back(indexOffset + 4);
indices.push_back(indexOffset + 5);
}
};
} // namespace entity