1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-19 23:23:52 +00:00
openmw-tes3mp/components/detournavigator/recastmeshmanager.cpp

60 lines
1.9 KiB
C++
Raw Normal View History

2018-03-13 22:49:08 +00:00
#include "recastmeshmanager.hpp"
#include <BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h>
namespace DetourNavigator
{
RecastMeshManager::RecastMeshManager(const Settings& settings)
: mShouldRebuild(false)
, mMeshBuilder(settings)
{}
2018-03-13 22:49:08 +00:00
bool RecastMeshManager::addObject(std::size_t id, const btHeightfieldTerrainShape& shape, const btTransform& transform)
{
if (!mObjects.insert(std::make_pair(id, Object {&shape, transform})).second)
return false;
mShouldRebuild = true;
return true;
}
bool RecastMeshManager::addObject(std::size_t id, const btConcaveShape& shape, const btTransform& transform)
{
if (!mObjects.insert(std::make_pair(id, Object {&shape, transform})).second)
return false;
mShouldRebuild = true;
return true;
}
boost::optional<RecastMeshManager::Object> RecastMeshManager::removeObject(std::size_t id)
2018-03-13 22:49:08 +00:00
{
const auto object = mObjects.find(id);
if (object == mObjects.end())
return boost::none;
const auto result = object->second;
mObjects.erase(object);
2018-03-13 22:49:08 +00:00
mShouldRebuild = true;
return result;
2018-03-13 22:49:08 +00:00
}
std::shared_ptr<RecastMesh> RecastMeshManager::getMesh()
{
rebuild();
return mMeshBuilder.create();
}
void RecastMeshManager::rebuild()
{
if (!mShouldRebuild)
return;
mMeshBuilder.reset();
for (const auto& v : mObjects)
{
if (v.second.mShape->getShapeType() == TERRAIN_SHAPE_PROXYTYPE)
mMeshBuilder.addObject(*static_cast<const btHeightfieldTerrainShape*>(v.second.mShape), v.second.mTransform);
else if (v.second.mShape->isConcave())
mMeshBuilder.addObject(*static_cast<const btConcaveShape*>(v.second.mShape), v.second.mTransform);
}
mShouldRebuild = false;
}
}