diff --git a/apps/openmw/mwinput/inputmanager.cpp b/apps/openmw/mwinput/inputmanager.cpp index 484491e09..f851b43b2 100644 --- a/apps/openmw/mwinput/inputmanager.cpp +++ b/apps/openmw/mwinput/inputmanager.cpp @@ -59,10 +59,6 @@ namespace MWInput MWRender::PlayerPos &player; MWGui::WindowManager &windows; - //may be better placed at OEngine::Renderer - Ogre::RaySceneQuery *mRaySceneQuery; - - // Count screenshots. int shotCount; @@ -141,47 +137,7 @@ namespace MWInput void activate() { - Ogre::Camera *mCamera = ogre.getCamera(); - - //get a ray pointing to the center of the viewport - Ogre::Ray centerRay = mCamera->getCameraToViewportRay ( 0.5, 0.5 ); - - // get all objects touched by the ray - mRaySceneQuery->setRay ( centerRay ); - Ogre::RaySceneQueryResult &result = mRaySceneQuery->execute(); - Ogre::RaySceneQueryResult::iterator itr = result.begin(); - - Ogre::RaySceneQueryResult::iterator nearest = result.end(); - for ( ; itr != result.end(); itr++ ) - { - /*if ( itr->worldFragment ) //world fragments aren't currently used by openw - { - Ogre::Vector3 location = itr->worldFragment->singleIntersection; - std::cout << "WorldFragment: (" << location.x << ", " << location.y << ", " << location.z << ")" << std::endl; - } // if*/ - - // Is this result a MovableObject? - // there seem to be omnipresent objects like the caelum sky dom, - // the distance of these objects is always 0 so this if excludes these - if ( itr->movable && itr->distance >= 0.1) - { - std::cout << "Movobj: " << itr->movable->getName() << " dist: " << itr->distance << "\n"; - - if ( nearest == result.end() ) //if no object is set - { - nearest = itr; - } - else if ( itr->distance < nearest->distance ) - { - nearest = itr; - } - } - } - - if ( nearest != result.end() ) - std::cout << "Nearest MovableObject: " << nearest->movable->getName() - << " Distance: " << nearest->distance << std::endl; } // Exit program now button (which is disabled in GUI mode) @@ -251,9 +207,6 @@ namespace MWInput // Start out in game mode setGuiMode(MWGui::GM_Game); - //init rayscene query (would be also better placed at Oengine::Renderer) - mRaySceneQuery = ogre.getScene()->createRayQuery(Ogre::Ray()); - /********************************** Key binding section diff --git a/apps/openmw/mwrender/mwscene.cpp b/apps/openmw/mwrender/mwscene.cpp index 489b5f242..7ba75193a 100644 --- a/apps/openmw/mwrender/mwscene.cpp +++ b/apps/openmw/mwrender/mwscene.cpp @@ -31,4 +31,54 @@ MWScene::MWScene(OEngine::Render::OgreRenderer &_rend) SceneNode *rt = rend.getScene()->getRootSceneNode(); mwRoot = rt->createChildSceneNode(); mwRoot->pitch(Degree(-90)); + + //used to obtain ingame information of ogre objects (which are faced or selected) + mRaySceneQuery = rend.getScene()->createRayQuery(Ray()); } + +void MWScene::getFacedHandle(std::string& handle, float& distance) +{ + handle = ""; + distance = -1; + + //get a ray pointing to the center of the viewport + Ray centerRay = getCamera()->getCameraToViewportRay( + getViewport()->getWidth()/2, + getViewport()->getHeight()/2); + + // get all objects touched by the ray + getRaySceneQuery()->setRay (centerRay ); + RaySceneQueryResult &result = getRaySceneQuery()->execute(); + + RaySceneQueryResult::iterator nearest = result.end(); + + for (RaySceneQueryResult::iterator itr = result.begin(); + itr != result.end(); itr++ ) + { + // there seem to be omnipresent objects like the caelum sky dom, + // the distance of these objects is always 0 so this if excludes these + // TODO: Check if the object can be focused (ignore walls etc.. + // in this state of openmw not possible) + if ( itr->movable && itr->distance >= 0.1) + { + if ( nearest == result.end() ) //if no object is set + { + nearest = itr; + } + else if ( itr->distance < nearest->distance ) + { + nearest = itr; + } + } + } + + if ( nearest != result.end() ) + { + std::cout << "Nearest MovableObject: " << nearest->movable->getParentSceneNode()->getName() + << " Distance: " << nearest->distance << std::endl; + + handle = nearest->movable->getParentSceneNode()->getName(); + distance = nearest->distance; + } +} + diff --git a/apps/openmw/mwrender/mwscene.hpp b/apps/openmw/mwrender/mwscene.hpp index a7339e1c8..fcd63df39 100644 --- a/apps/openmw/mwrender/mwscene.hpp +++ b/apps/openmw/mwrender/mwscene.hpp @@ -9,6 +9,7 @@ namespace Ogre class Viewport; class SceneManager; class SceneNode; + class RaySceneQuery; } namespace MWRender @@ -26,6 +27,7 @@ namespace MWRender // that the OGRE coordinate system matches that used internally in // Morrowind. Ogre::SceneNode *mwRoot; + Ogre::RaySceneQuery *mRaySceneQuery; public: MWScene(OEngine::Render::OgreRenderer &_rend); @@ -34,6 +36,10 @@ namespace MWRender Ogre::SceneNode *getRoot() { return mwRoot; } Ogre::SceneManager *getMgr() { return rend.getScene(); } Ogre::Viewport *getViewport() { return rend.getViewport(); } + Ogre::RaySceneQuery *getRaySceneQuery() { return mRaySceneQuery; } + + //gets the handle of the object the player is looking at + void getFacedHandle(std::string& handle, float& distance); }; }