1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-30 12:32:36 +00:00
OpenMW/apps/openmw/mwrender/recastmesh.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

100 lines
2.9 KiB
C++
Raw Normal View History

2019-11-27 23:45:01 +01:00
#include "recastmesh.hpp"
2022-08-16 20:06:25 +02:00
#include <algorithm>
2019-11-27 23:45:01 +01:00
#include <components/sceneutil/recastmesh.hpp>
#include <components/resource/resourcesystem.hpp>
#include <components/resource/scenemanager.hpp>
#include <components/detournavigator/settings.hpp>
#include <components/detournavigator/recastmesh.hpp>
2019-11-27 23:45:01 +01:00
#include <osg/PositionAttitudeTransform>
#include "vismask.hpp"
#include "../mwbase/environment.hpp"
2022-08-16 20:06:25 +02:00
2019-11-27 23:45:01 +01:00
namespace MWRender
{
RecastMesh::RecastMesh(const osg::ref_ptr<osg::Group>& root, bool enabled)
: mRootNode(root)
, mEnabled(enabled)
{
}
RecastMesh::~RecastMesh()
{
if (mEnabled)
disable();
}
bool RecastMesh::toggle()
{
if (mEnabled)
disable();
else
enable();
return mEnabled;
}
void RecastMesh::update(const DetourNavigator::RecastMeshTiles& tiles, const DetourNavigator::Settings& settings)
{
if (!mEnabled)
return;
for (auto it = mGroups.begin(); it != mGroups.end();)
{
const auto tile = tiles.find(it->first);
if (tile == tiles.end())
{
mRootNode->removeChild(it->second.mValue);
it = mGroups.erase(it);
continue;
}
if (it->second.mVersion != tile->second->getVersion())
2019-11-27 23:45:01 +01:00
{
const auto group = SceneUtil::createRecastMeshGroup(*tile->second, settings.mRecast);
MWBase::Environment::get().getResourceSystem()->getSceneManager()->recreateShaders(group, "debug");
group->setNodeMask(Mask_Debug);
2019-11-27 23:45:01 +01:00
mRootNode->removeChild(it->second.mValue);
mRootNode->addChild(group);
it->second.mValue = group;
it->second.mVersion = tile->second->getVersion();
2019-11-27 23:45:01 +01:00
}
++it;
}
for (const auto& tile : tiles)
{
if (mGroups.count(tile.first))
continue;
const auto group = SceneUtil::createRecastMeshGroup(*tile.second, settings.mRecast);
MWBase::Environment::get().getResourceSystem()->getSceneManager()->recreateShaders(group, "debug");
group->setNodeMask(Mask_Debug);
mGroups.emplace(tile.first, Group {tile.second->getVersion(), group});
2019-11-27 23:45:01 +01:00
mRootNode->addChild(group);
}
}
void RecastMesh::reset()
{
std::for_each(mGroups.begin(), mGroups.end(), [&] (const auto& v) { mRootNode->removeChild(v.second.mValue); });
mGroups.clear();
}
void RecastMesh::enable()
{
std::for_each(mGroups.begin(), mGroups.end(), [&] (const auto& v) { mRootNode->addChild(v.second.mValue); });
mEnabled = true;
}
void RecastMesh::disable()
{
std::for_each(mGroups.begin(), mGroups.end(), [&] (const auto& v) { mRootNode->removeChild(v.second.mValue); });
mEnabled = false;
}
}