|
地形模型加载之后,通过键盘实现前进后退,通过鼠标实现移动视角,怎么实现。
下面这段代码,好像没什么用。是不是mfc 中不是这样写。下面这段代码我是从网上看到的。在他们的程序中就可以,为啥我的不可以。郁闷
bool TrackballManipulator::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() == 0x20)
{
us.requestRedraw();
us.requestContinuousUpdate(false);
return true;
}
// 上移键key_home
if (ea.getKey() == 0xFF50)
{
ChangePosition(osg::Vec3(0, 0, m_fMoveSpeed));
return true;
}
// 下移键key_end
if (ea.getKey() == 0xFF57)
{
ChangePosition(osg::Vec3(0, 0, -m_fMoveSpeed));
return true;
}
// 加速enter
if (ea.getKey() == 0x2B)
{
m_fMoveSpeed += 1.0f;
return true;
}
// 减少速度
if (ea.getKey() == 0x2D)
{
m_fMoveSpeed -= 0.1f;
if(m_fMoveSpeed < 1.0f)
{
m_fMoveSpeed = 1.0f;
}
return true;
}
// 前进
if (ea.getKey() == 0xFF52 || ea.getKey() == 0x57 || ea.getKey() == 0x77)
{
ChangePosition(osg::Vec3(0, m_fMoveSpeed * sinf(osg:I_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)
{
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)
{
m_vRotation._v[2] -= osg:egreesToRadians(m_fAngle);
}
// 向左转
if (ea.getKey() == 0xFF51)
{
m_vRotation._v[2] += osg::DegreesToRadians(m_fAngle);
}
// 改变屏幕角度F键
if (ea.getKey() == 0x46 || ea.getKey() == 0x66)
{
m_fAngle -= 0.2;
return true;
}
//G键
if (ea.getKey() == 0x47 || ea.getKey() == 0x66)
{
m_fAngle += 0.2;
return true;
}
return true;
}
case(osgGA::GUIEventAdapter::PUSH):
if (ea.getButton() == 1)
{
m_fpushX = mouseX;
m_fpushY = mouseY;
m_bLeftButtonDown = true;
}
return true;
// 拖动
case(osgGA::GUIEventAdapter::DRAG):
if (m_bLeftButtonDown)
{
m_vRotation._v[2] -= osg::DegreesToRadians(m_fAngle * (mouseX - m_fpushX)) / 200;
m_vRotation._v[0] += osg::DegreesToRadians(1.1 * (mouseY - m_fpushY)) / 200;
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;
}
} |
|