mirror of
https://github.com/OpenMW/openmw.git
synced 2025-02-06 13:15:35 +00:00
Preload instances in BulletShapeManager
This commit is contained in:
parent
e28dc3e72f
commit
246566cef4
4 changed files with 51 additions and 7 deletions
|
@ -1088,7 +1088,7 @@ namespace MWPhysics
|
||||||
|
|
||||||
void PhysicsSystem::addObject (const MWWorld::Ptr& ptr, const std::string& mesh, int collisionType)
|
void PhysicsSystem::addObject (const MWWorld::Ptr& ptr, const std::string& mesh, int collisionType)
|
||||||
{
|
{
|
||||||
osg::ref_ptr<Resource::BulletShapeInstance> shapeInstance = mShapeManager->createInstance(mesh);
|
osg::ref_ptr<Resource::BulletShapeInstance> shapeInstance = mShapeManager->getInstance(mesh);
|
||||||
if (!shapeInstance || !shapeInstance->getCollisionShape())
|
if (!shapeInstance || !shapeInstance->getCollisionShape())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -1234,7 +1234,7 @@ namespace MWPhysics
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsSystem::addActor (const MWWorld::Ptr& ptr, const std::string& mesh) {
|
void PhysicsSystem::addActor (const MWWorld::Ptr& ptr, const std::string& mesh) {
|
||||||
osg::ref_ptr<Resource::BulletShapeInstance> shapeInstance = mShapeManager->createInstance(mesh);
|
osg::ref_ptr<Resource::BulletShapeInstance> shapeInstance = mShapeManager->getInstance(mesh);
|
||||||
if (!shapeInstance)
|
if (!shapeInstance)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
|
@ -87,7 +87,7 @@ namespace MWWorld
|
||||||
//std::cout << "preloading " << mesh << std::endl;
|
//std::cout << "preloading " << mesh << std::endl;
|
||||||
|
|
||||||
mPreloadedNodes.push_back(mSceneManager->cacheInstance(mesh));
|
mPreloadedNodes.push_back(mSceneManager->cacheInstance(mesh));
|
||||||
mPreloadedShapes.push_back(mBulletShapeManager->getShape(mesh));
|
mPreloadedShapes.push_back(mBulletShapeManager->cacheInstance(mesh));
|
||||||
|
|
||||||
size_t slashpos = mesh.find_last_of("/\\");
|
size_t slashpos = mesh.find_last_of("/\\");
|
||||||
if (slashpos != std::string::npos && slashpos != mesh.size()-1)
|
if (slashpos != std::string::npos && slashpos != mesh.size()-1)
|
||||||
|
@ -123,7 +123,7 @@ namespace MWWorld
|
||||||
|
|
||||||
// keep a ref to the loaded object to make sure it stays loaded as long as this cell is in the preloaded state
|
// keep a ref to the loaded object to make sure it stays loaded as long as this cell is in the preloaded state
|
||||||
std::vector<osg::ref_ptr<const osg::Node> > mPreloadedNodes;
|
std::vector<osg::ref_ptr<const osg::Node> > mPreloadedNodes;
|
||||||
std::vector<osg::ref_ptr<const Resource::BulletShape> > mPreloadedShapes;
|
std::vector<osg::ref_ptr<const Resource::BulletShapeInstance> > mPreloadedShapes;
|
||||||
std::vector<osg::ref_ptr<const NifOsg::KeyframeHolder> > mPreloadedKeyframes;
|
std::vector<osg::ref_ptr<const NifOsg::KeyframeHolder> > mPreloadedKeyframes;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
#include "scenemanager.hpp"
|
#include "scenemanager.hpp"
|
||||||
#include "niffilemanager.hpp"
|
#include "niffilemanager.hpp"
|
||||||
#include "objectcache.hpp"
|
#include "objectcache.hpp"
|
||||||
|
#include "multiobjectcache.hpp"
|
||||||
|
|
||||||
namespace Resource
|
namespace Resource
|
||||||
{
|
{
|
||||||
|
@ -98,6 +98,7 @@ private:
|
||||||
|
|
||||||
BulletShapeManager::BulletShapeManager(const VFS::Manager* vfs, SceneManager* sceneMgr, NifFileManager* nifFileManager)
|
BulletShapeManager::BulletShapeManager(const VFS::Manager* vfs, SceneManager* sceneMgr, NifFileManager* nifFileManager)
|
||||||
: ResourceManager(vfs)
|
: ResourceManager(vfs)
|
||||||
|
, mInstanceCache(new MultiObjectCache)
|
||||||
, mSceneManager(sceneMgr)
|
, mSceneManager(sceneMgr)
|
||||||
, mNifFileManager(nifFileManager)
|
, mNifFileManager(nifFileManager)
|
||||||
{
|
{
|
||||||
|
@ -151,6 +152,28 @@ osg::ref_ptr<const BulletShape> BulletShapeManager::getShape(const std::string &
|
||||||
return shape;
|
return shape;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
osg::ref_ptr<BulletShapeInstance> BulletShapeManager::cacheInstance(const std::string &name)
|
||||||
|
{
|
||||||
|
std::string normalized = name;
|
||||||
|
mVFS->normalizeFilename(normalized);
|
||||||
|
|
||||||
|
osg::ref_ptr<BulletShapeInstance> instance = createInstance(normalized);
|
||||||
|
mInstanceCache->addEntryToObjectCache(normalized, instance.get());
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
osg::ref_ptr<BulletShapeInstance> BulletShapeManager::getInstance(const std::string &name)
|
||||||
|
{
|
||||||
|
std::string normalized = name;
|
||||||
|
mVFS->normalizeFilename(normalized);
|
||||||
|
|
||||||
|
osg::ref_ptr<osg::Object> obj = mInstanceCache->takeFromObjectCache(normalized);
|
||||||
|
if (obj.get())
|
||||||
|
return static_cast<BulletShapeInstance*>(obj.get());
|
||||||
|
else
|
||||||
|
return createInstance(normalized);
|
||||||
|
}
|
||||||
|
|
||||||
osg::ref_ptr<BulletShapeInstance> BulletShapeManager::createInstance(const std::string &name)
|
osg::ref_ptr<BulletShapeInstance> BulletShapeManager::createInstance(const std::string &name)
|
||||||
{
|
{
|
||||||
osg::ref_ptr<const BulletShape> shape = getShape(name);
|
osg::ref_ptr<const BulletShape> shape = getShape(name);
|
||||||
|
@ -160,4 +183,11 @@ osg::ref_ptr<BulletShapeInstance> BulletShapeManager::createInstance(const std::
|
||||||
return osg::ref_ptr<BulletShapeInstance>();
|
return osg::ref_ptr<BulletShapeInstance>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BulletShapeManager::updateCache(double referenceTime)
|
||||||
|
{
|
||||||
|
ResourceManager::updateCache(referenceTime);
|
||||||
|
|
||||||
|
mInstanceCache->removeUnreferencedObjectsInCache();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,8 @@ namespace Resource
|
||||||
class BulletShape;
|
class BulletShape;
|
||||||
class BulletShapeInstance;
|
class BulletShapeInstance;
|
||||||
|
|
||||||
|
class MultiObjectCache;
|
||||||
|
|
||||||
/// Handles loading, caching and "instancing" of bullet shapes.
|
/// Handles loading, caching and "instancing" of bullet shapes.
|
||||||
/// A shape 'instance' is a clone of another shape, with the goal of setting a different scale on this instance.
|
/// A shape 'instance' is a clone of another shape, with the goal of setting a different scale on this instance.
|
||||||
/// @note May be used from any thread.
|
/// @note May be used from any thread.
|
||||||
|
@ -26,12 +28,24 @@ namespace Resource
|
||||||
BulletShapeManager(const VFS::Manager* vfs, SceneManager* sceneMgr, NifFileManager* nifFileManager);
|
BulletShapeManager(const VFS::Manager* vfs, SceneManager* sceneMgr, NifFileManager* nifFileManager);
|
||||||
~BulletShapeManager();
|
~BulletShapeManager();
|
||||||
|
|
||||||
|
/// @note May return a null pointer if the object has no shape.
|
||||||
osg::ref_ptr<const BulletShape> getShape(const std::string& name);
|
osg::ref_ptr<const BulletShape> getShape(const std::string& name);
|
||||||
|
|
||||||
/// Shorthand for getShape(name)->makeInstance();
|
/// Create an instance of the given shape and cache it for later use, so that future calls to getInstance() can simply return
|
||||||
osg::ref_ptr<BulletShapeInstance> createInstance(const std::string& name);
|
/// the cached instance instead of having to create a new one.
|
||||||
|
/// @note The returned ref_ptr may be kept by the caller to ensure that the instance stays in cache for as long as needed.
|
||||||
|
osg::ref_ptr<BulletShapeInstance> cacheInstance(const std::string& name);
|
||||||
|
|
||||||
|
/// @note May return a null pointer if the object has no shape.
|
||||||
|
osg::ref_ptr<BulletShapeInstance> getInstance(const std::string& name);
|
||||||
|
|
||||||
|
/// @see ResourceManager::updateCache
|
||||||
|
virtual void updateCache(double referenceTime);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
osg::ref_ptr<BulletShapeInstance> createInstance(const std::string& name);
|
||||||
|
|
||||||
|
osg::ref_ptr<MultiObjectCache> mInstanceCache;
|
||||||
SceneManager* mSceneManager;
|
SceneManager* mSceneManager;
|
||||||
NifFileManager* mNifFileManager;
|
NifFileManager* mNifFileManager;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue