navMesh.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _NAVMESH_H_
  23. #define _NAVMESH_H_
  24. #include <queue>
  25. #include "torqueRecast.h"
  26. #include "scene/sceneObject.h"
  27. #include "recastPolyList.h"
  28. #include "duDebugDrawTorque.h"
  29. #include <Recast.h>
  30. #include <DetourNavMesh.h>
  31. #include <DetourNavMeshBuilder.h>
  32. #include <DebugDraw.h>
  33. /// @class NavMesh
  34. /// Represents a set of bounds within which a Recast navigation mesh is generated.
  35. /// @see NavMeshPolyList
  36. /// @see Trigger
  37. class NavMesh : public SceneObject {
  38. typedef SceneObject Parent;
  39. friend class NavPath;
  40. public:
  41. /// @name NavMesh build
  42. /// @{
  43. /// Initiates the navmesh build process, which includes notifying the
  44. /// clients and posting an event.
  45. bool build(bool background = true, bool saveIntermediates = false);
  46. /// Stop a build in progress.
  47. void cancelBuild();
  48. /// Save the navmesh to a file.
  49. bool save();
  50. /// Load a saved navmesh from a file.
  51. bool load();
  52. /// Instantly rebuild the tiles in the navmesh that overlap the box.
  53. void buildTiles(const Box3F &box);
  54. /// Instantly rebuild a specific tile.
  55. void buildTile(const U32 &tile);
  56. /// Data file to store this nav mesh in. (From engine executable dir.)
  57. StringTableEntry mFileName;
  58. /// Cell width and height.
  59. F32 mCellSize, mCellHeight;
  60. /// @name Actor data
  61. /// @{
  62. F32 mWalkableHeight,
  63. mWalkableClimb,
  64. mWalkableRadius,
  65. mWalkableSlope;
  66. /// @}
  67. /// @name Generation data
  68. /// @{
  69. U32 mBorderSize;
  70. F32 mDetailSampleDist, mDetailSampleMaxError;
  71. U32 mMaxEdgeLen;
  72. F32 mMaxSimplificationError;
  73. static const U32 mMaxVertsPerPoly;
  74. U32 mMinRegionArea;
  75. U32 mMergeRegionArea;
  76. F32 mTileSize;
  77. U32 mMaxPolysPerTile;
  78. /// @}
  79. /// @}
  80. /// Return the index of the tile included by this point.
  81. S32 getTile(Point3F pos);
  82. /// Return the box of a given tile.
  83. Box3F getTileBox(U32 id);
  84. /// @name SimObject
  85. /// @{
  86. virtual void onEditorEnable();
  87. virtual void onEditorDisable();
  88. void write(Stream &stream, U32 tabStop, U32 flags);
  89. /// @}
  90. /// @name SceneObject
  91. /// @{
  92. static void initPersistFields();
  93. bool onAdd();
  94. void onRemove();
  95. enum flags {
  96. BuildFlag = Parent::NextFreeMask << 0,
  97. LoadFlag = Parent::NextFreeMask << 1,
  98. NextFreeMask = Parent::NextFreeMask << 2,
  99. };
  100. U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream);
  101. void unpackUpdate(NetConnection *conn, BitStream *stream);
  102. void setTransform(const MatrixF &mat);
  103. void setScale(const VectorF &scale);
  104. /// @}
  105. /// @name ProcessObject
  106. /// @{
  107. void processTick(const Move *move);
  108. /// @}
  109. /// @name Rendering
  110. /// @{
  111. void prepRenderImage(SceneRenderState *state);
  112. void render(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat);
  113. bool mAlwaysRender;
  114. /// @}
  115. NavMesh();
  116. ~NavMesh();
  117. DECLARE_CONOBJECT(NavMesh);
  118. void inspectPostApply();
  119. protected:
  120. dtNavMesh const* getNavMesh() { return nm; }
  121. private:
  122. /// Generates a navigation mesh for the collection of objects in this
  123. /// mesh. Returns true if successful. Stores the created mesh in tnm.
  124. bool generateMesh();
  125. /// Builds the next tile in the dirty list.
  126. void buildNextTile();
  127. /// @name Tiles
  128. /// @{
  129. struct Tile {
  130. /// Torque-space world box of this tile.
  131. Box3F box;
  132. /// Local coordinates of this box.
  133. U32 x, y;
  134. /// Recast min and max points.
  135. F32 bmin[3], bmax[3];
  136. /// Default constructor.
  137. Tile() : box(Box3F::Invalid), x(0), y(0)
  138. {
  139. bmin[0] = bmin[1] = bmin[2] = bmax[0] = bmax[1] = bmax[2] = 0.0f;
  140. }
  141. /// Value constructor.
  142. Tile(const Box3F &b, U32 _x, U32 _y, const F32 *min, const F32 *max)
  143. : box(b), x(_x), y(_y)
  144. {
  145. rcVcopy(bmin, min);
  146. rcVcopy(bmax, max);
  147. }
  148. };
  149. /// Intermediate data for tile creation.
  150. struct TileData {
  151. RecastPolyList geom;
  152. rcHeightfield *hf;
  153. rcCompactHeightfield *chf;
  154. rcContourSet *cs;
  155. rcPolyMesh *pm;
  156. rcPolyMeshDetail *pmd;
  157. TileData()
  158. {
  159. hf = NULL;
  160. chf = NULL;
  161. cs = NULL;
  162. pm = NULL;
  163. pmd = NULL;
  164. }
  165. void freeAll()
  166. {
  167. geom.clear();
  168. rcFreeHeightField(hf);
  169. rcFreeCompactHeightfield(chf);
  170. rcFreeContourSet(cs);
  171. rcFreePolyMesh(pm);
  172. rcFreePolyMeshDetail(pmd);
  173. }
  174. ~TileData()
  175. {
  176. freeAll();
  177. }
  178. };
  179. /// List of tiles.
  180. Vector<Tile> mTiles;
  181. /// List of indices to the tile array which are dirty.
  182. std::queue<U32> mDirtyTiles;
  183. /// Update tile dimensions.
  184. void updateTiles(bool dirty = false);
  185. /// Generates navmesh data for a single tile.
  186. unsigned char *buildTileData(const Tile &tile, TileData &data, U32 &dataSize);
  187. /// @}
  188. /// @name Intermediate data
  189. /// @{
  190. /// Config struct.
  191. rcConfig cfg;
  192. /// Updates our config from console members.
  193. void updateConfig();
  194. dtNavMesh *nm;
  195. /// @}
  196. /// Used to perform non-standard validation. detailSampleDist can be 0, or >= 0.9.
  197. static bool setProtectedDetailSampleDist(void *obj, const char *index, const char *data);
  198. /// Updates the client when we check the alwaysRender option.
  199. static bool setProtectedAlwaysRender(void *obj, const char *index, const char *data);
  200. /// @name Threaded updates
  201. /// @{
  202. /// A simple flag to say we are building.
  203. bool mBuilding;
  204. /// @}
  205. /// @name Rendering
  206. /// @{
  207. duDebugDrawTorque dd;
  208. void renderToDrawer();
  209. /// @}
  210. };
  211. #endif