[5] | 1 | /* |
---|
| 2 | ----------------------------------------------------------------------------- |
---|
| 3 | This source file is part of OGRE |
---|
| 4 | (Object-oriented Graphics Rendering Engine) |
---|
| 5 | For the latest info, see http://www.ogre3d.org/ |
---|
| 6 | |
---|
| 7 | Copyright 2000-2005 The OGRE Team |
---|
| 8 | Also see acknowledgements in Readme.html |
---|
| 9 | |
---|
| 10 | This program is free software; you can redistribute it and/or modify it under |
---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later |
---|
| 13 | version. |
---|
| 14 | |
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
| 18 | |
---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with |
---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. |
---|
| 23 | |
---|
| 24 | You may alternatively use this source under the terms of a specific version of |
---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
| 26 | Torus Knot Software Ltd. |
---|
| 27 | ----------------------------------------------------------------------------- |
---|
| 28 | */ |
---|
| 29 | /*************************************************************************** |
---|
| 30 | OgreOctreeSceneQuery.cpp - description |
---|
| 31 | ------------------- |
---|
| 32 | begin : Tues July 20, 2004 |
---|
| 33 | copyright : (C) 2004by Jon Anderson |
---|
| 34 | email : janders@users.sf.net |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | ***************************************************************************/ |
---|
| 39 | |
---|
| 40 | #include <OgreOctreeSceneQuery.h> |
---|
| 41 | #include <OgreOctreeSceneManager.h> |
---|
| 42 | #include <OgreEntity.h> |
---|
| 43 | #include <OgreRoot.h> |
---|
| 44 | |
---|
| 45 | namespace Ogre |
---|
| 46 | { |
---|
| 47 | |
---|
| 48 | //--------------------------------------------------------------------- |
---|
| 49 | OctreeIntersectionSceneQuery::OctreeIntersectionSceneQuery(SceneManager* creator) |
---|
| 50 | : DefaultIntersectionSceneQuery(creator) |
---|
| 51 | { |
---|
| 52 | |
---|
| 53 | } |
---|
| 54 | //--------------------------------------------------------------------- |
---|
| 55 | OctreeIntersectionSceneQuery::~OctreeIntersectionSceneQuery() |
---|
| 56 | {} |
---|
| 57 | //--------------------------------------------------------------------- |
---|
| 58 | void OctreeIntersectionSceneQuery::execute(IntersectionSceneQueryListener* listener) |
---|
| 59 | { |
---|
| 60 | typedef std::pair<MovableObject *, MovableObject *> MovablePair; |
---|
| 61 | typedef std::set |
---|
| 62 | < std::pair<MovableObject *, MovableObject *> > MovableSet; |
---|
| 63 | |
---|
| 64 | MovableSet set; |
---|
| 65 | |
---|
| 66 | // Iterate over all movable types |
---|
| 67 | Root::MovableObjectFactoryIterator factIt = |
---|
| 68 | Root::getSingleton().getMovableObjectFactoryIterator(); |
---|
| 69 | while(factIt.hasMoreElements()) |
---|
| 70 | { |
---|
| 71 | SceneManager::MovableObjectIterator it = |
---|
| 72 | mParentSceneMgr->getMovableObjectIterator( |
---|
| 73 | factIt.getNext()->getType()); |
---|
| 74 | while( it.hasMoreElements() ) |
---|
| 75 | { |
---|
| 76 | |
---|
| 77 | MovableObject * e = it.getNext(); |
---|
| 78 | |
---|
| 79 | std::list < SceneNode * > list; |
---|
| 80 | //find the nodes that intersect the AAB |
---|
| 81 | static_cast<OctreeSceneManager*>( mParentSceneMgr ) -> findNodesIn( e->getWorldBoundingBox(), list, 0 ); |
---|
| 82 | //grab all moveables from the node that intersect... |
---|
| 83 | std::list < SceneNode * >::iterator nit = list.begin(); |
---|
| 84 | while( nit != list.end() ) |
---|
| 85 | { |
---|
| 86 | SceneNode::ObjectIterator oit = (*nit) -> getAttachedObjectIterator(); |
---|
| 87 | while( oit.hasMoreElements() ) |
---|
| 88 | { |
---|
| 89 | MovableObject * m = oit.getNext(); |
---|
| 90 | |
---|
| 91 | if( m != e && |
---|
| 92 | set.find( MovablePair(e,m)) == set.end() && |
---|
| 93 | set.find( MovablePair(m,e)) == set.end() && |
---|
| 94 | (m->getQueryFlags() & mQueryMask) && |
---|
| 95 | (m->getTypeFlags() & mQueryTypeMask) && |
---|
| 96 | m->isInScene() && |
---|
| 97 | e->getWorldBoundingBox().intersects( m->getWorldBoundingBox() ) ) |
---|
| 98 | { |
---|
| 99 | listener -> queryResult( e, m ); |
---|
| 100 | // deal with attached objects, since they are not directly attached to nodes |
---|
| 101 | if (m->getMovableType() == "Entity") |
---|
| 102 | { |
---|
| 103 | Entity* e2 = static_cast<Entity*>(m); |
---|
| 104 | Entity::ChildObjectListIterator childIt = e2->getAttachedObjectIterator(); |
---|
| 105 | while(childIt.hasMoreElements()) |
---|
| 106 | { |
---|
| 107 | MovableObject* c = childIt.getNext(); |
---|
| 108 | if (c->getQueryFlags() & mQueryMask && |
---|
| 109 | e->getWorldBoundingBox().intersects( c->getWorldBoundingBox() )) |
---|
| 110 | { |
---|
| 111 | listener->queryResult(e, c); |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | set.insert( MovablePair(e,m) ); |
---|
| 117 | |
---|
| 118 | } |
---|
| 119 | ++nit; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | /** Creates a custom Octree AAB query */ |
---|
| 126 | OctreeAxisAlignedBoxSceneQuery::OctreeAxisAlignedBoxSceneQuery(SceneManager* creator) |
---|
| 127 | : DefaultAxisAlignedBoxSceneQuery(creator) |
---|
| 128 | { |
---|
| 129 | } |
---|
| 130 | /** Deletes the custom Octree query */ |
---|
| 131 | OctreeAxisAlignedBoxSceneQuery::~OctreeAxisAlignedBoxSceneQuery() |
---|
| 132 | {} |
---|
| 133 | |
---|
| 134 | /** Finds any entities that intersect the AAB for the query. */ |
---|
| 135 | void OctreeAxisAlignedBoxSceneQuery::execute(SceneQueryListener* listener) |
---|
| 136 | { |
---|
| 137 | std::list < SceneNode * > list; |
---|
| 138 | //find the nodes that intersect the AAB |
---|
| 139 | static_cast<OctreeSceneManager*>( mParentSceneMgr ) -> findNodesIn( mAABB, list, 0 ); |
---|
| 140 | |
---|
| 141 | //grab all moveables from the node that intersect... |
---|
| 142 | std::list < SceneNode * >::iterator it = list.begin(); |
---|
| 143 | while( it != list.end() ) |
---|
| 144 | { |
---|
| 145 | SceneNode::ObjectIterator oit = (*it) -> getAttachedObjectIterator(); |
---|
| 146 | while( oit.hasMoreElements() ) |
---|
| 147 | { |
---|
| 148 | MovableObject * m = oit.getNext(); |
---|
| 149 | if( (m->getQueryFlags() & mQueryMask) && |
---|
| 150 | (m->getTypeFlags() & mQueryTypeMask) && |
---|
| 151 | m->isInScene() && |
---|
| 152 | mAABB.intersects( m->getWorldBoundingBox() ) ) |
---|
| 153 | { |
---|
| 154 | listener -> queryResult( m ); |
---|
| 155 | // deal with attached objects, since they are not directly attached to nodes |
---|
| 156 | if (m->getMovableType() == "Entity") |
---|
| 157 | { |
---|
| 158 | Entity* e = static_cast<Entity*>(m); |
---|
| 159 | Entity::ChildObjectListIterator childIt = e->getAttachedObjectIterator(); |
---|
| 160 | while(childIt.hasMoreElements()) |
---|
| 161 | { |
---|
| 162 | MovableObject* c = childIt.getNext(); |
---|
| 163 | if (c->getQueryFlags() & mQueryMask) |
---|
| 164 | { |
---|
| 165 | listener->queryResult(c); |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | ++it; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | } |
---|
| 177 | //--------------------------------------------------------------------- |
---|
| 178 | OctreeRaySceneQuery:: |
---|
| 179 | OctreeRaySceneQuery(SceneManager* creator) : DefaultRaySceneQuery(creator) |
---|
| 180 | { |
---|
| 181 | } |
---|
| 182 | //--------------------------------------------------------------------- |
---|
| 183 | OctreeRaySceneQuery::~OctreeRaySceneQuery() |
---|
| 184 | {} |
---|
| 185 | //--------------------------------------------------------------------- |
---|
| 186 | void OctreeRaySceneQuery::execute(RaySceneQueryListener* listener) |
---|
| 187 | { |
---|
| 188 | std::list < SceneNode * > list; |
---|
| 189 | //find the nodes that intersect the AAB |
---|
| 190 | static_cast<OctreeSceneManager*>( mParentSceneMgr ) -> findNodesIn( mRay, list, 0 ); |
---|
| 191 | |
---|
| 192 | //grab all moveables from the node that intersect... |
---|
| 193 | std::list < SceneNode * >::iterator it = list.begin(); |
---|
| 194 | while( it != list.end() ) |
---|
| 195 | { |
---|
| 196 | SceneNode::ObjectIterator oit = (*it) -> getAttachedObjectIterator(); |
---|
| 197 | while( oit.hasMoreElements() ) |
---|
| 198 | { |
---|
| 199 | MovableObject * m = oit.getNext(); |
---|
| 200 | if( (m->getQueryFlags() & mQueryMask) && |
---|
| 201 | (m->getTypeFlags() & mQueryTypeMask) && m->isInScene() ) |
---|
| 202 | { |
---|
| 203 | std::pair<bool, Real> result = mRay.intersects(m->getWorldBoundingBox()); |
---|
| 204 | |
---|
| 205 | if( result.first ) |
---|
| 206 | { |
---|
| 207 | listener -> queryResult( m, result.second ); |
---|
| 208 | // deal with attached objects, since they are not directly attached to nodes |
---|
| 209 | if (m->getMovableType() == "Entity") |
---|
| 210 | { |
---|
| 211 | Entity* e = static_cast<Entity*>(m); |
---|
| 212 | Entity::ChildObjectListIterator childIt = e->getAttachedObjectIterator(); |
---|
| 213 | while(childIt.hasMoreElements()) |
---|
| 214 | { |
---|
| 215 | MovableObject* c = childIt.getNext(); |
---|
| 216 | if (c->getQueryFlags() & mQueryMask) |
---|
| 217 | { |
---|
| 218 | result = mRay.intersects(c->getWorldBoundingBox()); |
---|
| 219 | if (result.first) |
---|
| 220 | { |
---|
| 221 | listener->queryResult(c, result.second); |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | ++it; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | |
---|
| 236 | //--------------------------------------------------------------------- |
---|
| 237 | OctreeSphereSceneQuery:: |
---|
| 238 | OctreeSphereSceneQuery(SceneManager* creator) : DefaultSphereSceneQuery(creator) |
---|
| 239 | { |
---|
| 240 | } |
---|
| 241 | //--------------------------------------------------------------------- |
---|
| 242 | OctreeSphereSceneQuery::~OctreeSphereSceneQuery() |
---|
| 243 | {} |
---|
| 244 | //--------------------------------------------------------------------- |
---|
| 245 | void OctreeSphereSceneQuery::execute(SceneQueryListener* listener) |
---|
| 246 | { |
---|
| 247 | std::list < SceneNode * > list; |
---|
| 248 | //find the nodes that intersect the AAB |
---|
| 249 | static_cast<OctreeSceneManager*>( mParentSceneMgr ) -> findNodesIn( mSphere, list, 0 ); |
---|
| 250 | |
---|
| 251 | //grab all moveables from the node that intersect... |
---|
| 252 | std::list < SceneNode * >::iterator it = list.begin(); |
---|
| 253 | while( it != list.end() ) |
---|
| 254 | { |
---|
| 255 | SceneNode::ObjectIterator oit = (*it) -> getAttachedObjectIterator(); |
---|
| 256 | while( oit.hasMoreElements() ) |
---|
| 257 | { |
---|
| 258 | MovableObject * m = oit.getNext(); |
---|
| 259 | if( (m->getQueryFlags() & mQueryMask) && |
---|
| 260 | (m->getTypeFlags() & mQueryTypeMask) && |
---|
| 261 | m->isInScene() && |
---|
| 262 | mSphere.intersects( m->getWorldBoundingBox() ) ) |
---|
| 263 | { |
---|
| 264 | listener -> queryResult( m ); |
---|
| 265 | // deal with attached objects, since they are not directly attached to nodes |
---|
| 266 | if (m->getMovableType() == "Entity") |
---|
| 267 | { |
---|
| 268 | Entity* e = static_cast<Entity*>(m); |
---|
| 269 | Entity::ChildObjectListIterator childIt = e->getAttachedObjectIterator(); |
---|
| 270 | while(childIt.hasMoreElements()) |
---|
| 271 | { |
---|
| 272 | MovableObject* c = childIt.getNext(); |
---|
| 273 | if (c->getQueryFlags() & mQueryMask && |
---|
| 274 | mSphere.intersects( c->getWorldBoundingBox())) |
---|
| 275 | { |
---|
| 276 | listener->queryResult(c); |
---|
| 277 | } |
---|
| 278 | } |
---|
| 279 | } |
---|
| 280 | } |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | ++it; |
---|
| 284 | } |
---|
| 285 | } |
---|
| 286 | //--------------------------------------------------------------------- |
---|
| 287 | OctreePlaneBoundedVolumeListSceneQuery:: |
---|
| 288 | OctreePlaneBoundedVolumeListSceneQuery(SceneManager* creator) |
---|
| 289 | : DefaultPlaneBoundedVolumeListSceneQuery(creator) |
---|
| 290 | { |
---|
| 291 | |
---|
| 292 | } |
---|
| 293 | //--------------------------------------------------------------------- |
---|
| 294 | OctreePlaneBoundedVolumeListSceneQuery::~OctreePlaneBoundedVolumeListSceneQuery() |
---|
| 295 | {} |
---|
| 296 | //--------------------------------------------------------------------- |
---|
| 297 | void OctreePlaneBoundedVolumeListSceneQuery::execute(SceneQueryListener* listener) |
---|
| 298 | { |
---|
| 299 | std::set<SceneNode*> checkedSceneNodes; |
---|
| 300 | |
---|
| 301 | PlaneBoundedVolumeList::iterator pi, piend; |
---|
| 302 | piend = mVolumes.end(); |
---|
| 303 | for (pi = mVolumes.begin(); pi != piend; ++pi) |
---|
| 304 | { |
---|
| 305 | std::list < SceneNode * > list; |
---|
| 306 | //find the nodes that intersect the AAB |
---|
| 307 | static_cast<OctreeSceneManager*>( mParentSceneMgr ) -> findNodesIn( *pi, list, 0 ); |
---|
| 308 | |
---|
| 309 | //grab all moveables from the node that intersect... |
---|
| 310 | std::list < SceneNode * >::iterator it, itend; |
---|
| 311 | itend = list.end(); |
---|
| 312 | for (it = list.begin(); it != itend; ++it) |
---|
| 313 | { |
---|
| 314 | // avoid double-check same scene node |
---|
| 315 | if (!checkedSceneNodes.insert(*it).second) |
---|
| 316 | continue; |
---|
| 317 | SceneNode::ObjectIterator oit = (*it) -> getAttachedObjectIterator(); |
---|
| 318 | while( oit.hasMoreElements() ) |
---|
| 319 | { |
---|
| 320 | MovableObject * m = oit.getNext(); |
---|
| 321 | if( (m->getQueryFlags() & mQueryMask) && |
---|
| 322 | (m->getTypeFlags() & mQueryTypeMask) && |
---|
| 323 | m->isInScene() && |
---|
| 324 | (*pi).intersects( m->getWorldBoundingBox() ) ) |
---|
| 325 | { |
---|
| 326 | listener -> queryResult( m ); |
---|
| 327 | // deal with attached objects, since they are not directly attached to nodes |
---|
| 328 | if (m->getMovableType() == "Entity") |
---|
| 329 | { |
---|
| 330 | Entity* e = static_cast<Entity*>(m); |
---|
| 331 | Entity::ChildObjectListIterator childIt = e->getAttachedObjectIterator(); |
---|
| 332 | while(childIt.hasMoreElements()) |
---|
| 333 | { |
---|
| 334 | MovableObject* c = childIt.getNext(); |
---|
| 335 | if (c->getQueryFlags() & mQueryMask && |
---|
| 336 | (*pi).intersects( c->getWorldBoundingBox())) |
---|
| 337 | { |
---|
| 338 | listener->queryResult(c); |
---|
| 339 | } |
---|
| 340 | } |
---|
| 341 | } |
---|
| 342 | } |
---|
| 343 | } |
---|
| 344 | } |
---|
| 345 | }//for |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | |
---|
| 349 | } |
---|