parent
1caa18bb4f
commit
70a369f70e
@ -0,0 +1,63 @@
|
||||
#include "navmesh.hpp"
|
||||
#include "vismask.hpp"
|
||||
|
||||
#include <components/sceneutil/navmesh.hpp>
|
||||
|
||||
#include <osg/PositionAttitudeTransform>
|
||||
|
||||
namespace MWRender
|
||||
{
|
||||
NavMesh::NavMesh(const osg::ref_ptr<osg::Group>& root)
|
||||
: mRootNode(root)
|
||||
, mEnabled(false)
|
||||
, mRevision(0)
|
||||
{
|
||||
}
|
||||
|
||||
NavMesh::~NavMesh()
|
||||
{
|
||||
if (mEnabled)
|
||||
disable();
|
||||
}
|
||||
|
||||
bool NavMesh::toggle()
|
||||
{
|
||||
if (mEnabled)
|
||||
disable();
|
||||
else
|
||||
enable();
|
||||
|
||||
return mEnabled;
|
||||
}
|
||||
|
||||
void NavMesh::update(const DetourNavigator::SharedNavMesh& sharedNavMesh, std::size_t revision,
|
||||
const DetourNavigator::Settings& settings)
|
||||
{
|
||||
if (!mEnabled || mRevision >= revision)
|
||||
return;
|
||||
|
||||
mRevision = revision;
|
||||
if (mGroup)
|
||||
mRootNode->removeChild(mGroup);
|
||||
mGroup = SceneUtil::createNavMeshGroup(*sharedNavMesh.lock(), settings);
|
||||
if (mGroup)
|
||||
{
|
||||
mGroup->setNodeMask(Mask_Debug);
|
||||
mRootNode->addChild(mGroup);
|
||||
}
|
||||
}
|
||||
|
||||
void NavMesh::enable()
|
||||
{
|
||||
if (mGroup)
|
||||
mRootNode->addChild(mGroup);
|
||||
mEnabled = true;
|
||||
}
|
||||
|
||||
void NavMesh::disable()
|
||||
{
|
||||
if (mGroup)
|
||||
mRootNode->removeChild(mGroup);
|
||||
mEnabled = false;
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
#ifndef OPENMW_MWRENDER_NAVMESH_H
|
||||
#define OPENMW_MWRENDER_NAVMESH_H
|
||||
|
||||
#include <components/detournavigator/navigator.hpp>
|
||||
|
||||
#include <osg/ref_ptr>
|
||||
|
||||
namespace osg
|
||||
{
|
||||
class Group;
|
||||
class Geometry;
|
||||
}
|
||||
|
||||
namespace MWRender
|
||||
{
|
||||
class NavMesh
|
||||
{
|
||||
public:
|
||||
NavMesh(const osg::ref_ptr<osg::Group>& root);
|
||||
~NavMesh();
|
||||
|
||||
bool toggle();
|
||||
|
||||
void update(const DetourNavigator::SharedNavMesh& sharedNavMesh, std::size_t revision,
|
||||
const DetourNavigator::Settings& settings);
|
||||
|
||||
void enable();
|
||||
|
||||
void disable();
|
||||
|
||||
private:
|
||||
osg::ref_ptr<osg::Group> mRootNode;
|
||||
bool mEnabled;
|
||||
std::size_t mRevision;
|
||||
osg::ref_ptr<osg::Group> mGroup;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,123 @@
|
||||
#include "detourdebugdraw.hpp"
|
||||
#include "util.hpp"
|
||||
|
||||
#include <components/detournavigator/debug.hpp>
|
||||
|
||||
#include <osg/BlendFunc>
|
||||
#include <osg/Group>
|
||||
#include <osg/LineWidth>
|
||||
|
||||
#define OPENMW_TO_STRING(X) #X
|
||||
#define OPENMW_LINE_STRING OPENMW_TO_STRING(__LINE__)
|
||||
|
||||
namespace
|
||||
{
|
||||
using DetourNavigator::operator<<;
|
||||
|
||||
osg::PrimitiveSet::Mode toOsgPrimitiveSetMode(duDebugDrawPrimitives value)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
case DU_DRAW_POINTS:
|
||||
return osg::PrimitiveSet::POINTS;
|
||||
case DU_DRAW_LINES:
|
||||
return osg::PrimitiveSet::LINES;
|
||||
case DU_DRAW_TRIS:
|
||||
return osg::PrimitiveSet::TRIANGLES;
|
||||
case DU_DRAW_QUADS:
|
||||
return osg::PrimitiveSet::QUADS;
|
||||
}
|
||||
throw std::logic_error("Can't convert duDebugDrawPrimitives to osg::PrimitiveSet::Mode, value="
|
||||
+ std::to_string(value));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace SceneUtil
|
||||
{
|
||||
DebugDraw::DebugDraw(osg::Group& group, const osg::Vec3f& shift, float recastInvertedScaleFactor)
|
||||
: mGroup(group)
|
||||
, mShift(shift)
|
||||
, mRecastInvertedScaleFactor(recastInvertedScaleFactor)
|
||||
, mDepthMask(false)
|
||||
, mMode(osg::PrimitiveSet::POINTS)
|
||||
, mSize(1.0f)
|
||||
{
|
||||
}
|
||||
|
||||
void DebugDraw::depthMask(bool state)
|
||||
{
|
||||
mDepthMask = state;
|
||||
}
|
||||
|
||||
void DebugDraw::texture(bool state)
|
||||
{
|
||||
if (state)
|
||||
throw std::logic_error("DebugDraw does not support textures (at " __FILE__ ":" OPENMW_LINE_STRING ")");
|
||||
}
|
||||
|
||||
void DebugDraw::begin(duDebugDrawPrimitives prim, float size)
|
||||
{
|
||||
mMode = toOsgPrimitiveSetMode(prim);
|
||||
mVertices = new osg::Vec3Array;
|
||||
mColors = new osg::Vec4Array;
|
||||
mSize = size * mRecastInvertedScaleFactor;
|
||||
}
|
||||
|
||||
void DebugDraw::vertex(const float* pos, unsigned color)
|
||||
{
|
||||
vertex(pos[0], pos[1], pos[2], color);
|
||||
}
|
||||
|
||||
void DebugDraw::vertex(const float x, const float y, const float z, unsigned color)
|
||||
{
|
||||
addVertex(osg::Vec3f(x, y, z));
|
||||
addColor(SceneUtil::colourFromRGBA(color));
|
||||
}
|
||||
|
||||
void DebugDraw::vertex(const float* pos, unsigned color, const float* uv)
|
||||
{
|
||||
vertex(pos[0], pos[1], pos[2], color, uv[0], uv[1]);
|
||||
}
|
||||
|
||||
void DebugDraw::vertex(const float, const float, const float, unsigned, const float, const float)
|
||||
{
|
||||
throw std::logic_error("Not implemented (at " __FILE__ ":" OPENMW_LINE_STRING ")");
|
||||
}
|
||||
|
||||
void DebugDraw::end()
|
||||
{
|
||||
osg::ref_ptr<osg::StateSet> stateSet(new osg::StateSet);
|
||||
stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
|
||||
stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
|
||||
stateSet->setMode(GL_DEPTH, (mDepthMask ? osg::StateAttribute::ON : osg::StateAttribute::OFF));
|
||||
stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
|
||||
stateSet->setAttributeAndModes(new osg::LineWidth(mSize));
|
||||
stateSet->setAttributeAndModes(new osg::BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
|
||||
|
||||
osg::ref_ptr<osg::Geometry> geometry(new osg::Geometry);
|
||||
geometry->setStateSet(stateSet);
|
||||
geometry->setUseDisplayList(false);
|
||||
geometry->setVertexArray(mVertices);
|
||||
geometry->setColorArray(mColors, osg::Array::BIND_PER_VERTEX);
|
||||
geometry->addPrimitiveSet(new osg::DrawArrays(mMode, 0, static_cast<int>(mVertices->size())));
|
||||
|
||||
mGroup.addChild(geometry);
|
||||
mColors.release();
|
||||
mVertices.release();
|
||||
}
|
||||
|
||||
void DebugDraw::addVertex(osg::Vec3f&& position)
|
||||
{
|
||||
std::swap(position.y(), position.z());
|
||||
mVertices->push_back(position * mRecastInvertedScaleFactor + mShift);
|
||||
}
|
||||
|
||||
void DebugDraw::addColor(osg::Vec4f&& value)
|
||||
{
|
||||
mColors->push_back(value);
|
||||
}
|
||||
}
|
||||
|
||||
#undef OPENMW_TO_STRING
|
||||
#undef OPENMW_LINE_STRING
|
@ -0,0 +1,48 @@
|
||||
#include <DebugDraw.h>
|
||||
|
||||
#include <osg/Geometry>
|
||||
|
||||
namespace osg
|
||||
{
|
||||
class Group;
|
||||
}
|
||||
|
||||
namespace SceneUtil
|
||||
{
|
||||
class DebugDraw : public duDebugDraw
|
||||
{
|
||||
public:
|
||||
DebugDraw(osg::Group& group, const osg::Vec3f& shift, float recastInvertedScaleFactor);
|
||||
|
||||
void depthMask(bool state) override;
|
||||
|
||||
void texture(bool state) override;
|
||||
|
||||
void begin(duDebugDrawPrimitives prim, float size) override;
|
||||
|
||||
void vertex(const float* pos, unsigned int color) override;
|
||||
|
||||
void vertex(const float x, const float y, const float z, unsigned int color) override;
|
||||
|
||||
void vertex(const float* pos, unsigned int color, const float* uv) override;
|
||||
|
||||
void vertex(const float x, const float y, const float z, unsigned int color,
|
||||
const float u, const float v) override;
|
||||
|
||||
void end() override;
|
||||
|
||||
private:
|
||||
osg::Group& mGroup;
|
||||
osg::Vec3f mShift;
|
||||
float mRecastInvertedScaleFactor;
|
||||
bool mDepthMask;
|
||||
osg::PrimitiveSet::Mode mMode;
|
||||
float mSize;
|
||||
osg::ref_ptr<osg::Vec3Array> mVertices;
|
||||
osg::ref_ptr<osg::Vec4Array> mColors;
|
||||
|
||||
void addVertex(osg::Vec3f&& position);
|
||||
|
||||
void addColor(osg::Vec4f&& value);
|
||||
};
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
#include "navmesh.hpp"
|
||||
#include "detourdebugdraw.hpp"
|
||||
|
||||
#include <components/detournavigator/settings.hpp>
|
||||
|
||||
#include <DetourDebugDraw.h>
|
||||
|
||||
#include <osg/Group>
|
||||
|
||||
namespace SceneUtil
|
||||
{
|
||||
osg::ref_ptr<osg::Group> createNavMeshGroup(const dtNavMesh& navMesh, const DetourNavigator::Settings& settings)
|
||||
{
|
||||
const osg::ref_ptr<osg::Group> group(new osg::Group);
|
||||
DebugDraw debugDraw(*group, osg::Vec3f(0, 0, 10), 1.0f / settings.mRecastScaleFactor);
|
||||
dtNavMeshQuery navMeshQuery;
|
||||
navMeshQuery.init(&navMesh, settings.mMaxNavMeshQueryNodes);
|
||||
duDebugDrawNavMeshWithClosedList(&debugDraw, navMesh, navMeshQuery,
|
||||
DU_DRAWNAVMESH_OFFMESHCONS | DU_DRAWNAVMESH_CLOSEDLIST);
|
||||
return group;
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
#ifndef OPENMW_COMPONENTS_SCENEUTIL_NAVMESH_H
|
||||
#define OPENMW_COMPONENTS_SCENEUTIL_NAVMESH_H
|
||||
|
||||
#include <osg/ref_ptr>
|
||||
|
||||
class dtNavMesh;
|
||||
|
||||
namespace osg
|
||||
{
|
||||
class Group;
|
||||
}
|
||||
|
||||
namespace DetourNavigator
|
||||
{
|
||||
struct Settings;
|
||||
}
|
||||
|
||||
namespace SceneUtil
|
||||
{
|
||||
osg::ref_ptr<osg::Group> createNavMeshGroup(const dtNavMesh& navMesh, const DetourNavigator::Settings& settings);
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue