forest.h 6.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------

#ifndef _H_FOREST_
#define _H_FOREST_

#ifndef _FORESTITEM_H_
   #include "forest/forestItem.h"
#endif
#ifndef _FORESTDATAFILE_H_
   #include "forest/forestDataFile.h"
#endif
#ifndef _MATHUTIL_FRUSTUM_H_  
   #include "math/util/frustum.h"
#endif
#ifndef _GFXTEXTUREHANDLE_H_
   #include "gfx/gfxTextureHandle.h"
#endif
#ifndef _COLLISION_H_
   #include "collision/collision.h"
#endif
#ifndef _SCENEOBJECT_H_
   #include "scene/sceneObject.h"
#endif
#ifndef _SIGNAL_H_
   #include "core/util/tSignal.h"
#endif
#ifndef __RESOURCE_H__
   #include "core/resource.h"
#endif
#ifndef _CONVEX_H_
   #include "collision/convex.h"
#endif


class TSShapeInstance;
class Forest;
class ForestItemData;
class ForestData;
class GBitmap;
class IForestCollision;
class IForestMask;
class PhysicsBody;
struct ObjectRenderInst;
struct TreePlacementInfo;
class ForestRayInfo;
class SceneZoneSpaceManager;


struct TreeInfo
{
   U32                     treeId;   
   SimObjectId             treeTypeId;
   F32                     distance;
   SimObjectId             forestId;
   Point3F                 position;
};

typedef Signal<void( Forest *forest )> ForestCreatedSignal;


/// 
class Forest : public SceneObject
{
   friend class CreateForestEvent;
   friend class ForestConvex;

protected:

   typedef SceneObject Parent;
   
   /// Collision and Physics
   /// @{

   Convex* mConvexList;   
   
   /// @}

   /// The name of the planting data file.
   StringTableEntry mDataFileName;

   /// The forest data file which defines planting.
   Resource<ForestData> mData;

   /// Used to scale the tree LODs when rendering into 
   /// reflections.  It should be greater or equal to 1.
   F32 mReflectionLodScalar;

   /// Set when rezoning of forest cells is required.
   bool mZoningDirty;

   /// Debug helpers.
   static bool smForceImposters;
   static bool smDisableImposters;
   static bool smDrawCells;
   static bool smDrawBounds;

   ///
   bool mRegen;

   enum MaskBits
   {
      MediaMask         = Parent::NextFreeMask << 1,
      LodMask           = Parent::NextFreeMask << 2,
      NextFreeMask      = Parent::NextFreeMask << 3
   };


   static U32  smTotalCells;
   static U32  smCellsRendered;
   static U32  smCellItemsRendered;
   static U32  smCellsBatched;
   static U32  smCellItemsBatched;
   static F32  smAverageItemsPerCell;

   static void _clearStats(bool);

   void _renderCellBounds( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );

   void _onZoningChanged( SceneZoneSpaceManager *zoneManager );

   static ForestCreatedSignal smCreatedSignal;
   static ForestCreatedSignal smDestroyedSignal;

public:
   
   static ForestCreatedSignal& getCreatedSignal() { return smCreatedSignal; }
   static ForestCreatedSignal& getDestroyedSignal() { return smDestroyedSignal; }

   Forest();
   virtual ~Forest();

   DECLARE_CONOBJECT(Forest);
   static void consoleInit();
   static void initPersistFields();   

   // SimObject
   bool onAdd();
   void onRemove();

   /// Overloaded from SceneObject to properly update
   /// the client side forest when changes occur within
   /// the mission editor.
   void inspectPostApply();

   /// Overloaded from SceneObject for updating the 
   /// client side position of the forest.
   void setTransform( const MatrixF &mat );

   void prepRenderImage( SceneRenderState *state );

   bool isTreeInRange( const Point2F& point, F32 radius ) const;

   // Network
   U32 packUpdate( NetConnection *conn, U32 mask, BitStream *stream );
   void unpackUpdate( NetConnection *conn, BitStream *stream );

   //IForestCollision *getCollision() const { return mCollision; }

   // SceneObject - Collision
   virtual void buildConvex( const Box3F& box, Convex* convex );
   virtual bool buildPolyList( PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF &sphere );
   virtual bool castRay( const Point3F &start, const Point3F &end, RayInfo *outInfo );
   virtual bool castRayRendered( const Point3F &start, const Point3F &end, RayInfo *outInfo );
   virtual bool collideBox( const Point3F &start, const Point3F &end, RayInfo *outInfo );

   // SceneObject - Other
   virtual void applyRadialImpulse( const Point3F &origin, F32 radius, F32 magnitude );

   bool castRayBase( const Point3F &start, const Point3F &end, RayInfo *outInfo, bool rendered );
     
   const Resource<ForestData>& getData() const { return mData; }

   Resource<ForestData>& getData() { return mData; }

   //bool  buildPolyList(AbstractPolyList* polyList, const Box3F &box, const SphereF& sphere);
   //void  buildConvex(const Box3F& box, Convex* convex);

   void getLocalWindTrees( const Point3F &camPos, F32 radius, Vector<TreePlacementInfo> *placementInfo );

   /// Called to create a new empty planting data file and
   /// assign it to this forest.
   void createNewFile();

   ///
   void saveDataFile( const char *path = NULL );

   ///
   void clear() { mData->clear(); }

   /// Called to rebuild the collision state.
   void updateCollision();
};

#endif // _H_FOREST_