Files
Bret Curtis 4addabaf95 Let the resource cache be told what it stores, and free its values from OSG
GenericObjectCache stored osg::ref_ptr<osg::Object>, so everything cached had to
derive from osg::Object whether that meant anything or not: NifFileHolder and
BgsmFileHolder existed for no other reason, Resource::BulletShape and
ESMTerrain::LandObject carried META_Object and a CopyOp constructor nothing ever
called, and every getter paid a static_cast on the way out. The cache and
MultiObjectCache now take their value type as a template parameter, ask "is
anything else holding this?" through a useCount overload rather than a base class
method, and constrain accept and releaseGLObjects so a value type with neither is
not an error. Both holders are deleted; BulletShape, BulletShapeInstance and
LandObject become plain classes held by std::shared_ptr across physics,
pathfinding, navmeshtool and terrain storage; MWPhysics::HeightField's
type-erased osg::Object keep-alive becomes the LandObject it was always holding;
every remaining manager names the type it actually caches through the
NodeResourceManager alias, which removes eight static_casts and stops
clang-format wrapping the declarations. The preloader needs two keep-alive sets
for now, because the render-owned entries are still osg::Object; they merge once
those move behind the render backend.
2026-09-18 16:10:18 +02:00

110 lines
4.0 KiB
C++

#include "bulletshape.hpp"
#include <stdexcept>
#include <string>
#include <BulletCollision/CollisionShapes/btBoxShape.h>
#include <BulletCollision/CollisionShapes/btCompoundShape.h>
#include <BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h>
#include <BulletCollision/CollisionShapes/btScaledBvhTriangleMeshShape.h>
namespace Resource
{
namespace
{
CollisionShapePtr duplicateCollisionShape(const btCollisionShape* shape)
{
if (shape == nullptr)
return nullptr;
if (shape->isCompound())
{
const btCompoundShape* comp = static_cast<const btCompoundShape*>(shape);
std::unique_ptr<btCompoundShape, DeleteCollisionShape> newShape(new btCompoundShape);
for (int i = 0, n = comp->getNumChildShapes(); i < n; ++i)
{
auto child = duplicateCollisionShape(comp->getChildShape(i));
const btTransform& trans = comp->getChildTransform(i);
newShape->addChildShape(trans, child.release());
}
return newShape;
}
if (shape->getShapeType() == SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE)
{
const btScaledBvhTriangleMeshShape* trishape = static_cast<const btScaledBvhTriangleMeshShape*>(shape);
return CollisionShapePtr(new btScaledBvhTriangleMeshShape(
const_cast<btBvhTriangleMeshShape*>(trishape->getChildShape()), trishape->getLocalScaling()));
}
if (shape->getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE)
{
const btBvhTriangleMeshShape* trishape = static_cast<const btBvhTriangleMeshShape*>(shape);
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 CollisionShapePtr(new btBoxShape(*boxshape));
}
if (shape->getShapeType() == TERRAIN_SHAPE_PROXYTYPE)
return CollisionShapePtr(
new btHeightfieldTerrainShape(static_cast<const btHeightfieldTerrainShape&>(*shape)));
throw std::logic_error(std::string("Unhandled Bullet shape duplication: ") + shape->getName());
}
void deleteShape(btCollisionShape* shape)
{
if (shape->isCompound())
{
btCompoundShape* compound = static_cast<btCompoundShape*>(shape);
for (int i = 0, n = compound->getNumChildShapes(); i < n; i++)
if (btCollisionShape* child = compound->getChildShape(i))
deleteShape(child);
}
delete shape;
}
}
void DeleteCollisionShape::operator()(btCollisionShape* shape) const
{
deleteShape(shape);
}
BulletShape::BulletShape(const BulletShape& other)
: mCollisionShape(duplicateCollisionShape(other.mCollisionShape.get()))
, mAvoidCollisionShape(duplicateCollisionShape(other.mAvoidCollisionShape.get()))
, mCollisionBox(other.mCollisionBox)
, mAnimatedShapes(other.mAnimatedShapes)
, mFileName(other.mFileName)
, mFileHash(other.mFileHash)
, mVisualCollisionType(other.mVisualCollisionType)
{
}
void BulletShape::setLocalScaling(const btVector3& scale)
{
mCollisionShape->setLocalScaling(scale);
if (mAvoidCollisionShape)
mAvoidCollisionShape->setLocalScaling(scale);
}
std::shared_ptr<BulletShapeInstance> makeInstance(std::shared_ptr<const BulletShape> source)
{
return std::make_shared<BulletShapeInstance>(std::move(source));
}
BulletShapeInstance::BulletShapeInstance(std::shared_ptr<const BulletShape> source)
: BulletShape(*source)
, mSource(std::move(source))
{
}
}