diff --git a/apps/opencs/view/render/cameracontroller.cpp b/apps/opencs/view/render/cameracontroller.cpp index cefd81003..68f0c76ef 100644 --- a/apps/opencs/view/render/cameracontroller.cpp +++ b/apps/opencs/view/render/cameracontroller.cpp @@ -1,5 +1,7 @@ #include "cameracontroller.hpp" +#include + #include #include @@ -314,6 +316,24 @@ namespace CSVRender void FreeCameraController::pitch(double value) { + const double Constraint = osg::PI / 2 - 0.1; + + if (mLockUpright) + { + osg::Vec3d eye, center, up; + getCamera()->getViewMatrixAsLookAt(eye, center, up); + + osg::Vec3d forward = center - eye; + osg::Vec3d left = up ^ forward; + + double pitchAngle = std::acos(up * mUp); + if ((mUp ^ up) * left < 0) + pitchAngle *= -1; + + if (std::abs(pitchAngle + value) > Constraint) + value = (pitchAngle > 0 ? 1 : -1) * Constraint - pitchAngle; + } + getCamera()->getViewMatrix() *= osg::Matrixd::rotate(value, LocalLeft); mModified = true; }