From 93468a5e0b58dbfa08490e188e9f1f7329dd38bc Mon Sep 17 00:00:00 2001 From: Rexbas Date: Sun, 5 Nov 2023 16:01:14 +0100 Subject: [PATCH] Gui: Fix window center rotation mode and orthographic view clipping --- src/Gui/NavigationStyle.cpp | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/Gui/NavigationStyle.cpp b/src/Gui/NavigationStyle.cpp index b49e1ed45f..4f39c63ae6 100644 --- a/src/Gui/NavigationStyle.cpp +++ b/src/Gui/NavigationStyle.cpp @@ -503,17 +503,37 @@ void NavigationStyle::reorientCamera(SoCamera* camera, const SbRotation& rotatio // Set new orientation value by accumulating the new rotation camera->orientation = rotation * camera->orientation.getValue(); - // Fix issue with near clipping in orthogonal view - if (camera->getTypeId().isDerivedFrom(SoOrthographicCamera::getClassTypeId())) { - camera->focalDistance = static_cast(camera)->height; - } - // Distance from rotation center to new camera position in global coordinate system SbVec3f newRotationCenterDistance; camera->orientation.getValue().multVec(rotationCenterDistanceCam, newRotationCenterDistance); // Reposition camera so the rotation center stays in the same place camera->position = rotationCenter + newRotationCenterDistance; + + // Fix issue with near clipping in orthogonal view + if (camera->getTypeId().isDerivedFrom(SoOrthographicCamera::getClassTypeId())) { + + // Find a bounding sphere for the scene + SoGetBoundingBoxAction action(viewer->getSoRenderManager()->getViewportRegion()); + action.apply(viewer->getSceneGraph()); + SbSphere boundingSphere; + boundingSphere.circumscribe(action.getBoundingBox()); + + // The center of the bounding sphere in camera coordinate system + SbVec3f center; + camera->orientation.getValue().inverse().multVec(boundingSphere.getCenter() - camera->position.getValue(), center); + + SbVec3f dir; + camera->orientation.getValue().multVec(SbVec3f(0, 0, -1), dir); + + // Reposition the camera but keep the focal point the same + // nearDistance is 0 and farDistance is the diameter of the bounding sphere + float repositionDistance = -center.getValue()[2] - boundingSphere.getRadius(); + camera->position = camera->position.getValue() + repositionDistance * dir; + camera->nearDistance = 0; + camera->farDistance = 2 * boundingSphere.getRadius(); + camera->focalDistance = camera->focalDistance.getValue() - repositionDistance; + } } void NavigationStyle::panCamera(SoCamera * cam, float aspectratio, const SbPlane & panplane,