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     //-----------------------------------------------------------------------
00159     PlaneBoundedVolumeListSceneQuery::PlaneBoundedVolumeListSceneQuery(SceneManager* mgr)
00160         : RegionSceneQuery(mgr)
00161     {
00162     }
00163     //-----------------------------------------------------------------------
00164     PlaneBoundedVolumeListSceneQuery::~PlaneBoundedVolumeListSceneQuery()
00165     {
00166     }
00167     //-----------------------------------------------------------------------
00168     void PlaneBoundedVolumeListSceneQuery::setVolumes(const PlaneBoundedVolumeList& volumes)
00169     {
00170         mVolumes = volumes;
00171     }
00172     //-----------------------------------------------------------------------
00173     const PlaneBoundedVolumeList& PlaneBoundedVolumeListSceneQuery::getVolumes() const
00174     {
00175         return mVolumes;
00176     }
00177 
00178     //-----------------------------------------------------------------------
00179     RaySceneQuery::RaySceneQuery(SceneManager* mgr) : SceneQuery(mgr)
00180     {
00181         mSortByDistance = false;
00182         mMaxResults = 0;
00183         mLastResult = NULL;
00184     }
00185     //-----------------------------------------------------------------------
00186     RaySceneQuery::~RaySceneQuery()
00187     {
00188         clearResults();
00189     }
00190     //-----------------------------------------------------------------------
00191     void RaySceneQuery::setRay(const Ray& ray)
00192     {
00193         mRay = ray;
00194     }
00195     //-----------------------------------------------------------------------
00196     const Ray& RaySceneQuery::getRay(void) const
00197     {
00198         return mRay;
00199     }
00200     //-----------------------------------------------------------------------
00201     void RaySceneQuery::setSortByDistance(bool sort, ushort maxresults)
00202     {
00203         mSortByDistance = sort;
00204         mMaxResults = maxresults;
00205     }
00206     //-----------------------------------------------------------------------
00207     bool RaySceneQuery::getSortByDistance(void) const
00208     {
00209         return mSortByDistance;
00210     }
00211     //-----------------------------------------------------------------------
00212     ushort RaySceneQuery::getMaxResults(void) const
00213     {
00214         return mMaxResults;
00215     }
00216     //-----------------------------------------------------------------------
00217     RaySceneQueryResult& RaySceneQuery::execute(void)
00218     {
00219         clearResults();
00220         mLastResult = new RaySceneQueryResult();
00221         // Call callback version with self as listener
00222         this->execute(this);
00223 
00224         if (mSortByDistance)
00225         {
00226             // Perform sort
00227             mLastResult->sort();
00228 
00229             if (mMaxResults && mLastResult->size() > mMaxResults)
00230             {
00231                 // Constrain to maxresults
00232                 RaySceneQueryResult::iterator start;
00233                 int x = 0;
00234                 for(start = mLastResult->begin(); x < mMaxResults; ++x)
00235                 {
00236                     // Increment deletion start point
00237                     ++start;
00238                 }
00239                 // erase
00240                 mLastResult->erase(start, mLastResult->end());
00241             }
00242         }
00243 
00244         return *mLastResult;
00245 
00246     }
00247     //-----------------------------------------------------------------------
00248     RaySceneQueryResult& RaySceneQuery::getLastResults(void) const
00249     {
00250         assert (mLastResult);
00251         return *mLastResult;
00252     }
00253     //-----------------------------------------------------------------------
00254     void RaySceneQuery::clearResults(void)
00255     {
00256         if (mLastResult)
00257         {
00258             delete mLastResult;
00259         }
00260         mLastResult = NULL;
00261     }
00262     //-----------------------------------------------------------------------
00263     bool RaySceneQuery::queryResult(MovableObject* obj, Real distance)
00264     {
00265         // Add to internal list
00266         RaySceneQueryResultEntry dets;
00267         dets.distance = distance;
00268         dets.movable = obj;
00269         dets.worldFragment = NULL;
00270         mLastResult->push_back(dets);
00271         // Continue
00272         return true;
00273     }
00274     //-----------------------------------------------------------------------
00275     bool RaySceneQuery::queryResult(SceneQuery::WorldFragment* fragment, Real distance)
00276     {
00277         // Add to internal list
00278         RaySceneQueryResultEntry dets;
00279         dets.distance = distance;
00280         dets.movable = NULL;
00281         dets.worldFragment = fragment;
00282         mLastResult->push_back(dets);
00283         // Continue
00284         return true;
00285     }
00286     //-----------------------------------------------------------------------
00287     /*
00288     PyramidSceneQuery::PyramidSceneQuery(SceneManager* mgr) : RegionSceneQuery(mgr)
00289     {
00290     }
00291     //-----------------------------------------------------------------------
00292     PyramidSceneQuery::~PyramidSceneQuery()
00293     {
00294     }
00295     */
00296     //-----------------------------------------------------------------------
00297     IntersectionSceneQuery::IntersectionSceneQuery(SceneManager* mgr)
00298     : SceneQuery(mgr), mLastResult(NULL)
00299     {
00300     }
00301     //-----------------------------------------------------------------------
00302     IntersectionSceneQuery::~IntersectionSceneQuery()
00303     {
00304         clearResults();
00305     }
00306     //-----------------------------------------------------------------------
00307     IntersectionSceneQueryResult& IntersectionSceneQuery::getLastResults(void) const
00308     {
00309         assert(mLastResult);
00310         return *mLastResult;
00311     }
00312     //-----------------------------------------------------------------------
00313     void IntersectionSceneQuery::clearResults(void)
00314     {
00315         if (mLastResult)
00316         {
00317             delete mLastResult;
00318         }
00319         mLastResult = NULL;
00320     }
00321     //---------------------------------------------------------------------
00322     IntersectionSceneQueryResult&
00323     IntersectionSceneQuery::execute(void)
00324     {
00325         clearResults();
00326         mLastResult = new IntersectionSceneQueryResult();
00327         // Call callback version with self as listener
00328         execute(this);
00329         return *mLastResult;
00330     }
00331     //---------------------------------------------------------------------
00332     bool IntersectionSceneQuery::
00333         queryResult(MovableObject* first, MovableObject* second)
00334     {
00335         // Add to internal list
00336         mLastResult->movables2movables.push_back(
00337             SceneQueryMovableObjectPair(first, second)
00338             );
00339         // Continue
00340         return true;
00341     }
00342     //---------------------------------------------------------------------
00343     bool IntersectionSceneQuery::
00344         queryResult(MovableObject* movable, SceneQuery::WorldFragment* fragment)
00345     {
00346         // Add to internal list
00347         mLastResult->movables2world.push_back(
00348             SceneQueryMovableObjectWorldFragmentPair(movable, fragment)
00349             );
00350         // Continue
00351         return true;
00352     }
00353 
00354 
00355 
00356 
00357 }
00358     
00359 
00360 
00361 

Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:45 2004