forestItem.h 7.1 KB

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