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