zoukankan      html  css  js  c++  java
  • osgGA::KeySwitchMatrixManipulator 跟随

    #ifdef _WIN32
    #include <Windows.h>
    #endif // _WIN32
    
    #include <osg/Group>
    #include <osg/Camera>
    #include <osgDB/ReadFile>
    #include <osg/Node>
    
    #include <osg/Geometry>
    #include <osg/Image>
    #include <osg/ShapeDrawable>
    #include <osg/Texture2D>
    
    #include <osg/MatrixTransform>
    #include <osg/AnimationPath>
    
    #include <osgViewer/Viewer>
    #include <osgViewer/ViewerEventHandlers>
    
    #include <osgGA/DriveManipulator>
    #include <osgGA/GUIEventHandler>
    #include <osgGA/GUIEventAdapter>
    #include <osgGA/GUIActionAdapter>
    
    #include <osgGA/AnimationPathManipulator>
    #include <osgGA/KeySwitchMatrixManipulator>
    
    #include <osgUtil/LineSegmentIntersector>
    
    #include <iostream>
    using namespace std;
    
    class Follow:public osgGA::KeySwitchMatrixManipulator
    {
    public:
        Follow(osgViewer::Viewer *viewerParam)
        {
            m_vPosition = osg::Vec3(0.0, -200.0, -70.0);
            m_vRotation = osg::Vec3(osg::PI_2, 0.0f, 0.0f);
            m_fMoveSpeed = 2.0;
            m_fAnglg = 2.5;
            viewer1 = viewerParam;
        }
    
        virtual void setByMatrix()
        {
    
        }
    
        virtual void setByInverseMatrix()
        {
    
        }
    
        virtual osg::Matrixd getMatrix() const 
        {
            osg::Matrixd mat1;
            mat1.makeRotate(m_vRotation.x(), osg::Vec3(1.0, 0.0, 0.0), m_vRotation.y(), osg::Vec3(0.0, 1.0, 0.0), m_vRotation.z(), osg::Vec3(0.0, 0.0, 1.0));
            return mat1*osg::Matrixd::translate(m_vPosition);
        }
    
        virtual osg::Matrixd getInverseMatrix() const
        {
            osg::Matrixd mat1;
            mat1.makeRotate(m_vRotation.x(), osg::Vec3(1.0, 0.0, 0.0), m_vRotation.y(), osg::Vec3(0.0, 1.0, 0.0), m_vRotation.z(), osg::Vec3(0.0, 0.0, 1.0));
            return osg::Matrixd::inverse(mat1*osg::Matrixd::translate(m_vPosition));
        }
    
        virtual float getFusionDistanceValue() const { 
            //return _current->getFusionDistanceValue(); 
            return viewer1->getFusionDistanceValue();
        }
    
        virtual osgUtil::SceneView::FusionDistanceMode getFusionDistanceMode() const 
        { 
            
            return viewer1->getFusionDistanceMode();
        }
    
        void changePosition(osg::Vec3 delta)
        {
            m_vPosition += delta;
        }
    
        bool handle(const osgGA::GUIEventAdapter& gea,osgGA::GUIActionAdapter& gaa)
        {
            //
            switch (gea.getEventType())
            {
            case osgGA::GUIEventAdapter::DOUBLECLICK:
                std::cout << "-double click--" << std::endl;
                break;
    
            case osgGA::GUIEventAdapter::KEYDOWN:
                //std::cout << "-ket down--" << std::endl;
                if (gea.getKey()==87)
                {
                    changePosition(osg::Vec3(m_fMoveSpeed*cosf(osg::PI_2 + m_vRotation[2]), m_fMoveSpeed*sinf(osg::PI_2 + m_vRotation[2]), 0));
                }
                else if (gea.getKey()==83)
                {
                    changePosition(osg::Vec3(-m_fMoveSpeed*cosf(osg::PI_2 + m_vRotation[2]), -m_fMoveSpeed*sinf(osg::PI_2 + m_vRotation[2]), 0));
                }
                else if (gea.getKey()== 0xFF53)
                {
                    m_vRotation._v[2] -= osg::DegreesToRadians(m_fAnglg);
                }
                else if (gea.getKey()==0xFF51)
                {
                    m_vRotation._v[2] += osg::DegreesToRadians(m_fAnglg);
                }
                else 
                {
    
                }
                
                break;
    
            default:
                break;
            }
    
    
            return false;
        }
    
    private:
        osg::Vec3 m_vPosition;
        osg::Vec3 m_vRotation;
        float m_fMoveSpeed;
        float m_fAnglg;
        osgUtil::SceneView::FusionDistanceMode model1;
        osgViewer::Viewer *viewer1;
    };
    
    int main()
    {
        osg::ref_ptr<osgViewer::Viewer> viewer1 = new osgViewer::Viewer;
        osg::ref_ptr<osg::Group> group1 = new osg::Group;
        osg::ref_ptr<osg::Node> node1 = osgDB::readNodeFile("D:\参考手册\BIM\osg\四合院2019.osgb");
    
        group1->addChild(node1.get());
        viewer1->setSceneData(group1);
        viewer1->setUpViewInWindow(200, 200, 800, 600, 0);
        viewer1->setCameraManipulator(new Follow(viewer1));
        
        //viewer1->addEventHandler(new osgViewer::WindowSizeHandler);
        viewer1->realize();
    
        return viewer1->run();
    }

  • 相关阅读:
    del:根据索引值删除元素
    Python insert()方法插入元素
    Python extend()方法添加元素
    Python append()方法添加元素
    Python list列表添加元素的3种方法
    什么是序列,Python序列详解(包括序列类型和常用操作)
    Python运算符优先级和结合性一览表
    Python print()函数高级用法
    Python input()函数:获取用户输入的字符串
    Python变量的定义和使用
  • 原文地址:https://www.cnblogs.com/herd/p/11133301.html
Copyright © 2011-2022 走看看