Add command and settings option to enable actors paths render
parent
c95cea414c
commit
dc09674362
@ -0,0 +1,99 @@
|
||||
#include "actorspaths.hpp"
|
||||
#include "vismask.hpp"
|
||||
|
||||
#include <components/sceneutil/agentpath.hpp>
|
||||
|
||||
#include <osg/PositionAttitudeTransform>
|
||||
|
||||
namespace MWRender
|
||||
{
|
||||
ActorsPaths::ActorsPaths(const osg::ref_ptr<osg::Group>& root, bool enabled)
|
||||
: mRootNode(root)
|
||||
, mEnabled(enabled)
|
||||
{
|
||||
}
|
||||
|
||||
ActorsPaths::~ActorsPaths()
|
||||
{
|
||||
if (mEnabled)
|
||||
disable();
|
||||
}
|
||||
|
||||
bool ActorsPaths::toggle()
|
||||
{
|
||||
if (mEnabled)
|
||||
disable();
|
||||
else
|
||||
enable();
|
||||
|
||||
return mEnabled;
|
||||
}
|
||||
|
||||
void ActorsPaths::update(const MWWorld::ConstPtr& actor, const std::deque<osg::Vec3f>& path,
|
||||
const osg::Vec3f& halfExtents, const osg::Vec3f& start, const osg::Vec3f& end,
|
||||
const DetourNavigator::Settings& settings)
|
||||
{
|
||||
if (!mEnabled)
|
||||
return;
|
||||
|
||||
const auto group = mGroups.find(actor);
|
||||
if (group != mGroups.end())
|
||||
mRootNode->removeChild(group->second);
|
||||
|
||||
const auto newGroup = SceneUtil::createAgentPathGroup(path, halfExtents, start, end, settings);
|
||||
if (newGroup)
|
||||
{
|
||||
newGroup->setNodeMask(Mask_Debug);
|
||||
mRootNode->addChild(newGroup);
|
||||
mGroups[actor] = newGroup;
|
||||
}
|
||||
}
|
||||
|
||||
void ActorsPaths::remove(const MWWorld::ConstPtr& actor)
|
||||
{
|
||||
const auto group = mGroups.find(actor);
|
||||
if (group != mGroups.end())
|
||||
{
|
||||
mRootNode->removeChild(group->second);
|
||||
mGroups.erase(group);
|
||||
}
|
||||
}
|
||||
|
||||
void ActorsPaths::removeCell(const MWWorld::CellStore* const store)
|
||||
{
|
||||
for (auto it = mGroups.begin(); it != mGroups.end(); )
|
||||
{
|
||||
if (it->first.getCell() == store)
|
||||
{
|
||||
mRootNode->removeChild(it->second);
|
||||
it = mGroups.erase(it);
|
||||
}
|
||||
else
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
void ActorsPaths::updatePtr(const MWWorld::ConstPtr& old, const MWWorld::ConstPtr& updated)
|
||||
{
|
||||
const auto it = mGroups.find(old);
|
||||
if (it == mGroups.end())
|
||||
return;
|
||||
auto group = std::move(it->second);
|
||||
mGroups.erase(it);
|
||||
mGroups.insert(std::make_pair(updated, std::move(group)));
|
||||
}
|
||||
|
||||
void ActorsPaths::enable()
|
||||
{
|
||||
std::for_each(mGroups.begin(), mGroups.end(),
|
||||
[&] (const Groups::value_type& v) { mRootNode->addChild(v.second); });
|
||||
mEnabled = true;
|
||||
}
|
||||
|
||||
void ActorsPaths::disable()
|
||||
{
|
||||
std::for_each(mGroups.begin(), mGroups.end(),
|
||||
[&] (const Groups::value_type& v) { mRootNode->removeChild(v.second); });
|
||||
mEnabled = false;
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
#ifndef OPENMW_MWRENDER_AGENTSPATHS_H
|
||||
#define OPENMW_MWRENDER_AGENTSPATHS_H
|
||||
|
||||
#include <apps/openmw/mwworld/ptr.hpp>
|
||||
|
||||
#include <components/detournavigator/navigator.hpp>
|
||||
|
||||
#include <osg/ref_ptr>
|
||||
|
||||
#include <unordered_map>
|
||||
#include <deque>
|
||||
|
||||
namespace osg
|
||||
{
|
||||
class Group;
|
||||
}
|
||||
|
||||
namespace MWRender
|
||||
{
|
||||
class ActorsPaths
|
||||
{
|
||||
public:
|
||||
ActorsPaths(const osg::ref_ptr<osg::Group>& root, bool enabled);
|
||||
~ActorsPaths();
|
||||
|
||||
bool toggle();
|
||||
|
||||
void update(const MWWorld::ConstPtr& actor, const std::deque<osg::Vec3f>& path,
|
||||
const osg::Vec3f& halfExtents, const osg::Vec3f& start, const osg::Vec3f& end,
|
||||
const DetourNavigator::Settings& settings);
|
||||
|
||||
void remove(const MWWorld::ConstPtr& actor);
|
||||
|
||||
void removeCell(const MWWorld::CellStore* const store);
|
||||
|
||||
void updatePtr(const MWWorld::ConstPtr& old, const MWWorld::ConstPtr& updated);
|
||||
|
||||
void enable();
|
||||
|
||||
void disable();
|
||||
|
||||
private:
|
||||
using Groups = std::map<MWWorld::ConstPtr, osg::ref_ptr<osg::Group>>;
|
||||
|
||||
osg::ref_ptr<osg::Group> mRootNode;
|
||||
Groups mGroups;
|
||||
bool mEnabled;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,71 @@
|
||||
#include "agentpath.hpp"
|
||||
#include "detourdebugdraw.hpp"
|
||||
|
||||
#include <components/detournavigator/settings.hpp>
|
||||
#include <components/esm/loadpgrd.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace
|
||||
{
|
||||
void drawAgent(duDebugDraw& debugDraw, const osg::Vec3f& pos, const float radius, const float height,
|
||||
const float climb, const unsigned color)
|
||||
{
|
||||
debugDraw.depthMask(false);
|
||||
|
||||
duDebugDrawCylinderWire(&debugDraw, pos.x() - radius, pos.z() + 0.02f, pos.y() - radius, pos.x() + radius,
|
||||
pos.z() + height, pos.y() + radius, color, radius * 0.2f);
|
||||
|
||||
duDebugDrawCircle(&debugDraw, pos.x(), pos.z() + climb, pos.y(), radius, duRGBA(0, 0 , 0, 64), radius * 0.1f);
|
||||
|
||||
const auto colb = duRGBA(0, 0, 0, 196);
|
||||
debugDraw.begin(DU_DRAW_LINES);
|
||||
debugDraw.vertex(pos.x(), pos.z() - climb, pos.y(), colb);
|
||||
debugDraw.vertex(pos.x(), pos.z() + climb, pos.y(), colb);
|
||||
debugDraw.vertex(pos.x() - radius / 2, pos.z() + 0.02f, pos.y(), colb);
|
||||
debugDraw.vertex(pos.x() + radius / 2, pos.z() + 0.02f, pos.y(), colb);
|
||||
debugDraw.vertex(pos.x(), pos.z() + 0.02f, pos.y() - radius / 2, colb);
|
||||
debugDraw.vertex(pos.x(), pos.z() + 0.02f, pos.y() + radius / 2, colb);
|
||||
debugDraw.end();
|
||||
|
||||
debugDraw.depthMask(true);
|
||||
}
|
||||
}
|
||||
|
||||
namespace SceneUtil
|
||||
{
|
||||
osg::ref_ptr<osg::Group> createAgentPathGroup(const std::deque<osg::Vec3f>& path,
|
||||
const osg::Vec3f& halfExtents, const osg::Vec3f& start, const osg::Vec3f& end,
|
||||
const DetourNavigator::Settings& settings)
|
||||
{
|
||||
using namespace DetourNavigator;
|
||||
|
||||
const osg::ref_ptr<osg::Group> group(new osg::Group);
|
||||
|
||||
DebugDraw debugDraw(*group, osg::Vec3f(0, 0, 0), 1);
|
||||
|
||||
const auto agentRadius = halfExtents.x();
|
||||
const auto agentHeight = 2.0f * halfExtents.z();
|
||||
const auto agentClimb = settings.mMaxClimb;
|
||||
const auto startColor = duRGBA(128, 25, 0, 192);
|
||||
const auto endColor = duRGBA(51, 102, 0, 129);
|
||||
|
||||
drawAgent(debugDraw, start, agentRadius, agentHeight, agentClimb, startColor);
|
||||
drawAgent(debugDraw, end, agentRadius, agentHeight, agentClimb, endColor);
|
||||
|
||||
const auto pathColor = duRGBA(0, 0, 0, 220);
|
||||
|
||||
debugDraw.depthMask(false);
|
||||
|
||||
debugDraw.begin(osg::PrimitiveSet::LINE_STRIP, agentRadius * 0.5f);
|
||||
debugDraw.vertex(osg::Vec3f(start.x(), start.z() + agentClimb, start.y()).ptr(), startColor);
|
||||
std::for_each(path.begin(), path.end(),
|
||||
[&] (const osg::Vec3f& v) { debugDraw.vertex(osg::Vec3f(v.x(), v.z() + agentClimb, v.y()).ptr(), pathColor); });
|
||||
debugDraw.vertex(osg::Vec3f(end.x(), end.z() + agentClimb, end.y()).ptr(), endColor);
|
||||
debugDraw.end();
|
||||
|
||||
debugDraw.depthMask(true);
|
||||
|
||||
return group;
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
#ifndef OPENMW_COMPONENTS_SCENEUTIL_AGENTPATH_H
|
||||
#define OPENMW_COMPONENTS_SCENEUTIL_AGENTPATH_H
|
||||
|
||||
#include <osg/ref_ptr>
|
||||
|
||||
#include <deque>
|
||||
|
||||
namespace osg
|
||||
{
|
||||
class Group;
|
||||
class Vec3f;
|
||||
}
|
||||
|
||||
namespace DetourNavigator
|
||||
{
|
||||
struct Settings;
|
||||
}
|
||||
|
||||
namespace SceneUtil
|
||||
{
|
||||
osg::ref_ptr<osg::Group> createAgentPathGroup(const std::deque<osg::Vec3f>& path,
|
||||
const osg::Vec3f& halfExtents, const osg::Vec3f& start, const osg::Vec3f& end,
|
||||
const DetourNavigator::Settings& settings);
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue