1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-16 15:29:55 +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:
elsid 2021-11-02 02:43:09 +01:00
parent d1a1b8c01c
commit cffcb6a897
No known key found for this signature in database
GPG key ID: B845CB9FEE18AB40
8 changed files with 57 additions and 32 deletions

View file

@ -5,8 +5,10 @@
#include <components/resource/resourcesystem.hpp>
#include <components/resource/scenemanager.hpp>
#include <components/detournavigator/navmeshcacheitem.hpp>
#include <components/sceneutil/detourdebugdraw.hpp>
#include <osg/PositionAttitudeTransform>
#include <osg/StateSet>
#include "../mwbase/world.hpp"
#include "../mwbase/environment.hpp"
@ -17,6 +19,8 @@ namespace MWRender
{
NavMesh::NavMesh(const osg::ref_ptr<osg::Group>& root, bool enabled)
: mRootNode(root)
, mGroupStateSet(SceneUtil::makeNavMeshTileStateSet())
, mDebugDrawStateSet(SceneUtil::DebugDraw::makeStateSet())
, mEnabled(enabled)
, mId(std::numeric_limits<std::size_t>::max())
{
@ -64,7 +68,8 @@ namespace MWRender
return;
if (tile.mGroup != nullptr)
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)
return;
MWBase::Environment::get().getResourceSystem()->getSceneManager()->recreateShaders(tile.mGroup, "debug");

View file

@ -15,6 +15,7 @@ namespace osg
{
class Group;
class Geometry;
class StateSet;
}
namespace DetourNavigator
@ -55,6 +56,8 @@ namespace MWRender
};
osg::ref_ptr<osg::Group> mRootNode;
osg::ref_ptr<osg::StateSet> mGroupStateSet;
osg::ref_ptr<osg::StateSet> mDebugDrawStateSet;
bool mEnabled;
std::size_t mId;
DetourNavigator::Version mVersion;

View file

@ -43,7 +43,7 @@ namespace SceneUtil
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 agentHeight = 2.0f * halfExtents.z();

View file

@ -32,19 +32,19 @@ namespace
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)
, mStateSet(stateSet)
, mShift(shift)
, mRecastInvertedScaleFactor(recastInvertedScaleFactor)
, mDepthMask(false)
, mMode(osg::PrimitiveSet::POINTS)
, mSize(1.0f)
{
}
void DebugDraw::depthMask(bool state)
void DebugDraw::depthMask(bool)
{
mDepthMask = state;
}
void DebugDraw::texture(bool)
@ -88,17 +88,8 @@ namespace SceneUtil
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);
geometry->setStateSet(stateSet);
geometry->setStateSet(mStateSet);
geometry->setVertexArray(mVertices);
geometry->setColorArray(mColors, osg::Array::BIND_PER_VERTEX);
geometry->addPrimitiveSet(new osg::DrawArrays(mMode, 0, static_cast<int>(mVertices->size())));
@ -118,4 +109,16 @@ namespace SceneUtil
{
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;
}
}

View file

@ -15,7 +15,10 @@ namespace SceneUtil
class DebugDraw : public duDebugDraw
{
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;
@ -38,9 +41,9 @@ namespace SceneUtil
private:
osg::Group& mGroup;
osg::ref_ptr<osg::StateSet> mStateSet;
osg::Vec3f mShift;
float mRecastInvertedScaleFactor;
bool mDepthMask;
osg::PrimitiveSet::Mode mMode;
float mSize;
osg::ref_ptr<osg::Vec3Array> mVertices;

View file

@ -229,18 +229,8 @@ namespace
namespace SceneUtil
{
osg::ref_ptr<osg::Group> createNavMeshTileGroup(const dtNavMesh& navMesh, const dtMeshTile& meshTile,
const DetourNavigator::Settings& settings)
osg::ref_ptr<osg::StateSet> makeNavMeshTileStateSet()
{
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;
material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
@ -248,9 +238,26 @@ namespace SceneUtil
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::StateSet> stateSet = group->getOrCreateStateSet();
osg::ref_ptr<osg::StateSet> stateSet = new osg::StateSet;
stateSet->setAttribute(material);
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;
}

View file

@ -9,6 +9,7 @@ struct dtMeshTile;
namespace osg
{
class Group;
class StateSet;
}
namespace DetourNavigator
@ -18,8 +19,11 @@ namespace DetourNavigator
namespace SceneUtil
{
osg::ref_ptr<osg::StateSet> makeNavMeshTileStateSet();
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

View file

@ -47,7 +47,7 @@ namespace SceneUtil
using namespace DetourNavigator;
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();
std::vector<int> indices = mesh.getIndices();
std::vector<float> vertices = mesh.getVertices();