forestItem.h

Engine/source/forest/forestItem.h

More...

Classes:

Public Typedefs

ForestItemDataVector 
ForestItemKey 
ForestItemVector 

Detailed Description

Public Typedefs

typedef Vector< ForestItemData * > ForestItemDataVector 
typedef U32 ForestItemKey 
typedef Vector< ForestItem > ForestItemVector 
  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 _FORESTITEM_H_
 25#define _FORESTITEM_H_
 26
 27#ifndef _MMATH_H_
 28#include "math/mMath.h"
 29#endif
 30#ifndef _SIMBASE_H_
 31#include "console/simBase.h"
 32#endif
 33#ifndef _DYNAMIC_CONSOLETYPES_H_
 34#include "console/dynamicTypes.h"
 35#endif
 36
 37
 38class ForestItem;
 39class ForestCellBatch;
 40class ForestConvex;
 41class ForestWindAccumulator;
 42
 43class Box3F;
 44class Point3F;
 45class TSRenderState;
 46class SceneRenderState;
 47struct RayInfo;
 48class AbstractPolyList;
 49
 50
 51class ForestItemData : public SimDataBlock
 52{
 53protected:
 54
 55   typedef SimDataBlock Parent;
 56
 57   static SimSet* smSet;
 58
 59   bool mNeedPreload;
 60
 61   virtual void _preload() {}
 62
 63public:
 64   
 65   /// Shape file for this item type.
 66   StringTableEntry mShapeFile;   
 67
 68   /// This is the radius used during placement to ensure
 69   /// the element isn't crowded up against other trees.
 70   F32 mRadius;   
 71
 72   /// Overall scale to the effect of wind.
 73   F32 mWindScale;
 74
 75   /// This is used to control the overall bend amount of the tree by wind and impacts.
 76   F32 mTrunkBendScale;
 77
 78   /// Amplitude of the effect on larger branches.
 79   F32 mWindBranchAmp;
 80
 81   /// Amplitude of the winds effect on leafs/fronds.
 82   F32 mWindDetailAmp;
 83
 84   /// Frequency (speed) of the effect on leafs/fronds.
 85   F32 mWindDetailFreq;
 86
 87   /// Mass used in calculating spring forces.
 88   F32 mMass;
 89
 90   // The rigidity of the tree's trunk.
 91   F32 mRigidity;
 92
 93   // The tightness coefficient of the spring for this tree type's ForestWindAccumulator.
 94   F32 mTightnessCoefficient;
 95
 96   // The damping coefficient.
 97   F32 mDampingCoefficient;
 98
 99   /// Can other objects or spacial queries hit items of this type.
100   bool mCollidable;
101
102
103   static SimSet* getSet();
104   static ForestItemData* find( const char *name );
105
106   ForestItemData();
107   virtual ~ForestItemData() {}
108
109   DECLARE_CONOBJECT( ForestItemData );
110   
111   static void  consoleInit();
112   static void  initPersistFields();   
113
114   virtual void onNameChange(const char *name);
115   virtual bool onAdd();
116   virtual void packData(BitStream* stream);
117   virtual void unpackData(BitStream* stream);
118
119   /// Called from Forest the first time a datablock is used
120   /// in order to lazy load content.
121   void preload() 
122   { 
123      if ( !mNeedPreload ) 
124         return;
125
126      _preload();
127      mNeedPreload = false;
128   }
129
130   virtual const Box3F& getObjBox() const { return Box3F::Invalid; }
131
132   virtual bool render( TSRenderState *rdata, const ForestItem &item ) const { return false; }
133
134   virtual bool canBillboard( const SceneRenderState *state, const ForestItem &item, F32 distToCamera ) const { return false; }
135
136   virtual ForestCellBatch* allocateBatch() const { return NULL; }
137
138   typedef Signal<void(void)> ReloadSignal;
139
140   static ReloadSignal& getReloadSignal() 
141   { 
142      static ReloadSignal theSignal;
143      return theSignal;
144   }
145};
146
147typedef Vector<ForestItemData*> ForestItemDataVector;
148
149
150
151
152typedef U32 ForestItemKey;
153
154
155class ForestItem
156{
157protected:
158
159   ForestItemData *mDataBlock;
160   
161   // The unique identifier used when querying forest items.
162   ForestItemKey mKey;
163
164   MatrixF mTransform;
165
166   F32 mScale;
167
168   F32 mRadius;
169
170   Box3F mWorldBox;
171
172   // JCFHACK: change this to an abstract physics-rep.
173   //NxActor *mActor;
174
175   /// If we're currently being effected by one or
176   /// more wind emitters then we hold the results
177   /// in this class.
178   //ForestWindAccumulator *mWind;
179
180public:
181
182   // Constructs an invalid item.
183   ForestItem();
184  
185   // Note: We keep this non-virtual to save vtable space.
186   ~ForestItem();
187
188   static const ForestItem Invalid;
189
190   // Comparison operators with other ForestItems.
191   // Note that this compares only the ForestItemKey, we are not validating
192   // that any other data like the position actually matches.
193   bool operator==(const ForestItem&) const;
194   bool operator!=(const ForestItem&) const;
195
196   /// Returns true if this is a valid item.
197   bool isValid() const { return mKey != 0; }
198
199   /// Invalidates the item.
200   void makeInvalid() { mKey = 0; }
201
202   const ForestItemKey& getKey() const { return mKey; };
203
204   void setKey( const ForestItemKey &key ) { mKey = key; }
205
206   Point3F getPosition() const { return mTransform.getPosition(); }
207
208   const MatrixF& getTransform() const { return mTransform; }
209
210   F32 getScale() const { return mScale; }
211
212   F32 getRadius() const { return mRadius; }
213
214   Point3F getCenterPoint() const { return mWorldBox.getCenter(); }
215  
216   void setTransform( const MatrixF &xfm, F32 scale );
217
218   F32 getSqDistanceToPoint( const Point2F &point ) const;
219
220   void setData( ForestItemData *data );
221
222   const Box3F& getObjBox() const { return mDataBlock->getObjBox(); }
223
224   const Box3F& getWorldBox() const { return mWorldBox; }
225
226   Point3F getSize() const 
227   { 
228      if ( !mDataBlock )
229         return Point3F::One;
230
231      Box3F size = mDataBlock->getObjBox();
232      size.scale( mScale );
233      return size.getExtents();
234   }
235
236   ForestItemData* getData() const { return mDataBlock; };
237
238   inline bool canBillboard( const SceneRenderState *state, F32 distToCamera ) const
239   {
240      return mDataBlock && mDataBlock->canBillboard( state, *this, distToCamera );
241   }
242
243   /// Collision
244   /// @{
245   
246      bool castRay( const Point3F &start, const Point3F &end, RayInfo *outInfo, bool rendered ) const;         
247
248      bool buildPolyList( AbstractPolyList *polyList, const Box3F &box, const SphereF &sphere ) const;
249
250      //ForestConvex* buildConvex( const Box3F &box, Convex *convex ) const;
251
252   /// @}
253   
254};
255
256typedef Vector<ForestItem> ForestItemVector;
257
258
259inline F32 ForestItem::getSqDistanceToPoint( const Point2F &point ) const
260{
261   return ( getPosition().asPoint2F() - point ).lenSquared();
262}
263
264inline bool ForestItem::operator==(const ForestItem& _test) const
265{
266   return mKey == _test.mKey;
267}
268
269inline bool ForestItem::operator!=(const ForestItem& _test) const
270{
271   return mKey != _test.mKey;
272}
273
274
275#endif // _FORESTITEM_H_
276