terrCell.h 6.6 KB

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