From a4cc891383221c9eff156926680417c8c1771c00 Mon Sep 17 00:00:00 2001 From: Aesylwinn Date: Sat, 2 Apr 2016 13:36:12 -0400 Subject: [PATCH] Cleanup, move setup to CameraController class --- apps/opencs/view/render/cameracontroller.cpp | 35 +++++++++++++++++--- apps/opencs/view/render/cameracontroller.hpp | 4 +-- apps/opencs/view/render/scenewidget.cpp | 24 +------------- 3 files changed, 33 insertions(+), 30 deletions(-) diff --git a/apps/opencs/view/render/cameracontroller.cpp b/apps/opencs/view/render/cameracontroller.cpp index eb0515c28..57c87ecb3 100644 --- a/apps/opencs/view/render/cameracontroller.cpp +++ b/apps/opencs/view/render/cameracontroller.cpp @@ -2,8 +2,11 @@ #include +#include #include +#include #include +#include #include #include @@ -92,16 +95,38 @@ namespace CSVRender mWheelMoveMult = value; } - void CameraController::setSceneBounds(const osg::BoundingBox& bounds, const osg::Vec3d& up) + void CameraController::setup(osg::Group* root, unsigned int mask, const osg::Vec3d& up) { - osg::Vec3d minBounds = bounds.corner(0) - bounds.center(); - osg::Vec3d maxBounds = bounds.corner(7) - bounds.center(); + // Find World bounds + osg::ComputeBoundsVisitor boundsVisitor; + osg::BoundingBox& boundingBox = boundsVisitor.getBoundingBox(); + + boundsVisitor.setNodeMaskOverride(mask); + root->accept(boundsVisitor); + + if (!boundingBox.valid()) + { + // Try again without any mask + boundsVisitor.reset(); + boundsVisitor.setNodeMaskOverride(~0); + root->accept(boundsVisitor); + + // Last resort, set a default + if (!boundingBox.valid()) + { + boundingBox.set(-1, -1, -1, 1, 1, 1); + } + } + + // Calculate a good starting position + osg::Vec3d minBounds = boundingBox.corner(0) - boundingBox.center(); + osg::Vec3d maxBounds = boundingBox.corner(7) - boundingBox.center(); osg::Vec3d camOffset = up * maxBounds > 0 ? maxBounds : minBounds; camOffset *= 2; - osg::Vec3d eye = camOffset + bounds.center(); - osg::Vec3d center = bounds.center(); + osg::Vec3d eye = camOffset + boundingBox.center(); + osg::Vec3d center = boundingBox.center(); getCamera()->setViewMatrixAsLookAt(eye, center, up); } diff --git a/apps/opencs/view/render/cameracontroller.hpp b/apps/opencs/view/render/cameracontroller.hpp index 97a5ef2b2..bec76dd43 100644 --- a/apps/opencs/view/render/cameracontroller.hpp +++ b/apps/opencs/view/render/cameracontroller.hpp @@ -3,7 +3,6 @@ #include -#include #include #include @@ -12,6 +11,7 @@ class QKeyEvent; namespace osg { class Camera; + class Group; } namespace CSVRender @@ -43,7 +43,7 @@ namespace CSVRender void setWheelMovementMultiplier(double value); // moves the camera to an intelligent position - void setSceneBounds(const osg::BoundingBox& bounds, const osg::Vec3d& up); + void setup(osg::Group* root, unsigned int mask, const osg::Vec3d& up); virtual bool handleKeyEvent(QKeyEvent* event, bool pressed) = 0; virtual bool handleMouseMoveEvent(std::string mode, int x, int y) = 0; diff --git a/apps/opencs/view/render/scenewidget.cpp b/apps/opencs/view/render/scenewidget.cpp index 0ca495031..9923d6ef0 100644 --- a/apps/opencs/view/render/scenewidget.cpp +++ b/apps/opencs/view/render/scenewidget.cpp @@ -12,7 +12,6 @@ #include #include #include -#include #include #include @@ -315,28 +314,7 @@ void SceneWidget::update(double dt) } else { - osg::ComputeBoundsVisitor boundsVisitor; - osg::BoundingBox &boundingBox(boundsVisitor.getBoundingBox()); - boundsVisitor.setNodeMaskOverride(Mask_Reference | Mask_Terrain); - - mRootNode->accept(boundsVisitor); - - // Remove mask if nothing is found - if (!boundingBox.valid()) - { - boundsVisitor.reset(); - boundsVisitor.setNodeMaskOverride(~0); - mRootNode->accept(boundsVisitor); - } - - // Set a default if there is still nothing found - if (!boundingBox.valid()) - { - boundingBox.set(-1, -1, -1, 1, 1, 1); - } - - mCurrentCamControl->setSceneBounds(boundingBox, CameraController::WorldUp); - + mCurrentCamControl->setup(mRootNode, Mask_Reference | Mask_Terrain, CameraController::WorldUp); mCamPositionSet = true; } }