afxXM_Spin.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #include "afx/arcaneFX.h"
  25. #include "math/mathIO.h"
  26. #include "math/mathUtils.h"
  27. #include "afx/afxEffectWrapper.h"
  28. #include "afx/afxChoreographer.h"
  29. #include "afx/xm/afxXfmMod.h"
  30. #include "afx/util/afxPath3D.h"
  31. #include "afx/util/afxPath.h"
  32. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  33. class afxXM_SpinData : public afxXM_WeightedBaseData
  34. {
  35. typedef afxXM_WeightedBaseData Parent;
  36. public:
  37. Point3F spin_axis;
  38. F32 spin_angle;
  39. F32 spin_angle_var;
  40. F32 spin_rate;
  41. F32 spin_rate_var;
  42. public:
  43. /*C*/ afxXM_SpinData() : spin_axis(0,0,1), spin_angle(0), spin_angle_var(0), spin_rate(0), spin_rate_var(0) { }
  44. /*C*/ afxXM_SpinData(const afxXM_SpinData&, bool = false);
  45. void packData(BitStream* stream);
  46. void unpackData(BitStream* stream);
  47. bool onAdd();
  48. virtual bool allowSubstitutions() const { return true; }
  49. static void initPersistFields();
  50. afxXM_Base* create(afxEffectWrapper* fx, bool on_server);
  51. DECLARE_CONOBJECT(afxXM_SpinData);
  52. DECLARE_CATEGORY("AFX");
  53. };
  54. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  55. class afxXM_Spin_weighted : public afxXM_WeightedBase
  56. {
  57. typedef afxXM_WeightedBase Parent;
  58. Point3F spin_axis;
  59. F32 spin_rate;
  60. F32 theta;
  61. public:
  62. /*C*/ afxXM_Spin_weighted(afxXM_SpinData*, afxEffectWrapper*);
  63. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  64. };
  65. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  66. class afxXM_Spin_fixed : public afxXM_Base
  67. {
  68. typedef afxXM_Base Parent;
  69. Point3F spin_axis;
  70. F32 spin_rate;
  71. F32 theta;
  72. public:
  73. /*C*/ afxXM_Spin_fixed(afxXM_SpinData*, afxEffectWrapper*);
  74. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  75. };
  76. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  77. IMPLEMENT_CO_DATABLOCK_V1(afxXM_SpinData);
  78. ConsoleDocClass( afxXM_SpinData,
  79. "@brief An xmod datablock.\n\n"
  80. "@ingroup afxXMods\n"
  81. "@ingroup AFX\n"
  82. "@ingroup Datablocks\n"
  83. );
  84. afxXM_SpinData::afxXM_SpinData(const afxXM_SpinData& other, bool temp_clone) : afxXM_WeightedBaseData(other, temp_clone)
  85. {
  86. spin_axis = other.spin_axis;
  87. spin_angle = other.spin_angle;
  88. spin_angle_var = other.spin_angle_var;
  89. spin_rate = other.spin_rate;
  90. spin_rate_var = other.spin_rate_var;
  91. }
  92. void afxXM_SpinData::initPersistFields()
  93. {
  94. addField("spinAxis", TypePoint3F, Offset(spin_axis, afxXM_SpinData),
  95. "...");
  96. addField("spinAngle", TypeF32, Offset(spin_angle, afxXM_SpinData),
  97. "...");
  98. addField("spinAngleVariance", TypeF32, Offset(spin_angle_var, afxXM_SpinData),
  99. "...");
  100. addField("spinRate", TypeF32, Offset(spin_rate, afxXM_SpinData),
  101. "...");
  102. addField("spinRateVariance", TypeF32, Offset(spin_rate_var, afxXM_SpinData),
  103. "...");
  104. Parent::initPersistFields();
  105. }
  106. void afxXM_SpinData::packData(BitStream* stream)
  107. {
  108. Parent::packData(stream);
  109. mathWrite(*stream, spin_axis);
  110. stream->write(spin_angle);
  111. stream->write(spin_angle_var);
  112. stream->write(spin_rate);
  113. stream->write(spin_rate_var);
  114. }
  115. void afxXM_SpinData::unpackData(BitStream* stream)
  116. {
  117. Parent::unpackData(stream);
  118. mathRead(*stream, &spin_axis);
  119. stream->read(&spin_angle);
  120. stream->read(&spin_angle_var);
  121. stream->read(&spin_rate);
  122. stream->read(&spin_rate_var);
  123. }
  124. bool afxXM_SpinData::onAdd()
  125. {
  126. if (Parent::onAdd() == false)
  127. return false;
  128. spin_axis.normalizeSafe();
  129. return true;
  130. }
  131. afxXM_Base* afxXM_SpinData::create(afxEffectWrapper* fx, bool on_server)
  132. {
  133. afxXM_SpinData* datablock = this;
  134. if (getSubstitutionCount() > 0)
  135. {
  136. datablock = new afxXM_SpinData(*this, true);
  137. this->performSubstitutions(datablock, fx->getChoreographer(), fx->getGroupIndex());
  138. }
  139. if (datablock->hasFixedWeight())
  140. return new afxXM_Spin_fixed(datablock, fx);
  141. else
  142. return new afxXM_Spin_weighted(datablock, fx);
  143. }
  144. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  145. afxXM_Spin_weighted::afxXM_Spin_weighted(afxXM_SpinData* db, afxEffectWrapper* fxw)
  146. : afxXM_WeightedBase(db, fxw)
  147. {
  148. spin_axis = db->spin_axis;
  149. spin_rate = db->spin_rate;
  150. if (db->spin_rate_var != 0.0f)
  151. spin_rate += gRandGen.randF()*2.0f*db->spin_rate_var - db->spin_rate_var;
  152. spin_rate *= db->getWeightFactor()/time_factor;
  153. F32 spin_angle = db->spin_angle;
  154. if (db->spin_angle_var != 0.0f)
  155. spin_angle += gRandGen.randF()*2.0f*db->spin_angle_var - db->spin_angle_var;
  156. theta = mDegToRad(spin_angle);
  157. }
  158. void afxXM_Spin_weighted::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  159. {
  160. F32 wt_factor = calc_weight_factor(elapsed);
  161. F32 rate = spin_rate*wt_factor;
  162. theta += mDegToRad(dt*rate);
  163. AngAxisF spin_aa(spin_axis, theta);
  164. MatrixF spin_xfm; spin_aa.setMatrix(&spin_xfm);
  165. params.ori.mul(spin_xfm);
  166. }
  167. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  168. afxXM_Spin_fixed::afxXM_Spin_fixed(afxXM_SpinData* db, afxEffectWrapper* fxw)
  169. : afxXM_Base(db, fxw)
  170. {
  171. spin_axis = db->spin_axis;
  172. spin_rate = db->spin_rate;
  173. if (db->spin_rate_var != 0.0f)
  174. spin_rate += gRandGen.randF()*2.0f*db->spin_rate_var - db->spin_rate_var;
  175. spin_rate *= db->getWeightFactor()/time_factor;
  176. F32 spin_angle = db->spin_angle;
  177. if (db->spin_angle_var != 0.0f)
  178. spin_angle += gRandGen.randF()*2.0f*db->spin_angle_var - db->spin_angle_var;
  179. theta = mDegToRad(spin_angle);
  180. }
  181. void afxXM_Spin_fixed::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  182. {
  183. theta += mDegToRad(dt*spin_rate);
  184. AngAxisF spin_aa(spin_axis, theta);
  185. MatrixF spin_xfm; spin_aa.setMatrix(&spin_xfm);
  186. params.ori.mul(spin_xfm);
  187. }
  188. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//