#define _USE_MATH_DEFINES #include #include #include #include "SDL.h" #include "SDL_Opengl.h" #include "imgui.h" #include "Sample.h" #include "Sample_StatMeshSimple.h" #include "Recast.h" #include "RecastTimer.h" #include "RecastDebugDraw.h" #include "DetourStatNavMesh.h" #include "DetourStatNavMeshBuilder.h" #include "DetourDebugDraw.h" #ifdef WIN32 # define snprintf _snprintf #endif Sample_StatMeshSimple::Sample_StatMeshSimple() : m_keepInterResults(false), m_triflags(0), m_solid(0), m_chf(0), m_cset(0), m_polyMesh(0), m_drawMode(DRAWMODE_NAVMESH) { } Sample_StatMeshSimple::~Sample_StatMeshSimple() { cleanup(); } void Sample_StatMeshSimple::cleanup() { delete [] m_triflags; m_triflags = 0; delete m_solid; m_solid = 0; delete m_chf; m_chf = 0; delete m_cset; m_cset = 0; delete m_polyMesh; m_polyMesh = 0; toolCleanup(); } void Sample_StatMeshSimple::handleSettings() { Sample::handleCommonSettings(); if (imguiCheck("Keep Itermediate Results", m_keepInterResults)) m_keepInterResults = !m_keepInterResults; imguiSeparator(); } void Sample_StatMeshSimple::handleDebugMode() { // Check which modes are valid. bool valid[MAX_DRAWMODE]; for (int i = 0; i < MAX_DRAWMODE; ++i) valid[i] = false; if (m_verts && m_tris) { valid[DRAWMODE_NAVMESH] = m_navMesh != 0; valid[DRAWMODE_NAVMESH_TRANS] = m_navMesh != 0; valid[DRAWMODE_NAVMESH_BVTREE] = m_navMesh != 0; valid[DRAWMODE_NAVMESH_INVIS] = m_navMesh != 0; valid[DRAWMODE_MESH] = true; valid[DRAWMODE_VOXELS] = m_solid != 0; valid[DRAWMODE_VOXELS_WALKABLE] = m_solid != 0; valid[DRAWMODE_COMPACT] = m_chf != 0; valid[DRAWMODE_COMPACT_DISTANCE] = m_chf != 0; valid[DRAWMODE_COMPACT_REGIONS] = m_chf != 0; valid[DRAWMODE_REGION_CONNECTIONS] = m_cset != 0; valid[DRAWMODE_RAW_CONTOURS] = m_cset != 0; valid[DRAWMODE_BOTH_CONTOURS] = m_cset != 0; valid[DRAWMODE_CONTOURS] = m_cset != 0; valid[DRAWMODE_POLYMESH] = m_polyMesh != 0; } int unavail = 0; for (int i = 0; i < MAX_DRAWMODE; ++i) if (!valid[i]) unavail++; if (unavail == MAX_DRAWMODE) return; imguiLabel("Draw"); if (imguiCheck("Input Mesh", m_drawMode == DRAWMODE_MESH, valid[DRAWMODE_MESH])) m_drawMode = DRAWMODE_MESH; if (imguiCheck("Navmesh", m_drawMode == DRAWMODE_NAVMESH, valid[DRAWMODE_NAVMESH])) m_drawMode = DRAWMODE_NAVMESH; if (imguiCheck("Navmesh Invis", m_drawMode == DRAWMODE_NAVMESH_INVIS, valid[DRAWMODE_NAVMESH_INVIS])) m_drawMode = DRAWMODE_NAVMESH_INVIS; if (imguiCheck("Navmesh Trans", m_drawMode == DRAWMODE_NAVMESH_TRANS, valid[DRAWMODE_NAVMESH_TRANS])) m_drawMode = DRAWMODE_NAVMESH_TRANS; if (imguiCheck("Navmesh BVTree", m_drawMode == DRAWMODE_NAVMESH_BVTREE, valid[DRAWMODE_NAVMESH_BVTREE])) m_drawMode = DRAWMODE_NAVMESH_BVTREE; if (imguiCheck("Voxels", m_drawMode == DRAWMODE_VOXELS, valid[DRAWMODE_VOXELS])) m_drawMode = DRAWMODE_VOXELS; if (imguiCheck("Walkable Voxels", m_drawMode == DRAWMODE_VOXELS_WALKABLE, valid[DRAWMODE_VOXELS_WALKABLE])) m_drawMode = DRAWMODE_VOXELS_WALKABLE; if (imguiCheck("Compact", m_drawMode == DRAWMODE_COMPACT, valid[DRAWMODE_COMPACT])) m_drawMode = DRAWMODE_COMPACT; if (imguiCheck("Compact Distance", m_drawMode == DRAWMODE_COMPACT_DISTANCE, valid[DRAWMODE_COMPACT_DISTANCE])) m_drawMode = DRAWMODE_COMPACT_DISTANCE; if (imguiCheck("Compact Regions", m_drawMode == DRAWMODE_COMPACT_REGIONS, valid[DRAWMODE_COMPACT_REGIONS])) m_drawMode = DRAWMODE_COMPACT_REGIONS; if (imguiCheck("Region Connections", m_drawMode == DRAWMODE_REGION_CONNECTIONS, valid[DRAWMODE_REGION_CONNECTIONS])) m_drawMode = DRAWMODE_REGION_CONNECTIONS; if (imguiCheck("Raw Contours", m_drawMode == DRAWMODE_RAW_CONTOURS, valid[DRAWMODE_RAW_CONTOURS])) m_drawMode = DRAWMODE_RAW_CONTOURS; if (imguiCheck("Both Contours", m_drawMode == DRAWMODE_BOTH_CONTOURS, valid[DRAWMODE_BOTH_CONTOURS])) m_drawMode = DRAWMODE_BOTH_CONTOURS; if (imguiCheck("Contours", m_drawMode == DRAWMODE_CONTOURS, valid[DRAWMODE_CONTOURS])) m_drawMode = DRAWMODE_CONTOURS; if (imguiCheck("Poly Mesh", m_drawMode == DRAWMODE_POLYMESH, valid[DRAWMODE_POLYMESH])) m_drawMode = DRAWMODE_POLYMESH; if (unavail) { imguiValue("Tick 'Keep Itermediate Results'"); imguiValue("to see more debug mode options."); } } void Sample_StatMeshSimple::handleRender() { if (!m_verts || !m_tris || !m_trinorms) return; float col[4]; glEnable(GL_FOG); glDepthMask(GL_TRUE); if (m_drawMode == DRAWMODE_MESH) { // Draw mesh rcDebugDrawMeshSlope(m_verts, m_nverts, m_tris, m_trinorms, m_ntris, m_agentMaxSlope); } else if (m_drawMode != DRAWMODE_NAVMESH_TRANS) { // Draw mesh rcDebugDrawMesh(m_verts, m_nverts, m_tris, m_trinorms, m_ntris, 0); } glDisable(GL_FOG); glDepthMask(GL_FALSE); // Draw bounds col[0] = 1; col[1] = 1; col[2] = 1; col[3] = 0.5f; rcDebugDrawBoxWire(m_bmin[0],m_bmin[1],m_bmin[2], m_bmax[0],m_bmax[1],m_bmax[2], col); if (m_navMesh && (m_drawMode == DRAWMODE_NAVMESH || m_drawMode == DRAWMODE_NAVMESH_TRANS || m_drawMode == DRAWMODE_NAVMESH_BVTREE || m_drawMode == DRAWMODE_NAVMESH_INVIS)) { int flags = NAVMESH_TOOLS; if (m_drawMode != DRAWMODE_NAVMESH_INVIS) flags |= NAVMESH_POLYS; if (m_drawMode == DRAWMODE_NAVMESH_BVTREE) flags |= NAVMESH_BVTREE; toolRender(flags); } glDepthMask(GL_TRUE); if (m_chf && m_drawMode == DRAWMODE_COMPACT) rcDebugDrawCompactHeightfieldSolid(*m_chf); if (m_chf && m_drawMode == DRAWMODE_COMPACT_DISTANCE) rcDebugDrawCompactHeightfieldDistance(*m_chf); if (m_chf && m_drawMode == DRAWMODE_COMPACT_REGIONS) rcDebugDrawCompactHeightfieldRegions(*m_chf); if (m_solid && m_drawMode == DRAWMODE_VOXELS) { glEnable(GL_FOG); rcDebugDrawHeightfieldSolid(*m_solid); glDisable(GL_FOG); } if (m_solid && m_drawMode == DRAWMODE_VOXELS_WALKABLE) { glEnable(GL_FOG); rcDebugDrawHeightfieldWalkable(*m_solid); glDisable(GL_FOG); } if (m_cset && m_drawMode == DRAWMODE_RAW_CONTOURS) { glDepthMask(GL_FALSE); rcDebugDrawRawContours(*m_cset, m_cfg.bmin, m_cfg.cs, m_cfg.ch); glDepthMask(GL_TRUE); } if (m_cset && m_drawMode == DRAWMODE_BOTH_CONTOURS) { glDepthMask(GL_FALSE); rcDebugDrawRawContours(*m_cset, m_cfg.bmin, m_cfg.cs, m_cfg.ch, 0.5f); rcDebugDrawContours(*m_cset, m_cfg.bmin, m_cfg.cs, m_cfg.ch); glDepthMask(GL_TRUE); } if (m_cset && m_drawMode == DRAWMODE_CONTOURS) { glDepthMask(GL_FALSE); rcDebugDrawContours(*m_cset, m_cfg.bmin, m_cfg.cs, m_cfg.ch); glDepthMask(GL_TRUE); } if (m_chf && m_cset && m_drawMode == DRAWMODE_REGION_CONNECTIONS) { rcDebugDrawCompactHeightfieldRegions(*m_chf); glDepthMask(GL_FALSE); rcDebugDrawRegionConnections(*m_cset, m_cfg.bmin, m_cfg.cs, m_cfg.ch); glDepthMask(GL_TRUE); } if (m_polyMesh && m_drawMode == DRAWMODE_POLYMESH) { glDepthMask(GL_FALSE); rcDebugDrawPolyMesh(*m_polyMesh); glDepthMask(GL_TRUE); } static const float startCol[4] = { 0.5f, 0.1f, 0.0f, 0.75f }; static const float endCol[4] = { 0.2f, 0.4f, 0.0f, 0.75f }; if (m_sposSet) drawAgent(m_spos, m_agentRadius, m_agentHeight, m_agentMaxClimb, startCol); if (m_eposSet) drawAgent(m_epos, m_agentRadius, m_agentHeight, m_agentMaxClimb, endCol); } void Sample_StatMeshSimple::handleRenderOverlay(double* proj, double* model, int* view) { toolRenderOverlay(proj, model, view); } void Sample_StatMeshSimple::handleMeshChanged(const float* verts, int nverts, const int* tris, const float* trinorms, int ntris, const float* bmin, const float* bmax) { Sample::handleMeshChanged(verts, nverts, tris, trinorms, ntris, bmin, bmax); toolCleanup(); toolReset(); } bool Sample_StatMeshSimple::handleBuild() { if (!m_verts || ! m_tris) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Input mesh is not specified."); return false; } cleanup(); toolCleanup(); // // Step 1. Initialize build config. // // Init build configuration from GUI memset(&m_cfg, 0, sizeof(m_cfg)); m_cfg.cs = m_cellSize; m_cfg.ch = m_cellHeight; m_cfg.walkableSlopeAngle = m_agentMaxSlope; m_cfg.walkableHeight = (int)ceilf(m_agentHeight / m_cfg.ch); m_cfg.walkableClimb = (int)ceilf(m_agentMaxClimb / m_cfg.ch); m_cfg.walkableRadius = (int)ceilf(m_agentRadius / m_cfg.cs); m_cfg.maxEdgeLen = (int)(m_edgeMaxLen / m_cellSize); m_cfg.maxSimplificationError = m_edgeMaxError; m_cfg.minRegionSize = (int)rcSqr(m_regionMinSize); m_cfg.mergeRegionSize = (int)rcSqr(m_regionMergeSize); m_cfg.maxVertsPerPoly = (int)m_vertsPerPoly; // Set the area where the navigation will be build. // Here the bounds of the input mesh are used, but the // area could be specified by an user defined box, etc. vcopy(m_cfg.bmin, m_bmin); vcopy(m_cfg.bmax, m_bmax); rcCalcGridSize(m_cfg.bmin, m_cfg.bmax, m_cfg.cs, &m_cfg.width, &m_cfg.height); // Reset build times gathering. memset(&m_buildTimes, 0, sizeof(m_buildTimes)); rcSetBuildTimes(&m_buildTimes); // Start the build process. rcTimeVal totStartTime = rcGetPerformanceTimer(); if (rcGetLog()) { rcGetLog()->log(RC_LOG_PROGRESS, "Building navigation:"); rcGetLog()->log(RC_LOG_PROGRESS, " - %d x %d cells", m_cfg.width, m_cfg.height); rcGetLog()->log(RC_LOG_PROGRESS, " - %.1fK verts, %.1fK tris", m_nverts/1000.0f, m_ntris/1000.0f); } // // Step 2. Rasterize input polygon soup. // // Allocate voxel heighfield where we rasterize our input data to. m_solid = new rcHeightfield; if (!m_solid) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'solid'."); return false; } if (!rcCreateHeightfield(*m_solid, m_cfg.width, m_cfg.height, m_cfg.bmin, m_cfg.bmax, m_cfg.cs, m_cfg.ch)) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Could not create solid heightfield."); return false; } // Allocate array that can hold triangle flags. // If you have multiple meshes you need to process, allocate // and array which can hold the max number of triangles you need to process. m_triflags = new unsigned char[m_ntris]; if (!m_triflags) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'triangleFlags' (%d).", m_ntris); return false; } // Find triangles which are walkable based on their slope and rasterize them. // If your input data is multiple meshes, you can transform them here, calculate // the flags for each of the meshes and rasterize them. memset(m_triflags, 0, m_ntris*sizeof(unsigned char)); rcMarkWalkableTriangles(m_cfg.walkableSlopeAngle, m_verts, m_nverts, m_tris, m_ntris, m_triflags); rcRasterizeTriangles(m_verts, m_nverts, m_tris, m_triflags, m_ntris, *m_solid); if (!m_keepInterResults) { delete [] m_triflags; m_triflags = 0; } // // Step 3. Filter walkables surfaces. // // Once all geoemtry is rasterized, we do initial pass of filtering to // remove unwanted overhangs caused by the conservative rasterization // as well as filter spans where the character cannot possibly stand. rcFilterLedgeSpans(m_cfg.walkableHeight, m_cfg.walkableClimb, *m_solid); rcFilterWalkableLowHeightSpans(m_cfg.walkableHeight, *m_solid); // // Step 4. Partition walkable surface to simple regions. // // Compact the heightfield so that it is faster to handle from now on. // This will result more cache coherent data as well as the neighbours // between walkable cells will be calculated. m_chf = new rcCompactHeightfield; if (!m_chf) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'chf'."); return false; } if (!rcBuildCompactHeightfield(m_cfg.walkableHeight, m_cfg.walkableClimb, RC_WALKABLE, *m_solid, *m_chf)) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Could not build compact data."); return false; } if (!m_keepInterResults) { delete m_solid; m_solid = 0; } // Prepare for region partitioning, by calculating distance field along the walkable surface. if (!rcBuildDistanceField(*m_chf)) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Could not build distance field."); return false; } // Partition the walkable surface into simple regions without holes. if (!rcBuildRegions(*m_chf, m_cfg.walkableRadius, m_cfg.borderSize, m_cfg.minRegionSize, m_cfg.mergeRegionSize)) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Could not build regions."); } // // Step 5. Trace and simplify region contours. // // Create contours. m_cset = new rcContourSet; if (!m_cset) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'cset'."); return false; } if (!rcBuildContours(*m_chf, m_cfg.maxSimplificationError, m_cfg.maxEdgeLen, *m_cset)) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Could not create contours."); return false; } if (!m_keepInterResults) { delete m_chf; m_chf = 0; } // // Step 6. Build polygons mesh from contours. // // Build polygon navmesh from the contours. m_polyMesh = new rcPolyMesh; if (!m_polyMesh) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'polyMesh'."); return false; } if (!rcBuildPolyMesh(*m_cset, m_cfg.bmin, m_cfg.bmax, m_cfg.cs, m_cfg.ch, m_cfg.maxVertsPerPoly, *m_polyMesh)) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Could not triangulate contours."); return false; } if (!m_keepInterResults) { delete m_cset; m_cset = 0; } // At this point the navigation mesh data is ready, you can access it from m_polyMesh. // See rcDebugDrawPolyMesh or dtCreateNavMeshData as examples how to access the data. // // (Optional) Step 7. Create Detour data from detour poly mesh. // // The GUI may allow more max points per polygon than Detour can handle. // Only build the detour navmesh if we do not exceed the limit. if (m_cfg.maxVertsPerPoly <= DT_STAT_VERTS_PER_POLYGON) { unsigned char* navData = 0; int navDataSize = 0; if (!dtCreateNavMeshData(m_polyMesh->verts, m_polyMesh->nverts, m_polyMesh->polys, m_polyMesh->npolys, m_polyMesh->nvp, m_cfg.bmin, m_cfg.bmax, m_cfg.cs, m_cfg.ch, &navData, &navDataSize)) { if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "Could not build Detour navmesh."); return false; } m_navMesh = new dtStatNavMesh; if (!m_navMesh) { delete [] navData; if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "Could not create Detour navmesh"); return false; } if (!m_navMesh->init(navData, navDataSize, true)) { delete [] navData; if (rcGetLog()) rcGetLog()->log(RC_LOG_ERROR, "Could not init Detour navmesh"); return false; } } rcTimeVal totEndTime = rcGetPerformanceTimer(); // Show performance stats. if (rcGetLog()) { const float pc = 100.0f / rcGetDeltaTimeUsec(totStartTime, totEndTime); rcGetLog()->log(RC_LOG_PROGRESS, "Rasterize: %.1fms (%.1f%%)", m_buildTimes.rasterizeTriangles/1000.0f, m_buildTimes.rasterizeTriangles*pc); rcGetLog()->log(RC_LOG_PROGRESS, "Build Compact: %.1fms (%.1f%%)", m_buildTimes.buildCompact/1000.0f, m_buildTimes.buildCompact*pc); rcGetLog()->log(RC_LOG_PROGRESS, "Filter Border: %.1fms (%.1f%%)", m_buildTimes.filterBorder/1000.0f, m_buildTimes.filterBorder*pc); rcGetLog()->log(RC_LOG_PROGRESS, "Filter Walkable: %.1fms (%.1f%%)", m_buildTimes.filterWalkable/1000.0f, m_buildTimes.filterWalkable*pc); rcGetLog()->log(RC_LOG_PROGRESS, "Filter Reachable: %.1fms (%.1f%%)", m_buildTimes.filterMarkReachable/1000.0f, m_buildTimes.filterMarkReachable*pc); rcGetLog()->log(RC_LOG_PROGRESS, "Build Distancefield: %.1fms (%.1f%%)", m_buildTimes.buildDistanceField/1000.0f, m_buildTimes.buildDistanceField*pc); rcGetLog()->log(RC_LOG_PROGRESS, " - distance: %.1fms (%.1f%%)", m_buildTimes.buildDistanceFieldDist/1000.0f, m_buildTimes.buildDistanceFieldDist*pc); rcGetLog()->log(RC_LOG_PROGRESS, " - blur: %.1fms (%.1f%%)", m_buildTimes.buildDistanceFieldBlur/1000.0f, m_buildTimes.buildDistanceFieldBlur*pc); rcGetLog()->log(RC_LOG_PROGRESS, "Build Regions: %.1fms (%.1f%%)", m_buildTimes.buildRegions/1000.0f, m_buildTimes.buildRegions*pc); rcGetLog()->log(RC_LOG_PROGRESS, " - watershed: %.1fms (%.1f%%)", m_buildTimes.buildRegionsReg/1000.0f, m_buildTimes.buildRegionsReg*pc); rcGetLog()->log(RC_LOG_PROGRESS, " - expand: %.1fms (%.1f%%)", m_buildTimes.buildRegionsExp/1000.0f, m_buildTimes.buildRegionsExp*pc); rcGetLog()->log(RC_LOG_PROGRESS, " - find catchment basins: %.1fms (%.1f%%)", m_buildTimes.buildRegionsFlood/1000.0f, m_buildTimes.buildRegionsFlood*pc); rcGetLog()->log(RC_LOG_PROGRESS, " - filter: %.1fms (%.1f%%)", m_buildTimes.buildRegionsFilter/1000.0f, m_buildTimes.buildRegionsFilter*pc); rcGetLog()->log(RC_LOG_PROGRESS, "Build Contours: %.1fms (%.1f%%)", m_buildTimes.buildContours/1000.0f, m_buildTimes.buildContours*pc); rcGetLog()->log(RC_LOG_PROGRESS, " - trace: %.1fms (%.1f%%)", m_buildTimes.buildContoursTrace/1000.0f, m_buildTimes.buildContoursTrace*pc); rcGetLog()->log(RC_LOG_PROGRESS, " - simplify: %.1fms (%.1f%%)", m_buildTimes.buildContoursSimplify/1000.0f, m_buildTimes.buildContoursSimplify*pc); rcGetLog()->log(RC_LOG_PROGRESS, "Fixup contours: %.1fms (%.1f%%)", m_buildTimes.fixupContours/1000.0f, m_buildTimes.fixupContours*pc); rcGetLog()->log(RC_LOG_PROGRESS, "Build Polymesh: %.1fms (%.1f%%)", m_buildTimes.buildPolymesh/1000.0f, m_buildTimes.buildPolymesh*pc); rcGetLog()->log(RC_LOG_PROGRESS, "Polymesh: Verts:%d Polys:%d", m_polyMesh->nverts, m_polyMesh->npolys); rcGetLog()->log(RC_LOG_PROGRESS, "TOTAL: %.1fms", rcGetDeltaTimeUsec(totStartTime, totEndTime)/1000.0f); } toolRecalc(); return true; }