880 lines
25 KiB
C++
880 lines
25 KiB
C++
//
|
|
// Copyright (c) 2009 Mikko Mononen memon@inside.org
|
|
//
|
|
// This software is provided 'as-is', without any express or implied
|
|
// warranty. In no event will the authors be held liable for any damages
|
|
// arising from the use of this software.
|
|
// Permission is granted to anyone to use this software for any purpose,
|
|
// including commercial applications, and to alter it and redistribute it
|
|
// freely, subject to the following restrictions:
|
|
// 1. The origin of this software must not be misrepresented; you must not
|
|
// claim that you wrote the original software. If you use this software
|
|
// in a product, an acknowledgment in the product documentation would be
|
|
// appreciated but is not required.
|
|
// 2. Altered source versions must be plainly marked as such, and must not be
|
|
// misrepresented as being the original software.
|
|
// 3. This notice may not be removed or altered from any source distribution.
|
|
//
|
|
|
|
#define _USE_MATH_DEFINES
|
|
#include <math.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "SDL.h"
|
|
#include "SDL_opengl.h"
|
|
#include "imgui.h"
|
|
#include "InputGeom.h"
|
|
#include "Sample.h"
|
|
#include "Sample_TileMesh.h"
|
|
#include "Recast.h"
|
|
#include "RecastTimer.h"
|
|
#include "RecastDebugDraw.h"
|
|
#include "DetourNavMesh.h"
|
|
#include "DetourNavMeshBuilder.h"
|
|
#include "DetourDebugDraw.h"
|
|
#include "NavMeshTesterTool.h"
|
|
#include "OffMeshConnectionTool.h"
|
|
#include "BoxVolumeTool.h"
|
|
|
|
#ifdef WIN32
|
|
# define snprintf _snprintf
|
|
#endif
|
|
|
|
|
|
inline unsigned int nextPow2(unsigned int v)
|
|
{
|
|
v--;
|
|
v |= v >> 1;
|
|
v |= v >> 2;
|
|
v |= v >> 4;
|
|
v |= v >> 8;
|
|
v |= v >> 16;
|
|
v++;
|
|
return v;
|
|
}
|
|
|
|
inline unsigned int ilog2(unsigned int v)
|
|
{
|
|
unsigned int r;
|
|
unsigned int shift;
|
|
r = (v > 0xffff) << 4; v >>= r;
|
|
shift = (v > 0xff) << 3; v >>= shift; r |= shift;
|
|
shift = (v > 0xf) << 2; v >>= shift; r |= shift;
|
|
shift = (v > 0x3) << 1; v >>= shift; r |= shift;
|
|
r |= (v >> 1);
|
|
return r;
|
|
}
|
|
|
|
class NavMeshTileTool : public SampleTool
|
|
{
|
|
Sample_TileMesh* m_sample;
|
|
float m_hitPos[3];
|
|
bool m_hitPosSet;
|
|
float m_agentRadius;
|
|
|
|
public:
|
|
|
|
NavMeshTileTool() :
|
|
m_sample(0),
|
|
m_hitPosSet(false),
|
|
m_agentRadius(0)
|
|
{
|
|
}
|
|
|
|
virtual ~NavMeshTileTool()
|
|
{
|
|
}
|
|
|
|
virtual int type() { return TOOL_TILE_EDIT; }
|
|
|
|
virtual void init(Sample* sample)
|
|
{
|
|
m_sample = (Sample_TileMesh*)sample;
|
|
}
|
|
|
|
virtual void reset() {}
|
|
|
|
virtual void handleMenu()
|
|
{
|
|
imguiLabel("Create Tiles");
|
|
if (imguiButton("Create All"))
|
|
{
|
|
if (m_sample)
|
|
m_sample->buildAllTiles();
|
|
}
|
|
if (imguiButton("Remove All"))
|
|
{
|
|
if (m_sample)
|
|
m_sample->removeAllTiles();
|
|
}
|
|
imguiValue("Click LMB to create a tile.");
|
|
imguiValue("Shift+LMB to remove a tile.");
|
|
}
|
|
|
|
virtual void handleClick(const float* p, bool shift)
|
|
{
|
|
m_hitPosSet = true;
|
|
vcopy(m_hitPos,p);
|
|
if (m_sample)
|
|
{
|
|
if (shift)
|
|
m_sample->removeTile(m_hitPos);
|
|
else
|
|
m_sample->buildTile(m_hitPos);
|
|
}
|
|
}
|
|
|
|
virtual void handleRender()
|
|
{
|
|
if (m_hitPosSet)
|
|
{
|
|
const float s = m_sample->getAgentRadius();
|
|
glColor4ub(0,0,0,128);
|
|
glLineWidth(2.0f);
|
|
glBegin(GL_LINES);
|
|
glVertex3f(m_hitPos[0]-s,m_hitPos[1]+0.1f,m_hitPos[2]);
|
|
glVertex3f(m_hitPos[0]+s,m_hitPos[1]+0.1f,m_hitPos[2]);
|
|
glVertex3f(m_hitPos[0],m_hitPos[1]-s+0.1f,m_hitPos[2]);
|
|
glVertex3f(m_hitPos[0],m_hitPos[1]+s+0.1f,m_hitPos[2]);
|
|
glVertex3f(m_hitPos[0],m_hitPos[1]+0.1f,m_hitPos[2]-s);
|
|
glVertex3f(m_hitPos[0],m_hitPos[1]+0.1f,m_hitPos[2]+s);
|
|
glEnd();
|
|
glLineWidth(1.0f);
|
|
}
|
|
}
|
|
|
|
virtual void handleRenderOverlay(double* proj, double* model, int* view)
|
|
{
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
Sample_TileMesh::Sample_TileMesh() :
|
|
m_keepInterResults(false),
|
|
m_buildAll(true),
|
|
m_triflags(0),
|
|
m_solid(0),
|
|
m_chf(0),
|
|
m_cset(0),
|
|
m_pmesh(0),
|
|
m_dmesh(0),
|
|
m_maxTiles(0),
|
|
m_maxPolysPerTile(0),
|
|
m_tileSize(32),
|
|
m_tileBuildTime(0),
|
|
m_tileMemUsage(0),
|
|
m_tileTriCount(0)
|
|
{
|
|
resetCommonSettings();
|
|
memset(m_tileBmin, 0, sizeof(m_tileBmin));
|
|
memset(m_tileBmax, 0, sizeof(m_tileBmax));
|
|
|
|
setTool(new NavMeshTileTool);
|
|
}
|
|
|
|
Sample_TileMesh::~Sample_TileMesh()
|
|
{
|
|
cleanup();
|
|
delete m_navMesh;
|
|
m_navMesh = 0;
|
|
}
|
|
|
|
void Sample_TileMesh::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_pmesh;
|
|
m_pmesh = 0;
|
|
delete m_dmesh;
|
|
m_dmesh = 0;
|
|
}
|
|
|
|
void Sample_TileMesh::handleSettings()
|
|
{
|
|
Sample::handleCommonSettings();
|
|
|
|
if (imguiCheck("Keep Itermediate Results", m_keepInterResults))
|
|
m_keepInterResults = !m_keepInterResults;
|
|
|
|
if (imguiCheck("Build All Tiles", m_buildAll))
|
|
m_buildAll = !m_buildAll;
|
|
|
|
imguiLabel("Tiling");
|
|
imguiSlider("TileSize", &m_tileSize, 16.0f, 1024.0f, 16.0f);
|
|
|
|
if (m_geom)
|
|
{
|
|
const float* bmin = m_geom->getMeshBoundsMin();
|
|
const float* bmax = m_geom->getMeshBoundsMax();
|
|
char text[64];
|
|
int gw = 0, gh = 0;
|
|
rcCalcGridSize(bmin, bmax, m_cellSize, &gw, &gh);
|
|
const int ts = (int)m_tileSize;
|
|
const int tw = (gw + ts-1) / ts;
|
|
const int th = (gh + ts-1) / ts;
|
|
snprintf(text, 64, "Tiles %d x %d", tw, th);
|
|
imguiValue(text);
|
|
|
|
// Max tiles and max polys affect how the tile IDs are caculated.
|
|
// There are 22 bits available for identifying a tile and a polygon.
|
|
int tileBits = rcMin((int)ilog2(nextPow2(tw*th)), 14);
|
|
if (tileBits > 14) tileBits = 14;
|
|
int polyBits = 22 - tileBits;
|
|
m_maxTiles = 1 << tileBits;
|
|
m_maxPolysPerTile = 1 << polyBits;
|
|
snprintf(text, 64, "Max Tiles %d", m_maxTiles);
|
|
imguiValue(text);
|
|
snprintf(text, 64, "Max Polys %d", m_maxPolysPerTile);
|
|
imguiValue(text);
|
|
}
|
|
else
|
|
{
|
|
m_maxTiles = 0;
|
|
m_maxPolysPerTile = 0;
|
|
}
|
|
}
|
|
|
|
void Sample_TileMesh::handleTools()
|
|
{
|
|
int type = !m_tool ? TOOL_NONE : m_tool->type();
|
|
|
|
if (imguiCheck("Test Navmesh", type == TOOL_NAVMESH_TESTER))
|
|
{
|
|
setTool(new NavMeshTesterTool);
|
|
}
|
|
if (imguiCheck("Create Tiles", type == TOOL_TILE_EDIT))
|
|
{
|
|
setTool(new NavMeshTileTool);
|
|
}
|
|
if (imguiCheck("Create Off-Mesh Links", type == TOOL_OFFMESH_CONNECTION))
|
|
{
|
|
setTool(new OffMeshConnectionTool);
|
|
}
|
|
if (imguiCheck("Create Box Volumes", type == TOOL_BOX_VOLUME))
|
|
{
|
|
setTool(new BoxVolumeTool);
|
|
}
|
|
|
|
imguiSeparator();
|
|
|
|
if (m_tool)
|
|
m_tool->handleMenu();
|
|
}
|
|
|
|
void Sample_TileMesh::handleDebugMode()
|
|
{
|
|
if (m_navMesh)
|
|
{
|
|
imguiValue("Navmesh ready.");
|
|
imguiValue("Use 'Create Tiles' tool to experiment.");
|
|
imguiValue("LMB: (Re)Create tiles.");
|
|
imguiValue("LMB+SHIFT: Remove tiles.");
|
|
}
|
|
else
|
|
{
|
|
imguiValue("Press [Build] to create tile mesh");
|
|
imguiValue("with specified parameters.");
|
|
}
|
|
}
|
|
|
|
static void getPolyCenter(dtNavMesh* navMesh, dtPolyRef ref, float* center)
|
|
{
|
|
const dtPoly* p = navMesh->getPolyByRef(ref);
|
|
if (!p) return;
|
|
const float* verts = navMesh->getPolyVertsByRef(ref);
|
|
center[0] = 0;
|
|
center[1] = 0;
|
|
center[2] = 0;
|
|
for (int i = 0; i < (int)p->vertCount; ++i)
|
|
{
|
|
const float* v = &verts[p->verts[i]*3];
|
|
center[0] += v[0];
|
|
center[1] += v[1];
|
|
center[2] += v[2];
|
|
}
|
|
const float s = 1.0f / p->vertCount;
|
|
center[0] *= s;
|
|
center[1] *= s;
|
|
center[2] *= s;
|
|
}
|
|
|
|
void Sample_TileMesh::handleRender()
|
|
{
|
|
if (!m_geom || !m_geom->getMesh())
|
|
return;
|
|
|
|
DebugDrawGL dd;
|
|
|
|
// Draw mesh
|
|
duDebugDrawTriMesh(&dd, m_geom->getMesh()->getVerts(), m_geom->getMesh()->getVertCount(),
|
|
m_geom->getMesh()->getTris(), m_geom->getMesh()->getNormals(), m_geom->getMesh()->getTriCount(), 0);
|
|
m_geom->drawOffMeshConnections(&dd);
|
|
|
|
glDepthMask(GL_FALSE);
|
|
|
|
// Draw bounds
|
|
const float* bmin = m_geom->getMeshBoundsMin();
|
|
const float* bmax = m_geom->getMeshBoundsMax();
|
|
duDebugDrawBoxWire(&dd, bmin[0],bmin[1],bmin[2], bmax[0],bmax[1],bmax[2], duRGBA(255,255,255,128), 1.0f);
|
|
|
|
// Tiling grid.
|
|
int gw = 0, gh = 0;
|
|
rcCalcGridSize(bmin, bmax, m_cellSize, &gw, &gh);
|
|
const int tw = (gw + (int)m_tileSize-1) / (int)m_tileSize;
|
|
const int th = (gh + (int)m_tileSize-1) / (int)m_tileSize;
|
|
const float s = m_tileSize*m_cellSize;
|
|
duDebugDrawGridXZ(&dd, bmin[0],bmin[1],bmin[2], tw,th, s, duRGBA(0,0,0,64), 1.0f);
|
|
|
|
// Draw active tile
|
|
duDebugDrawBoxWire(&dd, m_tileBmin[0],m_tileBmin[1],m_tileBmin[2], m_tileBmax[0],m_tileBmax[1],m_tileBmax[2], m_tileCol, 2.0f);
|
|
|
|
if (m_navMesh)
|
|
duDebugDrawNavMesh(&dd, m_navMesh, m_navMeshDrawFlags);
|
|
|
|
if (m_tool)
|
|
m_tool->handleRender();
|
|
|
|
m_geom->drawBoxVolumes(&dd);
|
|
|
|
glDepthMask(GL_TRUE);
|
|
}
|
|
|
|
void Sample_TileMesh::handleRenderOverlay(double* proj, double* model, int* view)
|
|
{
|
|
GLdouble x, y, z;
|
|
|
|
// Draw start and end point labels
|
|
if (m_tileBuildTime > 0.0f && gluProject((GLdouble)(m_tileBmin[0]+m_tileBmax[0])/2, (GLdouble)(m_tileBmin[1]+m_tileBmax[1])/2, (GLdouble)(m_tileBmin[2]+m_tileBmax[2])/2,
|
|
model, proj, view, &x, &y, &z))
|
|
{
|
|
char text[32];
|
|
snprintf(text,32,"%.3fms / %dTris / %.1fkB", m_tileBuildTime, m_tileTriCount, m_tileMemUsage);
|
|
imguiDrawText((int)x, (int)y-25, IMGUI_ALIGN_CENTER, text, imguiRGBA(0,0,0,220));
|
|
}
|
|
|
|
if (m_tool)
|
|
m_tool->handleRenderOverlay(proj, model, view);
|
|
}
|
|
|
|
void Sample_TileMesh::handleMeshChanged(class InputGeom* geom)
|
|
{
|
|
Sample::handleMeshChanged(geom);
|
|
|
|
cleanup();
|
|
|
|
delete m_navMesh;
|
|
m_navMesh = 0;
|
|
|
|
if (m_tool)
|
|
{
|
|
m_tool->reset();
|
|
m_tool->init(this);
|
|
}
|
|
}
|
|
|
|
bool Sample_TileMesh::handleBuild()
|
|
{
|
|
if (!m_geom || !m_geom->getMesh())
|
|
{
|
|
if (rcGetLog())
|
|
rcGetLog()->log(RC_LOG_ERROR, "buildTiledNavigation: No vertices and triangles.");
|
|
return false;
|
|
}
|
|
|
|
delete m_navMesh;
|
|
m_navMesh = new dtNavMesh;
|
|
if (!m_navMesh)
|
|
{
|
|
if (rcGetLog())
|
|
rcGetLog()->log(RC_LOG_ERROR, "buildTiledNavigation: Could not allocate navmesh.");
|
|
return false;
|
|
}
|
|
const float* bmin = m_geom->getMeshBoundsMin();
|
|
const float tileWorldWidth = m_tileSize*m_cellSize;
|
|
const float tileWorldHeight = m_tileSize*m_cellSize;
|
|
|
|
if (!m_navMesh->init(bmin, tileWorldWidth, tileWorldHeight, m_maxTiles, m_maxPolysPerTile, 2048))
|
|
{
|
|
if (rcGetLog())
|
|
rcGetLog()->log(RC_LOG_ERROR, "buildTiledNavigation: Could not init navmesh.");
|
|
return false;
|
|
}
|
|
|
|
if (m_buildAll)
|
|
buildAllTiles();
|
|
|
|
if (m_tool)
|
|
m_tool->init(this);
|
|
|
|
return true;
|
|
}
|
|
|
|
void Sample_TileMesh::buildTile(const float* pos)
|
|
{
|
|
if (!m_navMesh)
|
|
return;
|
|
|
|
const float* bmin = m_geom->getMeshBoundsMin();
|
|
const float* bmax = m_geom->getMeshBoundsMax();
|
|
|
|
const float ts = m_tileSize*m_cellSize;
|
|
const int tx = (int)((pos[0] - bmin[0]) / ts);
|
|
const int ty = (int)((pos[2] - bmin[2]) / ts);
|
|
|
|
m_tileBmin[0] = bmin[0] + tx*ts;
|
|
m_tileBmin[1] = bmin[1];
|
|
m_tileBmin[2] = bmin[2] + ty*ts;
|
|
|
|
m_tileBmax[0] = bmin[0] + (tx+1)*ts;
|
|
m_tileBmax[1] = bmax[1];
|
|
m_tileBmax[2] = bmin[2] + (ty+1)*ts;
|
|
|
|
m_tileCol = duRGBA(77,204,0,255);
|
|
|
|
int dataSize = 0;
|
|
unsigned char* data = buildTileMesh(m_tileBmin, m_tileBmax, dataSize);
|
|
|
|
if (data)
|
|
{
|
|
// Remove any previous data (navmesh owns and deletes the data).
|
|
m_navMesh->removeTileAt(tx,ty,0,0);
|
|
|
|
// Let the navmesh own the data.
|
|
if (!m_navMesh->addTileAt(tx,ty,data,dataSize,true))
|
|
delete [] data;
|
|
}
|
|
}
|
|
|
|
void Sample_TileMesh::removeTile(const float* pos)
|
|
{
|
|
if (!m_navMesh)
|
|
return;
|
|
|
|
const float* bmin = m_geom->getMeshBoundsMin();
|
|
const float* bmax = m_geom->getMeshBoundsMax();
|
|
|
|
const float ts = m_tileSize*m_cellSize;
|
|
const int tx = (int)((pos[0] - bmin[0]) / ts);
|
|
const int ty = (int)((pos[2] - bmin[2]) / ts);
|
|
|
|
m_tileBmin[0] = bmin[0] + tx*ts;
|
|
m_tileBmin[1] = bmin[1];
|
|
m_tileBmin[2] = bmin[2] + ty*ts;
|
|
|
|
m_tileBmax[0] = bmin[0] + (tx+1)*ts;
|
|
m_tileBmax[1] = bmax[1];
|
|
m_tileBmax[2] = bmin[2] + (ty+1)*ts;
|
|
|
|
m_tileCol = duRGBA(204,25,0,255);
|
|
|
|
unsigned char* rdata = 0;
|
|
int rdataSize = 0;
|
|
if (m_navMesh->removeTileAt(tx,ty,&rdata,&rdataSize))
|
|
delete [] rdata;
|
|
}
|
|
|
|
void Sample_TileMesh::buildAllTiles()
|
|
{
|
|
const float* bmin = m_geom->getMeshBoundsMin();
|
|
const float* bmax = m_geom->getMeshBoundsMax();
|
|
int gw = 0, gh = 0;
|
|
rcCalcGridSize(bmin, bmax, m_cellSize, &gw, &gh);
|
|
const int ts = (int)m_tileSize;
|
|
const int tw = (gw + ts-1) / ts;
|
|
const int th = (gh + ts-1) / ts;
|
|
const float tcs = m_tileSize*m_cellSize;
|
|
|
|
for (int y = 0; y < th; ++y)
|
|
{
|
|
for (int x = 0; x < tw; ++x)
|
|
{
|
|
m_tileBmin[0] = bmin[0] + x*tcs;
|
|
m_tileBmin[1] = bmin[1];
|
|
m_tileBmin[2] = bmin[2] + y*tcs;
|
|
|
|
m_tileBmax[0] = bmin[0] + (x+1)*tcs;
|
|
m_tileBmax[1] = bmax[1];
|
|
m_tileBmax[2] = bmin[2] + (y+1)*tcs;
|
|
|
|
int dataSize = 0;
|
|
unsigned char* data = buildTileMesh(m_tileBmin, m_tileBmax, dataSize);
|
|
if (data)
|
|
{
|
|
// Remove any previous data (navmesh owns and deletes the data).
|
|
m_navMesh->removeTileAt(x,y,0,0);
|
|
// Let the navmesh own the data.
|
|
if (!m_navMesh->addTileAt(x,y,data,dataSize,true))
|
|
delete [] data;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void Sample_TileMesh::removeAllTiles()
|
|
{
|
|
const float* bmin = m_geom->getMeshBoundsMin();
|
|
const float* bmax = m_geom->getMeshBoundsMax();
|
|
int gw = 0, gh = 0;
|
|
rcCalcGridSize(bmin, bmax, m_cellSize, &gw, &gh);
|
|
const int ts = (int)m_tileSize;
|
|
const int tw = (gw + ts-1) / ts;
|
|
const int th = (gh + ts-1) / ts;
|
|
|
|
for (int y = 0; y < th; ++y)
|
|
for (int x = 0; x < tw; ++x)
|
|
m_navMesh->removeTileAt(x,y,0,0);
|
|
}
|
|
|
|
unsigned char* Sample_TileMesh::buildTileMesh(const float* bmin, const float* bmax, int& dataSize)
|
|
{
|
|
if (!m_geom || !m_geom->getMesh() || !m_geom->getChunkyMesh())
|
|
{
|
|
if (rcGetLog())
|
|
rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Input mesh is not specified.");
|
|
return 0;
|
|
}
|
|
|
|
cleanup();
|
|
|
|
const float* verts = m_geom->getMesh()->getVerts();
|
|
const int nverts = m_geom->getMesh()->getVertCount();
|
|
const int ntris = m_geom->getMesh()->getTriCount();
|
|
const rcChunkyTriMesh* chunkyMesh = m_geom->getChunkyMesh();
|
|
|
|
// 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)floorf(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;
|
|
m_cfg.tileSize = (int)m_tileSize;
|
|
m_cfg.borderSize = m_cfg.walkableRadius + 3; // Reserve enough padding.
|
|
m_cfg.width = m_cfg.tileSize + m_cfg.borderSize*2;
|
|
m_cfg.height = m_cfg.tileSize + m_cfg.borderSize*2;
|
|
m_cfg.detailSampleDist = m_detailSampleDist < 0.9f ? 0 : m_cellSize * m_detailSampleDist;
|
|
m_cfg.detailSampleMaxError = m_cellHeight * m_detailSampleMaxError;
|
|
|
|
vcopy(m_cfg.bmin, bmin);
|
|
vcopy(m_cfg.bmax, bmax);
|
|
m_cfg.bmin[0] -= m_cfg.borderSize*m_cfg.cs;
|
|
m_cfg.bmin[2] -= m_cfg.borderSize*m_cfg.cs;
|
|
m_cfg.bmax[0] += m_cfg.borderSize*m_cfg.cs;
|
|
m_cfg.bmax[2] += m_cfg.borderSize*m_cfg.cs;
|
|
|
|
// 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", nverts/1000.0f, ntris/1000.0f);
|
|
}
|
|
|
|
// 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 0;
|
|
}
|
|
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 0;
|
|
}
|
|
|
|
// 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[chunkyMesh->maxTrisPerChunk];
|
|
if (!m_triflags)
|
|
{
|
|
if (rcGetLog())
|
|
rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'triangleFlags' (%d).", chunkyMesh->maxTrisPerChunk);
|
|
return 0;
|
|
}
|
|
|
|
|
|
float tbmin[2], tbmax[2];
|
|
tbmin[0] = m_cfg.bmin[0];
|
|
tbmin[1] = m_cfg.bmin[2];
|
|
tbmax[0] = m_cfg.bmax[0];
|
|
tbmax[1] = m_cfg.bmax[2];
|
|
int cid[512];// TODO: Make grow when returning too many items.
|
|
const int ncid = rcGetChunksInRect(chunkyMesh, tbmin, tbmax, cid, 512);
|
|
if (!ncid)
|
|
return 0;
|
|
|
|
m_tileTriCount = 0;
|
|
|
|
for (int i = 0; i < ncid; ++i)
|
|
{
|
|
const rcChunkyTriMeshNode& node = chunkyMesh->nodes[cid[i]];
|
|
const int* tris = &chunkyMesh->tris[node.i*3];
|
|
const int ntris = node.n;
|
|
|
|
m_tileTriCount += ntris;
|
|
|
|
memset(m_triflags, 0, ntris*sizeof(unsigned char));
|
|
rcMarkWalkableTriangles(m_cfg.walkableSlopeAngle,
|
|
verts, nverts, tris, ntris, m_triflags);
|
|
|
|
rcRasterizeTriangles(verts, nverts, tris, m_triflags, ntris, *m_solid, m_cfg.walkableClimb);
|
|
}
|
|
|
|
if (!m_keepInterResults)
|
|
{
|
|
delete [] m_triflags;
|
|
m_triflags = 0;
|
|
}
|
|
|
|
// 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.
|
|
rcFilterLowHangingWalkableObstacles(m_cfg.walkableClimb, *m_solid);
|
|
rcFilterLedgeSpans(m_cfg.walkableHeight, m_cfg.walkableClimb, *m_solid);
|
|
rcFilterWalkableLowHeightSpans(m_cfg.walkableHeight, *m_solid);
|
|
|
|
// 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 0;
|
|
}
|
|
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 0;
|
|
}
|
|
|
|
if (!m_keepInterResults)
|
|
{
|
|
delete m_solid;
|
|
m_solid = 0;
|
|
}
|
|
|
|
// Erode the walkable area by agent radius.
|
|
if (!rcErodeArea(RC_WALKABLE_AREA, m_cfg.walkableRadius, *m_chf))
|
|
{
|
|
if (rcGetLog())
|
|
rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Could not erode.");
|
|
return false;
|
|
}
|
|
|
|
const float* boxVerts = m_geom->getBoxVolumeVerts();
|
|
const unsigned char* boxTypes = m_geom->getBoxVolumeTypes();
|
|
for (int i = 0; i < m_geom->getBoxVolumeCount(); ++i)
|
|
{
|
|
const float* v = &boxVerts[i*3*2];
|
|
rcMarkBoxArea(&v[0], &v[3], boxTypes[i], *m_chf);
|
|
}
|
|
|
|
|
|
// 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 0;
|
|
}
|
|
|
|
// Partition the walkable surface into simple regions without holes.
|
|
if (!rcBuildRegions(*m_chf, m_cfg.borderSize, m_cfg.minRegionSize, m_cfg.mergeRegionSize))
|
|
{
|
|
if (rcGetLog())
|
|
rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Could not build regions.");
|
|
return 0;
|
|
}
|
|
|
|
// Create contours.
|
|
m_cset = new rcContourSet;
|
|
if (!m_cset)
|
|
{
|
|
if (rcGetLog())
|
|
rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'cset'.");
|
|
return 0;
|
|
}
|
|
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 0;
|
|
}
|
|
|
|
if (m_cset->nconts == 0)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
// Build polygon navmesh from the contours.
|
|
m_pmesh = new rcPolyMesh;
|
|
if (!m_pmesh)
|
|
{
|
|
if (rcGetLog())
|
|
rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'pmesh'.");
|
|
return 0;
|
|
}
|
|
if (!rcBuildPolyMesh(*m_cset, m_cfg.maxVertsPerPoly, *m_pmesh))
|
|
{
|
|
if (rcGetLog())
|
|
rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Could not triangulate contours.");
|
|
return 0;
|
|
}
|
|
|
|
// Build detail mesh.
|
|
m_dmesh = new rcPolyMeshDetail;
|
|
if (!m_dmesh)
|
|
{
|
|
if (rcGetLog())
|
|
rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'dmesh'.");
|
|
return 0;
|
|
}
|
|
|
|
if (!rcBuildPolyMeshDetail(*m_pmesh, *m_chf,
|
|
m_cfg.detailSampleDist, m_cfg.detailSampleMaxError,
|
|
*m_dmesh))
|
|
{
|
|
if (rcGetLog())
|
|
rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Could build polymesh detail.");
|
|
return 0;
|
|
}
|
|
|
|
if (!m_keepInterResults)
|
|
{
|
|
delete m_chf;
|
|
m_chf = 0;
|
|
delete m_cset;
|
|
m_cset = 0;
|
|
}
|
|
|
|
unsigned char* navData = 0;
|
|
int navDataSize = 0;
|
|
if (m_cfg.maxVertsPerPoly <= DT_VERTS_PER_POLYGON)
|
|
{
|
|
// Remove padding from the polymesh data. TODO: Remove this odditity.
|
|
for (int i = 0; i < m_pmesh->nverts; ++i)
|
|
{
|
|
unsigned short* v = &m_pmesh->verts[i*3];
|
|
v[0] -= (unsigned short)m_cfg.borderSize;
|
|
v[2] -= (unsigned short)m_cfg.borderSize;
|
|
}
|
|
|
|
if (m_pmesh->nverts >= 0xffff)
|
|
{
|
|
// The vertex indices are ushorts, and cannot point to more than 0xffff vertices.
|
|
if (rcGetLog())
|
|
rcGetLog()->log(RC_LOG_ERROR, "Too many vertices per tile %d (max: %d).", m_pmesh->nverts, 0xffff);
|
|
return false;
|
|
}
|
|
|
|
dtNavMeshCreateParams params;
|
|
memset(¶ms, 0, sizeof(params));
|
|
params.verts = m_pmesh->verts;
|
|
params.vertCount = m_pmesh->nverts;
|
|
params.polys = m_pmesh->polys;
|
|
params.polyCount = m_pmesh->npolys;
|
|
params.nvp = m_pmesh->nvp;
|
|
params.detailMeshes = m_dmesh->meshes;
|
|
params.detailVerts = m_dmesh->verts;
|
|
params.detailVertsCount = m_dmesh->nverts;
|
|
params.detailTris = m_dmesh->tris;
|
|
params.detailTriCount = m_dmesh->ntris;
|
|
params.offMeshConVerts = m_geom->getOffMeshConnectionVerts();
|
|
params.offMeshConRad = m_geom->getOffMeshConnectionRads();
|
|
params.offMeshConDir = m_geom->getOffMeshConnectionDirs();
|
|
params.offMeshConCount = m_geom->getOffMeshConnectionCount();
|
|
params.walkableHeight = m_agentHeight;
|
|
params.walkableRadius = m_agentRadius;
|
|
params.walkableClimb = m_agentMaxClimb;
|
|
vcopy(params.bmin, bmin);
|
|
vcopy(params.bmax, bmax);
|
|
params.cs = m_cfg.cs;
|
|
params.ch = m_cfg.ch;
|
|
params.tileSize = m_cfg.tileSize;
|
|
|
|
if (!dtCreateNavMeshData(¶ms, &navData, &navDataSize))
|
|
{
|
|
if (rcGetLog())
|
|
rcGetLog()->log(RC_LOG_ERROR, "Could not build Detour navmesh.");
|
|
return 0;
|
|
}
|
|
}
|
|
m_tileMemUsage = navDataSize/1024.0f;
|
|
|
|
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, "Erode walkable area: %.1fms (%.1f%%)", m_buildTimes.erodeArea/1000.0f, m_buildTimes.erodeArea*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, "Build Polymesh: %.1fms (%.1f%%)", m_buildTimes.buildPolymesh/1000.0f, m_buildTimes.buildPolymesh*pc);
|
|
rcGetLog()->log(RC_LOG_PROGRESS, "Build Polymesh Detail: %.1fms (%.1f%%)", m_buildTimes.buildDetailMesh/1000.0f, m_buildTimes.buildDetailMesh*pc);
|
|
rcGetLog()->log(RC_LOG_PROGRESS, "Merge Polymeshes: %.1fms (%.1f%%)", m_buildTimes.mergePolyMesh/1000.0f, m_buildTimes.mergePolyMesh*pc);
|
|
rcGetLog()->log(RC_LOG_PROGRESS, "Merge Polymesh Details: %.1fms (%.1f%%)", m_buildTimes.mergePolyMeshDetail/1000.0f, m_buildTimes.mergePolyMeshDetail*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_pmesh->nverts, m_pmesh->npolys);
|
|
|
|
rcGetLog()->log(RC_LOG_PROGRESS, "TOTAL: %.1fms", rcGetDeltaTimeUsec(totStartTime, totEndTime)/1000.0f);
|
|
}
|
|
|
|
m_tileBuildTime = rcGetDeltaTimeUsec(totStartTime, totEndTime)/1000.0f;
|
|
|
|
dataSize = navDataSize;
|
|
return navData;
|
|
}
|