afxXM_WaveBase.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  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 _AFX_XFM_WAVE_BASE_H_
  25. #define _AFX_XFM_WAVE_BASE_H_
  26. #include "afx/xm/afxXfmMod.h"
  27. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  28. // WAVEFORM
  29. class afxXM_Waveform
  30. {
  31. public:
  32. virtual F32 evaluate(F32 t) = 0;
  33. };
  34. //~~~~~~~~~~~~~~~~~~~~//
  35. class afxXM_WaveformSine : public afxXM_Waveform
  36. {
  37. public:
  38. F32 evaluate(F32 t) override;
  39. };
  40. class afxXM_WaveformSquare : public afxXM_Waveform
  41. {
  42. public:
  43. F32 evaluate(F32 t) override;
  44. };
  45. class afxXM_WaveformTriangle : public afxXM_Waveform
  46. {
  47. public:
  48. F32 evaluate(F32 t) override;
  49. };
  50. class afxXM_WaveformSawtooth : public afxXM_Waveform
  51. {
  52. public:
  53. F32 evaluate(F32 t) override { return t; }
  54. };
  55. class afxXM_WaveformNoise : public afxXM_Waveform
  56. {
  57. public:
  58. F32 evaluate(F32 t) override { return gRandGen.randF(); };
  59. };
  60. class afxXM_WaveformOne : public afxXM_Waveform
  61. {
  62. public:
  63. F32 evaluate(F32 t) override { return 1.0f; };
  64. };
  65. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  66. // WAVE INTERPOLATOR
  67. class afxXM_WaveInterp
  68. {
  69. public:
  70. virtual ~afxXM_WaveInterp() { }
  71. virtual void interpolate(F32 t, afxXM_Params& params)=0;
  72. virtual void pulse()=0;
  73. };
  74. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  75. // WAVE BASE DATABLOCK
  76. class afxXM_WaveBaseData : public afxXM_WeightedBaseData
  77. {
  78. typedef afxXM_WeightedBaseData Parent;
  79. friend class afxXM_WaveRiderBaseData;
  80. public:
  81. enum WaveFormType
  82. {
  83. WAVEFORM_NONE = 0,
  84. WAVEFORM_SINE,
  85. WAVEFORM_SQUARE,
  86. WAVEFORM_TRIANGLE,
  87. WAVEFORM_SAWTOOTH,
  88. WAVEFORM_NOISE,
  89. WAVEFORM_ONE,
  90. WAVEFORM_BITS = 3
  91. };
  92. enum WaveOpType
  93. {
  94. OP_ADD = 0,
  95. OP_MULTIPLY,
  96. OP_REPLACE,
  97. OP_BITS = 2
  98. };
  99. enum WaveParamType
  100. {
  101. PARAM_NONE = 0,
  102. PARAM_POS,
  103. PARAM_POS_X,
  104. PARAM_POS_Y,
  105. PARAM_POS_Z,
  106. PARAM_ORI,
  107. PARAM_POS2,
  108. PARAM_POS2_X,
  109. PARAM_POS2_Y,
  110. PARAM_POS2_Z,
  111. PARAM_SCALE,
  112. PARAM_SCALE_X,
  113. PARAM_SCALE_Y,
  114. PARAM_SCALE_Z,
  115. PARAM_COLOR,
  116. PARAM_COLOR_R,
  117. PARAM_COLOR_G,
  118. PARAM_COLOR_B,
  119. PARAM_COLOR_A,
  120. PARAM_VIS,
  121. PARAM_BITS = 5,
  122. };
  123. U32 waveform_type;
  124. U32 parameter;
  125. U32 op;
  126. F32 speed;
  127. F32 speed_vari;
  128. F32 accel;
  129. F32 phase_shift;
  130. F32 duty_cycle;
  131. F32 duty_shift;
  132. F32 off_duty_t;
  133. ByteRange waves_per_pulse;
  134. ByteRange waves_per_rest;
  135. F32 rest_dur;
  136. F32 rest_dur_vari;
  137. Point3F axis;
  138. bool local_axis;
  139. public:
  140. /*C*/ afxXM_WaveBaseData();
  141. /*C*/ afxXM_WaveBaseData(const afxXM_WaveBaseData&, bool = false);
  142. void packData(BitStream* stream) override;
  143. void unpackData(BitStream* stream) override;
  144. static void initPersistFields();
  145. static void initParamInfo(U32 parameter, U32& parambit, S32& component);
  146. static afxXM_Waveform* getWaveform(U32 waveform_type);
  147. DECLARE_CONOBJECT(afxXM_WaveBaseData);
  148. };
  149. typedef afxXM_WaveBaseData::WaveFormType afxXM_WaveFormType;
  150. DefineEnumType( afxXM_WaveFormType );
  151. typedef afxXM_WaveBaseData::WaveOpType afxXM_WaveOpType;
  152. DefineEnumType( afxXM_WaveOpType );
  153. typedef afxXM_WaveBaseData::WaveParamType afxXM_WaveParamType;
  154. DefineEnumType( afxXM_WaveParamType );
  155. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  156. // WAVE RIDER BASE DATABLOCK
  157. class afxXM_WaveRiderBaseData : public afxXM_WeightedBaseData
  158. {
  159. typedef afxXM_WeightedBaseData Parent;
  160. public:
  161. U32 waveform_type;
  162. F32 off_duty_t;
  163. U32 parameter;
  164. U32 op;
  165. Point3F axis;
  166. bool local_axis;
  167. public:
  168. /*C*/ afxXM_WaveRiderBaseData();
  169. /*C*/ afxXM_WaveRiderBaseData(const afxXM_WaveRiderBaseData&, bool = false);
  170. void packData(BitStream* stream) override;
  171. void unpackData(BitStream* stream) override;
  172. static void initPersistFields();
  173. DECLARE_CONOBJECT(afxXM_WaveRiderBaseData);
  174. };
  175. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  176. // WAVE BASE
  177. class afxXM_WaveBase : public afxXM_WeightedBase
  178. {
  179. typedef afxXM_WeightedBase Parent;
  180. friend class afxXM_WaveRiderBase;
  181. protected:
  182. static bool last_was_pulsed;
  183. static bool last_was_off_duty;
  184. static F32 last_t;
  185. static F32 last_wave_t;
  186. afxXM_WaveInterp* interpolator;
  187. protected:
  188. afxXM_Waveform* waveform;
  189. afxXM_WaveBaseData* db;
  190. F32 speed;
  191. bool fixed_weight;
  192. bool speed_is_randomized;
  193. bool is_resting;
  194. F32 cur_pulse_time;
  195. F32 next_pulse_time;
  196. F32 calc_initial_speed();
  197. F32 calc_new_speed();
  198. F32 calc_new_restDur();
  199. S32 calc_new_wavesPerPulse();
  200. S32 calc_new_wavesPerRest();
  201. public:
  202. /*C*/ afxXM_WaveBase(afxXM_WaveBaseData*, afxEffectWrapper*, afxXM_WaveInterp*);
  203. /*D*/ virtual ~afxXM_WaveBase();
  204. void updateParams(F32 dt, F32 elapsed, afxXM_Params& params) override;
  205. };
  206. inline F32 afxXM_WaveBase::calc_initial_speed()
  207. {
  208. return (!speed_is_randomized) ?
  209. db->speed :
  210. (db->speed + gRandGen.randF()*2.0f*db->speed_vari - db->speed_vari);
  211. }
  212. inline F32 afxXM_WaveBase::calc_new_speed()
  213. {
  214. return mClampF((!speed_is_randomized) ?
  215. (speed + speed*db->accel) :
  216. (db->speed + gRandGen.randF()*2.0f*db->speed_vari - db->speed_vari), 0.001f, 200.0f);
  217. }
  218. inline F32 afxXM_WaveBase::calc_new_restDur()
  219. {
  220. return db->rest_dur + gRandGen.randF()*2.0f*db->rest_dur_vari - db->rest_dur_vari;
  221. }
  222. inline S32 afxXM_WaveBase::calc_new_wavesPerPulse()
  223. {
  224. return (db->waves_per_pulse.getSpan() == 0) ?
  225. db->waves_per_pulse.low :
  226. gRandGen.randI(db->waves_per_pulse.low, db->waves_per_pulse.high);
  227. }
  228. inline S32 afxXM_WaveBase::calc_new_wavesPerRest()
  229. {
  230. return (db->waves_per_rest.getSpan() == 0) ?
  231. db->waves_per_rest.low :
  232. gRandGen.randI(db->waves_per_rest.low, db->waves_per_rest.high);
  233. }
  234. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  235. // WAVE RIDER BASE
  236. class afxXM_WaveRiderBase : public afxXM_WeightedBase
  237. {
  238. typedef afxXM_WeightedBase Parent;
  239. protected:
  240. afxXM_WaveInterp* interpolator;
  241. afxXM_WaveRiderBaseData* db;
  242. afxXM_Waveform* waveform;
  243. bool fixed_weight;
  244. public:
  245. /*C*/ afxXM_WaveRiderBase(afxXM_WaveRiderBaseData*, afxEffectWrapper*, afxXM_WaveInterp*);
  246. /*D*/ ~afxXM_WaveRiderBase();
  247. void updateParams(F32 dt, F32 elapsed, afxXM_Params& params) override;
  248. };
  249. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  250. #endif // _AFX_XFM_WAVE_BASE_H_