1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-30 13:15:34 +00:00

Try to uncursify DebugDrawer scene representation

This commit is contained in:
Alexei Kotov 2024-02-07 07:06:24 +03:00
parent d975ed33ab
commit 38ab09a52e
5 changed files with 67 additions and 62 deletions

View file

@ -432,8 +432,9 @@ namespace MWRender
mViewer->getIncrementalCompileOperation()->setTargetFrameRate(Settings::cells().mTargetFramerate); mViewer->getIncrementalCompileOperation()->setTargetFrameRate(Settings::cells().mTargetFramerate);
} }
mDebugDraw mDebugDraw = new Debug::DebugDrawer(mResourceSystem->getSceneManager()->getShaderManager());
= std::make_unique<Debug::DebugDrawer>(mResourceSystem->getSceneManager()->getShaderManager(), mRootNode); mRootNode->addChild(mDebugDraw);
mResourceSystem->getSceneManager()->setIncrementalCompileOperation(mViewer->getIncrementalCompileOperation()); mResourceSystem->getSceneManager()->setIncrementalCompileOperation(mViewer->getIncrementalCompileOperation());
mEffectManager = std::make_unique<EffectManager>(sceneRoot, mResourceSystem); mEffectManager = std::make_unique<EffectManager>(sceneRoot, mResourceSystem);

View file

@ -335,7 +335,7 @@ namespace MWRender
osg::ref_ptr<NpcAnimation> mPlayerAnimation; osg::ref_ptr<NpcAnimation> mPlayerAnimation;
osg::ref_ptr<SceneUtil::PositionAttitudeTransform> mPlayerNode; osg::ref_ptr<SceneUtil::PositionAttitudeTransform> mPlayerNode;
std::unique_ptr<Camera> mCamera; std::unique_ptr<Camera> mCamera;
std::unique_ptr<Debug::DebugDrawer> mDebugDraw; osg::ref_ptr<Debug::DebugDrawer> mDebugDraw;
osg::ref_ptr<StateUpdater> mStateUpdater; osg::ref_ptr<StateUpdater> mStateUpdater;
osg::ref_ptr<SharedUniformStateUpdater> mSharedUniformStateUpdater; osg::ref_ptr<SharedUniformStateUpdater> mSharedUniformStateUpdater;

View file

@ -215,12 +215,12 @@ static void generateCylinder(osg::Geometry& geom, float radius, float height, in
geom.addPrimitiveSet(indices); geom.addPrimitiveSet(indices);
} }
static int getIdexBufferReadFromFrame(const long long int& nFrame) static int getIndexBufferReadFromFrame(const unsigned int& nFrame)
{ {
return nFrame % 2; return nFrame % 2;
} }
static int getIdexBufferWriteFromFrame(const long long int& nFrame) static int getIndexBufferWriteFromFrame(const unsigned int& nFrame)
{ {
return (nFrame + 1) % 2; return (nFrame + 1) % 2;
} }
@ -248,6 +248,16 @@ namespace Debug
makeLineInstance(*mLinesToDraw); makeLineInstance(*mLinesToDraw);
} }
DebugCustomDraw::DebugCustomDraw(const DebugCustomDraw& copy, const osg::CopyOp& copyop)
: Drawable(copy, copyop)
, mShapesToDraw(copy.mShapesToDraw)
, mLinesToDraw(copy.mLinesToDraw)
, mCubeGeometry(copy.mCubeGeometry)
, mCylinderGeometry(copy.mCylinderGeometry)
, mWireCubeGeometry(copy.mWireCubeGeometry)
{
}
void DebugCustomDraw::drawImplementation(osg::RenderInfo& renderInfo) const void DebugCustomDraw::drawImplementation(osg::RenderInfo& renderInfo) const
{ {
auto state = renderInfo.getState(); auto state = renderInfo.getState();
@ -308,43 +318,23 @@ namespace Debug
static_cast<osg::Vec3Array*>(mLinesToDraw->getVertexArray())->clear(); static_cast<osg::Vec3Array*>(mLinesToDraw->getVertexArray())->clear();
static_cast<osg::Vec3Array*>(mLinesToDraw->getNormalArray())->clear(); static_cast<osg::Vec3Array*>(mLinesToDraw->getNormalArray())->clear();
} }
class DebugDrawCallback : public SceneUtil::NodeCallback<DebugDrawCallback>
{
public:
DebugDrawCallback(Debug::DebugDrawer& debugDrawer)
: mDebugDrawer(debugDrawer)
{
}
void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
mDebugDrawer.mCurrentFrame = nv->getTraversalNumber();
int indexRead = getIdexBufferReadFromFrame(mDebugDrawer.mCurrentFrame);
auto& lines = mDebugDrawer.mCustomDebugDrawer[indexRead]->mLinesToDraw;
lines->removePrimitiveSet(0, 1);
lines->addPrimitiveSet(new osg::DrawArrays(
osg::PrimitiveSet::LINES, 0, static_cast<osg::Vec3Array*>(lines->getVertexArray())->size()));
nv->pushOntoNodePath(mDebugDrawer.mCustomDebugDrawer[indexRead]);
nv->apply(*mDebugDrawer.mCustomDebugDrawer[indexRead]);
nv->popFromNodePath();
}
Debug::DebugDrawer& mDebugDrawer;
};
} }
Debug::DebugDrawer::DebugDrawer(Shader::ShaderManager& shaderManager, osg::ref_ptr<osg::Group> parentNode) Debug::DebugDrawer::DebugDrawer(const DebugDrawer& copy, const osg::CopyOp& copyop)
: mParentNode(std::move(parentNode)) : Drawable(copy, copyop)
, mCurrentFrame(copy.mCurrentFrame)
, mCustomDebugDrawer(copy.mCustomDebugDrawer)
{
}
Debug::DebugDrawer::DebugDrawer(Shader::ShaderManager& shaderManager)
{ {
mCurrentFrame = 0; mCurrentFrame = 0;
auto program = shaderManager.getProgram("debug"); auto program = shaderManager.getProgram("debug");
mDebugDrawSceneObjects = new osg::Group; setCullingActive(false);
mDebugDrawSceneObjects->setCullingActive(false); osg::StateSet* stateset = getOrCreateStateSet();
osg::StateSet* stateset = mDebugDrawSceneObjects->getOrCreateStateSet();
stateset->addUniform(new osg::Uniform("color", osg::Vec3f(1., 1., 1.))); stateset->addUniform(new osg::Uniform("color", osg::Vec3f(1., 1., 1.)));
stateset->addUniform(new osg::Uniform("trans", osg::Vec3f(0., 0., 0.))); stateset->addUniform(new osg::Uniform("trans", osg::Vec3f(0., 0., 0.)));
stateset->addUniform(new osg::Uniform("scale", osg::Vec3f(1., 1., 1.))); stateset->addUniform(new osg::Uniform("scale", osg::Vec3f(1., 1., 1.)));
@ -378,19 +368,28 @@ Debug::DebugDrawer::DebugDrawer(Shader::ShaderManager& shaderManager, osg::ref_p
mCustomDebugDrawer[i]->mCubeGeometry = cubeGeometry; mCustomDebugDrawer[i]->mCubeGeometry = cubeGeometry;
mCustomDebugDrawer[i]->mCylinderGeometry = cylinderGeom; mCustomDebugDrawer[i]->mCylinderGeometry = cylinderGeom;
} }
mDebugDrawSceneObjects->addCullCallback(new DebugDrawCallback(*this));
mParentNode->addChild(mDebugDrawSceneObjects);
} }
Debug::DebugDrawer::~DebugDrawer() void Debug::DebugDrawer::accept(osg::NodeVisitor& nv)
{ {
mParentNode->removeChild(mDebugDrawSceneObjects); if (!nv.validNodeMask(*this))
return;
mCurrentFrame = nv.getTraversalNumber();
int indexRead = getIndexBufferReadFromFrame(mCurrentFrame);
auto& lines = mCustomDebugDrawer[indexRead]->mLinesToDraw;
lines->removePrimitiveSet(0, 1);
lines->addPrimitiveSet(new osg::DrawArrays(
osg::PrimitiveSet::LINES, 0, static_cast<osg::Vec3Array*>(lines->getVertexArray())->size()));
nv.pushOntoNodePath(this);
mCustomDebugDrawer[indexRead]->accept(nv);
nv.popFromNodePath();
} }
void Debug::DebugDrawer::drawCube(osg::Vec3f mPosition, osg::Vec3f mDims, osg::Vec3f mColor) void Debug::DebugDrawer::drawCube(osg::Vec3f mPosition, osg::Vec3f mDims, osg::Vec3f mColor)
{ {
mCustomDebugDrawer[getIdexBufferWriteFromFrame(mCurrentFrame)]->mShapesToDraw.push_back( mCustomDebugDrawer[getIndexBufferWriteFromFrame(mCurrentFrame)]->mShapesToDraw.push_back(
{ mPosition, mDims, mColor, DrawShape::Cube }); { mPosition, mDims, mColor, DrawShape::Cube });
} }
@ -403,12 +402,12 @@ void Debug::DebugDrawer::drawCubeMinMax(osg::Vec3f min, osg::Vec3f max, osg::Vec
void Debug::DebugDrawer::addDrawCall(const DrawCall& draw) void Debug::DebugDrawer::addDrawCall(const DrawCall& draw)
{ {
mCustomDebugDrawer[getIdexBufferWriteFromFrame(mCurrentFrame)]->mShapesToDraw.push_back(draw); mCustomDebugDrawer[getIndexBufferWriteFromFrame(mCurrentFrame)]->mShapesToDraw.push_back(draw);
} }
void Debug::DebugDrawer::addLine(const osg::Vec3& start, const osg::Vec3& end, const osg::Vec3 color) void Debug::DebugDrawer::addLine(const osg::Vec3& start, const osg::Vec3& end, const osg::Vec3 color)
{ {
const int indexWrite = getIdexBufferWriteFromFrame(mCurrentFrame); const int indexWrite = getIndexBufferWriteFromFrame(mCurrentFrame);
const auto& lines = mCustomDebugDrawer[indexWrite]->mLinesToDraw; const auto& lines = mCustomDebugDrawer[indexWrite]->mLinesToDraw;
auto vertices = static_cast<osg::Vec3Array*>(lines->getVertexArray()); auto vertices = static_cast<osg::Vec3Array*>(lines->getVertexArray());
auto colors = static_cast<osg::Vec3Array*>(lines->getNormalArray()); auto colors = static_cast<osg::Vec3Array*>(lines->getNormalArray());

View file

@ -70,6 +70,9 @@ namespace Debug
{ {
public: public:
DebugCustomDraw(); DebugCustomDraw();
DebugCustomDraw(const DebugCustomDraw& copy, const osg::CopyOp& copyop);
META_Object(Debug, DebugCustomDraw)
mutable std::vector<DrawCall> mShapesToDraw; mutable std::vector<DrawCall> mShapesToDraw;
osg::ref_ptr<osg::Geometry> mLinesToDraw; osg::ref_ptr<osg::Geometry> mLinesToDraw;
@ -81,12 +84,15 @@ namespace Debug
virtual void drawImplementation(osg::RenderInfo&) const override; virtual void drawImplementation(osg::RenderInfo&) const override;
}; };
struct DebugDrawer struct DebugDrawer : public osg::Drawable
{ {
friend DebugDrawCallback; DebugDrawer() = default;
DebugDrawer(const DebugDrawer& copy, const osg::CopyOp& copyop);
DebugDrawer(Shader::ShaderManager& shaderManager);
DebugDrawer(Shader::ShaderManager& shaderManager, osg::ref_ptr<osg::Group> parentNode); META_Object(Debug, DebugDrawer)
~DebugDrawer();
void accept(osg::NodeVisitor& nv) override;
void drawCube( void drawCube(
osg::Vec3f mPosition, osg::Vec3f mDims = osg::Vec3(50., 50., 50.), osg::Vec3f mColor = colorWhite); osg::Vec3f mPosition, osg::Vec3f mDims = osg::Vec3(50., 50., 50.), osg::Vec3f mColor = colorWhite);
@ -95,11 +101,9 @@ namespace Debug
void addLine(const osg::Vec3& start, const osg::Vec3& end, const osg::Vec3 color = colorWhite); void addLine(const osg::Vec3& start, const osg::Vec3& end, const osg::Vec3 color = colorWhite);
private: private:
long long int mCurrentFrame; unsigned int mCurrentFrame;
std::array<osg::ref_ptr<DebugCustomDraw>, 2> mCustomDebugDrawer; std::array<osg::ref_ptr<DebugCustomDraw>, 2> mCustomDebugDrawer;
osg::ref_ptr<osg::Group> mDebugDrawSceneObjects;
osg::ref_ptr<osg::Group> mParentNode;
}; };
} }
#endif // ! #endif // !

View file

@ -175,18 +175,19 @@ namespace SceneUtil
mgr->addWrapper(new GeometrySerializer); mgr->addWrapper(new GeometrySerializer);
// ignore the below for now to avoid warning spam // ignore the below for now to avoid warning spam
const char* ignore[] = { "MWRender::PtrHolder", "Resource::TemplateRef", "Resource::TemplateMultiRef", const char* ignore[]
"SceneUtil::CompositeStateSetUpdater", "SceneUtil::UBOManager", "SceneUtil::LightListCallback", = { "Debug::DebugDrawer", "MWRender::PtrHolder", "Resource::TemplateRef", "Resource::TemplateMultiRef",
"SceneUtil::LightManagerUpdateCallback", "SceneUtil::FFPLightStateAttribute", "SceneUtil::CompositeStateSetUpdater", "SceneUtil::UBOManager", "SceneUtil::LightListCallback",
"SceneUtil::UpdateRigBounds", "SceneUtil::UpdateRigGeometry", "SceneUtil::LightSource", "SceneUtil::LightManagerUpdateCallback", "SceneUtil::FFPLightStateAttribute",
"SceneUtil::DisableLight", "SceneUtil::MWShadowTechnique", "SceneUtil::TextKeyMapHolder", "SceneUtil::UpdateRigBounds", "SceneUtil::UpdateRigGeometry", "SceneUtil::LightSource",
"Shader::AddedState", "Shader::RemovedAlphaFunc", "NifOsg::FlipController", "SceneUtil::DisableLight", "SceneUtil::MWShadowTechnique", "SceneUtil::TextKeyMapHolder",
"NifOsg::KeyframeController", "NifOsg::Emitter", "NifOsg::ParticleColorAffector", "Shader::AddedState", "Shader::RemovedAlphaFunc", "NifOsg::FlipController",
"NifOsg::ParticleSystem", "NifOsg::GravityAffector", "NifOsg::ParticleBomb", "NifOsg::GrowFadeAffector", "NifOsg::KeyframeController", "NifOsg::Emitter", "NifOsg::ParticleColorAffector",
"NifOsg::InverseWorldMatrix", "NifOsg::StaticBoundingBoxCallback", "NifOsg::GeomMorpherController", "NifOsg::ParticleSystem", "NifOsg::GravityAffector", "NifOsg::ParticleBomb",
"NifOsg::UpdateMorphGeometry", "NifOsg::UVController", "NifOsg::VisController", "osgMyGUI::Drawable", "NifOsg::GrowFadeAffector", "NifOsg::InverseWorldMatrix", "NifOsg::StaticBoundingBoxCallback",
"osg::DrawCallback", "osg::UniformBufferObject", "osgOQ::ClearQueriesCallback", "NifOsg::GeomMorpherController", "NifOsg::UpdateMorphGeometry", "NifOsg::UVController",
"osgOQ::RetrieveQueriesCallback", "osg::DummyObject" }; "NifOsg::VisController", "osgMyGUI::Drawable", "osg::DrawCallback", "osg::UniformBufferObject",
"osgOQ::ClearQueriesCallback", "osgOQ::RetrieveQueriesCallback", "osg::DummyObject" };
for (size_t i = 0; i < sizeof(ignore) / sizeof(ignore[0]); ++i) for (size_t i = 0; i < sizeof(ignore) / sizeof(ignore[0]); ++i)
{ {
mgr->addWrapper(makeDummySerializer(ignore[i])); mgr->addWrapper(makeDummySerializer(ignore[i]));