Sample_SoloMesh.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. //
  2. // Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #define _USE_MATH_DEFINES
  19. #include <math.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include "SDL.h"
  23. #include "SDL_opengl.h"
  24. #include "imgui.h"
  25. #include "InputGeom.h"
  26. #include "Sample.h"
  27. #include "Sample_SoloMesh.h"
  28. #include "Recast.h"
  29. #include "RecastDebugDraw.h"
  30. #include "RecastDump.h"
  31. #include "DetourNavMesh.h"
  32. #include "DetourNavMeshBuilder.h"
  33. #include "DetourDebugDraw.h"
  34. #include "NavMeshTesterTool.h"
  35. #include "NavMeshPruneTool.h"
  36. #include "OffMeshConnectionTool.h"
  37. #include "ConvexVolumeTool.h"
  38. #include "CrowdTool.h"
  39. #ifdef WIN32
  40. # define snprintf _snprintf
  41. #endif
  42. Sample_SoloMesh::Sample_SoloMesh() :
  43. m_keepInterResults(true),
  44. m_totalBuildTimeMs(0),
  45. m_triareas(0),
  46. m_solid(0),
  47. m_chf(0),
  48. m_cset(0),
  49. m_pmesh(0),
  50. m_dmesh(0),
  51. m_drawMode(DRAWMODE_NAVMESH)
  52. {
  53. setTool(new NavMeshTesterTool);
  54. }
  55. Sample_SoloMesh::~Sample_SoloMesh()
  56. {
  57. cleanup();
  58. }
  59. void Sample_SoloMesh::cleanup()
  60. {
  61. delete [] m_triareas;
  62. m_triareas = 0;
  63. rcFreeHeightField(m_solid);
  64. m_solid = 0;
  65. rcFreeCompactHeightfield(m_chf);
  66. m_chf = 0;
  67. rcFreeContourSet(m_cset);
  68. m_cset = 0;
  69. rcFreePolyMesh(m_pmesh);
  70. m_pmesh = 0;
  71. rcFreePolyMeshDetail(m_dmesh);
  72. m_dmesh = 0;
  73. dtFreeNavMesh(m_navMesh);
  74. m_navMesh = 0;
  75. }
  76. void Sample_SoloMesh::handleSettings()
  77. {
  78. Sample::handleCommonSettings();
  79. if (imguiCheck("Keep Itermediate Results", m_keepInterResults))
  80. m_keepInterResults = !m_keepInterResults;
  81. imguiSeparator();
  82. imguiIndent();
  83. imguiIndent();
  84. if (imguiButton("Save"))
  85. {
  86. Sample::saveAll("solo_navmesh.bin", m_navMesh);
  87. }
  88. if (imguiButton("Load"))
  89. {
  90. dtFreeNavMesh(m_navMesh);
  91. m_navMesh = Sample::loadAll("solo_navmesh.bin");
  92. m_navQuery->init(m_navMesh, 2048);
  93. }
  94. imguiUnindent();
  95. imguiUnindent();
  96. char msg[64];
  97. snprintf(msg, 64, "Build Time: %.1fms", m_totalBuildTimeMs);
  98. imguiLabel(msg);
  99. imguiSeparator();
  100. }
  101. void Sample_SoloMesh::handleTools()
  102. {
  103. int type = !m_tool ? TOOL_NONE : m_tool->type();
  104. if (imguiCheck("Test Navmesh", type == TOOL_NAVMESH_TESTER))
  105. {
  106. setTool(new NavMeshTesterTool);
  107. }
  108. if (imguiCheck("Prune Navmesh", type == TOOL_NAVMESH_PRUNE))
  109. {
  110. setTool(new NavMeshPruneTool);
  111. }
  112. if (imguiCheck("Create Off-Mesh Connections", type == TOOL_OFFMESH_CONNECTION))
  113. {
  114. setTool(new OffMeshConnectionTool);
  115. }
  116. if (imguiCheck("Create Convex Volumes", type == TOOL_CONVEX_VOLUME))
  117. {
  118. setTool(new ConvexVolumeTool);
  119. }
  120. if (imguiCheck("Create Crowds", type == TOOL_CROWD))
  121. {
  122. setTool(new CrowdTool);
  123. }
  124. imguiSeparatorLine();
  125. imguiIndent();
  126. if (m_tool)
  127. m_tool->handleMenu();
  128. imguiUnindent();
  129. }
  130. void Sample_SoloMesh::handleDebugMode()
  131. {
  132. // Check which modes are valid.
  133. bool valid[MAX_DRAWMODE];
  134. for (int i = 0; i < MAX_DRAWMODE; ++i)
  135. valid[i] = false;
  136. if (m_geom)
  137. {
  138. valid[DRAWMODE_NAVMESH] = m_navMesh != 0;
  139. valid[DRAWMODE_NAVMESH_TRANS] = m_navMesh != 0;
  140. valid[DRAWMODE_NAVMESH_BVTREE] = m_navMesh != 0;
  141. valid[DRAWMODE_NAVMESH_NODES] = m_navQuery != 0;
  142. valid[DRAWMODE_NAVMESH_INVIS] = m_navMesh != 0;
  143. valid[DRAWMODE_MESH] = true;
  144. valid[DRAWMODE_VOXELS] = m_solid != 0;
  145. valid[DRAWMODE_VOXELS_WALKABLE] = m_solid != 0;
  146. valid[DRAWMODE_COMPACT] = m_chf != 0;
  147. valid[DRAWMODE_COMPACT_DISTANCE] = m_chf != 0;
  148. valid[DRAWMODE_COMPACT_REGIONS] = m_chf != 0;
  149. valid[DRAWMODE_REGION_CONNECTIONS] = m_cset != 0;
  150. valid[DRAWMODE_RAW_CONTOURS] = m_cset != 0;
  151. valid[DRAWMODE_BOTH_CONTOURS] = m_cset != 0;
  152. valid[DRAWMODE_CONTOURS] = m_cset != 0;
  153. valid[DRAWMODE_POLYMESH] = m_pmesh != 0;
  154. valid[DRAWMODE_POLYMESH_DETAIL] = m_dmesh != 0;
  155. }
  156. int unavail = 0;
  157. for (int i = 0; i < MAX_DRAWMODE; ++i)
  158. if (!valid[i]) unavail++;
  159. if (unavail == MAX_DRAWMODE)
  160. return;
  161. imguiLabel("Draw");
  162. if (imguiCheck("Input Mesh", m_drawMode == DRAWMODE_MESH, valid[DRAWMODE_MESH]))
  163. m_drawMode = DRAWMODE_MESH;
  164. if (imguiCheck("Navmesh", m_drawMode == DRAWMODE_NAVMESH, valid[DRAWMODE_NAVMESH]))
  165. m_drawMode = DRAWMODE_NAVMESH;
  166. if (imguiCheck("Navmesh Invis", m_drawMode == DRAWMODE_NAVMESH_INVIS, valid[DRAWMODE_NAVMESH_INVIS]))
  167. m_drawMode = DRAWMODE_NAVMESH_INVIS;
  168. if (imguiCheck("Navmesh Trans", m_drawMode == DRAWMODE_NAVMESH_TRANS, valid[DRAWMODE_NAVMESH_TRANS]))
  169. m_drawMode = DRAWMODE_NAVMESH_TRANS;
  170. if (imguiCheck("Navmesh BVTree", m_drawMode == DRAWMODE_NAVMESH_BVTREE, valid[DRAWMODE_NAVMESH_BVTREE]))
  171. m_drawMode = DRAWMODE_NAVMESH_BVTREE;
  172. if (imguiCheck("Navmesh Nodes", m_drawMode == DRAWMODE_NAVMESH_NODES, valid[DRAWMODE_NAVMESH_NODES]))
  173. m_drawMode = DRAWMODE_NAVMESH_NODES;
  174. if (imguiCheck("Voxels", m_drawMode == DRAWMODE_VOXELS, valid[DRAWMODE_VOXELS]))
  175. m_drawMode = DRAWMODE_VOXELS;
  176. if (imguiCheck("Walkable Voxels", m_drawMode == DRAWMODE_VOXELS_WALKABLE, valid[DRAWMODE_VOXELS_WALKABLE]))
  177. m_drawMode = DRAWMODE_VOXELS_WALKABLE;
  178. if (imguiCheck("Compact", m_drawMode == DRAWMODE_COMPACT, valid[DRAWMODE_COMPACT]))
  179. m_drawMode = DRAWMODE_COMPACT;
  180. if (imguiCheck("Compact Distance", m_drawMode == DRAWMODE_COMPACT_DISTANCE, valid[DRAWMODE_COMPACT_DISTANCE]))
  181. m_drawMode = DRAWMODE_COMPACT_DISTANCE;
  182. if (imguiCheck("Compact Regions", m_drawMode == DRAWMODE_COMPACT_REGIONS, valid[DRAWMODE_COMPACT_REGIONS]))
  183. m_drawMode = DRAWMODE_COMPACT_REGIONS;
  184. if (imguiCheck("Region Connections", m_drawMode == DRAWMODE_REGION_CONNECTIONS, valid[DRAWMODE_REGION_CONNECTIONS]))
  185. m_drawMode = DRAWMODE_REGION_CONNECTIONS;
  186. if (imguiCheck("Raw Contours", m_drawMode == DRAWMODE_RAW_CONTOURS, valid[DRAWMODE_RAW_CONTOURS]))
  187. m_drawMode = DRAWMODE_RAW_CONTOURS;
  188. if (imguiCheck("Both Contours", m_drawMode == DRAWMODE_BOTH_CONTOURS, valid[DRAWMODE_BOTH_CONTOURS]))
  189. m_drawMode = DRAWMODE_BOTH_CONTOURS;
  190. if (imguiCheck("Contours", m_drawMode == DRAWMODE_CONTOURS, valid[DRAWMODE_CONTOURS]))
  191. m_drawMode = DRAWMODE_CONTOURS;
  192. if (imguiCheck("Poly Mesh", m_drawMode == DRAWMODE_POLYMESH, valid[DRAWMODE_POLYMESH]))
  193. m_drawMode = DRAWMODE_POLYMESH;
  194. if (imguiCheck("Poly Mesh Detail", m_drawMode == DRAWMODE_POLYMESH_DETAIL, valid[DRAWMODE_POLYMESH_DETAIL]))
  195. m_drawMode = DRAWMODE_POLYMESH_DETAIL;
  196. if (unavail)
  197. {
  198. imguiValue("Tick 'Keep Itermediate Results'");
  199. imguiValue("to see more debug mode options.");
  200. }
  201. }
  202. void Sample_SoloMesh::handleRender()
  203. {
  204. if (!m_geom || !m_geom->getMesh())
  205. return;
  206. glEnable(GL_FOG);
  207. glDepthMask(GL_TRUE);
  208. const float texScale = 1.0f / (m_cellSize * 10.0f);
  209. if (m_drawMode != DRAWMODE_NAVMESH_TRANS)
  210. {
  211. // Draw mesh
  212. duDebugDrawTriMeshSlope(&m_dd, m_geom->getMesh()->getVerts(), m_geom->getMesh()->getVertCount(),
  213. m_geom->getMesh()->getTris(), m_geom->getMesh()->getNormals(), m_geom->getMesh()->getTriCount(),
  214. m_agentMaxSlope, texScale);
  215. m_geom->drawOffMeshConnections(&m_dd);
  216. }
  217. glDisable(GL_FOG);
  218. glDepthMask(GL_FALSE);
  219. // Draw bounds
  220. const float* bmin = m_geom->getNavMeshBoundsMin();
  221. const float* bmax = m_geom->getNavMeshBoundsMax();
  222. duDebugDrawBoxWire(&m_dd, bmin[0],bmin[1],bmin[2], bmax[0],bmax[1],bmax[2], duRGBA(255,255,255,128), 1.0f);
  223. m_dd.begin(DU_DRAW_POINTS, 5.0f);
  224. m_dd.vertex(bmin[0],bmin[1],bmin[2],duRGBA(255,255,255,128));
  225. m_dd.end();
  226. if (m_navMesh && m_navQuery &&
  227. (m_drawMode == DRAWMODE_NAVMESH ||
  228. m_drawMode == DRAWMODE_NAVMESH_TRANS ||
  229. m_drawMode == DRAWMODE_NAVMESH_BVTREE ||
  230. m_drawMode == DRAWMODE_NAVMESH_NODES ||
  231. m_drawMode == DRAWMODE_NAVMESH_INVIS))
  232. {
  233. if (m_drawMode != DRAWMODE_NAVMESH_INVIS)
  234. duDebugDrawNavMeshWithClosedList(&m_dd, *m_navMesh, *m_navQuery, m_navMeshDrawFlags);
  235. if (m_drawMode == DRAWMODE_NAVMESH_BVTREE)
  236. duDebugDrawNavMeshBVTree(&m_dd, *m_navMesh);
  237. if (m_drawMode == DRAWMODE_NAVMESH_NODES)
  238. duDebugDrawNavMeshNodes(&m_dd, *m_navQuery);
  239. duDebugDrawNavMeshPolysWithFlags(&m_dd, *m_navMesh, SAMPLE_POLYFLAGS_DISABLED, duRGBA(0,0,0,128));
  240. }
  241. glDepthMask(GL_TRUE);
  242. if (m_chf && m_drawMode == DRAWMODE_COMPACT)
  243. duDebugDrawCompactHeightfieldSolid(&m_dd, *m_chf);
  244. if (m_chf && m_drawMode == DRAWMODE_COMPACT_DISTANCE)
  245. duDebugDrawCompactHeightfieldDistance(&m_dd, *m_chf);
  246. if (m_chf && m_drawMode == DRAWMODE_COMPACT_REGIONS)
  247. duDebugDrawCompactHeightfieldRegions(&m_dd, *m_chf);
  248. if (m_solid && m_drawMode == DRAWMODE_VOXELS)
  249. {
  250. glEnable(GL_FOG);
  251. duDebugDrawHeightfieldSolid(&m_dd, *m_solid);
  252. glDisable(GL_FOG);
  253. }
  254. if (m_solid && m_drawMode == DRAWMODE_VOXELS_WALKABLE)
  255. {
  256. glEnable(GL_FOG);
  257. duDebugDrawHeightfieldWalkable(&m_dd, *m_solid);
  258. glDisable(GL_FOG);
  259. }
  260. if (m_cset && m_drawMode == DRAWMODE_RAW_CONTOURS)
  261. {
  262. glDepthMask(GL_FALSE);
  263. duDebugDrawRawContours(&m_dd, *m_cset);
  264. glDepthMask(GL_TRUE);
  265. }
  266. if (m_cset && m_drawMode == DRAWMODE_BOTH_CONTOURS)
  267. {
  268. glDepthMask(GL_FALSE);
  269. duDebugDrawRawContours(&m_dd, *m_cset, 0.5f);
  270. duDebugDrawContours(&m_dd, *m_cset);
  271. glDepthMask(GL_TRUE);
  272. }
  273. if (m_cset && m_drawMode == DRAWMODE_CONTOURS)
  274. {
  275. glDepthMask(GL_FALSE);
  276. duDebugDrawContours(&m_dd, *m_cset);
  277. glDepthMask(GL_TRUE);
  278. }
  279. if (m_chf && m_cset && m_drawMode == DRAWMODE_REGION_CONNECTIONS)
  280. {
  281. duDebugDrawCompactHeightfieldRegions(&m_dd, *m_chf);
  282. glDepthMask(GL_FALSE);
  283. duDebugDrawRegionConnections(&m_dd, *m_cset);
  284. glDepthMask(GL_TRUE);
  285. }
  286. if (m_pmesh && m_drawMode == DRAWMODE_POLYMESH)
  287. {
  288. glDepthMask(GL_FALSE);
  289. duDebugDrawPolyMesh(&m_dd, *m_pmesh);
  290. glDepthMask(GL_TRUE);
  291. }
  292. if (m_dmesh && m_drawMode == DRAWMODE_POLYMESH_DETAIL)
  293. {
  294. glDepthMask(GL_FALSE);
  295. duDebugDrawPolyMeshDetail(&m_dd, *m_dmesh);
  296. glDepthMask(GL_TRUE);
  297. }
  298. m_geom->drawConvexVolumes(&m_dd);
  299. if (m_tool)
  300. m_tool->handleRender();
  301. renderToolStates();
  302. glDepthMask(GL_TRUE);
  303. }
  304. void Sample_SoloMesh::handleRenderOverlay(double* proj, double* model, int* view)
  305. {
  306. if (m_tool)
  307. m_tool->handleRenderOverlay(proj, model, view);
  308. renderOverlayToolStates(proj, model, view);
  309. }
  310. void Sample_SoloMesh::handleMeshChanged(class InputGeom* geom)
  311. {
  312. Sample::handleMeshChanged(geom);
  313. dtFreeNavMesh(m_navMesh);
  314. m_navMesh = 0;
  315. if (m_tool)
  316. {
  317. m_tool->reset();
  318. m_tool->init(this);
  319. }
  320. resetToolStates();
  321. initToolStates(this);
  322. }
  323. bool Sample_SoloMesh::handleBuild()
  324. {
  325. if (!m_geom || !m_geom->getMesh())
  326. {
  327. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Input mesh is not specified.");
  328. return false;
  329. }
  330. cleanup();
  331. const float* bmin = m_geom->getNavMeshBoundsMin();
  332. const float* bmax = m_geom->getNavMeshBoundsMax();
  333. const float* verts = m_geom->getMesh()->getVerts();
  334. const int nverts = m_geom->getMesh()->getVertCount();
  335. const int* tris = m_geom->getMesh()->getTris();
  336. const int ntris = m_geom->getMesh()->getTriCount();
  337. //
  338. // Step 1. Initialize build config.
  339. //
  340. // Init build configuration from GUI
  341. memset(&m_cfg, 0, sizeof(m_cfg));
  342. m_cfg.cs = m_cellSize;
  343. m_cfg.ch = m_cellHeight;
  344. m_cfg.walkableSlopeAngle = m_agentMaxSlope;
  345. m_cfg.walkableHeight = (int)ceilf(m_agentHeight / m_cfg.ch);
  346. m_cfg.walkableClimb = (int)floorf(m_agentMaxClimb / m_cfg.ch);
  347. m_cfg.walkableRadius = (int)ceilf(m_agentRadius / m_cfg.cs);
  348. m_cfg.maxEdgeLen = (int)(m_edgeMaxLen / m_cellSize);
  349. m_cfg.maxSimplificationError = m_edgeMaxError;
  350. m_cfg.minRegionArea = (int)rcSqr(m_regionMinSize); // Note: area = size*size
  351. m_cfg.mergeRegionArea = (int)rcSqr(m_regionMergeSize); // Note: area = size*size
  352. m_cfg.maxVertsPerPoly = (int)m_vertsPerPoly;
  353. m_cfg.detailSampleDist = m_detailSampleDist < 0.9f ? 0 : m_cellSize * m_detailSampleDist;
  354. m_cfg.detailSampleMaxError = m_cellHeight * m_detailSampleMaxError;
  355. // Set the area where the navigation will be build.
  356. // Here the bounds of the input mesh are used, but the
  357. // area could be specified by an user defined box, etc.
  358. rcVcopy(m_cfg.bmin, bmin);
  359. rcVcopy(m_cfg.bmax, bmax);
  360. rcCalcGridSize(m_cfg.bmin, m_cfg.bmax, m_cfg.cs, &m_cfg.width, &m_cfg.height);
  361. // Reset build times gathering.
  362. m_ctx->resetTimers();
  363. // Start the build process.
  364. m_ctx->startTimer(RC_TIMER_TOTAL);
  365. m_ctx->log(RC_LOG_PROGRESS, "Building navigation:");
  366. m_ctx->log(RC_LOG_PROGRESS, " - %d x %d cells", m_cfg.width, m_cfg.height);
  367. m_ctx->log(RC_LOG_PROGRESS, " - %.1fK verts, %.1fK tris", nverts/1000.0f, ntris/1000.0f);
  368. //
  369. // Step 2. Rasterize input polygon soup.
  370. //
  371. // Allocate voxel heightfield where we rasterize our input data to.
  372. m_solid = rcAllocHeightfield();
  373. if (!m_solid)
  374. {
  375. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'solid'.");
  376. return false;
  377. }
  378. if (!rcCreateHeightfield(m_ctx, *m_solid, m_cfg.width, m_cfg.height, m_cfg.bmin, m_cfg.bmax, m_cfg.cs, m_cfg.ch))
  379. {
  380. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not create solid heightfield.");
  381. return false;
  382. }
  383. // Allocate array that can hold triangle area types.
  384. // If you have multiple meshes you need to process, allocate
  385. // and array which can hold the max number of triangles you need to process.
  386. m_triareas = new unsigned char[ntris];
  387. if (!m_triareas)
  388. {
  389. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'm_triareas' (%d).", ntris);
  390. return false;
  391. }
  392. // Find triangles which are walkable based on their slope and rasterize them.
  393. // If your input data is multiple meshes, you can transform them here, calculate
  394. // the are type for each of the meshes and rasterize them.
  395. memset(m_triareas, 0, ntris*sizeof(unsigned char));
  396. rcMarkWalkableTriangles(m_ctx, m_cfg.walkableSlopeAngle, verts, nverts, tris, ntris, m_triareas);
  397. if (!rcRasterizeTriangles(m_ctx, verts, nverts, tris, m_triareas, ntris, *m_solid, m_cfg.walkableClimb))
  398. {
  399. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not rasterize triangles.");
  400. return false;
  401. }
  402. if (!m_keepInterResults)
  403. {
  404. delete [] m_triareas;
  405. m_triareas = 0;
  406. }
  407. //
  408. // Step 3. Filter walkables surfaces.
  409. //
  410. // Once all geoemtry is rasterized, we do initial pass of filtering to
  411. // remove unwanted overhangs caused by the conservative rasterization
  412. // as well as filter spans where the character cannot possibly stand.
  413. if (m_filterLowHangingObstacles)
  414. rcFilterLowHangingWalkableObstacles(m_ctx, m_cfg.walkableClimb, *m_solid);
  415. if (m_filterLedgeSpans)
  416. rcFilterLedgeSpans(m_ctx, m_cfg.walkableHeight, m_cfg.walkableClimb, *m_solid);
  417. if (m_filterWalkableLowHeightSpans)
  418. rcFilterWalkableLowHeightSpans(m_ctx, m_cfg.walkableHeight, *m_solid);
  419. //
  420. // Step 4. Partition walkable surface to simple regions.
  421. //
  422. // Compact the heightfield so that it is faster to handle from now on.
  423. // This will result more cache coherent data as well as the neighbours
  424. // between walkable cells will be calculated.
  425. m_chf = rcAllocCompactHeightfield();
  426. if (!m_chf)
  427. {
  428. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'chf'.");
  429. return false;
  430. }
  431. if (!rcBuildCompactHeightfield(m_ctx, m_cfg.walkableHeight, m_cfg.walkableClimb, *m_solid, *m_chf))
  432. {
  433. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not build compact data.");
  434. return false;
  435. }
  436. if (!m_keepInterResults)
  437. {
  438. rcFreeHeightField(m_solid);
  439. m_solid = 0;
  440. }
  441. // Erode the walkable area by agent radius.
  442. if (!rcErodeWalkableArea(m_ctx, m_cfg.walkableRadius, *m_chf))
  443. {
  444. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not erode.");
  445. return false;
  446. }
  447. // (Optional) Mark areas.
  448. const ConvexVolume* vols = m_geom->getConvexVolumes();
  449. for (int i = 0; i < m_geom->getConvexVolumeCount(); ++i)
  450. rcMarkConvexPolyArea(m_ctx, vols[i].verts, vols[i].nverts, vols[i].hmin, vols[i].hmax, (unsigned char)vols[i].area, *m_chf);
  451. // Partition the heightfield so that we can use simple algorithm later to triangulate the walkable areas.
  452. // There are 3 martitioning methods, each with some pros and cons:
  453. // 1) Watershed partitioning
  454. // - the classic Recast partitioning
  455. // - creates the nicest tessellation
  456. // - usually slowest
  457. // - partitions the heightfield into nice regions without holes or overlaps
  458. // - the are some corner cases where this method creates produces holes and overlaps
  459. // - holes may appear when a small obstacles is close to large open area (triangulation can handle this)
  460. // - overlaps may occur if you have narrow spiral corridors (i.e stairs), this make triangulation to fail
  461. // * generally the best choice if you precompute the nacmesh, use this if you have large open areas
  462. // 2) Monotone partioning
  463. // - fastest
  464. // - partitions the heightfield into regions without holes and overlaps (guaranteed)
  465. // - creates long thin polygons, which sometimes causes paths with detours
  466. // * use this if you want fast navmesh generation
  467. // 3) Layer partitoining
  468. // - quite fast
  469. // - partitions the heighfield into non-overlapping regions
  470. // - relies on the triangulation code to cope with holes (thus slower than monotone partitioning)
  471. // - produces better triangles than monotone partitioning
  472. // - does not have the corner cases of watershed partitioning
  473. // - can be slow and create a bit ugly tessellation (still better than monotone)
  474. // if you have large open areas with small obstacles (not a problem if you use tiles)
  475. // * good choice to use for tiled navmesh with medium and small sized tiles
  476. if (m_partitionType == SAMPLE_PARTITION_WATERSHED)
  477. {
  478. // Prepare for region partitioning, by calculating distance field along the walkable surface.
  479. if (!rcBuildDistanceField(m_ctx, *m_chf))
  480. {
  481. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not build distance field.");
  482. return false;
  483. }
  484. // Partition the walkable surface into simple regions without holes.
  485. if (!rcBuildRegions(m_ctx, *m_chf, 0, m_cfg.minRegionArea, m_cfg.mergeRegionArea))
  486. {
  487. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not build watershed regions.");
  488. return false;
  489. }
  490. }
  491. else if (m_partitionType == SAMPLE_PARTITION_MONOTONE)
  492. {
  493. // Partition the walkable surface into simple regions without holes.
  494. // Monotone partitioning does not need distancefield.
  495. if (!rcBuildRegionsMonotone(m_ctx, *m_chf, 0, m_cfg.minRegionArea, m_cfg.mergeRegionArea))
  496. {
  497. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not build monotone regions.");
  498. return false;
  499. }
  500. }
  501. else // SAMPLE_PARTITION_LAYERS
  502. {
  503. // Partition the walkable surface into simple regions without holes.
  504. if (!rcBuildLayerRegions(m_ctx, *m_chf, 0, m_cfg.minRegionArea))
  505. {
  506. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not build layer regions.");
  507. return false;
  508. }
  509. }
  510. //
  511. // Step 5. Trace and simplify region contours.
  512. //
  513. // Create contours.
  514. m_cset = rcAllocContourSet();
  515. if (!m_cset)
  516. {
  517. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'cset'.");
  518. return false;
  519. }
  520. if (!rcBuildContours(m_ctx, *m_chf, m_cfg.maxSimplificationError, m_cfg.maxEdgeLen, *m_cset))
  521. {
  522. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not create contours.");
  523. return false;
  524. }
  525. //
  526. // Step 6. Build polygons mesh from contours.
  527. //
  528. // Build polygon navmesh from the contours.
  529. m_pmesh = rcAllocPolyMesh();
  530. if (!m_pmesh)
  531. {
  532. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'pmesh'.");
  533. return false;
  534. }
  535. if (!rcBuildPolyMesh(m_ctx, *m_cset, m_cfg.maxVertsPerPoly, *m_pmesh))
  536. {
  537. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not triangulate contours.");
  538. return false;
  539. }
  540. //
  541. // Step 7. Create detail mesh which allows to access approximate height on each polygon.
  542. //
  543. m_dmesh = rcAllocPolyMeshDetail();
  544. if (!m_dmesh)
  545. {
  546. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'pmdtl'.");
  547. return false;
  548. }
  549. if (!rcBuildPolyMeshDetail(m_ctx, *m_pmesh, *m_chf, m_cfg.detailSampleDist, m_cfg.detailSampleMaxError, *m_dmesh))
  550. {
  551. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not build detail mesh.");
  552. return false;
  553. }
  554. if (!m_keepInterResults)
  555. {
  556. rcFreeCompactHeightfield(m_chf);
  557. m_chf = 0;
  558. rcFreeContourSet(m_cset);
  559. m_cset = 0;
  560. }
  561. // At this point the navigation mesh data is ready, you can access it from m_pmesh.
  562. // See duDebugDrawPolyMesh or dtCreateNavMeshData as examples how to access the data.
  563. //
  564. // (Optional) Step 8. Create Detour data from Recast poly mesh.
  565. //
  566. // The GUI may allow more max points per polygon than Detour can handle.
  567. // Only build the detour navmesh if we do not exceed the limit.
  568. if (m_cfg.maxVertsPerPoly <= DT_VERTS_PER_POLYGON)
  569. {
  570. unsigned char* navData = 0;
  571. int navDataSize = 0;
  572. // Update poly flags from areas.
  573. for (int i = 0; i < m_pmesh->npolys; ++i)
  574. {
  575. if (m_pmesh->areas[i] == RC_WALKABLE_AREA)
  576. m_pmesh->areas[i] = SAMPLE_POLYAREA_GROUND;
  577. if (m_pmesh->areas[i] == SAMPLE_POLYAREA_GROUND ||
  578. m_pmesh->areas[i] == SAMPLE_POLYAREA_GRASS ||
  579. m_pmesh->areas[i] == SAMPLE_POLYAREA_ROAD)
  580. {
  581. m_pmesh->flags[i] = SAMPLE_POLYFLAGS_WALK;
  582. }
  583. else if (m_pmesh->areas[i] == SAMPLE_POLYAREA_WATER)
  584. {
  585. m_pmesh->flags[i] = SAMPLE_POLYFLAGS_SWIM;
  586. }
  587. else if (m_pmesh->areas[i] == SAMPLE_POLYAREA_DOOR)
  588. {
  589. m_pmesh->flags[i] = SAMPLE_POLYFLAGS_WALK | SAMPLE_POLYFLAGS_DOOR;
  590. }
  591. }
  592. dtNavMeshCreateParams params;
  593. memset(&params, 0, sizeof(params));
  594. params.verts = m_pmesh->verts;
  595. params.vertCount = m_pmesh->nverts;
  596. params.polys = m_pmesh->polys;
  597. params.polyAreas = m_pmesh->areas;
  598. params.polyFlags = m_pmesh->flags;
  599. params.polyCount = m_pmesh->npolys;
  600. params.nvp = m_pmesh->nvp;
  601. params.detailMeshes = m_dmesh->meshes;
  602. params.detailVerts = m_dmesh->verts;
  603. params.detailVertsCount = m_dmesh->nverts;
  604. params.detailTris = m_dmesh->tris;
  605. params.detailTriCount = m_dmesh->ntris;
  606. params.offMeshConVerts = m_geom->getOffMeshConnectionVerts();
  607. params.offMeshConRad = m_geom->getOffMeshConnectionRads();
  608. params.offMeshConDir = m_geom->getOffMeshConnectionDirs();
  609. params.offMeshConAreas = m_geom->getOffMeshConnectionAreas();
  610. params.offMeshConFlags = m_geom->getOffMeshConnectionFlags();
  611. params.offMeshConUserID = m_geom->getOffMeshConnectionId();
  612. params.offMeshConCount = m_geom->getOffMeshConnectionCount();
  613. params.walkableHeight = m_agentHeight;
  614. params.walkableRadius = m_agentRadius;
  615. params.walkableClimb = m_agentMaxClimb;
  616. rcVcopy(params.bmin, m_pmesh->bmin);
  617. rcVcopy(params.bmax, m_pmesh->bmax);
  618. params.cs = m_cfg.cs;
  619. params.ch = m_cfg.ch;
  620. params.buildBvTree = true;
  621. if (!dtCreateNavMeshData(&params, &navData, &navDataSize))
  622. {
  623. m_ctx->log(RC_LOG_ERROR, "Could not build Detour navmesh.");
  624. return false;
  625. }
  626. m_navMesh = dtAllocNavMesh();
  627. if (!m_navMesh)
  628. {
  629. dtFree(navData);
  630. m_ctx->log(RC_LOG_ERROR, "Could not create Detour navmesh");
  631. return false;
  632. }
  633. dtStatus status;
  634. status = m_navMesh->init(navData, navDataSize, DT_TILE_FREE_DATA);
  635. if (dtStatusFailed(status))
  636. {
  637. dtFree(navData);
  638. m_ctx->log(RC_LOG_ERROR, "Could not init Detour navmesh");
  639. return false;
  640. }
  641. status = m_navQuery->init(m_navMesh, 2048);
  642. if (dtStatusFailed(status))
  643. {
  644. m_ctx->log(RC_LOG_ERROR, "Could not init Detour navmesh query");
  645. return false;
  646. }
  647. }
  648. m_ctx->stopTimer(RC_TIMER_TOTAL);
  649. // Show performance stats.
  650. duLogBuildTimes(*m_ctx, m_ctx->getAccumulatedTime(RC_TIMER_TOTAL));
  651. m_ctx->log(RC_LOG_PROGRESS, ">> Polymesh: %d vertices %d polygons", m_pmesh->nverts, m_pmesh->npolys);
  652. m_totalBuildTimeMs = m_ctx->getAccumulatedTime(RC_TIMER_TOTAL)/1000.0f;
  653. if (m_tool)
  654. m_tool->init(this);
  655. initToolStates(this);
  656. return true;
  657. }