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

484

被折叠的 条评论
为什么被折叠?



