groundPlane.h 5.2 KB

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