1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-04-02 06:36:39 +00:00

Use unique_ptr to store btCollisionShape in BulletShape

This commit is contained in:
elsid 2021-10-30 03:06:22 +02:00
parent 80e3623d9a
commit b905dd17c3
No known key found for this signature in database
GPG key ID: B845CB9FEE18AB40
6 changed files with 55 additions and 60 deletions

View file

@ -117,7 +117,7 @@ namespace
osg::ref_ptr<const Resource::BulletShapeInstance> makeBulletShapeInstance(std::unique_ptr<T>&& shape)
{
osg::ref_ptr<Resource::BulletShape> bulletShape(new Resource::BulletShape);
bulletShape->mCollisionShape = std::move(shape).release();
bulletShape->mCollisionShape.reset(std::move(shape).release());
return new Resource::BulletShapeInstance(bulletShape);
}
@ -466,7 +466,7 @@ namespace
}};
std::unique_ptr<btHeightfieldTerrainShape> shapePtr = makeSquareHeightfieldTerrainShape(heightfieldData);
shapePtr->setLocalScaling(btVector3(128, 128, 1));
bulletShape->mCollisionShape = shapePtr.release();
bulletShape->mCollisionShape.reset(shapePtr.release());
std::array<btScalar, 5 * 5> heightfieldDataAvoid {{
-25, -25, -25, -25, -25,
@ -477,7 +477,7 @@ namespace
}};
std::unique_ptr<btHeightfieldTerrainShape> shapeAvoidPtr = makeSquareHeightfieldTerrainShape(heightfieldDataAvoid);
shapeAvoidPtr->setLocalScaling(btVector3(128, 128, 1));
bulletShape->mAvoidCollisionShape = shapeAvoidPtr.release();
bulletShape->mAvoidCollisionShape.reset(shapeAvoidPtr.release());
osg::ref_ptr<const Resource::BulletShapeInstance> instance(new Resource::BulletShapeInstance(bulletShape));

View file

@ -160,8 +160,8 @@ namespace Resource
{
static bool operator ==(const Resource::BulletShape& lhs, const Resource::BulletShape& rhs)
{
return compareObjects(lhs.mCollisionShape, rhs.mCollisionShape)
&& compareObjects(lhs.mAvoidCollisionShape, rhs.mAvoidCollisionShape)
return compareObjects(lhs.mCollisionShape.get(), rhs.mCollisionShape.get())
&& compareObjects(lhs.mAvoidCollisionShape.get(), rhs.mAvoidCollisionShape.get())
&& lhs.mCollisionBox.mExtents == rhs.mCollisionBox.mExtents
&& lhs.mCollisionBox.mCenter == rhs.mCollisionBox.mCenter
&& lhs.mAnimatedShapes == rhs.mAnimatedShapes;
@ -170,8 +170,8 @@ namespace Resource
static std::ostream& operator <<(std::ostream& stream, const Resource::BulletShape& value)
{
return stream << "Resource::BulletShape {"
<< value.mCollisionShape << ", "
<< value.mAvoidCollisionShape << ", "
<< value.mCollisionShape.get() << ", "
<< value.mAvoidCollisionShape.get() << ", "
<< "osg::Vec3f {" << value.mCollisionBox.mExtents << "}" << ", "
<< "osg::Vec3f {" << value.mCollisionBox.mCenter << "}" << ", "
<< value.mAnimatedShapes
@ -446,7 +446,7 @@ namespace
std::unique_ptr<btBoxShape> box(new btBoxShape(btVector3(1, 2, 3)));
std::unique_ptr<btCompoundShape> shape(new btCompoundShape);
shape->addChildShape(btTransform(btMatrix3x3::getIdentity(), btVector3(-1, -2, -3)), box.release());
expected.mCollisionShape = shape.release();
expected.mCollisionShape.reset(shape.release());
EXPECT_EQ(*result, expected);
}
@ -471,7 +471,7 @@ namespace
std::unique_ptr<btBoxShape> box(new btBoxShape(btVector3(1, 2, 3)));
std::unique_ptr<btCompoundShape> shape(new btCompoundShape);
shape->addChildShape(btTransform(btMatrix3x3::getIdentity(), btVector3(-1, -2, -3)), box.release());
expected.mCollisionShape = shape.release();
expected.mCollisionShape.reset(shape.release());
EXPECT_EQ(*result, expected);
}
@ -501,7 +501,7 @@ namespace
std::unique_ptr<btBoxShape> box(new btBoxShape(btVector3(1, 2, 3)));
std::unique_ptr<btCompoundShape> shape(new btCompoundShape);
shape->addChildShape(btTransform(btMatrix3x3::getIdentity(), btVector3(-1, -2, -3)), box.release());
expected.mCollisionShape = shape.release();
expected.mCollisionShape.reset(shape.release());
EXPECT_EQ(*result, expected);
}
@ -536,7 +536,7 @@ namespace
std::unique_ptr<btBoxShape> box(new btBoxShape(btVector3(1, 2, 3)));
std::unique_ptr<btCompoundShape> shape(new btCompoundShape);
shape->addChildShape(btTransform(btMatrix3x3::getIdentity(), btVector3(-1, -2, -3)), box.release());
expected.mCollisionShape = shape.release();
expected.mCollisionShape.reset(shape.release());
EXPECT_EQ(*result, expected);
}
@ -571,7 +571,7 @@ namespace
std::unique_ptr<btBoxShape> box(new btBoxShape(btVector3(4, 5, 6)));
std::unique_ptr<btCompoundShape> shape(new btCompoundShape);
shape->addChildShape(btTransform(btMatrix3x3::getIdentity(), btVector3(-4, -5, -6)), box.release());
expected.mCollisionShape = shape.release();
expected.mCollisionShape.reset(shape.release());
EXPECT_EQ(*result, expected);
}
@ -605,7 +605,7 @@ namespace
std::unique_ptr<btTriangleMesh> triangles(new btTriangleMesh(false));
triangles->addTriangle(btVector3(0, 0, 0), btVector3(1, 0, 0), btVector3(1, 1, 0));
Resource::BulletShape expected;
expected.mCollisionShape = new Resource::TriangleMeshShape(triangles.release(), true);
expected.mCollisionShape.reset(new Resource::TriangleMeshShape(triangles.release(), true));
EXPECT_EQ(*result, expected);
}
@ -641,7 +641,7 @@ namespace
std::unique_ptr<btTriangleMesh> triangles(new btTriangleMesh(false));
triangles->addTriangle(btVector3(0, 0, 0), btVector3(1, 0, 0), btVector3(1, 1, 0));
Resource::BulletShape expected;
expected.mCollisionShape = new Resource::TriangleMeshShape(triangles.release(), true);
expected.mCollisionShape.reset(new Resource::TriangleMeshShape(triangles.release(), true));
EXPECT_EQ(*result, expected);
}
@ -659,7 +659,7 @@ namespace
std::unique_ptr<btTriangleMesh> triangles(new btTriangleMesh(false));
triangles->addTriangle(btVector3(0, 0, 0), btVector3(1, 0, 0), btVector3(1, 1, 0));
Resource::BulletShape expected;
expected.mCollisionShape = new Resource::TriangleMeshShape(triangles.release(), true);
expected.mCollisionShape.reset(new Resource::TriangleMeshShape(triangles.release(), true));
EXPECT_EQ(*result, expected);
}
@ -680,7 +680,7 @@ namespace
triangles->addTriangle(btVector3(0, 0, 1), btVector3(1, 0, 1), btVector3(1, 1, 1));
triangles->addTriangle(btVector3(0, 0, 0), btVector3(1, 0, 0), btVector3(1, 1, 0));
Resource::BulletShape expected;
expected.mCollisionShape = new Resource::TriangleMeshShape(triangles.release(), true);
expected.mCollisionShape.reset(new Resource::TriangleMeshShape(triangles.release(), true));
EXPECT_EQ(*result, expected);
}
@ -698,7 +698,7 @@ namespace
std::unique_ptr<btTriangleMesh> triangles(new btTriangleMesh(false));
triangles->addTriangle(btVector3(0, 0, 0), btVector3(1, 0, 0), btVector3(1, 1, 0));
Resource::BulletShape expected;
expected.mCollisionShape = new Resource::TriangleMeshShape(triangles.release(), true);
expected.mCollisionShape.reset(new Resource::TriangleMeshShape(triangles.release(), true));
EXPECT_EQ(*result, expected);
}
@ -720,7 +720,7 @@ namespace
std::unique_ptr<btCompoundShape> shape(new btCompoundShape);
shape->addChildShape(mResultTransform, mesh.release());
Resource::BulletShape expected;
expected.mCollisionShape = shape.release();
expected.mCollisionShape.reset(shape.release());
expected.mAnimatedShapes = {{-1, 0}};
EXPECT_EQ(*result, expected);
@ -746,7 +746,7 @@ namespace
std::unique_ptr<btCompoundShape> shape(new btCompoundShape);
shape->addChildShape(mResultTransform2, mesh.release());
Resource::BulletShape expected;
expected.mCollisionShape = shape.release();
expected.mCollisionShape.reset(shape.release());
expected.mAnimatedShapes = {{-1, 0}};
EXPECT_EQ(*result, expected);
@ -784,7 +784,7 @@ namespace
shape->addChildShape(mResultTransform, mesh.release());
shape->addChildShape(mResultTransform, mesh2.release());
Resource::BulletShape expected;
expected.mCollisionShape = shape.release();
expected.mCollisionShape.reset(shape.release());
expected.mAnimatedShapes = {{-1, 0}};
EXPECT_EQ(*result, expected);
@ -813,7 +813,7 @@ namespace
std::unique_ptr<btCompoundShape> shape(new btCompoundShape);
shape->addChildShape(mResultTransform2, mesh.release());
Resource::BulletShape expected;
expected.mCollisionShape = shape.release();
expected.mCollisionShape.reset(shape.release());
expected.mAnimatedShapes = {{-1, 0}};
EXPECT_EQ(*result, expected);
@ -854,7 +854,7 @@ namespace
shape->addChildShape(mResultTransform2, mesh2.release());
shape->addChildShape(btTransform::getIdentity(), mesh.release());
Resource::BulletShape expected;
expected.mCollisionShape = shape.release();
expected.mCollisionShape.reset(shape.release());
expected.mAnimatedShapes = {{-1, 0}};
EXPECT_EQ(*result, expected);
@ -873,7 +873,7 @@ namespace
std::unique_ptr<btTriangleMesh> triangles(new btTriangleMesh(false));
triangles->addTriangle(btVector3(0, 0, 0), btVector3(1, 0, 0), btVector3(1, 1, 0));
Resource::BulletShape expected;
expected.mAvoidCollisionShape = new Resource::TriangleMeshShape(triangles.release(), false);
expected.mAvoidCollisionShape.reset(new Resource::TriangleMeshShape(triangles.release(), false));
EXPECT_EQ(*result, expected);
}
@ -979,7 +979,7 @@ namespace
std::unique_ptr<btTriangleMesh> triangles(new btTriangleMesh(false));
triangles->addTriangle(btVector3(0, 0, 0), btVector3(1, 0, 0), btVector3(1, 1, 0));
Resource::BulletShape expected;
expected.mCollisionShape = new Resource::TriangleMeshShape(triangles.release(), true);
expected.mCollisionShape.reset(new Resource::TriangleMeshShape(triangles.release(), true));
EXPECT_EQ(*result, expected);
}

View file

@ -152,7 +152,7 @@ osg::ref_ptr<Resource::BulletShape> BulletNifLoader::load(const Nif::File& nif)
compound->addChildShape(transform, boxShape.get());
boxShape.release();
mShape->mCollisionShape = compound.release();
mShape->mCollisionShape.reset(compound.release());
return mShape;
}
}
@ -179,17 +179,17 @@ osg::ref_ptr<Resource::BulletShape> BulletNifLoader::load(const Nif::File& nif)
child.release();
mStaticMesh.release();
}
mShape->mCollisionShape = mCompoundShape.release();
mShape->mCollisionShape.reset(mCompoundShape.release());
}
else if (mStaticMesh)
{
mShape->mCollisionShape = new Resource::TriangleMeshShape(mStaticMesh.get(), true);
mShape->mCollisionShape.reset(new Resource::TriangleMeshShape(mStaticMesh.get(), true));
mStaticMesh.release();
}
if (mAvoidStaticMesh)
{
mShape->mAvoidCollisionShape = new Resource::TriangleMeshShape(mAvoidStaticMesh.get(), false);
mShape->mAvoidCollisionShape.reset(new Resource::TriangleMeshShape(mAvoidStaticMesh.get(), false));
mAvoidStaticMesh.release();
}

View file

@ -12,7 +12,7 @@ namespace Resource
{
namespace
{
btCollisionShape* duplicateCollisionShape(const btCollisionShape *shape)
CollisionShapePtr duplicateCollisionShape(const btCollisionShape *shape)
{
if (shape == nullptr)
return nullptr;
@ -20,13 +20,13 @@ namespace
if (shape->isCompound())
{
const btCompoundShape *comp = static_cast<const btCompoundShape*>(shape);
btCompoundShape* newShape = new btCompoundShape;
std::unique_ptr<btCompoundShape, DeleteCollisionShape> newShape(new btCompoundShape);
for (int i = 0, n = comp->getNumChildShapes(); i < n; ++i)
{
btCollisionShape* child = duplicateCollisionShape(comp->getChildShape(i));
auto child = duplicateCollisionShape(comp->getChildShape(i));
const btTransform& trans = comp->getChildTransform(i);
newShape->addChildShape(trans, child);
newShape->addChildShape(trans, child.release());
}
return newShape;
@ -35,17 +35,17 @@ namespace
if (shape->getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE)
{
const btBvhTriangleMeshShape* trishape = static_cast<const btBvhTriangleMeshShape*>(shape);
return new btScaledBvhTriangleMeshShape(const_cast<btBvhTriangleMeshShape*>(trishape), btVector3(1.f, 1.f, 1.f));
return CollisionShapePtr(new btScaledBvhTriangleMeshShape(const_cast<btBvhTriangleMeshShape*>(trishape), btVector3(1.f, 1.f, 1.f)));
}
if (shape->getShapeType() == BOX_SHAPE_PROXYTYPE)
{
const btBoxShape* boxshape = static_cast<const btBoxShape*>(shape);
return new btBoxShape(*boxshape);
return CollisionShapePtr(new btBoxShape(*boxshape));
}
if (shape->getShapeType() == TERRAIN_SHAPE_PROXYTYPE)
return new btHeightfieldTerrainShape(static_cast<const btHeightfieldTerrainShape&>(*shape));
return CollisionShapePtr(new btHeightfieldTerrainShape(static_cast<const btHeightfieldTerrainShape&>(*shape)));
throw std::logic_error(std::string("Unhandled Bullet shape duplication: ") + shape->getName());
}
@ -64,37 +64,27 @@ namespace
}
}
BulletShape::BulletShape()
: mCollisionShape(nullptr)
, mAvoidCollisionShape(nullptr)
void DeleteCollisionShape::operator()(btCollisionShape* shape) const
{
deleteShape(shape);
}
BulletShape::BulletShape(const BulletShape &copy, const osg::CopyOp &copyop)
: mCollisionShape(duplicateCollisionShape(copy.mCollisionShape))
, mAvoidCollisionShape(duplicateCollisionShape(copy.mAvoidCollisionShape))
: mCollisionShape(duplicateCollisionShape(copy.mCollisionShape.get()))
, mAvoidCollisionShape(duplicateCollisionShape(copy.mAvoidCollisionShape.get()))
, mCollisionBox(copy.mCollisionBox)
, mAnimatedShapes(copy.mAnimatedShapes)
{
}
BulletShape::~BulletShape()
{
if (mAvoidCollisionShape != nullptr)
deleteShape(mAvoidCollisionShape);
if (mCollisionShape != nullptr)
deleteShape(mCollisionShape);
}
btCollisionShape *BulletShape::getCollisionShape() const
{
return mCollisionShape;
return mCollisionShape.get();
}
btCollisionShape *BulletShape::getAvoidCollisionShape() const
{
return mAvoidCollisionShape;
return mAvoidCollisionShape.get();
}
void BulletShape::setLocalScaling(const btVector3& scale)
@ -116,13 +106,12 @@ osg::ref_ptr<BulletShapeInstance> BulletShape::makeInstance() const
}
BulletShapeInstance::BulletShapeInstance(osg::ref_ptr<const BulletShape> source)
: BulletShape()
, mSource(source)
: mSource(source)
{
mCollisionBox = source->mCollisionBox;
mAnimatedShapes = source->mAnimatedShapes;
mCollisionShape = duplicateCollisionShape(source->mCollisionShape);
mAvoidCollisionShape = duplicateCollisionShape(source->mAvoidCollisionShape);
mCollisionShape = duplicateCollisionShape(source->mCollisionShape.get());
mAvoidCollisionShape = duplicateCollisionShape(source->mAvoidCollisionShape.get());
}
}

View file

@ -2,6 +2,7 @@
#define OPENMW_COMPONENTS_RESOURCE_BULLETSHAPE_H
#include <map>
#include <memory>
#include <osg/Object>
#include <osg/ref_ptr>
@ -13,19 +14,24 @@ class btCollisionShape;
namespace Resource
{
struct DeleteCollisionShape
{
void operator()(btCollisionShape* shape) const;
};
using CollisionShapePtr = std::unique_ptr<btCollisionShape, DeleteCollisionShape>;
class BulletShapeInstance;
class BulletShape : public osg::Object
{
public:
BulletShape();
BulletShape() = default;
BulletShape(const BulletShape& copy, const osg::CopyOp& copyop);
virtual ~BulletShape();
META_Object(Resource, BulletShape)
btCollisionShape* mCollisionShape;
btCollisionShape* mAvoidCollisionShape;
CollisionShapePtr mCollisionShape;
CollisionShapePtr mAvoidCollisionShape;
struct CollisionBox
{

View file

@ -92,7 +92,7 @@ public:
shape->mCollisionBox.mCenter = osg::Vec3f( (aabbMax[0] + aabbMin[0]) / 2.0f,
(aabbMax[1] + aabbMin[1]) / 2.0f,
(aabbMax[2] + aabbMin[2]) / 2.0f );
shape->mCollisionShape = triangleMeshShape;
shape->mCollisionShape.reset(triangleMeshShape);
return shape;
}