osg视角切换,第一人称和第三人称切换

代码示例展示了如何在osgViewer中通过键盘事件处理程序切换第一人称和第三人称(实际使用OrbitManipulator替代)视角。创建FirstPersonManipulator和OrbitManipulator,设置默认操纵器,然后添加一个键盘处理器来监听1和3键,用于在两者间切换视角。此外,还提及了一种直接通过全局变量和函数进行视角切换的方法。

AI提供的osg第一人称视角和第三人称视角切换示例

// Create a viewer
osgViewer::Viewer viewer;

// Create a scene
osg::ref_ptr<osg::Node> scene = createScene();

// Create a first person manipulator
osg::ref_ptr<osgGA::FirstPersonManipulator> firstPersonManipulator = new osgGA::FirstPersonManipulator();

// Create a third person manipulator
osg::ref_ptr<osgGA::ThirdPersonManipulator> thirdPersonManipulator = new osgGA::ThirdPersonManipulator();

// Set the first person manipulator as the default manipulator
viewer.setCameraManipulator(firstPersonManipulator);

// Add the scene to the viewer
viewer.setSceneData(scene);

// Set up a keyboard handler to switch between the first and third person manipulators
class SwitchManipulatorHandler : public osgGA::GUIEventHandler
{
public:
    SwitchManipulatorHandler(osgGA::FirstPersonManipulator* firstPersonManipulator, osgGA::ThirdPersonManipulator* thirdPersonManipulator)
        : _firstPersonManipulator(firstPersonManipulator), _thirdPersonManipulator(thirdPersonManipulator)
    {
    }

    virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
    {
        if (ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN)
        {
            if (ea.getKey() == '1')
            {
                aa.getCameraManipulator()->setHomePosition(osg::Vec3(0.0f, 0.0f, 0.0f), osg::Vec3(0.0f, 0.0f, -1.0f), osg::Vec3(0.0f, 1.0f, 0.0f));
                aa.setCameraManipulator(_firstPersonManipulator);
                return true;
            }
            else if (ea.getKey() == '3')
            {
                aa.getCameraManipulator()->setHomePosition(osg::Vec3(0.0f, 0.0f, 10.0f), osg::Vec3(0.0f, 0.0f, 0.0f), osg::Vec3(0.0f, 1.0f, 0.0f));
                aa.setCameraManipulator(_thirdPersonManipulator);
                return true;
            }
        }
        return false;
    }

private:
    osg::ref_ptr<osgGA::FirstPersonManipulator> _firstPersonManipulator;
    osg::ref_ptr<osgGA::ThirdPersonManipulator> _thirdPersonManipulator;
};

// Add the keyboard handler to the viewer
viewer.addEventHandler(new SwitchManipulatorHandler(firstPersonManipulator, thirdPersonManipulator));

// Run the viewer
viewer.run();

试了一下发现,osg没有osgGA::ThirdPersonManipulator这个类,AI还挺能编的,把osgGA::ThirdPersonManipulator未定义的错误传回给AI,获得下面代码

// Create a viewer
osgViewer::Viewer viewer;

// Create a scene
osg::ref_ptr<osg::Node> scene = createScene();

// Create a first person manipulator
osg::ref_ptr<osgGA::FirstPersonManipulator> firstPersonManipulator = new osgGA::FirstPersonManipulator();

// Create an orbit manipulator
osg::ref_ptr<osgGA::OrbitManipulator> orbitManipulator = new osgGA::OrbitManipulator();

// Set the first person manipulator as the default manipulator
viewer.setCameraManipulator(firstPersonManipulator);

// Add the scene to the viewer
viewer.setSceneData(scene);

// Set up a keyboard handler to switch between the first and third person manipulators
class SwitchManipulatorHandler : public osgGA::GUIEventHandler
{
public:
    SwitchManipulatorHandler(osgGA::FirstPersonManipulator* firstPersonManipulator, osgGA::OrbitManipulator* orbitManipulator)
        : _firstPersonManipulator(firstPersonManipulator), _orbitManipulator(orbitManipulator)
    {
    }

    virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
    {
        if (ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN)
        {
            if (ea.getKey() == '1')
            {
                aa.getCameraManipulator()->setHomePosition(osg::Vec3(0.0f, 0.0f, 0.0f), osg::Vec3(0.0f, 0.0f, -1.0f), osg::Vec3(0.0f, 1.0f, 0.0f));
                aa.setCameraManipulator(_firstPersonManipulator);
                return true;
            }
            else if (ea.getKey() == '3')
            {
                aa.getCameraManipulator()->setHomePosition(osg::Vec3(0.0f, 0.0f, 10.0f), osg::Vec3(0.0f, 0.0f, 0.0f), osg::Vec3(0.0f, 1.0f, 0.0f));
                aa.setCameraManipulator(_orbitManipulator);
                return true;
            }
        }
        return false;
    }

private:
    osg::ref_ptr<osgGA::FirstPersonManipulator> _firstPersonManipulator;
    osg::ref_ptr<osgGA::OrbitManipulator> _orbitManipulator;
};

// Add the keyboard handler to the viewer
viewer.addEventHandler(new SwitchManipulatorHandler(firstPersonManipulator, orbitManipulator));

// Run the viewer
viewer.run();

实际上我是这么用的,直接建立场景后添加了一个按钮,进行视角切换
1.建立全局的漫游器

private:
    osg::ref_ptr<osgGA::FirstPersonManipulator> _firstPersonManipulator;
    osg::ref_ptr<osgGA::OrbitManipulator> _orbitManipulator;
    //设置视角切换标志
    bool ManipulatorFlag ;

2.在函数中切换视角

void ManipulatorChange()
{
	//viewer是全局的变量
	if(ManipulatorFlag)
	{
		viewer->setCameraManipulator(_firstPersonManipulator);
		ManipulatorFlag = !ManipulatorFlag ;
	}
	else
	{
		viewer->setCameraManipulator(_orbitManipulator);
		ManipulatorFlag = !ManipulatorFlag ;
	}
}

触发函数就可以进行视角切换了

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值