groundPlane.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. GroundPlane();
  54. virtual ~GroundPlane();
  55. virtual bool onAdd();
  56. virtual void onRemove();
  57. virtual U32 packUpdate( NetConnection* connection, U32 mask, BitStream* stream );
  58. virtual void unpackUpdate( NetConnection* connection, BitStream* stream );
  59. virtual void prepRenderImage( SceneRenderState* state );
  60. virtual bool castRay( const Point3F& start, const Point3F& end, RayInfo* info );
  61. virtual void buildConvex( const Box3F& box, Convex* convex );
  62. virtual bool buildPolyList( PolyListContext context, AbstractPolyList* polyList, const Box3F& box, const SphereF& sphere );
  63. virtual void inspectPostApply();
  64. virtual void setTransform( const MatrixF &mat );
  65. virtual void setScale( const Point3F& scale );
  66. static void initPersistFields();
  67. virtual void getUtilizedAssets(Vector<StringTableEntry>* usedAssetsList);
  68. protected:
  69. typedef GFXVertexPNTBT VertexType;
  70. void _updateMaterial();
  71. void createGeometry( const Frustum& frustum );
  72. void projectFrustum( const Frustum& frustum, F32 squareSize,
  73. Point2F& outMin, Point2F& outMax );
  74. void generateGrid( U32 width, U32 height, F32 squareSize,
  75. const Point2F& min, const Point2F& max,
  76. GFXVertexBufferHandle< VertexType >& outVertices,
  77. GFXPrimitiveBufferHandle& outPrimitives );
  78. Box3F getPlaneBox();
  79. private:
  80. typedef GFXVertexBufferHandle< VertexType > VertexBuffer;
  81. typedef GFXPrimitiveBufferHandle PrimitiveBuffer;
  82. F32 mSquareSize; ///< World units per grid cell edge.
  83. F32 mScaleU; ///< Scale factor for U texture coordinates.
  84. F32 mScaleV; ///< Scale factor for V texture coordinates.
  85. BaseMatInstance* mMaterialInst;
  86. DECLARE_MATERIALASSET(GroundPlane, Material);
  87. DECLARE_ASSET_NET_SETGET(GroundPlane, Material, -1);
  88. PhysicsBody *mPhysicsRep;
  89. /// @name Rendering State
  90. /// @{
  91. Point2F mMin;
  92. Point2F mMax;
  93. VertexBuffer mVertexBuffer;
  94. PrimitiveBuffer mPrimitiveBuffer;
  95. GFXPrimitive mPrimitive;
  96. /// @}
  97. Convex* mConvexList; ///< List of collision convexes we have created; for cleanup.
  98. };
  99. static const F32 GROUND_PLANE_BOX_HEIGHT_HALF = 1.0f;
  100. static const F32 GROUND_PLANE_BOX_EXTENT_HALF = 16000.0f;
  101. inline Box3F GroundPlane::getPlaneBox()
  102. {
  103. Box3F planeBox;
  104. planeBox.minExtents = Point3F( - GROUND_PLANE_BOX_EXTENT_HALF,
  105. - GROUND_PLANE_BOX_EXTENT_HALF,
  106. - 0.05f );
  107. planeBox.maxExtents = Point3F( GROUND_PLANE_BOX_EXTENT_HALF,
  108. GROUND_PLANE_BOX_EXTENT_HALF,
  109. 0.05f );
  110. return planeBox;
  111. }
  112. #endif // _TORQUE_T3D_GROUNDPLANE_H_