mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-01-21 09:53:50 +00:00
Cleanup for better legibility.
This commit is contained in:
parent
fb0f85c8db
commit
dd2c067e17
2 changed files with 149 additions and 140 deletions
|
@ -1,6 +1,6 @@
|
||||||
#include "physicssystem.hpp"
|
#include "physicssystem.hpp"
|
||||||
|
|
||||||
#include <iostream> // FIXME: debug only
|
#include <iostream>
|
||||||
|
|
||||||
#include <OgreRay.h>
|
#include <OgreRay.h>
|
||||||
#include <OgreCamera.h>
|
#include <OgreCamera.h>
|
||||||
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
// FIXME: this section should be removed once the debugging is completed
|
||||||
void showHitPoint(Ogre::SceneManager *sceneMgr, std::string name, Ogre::Vector3 point)
|
void showHitPoint(Ogre::SceneManager *sceneMgr, std::string name, Ogre::Vector3 point)
|
||||||
{
|
{
|
||||||
sceneMgr->destroyManualObject("manual" + name);
|
sceneMgr->destroyManualObject("manual" + name);
|
||||||
|
@ -73,12 +74,8 @@ namespace CSVWorld
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsSystem::addObject(const std::string &mesh,
|
void PhysicsSystem::addObject(const std::string &mesh,
|
||||||
const std::string &name,
|
const std::string &name, const std::string &referenceId, float scale,
|
||||||
const std::string &referenceId,
|
const Ogre::Vector3 &position, const Ogre::Quaternion &rotation, bool placeable)
|
||||||
float scale,
|
|
||||||
const Ogre::Vector3 &position,
|
|
||||||
const Ogre::Quaternion &rotation,
|
|
||||||
bool placeable)
|
|
||||||
{
|
{
|
||||||
mRefToSceneNode[referenceId] = name;
|
mRefToSceneNode[referenceId] = name;
|
||||||
|
|
||||||
|
@ -89,12 +86,93 @@ namespace CSVWorld
|
||||||
placeable);
|
placeable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PhysicsSystem::removeObject(const std::string& name)
|
||||||
|
{
|
||||||
|
mEngine->removeRigidBody(name);
|
||||||
|
mEngine->deleteRigidBody(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PhysicsSystem::addHeightField(float* heights, int x, int y, float yoffset,
|
||||||
|
float triSize, float sqrtVerts)
|
||||||
|
{
|
||||||
|
mEngine->addHeightField(heights, x, y, yoffset, triSize, sqrtVerts);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PhysicsSystem::removeHeightField(int x, int y)
|
||||||
|
{
|
||||||
|
mEngine->removeHeightField(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::pair<std::string, Ogre::Vector3> PhysicsSystem::castRay(float mouseX, float mouseY,
|
||||||
|
Ogre::Vector3* normal, std::string* hit, Ogre::Camera *camera)
|
||||||
|
{
|
||||||
|
if(!mSceneMgr || !camera || !camera->getViewport())
|
||||||
|
return std::make_pair("", Ogre::Vector3(0,0,0)); // FIXME: this should be an exception
|
||||||
|
|
||||||
|
|
||||||
|
// using a really small value seems to mess up with the projections
|
||||||
|
float nearClipDistance = camera->getNearClipDistance(); // save existing
|
||||||
|
camera->setNearClipDistance(10.0f); // arbitrary number
|
||||||
|
Ogre::Ray ray = camera->getCameraToViewportRay(mouseX, mouseY);
|
||||||
|
camera->setNearClipDistance(nearClipDistance); // restore
|
||||||
|
|
||||||
|
Ogre::Vector3 from = ray.getOrigin();
|
||||||
|
CSMSettings::UserSettings &userSettings = CSMSettings::UserSettings::instance();
|
||||||
|
float farClipDist = userSettings.setting("Scene/far clip distance", QString("300000")).toFloat();
|
||||||
|
Ogre::Vector3 to = ray.getPoint(farClipDist);
|
||||||
|
|
||||||
|
btVector3 _from, _to;
|
||||||
|
_from = btVector3(from.x, from.y, from.z);
|
||||||
|
_to = btVector3(to.x, to.y, to.z);
|
||||||
|
|
||||||
|
uint32_t visibilityMask = camera->getViewport()->getVisibilityMask();
|
||||||
|
bool ignoreHeightMap = !(visibilityMask & (uint32_t)CSVRender::Element_Terrain);
|
||||||
|
bool ignoreObjects = !(visibilityMask & (uint32_t)CSVRender::Element_Reference);
|
||||||
|
|
||||||
|
Ogre::Vector3 norm;
|
||||||
|
std::pair<std::string, float> result =
|
||||||
|
mEngine->rayTest(_from, _to, !ignoreObjects, ignoreHeightMap, &norm);
|
||||||
|
|
||||||
|
if(result.first == "")
|
||||||
|
return std::make_pair("", Ogre::Vector3(0,0,0)); // rayTest found nothing
|
||||||
|
|
||||||
|
Ogre::Vector3 position = ray.getPoint(farClipDist*result.second);
|
||||||
|
std::string sceneNode = mRefToSceneNode[result.first];
|
||||||
|
if(!ignoreObjects && mSceneMgr->hasSceneNode(sceneNode))
|
||||||
|
{
|
||||||
|
if(userSettings.setting("debug/mouse-picking", QString("false")) == "true" ? true : false)
|
||||||
|
updateSelectionHighlight(sceneNode, position);
|
||||||
|
}
|
||||||
|
// else terrain
|
||||||
|
return std::make_pair(result.first, position);
|
||||||
|
}
|
||||||
|
|
||||||
void PhysicsSystem::setSceneManager(Ogre::SceneManager *sceneMgr)
|
void PhysicsSystem::setSceneManager(Ogre::SceneManager *sceneMgr)
|
||||||
{
|
{
|
||||||
mSceneMgr = sceneMgr;
|
mSceneMgr = sceneMgr;
|
||||||
|
|
||||||
mEngine->setSceneManager(sceneMgr); // needed for toggleDebugRendering()
|
mEngine->setSceneManager(sceneMgr); // needed for toggleDebugRendering()
|
||||||
|
|
||||||
|
initDebug();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PhysicsSystem::toggleDebugRendering()
|
||||||
|
{
|
||||||
|
if(!mSceneMgr)
|
||||||
|
return; // FIXME: maybe this should be an exception
|
||||||
|
|
||||||
|
CSMSettings::UserSettings &userSettings = CSMSettings::UserSettings::instance();
|
||||||
|
if(!(userSettings.setting("debug/mouse-picking", QString("false")) == "true" ? true : false))
|
||||||
|
{
|
||||||
|
std::cerr << "Turn on mouse-picking debug option to see collision shapes." << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
mEngine->toggleDebugRendering();
|
||||||
|
mEngine->stepSimulation(0.0167); // DebugDrawer::step() not directly accessible
|
||||||
|
}
|
||||||
|
|
||||||
|
void PhysicsSystem::initDebug()
|
||||||
|
{
|
||||||
// material for visual cue on selected objects
|
// material for visual cue on selected objects
|
||||||
Ogre::TexturePtr texture = Ogre::TextureManager::getSingleton().getByName("DynamicTrans");
|
Ogre::TexturePtr texture = Ogre::TextureManager::getSingleton().getByName("DynamicTrans");
|
||||||
if(texture.isNull())
|
if(texture.isNull())
|
||||||
|
@ -150,76 +228,17 @@ namespace CSVWorld
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsSystem::removeObject(const std::string& name)
|
void PhysicsSystem::updateSelectionHighlight(std::string sceneNode, const Ogre::Vector3 &position)
|
||||||
{
|
|
||||||
mEngine->removeRigidBody(name);
|
|
||||||
mEngine->deleteRigidBody(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
void PhysicsSystem::addHeightField(float* heights, int x, int y, float yoffset,
|
|
||||||
float triSize, float sqrtVerts)
|
|
||||||
{
|
|
||||||
mEngine->addHeightField(heights, x, y, yoffset, triSize, sqrtVerts);
|
|
||||||
}
|
|
||||||
|
|
||||||
void PhysicsSystem::removeHeightField(int x, int y)
|
|
||||||
{
|
|
||||||
mEngine->removeHeightField(x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
void PhysicsSystem::toggleDebugRendering()
|
|
||||||
{
|
{
|
||||||
CSMSettings::UserSettings &userSettings = CSMSettings::UserSettings::instance();
|
CSMSettings::UserSettings &userSettings = CSMSettings::UserSettings::instance();
|
||||||
bool debug = userSettings.setting ("debug/mouse-picking", QString("false")) == "true" ? true : false;
|
bool debugCursor = userSettings.setting(
|
||||||
if(!mSceneMgr || !debug)
|
"debug/mouse-position", QString("false")) == "true" ? true : false;
|
||||||
return; // FIXME: add a warning message
|
|
||||||
|
|
||||||
mEngine->toggleDebugRendering();
|
|
||||||
mEngine->stepSimulation(0.0167); // DebugDrawer::step() not directly accessible
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME: this method needs a cleanup/refactoring
|
|
||||||
std::pair<std::string, Ogre::Vector3> PhysicsSystem::castRay(float mouseX, float mouseY,
|
|
||||||
Ogre::Vector3* normal, std::string* hit, Ogre::Camera *camera)
|
|
||||||
{
|
|
||||||
CSMSettings::UserSettings &userSettings = CSMSettings::UserSettings::instance();
|
|
||||||
bool debug = userSettings.setting ("debug/mouse-picking", QString("false")) == "true" ? true : false;
|
|
||||||
if(!mSceneMgr || !camera || !camera->getViewport())
|
|
||||||
return std::make_pair("", Ogre::Vector3(0,0,0)); // FIXME: add a warning message
|
|
||||||
|
|
||||||
// using a really small value seems to mess up with the projections
|
|
||||||
float nearClipDistance = camera->getNearClipDistance();
|
|
||||||
camera->setNearClipDistance(10.0f); // arbitrary number
|
|
||||||
Ogre::Ray ray = camera->getCameraToViewportRay(mouseX, mouseY);
|
|
||||||
camera->setNearClipDistance(nearClipDistance);
|
|
||||||
|
|
||||||
Ogre::Vector3 from = ray.getOrigin();
|
|
||||||
float farClipDist = userSettings.setting("Scene/far clip distance", QString("300000")).toFloat();
|
|
||||||
Ogre::Vector3 to = ray.getPoint(farClipDist);
|
|
||||||
|
|
||||||
btVector3 _from, _to;
|
|
||||||
_from = btVector3(from.x, from.y, from.z);
|
|
||||||
_to = btVector3(to.x, to.y, to.z);
|
|
||||||
|
|
||||||
uint32_t visibilityMask = camera->getViewport()->getVisibilityMask();
|
|
||||||
bool ignoreHeightMap = !(visibilityMask & (uint32_t)CSVRender::Element_Terrain);
|
|
||||||
bool ignoreObjects = !(visibilityMask & (uint32_t)CSVRender::Element_Reference);
|
|
||||||
|
|
||||||
Ogre::Vector3 norm;
|
|
||||||
std::pair<std::string, float> result =
|
|
||||||
mEngine->rayTest(_from, _to, !ignoreObjects, ignoreHeightMap, &norm);
|
|
||||||
|
|
||||||
if(result.first == "")
|
|
||||||
return std::make_pair("", Ogre::Vector3(0,0,0));
|
|
||||||
|
|
||||||
std::string sceneNode = mRefToSceneNode[result.first];
|
|
||||||
if(!ignoreObjects && mSceneMgr->hasSceneNode(sceneNode))
|
|
||||||
{
|
|
||||||
//TODO: Try http://www.ogre3d.org/tikiwiki/Create+outline+around+a+character
|
//TODO: Try http://www.ogre3d.org/tikiwiki/Create+outline+around+a+character
|
||||||
Ogre::SceneNode *scene = mSceneMgr->getSceneNode(sceneNode);
|
Ogre::SceneNode *scene = mSceneMgr->getSceneNode(sceneNode);
|
||||||
std::map<std::string, std::vector<std::string> >::iterator iter =
|
std::map<std::string, std::vector<std::string> >::iterator iter =
|
||||||
mSelectedEntities.find(sceneNode);
|
mSelectedEntities.find(sceneNode);
|
||||||
if(debug && iter != mSelectedEntities.end()) // currently selected
|
if(iter != mSelectedEntities.end()) // currently selected
|
||||||
{
|
{
|
||||||
std::vector<std::string> clonedEntities = mSelectedEntities[sceneNode];
|
std::vector<std::string> clonedEntities = mSelectedEntities[sceneNode];
|
||||||
while(!clonedEntities.empty())
|
while(!clonedEntities.empty())
|
||||||
|
@ -233,12 +252,10 @@ namespace CSVWorld
|
||||||
}
|
}
|
||||||
mSelectedEntities.erase(iter);
|
mSelectedEntities.erase(iter);
|
||||||
|
|
||||||
bool debugCursor = userSettings.setting (
|
if(debugCursor)
|
||||||
"debug/mouse-position", QString("false")) == "true" ? true : false;
|
|
||||||
if(debugCursor) // FIXME: show cursor position for debugging
|
|
||||||
removeHitPoint(mSceneMgr, sceneNode);
|
removeHitPoint(mSceneMgr, sceneNode);
|
||||||
}
|
}
|
||||||
else if(debug)
|
else
|
||||||
{
|
{
|
||||||
std::vector<std::string> clonedEntities;
|
std::vector<std::string> clonedEntities;
|
||||||
Ogre::SceneNode::ObjectIterator iter = scene->getAttachedObjectIterator();
|
Ogre::SceneNode::ObjectIterator iter = scene->getAttachedObjectIterator();
|
||||||
|
@ -269,22 +286,11 @@ namespace CSVWorld
|
||||||
scene->attachObject(clone);
|
scene->attachObject(clone);
|
||||||
clonedEntities.push_back(entity->getName()+"cover");
|
clonedEntities.push_back(entity->getName()+"cover");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
mSelectedEntities[sceneNode] = clonedEntities;
|
mSelectedEntities[sceneNode] = clonedEntities;
|
||||||
|
|
||||||
bool debugCursor = userSettings.setting (
|
if(debugCursor)
|
||||||
"debug/mouse-position", QString("false")) == "true" ? true : false;
|
showHitPoint(mSceneMgr, sceneNode, position);
|
||||||
if(debugCursor) // FIXME: show cursor position for debugging
|
|
||||||
showHitPoint(mSceneMgr, sceneNode, ray.getPoint(farClipDist*result.second));
|
|
||||||
}
|
|
||||||
|
|
||||||
return std::make_pair(result.first, ray.getPoint(farClipDist*result.second));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// terrain
|
|
||||||
return std::make_pair(result.first, ray.getPoint(farClipDist*result.second));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,7 @@ namespace CSVWorld
|
||||||
static PhysicsSystem *mPhysicsSystemInstance;
|
static PhysicsSystem *mPhysicsSystemInstance;
|
||||||
std::map<std::string, std::string> mRefToSceneNode;
|
std::map<std::string, std::string> mRefToSceneNode;
|
||||||
OEngine::Physic::PhysicEngine* mEngine;
|
OEngine::Physic::PhysicEngine* mEngine;
|
||||||
|
|
||||||
Ogre::SceneManager *mSceneMgr;
|
Ogre::SceneManager *mSceneMgr;
|
||||||
std::map<std::string, std::vector<std::string> > mSelectedEntities;
|
std::map<std::string, std::vector<std::string> > mSelectedEntities;
|
||||||
|
|
||||||
|
@ -40,12 +41,9 @@ namespace CSVWorld
|
||||||
|
|
||||||
void setSceneManager(Ogre::SceneManager *sceneMgr);
|
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,
|
const std::string &referenceId, float scale,
|
||||||
const std::string &referenceId,
|
const Ogre::Vector3 &position, const Ogre::Quaternion &rotation,
|
||||||
float scale,
|
|
||||||
const Ogre::Vector3 &position,
|
|
||||||
const Ogre::Quaternion &rotation,
|
|
||||||
bool placeable=false);
|
bool placeable=false);
|
||||||
|
|
||||||
void removeObject(const std::string &name);
|
void removeObject(const std::string &name);
|
||||||
|
@ -59,6 +57,11 @@ namespace CSVWorld
|
||||||
|
|
||||||
std::pair<std::string, Ogre::Vector3> castRay(float mouseX, float mouseY,
|
std::pair<std::string, Ogre::Vector3> castRay(float mouseX, float mouseY,
|
||||||
Ogre::Vector3* normal, std::string* hit, Ogre::Camera *camera);
|
Ogre::Vector3* normal, std::string* hit, Ogre::Camera *camera);
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
void initDebug();
|
||||||
|
void updateSelectionHighlight(std::string sceneNode, const Ogre::Vector3 &position);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue