sceneZoneSpace.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 _SCENEZONESPACE_H_
  23. #define _SCENEZONESPACE_H_
  24. #ifndef _SCENESPACE_H_
  25. #include "scene/sceneSpace.h"
  26. #endif
  27. #ifndef _TVECTOR_H_
  28. #include "core/util/tVector.h"
  29. #endif
  30. class SceneZoneSpaceManager;
  31. class SceneCullingState;
  32. /// Abstract base class for an object that manages zones in a scene.
  33. ///
  34. /// This class adds the ability to SceneSpace to define and manage zones within the object's
  35. /// space. Zones are used to determine visibility in a scene.
  36. ///
  37. /// Each zone space manages one or more zones in a scene. All the zones must be within the
  38. /// AABB of the zone space but within that AABB, the zone space is free to distribute and
  39. /// manage zones in arbitrary fashion.
  40. ///
  41. /// For scene traversal, zone spaces are interconnected. By default, zone spaces get a chance
  42. /// to connect to each other when being moved into each other's zones. An exception to this
  43. /// is the root zone since it is both immobile and without position and (limited) extents. If
  44. /// a zone space wants to connect to the root zone, it must do so manually (same goes for
  45. /// disconnecting).
  46. class SceneZoneSpace : public SceneSpace
  47. {
  48. public:
  49. typedef SceneSpace Parent;
  50. friend class SceneZoneSpaceManager;
  51. friend class SceneRootZone; // mZoneFlags, connectZoneSpace, disconnectZoneSpace
  52. enum
  53. {
  54. InvalidZoneGroup = 0
  55. };
  56. enum ZoneFlags
  57. {
  58. /// Whether this zoning space is "closed off" or not. When connections between
  59. /// zoning spaces are established, by default only spaces that are closed off are
  60. /// connected to those that are *not* and vice versa. This defines a natural progression
  61. /// where spaces that explicitly want to propagate traversals are connected to those
  62. /// that want to contain it.
  63. ///
  64. /// This flag is set by default.
  65. ZoneFlag_IsClosedOffSpace = BIT( 0 ),
  66. };
  67. protected:
  68. enum
  69. {
  70. ZoneGroupMask = Parent::NextFreeMask << 0,
  71. NextFreeMask = Parent::NextFreeMask << 1
  72. };
  73. /// The manager to which this zone space is registered.
  74. SceneZoneSpaceManager* mManager;
  75. /// ID of first zone defined by object.
  76. U32 mZoneRangeStart;
  77. /// Number of zones managed by #obj. IDs are consecutive
  78. /// starting with #mZoneRangeStart.
  79. U32 mNumZones;
  80. /// Group which the zone is assigned to. Zone spaces that would normally not connect
  81. /// do connect if they are assigned the same zone group. O to disable (default).
  82. U32 mZoneGroup;
  83. ///
  84. BitSet32 mZoneFlags;
  85. /// @name Occluders
  86. ///
  87. /// Zone spaces keep track of the occluders that get added to them so that during
  88. /// traversal, they can be taken on board as early as possible. This allows traversals
  89. /// themselves to take occlusion into account.
  90. ///
  91. /// @{
  92. /// Occluders in this space. Every object that is assigned to this space
  93. /// and has the OccluderObjectType flag set, is added to this list.
  94. Vector< SceneObject* > mOccluders;
  95. /// Add the given object to the list of occluders in this zone space.
  96. void _addOccluder( SceneObject* object );
  97. /// Remove the given object from the list of occluders in this zone space.
  98. void _removeOccluder( SceneObject* object );
  99. /// Register the occluders in this zone with the given culling state.
  100. void _addOccludersToCullingState( SceneCullingState* state ) const;
  101. /// @}
  102. /// @name Zone Space Connectivity
  103. /// @{
  104. //TODO: we should have both automatic and manual connections; only automatic connections
  105. // should get reset when a zone space moves
  106. /// Link to another zone space.
  107. struct ZoneSpaceRef
  108. {
  109. // We could be storing zone IDs here to connect individual zones rather than just
  110. // the managers but since no one requires this at the moment, the code doesn't do it.
  111. // However, it's trivial to add.
  112. SceneZoneSpace* mZoneSpace;
  113. ZoneSpaceRef* mNext;
  114. };
  115. /// List of zone spaces that this space is connected to. This is used
  116. /// for traversals.
  117. ZoneSpaceRef* mConnectedZoneSpaces;
  118. /// Allocator for ZoneSpaceRefs.
  119. static ClassChunker< ZoneSpaceRef > smZoneSpaceRefChunker;
  120. /// Disconnect all zone spaces currently connected to this space.
  121. virtual void _disconnectAllZoneSpaces();
  122. /// Hand the traversal over to connected zone spaces.
  123. virtual void _traverseConnectedZoneSpaces( SceneTraversalState* state );
  124. /// Return true if the given zone space, which has crossed into this space, should
  125. /// be automatically connected. Note that event
  126. virtual bool _automaticallyConnectZoneSpace( SceneZoneSpace* zoneSpace ) const;
  127. /// @}
  128. /// @name SceneManager Notifications
  129. ///
  130. /// These methods are called when SceneManager assigns or removes objects to/from our zones.
  131. ///
  132. /// @{
  133. /// Called by the SceneManager when an object is added to one or more zones that
  134. /// are managed by this object.
  135. virtual void _onZoneAddObject( SceneObject* object, const U32* zoneIDs, U32 numZones );
  136. /// Called by the SceneManager when an object that was previously added to one or more
  137. /// zones managed by this object is now removed.
  138. virtual void _onZoneRemoveObject( SceneObject* object );
  139. /// @}
  140. // SceneObject.
  141. virtual void onSceneRemove();
  142. public:
  143. SceneZoneSpace();
  144. virtual ~SceneZoneSpace();
  145. /// Return true if this is the outdoor zone.
  146. bool isRootZone() const { return ( getZoneRangeStart() == 0 ); }
  147. /// Gets the index of the first zone this object manages in the collection of zones or 0xFFFFFFFF if the
  148. /// object is not managing zones.
  149. U32 getZoneRangeStart() const { return mZoneRangeStart; }
  150. /// Return the number of zones that are managed by this object.
  151. U32 getZoneRange() const { return mNumZones; }
  152. /// Return the zone group that this zone space belongs to. 0 by default which means the
  153. /// zone space is not allocated to a specific zone group.
  154. U32 getZoneGroup() const { return mZoneGroup; }
  155. /// Set the zone group of this zone space. Zone spaces in the same group will connect even if
  156. /// not connecting by default. Set to 0 to deactivate.
  157. void setZoneGroup( U32 group );
  158. /// Dump a listing of all objects assigned to this zone space to the console as well
  159. /// as a list of all connected spaces.
  160. ///
  161. /// @param update Whether to update the zone contents before dumping. Since the zoning states of
  162. /// SceneObjects are updated lazily, the contents of a zone can be outdated.
  163. void dumpZoneState( bool update = true );
  164. /// Get the ambient light color of the given zone in this space or return false if the
  165. /// given zone does not have an ambient light color assigned to it.
  166. virtual bool getZoneAmbientLightColor( U32 zone, LinearColorF& outColor ) const { return false; }
  167. /// @name Containment Tests
  168. /// @{
  169. ///
  170. virtual bool getOverlappingZones( const Box3F& aabb, U32* zones, U32& numZones ) = 0;
  171. /// Find the zones in this object that @a obj is part of.
  172. ///
  173. /// @param obj Object in question.
  174. /// @param outZones Indices of zones containing the object. Must have at least as many entries
  175. /// as there as zones in this object or SceneObject::MaxObjectZones, whichever is smaller.
  176. /// Note that implementations should never write more than SceneObject::MaxObjectZones entries.
  177. /// @param outNumZones Number of elements in the returned array.
  178. ///
  179. /// @return Return true if the world box of @a obj is fully contained within the zones of this object or
  180. /// false if it is at least partially outside of them.
  181. virtual bool getOverlappingZones( SceneObject* obj, U32* outZones, U32& outNumZones );
  182. /// Returns the ID of the zone that are managed by this object that contains @a p.
  183. /// @param p Point to test.
  184. /// @return ID of the zone containing @a p or InvalidZoneId if none of the zones defined by this
  185. /// object contain the point.
  186. virtual U32 getPointZone( const Point3F& p ) = 0;
  187. /// @}
  188. /// @name Connectivity
  189. /// @{
  190. /// Connect this zone space to the given zone space.
  191. ///
  192. /// @param zoneSpace A zone space.
  193. ///
  194. /// @note Connectivity is reset when a zone space is moved!
  195. virtual void connectZoneSpace( SceneZoneSpace* zoneSpace );
  196. /// If the object is a zone space, then this method is called to instruct the object
  197. /// to remove any zone connectivity to @a zoneSpace.
  198. ///
  199. /// @param zoneSpace A zone space which had previously been passed to connectZoneSpace().
  200. virtual void disconnectZoneSpace( SceneZoneSpace* zoneSpace );
  201. /// @}
  202. /// @name Traversals
  203. /// @{
  204. /// Traverse into the zones of this space. Set the render states of those zones and add the frustums
  205. /// that lead into them.
  206. ///
  207. /// The traversal stack is expected to not be empty and the topmost entry on the stack should be the
  208. /// zone of another manager from which traversal is handed over to this manager.
  209. ///
  210. /// @param state Scene traversal state.
  211. ///
  212. /// @note If the zone's of a zone space are reached via different traversal paths, this method
  213. /// will be called multiple times on the same space.
  214. virtual void traverseZones( SceneTraversalState* state ) = 0;
  215. /// Traverse the zones in this space starting with the given zone. Where appropriate, traversal
  216. /// should be handed off to connected spaces.
  217. ///
  218. /// @param state State which should be updated by the traversal.
  219. /// @param startZoneId ID of zone in this manager in which to start traversal.
  220. virtual void traverseZones( SceneTraversalState* state, U32 startZoneId ) = 0;
  221. /// @}
  222. // SimObject.
  223. virtual bool writeField( StringTableEntry fieldName, const char* value );
  224. static void initPersistFields();
  225. // NetObject.
  226. virtual U32 packUpdate( NetConnection* connection, U32 mask, BitStream* stream );
  227. virtual void unpackUpdate( NetConnection* connection, BitStream* stream );
  228. private:
  229. static bool _setZoneGroup( void* object, const char* index, const char* data );
  230. };
  231. #endif // !_SCENEZONESPACE_H_