Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
NavMesh.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32//
33// Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
34//
35// This software is provided 'as-is', without any express or implied
36// warranty. In no event will the authors be held liable for any damages
37// arising from the use of this software.
38// Permission is granted to anyone to use this software for any purpose,
39// including commercial applications, and to alter it and redistribute it
40// freely, subject to the following restrictions:
41// 1. The origin of this software must not be misrepresented; you must not
42// claim that you wrote the original software. If you use this software
43// in a product, an acknowledgment in the product documentation would be
44// appreciated but is not required.
45// 2. Altered source versions must be plainly marked as such, and must not be
46// misrepresented as being the original software.
47// 3. This notice may not be removed or altered from any source distribution.
48//
49// Changes, Additions and Refactoring : Copyright (c) 2010-2011 Lethal Concept,
50// LLC
51// Changes, Additions and Refactoring Author: Simon Wittenberg (MD)
52// The above license is fully inherited.
53//
54// T3D 1.1 NavMesh class and supporting methods.
55// Daniel Buckmaster, 2011
56// With huge thanks to:
57// Lethal Concept http://www.lethalconcept.com/
58// Mikko Mononen http://code.google.com/p/recastnavigation/
59//
60
61#pragma once
62#ifndef DVD_NAVIGATION_MESH_H_
63#define DVD_NAVIGATION_MESH_H_
64
67#include "NavMeshConfig.h"
68#include "NavMeshLoader.h"
69#include "NavMeshContext.h"
70
71namespace Divide {
72
73namespace GFX {
74 class CommandBuffer;
75}
76
77class PlatformContext;
78
80
81namespace AI {
82namespace Navigation {
83
85 'M' << 24 | 'S' << 16 | 'E' << 8 | 'T'; //'MSET';
86constexpr I32 NAVMESHSET_VERSION = 1;
87
93 dtNavMeshParams params;
94};
95
97 dtTileRef tileRef;
99};
100
104class NavMeshDebugDraw;
105class DivideDtCrowd;
106class DivideRecast;
107
108
109namespace Attorney {
111}
112
113class NavigationMesh : public GUIDWrapper, public PlatformContextComponent /*,public SceneObject */ {
115
116 protected:
117 enum class RenderMode : U8 {
118 RENDER_NAVMESH = 0,
123 };
124
125 public:
127
128 void setFileName(const Str<256>& fileName)
129 {
130 _fileName.append(fileName);
131 }
134 bool build(SceneGraphNode* sgn, CreationCallback creationCompleteCallback, bool threaded = true);
136 bool save(const SceneGraphNode* sgn);
138 bool load(const SceneGraphNode* sgn);
140 bool unload();
142 void draw(bool force, GFX::CommandBuffer& bufferInOut, GFX::MemoryBarrierCommand& memCmdInOut );
143 void debugDraw(const bool state) noexcept { _debugDraw = state; }
144 bool debugDraw() const noexcept { return _debugDraw; }
145
146 void setRenderMode(const RenderMode& mode) noexcept { _renderMode = mode; }
147 void setRenderConnections(const bool state) noexcept { _renderConnections = state; }
148
149 const vec3<F32>& getExtents() const noexcept { return _extents; }
150
151 const dtNavMeshQuery& getNavQuery() const noexcept { return *_navQuery; }
152
153 bool getRandomPosition(vec3<F32>& result) const;
154
155 bool getClosestPosition(const vec3<F32>& destination,
156 const vec3<F32>& extents, F32 delta,
157 vec3<F32>& result) const;
158
159 bool getRandomPositionInCircle(const vec3<F32>& center, F32 radius,
160 const vec3<F32>& extents, vec3<F32>& result,
161 U8 maxIters = 15) const;
162
163 NavigationMesh(PlatformContext& context, DivideRecast& recastInterface, Scene& parentScene);
164 ~NavigationMesh() override;
165
166 private:
168 bool buildThreaded();
170 void stopThreadedBuild();
173 bool buildProcess();
175 void buildInternal();
178 bool generateMesh();
180 bool createPolyMesh(const rcConfig& cfg, const NavModelData& data, rcContextDivide* ctx);
182 bool createNavigationMesh(dtNavMeshCreateParams& params);
184 bool loadConfigFromFile();
186 bool createNavigationQuery(U32 maxNodes = 2048);
188 static Str<256> GenerateMeshName(const SceneGraphNode* sgn);
189 private:
191 bool _saveIntermediates = false;
199 rcHeightfield* _heightField;
200 rcCompactHeightfield* _compactHeightField;
201 rcContourSet* _countourSet;
202 rcPolyMesh* _polyMesh;
203 rcPolyMeshDetail* _polyMeshDetail;
204 dtNavMesh* _navMesh;
205 dtNavMesh* _tempNavMesh;
208 void freeIntermediates(bool freeAll);
210
216 std::atomic_bool _building;
227 dtNavMeshQuery* _navQuery;
230 std::atomic_bool _debugDraw;
234 std::unique_ptr<NavMeshDebugDraw> _debugDrawInterface;
235
236 Task* _buildTask = nullptr;
238};
239
240namespace Attorney {
242 static dtNavMesh* getNavigationMesh(NavigationMesh& navMesh) noexcept {
243 return navMesh._navMesh;
244 }
245 static const NavigationMeshConfig& getConfigParams(const NavigationMesh& navMesh) noexcept {
246 return navMesh._configParams;
247 }
249};
250} // namespace Attorney
251} // namespace Navigation
252} // namespace AI
253} // namespace Divide
254
255#endif //DVD_NAVIGATION_MESH_H_
#define FWD_DECLARE_MANAGED_CLASS(T)
static const NavigationMeshConfig & getConfigParams(const NavigationMesh &navMesh) noexcept
Definition: NavMesh.h:245
static dtNavMesh * getNavigationMesh(NavigationMesh &navMesh) noexcept
Definition: NavMesh.h:242
NavigationMeshConfig _configParams
Definition: NavMesh.h:192
bool debugDraw() const noexcept
Definition: NavMesh.h:144
bool save(const SceneGraphNode *sgn)
Save the NavigationMesh to a file.
Definition: NavMesh.cpp:784
void draw(bool force, GFX::CommandBuffer &bufferInOut, GFX::MemoryBarrierCommand &memCmdInOut)
Render the debug mesh if debug drawing is enabled.
Definition: NavMesh.cpp:634
Mutex _navigationMeshLock
A mutex for accessing our actual NavigationMesh.
Definition: NavMesh.h:214
bool unload()
Unload the navmesh reverting the instance to an empty container.
Definition: NavMesh.cpp:56
DELEGATE< void, NavigationMesh * > CreationCallback
Definition: NavMesh.h:126
bool loadConfigFromFile()
Load nav mesh configuration from file.
Definition: NavMesh.cpp:123
static Str< 256 > GenerateMeshName(const SceneGraphNode *sgn)
Create a unique mesh name using the given root node.
Definition: NavMesh.cpp:851
bool build(SceneGraphNode *sgn, CreationCallback creationCompleteCallback, bool threaded=true)
Definition: NavMesh.cpp:174
I64 _buildJobGUID
A thread for us to update in.
Definition: NavMesh.h:212
std::atomic_bool _building
A simple flag to say we are building.
Definition: NavMesh.h:216
CreationCallback _loadCompleteClbk
A callback function to call after building is complete.
Definition: NavMesh.h:218
void buildInternal()
Used for multithreaded loading.
Definition: NavMesh.cpp:211
bool getClosestPosition(const vec3< F32 > &destination, const vec3< F32 > &extents, F32 delta, vec3< F32 > &result) const
Definition: NavMesh.cpp:858
void debugDraw(const bool state) noexcept
Definition: NavMesh.h:143
bool getRandomPosition(vec3< F32 > &result) const
Definition: NavMesh.cpp:867
bool createPolyMesh(const rcConfig &cfg, const NavModelData &data, rcContextDivide *ctx)
Performs the Recast part of the build process.
Definition: NavMesh.cpp:442
SceneGraphNode * _sgn
SceneGraphNode from which to build.
Definition: NavMesh.h:229
const vec3< F32 > & getExtents() const noexcept
Definition: NavMesh.h:149
std::unique_ptr< NavMeshDebugDraw > _debugDrawInterface
DebugDraw interface.
Definition: NavMesh.h:234
rcCompactHeightfield * _compactHeightField
Definition: NavMesh.h:200
dtNavMeshQuery * _navQuery
Query object used for this mesh.
Definition: NavMesh.h:227
void setRenderMode(const RenderMode &mode) noexcept
Definition: NavMesh.h:146
vec3< F32 > _extents
NavMesh extents.
Definition: NavMesh.h:225
void setFileName(const Str< 256 > &fileName)
Definition: NavMesh.h:128
string _configFile
Configuration file.
Definition: NavMesh.h:223
bool createNavigationMesh(dtNavMeshCreateParams &params)
Performs the Detour part of the build process.
Definition: NavMesh.cpp:585
void stopThreadedBuild()
Stop the threaded build process;.
Definition: NavMesh.cpp:75
void freeIntermediates(bool freeAll)
Definition: NavMesh.cpp:85
const dtNavMeshQuery & getNavQuery() const noexcept
Definition: NavMesh.h:151
void setRenderConnections(const bool state) noexcept
Definition: NavMesh.h:147
bool createNavigationQuery(U32 maxNodes=2048)
Create a navigation mesh query to help in pathfinding.
Definition: NavMesh.cpp:436
Str< 256 > _fileName
Data file to store this nav mesh in.
Definition: NavMesh.h:220
bool buildThreaded()
Initiates the build process in a separate thread.
Definition: NavMesh.cpp:197
bool getRandomPositionInCircle(const vec3< F32 > &center, F32 radius, const vec3< F32 > &extents, vec3< F32 > &result, U8 maxIters=15) const
Definition: NavMesh.cpp:872
rcPolyMeshDetail * _polyMeshDetail
Definition: NavMesh.h:203
Utility class that adds basic GUID management to objects.
Definition: GUIDWrapper.h:44
PlatformContext & context() noexcept
constexpr I32 NAVMESHSET_MAGIC
Definition: NavMesh.h:84
constexpr I32 NAVMESHSET_VERSION
Definition: NavMesh.h:86
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
DELEGATE_STD< Ret, Args... > DELEGATE
bool load() override
std::mutex Mutex
Definition: SharedMutex.h:40
int32_t I32
uint8_t U8
int64_t I64
uint32_t U32