forked from teamnwah/openmw-tes3coop
Move BulletShapeManager and BulletShape to resource/
parent
eb2f16d682
commit
8cf57ef6ac
@ -0,0 +1,102 @@
|
|||||||
|
#include "bulletshape.hpp"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
#include <BulletCollision/CollisionShapes/btBoxShape.h>
|
||||||
|
#include <BulletCollision/CollisionShapes/btTriangleMesh.h>
|
||||||
|
#include <BulletCollision/CollisionShapes/btScaledBvhTriangleMeshShape.h>
|
||||||
|
#include <BulletCollision/CollisionShapes/btCompoundShape.h>
|
||||||
|
|
||||||
|
namespace Resource
|
||||||
|
{
|
||||||
|
|
||||||
|
BulletShape::BulletShape()
|
||||||
|
: mCollisionShape(NULL)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
BulletShape::~BulletShape()
|
||||||
|
{
|
||||||
|
deleteShape(mCollisionShape);
|
||||||
|
}
|
||||||
|
|
||||||
|
void BulletShape::deleteShape(btCollisionShape* shape)
|
||||||
|
{
|
||||||
|
if(shape!=NULL)
|
||||||
|
{
|
||||||
|
if(shape->isCompound())
|
||||||
|
{
|
||||||
|
btCompoundShape* ms = static_cast<btCompoundShape*>(shape);
|
||||||
|
int a = ms->getNumChildShapes();
|
||||||
|
for(int i=0; i <a;i++)
|
||||||
|
deleteShape(ms->getChildShape(i));
|
||||||
|
}
|
||||||
|
delete shape;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
btCollisionShape* BulletShape::duplicateCollisionShape(btCollisionShape *shape) const
|
||||||
|
{
|
||||||
|
if(shape->isCompound())
|
||||||
|
{
|
||||||
|
btCompoundShape *comp = static_cast<btCompoundShape*>(shape);
|
||||||
|
btCompoundShape *newShape = new btCompoundShape;
|
||||||
|
|
||||||
|
int numShapes = comp->getNumChildShapes();
|
||||||
|
for(int i = 0;i < numShapes;++i)
|
||||||
|
{
|
||||||
|
btCollisionShape *child = duplicateCollisionShape(comp->getChildShape(i));
|
||||||
|
btTransform trans = comp->getChildTransform(i);
|
||||||
|
newShape->addChildShape(trans, child);
|
||||||
|
}
|
||||||
|
|
||||||
|
return newShape;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(btBvhTriangleMeshShape* trishape = dynamic_cast<btBvhTriangleMeshShape*>(shape))
|
||||||
|
{
|
||||||
|
#if BT_BULLET_VERSION >= 283
|
||||||
|
btScaledBvhTriangleMeshShape* newShape = new btScaledBvhTriangleMeshShape(trishape, btVector3(1.f, 1.f, 1.f));
|
||||||
|
#else
|
||||||
|
// work around btScaledBvhTriangleMeshShape bug ( https://code.google.com/p/bullet/issues/detail?id=371 ) in older bullet versions
|
||||||
|
btTriangleMesh* oldMesh = static_cast<btTriangleMesh*>(trishape->getMeshInterface());
|
||||||
|
btTriangleMesh* newMesh = new btTriangleMesh(*oldMesh);
|
||||||
|
TriangleMeshShape* newShape = new TriangleMeshShape(newMesh, true);
|
||||||
|
#endif
|
||||||
|
return newShape;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (btBoxShape* boxshape = dynamic_cast<btBoxShape*>(shape))
|
||||||
|
{
|
||||||
|
return new btBoxShape(*boxshape);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw std::logic_error(std::string("Unhandled Bullet shape duplication: ")+shape->getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
btCollisionShape *BulletShape::getCollisionShape()
|
||||||
|
{
|
||||||
|
return mCollisionShape;
|
||||||
|
}
|
||||||
|
|
||||||
|
osg::ref_ptr<BulletShapeInstance> BulletShape::makeInstance()
|
||||||
|
{
|
||||||
|
osg::ref_ptr<BulletShapeInstance> instance (new BulletShapeInstance(this));
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
BulletShapeInstance::BulletShapeInstance(osg::ref_ptr<BulletShape> source)
|
||||||
|
: BulletShape()
|
||||||
|
, mSource(source)
|
||||||
|
{
|
||||||
|
mCollisionBoxHalfExtents = source->mCollisionBoxHalfExtents;
|
||||||
|
mCollisionBoxTranslate = source->mCollisionBoxTranslate;
|
||||||
|
|
||||||
|
mAnimatedShapes = source->mAnimatedShapes;
|
||||||
|
|
||||||
|
if (source->mCollisionShape)
|
||||||
|
mCollisionShape = duplicateCollisionShape(source->mCollisionShape);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,78 @@
|
|||||||
|
#ifndef OPENMW_COMPONENTS_RESOURCE_BULLETSHAPE_H
|
||||||
|
#define OPENMW_COMPONENTS_RESOURCE_BULLETSHAPE_H
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
#include <osg/Referenced>
|
||||||
|
#include <osg/ref_ptr>
|
||||||
|
#include <osg/Vec3f>
|
||||||
|
|
||||||
|
#include <BulletCollision/CollisionShapes/btBvhTriangleMeshShape.h>
|
||||||
|
|
||||||
|
class btCollisionShape;
|
||||||
|
|
||||||
|
namespace Resource
|
||||||
|
{
|
||||||
|
|
||||||
|
class BulletShapeInstance;
|
||||||
|
class BulletShape : public osg::Referenced
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
BulletShape();
|
||||||
|
virtual ~BulletShape();
|
||||||
|
|
||||||
|
btCollisionShape* mCollisionShape;
|
||||||
|
|
||||||
|
// Used for actors. Note, ideally actors would use a separate loader - as it is
|
||||||
|
// we have to keep a redundant copy of the actor model around in mCollisionShape, which isn't used.
|
||||||
|
// For now, use one file <-> one resource for simplicity.
|
||||||
|
osg::Vec3f mCollisionBoxHalfExtents;
|
||||||
|
osg::Vec3f mCollisionBoxTranslate;
|
||||||
|
|
||||||
|
// Stores animated collision shapes. If any collision nodes in the NIF are animated, then mCollisionShape
|
||||||
|
// will be a btCompoundShape (which consists of one or more child shapes).
|
||||||
|
// In this map, for each animated collision shape,
|
||||||
|
// we store the node's record index mapped to the child index of the shape in the btCompoundShape.
|
||||||
|
std::map<int, int> mAnimatedShapes;
|
||||||
|
|
||||||
|
osg::ref_ptr<BulletShapeInstance> makeInstance();
|
||||||
|
|
||||||
|
btCollisionShape* duplicateCollisionShape(btCollisionShape* shape) const;
|
||||||
|
|
||||||
|
btCollisionShape* getCollisionShape();
|
||||||
|
|
||||||
|
private:
|
||||||
|
void deleteShape(btCollisionShape* shape);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// An instance of a BulletShape that may have its own unique scaling set on the mCollisionShape.
|
||||||
|
// Vertex data is shallow-copied where possible. A ref_ptr to the original shape is held to keep vertex pointers intact.
|
||||||
|
class BulletShapeInstance : public BulletShape
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
BulletShapeInstance(osg::ref_ptr<BulletShape> source);
|
||||||
|
|
||||||
|
private:
|
||||||
|
osg::ref_ptr<BulletShape> mSource;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Subclass btBhvTriangleMeshShape to auto-delete the meshInterface
|
||||||
|
struct TriangleMeshShape : public btBvhTriangleMeshShape
|
||||||
|
{
|
||||||
|
TriangleMeshShape(btStridingMeshInterface* meshInterface, bool useQuantizedAabbCompression)
|
||||||
|
: btBvhTriangleMeshShape(meshInterface, useQuantizedAabbCompression)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~TriangleMeshShape()
|
||||||
|
{
|
||||||
|
delete getTriangleInfoMap();
|
||||||
|
delete m_meshInterface;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue