1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-02-01 01:45:36 +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/debug/debuglog.hpp>
#include <components/misc/convert.hpp> #include <components/misc/convert.hpp>
#include <components/sceneutil/util.hpp>
#include <osg/PolygonMode> #include <osg/PolygonMode>
#include <osg/PolygonOffset>
#include <osg/ShapeDrawable> #include <osg/ShapeDrawable>
#include <osg/StateSet> #include <osg/StateSet>
@ -33,50 +35,68 @@ DebugDrawer::DebugDrawer(osg::ref_ptr<osg::Group> parentNode, btCollisionWorld *
void DebugDrawer::createGeometry() void DebugDrawer::createGeometry()
{ {
if (!mGeometry) if (!mLinesGeometry)
{ {
mGeometry = new osg::Geometry; mLinesGeometry = new osg::Geometry;
mGeometry->setNodeMask(Mask_Debug); mTrisGeometry = new osg::Geometry;
mLinesGeometry->setNodeMask(Mask_Debug);
mTrisGeometry->setNodeMask(Mask_Debug);
mVertices = new osg::Vec3Array; mLinesVertices = new osg::Vec3Array;
mColors = new osg::Vec4Array; 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); mLinesGeometry->setUseDisplayList(false);
mGeometry->setVertexArray(mVertices); mLinesGeometry->setVertexArray(mLinesVertices);
mGeometry->setColorArray(mColors); mLinesGeometry->setColorArray(mLinesColors);
mGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); mLinesGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
mGeometry->setDataVariance(osg::Object::DYNAMIC); mLinesGeometry->setDataVariance(osg::Object::DYNAMIC);
mGeometry->addPrimitiveSet(mDrawArrays); mLinesGeometry->addPrimitiveSet(mLinesDrawArrays);
osg::ref_ptr<osg::Material> material = new osg::Material; mTrisGeometry->setUseDisplayList(false);
material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE); mTrisGeometry->setVertexArray(mTrisVertices);
mGeometry->getOrCreateStateSet()->setAttribute(material); mTrisGeometry->setDataVariance(osg::Object::DYNAMIC);
mTrisGeometry->addPrimitiveSet(mTrisDrawArrays);
mParentNode->addChild(mGeometry); mParentNode->addChild(mLinesGeometry);
mParentNode->addChild(mTrisGeometry);
auto* stateSet = new osg::StateSet; 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::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 = new osg::Group;
mShapesRoot->setStateSet(stateSet); mShapesRoot->setStateSet(stateSet);
mShapesRoot->setDataVariance(osg::Object::DYNAMIC); mShapesRoot->setDataVariance(osg::Object::DYNAMIC);
mShapesRoot->setNodeMask(Mask_Debug); mShapesRoot->setNodeMask(Mask_Debug);
mParentNode->addChild(mShapesRoot); 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() void DebugDrawer::destroyGeometry()
{ {
if (mGeometry) if (mLinesGeometry)
{ {
mParentNode->removeChild(mGeometry); mParentNode->removeChild(mLinesGeometry);
mParentNode->removeChild(mTrisGeometry);
mParentNode->removeChild(mShapesRoot); mParentNode->removeChild(mShapesRoot);
mGeometry = nullptr; mLinesGeometry = nullptr;
mVertices = nullptr; mLinesVertices = nullptr;
mDrawArrays = nullptr; mLinesColors = nullptr;
mLinesDrawArrays = nullptr;
mTrisGeometry = nullptr;
mTrisVertices = nullptr;
mTrisDrawArrays = nullptr;
} }
} }
@ -89,24 +109,58 @@ void DebugDrawer::step()
{ {
if (mDebugOn) if (mDebugOn)
{ {
mVertices->clear(); mLinesVertices->clear();
mColors->clear(); mTrisVertices->clear();
mLinesColors->clear();
mShapesRoot->removeChildren(0, mShapesRoot->getNumChildren()); mShapesRoot->removeChildren(0, mShapesRoot->getNumChildren());
mWorld->debugDrawWorld(); mWorld->debugDrawWorld();
showCollisions(); showCollisions();
mDrawArrays->setCount(mVertices->size()); mLinesDrawArrays->setCount(mLinesVertices->size());
mVertices->dirty(); mTrisDrawArrays->setCount(mTrisVertices->size());
mColors->dirty(); mLinesVertices->dirty();
mGeometry->dirtyBound(); mTrisVertices->dirty();
mLinesColors->dirty();
mLinesGeometry->dirtyBound();
mTrisGeometry->dirtyBound();
} }
} }
void DebugDrawer::drawLine(const btVector3 &from, const btVector3 &to, const btVector3 &color) void DebugDrawer::drawLine(const btVector3 &from, const btVector3 &to, const btVector3 &color)
{ {
mVertices->push_back(Misc::Convert::toOsg(from)); mLinesVertices->push_back(Misc::Convert::toOsg(from));
mVertices->push_back(Misc::Convert::toOsg(to)); mLinesVertices->push_back(Misc::Convert::toOsg(to));
mColors->push_back({1,1,1,1}); mLinesColors->push_back({1,1,1,1});
mColors->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) void DebugDrawer::addCollision(const btVector3& orig, const btVector3& normal)
@ -121,10 +175,10 @@ void DebugDrawer::showCollisions()
{ {
if (now - created < std::chrono::seconds(2)) if (now - created < std::chrono::seconds(2))
{ {
mVertices->push_back(Misc::Convert::toOsg(from)); mLinesVertices->push_back(Misc::Convert::toOsg(from));
mVertices->push_back(Misc::Convert::toOsg(to)); mLinesVertices->push_back(Misc::Convert::toOsg(to));
mColors->push_back({1,0,0,1}); mLinesColors->push_back({1,0,0,1});
mColors->push_back({1,0,0,1}); mLinesColors->push_back({1,0,0,1});
} }
} }
mCollisionViews.erase(std::remove_if(mCollisionViews.begin(), mCollisionViews.end(), mCollisionViews.erase(std::remove_if(mCollisionViews.begin(), mCollisionViews.end(),

View file

@ -37,10 +37,13 @@ private:
protected: protected:
osg::ref_ptr<osg::Group> mParentNode; osg::ref_ptr<osg::Group> mParentNode;
btCollisionWorld *mWorld; btCollisionWorld *mWorld;
osg::ref_ptr<osg::Geometry> mGeometry; osg::ref_ptr<osg::Geometry> mLinesGeometry;
osg::ref_ptr<osg::Vec3Array> mVertices; osg::ref_ptr<osg::Geometry> mTrisGeometry;
osg::ref_ptr<osg::Vec4Array> mColors; osg::ref_ptr<osg::Vec3Array> mLinesVertices;
osg::ref_ptr<osg::DrawArrays> mDrawArrays; 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; bool mDebugOn;
@ -56,6 +59,8 @@ public:
void drawLine(const btVector3& from,const btVector3& to,const btVector3& color) override; 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 addCollision(const btVector3& orig, const btVector3& normal);
void showCollisions(); void showCollisions();