NoJobEngine
2.0.7
Toggle main menu visibility
Loading...
Searching...
No Matches
NavMeshDebugRenderer.cpp
Go to the documentation of this file.
1
#include "
Engine/AI/Navigation/NavMeshDebugRenderer.h
"
2
3
#include "
Engine/AI/Navigation/NavMesh.h
"
4
#include "
Engine/Renderer/Buffer.h
"
5
#include "
Engine/Renderer/RenderCommand.h
"
6
#include "
Engine/Renderer/Shader.h
"
7
#include "
Engine/Renderer/VertexArray.h
"
8
9
#include <limits>
10
#include <vector>
11
12
namespace
NoJob
13
{
14
namespace
15
{
16
constexpr
const
char
* NavMeshDebugVertexShader = R
"(
17
#version 460 core
18
19
layout(location = 0) in vec3 a_Position;
20
21
uniform mat4 u_ViewProjection;
22
23
void main()
24
{
25
gl_Position =
26
u_ViewProjection *
27
vec4(a_Position, 1.0);
28
}
29
)";
30
31
constexpr
const
char
* NavMeshDebugFragmentShader = R
"(
32
#version 460 core
33
34
layout(location = 0) out vec4 FragColor;
35
36
uniform vec4 u_Color;
37
38
void main()
39
{
40
FragColor = u_Color;
41
}
42
)";
43
}
44
45
NavMeshDebugRenderer::NavMeshDebugRenderer
()
46
{
47
m_Shader =
48
Shader::Create
(
49
NavMeshDebugVertexShader,
50
NavMeshDebugFragmentShader);
51
}
52
53
void
NavMeshDebugRenderer::Build
(
54
const
NavMesh
& navMesh)
55
{
56
Clear
();
57
58
std::vector<float> lineVertices;
59
60
//
61
// Each NavEdge becomes two position-only vertices:
62
//
63
// StartVertex ---- EndVertex
64
//
65
// GL_LINES interprets every pair as one independent line.
66
//
67
for
(std::size_t polygonIndex = 0;
68
polygonIndex < navMesh.
GetPolygonCount
();
69
++polygonIndex)
70
{
71
const
NavPolygon
* polygon =
72
navMesh.
GetPolygon
(
73
static_cast<
NavPolygonID
>
(
74
polygonIndex));
75
76
if
(!polygon)
77
{
78
continue
;
79
}
80
81
for
(
const
NavEdge
& edge :
82
polygon->
Edges
)
83
{
84
const
NavVertex
* start =
85
navMesh.
GetVertex
(
86
edge.
StartVertex
);
87
88
const
NavVertex
* end =
89
navMesh.
GetVertex
(
90
edge.
EndVertex
);
91
92
if
(!start || !end)
93
{
94
continue
;
95
}
96
97
lineVertices.push_back(
98
start->
Position
.x);
99
100
lineVertices.push_back(
101
start->
Position
.y);
102
103
lineVertices.push_back(
104
start->
Position
.z);
105
106
lineVertices.push_back(
107
end->
Position
.x);
108
109
lineVertices.push_back(
110
end->
Position
.y);
111
112
lineVertices.push_back(
113
end->
Position
.z);
114
}
115
}
116
117
if
(lineVertices.empty())
118
{
119
return
;
120
}
121
122
const
std::size_t vertexCount =
123
lineVertices.size() / 3;
124
125
if
(vertexCount >
126
std::numeric_limits<std::uint32_t>::max())
127
{
128
return
;
129
}
130
131
const
std::size_t byteSize =
132
lineVertices.size() *
133
sizeof
(float);
134
135
if
(byteSize >
136
std::numeric_limits<std::uint32_t>::max())
137
{
138
return
;
139
}
140
141
m_VertexCount =
142
static_cast<
std::uint32_t
>
(
143
vertexCount);
144
145
m_VertexBuffer =
146
VertexBuffer::Create
(
147
lineVertices.data(),
148
static_cast<
std::uint32_t
>
(
149
byteSize));
150
151
m_VertexArray =
152
VertexArray::Create
();
153
154
m_VertexArray->SetVertexBuffer(
155
m_VertexBuffer);
156
}
157
158
void
NavMeshDebugRenderer::Draw
(
159
const
glm::mat4& viewProjection,
160
const
glm::vec4& color)
const
161
{
162
if
(!m_VertexArray ||
163
!m_Shader ||
164
m_VertexCount < 2)
165
{
166
return
;
167
}
168
169
m_Shader->Bind();
170
171
m_Shader->SetMat4(
172
"u_ViewProjection"
,
173
viewProjection);
174
175
m_Shader->SetFloat4(
176
"u_Color"
,
177
color);
178
179
RenderCommand::DrawLines
(
180
*m_VertexArray,
181
m_VertexCount);
182
}
183
184
void
NavMeshDebugRenderer::Clear
()
185
{
186
m_VertexArray.reset();
187
m_VertexBuffer.reset();
188
m_VertexCount = 0;
189
}
190
}
Buffer.h
Buffer declarations for the Rendering subsystem.
NavMesh.h
NavMesh declarations for the Navigation subsystem.
NavMeshDebugRenderer.h
NavMeshDebugRenderer declarations for the Navigation subsystem.
RenderCommand.h
RenderCommand declarations for the Rendering subsystem.
Shader.h
Shader declarations for the Rendering subsystem.
VertexArray.h
VertexArray declarations for the Rendering subsystem.
NoJob::NavMeshDebugRenderer::Build
void Build(const NavMesh &navMesh)
Definition
NavMeshDebugRenderer.cpp:53
NoJob::NavMeshDebugRenderer::NavMeshDebugRenderer
NavMeshDebugRenderer()
Definition
NavMeshDebugRenderer.cpp:45
NoJob::NavMeshDebugRenderer::Clear
void Clear()
Definition
NavMeshDebugRenderer.cpp:184
NoJob::NavMeshDebugRenderer::Draw
void Draw(const glm::mat4 &viewProjection, const glm::vec4 &color=glm::vec4(0.15f, 0.85f, 1.0f, 1.0f)) const
Definition
NavMeshDebugRenderer.cpp:158
NoJob::NavMesh
Definition
NavMesh.h:53
NoJob::NavMesh::GetVertex
const NavVertex * GetVertex(NavVertexID id) const
Definition
NavMesh.cpp:127
NoJob::NavMesh::GetPolygon
const NavPolygon * GetPolygon(NavPolygonID id) const
Definition
NavMesh.cpp:137
NoJob::NavMesh::GetPolygonCount
size_t GetPolygonCount() const
Definition
NavMesh.h:69
NoJob::RenderCommand::DrawLines
static void DrawLines(const VertexArray &vertexArray, std::uint32_t vertexCount)
Definition
RenderCommand.cpp:60
NoJob::Shader::Create
static std::shared_ptr< Shader > Create(const std::string &vertexSource, const std::string &fragmentSource)
Definition
Shader.cpp:8
NoJob::VertexArray::Create
static std::shared_ptr< VertexArray > Create()
Definition
VertexArray.cpp:8
NoJob::VertexBuffer::Create
static std::shared_ptr< VertexBuffer > Create(const float *vertices, std::uint32_t size)
Definition
Buffer.cpp:8
NoJob
Definition
BTActionNode.h:13
NoJob::NavPolygonID
uint32_t NavPolygonID
Definition
NavMesh.h:18
NoJob::NavEdge
Definition
NavMesh.h:32
NoJob::NavEdge::EndVertex
NavVertexID EndVertex
Definition
NavMesh.h:34
NoJob::NavEdge::StartVertex
NavVertexID StartVertex
Definition
NavMesh.h:33
NoJob::NavPolygon
Definition
NavMesh.h:43
NoJob::NavPolygon::Edges
std::vector< NavEdge > Edges
Definition
NavMesh.h:45
NoJob::NavVertex
Definition
NavMesh.h:27
NoJob::NavVertex::Position
glm::vec3 Position
Definition
NavMesh.h:28
Engine
AI
Navigation
NavMeshDebugRenderer.cpp
Generated by
1.18.0