From 6d82f8b00d006d693ae7b6531ff90c07f1a1d354 Mon Sep 17 00:00:00 2001 From: Alexei Kotov Date: Wed, 13 Sep 2023 23:55:26 +0300 Subject: [PATCH] Refactor NiKeyframeController and NiTransformInterpolator --- components/nif/controller.cpp | 19 +++++-------------- components/nif/controller.hpp | 6 ++---- components/nifosg/controller.cpp | 30 +++++++++++++++++------------- 3 files changed, 24 insertions(+), 31 deletions(-) diff --git a/components/nif/controller.cpp b/components/nif/controller.cpp index 0e08c4d255..7d6706aeae 100644 --- a/components/nif/controller.cpp +++ b/components/nif/controller.cpp @@ -292,6 +292,7 @@ namespace Nif void NiKeyframeController::read(NIFStream* nif) { NiSingleInterpController::read(nif); + if (nif->getVersion() <= NIFStream::generateVersion(10, 1, 0, 103)) mData.read(nif); } @@ -299,6 +300,7 @@ namespace Nif void NiKeyframeController::post(Reader& nif) { NiSingleInterpController::post(nif); + mData.post(nif); } @@ -503,24 +505,13 @@ namespace Nif void NiTransformInterpolator::read(NIFStream* nif) { - defaultPos = nif->getVector3(); - nif->read(defaultRot); - defaultScale = nif->getFloat(); - if (nif->getVersion() <= NIFStream::generateVersion(10, 1, 0, 109)) - { - if (!nif->getBoolean()) - defaultPos = osg::Vec3f(); - if (!nif->getBoolean()) - defaultRot = osg::Quat(); - if (!nif->getBoolean()) - defaultScale = 1.f; - } - data.read(nif); + nif->read(mDefaultTransform); + mData.read(nif); } void NiTransformInterpolator::post(Reader& nif) { - data.post(nif); + mData.post(nif); } void NiColorInterpolator::read(NIFStream* nif) diff --git a/components/nif/controller.hpp b/components/nif/controller.hpp index 4b46840d4a..24d5b17465 100644 --- a/components/nif/controller.hpp +++ b/components/nif/controller.hpp @@ -344,10 +344,8 @@ namespace Nif struct NiTransformInterpolator : public NiInterpolator { - osg::Vec3f defaultPos; - osg::Quat defaultRot; - float defaultScale; - NiKeyframeDataPtr data; + NiQuatTransform mDefaultTransform; + NiKeyframeDataPtr mData; void read(NIFStream* nif) override; void post(Reader& nif) override; diff --git a/components/nifosg/controller.cpp b/components/nifosg/controller.cpp index 8e9efba8a9..88a88f46be 100644 --- a/components/nifosg/controller.cpp +++ b/components/nifosg/controller.cpp @@ -90,21 +90,23 @@ namespace NifOsg { const Nif::NiTransformInterpolator* interp = static_cast(keyctrl->mInterpolator.getPtr()); - if (!interp->data.empty()) + const Nif::NiQuatTransform& defaultTransform = interp->mDefaultTransform; + if (!interp->mData.empty()) { - mRotations = QuaternionInterpolator(interp->data->mRotations, interp->defaultRot); - mXRotations = FloatInterpolator(interp->data->mXRotations); - mYRotations = FloatInterpolator(interp->data->mYRotations); - mZRotations = FloatInterpolator(interp->data->mZRotations); - mTranslations = Vec3Interpolator(interp->data->mTranslations, interp->defaultPos); - mScales = FloatInterpolator(interp->data->mScales, interp->defaultScale); - mAxisOrder = interp->data->mAxisOrder; + mRotations = QuaternionInterpolator(interp->mData->mRotations, defaultTransform.mRotation); + mXRotations = FloatInterpolator(interp->mData->mXRotations); + mYRotations = FloatInterpolator(interp->mData->mYRotations); + mZRotations = FloatInterpolator(interp->mData->mZRotations); + mTranslations = Vec3Interpolator(interp->mData->mTranslations, defaultTransform.mTranslation); + mScales = FloatInterpolator(interp->mData->mScales, defaultTransform.mScale); + + mAxisOrder = interp->mData->mAxisOrder; } else { - mRotations = QuaternionInterpolator(Nif::QuaternionKeyMapPtr(), interp->defaultRot); - mTranslations = Vec3Interpolator(Nif::Vector3KeyMapPtr(), interp->defaultPos); - mScales = FloatInterpolator(Nif::FloatKeyMapPtr(), interp->defaultScale); + mRotations = QuaternionInterpolator(Nif::QuaternionKeyMapPtr(), defaultTransform.mRotation); + mTranslations = Vec3Interpolator(Nif::Vector3KeyMapPtr(), defaultTransform.mTranslation); + mScales = FloatInterpolator(Nif::FloatKeyMapPtr(), defaultTransform.mScale); } } } @@ -117,6 +119,7 @@ namespace NifOsg mZRotations = FloatInterpolator(keydata->mZRotations); mTranslations = Vec3Interpolator(keydata->mTranslations); mScales = FloatInterpolator(keydata->mScales, 1.f); + mAxisOrder = keydata->mAxisOrder; } } @@ -177,11 +180,12 @@ namespace NifOsg else node->setRotation(node->mRotationScale); + if (!mTranslations.empty()) + node->setTranslation(mTranslations.interpKey(time)); + if (!mScales.empty()) node->setScale(mScales.interpKey(time)); - if (!mTranslations.empty()) - node->setTranslation(mTranslations.interpKey(time)); } traverse(node, nv);