vrshoot
diff libs/assimp/LWOAnimation.h @ 0:b2f14e535253
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 19:58:19 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/assimp/LWOAnimation.h Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,336 @@ 1.4 +/* 1.5 +Open Asset Import Library (assimp) 1.6 +---------------------------------------------------------------------- 1.7 + 1.8 +Copyright (c) 2006-2012, assimp team 1.9 +All rights reserved. 1.10 + 1.11 +Redistribution and use of this software in source and binary forms, 1.12 +with or without modification, are permitted provided that the 1.13 +following conditions are met: 1.14 + 1.15 +* Redistributions of source code must retain the above 1.16 + copyright notice, this list of conditions and the 1.17 + following disclaimer. 1.18 + 1.19 +* Redistributions in binary form must reproduce the above 1.20 + copyright notice, this list of conditions and the 1.21 + following disclaimer in the documentation and/or other 1.22 + materials provided with the distribution. 1.23 + 1.24 +* Neither the name of the assimp team, nor the names of its 1.25 + contributors may be used to endorse or promote products 1.26 + derived from this software without specific prior 1.27 + written permission of the assimp team. 1.28 + 1.29 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.30 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.31 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.32 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.33 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.34 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.35 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.36 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.37 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.38 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.39 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.40 + 1.41 +---------------------------------------------------------------------- 1.42 +*/ 1.43 + 1.44 +/** @file LWOAnimation.h 1.45 + * @brief LWOAnimationResolver utility class 1.46 + * 1.47 + * This is for all lightwave-related file format, not only LWO. 1.48 + * LWS isthe main purpose. 1.49 +*/ 1.50 +#ifndef AI_LWO_ANIMATION_INCLUDED 1.51 +#define AI_LWO_ANIMATION_INCLUDED 1.52 + 1.53 +namespace Assimp { 1.54 +namespace LWO { 1.55 + 1.56 +// --------------------------------------------------------------------------- 1.57 +/** \brief List of recognized LWO envelopes 1.58 + */ 1.59 +enum EnvelopeType 1.60 +{ 1.61 + EnvelopeType_Position_X = 0x1, 1.62 + EnvelopeType_Position_Y = 0x2, 1.63 + EnvelopeType_Position_Z = 0x3, 1.64 + 1.65 + EnvelopeType_Rotation_Heading = 0x4, 1.66 + EnvelopeType_Rotation_Pitch = 0x5, 1.67 + EnvelopeType_Rotation_Bank = 0x6, 1.68 + 1.69 + EnvelopeType_Scaling_X = 0x7, 1.70 + EnvelopeType_Scaling_Y = 0x8, 1.71 + EnvelopeType_Scaling_Z = 0x9, 1.72 + 1.73 + // -- currently not yet handled 1.74 + EnvelopeType_Color_R = 0xa, 1.75 + EnvelopeType_Color_G = 0xb, 1.76 + EnvelopeType_Color_B = 0xc, 1.77 + 1.78 + EnvelopeType_Falloff_X = 0xd, 1.79 + EnvelopeType_Falloff_Y = 0xe, 1.80 + EnvelopeType_Falloff_Z = 0xf, 1.81 + 1.82 + EnvelopeType_Unknown 1.83 +}; 1.84 + 1.85 +// --------------------------------------------------------------------------- 1.86 +/** \brief List of recognized LWO interpolation modes 1.87 + */ 1.88 +enum InterpolationType 1.89 +{ 1.90 + IT_STEP, IT_LINE, IT_TCB, IT_HERM, IT_BEZI, IT_BEZ2 1.91 +}; 1.92 + 1.93 + 1.94 +// --------------------------------------------------------------------------- 1.95 +/** \brief List of recognized LWO pre or post range behaviours 1.96 + */ 1.97 +enum PrePostBehaviour 1.98 +{ 1.99 + PrePostBehaviour_Reset = 0x0, 1.100 + PrePostBehaviour_Constant = 0x1, 1.101 + PrePostBehaviour_Repeat = 0x2, 1.102 + PrePostBehaviour_Oscillate = 0x3, 1.103 + PrePostBehaviour_OffsetRepeat = 0x4, 1.104 + PrePostBehaviour_Linear = 0x5 1.105 +}; 1.106 + 1.107 +// --------------------------------------------------------------------------- 1.108 +/** \brief Data structure for a LWO animation keyframe 1.109 + */ 1.110 +struct Key 1.111 +{ 1.112 + Key() 1.113 + : inter (IT_LINE) 1.114 + {} 1.115 + 1.116 + //! Current time 1.117 + double time; 1.118 + 1.119 + //! Current value 1.120 + float value; 1.121 + 1.122 + //! How to interpolate this key with previous key? 1.123 + InterpolationType inter; 1.124 + 1.125 + //! Interpolation parameters 1.126 + float params[5]; 1.127 + 1.128 + 1.129 + // for std::find() 1.130 + operator double () { 1.131 + return time; 1.132 + } 1.133 +}; 1.134 + 1.135 +// --------------------------------------------------------------------------- 1.136 +/** \brief Data structure for a LWO animation envelope 1.137 + */ 1.138 +struct Envelope 1.139 +{ 1.140 + Envelope() 1.141 + : type (EnvelopeType_Unknown) 1.142 + , pre (PrePostBehaviour_Constant) 1.143 + , post (PrePostBehaviour_Constant) 1.144 + 1.145 + , old_first (0) 1.146 + , old_last (0) 1.147 + {} 1.148 + 1.149 + //! Index of this envelope 1.150 + unsigned int index; 1.151 + 1.152 + //! Type of envelope 1.153 + EnvelopeType type; 1.154 + 1.155 + //! Pre and post-behaviour 1.156 + PrePostBehaviour pre,post; 1.157 + 1.158 + //! Keyframes for this envelope 1.159 + std::vector<Key> keys; 1.160 + 1.161 + 1.162 + // temporary data for AnimResolver 1.163 + size_t old_first,old_last; 1.164 +}; 1.165 + 1.166 +// --------------------------------------------------------------------------- 1.167 +//! @def AI_LWO_ANIM_FLAG_SAMPLE_ANIMS 1.168 +//! Flag for AnimResolver, subsamples the input data with the rate specified 1.169 +//! by AnimResolver::SetSampleRate(). 1.170 +#define AI_LWO_ANIM_FLAG_SAMPLE_ANIMS 0x1 1.171 + 1.172 + 1.173 +// --------------------------------------------------------------------------- 1.174 +//! @def AI_LWO_ANIM_FLAG_START_AT_ZERO 1.175 +//! Flag for AnimResolver, ensures that the animations starts at zero. 1.176 +#define AI_LWO_ANIM_FLAG_START_AT_ZERO 0x2 1.177 + 1.178 +// --------------------------------------------------------------------------- 1.179 +/** @brief Utility class to build Assimp animations from LWO envelopes. 1.180 + * 1.181 + * Used for both LWO and LWS (MOT also). 1.182 + */ 1.183 +class AnimResolver 1.184 +{ 1.185 +public: 1.186 + 1.187 + // ------------------------------------------------------------------ 1.188 + /** @brief Construct an AnimResolver from a given list of envelopes 1.189 + * @param envelopes Input envelopes. May be empty. 1.190 + * @param Output tick rate, per second 1.191 + * @note The input envelopes are possibly modified. 1.192 + */ 1.193 + AnimResolver(std::list<Envelope>& envelopes, 1.194 + double tick); 1.195 + 1.196 +public: 1.197 + 1.198 + // ------------------------------------------------------------------ 1.199 + /** @brief Extract the bind-pose transformation matrix. 1.200 + * @param out Receives bind-pose transformation matrix 1.201 + */ 1.202 + void ExtractBindPose(aiMatrix4x4& out); 1.203 + 1.204 + // ------------------------------------------------------------------ 1.205 + /** @brief Extract a node animation channel 1.206 + * @param out Receives a pointer to a newly allocated node anim. 1.207 + * If there's just one keyframe defined, *out is set to NULL and 1.208 + * no animation channel is computed. 1.209 + * @param flags Any combination of the AI_LWO_ANIM_FLAG_XXX flags. 1.210 + */ 1.211 + void ExtractAnimChannel(aiNodeAnim** out, unsigned int flags = 0); 1.212 + 1.213 + 1.214 + // ------------------------------------------------------------------ 1.215 + /** @brief Set the sampling rate for ExtractAnimChannel(). 1.216 + * 1.217 + * Non-linear interpolations are subsampled with this rate (keys 1.218 + * per second). Closer sampling positions, if existent, are kept. 1.219 + * The sampling rate defaults to 0, if this value is not changed and 1.220 + * AI_LWO_ANIM_FLAG_SAMPLE_ANIMS is specified for ExtractAnimChannel(), 1.221 + * the class finds a suitable sample rate by itself. 1.222 + */ 1.223 + void SetSampleRate(double sr) { 1.224 + sample_rate = sr; 1.225 + } 1.226 + 1.227 + // ------------------------------------------------------------------ 1.228 + /** @brief Getter for SetSampleRate() 1.229 + */ 1.230 + double GetSampleRate() const { 1.231 + return sample_rate; 1.232 + } 1.233 + 1.234 + // ------------------------------------------------------------------ 1.235 + /** @brief Set the animation time range 1.236 + * 1.237 + * @param first Time where the animation starts, in ticks 1.238 + * @param last Time where the animation ends, in ticks 1.239 + */ 1.240 + void SetAnimationRange(double _first, double _last) { 1.241 + first = _first; 1.242 + last = _last; 1.243 + 1.244 + ClearAnimRangeSetup(); 1.245 + UpdateAnimRangeSetup(); 1.246 + } 1.247 + 1.248 +protected: 1.249 + 1.250 + // ------------------------------------------------------------------ 1.251 + /** @brief Build linearly subsampled keys from 3 single envelopes 1.252 + * @param out Receives output keys 1.253 + * @param envl_x X-component envelope 1.254 + * @param envl_y Y-component envelope 1.255 + * @param envl_z Z-component envelope 1.256 + * @param flags Any combination of the AI_LWO_ANIM_FLAG_XXX flags. 1.257 + * @note Up to two input envelopes may be NULL 1.258 + */ 1.259 + void GetKeys(std::vector<aiVectorKey>& out, 1.260 + LWO::Envelope* envl_x, 1.261 + LWO::Envelope* envl_y, 1.262 + LWO::Envelope* envl_z, 1.263 + unsigned int flags); 1.264 + 1.265 + // ------------------------------------------------------------------ 1.266 + /** @brief Resolve a single animation key by applying the right 1.267 + * interpolation to it. 1.268 + * @param cur Current key 1.269 + * @param envl Envelope working on 1.270 + * @param time time to be interpolated 1.271 + * @param fill Receives the interpolated output value. 1.272 + */ 1.273 + void DoInterpolation(std::vector<LWO::Key>::const_iterator cur, 1.274 + LWO::Envelope* envl,double time, float& fill); 1.275 + 1.276 + // ------------------------------------------------------------------ 1.277 + /** @brief Almost the same, except we won't handle pre/post 1.278 + * conditions here. 1.279 + * @see DoInterpolation 1.280 + */ 1.281 + void DoInterpolation2(std::vector<LWO::Key>::const_iterator beg, 1.282 + std::vector<LWO::Key>::const_iterator end,double time, float& fill); 1.283 + 1.284 + // ------------------------------------------------------------------ 1.285 + /** @brief Interpolate 2 tracks if one is given 1.286 + * 1.287 + * @param out Receives extra output keys 1.288 + * @param key_out Primary output key 1.289 + * @param time Time to interpolate for 1.290 + */ 1.291 + void InterpolateTrack(std::vector<aiVectorKey>& out, 1.292 + aiVectorKey& key_out,double time); 1.293 + 1.294 + // ------------------------------------------------------------------ 1.295 + /** @brief Subsample an animation track by a given sampling rate 1.296 + * 1.297 + * @param out Receives output keys. Last key at input defines the 1.298 + * time where subsampling starts. 1.299 + * @param time Time to end subsampling at 1.300 + * @param sample_delta Time delta between two samples 1.301 + */ 1.302 + void SubsampleAnimTrack(std::vector<aiVectorKey>& out, 1.303 + double time,double sample_delta); 1.304 + 1.305 + // ------------------------------------------------------------------ 1.306 + /** @brief Delete all keys which we inserted to match anim setup 1.307 + */ 1.308 + void ClearAnimRangeSetup(); 1.309 + 1.310 + // ------------------------------------------------------------------ 1.311 + /** @brief Insert extra keys to match LWO's pre and post behaviours 1.312 + * in a given time range [first...last] 1.313 + */ 1.314 + void UpdateAnimRangeSetup(); 1.315 + 1.316 +private: 1.317 + std::list<Envelope>& envelopes; 1.318 + double sample_rate; 1.319 + 1.320 + LWO::Envelope* trans_x, *trans_y, *trans_z; 1.321 + LWO::Envelope* rotat_x, *rotat_y, *rotat_z; 1.322 + LWO::Envelope* scale_x, *scale_y, *scale_z; 1.323 + 1.324 + double first, last; 1.325 + bool need_to_setup; 1.326 + 1.327 + // temporary storage 1.328 + LWO::Envelope* envl_x, * envl_y, * envl_z; 1.329 + std::vector<LWO::Key>::const_iterator cur_x,cur_y,cur_z; 1.330 + bool end_x, end_y, end_z; 1.331 + 1.332 + unsigned int flags; 1.333 + double sample_delta; 1.334 +}; 1.335 + 1.336 +} // end namespace LWO 1.337 +} // end namespace Assimp 1.338 + 1.339 +#endif // !! AI_LWO_ANIMATION_INCLUDED