afxXM_WaveBase.h 8.5 KB

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