From f93655e803d1b611f53477872f41a37eac5a8679 Mon Sep 17 00:00:00 2001 From: Capostrophic Date: Sat, 11 Jul 2020 20:59:21 +0300 Subject: [PATCH] Encapsulate NIF transform changes in NifOsg::MatrixTransform --- components/nifosg/controller.cpp | 47 +++++++-------------------- components/nifosg/matrixtransform.cpp | 33 +++++++++++++++++++ components/nifosg/matrixtransform.hpp | 13 ++++++++ 3 files changed, 57 insertions(+), 36 deletions(-) diff --git a/components/nifosg/controller.cpp b/components/nifosg/controller.cpp index a4db2cba3e..203951eddc 100644 --- a/components/nifosg/controller.cpp +++ b/components/nifosg/controller.cpp @@ -119,48 +119,23 @@ void KeyframeController::operator() (osg::Node* node, osg::NodeVisitor* nv) if (hasInput()) { NifOsg::MatrixTransform* trans = static_cast(node); - osg::Matrix mat = trans->getMatrix(); float time = getInputValue(nv); - Nif::Matrix3& rot = trans->mRotationScale; - - bool setRot = false; - if(!mRotations.empty()) - { - mat.setRotate(mRotations.interpKey(time)); - setRot = true; - } + if (!mRotations.empty()) + trans->updateRotation(mRotations.interpKey(time)); else if (!mXRotations.empty() || !mYRotations.empty() || !mZRotations.empty()) - { - mat.setRotate(getXYZRotation(time)); - setRot = true; - } - else - { - // no rotation specified, use the previous value - for (int i=0;i<3;++i) - for (int j=0;j<3;++j) - mat(j,i) = rot.mValues[i][j]; // NB column/row major difference - } + trans->updateRotation(getXYZRotation(time)); + else // no rotation specified, use the previous value + trans->applyCurrentRotation(); - if (setRot) // copy the new values back - for (int i=0;i<3;++i) - for (int j=0;j<3;++j) - rot.mValues[i][j] = mat(j,i); // NB column/row major difference + if (!mScales.empty()) + trans->updateScale(mScales.interpKey(time)); + else // no scale specified, use the previous value + trans->applyCurrentScale(); - float& scale = trans->mScale; - if(!mScales.empty()) - scale = mScales.interpKey(time); - - for (int i=0;i<3;++i) - for (int j=0;j<3;++j) - mat(i,j) *= scale; - - if(!mTranslations.empty()) - mat.setTrans(mTranslations.interpKey(time)); - - trans->setMatrix(mat); + if (!mTranslations.empty()) + trans->setTranslation(mTranslations.interpKey(time)); } traverse(node, nv); diff --git a/components/nifosg/matrixtransform.cpp b/components/nifosg/matrixtransform.cpp index b61df834b6..12144b62a6 100644 --- a/components/nifosg/matrixtransform.cpp +++ b/components/nifosg/matrixtransform.cpp @@ -22,4 +22,37 @@ namespace NifOsg , mRotationScale(copy.mRotationScale) { } + + void MatrixTransform::applyCurrentRotation() + { + for (int i = 0; i < 3; ++i) + for (int j = 0; j < 3; ++j) + _matrix(j,i) = mRotationScale.mValues[i][j]; // NB column/row major difference + } + + void MatrixTransform::applyCurrentScale() + { + for (int i = 0; i < 3; ++i) + for (int j = 0; j < 3; ++j) + _matrix(i,j) *= mScale; + } + + void MatrixTransform::updateRotation(const osg::Quat& rotation) + { + _matrix.setRotate(rotation); + for (int i = 0; i < 3; ++i) + for (int j = 0; j < 3; ++j) + mRotationScale.mValues[i][j] = _matrix(j,i); // NB column/row major difference + } + + void MatrixTransform::updateScale(const float scale) + { + mScale = scale; + applyCurrentScale(); + } + + void MatrixTransform::setTranslation(const osg::Vec3f& translation) + { + _matrix.setTrans(translation); + } } diff --git a/components/nifosg/matrixtransform.hpp b/components/nifosg/matrixtransform.hpp index 86c9034911..b633efaad6 100644 --- a/components/nifosg/matrixtransform.hpp +++ b/components/nifosg/matrixtransform.hpp @@ -17,9 +17,22 @@ namespace NifOsg META_Node(NifOsg, MatrixTransform) + // Apply the current NIF rotation or scale to OSG matrix. + void applyCurrentRotation(); + void applyCurrentScale(); + + // Apply the given rotation to OSG matrix directly and update NIF rotation matrix. + void updateRotation(const osg::Quat& rotation); + // Update current NIF scale and apply it to OSG matrix. + void updateScale(const float scale); + + // Apply the given translation to OSG matrix. + void setTranslation(const osg::Vec3f& translation); + // NIF record index int mIndex{0}; + private: // Hack: account for Transform differences between OSG and NIFs. // OSG uses a 4x4 matrix, NIF's use a 3x3 rotationScale, float scale, and vec3 position. // Decomposing the original components from the 4x4 matrix isn't possible, which causes