terrCell.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 _TERRCELL_H_
  27. #define _TERRCELL_H_
  28. #ifndef _GFXVERTEXBUFFER_H_
  29. #include "gfx/gfxVertexBuffer.h"
  30. #endif
  31. #ifndef _GFXPRIMITIVEBUFFER_H_
  32. #include "gfx/gfxPrimitiveBuffer.h"
  33. #endif
  34. #ifndef _TDICTIONARY_H_
  35. #include "core/util/tDictionary.h"
  36. #endif
  37. #ifndef _MORIENTEDBOX_H_
  38. #include "math/mOrientedBox.h"
  39. #endif
  40. #ifndef _BITVECTOR_H_
  41. #include "core/bitVector.h"
  42. #endif
  43. class TerrainBlock;
  44. class TerrainCellMaterial;
  45. class Frustum;
  46. class SceneRenderState;
  47. class SceneZoneSpaceManager;
  48. /// The TerrainCell vertex format optimized to
  49. /// 32 bytes for optimal vertex cache performance.
  50. GFXDeclareVertexFormat( TerrVertex )
  51. {
  52. /// The position.
  53. Point3F point;
  54. /// The normal.
  55. Point3F normal;
  56. /// The height for calculating the
  57. /// tangent vector on the GPU.
  58. F32 tangentZ;
  59. /// The empty flag state which is either
  60. /// -1 or 1 so we can do the special
  61. /// interpolation trick.
  62. F32 empty;
  63. };
  64. /// The TerrCell is a single quadrant of the terrain geometry quadtree.
  65. class TerrCell
  66. {
  67. protected:
  68. /// The handle to the static vertex buffer which holds the
  69. /// vertices for this cell.
  70. GFXVertexBufferHandle<TerrVertex> mVertexBuffer;
  71. /// The handle to the static primitive buffer for this cell.
  72. /// It is only used if this cell has any empty squares
  73. GFXPrimitiveBufferHandle mPrimBuffer;
  74. ///
  75. Point2I mPoint;
  76. ///
  77. U32 mSize;
  78. /// The level of this cell within the quadtree (of cells) where
  79. /// zero is the root and one is a direct child of the root, etc.
  80. U32 mLevel;
  81. /// Statics used in VB and PB generation.
  82. static const U32 smVBStride;
  83. static const U32 smMinCellSize;
  84. static const U32 smVBSize;
  85. static const U32 smPBSize;
  86. static const U32 smTriCount;
  87. /// Triangle count for our own primitive buffer, if any
  88. U32 mTriCount;
  89. /// Indicates if this cell has any empty squares
  90. bool mHasEmpty;
  91. /// A list of all empty vertices for this cell
  92. Vector<U32> mEmptyVertexList;
  93. /// The terrain this cell is based on.
  94. TerrainBlock *mTerrain;
  95. /// The material used to render the cell.
  96. TerrainCellMaterial *mMaterial;
  97. /// The bounding box of this cell in
  98. /// TerrainBlock object space.
  99. Box3F mBounds;
  100. /// The OBB of this cell in world space.
  101. OrientedBox3F mOBB;
  102. /// The bounding radius of this cell.
  103. F32 mRadius;
  104. /// The child cells of this one.
  105. TerrCell *mChildren[4];
  106. /// This bit flag tells us which materials effect
  107. /// this cell and is used for optimizing rendering.
  108. /// @see TerrainFile::mMaterialAlphaMap
  109. U64 mMaterials;
  110. /// Whether this cell is fully contained inside interior zones.
  111. bool mIsInteriorOnly;
  112. /// The zone overlap for this cell.
  113. /// @note The bit for the outdoor zone is never set.
  114. BitVector mZoneOverlap;
  115. ///
  116. void _updateBounds();
  117. /// Update #mOBB from the current terrain transform state.
  118. void _updateOBB();
  119. //
  120. void _init( TerrainBlock *terrain,
  121. const Point2I &point,
  122. U32 size,
  123. U32 level );
  124. //
  125. void _updateVertexBuffer();
  126. //
  127. void _updatePrimitiveBuffer();
  128. //
  129. void _updateMaterials();
  130. //
  131. bool _isVertIndexEmpty( U32 index ) const;
  132. public:
  133. TerrCell();
  134. virtual ~TerrCell();
  135. static TerrCell* init( TerrainBlock *terrain );
  136. void getRenderPrimitive( GFXPrimitive *prim,
  137. GFXVertexBufferHandleBase *vertBuff,
  138. GFXPrimitiveBufferHandle *primBuff ) const;
  139. void updateGrid( const RectI &gridRect, bool opacityOnly = false );
  140. /// Update the world-space OBBs used for culling.
  141. void updateOBBs();
  142. ///
  143. void updateZoning( const SceneZoneSpaceManager *zoneManager );
  144. void cullCells( const SceneRenderState *state,
  145. const Point3F &objLodPos,
  146. Vector<TerrCell*> *outCells );
  147. const Box3F& getBounds() const { return mBounds; }
  148. /// Returns the object space sphere bounds.
  149. SphereF getSphereBounds() const { return SphereF( mBounds.getCenter(), mRadius ); }
  150. F32 getSqDistanceTo( const Point3F &pt ) const;
  151. F32 getDistanceTo( const Point3F &pt ) const;
  152. U64 getMaterials() const { return mMaterials; }
  153. /// Returns a bit vector of what zones overlap this cell.
  154. const BitVector& getZoneOverlap() const { return mZoneOverlap; }
  155. /// Forces the loading of the materials for this
  156. /// cell and all its child cells.
  157. void preloadMaterials();
  158. TerrainCellMaterial* getMaterial();
  159. /// Return true if this is a leaf cell, i.e. a cell without children.
  160. bool isLeaf() const { return !mChildren[ 0 ]; }
  161. /// Deletes the materials for this cell
  162. /// and all its children. They will be
  163. /// recreate on the next request.
  164. void deleteMaterials();
  165. U32 getSize() const { return mSize; }
  166. Point2I getPoint() const { return mPoint; }
  167. /// Initializes a primitive buffer for rendering any cell.
  168. static void createPrimBuffer( GFXPrimitiveBufferHandle *primBuffer );
  169. /// Debug Rendering
  170. /// @{
  171. /// Renders the debug bounds for this cell.
  172. void renderBounds() const;
  173. /// @}
  174. protected:
  175. Point3F* zode_vertexBuffer;
  176. void createZodiacVertexBuffer();
  177. public:
  178. const Point3F* getZodiacVertexBuffer();
  179. void deleteZodiacVertexBuffer();
  180. static void createZodiacPrimBuffer(U16** primBuffer);
  181. };
  182. inline F32 TerrCell::getDistanceTo( const Point3F &pt ) const
  183. {
  184. return ( mBounds.getCenter() - pt ).len() - mRadius;
  185. }
  186. inline bool TerrCell::_isVertIndexEmpty( U32 index ) const
  187. {
  188. for ( U32 i = 0; i < mEmptyVertexList.size(); ++i )
  189. {
  190. if ( mEmptyVertexList[i] == index )
  191. {
  192. return true;
  193. }
  194. }
  195. return false;
  196. }
  197. #endif // _TERRCELL_H_