/
RenderableEntityBox.cpp
159 lines (130 loc) · 4.79 KB
/
RenderableEntityBox.cpp
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
#include "RenderableEntityBox.h"
#include "EntityNode.h"
namespace entity
{
namespace
{
inline std::vector<ArbitraryMeshVertex> getFillBoxVertices(const Vector3& min, const Vector3& max)
{
// Load the 6 times 4 = 24 corner points, each with the correct face normal
return
{
// Bottom quad
ArbitraryMeshVertex({ min[0], min[1], min[2] }, {0,0,-1}, {0,0}),
ArbitraryMeshVertex({ max[0], min[1], min[2] }, {0,0,-1}, {0,0}),
ArbitraryMeshVertex({ max[0], max[1], min[2] }, {0,0,-1}, {0,0}),
ArbitraryMeshVertex({ min[0], max[1], min[2] }, {0,0,-1}, {0,0}),
// Top quad
ArbitraryMeshVertex({ min[0], min[1], max[2] }, {0,0,+1}, {0,0}),
ArbitraryMeshVertex({ max[0], min[1], max[2] }, {0,0,+1}, {0,0}),
ArbitraryMeshVertex({ max[0], max[1], max[2] }, {0,0,+1}, {0,0}),
ArbitraryMeshVertex({ min[0], max[1], max[2] }, {0,0,+1}, {0,0}),
// Front quad
ArbitraryMeshVertex({ min[0], min[1], min[2] }, {0,-1,0}, {0,0}),
ArbitraryMeshVertex({ max[0], min[1], min[2] }, {0,-1,0}, {0,0}),
ArbitraryMeshVertex({ max[0], min[1], max[2] }, {0,-1,0}, {0,0}),
ArbitraryMeshVertex({ min[0], min[1], max[2] }, {0,-1,0}, {0,0}),
// Back quad
ArbitraryMeshVertex({ max[0], max[1], min[2] }, {0,+1,0}, {0,0}),
ArbitraryMeshVertex({ min[0], max[1], min[2] }, {0,+1,0}, {0,0}),
ArbitraryMeshVertex({ min[0], max[1], max[2] }, {0,+1,0}, {0,0}),
ArbitraryMeshVertex({ max[0], max[1], max[2] }, {0,+1,0}, {0,0}),
// Right quad
ArbitraryMeshVertex({ max[0], min[1], min[2] }, {+1,0,0}, {0,0}),
ArbitraryMeshVertex({ max[0], max[1], min[2] }, {+1,0,0}, {0,0}),
ArbitraryMeshVertex({ max[0], max[1], max[2] }, {+1,0,0}, {0,0}),
ArbitraryMeshVertex({ min[0], max[1], max[2] }, {+1,0,0}, {0,0}),
// Left quad
ArbitraryMeshVertex({ min[0], max[1], min[2] }, {-1,0,0}, {0,0}),
ArbitraryMeshVertex({ min[0], min[1], min[2] }, {-1,0,0}, {0,0}),
ArbitraryMeshVertex({ min[0], min[1], max[2] }, {-1,0,0}, {0,0}),
ArbitraryMeshVertex({ min[0], max[1], max[2] }, {-1,0,0}, {0,0}),
};
}
inline std::vector<ArbitraryMeshVertex> getWireframeBoxVertices(const Vector3& min, const Vector3& max)
{
// Load the 8 corner points
return
{
// Bottom quad
ArbitraryMeshVertex({ min[0], min[1], min[2] }, {0,0,1}, {0,0}),
ArbitraryMeshVertex({ max[0], min[1], min[2] }, {0,0,1}, {0,0}),
ArbitraryMeshVertex({ max[0], max[1], min[2] }, {0,0,1}, {0,0}),
ArbitraryMeshVertex({ min[0], max[1], min[2] }, {0,0,1}, {0,0}),
// Top quad
ArbitraryMeshVertex({ min[0], min[1], max[2] }, {0,0,1}, {0,0}),
ArbitraryMeshVertex({ max[0], min[1], max[2] }, {0,0,1}, {0,0}),
ArbitraryMeshVertex({ max[0], max[1], max[2] }, {0,0,1}, {0,0}),
ArbitraryMeshVertex({ min[0], max[1], max[2] }, {0,0,1}, {0,0}),
};
}
// Indices drawing a hollow box outline, corresponding to the order in getWireframeBoxVertices()
static const std::vector<unsigned int> WireframeBoxIndices
{
0, 1, // bottom rectangle
1, 2, //
2, 3, //
3, 0, //
4, 5, // top rectangle
5, 6, //
6, 7, //
7, 4, //
0, 4, // vertical edges
1, 5, //
2, 6, //
3, 7, //
};
// Indices drawing a hollow box outline, corresponding to the order in getFillBoxVertices()
static const std::vector<unsigned int> FillBoxIndices
{
3, 2, 1, 0, // bottom rectangle
7, 6, 5, 4, // top rectangle
4, 5, 1, 0, // sides
5, 6, 2, 1,
6, 7, 3, 2,
7, 4, 0, 3,
};
}
RenderableEntityBox::RenderableEntityBox(EntityNode& node) :
_node(node),
_needsUpdate(true),
_filledBox(true)
{}
void RenderableEntityBox::queueUpdate()
{
_needsUpdate = true;
}
void RenderableEntityBox::setFillMode(bool fill)
{
if (_filledBox != fill)
{
_filledBox = fill;
clear();
}
}
void RenderableEntityBox::updateGeometry()
{
if (!_needsUpdate) return;
_needsUpdate = false;
static Vector3 Origin(0, 0, 0);
const auto& bounds = _node.localAABB();
// Calculate the corner vertices of this bounding box, plus the mid-point
Vector3 max(Origin + bounds.extents);
Vector3 min(Origin - bounds.extents);
auto vertices = _filledBox ? getFillBoxVertices(min, max) : getWireframeBoxVertices(min, max);
// Move the points to their world position
const auto& translation = _node.worldAABB().getOrigin();
for (auto& vertex : vertices)
{
vertex.vertex += translation;
}
if (_filledBox)
{
RenderableGeometry::updateGeometry(render::GeometryType::Quads, vertices, FillBoxIndices);
}
else
{
RenderableGeometry::updateGeometry(render::GeometryType::Lines, vertices, WireframeBoxIndices);
}
}
}