forestItem.h 7.0 KB

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