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.
118 lines
3.5 KiB
C++
118 lines
3.5 KiB
C++
#ifndef OPENMW_COMPONENTS_RESOURCE_BULLETSHAPE_H
|
|
#define OPENMW_COMPONENTS_RESOURCE_BULLETSHAPE_H
|
|
|
|
#include <map>
|
|
#include <memory>
|
|
|
|
#include <osg/Vec3f>
|
|
|
|
#include <BulletCollision/CollisionShapes/btBvhTriangleMeshShape.h>
|
|
#include <BulletCollision/CollisionShapes/btScaledBvhTriangleMeshShape.h>
|
|
|
|
#include <components/vfs/pathutil.hpp>
|
|
|
|
class btCollisionShape;
|
|
|
|
namespace NifBullet
|
|
{
|
|
class BulletNifLoader;
|
|
}
|
|
|
|
namespace Resource
|
|
{
|
|
struct DeleteCollisionShape
|
|
{
|
|
void operator()(btCollisionShape* shape) const;
|
|
};
|
|
|
|
using CollisionShapePtr = std::unique_ptr<btCollisionShape, DeleteCollisionShape>;
|
|
|
|
struct CollisionBox
|
|
{
|
|
osg::Vec3f mExtents;
|
|
osg::Vec3f mCenter;
|
|
};
|
|
|
|
enum class VisualCollisionType
|
|
{
|
|
None,
|
|
Default,
|
|
Camera
|
|
};
|
|
|
|
struct BulletShape
|
|
{
|
|
CollisionShapePtr mCollisionShape;
|
|
CollisionShapePtr mAvoidCollisionShape;
|
|
|
|
// Used for actors and projectiles. mCollisionShape is used for actors only when we need to autogenerate
|
|
// collision box for creatures. For now, use one file <-> one resource for simplicity.
|
|
CollisionBox mCollisionBox;
|
|
|
|
// Stores animated collision shapes.
|
|
// mCollisionShape is 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;
|
|
|
|
VFS::Path::Normalized mFileName;
|
|
std::string mFileHash;
|
|
|
|
VisualCollisionType mVisualCollisionType = VisualCollisionType::None;
|
|
|
|
BulletShape() = default;
|
|
// Note this is always a shallow copy and the copy will not autodelete underlying vertex data
|
|
BulletShape(const BulletShape& other);
|
|
|
|
void setLocalScaling(const btVector3& scale);
|
|
|
|
bool isAnimated() const { return !mAnimatedShapes.empty(); }
|
|
};
|
|
|
|
// An instance of a BulletShape that may have its own unique scaling set on collision shapes.
|
|
// Vertex data is shallow-copied where possible. A shared_ptr to the original shape is held to keep vertex pointers
|
|
// intact.
|
|
class BulletShapeInstance : public BulletShape
|
|
{
|
|
public:
|
|
explicit BulletShapeInstance(std::shared_ptr<const BulletShape> source);
|
|
|
|
const std::shared_ptr<const BulletShape>& getSource() const { return mSource; }
|
|
|
|
private:
|
|
std::shared_ptr<const BulletShape> mSource;
|
|
};
|
|
|
|
std::shared_ptr<BulletShapeInstance> makeInstance(std::shared_ptr<const BulletShape> source);
|
|
|
|
// Subclass btBhvTriangleMeshShape to auto-delete the meshInterface
|
|
struct TriangleMeshShape : public btBvhTriangleMeshShape
|
|
{
|
|
TriangleMeshShape(
|
|
btStridingMeshInterface* meshInterface, bool useQuantizedAabbCompression, bool buildBvh = true)
|
|
: btBvhTriangleMeshShape(meshInterface, useQuantizedAabbCompression, buildBvh)
|
|
{
|
|
}
|
|
|
|
virtual ~TriangleMeshShape()
|
|
{
|
|
delete getTriangleInfoMap();
|
|
delete m_meshInterface;
|
|
}
|
|
};
|
|
|
|
// btScaledBvhTriangleMeshShape that auto-deletes the child shape
|
|
struct ScaledTriangleMeshShape : public btScaledBvhTriangleMeshShape
|
|
{
|
|
ScaledTriangleMeshShape(btBvhTriangleMeshShape* childShape, const btVector3& localScaling)
|
|
: btScaledBvhTriangleMeshShape(childShape, localScaling)
|
|
{
|
|
}
|
|
|
|
~ScaledTriangleMeshShape() override { delete getChildShape(); }
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|