sceneContainer.h
Engine/source/scene/sceneContainer.h
SceneObject database.
Classes:
Database for SceneObjects.
Reference to a scene object.
For simple queries. Simply creates a vector of the objects.
Public Enumerations
PolyListContext { PLC_Collision PLC_Decal PLC_Selection PLC_Navigation PLC_Export }
A contextual hint passed to the polylist methods which allows it to return the appropriate geometry.
Public Variables
Detailed Description
SceneObject database.
Public Enumerations
PolyListContext
Enumerator
- PLC_Collision
A hint that the polyist is intended for collision testing.
- PLC_Decal
A hint that the polyist is for decal geometry generation.
- PLC_Selection
A hint that the polyist is used for selection from an editor or other tool.
- PLC_Navigation
A hint that the polylist is used for building a representation of the environment used for navigation.
- PLC_Export
A hint that the polyist will be used to export geometry and would like to have texture coords and materials.
A contextual hint passed to the polylist methods which allows it to return the appropriate geometry.
Public Variables
SceneContainer gClientContainer
SceneContainer gServerContainer
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#ifndef _SCENECONTAINER_H_ 25#define _SCENECONTAINER_H_ 26 27#ifndef _MBOX_H_ 28#include "math/mBox.h" 29#endif 30 31#ifndef _MSPHERE_H_ 32#include "math/mSphere.h" 33#endif 34 35#ifndef _TVECTOR_H_ 36#include "core/util/tVector.h" 37#endif 38 39#ifndef _MPOLYHEDRON_H_ 40#include "math/mPolyhedron.h" 41#endif 42 43#ifndef _SIMOBJECT_H_ 44#include "console/simObject.h" 45#endif 46 47 48/// @file 49/// SceneObject database. 50 51 52class SceneObject; 53class AbstractPolyList; 54class OptimizedPolyList; 55class Frustum; 56class Point3F; 57 58struct RayInfo; 59 60 61template< typename T > 62class SceneObjectRefBase 63{ 64 public: 65 66 /// Object that is referenced in the link. 67 SceneObject* object; 68 69 /// Next link in chain of container. 70 T* nextInBin; 71 72 /// Previous link in chain of container. 73 T* prevInBin; 74 75 /// Next link in chain that is associated with #object. 76 T* nextInObj; 77}; 78 79 80/// Reference to a scene object. 81class SceneObjectRef : public SceneObjectRefBase< SceneObjectRef > {}; 82 83 84/// A contextual hint passed to the polylist methods which 85/// allows it to return the appropriate geometry. 86enum PolyListContext 87{ 88 /// A hint that the polyist is intended 89 /// for collision testing. 90 PLC_Collision, 91 92 /// A hint that the polyist is for decal 93 /// geometry generation. 94 PLC_Decal, 95 96 /// A hint that the polyist is used for 97 /// selection from an editor or other tool. 98 PLC_Selection, 99 100 /// A hint that the polylist is used for 101 /// building a representation of the environment 102 /// used for navigation. 103 PLC_Navigation, 104 105 /// A hint that the polyist will be used 106 /// to export geometry and would like to have 107 /// texture coords and materials. 108 PLC_Export 109}; 110 111 112/// For simple queries. Simply creates a vector of the objects 113class SimpleQueryList 114{ 115 public: 116 117 Vector< SceneObject*> mList; 118 119 SimpleQueryList() 120 { 121 VECTOR_SET_ASSOCIATION( mList ); 122 } 123 124 void insertObject( SceneObject* obj ) { mList.push_back(obj); } 125 static void insertionCallback( SceneObject* obj, void* key ) 126 { 127 SimpleQueryList* pList = reinterpret_cast< SimpleQueryList* >( key ); 128 pList->insertObject( obj ); 129 } 130}; 131 132 133//---------------------------------------------------------------------------- 134 135/// Database for SceneObjects. 136/// 137/// ScenceContainer implements a grid-based spatial subdivision for the contents of a scene. 138class SceneContainer 139{ 140 enum CastRayType 141 { 142 CollisionGeometry, 143 RenderedGeometry, 144 }; 145 146 public: 147 148 struct Link 149 { 150 Link* next; 151 Link* prev; 152 Link(); 153 void unlink(); 154 void linkAfter(Link* ptr); 155 }; 156 157 struct CallbackInfo 158 { 159 PolyListContext context; 160 AbstractPolyList* polyList; 161 Box3F boundingBox; 162 SphereF boundingSphere; 163 void *key; 164 }; 165 166 private: 167 168 Link mStart; 169 Link mEnd; 170 171 /// Container queries based on #mCurrSeqKey are are not re-entrant; 172 /// this is used to detect when it happens. 173 bool mSearchInProgress; 174 175 /// Current sequence key. 176 U32 mCurrSeqKey; 177 178 SceneObjectRef* mFreeRefPool; 179 Vector< SceneObjectRef*> mRefPoolBlocks; 180 181 SceneObjectRef* mBinArray; 182 SceneObjectRef mOverflowBin; 183 184 /// A vector that contains just the water and physical zone 185 /// object types which is used to optimize searches. 186 Vector< SceneObject*> mWaterAndZones; 187 188 /// Vector that contains just the terrain objects in the container. 189 Vector< SceneObject*> mTerrains; 190 191 static const U32 csmNumBins; 192 static const F32 csmBinSize; 193 static const F32 csmTotalBinSize; 194 static const U32 csmRefPoolBlockSize; 195 196 public: 197 198 SceneContainer(); 199 ~SceneContainer(); 200 201 /// Return a vector containing all the water and physical zone objects in this container. 202 const Vector< SceneObject*>& getWaterAndPhysicalZones() const { return mWaterAndZones; } 203 204 /// Return a vector containing all terrain objects in this container. 205 const Vector< SceneObject*>& getTerrains() const { return mTerrains; } 206 207 /// @name Basic database operations 208 /// @{ 209 210 /// 211 typedef void ( *FindCallback )( SceneObject* object, void* key ); 212 213 /// Find all objects of the given type(s) and invoke the given callback for each 214 /// of them. 215 /// @param mask Object type mask (@see SimObjectTypes). 216 /// @param callback Pointer to function to invoke for each object. 217 /// @param key User data to pass to the "key" argument of @a callback. 218 void findObjects( U32 mask, FindCallback callback, void* key = NULL ); 219 220 void findObjects( const Box3F& box, U32 mask, FindCallback, void *key = NULL ); 221 void findObjects( const Frustum& frustum, U32 mask, FindCallback, void *key = NULL ); 222 223 void polyhedronFindObjects( const Polyhedron& polyhedron, U32 mask, FindCallback, void *key = NULL ); 224 225 /// Find all objects of the given type(s) and add them to the given vector. 226 /// @param mask Object type mask (@see SimObjectTypes). 227 /// @param outFound Vector to add found objects to. 228 void findObjectList( U32 mask, Vector< SceneObject*>* outFound ); 229 230 /// 231 void findObjectList( const Box3F& box, U32 mask, Vector< SceneObject*>* outFound ); 232 233 /// 234 void findObjectList( const Frustum& frustum, U32 mask, Vector< SceneObject*>* outFound ); 235 236 /// @} 237 238 /// @name Line intersection 239 /// @{ 240 241 typedef bool ( *CastRayCallback )( RayInfo* ri ); 242 243 /// Test against collision geometry -- fast. 244 bool castRay( const Point3F &start, const Point3F &end, U32 mask, RayInfo* info, CastRayCallback callback = NULL ); 245 246 /// Test against rendered geometry -- slow. 247 bool castRayRendered( const Point3F &start, const Point3F &end, U32 mask, RayInfo* info, CastRayCallback callback = NULL ); 248 249 bool collideBox(const Point3F &start, const Point3F &end, U32 mask, RayInfo* info); 250 251 /// @} 252 253 /// @name Poly list 254 /// @{ 255 256 /// 257 bool buildPolyList( PolyListContext context, 258 const Box3F &box, 259 U32 typeMask, 260 AbstractPolyList *polylist ); 261 262 /// @} 263 264 /// Add an object to the database. 265 /// @param object A SceneObject. 266 bool addObject( SceneObject* object ); 267 268 /// Remove an object from the database. 269 /// @param object A SceneObject. 270 bool removeObject( SceneObject* object ); 271 272 void addRefPoolBlock(); 273 SceneObjectRef* allocateObjectRef(); 274 void freeObjectRef(SceneObjectRef*); 275 void insertIntoBins( SceneObject* object ); 276 void removeFromBins( SceneObject* object ); 277 278 /// Make sure that we're not just sticking the object right back 279 /// where it came from. The overloaded insertInto is so we don't calculate 280 /// the ranges twice. 281 void checkBins( SceneObject* object ); 282 void insertIntoBins(SceneObject*, U32, U32, U32, U32); 283 284 void initRadiusSearch(const Point3F& searchPoint, 285 const F32 searchRadius, 286 const U32 searchMask); 287 void initTypeSearch(const U32 searchMask); 288 SceneObject* containerSearchNextObject(); 289 U32 containerSearchNext(); 290 F32 containerSearchCurrDist(); 291 F32 containerSearchCurrRadiusDist(); 292 293 private: 294 295 Vector<SimObjectPtr<SceneObject>*> mSearchList;///< Object searches to support console querying of the database. ONLY WORKS ON SERVER 296 S32 mCurrSearchPos; 297 Point3F mSearchReferencePoint; 298 299 void cleanupSearchVectors(); 300 301 /// Base cast ray code 302 bool _castRay( U32 type, const Point3F &start, const Point3F &end, U32 mask, RayInfo* info, CastRayCallback callback ); 303 304 void _findSpecialObjects( const Vector< SceneObject*>& vector, U32 mask, FindCallback, void *key = NULL ); 305 void _findSpecialObjects( const Vector< SceneObject*>& vector, const Box3F &box, U32 mask, FindCallback callback, void *key = NULL ); 306 307 static void getBinRange( const F32 min, const F32 max, U32& minBin, U32& maxBin ); 308}; 309 310//----------------------------------------------------------------------------- 311 312extern SceneContainer gServerContainer; 313extern SceneContainer gClientContainer; 314 315//----------------------------------------------------------------------------- 316 317inline void SceneContainer::freeObjectRef(SceneObjectRef* trash) 318{ 319 trash->object = NULL; 320 trash->nextInBin = NULL; 321 trash->prevInBin = NULL; 322 trash->nextInObj = mFreeRefPool; 323 mFreeRefPool = trash; 324} 325 326//----------------------------------------------------------------------------- 327 328inline SceneObjectRef* SceneContainer::allocateObjectRef() 329{ 330 if( mFreeRefPool == NULL ) 331 addRefPoolBlock(); 332 AssertFatal( mFreeRefPool!=<a href="/coding/file/types_8lint_8h/#types_8lint_8h_1a070d2ce7b6bb7e5c05602aa8c308d0c4">NULL</a>, "Error, should always have a free reference here!" ); 333 334 SceneObjectRef* ret = mFreeRefPool; 335 mFreeRefPool = mFreeRefPool->nextInObj; 336 337 ret->nextInObj = NULL; 338 return ret; 339} 340 341#endif // !_SCENECONTAINER_H_ 342
