mirror of
https://github.com/OpenMW/openmw.git
synced 2025-05-10 21:41:30 +00:00
Share state set between all navmesh tiles
Do not change GL_DEPTH because it's always disabled anyway.
This commit is contained in:
parent
d1a1b8c01c
commit
cffcb6a897
8 changed files with 57 additions and 32 deletions
|
@ -5,8 +5,10 @@
|
||||||
#include <components/resource/resourcesystem.hpp>
|
#include <components/resource/resourcesystem.hpp>
|
||||||
#include <components/resource/scenemanager.hpp>
|
#include <components/resource/scenemanager.hpp>
|
||||||
#include <components/detournavigator/navmeshcacheitem.hpp>
|
#include <components/detournavigator/navmeshcacheitem.hpp>
|
||||||
|
#include <components/sceneutil/detourdebugdraw.hpp>
|
||||||
|
|
||||||
#include <osg/PositionAttitudeTransform>
|
#include <osg/PositionAttitudeTransform>
|
||||||
|
#include <osg/StateSet>
|
||||||
|
|
||||||
#include "../mwbase/world.hpp"
|
#include "../mwbase/world.hpp"
|
||||||
#include "../mwbase/environment.hpp"
|
#include "../mwbase/environment.hpp"
|
||||||
|
@ -17,6 +19,8 @@ namespace MWRender
|
||||||
{
|
{
|
||||||
NavMesh::NavMesh(const osg::ref_ptr<osg::Group>& root, bool enabled)
|
NavMesh::NavMesh(const osg::ref_ptr<osg::Group>& root, bool enabled)
|
||||||
: mRootNode(root)
|
: mRootNode(root)
|
||||||
|
, mGroupStateSet(SceneUtil::makeNavMeshTileStateSet())
|
||||||
|
, mDebugDrawStateSet(SceneUtil::DebugDraw::makeStateSet())
|
||||||
, mEnabled(enabled)
|
, mEnabled(enabled)
|
||||||
, mId(std::numeric_limits<std::size_t>::max())
|
, mId(std::numeric_limits<std::size_t>::max())
|
||||||
{
|
{
|
||||||
|
@ -64,7 +68,8 @@ namespace MWRender
|
||||||
return;
|
return;
|
||||||
if (tile.mGroup != nullptr)
|
if (tile.mGroup != nullptr)
|
||||||
mRootNode->removeChild(tile.mGroup);
|
mRootNode->removeChild(tile.mGroup);
|
||||||
tile.mGroup = SceneUtil::createNavMeshTileGroup(navMesh.getImpl(), meshTile, settings);
|
tile.mGroup = SceneUtil::createNavMeshTileGroup(navMesh.getImpl(), meshTile, settings,
|
||||||
|
mGroupStateSet, mDebugDrawStateSet);
|
||||||
if (tile.mGroup == nullptr)
|
if (tile.mGroup == nullptr)
|
||||||
return;
|
return;
|
||||||
MWBase::Environment::get().getResourceSystem()->getSceneManager()->recreateShaders(tile.mGroup, "debug");
|
MWBase::Environment::get().getResourceSystem()->getSceneManager()->recreateShaders(tile.mGroup, "debug");
|
||||||
|
|
|
@ -15,6 +15,7 @@ namespace osg
|
||||||
{
|
{
|
||||||
class Group;
|
class Group;
|
||||||
class Geometry;
|
class Geometry;
|
||||||
|
class StateSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace DetourNavigator
|
namespace DetourNavigator
|
||||||
|
@ -55,6 +56,8 @@ namespace MWRender
|
||||||
};
|
};
|
||||||
|
|
||||||
osg::ref_ptr<osg::Group> mRootNode;
|
osg::ref_ptr<osg::Group> mRootNode;
|
||||||
|
osg::ref_ptr<osg::StateSet> mGroupStateSet;
|
||||||
|
osg::ref_ptr<osg::StateSet> mDebugDrawStateSet;
|
||||||
bool mEnabled;
|
bool mEnabled;
|
||||||
std::size_t mId;
|
std::size_t mId;
|
||||||
DetourNavigator::Version mVersion;
|
DetourNavigator::Version mVersion;
|
||||||
|
|
|
@ -43,7 +43,7 @@ namespace SceneUtil
|
||||||
|
|
||||||
const osg::ref_ptr<osg::Group> group(new osg::Group);
|
const osg::ref_ptr<osg::Group> group(new osg::Group);
|
||||||
|
|
||||||
DebugDraw debugDraw(*group, osg::Vec3f(0, 0, 0), 1);
|
DebugDraw debugDraw(*group, DebugDraw::makeStateSet(), osg::Vec3f(0, 0, 0), 1);
|
||||||
|
|
||||||
const auto agentRadius = halfExtents.x();
|
const auto agentRadius = halfExtents.x();
|
||||||
const auto agentHeight = 2.0f * halfExtents.z();
|
const auto agentHeight = 2.0f * halfExtents.z();
|
||||||
|
|
|
@ -32,19 +32,19 @@ namespace
|
||||||
|
|
||||||
namespace SceneUtil
|
namespace SceneUtil
|
||||||
{
|
{
|
||||||
DebugDraw::DebugDraw(osg::Group& group, const osg::Vec3f& shift, float recastInvertedScaleFactor)
|
DebugDraw::DebugDraw(osg::Group& group, const osg::ref_ptr<osg::StateSet>& stateSet,
|
||||||
|
const osg::Vec3f& shift, float recastInvertedScaleFactor)
|
||||||
: mGroup(group)
|
: mGroup(group)
|
||||||
|
, mStateSet(stateSet)
|
||||||
, mShift(shift)
|
, mShift(shift)
|
||||||
, mRecastInvertedScaleFactor(recastInvertedScaleFactor)
|
, mRecastInvertedScaleFactor(recastInvertedScaleFactor)
|
||||||
, mDepthMask(false)
|
|
||||||
, mMode(osg::PrimitiveSet::POINTS)
|
, mMode(osg::PrimitiveSet::POINTS)
|
||||||
, mSize(1.0f)
|
, mSize(1.0f)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void DebugDraw::depthMask(bool state)
|
void DebugDraw::depthMask(bool)
|
||||||
{
|
{
|
||||||
mDepthMask = state;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DebugDraw::texture(bool)
|
void DebugDraw::texture(bool)
|
||||||
|
@ -88,17 +88,8 @@ namespace SceneUtil
|
||||||
|
|
||||||
void DebugDraw::end()
|
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);
|
|
||||||
// TODO: mSize has to be used for the line width, but not for glLineWidth because of the spec limitations
|
|
||||||
stateSet->setAttributeAndModes(new osg::LineWidth());
|
|
||||||
stateSet->setAttributeAndModes(new osg::BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
|
|
||||||
|
|
||||||
osg::ref_ptr<osg::Geometry> geometry(new osg::Geometry);
|
osg::ref_ptr<osg::Geometry> geometry(new osg::Geometry);
|
||||||
geometry->setStateSet(stateSet);
|
geometry->setStateSet(mStateSet);
|
||||||
geometry->setVertexArray(mVertices);
|
geometry->setVertexArray(mVertices);
|
||||||
geometry->setColorArray(mColors, osg::Array::BIND_PER_VERTEX);
|
geometry->setColorArray(mColors, osg::Array::BIND_PER_VERTEX);
|
||||||
geometry->addPrimitiveSet(new osg::DrawArrays(mMode, 0, static_cast<int>(mVertices->size())));
|
geometry->addPrimitiveSet(new osg::DrawArrays(mMode, 0, static_cast<int>(mVertices->size())));
|
||||||
|
@ -118,4 +109,16 @@ namespace SceneUtil
|
||||||
{
|
{
|
||||||
mColors->push_back(value);
|
mColors->push_back(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
osg::ref_ptr<osg::StateSet> DebugDraw::makeStateSet()
|
||||||
|
{
|
||||||
|
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, osg::StateAttribute::OFF);
|
||||||
|
stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
|
||||||
|
stateSet->setAttributeAndModes(new osg::LineWidth());
|
||||||
|
stateSet->setAttributeAndModes(new osg::BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
|
||||||
|
return stateSet;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,10 @@ namespace SceneUtil
|
||||||
class DebugDraw : public duDebugDraw
|
class DebugDraw : public duDebugDraw
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit DebugDraw(osg::Group& group, const osg::Vec3f& shift, float recastInvertedScaleFactor);
|
explicit DebugDraw(osg::Group& group, const osg::ref_ptr<osg::StateSet>& stateSet,
|
||||||
|
const osg::Vec3f& shift, float recastInvertedScaleFactor);
|
||||||
|
|
||||||
|
static osg::ref_ptr<osg::StateSet> makeStateSet();
|
||||||
|
|
||||||
void depthMask(bool state) override;
|
void depthMask(bool state) override;
|
||||||
|
|
||||||
|
@ -38,9 +41,9 @@ namespace SceneUtil
|
||||||
|
|
||||||
private:
|
private:
|
||||||
osg::Group& mGroup;
|
osg::Group& mGroup;
|
||||||
|
osg::ref_ptr<osg::StateSet> mStateSet;
|
||||||
osg::Vec3f mShift;
|
osg::Vec3f mShift;
|
||||||
float mRecastInvertedScaleFactor;
|
float mRecastInvertedScaleFactor;
|
||||||
bool mDepthMask;
|
|
||||||
osg::PrimitiveSet::Mode mMode;
|
osg::PrimitiveSet::Mode mMode;
|
||||||
float mSize;
|
float mSize;
|
||||||
osg::ref_ptr<osg::Vec3Array> mVertices;
|
osg::ref_ptr<osg::Vec3Array> mVertices;
|
||||||
|
|
|
@ -229,18 +229,8 @@ namespace
|
||||||
|
|
||||||
namespace SceneUtil
|
namespace SceneUtil
|
||||||
{
|
{
|
||||||
osg::ref_ptr<osg::Group> createNavMeshTileGroup(const dtNavMesh& navMesh, const dtMeshTile& meshTile,
|
osg::ref_ptr<osg::StateSet> makeNavMeshTileStateSet()
|
||||||
const DetourNavigator::Settings& settings)
|
|
||||||
{
|
{
|
||||||
if (meshTile.header == nullptr)
|
|
||||||
return nullptr;
|
|
||||||
|
|
||||||
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);
|
|
||||||
drawMeshTile(&debugDraw, navMesh, &navMeshQuery, &meshTile, DU_DRAWNAVMESH_OFFMESHCONS | DU_DRAWNAVMESH_CLOSEDLIST);
|
|
||||||
|
|
||||||
osg::ref_ptr<osg::Material> material = new osg::Material;
|
osg::ref_ptr<osg::Material> material = new osg::Material;
|
||||||
material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
|
material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
|
||||||
|
|
||||||
|
@ -248,9 +238,26 @@ namespace SceneUtil
|
||||||
const float polygonOffsetUnits = SceneUtil::AutoDepth::isReversed() ? 1.0 : -1.0;
|
const float polygonOffsetUnits = SceneUtil::AutoDepth::isReversed() ? 1.0 : -1.0;
|
||||||
osg::ref_ptr<osg::PolygonOffset> polygonOffset = new osg::PolygonOffset(polygonOffsetFactor, polygonOffsetUnits);
|
osg::ref_ptr<osg::PolygonOffset> polygonOffset = new osg::PolygonOffset(polygonOffsetFactor, polygonOffsetUnits);
|
||||||
|
|
||||||
osg::ref_ptr<osg::StateSet> stateSet = group->getOrCreateStateSet();
|
osg::ref_ptr<osg::StateSet> stateSet = new osg::StateSet;
|
||||||
stateSet->setAttribute(material);
|
stateSet->setAttribute(material);
|
||||||
stateSet->setAttributeAndModes(polygonOffset);
|
stateSet->setAttributeAndModes(polygonOffset);
|
||||||
|
return stateSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
osg::ref_ptr<osg::Group> createNavMeshTileGroup(const dtNavMesh& navMesh, const dtMeshTile& meshTile,
|
||||||
|
const DetourNavigator::Settings& settings, const osg::ref_ptr<osg::StateSet>& groupStateSet,
|
||||||
|
const osg::ref_ptr<osg::StateSet>& debugDrawStateSet)
|
||||||
|
{
|
||||||
|
if (meshTile.header == nullptr)
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
osg::ref_ptr<osg::Group> group(new osg::Group);
|
||||||
|
group->setStateSet(groupStateSet);
|
||||||
|
constexpr float shift = 10.0f;
|
||||||
|
DebugDraw debugDraw(*group, debugDrawStateSet, osg::Vec3f(0, 0, shift), 1.0f / settings.mRecastScaleFactor);
|
||||||
|
dtNavMeshQuery navMeshQuery;
|
||||||
|
navMeshQuery.init(&navMesh, settings.mMaxNavMeshQueryNodes);
|
||||||
|
drawMeshTile(&debugDraw, navMesh, &navMeshQuery, &meshTile, DU_DRAWNAVMESH_OFFMESHCONS | DU_DRAWNAVMESH_CLOSEDLIST);
|
||||||
|
|
||||||
return group;
|
return group;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ struct dtMeshTile;
|
||||||
namespace osg
|
namespace osg
|
||||||
{
|
{
|
||||||
class Group;
|
class Group;
|
||||||
|
class StateSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace DetourNavigator
|
namespace DetourNavigator
|
||||||
|
@ -18,8 +19,11 @@ namespace DetourNavigator
|
||||||
|
|
||||||
namespace SceneUtil
|
namespace SceneUtil
|
||||||
{
|
{
|
||||||
|
osg::ref_ptr<osg::StateSet> makeNavMeshTileStateSet();
|
||||||
|
|
||||||
osg::ref_ptr<osg::Group> createNavMeshTileGroup(const dtNavMesh& navMesh, const dtMeshTile& meshTile,
|
osg::ref_ptr<osg::Group> createNavMeshTileGroup(const dtNavMesh& navMesh, const dtMeshTile& meshTile,
|
||||||
const DetourNavigator::Settings& settings);
|
const DetourNavigator::Settings& settings, const osg::ref_ptr<osg::StateSet>& groupStateSet,
|
||||||
|
const osg::ref_ptr<osg::StateSet>& debugDrawStateSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -47,7 +47,7 @@ namespace SceneUtil
|
||||||
using namespace DetourNavigator;
|
using namespace DetourNavigator;
|
||||||
|
|
||||||
const osg::ref_ptr<osg::Group> group(new osg::Group);
|
const osg::ref_ptr<osg::Group> group(new osg::Group);
|
||||||
DebugDraw debugDraw(*group, osg::Vec3f(0, 0, 0), 1.0f);
|
DebugDraw debugDraw(*group, DebugDraw::makeStateSet(), osg::Vec3f(0, 0, 0), 1.0f);
|
||||||
const DetourNavigator::Mesh& mesh = recastMesh.getMesh();
|
const DetourNavigator::Mesh& mesh = recastMesh.getMesh();
|
||||||
std::vector<int> indices = mesh.getIndices();
|
std::vector<int> indices = mesh.getIndices();
|
||||||
std::vector<float> vertices = mesh.getVertices();
|
std::vector<float> vertices = mesh.getVertices();
|
||||||
|
|
Loading…
Reference in a new issue