2015-05-31 18:53:16 +02:00
|
|
|
#include "rotatecontroller.hpp"
|
|
|
|
|
|
|
|
#include <osg/MatrixTransform>
|
2024-04-04 00:11:15 +01:00
|
|
|
#include <osgAnimation/Bone>
|
2015-05-31 18:53:16 +02:00
|
|
|
|
|
|
|
namespace MWRender
|
|
|
|
{
|
|
|
|
|
|
|
|
RotateController::RotateController(osg::Node* relativeTo)
|
|
|
|
: mEnabled(true)
|
|
|
|
, mRelativeTo(relativeTo)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void RotateController::setEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
mEnabled = enabled;
|
|
|
|
}
|
|
|
|
|
2021-10-05 12:21:12 +00:00
|
|
|
void RotateController::setRotate(const osg::Quat& rotate)
|
|
|
|
{
|
|
|
|
mRotate = rotate;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RotateController::setOffset(const osg::Vec3f& offset)
|
2015-05-31 18:53:16 +02:00
|
|
|
{
|
|
|
|
mOffset = offset;
|
|
|
|
}
|
|
|
|
|
2021-10-05 12:21:12 +00:00
|
|
|
void RotateController::operator()(osg::MatrixTransform* node, osg::NodeVisitor* nv)
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2015-05-31 18:53:16 +02:00
|
|
|
if (!mEnabled)
|
2022-09-22 21:26:05 +03:00
|
|
|
{
|
2015-05-31 18:53:16 +02:00
|
|
|
traverse(node, nv);
|
2022-09-22 21:26:05 +03:00
|
|
|
return;
|
|
|
|
}
|
2021-10-05 12:21:12 +00:00
|
|
|
osg::Matrix matrix = node->getMatrix();
|
2024-06-29 23:15:18 -05:00
|
|
|
|
|
|
|
osg::Quat worldOrient;
|
|
|
|
osg::NodePathList nodepaths = node->getParentalNodePaths(mRelativeTo);
|
|
|
|
|
|
|
|
if (!nodepaths.empty())
|
|
|
|
{
|
|
|
|
osg::Matrixf worldMat = osg::computeLocalToWorld(nodepaths[0]);
|
|
|
|
worldOrient = worldMat.getRotate();
|
|
|
|
}
|
|
|
|
|
2021-10-05 12:21:12 +00:00
|
|
|
osg::Quat worldOrientInverse = worldOrient.inverse();
|
2022-09-22 21:26:05 +03:00
|
|
|
|
2021-10-05 12:21:12 +00:00
|
|
|
osg::Quat orient = worldOrient * mRotate * worldOrientInverse * matrix.getRotate();
|
2015-05-31 18:53:16 +02:00
|
|
|
matrix.setRotate(orient);
|
2024-07-03 23:02:20 -05:00
|
|
|
matrix.setTrans(matrix.getTrans() + worldOrientInverse * mOffset);
|
2024-06-29 22:52:48 -05:00
|
|
|
|
2021-10-05 12:21:12 +00:00
|
|
|
node->setMatrix(matrix);
|
2015-05-31 18:53:16 +02:00
|
|
|
|
2024-04-04 00:11:15 +01:00
|
|
|
// If we are linked to a bone we must call setMatrixInSkeletonSpace
|
|
|
|
osgAnimation::Bone* b = dynamic_cast<osgAnimation::Bone*>(node);
|
|
|
|
if (b)
|
|
|
|
{
|
|
|
|
osgAnimation::Bone* parent = b->getBoneParent();
|
|
|
|
if (parent)
|
2024-04-11 02:16:06 +01:00
|
|
|
matrix *= parent->getMatrixInSkeletonSpace();
|
|
|
|
|
|
|
|
b->setMatrixInSkeletonSpace(matrix);
|
2024-04-04 00:11:15 +01:00
|
|
|
}
|
|
|
|
|
2015-05-31 18:53:16 +02:00
|
|
|
traverse(node, nv);
|
|
|
|
}
|
|
|
|
}
|