Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreSceneQuery.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright © 2000-2003 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #include "OgreStableHeaders.h"
00026 #include "OgreSceneQuery.h"
00027 #include "OgreException.h"
00028 
00029 namespace Ogre {
00030 
00031     //-----------------------------------------------------------------------
00032     SceneQuery::SceneQuery(SceneManager* mgr)
00033         : mParentSceneMgr(mgr), mQueryMask(0xFFFFFFFF), mWorldFragmentType(SceneQuery::WFT_NONE)
00034     {
00035     }
00036     //-----------------------------------------------------------------------
00037     SceneQuery::~SceneQuery()
00038     {
00039     }
00040     //-----------------------------------------------------------------------
00041     void SceneQuery::setQueryMask(unsigned long mask)
00042     {
00043         mQueryMask = mask;
00044     }
00045     //-----------------------------------------------------------------------
00046     unsigned long SceneQuery::getQueryMask(void) const
00047     {
00048         return mQueryMask;
00049     }
00050     //-----------------------------------------------------------------------
00051     void SceneQuery::setWorldFragmentType(enum SceneQuery::WorldFragmentType wft)
00052     {
00053         // Check supported
00054         if (mSupportedWorldFragments.find(wft) == mSupportedWorldFragments.end())
00055         {
00056             Except(Exception::ERR_INVALIDPARAMS, "This world fragment type is not supported.",
00057                 "SceneQuery::setWorldFragmentType");
00058         }
00059         mWorldFragmentType = wft;
00060     }
00061     //-----------------------------------------------------------------------
00062     SceneQuery::WorldFragmentType 
00063     SceneQuery::getWorldFragmentType(void) const
00064     {
00065         return mWorldFragmentType;
00066     }
00067     //-----------------------------------------------------------------------
00068     RegionSceneQuery::RegionSceneQuery(SceneManager* mgr)
00069         :SceneQuery(mgr), mLastResult(NULL)
00070     {
00071     }
00072     //-----------------------------------------------------------------------
00073     RegionSceneQuery::~RegionSceneQuery()
00074     {
00075         clearResults();
00076     }
00077     //-----------------------------------------------------------------------
00078     SceneQueryResult& RegionSceneQuery::getLastResults(void) const
00079     {
00080         assert(mLastResult);
00081         return *mLastResult;
00082     }
00083     //-----------------------------------------------------------------------
00084     void RegionSceneQuery::clearResults(void)
00085     {
00086         if (mLastResult)
00087         {
00088             delete mLastResult;
00089         }
00090         mLastResult = NULL;
00091     }
00092     //---------------------------------------------------------------------
00093     SceneQueryResult&
00094     RegionSceneQuery::execute(void)
00095     {
00096         clearResults();
00097         mLastResult = new SceneQueryResult();
00098         // Call callback version with self as listener
00099         execute(this);
00100         return *mLastResult;
00101     }
00102     //---------------------------------------------------------------------
00103     bool RegionSceneQuery::
00104         queryResult(MovableObject* obj)
00105     {
00106         // Add to internal list
00107         mLastResult->movables.push_back(obj);
00108         // Continue
00109         return true;
00110     }
00111     //---------------------------------------------------------------------
00112     bool RegionSceneQuery::queryResult(SceneQuery::WorldFragment* fragment)
00113     {
00114         // Add to internal list
00115         mLastResult->worldFragments.push_back(fragment);
00116         // Continue
00117         return true;
00118     }
00119     //-----------------------------------------------------------------------
00120     AxisAlignedBoxSceneQuery::AxisAlignedBoxSceneQuery(SceneManager* mgr)
00121         : RegionSceneQuery(mgr)
00122     {
00123     }
00124     //-----------------------------------------------------------------------
00125     AxisAlignedBoxSceneQuery::~AxisAlignedBoxSceneQuery()
00126     {
00127     }
00128     //-----------------------------------------------------------------------
00129     void AxisAlignedBoxSceneQuery::setBox(const AxisAlignedBox& box)
00130     {
00131         mAABB = box;
00132     }
00133     //-----------------------------------------------------------------------
00134     const AxisAlignedBox& AxisAlignedBoxSceneQuery::getBox(void) const
00135     {
00136         return mAABB;
00137     }
00138     //-----------------------------------------------------------------------
00139     SphereSceneQuery::SphereSceneQuery(SceneManager* mgr)
00140         : RegionSceneQuery(mgr)
00141     {
00142     }
00143     //-----------------------------------------------------------------------
00144     SphereSceneQuery::~SphereSceneQuery()
00145     {
00146     }
00147     //-----------------------------------------------------------------------
00148     void SphereSceneQuery::setSphere(const Sphere& sphere)
00149     {
00150         mSphere = sphere;
00151     }
00152     //-----------------------------------------------------------------------
00153     const Sphere& SphereSceneQuery::getSphere() const
00154     {
00155         return mSphere;
00156     }
00157     //-----------------------------------------------------------------------
00158     RaySceneQuery::RaySceneQuery(SceneManager* mgr) : SceneQuery(mgr)
00159     {
00160         mSortByDistance = false;
00161         mMaxResults = 0;
00162         mLastResult = NULL;
00163     }
00164     //-----------------------------------------------------------------------
00165     RaySceneQuery::~RaySceneQuery()
00166     {
00167         clearResults();
00168     }
00169     //-----------------------------------------------------------------------
00170     void RaySceneQuery::setRay(const Ray& ray)
00171     {
00172         mRay = ray;
00173     }
00174     //-----------------------------------------------------------------------
00175     const Ray& RaySceneQuery::getRay(void) const
00176     {
00177         return mRay;
00178     }
00179     //-----------------------------------------------------------------------
00180     void RaySceneQuery::setSortByDistance(bool sort, ushort maxresults)
00181     {
00182         mSortByDistance = sort;
00183         mMaxResults = maxresults;
00184     }
00185     //-----------------------------------------------------------------------
00186     bool RaySceneQuery::getSortByDistance(void) const
00187     {
00188         return mSortByDistance;
00189     }
00190     //-----------------------------------------------------------------------
00191     ushort RaySceneQuery::getMaxResults(void) const
00192     {
00193         return mMaxResults;
00194     }
00195     //-----------------------------------------------------------------------
00196     RaySceneQueryResult& RaySceneQuery::execute(void)
00197     {
00198         clearResults();
00199         mLastResult = new RaySceneQueryResult();
00200         // Call callback version with self as listener
00201         this->execute(this);
00202 
00203         if (mSortByDistance)
00204         {
00205             // Perform sort
00206             mLastResult->sort();
00207 
00208             if (mMaxResults && mLastResult->size() > mMaxResults)
00209             {
00210                 // Constrain to maxresults
00211                 RaySceneQueryResult::iterator start;
00212                 int x = 0;
00213                 for(start = mLastResult->begin(); x < mMaxResults; ++x)
00214                 {
00215                     // Increment deletion start point
00216                     ++start;
00217                 }
00218                 // erase
00219                 mLastResult->erase(start, mLastResult->end());
00220             }
00221         }
00222 
00223         return *mLastResult;
00224 
00225     }
00226     //-----------------------------------------------------------------------
00227     RaySceneQueryResult& RaySceneQuery::getLastResults(void) const
00228     {
00229         assert (mLastResult);
00230         return *mLastResult;
00231     }
00232     //-----------------------------------------------------------------------
00233     void RaySceneQuery::clearResults(void)
00234     {
00235         if (mLastResult)
00236         {
00237             delete mLastResult;
00238         }
00239         mLastResult = NULL;
00240     }
00241     //-----------------------------------------------------------------------
00242     bool RaySceneQuery::queryResult(MovableObject* obj, Real distance)
00243     {
00244         // Add to internal list
00245         RaySceneQueryResultEntry dets;
00246         dets.distance = distance;
00247         dets.movable = obj;
00248         dets.worldFragment = NULL;
00249         mLastResult->push_back(dets);
00250         // Continue
00251         return true;
00252     }
00253     //-----------------------------------------------------------------------
00254     bool RaySceneQuery::queryResult(SceneQuery::WorldFragment* fragment, Real distance)
00255     {
00256         // Add to internal list
00257         RaySceneQueryResultEntry dets;
00258         dets.distance = distance;
00259         dets.movable = NULL;
00260         dets.worldFragment = fragment;
00261         mLastResult->push_back(dets);
00262         // Continue
00263         return true;
00264     }
00265     //-----------------------------------------------------------------------
00266     /*
00267     PyramidSceneQuery::PyramidSceneQuery(SceneManager* mgr) : RegionSceneQuery(mgr)
00268     {
00269     }
00270     //-----------------------------------------------------------------------
00271     PyramidSceneQuery::~PyramidSceneQuery()
00272     {
00273     }
00274     */
00275     //-----------------------------------------------------------------------
00276     IntersectionSceneQuery::IntersectionSceneQuery(SceneManager* mgr)
00277     : SceneQuery(mgr), mLastResult(NULL)
00278     {
00279     }
00280     //-----------------------------------------------------------------------
00281     IntersectionSceneQuery::~IntersectionSceneQuery()
00282     {
00283         clearResults();
00284     }
00285     //-----------------------------------------------------------------------
00286     IntersectionSceneQueryResult& IntersectionSceneQuery::getLastResults(void) const
00287     {
00288         assert(mLastResult);
00289         return *mLastResult;
00290     }
00291     //-----------------------------------------------------------------------
00292     void IntersectionSceneQuery::clearResults(void)
00293     {
00294         if (mLastResult)
00295         {
00296             delete mLastResult;
00297         }
00298         mLastResult = NULL;
00299     }
00300     //---------------------------------------------------------------------
00301     IntersectionSceneQueryResult&
00302     IntersectionSceneQuery::execute(void)
00303     {
00304         clearResults();
00305         mLastResult = new IntersectionSceneQueryResult();
00306         // Call callback version with self as listener
00307         execute(this);
00308         return *mLastResult;
00309     }
00310     //---------------------------------------------------------------------
00311     bool IntersectionSceneQuery::
00312         queryResult(MovableObject* first, MovableObject* second)
00313     {
00314         // Add to internal list
00315         mLastResult->movables2movables.push_back(
00316             SceneQueryMovableObjectPair(first, second)
00317             );
00318         // Continue
00319         return true;
00320     }
00321     //---------------------------------------------------------------------
00322     bool IntersectionSceneQuery::
00323         queryResult(MovableObject* movable, SceneQuery::WorldFragment* fragment)
00324     {
00325         // Add to internal list
00326         mLastResult->movables2world.push_back(
00327             SceneQueryMovableObjectWorldFragmentPair(movable, fragment)
00328             );
00329         // Continue
00330         return true;
00331     }
00332 
00333 
00334 
00335 
00336 }
00337     
00338 
00339 
00340 

Copyright © 2002-2003 by The OGRE Team
Last modified Wed Jan 21 00:10:26 2004