mirror of
https://github.com/OpenMW/openmw.git
synced 2025-02-06 09:45:33 +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:
commit
f9820e2061
5 changed files with 82 additions and 97 deletions
|
@ -433,8 +433,10 @@ namespace MWRender
|
|||
mViewer->getIncrementalCompileOperation()->setTargetFrameRate(Settings::cells().mTargetFramerate);
|
||||
}
|
||||
|
||||
mDebugDraw
|
||||
= std::make_unique<Debug::DebugDrawer>(mResourceSystem->getSceneManager()->getShaderManager(), mRootNode);
|
||||
mDebugDraw = new Debug::DebugDrawer(mResourceSystem->getSceneManager()->getShaderManager());
|
||||
mDebugDraw->setNodeMask(Mask_Debug);
|
||||
sceneRoot->addChild(mDebugDraw);
|
||||
|
||||
mResourceSystem->getSceneManager()->setIncrementalCompileOperation(mViewer->getIncrementalCompileOperation());
|
||||
|
||||
mEffectManager = std::make_unique<EffectManager>(sceneRoot, mResourceSystem);
|
||||
|
|
|
@ -338,7 +338,7 @@ namespace MWRender
|
|||
osg::ref_ptr<NpcAnimation> mPlayerAnimation;
|
||||
osg::ref_ptr<SceneUtil::PositionAttitudeTransform> mPlayerNode;
|
||||
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<SharedUniformStateUpdater> mSharedUniformStateUpdater;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
@ -232,7 +232,7 @@ namespace Debug
|
|||
auto vertices = new osg::Vec3Array;
|
||||
auto color = new osg::Vec3Array;
|
||||
lines.setDataVariance(osg::Object::STATIC);
|
||||
lines.setUseVertexArrayObject(true);
|
||||
lines.setUseVertexBufferObjects(true);
|
||||
lines.setUseDisplayList(false);
|
||||
lines.setCullingActive(false);
|
||||
|
||||
|
@ -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();
|
||||
|
@ -255,96 +265,63 @@ namespace Debug
|
|||
|
||||
const osg::StateSet* stateSet = getStateSet();
|
||||
|
||||
auto program = static_cast<const osg::Program*>(stateSet->getAttribute(osg::StateAttribute::PROGRAM));
|
||||
const osg::Program::PerContextProgram* pcp = program->getPCP(*state);
|
||||
if (!pcp)
|
||||
{
|
||||
return;
|
||||
}
|
||||
const osg::Program::PerContextProgram& pcp = *state->getLastAppliedProgramObject();
|
||||
auto transLocation = pcp.getUniformLocation(stateSet->getUniform("trans")->getNameID());
|
||||
auto colLocation = pcp.getUniformLocation(stateSet->getUniform("color")->getNameID());
|
||||
auto scaleLocation = pcp.getUniformLocation(stateSet->getUniform("scale")->getNameID());
|
||||
auto normalAsColorLocation = pcp.getUniformLocation(stateSet->getUniform("useNormalAsColor")->getNameID());
|
||||
|
||||
const osg::Uniform* uTrans = stateSet->getUniform("trans");
|
||||
const osg::Uniform* uCol = stateSet->getUniform("color");
|
||||
const osg::Uniform* uScale = stateSet->getUniform("scale");
|
||||
const osg::Uniform* uUseNormalAsColor = stateSet->getUniform("useNormalAsColor");
|
||||
auto drawPrimitive = [&](const osg::Drawable* primitive, const osg::Vec3f& pos, const osg::Vec3f& color,
|
||||
const osg::Vec3f& scale, const bool normalAsColor) {
|
||||
ext->glUniform3f(transLocation, pos.x(), pos.y(), pos.z());
|
||||
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());
|
||||
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);
|
||||
drawPrimitive(mLinesToDraw, { 0.f, 0.f, 0.f }, { 1.f, 1.f, 1.f }, { 1.f, 1.f, 1.f }, true);
|
||||
|
||||
for (const auto& shapeToDraw : mShapesToDraw)
|
||||
{
|
||||
osg::Vec3f translation = shapeToDraw.mPosition;
|
||||
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());
|
||||
|
||||
const osg::Geometry* geometry = nullptr;
|
||||
switch (shapeToDraw.mDrawShape)
|
||||
{
|
||||
case DrawShape::Cube:
|
||||
mCubeGeometry->drawImplementation(renderInfo);
|
||||
geometry = mCubeGeometry;
|
||||
break;
|
||||
case DrawShape::Cylinder:
|
||||
mCylinderGeometry->drawImplementation(renderInfo);
|
||||
geometry = mCylinderGeometry;
|
||||
break;
|
||||
case DrawShape::WireCube:
|
||||
mWireCubeGeometry->drawImplementation(renderInfo);
|
||||
geometry = mWireCubeGeometry;
|
||||
break;
|
||||
}
|
||||
drawPrimitive(geometry, shapeToDraw.mPosition, shapeToDraw.mColor, shapeToDraw.mDims, false);
|
||||
}
|
||||
mShapesToDraw.clear();
|
||||
static_cast<osg::Vec3Array*>(mLinesToDraw->getVertexArray())->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>
|
||||
{
|
||||
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)
|
||||
: mParentNode(std::move(parentNode))
|
||||
Debug::DebugDrawer::DebugDrawer(const DebugDrawer& copy, const osg::CopyOp& copyop)
|
||||
: Node(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 +355,17 @@ 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::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)
|
||||
{
|
||||
mCustomDebugDrawer[getIdexBufferWriteFromFrame(mCurrentFrame)]->mShapesToDraw.push_back(
|
||||
mCustomDebugDrawer[getIndexBufferWriteFromFrame(mCurrentFrame)]->mShapesToDraw.push_back(
|
||||
{ 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)
|
||||
{
|
||||
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<osg::Vec3Array*>(lines->getVertexArray());
|
||||
auto colors = static_cast<osg::Vec3Array*>(lines->getNormalArray());
|
||||
auto primitive = static_cast<osg::DrawArrays*>(lines->getPrimitiveSet(0));
|
||||
|
||||
vertices->push_back(start);
|
||||
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->dirty();
|
||||
|
||||
primitive->setCount(vertices->size());
|
||||
}
|
||||
|
|
|
@ -70,6 +70,9 @@ namespace Debug
|
|||
{
|
||||
public:
|
||||
DebugCustomDraw();
|
||||
DebugCustomDraw(const DebugCustomDraw& copy, const osg::CopyOp& copyop);
|
||||
|
||||
META_Node(Debug, DebugCustomDraw)
|
||||
|
||||
mutable std::vector<DrawCall> mShapesToDraw;
|
||||
osg::ref_ptr<osg::Geometry> mLinesToDraw;
|
||||
|
@ -81,12 +84,15 @@ namespace Debug
|
|||
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);
|
||||
~DebugDrawer();
|
||||
META_Node(Debug, DebugDrawer)
|
||||
|
||||
void traverse(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<osg::ref_ptr<DebugCustomDraw>, 2> mCustomDebugDrawer;
|
||||
osg::ref_ptr<osg::Group> mDebugDrawSceneObjects;
|
||||
osg::ref_ptr<osg::Group> mParentNode;
|
||||
};
|
||||
}
|
||||
#endif // !
|
||||
|
|
|
@ -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",
|
||||
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" };
|
||||
"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]));
|
||||
|
|
Loading…
Reference in a new issue