Cleanup, move setup to CameraController class

openmw-39
Aesylwinn 9 years ago
parent efa2ec21ff
commit a4cc891383

@ -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>
@ -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);
}

@ -3,7 +3,6 @@
#include <string>
#include <osg/BoundingBox>
#include <osg/ref_ptr>
#include <osg/Vec3d>
@ -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;

@ -12,7 +12,6 @@
#include <osgViewer/ViewerEventHandlers>
#include <osg/LightModel>
#include <osg/BoundingBox>
#include <osg/ComputeBoundsVisitor>
#include <osgGA/TrackballManipulator>
#include <osgGA/FirstPersonManipulator>
@ -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;
}
}

Loading…
Cancel
Save