自定义漫游器

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();
}
上一篇:指数基金-投资中的仓位管理也是可以量化的


下一篇:LINUX高性能网络通信堵住笔记之UDP数据读写