/
RenderableTargetLines.h
148 lines (116 loc) · 4.81 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
#pragma once
#include "TargetKeyCollection.h"
#include "render.h"
#include "irenderable.h"
#include "ivolumetest.h"
#include "math/Segment.h"
#include "render/RenderableGeometry.h"
namespace entity
{
namespace
{
constexpr const double TARGET_MAX_ARROW_LENGTH = 10;
}
/**
* greebo: This is a helper object owned by the TargetableInstance.
* It sets up a line-based renderable 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 render::RenderableGeometry
{
private:
const IEntityNode& _entity;
const TargetKeyCollection& _targetKeys;
Vector3 _worldPosition;
public:
RenderableTargetLines(const IEntityNode& entity, const TargetKeyCollection& targetKeys) :
_entity(entity),
_targetKeys(targetKeys)
{}
bool hasTargets() const
{
return !_targetKeys.empty();
}
void update(const ShaderPtr& shader, const Vector3& worldPosition)
{
// Store the new world position for use in updateGeometry()
_worldPosition = worldPosition;
// Tell the base class to run the rest of the update routine
RenderableGeometry::update(shader);
}
protected:
void updateGeometry() override
{
// 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 time we're asked to
// Collect vertex and index data
std::vector<ArbitraryMeshVertex> vertices;
std::vector<unsigned int> indices;
auto maxTargets = _targetKeys.getNumTargets();
vertices.reserve(6 * maxTargets);
indices.reserve(6 * maxTargets);
_targetKeys.forEachTarget([&](const TargetPtr& target)
{
if (!target || target->isEmpty() || !target->isVisible())
{
return;
}
auto targetPosition = target->getPosition();
addTargetLine(_worldPosition, targetPosition, vertices, indices);
});
RenderableGeometry::updateGeometryWithData(render::GeometryType::Lines, vertices, indices);
}
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 colour = _entity.getEntityColour();
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 }, colour));
vertices.push_back(ArbitraryMeshVertex(endPosition, { 1,0,0 }, { 0, 0 }, colour));
// The "arrow indicators" in the xy plane
vertices.push_back(ArbitraryMeshVertex(mid, { 1,0,0 }, { 0, 0 }, colour));
vertices.push_back(ArbitraryMeshVertex(xyPoint1, { 1,0,0 }, { 0, 0 }, colour));
vertices.push_back(ArbitraryMeshVertex(mid, { 1,0,0 }, { 0, 0 }, colour));
vertices.push_back(ArbitraryMeshVertex(xyPoint2, { 1,0,0 }, { 0, 0 }, colour));
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