portal.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 _PORTAL_H_
  23. #define _PORTAL_H_
  24. #ifndef _ZONE_H_
  25. #include "T3D/zone.h"
  26. #endif
  27. #ifndef _TVECTOR_H_
  28. #include "core/util/tVector.h"
  29. #endif
  30. class SceneCullingState;
  31. class SceneCullingVolume;
  32. /// A transitioning zone that connects other zones.
  33. ///
  34. /// Basically a portal is two things:
  35. ///
  36. /// 1) A zone that overlaps multiple other zones and thus connects them.
  37. /// 2) A polygon standing upright in the middle of the portal zone's world box.
  38. ///
  39. /// When traversing from zone to zone, portals serve as both zones in their own
  40. /// right (i.e. objects may be located in a portal zone) as well as a peek hole
  41. /// that determines what area of a target zone is visible through a portal.
  42. ///
  43. /// Torque's portals are special in that they are two-way by default. This greatly
  44. /// simplifies zone setups but it also complicates handling in the engine somewhat.
  45. /// Also, these portals here are nothing but peek holes--they do not define transform
  46. /// portals that could be looking at a different location in space altogether.
  47. ///
  48. /// Portals can be marked explicitly as being one-sided by flagging either of the portal's
  49. /// sides as impassable. This flagging can also be used dynamically to, for example, block
  50. /// a portal while a door is still down and then unblock the portal when the door is
  51. /// opened.
  52. ///
  53. /// Portals are classified as either interior or exterior portals. An exterior portal is
  54. /// a portal that has only non-SceneRootZone zones on side of the portal plane and only the
  55. /// SceneRootZone on the other side of it. An interior portal is a portal that has only
  56. /// non-SceneRootZone zones on both sides of the portal plane. A mixture of the two is not
  57. /// allowed – when adding SceneRootZone to a portal, it must exist alone on its portal
  58. /// side.
  59. class Portal : public Zone
  60. {
  61. public:
  62. typedef Zone Parent;
  63. /// Identifies the subspaces defined by the portal plane.
  64. enum Side
  65. {
  66. FrontSide, ///< Subspace on front side of portal plane.
  67. BackSide ///< Subspace on back side of portal plane.
  68. };
  69. /// Identifies the type of portal.
  70. enum Classification
  71. {
  72. InvalidPortal, ///< Portal does not connect anything.
  73. InteriorPortal, ///< Portal between interior zones.
  74. ExteriorPortal ///< Portal between interior zones on one and side and SceneRootZone on the other.
  75. };
  76. protected:
  77. enum
  78. {
  79. PassableMask = Parent::NextFreeMask << 0, ///< #mPassableSides has changed.
  80. NextFreeMask = Parent::NextFreeMask << 1,
  81. };
  82. /// Flags that allow preventing traversal through specific
  83. /// sides of the portal. By default, both sides are passable.
  84. bool mPassableSides[ 2 ];
  85. /// @name Derived Portal Data
  86. /// @{
  87. /// Classification of this portal as interior or exterior portal.
  88. Classification mClassification;
  89. /// For exterior portals, this is the side of the portal on which
  90. /// the connected interior zones lie.
  91. Side mInteriorSide;
  92. /// Whether the portal plane and polygon need to be updated.
  93. bool mIsGeometryDirty;
  94. /// Portal polygon in world space.
  95. Vector< Point3F > mPortalPolygonWS;
  96. /// The plane defined by the portal's rectangle.
  97. PlaneF mPortalPlane;
  98. /// Update derived data, if necessary.
  99. void _update();
  100. /// Update the world space portal geometry.
  101. void _updateGeometry();
  102. /// Detect whether this is an exterior, interior, or invalid portal.
  103. void _updateConnectivity();
  104. /// @}
  105. /// Compute a clipped culling volume from the portal geometry and current
  106. /// traversal state. If successful, store the resulting culling volume in
  107. /// @a outVolume and return true.
  108. bool _generateCullingVolume( SceneTraversalState* state, SceneCullingVolume& outVolume ) const;
  109. // SceneSpace.
  110. virtual void _renderObject( ObjectRenderInst* ri, SceneRenderState* state, BaseMatInstance* overrideMat );
  111. virtual ColorI _getDefaultEditorSolidColor() const { return ColorI( 0, 255, 0, 45 ); }
  112. virtual ColorI _getDefaultEditorWireframeColor() const
  113. {
  114. switch( mClassification )
  115. {
  116. case ExteriorPortal: return ColorI( 0, 128, 128, 255 ); break;
  117. case InteriorPortal: return ColorI( 128, 128, 0, 255 );
  118. default: return ColorI( 255, 255, 255, 255 ); break;
  119. }
  120. }
  121. // SceneObject.
  122. virtual void onSceneRemove();
  123. // SceneZoneSpace.
  124. virtual void _traverseConnectedZoneSpaces( SceneTraversalState* state );
  125. virtual void _disconnectAllZoneSpaces();
  126. public:
  127. Portal();
  128. /// Return what kind of portal this is (interior or exterior).
  129. Classification getClassification() const { return mClassification; }
  130. /// Return the plane that is defined by the portal's rectangle.
  131. const PlaneF& getPortalPlane() const { return mPortalPlane; }
  132. /// Return the side that the given point is in relative to the portal plane.
  133. Side getSideRelativeToPortalPlane( const Point3F& point ) const;
  134. /// Test whether the given side of the portal is open for traversal.
  135. bool isSidePassable( Side side ) const { return mPassableSides[ side ]; }
  136. /// Set whether the given portal side is passable.
  137. void setSidePassable( Side side, bool value );
  138. /// Return true if the portal leads to the outdoor zone.
  139. bool isExteriorPortal() const { return ( getClassification() == ExteriorPortal ); }
  140. /// Return true if the portal connects interior zones only.
  141. bool isInteriorPortal() const { return ( getClassification() == InteriorPortal ); }
  142. /// For exterior portals, get the side on which the interior zones of the portal lie.
  143. Side getInteriorSideOfExteriorPortal() const
  144. {
  145. AssertFatal( isExteriorPortal(), "Portal::getInteriorSideOfExteriorPortal - Not an exterior portal!" );
  146. return mInteriorSide;
  147. }
  148. // SimObject.
  149. DECLARE_CONOBJECT( Portal );
  150. static void initPersistFields();
  151. static void consoleInit();
  152. virtual bool writeField( StringTableEntry fieldName, const char* value );
  153. virtual String describeSelf() const;
  154. // NetObject.
  155. virtual U32 packUpdate( NetConnection* conn, U32 mask, BitStream* stream );
  156. virtual void unpackUpdate( NetConnection* conn, BitStream* stream );
  157. // SceneObject.
  158. virtual void setTransform( const MatrixF &mat );
  159. // SceneZoneSpace.
  160. virtual void traverseZones( SceneTraversalState* state, U32 startZoneId );
  161. virtual void connectZoneSpace( SceneZoneSpace* zoneSpace );
  162. virtual void disconnectZoneSpace( SceneZoneSpace* zoneSpace );
  163. private:
  164. static bool _setFrontSidePassable( void* object, const char* index, const char* data );
  165. static bool _setBackSidePassable( void* object, const char* index, const char* data );
  166. };
  167. #endif // _PORTAL_H_