diff --git a/apps/openmw/mwrender/renderingmanager.cpp b/apps/openmw/mwrender/renderingmanager.cpp index 15faabb6df..978fe47f44 100644 --- a/apps/openmw/mwrender/renderingmanager.cpp +++ b/apps/openmw/mwrender/renderingmanager.cpp @@ -432,8 +432,9 @@ namespace MWRender mViewer->getIncrementalCompileOperation()->setTargetFrameRate(Settings::cells().mTargetFramerate); } - mDebugDraw - = std::make_unique(mResourceSystem->getSceneManager()->getShaderManager(), mRootNode); + mDebugDraw = new Debug::DebugDrawer(mResourceSystem->getSceneManager()->getShaderManager()); + mRootNode->addChild(mDebugDraw); + mResourceSystem->getSceneManager()->setIncrementalCompileOperation(mViewer->getIncrementalCompileOperation()); mEffectManager = std::make_unique(sceneRoot, mResourceSystem); diff --git a/apps/openmw/mwrender/renderingmanager.hpp b/apps/openmw/mwrender/renderingmanager.hpp index 22ef987c01..8f85ce6a3f 100644 --- a/apps/openmw/mwrender/renderingmanager.hpp +++ b/apps/openmw/mwrender/renderingmanager.hpp @@ -335,7 +335,7 @@ namespace MWRender osg::ref_ptr mPlayerAnimation; osg::ref_ptr mPlayerNode; std::unique_ptr mCamera; - std::unique_ptr mDebugDraw; + osg::ref_ptr mDebugDraw; osg::ref_ptr mStateUpdater; osg::ref_ptr mSharedUniformStateUpdater; diff --git a/components/debug/debugdraw.cpp b/components/debug/debugdraw.cpp index 32f71580a8..b83829facd 100644 --- a/components/debug/debugdraw.cpp +++ b/components/debug/debugdraw.cpp @@ -215,12 +215,12 @@ static void generateCylinder(osg::Geometry& geom, float radius, float height, in geom.addPrimitiveSet(indices); } -static int getIdexBufferReadFromFrame(const long long int& nFrame) +static int getIndexBufferReadFromFrame(const unsigned int& nFrame) { return nFrame % 2; } -static int getIdexBufferWriteFromFrame(const long long int& nFrame) +static int getIndexBufferWriteFromFrame(const unsigned int& nFrame) { return (nFrame + 1) % 2; } @@ -248,6 +248,16 @@ namespace Debug 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 { auto state = renderInfo.getState(); @@ -308,43 +318,23 @@ namespace Debug static_cast(mLinesToDraw->getVertexArray())->clear(); static_cast(mLinesToDraw->getNormalArray())->clear(); } - - class DebugDrawCallback : public SceneUtil::NodeCallback - { - 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(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 parentNode) - : mParentNode(std::move(parentNode)) +Debug::DebugDrawer::DebugDrawer(const DebugDrawer& copy, const osg::CopyOp& copyop) + : Drawable(copy, copyop) + , mCurrentFrame(copy.mCurrentFrame) + , mCustomDebugDrawer(copy.mCustomDebugDrawer) +{ +} + +Debug::DebugDrawer::DebugDrawer(Shader::ShaderManager& shaderManager) { mCurrentFrame = 0; auto program = shaderManager.getProgram("debug"); - mDebugDrawSceneObjects = new osg::Group; - mDebugDrawSceneObjects->setCullingActive(false); - osg::StateSet* stateset = mDebugDrawSceneObjects->getOrCreateStateSet(); + setCullingActive(false); + osg::StateSet* stateset = getOrCreateStateSet(); 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("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]->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(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) { - mCustomDebugDrawer[getIdexBufferWriteFromFrame(mCurrentFrame)]->mShapesToDraw.push_back( + mCustomDebugDrawer[getIndexBufferWriteFromFrame(mCurrentFrame)]->mShapesToDraw.push_back( { 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) { - 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) { - const int indexWrite = getIdexBufferWriteFromFrame(mCurrentFrame); + const int indexWrite = getIndexBufferWriteFromFrame(mCurrentFrame); const auto& lines = mCustomDebugDrawer[indexWrite]->mLinesToDraw; auto vertices = static_cast(lines->getVertexArray()); auto colors = static_cast(lines->getNormalArray()); diff --git a/components/debug/debugdraw.hpp b/components/debug/debugdraw.hpp index 659968d35a..610c89d656 100644 --- a/components/debug/debugdraw.hpp +++ b/components/debug/debugdraw.hpp @@ -70,6 +70,9 @@ namespace Debug { public: DebugCustomDraw(); + DebugCustomDraw(const DebugCustomDraw& copy, const osg::CopyOp& copyop); + + META_Object(Debug, DebugCustomDraw) mutable std::vector mShapesToDraw; osg::ref_ptr mLinesToDraw; @@ -81,12 +84,15 @@ namespace Debug 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 parentNode); - ~DebugDrawer(); + META_Object(Debug, DebugDrawer) + + void accept(osg::NodeVisitor& nv) override; void drawCube( 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); private: - long long int mCurrentFrame; + unsigned int mCurrentFrame; std::array, 2> mCustomDebugDrawer; - osg::ref_ptr mDebugDrawSceneObjects; - osg::ref_ptr mParentNode; }; } #endif // ! diff --git a/components/sceneutil/serialize.cpp b/components/sceneutil/serialize.cpp index 8d8acacae4..fa239e692f 100644 --- a/components/sceneutil/serialize.cpp +++ b/components/sceneutil/serialize.cpp @@ -175,18 +175,19 @@ namespace SceneUtil mgr->addWrapper(new GeometrySerializer); // ignore the below for now to avoid warning spam - const char* ignore[] = { "MWRender::PtrHolder", "Resource::TemplateRef", "Resource::TemplateMultiRef", - "SceneUtil::CompositeStateSetUpdater", "SceneUtil::UBOManager", "SceneUtil::LightListCallback", - "SceneUtil::LightManagerUpdateCallback", "SceneUtil::FFPLightStateAttribute", - "SceneUtil::UpdateRigBounds", "SceneUtil::UpdateRigGeometry", "SceneUtil::LightSource", - "SceneUtil::DisableLight", "SceneUtil::MWShadowTechnique", "SceneUtil::TextKeyMapHolder", - "Shader::AddedState", "Shader::RemovedAlphaFunc", "NifOsg::FlipController", - "NifOsg::KeyframeController", "NifOsg::Emitter", "NifOsg::ParticleColorAffector", - "NifOsg::ParticleSystem", "NifOsg::GravityAffector", "NifOsg::ParticleBomb", "NifOsg::GrowFadeAffector", - "NifOsg::InverseWorldMatrix", "NifOsg::StaticBoundingBoxCallback", "NifOsg::GeomMorpherController", - "NifOsg::UpdateMorphGeometry", "NifOsg::UVController", "NifOsg::VisController", "osgMyGUI::Drawable", - "osg::DrawCallback", "osg::UniformBufferObject", "osgOQ::ClearQueriesCallback", - "osgOQ::RetrieveQueriesCallback", "osg::DummyObject" }; + const char* ignore[] + = { "Debug::DebugDrawer", "MWRender::PtrHolder", "Resource::TemplateRef", "Resource::TemplateMultiRef", + "SceneUtil::CompositeStateSetUpdater", "SceneUtil::UBOManager", "SceneUtil::LightListCallback", + "SceneUtil::LightManagerUpdateCallback", "SceneUtil::FFPLightStateAttribute", + "SceneUtil::UpdateRigBounds", "SceneUtil::UpdateRigGeometry", "SceneUtil::LightSource", + "SceneUtil::DisableLight", "SceneUtil::MWShadowTechnique", "SceneUtil::TextKeyMapHolder", + "Shader::AddedState", "Shader::RemovedAlphaFunc", "NifOsg::FlipController", + "NifOsg::KeyframeController", "NifOsg::Emitter", "NifOsg::ParticleColorAffector", + "NifOsg::ParticleSystem", "NifOsg::GravityAffector", "NifOsg::ParticleBomb", + "NifOsg::GrowFadeAffector", "NifOsg::InverseWorldMatrix", "NifOsg::StaticBoundingBoxCallback", + "NifOsg::GeomMorpherController", "NifOsg::UpdateMorphGeometry", "NifOsg::UVController", + "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) { mgr->addWrapper(makeDummySerializer(ignore[i]));