Sample_TileMesh.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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. #ifdef __APPLE__
  25. # include <OpenGL/glu.h>
  26. #else
  27. # include <GL/glu.h>
  28. #endif
  29. #include "imgui.h"
  30. #include "InputGeom.h"
  31. #include "Sample.h"
  32. #include "Sample_TileMesh.h"
  33. #include "Recast.h"
  34. #include "RecastDebugDraw.h"
  35. #include "DetourNavMesh.h"
  36. #include "DetourNavMeshBuilder.h"
  37. #include "DetourDebugDraw.h"
  38. #include "NavMeshTesterTool.h"
  39. #include "NavMeshPruneTool.h"
  40. #include "OffMeshConnectionTool.h"
  41. #include "ConvexVolumeTool.h"
  42. #include "CrowdTool.h"
  43. #ifdef WIN32
  44. # define snprintf _snprintf
  45. #endif
  46. inline unsigned int nextPow2(unsigned int v)
  47. {
  48. v--;
  49. v |= v >> 1;
  50. v |= v >> 2;
  51. v |= v >> 4;
  52. v |= v >> 8;
  53. v |= v >> 16;
  54. v++;
  55. return v;
  56. }
  57. inline unsigned int ilog2(unsigned int v)
  58. {
  59. unsigned int r;
  60. unsigned int shift;
  61. r = (v > 0xffff) << 4; v >>= r;
  62. shift = (v > 0xff) << 3; v >>= shift; r |= shift;
  63. shift = (v > 0xf) << 2; v >>= shift; r |= shift;
  64. shift = (v > 0x3) << 1; v >>= shift; r |= shift;
  65. r |= (v >> 1);
  66. return r;
  67. }
  68. class NavMeshTileTool : public SampleTool
  69. {
  70. Sample_TileMesh* m_sample;
  71. float m_hitPos[3];
  72. bool m_hitPosSet;
  73. public:
  74. NavMeshTileTool() :
  75. m_sample(0),
  76. m_hitPosSet(false)
  77. {
  78. m_hitPos[0] = m_hitPos[1] = m_hitPos[2] = 0;
  79. }
  80. virtual ~NavMeshTileTool()
  81. {
  82. }
  83. virtual int type() { return TOOL_TILE_EDIT; }
  84. virtual void init(Sample* sample)
  85. {
  86. m_sample = (Sample_TileMesh*)sample;
  87. }
  88. virtual void reset() {}
  89. virtual void handleMenu()
  90. {
  91. imguiLabel("Create Tiles");
  92. if (imguiButton("Create All"))
  93. {
  94. if (m_sample)
  95. m_sample->buildAllTiles();
  96. }
  97. if (imguiButton("Remove All"))
  98. {
  99. if (m_sample)
  100. m_sample->removeAllTiles();
  101. }
  102. }
  103. virtual void handleClick(const float* /*s*/, const float* p, bool shift)
  104. {
  105. m_hitPosSet = true;
  106. rcVcopy(m_hitPos,p);
  107. if (m_sample)
  108. {
  109. if (shift)
  110. m_sample->removeTile(m_hitPos);
  111. else
  112. m_sample->buildTile(m_hitPos);
  113. }
  114. }
  115. virtual void handleToggle() {}
  116. virtual void handleStep() {}
  117. virtual void handleUpdate(const float /*dt*/) {}
  118. virtual void handleRender()
  119. {
  120. if (m_hitPosSet)
  121. {
  122. const float s = m_sample->getAgentRadius();
  123. glColor4ub(0,0,0,128);
  124. glLineWidth(2.0f);
  125. glBegin(GL_LINES);
  126. glVertex3f(m_hitPos[0]-s,m_hitPos[1]+0.1f,m_hitPos[2]);
  127. glVertex3f(m_hitPos[0]+s,m_hitPos[1]+0.1f,m_hitPos[2]);
  128. glVertex3f(m_hitPos[0],m_hitPos[1]-s+0.1f,m_hitPos[2]);
  129. glVertex3f(m_hitPos[0],m_hitPos[1]+s+0.1f,m_hitPos[2]);
  130. glVertex3f(m_hitPos[0],m_hitPos[1]+0.1f,m_hitPos[2]-s);
  131. glVertex3f(m_hitPos[0],m_hitPos[1]+0.1f,m_hitPos[2]+s);
  132. glEnd();
  133. glLineWidth(1.0f);
  134. }
  135. }
  136. virtual void handleRenderOverlay(double* proj, double* model, int* view)
  137. {
  138. GLdouble x, y, z;
  139. if (m_hitPosSet && gluProject((GLdouble)m_hitPos[0], (GLdouble)m_hitPos[1], (GLdouble)m_hitPos[2],
  140. model, proj, view, &x, &y, &z))
  141. {
  142. int tx=0, ty=0;
  143. m_sample->getTilePos(m_hitPos, tx, ty);
  144. char text[32];
  145. snprintf(text,32,"(%d,%d)", tx,ty);
  146. imguiDrawText((int)x, (int)y-25, IMGUI_ALIGN_CENTER, text, imguiRGBA(0,0,0,220));
  147. }
  148. // Tool help
  149. const int h = view[3];
  150. imguiDrawText(280, h-40, IMGUI_ALIGN_LEFT, "LMB: Rebuild hit tile. Shift+LMB: Clear hit tile.", imguiRGBA(255,255,255,192));
  151. }
  152. };
  153. Sample_TileMesh::Sample_TileMesh() :
  154. m_keepInterResults(false),
  155. m_buildAll(true),
  156. m_totalBuildTimeMs(0),
  157. m_triareas(0),
  158. m_solid(0),
  159. m_chf(0),
  160. m_cset(0),
  161. m_pmesh(0),
  162. m_dmesh(0),
  163. m_drawMode(DRAWMODE_NAVMESH),
  164. m_maxTiles(0),
  165. m_maxPolysPerTile(0),
  166. m_tileSize(32),
  167. m_tileCol(duRGBA(0,0,0,32)),
  168. m_tileBuildTime(0),
  169. m_tileMemUsage(0),
  170. m_tileTriCount(0)
  171. {
  172. resetCommonSettings();
  173. memset(m_lastBuiltTileBmin, 0, sizeof(m_lastBuiltTileBmin));
  174. memset(m_lastBuiltTileBmax, 0, sizeof(m_lastBuiltTileBmax));
  175. setTool(new NavMeshTileTool);
  176. }
  177. Sample_TileMesh::~Sample_TileMesh()
  178. {
  179. cleanup();
  180. dtFreeNavMesh(m_navMesh);
  181. m_navMesh = 0;
  182. }
  183. void Sample_TileMesh::cleanup()
  184. {
  185. delete [] m_triareas;
  186. m_triareas = 0;
  187. rcFreeHeightField(m_solid);
  188. m_solid = 0;
  189. rcFreeCompactHeightfield(m_chf);
  190. m_chf = 0;
  191. rcFreeContourSet(m_cset);
  192. m_cset = 0;
  193. rcFreePolyMesh(m_pmesh);
  194. m_pmesh = 0;
  195. rcFreePolyMeshDetail(m_dmesh);
  196. m_dmesh = 0;
  197. }
  198. void Sample_TileMesh::handleSettings()
  199. {
  200. Sample::handleCommonSettings();
  201. if (imguiCheck("Keep Itermediate Results", m_keepInterResults))
  202. m_keepInterResults = !m_keepInterResults;
  203. if (imguiCheck("Build All Tiles", m_buildAll))
  204. m_buildAll = !m_buildAll;
  205. imguiLabel("Tiling");
  206. imguiSlider("TileSize", &m_tileSize, 16.0f, 1024.0f, 16.0f);
  207. if (m_geom)
  208. {
  209. char text[64];
  210. int gw = 0, gh = 0;
  211. const float* bmin = m_geom->getNavMeshBoundsMin();
  212. const float* bmax = m_geom->getNavMeshBoundsMax();
  213. rcCalcGridSize(bmin, bmax, m_cellSize, &gw, &gh);
  214. const int ts = (int)m_tileSize;
  215. const int tw = (gw + ts-1) / ts;
  216. const int th = (gh + ts-1) / ts;
  217. snprintf(text, 64, "Tiles %d x %d", tw, th);
  218. imguiValue(text);
  219. // Max tiles and max polys affect how the tile IDs are caculated.
  220. // There are 22 bits available for identifying a tile and a polygon.
  221. int tileBits = rcMin((int)ilog2(nextPow2(tw*th)), 14);
  222. if (tileBits > 14) tileBits = 14;
  223. int polyBits = 22 - tileBits;
  224. m_maxTiles = 1 << tileBits;
  225. m_maxPolysPerTile = 1 << polyBits;
  226. snprintf(text, 64, "Max Tiles %d", m_maxTiles);
  227. imguiValue(text);
  228. snprintf(text, 64, "Max Polys %d", m_maxPolysPerTile);
  229. imguiValue(text);
  230. }
  231. else
  232. {
  233. m_maxTiles = 0;
  234. m_maxPolysPerTile = 0;
  235. }
  236. imguiSeparator();
  237. imguiIndent();
  238. imguiIndent();
  239. if (imguiButton("Save"))
  240. {
  241. Sample::saveAll("all_tiles_navmesh.bin", m_navMesh);
  242. }
  243. if (imguiButton("Load"))
  244. {
  245. dtFreeNavMesh(m_navMesh);
  246. m_navMesh = Sample::loadAll("all_tiles_navmesh.bin");
  247. m_navQuery->init(m_navMesh, 2048);
  248. }
  249. imguiUnindent();
  250. imguiUnindent();
  251. char msg[64];
  252. snprintf(msg, 64, "Build Time: %.1fms", m_totalBuildTimeMs);
  253. imguiLabel(msg);
  254. imguiSeparator();
  255. imguiSeparator();
  256. }
  257. void Sample_TileMesh::handleTools()
  258. {
  259. int type = !m_tool ? TOOL_NONE : m_tool->type();
  260. if (imguiCheck("Test Navmesh", type == TOOL_NAVMESH_TESTER))
  261. {
  262. setTool(new NavMeshTesterTool);
  263. }
  264. if (imguiCheck("Prune Navmesh", type == TOOL_NAVMESH_PRUNE))
  265. {
  266. setTool(new NavMeshPruneTool);
  267. }
  268. if (imguiCheck("Create Tiles", type == TOOL_TILE_EDIT))
  269. {
  270. setTool(new NavMeshTileTool);
  271. }
  272. if (imguiCheck("Create Off-Mesh Links", type == TOOL_OFFMESH_CONNECTION))
  273. {
  274. setTool(new OffMeshConnectionTool);
  275. }
  276. if (imguiCheck("Create Convex Volumes", type == TOOL_CONVEX_VOLUME))
  277. {
  278. setTool(new ConvexVolumeTool);
  279. }
  280. if (imguiCheck("Create Crowds", type == TOOL_CROWD))
  281. {
  282. setTool(new CrowdTool);
  283. }
  284. imguiSeparatorLine();
  285. imguiIndent();
  286. if (m_tool)
  287. m_tool->handleMenu();
  288. imguiUnindent();
  289. }
  290. void Sample_TileMesh::handleDebugMode()
  291. {
  292. // Check which modes are valid.
  293. bool valid[MAX_DRAWMODE];
  294. for (int i = 0; i < MAX_DRAWMODE; ++i)
  295. valid[i] = false;
  296. if (m_geom)
  297. {
  298. valid[DRAWMODE_NAVMESH] = m_navMesh != 0;
  299. valid[DRAWMODE_NAVMESH_TRANS] = m_navMesh != 0;
  300. valid[DRAWMODE_NAVMESH_BVTREE] = m_navMesh != 0;
  301. valid[DRAWMODE_NAVMESH_NODES] = m_navQuery != 0;
  302. valid[DRAWMODE_NAVMESH_PORTALS] = m_navMesh != 0;
  303. valid[DRAWMODE_NAVMESH_INVIS] = m_navMesh != 0;
  304. valid[DRAWMODE_MESH] = true;
  305. valid[DRAWMODE_VOXELS] = m_solid != 0;
  306. valid[DRAWMODE_VOXELS_WALKABLE] = m_solid != 0;
  307. valid[DRAWMODE_COMPACT] = m_chf != 0;
  308. valid[DRAWMODE_COMPACT_DISTANCE] = m_chf != 0;
  309. valid[DRAWMODE_COMPACT_REGIONS] = m_chf != 0;
  310. valid[DRAWMODE_REGION_CONNECTIONS] = m_cset != 0;
  311. valid[DRAWMODE_RAW_CONTOURS] = m_cset != 0;
  312. valid[DRAWMODE_BOTH_CONTOURS] = m_cset != 0;
  313. valid[DRAWMODE_CONTOURS] = m_cset != 0;
  314. valid[DRAWMODE_POLYMESH] = m_pmesh != 0;
  315. valid[DRAWMODE_POLYMESH_DETAIL] = m_dmesh != 0;
  316. }
  317. int unavail = 0;
  318. for (int i = 0; i < MAX_DRAWMODE; ++i)
  319. if (!valid[i]) unavail++;
  320. if (unavail == MAX_DRAWMODE)
  321. return;
  322. imguiLabel("Draw");
  323. if (imguiCheck("Input Mesh", m_drawMode == DRAWMODE_MESH, valid[DRAWMODE_MESH]))
  324. m_drawMode = DRAWMODE_MESH;
  325. if (imguiCheck("Navmesh", m_drawMode == DRAWMODE_NAVMESH, valid[DRAWMODE_NAVMESH]))
  326. m_drawMode = DRAWMODE_NAVMESH;
  327. if (imguiCheck("Navmesh Invis", m_drawMode == DRAWMODE_NAVMESH_INVIS, valid[DRAWMODE_NAVMESH_INVIS]))
  328. m_drawMode = DRAWMODE_NAVMESH_INVIS;
  329. if (imguiCheck("Navmesh Trans", m_drawMode == DRAWMODE_NAVMESH_TRANS, valid[DRAWMODE_NAVMESH_TRANS]))
  330. m_drawMode = DRAWMODE_NAVMESH_TRANS;
  331. if (imguiCheck("Navmesh BVTree", m_drawMode == DRAWMODE_NAVMESH_BVTREE, valid[DRAWMODE_NAVMESH_BVTREE]))
  332. m_drawMode = DRAWMODE_NAVMESH_BVTREE;
  333. if (imguiCheck("Navmesh Nodes", m_drawMode == DRAWMODE_NAVMESH_NODES, valid[DRAWMODE_NAVMESH_NODES]))
  334. m_drawMode = DRAWMODE_NAVMESH_NODES;
  335. if (imguiCheck("Navmesh Portals", m_drawMode == DRAWMODE_NAVMESH_PORTALS, valid[DRAWMODE_NAVMESH_PORTALS]))
  336. m_drawMode = DRAWMODE_NAVMESH_PORTALS;
  337. if (imguiCheck("Voxels", m_drawMode == DRAWMODE_VOXELS, valid[DRAWMODE_VOXELS]))
  338. m_drawMode = DRAWMODE_VOXELS;
  339. if (imguiCheck("Walkable Voxels", m_drawMode == DRAWMODE_VOXELS_WALKABLE, valid[DRAWMODE_VOXELS_WALKABLE]))
  340. m_drawMode = DRAWMODE_VOXELS_WALKABLE;
  341. if (imguiCheck("Compact", m_drawMode == DRAWMODE_COMPACT, valid[DRAWMODE_COMPACT]))
  342. m_drawMode = DRAWMODE_COMPACT;
  343. if (imguiCheck("Compact Distance", m_drawMode == DRAWMODE_COMPACT_DISTANCE, valid[DRAWMODE_COMPACT_DISTANCE]))
  344. m_drawMode = DRAWMODE_COMPACT_DISTANCE;
  345. if (imguiCheck("Compact Regions", m_drawMode == DRAWMODE_COMPACT_REGIONS, valid[DRAWMODE_COMPACT_REGIONS]))
  346. m_drawMode = DRAWMODE_COMPACT_REGIONS;
  347. if (imguiCheck("Region Connections", m_drawMode == DRAWMODE_REGION_CONNECTIONS, valid[DRAWMODE_REGION_CONNECTIONS]))
  348. m_drawMode = DRAWMODE_REGION_CONNECTIONS;
  349. if (imguiCheck("Raw Contours", m_drawMode == DRAWMODE_RAW_CONTOURS, valid[DRAWMODE_RAW_CONTOURS]))
  350. m_drawMode = DRAWMODE_RAW_CONTOURS;
  351. if (imguiCheck("Both Contours", m_drawMode == DRAWMODE_BOTH_CONTOURS, valid[DRAWMODE_BOTH_CONTOURS]))
  352. m_drawMode = DRAWMODE_BOTH_CONTOURS;
  353. if (imguiCheck("Contours", m_drawMode == DRAWMODE_CONTOURS, valid[DRAWMODE_CONTOURS]))
  354. m_drawMode = DRAWMODE_CONTOURS;
  355. if (imguiCheck("Poly Mesh", m_drawMode == DRAWMODE_POLYMESH, valid[DRAWMODE_POLYMESH]))
  356. m_drawMode = DRAWMODE_POLYMESH;
  357. if (imguiCheck("Poly Mesh Detail", m_drawMode == DRAWMODE_POLYMESH_DETAIL, valid[DRAWMODE_POLYMESH_DETAIL]))
  358. m_drawMode = DRAWMODE_POLYMESH_DETAIL;
  359. if (unavail)
  360. {
  361. imguiValue("Tick 'Keep Itermediate Results'");
  362. imguiValue("rebuild some tiles to see");
  363. imguiValue("more debug mode options.");
  364. }
  365. }
  366. void Sample_TileMesh::handleRender()
  367. {
  368. if (!m_geom || !m_geom->getMesh())
  369. return;
  370. const float texScale = 1.0f / (m_cellSize * 10.0f);
  371. // Draw mesh
  372. if (m_drawMode != DRAWMODE_NAVMESH_TRANS)
  373. {
  374. // Draw mesh
  375. duDebugDrawTriMeshSlope(&m_dd, m_geom->getMesh()->getVerts(), m_geom->getMesh()->getVertCount(),
  376. m_geom->getMesh()->getTris(), m_geom->getMesh()->getNormals(), m_geom->getMesh()->getTriCount(),
  377. m_agentMaxSlope, texScale);
  378. m_geom->drawOffMeshConnections(&m_dd);
  379. }
  380. glDepthMask(GL_FALSE);
  381. // Draw bounds
  382. const float* bmin = m_geom->getNavMeshBoundsMin();
  383. const float* bmax = m_geom->getNavMeshBoundsMax();
  384. duDebugDrawBoxWire(&m_dd, bmin[0],bmin[1],bmin[2], bmax[0],bmax[1],bmax[2], duRGBA(255,255,255,128), 1.0f);
  385. // Tiling grid.
  386. int gw = 0, gh = 0;
  387. rcCalcGridSize(bmin, bmax, m_cellSize, &gw, &gh);
  388. const int tw = (gw + (int)m_tileSize-1) / (int)m_tileSize;
  389. const int th = (gh + (int)m_tileSize-1) / (int)m_tileSize;
  390. const float s = m_tileSize*m_cellSize;
  391. duDebugDrawGridXZ(&m_dd, bmin[0],bmin[1],bmin[2], tw,th, s, duRGBA(0,0,0,64), 1.0f);
  392. // Draw active tile
  393. duDebugDrawBoxWire(&m_dd, m_lastBuiltTileBmin[0],m_lastBuiltTileBmin[1],m_lastBuiltTileBmin[2],
  394. m_lastBuiltTileBmax[0],m_lastBuiltTileBmax[1],m_lastBuiltTileBmax[2], m_tileCol, 1.0f);
  395. if (m_navMesh && m_navQuery &&
  396. (m_drawMode == DRAWMODE_NAVMESH ||
  397. m_drawMode == DRAWMODE_NAVMESH_TRANS ||
  398. m_drawMode == DRAWMODE_NAVMESH_BVTREE ||
  399. m_drawMode == DRAWMODE_NAVMESH_NODES ||
  400. m_drawMode == DRAWMODE_NAVMESH_PORTALS ||
  401. m_drawMode == DRAWMODE_NAVMESH_INVIS))
  402. {
  403. if (m_drawMode != DRAWMODE_NAVMESH_INVIS)
  404. duDebugDrawNavMeshWithClosedList(&m_dd, *m_navMesh, *m_navQuery, m_navMeshDrawFlags);
  405. if (m_drawMode == DRAWMODE_NAVMESH_BVTREE)
  406. duDebugDrawNavMeshBVTree(&m_dd, *m_navMesh);
  407. if (m_drawMode == DRAWMODE_NAVMESH_PORTALS)
  408. duDebugDrawNavMeshPortals(&m_dd, *m_navMesh);
  409. if (m_drawMode == DRAWMODE_NAVMESH_NODES)
  410. duDebugDrawNavMeshNodes(&m_dd, *m_navQuery);
  411. duDebugDrawNavMeshPolysWithFlags(&m_dd, *m_navMesh, SAMPLE_POLYFLAGS_DISABLED, duRGBA(0,0,0,128));
  412. }
  413. glDepthMask(GL_TRUE);
  414. if (m_chf && m_drawMode == DRAWMODE_COMPACT)
  415. duDebugDrawCompactHeightfieldSolid(&m_dd, *m_chf);
  416. if (m_chf && m_drawMode == DRAWMODE_COMPACT_DISTANCE)
  417. duDebugDrawCompactHeightfieldDistance(&m_dd, *m_chf);
  418. if (m_chf && m_drawMode == DRAWMODE_COMPACT_REGIONS)
  419. duDebugDrawCompactHeightfieldRegions(&m_dd, *m_chf);
  420. if (m_solid && m_drawMode == DRAWMODE_VOXELS)
  421. {
  422. glEnable(GL_FOG);
  423. duDebugDrawHeightfieldSolid(&m_dd, *m_solid);
  424. glDisable(GL_FOG);
  425. }
  426. if (m_solid && m_drawMode == DRAWMODE_VOXELS_WALKABLE)
  427. {
  428. glEnable(GL_FOG);
  429. duDebugDrawHeightfieldWalkable(&m_dd, *m_solid);
  430. glDisable(GL_FOG);
  431. }
  432. if (m_cset && m_drawMode == DRAWMODE_RAW_CONTOURS)
  433. {
  434. glDepthMask(GL_FALSE);
  435. duDebugDrawRawContours(&m_dd, *m_cset);
  436. glDepthMask(GL_TRUE);
  437. }
  438. if (m_cset && m_drawMode == DRAWMODE_BOTH_CONTOURS)
  439. {
  440. glDepthMask(GL_FALSE);
  441. duDebugDrawRawContours(&m_dd, *m_cset, 0.5f);
  442. duDebugDrawContours(&m_dd, *m_cset);
  443. glDepthMask(GL_TRUE);
  444. }
  445. if (m_cset && m_drawMode == DRAWMODE_CONTOURS)
  446. {
  447. glDepthMask(GL_FALSE);
  448. duDebugDrawContours(&m_dd, *m_cset);
  449. glDepthMask(GL_TRUE);
  450. }
  451. if (m_chf && m_cset && m_drawMode == DRAWMODE_REGION_CONNECTIONS)
  452. {
  453. duDebugDrawCompactHeightfieldRegions(&m_dd, *m_chf);
  454. glDepthMask(GL_FALSE);
  455. duDebugDrawRegionConnections(&m_dd, *m_cset);
  456. glDepthMask(GL_TRUE);
  457. }
  458. if (m_pmesh && m_drawMode == DRAWMODE_POLYMESH)
  459. {
  460. glDepthMask(GL_FALSE);
  461. duDebugDrawPolyMesh(&m_dd, *m_pmesh);
  462. glDepthMask(GL_TRUE);
  463. }
  464. if (m_dmesh && m_drawMode == DRAWMODE_POLYMESH_DETAIL)
  465. {
  466. glDepthMask(GL_FALSE);
  467. duDebugDrawPolyMeshDetail(&m_dd, *m_dmesh);
  468. glDepthMask(GL_TRUE);
  469. }
  470. m_geom->drawConvexVolumes(&m_dd);
  471. if (m_tool)
  472. m_tool->handleRender();
  473. renderToolStates();
  474. glDepthMask(GL_TRUE);
  475. }
  476. void Sample_TileMesh::handleRenderOverlay(double* proj, double* model, int* view)
  477. {
  478. GLdouble x, y, z;
  479. // Draw start and end point labels
  480. if (m_tileBuildTime > 0.0f && gluProject((GLdouble)(m_lastBuiltTileBmin[0]+m_lastBuiltTileBmax[0])/2, (GLdouble)(m_lastBuiltTileBmin[1]+m_lastBuiltTileBmax[1])/2, (GLdouble)(m_lastBuiltTileBmin[2]+m_lastBuiltTileBmax[2])/2,
  481. model, proj, view, &x, &y, &z))
  482. {
  483. char text[32];
  484. snprintf(text,32,"%.3fms / %dTris / %.1fkB", m_tileBuildTime, m_tileTriCount, m_tileMemUsage);
  485. imguiDrawText((int)x, (int)y-25, IMGUI_ALIGN_CENTER, text, imguiRGBA(0,0,0,220));
  486. }
  487. if (m_tool)
  488. m_tool->handleRenderOverlay(proj, model, view);
  489. renderOverlayToolStates(proj, model, view);
  490. }
  491. void Sample_TileMesh::handleMeshChanged(InputGeom* geom)
  492. {
  493. Sample::handleMeshChanged(geom);
  494. const BuildSettings* buildSettings = geom->getBuildSettings();
  495. if (buildSettings && buildSettings->tileSize > 0)
  496. m_tileSize = buildSettings->tileSize;
  497. cleanup();
  498. dtFreeNavMesh(m_navMesh);
  499. m_navMesh = 0;
  500. if (m_tool)
  501. {
  502. m_tool->reset();
  503. m_tool->init(this);
  504. }
  505. resetToolStates();
  506. initToolStates(this);
  507. }
  508. bool Sample_TileMesh::handleBuild()
  509. {
  510. if (!m_geom || !m_geom->getMesh())
  511. {
  512. m_ctx->log(RC_LOG_ERROR, "buildTiledNavigation: No vertices and triangles.");
  513. return false;
  514. }
  515. dtFreeNavMesh(m_navMesh);
  516. m_navMesh = dtAllocNavMesh();
  517. if (!m_navMesh)
  518. {
  519. m_ctx->log(RC_LOG_ERROR, "buildTiledNavigation: Could not allocate navmesh.");
  520. return false;
  521. }
  522. dtNavMeshParams params;
  523. rcVcopy(params.orig, m_geom->getNavMeshBoundsMin());
  524. params.tileWidth = m_tileSize*m_cellSize;
  525. params.tileHeight = m_tileSize*m_cellSize;
  526. params.maxTiles = m_maxTiles;
  527. params.maxPolys = m_maxPolysPerTile;
  528. dtStatus status;
  529. status = m_navMesh->init(&params);
  530. if (dtStatusFailed(status))
  531. {
  532. m_ctx->log(RC_LOG_ERROR, "buildTiledNavigation: Could not init navmesh.");
  533. return false;
  534. }
  535. status = m_navQuery->init(m_navMesh, 2048);
  536. if (dtStatusFailed(status))
  537. {
  538. m_ctx->log(RC_LOG_ERROR, "buildTiledNavigation: Could not init Detour navmesh query");
  539. return false;
  540. }
  541. if (m_buildAll)
  542. buildAllTiles();
  543. if (m_tool)
  544. m_tool->init(this);
  545. initToolStates(this);
  546. return true;
  547. }
  548. void Sample_TileMesh::collectSettings(BuildSettings& settings)
  549. {
  550. Sample::collectSettings(settings);
  551. settings.tileSize = m_tileSize;
  552. }
  553. void Sample_TileMesh::buildTile(const float* pos)
  554. {
  555. if (!m_geom) return;
  556. if (!m_navMesh) return;
  557. const float* bmin = m_geom->getNavMeshBoundsMin();
  558. const float* bmax = m_geom->getNavMeshBoundsMax();
  559. const float ts = m_tileSize*m_cellSize;
  560. const int tx = (int)((pos[0] - bmin[0]) / ts);
  561. const int ty = (int)((pos[2] - bmin[2]) / ts);
  562. m_lastBuiltTileBmin[0] = bmin[0] + tx*ts;
  563. m_lastBuiltTileBmin[1] = bmin[1];
  564. m_lastBuiltTileBmin[2] = bmin[2] + ty*ts;
  565. m_lastBuiltTileBmax[0] = bmin[0] + (tx+1)*ts;
  566. m_lastBuiltTileBmax[1] = bmax[1];
  567. m_lastBuiltTileBmax[2] = bmin[2] + (ty+1)*ts;
  568. m_tileCol = duRGBA(255,255,255,64);
  569. m_ctx->resetLog();
  570. int dataSize = 0;
  571. unsigned char* data = buildTileMesh(tx, ty, m_lastBuiltTileBmin, m_lastBuiltTileBmax, dataSize);
  572. // Remove any previous data (navmesh owns and deletes the data).
  573. m_navMesh->removeTile(m_navMesh->getTileRefAt(tx,ty,0),0,0);
  574. // Add tile, or leave the location empty.
  575. if (data)
  576. {
  577. // Let the navmesh own the data.
  578. dtStatus status = m_navMesh->addTile(data,dataSize,DT_TILE_FREE_DATA,0,0);
  579. if (dtStatusFailed(status))
  580. dtFree(data);
  581. }
  582. m_ctx->dumpLog("Build Tile (%d,%d):", tx,ty);
  583. }
  584. void Sample_TileMesh::getTilePos(const float* pos, int& tx, int& ty)
  585. {
  586. if (!m_geom) return;
  587. const float* bmin = m_geom->getNavMeshBoundsMin();
  588. const float ts = m_tileSize*m_cellSize;
  589. tx = (int)((pos[0] - bmin[0]) / ts);
  590. ty = (int)((pos[2] - bmin[2]) / ts);
  591. }
  592. void Sample_TileMesh::removeTile(const float* pos)
  593. {
  594. if (!m_geom) return;
  595. if (!m_navMesh) return;
  596. const float* bmin = m_geom->getNavMeshBoundsMin();
  597. const float* bmax = m_geom->getNavMeshBoundsMax();
  598. const float ts = m_tileSize*m_cellSize;
  599. const int tx = (int)((pos[0] - bmin[0]) / ts);
  600. const int ty = (int)((pos[2] - bmin[2]) / ts);
  601. m_lastBuiltTileBmin[0] = bmin[0] + tx*ts;
  602. m_lastBuiltTileBmin[1] = bmin[1];
  603. m_lastBuiltTileBmin[2] = bmin[2] + ty*ts;
  604. m_lastBuiltTileBmax[0] = bmin[0] + (tx+1)*ts;
  605. m_lastBuiltTileBmax[1] = bmax[1];
  606. m_lastBuiltTileBmax[2] = bmin[2] + (ty+1)*ts;
  607. m_tileCol = duRGBA(128,32,16,64);
  608. m_navMesh->removeTile(m_navMesh->getTileRefAt(tx,ty,0),0,0);
  609. }
  610. void Sample_TileMesh::buildAllTiles()
  611. {
  612. if (!m_geom) return;
  613. if (!m_navMesh) return;
  614. const float* bmin = m_geom->getNavMeshBoundsMin();
  615. const float* bmax = m_geom->getNavMeshBoundsMax();
  616. int gw = 0, gh = 0;
  617. rcCalcGridSize(bmin, bmax, m_cellSize, &gw, &gh);
  618. const int ts = (int)m_tileSize;
  619. const int tw = (gw + ts-1) / ts;
  620. const int th = (gh + ts-1) / ts;
  621. const float tcs = m_tileSize*m_cellSize;
  622. // Start the build process.
  623. m_ctx->startTimer(RC_TIMER_TEMP);
  624. for (int y = 0; y < th; ++y)
  625. {
  626. for (int x = 0; x < tw; ++x)
  627. {
  628. m_lastBuiltTileBmin[0] = bmin[0] + x*tcs;
  629. m_lastBuiltTileBmin[1] = bmin[1];
  630. m_lastBuiltTileBmin[2] = bmin[2] + y*tcs;
  631. m_lastBuiltTileBmax[0] = bmin[0] + (x+1)*tcs;
  632. m_lastBuiltTileBmax[1] = bmax[1];
  633. m_lastBuiltTileBmax[2] = bmin[2] + (y+1)*tcs;
  634. int dataSize = 0;
  635. unsigned char* data = buildTileMesh(x, y, m_lastBuiltTileBmin, m_lastBuiltTileBmax, dataSize);
  636. if (data)
  637. {
  638. // Remove any previous data (navmesh owns and deletes the data).
  639. m_navMesh->removeTile(m_navMesh->getTileRefAt(x,y,0),0,0);
  640. // Let the navmesh own the data.
  641. dtStatus status = m_navMesh->addTile(data,dataSize,DT_TILE_FREE_DATA,0,0);
  642. if (dtStatusFailed(status))
  643. dtFree(data);
  644. }
  645. }
  646. }
  647. // Start the build process.
  648. m_ctx->stopTimer(RC_TIMER_TEMP);
  649. m_totalBuildTimeMs = m_ctx->getAccumulatedTime(RC_TIMER_TEMP)/1000.0f;
  650. }
  651. void Sample_TileMesh::removeAllTiles()
  652. {
  653. if (!m_geom || !m_navMesh)
  654. return;
  655. const float* bmin = m_geom->getNavMeshBoundsMin();
  656. const float* bmax = m_geom->getNavMeshBoundsMax();
  657. int gw = 0, gh = 0;
  658. rcCalcGridSize(bmin, bmax, m_cellSize, &gw, &gh);
  659. const int ts = (int)m_tileSize;
  660. const int tw = (gw + ts-1) / ts;
  661. const int th = (gh + ts-1) / ts;
  662. for (int y = 0; y < th; ++y)
  663. for (int x = 0; x < tw; ++x)
  664. m_navMesh->removeTile(m_navMesh->getTileRefAt(x,y,0),0,0);
  665. }
  666. unsigned char* Sample_TileMesh::buildTileMesh(const int tx, const int ty, const float* bmin, const float* bmax, int& dataSize)
  667. {
  668. if (!m_geom || !m_geom->getMesh() || !m_geom->getChunkyMesh())
  669. {
  670. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Input mesh is not specified.");
  671. return 0;
  672. }
  673. m_tileMemUsage = 0;
  674. m_tileBuildTime = 0;
  675. cleanup();
  676. const float* verts = m_geom->getMesh()->getVerts();
  677. const int nverts = m_geom->getMesh()->getVertCount();
  678. const int ntris = m_geom->getMesh()->getTriCount();
  679. const rcChunkyTriMesh* chunkyMesh = m_geom->getChunkyMesh();
  680. // Init build configuration from GUI
  681. memset(&m_cfg, 0, sizeof(m_cfg));
  682. m_cfg.cs = m_cellSize;
  683. m_cfg.ch = m_cellHeight;
  684. m_cfg.walkableSlopeAngle = m_agentMaxSlope;
  685. m_cfg.walkableHeight = (int)ceilf(m_agentHeight / m_cfg.ch);
  686. m_cfg.walkableClimb = (int)floorf(m_agentMaxClimb / m_cfg.ch);
  687. m_cfg.walkableRadius = (int)ceilf(m_agentRadius / m_cfg.cs);
  688. m_cfg.maxEdgeLen = (int)(m_edgeMaxLen / m_cellSize);
  689. m_cfg.maxSimplificationError = m_edgeMaxError;
  690. m_cfg.minRegionArea = (int)rcSqr(m_regionMinSize); // Note: area = size*size
  691. m_cfg.mergeRegionArea = (int)rcSqr(m_regionMergeSize); // Note: area = size*size
  692. m_cfg.maxVertsPerPoly = (int)m_vertsPerPoly;
  693. m_cfg.tileSize = (int)m_tileSize;
  694. m_cfg.borderSize = m_cfg.walkableRadius + 3; // Reserve enough padding.
  695. m_cfg.width = m_cfg.tileSize + m_cfg.borderSize*2;
  696. m_cfg.height = m_cfg.tileSize + m_cfg.borderSize*2;
  697. m_cfg.detailSampleDist = m_detailSampleDist < 0.9f ? 0 : m_cellSize * m_detailSampleDist;
  698. m_cfg.detailSampleMaxError = m_cellHeight * m_detailSampleMaxError;
  699. // Expand the heighfield bounding box by border size to find the extents of geometry we need to build this tile.
  700. //
  701. // This is done in order to make sure that the navmesh tiles connect correctly at the borders,
  702. // and the obstacles close to the border work correctly with the dilation process.
  703. // No polygons (or contours) will be created on the border area.
  704. //
  705. // IMPORTANT!
  706. //
  707. // :''''''''':
  708. // : +-----+ :
  709. // : | | :
  710. // : | |<--- tile to build
  711. // : | | :
  712. // : +-----+ :<-- geometry needed
  713. // :.........:
  714. //
  715. // You should use this bounding box to query your input geometry.
  716. //
  717. // For example if you build a navmesh for terrain, and want the navmesh tiles to match the terrain tile size
  718. // you will need to pass in data from neighbour terrain tiles too! In a simple case, just pass in all the 8 neighbours,
  719. // or use the bounding box below to only pass in a sliver of each of the 8 neighbours.
  720. rcVcopy(m_cfg.bmin, bmin);
  721. rcVcopy(m_cfg.bmax, bmax);
  722. m_cfg.bmin[0] -= m_cfg.borderSize*m_cfg.cs;
  723. m_cfg.bmin[2] -= m_cfg.borderSize*m_cfg.cs;
  724. m_cfg.bmax[0] += m_cfg.borderSize*m_cfg.cs;
  725. m_cfg.bmax[2] += m_cfg.borderSize*m_cfg.cs;
  726. // Reset build times gathering.
  727. m_ctx->resetTimers();
  728. // Start the build process.
  729. m_ctx->startTimer(RC_TIMER_TOTAL);
  730. m_ctx->log(RC_LOG_PROGRESS, "Building navigation:");
  731. m_ctx->log(RC_LOG_PROGRESS, " - %d x %d cells", m_cfg.width, m_cfg.height);
  732. m_ctx->log(RC_LOG_PROGRESS, " - %.1fK verts, %.1fK tris", nverts/1000.0f, ntris/1000.0f);
  733. // Allocate voxel heightfield where we rasterize our input data to.
  734. m_solid = rcAllocHeightfield();
  735. if (!m_solid)
  736. {
  737. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'solid'.");
  738. return 0;
  739. }
  740. if (!rcCreateHeightfield(m_ctx, *m_solid, m_cfg.width, m_cfg.height, m_cfg.bmin, m_cfg.bmax, m_cfg.cs, m_cfg.ch))
  741. {
  742. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not create solid heightfield.");
  743. return 0;
  744. }
  745. // Allocate array that can hold triangle flags.
  746. // If you have multiple meshes you need to process, allocate
  747. // and array which can hold the max number of triangles you need to process.
  748. m_triareas = new unsigned char[chunkyMesh->maxTrisPerChunk];
  749. if (!m_triareas)
  750. {
  751. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'm_triareas' (%d).", chunkyMesh->maxTrisPerChunk);
  752. return 0;
  753. }
  754. float tbmin[2], tbmax[2];
  755. tbmin[0] = m_cfg.bmin[0];
  756. tbmin[1] = m_cfg.bmin[2];
  757. tbmax[0] = m_cfg.bmax[0];
  758. tbmax[1] = m_cfg.bmax[2];
  759. int cid[512];// TODO: Make grow when returning too many items.
  760. const int ncid = rcGetChunksOverlappingRect(chunkyMesh, tbmin, tbmax, cid, 512);
  761. if (!ncid)
  762. return 0;
  763. m_tileTriCount = 0;
  764. for (int i = 0; i < ncid; ++i)
  765. {
  766. const rcChunkyTriMeshNode& node = chunkyMesh->nodes[cid[i]];
  767. const int* ctris = &chunkyMesh->tris[node.i*3];
  768. const int nctris = node.n;
  769. m_tileTriCount += nctris;
  770. memset(m_triareas, 0, nctris*sizeof(unsigned char));
  771. rcMarkWalkableTriangles(m_ctx, m_cfg.walkableSlopeAngle,
  772. verts, nverts, ctris, nctris, m_triareas);
  773. if (!rcRasterizeTriangles(m_ctx, verts, nverts, ctris, m_triareas, nctris, *m_solid, m_cfg.walkableClimb))
  774. return 0;
  775. }
  776. if (!m_keepInterResults)
  777. {
  778. delete [] m_triareas;
  779. m_triareas = 0;
  780. }
  781. // Once all geometry is rasterized, we do initial pass of filtering to
  782. // remove unwanted overhangs caused by the conservative rasterization
  783. // as well as filter spans where the character cannot possibly stand.
  784. if (m_filterLowHangingObstacles)
  785. rcFilterLowHangingWalkableObstacles(m_ctx, m_cfg.walkableClimb, *m_solid);
  786. if (m_filterLedgeSpans)
  787. rcFilterLedgeSpans(m_ctx, m_cfg.walkableHeight, m_cfg.walkableClimb, *m_solid);
  788. if (m_filterWalkableLowHeightSpans)
  789. rcFilterWalkableLowHeightSpans(m_ctx, m_cfg.walkableHeight, *m_solid);
  790. // Compact the heightfield so that it is faster to handle from now on.
  791. // This will result more cache coherent data as well as the neighbours
  792. // between walkable cells will be calculated.
  793. m_chf = rcAllocCompactHeightfield();
  794. if (!m_chf)
  795. {
  796. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'chf'.");
  797. return 0;
  798. }
  799. if (!rcBuildCompactHeightfield(m_ctx, m_cfg.walkableHeight, m_cfg.walkableClimb, *m_solid, *m_chf))
  800. {
  801. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not build compact data.");
  802. return 0;
  803. }
  804. if (!m_keepInterResults)
  805. {
  806. rcFreeHeightField(m_solid);
  807. m_solid = 0;
  808. }
  809. // Erode the walkable area by agent radius.
  810. if (!rcErodeWalkableArea(m_ctx, m_cfg.walkableRadius, *m_chf))
  811. {
  812. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not erode.");
  813. return 0;
  814. }
  815. // (Optional) Mark areas.
  816. const ConvexVolume* vols = m_geom->getConvexVolumes();
  817. for (int i = 0; i < m_geom->getConvexVolumeCount(); ++i)
  818. rcMarkConvexPolyArea(m_ctx, vols[i].verts, vols[i].nverts, vols[i].hmin, vols[i].hmax, (unsigned char)vols[i].area, *m_chf);
  819. // Partition the heightfield so that we can use simple algorithm later to triangulate the walkable areas.
  820. // There are 3 martitioning methods, each with some pros and cons:
  821. // 1) Watershed partitioning
  822. // - the classic Recast partitioning
  823. // - creates the nicest tessellation
  824. // - usually slowest
  825. // - partitions the heightfield into nice regions without holes or overlaps
  826. // - the are some corner cases where this method creates produces holes and overlaps
  827. // - holes may appear when a small obstacles is close to large open area (triangulation can handle this)
  828. // - overlaps may occur if you have narrow spiral corridors (i.e stairs), this make triangulation to fail
  829. // * generally the best choice if you precompute the nacmesh, use this if you have large open areas
  830. // 2) Monotone partioning
  831. // - fastest
  832. // - partitions the heightfield into regions without holes and overlaps (guaranteed)
  833. // - creates long thin polygons, which sometimes causes paths with detours
  834. // * use this if you want fast navmesh generation
  835. // 3) Layer partitoining
  836. // - quite fast
  837. // - partitions the heighfield into non-overlapping regions
  838. // - relies on the triangulation code to cope with holes (thus slower than monotone partitioning)
  839. // - produces better triangles than monotone partitioning
  840. // - does not have the corner cases of watershed partitioning
  841. // - can be slow and create a bit ugly tessellation (still better than monotone)
  842. // if you have large open areas with small obstacles (not a problem if you use tiles)
  843. // * good choice to use for tiled navmesh with medium and small sized tiles
  844. if (m_partitionType == SAMPLE_PARTITION_WATERSHED)
  845. {
  846. // Prepare for region partitioning, by calculating distance field along the walkable surface.
  847. if (!rcBuildDistanceField(m_ctx, *m_chf))
  848. {
  849. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not build distance field.");
  850. return 0;
  851. }
  852. // Partition the walkable surface into simple regions without holes.
  853. if (!rcBuildRegions(m_ctx, *m_chf, m_cfg.borderSize, m_cfg.minRegionArea, m_cfg.mergeRegionArea))
  854. {
  855. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not build watershed regions.");
  856. return 0;
  857. }
  858. }
  859. else if (m_partitionType == SAMPLE_PARTITION_MONOTONE)
  860. {
  861. // Partition the walkable surface into simple regions without holes.
  862. // Monotone partitioning does not need distancefield.
  863. if (!rcBuildRegionsMonotone(m_ctx, *m_chf, m_cfg.borderSize, m_cfg.minRegionArea, m_cfg.mergeRegionArea))
  864. {
  865. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not build monotone regions.");
  866. return 0;
  867. }
  868. }
  869. else // SAMPLE_PARTITION_LAYERS
  870. {
  871. // Partition the walkable surface into simple regions without holes.
  872. if (!rcBuildLayerRegions(m_ctx, *m_chf, m_cfg.borderSize, m_cfg.minRegionArea))
  873. {
  874. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not build layer regions.");
  875. return 0;
  876. }
  877. }
  878. // Create contours.
  879. m_cset = rcAllocContourSet();
  880. if (!m_cset)
  881. {
  882. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'cset'.");
  883. return 0;
  884. }
  885. if (!rcBuildContours(m_ctx, *m_chf, m_cfg.maxSimplificationError, m_cfg.maxEdgeLen, *m_cset))
  886. {
  887. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not create contours.");
  888. return 0;
  889. }
  890. if (m_cset->nconts == 0)
  891. {
  892. return 0;
  893. }
  894. // Build polygon navmesh from the contours.
  895. m_pmesh = rcAllocPolyMesh();
  896. if (!m_pmesh)
  897. {
  898. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'pmesh'.");
  899. return 0;
  900. }
  901. if (!rcBuildPolyMesh(m_ctx, *m_cset, m_cfg.maxVertsPerPoly, *m_pmesh))
  902. {
  903. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not triangulate contours.");
  904. return 0;
  905. }
  906. // Build detail mesh.
  907. m_dmesh = rcAllocPolyMeshDetail();
  908. if (!m_dmesh)
  909. {
  910. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'dmesh'.");
  911. return 0;
  912. }
  913. if (!rcBuildPolyMeshDetail(m_ctx, *m_pmesh, *m_chf,
  914. m_cfg.detailSampleDist, m_cfg.detailSampleMaxError,
  915. *m_dmesh))
  916. {
  917. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could build polymesh detail.");
  918. return 0;
  919. }
  920. if (!m_keepInterResults)
  921. {
  922. rcFreeCompactHeightfield(m_chf);
  923. m_chf = 0;
  924. rcFreeContourSet(m_cset);
  925. m_cset = 0;
  926. }
  927. unsigned char* navData = 0;
  928. int navDataSize = 0;
  929. if (m_cfg.maxVertsPerPoly <= DT_VERTS_PER_POLYGON)
  930. {
  931. if (m_pmesh->nverts >= 0xffff)
  932. {
  933. // The vertex indices are ushorts, and cannot point to more than 0xffff vertices.
  934. m_ctx->log(RC_LOG_ERROR, "Too many vertices per tile %d (max: %d).", m_pmesh->nverts, 0xffff);
  935. return 0;
  936. }
  937. // Update poly flags from areas.
  938. for (int i = 0; i < m_pmesh->npolys; ++i)
  939. {
  940. if (m_pmesh->areas[i] == RC_WALKABLE_AREA)
  941. m_pmesh->areas[i] = SAMPLE_POLYAREA_GROUND;
  942. if (m_pmesh->areas[i] == SAMPLE_POLYAREA_GROUND ||
  943. m_pmesh->areas[i] == SAMPLE_POLYAREA_GRASS ||
  944. m_pmesh->areas[i] == SAMPLE_POLYAREA_ROAD)
  945. {
  946. m_pmesh->flags[i] = SAMPLE_POLYFLAGS_WALK;
  947. }
  948. else if (m_pmesh->areas[i] == SAMPLE_POLYAREA_WATER)
  949. {
  950. m_pmesh->flags[i] = SAMPLE_POLYFLAGS_SWIM;
  951. }
  952. else if (m_pmesh->areas[i] == SAMPLE_POLYAREA_DOOR)
  953. {
  954. m_pmesh->flags[i] = SAMPLE_POLYFLAGS_WALK | SAMPLE_POLYFLAGS_DOOR;
  955. }
  956. }
  957. dtNavMeshCreateParams params;
  958. memset(&params, 0, sizeof(params));
  959. params.verts = m_pmesh->verts;
  960. params.vertCount = m_pmesh->nverts;
  961. params.polys = m_pmesh->polys;
  962. params.polyAreas = m_pmesh->areas;
  963. params.polyFlags = m_pmesh->flags;
  964. params.polyCount = m_pmesh->npolys;
  965. params.nvp = m_pmesh->nvp;
  966. params.detailMeshes = m_dmesh->meshes;
  967. params.detailVerts = m_dmesh->verts;
  968. params.detailVertsCount = m_dmesh->nverts;
  969. params.detailTris = m_dmesh->tris;
  970. params.detailTriCount = m_dmesh->ntris;
  971. params.offMeshConVerts = m_geom->getOffMeshConnectionVerts();
  972. params.offMeshConRad = m_geom->getOffMeshConnectionRads();
  973. params.offMeshConDir = m_geom->getOffMeshConnectionDirs();
  974. params.offMeshConAreas = m_geom->getOffMeshConnectionAreas();
  975. params.offMeshConFlags = m_geom->getOffMeshConnectionFlags();
  976. params.offMeshConUserID = m_geom->getOffMeshConnectionId();
  977. params.offMeshConCount = m_geom->getOffMeshConnectionCount();
  978. params.walkableHeight = m_agentHeight;
  979. params.walkableRadius = m_agentRadius;
  980. params.walkableClimb = m_agentMaxClimb;
  981. params.tileX = tx;
  982. params.tileY = ty;
  983. params.tileLayer = 0;
  984. rcVcopy(params.bmin, m_pmesh->bmin);
  985. rcVcopy(params.bmax, m_pmesh->bmax);
  986. params.cs = m_cfg.cs;
  987. params.ch = m_cfg.ch;
  988. params.buildBvTree = true;
  989. if (!dtCreateNavMeshData(&params, &navData, &navDataSize))
  990. {
  991. m_ctx->log(RC_LOG_ERROR, "Could not build Detour navmesh.");
  992. return 0;
  993. }
  994. }
  995. m_tileMemUsage = navDataSize/1024.0f;
  996. m_ctx->stopTimer(RC_TIMER_TOTAL);
  997. // Show performance stats.
  998. duLogBuildTimes(*m_ctx, m_ctx->getAccumulatedTime(RC_TIMER_TOTAL));
  999. m_ctx->log(RC_LOG_PROGRESS, ">> Polymesh: %d vertices %d polygons", m_pmesh->nverts, m_pmesh->npolys);
  1000. m_tileBuildTime = m_ctx->getAccumulatedTime(RC_TIMER_TOTAL)/1000.0f;
  1001. dataSize = navDataSize;
  1002. return navData;
  1003. }