NoJobEngine 2.0.7
Loading...
Searching...
No Matches
NavMeshDebugRenderer.cpp
Go to the documentation of this file.
2
8
9#include <limits>
10#include <vector>
11
12namespace NoJob
13{
14 namespace
15 {
16 constexpr const char* NavMeshDebugVertexShader = R"(
17#version 460 core
18
19layout(location = 0) in vec3 a_Position;
20
21uniform mat4 u_ViewProjection;
22
23void 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
34layout(location = 0) out vec4 FragColor;
35
36uniform vec4 u_Color;
37
38void main()
39{
40 FragColor = u_Color;
41}
42)";
43 }
44
46 {
47 m_Shader =
49 NavMeshDebugVertexShader,
50 NavMeshDebugFragmentShader);
51 }
52
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 =
147 lineVertices.data(),
148 static_cast<std::uint32_t>(
149 byteSize));
150
151 m_VertexArray =
153
154 m_VertexArray->SetVertexBuffer(
155 m_VertexBuffer);
156 }
157
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
180 *m_VertexArray,
181 m_VertexCount);
182 }
183
185 {
186 m_VertexArray.reset();
187 m_VertexBuffer.reset();
188 m_VertexCount = 0;
189 }
190}
Buffer declarations for the Rendering subsystem.
NavMesh declarations for the Navigation subsystem.
NavMeshDebugRenderer declarations for the Navigation subsystem.
RenderCommand declarations for the Rendering subsystem.
Shader declarations for the Rendering subsystem.
VertexArray declarations for the Rendering subsystem.
void Build(const NavMesh &navMesh)
void Draw(const glm::mat4 &viewProjection, const glm::vec4 &color=glm::vec4(0.15f, 0.85f, 1.0f, 1.0f)) const
const NavVertex * GetVertex(NavVertexID id) const
Definition NavMesh.cpp:127
const NavPolygon * GetPolygon(NavPolygonID id) const
Definition NavMesh.cpp:137
size_t GetPolygonCount() const
Definition NavMesh.h:69
static void DrawLines(const VertexArray &vertexArray, std::uint32_t vertexCount)
static std::shared_ptr< Shader > Create(const std::string &vertexSource, const std::string &fragmentSource)
Definition Shader.cpp:8
static std::shared_ptr< VertexArray > Create()
static std::shared_ptr< VertexBuffer > Create(const float *vertices, std::uint32_t size)
Definition Buffer.cpp:8
uint32_t NavPolygonID
Definition NavMesh.h:18
NavVertexID EndVertex
Definition NavMesh.h:34
NavVertexID StartVertex
Definition NavMesh.h:33
std::vector< NavEdge > Edges
Definition NavMesh.h:45
glm::vec3 Position
Definition NavMesh.h:28