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.
188 lines
6.3 KiB
C++
188 lines
6.3 KiB
C++
#include "object.hpp"
|
|
#include "mtphysics.hpp"
|
|
#include <memory>
|
|
|
|
#include <components/bullethelpers/collisionobject.hpp>
|
|
#include <components/debug/debuglog.hpp>
|
|
#include <components/misc/convert.hpp>
|
|
#include <components/nifosg/particle.hpp>
|
|
#include <components/resource/bulletshape.hpp>
|
|
#include <components/sceneutil/positionattitudetransform.hpp>
|
|
|
|
#include <BulletCollision/CollisionShapes/btCompoundShape.h>
|
|
|
|
#include <LinearMath/btTransform.h>
|
|
|
|
namespace MWPhysics
|
|
{
|
|
Object::Object(const MWWorld::Ptr& ptr, std::shared_ptr<Resource::BulletShapeInstance> shapeInstance,
|
|
osg::Quat rotation, int collisionType, PhysicsTaskScheduler* scheduler)
|
|
: PtrHolder(ptr, osg::Vec3f())
|
|
, mShapeInstance(std::move(shapeInstance))
|
|
, mSolid(true)
|
|
, mScale(ptr.getCellRef().getScale(), ptr.getCellRef().getScale(), ptr.getCellRef().getScale())
|
|
, mPosition(ptr.getRefData().getPosition().asVec3())
|
|
, mRotation(rotation)
|
|
, mTaskScheduler(scheduler)
|
|
, mCollidedWith(ScriptedCollisionType_None)
|
|
{
|
|
mCollisionObject = BulletHelpers::makeCollisionObject(mShapeInstance->mCollisionShape.get(),
|
|
Misc::Convert::toBullet(mPosition), Misc::Convert::toBullet(rotation));
|
|
mCollisionObject->setUserPointer(this);
|
|
mShapeInstance->setLocalScaling(mScale);
|
|
mTaskScheduler->addCollisionObject(mCollisionObject.get(), collisionType,
|
|
CollisionType_Actor | CollisionType_HeightMap | CollisionType_Projectile);
|
|
}
|
|
|
|
Object::~Object()
|
|
{
|
|
mTaskScheduler->removeCollisionObject(mCollisionObject.get());
|
|
}
|
|
|
|
const std::shared_ptr<Resource::BulletShapeInstance>& Object::getShapeInstance() const
|
|
{
|
|
return mShapeInstance;
|
|
}
|
|
|
|
void Object::setScale(float scale)
|
|
{
|
|
std::unique_lock<std::mutex> lock(mPositionMutex);
|
|
mScale = { scale, scale, scale };
|
|
mScaleUpdatePending = true;
|
|
}
|
|
|
|
void Object::setRotation(osg::Quat quat)
|
|
{
|
|
std::unique_lock<std::mutex> lock(mPositionMutex);
|
|
mRotation = quat;
|
|
mTransformUpdatePending = true;
|
|
}
|
|
|
|
void Object::updatePosition()
|
|
{
|
|
std::unique_lock<std::mutex> lock(mPositionMutex);
|
|
mPosition = mPtr.getRefData().getPosition().asVec3();
|
|
mTransformUpdatePending = true;
|
|
}
|
|
|
|
void Object::commitPositionChange()
|
|
{
|
|
std::unique_lock<std::mutex> lock(mPositionMutex);
|
|
if (mScaleUpdatePending)
|
|
{
|
|
mShapeInstance->setLocalScaling(mScale);
|
|
mScaleUpdatePending = false;
|
|
}
|
|
if (mTransformUpdatePending)
|
|
{
|
|
btTransform trans;
|
|
trans.setOrigin(Misc::Convert::toBullet(mPosition));
|
|
trans.setRotation(Misc::Convert::toBullet(mRotation));
|
|
mCollisionObject->setWorldTransform(trans);
|
|
mTransformUpdatePending = false;
|
|
}
|
|
}
|
|
|
|
btTransform Object::getTransform() const
|
|
{
|
|
std::unique_lock<std::mutex> lock(mPositionMutex);
|
|
btTransform trans;
|
|
trans.setOrigin(Misc::Convert::toBullet(mPosition));
|
|
trans.setRotation(Misc::Convert::toBullet(mRotation));
|
|
return trans;
|
|
}
|
|
|
|
bool Object::isSolid() const
|
|
{
|
|
return mSolid;
|
|
}
|
|
|
|
void Object::setSolid(bool solid)
|
|
{
|
|
mSolid = solid;
|
|
}
|
|
|
|
bool Object::isAnimated() const
|
|
{
|
|
return mShapeInstance->isAnimated();
|
|
}
|
|
|
|
bool Object::animateCollisionShapes()
|
|
{
|
|
if (mShapeInstance->mAnimatedShapes.empty())
|
|
return false;
|
|
|
|
if (!mPtr.getRefData().getBaseNode())
|
|
return false;
|
|
|
|
assert(mShapeInstance->mCollisionShape->isCompound());
|
|
|
|
btCompoundShape* compound = static_cast<btCompoundShape*>(mShapeInstance->mCollisionShape.get());
|
|
bool result = false;
|
|
for (const auto& [recordIndex, shapeIndex] : mShapeInstance->mAnimatedShapes)
|
|
{
|
|
auto nodePathFound = mRecordIndexToNodePath.find(recordIndex);
|
|
if (nodePathFound == mRecordIndexToNodePath.end())
|
|
{
|
|
NifOsg::FindGroupByRecordIndex visitor(recordIndex);
|
|
mPtr.getRefData().getBaseNode()->accept(visitor);
|
|
if (!visitor.mFound)
|
|
{
|
|
Log(Debug::Warning) << "Warning: animateCollisionShapes can't find node " << recordIndex << " for "
|
|
<< mPtr.getCellRef().getRefId();
|
|
|
|
// Remove nonexistent nodes from animated shapes map and early out
|
|
mShapeInstance->mAnimatedShapes.erase(recordIndex);
|
|
return false;
|
|
}
|
|
osg::NodePath nodePath = visitor.mFoundPath;
|
|
nodePath.erase(nodePath.begin());
|
|
nodePathFound = mRecordIndexToNodePath.emplace(recordIndex, nodePath).first;
|
|
}
|
|
|
|
osg::NodePath& nodePath = nodePathFound->second;
|
|
osg::Matrixf matrix = osg::computeLocalToWorld(nodePath);
|
|
btVector3 scale = Misc::Convert::toBullet(matrix.getScale());
|
|
matrix.orthoNormalize(matrix);
|
|
|
|
btTransform transform;
|
|
transform.setOrigin(Misc::Convert::toBullet(matrix.getTrans()) * compound->getLocalScaling());
|
|
for (int i = 0; i < 3; ++i)
|
|
for (int j = 0; j < 3; ++j)
|
|
transform.getBasis()[i][j] = matrix(j, i); // NB column/row major difference
|
|
|
|
btCollisionShape* childShape = compound->getChildShape(shapeIndex);
|
|
btVector3 newScale = compound->getLocalScaling() * scale;
|
|
|
|
if (childShape->getLocalScaling() != newScale)
|
|
{
|
|
childShape->setLocalScaling(newScale);
|
|
result = true;
|
|
}
|
|
|
|
if (!(transform == compound->getChildTransform(shapeIndex)))
|
|
{
|
|
compound->updateChildTransform(shapeIndex, transform);
|
|
result = true;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
bool Object::collidedWith(ScriptedCollisionType type) const
|
|
{
|
|
return mCollidedWith & type;
|
|
}
|
|
|
|
void Object::addCollision(ScriptedCollisionType type)
|
|
{
|
|
std::unique_lock<std::mutex> lock(mPositionMutex);
|
|
mCollidedWith |= type;
|
|
}
|
|
|
|
void Object::resetCollisions()
|
|
{
|
|
mCollidedWith = ScriptedCollisionType_None;
|
|
}
|
|
}
|