其他分享
首页 > 其他分享> > 自定义漫游器

自定义漫游器

作者:互联网

1 前视图(正视图)
2 后视图
3 右视图
4 左视图
5 上视图(顶视图)
6 下视图(仰视图)

class ViewManipulator : public osgGA::TrackballManipulator
{
public:
	virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) override
	{
		switch (ea.getEventType())
		{
		case osgGA::GUIEventAdapter::KEYDOWN:
			moveToView(ea);
			break;
		}

		if (ea.getHandled())
			return false;

		return osgGA::TrackballManipulator::handle(ea, aa);
	}

private:
	void moveToView(const osgGA::GUIEventAdapter& ea)
	{
		home(0.0);
		switch (ea.getKey())
		{
		case osgGA::GUIEventAdapter::KEY_1:
		{
			setRotation(osg::Quat(
				osg::DegreesToRadians(90.0f), osg::X_AXIS,
				osg::DegreesToRadians(0.0f), osg::Y_AXIS,
				osg::DegreesToRadians(0.0f), osg::Z_AXIS
			));
			ea.setHandled(true);
			break;
		}
		case osgGA::GUIEventAdapter::KEY_2:
		{
			setRotation(osg::Quat(
				osg::DegreesToRadians(90.0f), osg::X_AXIS,
				osg::DegreesToRadians(0.0f), osg::Y_AXIS,
				osg::DegreesToRadians(180.0f), osg::Z_AXIS
			));
			ea.setHandled(true);
			break;
		}
		case osgGA::GUIEventAdapter::KEY_3:
		{
			setRotation(osg::Quat(
				osg::DegreesToRadians(90.0f), osg::X_AXIS,
				osg::DegreesToRadians(0.0f), osg::Y_AXIS,
				osg::DegreesToRadians(90.0f), osg::Z_AXIS
			));
			ea.setHandled(true);
			break;
		}
		case osgGA::GUIEventAdapter::KEY_4:
		{
			setRotation(osg::Quat(
				osg::DegreesToRadians(90.0f), osg::X_AXIS,
				osg::DegreesToRadians(0.0f), osg::Y_AXIS,
				osg::DegreesToRadians(270.0f), osg::Z_AXIS
			));
			ea.setHandled(true);
			break;
		}
		case osgGA::GUIEventAdapter::KEY_5:
		{
			setRotation(osg::Quat(osg::DegreesToRadians(0.0f), osg::X_AXIS));
			ea.setHandled(true);
			break;;
		}
		case osgGA::GUIEventAdapter::KEY_6:
		{
			setRotation(osg::Quat(osg::DegreesToRadians(180.0f), osg::X_AXIS));
			ea.setHandled(true);
			break;
		}
		}
	}
};

int main()
{
	osgViewer::Viewer viewer;

	osg::ref_ptr<osg::Group> group = new osg::Group();
	group->addChild(osgDB::readNodeFile("cow.osg"));

	osg::ref_ptr<ViewManipulator> vm = new ViewManipulator();
	viewer.setCameraManipulator(vm);
	viewer.setSceneData(group);
	viewer.setUpViewInWindow(100, 100, 500, 400);
	return viewer.run();
}

标签:自定义,GUIEventAdapter,ea,DegreesToRadians,osgGA,漫游,osg,AXIS
来源: https://www.cnblogs.com/shiguoliang/p/15239692.html