sceneContainer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 polyist will be used
  76. /// to export geometry and would like to have
  77. /// texture coords and materials.
  78. PLC_Export
  79. };
  80. /// For simple queries. Simply creates a vector of the objects
  81. class SimpleQueryList
  82. {
  83. public:
  84. Vector< SceneObject* > mList;
  85. SimpleQueryList()
  86. {
  87. VECTOR_SET_ASSOCIATION( mList );
  88. }
  89. void insertObject( SceneObject* obj ) { mList.push_back(obj); }
  90. static void insertionCallback( SceneObject* obj, void* key )
  91. {
  92. SimpleQueryList* pList = reinterpret_cast< SimpleQueryList* >( key );
  93. pList->insertObject( obj );
  94. }
  95. };
  96. //----------------------------------------------------------------------------
  97. /// Database for SceneObjects.
  98. ///
  99. /// ScenceContainer implements a grid-based spatial subdivision for the contents of a scene.
  100. class SceneContainer
  101. {
  102. enum CastRayType
  103. {
  104. CollisionGeometry,
  105. RenderedGeometry,
  106. };
  107. public:
  108. struct Link
  109. {
  110. Link* next;
  111. Link* prev;
  112. Link();
  113. void unlink();
  114. void linkAfter(Link* ptr);
  115. };
  116. struct CallbackInfo
  117. {
  118. PolyListContext context;
  119. AbstractPolyList* polyList;
  120. Box3F boundingBox;
  121. SphereF boundingSphere;
  122. void *key;
  123. };
  124. private:
  125. Link mStart;
  126. Link mEnd;
  127. /// Container queries based on #mCurrSeqKey are are not re-entrant;
  128. /// this is used to detect when it happens.
  129. bool mSearchInProgress;
  130. /// Current sequence key.
  131. U32 mCurrSeqKey;
  132. SceneObjectRef* mFreeRefPool;
  133. Vector< SceneObjectRef* > mRefPoolBlocks;
  134. SceneObjectRef* mBinArray;
  135. SceneObjectRef mOverflowBin;
  136. /// A vector that contains just the water and physical zone
  137. /// object types which is used to optimize searches.
  138. Vector< SceneObject* > mWaterAndZones;
  139. /// Vector that contains just the terrain objects in the container.
  140. Vector< SceneObject* > mTerrains;
  141. static const U32 csmNumBins;
  142. static const F32 csmBinSize;
  143. static const F32 csmTotalBinSize;
  144. static const U32 csmRefPoolBlockSize;
  145. public:
  146. SceneContainer();
  147. ~SceneContainer();
  148. /// Return a vector containing all the water and physical zone objects in this container.
  149. const Vector< SceneObject* >& getWaterAndPhysicalZones() const { return mWaterAndZones; }
  150. /// Return a vector containing all terrain objects in this container.
  151. const Vector< SceneObject* >& getTerrains() const { return mTerrains; }
  152. /// @name Basic database operations
  153. /// @{
  154. ///
  155. typedef void ( *FindCallback )( SceneObject* object, void* key );
  156. /// Find all objects of the given type(s) and invoke the given callback for each
  157. /// of them.
  158. /// @param mask Object type mask (@see SimObjectTypes).
  159. /// @param callback Pointer to function to invoke for each object.
  160. /// @param key User data to pass to the "key" argument of @a callback.
  161. void findObjects( U32 mask, FindCallback callback, void* key = NULL );
  162. void findObjects( const Box3F& box, U32 mask, FindCallback, void *key = NULL );
  163. void findObjects( const Frustum& frustum, U32 mask, FindCallback, void *key = NULL );
  164. void polyhedronFindObjects( const Polyhedron& polyhedron, U32 mask, FindCallback, void *key = NULL );
  165. /// Find all objects of the given type(s) and add them to the given vector.
  166. /// @param mask Object type mask (@see SimObjectTypes).
  167. /// @param outFound Vector to add found objects to.
  168. void findObjectList( U32 mask, Vector< SceneObject* >* outFound );
  169. ///
  170. void findObjectList( const Box3F& box, U32 mask, Vector< SceneObject* >* outFound );
  171. ///
  172. void findObjectList( const Frustum& frustum, U32 mask, Vector< SceneObject* >* outFound );
  173. /// @}
  174. /// @name Line intersection
  175. /// @{
  176. typedef bool ( *CastRayCallback )( RayInfo* ri );
  177. /// Test against collision geometry -- fast.
  178. bool castRay( const Point3F &start, const Point3F &end, U32 mask, RayInfo* info, CastRayCallback callback = NULL );
  179. /// Test against rendered geometry -- slow.
  180. bool castRayRendered( const Point3F &start, const Point3F &end, U32 mask, RayInfo* info, CastRayCallback callback = NULL );
  181. bool collideBox(const Point3F &start, const Point3F &end, U32 mask, RayInfo* info);
  182. /// @}
  183. /// @name Poly list
  184. /// @{
  185. ///
  186. bool buildPolyList( PolyListContext context,
  187. const Box3F &box,
  188. U32 typeMask,
  189. AbstractPolyList *polylist );
  190. /// @}
  191. /// Add an object to the database.
  192. /// @param object A SceneObject.
  193. bool addObject( SceneObject* object );
  194. /// Remove an object from the database.
  195. /// @param object A SceneObject.
  196. bool removeObject( SceneObject* object );
  197. void addRefPoolBlock();
  198. SceneObjectRef* allocateObjectRef();
  199. void freeObjectRef(SceneObjectRef*);
  200. void insertIntoBins( SceneObject* object );
  201. void removeFromBins( SceneObject* object );
  202. /// Make sure that we're not just sticking the object right back
  203. /// where it came from. The overloaded insertInto is so we don't calculate
  204. /// the ranges twice.
  205. void checkBins( SceneObject* object );
  206. void insertIntoBins(SceneObject*, U32, U32, U32, U32);
  207. void initRadiusSearch(const Point3F& searchPoint,
  208. const F32 searchRadius,
  209. const U32 searchMask);
  210. void initTypeSearch(const U32 searchMask);
  211. SceneObject* containerSearchNextObject();
  212. U32 containerSearchNext();
  213. F32 containerSearchCurrDist();
  214. F32 containerSearchCurrRadiusDist();
  215. private:
  216. Vector<SimObjectPtr<SceneObject>*> mSearchList;///< Object searches to support console querying of the database. ONLY WORKS ON SERVER
  217. S32 mCurrSearchPos;
  218. Point3F mSearchReferencePoint;
  219. void cleanupSearchVectors();
  220. /// Base cast ray code
  221. bool _castRay( U32 type, const Point3F &start, const Point3F &end, U32 mask, RayInfo* info, CastRayCallback callback );
  222. void _findSpecialObjects( const Vector< SceneObject* >& vector, U32 mask, FindCallback, void *key = NULL );
  223. void _findSpecialObjects( const Vector< SceneObject* >& vector, const Box3F &box, U32 mask, FindCallback callback, void *key = NULL );
  224. static void getBinRange( const F32 min, const F32 max, U32& minBin, U32& maxBin );
  225. };
  226. //-----------------------------------------------------------------------------
  227. extern SceneContainer gServerContainer;
  228. extern SceneContainer gClientContainer;
  229. //-----------------------------------------------------------------------------
  230. inline void SceneContainer::freeObjectRef(SceneObjectRef* trash)
  231. {
  232. trash->object = NULL;
  233. trash->nextInBin = NULL;
  234. trash->prevInBin = NULL;
  235. trash->nextInObj = mFreeRefPool;
  236. mFreeRefPool = trash;
  237. }
  238. //-----------------------------------------------------------------------------
  239. inline SceneObjectRef* SceneContainer::allocateObjectRef()
  240. {
  241. if( mFreeRefPool == NULL )
  242. addRefPoolBlock();
  243. AssertFatal( mFreeRefPool!=NULL, "Error, should always have a free reference here!" );
  244. SceneObjectRef* ret = mFreeRefPool;
  245. mFreeRefPool = mFreeRefPool->nextInObj;
  246. ret->nextInObj = NULL;
  247. return ret;
  248. }
  249. #endif // !_SCENECONTAINER_H_