1
This commit is contained in:
parent
d7c6c79045
commit
13a3180041
@ -105,13 +105,6 @@ struct ConvexVolume
|
|||||||
rcAreaModification areaMod;
|
rcAreaModification areaMod;
|
||||||
};
|
};
|
||||||
|
|
||||||
static int calcLayerBufferSize(const int gridWidth, const int gridHeight)
|
|
||||||
{
|
|
||||||
const int headerSize = dtAlign4(sizeof(dtTileCacheLayerHeader));
|
|
||||||
const int gridSize = gridWidth * gridHeight;
|
|
||||||
return headerSize + gridSize*4;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool checkOverlapRect(const float amin[2], const float amax[2],
|
inline bool checkOverlapRect(const float amin[2], const float amax[2],
|
||||||
const float bmin[2], const float bmax[2])
|
const float bmin[2], const float bmax[2])
|
||||||
{
|
{
|
||||||
@ -306,6 +299,8 @@ struct BuilderParams
|
|||||||
LinearAllocator* talloc = nullptr;
|
LinearAllocator* talloc = nullptr;
|
||||||
FastLZCompressor* tcomp = nullptr;
|
FastLZCompressor* tcomp = nullptr;
|
||||||
MeshProcess* tmproc = nullptr;
|
MeshProcess* tmproc = nullptr;
|
||||||
|
dtTileCache* tile_cache = nullptr;
|
||||||
|
dtNavMesh* navmesh = nullptr;
|
||||||
int gw = 0;
|
int gw = 0;
|
||||||
int gh = 0;
|
int gh = 0;
|
||||||
|
|
||||||
@ -317,87 +312,28 @@ struct BuilderParams
|
|||||||
dtNavMesh* NavMeshBuilder::Build(MapInstance* map_instance)
|
dtNavMesh* NavMeshBuilder::Build(MapInstance* map_instance)
|
||||||
{
|
{
|
||||||
BuilderParams builder_params;
|
BuilderParams builder_params;
|
||||||
// Init cache
|
InitBasic(builder_params);
|
||||||
rcCalcGridSize(builder_params.bmin,
|
|
||||||
builder_params.bmax,
|
|
||||||
builder_params.kCellSize,
|
|
||||||
&builder_params.gw,
|
|
||||||
&builder_params.gh);
|
|
||||||
builder_params.ts = (int)kTileSize;
|
|
||||||
builder_params.tw = (builder_params.gw + builder_params.ts-1) / builder_params.ts;
|
|
||||||
builder_params.th = (builder_params.gh + builder_params.ts-1) / builder_params.ts;
|
|
||||||
|
|
||||||
InitRcConfig(builder_params);
|
InitRcConfig(builder_params);
|
||||||
InitTileCacheParams(builder_params);
|
InitTileCacheParams(builder_params);
|
||||||
|
|
||||||
dtStatus status;
|
dtStatus status;
|
||||||
|
|
||||||
dtTileCache* tile_cache = dtAllocTileCache();
|
builder_params.tile_cache = dtAllocTileCache();
|
||||||
status = tile_cache->init(&builder_params.tcparams,
|
status = builder_params.tile_cache->init(&builder_params.tcparams,
|
||||||
builder_params.talloc,
|
builder_params.talloc,
|
||||||
builder_params.tcomp,
|
builder_params.tcomp,
|
||||||
builder_params.tmproc);
|
builder_params.tmproc);
|
||||||
|
|
||||||
dtNavMeshParams params;
|
dtNavMeshParams params;
|
||||||
{
|
InitNavMeshParams(builder_params, params);
|
||||||
memset(¶ms, 0, sizeof(params));
|
builder_params.navmesh = dtAllocNavMesh();
|
||||||
rcVcopy(params.orig, builder_params.bmin);
|
|
||||||
params.tileWidth = kTileSize * builder_params.kCellSize;
|
|
||||||
params.tileHeight = kTileSize * builder_params.kCellSize;
|
|
||||||
params.maxTiles = kMaxTiles;
|
|
||||||
params.maxPolys = kMaxPolysPerTile;
|
|
||||||
}
|
|
||||||
|
|
||||||
dtNavMesh* navmesh = dtAllocNavMesh();
|
status = builder_params.navmesh->init(¶ms);
|
||||||
|
|
||||||
status = navmesh->init(¶ms);
|
|
||||||
if (dtStatusFailed(status)) {
|
if (dtStatusFailed(status)) {
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
BuildTiles(builder_params);
|
||||||
int m_cacheLayerCount = 0;
|
return builder_params.navmesh;
|
||||||
int m_cacheCompressedSize = 0;
|
|
||||||
int m_cacheRawSize = 0;
|
|
||||||
|
|
||||||
for (int y = 0; y < builder_params.th; ++y) {
|
|
||||||
for (int x = 0; x < builder_params.tw; ++x) {
|
|
||||||
TileCacheData tiles[MAX_LAYERS];
|
|
||||||
memset(tiles, 0, sizeof(tiles));
|
|
||||||
int ntiles = RasterizeTileLayers(x, y, builder_params.cfg, tiles, MAX_LAYERS);
|
|
||||||
|
|
||||||
for (int i = 0; i < ntiles; ++i) {
|
|
||||||
TileCacheData* tile = &tiles[i];
|
|
||||||
status = tile_cache->addTile(tile->data, tile->dataSize, DT_COMPRESSEDTILE_FREE_DATA, 0);
|
|
||||||
if (dtStatusFailed(status))
|
|
||||||
{
|
|
||||||
dtFree(tile->data);
|
|
||||||
tile->data = 0;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_cacheLayerCount++;
|
|
||||||
m_cacheCompressedSize += tile->dataSize;
|
|
||||||
m_cacheRawSize += calcLayerBufferSize(builder_params.tcparams.width,
|
|
||||||
builder_params.tcparams.height);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build initial meshes
|
|
||||||
for (int y = 0; y < builder_params.th; ++y) {
|
|
||||||
for (int x = 0; x < builder_params.tw; ++x) {
|
|
||||||
tile_cache->buildNavMeshTilesAt(x,y, navmesh);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const dtNavMesh* nav = navmesh;
|
|
||||||
int navmeshMemUsage = 0;
|
|
||||||
for (int i = 0; i < nav->getMaxTiles(); ++i) {
|
|
||||||
const dtMeshTile* tile = nav->getTile(i);
|
|
||||||
if (tile->header) {
|
|
||||||
navmeshMemUsage += tile->dataSize;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NavMeshBuilder::OutputObjFile(MapInstance* map_instance)
|
void NavMeshBuilder::OutputObjFile(MapInstance* map_instance)
|
||||||
@ -502,6 +438,19 @@ void NavMeshBuilder::OutputObjFile(MapInstance* map_instance)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NavMeshBuilder::InitBasic(BuilderParams& builder_params)
|
||||||
|
{
|
||||||
|
// Init cache
|
||||||
|
rcCalcGridSize(builder_params.bmin,
|
||||||
|
builder_params.bmax,
|
||||||
|
builder_params.kCellSize,
|
||||||
|
&builder_params.gw,
|
||||||
|
&builder_params.gh);
|
||||||
|
builder_params.ts = (int)kTileSize;
|
||||||
|
builder_params.tw = (builder_params.gw + builder_params.ts-1) / builder_params.ts;
|
||||||
|
builder_params.th = (builder_params.gh + builder_params.ts-1) / builder_params.ts;
|
||||||
|
}
|
||||||
|
|
||||||
void NavMeshBuilder::InitRcConfig(BuilderParams& builder_params)
|
void NavMeshBuilder::InitRcConfig(BuilderParams& builder_params)
|
||||||
{
|
{
|
||||||
rcConfig& cfg = builder_params.cfg;
|
rcConfig& cfg = builder_params.cfg;
|
||||||
@ -545,6 +494,47 @@ void NavMeshBuilder::InitTileCacheParams(BuilderParams& builder_params)
|
|||||||
tcparams.maxObstacles = 128;
|
tcparams.maxObstacles = 128;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NavMeshBuilder::InitNavMeshParams(BuilderParams& builder_params, dtNavMeshParams& params)
|
||||||
|
{
|
||||||
|
memset(¶ms, 0, sizeof(params));
|
||||||
|
rcVcopy(params.orig, builder_params.bmin);
|
||||||
|
params.tileWidth = kTileSize * builder_params.kCellSize;
|
||||||
|
params.tileHeight = kTileSize * builder_params.kCellSize;
|
||||||
|
params.maxTiles = kMaxTiles;
|
||||||
|
params.maxPolys = kMaxPolysPerTile;
|
||||||
|
}
|
||||||
|
|
||||||
|
void NavMeshBuilder::BuildTiles(BuilderParams& builder_params)
|
||||||
|
{
|
||||||
|
for (int y = 0; y < builder_params.th; ++y) {
|
||||||
|
for (int x = 0; x < builder_params.tw; ++x) {
|
||||||
|
TileCacheData tiles[MAX_LAYERS];
|
||||||
|
memset(tiles, 0, sizeof(tiles));
|
||||||
|
int ntiles = RasterizeTileLayers(x, y, builder_params.cfg, tiles, MAX_LAYERS);
|
||||||
|
|
||||||
|
for (int i = 0; i < ntiles; ++i) {
|
||||||
|
TileCacheData* tile = &tiles[i];
|
||||||
|
dtStatus status = builder_params.tile_cache->addTile(tile->data,
|
||||||
|
tile->dataSize,
|
||||||
|
DT_COMPRESSEDTILE_FREE_DATA,
|
||||||
|
0);
|
||||||
|
if (dtStatusFailed(status)) {
|
||||||
|
dtFree(tile->data);
|
||||||
|
tile->data = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build initial meshes
|
||||||
|
for (int y = 0; y < builder_params.th; ++y) {
|
||||||
|
for (int x = 0; x < builder_params.tw; ++x) {
|
||||||
|
builder_params.tile_cache->buildNavMeshTilesAt(x,y, builder_params.navmesh);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int NavMeshBuilder::RasterizeTileLayers(const int tx, const int ty,
|
int NavMeshBuilder::RasterizeTileLayers(const int tx, const int ty,
|
||||||
const rcConfig& cfg,
|
const rcConfig& cfg,
|
||||||
TileCacheData* tiles,
|
TileCacheData* tiles,
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
struct rcConfig;
|
struct rcConfig;
|
||||||
struct dtTileCacheParams;
|
struct dtTileCacheParams;
|
||||||
|
struct dtNavMeshParams;
|
||||||
struct BuilderParams;
|
struct BuilderParams;
|
||||||
struct TileCacheData;
|
struct TileCacheData;
|
||||||
class dtNavMesh;
|
class dtNavMesh;
|
||||||
@ -20,8 +21,11 @@ public:
|
|||||||
void OutputObjFile(MapInstance* map_instance);
|
void OutputObjFile(MapInstance* map_instance);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void InitBasic(BuilderParams& builder_params);
|
||||||
void InitRcConfig(BuilderParams& builder_params);
|
void InitRcConfig(BuilderParams& builder_params);
|
||||||
void InitTileCacheParams(BuilderParams& builder_params);
|
void InitTileCacheParams(BuilderParams& builder_params);
|
||||||
|
void InitNavMeshParams(BuilderParams& builder_params, dtNavMeshParams& params);
|
||||||
|
void BuildTiles(BuilderParams& builder_params);
|
||||||
int RasterizeTileLayers(const int tx, const int ty,
|
int RasterizeTileLayers(const int tx, const int ty,
|
||||||
const rcConfig& cfg,
|
const rcConfig& cfg,
|
||||||
TileCacheData* tiles,
|
TileCacheData* tiles,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user