|
这个漫游器,当同时按下 方向键(wasd) 和 鼠标左键 进行旋转 的时候,鼠标和键盘大概在同时释放时。摄像机 会一直 沿着 某个放心 移动。 keydown 后。鼠标释放后,keyup 没有在执行 不知道为什么? 看了好久了。请大家 帮忙看下 什么问题?我在控制台下 测试的。
- #ifndef _CAMERAMANIPULATOR_H_
- #define _CAMERAMANIPULATOR_H_
- #include <osgGA/MatrixManipulator>
- class FPCameraManipulator : public osgGA::MatrixManipulator {
- public:
- FPCameraManipulator();
- ~FPCameraManipulator(){};
- virtual void setByMatrix( const osg::Matrixd& matrix );
- virtual void setByInverseMatrix( const osg::Matrixd& matrix );
- virtual osg::Matrixd getMatrix() const;
- virtual osg::Matrixd getInverseMatrix() const;
- virtual bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa );
- void updatePosition();
- private:
- osg::Vec3 m_vPosition;
- osg::Vec3 m_vRotation;
- osg::Vec2 m_vPos;
- osg::Vec2 m_vTemp;
- std::set<int> m_kSet;
- float m_fMoveSpeed;
- protected:
- };
- #endif
复制代码
- #include "CameraManipulator.h"
- FPCameraManipulator::FPCameraManipulator() : m_fMoveSpeed( 10.5f ){
- m_vPosition = osg::Vec3( 0.0f, 0.0f, 0.0f );
- m_vRotation = osg::Vec3( osg::PI_2, 0.0f, 0.0f );
- m_kSet.clear();
- }
- void FPCameraManipulator::setByMatrix( const osg::Matrixd& matrix ){
- m_vPosition = matrix.getTrans();
- m_vRotation = matrix.getRotate().asVec3();
- }
- void FPCameraManipulator::setByInverseMatrix( const osg::Matrixd& matrix ){
- setByMatrix( osg::Matrixd::inverse( matrix ) );
- }
- osg::Matrixd FPCameraManipulator::getMatrix() 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 FPCameraManipulator::getInverseMatrix() 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 FPCameraManipulator::handle( const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa ){
- switch( ea.getEventType() ){
- case osgGA::GUIEventAdapter::FRAME:
- FPCameraManipulator::updatePosition();
- return true;
- case osgGA::GUIEventAdapter::KEYDOWN:
- if( ea.getKey() == 0x20 ){
- aa.requestRedraw();
- aa.requestContinuousUpdate( false );
- return true;
- }
- m_kSet.insert( ea.getKey() );
- return false;
- case osgGA::GUIEventAdapter::KEYUP:
- m_kSet.erase( ea.getKey() );
- return true;
- case osgGA::GUIEventAdapter::PUSH:
- if( ea.getHandled() )
- return false;
- if( ea.getButton() == 1 ){
- m_vPos.set( ea.getX(), ea.getY() );
- m_vTemp.set( m_vRotation._v[2], m_vRotation._v[0] );
- return true;
- }
- return false;
- case osgGA::GUIEventAdapter::DRAG:
- if( ea.getHandled() )
- return false;
- m_vRotation._v[2] = m_vTemp.x() - 0.1 * osg::DegreesToRadians( ea.getX() - m_vPos.x() );
- m_vRotation._v[0] = m_vTemp.y() + 0.1 * osg::DegreesToRadians( ea.getY() - m_vPos.y() );
- return true;
- default:
- return false;
- }
- }
- void FPCameraManipulator::updatePosition(){
- osg::Vec3f eye, center, up, orient, orient_upright;
- FPCameraManipulator::getInverseMatrix().getLookAt( eye, center, up );
- orient.set( ( center - eye ).x() / ( center - eye ).length(),
- ( center - eye ).y() / ( center - eye ).length(),
- ( center - eye ).z() / ( center - eye ).length() );
- orient.normalize();
- orient_upright.set( ( orient ^ up ).x() / ( orient ^ up ).length(),
- ( orient ^ up ).y() / ( orient ^ up ).length(),
- ( orient ^ up ).z() / ( orient ^ up ).length() );
- orient_upright.normalize();
- float avgfMoveSpeed = m_fMoveSpeed / m_kSet.size();
- if( m_kSet.find( 'w' ) != m_kSet.end() ){
- m_vPosition += osg::Vec3( avgfMoveSpeed * orient.x(),
- avgfMoveSpeed * orient.y(),
- avgfMoveSpeed * orient.z() );
- }
- if( m_kSet.find( 's' ) != m_kSet.end() ){
- m_vPosition += osg::Vec3( -avgfMoveSpeed * orient.x(),
- -avgfMoveSpeed * orient.y(),
- -avgfMoveSpeed * orient.z() );
- }
- if( m_kSet.find( 'a' ) != m_kSet.end() ){
- m_vPosition += osg::Vec3( -avgfMoveSpeed * orient_upright.x(),
- -avgfMoveSpeed * orient_upright.y(),
- -avgfMoveSpeed * orient_upright.z() );
- }
- if( m_kSet.find( 'd' ) != m_kSet.end() ){
- m_vPosition += osg::Vec3( avgfMoveSpeed * orient_upright.x(),
- avgfMoveSpeed * orient_upright.y(),
- avgfMoveSpeed * orient_upright.z() );
- }
- if( m_kSet.find( 'q' ) != m_kSet.end() )
- m_vPosition += osg::Vec3( 0.0f, 0.0f, m_fMoveSpeed );
- if( m_kSet.find( 'e' ) != m_kSet.end() )
- m_vPosition += osg::Vec3( 0.0f, 0.0f, -m_fMoveSpeed );
- }
复制代码 |
|