1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-02-21 09:39:56 +00:00

Merge branch 'fix_debug_render' into 'master'

Fix debug render for recast mesh and navmesh

See merge request OpenMW/openmw!4127
This commit is contained in:
psi29a 2024-05-28 07:38:00 +00:00
commit 8cf6fde8b6
15 changed files with 149 additions and 89 deletions

View File

@ -1,12 +1,17 @@
#include "actorspaths.hpp"
#include "vismask.hpp"
#include <components/detournavigator/settings.hpp>
#include <components/resource/resourcesystem.hpp>
#include <components/resource/scenemanager.hpp>
#include <components/sceneutil/agentpath.hpp>
#include <components/sceneutil/detourdebugdraw.hpp>
#include <osg/LineWidth>
#include <osg/Material>
#include <osg/PositionAttitudeTransform>
#include <osg/StateSet>
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
@ -15,9 +20,32 @@
namespace MWRender
{
namespace
{
osg::ref_ptr<osg::StateSet> makeGroupStateSet()
{
osg::ref_ptr<osg::Material> material = new osg::Material;
material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
osg::ref_ptr<osg::StateSet> stateSet = new osg::StateSet;
stateSet->setAttribute(material);
return stateSet;
}
osg::ref_ptr<osg::StateSet> makeDebugDrawStateSet()
{
osg::ref_ptr<osg::StateSet> stateSet = new osg::StateSet;
stateSet->setAttributeAndModes(new osg::LineWidth());
return stateSet;
}
}
ActorsPaths::ActorsPaths(const osg::ref_ptr<osg::Group>& root, bool enabled)
: mRootNode(root)
, mEnabled(enabled)
, mGroupStateSet(makeGroupStateSet())
, mDebugDrawStateSet(makeDebugDrawStateSet())
{
}
@ -48,14 +76,15 @@ namespace MWRender
if (group != mGroups.end())
mRootNode->removeChild(group->second.mNode);
auto newGroup = SceneUtil::createAgentPathGroup(path, agentBounds, start, end, settings.mRecast);
if (newGroup)
{
MWBase::Environment::get().getResourceSystem()->getSceneManager()->recreateShaders(newGroup, "debug");
newGroup->setNodeMask(Mask_Debug);
mRootNode->addChild(newGroup);
mGroups[actor.mRef] = Group{ actor.mCell, std::move(newGroup) };
}
osg::ref_ptr<osg::Group> newGroup
= SceneUtil::createAgentPathGroup(path, agentBounds, start, end, settings.mRecast, mDebugDrawStateSet);
newGroup->setNodeMask(Mask_Debug);
newGroup->setStateSet(mGroupStateSet);
MWBase::Environment::get().getResourceSystem()->getSceneManager()->recreateShaders(newGroup, "debug");
mRootNode->addChild(newGroup);
mGroups.insert_or_assign(group, actor.mRef, Group{ actor.mCell, std::move(newGroup) });
}
void ActorsPaths::remove(const MWWorld::ConstPtr& actor)

View File

@ -7,11 +7,11 @@
#include <deque>
#include <map>
#include <unordered_map>
namespace osg
{
class Group;
class StateSet;
}
namespace DetourNavigator
@ -56,6 +56,8 @@ namespace MWRender
osg::ref_ptr<osg::Group> mRootNode;
Groups mGroups;
bool mEnabled;
osg::ref_ptr<osg::StateSet> mGroupStateSet;
osg::ref_ptr<osg::StateSet> mDebugDrawStateSet;
};
}

View File

@ -1,4 +1,5 @@
#include "navmesh.hpp"
#include "vismask.hpp"
#include <components/detournavigator/guardednavmeshcacheitem.hpp>
@ -6,10 +7,13 @@
#include <components/detournavigator/settings.hpp>
#include <components/resource/resourcesystem.hpp>
#include <components/resource/scenemanager.hpp>
#include <components/sceneutil/depth.hpp>
#include <components/sceneutil/detourdebugdraw.hpp>
#include <components/sceneutil/navmesh.hpp>
#include <components/sceneutil/workqueue.hpp>
#include <osg/BlendFunc>
#include <osg/LineWidth>
#include <osg/PositionAttitudeTransform>
#include <osg/StateSet>
@ -22,6 +26,29 @@
namespace MWRender
{
namespace
{
osg::ref_ptr<osg::StateSet> makeDebugDrawStateSet()
{
const osg::ref_ptr<osg::LineWidth> lineWidth = new osg::LineWidth();
const osg::ref_ptr<osg::BlendFunc> blendFunc = new osg::BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
const osg::ref_ptr<SceneUtil::AutoDepth> depth = new SceneUtil::AutoDepth;
depth->setWriteMask(false);
osg::ref_ptr<osg::StateSet> stateSet = new osg::StateSet;
stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
stateSet->setAttributeAndModes(lineWidth);
stateSet->setAttributeAndModes(blendFunc);
stateSet->setAttributeAndModes(depth);
return stateSet;
}
}
struct NavMesh::LessByTilePosition
{
bool operator()(const DetourNavigator::TilePosition& lhs,
@ -129,16 +156,17 @@ namespace MWRender
if (mAborted.load(std::memory_order_acquire))
return;
group = SceneUtil::createNavMeshTileGroup(navMesh->getImpl(), *meshTile, mSettings, mGroupStateSet,
mDebugDrawStateSet, flags, minSalt, maxSalt);
group = SceneUtil::createNavMeshTileGroup(
navMesh->getImpl(), *meshTile, mSettings, mDebugDrawStateSet, flags, minSalt, maxSalt);
}
if (group == nullptr)
{
removedTiles.push_back(position);
continue;
}
MWBase::Environment::get().getResourceSystem()->getSceneManager()->recreateShaders(group, "debug");
group->setNodeMask(Mask_Debug);
group->setStateSet(mGroupStateSet);
MWBase::Environment::get().getResourceSystem()->getSceneManager()->recreateShaders(group, "debug");
updatedTiles.emplace_back(position, Tile{ version, std::move(group) });
}
@ -166,8 +194,8 @@ namespace MWRender
bool enabled, Settings::NavMeshRenderMode mode)
: mRootNode(root)
, mWorkQueue(workQueue)
, mGroupStateSet(SceneUtil::makeNavMeshTileStateSet())
, mDebugDrawStateSet(SceneUtil::DebugDraw::makeStateSet())
, mGroupStateSet(SceneUtil::makeDetourGroupStateSet())
, mDebugDrawStateSet(makeDebugDrawStateSet())
, mEnabled(enabled)
, mMode(mode)
, mId(std::numeric_limits<std::size_t>::max())

View File

@ -11,7 +11,6 @@
#include <cstddef>
#include <map>
#include <memory>
#include <string_view>
#include <vector>
class dtNavMesh;

View File

@ -6,6 +6,7 @@
#include <components/detournavigator/settings.hpp>
#include <components/resource/resourcesystem.hpp>
#include <components/resource/scenemanager.hpp>
#include <components/sceneutil/detourdebugdraw.hpp>
#include <components/sceneutil/recastmesh.hpp>
#include <osg/PositionAttitudeTransform>
@ -16,9 +17,22 @@
namespace MWRender
{
namespace
{
osg::ref_ptr<osg::StateSet> makeDebugDrawStateSet()
{
osg::ref_ptr<osg::StateSet> stateSet = new osg::StateSet;
stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
return stateSet;
}
}
RecastMesh::RecastMesh(const osg::ref_ptr<osg::Group>& root, bool enabled)
: mRootNode(root)
, mEnabled(enabled)
, mGroupStateSet(SceneUtil::makeDetourGroupStateSet())
, mDebugDrawStateSet(makeDebugDrawStateSet())
{
}
@ -55,11 +69,16 @@ namespace MWRender
if (it->second.mVersion != tile->second->getVersion())
{
const auto group = SceneUtil::createRecastMeshGroup(*tile->second, settings.mRecast);
MWBase::Environment::get().getResourceSystem()->getSceneManager()->recreateShaders(group, "debug");
const osg::ref_ptr<osg::Group> group
= SceneUtil::createRecastMeshGroup(*tile->second, settings.mRecast, mDebugDrawStateSet);
group->setNodeMask(Mask_Debug);
group->setStateSet(mGroupStateSet);
MWBase::Environment::get().getResourceSystem()->getSceneManager()->recreateShaders(group, "debug");
mRootNode->removeChild(it->second.mValue);
mRootNode->addChild(group);
it->second.mValue = group;
it->second.mVersion = tile->second->getVersion();
}
@ -79,9 +98,13 @@ namespace MWRender
mRootNode->removeChild(it->second.mValue);
}
const auto group = SceneUtil::createRecastMeshGroup(*mesh, settings.mRecast);
MWBase::Environment::get().getResourceSystem()->getSceneManager()->recreateShaders(group, "debug");
const osg::ref_ptr<osg::Group> group
= SceneUtil::createRecastMeshGroup(*mesh, settings.mRecast, mDebugDrawStateSet);
group->setNodeMask(Mask_Debug);
group->setStateSet(mGroupStateSet);
MWBase::Environment::get().getResourceSystem()->getSceneManager()->recreateShaders(group, "debug");
mGroups.insert_or_assign(it, position, Group{ mesh->getVersion(), group });
mRootNode->addChild(group);
}

View File

@ -10,6 +10,7 @@ namespace osg
{
class Group;
class Geometry;
class StateSet;
}
namespace DetourNavigator
@ -47,6 +48,8 @@ namespace MWRender
osg::ref_ptr<osg::Group> mRootNode;
bool mEnabled;
std::map<DetourNavigator::TilePosition, Group> mGroups;
osg::ref_ptr<osg::StateSet> mGroupStateSet;
osg::ref_ptr<osg::StateSet> mDebugDrawStateSet;
};
}

View File

@ -59,8 +59,8 @@ namespace MWRender
};
// Defines masks to remove when using ToggleWorld command
constexpr static unsigned int sToggleWorldMask
= Mask_Debug | Mask_Actor | Mask_Terrain | Mask_Object | Mask_Static | Mask_Groundcover;
constexpr inline unsigned int sToggleWorldMask
= Mask_Actor | Mask_Terrain | Mask_Object | Mask_Static | Mask_Groundcover;
}

View File

@ -1,4 +1,5 @@
#include "agentpath.hpp"
#include "detourdebugdraw.hpp"
#include <osg/Material>
@ -38,13 +39,13 @@ namespace SceneUtil
{
osg::ref_ptr<osg::Group> createAgentPathGroup(const std::deque<osg::Vec3f>& path,
const DetourNavigator::AgentBounds& agentBounds, const osg::Vec3f& start, const osg::Vec3f& end,
const DetourNavigator::RecastSettings& settings)
const DetourNavigator::RecastSettings& settings, const osg::ref_ptr<osg::StateSet>& debugDrawStateSet)
{
using namespace DetourNavigator;
const osg::ref_ptr<osg::Group> group(new osg::Group);
osg::ref_ptr<osg::Group> group(new osg::Group);
DebugDraw debugDraw(*group, DebugDraw::makeStateSet(), osg::Vec3f(0, 0, 0), 1);
DebugDraw debugDraw(*group, debugDrawStateSet, osg::Vec3f(0, 0, 0), 1);
const auto agentRadius = DetourNavigator::getAgentRadius(agentBounds);
const auto agentHeight = DetourNavigator::getAgentHeight(agentBounds);
@ -69,10 +70,6 @@ namespace SceneUtil
debugDraw.depthMask(true);
osg::ref_ptr<osg::Material> material = new osg::Material;
material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
group->getOrCreateStateSet()->setAttribute(material);
return group;
}
}

View File

@ -9,6 +9,7 @@ namespace osg
{
class Group;
class Vec3f;
class StateSet;
}
namespace DetourNavigator
@ -21,7 +22,7 @@ namespace SceneUtil
{
osg::ref_ptr<osg::Group> createAgentPathGroup(const std::deque<osg::Vec3f>& path,
const DetourNavigator::AgentBounds& agentBounds, const osg::Vec3f& start, const osg::Vec3f& end,
const DetourNavigator::RecastSettings& settings);
const DetourNavigator::RecastSettings& settings, const osg::ref_ptr<osg::StateSet>& debugDrawStateSet);
}
#endif

View File

@ -1,9 +1,15 @@
#include "detourdebugdraw.hpp"
#include "depth.hpp"
#include "util.hpp"
#include <osg/BlendFunc>
#include <osg/Group>
#include <osg/LineWidth>
#include <osg/Material>
#include <osg/PolygonOffset>
#include <algorithm>
namespace
{
@ -80,15 +86,16 @@ namespace SceneUtil
void DebugDraw::end()
{
const osg::ref_ptr<osg::DrawArrays> drawArrays
= new osg::DrawArrays(mMode, 0, static_cast<int>(mVertices->size()));
osg::ref_ptr<osg::Geometry> geometry(new osg::Geometry);
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())));
geometry->addPrimitiveSet(drawArrays);
geometry->setVertexArray(std::exchange(mVertices, nullptr));
geometry->setColorArray(std::exchange(mColors, nullptr), osg::Array::BIND_PER_VERTEX);
mGroup.addChild(geometry);
mColors.release();
mVertices.release();
}
void DebugDraw::addVertex(osg::Vec3f&& position)
@ -102,15 +109,19 @@ namespace SceneUtil
mColors->push_back(value);
}
osg::ref_ptr<osg::StateSet> DebugDraw::makeStateSet()
osg::ref_ptr<osg::StateSet> makeDetourGroupStateSet()
{
osg::ref_ptr<osg::Material> material = new osg::Material;
material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
const float polygonOffsetFactor = 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::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));
stateSet->setAttribute(material);
stateSet->setAttributeAndModes(polygonOffset);
return stateSet;
}
}

View File

@ -18,8 +18,6 @@ namespace SceneUtil
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 texture(bool state) override;
@ -53,6 +51,8 @@ namespace SceneUtil
void addColor(osg::Vec4f&& value);
};
osg::ref_ptr<osg::StateSet> makeDetourGroupStateSet();
}
#endif

View File

@ -1,5 +1,5 @@
#include "navmesh.hpp"
#include "depth.hpp"
#include "detourdebugdraw.hpp"
#include <components/detournavigator/settings.hpp>
@ -7,10 +7,6 @@
#include <DetourDebugDraw.h>
#include <osg/Group>
#include <osg/Material>
#include <osg/PolygonOffset>
#include <algorithm>
namespace
{
@ -270,31 +266,14 @@ namespace
namespace SceneUtil
{
osg::ref_ptr<osg::StateSet> makeNavMeshTileStateSet()
{
osg::ref_ptr<osg::Material> material = new osg::Material;
material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
const float polygonOffsetFactor = 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::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, unsigned char flags, unsigned minSalt, unsigned maxSalt)
const DetourNavigator::Settings& settings, const osg::ref_ptr<osg::StateSet>& debugDrawStateSet,
unsigned char flags, unsigned minSalt, unsigned maxSalt)
{
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.mRecast.mRecastScaleFactor);

View File

@ -27,11 +27,9 @@ namespace SceneUtil
NavMeshTileDrawFlagsHeat = 1 << 3,
};
osg::ref_ptr<osg::StateSet> makeNavMeshTileStateSet();
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, unsigned char flags, unsigned minSalt, unsigned maxSalt);
const DetourNavigator::Settings& settings, const osg::ref_ptr<osg::StateSet>& debugDrawStateSet,
unsigned char flags, unsigned minSalt, unsigned maxSalt);
}
#endif

View File

@ -1,5 +1,5 @@
#include "recastmesh.hpp"
#include "depth.hpp"
#include "detourdebugdraw.hpp"
#include <components/detournavigator/recastmesh.hpp>
@ -41,13 +41,14 @@ namespace
namespace SceneUtil
{
osg::ref_ptr<osg::Group> createRecastMeshGroup(
const DetourNavigator::RecastMesh& recastMesh, const DetourNavigator::RecastSettings& settings)
osg::ref_ptr<osg::Group> createRecastMeshGroup(const DetourNavigator::RecastMesh& recastMesh,
const DetourNavigator::RecastSettings& settings, const osg::ref_ptr<osg::StateSet>& debugDrawStateSet)
{
using namespace DetourNavigator;
const osg::ref_ptr<osg::Group> group(new osg::Group);
DebugDraw debugDraw(*group, DebugDraw::makeStateSet(), osg::Vec3f(0, 0, 0), 1.0f);
DebugDraw debugDraw(*group, debugDrawStateSet, 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();
@ -70,18 +71,6 @@ namespace SceneUtil
duDebugDrawTriMeshSlope(&debugDraw, vertices.data(), static_cast<int>(vertices.size() / 3), indices.data(),
normals.data(), static_cast<int>(indices.size() / 3), settings.mMaxSlope, texScale);
osg::ref_ptr<osg::Material> material = new osg::Material;
material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
const float polygonOffsetFactor = 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::StateSet> stateSet = group->getOrCreateStateSet();
stateSet->setAttribute(material);
stateSet->setAttributeAndModes(polygonOffset);
return group;
}
}

View File

@ -6,6 +6,7 @@
namespace osg
{
class Group;
class StateSet;
}
namespace DetourNavigator
@ -16,8 +17,8 @@ namespace DetourNavigator
namespace SceneUtil
{
osg::ref_ptr<osg::Group> createRecastMeshGroup(
const DetourNavigator::RecastMesh& recastMesh, const DetourNavigator::RecastSettings& settings);
osg::ref_ptr<osg::Group> createRecastMeshGroup(const DetourNavigator::RecastMesh& recastMesh,
const DetourNavigator::RecastSettings& settings, const osg::ref_ptr<osg::StateSet>& debugDrawStateSet);
}
#endif