重写
osgGA::TrackballManipulator的三视图类
osgGA::StandardManipulator的home函数实现了视图恢复到默认位置(默认空格键)即正视图,在自己的继承类中实现箭头按键控制左右侧视图,俯视图。
继承关系如下图:
osgGA::GUIEventHandler -> osgGA::CameraManipulator ->osgGA::StandardManipulator -> osgGA:: OrbitManipulator
-> osgGA::TrackballManipulator
重写的三视图视图控制类class ViewControl继承自 osgGA::TrackballManipulator。
上代码:
ViewControl.h
#ifndef VIEWCONTROL_H
#define VIEWCONTROL_H
#include<osgGA/TrackballManipulator>
class ViewSelect: public osgGA::TrackballManipulator
{
public:
ViewSelect():TrackballManipulator() {}
protected:
bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &us);
private:
void moveToView(const osgGA::GUIEventAdapter & ea);
};
#endif // VIEWCONTROL_H
ViewControl.cpp
#include "viewcontrol.h"
#include <osgGA/CameraManipulator>
#include <osgGA/GUIActionAdapter>
void ViewSelect::moveToView(const osgGA::GUIEventAdapter & ea)
{
_thrown = false;
home(0.0); //this fuction does not trigger redraw
switch ( ea.getKey())
{
case osgGA::GUIEventAdapter::KEY_Down: //TOP_VIEW :
this->setRotation(osg::Quat(1.0f, 0.0, 0.0, 0.0));
break;
case osgGA::GUIEventAdapter::KEY_Up: //BOTTOM_VIEW :
this->setRotation(osg::Quat(0.0f, 0.0f, 0.0f, 1.0f));
break;
case osgGA::GUIEventAdapter::KEY_Left: // LEFT_VIEW :
this->setRotation(osg::Quat(1.0f, -1.0, -1.0, 1.0f));
break;
case osgGA::GUIEventAdapter::KEY_Right: //RIGHT_VIEW:
this->setRotation(osg::Quat(1.0f, 1.0f, 1.0f, 1.0f));
break;
//case osgGA::GUIEventAdapter::KEY_Down: // BACK_VIEW:
// this->setRotation(osg::Quat(0.0f,1.0f,1.0f,0.0f));
break;
default :
break;
}
ea.setHandled(true); //been handled
}
bool ViewSelect::handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &us)
{
switch( ea.getEventType() )
{
case osgGA::GUIEventAdapter::KEYDOWN:
moveToView(ea);
break;
default:
break;
}
if( ea.getHandled() )
return false;
return osgGA::StandardManipulator::handle(ea,us);
}
main.cpp
#include <osg/Node>
#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>
#include <osgViewer/Viewer>
#ifdef _DEBUG
#pragma comment(lib,"osgd.lib")
#pragma comment(lib,"osgDBd.lib")
#pragma comment(lib,"osgViewerd.lib")
#else
#pragma comment(lib,"osg.lib")
#pragma comment(lib,"osgDB.lib")
#pragma comment(lib,"osgViewer.lib")
#pragma comment(lib,"osgGA.lib")
#endif
#include "viewcontrol.h"
int main(int, char **)
{
osgViewer::Viewer viewer;
viewer.setCameraManipulator(new ViewSelect());
viewer.setSceneData(osgDB::readNodeFile("cow.osg"));
return viewer.run();
}
参考:https://blog.csdn.net/pengjiaqi1028/article/details/78583551
https://blog.csdn.net/qq_35040828/article/details/68525907