Skip to content

Commit

Permalink
Cleanup, move setup to CameraController class
Browse files Browse the repository at this point in the history
  • Loading branch information
Aesylwinn committed Apr 2, 2016
1 parent efa2ec2 commit a4cc891
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
35 changes: 30 additions & 5 deletions apps/opencs/view/render/cameracontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

#include <QKeyEvent>

#include <osg/BoundingBox>
#include <osg/Camera>
#include <osg/ComputeBoundsVisitor>
#include <osg/Drawable>
#include <osg/Group>
#include <osg/Matrixd>
#include <osg/Quat>

Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions apps/opencs/view/render/cameracontroller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <string>

#include <osg/BoundingBox>
#include <osg/ref_ptr>
#include <osg/Vec3d>

Expand All @@ -12,6 +11,7 @@ class QKeyEvent;
namespace osg
{
class Camera;
class Group;
}

namespace CSVRender
Expand Down Expand Up @@ -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;
Expand Down
24 changes: 1 addition & 23 deletions apps/opencs/view/render/scenewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <osgViewer/ViewerEventHandlers>
#include <osg/LightModel>
#include <osg/BoundingBox>
#include <osg/ComputeBoundsVisitor>

#include <osgGA/TrackballManipulator>
#include <osgGA/FirstPersonManipulator>
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit a4cc891

Please sign in to comment.