From b111febfc9d8d25dbe2d19af75d6c810a30c69fc Mon Sep 17 00:00:00 2001 From: Aesylwinn Date: Mon, 4 Apr 2016 20:56:13 -0400 Subject: [PATCH] Add pitch constraint for first person camera. --- apps/opencs/view/render/cameracontroller.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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; }