查看: 1362|回复: 7

怎么让摇杆控制场景中模型

[复制链接]

该用户从未签到

发表于 2012-3-21 21:54:26 | 显示全部楼层 |阅读模式
利用的directinput 的摇杆joystick,来实现场景交互

该用户从未签到

发表于 2012-3-22 09:26:32 | 显示全部楼层
just do it

该用户从未签到

 楼主| 发表于 2012-3-22 14:48:24 | 显示全部楼层
tianxiao888 发表于 2012-3-22 09:26
just do it

可是不行啊 ,用的osg自带的osgdirectinput例子,只能输出X+,Y+,X-,Y-,我想通过推动摇杆来控制场景中的模型,怎么通过回调和JoyEvent联系起来,比如向前推摇杆,场景中的模型也向前移动,这能实现么,/* OpenSceneGraph example, osgdirectinput.
*
*  Permission is hereby granted, free of charge, to any person obtaining a copy
*  of this software and associated documentation files (the "Software"), to deal
*  in the Software without restriction, including without limitation the rights
*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*  copies of the Software, and to permit persons to whom the Software is
*  furnished to do so, subject to the following conditions:
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*  THE SOFTWARE.
*/

#include <osgDB/ReadFile>
#include <osgGA/StateSetManipulator>
#include <osgViewer/api/Win32/GraphicsWindowWin32>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <iostream>
#include "DirectInputRegistry"

class CustomViewer : public osgViewer::Viewer
{
public:
    CustomViewer() : osgViewer::Viewer() {}
    virtual ~CustomViewer() {}
   
    virtual void eventTraversal()
    {
        DirectInputRegistry::instance()->updateState( _eventQueue.get() );
        osgViewer::Viewer::eventTraversal();
    }
   
protected:
    virtual void viewerInit()
    {
        osgViewer::GraphicsWindowWin32* windowWin32 =
            dynamic_cast<osgViewer::GraphicsWindowWin32*>( _camera->getGraphicsContext() );
        if ( windowWin32 )
        {
            HWND hwnd = windowWin32->getHWND();
            DirectInputRegistry::instance()->initKeyboard( hwnd );
            //DirectInputRegistry::instance()->initMouse( hwnd );
            DirectInputRegistry::instance()->initJoystick( hwnd );
        }
        osgViewer::Viewer::viewerInit();
    }
};

class JoystickHandler : public osgGA::GUIEventHandler
{
public:
    JoystickHandler() {}
   
    bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
    {
        switch ( ea.getEventType() )
        {
        case osgGA::GUIEventAdapter::KEYDOWN:
            std::cout << "*** Key 0x" << std::hex << ea.getKey() << std::dec << " down ***" << std::endl;
            break;
        case osgGA::GUIEventAdapter::KEYUP:
            std::cout << "*** Key 0x" << std::hex << ea.getKey() << std::dec << " up ***" << std::endl;
            break;
        case osgGA::GUIEventAdapter::USER:
            {
                const JoystickEvent* event = dynamic_cast<const JoystickEvent*>( ea.getUserData() );
                if ( !event ) break;
               
                const DIJOYSTATE2& js = event->_js;
                for ( unsigned int i=0; i<128; ++i )
                {
                    if ( js.rgbButtons )
                        std::cout << "*** Joystick Btn" << i << " = " << (int)js.rgbButtons << std::endl;
                }
               
                if ( js.lX==0x0000 ) std::cout << "*** Joystick X-" << std::endl;
                else if ( js.lX==0xffff ) std::cout << "*** Joystick X+" << std::endl;
               
                if ( js.lY==0 ) std::cout << "*** Joystick Y-" << std::endl;
                else if ( js.lY==0xffff ) std::cout << "*** Joystick Y+" << std::endl;
            }
            return true;
        default:
            break;
        }
        return false;
    }
};

int main( int argc, char** argv )
{
    osg::ArgumentParser arguments( &argc, argv );
    osg::Node* model = osgDB::readNodeFiles( arguments );
    if ( !model ) model = osgDB::readNodeFile( "cow.osgt" );
    if ( !model )
    {
        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
        return 1;
    }
   
    CustomViewer viewer;
    viewer.addEventHandler( new JoystickHandler );
    viewer.addEventHandler( new osgViewer::StatsHandler );
    viewer.addEventHandler( new osgViewer::WindowSizeHandler );
    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
    viewer.setSceneData( model );
    viewer.setUpViewInWindow( 250, 50, 800, 600 );
    return viewer.run();
}

该用户从未签到

发表于 2012-3-23 14:28:35 | 显示全部楼层
应该都能实现啊,你看看例子是怎么做的,照猫画虎啊

该用户从未签到

发表于 2012-3-26 11:54:32 | 显示全部楼层
可以看看锐哥的3.0的codeExample,看能不能帮上忙。这个我是不懂啦。

该用户从未签到

发表于 2012-3-29 17:59:44 | 显示全部楼层
一样的,剩下的就是如何调用和使用Joystick API而已

该用户从未签到

 楼主| 发表于 2012-3-30 21:04:17 | 显示全部楼层
array 老师,我现在用的美国海军教程,坦克自由度的节点运动的例子,我想得到其中一个运动节点的中心坐标,如包围盒中心坐标,是放在回调里么

该用户从未签到

发表于 2012-4-1 15:14:18 | 显示全部楼层
可以
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表