groundPlane.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 _TORQUE_T3D_GROUNDPLANE_H_
  23. #define _TORQUE_T3D_GROUNDPLANE_H_
  24. #ifndef _SCENEOBJECT_H_
  25. #include "scene/sceneObject.h"
  26. #endif
  27. #ifndef _GFXVERTEXBUFFER_H_
  28. #include "gfx/gfxVertexBuffer.h"
  29. #endif
  30. #ifndef _GFXPRIMITIVEBUFFER_H_
  31. #include "gfx/gfxPrimitiveBuffer.h"
  32. #endif
  33. #include "T3D/assets/MaterialAsset.h"
  34. class PhysicsBody;
  35. class BaseMatInstance;
  36. /// A virtually infinite XY ground plane primitive.
  37. ///
  38. /// For rendering, a subset of the plane spanning the view frustum is generated
  39. /// and rendered. Tesselation is determined by the given squareSize property.
  40. ///
  41. /// For collision detection, a finite bounding box is used to deal with finite
  42. /// precision of floating-point operations (we can't use floating-point infinity
  43. /// as infinity*0 is undefined.)
  44. ///
  45. /// The ground plane can be textured like regular geometry by assigning a material
  46. /// name to its 'material' property. UVs mirror grid coordinates so that when
  47. /// using UV wrapping, textures will tile nicely.
  48. class GroundPlane : public SceneObject
  49. {
  50. public:
  51. typedef SceneObject Parent;
  52. DECLARE_CONOBJECT( GroundPlane );
  53. DECLARE_CATEGORY("Environment \t BackGround");
  54. GroundPlane();
  55. virtual ~GroundPlane();
  56. bool onAdd() override;
  57. void onRemove() override;
  58. U32 packUpdate( NetConnection* connection, U32 mask, BitStream* stream ) override;
  59. void unpackUpdate( NetConnection* connection, BitStream* stream ) override;
  60. void prepRenderImage( SceneRenderState* state ) override;
  61. bool castRay( const Point3F& start, const Point3F& end, RayInfo* info ) override;
  62. void buildConvex( const Box3F& box, Convex* convex ) override;
  63. bool buildPolyList( PolyListContext context, AbstractPolyList* polyList, const Box3F& box, const SphereF& sphere ) override;
  64. void inspectPostApply() override;
  65. void setTransform( const MatrixF &mat ) override;
  66. void setScale( const Point3F& scale ) override;
  67. static void initPersistFields();
  68. void getUtilizedAssets(Vector<StringTableEntry>* usedAssetsList) override;
  69. protected:
  70. typedef GFXVertexPNTBT VertexType;
  71. void _updateMaterial();
  72. void createGeometry( const Frustum& frustum );
  73. void projectFrustum( const Frustum& frustum, F32 squareSize,
  74. Point2F& outMin, Point2F& outMax );
  75. void generateGrid( U32 width, U32 height, F32 squareSize,
  76. const Point2F& min, const Point2F& max,
  77. GFXVertexBufferHandle< VertexType >& outVertices,
  78. GFXPrimitiveBufferHandle& outPrimitives );
  79. Box3F getPlaneBox();
  80. private:
  81. typedef GFXVertexBufferHandle< VertexType > VertexBuffer;
  82. typedef GFXPrimitiveBufferHandle PrimitiveBuffer;
  83. F32 mSquareSize; ///< World units per grid cell edge.
  84. F32 mScaleU; ///< Scale factor for U texture coordinates.
  85. F32 mScaleV; ///< Scale factor for V texture coordinates.
  86. BaseMatInstance* mMaterialInst;
  87. DECLARE_MATERIALASSET(GroundPlane, Material);
  88. DECLARE_ASSET_NET_SETGET(GroundPlane, Material, -1);
  89. PhysicsBody *mPhysicsRep;
  90. /// @name Rendering State
  91. /// @{
  92. Point2F mMin;
  93. Point2F mMax;
  94. VertexBuffer mVertexBuffer;
  95. PrimitiveBuffer mPrimitiveBuffer;
  96. GFXPrimitive mPrimitive;
  97. /// @}
  98. Convex* mConvexList; ///< List of collision convexes we have created; for cleanup.
  99. };
  100. static const F32 GROUND_PLANE_BOX_HEIGHT_HALF = 1.0f;
  101. static const F32 GROUND_PLANE_BOX_EXTENT_HALF = 16000.0f;
  102. inline Box3F GroundPlane::getPlaneBox()
  103. {
  104. Box3F planeBox;
  105. planeBox.minExtents = Point3F( - GROUND_PLANE_BOX_EXTENT_HALF,
  106. - GROUND_PLANE_BOX_EXTENT_HALF,
  107. - 0.05f );
  108. planeBox.maxExtents = Point3F( GROUND_PLANE_BOX_EXTENT_HALF,
  109. GROUND_PLANE_BOX_EXTENT_HALF,
  110. 0.05f );
  111. return planeBox;
  112. }
  113. #endif // _TORQUE_T3D_GROUNDPLANE_H_