1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-30 21:32:42 +00:00
OpenMW/components/detournavigator/recastmeshmanager.cpp

97 lines
3.1 KiB
C++
Raw Normal View History

2018-03-14 01:49:08 +03:00
#include "recastmeshmanager.hpp"
#include <BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h>
namespace DetourNavigator
{
2019-11-27 23:45:01 +01:00
RecastMeshManager::RecastMeshManager(const Settings& settings, const TileBounds& bounds, std::size_t generation)
: mGeneration(generation)
2018-04-16 01:07:18 +03:00
, mMeshBuilder(settings, bounds)
{
}
2018-03-14 01:49:08 +03:00
2018-09-22 18:36:57 +03:00
bool RecastMeshManager::addObject(const ObjectId id, const btCollisionShape& shape, const btTransform& transform,
2018-07-18 22:09:50 +03:00
const AreaType areaType)
2018-03-14 01:49:08 +03:00
{
const auto iterator = mObjectsOrder.emplace(mObjectsOrder.end(), RecastMeshObject(shape, transform, areaType));
if (!mObjects.emplace(id, iterator).second)
{
mObjectsOrder.erase(iterator);
2018-03-14 01:49:08 +03:00
return false;
}
2019-11-27 23:45:01 +01:00
++mRevision;
return true;
2018-03-14 01:49:08 +03:00
}
2018-09-22 18:36:57 +03:00
bool RecastMeshManager::updateObject(const ObjectId id, const btTransform& transform, const AreaType areaType)
2018-05-26 17:44:25 +03:00
{
const auto object = mObjects.find(id);
if (object == mObjects.end())
return false;
if (!object->second->update(transform, areaType))
2018-05-26 17:44:25 +03:00
return false;
2019-11-27 23:45:01 +01:00
++mRevision;
return true;
2018-05-26 17:44:25 +03:00
}
2018-09-22 18:36:57 +03:00
boost::optional<RemovedRecastMeshObject> RecastMeshManager::removeObject(const ObjectId id)
2018-03-14 01:49:08 +03:00
{
const auto object = mObjects.find(id);
if (object == mObjects.end())
return boost::none;
const RemovedRecastMeshObject result {object->second->getShape(), object->second->getTransform()};
mObjectsOrder.erase(object->second);
mObjects.erase(object);
2019-11-27 23:45:01 +01:00
++mRevision;
return result;
2018-03-14 01:49:08 +03:00
}
2018-07-20 22:11:34 +03:00
bool RecastMeshManager::addWater(const osg::Vec2i& cellPosition, const int cellSize,
const btTransform& transform)
{
const auto iterator = mWaterOrder.emplace(mWaterOrder.end(), Water {cellSize, transform});
if (!mWater.emplace(cellPosition, iterator).second)
{
mWaterOrder.erase(iterator);
2018-07-20 22:11:34 +03:00
return false;
}
2019-11-27 23:45:01 +01:00
++mRevision;
2018-07-20 22:11:34 +03:00
return true;
}
boost::optional<RecastMeshManager::Water> RecastMeshManager::removeWater(const osg::Vec2i& cellPosition)
{
const auto water = mWater.find(cellPosition);
if (water == mWater.end())
return boost::none;
2019-11-27 23:45:01 +01:00
++mRevision;
const auto result = *water->second;
mWaterOrder.erase(water->second);
2018-07-20 22:11:34 +03:00
mWater.erase(water);
return result;
}
2018-03-14 01:49:08 +03:00
std::shared_ptr<RecastMesh> RecastMeshManager::getMesh()
{
rebuild();
2019-11-27 23:45:01 +01:00
return mMeshBuilder.create(mGeneration, mLastBuildRevision);
2018-03-14 01:49:08 +03:00
}
2018-04-16 01:07:18 +03:00
bool RecastMeshManager::isEmpty() const
{
return mObjects.empty();
}
2018-03-14 01:49:08 +03:00
void RecastMeshManager::rebuild()
{
2019-11-27 23:45:01 +01:00
if (mLastBuildRevision == mRevision)
2018-03-14 01:49:08 +03:00
return;
mMeshBuilder.reset();
for (const auto& v : mWaterOrder)
mMeshBuilder.addWater(v.mCellSize, v.mTransform);
for (const auto& v : mObjectsOrder)
mMeshBuilder.addObject(v.getShape(), v.getTransform(), v.getAreaType());
2019-11-27 23:45:01 +01:00
mLastBuildRevision = mRevision;
2018-03-14 01:49:08 +03:00
}
}