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