|
楼主 |
发表于 2010-8-11 21:49:34
|
显示全部楼层
#include "TravelManipulator.h"
TravelManipulator::TravelManipulator():m_fMoveSpeed( 1.0f ),m_bLeftButtonDown( false ),m_bPeng( true ),m_fAngle( 2.5 ),m_fpushX( 0 ),m_fpushY (0)
{
m_vRotation = osg::Vec3(osg:I_2,0.0f,0.0f);
m_vPosition = osg::Vec3( 6.0f, -205.0f, 0.0f );
//这里是初始相机的位置和视口的位置 但是 如果要初始两个相机的话 该如何设置初始点呢??????
}
TravelManipulator::~TravelManipulator()
{
}
//把漫游加入场景
TravelManipulator* TravelManipulator::TravelToScene(osg::ref_ptr<osgViewer::Viewer> viewer)
{
TravelManipulator* camera = new TravelManipulator;
viewer->setCameraManipulator( camera );
camera->m_pHostViewer = viewer;
return camera;
}
void TravelManipulator::setByMatrix( const osg::Matrixd& matrix)
{
//
}
void TravelManipulator::setByInverseMatrix(const osg::Matrixd &matrix)
{
//
}
osg::Matrixd TravelManipulator::getMatrix(void) const
{
osg::Matrixd mat;
mat.makeRotate(m_vRotation._v[0], osg::Vec3(1.0f, 0.0f, 0.0f),
m_vRotation._v[1], osg::Vec3(0.0f, 1.0f, 0.0f),
m_vRotation._v[2], osg::Vec3(0.0f, 0.0f, 1.0f));
return mat * osg::Matrixd::translate(m_vPosition); //*************************
}
osg::Matrixd TravelManipulator::getInverseMatrix(void) const
{
osg::Matrixd mat;
mat.makeRotate(m_vRotation._v[0], osg::Vec3(1.0f, 0.0f, 0.0f),
m_vRotation._v[1], osg::Vec3(0.0f, 1.0f, 0.0f),
m_vRotation._v[2], osg::Vec3(0.0f, 0.0f, 1.0f));
return osg::Matrixd::inverse(mat * osg::Matrixd::translate(m_vPosition)); //***********************
}
bool TravelManipulator::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& us)
{
float mouseX = ea.getX();
float mouseY = ea.getY();
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter::KEYDOWN):
{
if (ea.getKey() == 0x6D )//0xFF09 tabjian
{
osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&us); //*****************
viewer->getCameraManipulator()->setHomePosition
(osg::Vec3( 106.0f, -205.0f, 0.0f ),osg::Vec3( 6.0f, -205.0f, 0.0f ),
osg::Vec3( 6.0f, -205.0f, 5.0f ));
//viewer->home();
return true;
}
//if (ea.getKey() == 0x20 0x0D)
//{
// us.requestRedraw();
// us.requestContinuousUpdate(false);
// return true;
//}
if (ea.getKey() == 0xFF50)
{
ChangePosition(osg::Vec3 (0, 0, m_fMoveSpeed)) ;
return true;
}
if (ea.getKey() == 0xFF57) //end
{
ChangePosition(osg::Vec3 (0, 0, -m_fMoveSpeed)) ;
return true;
}
if (ea.getKey() == 0x2B)//+
{
m_fMoveSpeed += 1.0f;
return true;
}
if (ea.getKey() == 0x2D)//-
{
m_fMoveSpeed -= 1.0f;
if (m_fMoveSpeed < 1.0f)
{
m_fMoveSpeed = 1.0f;
}
return true;
}
if (ea.getKey() == 0xFF52 || ea.getKey () == 0x57 || ea.getKey () == 0x77)//up
{
ChangePosition(osg::Vec3 (0, m_fMoveSpeed * sinf(osg::PI_2 + m_vRotation._v[2]), 0)) ;
ChangePosition(osg::Vec3 (m_fMoveSpeed * cosf(osg::PI_2 + m_vRotation._v[2]), 0, 0)) ;
return true;
}
if (ea.getKey() == 0xFF54 || ea.getKey () == 0x53 || ea.getKey () == 0x73 )//down
{
ChangePosition(osg::Vec3 (0, -m_fMoveSpeed * sinf(osg::PI_2 + m_vRotation._v[2]), 0)) ;
ChangePosition(osg::Vec3(-m_fMoveSpeed * cosf(osg::PI_2 + m_vRotation._v[2]), 0, 0)) ;
return true;
}
if (ea.getKey () == 0x41||ea.getKey () == 0x61)
{
ChangePosition(osg::Vec3 (0, m_fMoveSpeed * cosf(osg::PI_2 + m_vRotation._v[2]), 0)) ;
ChangePosition(osg::Vec3 (-m_fMoveSpeed * sinf(osg::PI_2 + m_vRotation._v[2]), 0, 0)) ;
return true;
}
if (ea.getKey () == 0x44||ea.getKey () == 0x64)
{
ChangePosition(osg::Vec3 (0,-m_fMoveSpeed * cosf(osg::PI_2 + m_vRotation._v[2]), 0)) ;
ChangePosition(osg::Vec3 (m_fMoveSpeed * sinf(osg::PI_2 + m_vRotation._v[2]), 0, 0)) ;
return true;
}
if (ea.getKey() == 0xFF53)//Right
{
m_vRotation._v[2] -= osg:egreesToRadians(m_fAngle);
}
if (ea.getKey()== 0xFF51)//Left
{
m_vRotation._v[2] += osg::DegreesToRadians(m_fAngle);
}
if (ea.getKey() == 0x46 || ea.getKey() == 0x66)//F
{
computeHomePosition();
m_fAngle -= 0.2 ;
return true ;
}
if (ea.getKey() == 0x47 || ea.getKey() == 0x67)//G
{
m_fAngle += 0.2 ;
return true ;
}
return false;
}
case (osgGA::GUIEventAdapter ::PUSH ):
//
if ( ea.getButton () == 1)
{
m_fpushX = mouseX ;
m_fpushY = mouseY ;
m_bLeftButtonDown = true ;
}
return false ;
case (osgGA::GUIEventAdapter ::DRAG ):
if ( m_bLeftButtonDown)
{
m_vRotation._v[2] -= osg::DegreesToRadians(m_fAngle * (mouseX-m_fpushX));
m_vRotation._v[0] += osg::DegreesToRadians(1.1*(mouseY-m_fpushY)) ;
//
if (m_vRotation._v [0] >= 3.14)
m_vRotation._v [0] = 3.14 ;
if (m_vRotation._v [0] <= 0)
m_vRotation._v [0] = 0 ;
}
return false ;
case (osgGA::GUIEventAdapter ::RELEASE ):
if ( ea.getButton () == 1)
{
m_bLeftButtonDown = false ;
}
return false ;
default:
return false;
}
}
void TravelManipulator::ChangePosition(osg::Vec3& delta)
{
if (m_bPeng)
{
osg::Vec3 newPos1 = m_vPosition + delta;
osgUtil::IntersectVisitor ivXY; ////????????????
osg::ref_ptr<osg:ineSegment>lineXY=new osg::LineSegment(newPos1,m_vPosition);
osg::ref_ptr<osg::LineSegment> lineZ = new osg::LineSegment(newPos1 + osg::Vec3(0.0f, 0.0f,10.0f),newPos1 - osg::Vec3(0.0f, 0.0f, -10.0f));
ivXY.addLineSegment(lineXY.get());
ivXY.addLineSegment (lineZ.get());
m_pHostViewer->getSceneData()->accept(ivXY);
if (!ivXY.hits())
{
m_vPosition += delta;
}
}
else
m_vPosition += delta;
}
void TravelManipulator::setSpeed (float &sp)
{
m_fMoveSpeed = sp ;
}
float TravelManipulator::getSpeed()
{
return m_fMoveSpeed ;
}
void TravelManipulator::SetPosition(osg::Vec3 &position)
{
m_vPosition;
}
osg::Vec3 TravelManipulator::GetPosition()
{
return m_vPosition;
} |
|