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

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

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 ;
	}
}

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要让DriveManipulator实现第三人称漫游,可以通过以下步骤实现: 1. 在场景中添加一个相机,作为第三人称视角视角。 2. 将相机绑定到DriveManipulator上,使其跟随DriveManipulator的运动。 3. 修改DriveManipulator的控制方式,使其能够控制相机的位置和方向,从而实现第三人称漫游的效果。 具体实现方法如下: 1. 在场景中添加一个相机,将其命名为"ThirdPersonCamera"。 2. 在代码中获取到相机的节点,并将其绑定到DriveManipulator上: ```cpp osg::ref_ptr<osg::Node> cameraNode = osgDB::readNodeFile("ThirdPersonCamera.osg"); driveManipulator->setCameraManipulator(cameraNode); ``` 3. 修改DriveManipulator的控制方式,在handleKeyDown方法中添加控制相机位置和方向的代码: ```cpp case 'w': // 向前移动相机 cameraManipulator->moveForward(); break; case 's': // 向后移动相机 cameraManipulator->moveBackward(); break; case 'a': // 向左移动相机 cameraManipulator->moveLeft(); break; case 'd': // 向右移动相机 cameraManipulator->moveRight(); break; case 'q': // 上升相机 cameraManipulator->moveUp(); break; case 'e': // 下降相机 cameraManipulator->moveDown(); break; case osgGA::GUIEventAdapter::SCROLL: // 滚动鼠标控制相机旋转 cameraManipulator->rotate(event.getScrollingDeltaY()); break; ``` 4. 在CameraManipulator类中添加相机位置和方向的控制方法,例如moveForward、moveBackward、moveLeft、moveRight、moveUp和moveDown等。 5. 在CameraManipulator类中添加相机旋转的控制方法,例如rotate方法,通过改变相机的朝向实现旋转功能。 通过以上步骤,就可以实现DriveManipulator第三人称漫游功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值