重写osgGA::TrackballManipulator实现osg的三视图

重写

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

 

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值