nuclear@0: /* nuclear@0: Open Asset Import Library (assimp) nuclear@0: ---------------------------------------------------------------------- nuclear@0: nuclear@0: Copyright (c) 2006-2012, assimp team nuclear@0: All rights reserved. nuclear@0: nuclear@0: Redistribution and use of this software in source and binary forms, nuclear@0: with or without modification, are permitted provided that the nuclear@0: following conditions are met: nuclear@0: nuclear@0: * Redistributions of source code must retain the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer. nuclear@0: nuclear@0: * Redistributions in binary form must reproduce the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer in the documentation and/or other nuclear@0: materials provided with the distribution. nuclear@0: nuclear@0: * Neither the name of the assimp team, nor the names of its nuclear@0: contributors may be used to endorse or promote products nuclear@0: derived from this software without specific prior nuclear@0: written permission of the assimp team. nuclear@0: nuclear@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS nuclear@0: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT nuclear@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR nuclear@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT nuclear@0: OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, nuclear@0: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT nuclear@0: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, nuclear@0: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY nuclear@0: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT nuclear@0: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE nuclear@0: OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. nuclear@0: nuclear@0: ---------------------------------------------------------------------- nuclear@0: */ nuclear@0: nuclear@0: /** @file LWOAnimation.cpp nuclear@0: * @brief LWOAnimationResolver utility class nuclear@0: * nuclear@0: * It's a very generic implementation of LightWave's system of nuclear@0: * componentwise-animated stuff. The one and only fully free nuclear@0: * implementation of LightWave envelopes of which I know. nuclear@0: */ nuclear@0: nuclear@0: #include "AssimpPCH.h" nuclear@0: #if (!defined ASSIMP_BUILD_NO_LWO_IMPORTER) && (!defined ASSIMP_BUILD_NO_LWS_IMPORTER) nuclear@0: nuclear@0: #include nuclear@0: nuclear@0: // internal headers nuclear@0: #include "LWOFileData.h" nuclear@0: nuclear@0: using namespace Assimp; nuclear@0: using namespace Assimp::LWO; nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Construct an animation resolver from a given list of envelopes nuclear@0: AnimResolver::AnimResolver(std::list& _envelopes,double tick) nuclear@0: : envelopes (_envelopes) nuclear@0: , sample_rate (0.) nuclear@0: { nuclear@0: trans_x = trans_y = trans_z = NULL; nuclear@0: rotat_x = rotat_y = rotat_z = NULL; nuclear@0: scale_x = scale_y = scale_z = NULL; nuclear@0: nuclear@0: first = last = 150392.; nuclear@0: nuclear@0: // find transformation envelopes nuclear@0: for (std::list::iterator it = envelopes.begin(); it != envelopes.end(); ++it) { nuclear@0: nuclear@0: (*it).old_first = 0; nuclear@0: (*it).old_last = (*it).keys.size()-1; nuclear@0: nuclear@0: if ((*it).keys.empty()) continue; nuclear@0: switch ((*it).type) { nuclear@0: nuclear@0: // translation nuclear@0: case LWO::EnvelopeType_Position_X: nuclear@0: trans_x = &*it;break; nuclear@0: case LWO::EnvelopeType_Position_Y: nuclear@0: trans_y = &*it;break; nuclear@0: case LWO::EnvelopeType_Position_Z: nuclear@0: trans_z = &*it;break; nuclear@0: nuclear@0: // rotation nuclear@0: case LWO::EnvelopeType_Rotation_Heading: nuclear@0: rotat_x = &*it;break; nuclear@0: case LWO::EnvelopeType_Rotation_Pitch: nuclear@0: rotat_y = &*it;break; nuclear@0: case LWO::EnvelopeType_Rotation_Bank: nuclear@0: rotat_z = &*it;break; nuclear@0: nuclear@0: // scaling nuclear@0: case LWO::EnvelopeType_Scaling_X: nuclear@0: scale_x = &*it;break; nuclear@0: case LWO::EnvelopeType_Scaling_Y: nuclear@0: scale_y = &*it;break; nuclear@0: case LWO::EnvelopeType_Scaling_Z: nuclear@0: scale_z = &*it;break; nuclear@0: default: nuclear@0: continue; nuclear@0: }; nuclear@0: nuclear@0: // convert from seconds to ticks nuclear@0: for (std::vector::iterator d = (*it).keys.begin(); d != (*it).keys.end(); ++d) nuclear@0: (*d).time *= tick; nuclear@0: nuclear@0: // set default animation range (minimum and maximum time value for which we have a keyframe) nuclear@0: first = std::min(first, (*it).keys.front().time ); nuclear@0: last = std::max(last, (*it).keys.back().time ); nuclear@0: } nuclear@0: nuclear@0: // deferred setup of animation range to increase performance. nuclear@0: // typically the application will want to specify its own. nuclear@0: need_to_setup = true; nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Reset all envelopes to their original contents nuclear@0: void AnimResolver::ClearAnimRangeSetup() nuclear@0: { nuclear@0: for (std::list::iterator it = envelopes.begin(); it != envelopes.end(); ++it) { nuclear@0: nuclear@0: (*it).keys.erase((*it).keys.begin(),(*it).keys.begin()+(*it).old_first); nuclear@0: (*it).keys.erase((*it).keys.begin()+(*it).old_last+1,(*it).keys.end()); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Insert additional keys to match LWO's pre& post behaviours. nuclear@0: void AnimResolver::UpdateAnimRangeSetup() nuclear@0: { nuclear@0: // XXX doesn't work yet (hangs if more than one envelope channels needs to be interpolated) nuclear@0: nuclear@0: for (std::list::iterator it = envelopes.begin(); it != envelopes.end(); ++it) { nuclear@0: if ((*it).keys.empty()) continue; nuclear@0: nuclear@0: const double my_first = (*it).keys.front().time; nuclear@0: const double my_last = (*it).keys.back().time; nuclear@0: nuclear@0: const double delta = my_last-my_first; nuclear@0: const size_t old_size = (*it).keys.size(); nuclear@0: nuclear@0: const float value_delta = (*it).keys.back().value - (*it).keys.front().value; nuclear@0: nuclear@0: // NOTE: We won't handle reset, linear and constant here. nuclear@0: // See DoInterpolation() for their implementation. nuclear@0: nuclear@0: // process pre behaviour nuclear@0: switch ((*it).pre) { nuclear@0: case LWO::PrePostBehaviour_OffsetRepeat: nuclear@0: case LWO::PrePostBehaviour_Repeat: nuclear@0: case LWO::PrePostBehaviour_Oscillate: nuclear@0: { nuclear@0: const double start_time = delta - fmod(my_first-first,delta); nuclear@0: std::vector::iterator n = std::find_if((*it).keys.begin(),(*it).keys.end(), nuclear@0: std::bind1st(std::greater(),start_time)),m; nuclear@0: nuclear@0: size_t ofs = 0; nuclear@0: if (n != (*it).keys.end()) { nuclear@0: // copy from here - don't use iterators, insert() would invalidate them nuclear@0: ofs = (*it).keys.end()-n; nuclear@0: (*it).keys.insert((*it).keys.begin(),ofs,LWO::Key()); nuclear@0: nuclear@0: std::copy((*it).keys.end()-ofs,(*it).keys.end(),(*it).keys.begin()); nuclear@0: } nuclear@0: nuclear@0: // do full copies. again, no iterators nuclear@0: const unsigned int num = (unsigned int)((my_first-first) / delta); nuclear@0: (*it).keys.resize((*it).keys.size() + num*old_size); nuclear@0: nuclear@0: n = (*it).keys.begin()+ofs; nuclear@0: bool reverse = false; nuclear@0: for (unsigned int i = 0; i < num; ++i) { nuclear@0: m = n+old_size*(i+1); nuclear@0: std::copy(n,n+old_size,m); nuclear@0: nuclear@0: if ((*it).pre == LWO::PrePostBehaviour_Oscillate && (reverse = !reverse)) nuclear@0: std::reverse(m,m+old_size-1); nuclear@0: } nuclear@0: nuclear@0: // update time values nuclear@0: n = (*it).keys.end() - (old_size+1); nuclear@0: double cur_minus = delta; nuclear@0: unsigned int tt = 1; nuclear@0: for (const double tmp = delta*(num+1);cur_minus <= tmp;cur_minus += delta,++tt) { nuclear@0: m = (delta == tmp ? (*it).keys.begin() : n - (old_size+1)); nuclear@0: for (;m != n; --n) { nuclear@0: (*n).time -= cur_minus; nuclear@0: nuclear@0: // offset repeat? add delta offset to key value nuclear@0: if ((*it).pre == LWO::PrePostBehaviour_OffsetRepeat) { nuclear@0: (*n).value += tt * value_delta; nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: break; nuclear@0: } nuclear@0: default: nuclear@0: // silence compiler warning nuclear@0: break; nuclear@0: } nuclear@0: nuclear@0: // process post behaviour nuclear@0: switch ((*it).post) { nuclear@0: nuclear@0: case LWO::PrePostBehaviour_OffsetRepeat: nuclear@0: case LWO::PrePostBehaviour_Repeat: nuclear@0: case LWO::PrePostBehaviour_Oscillate: nuclear@0: nuclear@0: break; nuclear@0: nuclear@0: default: nuclear@0: // silence compiler warning nuclear@0: break; nuclear@0: } nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Extract bind pose matrix nuclear@0: void AnimResolver::ExtractBindPose(aiMatrix4x4& out) nuclear@0: { nuclear@0: // If we have no envelopes, return identity nuclear@0: if (envelopes.empty()) { nuclear@0: out = aiMatrix4x4(); nuclear@0: return; nuclear@0: } nuclear@0: aiVector3D angles, scaling(1.f,1.f,1.f), translation; nuclear@0: nuclear@0: if (trans_x) translation.x = trans_x->keys[0].value; nuclear@0: if (trans_y) translation.y = trans_y->keys[0].value; nuclear@0: if (trans_z) translation.z = trans_z->keys[0].value; nuclear@0: nuclear@0: if (rotat_x) angles.x = rotat_x->keys[0].value; nuclear@0: if (rotat_y) angles.y = rotat_y->keys[0].value; nuclear@0: if (rotat_z) angles.z = rotat_z->keys[0].value; nuclear@0: nuclear@0: if (scale_x) scaling.x = scale_x->keys[0].value; nuclear@0: if (scale_y) scaling.y = scale_y->keys[0].value; nuclear@0: if (scale_z) scaling.z = scale_z->keys[0].value; nuclear@0: nuclear@0: // build the final matrix nuclear@0: aiMatrix4x4 s,rx,ry,rz,t; nuclear@0: aiMatrix4x4::RotationZ(angles.z, rz); nuclear@0: aiMatrix4x4::RotationX(angles.y, rx); nuclear@0: aiMatrix4x4::RotationY(angles.x, ry); nuclear@0: aiMatrix4x4::Translation(translation,t); nuclear@0: aiMatrix4x4::Scaling(scaling,s); nuclear@0: out = t*ry*rx*rz*s; nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Do a single interpolation on a channel nuclear@0: void AnimResolver::DoInterpolation(std::vector::const_iterator cur, nuclear@0: LWO::Envelope* envl,double time, float& fill) nuclear@0: { nuclear@0: if (envl->keys.size() == 1) { nuclear@0: fill = envl->keys[0].value; nuclear@0: return; nuclear@0: } nuclear@0: nuclear@0: // check whether we're at the beginning of the animation track nuclear@0: if (cur == envl->keys.begin()) { nuclear@0: nuclear@0: // ok ... this depends on pre behaviour now nuclear@0: // we don't need to handle repeat&offset repeat&oszillate here, see UpdateAnimRangeSetup() nuclear@0: switch (envl->pre) nuclear@0: { nuclear@0: case LWO::PrePostBehaviour_Linear: nuclear@0: DoInterpolation2(cur,cur+1,time,fill); nuclear@0: return; nuclear@0: nuclear@0: case LWO::PrePostBehaviour_Reset: nuclear@0: fill = 0.f; nuclear@0: return; nuclear@0: nuclear@0: default : //case LWO::PrePostBehaviour_Constant: nuclear@0: fill = (*cur).value; nuclear@0: return; nuclear@0: } nuclear@0: } nuclear@0: // check whether we're at the end of the animation track nuclear@0: else if (cur == envl->keys.end()-1 && time > envl->keys.rbegin()->time) { nuclear@0: // ok ... this depends on post behaviour now nuclear@0: switch (envl->post) nuclear@0: { nuclear@0: case LWO::PrePostBehaviour_Linear: nuclear@0: DoInterpolation2(cur,cur-1,time,fill); nuclear@0: return; nuclear@0: nuclear@0: case LWO::PrePostBehaviour_Reset: nuclear@0: fill = 0.f; nuclear@0: return; nuclear@0: nuclear@0: default : //case LWO::PrePostBehaviour_Constant: nuclear@0: fill = (*cur).value; nuclear@0: return; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // Otherwise do a simple interpolation nuclear@0: DoInterpolation2(cur-1,cur,time,fill); nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Almost the same, except we won't handle pre/post conditions here nuclear@0: void AnimResolver::DoInterpolation2(std::vector::const_iterator beg, nuclear@0: std::vector::const_iterator end,double time, float& fill) nuclear@0: { nuclear@0: switch ((*end).inter) { nuclear@0: nuclear@0: case LWO::IT_STEP: nuclear@0: // no interpolation at all - take the value of the last key nuclear@0: fill = (*beg).value; nuclear@0: return; nuclear@0: default: nuclear@0: nuclear@0: // silence compiler warning nuclear@0: break; nuclear@0: } nuclear@0: // linear interpolation - default nuclear@0: fill = (*beg).value + ((*end).value - (*beg).value)*(float)(((time - (*beg).time) / ((*end).time - (*beg).time))); nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Subsample animation track by given key values nuclear@0: void AnimResolver::SubsampleAnimTrack(std::vector& /*out*/, nuclear@0: double /*time*/ ,double /*sample_delta*/ ) nuclear@0: { nuclear@0: //ai_assert(out.empty() && sample_delta); nuclear@0: nuclear@0: //const double time_start = out.back().mTime; nuclear@0: // for () nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Track interpolation nuclear@0: void AnimResolver::InterpolateTrack(std::vector& out,aiVectorKey& fill,double time) nuclear@0: { nuclear@0: // subsample animation track? nuclear@0: if (flags & AI_LWO_ANIM_FLAG_SAMPLE_ANIMS) { nuclear@0: SubsampleAnimTrack(out,time, sample_delta); nuclear@0: } nuclear@0: nuclear@0: fill.mTime = time; nuclear@0: nuclear@0: // get x nuclear@0: if ((*cur_x).time == time) { nuclear@0: fill.mValue.x = (*cur_x).value; nuclear@0: nuclear@0: if (cur_x != envl_x->keys.end()-1) /* increment x */ nuclear@0: ++cur_x; nuclear@0: else end_x = true; nuclear@0: } nuclear@0: else DoInterpolation(cur_x,envl_x,time,(float&)fill.mValue.x); nuclear@0: nuclear@0: // get y nuclear@0: if ((*cur_y).time == time) { nuclear@0: fill.mValue.y = (*cur_y).value; nuclear@0: nuclear@0: if (cur_y != envl_y->keys.end()-1) /* increment y */ nuclear@0: ++cur_y; nuclear@0: else end_y = true; nuclear@0: } nuclear@0: else DoInterpolation(cur_y,envl_y,time,(float&)fill.mValue.y); nuclear@0: nuclear@0: // get z nuclear@0: if ((*cur_z).time == time) { nuclear@0: fill.mValue.z = (*cur_z).value; nuclear@0: nuclear@0: if (cur_z != envl_z->keys.end()-1) /* increment z */ nuclear@0: ++cur_z; nuclear@0: else end_x = true; nuclear@0: } nuclear@0: else DoInterpolation(cur_z,envl_z,time,(float&)fill.mValue.z); nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Build linearly subsampled keys from three single envelopes, one for each component (x,y,z) nuclear@0: void AnimResolver::GetKeys(std::vector& out, nuclear@0: LWO::Envelope* _envl_x, nuclear@0: LWO::Envelope* _envl_y, nuclear@0: LWO::Envelope* _envl_z, nuclear@0: unsigned int _flags) nuclear@0: { nuclear@0: envl_x = _envl_x; nuclear@0: envl_y = _envl_y; nuclear@0: envl_z = _envl_z; nuclear@0: flags = _flags; nuclear@0: nuclear@0: // generate default channels if none are given nuclear@0: LWO::Envelope def_x, def_y, def_z; nuclear@0: LWO::Key key_dummy; nuclear@0: key_dummy.time = 0.f; nuclear@0: if ((envl_x && envl_x->type == LWO::EnvelopeType_Scaling_X) || nuclear@0: (envl_y && envl_y->type == LWO::EnvelopeType_Scaling_Y) || nuclear@0: (envl_z && envl_z->type == LWO::EnvelopeType_Scaling_Z)) { nuclear@0: key_dummy.value = 1.f; nuclear@0: } nuclear@0: else key_dummy.value = 0.f; nuclear@0: nuclear@0: if (!envl_x) { nuclear@0: envl_x = &def_x; nuclear@0: envl_x->keys.push_back(key_dummy); nuclear@0: } nuclear@0: if (!envl_y) { nuclear@0: envl_y = &def_y; nuclear@0: envl_y->keys.push_back(key_dummy); nuclear@0: } nuclear@0: if (!envl_z) { nuclear@0: envl_z = &def_z; nuclear@0: envl_z->keys.push_back(key_dummy); nuclear@0: } nuclear@0: nuclear@0: // guess how many keys we'll get nuclear@0: size_t reserve; nuclear@0: double sr = 1.; nuclear@0: if (flags & AI_LWO_ANIM_FLAG_SAMPLE_ANIMS) { nuclear@0: if (!sample_rate) nuclear@0: sr = 100.f; nuclear@0: else sr = sample_rate; nuclear@0: sample_delta = 1.f / sr; nuclear@0: nuclear@0: reserve = (size_t)( nuclear@0: std::max( envl_x->keys.rbegin()->time, nuclear@0: std::max( envl_y->keys.rbegin()->time, envl_z->keys.rbegin()->time )) * sr); nuclear@0: } nuclear@0: else reserve = std::max(envl_x->keys.size(),std::max(envl_x->keys.size(),envl_z->keys.size())); nuclear@0: out.reserve(reserve+(reserve>>1)); nuclear@0: nuclear@0: // Iterate through all three arrays at once - it's tricky, but nuclear@0: // rather interesting to implement. nuclear@0: double lasttime = std::min(envl_x->keys[0].time,std::min(envl_y->keys[0].time,envl_z->keys[0].time)); nuclear@0: nuclear@0: cur_x = envl_x->keys.begin(); nuclear@0: cur_y = envl_y->keys.begin(); nuclear@0: cur_z = envl_z->keys.begin(); nuclear@0: nuclear@0: end_x = end_y = end_z = false; nuclear@0: while (1) { nuclear@0: nuclear@0: aiVectorKey fill; nuclear@0: nuclear@0: if ((*cur_x).time == (*cur_y).time && (*cur_x).time == (*cur_z).time ) { nuclear@0: nuclear@0: // we have a keyframe for all of them defined .. this means nuclear@0: // we don't need to interpolate here. nuclear@0: fill.mTime = (*cur_x).time; nuclear@0: nuclear@0: fill.mValue.x = (*cur_x).value; nuclear@0: fill.mValue.y = (*cur_y).value; nuclear@0: fill.mValue.z = (*cur_z).value; nuclear@0: nuclear@0: // subsample animation track nuclear@0: if (flags & AI_LWO_ANIM_FLAG_SAMPLE_ANIMS) { nuclear@0: //SubsampleAnimTrack(out,cur_x, cur_y, cur_z, d, sample_delta); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // Find key with lowest time value nuclear@0: else if ((*cur_x).time <= (*cur_y).time && !end_x) { nuclear@0: nuclear@0: if ((*cur_z).time <= (*cur_x).time && !end_z) { nuclear@0: InterpolateTrack(out,fill,(*cur_z).time); nuclear@0: } nuclear@0: else { nuclear@0: InterpolateTrack(out,fill,(*cur_x).time); nuclear@0: } nuclear@0: } nuclear@0: else if ((*cur_z).time <= (*cur_y).time && !end_y) { nuclear@0: InterpolateTrack(out,fill,(*cur_y).time); nuclear@0: } nuclear@0: else if (!end_y) { nuclear@0: // welcome on the server, y nuclear@0: InterpolateTrack(out,fill,(*cur_y).time); nuclear@0: } nuclear@0: else { nuclear@0: // we have reached the end of at least 2 channels, nuclear@0: // only one is remaining. Extrapolate the 2. nuclear@0: if (end_y) { nuclear@0: InterpolateTrack(out,fill,(end_x ? (*cur_z) : (*cur_x)).time); nuclear@0: } nuclear@0: else if (end_x) { nuclear@0: InterpolateTrack(out,fill,(end_z ? (*cur_y) : (*cur_z)).time); nuclear@0: } nuclear@0: else { // if (end_z) nuclear@0: InterpolateTrack(out,fill,(end_y ? (*cur_x) : (*cur_y)).time); nuclear@0: } nuclear@0: } nuclear@0: lasttime = fill.mTime; nuclear@0: out.push_back(fill); nuclear@0: nuclear@0: if (lasttime >= (*cur_x).time) { nuclear@0: if (cur_x != envl_x->keys.end()-1) nuclear@0: ++cur_x; nuclear@0: else end_x = true; nuclear@0: } nuclear@0: if (lasttime >= (*cur_y).time) { nuclear@0: if (cur_y != envl_y->keys.end()-1) nuclear@0: ++cur_y; nuclear@0: else end_y = true; nuclear@0: } nuclear@0: if (lasttime >= (*cur_z).time) { nuclear@0: if (cur_z != envl_z->keys.end()-1) nuclear@0: ++cur_z; nuclear@0: else end_z = true; nuclear@0: } nuclear@0: nuclear@0: if( end_x && end_y && end_z ) /* finished? */ nuclear@0: break; nuclear@0: } nuclear@0: nuclear@0: if (flags & AI_LWO_ANIM_FLAG_START_AT_ZERO) { nuclear@0: for (std::vector::iterator it = out.begin(); it != out.end(); ++it) nuclear@0: (*it).mTime -= first; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Extract animation channel nuclear@0: void AnimResolver::ExtractAnimChannel(aiNodeAnim** out, unsigned int flags /*= 0*/) nuclear@0: { nuclear@0: *out = NULL; nuclear@0: nuclear@0: nuclear@0: //FIXME: crashes if more than one component is animated at different timings, to be resolved. nuclear@0: nuclear@0: // If we have no envelopes, return NULL nuclear@0: if (envelopes.empty()) { nuclear@0: return; nuclear@0: } nuclear@0: nuclear@0: // We won't spawn an animation channel if we don't have at least one envelope with more than one keyframe defined. nuclear@0: const bool trans = ((trans_x && trans_x->keys.size() > 1) || (trans_y && trans_y->keys.size() > 1) || (trans_z && trans_z->keys.size() > 1)); nuclear@0: const bool rotat = ((rotat_x && rotat_x->keys.size() > 1) || (rotat_y && rotat_y->keys.size() > 1) || (rotat_z && rotat_z->keys.size() > 1)); nuclear@0: const bool scale = ((scale_x && scale_x->keys.size() > 1) || (scale_y && scale_y->keys.size() > 1) || (scale_z && scale_z->keys.size() > 1)); nuclear@0: if (!trans && !rotat && !scale) nuclear@0: return; nuclear@0: nuclear@0: // Allocate the output animation nuclear@0: aiNodeAnim* anim = *out = new aiNodeAnim(); nuclear@0: nuclear@0: // Setup default animation setup if necessary nuclear@0: if (need_to_setup) { nuclear@0: UpdateAnimRangeSetup(); nuclear@0: need_to_setup = false; nuclear@0: } nuclear@0: nuclear@0: // copy translation keys nuclear@0: if (trans) { nuclear@0: std::vector keys; nuclear@0: GetKeys(keys,trans_x,trans_y,trans_z,flags); nuclear@0: nuclear@0: anim->mPositionKeys = new aiVectorKey[ anim->mNumPositionKeys = keys.size() ]; nuclear@0: std::copy(keys.begin(),keys.end(),anim->mPositionKeys); nuclear@0: } nuclear@0: nuclear@0: // copy rotation keys nuclear@0: if (rotat) { nuclear@0: std::vector keys; nuclear@0: GetKeys(keys,rotat_x,rotat_y,rotat_z,flags); nuclear@0: nuclear@0: anim->mRotationKeys = new aiQuatKey[ anim->mNumRotationKeys = keys.size() ]; nuclear@0: nuclear@0: // convert heading, pitch, bank to quaternion nuclear@0: // mValue.x=Heading=Rot(Y), mValue.y=Pitch=Rot(X), mValue.z=Bank=Rot(Z) nuclear@0: // Lightwave's rotation order is ZXY nuclear@0: aiVector3D X(1.0,0.0,0.0); nuclear@0: aiVector3D Y(0.0,1.0,0.0); nuclear@0: aiVector3D Z(0.0,0.0,1.0); nuclear@0: for (unsigned int i = 0; i < anim->mNumRotationKeys; ++i) { nuclear@0: aiQuatKey& qk = anim->mRotationKeys[i]; nuclear@0: qk.mTime = keys[i].mTime; nuclear@0: qk.mValue = aiQuaternion(Y,keys[i].mValue.x)*aiQuaternion(X,keys[i].mValue.y)*aiQuaternion(Z,keys[i].mValue.z); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: // copy scaling keys nuclear@0: if (scale) { nuclear@0: std::vector keys; nuclear@0: GetKeys(keys,scale_x,scale_y,scale_z,flags); nuclear@0: nuclear@0: anim->mScalingKeys = new aiVectorKey[ anim->mNumScalingKeys = keys.size() ]; nuclear@0: std::copy(keys.begin(),keys.end(),anim->mScalingKeys); nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: nuclear@0: #endif // no lwo or no lws