sceneContainer.h 10 KB

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