Removed remaining dependancies on osg::Camera.

This commit is contained in:
Robert Osfield
2003-05-19 15:15:17 +00:00
parent 4151312dc5
commit 940ce67133
31 changed files with 506 additions and 616 deletions

View File

@@ -1,5 +1,4 @@
#include "GliderManipulator.h"
#include <osg/Notify>
using namespace osg;
@@ -10,6 +9,8 @@ GliderManipulator::GliderManipulator()
_modelScale = 0.01f;
_velocity = 0.0f;
_yawMode = YAW_AUTOMATICALLY_WHEN_BANKED;
_distance = 1.0f;
}
@@ -35,17 +36,23 @@ const osg::Node* GliderManipulator::getNode() const
}
osg::Node* GliderManipulator::getNode()
{
return _node.get();
}
void GliderManipulator::home(const GUIEventAdapter& ea,GUIActionAdapter& us)
{
if(_node.get() && _camera.get())
if(_node.get())
{
const osg::BoundingSphere& boundingSphere=_node->getBound();
osg::Vec3 eye = boundingSphere._center+osg::Vec3(-boundingSphere._radius*0.25f,-boundingSphere._radius*0.25f,-boundingSphere._radius*0.03f);
_camera->setView(eye,
eye+osg::Vec3(1.0f,1.0f,-0.1f),
computePosition(eye,
osg::Vec3(1.0f,1.0f,-0.1f),
osg::Vec3(0.0f,0.0f,1.0f));
_velocity = boundingSphere._radius*0.01f;
@@ -67,23 +74,17 @@ void GliderManipulator::init(const GUIEventAdapter& ea,GUIActionAdapter& us)
us.requestContinuousUpdate(false);
const osg::BoundingSphere& boundingSphere=_node->getBound();
_velocity = boundingSphere._radius*0.01f;
_velocity = 0.0f;
if (ea.getEventType()!=GUIEventAdapter::RESIZE)
{
us.requestWarpPointer((ea.getXmin()+ea.getXmax())/2.0f,(ea.getYmin()+ea.getYmax())/2.0f);
}
_camera->setFusionDistanceRatio(1/1000.0f);
}
bool GliderManipulator::handle(const GUIEventAdapter& ea,GUIActionAdapter& us)
{
if(!_camera.get()) return false;
switch(ea.getEventType())
{
case(GUIEventAdapter::PUSH):
@@ -132,6 +133,16 @@ bool GliderManipulator::handle(const GUIEventAdapter& ea,GUIActionAdapter& us)
us.requestContinuousUpdate(false);
return true;
}
else if (ea.getKey()=='q')
{
_yawMode = YAW_AUTOMATICALLY_WHEN_BANKED;
return true;
}
else if (ea.getKey()=='a')
{
_yawMode = NO_AUTOMATIC_YAW;
return true;
}
return false;
case(GUIEventAdapter::FRAME):
@@ -149,6 +160,12 @@ bool GliderManipulator::handle(const GUIEventAdapter& ea,GUIActionAdapter& us)
}
}
void GliderManipulator::getUsage(osg::ApplicationUsage& usage) const
{
usage.addKeyboardMouseBinding("Flight: Space","Reset the viewing position to home");
usage.addKeyboardMouseBinding("Flight: q","Automatically yaw when banked (default)");
usage.addKeyboardMouseBinding("Flight: a","No yaw when banked");
}
void GliderManipulator::flushMouseEventStack()
{
@@ -164,16 +181,53 @@ void GliderManipulator::addMouseEvent(const GUIEventAdapter& ea)
}
void GliderManipulator::setByMatrix(const osg::Matrix& matrix)
{
_eye = matrix.getTrans();
_rotation.set(matrix);
_distance = 1.0f;
}
osg::Matrix GliderManipulator::getMatrix() const
{
return osg::Matrix::rotate(_rotation)*osg::Matrix::translate(_eye);
}
osg::Matrix GliderManipulator::getInverseMatrix() const
{
return osg::Matrix::translate(-_eye)*osg::Matrix::rotate(_rotation.inverse());
}
void GliderManipulator::computePosition(const osg::Vec3& eye,const osg::Vec3& lv,const osg::Vec3& up)
{
osg::Vec3 f(lv);
f.normalize();
osg::Vec3 s(f^up);
s.normalize();
osg::Vec3 u(s^f);
u.normalize();
osg::Matrix rotation_matrix(s[0], u[0], -f[0], 0.0f,
s[1], u[1], -f[1], 0.0f,
s[2], u[2], -f[2], 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
_eye = eye;
_distance = lv.length();
_rotation.set(rotation_matrix);
_rotation = _rotation.inverse();
}
bool GliderManipulator::calcMovement()
{
_camera->setFusionDistanceMode(osg::Camera::PROPORTIONAL_TO_SCREEN_DISTANCE);
_camera->setFusionDistanceRatio(1/300.0f);
//_camera->setFusionDistanceMode(osg::Camera::PROPORTIONAL_TO_SCREEN_DISTANCE);
// return if less then two events have been added.
if (_ga_t0.get()==NULL || _ga_t1.get()==NULL) return false;
float dt = _ga_t0->time()-_ga_t1->time();
double dt = _ga_t0->time()-_ga_t1->time();
if (dt<0.0f)
{
@@ -206,29 +260,44 @@ bool GliderManipulator::calcMovement()
float dx = _ga_t0->getXnormalized();
float dy = _ga_t0->getYnormalized();
osg::Vec3 center = _camera->getEyePoint();
osg::Vec3 sv = _camera->getSideVector();
osg::Vec3 lv = _camera->getLookVector();
float pitch = inDegrees(-dy*70.0f*dt);
float roll = inDegrees(dx*60.0f*dt);
osg::Matrix rotation_matrix;
rotation_matrix.makeRotate(_rotation);
osg::Vec3 up = osg::Vec3(0.0f,1.0f,0.0) * rotation_matrix;
osg::Vec3 lv = osg::Vec3(0.0f,0.0f,-1.0f) * rotation_matrix;
osg::Vec3 sv = lv^up;
sv.normalize();
float pitch = -inDegrees(dy*75.0f*dt);
float roll = inDegrees(dx*50.0f*dt);
osg::Quat delta_rotate;
osg::Quat roll_rotate;
osg::Quat pitch_rotate;
pitch_rotate.makeRotate(pitch,sv.x(),sv.y(),sv.z());
roll_rotate.makeRotate(roll,lv.x(),lv.y(),lv.z());
delta_rotate = pitch_rotate*roll_rotate;
osg::Matrix mat;
mat.makeTranslate(-center);
mat *= Matrix::rotate(pitch,sv.x(),sv.y(),sv.z());
mat *= Matrix::rotate(roll,lv.x(),lv.y(),lv.z());
if (_yawMode==YAW_AUTOMATICALLY_WHEN_BANKED)
{
float bank = asinf(sv.z());
float yaw = inRadians(bank)*dt;
mat *= Matrix::rotate(yaw,0.0f,0.0f,1.0f);
osg::Quat yaw_rotate;
yaw_rotate.makeRotate(yaw,0.0f,0.0f,1.0f);
delta_rotate = delta_rotate*yaw_rotate;
}
lv *= (_velocity*dt);
mat *= Matrix::translate(center+lv);
_camera->transformLookAt(mat);
_eye += lv;
_rotation = _rotation*delta_rotate;
return true;
}

View File

@@ -1,53 +1,89 @@
#ifndef HANGGLIDE_GLIDERMANIPULATOR
#define HANGGLIDE_GLIDERMANIPULATOR 1
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osgGA/CameraManipulator>
#ifndef OSGGA_GliderMANIPULATOR
#define OSGGA_GliderMANIPULATOR 1
class GliderManipulator : public osgGA::CameraManipulator
#include <osgGA/MatrixManipulator>
#include <osg/Quat>
/**
GliderManipulator is a MatrixManipulator which provides Glider simulator-like
updating of the camera position & orientation. By default, the left mouse
button accelerates, the right mouse button decelerates, and the middle mouse
button (or left and right simultaneously) stops dead.
*/
class OSGGA_EXPORT GliderManipulator : public osgGA::MatrixManipulator
{
public:
GliderManipulator();
virtual ~GliderManipulator();
GliderManipulator();
virtual const char* className() const { return "Glider"; }
/** set the position of the matrix manipulator using a 4x4 Matrix.*/
virtual void setByMatrix(const osg::Matrix& matrix);
/** set the position of the matrix manipulator using a 4x4 Matrix.*/
virtual void setByInverseMatrix(const osg::Matrix& matrix) { setByMatrix(osg::Matrix::inverse(matrix)); }
/** get the position of the manipulator as 4x4 Matrix.*/
virtual osg::Matrix getMatrix() const;
/** get the position of the manipulator as a inverse matrix of the manipulator, typically used as a model view matrix.*/
virtual osg::Matrix getInverseMatrix() const;
/** Attach a node to the manipulator.
Automatically detaches previously attached node.
setNode(NULL) detaches previously nodes.
Is ignored by manipulators which do not require a reference model.*/
virtual void setNode(osg::Node*);
/** Return node if attached.*/
virtual const osg::Node* getNode() const;
/** Move the camera to the default position.
May be ignored by manipulators if home functionality is not appropriate.*/
virtual osg::Node* getNode();
virtual void home(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& us);
/** Start/restart the manipulator.*/
virtual void init(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& us);
/** handle events, return true if handled, false otherwise.*/
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& us);
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& us);
/** Get the keyboard and mouse usage of this manipulator.*/
virtual void getUsage(osg::ApplicationUsage& usage) const;
enum YawControlMode {
YAW_AUTOMATICALLY_WHEN_BANKED,
NO_AUTOMATIC_YAW
YAW_AUTOMATICALLY_WHEN_BANKED,
NO_AUTOMATIC_YAW
};
/** Set the yaw control between no yaw and yawing when banked.*/
/** Configure the Yaw control for the Glider model. */
void setYawControlMode(YawControlMode ycm) { _yawMode = ycm; }
private:
protected:
virtual ~GliderManipulator();
/** Reset the internal GUIEvent stack.*/
void flushMouseEventStack();
/** Add the current mouse GUIEvent to internal stack.*/
void addMouseEvent(const osgGA::GUIEventAdapter& ea);
void computePosition(const osg::Vec3& eye,const osg::Vec3& lv,const osg::Vec3& up);
/** For the give mouse movement calculate the movement of the camera.
Return true is camera has moved and a redraw is required.*/
bool calcMovement();
// Internal event stack comprising last three mouse events.
osg::ref_ptr<const osgGA::GUIEventAdapter> _ga_t1;
osg::ref_ptr<const osgGA::GUIEventAdapter> _ga_t0;
@@ -56,8 +92,12 @@ class GliderManipulator : public osgGA::CameraManipulator
float _modelScale;
float _velocity;
YawControlMode _yawMode;
osg::Vec3 _eye;
osg::Quat _rotation;
float _distance;
};

View File

@@ -45,17 +45,15 @@ osg::Node* TestManipulator::getNode()
/*ea*/
void TestManipulator::home(const GUIEventAdapter& ,GUIActionAdapter& us)
{
if(_node.get() && _camera.get())
if(_node.get())
{
const osg::BoundingSphere& boundingSphere=_node->getBound();
_camera->setView(boundingSphere.center()+osg::Vec3(0.0f, 0.0f, 20.0f),
boundingSphere.center()+osg::Vec3(0.0f, 1.0f, 20.0f),
computePosition(boundingSphere.center()+osg::Vec3(0.0f, 0.0f, 20.0f),
osg::Vec3(0.0f, 1.0f, 20.0f),
osg::Vec3(0.0f, 0.0f, 1.0f));
computeLocalDataFromCamera();
us.requestRedraw();
}
}
@@ -64,14 +62,10 @@ void TestManipulator::home(const GUIEventAdapter& ,GUIActionAdapter& us)
void TestManipulator::init(const GUIEventAdapter& ,GUIActionAdapter& )
{
flushMouseEventStack();
computeLocalDataFromCamera();
}
bool TestManipulator::handle(const GUIEventAdapter& ea,GUIActionAdapter& us)
{
if(!_camera.get()) return false;
switch(ea.getEventType())
{
case(GUIEventAdapter::PUSH):
@@ -142,26 +136,9 @@ bool TestManipulator::handle(const GUIEventAdapter& ea,GUIActionAdapter& us)
us.requestRedraw();
us.requestContinuousUpdate(false);
return true;
} else if (ea.getKey()=='+')
{
_camera->setFusionDistanceRatio(_camera->getFusionDistanceRatio()*1.25f);
return true;
}
else if (ea.getKey()=='-')
{
_camera->setFusionDistanceRatio(_camera->getFusionDistanceRatio()/1.25f);
return true;
}
// this is quick hack to test out othographic projection.
// else if (ea.getKey()=='O')
// {
// float dist = _camera->getLookDistance();
// _camera->setOrtho(-dist,dist,-dist,dist,-dist,dist);
// return true;
// }
}
return false;
case(GUIEventAdapter::FRAME):
_camera->setFusionDistanceMode(osg::Camera::PROPORTIONAL_TO_LOOK_DISTANCE);
if (_thrown)
{
if (calcMovement()) us.requestRedraw();
@@ -202,39 +179,41 @@ void TestManipulator::addMouseEvent(const GUIEventAdapter& ea)
_ga_t0 = &ea;
}
void TestManipulator::computeLocalDataFromCamera()
void TestManipulator::setByMatrix(const osg::Matrix& matrix)
{
// maths from gluLookAt/osg::Matrix::makeLookAt
osg::Vec3 f(_camera->getCenterPoint()-_camera->getEyePoint());
_center = matrix.getTrans();
_rotation.set(matrix);
_distance = 1.0f;
}
osg::Matrix TestManipulator::getMatrix() const
{
return osg::Matrix::rotate(_rotation)*osg::Matrix::translate(_center);
}
osg::Matrix TestManipulator::getInverseMatrix() const
{
return osg::Matrix::translate(-_center)*osg::Matrix::rotate(_rotation.inverse());
}
void TestManipulator::computePosition(const osg::Vec3& eye,const osg::Vec3& lv,const osg::Vec3& up)
{
osg::Vec3 f(lv);
f.normalize();
osg::Vec3 s(f^_camera->getUpVector());
osg::Vec3 s(f^up);
s.normalize();
osg::Vec3 u(s^f);
u.normalize();
osg::Matrix rotation_matrix(s[0], u[0], -f[0], 0.0f,
s[1], u[1], -f[1], 0.0f,
s[2], u[2], -f[2], 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
_center = _camera->getCenterPoint();
_distance = _camera->getLookDistance();
_center = eye+lv;
_distance = lv.length();
_rotation.set(rotation_matrix);
_rotation = _rotation.inverse();
}
void TestManipulator::computeCameraFromLocalData()
{
osg::Matrix new_rotation;
new_rotation.makeRotate(_rotation);
osg::Vec3 up = osg::Vec3(0.0f,1.0f,0.0) * new_rotation;
osg::Vec3 eye = (osg::Vec3(0.0f,0.0f,_distance) * new_rotation) + _center;
_camera->setLookAt(eye,_center,up);
}
@@ -262,8 +241,6 @@ bool TestManipulator::calcMovement()
_rotation = _rotation*new_rotate;
computeCameraFromLocalData();
return true;
}
@@ -276,104 +253,24 @@ bool TestManipulator::calcMovement()
_center += dv;
computeCameraFromLocalData();
return true;
}
else if (buttonMask==GUIEventAdapter::RIGHT_MOUSE_BUTTON)
{
osg::Vec3 uv = _camera->getUpVector();
osg::Vec3 sv = _camera->getSideVector();
osg::Matrix rotation_matrix;
_rotation.get(rotation_matrix);
osg::Vec3 uv = osg::Vec3(0.0f,1.0f,0.0f)*rotation_matrix;
osg::Vec3 sv = osg::Vec3(1.0f,0.0f,0.0f)*rotation_matrix;
osg::Vec3 fv = uv ^ sv;
osg::Vec3 dv = fv*(dy*-500.0f)-sv*(dx*500.0f);
_center += dv;
computeCameraFromLocalData();
return true;
}
return false;
}
/*
* This size should really be based on the distance from the center of
* rotation to the point on the object underneath the mouse. That
* point would then track the mouse as closely as possible. This is a
* simple example, though, so that is left as an Exercise for the
* Programmer.
*/
const float TRACKBALLSIZE = 0.8f;
/*
* Ok, simulate a track-ball. Project the points onto the virtual
* trackball, then figure out the axis of rotation, which is the cross
* product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
* Note: This is a deformed trackball-- is a trackball in the center,
* but is deformed into a hyperbolic sheet of rotation away from the
* center. This particular function was chosen after trying out
* several variations.
*
* It is assumed that the arguments to this routine are in the range
* (-1.0 ... 1.0)
*/
void TestManipulator::trackball(osg::Vec3& axis,float& angle, float p1x, float p1y, float p2x, float p2y)
{
/*
* First, figure out z-coordinates for projection of P1 and P2 to
* deformed sphere
*/
osg::Vec3 uv = _camera->getUpVector();
osg::Vec3 sv = _camera->getSideVector();
osg::Vec3 lv = _camera->getLookVector();
osg::Vec3 p1 = sv*p1x+uv*p1y-lv*tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y);
osg::Vec3 p2 = sv*p2x+uv*p2y-lv*tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y);
/*
* Now, we want the cross product of P1 and P2
*/
axis = p2^p1;
axis.normalize();
/*
* Figure out how much to rotate around that axis.
*/
float t = (p2-p1).length() / (2.0*TRACKBALLSIZE);
/*
* Avoid problems with out-of-control values...
*/
if (t > 1.0) t = 1.0;
if (t < -1.0) t = -1.0;
angle = inRadians(asin(t));
}
/*
* Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
* if we are away from the center of the sphere.
*/
float TestManipulator::tb_project_to_sphere(float r, float x, float y)
{
float d, t, z;
d = sqrt(x*x + y*y);
/* Inside sphere */
if (d < r * 0.70710678118654752440)
{
z = sqrt(r*r - d*d);
} /* On hyperbola */
else
{
t = r / 1.41421356237309504880;
z = t*t / d;
}
return z;
}

View File

@@ -5,15 +5,28 @@
#ifndef OSGGA_TESTMANIPULATOR
#define OSGGA_TESTMANIPULATOR 1
#include <osgGA/CameraManipulator>
#include <osgGA/MatrixManipulator>
#include <osg/Quat>
class TestManipulator : public osgGA::CameraManipulator
class TestManipulator : public osgGA::MatrixManipulator
{
public:
TestManipulator();
virtual ~TestManipulator();
/** set the position of the matrix manipulator using a 4x4 Matrix.*/
virtual void setByMatrix(const osg::Matrix& matrix);
/** set the position of the matrix manipulator using a 4x4 Matrix.*/
virtual void setByInverseMatrix(const osg::Matrix& matrix) { setByMatrix(osg::Matrix::inverse(matrix)); }
/** get the position of the manipulator as 4x4 Matrix.*/
virtual osg::Matrix getMatrix() const;
/** get the position of the manipulator as a inverse matrix of the manipulator, typically used as a model view matrix.*/
virtual osg::Matrix getInverseMatrix() const;
/** Attach a node to the manipulator.
Automatically detaches previously attached node.
setNode(NULL) detaches previously nodes.
@@ -44,18 +57,12 @@ class TestManipulator : public osgGA::CameraManipulator
/** Add the current mouse GUIEvent to internal stack.*/
void addMouseEvent(const osgGA::GUIEventAdapter& ea);
void computeLocalDataFromCamera();
void computeCameraFromLocalData();
void computePosition(const osg::Vec3& eye,const osg::Vec3& lv,const osg::Vec3& up);
/** For the give mouse movement calculate the movement of the camera.
Return true is camera has moved and a redraw is required.*/
bool calcMovement();
void trackball(osg::Vec3& axis,float& angle, float p1x, float p1y, float p2x, float p2y);
float tb_project_to_sphere(float r, float x, float y);
/** Check the speed at which the mouse is moving.
If speed is below a threshold then return false, otherwise return true.*/
bool isMouseMoving();

View File

@@ -386,9 +386,7 @@ int main(int argc, char **argv)
viewer.setSceneData(root);
// create the windows and run the threads.
// viewer.realize();
// run single threaded since osgParticle still writes during cull.
viewer.realize(Producer::CameraGroup::SingleThreaded);
viewer.realize();
while( !viewer.done() )
{

View File

@@ -513,7 +513,6 @@ int main( int argc, char **argv )
sceneview->setCullMaskLeft(0x00000001);
sceneview->setCullMaskRight(0x00000002);
sceneview->setFusionDistance(osgUtil::SceneView::USE_FUSION_DISTANCE_VALUE,radius);
sceneview->setCamera(0);
}