sceneContainer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #ifndef _SCENECONTAINER_H_
  27. #define _SCENECONTAINER_H_
  28. #ifndef _MBOX_H_
  29. #include "math/mBox.h"
  30. #endif
  31. #ifndef _MSPHERE_H_
  32. #include "math/mSphere.h"
  33. #endif
  34. #ifndef _TVECTOR_H_
  35. #include "core/util/tVector.h"
  36. #endif
  37. #ifndef _MPOLYHEDRON_H_
  38. #include "math/mPolyhedron.h"
  39. #endif
  40. #ifndef _SIMOBJECT_H_
  41. #include "console/simObject.h"
  42. #endif
  43. /// @file
  44. /// SceneObject database.
  45. class SceneObject;
  46. class AbstractPolyList;
  47. class OptimizedPolyList;
  48. class Frustum;
  49. class Point3F;
  50. struct RayInfo;
  51. template< typename T >
  52. class SceneObjectRefBase
  53. {
  54. public:
  55. /// Object that is referenced in the link.
  56. SceneObject* object;
  57. /// Next link in chain of container.
  58. T* nextInBin;
  59. /// Previous link in chain of container.
  60. T* prevInBin;
  61. /// Next link in chain that is associated with #object.
  62. T* nextInObj;
  63. };
  64. /// Reference to a scene object.
  65. class SceneObjectRef : public SceneObjectRefBase< SceneObjectRef > {};
  66. /// A contextual hint passed to the polylist methods which
  67. /// allows it to return the appropriate geometry.
  68. enum PolyListContext
  69. {
  70. /// A hint that the polyist is intended
  71. /// for collision testing.
  72. PLC_Collision,
  73. /// A hint that the polyist is for decal
  74. /// geometry generation.
  75. PLC_Decal,
  76. /// A hint that the polyist is used for
  77. /// selection from an editor or other tool.
  78. PLC_Selection,
  79. /// A hint that the polylist is used for
  80. /// building a representation of the environment
  81. /// used for navigation.
  82. PLC_Navigation,
  83. /// A hint that the polyist will be used
  84. /// to export geometry and would like to have
  85. /// texture coords and materials.
  86. PLC_Export
  87. };
  88. /// For simple queries. Simply creates a vector of the objects
  89. class SimpleQueryList
  90. {
  91. public:
  92. Vector< SceneObject* > mList;
  93. SimpleQueryList()
  94. {
  95. VECTOR_SET_ASSOCIATION( mList );
  96. }
  97. void insertObject( SceneObject* obj ) { mList.push_back(obj); }
  98. static void insertionCallback( SceneObject* obj, void* key )
  99. {
  100. SimpleQueryList* pList = reinterpret_cast< SimpleQueryList* >( key );
  101. pList->insertObject( obj );
  102. }
  103. };
  104. //----------------------------------------------------------------------------
  105. /// Database for SceneObjects.
  106. ///
  107. /// ScenceContainer implements a grid-based spatial subdivision for the contents of a scene.
  108. class SceneContainer
  109. {
  110. enum CastRayType
  111. {
  112. CollisionGeometry,
  113. RenderedGeometry,
  114. };
  115. public:
  116. struct Link
  117. {
  118. Link* mNext;
  119. Link* mPrev;
  120. Link();
  121. void unlink();
  122. void linkAfter(Link* ptr);
  123. };
  124. struct CallbackInfo
  125. {
  126. PolyListContext context;
  127. AbstractPolyList* polyList;
  128. Box3F boundingBox;
  129. SphereF boundingSphere;
  130. void *key;
  131. };
  132. private:
  133. Link mStart;
  134. Link mEnd;
  135. /// Container queries based on #mCurrSeqKey are are not re-entrant;
  136. /// this is used to detect when it happens.
  137. bool mSearchInProgress;
  138. /// Current sequence key.
  139. U32 mCurrSeqKey;
  140. SceneObjectRef* mFreeRefPool;
  141. Vector< SceneObjectRef* > mRefPoolBlocks;
  142. SceneObjectRef* mBinArray;
  143. SceneObjectRef mOverflowBin;
  144. /// A vector that contains just the water and physical zone
  145. /// object types which is used to optimize searches.
  146. Vector< SceneObject* > mWaterAndZones;
  147. /// Vector that contains just the terrain objects in the container.
  148. Vector< SceneObject* > mTerrains;
  149. static const U32 csmNumBins;
  150. static const F32 csmBinSize;
  151. static const F32 csmTotalBinSize;
  152. static const U32 csmRefPoolBlockSize;
  153. public:
  154. SceneContainer();
  155. ~SceneContainer();
  156. /// Return a vector containing all the water and physical zone objects in this container.
  157. const Vector< SceneObject* >& getWaterAndPhysicalZones() const { return mWaterAndZones; }
  158. /// Return a vector containing all terrain objects in this container.
  159. const Vector< SceneObject* >& getTerrains() const { return mTerrains; }
  160. /// @name Basic database operations
  161. /// @{
  162. ///
  163. typedef void ( *FindCallback )( SceneObject* object, void* key );
  164. /// Find all objects of the given type(s) and invoke the given callback for each
  165. /// of them.
  166. /// @param mask Object type mask (@see SimObjectTypes).
  167. /// @param callback Pointer to function to invoke for each object.
  168. /// @param key User data to pass to the "key" argument of @a callback.
  169. void findObjects( U32 mask, FindCallback callback, void* key = NULL );
  170. void findObjects( const Box3F& box, U32 mask, FindCallback, void *key = NULL );
  171. void findObjects( const Frustum& frustum, U32 mask, FindCallback, void *key = NULL );
  172. void polyhedronFindObjects( const Polyhedron& polyhedron, U32 mask, FindCallback, void *key = NULL );
  173. /// Find all objects of the given type(s) and add them to the given vector.
  174. /// @param mask Object type mask (@see SimObjectTypes).
  175. /// @param outFound Vector to add found objects to.
  176. void findObjectList( U32 mask, Vector< SceneObject* >* outFound );
  177. ///
  178. void findObjectList( const Box3F& box, U32 mask, Vector< SceneObject* >* outFound );
  179. ///
  180. void findObjectList( const Frustum& frustum, U32 mask, Vector< SceneObject* >* outFound );
  181. /// @}
  182. /// @name Line intersection
  183. /// @{
  184. typedef bool ( *CastRayCallback )( RayInfo* ri );
  185. /// Test against collision geometry -- fast.
  186. bool castRay( const Point3F &start, const Point3F &end, U32 mask, RayInfo* info, CastRayCallback callback = NULL );
  187. /// Test against rendered geometry -- slow.
  188. bool castRayRendered( const Point3F &start, const Point3F &end, U32 mask, RayInfo* info, CastRayCallback callback = NULL );
  189. bool collideBox(const Point3F &start, const Point3F &end, U32 mask, RayInfo* info);
  190. /// @}
  191. /// @name Poly list
  192. /// @{
  193. ///
  194. bool buildPolyList( PolyListContext context,
  195. const Box3F &box,
  196. U32 typeMask,
  197. AbstractPolyList *polylist );
  198. /// @}
  199. /// Add an object to the database.
  200. /// @param object A SceneObject.
  201. bool addObject( SceneObject* object );
  202. /// Remove an object from the database.
  203. /// @param object A SceneObject.
  204. bool removeObject( SceneObject* object );
  205. void addRefPoolBlock();
  206. SceneObjectRef* allocateObjectRef();
  207. void freeObjectRef(SceneObjectRef*);
  208. void insertIntoBins( SceneObject* object );
  209. void removeFromBins( SceneObject* object );
  210. /// Make sure that we're not just sticking the object right back
  211. /// where it came from. The overloaded insertInto is so we don't calculate
  212. /// the ranges twice.
  213. void checkBins( SceneObject* object );
  214. void insertIntoBins(SceneObject*, U32, U32, U32, U32);
  215. void initRadiusSearch(const Point3F& searchPoint,
  216. const F32 searchRadius,
  217. const U32 searchMask);
  218. void initTypeSearch(const U32 searchMask);
  219. SceneObject* containerSearchNextObject();
  220. U32 containerSearchNext();
  221. F32 containerSearchCurrDist();
  222. F32 containerSearchCurrRadiusDist();
  223. private:
  224. Vector<SimObjectPtr<SceneObject>*> mSearchList;///< Object searches to support console querying of the database. ONLY WORKS ON SERVER
  225. S32 mCurrSearchPos;
  226. Point3F mSearchReferencePoint;
  227. void cleanupSearchVectors();
  228. /// Base cast ray code
  229. bool _castRay( U32 type, const Point3F &start, const Point3F &end, U32 mask, RayInfo* info, CastRayCallback callback );
  230. void _findSpecialObjects( const Vector< SceneObject* >& vector, U32 mask, FindCallback, void *key = NULL );
  231. void _findSpecialObjects( const Vector< SceneObject* >& vector, const Box3F &box, U32 mask, FindCallback callback, void *key = NULL );
  232. static void getBinRange( const F32 min, const F32 max, U32& minBin, U32& maxBin );
  233. public:
  234. Vector<SimObjectPtr<SceneObject>*>& getRadiusSearchList() { return mSearchList; }
  235. };
  236. //-----------------------------------------------------------------------------
  237. extern SceneContainer gServerContainer;
  238. extern SceneContainer gClientContainer;
  239. //-----------------------------------------------------------------------------
  240. inline void SceneContainer::freeObjectRef(SceneObjectRef* trash)
  241. {
  242. trash->object = NULL;
  243. trash->nextInBin = NULL;
  244. trash->prevInBin = NULL;
  245. trash->nextInObj = mFreeRefPool;
  246. mFreeRefPool = trash;
  247. }
  248. //-----------------------------------------------------------------------------
  249. inline SceneObjectRef* SceneContainer::allocateObjectRef()
  250. {
  251. if( mFreeRefPool == NULL )
  252. addRefPoolBlock();
  253. AssertFatal( mFreeRefPool!=NULL, "Error, should always have a free reference here!" );
  254. SceneObjectRef* ret = mFreeRefPool;
  255. mFreeRefPool = mFreeRefPool->nextInObj;
  256. ret->nextInObj = NULL;
  257. return ret;
  258. }
  259. #endif // !_SCENECONTAINER_H_