forestWindEmitter.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 _FORESTWINDEMITTER_H_
  23. #define _FORESTWINDEMITTER_H_
  24. #ifndef _SCENEOBJECT_H_
  25. #include "scene/sceneObject.h"
  26. #endif
  27. #ifndef _MMATRIX_H_
  28. #include "math/mMatrix.h"
  29. #endif
  30. #ifndef _MPOINT3_H_
  31. #include "math/mPoint3.h"
  32. #endif
  33. #ifndef _MSPHERE_H_
  34. #include "math/mSphere.h"
  35. #endif
  36. #ifndef _TVECTOR_H_
  37. #include "core/util/tVector.h"
  38. #endif
  39. class ForestWindEmitter;
  40. class ForestWindAccumulator;
  41. class BaseMatInstance;
  42. class ForestWind
  43. {
  44. protected:
  45. F32 mStrength;
  46. VectorF mDirection;
  47. F32 mLastGustTime;
  48. F32 mLastYawTime;
  49. F32 mTargetYawAngle;
  50. F32 mCurrentInterp;
  51. Point2F mCurrentTarget;
  52. ForestWindEmitter *mParent;
  53. MRandom mRandom;
  54. bool mIsDirty;
  55. public:
  56. ForestWind( ForestWindEmitter *emitter );
  57. virtual ~ForestWind();
  58. void processTick();
  59. void setStrengthAndDirection( F32 strength, const VectorF &direction );
  60. void setStrength( F32 strength );
  61. void setDirection( const VectorF &direction );
  62. void setIsDirty( bool isDirty ) { mIsDirty = isDirty; }
  63. F32 getStrength() const { return mStrength; }
  64. const VectorF& getDirection() const { return mDirection; }
  65. VectorF getTarget() const { return VectorF( mCurrentTarget.x, mCurrentTarget.y, 0 ); }
  66. };
  67. /// A vector of WindEmitter pointers.
  68. typedef Vector<ForestWindEmitter*> ForestWindEmitterList;
  69. class ForestWindEmitter : public SceneObject
  70. {
  71. typedef SceneObject Parent;
  72. friend class ForestWind;
  73. friend class ForestWindAccumulator;
  74. protected:
  75. enum
  76. {
  77. EnabledMask = Parent::NextFreeMask << 0,
  78. WindMask = Parent::NextFreeMask << 1,
  79. NextFreeMask = Parent::NextFreeMask << 2
  80. };
  81. bool mAddedToScene;
  82. bool mEnabled;
  83. ForestWind *mWind;
  84. F32 mWindStrength;
  85. VectorF mWindDirection;
  86. /// Controls how often the wind gust peaks per second.
  87. F32 mWindGustFrequency;
  88. /// The maximum distance in meters that the peak wind
  89. /// gust will displace an element.
  90. F32 mWindGustStrength;
  91. /// The range that the wind direction
  92. /// will yaw between (-val to +val).
  93. F32 mWindGustYawAngle;
  94. /// The frequency, in seconds, between
  95. /// animations in the gust yaw angle.
  96. F32 mWindGustYawFrequency;
  97. /// The maximum amount of random wobble
  98. /// that will be applied to the gusting
  99. /// as well as the yaw rotation.
  100. F32 mWindGustWobbleStrength;
  101. /// Controls the overally rapidity of the wind turbulence.
  102. F32 mWindTurbulenceFrequency;
  103. /// The maximum distance in meters that the turbulence can
  104. /// displace a ground cover element.
  105. F32 mWindTurbulenceStrength;
  106. /// If the radius is greater than zero then this is
  107. /// a localized radial wind emitter.
  108. F32 mWindRadius;
  109. /// Explicitly denotes whether this is a
  110. /// global directional wind emitter or a
  111. /// localized radial wind emitter.
  112. bool mRadialEmitter;
  113. bool mHasMount;
  114. bool mIsMounted;
  115. /// An object that the emitter can
  116. /// get position updates from.
  117. SimObjectPtr<SceneObject> mMountObject;
  118. static ForestWindEmitterList smEmitters;
  119. void _initWind( U32 mask = 0xFFFFFFFF );
  120. void _onMountObjectGhostReceived( SceneObject *object );
  121. public:
  122. ForestWindEmitter( bool makeClientObject = false );
  123. virtual ~ForestWindEmitter();
  124. bool isEnabled() const { return mEnabled; }
  125. ForestWind* getWind() { return mWind; }
  126. bool isLocalWind() const { return mWindRadius > 0.0f; }
  127. bool isRadialEmitter() const { return mRadialEmitter; }
  128. F32 getWindRadius() const { return mWindRadius; }
  129. F32 getWindRadiusSquared() const { return mWindRadius * mWindRadius; }
  130. void setWindRadius( F32 radius ) { mWindRadius = radius; }
  131. F32 getStrength() const;
  132. void setStrength( F32 strength );
  133. void _renderEmitterInfo( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  134. void attachToObject( SceneObject *obj );
  135. void updateMountPosition();
  136. // SceneObject
  137. virtual void setTransform( const MatrixF &mat );
  138. void prepRenderImage( SceneRenderState *state );
  139. // SimObject
  140. bool onAdd();
  141. void onRemove();
  142. void inspectPostApply();
  143. void onEditorEnable();
  144. void onEditorDisable();
  145. void onDeleteNotify(SimObject *object);
  146. // NetObject
  147. U32 packUpdate( NetConnection *conn, U32 mask, BitStream *stream );
  148. void unpackUpdate( NetConnection *conn, BitStream *stream );
  149. // ConObject.
  150. static void initPersistFields();
  151. DECLARE_CONOBJECT(ForestWindEmitter);
  152. };
  153. #endif // _FORESTWINDEMITTER_H_