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