文档章节

osg源码分析osgGA/KeySwitchMatrixManipulator

哈全文
 哈全文
发布于 2016/11/18 21:57
字数 1222
阅读 41
收藏 0
点赞 0
评论 0

/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
 *
 * This library is open source and may be redistributed and/or modified under
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * OpenSceneGraph Public License for more details.
*/

#ifndef OSGUTIL_KEYSWITCMATRIXMANIPULATOR
#define OSGUTIL_KEYSWITCMATRIXMANIPULATOR 1

#include <osgGA/Export>
#include <osgGA/CameraManipulator>
#include <osgGA/GUIEventHandler>

namespace osgGA{

class GUIActionAdapter;

/**
KeySwitchMatrixManipulator is a decorator which allows the type of camera manipulator
being used to be switched by pressing a key. E.g. '1' for a TrackballManipultor,
'2' for a DriveManipulator, '3' for a FlightManipulator. The manipulators available,
and the associated switch keys, can be configured.
*/

/**

使用按键来操切换操作器类

**/
class OSGGA_EXPORT KeySwitchMatrixManipulator : public CameraManipulator
{
    public:

    /*

std::pair是一个结构体,定义pair比然后插入到map中比在map中直接插入键值对效率要高,标准的OSG应该用这种方式来操作map;

*/

        typedef std::pair<std::string, osg::ref_ptr<CameraManipulator> > NamedManipulator;
        typedef std::map<int, NamedManipulator> KeyManipMap;

        virtual const char* className() const { return "KeySwitchMatrixManipulator"; }

        /**
        Add a camera manipulator with an associated name, and a key to
        trigger the switch,

添加操作器(漫游器)到场景中,同时添加快捷键和标示,这样操作快捷键就可以调用操作器了;
        */

        void addMatrixManipulator(int key, std::string name, CameraManipulator *cm);

        /**
        Add a camera manipulator with an autogenerated keybinding which is '1' + previous number of camera's registerd.
        */
        void addNumberedMatrixManipulator(CameraManipulator *cm);

        unsigned int getNumMatrixManipulators() const { return _manips.size(); }

        void selectMatrixManipulator(unsigned int num);

        /** Get the complete list of manipulators attached to this keyswitch manipulator.*/
        KeyManipMap& getKeyManipMap() { return _manips; }

        /** Get the const complete list of manipulators attached to this keyswitch manipulator.*/
        const KeyManipMap& getKeyManipMap() const { return _manips; }


        /** Get the current active manipulators.*/
        CameraManipulator* getCurrentMatrixManipulator() { return _current.get(); }

        /** Get the const current active manipulators.*/
        const CameraManipulator* getCurrentMatrixManipulator() const { return _current.get(); }


        /** Get manipulator assigned to a specified index.*/
        CameraManipulator* getMatrixManipulatorWithIndex(unsigned int key);

        /** Get const manipulator assigned to a specified index.*/
        const CameraManipulator* getMatrixManipulatorWithIndex(unsigned int key) const;

        /** Get manipulator assigned to a specified key.*/
        CameraManipulator* getMatrixManipulatorWithKey(unsigned int key);

        /** Get const manipulator assigned to a specified key.*/
        const CameraManipulator* getMatrixManipulatorWithKey(unsigned int key) const;


        // Overrides from CameraManipulator...

        /** set the coordinate frame which callback tells the manipulator which way is up, east and north.*/
        virtual void setCoordinateFrameCallback(CoordinateFrameCallback* cb);

        /** Set the position of the matrix manipulator using a 4x4 Matrix.*/
        virtual void setByMatrix(const osg::Matrixd& matrix) { _current->setByMatrix(matrix); }

        /** set the position of the matrix manipulator using a 4x4 Matrix.*/
        virtual void setByInverseMatrix(const osg::Matrixd& matrix) { _current->setByInverseMatrix(matrix); }

        /** get the position of the manipulator as 4x4 Matrix.*/
        virtual osg::Matrixd getMatrix() const { return _current->getMatrix(); }

        /** get the position of the manipulator as a inverse matrix of the manipulator, typically used as a model view matrix.*/
        virtual osg::Matrixd getInverseMatrix() const { return _current->getInverseMatrix(); }

        /** Get the FusionDistanceMode. Used by SceneView for setting up stereo convergence.*/
        virtual osgUtil::SceneView::FusionDistanceMode getFusionDistanceMode() const { return _current->getFusionDistanceMode(); }

        /** Get the FusionDistanceValue. Used by SceneView for setting up stereo convergence.*/
        virtual float getFusionDistanceValue() const { return _current->getFusionDistanceValue(); }


        virtual void setNode(osg::Node* n);

        virtual const osg::Node* getNode() const        { return _current->getNode(); }

        virtual osg::Node* getNode()                    { return _current->getNode(); }

        virtual void setHomePosition(const osg::Vec3d& eye, const osg::Vec3d& center, const osg::Vec3d& up, bool autoComputeHomePosition=false);

        virtual void setAutoComputeHomePosition(bool flag);

        virtual void computeHomePosition();

        virtual void home(const GUIEventAdapter& ee,GUIActionAdapter& aa);

        virtual void init(const GUIEventAdapter& ee,GUIActionAdapter& aa) { if (_current.valid()) _current->init(ee,aa); }

        virtual bool handle(const GUIEventAdapter& ea,GUIActionAdapter& us);

        /** Get the keyboard and mouse usage of this manipulator.*/
        virtual void getUsage(osg::ApplicationUsage& usage) const;

    private:

        KeyManipMap _manips;

        osg::ref_ptr<CameraManipulator> _current;
};

}

#endif

//////////////////////////////////////源码////////////////////////////////////////////////////////////////

#include <osgGA/KeySwitchMatrixManipulator>
#include <osg/Notify>

using namespace osgGA;

void KeySwitchMatrixManipulator::addMatrixManipulator(int key, std::string name, CameraManipulator *cm)
{
    if(!cm) return;

    _manips[key]=std::make_pair(name,osg::ref_ptr<CameraManipulator>(cm));

    if(!_current)
    {
        _current=cm;
        _current->setHomePosition(_homeEye,_homeCenter,_homeUp,_autoComputeHomePosition);
        _current->setNode(0);
        _current->setCoordinateFrameCallback(getCoordinateFrameCallback());
        _current->setByMatrix(getMatrix());
    }
}

void KeySwitchMatrixManipulator::addNumberedMatrixManipulator(CameraManipulator *cm)
{
    if(!cm) return;
    addMatrixManipulator('1'+_manips.size(),cm->className(),cm);
}

void KeySwitchMatrixManipulator::selectMatrixManipulator(unsigned int num)
{
    unsigned int manipNo = 0;
    KeyManipMap::iterator itr;
    for(itr=_manips.begin();
        manipNo!=num && itr!=_manips.end();
        ++itr,++manipNo)
    {
    }

    if (itr!=_manips.end())
    {
        itr->second.second->setHomePosition(_homeEye,_homeCenter,_homeUp,_autoComputeHomePosition);

        if (_current.valid())
        {
            if ( !itr->second.second->getCoordinateFrameCallback() )
            {
                itr->second.second->setCoordinateFrameCallback(_current->getCoordinateFrameCallback());
            }

            if ( !itr->second.second->getNode() )
            {
                itr->second.second->setNode(_current->getNode());
            }
            itr->second.second->setByMatrix(_current->getMatrix());
        }
        _current = itr->second.second;
    }
}

void KeySwitchMatrixManipulator::setNode(osg::Node* node)
{
    for(KeyManipMap::iterator itr=_manips.begin();
        itr!=_manips.end();
        ++itr)
    {
        itr->second.second->setNode(node);
    }
}

void KeySwitchMatrixManipulator::setHomePosition(const osg::Vec3d& eye, const osg::Vec3d& center, const osg::Vec3d& up, bool autoComputeHomePosition)
{
    CameraManipulator::setHomePosition(eye, center, up, autoComputeHomePosition);
    for(KeyManipMap::iterator itr=_manips.begin();
        itr!=_manips.end();
        ++itr)
    {
        itr->second.second->setHomePosition(eye, center, up, autoComputeHomePosition);
    }
}

void KeySwitchMatrixManipulator::setAutoComputeHomePosition(bool flag)
{
    _autoComputeHomePosition = flag;
    for(KeyManipMap::iterator itr=_manips.begin();
        itr!=_manips.end();
        ++itr)
    {
        itr->second.second->setAutoComputeHomePosition(flag);
    }
}

void KeySwitchMatrixManipulator::computeHomePosition()
{
    for(KeyManipMap::iterator itr=_manips.begin();
        itr!=_manips.end();
        ++itr)
    {
        itr->second.second->computeHomePosition();
    }
}

void KeySwitchMatrixManipulator::home(const GUIEventAdapter& ee,GUIActionAdapter& aa)
{
    // call home for all child manipulators
    // (this can not be done just for current manipulator,
    // because it is not possible to transfer some manipulator
    // settings across manipulators using just MatrixManipulator interface
    // (one problematic variable is for example OrbitManipulator::distance
    // that can not be passed by setByMatrix method),
    // thus we have to call home on all of them)
    for(KeyManipMap::iterator itr=_manips.begin();
        itr!=_manips.end();
        ++itr)
    {
        itr->second.second->home(ee,aa);
    }
}

void KeySwitchMatrixManipulator::setCoordinateFrameCallback(CoordinateFrameCallback* cb)
{
    _coordinateFrameCallback = cb;
    for(KeyManipMap::iterator itr=_manips.begin();
        itr!=_manips.end();
        ++itr)
    {
        itr->second.second->setCoordinateFrameCallback(cb);
    }
}

CameraManipulator* KeySwitchMatrixManipulator::getMatrixManipulatorWithIndex(unsigned int index)
{
    unsigned i=0;
    for(KeyManipMap::iterator itr = _manips.begin();
         itr != _manips.end();
         ++itr, ++i)
    {
        if (i==index) return itr->second.second.get();
    }
    return 0;
}

const CameraManipulator* KeySwitchMatrixManipulator::getMatrixManipulatorWithIndex(unsigned int index) const
{
    unsigned i=0;
    for(KeyManipMap::const_iterator itr = _manips.begin();
         itr != _manips.end();
         ++itr, ++i)
    {
        if (i==index) return itr->second.second.get();
    }
    return 0;
}

CameraManipulator* KeySwitchMatrixManipulator::getMatrixManipulatorWithKey(unsigned int key)
{
    KeyManipMap::iterator itr = _manips.find(key);
    if (itr!=_manips.end()) return itr->second.second.get();
    else return 0;
}

const CameraManipulator* KeySwitchMatrixManipulator::getMatrixManipulatorWithKey(unsigned int key) const
{
    KeyManipMap::const_iterator itr = _manips.find(key);
    if (itr!=_manips.end()) return itr->second.second.get();
    else return 0;
}

bool KeySwitchMatrixManipulator::handle(const GUIEventAdapter& ea,GUIActionAdapter& aa)
{
    if (!_current) return false;

    bool handled = false;

    if (!ea.getHandled() && ea.getEventType()==GUIEventAdapter::KEYDOWN)
    {

        KeyManipMap::iterator it=_manips.find(ea.getKey());
        if(it != _manips.end())
        {
            OSG_INFO<<"Switching to manipulator: "<<(*it).second.first<<std::endl;
            if ( !it->second.second->getNode() )
            {
                it->second.second->setNode(_current->getNode());
            }
            it->second.second->setByMatrix(_current->getMatrix());
            it->second.second->init(ea,aa);
            _current = it->second.second;

            handled = true;
        }
    }

    return _current->handle(ea,aa) || handled;
}

void KeySwitchMatrixManipulator::getUsage(osg::ApplicationUsage& usage) const
{
    for(KeyManipMap::const_iterator itr=_manips.begin();
        itr!=_manips.end();
        ++itr)
    {
        std::string key; key += (char)(itr->first);
        std::string explanation(std::string("Select '")+itr->second.first+std::string("' camera manipulator"));
        if (_current==itr->second.second) explanation += " (default)";

        usage.addKeyboardMouseBinding(key,explanation);
        itr->second.second->getUsage(usage);
    }
}

© 著作权归作者所有

共有 人打赏支持
哈全文
粉丝 14
博文 89
码字总数 22122
作品 0
海淀
其他
osg 对象选择

/////////////////////////////////////框选////////////////////////////////////// class BoxPicker : public osgGA::GUIEventHandler { public: BoxPicker() { x=0.0f; y=0.0f; OK= fals......

wwl33695 ⋅ 2014/05/05 ⋅ 0

osg源码分析osgViewer/Viewer

/ --c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield * * This library is open source and may be redistributed and/or modified under * the terms of the OpenSceneGra......

哈全文 ⋅ 2016/11/18 ⋅ 0

OSG向场景中添加osgParticle粒子效果

目的: 向场景中添加自定义的osgParticle实例,模拟坦克模型在地形上运动时产生的烟尘。 ----------------------------- 概述: 添加粒子效果可以有效提高仿真程序的外观和真实性。粒子引擎一...

长平狐 ⋅ 2012/10/08 ⋅ 0

osg的一些小问题 关于漫游器的 求解答

分析了一个漫游器 , 其中有一点不是很明白,就是在交互操作的时候 响应键盘的WASD的字母,例如 W 前进 ChangePosition(osg::vec3(0,mspeed*sinf(osg::PI2+m_Vposition.v[2]),0)) ChangePosi...

追寻灯火阑珊 ⋅ 2014/05/09 ⋅ 0

lemonrong/osg3DViewer

osg3DViewer A 3D File Viewer based on OSG. Use QT UI. Created for self learning for OSG. It's re-writed from osGraphX. CMake files was changed. It's more easy to build by cmake.......

lemonrong ⋅ 2016/05/07 ⋅ 0

osgjs模型显示黑色的问题

问题描述 问题:osgb文件直接转换成osgjs之后,在网页上不显示纹理(不管纹理是否设置,或者纹理图片是否能读取,模型始终是黑色)。只有将osgb先转换成obj,再将obj转换成osgjs,纹理才能显...

5457f2 ⋅ 2016/06/22 ⋅ 0

OSG与OpenInventor

本人新手,不了解OSG,用过一段时间的OpenInventor。之前去过一家公司面试,招OSG方面。想请教牛人OSG与OpenInventor,是同类的东西,如果是的话,哪个更好,或者说哪个应用更广泛...

会说话的哑巴 ⋅ 2012/03/28 ⋅ 0

OSG粒子仿真,不管怎么用setDefaultAttributes渲染,粒子怎么都是正方形?

我是一个OSG新手,老师让我做一个OSG的特效粒子雪景仿真,但是我用setDefaultAttributes渲染之后,粒子一直都是正方形,有哪位大神可以指导一下啊?还有一运行,OSG场景就会很卡,那个高手有...

段晓云 ⋅ 2013/05/19 ⋅ 1

OpenSceneGraph 应用的问题

OpenSceneGraph 我都已经编译通过,环境变量添加好了,可在命令行(CMD)进行测试osgversion,osglogo,osgviewer cow.osg,都通过了。但是 在 vs2010中新建一个项目,测试时 出现了这样的错误...

yuxinabc1 ⋅ 2015/07/07 ⋅ 1

python 爬虫 极客学院

coding: utf-8import requestsimport reimport sys reload(sys)sys.setdefaultencoding("utf-8") class Spider(object): self.urls = [] self.page = '0' def getPage(self, urlbase='http:/......

hyhlinux ⋅ 2016/06/11 ⋅ 0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

win10怎么彻底关闭自动更新

win10自带的更新每天都很多,每一次下载都要占用大量网络,而且安装要等得时间也蛮久的。 工具/原料 Win10 方法/步骤 单击左下角开始菜单点击设置图标进入设置界面 在设置窗口中输入“服务”...

阿K1225 ⋅ 今天 ⋅ 0

Elasticsearch 6.3.0 SQL功能使用案例分享

The best elasticsearch highlevel java rest api-----bboss Elasticsearch 6.3.0 官方新推出的SQL检索插件非常不错,本文一个实际案例来介绍其使用方法。 1.代码中的sql检索 @Testpu...

bboss ⋅ 今天 ⋅ 0

informix数据库在linux中的安装以及用java/c/c++访问

一、安装前准备 安装JDK(略) 到IBM官网上下载informix软件:iif.12.10.FC9DE.linux-x86_64.tar放在某个大家都可以访问的目录比如:/mypkg,并解压到该目录下。 我也放到了百度云和天翼云上...

wangxuwei ⋅ 今天 ⋅ 0

PHP语言系统ZBLOG或许无法重现月光博客的闪耀历史[图]

最近在写博客,希望通过自己努力打造一个优秀的教育类主题博客,名动江湖,但是问题来了,现在写博客还有前途吗?面对强大的自媒体站点围剿,还有信心和可能型吗? 至于程序部分,我选择了P...

原创小博客 ⋅ 今天 ⋅ 0

IntelliJ IDEA 2018.1新特性

工欲善其事必先利其器,如果有一款IDE可以让你更高效地专注于开发以及源码阅读,为什么不试一试? 本文转载自:netty技术内幕 3月27日,jetbrains正式发布期待已久的IntelliJ IDEA 2018.1,再...

Romane ⋅ 今天 ⋅ 0

浅谈设计模式之工厂模式

工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。 在工厂模式中,我们在创建对象时不会对客户端暴露创建逻...

佛系程序猿灬 ⋅ 今天 ⋅ 0

Dockerfile基础命令总结

FROM 指定使用的基础base image FROM scratch # 制作base image ,不使用任何基础imageFROM centos # 使用base imageFROM ubuntu:14.04 尽量使用官方的base image,为了安全 LABEL 描述作...

ExtreU ⋅ 昨天 ⋅ 0

存储,对比私有云和公有云的不同

导读 说起公共存储,很难不与后网络公司时代的选择性外包联系起来,但尽管如此,它还是具备着简单和固有的可用性。公共存储的名字听起来也缺乏专有性,很像是把东西直接堆放在那里而不会得到...

问题终结者 ⋅ 昨天 ⋅ 0

C++难点解析之const修饰符

C++难点解析之const修饰符 c++ 相比于其他编程语言,可能是最为难掌握,概念最为复杂的。结合自己平时的C++使用经验,这里将会列举出一些常见的难点并给出相应的解释。 const修饰符 const在c...

jackie8tao ⋅ 昨天 ⋅ 0

聊聊spring cloud netflix的HystrixCommands

序 本文主要研究一下spring cloud netflix的HystrixCommands。 maven <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-clo......

go4it ⋅ 昨天 ⋅ 0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部