Improve const-correctness in BulletShapeManager

Sadly, two const_cast's are needed to work around Bullet API quirks.
coverity_scan
scrawl 9 years ago
parent b7e69cbc64
commit e055ae094a

@ -45,11 +45,11 @@ void BulletShape::deleteShape(btCollisionShape* shape)
} }
} }
btCollisionShape* BulletShape::duplicateCollisionShape(btCollisionShape *shape) const btCollisionShape* BulletShape::duplicateCollisionShape(const btCollisionShape *shape) const
{ {
if(shape->isCompound()) if(shape->isCompound())
{ {
btCompoundShape *comp = static_cast<btCompoundShape*>(shape); const btCompoundShape *comp = static_cast<const btCompoundShape*>(shape);
btCompoundShape *newShape = new btCompoundShape; btCompoundShape *newShape = new btCompoundShape;
int numShapes = comp->getNumChildShapes(); int numShapes = comp->getNumChildShapes();
@ -63,29 +63,27 @@ btCollisionShape* BulletShape::duplicateCollisionShape(btCollisionShape *shape)
return newShape; return newShape;
} }
if(btBvhTriangleMeshShape* trishape = dynamic_cast<btBvhTriangleMeshShape*>(shape)) if(const btBvhTriangleMeshShape* trishape = dynamic_cast<const btBvhTriangleMeshShape*>(shape))
{ {
#if BT_BULLET_VERSION >= 283 #if BT_BULLET_VERSION >= 283
btScaledBvhTriangleMeshShape* newShape = new btScaledBvhTriangleMeshShape(trishape, btVector3(1.f, 1.f, 1.f)); btScaledBvhTriangleMeshShape* newShape = new btScaledBvhTriangleMeshShape(const_cast<btBvhTriangleMeshShape*>(trishape), btVector3(1.f, 1.f, 1.f));
#else #else
// work around btScaledBvhTriangleMeshShape bug ( https://code.google.com/p/bullet/issues/detail?id=371 ) in older bullet versions // 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()); const btTriangleMesh* oldMesh = static_cast<const btTriangleMesh*>(trishape->getMeshInterface());
btTriangleMesh* newMesh = new btTriangleMesh(*oldMesh); btTriangleMesh* newMesh = new btTriangleMesh(*oldMesh);
// Do not build a new bvh (not needed, since it's the same as the original shape's bvh) // Do not build a new bvh (not needed, since it's the same as the original shape's bvh)
bool buildBvh = true; btOptimizedBvh* bvh = const_cast<btBvhTriangleMeshShape*>(trishape)->getOptimizedBvh();
if (trishape->getOptimizedBvh()) TriangleMeshShape* newShape = new TriangleMeshShape(newMesh, true, bvh == NULL);
buildBvh = false;
TriangleMeshShape* newShape = new TriangleMeshShape(newMesh, true, buildBvh);
// Set original shape's bvh via pointer // Set original shape's bvh via pointer
// The pointer is safe because the BulletShapeInstance keeps a ref_ptr to the original BulletShape // The pointer is safe because the BulletShapeInstance keeps a ref_ptr to the original BulletShape
if (!buildBvh) if (bvh)
newShape->setOptimizedBvh(trishape->getOptimizedBvh()); newShape->setOptimizedBvh(bvh);
#endif #endif
return newShape; return newShape;
} }
if (btBoxShape* boxshape = dynamic_cast<btBoxShape*>(shape)) if (const btBoxShape* boxshape = dynamic_cast<const btBoxShape*>(shape))
{ {
return new btBoxShape(*boxshape); return new btBoxShape(*boxshape);
} }
@ -98,13 +96,13 @@ btCollisionShape *BulletShape::getCollisionShape()
return mCollisionShape; return mCollisionShape;
} }
osg::ref_ptr<BulletShapeInstance> BulletShape::makeInstance() osg::ref_ptr<BulletShapeInstance> BulletShape::makeInstance() const
{ {
osg::ref_ptr<BulletShapeInstance> instance (new BulletShapeInstance(this)); osg::ref_ptr<BulletShapeInstance> instance (new BulletShapeInstance(this));
return instance; return instance;
} }
BulletShapeInstance::BulletShapeInstance(osg::ref_ptr<BulletShape> source) BulletShapeInstance::BulletShapeInstance(osg::ref_ptr<const BulletShape> source)
: BulletShape() : BulletShape()
, mSource(source) , mSource(source)
{ {

@ -38,9 +38,9 @@ namespace Resource
// we store the node's record index mapped to the child index of the shape in the btCompoundShape. // we store the node's record index mapped to the child index of the shape in the btCompoundShape.
std::map<int, int> mAnimatedShapes; std::map<int, int> mAnimatedShapes;
osg::ref_ptr<BulletShapeInstance> makeInstance(); osg::ref_ptr<BulletShapeInstance> makeInstance() const;
btCollisionShape* duplicateCollisionShape(btCollisionShape* shape) const; btCollisionShape* duplicateCollisionShape(const btCollisionShape* shape) const;
btCollisionShape* getCollisionShape(); btCollisionShape* getCollisionShape();
@ -55,10 +55,10 @@ namespace Resource
class BulletShapeInstance : public BulletShape class BulletShapeInstance : public BulletShape
{ {
public: public:
BulletShapeInstance(osg::ref_ptr<BulletShape> source); BulletShapeInstance(osg::ref_ptr<const BulletShape> source);
private: private:
osg::ref_ptr<BulletShape> mSource; osg::ref_ptr<const BulletShape> mSource;
}; };
// Subclass btBhvTriangleMeshShape to auto-delete the meshInterface // Subclass btBhvTriangleMeshShape to auto-delete the meshInterface

@ -109,7 +109,7 @@ BulletShapeManager::~BulletShapeManager()
} }
osg::ref_ptr<BulletShapeInstance> BulletShapeManager::createInstance(const std::string &name) osg::ref_ptr<const BulletShape> BulletShapeManager::getShape(const std::string &name)
{ {
std::string normalized = name; std::string normalized = name;
mVFS->normalizeFilename(normalized); mVFS->normalizeFilename(normalized);
@ -142,13 +142,18 @@ osg::ref_ptr<BulletShapeInstance> BulletShapeManager::createInstance(const std::
if (!shape) if (!shape)
{ {
mCache->addEntryToObjectCache(normalized, NULL); mCache->addEntryToObjectCache(normalized, NULL);
return osg::ref_ptr<BulletShapeInstance>(); return osg::ref_ptr<BulletShape>();
} }
} }
mCache->addEntryToObjectCache(normalized, shape); mCache->addEntryToObjectCache(normalized, shape);
} }
return shape;
}
osg::ref_ptr<BulletShapeInstance> BulletShapeManager::createInstance(const std::string &name)
{
osg::ref_ptr<const BulletShape> shape = getShape(name);
if (shape) if (shape)
return shape->makeInstance(); return shape->makeInstance();
else else

@ -26,6 +26,9 @@ namespace Resource
BulletShapeManager(const VFS::Manager* vfs, SceneManager* sceneMgr, NifFileManager* nifFileManager); BulletShapeManager(const VFS::Manager* vfs, SceneManager* sceneMgr, NifFileManager* nifFileManager);
~BulletShapeManager(); ~BulletShapeManager();
osg::ref_ptr<const BulletShape> getShape(const std::string& name);
/// Shorthand for getShape(name)->makeInstance();
osg::ref_ptr<BulletShapeInstance> createInstance(const std::string& name); osg::ref_ptr<BulletShapeInstance> createInstance(const std::string& name);
private: private:

Loading…
Cancel
Save