1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-19 22:23:51 +00:00

Convert PhysicsSystem to a singleton.

This commit is contained in:
cc9cii 2014-10-24 19:14:02 +10:00
parent 98ff3e7307
commit cb53e714f7
13 changed files with 75 additions and 72 deletions

View file

@ -21,7 +21,7 @@
CS::Editor::Editor (OgreInit::OgreInit& ogreInit) CS::Editor::Editor (OgreInit::OgreInit& ogreInit)
: mUserSettings (mCfgMgr), mOverlaySystem (0), mDocumentManager (mCfgMgr), : mUserSettings (mCfgMgr), mOverlaySystem (0), mDocumentManager (mCfgMgr),
mViewManager (mDocumentManager), mViewManager (mDocumentManager), mPhysicsSystem (0),
mIpcServerName ("org.openmw.OpenCS"), mServer(NULL), mClientSocket(NULL) mIpcServerName ("org.openmw.OpenCS"), mServer(NULL), mClientSocket(NULL)
{ {
std::pair<Files::PathContainer, std::vector<std::string> > config = readConfig(); std::pair<Files::PathContainer, std::vector<std::string> > config = readConfig();
@ -34,6 +34,7 @@ CS::Editor::Editor (OgreInit::OgreInit& ogreInit)
ogreInit.init ((mCfgMgr.getUserConfigPath() / "opencsOgre.log").string()); ogreInit.init ((mCfgMgr.getUserConfigPath() / "opencsOgre.log").string());
mOverlaySystem.reset (new CSVRender::OverlaySystem); mOverlaySystem.reset (new CSVRender::OverlaySystem);
mPhysicsSystem.reset (new CSVWorld::PhysicsSystem);
Bsa::registerResources (Files::Collections (config.first, !mFsStrict), config.second, true, Bsa::registerResources (Files::Collections (config.first, !mFsStrict), config.second, true,
mFsStrict); mFsStrict);

View file

@ -28,6 +28,7 @@
#include "view/settings/dialog.hpp" #include "view/settings/dialog.hpp"
#include "view/render/overlaysystem.hpp" #include "view/render/overlaysystem.hpp"
#include "view/world/physicssystem.hpp"
namespace OgreInit namespace OgreInit
{ {
@ -44,6 +45,7 @@ namespace CS
Files::ConfigurationManager mCfgMgr; Files::ConfigurationManager mCfgMgr;
CSMSettings::UserSettings mUserSettings; CSMSettings::UserSettings mUserSettings;
std::auto_ptr<CSVRender::OverlaySystem> mOverlaySystem; std::auto_ptr<CSVRender::OverlaySystem> mOverlaySystem;
std::auto_ptr<CSVWorld::PhysicsSystem> mPhysicsSystem;
CSMDoc::DocumentManager mDocumentManager; CSMDoc::DocumentManager mDocumentManager;
CSVDoc::ViewManager mViewManager; CSVDoc::ViewManager mViewManager;
CSVDoc::StartupDialogue mStartup; CSVDoc::StartupDialogue mStartup;

View file

@ -10,8 +10,6 @@
#include "../../model/world/columns.hpp" #include "../../model/world/columns.hpp"
#include "../../model/world/data.hpp" #include "../../model/world/data.hpp"
#include "../world/physicssystem.hpp"
#include "elements.hpp" #include "elements.hpp"
#include "terrainstorage.hpp" #include "terrainstorage.hpp"
@ -51,7 +49,7 @@ bool CSVRender::Cell::addObjects (int start, int end)
std::string id = Misc::StringUtils::lowerCase (references.data ( std::string id = Misc::StringUtils::lowerCase (references.data (
references.index (i, idColumn)).toString().toUtf8().constData()); references.index (i, idColumn)).toString().toUtf8().constData());
mObjects.insert (std::make_pair (id, new Object (mData, mCellNode, id, false, mPhysics))); mObjects.insert (std::make_pair (id, new Object (mData, mCellNode, id, false)));
modified = true; modified = true;
} }
} }
@ -60,8 +58,8 @@ bool CSVRender::Cell::addObjects (int start, int end)
} }
CSVRender::Cell::Cell (CSMWorld::Data& data, Ogre::SceneManager *sceneManager, CSVRender::Cell::Cell (CSMWorld::Data& data, Ogre::SceneManager *sceneManager,
const std::string& id, CSVWorld::PhysicsSystem *physics, const Ogre::Vector3& origin) const std::string& id, const Ogre::Vector3& origin)
: mData (data), mId (Misc::StringUtils::lowerCase (id)), mPhysics(physics) : mData (data), mId (Misc::StringUtils::lowerCase (id))
{ {
mCellNode = sceneManager->getRootSceneNode()->createChildSceneNode(); mCellNode = sceneManager->getRootSceneNode()->createChildSceneNode();
mCellNode->setPosition (origin); mCellNode->setPosition (origin);
@ -180,7 +178,7 @@ bool CSVRender::Cell::referenceDataChanged (const QModelIndex& topLeft,
for (std::map<std::string, bool>::iterator iter (ids.begin()); iter!=ids.end(); ++iter) for (std::map<std::string, bool>::iterator iter (ids.begin()); iter!=ids.end(); ++iter)
{ {
mObjects.insert (std::make_pair ( mObjects.insert (std::make_pair (
iter->first, new Object (mData, mCellNode, iter->first, false, mPhysics))); iter->first, new Object (mData, mCellNode, iter->first, false)));
modified = true; modified = true;
} }

View file

@ -23,11 +23,6 @@ namespace CSMWorld
class Data; class Data;
} }
namespace CSVWorld
{
class PhysicsSystem;
}
namespace CSVRender namespace CSVRender
{ {
class Cell class Cell
@ -37,7 +32,6 @@ namespace CSVRender
Ogre::SceneNode *mCellNode; Ogre::SceneNode *mCellNode;
std::map<std::string, Object *> mObjects; std::map<std::string, Object *> mObjects;
std::auto_ptr<Terrain::TerrainGrid> mTerrain; std::auto_ptr<Terrain::TerrainGrid> mTerrain;
CSVWorld::PhysicsSystem *mPhysics;
/// Ignored if cell does not have an object with the given ID. /// Ignored if cell does not have an object with the given ID.
/// ///
@ -52,8 +46,7 @@ namespace CSVRender
public: public:
Cell (CSMWorld::Data& data, Ogre::SceneManager *sceneManager, Cell (CSMWorld::Data& data, Ogre::SceneManager *sceneManager,
const std::string& id, CSVWorld::PhysicsSystem *physics, const std::string& id, const Ogre::Vector3& origin = Ogre::Vector3 (0, 0, 0));
const Ogre::Vector3& origin = Ogre::Vector3 (0, 0, 0));
~Cell(); ~Cell();

View file

@ -78,7 +78,7 @@ void CSVRender::Object::update()
mObject = NifOgre::Loader::createObjects (mBase, "Meshes\\" + model); mObject = NifOgre::Loader::createObjects (mBase, "Meshes\\" + model);
mObject->setVisibilityFlags (Element_Reference); mObject->setVisibilityFlags (Element_Reference);
if (mPhysics && !mReferenceId.empty()) if (!mReferenceId.empty())
{ {
const CSMWorld::CellRef& reference = getReference(); const CSMWorld::CellRef& reference = getReference();
@ -92,7 +92,8 @@ void CSVRender::Object::update()
Ogre::Quaternion yr (Ogre::Radian (-reference.mPos.rot[1]), Ogre::Vector3::UNIT_Y); Ogre::Quaternion yr (Ogre::Radian (-reference.mPos.rot[1]), Ogre::Vector3::UNIT_Y);
Ogre::Quaternion zr (Ogre::Radian (-reference.mPos.rot[2]), Ogre::Vector3::UNIT_Z); Ogre::Quaternion zr (Ogre::Radian (-reference.mPos.rot[2]), Ogre::Vector3::UNIT_Z);
mPhysics->addObject("meshes\\" + model, mBase->getName(), reference.mScale, position, xr*yr*zr); CSVWorld::PhysicsSystem::instance()->addObject("meshes\\" + model,
mBase->getName(), reference.mScale, position, xr*yr*zr);
} }
} }
} }
@ -131,9 +132,8 @@ const CSMWorld::CellRef& CSVRender::Object::getReference() const
} }
CSVRender::Object::Object (const CSMWorld::Data& data, Ogre::SceneNode *cellNode, CSVRender::Object::Object (const CSMWorld::Data& data, Ogre::SceneNode *cellNode,
const std::string& id, bool referenceable, CSVWorld::PhysicsSystem* physics, const std::string& id, bool referenceable, bool forceBaseToZero)
bool forceBaseToZero) : mData (data), mBase (0), mForceBaseToZero (forceBaseToZero)
: mData (data), mBase (0), mForceBaseToZero (forceBaseToZero), mPhysics(physics)
{ {
mBase = cellNode->createChildSceneNode(); mBase = cellNode->createChildSceneNode();

View file

@ -16,11 +16,6 @@ namespace CSMWorld
class CellRef; class CellRef;
} }
namespace CSVWorld
{
class PhysicsSystem;
}
namespace CSVRender namespace CSVRender
{ {
class Object class Object
@ -31,7 +26,6 @@ namespace CSVRender
Ogre::SceneNode *mBase; Ogre::SceneNode *mBase;
NifOgre::ObjectScenePtr mObject; NifOgre::ObjectScenePtr mObject;
bool mForceBaseToZero; bool mForceBaseToZero;
CSVWorld::PhysicsSystem *mPhysics;
/// Not implemented /// Not implemented
Object (const Object&); Object (const Object&);
@ -57,8 +51,7 @@ namespace CSVRender
public: public:
Object (const CSMWorld::Data& data, Ogre::SceneNode *cellNode, Object (const CSMWorld::Data& data, Ogre::SceneNode *cellNode,
const std::string& id, bool referenceable, const std::string& id, bool referenceable, bool forceBaseToZero = false);
CSVWorld::PhysicsSystem *physics = NULL, bool forceBaseToZero = false);
/// \param forceBaseToZero If this is a reference ignore the coordinates and place /// \param forceBaseToZero If this is a reference ignore the coordinates and place
/// it at 0, 0, 0 instead. /// it at 0, 0, 0 instead.

View file

@ -110,7 +110,7 @@ bool CSVRender::PagedWorldspaceWidget::adjustCells()
mCells.find (*iter)==mCells.end()) mCells.find (*iter)==mCells.end())
{ {
Cell *cell = new Cell (mDocument.getData(), getSceneManager(), Cell *cell = new Cell (mDocument.getData(), getSceneManager(),
iter->getId (mWorldspace), getPhysics()); iter->getId (mWorldspace));
mCells.insert (std::make_pair (*iter, cell)); mCells.insert (std::make_pair (*iter, cell));
float height = cell->getTerrainHeightAt(Ogre::Vector3( float height = cell->getTerrainHeightAt(Ogre::Vector3(
@ -198,7 +198,9 @@ void CSVRender::PagedWorldspaceWidget::mouseReleaseEvent (QMouseEvent *event)
float mouseX = (float) event->x()/viewportWidth; float mouseX = (float) event->x()/viewportWidth;
float mouseY = (float) event->y()/viewportHeight; float mouseY = (float) event->y()/viewportHeight;
getPhysics()->castRay(mouseX, mouseY, NULL, NULL, getCamera()); // Need to set each time in case there are multiple subviews
CSVWorld::PhysicsSystem::instance()->setSceneManager(getSceneManager());
CSVWorld::PhysicsSystem::instance()->castRay(mouseX, mouseY, NULL, NULL, getCamera());
flagAsModified(); flagAsModified();
#if 0 #if 0
std::cout << "geometry: " + std::to_string(width()) + ", " + std::to_string(height()) << std::endl; std::cout << "geometry: " + std::to_string(width()) + ", " + std::to_string(height()) << std::endl;
@ -219,7 +221,9 @@ void CSVRender::PagedWorldspaceWidget::mouseDoubleClickEvent (QMouseEvent *event
{ {
std::cout << "double clicked" << std::endl; std::cout << "double clicked" << std::endl;
getPhysics()->toggleDebugRendering(); // Need to set each time in case there are multiple subviews
CSVWorld::PhysicsSystem::instance()->setSceneManager(getSceneManager());
CSVWorld::PhysicsSystem::instance()->toggleDebugRendering();
} }
} }

View file

@ -10,7 +10,7 @@
CSVRender::PreviewWidget::PreviewWidget (CSMWorld::Data& data, CSVRender::PreviewWidget::PreviewWidget (CSMWorld::Data& data,
const std::string& id, bool referenceable, QWidget *parent) const std::string& id, bool referenceable, QWidget *parent)
: SceneWidget (parent), mData (data), : SceneWidget (parent), mData (data),
mObject (data, getSceneManager()->getRootSceneNode(), id, referenceable, NULL, true) mObject (data, getSceneManager()->getRootSceneNode(), id, referenceable, true)
{ {
setNavigation (&mOrbit); setNavigation (&mOrbit);

View file

@ -66,8 +66,7 @@ namespace CSVRender
mOverlaySystem = OverlaySystem::instance().get(); mOverlaySystem = OverlaySystem::instance().get();
mSceneMgr->addRenderQueueListener(mOverlaySystem); mSceneMgr->addRenderQueueListener(mOverlaySystem);
// FIXME: singleton probably needed CSVWorld::PhysicsSystem::instance()->setSceneManager(mSceneMgr);
mPhysics = new CSVWorld::PhysicsSystem(mSceneMgr);
QTimer *timer = new QTimer (this); QTimer *timer = new QTimer (this);
@ -176,7 +175,6 @@ namespace CSVRender
if (mSceneMgr) if (mSceneMgr)
Ogre::Root::getSingleton().destroySceneManager (mSceneMgr); Ogre::Root::getSingleton().destroySceneManager (mSceneMgr);
delete mPhysics;
} }
void SceneWidget::setVisibilityMask (unsigned int mask) void SceneWidget::setVisibilityMask (unsigned int mask)
@ -212,11 +210,6 @@ namespace CSVRender
return mViewport; return mViewport;
} }
CSVWorld::PhysicsSystem *SceneWidget::getPhysics()
{
return mPhysics;
}
Ogre::SceneManager *SceneWidget::getSceneManager() Ogre::SceneManager *SceneWidget::getSceneManager()
{ {
return mSceneMgr; return mSceneMgr;

View file

@ -25,11 +25,6 @@ namespace CSVWidget
class SceneToolbar; class SceneToolbar;
} }
namespace CSVWorld
{
class PhysicsSystem;
}
namespace CSVRender namespace CSVRender
{ {
class Navigation; class Navigation;
@ -63,8 +58,6 @@ namespace CSVRender
Ogre::Viewport *getViewport(); Ogre::Viewport *getViewport();
CSVWorld::PhysicsSystem *getPhysics();
Ogre::SceneManager *getSceneManager(); Ogre::SceneManager *getSceneManager();
Ogre::Camera *getCamera(); Ogre::Camera *getCamera();
@ -105,7 +98,6 @@ namespace CSVRender
Ogre::RenderWindow* mWindow; Ogre::RenderWindow* mWindow;
Ogre::Viewport *mViewport; Ogre::Viewport *mViewport;
Ogre::OverlaySystem *mOverlaySystem; Ogre::OverlaySystem *mOverlaySystem;
CSVWorld::PhysicsSystem *mPhysics;
Navigation *mNavigation; Navigation *mNavigation;
Lighting *mLighting; Lighting *mLighting;

View file

@ -56,7 +56,7 @@ CSVRender::UnpagedWorldspaceWidget::UnpagedWorldspaceWidget (const std::string&
update(); update();
mCell.reset (new Cell (document.getData(), getSceneManager(), mCellId, getPhysics())); mCell.reset (new Cell (document.getData(), getSceneManager(), mCellId));
} }
void CSVRender::UnpagedWorldspaceWidget::cellDataChanged (const QModelIndex& topLeft, void CSVRender::UnpagedWorldspaceWidget::cellDataChanged (const QModelIndex& topLeft,
@ -98,7 +98,7 @@ bool CSVRender::UnpagedWorldspaceWidget::handleDrop (const std::vector<CSMWorld:
return false; return false;
mCellId = data.begin()->getId(); mCellId = data.begin()->getId();
mCell.reset (new Cell (getDocument().getData(), getSceneManager(), mCellId, getPhysics())); mCell.reset (new Cell (getDocument().getData(), getSceneManager(), mCellId));
update(); update();
emit cellChanged(*data.begin()); emit cellChanged(*data.begin());

View file

@ -39,11 +39,48 @@ namespace
namespace CSVWorld namespace CSVWorld
{ {
PhysicsSystem *PhysicsSystem::mPhysicsSystemInstance = 0;
PhysicsSystem::PhysicsSystem(Ogre::SceneManager *sceneMgr) : mSceneMgr(sceneMgr) PhysicsSystem::PhysicsSystem(Ogre::SceneManager *sceneMgr) : mSceneMgr(sceneMgr)
{ {
assert(!mPhysicsSystemInstance);
mPhysicsSystemInstance = this;
// Create physics. shapeLoader is deleted by the physic engine // Create physics. shapeLoader is deleted by the physic engine
NifBullet::ManualBulletShapeLoader* shapeLoader = new NifBullet::ManualBulletShapeLoader(); NifBullet::ManualBulletShapeLoader* shapeLoader = new NifBullet::ManualBulletShapeLoader();
mEngine = new OEngine::Physic::PhysicEngine(shapeLoader); mEngine = new OEngine::Physic::PhysicEngine(shapeLoader);
}
PhysicsSystem::~PhysicsSystem()
{
delete mEngine;
}
PhysicsSystem *PhysicsSystem::instance()
{
assert(mPhysicsSystemInstance);
return mPhysicsSystemInstance;
}
void PhysicsSystem::addObject(const std::string &mesh,
const std::string &name,
float scale,
const Ogre::Vector3 &position,
const Ogre::Quaternion &rotation,
bool placeable)
{
//mHandleToMesh[name] = mesh;
mEngine->createAndAdjustRigidBody(mesh, name, scale, position, rotation,
0, // scaledBoxTranslation
0, // boxRotation
true, // raycasting
placeable);
}
void PhysicsSystem::setSceneManager(Ogre::SceneManager *sceneMgr)
{
mSceneMgr = sceneMgr;
mEngine->setSceneManager(sceneMgr); // needed for toggleDebugRendering() mEngine->setSceneManager(sceneMgr); // needed for toggleDebugRendering()
@ -102,27 +139,6 @@ namespace CSVWorld
} }
} }
PhysicsSystem::~PhysicsSystem()
{
delete mEngine;
}
void PhysicsSystem::addObject(const std::string &mesh,
const std::string &name,
float scale,
const Ogre::Vector3 &position,
const Ogre::Quaternion &rotation,
bool placeable)
{
//mHandleToMesh[name] = mesh;
mEngine->createAndAdjustRigidBody(mesh, name, scale, position, rotation,
0, // scaledBoxTranslation
0, // boxRotation
true, // raycasting
placeable);
}
void PhysicsSystem::removeObject(const std::string& name) void PhysicsSystem::removeObject(const std::string& name)
{ {
mEngine->removeRigidBody(name); mEngine->removeRigidBody(name);
@ -131,6 +147,9 @@ namespace CSVWorld
void PhysicsSystem::toggleDebugRendering() void PhysicsSystem::toggleDebugRendering()
{ {
if(!mSceneMgr)
return; // FIXME: add a warning message
mEngine->toggleDebugRendering(); mEngine->toggleDebugRendering();
mEngine->stepSimulation(0.0167); // FIXME: DebugDrawer::step() not accessible mEngine->stepSimulation(0.0167); // FIXME: DebugDrawer::step() not accessible
} }
@ -138,6 +157,9 @@ namespace CSVWorld
std::pair<bool, Ogre::Vector3> PhysicsSystem::castRay(float mouseX, float mouseY, std::pair<bool, Ogre::Vector3> PhysicsSystem::castRay(float mouseX, float mouseY,
Ogre::Vector3* normal, std::string* hit, Ogre::Camera *camera) Ogre::Vector3* normal, std::string* hit, Ogre::Camera *camera)
{ {
if(!mSceneMgr)
return std::make_pair(false, Ogre::Vector3()); // FIXME: add a warning message
// using a really small value seems to mess up with the projections // using a really small value seems to mess up with the projections
float nearClipDistance = camera->getNearClipDistance(); float nearClipDistance = camera->getNearClipDistance();
camera->setNearClipDistance(10.0f); // arbitrary number camera->setNearClipDistance(10.0f); // arbitrary number

View file

@ -25,6 +25,7 @@ namespace CSVWorld
{ {
class PhysicsSystem class PhysicsSystem
{ {
static PhysicsSystem *mPhysicsSystemInstance;
//std::map<std::string, std::string> mHandleToMesh; //std::map<std::string, std::string> mHandleToMesh;
OEngine::Physic::PhysicEngine* mEngine; OEngine::Physic::PhysicEngine* mEngine;
Ogre::SceneManager *mSceneMgr; Ogre::SceneManager *mSceneMgr;
@ -32,9 +33,13 @@ namespace CSVWorld
public: public:
PhysicsSystem(Ogre::SceneManager *sceneMgr); PhysicsSystem(Ogre::SceneManager *sceneMgr = NULL);
~PhysicsSystem(); ~PhysicsSystem();
static PhysicsSystem *instance();
void setSceneManager(Ogre::SceneManager *sceneMgr);
void addObject(const std::string &mesh, void addObject(const std::string &mesh,
const std::string &name, const std::string &name,
float scale, float scale,