afxXM_WaveBase.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. };
  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);
  143. void unpackData(BitStream* stream);
  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. DECLARE_CATEGORY("AFX");
  149. };
  150. typedef afxXM_WaveBaseData::WaveFormType afxXM_WaveFormType;
  151. DefineEnumType( afxXM_WaveFormType );
  152. typedef afxXM_WaveBaseData::WaveOpType afxXM_WaveOpType;
  153. DefineEnumType( afxXM_WaveOpType );
  154. typedef afxXM_WaveBaseData::WaveParamType afxXM_WaveParamType;
  155. DefineEnumType( afxXM_WaveParamType );
  156. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  157. // WAVE RIDER BASE DATABLOCK
  158. class afxXM_WaveRiderBaseData : public afxXM_WeightedBaseData
  159. {
  160. typedef afxXM_WeightedBaseData Parent;
  161. public:
  162. U32 waveform_type;
  163. F32 off_duty_t;
  164. U32 parameter;
  165. U32 op;
  166. Point3F axis;
  167. bool local_axis;
  168. public:
  169. /*C*/ afxXM_WaveRiderBaseData();
  170. /*C*/ afxXM_WaveRiderBaseData(const afxXM_WaveRiderBaseData&, bool = false);
  171. void packData(BitStream* stream);
  172. void unpackData(BitStream* stream);
  173. static void initPersistFields();
  174. DECLARE_CONOBJECT(afxXM_WaveRiderBaseData);
  175. DECLARE_CATEGORY("AFX");
  176. };
  177. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  178. // WAVE BASE
  179. class afxXM_WaveBase : public afxXM_WeightedBase
  180. {
  181. typedef afxXM_WeightedBase Parent;
  182. friend class afxXM_WaveRiderBase;
  183. protected:
  184. static bool last_was_pulsed;
  185. static bool last_was_off_duty;
  186. static F32 last_t;
  187. static F32 last_wave_t;
  188. afxXM_WaveInterp* interpolator;
  189. protected:
  190. afxXM_Waveform* waveform;
  191. afxXM_WaveBaseData* db;
  192. F32 speed;
  193. bool fixed_weight;
  194. bool speed_is_randomized;
  195. bool is_resting;
  196. F32 cur_pulse_time;
  197. F32 next_pulse_time;
  198. F32 calc_initial_speed();
  199. F32 calc_new_speed();
  200. F32 calc_new_restDur();
  201. S32 calc_new_wavesPerPulse();
  202. S32 calc_new_wavesPerRest();
  203. public:
  204. /*C*/ afxXM_WaveBase(afxXM_WaveBaseData*, afxEffectWrapper*, afxXM_WaveInterp*);
  205. /*D*/ virtual ~afxXM_WaveBase();
  206. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  207. };
  208. inline F32 afxXM_WaveBase::calc_initial_speed()
  209. {
  210. return (!speed_is_randomized) ?
  211. db->speed :
  212. (db->speed + gRandGen.randF()*2.0f*db->speed_vari - db->speed_vari);
  213. }
  214. inline F32 afxXM_WaveBase::calc_new_speed()
  215. {
  216. return mClampF((!speed_is_randomized) ?
  217. (speed + speed*db->accel) :
  218. (db->speed + gRandGen.randF()*2.0f*db->speed_vari - db->speed_vari), 0.001f, 200.0f);
  219. }
  220. inline F32 afxXM_WaveBase::calc_new_restDur()
  221. {
  222. return db->rest_dur + gRandGen.randF()*2.0f*db->rest_dur_vari - db->rest_dur_vari;
  223. }
  224. inline S32 afxXM_WaveBase::calc_new_wavesPerPulse()
  225. {
  226. return (db->waves_per_pulse.getSpan() == 0) ?
  227. db->waves_per_pulse.low :
  228. gRandGen.randI(db->waves_per_pulse.low, db->waves_per_pulse.high);
  229. }
  230. inline S32 afxXM_WaveBase::calc_new_wavesPerRest()
  231. {
  232. return (db->waves_per_rest.getSpan() == 0) ?
  233. db->waves_per_rest.low :
  234. gRandGen.randI(db->waves_per_rest.low, db->waves_per_rest.high);
  235. }
  236. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  237. // WAVE RIDER BASE
  238. class afxXM_WaveRiderBase : public afxXM_WeightedBase
  239. {
  240. typedef afxXM_WeightedBase Parent;
  241. protected:
  242. afxXM_WaveInterp* interpolator;
  243. afxXM_WaveRiderBaseData* db;
  244. afxXM_Waveform* waveform;
  245. bool fixed_weight;
  246. public:
  247. /*C*/ afxXM_WaveRiderBase(afxXM_WaveRiderBaseData*, afxEffectWrapper*, afxXM_WaveInterp*);
  248. /*D*/ ~afxXM_WaveRiderBase();
  249. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  250. };
  251. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  252. #endif // _AFX_XFM_WAVE_BASE_H_