forestItem.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. void onNameChange(const char *name) override;
  85. bool onAdd() override;
  86. void packData(BitStream* stream) override;
  87. void unpackData(BitStream* stream) override;
  88. /// Called from Forest the first time a datablock is used
  89. /// in order to lazy load content.
  90. bool preload(bool server, String& errorStr) override { return false; }; // we don't ghost ForestItemData specifically. we do do so for TSForestItemData
  91. void preload()
  92. {
  93. if (!mNeedPreload)
  94. return;
  95. _preload();
  96. mNeedPreload = false;
  97. }
  98. virtual const Box3F& getObjBox() const { return Box3F::Invalid; }
  99. virtual bool render( TSRenderState *rdata, const ForestItem &item ) const { return false; }
  100. virtual bool canBillboard( const SceneRenderState *state, const ForestItem &item, F32 distToCamera ) const { return false; }
  101. virtual ForestCellBatch* allocateBatch() const { return NULL; }
  102. typedef Signal<void(void)> ReloadSignal;
  103. static ReloadSignal& getReloadSignal()
  104. {
  105. static ReloadSignal theSignal;
  106. return theSignal;
  107. }
  108. void onShapeChanged()
  109. {
  110. reloadOnLocalClient();
  111. }
  112. };
  113. typedef Vector<ForestItemData*> ForestItemDataVector;
  114. typedef U32 ForestItemKey;
  115. class ForestItem
  116. {
  117. protected:
  118. ForestItemData *mDataBlock;
  119. // The unique identifier used when querying forest items.
  120. ForestItemKey mKey;
  121. MatrixF mTransform;
  122. F32 mScale;
  123. F32 mRadius;
  124. Box3F mWorldBox;
  125. // JCFHACK: change this to an abstract physics-rep.
  126. //NxActor *mActor;
  127. /// If we're currently being effected by one or
  128. /// more wind emitters then we hold the results
  129. /// in this class.
  130. //ForestWindAccumulator *mWind;
  131. public:
  132. // Constructs an invalid item.
  133. ForestItem();
  134. // Note: We keep this non-virtual to save vtable space.
  135. ~ForestItem();
  136. static const ForestItem Invalid;
  137. // Comparison operators with other ForestItems.
  138. // Note that this compares only the ForestItemKey, we are not validating
  139. // that any other data like the position actually matches.
  140. bool operator==(const ForestItem&) const;
  141. bool operator!=(const ForestItem&) const;
  142. /// Returns true if this is a valid item.
  143. bool isValid() const { return mKey != 0; }
  144. /// Invalidates the item.
  145. void makeInvalid() { mKey = 0; }
  146. const ForestItemKey& getKey() const { return mKey; };
  147. void setKey( const ForestItemKey &key ) { mKey = key; }
  148. Point3F getPosition() const { return mTransform.getPosition(); }
  149. const MatrixF& getTransform() const { return mTransform; }
  150. F32 getScale() const { return mScale; }
  151. F32 getRadius() const { return mRadius; }
  152. Point3F getCenterPoint() const { return mWorldBox.getCenter(); }
  153. void setTransform( const MatrixF &xfm, F32 scale );
  154. F32 getSqDistanceToPoint( const Point2F &point ) const;
  155. void setData( ForestItemData *data );
  156. const Box3F& getObjBox() const { return mDataBlock->getObjBox(); }
  157. const Box3F& getWorldBox() const { return mWorldBox; }
  158. Point3F getSize() const
  159. {
  160. if ( !mDataBlock )
  161. return Point3F::One;
  162. Box3F size = mDataBlock->getObjBox();
  163. size.scale( mScale );
  164. return size.getExtents();
  165. }
  166. ForestItemData* getData() const { return mDataBlock; };
  167. inline bool canBillboard( const SceneRenderState *state, F32 distToCamera ) const
  168. {
  169. return mDataBlock && mDataBlock->canBillboard( state, *this, distToCamera );
  170. }
  171. /// Collision
  172. /// @{
  173. bool castRay( const Point3F &start, const Point3F &end, RayInfo *outInfo, bool rendered ) const;
  174. bool buildPolyList( AbstractPolyList *polyList, const Box3F &box, const SphereF &sphere ) const;
  175. //ForestConvex* buildConvex( const Box3F &box, Convex *convex ) const;
  176. /// @}
  177. };
  178. typedef Vector<ForestItem> ForestItemVector;
  179. inline F32 ForestItem::getSqDistanceToPoint( const Point2F &point ) const
  180. {
  181. return ( getPosition().asPoint2F() - point ).lenSquared();
  182. }
  183. inline bool ForestItem::operator==(const ForestItem& _test) const
  184. {
  185. return mKey == _test.mKey;
  186. }
  187. inline bool ForestItem::operator!=(const ForestItem& _test) const
  188. {
  189. return mKey != _test.mKey;
  190. }
  191. #endif // _FORESTITEM_H_