Restore ranged weapon aiming
parent
71bafcb52b
commit
ed4863ad7d
@ -0,0 +1,57 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
void RotateController::operator()(osg::Node *node, osg::NodeVisitor *nv)
|
||||
{
|
||||
if (!mEnabled)
|
||||
{
|
||||
traverse(node, nv);
|
||||
return;
|
||||
}
|
||||
osg::MatrixTransform* transform = static_cast<osg::MatrixTransform*>(node);
|
||||
osg::Matrix matrix = transform->getMatrix();
|
||||
osg::Quat worldOrient = getWorldOrientation(node);
|
||||
|
||||
osg::Quat orient = worldOrient * mRotate * worldOrient.inverse() * matrix.getRotate();
|
||||
matrix.setRotate(orient);
|
||||
|
||||
transform->setMatrix(matrix);
|
||||
|
||||
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
|
||||
osg::MatrixList worldMats = node->getWorldMatrices(mRelativeTo);
|
||||
osg::Quat worldOrient;
|
||||
if (!worldMats.empty())
|
||||
{
|
||||
osg::Matrixf worldMat = worldMats[0];
|
||||
worldOrient = worldMat.getRotate();
|
||||
}
|
||||
return worldOrient;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
#ifndef OPENMW_MWRENDER_ROTATECONTROLLER_H
|
||||
#define OPENMW_MWRENDER_ROTATECONTROLLER_H
|
||||
|
||||
#include <osg/NodeCallback>
|
||||
#include <osg/Quat>
|
||||
|
||||
namespace MWRender
|
||||
{
|
||||
|
||||
/// Applies a rotation in \a relativeTo's space.
|
||||
/// @note Assumes that the node being rotated has its "original" orientation set every frame by a different controller.
|
||||
/// The rotation is then applied on top of that orientation.
|
||||
/// @note Must be set on a MatrixTransform.
|
||||
class RotateController : public osg::NodeCallback
|
||||
{
|
||||
public:
|
||||
RotateController(osg::Node* relativeTo);
|
||||
|
||||
void setEnabled(bool enabled);
|
||||
|
||||
void setRotate(const osg::Quat& rotate);
|
||||
|
||||
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
|
||||
|
||||
protected:
|
||||
osg::Quat getWorldOrientation(osg::Node* node);
|
||||
|
||||
bool mEnabled;
|
||||
osg::Quat mRotate;
|
||||
osg::ref_ptr<osg::Node> mRelativeTo;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue