mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-01-20 06:53:52 +00:00
Auto-generate the collision shape for native mesh formats
This commit is contained in:
parent
8cf57ef6ac
commit
e62470d674
3 changed files with 102 additions and 8 deletions
|
@ -605,7 +605,7 @@ namespace MWPhysics
|
||||||
// ---------------------------------------------------------------
|
// ---------------------------------------------------------------
|
||||||
|
|
||||||
PhysicsSystem::PhysicsSystem(Resource::ResourceSystem* resourceSystem, osg::ref_ptr<osg::Group> parentNode)
|
PhysicsSystem::PhysicsSystem(Resource::ResourceSystem* resourceSystem, osg::ref_ptr<osg::Group> parentNode)
|
||||||
: mShapeManager(new Resource::BulletShapeManager(resourceSystem->getVFS()))
|
: mShapeManager(new Resource::BulletShapeManager(resourceSystem->getVFS(), resourceSystem->getSceneManager()))
|
||||||
, mDebugDrawEnabled(false)
|
, mDebugDrawEnabled(false)
|
||||||
, mTimeAccum(0.0f)
|
, mTimeAccum(0.0f)
|
||||||
, mWaterHeight(0)
|
, mWaterHeight(0)
|
||||||
|
|
|
@ -1,16 +1,99 @@
|
||||||
#include "bulletshapemanager.hpp"
|
#include "bulletshapemanager.hpp"
|
||||||
|
|
||||||
|
#include <osg/NodeVisitor>
|
||||||
|
#include <osg/Geode>
|
||||||
|
#include <osg/TriangleFunctor>
|
||||||
|
|
||||||
|
#include <BulletCollision/CollisionShapes/btTriangleMesh.h>
|
||||||
|
|
||||||
#include <components/vfs/manager.hpp>
|
#include <components/vfs/manager.hpp>
|
||||||
|
|
||||||
#include <components/nifbullet/bulletnifloader.hpp>
|
#include <components/nifbullet/bulletnifloader.hpp>
|
||||||
|
|
||||||
#include "bulletshape.hpp"
|
#include "bulletshape.hpp"
|
||||||
|
#include "scenemanager.hpp"
|
||||||
|
|
||||||
|
|
||||||
namespace Resource
|
namespace Resource
|
||||||
{
|
{
|
||||||
|
|
||||||
BulletShapeManager::BulletShapeManager(const VFS::Manager* vfs)
|
struct GetTriangleFunctor
|
||||||
|
{
|
||||||
|
GetTriangleFunctor()
|
||||||
|
: mTriMesh(NULL)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void setTriMesh(btTriangleMesh* triMesh)
|
||||||
|
{
|
||||||
|
mTriMesh = triMesh;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setMatrix(const osg::Matrixf& matrix)
|
||||||
|
{
|
||||||
|
mMatrix = matrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline btVector3 toBullet(const osg::Vec3f& vec)
|
||||||
|
{
|
||||||
|
return btVector3(vec.x(), vec.y(), vec.z());
|
||||||
|
}
|
||||||
|
|
||||||
|
void inline operator()( const osg::Vec3 v1, const osg::Vec3 v2, const osg::Vec3 v3, bool _temp )
|
||||||
|
{
|
||||||
|
if (mTriMesh)
|
||||||
|
mTriMesh->addTriangle( toBullet(mMatrix.preMult(v1)), toBullet(mMatrix.preMult(v2)), toBullet(mMatrix.preMult(v3)));
|
||||||
|
}
|
||||||
|
|
||||||
|
btTriangleMesh* mTriMesh;
|
||||||
|
osg::Matrixf mMatrix;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Creates a BulletShape out of a Node hierarchy.
|
||||||
|
class NodeToShapeVisitor : public osg::NodeVisitor
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
NodeToShapeVisitor()
|
||||||
|
: osg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
|
||||||
|
, mTriangleMesh(NULL)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void apply(osg::Geode& geode)
|
||||||
|
{
|
||||||
|
for (unsigned int i=0; i<geode.getNumDrawables(); ++i)
|
||||||
|
apply(*geode.getDrawable(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void apply(osg::Drawable &drawable)
|
||||||
|
{
|
||||||
|
if (!mTriangleMesh)
|
||||||
|
mTriangleMesh = new btTriangleMesh;
|
||||||
|
|
||||||
|
osg::Matrixf worldMat = osg::computeLocalToWorld(getNodePath());
|
||||||
|
osg::TriangleFunctor<GetTriangleFunctor> functor;
|
||||||
|
functor.setTriMesh(mTriangleMesh);
|
||||||
|
functor.setMatrix(worldMat);
|
||||||
|
drawable.accept(functor);
|
||||||
|
}
|
||||||
|
|
||||||
|
osg::ref_ptr<BulletShape> getShape()
|
||||||
|
{
|
||||||
|
osg::ref_ptr<BulletShape> shape (new BulletShape);
|
||||||
|
TriangleMeshShape* meshShape = new TriangleMeshShape(mTriangleMesh, true);
|
||||||
|
shape->mCollisionShape = meshShape;
|
||||||
|
mTriangleMesh = NULL;
|
||||||
|
return shape;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
btTriangleMesh* mTriangleMesh;
|
||||||
|
};
|
||||||
|
|
||||||
|
BulletShapeManager::BulletShapeManager(const VFS::Manager* vfs, SceneManager* sceneMgr)
|
||||||
: mVFS(vfs)
|
: mVFS(vfs)
|
||||||
|
, mSceneManager(sceneMgr)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -37,12 +120,22 @@ osg::ref_ptr<BulletShapeInstance> BulletShapeManager::createInstance(const std::
|
||||||
if (extPos != std::string::npos && extPos+1 < normalized.size())
|
if (extPos != std::string::npos && extPos+1 < normalized.size())
|
||||||
ext = normalized.substr(extPos+1);
|
ext = normalized.substr(extPos+1);
|
||||||
|
|
||||||
if (ext != "nif")
|
if (ext == "nif")
|
||||||
return NULL;
|
{
|
||||||
|
|
||||||
NifBullet::BulletNifLoader loader;
|
NifBullet::BulletNifLoader loader;
|
||||||
// might be worth sharing NIFFiles with SceneManager in some way
|
// might be worth sharing NIFFiles with SceneManager in some way
|
||||||
shape = loader.load(Nif::NIFFilePtr(new Nif::NIFFile(file, normalized)));
|
shape = loader.load(Nif::NIFFilePtr(new Nif::NIFFile(file, normalized)));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// TODO: support .bullet shape files
|
||||||
|
|
||||||
|
osg::ref_ptr<const osg::Node> constNode (mSceneManager->getTemplate(normalized));
|
||||||
|
osg::ref_ptr<osg::Node> node (const_cast<osg::Node*>(constNode.get())); // const-trickery required because there is no const version of NodeVisitor
|
||||||
|
NodeToShapeVisitor visitor;
|
||||||
|
node->accept(visitor);
|
||||||
|
shape = visitor.getShape();
|
||||||
|
}
|
||||||
|
|
||||||
mIndex[normalized] = shape;
|
mIndex[normalized] = shape;
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,13 +23,14 @@ namespace Resource
|
||||||
class BulletShapeManager
|
class BulletShapeManager
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
BulletShapeManager(const VFS::Manager* vfs);
|
BulletShapeManager(const VFS::Manager* vfs, SceneManager* sceneMgr);
|
||||||
~BulletShapeManager();
|
~BulletShapeManager();
|
||||||
|
|
||||||
osg::ref_ptr<BulletShapeInstance> createInstance(const std::string& name);
|
osg::ref_ptr<BulletShapeInstance> createInstance(const std::string& name);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const VFS::Manager* mVFS;
|
const VFS::Manager* mVFS;
|
||||||
|
SceneManager* mSceneManager;
|
||||||
|
|
||||||
typedef std::map<std::string, osg::ref_ptr<BulletShape> > Index;
|
typedef std::map<std::string, osg::ref_ptr<BulletShape> > Index;
|
||||||
Index mIndex;
|
Index mIndex;
|
||||||
|
|
Loading…
Reference in a new issue