groundPlane.h

Engine/source/T3D/groundPlane.h

More...

Classes:

class

A virtually infinite XY ground plane primitive.

Detailed Description

Public Variables

const F32 GROUND_PLANE_BOX_EXTENT_HALF 
const F32 GROUND_PLANE_BOX_HEIGHT_HALF 
  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _TORQUE_T3D_GROUNDPLANE_H_
 25#define _TORQUE_T3D_GROUNDPLANE_H_
 26
 27#ifndef _SCENEOBJECT_H_
 28#include "scene/sceneObject.h"
 29#endif
 30#ifndef _GFXVERTEXBUFFER_H_
 31#include "gfx/gfxVertexBuffer.h"
 32#endif
 33#ifndef _GFXPRIMITIVEBUFFER_H_
 34#include "gfx/gfxPrimitiveBuffer.h"
 35#endif
 36
 37#include "T3D/assets/MaterialAsset.h"
 38
 39class PhysicsBody;
 40class BaseMatInstance;
 41
 42
 43/// A virtually infinite XY ground plane primitive.
 44///
 45/// For rendering, a subset of the plane spanning the view frustum is generated
 46/// and rendered.  Tesselation is determined by the given squareSize property.
 47///
 48/// For collision detection, a finite bounding box is used to deal with finite
 49/// precision of floating-point operations (we can't use floating-point infinity
 50/// as infinity*0 is undefined.)
 51///
 52/// The ground plane can be textured like regular geometry by assigning a material
 53/// name to its 'material' property.  UVs mirror grid coordinates so that when
 54/// using UV wrapping, textures will tile nicely.
 55
 56
 57class GroundPlane : public SceneObject
 58{
 59public:
 60
 61   typedef SceneObject Parent;
 62
 63   DECLARE_CONOBJECT( GroundPlane );
 64
 65   GroundPlane();
 66   virtual ~GroundPlane();
 67
 68   virtual bool      onAdd();
 69   virtual void      onRemove();
 70   virtual U32       packUpdate( NetConnection* connection, U32 mask, BitStream* stream );
 71   virtual void      unpackUpdate( NetConnection* connection, BitStream* stream );
 72   virtual void      prepRenderImage( SceneRenderState* state );
 73   virtual bool      castRay( const Point3F& start, const Point3F& end, RayInfo* info );
 74   virtual void      buildConvex( const Box3F& box, Convex* convex );
 75   virtual bool      buildPolyList( PolyListContext context, AbstractPolyList* polyList, const Box3F& box, const SphereF& sphere );
 76   virtual void      inspectPostApply();
 77   virtual void      setTransform( const MatrixF &mat );
 78   virtual void      setScale( const Point3F& scale );
 79
 80   static void       initPersistFields();
 81
 82   virtual void getUtilizedAssets(Vector<StringTableEntry>* usedAssetsList);
 83
 84protected:
 85
 86   typedef GFXVertexPNTBT VertexType;
 87
 88   void _updateMaterial();
 89
 90   void              createGeometry( const Frustum& frustum );
 91   void              projectFrustum( const Frustum& frustum, F32 squareSize,
 92                                     Point2F& outMin, Point2F& outMax );
 93   void              generateGrid( U32 width, U32 height, F32 squareSize,
 94                                   const Point2F& min, const Point2F& max,
 95                                   GFXVertexBufferHandle< VertexType>& outVertices,
 96                                   GFXPrimitiveBufferHandle& outPrimitives );
 97
 98   Box3F             getPlaneBox();
 99
100private:
101
102   typedef GFXVertexBufferHandle< VertexType> VertexBuffer;
103   typedef GFXPrimitiveBufferHandle PrimitiveBuffer;
104
105   F32               mSquareSize;   ///< World units per grid cell edge.
106   F32               mScaleU;       ///< Scale factor for U texture coordinates.
107   F32               mScaleV;       ///< Scale factor for V texture coordinates.
108   BaseMatInstance*  mMaterial;     ///< Instantiated material based on given material name.
109
110   DECLARE_NET_MATERIALASSET(GroundPlane, Material, -1);
111
112   PhysicsBody *mPhysicsRep;
113
114   /// @name Rendering State
115   /// @{
116
117   Point2F           mMin;
118   Point2F           mMax;
119   VertexBuffer      mVertexBuffer;
120   PrimitiveBuffer   mPrimitiveBuffer;
121   GFXPrimitive      mPrimitive;
122
123   /// @}
124
125   Convex*           mConvexList;   ///< List of collision convexes we have created; for cleanup.
126};
127
128static const F32 GROUND_PLANE_BOX_HEIGHT_HALF = 1.0f;
129static const F32 GROUND_PLANE_BOX_EXTENT_HALF = 16000.0f;
130
131inline Box3F GroundPlane::getPlaneBox()
132{
133   Box3F planeBox;
134
135   planeBox.minExtents = Point3F( - GROUND_PLANE_BOX_EXTENT_HALF,
136                                  - GROUND_PLANE_BOX_EXTENT_HALF,
137                                  - 0.05f );
138   planeBox.maxExtents = Point3F( GROUND_PLANE_BOX_EXTENT_HALF,
139                                  GROUND_PLANE_BOX_EXTENT_HALF,
140                                  0.05f );
141   return planeBox;
142}
143
144#endif // _TORQUE_T3D_GROUNDPLANE_H_
145