1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-12-14 18:13:06 +00:00

Merge branch 'debugdrawdebugged' into 'master'

Try to uncursify debug primitive drawer (#7767)

Closes #7767

See merge request OpenMW/openmw!3852
This commit is contained in:
psi29a 2024-02-17 18:56:21 +00:00
commit f9820e2061
5 changed files with 82 additions and 97 deletions

View file

@ -433,8 +433,10 @@ 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); mDebugDraw->setNodeMask(Mask_Debug);
sceneRoot->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

@ -338,7 +338,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;
} }
@ -232,7 +232,7 @@ namespace Debug
auto vertices = new osg::Vec3Array; auto vertices = new osg::Vec3Array;
auto color = new osg::Vec3Array; auto color = new osg::Vec3Array;
lines.setDataVariance(osg::Object::STATIC); lines.setDataVariance(osg::Object::STATIC);
lines.setUseVertexArrayObject(true); lines.setUseVertexBufferObjects(true);
lines.setUseDisplayList(false); lines.setUseDisplayList(false);
lines.setCullingActive(false); lines.setCullingActive(false);
@ -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();
@ -255,96 +265,63 @@ namespace Debug
const osg::StateSet* stateSet = getStateSet(); const osg::StateSet* stateSet = getStateSet();
auto program = static_cast<const osg::Program*>(stateSet->getAttribute(osg::StateAttribute::PROGRAM)); const osg::Program::PerContextProgram& pcp = *state->getLastAppliedProgramObject();
const osg::Program::PerContextProgram* pcp = program->getPCP(*state); auto transLocation = pcp.getUniformLocation(stateSet->getUniform("trans")->getNameID());
if (!pcp) auto colLocation = pcp.getUniformLocation(stateSet->getUniform("color")->getNameID());
{ auto scaleLocation = pcp.getUniformLocation(stateSet->getUniform("scale")->getNameID());
return; auto normalAsColorLocation = pcp.getUniformLocation(stateSet->getUniform("useNormalAsColor")->getNameID());
}
const osg::Uniform* uTrans = stateSet->getUniform("trans"); auto drawPrimitive = [&](const osg::Drawable* primitive, const osg::Vec3f& pos, const osg::Vec3f& color,
const osg::Uniform* uCol = stateSet->getUniform("color"); const osg::Vec3f& scale, const bool normalAsColor) {
const osg::Uniform* uScale = stateSet->getUniform("scale"); ext->glUniform3f(transLocation, pos.x(), pos.y(), pos.z());
const osg::Uniform* uUseNormalAsColor = stateSet->getUniform("useNormalAsColor"); ext->glUniform3f(colLocation, color.x(), color.y(), color.z());
ext->glUniform3f(scaleLocation, scale.x(), scale.y(), scale.z());
ext->glUniform1i(normalAsColorLocation, normalAsColor);
primitive->drawImplementation(renderInfo);
};
auto transLocation = pcp->getUniformLocation(uTrans->getNameID()); drawPrimitive(mLinesToDraw, { 0.f, 0.f, 0.f }, { 1.f, 1.f, 1.f }, { 1.f, 1.f, 1.f }, true);
auto colLocation = pcp->getUniformLocation(uCol->getNameID());
auto scaleLocation = pcp->getUniformLocation(uScale->getNameID());
auto normalAsColorLocation = pcp->getUniformLocation(uUseNormalAsColor->getNameID());
ext->glUniform3f(transLocation, 0., 0., 0.);
ext->glUniform3f(colLocation, 1., 1., 1.);
ext->glUniform3f(scaleLocation, 1., 1., 1.);
ext->glUniform1i(normalAsColorLocation, true);
mLinesToDraw->drawImplementation(renderInfo);
ext->glUniform1i(normalAsColorLocation, false);
for (const auto& shapeToDraw : mShapesToDraw) for (const auto& shapeToDraw : mShapesToDraw)
{ {
osg::Vec3f translation = shapeToDraw.mPosition; const osg::Geometry* geometry = nullptr;
osg::Vec3f color = shapeToDraw.mColor;
osg::Vec3f scale = shapeToDraw.mDims;
ext->glUniform3f(transLocation, translation.x(), translation.y(), translation.z());
ext->glUniform3f(colLocation, color.x(), color.y(), color.z());
ext->glUniform3f(scaleLocation, scale.x(), scale.y(), scale.z());
switch (shapeToDraw.mDrawShape) switch (shapeToDraw.mDrawShape)
{ {
case DrawShape::Cube: case DrawShape::Cube:
mCubeGeometry->drawImplementation(renderInfo); geometry = mCubeGeometry;
break; break;
case DrawShape::Cylinder: case DrawShape::Cylinder:
mCylinderGeometry->drawImplementation(renderInfo); geometry = mCylinderGeometry;
break; break;
case DrawShape::WireCube: case DrawShape::WireCube:
mWireCubeGeometry->drawImplementation(renderInfo); geometry = mWireCubeGeometry;
break; break;
} }
drawPrimitive(geometry, shapeToDraw.mPosition, shapeToDraw.mColor, shapeToDraw.mDims, false);
} }
mShapesToDraw.clear(); mShapesToDraw.clear();
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();
static_cast<osg::DrawArrays*>(mLinesToDraw->getPrimitiveSet(0))->setCount(0);
pcp.resetAppliedUniforms();
}
} }
class DebugDrawCallback : public SceneUtil::NodeCallback<DebugDrawCallback> Debug::DebugDrawer::DebugDrawer(const DebugDrawer& copy, const osg::CopyOp& copyop)
{ : Node(copy, copyop)
public: , mCurrentFrame(copy.mCurrentFrame)
DebugDrawCallback(Debug::DebugDrawer& debugDrawer) , mCustomDebugDrawer(copy.mCustomDebugDrawer)
: mDebugDrawer(debugDrawer)
{ {
} }
void operator()(osg::Node* node, osg::NodeVisitor* nv) Debug::DebugDrawer::DebugDrawer(Shader::ShaderManager& shaderManager)
{
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)
: mParentNode(std::move(parentNode))
{ {
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 +355,17 @@ 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::traverse(osg::NodeVisitor& nv)
{ {
mParentNode->removeChild(mDebugDrawSceneObjects); mCurrentFrame = nv.getTraversalNumber();
mCustomDebugDrawer[getIndexBufferReadFromFrame(mCurrentFrame)]->accept(nv);
} }
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,15 +378,16 @@ 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());
auto primitive = static_cast<osg::DrawArrays*>(lines->getPrimitiveSet(0));
vertices->push_back(start); vertices->push_back(start);
vertices->push_back(end); vertices->push_back(end);
@ -420,4 +396,6 @@ void Debug::DebugDrawer::addLine(const osg::Vec3& start, const osg::Vec3& end, c
colors->push_back(color); colors->push_back(color);
colors->push_back(color); colors->push_back(color);
colors->dirty(); colors->dirty();
primitive->setCount(vertices->size());
} }

View file

@ -70,6 +70,9 @@ namespace Debug
{ {
public: public:
DebugCustomDraw(); DebugCustomDraw();
DebugCustomDraw(const DebugCustomDraw& copy, const osg::CopyOp& copyop);
META_Node(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::Node
{ {
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_Node(Debug, DebugDrawer)
~DebugDrawer();
void traverse(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[]
= { "Debug::DebugDrawer", "MWRender::PtrHolder", "Resource::TemplateRef", "Resource::TemplateMultiRef",
"SceneUtil::CompositeStateSetUpdater", "SceneUtil::UBOManager", "SceneUtil::LightListCallback", "SceneUtil::CompositeStateSetUpdater", "SceneUtil::UBOManager", "SceneUtil::LightListCallback",
"SceneUtil::LightManagerUpdateCallback", "SceneUtil::FFPLightStateAttribute", "SceneUtil::LightManagerUpdateCallback", "SceneUtil::FFPLightStateAttribute",
"SceneUtil::UpdateRigBounds", "SceneUtil::UpdateRigGeometry", "SceneUtil::LightSource", "SceneUtil::UpdateRigBounds", "SceneUtil::UpdateRigGeometry", "SceneUtil::LightSource",
"SceneUtil::DisableLight", "SceneUtil::MWShadowTechnique", "SceneUtil::TextKeyMapHolder", "SceneUtil::DisableLight", "SceneUtil::MWShadowTechnique", "SceneUtil::TextKeyMapHolder",
"Shader::AddedState", "Shader::RemovedAlphaFunc", "NifOsg::FlipController", "Shader::AddedState", "Shader::RemovedAlphaFunc", "NifOsg::FlipController",
"NifOsg::KeyframeController", "NifOsg::Emitter", "NifOsg::ParticleColorAffector", "NifOsg::KeyframeController", "NifOsg::Emitter", "NifOsg::ParticleColorAffector",
"NifOsg::ParticleSystem", "NifOsg::GravityAffector", "NifOsg::ParticleBomb", "NifOsg::GrowFadeAffector", "NifOsg::ParticleSystem", "NifOsg::GravityAffector", "NifOsg::ParticleBomb",
"NifOsg::InverseWorldMatrix", "NifOsg::StaticBoundingBoxCallback", "NifOsg::GeomMorpherController", "NifOsg::GrowFadeAffector", "NifOsg::InverseWorldMatrix", "NifOsg::StaticBoundingBoxCallback",
"NifOsg::UpdateMorphGeometry", "NifOsg::UVController", "NifOsg::VisController", "osgMyGUI::Drawable", "NifOsg::GeomMorpherController", "NifOsg::UpdateMorphGeometry", "NifOsg::UVController",
"osg::DrawCallback", "osg::UniformBufferObject", "osgOQ::ClearQueriesCallback", "NifOsg::VisController", "osgMyGUI::Drawable", "osg::DrawCallback", "osg::UniformBufferObject",
"osgOQ::RetrieveQueriesCallback", "osg::DummyObject" }; "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]));