LWOAnimation.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2016, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file LWOAnimation.h
  34. * @brief LWOAnimationResolver utility class
  35. *
  36. * This is for all lightwave-related file format, not only LWO.
  37. * LWS isthe main purpose.
  38. */
  39. #ifndef AI_LWO_ANIMATION_INCLUDED
  40. #define AI_LWO_ANIMATION_INCLUDED
  41. #include <assimp/anim.h>
  42. #include <vector>
  43. #include <list>
  44. struct aiNodeAnim;
  45. namespace Assimp {
  46. namespace LWO {
  47. // ---------------------------------------------------------------------------
  48. /** \brief List of recognized LWO envelopes
  49. */
  50. enum EnvelopeType
  51. {
  52. EnvelopeType_Position_X = 0x1,
  53. EnvelopeType_Position_Y = 0x2,
  54. EnvelopeType_Position_Z = 0x3,
  55. EnvelopeType_Rotation_Heading = 0x4,
  56. EnvelopeType_Rotation_Pitch = 0x5,
  57. EnvelopeType_Rotation_Bank = 0x6,
  58. EnvelopeType_Scaling_X = 0x7,
  59. EnvelopeType_Scaling_Y = 0x8,
  60. EnvelopeType_Scaling_Z = 0x9,
  61. // -- currently not yet handled
  62. EnvelopeType_Color_R = 0xa,
  63. EnvelopeType_Color_G = 0xb,
  64. EnvelopeType_Color_B = 0xc,
  65. EnvelopeType_Falloff_X = 0xd,
  66. EnvelopeType_Falloff_Y = 0xe,
  67. EnvelopeType_Falloff_Z = 0xf,
  68. EnvelopeType_Unknown
  69. };
  70. // ---------------------------------------------------------------------------
  71. /** \brief List of recognized LWO interpolation modes
  72. */
  73. enum InterpolationType
  74. {
  75. IT_STEP, IT_LINE, IT_TCB, IT_HERM, IT_BEZI, IT_BEZ2
  76. };
  77. // ---------------------------------------------------------------------------
  78. /** \brief List of recognized LWO pre or post range behaviours
  79. */
  80. enum PrePostBehaviour
  81. {
  82. PrePostBehaviour_Reset = 0x0,
  83. PrePostBehaviour_Constant = 0x1,
  84. PrePostBehaviour_Repeat = 0x2,
  85. PrePostBehaviour_Oscillate = 0x3,
  86. PrePostBehaviour_OffsetRepeat = 0x4,
  87. PrePostBehaviour_Linear = 0x5
  88. };
  89. // ---------------------------------------------------------------------------
  90. /** \brief Data structure for a LWO animation keyframe
  91. */
  92. struct Key
  93. {
  94. Key()
  95. : time(),
  96. value(),
  97. inter (IT_LINE),
  98. params()
  99. {}
  100. //! Current time
  101. double time;
  102. //! Current value
  103. float value;
  104. //! How to interpolate this key with previous key?
  105. InterpolationType inter;
  106. //! Interpolation parameters
  107. float params[5];
  108. // for std::find()
  109. operator double () {
  110. return time;
  111. }
  112. };
  113. // ---------------------------------------------------------------------------
  114. /** \brief Data structure for a LWO animation envelope
  115. */
  116. struct Envelope
  117. {
  118. Envelope()
  119. : index()
  120. , type (EnvelopeType_Unknown)
  121. , pre (PrePostBehaviour_Constant)
  122. , post (PrePostBehaviour_Constant)
  123. , old_first (0)
  124. , old_last (0)
  125. {}
  126. //! Index of this envelope
  127. unsigned int index;
  128. //! Type of envelope
  129. EnvelopeType type;
  130. //! Pre and post-behaviour
  131. PrePostBehaviour pre,post;
  132. //! Keyframes for this envelope
  133. std::vector<Key> keys;
  134. // temporary data for AnimResolver
  135. size_t old_first,old_last;
  136. };
  137. // ---------------------------------------------------------------------------
  138. //! @def AI_LWO_ANIM_FLAG_SAMPLE_ANIMS
  139. //! Flag for AnimResolver, subsamples the input data with the rate specified
  140. //! by AnimResolver::SetSampleRate().
  141. #define AI_LWO_ANIM_FLAG_SAMPLE_ANIMS 0x1
  142. // ---------------------------------------------------------------------------
  143. //! @def AI_LWO_ANIM_FLAG_START_AT_ZERO
  144. //! Flag for AnimResolver, ensures that the animations starts at zero.
  145. #define AI_LWO_ANIM_FLAG_START_AT_ZERO 0x2
  146. // ---------------------------------------------------------------------------
  147. /** @brief Utility class to build Assimp animations from LWO envelopes.
  148. *
  149. * Used for both LWO and LWS (MOT also).
  150. */
  151. class AnimResolver
  152. {
  153. public:
  154. // ------------------------------------------------------------------
  155. /** @brief Construct an AnimResolver from a given list of envelopes
  156. * @param envelopes Input envelopes. May be empty.
  157. * @param Output tick rate, per second
  158. * @note The input envelopes are possibly modified.
  159. */
  160. AnimResolver(std::list<Envelope>& envelopes,
  161. double tick);
  162. public:
  163. // ------------------------------------------------------------------
  164. /** @brief Extract the bind-pose transformation matrix.
  165. * @param out Receives bind-pose transformation matrix
  166. */
  167. void ExtractBindPose(aiMatrix4x4& out);
  168. // ------------------------------------------------------------------
  169. /** @brief Extract a node animation channel
  170. * @param out Receives a pointer to a newly allocated node anim.
  171. * If there's just one keyframe defined, *out is set to NULL and
  172. * no animation channel is computed.
  173. * @param flags Any combination of the AI_LWO_ANIM_FLAG_XXX flags.
  174. */
  175. void ExtractAnimChannel(aiNodeAnim** out, unsigned int flags = 0);
  176. // ------------------------------------------------------------------
  177. /** @brief Set the sampling rate for ExtractAnimChannel().
  178. *
  179. * Non-linear interpolations are subsampled with this rate (keys
  180. * per second). Closer sampling positions, if existent, are kept.
  181. * The sampling rate defaults to 0, if this value is not changed and
  182. * AI_LWO_ANIM_FLAG_SAMPLE_ANIMS is specified for ExtractAnimChannel(),
  183. * the class finds a suitable sample rate by itself.
  184. */
  185. void SetSampleRate(double sr) {
  186. sample_rate = sr;
  187. }
  188. // ------------------------------------------------------------------
  189. /** @brief Getter for SetSampleRate()
  190. */
  191. double GetSampleRate() const {
  192. return sample_rate;
  193. }
  194. // ------------------------------------------------------------------
  195. /** @brief Set the animation time range
  196. *
  197. * @param first Time where the animation starts, in ticks
  198. * @param last Time where the animation ends, in ticks
  199. */
  200. void SetAnimationRange(double _first, double _last) {
  201. first = _first;
  202. last = _last;
  203. ClearAnimRangeSetup();
  204. UpdateAnimRangeSetup();
  205. }
  206. protected:
  207. // ------------------------------------------------------------------
  208. /** @brief Build linearly subsampled keys from 3 single envelopes
  209. * @param out Receives output keys
  210. * @param envl_x X-component envelope
  211. * @param envl_y Y-component envelope
  212. * @param envl_z Z-component envelope
  213. * @param flags Any combination of the AI_LWO_ANIM_FLAG_XXX flags.
  214. * @note Up to two input envelopes may be NULL
  215. */
  216. void GetKeys(std::vector<aiVectorKey>& out,
  217. LWO::Envelope* envl_x,
  218. LWO::Envelope* envl_y,
  219. LWO::Envelope* envl_z,
  220. unsigned int flags);
  221. // ------------------------------------------------------------------
  222. /** @brief Resolve a single animation key by applying the right
  223. * interpolation to it.
  224. * @param cur Current key
  225. * @param envl Envelope working on
  226. * @param time time to be interpolated
  227. * @param fill Receives the interpolated output value.
  228. */
  229. void DoInterpolation(std::vector<LWO::Key>::const_iterator cur,
  230. LWO::Envelope* envl,double time, float& fill);
  231. // ------------------------------------------------------------------
  232. /** @brief Almost the same, except we won't handle pre/post
  233. * conditions here.
  234. * @see DoInterpolation
  235. */
  236. void DoInterpolation2(std::vector<LWO::Key>::const_iterator beg,
  237. std::vector<LWO::Key>::const_iterator end,double time, float& fill);
  238. // ------------------------------------------------------------------
  239. /** @brief Interpolate 2 tracks if one is given
  240. *
  241. * @param out Receives extra output keys
  242. * @param key_out Primary output key
  243. * @param time Time to interpolate for
  244. */
  245. void InterpolateTrack(std::vector<aiVectorKey>& out,
  246. aiVectorKey& key_out,double time);
  247. // ------------------------------------------------------------------
  248. /** @brief Subsample an animation track by a given sampling rate
  249. *
  250. * @param out Receives output keys. Last key at input defines the
  251. * time where subsampling starts.
  252. * @param time Time to end subsampling at
  253. * @param sample_delta Time delta between two samples
  254. */
  255. void SubsampleAnimTrack(std::vector<aiVectorKey>& out,
  256. double time,double sample_delta);
  257. // ------------------------------------------------------------------
  258. /** @brief Delete all keys which we inserted to match anim setup
  259. */
  260. void ClearAnimRangeSetup();
  261. // ------------------------------------------------------------------
  262. /** @brief Insert extra keys to match LWO's pre and post behaviours
  263. * in a given time range [first...last]
  264. */
  265. void UpdateAnimRangeSetup();
  266. private:
  267. std::list<Envelope>& envelopes;
  268. double sample_rate;
  269. LWO::Envelope* trans_x, *trans_y, *trans_z;
  270. LWO::Envelope* rotat_x, *rotat_y, *rotat_z;
  271. LWO::Envelope* scale_x, *scale_y, *scale_z;
  272. double first, last;
  273. bool need_to_setup;
  274. // temporary storage
  275. LWO::Envelope* envl_x, * envl_y, * envl_z;
  276. std::vector<LWO::Key>::const_iterator cur_x,cur_y,cur_z;
  277. bool end_x, end_y, end_z;
  278. unsigned int flags;
  279. double sample_delta;
  280. };
  281. } // end namespace LWO
  282. } // end namespace Assimp
  283. #endif // !! AI_LWO_ANIMATION_INCLUDED