1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-18 13:12:50 +00:00

WIP-ish glPolygonOffset for Bullet debug geometry

This commit is contained in:
AnyOldName3 2021-03-23 22:40:19 +00:00 committed by AnyOldName3
parent 8ad3d3d792
commit 8b4c2d205d
2 changed files with 99 additions and 40 deletions

View File

@ -8,7 +8,9 @@
#include <components/debug/debuglog.hpp>
#include <components/misc/convert.hpp>
#include <components/sceneutil/util.hpp>
#include <osg/PolygonMode>
#include <osg/PolygonOffset>
#include <osg/ShapeDrawable>
#include <osg/StateSet>
@ -33,50 +35,68 @@ DebugDrawer::DebugDrawer(osg::ref_ptr<osg::Group> parentNode, btCollisionWorld *
void DebugDrawer::createGeometry()
{
if (!mGeometry)
if (!mLinesGeometry)
{
mGeometry = new osg::Geometry;
mGeometry->setNodeMask(Mask_Debug);
mLinesGeometry = new osg::Geometry;
mTrisGeometry = new osg::Geometry;
mLinesGeometry->setNodeMask(Mask_Debug);
mTrisGeometry->setNodeMask(Mask_Debug);
mVertices = new osg::Vec3Array;
mColors = new osg::Vec4Array;
mLinesVertices = new osg::Vec3Array;
mTrisVertices = new osg::Vec3Array;
mLinesColors = new osg::Vec4Array;
mDrawArrays = new osg::DrawArrays(osg::PrimitiveSet::LINES);
mLinesDrawArrays = new osg::DrawArrays(osg::PrimitiveSet::LINES);
mTrisDrawArrays = new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES);
mGeometry->setUseDisplayList(false);
mGeometry->setVertexArray(mVertices);
mGeometry->setColorArray(mColors);
mGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
mGeometry->setDataVariance(osg::Object::DYNAMIC);
mGeometry->addPrimitiveSet(mDrawArrays);
mLinesGeometry->setUseDisplayList(false);
mLinesGeometry->setVertexArray(mLinesVertices);
mLinesGeometry->setColorArray(mLinesColors);
mLinesGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
mLinesGeometry->setDataVariance(osg::Object::DYNAMIC);
mLinesGeometry->addPrimitiveSet(mLinesDrawArrays);
osg::ref_ptr<osg::Material> material = new osg::Material;
material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
mGeometry->getOrCreateStateSet()->setAttribute(material);
mTrisGeometry->setUseDisplayList(false);
mTrisGeometry->setVertexArray(mTrisVertices);
mTrisGeometry->setDataVariance(osg::Object::DYNAMIC);
mTrisGeometry->addPrimitiveSet(mTrisDrawArrays);
mParentNode->addChild(mGeometry);
mParentNode->addChild(mLinesGeometry);
mParentNode->addChild(mTrisGeometry);
auto* stateSet = new osg::StateSet;
stateSet->setAttributeAndModes(new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE), osg::StateAttribute::ON);
stateSet->setAttributeAndModes(new osg::PolygonOffset(SceneUtil::getReverseZ() ? 1.0 : -1.0, SceneUtil::getReverseZ() ? 1.0 : -1.0));
osg::ref_ptr<osg::Material> material = new osg::Material;
material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
stateSet->setAttribute(material);
mTrisGeometry->setStateSet(stateSet);
mShapesRoot = new osg::Group;
mShapesRoot->setStateSet(stateSet);
mShapesRoot->setDataVariance(osg::Object::DYNAMIC);
mShapesRoot->setNodeMask(Mask_Debug);
mParentNode->addChild(mShapesRoot);
MWBase::Environment::get().getResourceSystem()->getSceneManager()->recreateShaders(mGeometry, "debug");
MWBase::Environment::get().getResourceSystem()->getSceneManager()->recreateShaders(mLinesGeometry, "debug");
MWBase::Environment::get().getResourceSystem()->getSceneManager()->recreateShaders(mTrisGeometry, "debug");
MWBase::Environment::get().getResourceSystem()->getSceneManager()->recreateShaders(mShapesRoot, "debug");
}
}
void DebugDrawer::destroyGeometry()
{
if (mGeometry)
if (mLinesGeometry)
{
mParentNode->removeChild(mGeometry);
mParentNode->removeChild(mLinesGeometry);
mParentNode->removeChild(mTrisGeometry);
mParentNode->removeChild(mShapesRoot);
mGeometry = nullptr;
mVertices = nullptr;
mDrawArrays = nullptr;
mLinesGeometry = nullptr;
mLinesVertices = nullptr;
mLinesColors = nullptr;
mLinesDrawArrays = nullptr;
mTrisGeometry = nullptr;
mTrisVertices = nullptr;
mTrisDrawArrays = nullptr;
}
}
@ -89,24 +109,58 @@ void DebugDrawer::step()
{
if (mDebugOn)
{
mVertices->clear();
mColors->clear();
mLinesVertices->clear();
mTrisVertices->clear();
mLinesColors->clear();
mShapesRoot->removeChildren(0, mShapesRoot->getNumChildren());
mWorld->debugDrawWorld();
showCollisions();
mDrawArrays->setCount(mVertices->size());
mVertices->dirty();
mColors->dirty();
mGeometry->dirtyBound();
mLinesDrawArrays->setCount(mLinesVertices->size());
mTrisDrawArrays->setCount(mTrisVertices->size());
mLinesVertices->dirty();
mTrisVertices->dirty();
mLinesColors->dirty();
mLinesGeometry->dirtyBound();
mTrisGeometry->dirtyBound();
}
}
void DebugDrawer::drawLine(const btVector3 &from, const btVector3 &to, const btVector3 &color)
{
mVertices->push_back(Misc::Convert::toOsg(from));
mVertices->push_back(Misc::Convert::toOsg(to));
mColors->push_back({1,1,1,1});
mColors->push_back({1,1,1,1});
mLinesVertices->push_back(Misc::Convert::toOsg(from));
mLinesVertices->push_back(Misc::Convert::toOsg(to));
mLinesColors->push_back({1,1,1,1});
mLinesColors->push_back({1,1,1,1});
size_t size = mLinesVertices->size();
if (size >= 6
&& (*mLinesVertices)[size - 1] == (*mLinesVertices)[size - 6]
&& (*mLinesVertices)[size - 2] == (*mLinesVertices)[size - 3]
&& (*mLinesVertices)[size - 4] == (*mLinesVertices)[size - 5])
{
mTrisVertices->push_back(mLinesVertices->back());
mLinesVertices->pop_back();
mLinesColors->pop_back();
mTrisVertices->push_back(mLinesVertices->back());
mLinesVertices->pop_back();
mLinesColors->pop_back();
mLinesVertices->pop_back();
mLinesColors->pop_back();
mTrisVertices->push_back(mLinesVertices->back());
mLinesVertices->pop_back();
mLinesColors->pop_back();
mLinesVertices->pop_back();
mLinesColors->pop_back();
mLinesVertices->pop_back();
mLinesColors->pop_back();
}
}
void DebugDrawer::drawTriangle(const btVector3& v0, const btVector3& v1, const btVector3& v2, const btVector3& color, btScalar)
{
mTrisVertices->push_back(Misc::Convert::toOsg(v0));
mTrisVertices->push_back(Misc::Convert::toOsg(v1));
mTrisVertices->push_back(Misc::Convert::toOsg(v2));
}
void DebugDrawer::addCollision(const btVector3& orig, const btVector3& normal)
@ -121,10 +175,10 @@ void DebugDrawer::showCollisions()
{
if (now - created < std::chrono::seconds(2))
{
mVertices->push_back(Misc::Convert::toOsg(from));
mVertices->push_back(Misc::Convert::toOsg(to));
mColors->push_back({1,0,0,1});
mColors->push_back({1,0,0,1});
mLinesVertices->push_back(Misc::Convert::toOsg(from));
mLinesVertices->push_back(Misc::Convert::toOsg(to));
mLinesColors->push_back({1,0,0,1});
mLinesColors->push_back({1,0,0,1});
}
}
mCollisionViews.erase(std::remove_if(mCollisionViews.begin(), mCollisionViews.end(),

View File

@ -37,10 +37,13 @@ private:
protected:
osg::ref_ptr<osg::Group> mParentNode;
btCollisionWorld *mWorld;
osg::ref_ptr<osg::Geometry> mGeometry;
osg::ref_ptr<osg::Vec3Array> mVertices;
osg::ref_ptr<osg::Vec4Array> mColors;
osg::ref_ptr<osg::DrawArrays> mDrawArrays;
osg::ref_ptr<osg::Geometry> mLinesGeometry;
osg::ref_ptr<osg::Geometry> mTrisGeometry;
osg::ref_ptr<osg::Vec3Array> mLinesVertices;
osg::ref_ptr<osg::Vec3Array> mTrisVertices;
osg::ref_ptr<osg::Vec4Array> mLinesColors;
osg::ref_ptr<osg::DrawArrays> mLinesDrawArrays;
osg::ref_ptr<osg::DrawArrays> mTrisDrawArrays;
bool mDebugOn;
@ -56,6 +59,8 @@ public:
void drawLine(const btVector3& from,const btVector3& to,const btVector3& color) override;
void drawTriangle(const btVector3& v0, const btVector3& v1, const btVector3& v2, const btVector3& color, btScalar) override;
void addCollision(const btVector3& orig, const btVector3& normal);
void showCollisions();