More fixes to the orbit manipulator

openmw-39
Aesylwinn 9 years ago
parent 2903271331
commit 342d31de76

@ -275,6 +275,7 @@ namespace CSVRender
, mRollLeft(false) , mRollLeft(false)
, mRollRight(false) , mRollRight(false)
, mCenter(0,0,0) , mCenter(0,0,0)
, mDistance(0)
, mCenterNode(new osg::PositionAttitudeTransform()) , mCenterNode(new osg::PositionAttitudeTransform())
{ {
group->addChild(mCenterNode); group->addChild(mCenterNode);
@ -343,7 +344,7 @@ namespace CSVRender
if (mode == "p-navi") if (mode == "p-navi")
{ {
rotateHorizontal(x * getMouseScalar()); rotateHorizontal(x * getMouseScalar());
rotateVertical(y * getMouseScalar()); rotateVertical(-y * getMouseScalar());
setModified(); setModified();
} }
else if (mode == "s-navi") else if (mode == "s-navi")
@ -353,7 +354,7 @@ namespace CSVRender
} }
else if (mode == "t-navi") else if (mode == "t-navi")
{ {
zoom(x * (mFast ? SpeedMultiplier : 1)); zoom(-x * (mFast ? SpeedMultiplier : 1));
} }
else else
{ {
@ -392,8 +393,6 @@ namespace CSVRender
mCenterNode->setPosition(mCenter); mCenterNode->setPosition(mCenter);
lookAtCenter();
// Normalize the matrix to counter drift // Normalize the matrix to counter drift
getCamera()->getViewMatrix().orthoNormal(getCamera()->getViewMatrix()); getCamera()->getViewMatrix().orthoNormal(getCamera()->getViewMatrix());
@ -412,6 +411,8 @@ namespace CSVRender
osg::Vec3d eye, up; osg::Vec3d eye, up;
getCamera()->getViewMatrixAsLookAt(eye, mCenter, up, DefaultStartDistance); getCamera()->getViewMatrixAsLookAt(eye, mCenter, up, DefaultStartDistance);
mDistance = DefaultStartDistance;
mInitialized = true; mInitialized = true;
} }
@ -430,26 +431,27 @@ namespace CSVRender
void OrbitCameraController::rotateHorizontal(double value) void OrbitCameraController::rotateHorizontal(double value)
{ {
osg::Vec3d position = getCamera()->getViewMatrix().getTrans(); osg::Vec3d eye, center, up;
osg::Vec3d offset = position - mCenter; getCamera()->getViewMatrixAsLookAt(eye, center, up);
osg::Quat rotation = getCamera()->getViewMatrix().getRotate();
osg::Quat offsetRotation = osg::Quat(value, LocalUp); osg::Quat rotation = osg::Quat(value, up);
osg::Vec3d newOffset = (rotation * offsetRotation) * (rotation.inverse() * offset); osg::Vec3d oldOffset = eye - mCenter;
osg::Vec3d newOffset = rotation * oldOffset;
getCamera()->getViewMatrix().setTrans(mCenter + newOffset); getCamera()->setViewMatrixAsLookAt(mCenter + newOffset, mCenter, up);
} }
void OrbitCameraController::rotateVertical(double value) void OrbitCameraController::rotateVertical(double value)
{ {
osg::Vec3d position = getCamera()->getViewMatrix().getTrans(); osg::Vec3d eye, center, up;
osg::Vec3d offset = position - mCenter; getCamera()->getViewMatrixAsLookAt(eye, center, up);
osg::Quat rotation = getCamera()->getViewMatrix().getRotate();
osg::Quat offsetRotation = osg::Quat(value, LocalLeft); osg::Vec3d forward = center - eye;
osg::Vec3d newOffset = (rotation * offsetRotation) * (rotation.inverse() * offset); osg::Quat rotation = osg::Quat(value, up ^ forward);
osg::Vec3d oldOffset = eye - mCenter;
osg::Vec3d newOffset = rotation * oldOffset;
getCamera()->getViewMatrix().setTrans(mCenter + newOffset); getCamera()->setViewMatrixAsLookAt(mCenter + newOffset, mCenter, up);
} }
void OrbitCameraController::roll(double value) void OrbitCameraController::roll(double value)
@ -465,24 +467,13 @@ namespace CSVRender
void OrbitCameraController::zoom(double value) void OrbitCameraController::zoom(double value)
{ {
osg::Vec3d dir = mCenter - getCamera()->getViewMatrix().getTrans(); mDistance = std::max(10., mDistance + value);
double distance = dir.normalize();
if (distance > 1 || value < 0) osg::Vec3d eye, center, up;
{ getCamera()->getViewMatrixAsLookAt(eye, center, up, 1.f);
getCamera()->getViewMatrix() *= osg::Matrixd::translate(dir * value);
}
}
void OrbitCameraController::lookAtCenter()
{
osg::Vec3d position = getCamera()->getViewMatrix().getTrans();
osg::Vec3d offset = mCenter - position;
osg::Quat rotation = getCamera()->getViewMatrix().getRotate();
osg::Quat newRotation; osg::Vec3d offset = (eye - center) * mDistance;
newRotation.makeRotate(LocalForward, offset);
getCamera()->getViewMatrix().setRotate(newRotation); getCamera()->setViewMatrixAsLookAt(mCenter + offset, mCenter, up);
} }
} }

@ -119,11 +119,10 @@ namespace CSVRender
void translate(const osg::Vec3d& offset); void translate(const osg::Vec3d& offset);
void zoom(double value); void zoom(double value);
void lookAtCenter();
bool mInitialized; bool mInitialized;
bool mFast, mLeft, mRight, mUp, mDown, mRollLeft, mRollRight; bool mFast, mLeft, mRight, mUp, mDown, mRollLeft, mRollRight;
osg::Vec3d mCenter; osg::Vec3d mCenter;
double mDistance;
osg::ref_ptr<osg::PositionAttitudeTransform> mCenterNode; osg::ref_ptr<osg::PositionAttitudeTransform> mCenterNode;
}; };

Loading…
Cancel
Save