OSG aprende desde cero eventos de teclado-ejemplo-documental

OSG aprende desde cero eventos de teclado-ejemplo-documental

Los cimientos no son fuertes, el suelo se balancea y las montañas se balancean. No puedes girar en la puerta. Por qué, no hay habilidades básicas. Aprende desde cero. El avance está en el estudio de caso. Solo si comprendes y comprendes OSG en cada caso, puedes ser competente en su uso. A partir de hoy, no El límite superior del tiempo no se apresura. Solo uno al día, si sientes lo mismo, ¡comencemos!
El primer ejemplo: osgkeyboard.exe; código fuente

Bloque de código

La sintaxis del bloque de código sigue el código de descuento estándar, por ejemplo:



//*sunsenzhen
//*osgkeyboarf 键盘事件练习 
//*20160320
/

#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osg/io_utils>
#include <osg/MatrixTransform>
#include <osg/Geode>
#include <osg/Group>
#include <osg/Switch>
#include <osg/Notify>
#include <osg/Geometry>
#include <osgText/Text>
#include <osgDB/Registry>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>


#ifdef _DEBUG
#pragma comment(lib, "User32.lib")     
#pragma comment(lib, "glu32.lib")   
#pragma comment(lib, "opengl32.lib")   
#pragma comment(lib,"osgd.lib")
#pragma comment(lib,"osgDBd.lib")
#pragma comment(lib,"osgGAd.lib")
#pragma comment(lib,"osgManipulatord.lib")
#pragma comment(lib,"osgUtild.lib")
#pragma comment(lib,"osgViewerd.lib")
#pragma comment(lib,"osgWidgetd.lib")
#pragma comment(lib,"osgFXd.lib")
#pragma comment(lib,"osgShadowd.lib")
#pragma comment(lib,"OpenThreadsd.lib")
#pragma comment(lib, "osgAnimationd.lib")
#pragma comment(lib, "osgParticled.lib")
#pragma comment(lib, "osgTerraind.lib")
#pragma comment(lib, "osgPresentationd.lib")
#pragma comment(lib, "osgSimd.lib")
#pragma comment(lib, "osgVolumed.lib")
#pragma comment(lib, "osgTextd.lib")
#pragma comment(lib,"osgdb_gdald.lib")
#pragma comment(lib,"osgdb_ogrd.lib")
#pragma comment(lib,"osgUId.lib")
#pragma comment(lib,"osgWidgetd.lib")
#else
#pragma comment(lib, "User32.lib")     
#pragma comment(lib, "glu32.lib")   
#pragma comment(lib, "opengl32.lib")   
#pragma comment(lib,"osg.lib")
#pragma comment(lib,"osgDB.lib")
#pragma comment(lib,"osgGA.lib")
#pragma comment(lib,"osgManipulator.lib")
#pragma comment(lib,"osgUtil.lib")
#pragma comment(lib,"osgViewer.lib")
#pragma comment(lib,"osgWidget.lib")
#pragma comment(lib,"osgFX.lib")
#pragma comment(lib,"osgShadow.lib")
#pragma comment(lib,"OpenThreads.lib")
#pragma comment(lib, "osgAnimation.lib")
#pragma comment(lib, "osgParticle.lib")
#pragma comment(lib, "osgTerrain.lib")
#pragma comment(lib, "osgPresentation.lib")
#pragma comment(lib, "osgSim.lib")
#pragma comment(lib, "osgVolume.lib")
#pragma comment(lib, "osgText.lib")
#pragma comment(lib,"osgdb_gdal.lib")
#pragma comment(lib,"osgdb_ogr.lib")
#pragma comment(lib,"osgUI.lib")
#pragma comment(lib,"osgWidget.lib")
#endif //_debug

class KeyboardModel : public osg::Referenced
{
public:

        KeyboardModel() { createKeyboard(); }//创建键盘
        
        osg::Group* getScene() { return _scene.get(); }//返回键盘几何图形
        
        void keyChange(int key, int virtualKey, int value);//键盘输入
        
protected:
        
        ~KeyboardModel() {}

        osg::Switch* addKey(osg::Vec3& pos, int key,const std::string& text,float width, float height);//增加一个按钮

        void createKeyboard();

        typedef std::map<int, osg::ref_ptr<osg::Switch> > KeyModelMap;
        
        osg::ref_ptr<osg::Group>    _scene;
        KeyModelMap                 _keyModelMap;
        osg::ref_ptr<osgText::Text> _inputText;

};

void KeyboardModel::keyChange(int key, int virtualKey, int value)
{
    osg::notify(osg::INFO) << "key value change, code="<<std::hex << key << "\t value="<< value << std::dec  << std::endl;

    // toggle the keys graphical representation on or off via osg::Swithc
    KeyModelMap::iterator itr = _keyModelMap.find(virtualKey);
    if (itr!=_keyModelMap.end())
    {
        itr->second->setSingleChildOn(value);
    }
    //控制显示
    if (value)
    {
        // when a key is pressed add the new data to the text field
        
        if (key>0 && key<256)
        {
            // just add ascii characters right now...
            _inputText->getText().push_back(key);
            _inputText->update();
        }
        else if (key==osgGA::GUIEventAdapter::KEY_Return)
        {
            _inputText->getText().push_back('\n');
            _inputText->update();
        }
        else if (key==osgGA::GUIEventAdapter::KEY_BackSpace || key==osgGA::GUIEventAdapter::KEY_Delete) 
        {
            if (!_inputText->getText().empty())
            {
                _inputText->getText().pop_back();
                _inputText->update();
            }
        }
        
    }
    
}
//添加键盘上的数字
//pos表示位置
//key键盘ID
//text字符数字
//width/height键盘的宽和高
osg::Switch* KeyboardModel::addKey(osg::Vec3& pos, int key,const std::string& text,float width, float height)
{

    osg::Geode* geodeUp = new osg::Geode;//键盘弹起的时候的几何体
    {
        osgText::Text* textUp = new osgText::Text;//新建一个osg文本
        textUp->setFont("fonts/arial.ttf");//设置文本字体
        textUp->setColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));//设置为白色
        textUp->setCharacterSize(height);//设置字体大小
        textUp->setPosition(pos);//设置字体位置
		//textUp->setDrawMode(osgText::Text::BOUNDINGBOX);//绘制它的外包围盒
		textUp->setDrawMode(osgText::Text::TEXT/*|osgText::Text::BOUNDINGBOX*/);
        textUp->setAlignment(osgText::Text::LEFT_CENTER);//文本对齐格式
        textUp->setAxisAlignment(osgText::Text::XZ_PLANE);//在XZ平面上
        textUp->setText(text);//设置增加的按钮文本
        
        geodeUp->addDrawable(textUp);//绘制几何
    }
    
    osg::Geode* geodeDown = new osg::Geode;//同理这是按下的效果
    {
        osgText::Text* textDown = new osgText::Text;
        textDown->setFont("fonts/arial.ttf");
        textDown->setColor(osg::Vec4(1.0f,0.0f,1.0f,1.0f));
        textDown->setCharacterSize(height);
        textDown->setPosition(pos);
        textDown->setDrawMode(osgText::Text::TEXT/*||osgText::Text::BOUNDINGBOX*/);
        textDown->setAlignment(osgText::Text::LEFT_CENTER);
        textDown->setAxisAlignment(osgText::Text::XZ_PLANE);
        textDown->setText(text);
        
        geodeDown->addDrawable(textDown);
    }

    osg::Switch* model = new osg::Switch;//做一个模型切换,将上面的两个几何体加入
    model->addChild(geodeUp,true);//默认为弹起
    model->addChild(geodeDown,false);//按下设为假
    
    _scene->addChild(model);//在主场景中增加该切换模型

    _keyModelMap[key] = model;//将该模型加入键盘MAP,对照表里
    
    pos.x() += width;//将传进来的参数X坐标增加字符的宽度
    
    return model;//返回干模型
    
}
//创建键盘
void KeyboardModel::createKeyboard()
{
    _scene = new osg::Group;//初始化场景树
    
    osg::Vec3 origin(0.0f,0.0f,0.0f);//坐标中心
    osg::Vec3 pos=origin;
    //从键盘左下角的Ctrl开始布局整个键盘
    addKey(pos,osgGA::GUIEventAdapter::KEY_Control_L,"Ctrl",2.0f,0.5f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Super_L,"Super",2.0f,0.5f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Alt_L,"Alt",2.0f,0.5f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Space,"Space",3.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Mode_switch,"Switch",2.0f,0.5f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Super_R,"Super",2.0f,0.5f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Menu,"Menu",2.0f,0.5f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Control_R,"Ctrl",2.0f,0.5f);
	
    osg::Vec3 middle(pos.x()+1.0f,0.0f,0.0f);//定义一个新的位置;注意这里的pos.x已经改变好多次了
    
    pos.x() = 0.0f;//重新回零
    pos.z() += 1.0f;//提高一行
	//第二行
    addKey(pos,osgGA::GUIEventAdapter::KEY_Shift_L,"Shift",2.0f,0.5f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Backslash,"\\",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Z,"Z",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_X,"X",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_C,"C",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_V,"V",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_B,"B",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_N,"N",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_M,"M",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Comma,",",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Period,".",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Slash,"/",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Shift_R,"Shift",2.0f,0.5f);
	//同理第三行
    pos.x() = 0.0f;
    pos.z() += 1.0f;
   
    addKey(pos,osgGA::GUIEventAdapter::KEY_Caps_Lock,"Caps",2.0f,0.5f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_A,"A",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_S,"S",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_D,"D",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_F,"F",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_G,"G",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_H,"H",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_J,"J",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_K,"K",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_L,"L",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Semicolon,";",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Quote,"'",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Hash,"#",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Return,"Return",4.0f,0.5f);
	 //第四行
    pos.x() = 0.0f;
    pos.z() += 1.0f;
    
    addKey(pos,osgGA::GUIEventAdapter::KEY_Tab,"Tab",2.0f,0.5f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Q,"Q",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_W,"W",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_E,"E",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_R,"R",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_T,"T",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Y,"Y",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_U,"U",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_I,"I",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_O,"O",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_P,"P",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Leftbracket,"[",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Rightbracket,"]",1.0f,1.0f);
	 //第五行
    pos.x() = 0.0f;
    pos.z() += 1.0f;
    
    addKey(pos,osgGA::GUIEventAdapter::KEY_Backquote,"`",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_1,"1",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_2,"2",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_3,"3",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_4,"4",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_5,"5",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_6,"6",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_7,"7",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_8,"8",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_9,"9",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_0,"0",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Minus,"-",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Equals,"=",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_BackSpace,"Backspace",3.0f,0.5f);

    pos.x() = 0.0f;
    pos.z() += 1.5f;
     //第六行
    float F_height = 0.5f;
    addKey(pos,osgGA::GUIEventAdapter::KEY_Escape,"Esc",2.0f,F_height);
    addKey(pos,osgGA::GUIEventAdapter::KEY_F1,"F1",1.0f,F_height);
    addKey(pos,osgGA::GUIEventAdapter::KEY_F2,"F2",1.0f,F_height);
    addKey(pos,osgGA::GUIEventAdapter::KEY_F3,"F3",1.0f,F_height);
    addKey(pos,osgGA::GUIEventAdapter::KEY_F4,"F4",1.0f,F_height);
    addKey(pos,osgGA::GUIEventAdapter::KEY_F5,"F5",1.0f,F_height);
    addKey(pos,osgGA::GUIEventAdapter::KEY_F6,"F6",1.0f,F_height);
    addKey(pos,osgGA::GUIEventAdapter::KEY_F7,"F7",1.0f,F_height);
    addKey(pos,osgGA::GUIEventAdapter::KEY_F8,"F8",1.0f,F_height);
    addKey(pos,osgGA::GUIEventAdapter::KEY_F9,"F9",1.0f,F_height);
    addKey(pos,osgGA::GUIEventAdapter::KEY_F10,"F10",1.0f,F_height);
    addKey(pos,osgGA::GUIEventAdapter::KEY_F11,"F11",1.0f,F_height);
    addKey(pos,osgGA::GUIEventAdapter::KEY_F12,"F12",1.0f,F_height);


    
    float cursorMoveHeight=0.35f;

    pos = middle;    //前面第一行右边+1,前后左右很值得玩味
    addKey(pos,osgGA::GUIEventAdapter::KEY_Left,"Left",1.0f,cursorMoveHeight);
    osg::Vec3 down = pos;
    addKey(pos,osgGA::GUIEventAdapter::KEY_Down,"Down",1.0f,cursorMoveHeight);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Right,"Right",1.0f,cursorMoveHeight);
    
    osg::Vec3 keypad = pos;
    keypad.x()+=1.0f;

    pos = down;
    pos.z() += 1.0f;
    
    addKey(pos,osgGA::GUIEventAdapter::KEY_Up,"Up",1.0f,cursorMoveHeight);
    

    float homeHeight = 0.35f;//继续从主键盘右边开始Z的高度增加
    pos = middle;
    pos.z() += 3.0;    
    addKey(pos,osgGA::GUIEventAdapter::KEY_Delete,"Delete",1.0f,homeHeight);
    addKey(pos,osgGA::GUIEventAdapter::KEY_End,"End",1.0f,homeHeight);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Page_Down,"Page\nDown",1.0f,homeHeight);
    
    pos = middle;
    pos.z() += 4.0;    
    addKey(pos,osgGA::GUIEventAdapter::KEY_Insert,"Insert",1.0f,homeHeight);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Home,"Home",1.0f,homeHeight);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Page_Up,"Page\nUp",1.0f,homeHeight);

    pos = middle;
    pos.z() += 5.5;    
    addKey(pos,osgGA::GUIEventAdapter::KEY_Print,"PrtScrn\nSysRq",1.0f,homeHeight);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Scroll_Lock,"ScrLk",1.0f,homeHeight);
    addKey(pos,osgGA::GUIEventAdapter::KEY_Pause,"Pause\nBreak",1.0f,homeHeight);
    
    

    pos = keypad;//接着从前后左右的右边继续
    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Insert,"0",2.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Delete,".",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Enter,"Enter",1.0f,homeHeight);
    
    pos = keypad;
    pos.z() += 1.0f;
    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_End,"1",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Down,"2",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Page_Down,"3",1.0f,1.0f);

    pos = keypad;
    pos.z() += 2.0f;
    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Left,"4",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Begin,"5",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Right,"6",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Add,"+",1.0f,1.0f);

    pos = keypad;
    pos.z() += 3.0f;
    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Home,"7",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Up,"8",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Page_Up,"9",1.0f,1.0f);

    pos = keypad;
    pos.z() += 4.0f;
    addKey(pos,osgGA::GUIEventAdapter::KEY_Num_Lock,"Num\nLock",1.0f,0.3f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Divide,"/",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Multiply,"*",1.0f,1.0f);
    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Subtract,"-",1.0f,1.0f);
    //例子写得好耐心!赞一个
    float totalWidth = pos.x()-origin.x();//最后得到整个键盘的宽度
    pos = origin;//回零
    pos.z() += -1.5f;//向下定位

    osg::Geode* geodeInput = new osg::Geode;//定义输入绘制
    {
        _inputText = new osgText::Text;
        _inputText->setFont("fonts/arial.ttf");
        _inputText->setColor(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
        _inputText->setCharacterSize(1.0f);
        _inputText->setMaximumWidth(totalWidth);//设置这行文本最大的跨度
        _inputText->setPosition(pos);
        _inputText->setDrawMode(osgText::Text::TEXT/*||osgText::Text::BOUNDINGBOX*/);
        _inputText->setAlignment(osgText::Text::BASE_LINE);
        _inputText->setAxisAlignment(osgText::Text::XZ_PLANE);
        _inputText->setDataVariance(osg::Object::DYNAMIC);这个函数不觉得很神秘吗动态的东东
        _inputText->setText("Press some keys...");
        
        geodeInput->addDrawable(_inputText.get());
        
        _scene->addChild(geodeInput);//将输入部分也加入场景
    }

}



//来到了键盘接受函数


class KeyboardEventHandler : public osgGA::GUIEventHandler
{
public:
    
        KeyboardEventHandler(KeyboardModel* keyboardModel):
            _keyboardModel(keyboardModel) {}
    
        virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
        {

#if 1

//            osg::notify(osg::NOTICE)<<"Mouse "<<ea.getButtonMask()<<std::endl;

            #define PRINT(mask) osg::notify(osg::NOTICE)<<#mask<<" ="<<(ea.getModKeyMask() & mask)<<std::endl;
            switch(ea.getEventType())
            {
                case(osgGA::GUIEventAdapter::KEYDOWN):
                case(osgGA::GUIEventAdapter::KEYUP):
                {
                    osg::notify(osg::NOTICE)<<std::endl;
                    PRINT(osgGA::GUIEventAdapter::MODKEY_LEFT_SHIFT);
                    PRINT(osgGA::GUIEventAdapter::MODKEY_RIGHT_SHIFT);
                    PRINT(osgGA::GUIEventAdapter::MODKEY_LEFT_ALT);
                    PRINT(osgGA::GUIEventAdapter::MODKEY_RIGHT_ALT);
                    PRINT(osgGA::GUIEventAdapter::MODKEY_LEFT_CTRL);
                    PRINT(osgGA::GUIEventAdapter::MODKEY_RIGHT_CTRL);
                    PRINT(osgGA::GUIEventAdapter::MODKEY_LEFT_META);
                    PRINT(osgGA::GUIEventAdapter::MODKEY_RIGHT_META);
                    PRINT(osgGA::GUIEventAdapter::MODKEY_LEFT_SUPER);
                    PRINT(osgGA::GUIEventAdapter::MODKEY_RIGHT_SUPER);
                    PRINT(osgGA::GUIEventAdapter::MODKEY_LEFT_HYPER);
                    PRINT(osgGA::GUIEventAdapter::MODKEY_RIGHT_HYPER);
                    PRINT(osgGA::GUIEventAdapter::MODKEY_NUM_LOCK);
                    PRINT(osgGA::GUIEventAdapter::MODKEY_CAPS_LOCK);
                    break;
                }
                default:
                    break;
            }
#endif
            switch(ea.getEventType())
            {
                case(osgGA::GUIEventAdapter::KEYDOWN):
                {
                    _keyboardModel->keyChange(ea.getKey(), ea.getUnmodifiedKey(),1);//显示那个按下的
                    return true;
                }
                case(osgGA::GUIEventAdapter::KEYUP):
                {
                    _keyboardModel->keyChange(ea.getKey(), ea.getUnmodifiedKey(),0);//显示弹起后
                    return true;
                }

                default:
                    return false;
            }
		}
        
        osg::ref_ptr<KeyboardModel> _keyboardModel;
        
};

int main(int , char **)
{
    osgViewer::Viewer viewer;
	//键盘类
    osg::ref_ptr<KeyboardModel> keyboardModel = new KeyboardModel;

    viewer.addEventHandler(new osgViewer::StatsHandler);
    viewer.addEventHandler(new osgViewer::WindowSizeHandler());
	//将加输入时间
    viewer.addEventHandler(new KeyboardEventHandler(keyboardModel.get()));
	//渲染
    viewer.setSceneData( keyboardModel->getScene() );

    return viewer.run();
}

Este ejemplo aprende eventos de entrada de teclado e indirectamente comprende la función de cambio de nodo

Supongo que te gusta

Origin blog.csdn.net/sun19890716/article/details/50945950
Recomendado
Clasificación