Skip to content

Commit

Permalink
Update generate TileMap by RecashMesh
Browse files Browse the repository at this point in the history
  • Loading branch information
Pham Hong Duc committed Nov 26, 2024
1 parent 5c45241 commit 2585473
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 15 deletions.
21 changes: 17 additions & 4 deletions Projects/Recast/Include/Recast.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,23 +166,36 @@ class rcContext
/// @param[in] category The category of the message.
/// @param[in] msg The formatted message.
/// @param[in] len The length of the formatted message.
virtual void doLog(const rcLogCategory category, const char* msg, const int len) { rcIgnoreUnused(category); rcIgnoreUnused(msg); rcIgnoreUnused(len); }
virtual void doLog(const rcLogCategory category, const char* msg, const int len)
{
// rcIgnoreUnused(category); rcIgnoreUnused(msg); rcIgnoreUnused(len);
}

/// Clears all timers. (Resets all to unused.)
virtual void doResetTimers() {}

/// Starts the specified performance timer.
/// @param[in] label The category of timer.
virtual void doStartTimer(const rcTimerLabel label) { rcIgnoreUnused(label); }
virtual void doStartTimer(const rcTimerLabel label)
{
// rcIgnoreUnused(label);
}

/// Stops the specified performance timer.
/// @param[in] label The category of the timer.
virtual void doStopTimer(const rcTimerLabel label) { rcIgnoreUnused(label); }
virtual void doStopTimer(const rcTimerLabel label)
{
// rcIgnoreUnused(label);
}

/// Returns the total accumulated time of the specified performance timer.
/// @param[in] label The category of the timer.
/// @return The accumulated time of the timer, or -1 if timers are disabled or the timer has never been started.
virtual int doGetAccumulatedTime(const rcTimerLabel label) const { rcIgnoreUnused(label); return -1; }
virtual int doGetAccumulatedTime(const rcTimerLabel label) const
{
// rcIgnoreUnused(label);
return -1;
}

/// True if logging is enabled.
bool m_logEnabled;
Expand Down
10 changes: 7 additions & 3 deletions Projects/Skylicht/Graph/RecastMesh/CRecastBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,14 @@ namespace Skylicht
}
}
}


buffer->recalculateBoundingBox();

output->addMeshBuffer(buffer, "default");
output->recalculateBoundingBox();

buffer->drop();

obstacle->clear();
core::vector3df seg[2];

Expand Down Expand Up @@ -312,4 +316,4 @@ namespace Skylicht
return true;
}
}
}
}
53 changes: 51 additions & 2 deletions Projects/Skylicht/Graph/TileMap/CTileMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ namespace Skylicht
int nX = (int)ceilf(size.X / tileWidth);
int nZ = (int)ceilf(size.Z / tileWidth);

core::vector3df boxSize(tileWidth, tileHeight, tileWidth);

for (int y = 0; y <= nY; y++)
{
for (int z = 0; z <= nZ; z++)
Expand All @@ -64,13 +66,60 @@ namespace Skylicht
tile->X = x;
tile->Y = y;
tile->Z = z;
tile->Position = bbox.MinEdge + core::vector3df(x * tileWidth, y * tileHeight, z * tileWidth);
tile->BBox.MinEdge = bbox.MinEdge + core::vector3df(x * tileWidth, y * tileHeight, z * tileWidth);
tile->BBox.MaxEdge = tile->BBox.MinEdge + boxSize;
tile->Position = tile->BBox.getCenter();
m_tiles.push_back(tile);
}
}
}
}

void CTileMap::generate(float tileWidth, float tileHeight, CMesh* recastMesh)
{
generate(tileWidth, tileHeight, recastMesh->getBoundingBox());

// check used tile
IMeshBuffer* mb = recastMesh->getMeshBuffer(0);
IVertexBuffer* vb = mb->getVertexBuffer();
IIndexBuffer* ib = mb->getIndexBuffer();

for (u32 i = 0, n = ib->getIndexCount(); i < n; i += 3)
{
u32 ixA = ib->getIndex(i);
u32 ixB = ib->getIndex(i + 1);
u32 ixC = ib->getIndex(i + 2);

S3DVertex *a = (S3DVertex*)vb->getVertex(ixA);
S3DVertex *b = (S3DVertex*)vb->getVertex(ixB);
S3DVertex *c = (S3DVertex*)vb->getVertex(ixC);

core::aabbox3df box;
box.reset(a->Pos);
box.addInternalPoint(b->Pos);
box.addInternalPoint(c->Pos);

core::triangle3df tri(a->Pos, b->Pos, c->Pos);

for (u32 j = 0, m = m_tiles.size(); j < m; j++)
{
if (m_tiles[j]->BBox.intersectsWithBox(box))
{
m_tiles[j]->Tris.push_back(tri);
}
}
}

for (int i = (int)m_tiles.size() - 1; i >= 0; i--)
{
if (m_tiles[i]->Tris.size() == 0)
{
delete m_tiles[i];
m_tiles.erase(i);
}
}
}

void CTileMap::release()
{
for (u32 i = 0, n = m_tiles.size(); i < n; i++)
Expand All @@ -80,4 +129,4 @@ namespace Skylicht
m_tiles.clear();
}
}
}
}
12 changes: 9 additions & 3 deletions Projects/Skylicht/Graph/TileMap/CTileMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace Skylicht
int Y;
int Z;
core::vector3df Position;
core::aabbox3df BBox;
core::array<SObstacleSegment> Cols;
core::array<core::triangle3df> Tris;

Expand Down Expand Up @@ -67,8 +68,8 @@ namespace Skylicht

virtual ~CTileMap();

void generate(float tileWidth, float tileHeight, const core::aabbox3df& bbox);

void generate(float tileWidth, float tileHeight, CMesh* recastMesh);
void release();

inline float getTileWidth()
Expand All @@ -85,6 +86,11 @@ namespace Skylicht
{
return m_tiles;
}

protected:

void generate(float tileWidth, float tileHeight, const core::aabbox3df& bbox);

};
}
}
}
23 changes: 22 additions & 1 deletion Samples/Graph/Source/CDemoNavMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ CDemoNavMesh::CDemoNavMesh(CZone* zone) :
{
m_builder = new Graph::CRecastBuilder();
m_obstacle = new Graph::CObstacleAvoidance();
m_tileMap = new Graph::CTileMap();
m_outputMesh = new CMesh();
}

Expand All @@ -29,6 +30,7 @@ CDemoNavMesh::~CDemoNavMesh()
delete m_builder;
delete m_obstacle;
delete m_outputMesh;
delete m_tileMap;
}

void CDemoNavMesh::init()
Expand Down Expand Up @@ -86,6 +88,7 @@ void CDemoNavMesh::update()

SColor red(255, 255, 0, 0);
SColor white(255, 100, 100, 100);
SColor green(255, 0,255,0);

// draw recast polymesh
for (u32 i = 0, n = m_outputMesh->getMeshBufferCount(); i < n; i++)
Expand Down Expand Up @@ -116,6 +119,14 @@ void CDemoNavMesh::update()
{
debug->addLine(segments[i].Begin, segments[i].End, red);
}

// draw tilemap
core::array<Graph::STile*>& tiles = m_tileMap->getTiles();
for (u32 i = 0, n = tiles.size(); i < n; i++)
{
Graph::STile* t = tiles[i];
debug->addPosition(t->Position, 0.2f, green);
}
}

void CDemoNavMesh::onGUI()
Expand All @@ -125,6 +136,11 @@ void CDemoNavMesh::onGUI()
buildNavMesh();
}

if (ImGui::Button("Build TileMap"))
{
buildTileMap();
}

ImGui::Spacing();
ImGui::Spacing();
}
Expand All @@ -137,4 +153,9 @@ void CDemoNavMesh::onLeftClickPosition(bool holdShift, const core::vector3df& po
void CDemoNavMesh::buildNavMesh()
{
m_builder->build(m_recastMesh, m_outputMesh, m_obstacle);
}
}

void CDemoNavMesh::buildTileMap()
{
m_tileMap->generate(4.0, 4.0, m_outputMesh);
}
8 changes: 6 additions & 2 deletions Samples/Graph/Source/CDemoNavMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "ObstacleAvoidance/CObstacleAvoidance.h"
#include "RecastMesh/CRecastMesh.h"
#include "RecastMesh/CRecastBuilder.h"
#include "TileMap/CTileMap.h"

class CDemoNavMesh : public CDemo
{
Expand All @@ -17,7 +18,8 @@ class CDemoNavMesh : public CDemo
Graph::CRecastMesh* m_recastMesh;
Graph::CRecastBuilder* m_builder;
Graph::CObstacleAvoidance* m_obstacle;

Graph::CTileMap* m_tileMap;

CMesh* m_outputMesh;

public:
Expand All @@ -36,4 +38,6 @@ class CDemoNavMesh : public CDemo
virtual void onLeftClickPosition(bool holdShift, const core::vector3df& pos);

void buildNavMesh();
};

void buildTileMap();
};

0 comments on commit 2585473

Please sign in to comment.