You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

337 lines
10 KiB

  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, 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. namespace Assimp {
  42. namespace LWO {
  43. // ---------------------------------------------------------------------------
  44. /** \brief List of recognized LWO envelopes
  45. */
  46. enum EnvelopeType
  47. {
  48. EnvelopeType_Position_X = 0x1,
  49. EnvelopeType_Position_Y = 0x2,
  50. EnvelopeType_Position_Z = 0x3,
  51. EnvelopeType_Rotation_Heading = 0x4,
  52. EnvelopeType_Rotation_Pitch = 0x5,
  53. EnvelopeType_Rotation_Bank = 0x6,
  54. EnvelopeType_Scaling_X = 0x7,
  55. EnvelopeType_Scaling_Y = 0x8,
  56. EnvelopeType_Scaling_Z = 0x9,
  57. // -- currently not yet handled
  58. EnvelopeType_Color_R = 0xa,
  59. EnvelopeType_Color_G = 0xb,
  60. EnvelopeType_Color_B = 0xc,
  61. EnvelopeType_Falloff_X = 0xd,
  62. EnvelopeType_Falloff_Y = 0xe,
  63. EnvelopeType_Falloff_Z = 0xf,
  64. EnvelopeType_Unknown
  65. };
  66. // ---------------------------------------------------------------------------
  67. /** \brief List of recognized LWO interpolation modes
  68. */
  69. enum InterpolationType
  70. {
  71. IT_STEP, IT_LINE, IT_TCB, IT_HERM, IT_BEZI, IT_BEZ2
  72. };
  73. // ---------------------------------------------------------------------------
  74. /** \brief List of recognized LWO pre or post range behaviours
  75. */
  76. enum PrePostBehaviour
  77. {
  78. PrePostBehaviour_Reset = 0x0,
  79. PrePostBehaviour_Constant = 0x1,
  80. PrePostBehaviour_Repeat = 0x2,
  81. PrePostBehaviour_Oscillate = 0x3,
  82. PrePostBehaviour_OffsetRepeat = 0x4,
  83. PrePostBehaviour_Linear = 0x5
  84. };
  85. // ---------------------------------------------------------------------------
  86. /** \brief Data structure for a LWO animation keyframe
  87. */
  88. struct Key
  89. {
  90. Key()
  91. : inter (IT_LINE)
  92. {}
  93. //! Current time
  94. double time;
  95. //! Current value
  96. float value;
  97. //! How to interpolate this key with previous key?
  98. InterpolationType inter;
  99. //! Interpolation parameters
  100. float params[5];
  101. // for std::find()
  102. operator double () {
  103. return time;
  104. }
  105. };
  106. // ---------------------------------------------------------------------------
  107. /** \brief Data structure for a LWO animation envelope
  108. */
  109. struct Envelope
  110. {
  111. Envelope()
  112. : type (EnvelopeType_Unknown)
  113. , pre (PrePostBehaviour_Constant)
  114. , post (PrePostBehaviour_Constant)
  115. , old_first (0)
  116. , old_last (0)
  117. {}
  118. //! Index of this envelope
  119. unsigned int index;
  120. //! Type of envelope
  121. EnvelopeType type;
  122. //! Pre and post-behaviour
  123. PrePostBehaviour pre,post;
  124. //! Keyframes for this envelope
  125. std::vector<Key> keys;
  126. // temporary data for AnimResolver
  127. size_t old_first,old_last;
  128. };
  129. // ---------------------------------------------------------------------------
  130. //! @def AI_LWO_ANIM_FLAG_SAMPLE_ANIMS
  131. //! Flag for AnimResolver, subsamples the input data with the rate specified
  132. //! by AnimResolver::SetSampleRate().
  133. #define AI_LWO_ANIM_FLAG_SAMPLE_ANIMS 0x1
  134. // ---------------------------------------------------------------------------
  135. //! @def AI_LWO_ANIM_FLAG_START_AT_ZERO
  136. //! Flag for AnimResolver, ensures that the animations starts at zero.
  137. #define AI_LWO_ANIM_FLAG_START_AT_ZERO 0x2
  138. // ---------------------------------------------------------------------------
  139. /** @brief Utility class to build Assimp animations from LWO envelopes.
  140. *
  141. * Used for both LWO and LWS (MOT also).
  142. */
  143. class AnimResolver
  144. {
  145. public:
  146. // ------------------------------------------------------------------
  147. /** @brief Construct an AnimResolver from a given list of envelopes
  148. * @param envelopes Input envelopes. May be empty.
  149. * @param Output tick rate, per second
  150. * @note The input envelopes are possibly modified.
  151. */
  152. AnimResolver(std::list<Envelope>& envelopes,
  153. double tick);
  154. public:
  155. // ------------------------------------------------------------------
  156. /** @brief Extract the bind-pose transformation matrix.
  157. * @param out Receives bind-pose transformation matrix
  158. */
  159. void ExtractBindPose(aiMatrix4x4& out);
  160. // ------------------------------------------------------------------
  161. /** @brief Extract a node animation channel
  162. * @param out Receives a pointer to a newly allocated node anim.
  163. * If there's just one keyframe defined, *out is set to NULL and
  164. * no animation channel is computed.
  165. * @param flags Any combination of the AI_LWO_ANIM_FLAG_XXX flags.
  166. */
  167. void ExtractAnimChannel(aiNodeAnim** out, unsigned int flags = 0);
  168. // ------------------------------------------------------------------
  169. /** @brief Set the sampling rate for ExtractAnimChannel().
  170. *
  171. * Non-linear interpolations are subsampled with this rate (keys
  172. * per second). Closer sampling positions, if existent, are kept.
  173. * The sampling rate defaults to 0, if this value is not changed and
  174. * AI_LWO_ANIM_FLAG_SAMPLE_ANIMS is specified for ExtractAnimChannel(),
  175. * the class finds a suitable sample rate by itself.
  176. */
  177. void SetSampleRate(double sr) {
  178. sample_rate = sr;
  179. }
  180. // ------------------------------------------------------------------
  181. /** @brief Getter for SetSampleRate()
  182. */
  183. double GetSampleRate() const {
  184. return sample_rate;
  185. }
  186. // ------------------------------------------------------------------
  187. /** @brief Set the animation time range
  188. *
  189. * @param first Time where the animation starts, in ticks
  190. * @param last Time where the animation ends, in ticks
  191. */
  192. void SetAnimationRange(double _first, double _last) {
  193. first = _first;
  194. last = _last;
  195. ClearAnimRangeSetup();
  196. UpdateAnimRangeSetup();
  197. }
  198. protected:
  199. // ------------------------------------------------------------------
  200. /** @brief Build linearly subsampled keys from 3 single envelopes
  201. * @param out Receives output keys
  202. * @param envl_x X-component envelope
  203. * @param envl_y Y-component envelope
  204. * @param envl_z Z-component envelope
  205. * @param flags Any combination of the AI_LWO_ANIM_FLAG_XXX flags.
  206. * @note Up to two input envelopes may be NULL
  207. */
  208. void GetKeys(std::vector<aiVectorKey>& out,
  209. LWO::Envelope* envl_x,
  210. LWO::Envelope* envl_y,
  211. LWO::Envelope* envl_z,
  212. unsigned int flags);
  213. // ------------------------------------------------------------------
  214. /** @brief Resolve a single animation key by applying the right
  215. * interpolation to it.
  216. * @param cur Current key
  217. * @param envl Envelope working on
  218. * @param time time to be interpolated
  219. * @param fill Receives the interpolated output value.
  220. */
  221. void DoInterpolation(std::vector<LWO::Key>::const_iterator cur,
  222. LWO::Envelope* envl,double time, float& fill);
  223. // ------------------------------------------------------------------
  224. /** @brief Almost the same, except we won't handle pre/post
  225. * conditions here.
  226. * @see DoInterpolation
  227. */
  228. void DoInterpolation2(std::vector<LWO::Key>::const_iterator beg,
  229. std::vector<LWO::Key>::const_iterator end,double time, float& fill);
  230. // ------------------------------------------------------------------
  231. /** @brief Interpolate 2 tracks if one is given
  232. *
  233. * @param out Receives extra output keys
  234. * @param key_out Primary output key
  235. * @param time Time to interpolate for
  236. */
  237. void InterpolateTrack(std::vector<aiVectorKey>& out,
  238. aiVectorKey& key_out,double time);
  239. // ------------------------------------------------------------------
  240. /** @brief Subsample an animation track by a given sampling rate
  241. *
  242. * @param out Receives output keys. Last key at input defines the
  243. * time where subsampling starts.
  244. * @param time Time to end subsampling at
  245. * @param sample_delta Time delta between two samples
  246. */
  247. void SubsampleAnimTrack(std::vector<aiVectorKey>& out,
  248. double time,double sample_delta);
  249. // ------------------------------------------------------------------
  250. /** @brief Delete all keys which we inserted to match anim setup
  251. */
  252. void ClearAnimRangeSetup();
  253. // ------------------------------------------------------------------
  254. /** @brief Insert extra keys to match LWO's pre and post behaviours
  255. * in a given time range [first...last]
  256. */
  257. void UpdateAnimRangeSetup();
  258. private:
  259. std::list<Envelope>& envelopes;
  260. double sample_rate;
  261. LWO::Envelope* trans_x, *trans_y, *trans_z;
  262. LWO::Envelope* rotat_x, *rotat_y, *rotat_z;
  263. LWO::Envelope* scale_x, *scale_y, *scale_z;
  264. double first, last;
  265. bool need_to_setup;
  266. // temporary storage
  267. LWO::Envelope* envl_x, * envl_y, * envl_z;
  268. std::vector<LWO::Key>::const_iterator cur_x,cur_y,cur_z;
  269. bool end_x, end_y, end_z;
  270. unsigned int flags;
  271. double sample_delta;
  272. };
  273. } // end namespace LWO
  274. } // end namespace Assimp
  275. #endif // !! AI_LWO_ANIMATION_INCLUDED