2015-05-31 18:53:16 +02:00
|
|
|
#include "rotatecontroller.hpp"
|
|
|
|
|
|
|
|
#include <osg/MatrixTransform>
|
|
|
|
|
|
|
|
namespace MWRender
|
|
|
|
{
|
|
|
|
|
|
|
|
RotateController::RotateController(osg::Node *relativeTo)
|
|
|
|
: mEnabled(true)
|
|
|
|
, mRelativeTo(relativeTo)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void RotateController::setEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
mEnabled = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RotateController::setRotate(const osg::Quat &rotate)
|
|
|
|
{
|
|
|
|
mRotate = rotate;
|
|
|
|
}
|
|
|
|
|
2021-10-05 12:21:12 +00:00
|
|
|
void RotateController::setOffset(const osg::Vec3f& offset)
|
|
|
|
{
|
|
|
|
mOffset = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RotateController::operator()(osg::MatrixTransform *node, osg::NodeVisitor *nv)
|
2015-05-31 18:53:16 +02:00
|
|
|
{
|
|
|
|
if (!mEnabled)
|
|
|
|
{
|
|
|
|
traverse(node, nv);
|
|
|
|
return;
|
|
|
|
}
|
2021-10-05 12:21:12 +00:00
|
|
|
osg::Matrix matrix = node->getMatrix();
|
2015-05-31 18:53:16 +02:00
|
|
|
osg::Quat worldOrient = getWorldOrientation(node);
|
2021-10-05 12:21:12 +00:00
|
|
|
osg::Quat worldOrientInverse = worldOrient.inverse();
|
2015-05-31 18:53:16 +02: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);
|
2021-10-05 12:21:12 +00:00
|
|
|
matrix.setTrans(matrix.getTrans() + worldOrientInverse * mOffset);
|
2015-05-31 18:53:16 +02:00
|
|
|
|
2021-10-05 12:21:12 +00:00
|
|
|
node->setMatrix(matrix);
|
2015-05-31 18:53:16 +02:00
|
|
|
|
|
|
|
traverse(node,nv);
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::Quat RotateController::getWorldOrientation(osg::Node *node)
|
|
|
|
{
|
|
|
|
// this could be optimized later, we just need the world orientation, not the full matrix
|
2016-02-22 18:58:19 +01:00
|
|
|
osg::NodePathList nodepaths = node->getParentalNodePaths(mRelativeTo);
|
2015-05-31 18:53:16 +02:00
|
|
|
osg::Quat worldOrient;
|
2016-02-22 18:58:19 +01:00
|
|
|
if (!nodepaths.empty())
|
2015-05-31 18:53:16 +02:00
|
|
|
{
|
2016-02-22 18:58:19 +01:00
|
|
|
osg::Matrixf worldMat = osg::computeLocalToWorld(nodepaths[0]);
|
2015-05-31 18:53:16 +02:00
|
|
|
worldOrient = worldMat.getRotate();
|
|
|
|
}
|
|
|
|
return worldOrient;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|