40 #ifndef _rayquerytool_cpp
41 #define _rayquerytool_cpp
45 #include "rayquerytool.h"
47 #include "actormanager.h"
48 #include "areaeffect.h"
49 #include "areaeffectmanager.h"
52 #include "eventmanager.h"
53 #include "worldobject.h"
60 #include "Graphics/graphicsmanager.h"
61 #include "Graphics/scenemanager.h"
62 #include "Graphics/cameramanager.h"
64 #include "Graphics/gamewindow.h"
65 #include "Graphics/viewport.h"
68 #include "Input/mouse.h"
69 #include "Input/inputmanager.h"
71 #include "Internal/meshtools.h.cpp"
92 typedef Ogre::RaySceneQuery* TargetPtrType;
94 typedef Ogre::RaySceneQuery TargetType;
96 Ogre::RaySceneQuery* RayQuery;
101 RayQuery = Entresol::GetSingletonPtr()->GetSceneManager()->_GetGraphicsWorldPointer()->createRayQuery(Ogre::Ray(), Ogre::SceneManager::WORLD_GEOMETRY_TYPE_MASK);
102 RayQuery->setSortByDistance(
true);
108 { Entresol::GetSingletonPtr()->GetSceneManager()->_GetGraphicsWorldPointer()->destroyQuery(RayQuery); }
113 void SetPointer(TargetPtrType Value)
114 { RayQuery = Value; }
117 TargetPtrType GetPointer()
121 typedef ManagedPtr<RayQueryHandle> ManagedRayQuery;
128 Boolean ExecuteQuery(ManagedRayQuery& RayQuery, Ogre::Ray& Ooray)
132 RayQuery->setRay(Ooray);
133 RayQuery->setQueryMask(-1);
134 if( RayQuery->execute().size() <= 0 )
138 MEZZ_EXCEPTION(Exception::PARAMETERS_EXCEPTION,
"Attempting to run a query on Null RaySceneQuery");
143 RayQueryTool::RayQueryTool()
144 : ValidResult(false), IntersectedObject(NULL)
155 IntersectedObject = NULL;
160 {
return ValidResult; }
166 {
return IntersectedObject; }
178 ManagedRayQuery RayQuery;
181 if(!ExecuteQuery(RayQuery, Ooray))
191 IntersectedObject=NULL;
192 Ogre::RaySceneQueryResult &query_result = RayQuery->getLastResults();
193 for (
size_t qr_idx = 0; qr_idx < query_result.size(); qr_idx++)
196 if ( (0.0f <= closest_distance) && (closest_distance < query_result[qr_idx].distance))
200 if ((NULL != query_result[qr_idx].movable) && (0 == query_result[qr_idx].movable->getMovableType().compare(
"Entity")))
203 Ogre::Entity *pentity =
static_cast<Ogre::Entity*
>(query_result[qr_idx].movable);
208 if( HitMetaInfo && ( HitMetaInfo->
GetType() & ObjectFlags ) )
213 Ogre::Vector3 *vertices;
214 unsigned long *indices;
217 Internal::MeshTools::GetMeshInformation( pentity, vertex_count, vertices, index_count, indices,
218 pentity->getParentNode()->_getDerivedPosition(),
219 pentity->getParentNode()->_getDerivedOrientation(),
220 pentity->getParentNode()->_getDerivedScale());
223 bool new_closest_found =
false;
224 for (
size_t i = 0; i < index_count; i += 3)
227 std::pair<bool, Ogre::Real> hit = Ogre::Math::intersects(Ooray, vertices[indices[i]], vertices[indices[i+1]], vertices[indices[i+2]],
true,
false);
230 if (hit.first && ((0.0f > closest_distance) || (hit.second < closest_distance)) )
232 closest_distance = hit.second;
233 new_closest_found =
true;
242 if (new_closest_found)
244 closest_result = Ooray.getPoint(closest_distance);
245 IntersectedObject = Ogre::any_cast<Graphics::RenderableProxy*>(pentity->getUserAny())->GetParentObject();
257 if (IntersectedObject)
269 ManagedRayQuery RayQuery;
271 if(!ExecuteQuery(RayQuery, Ooray))
274 Ogre::RaySceneQueryResult &query_result = RayQuery->getLastResults();
276 if (0 < query_result.size())
278 Ogre::Entity *pentity =
static_cast<Ogre::Entity*
>(query_result[0].movable);
303 else if(QueryPlane.
Normal.
Y != 0)
314 Real SMALL_NUM = 0.00000001;
316 if( (D<0? -D : D) < SMALL_NUM)
321 IntersectedObject=NULL;
341 Offset=return_vector;
342 IntersectedObject=NULL;
345 }
catch(exception e) {
347 Entresol::GetSingletonPtr()->
Log(
"WorldQueryTool Error:Failed while calculating Ray/Plane Intersection, Assuming no valid intersection. Error follows:");
374 if( VersionAttr && VersionAttr.
SetValue(
"1"))
377 if( ResultAttr && ResultAttr.
SetValue(ValidResult))
389 if( WorldObjectAttr )
391 if( IntersectedObject )
420 if (WorldObjectName.size()) {
423 if( IntersectedObject == NULL ) {
426 if( IntersectedObject == NULL ) {
430 IntersectedObject = NULL;
441 {
return "RayQueryTool"; }