vrshoot

view libs/assimp/LWOAnimation.h @ 2:334d17aed7de

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