osg拖拽器

#include <osgViewer/Viewer>
#include <osg/Node>
#include <osg/Geometry>
#include <osg/Geode>
#include <osg/Group>
#include <osg/AutoTransform>
#include <osg/ProxyNode>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgText/Text>
#include <osgUtil/Optimizer>
#include<osg/Camera>
#include<osgViewer/Viewer>
#include <osgManipulator/Dragger>
#include <osgManipulator/Selection>
#include <osgManipulator/Constraint>
#include <osgManipulator/TranslateAxisDragger>
#include <osgManipulator/CommandManager>
#include<osgFX/Scribe>
#include <iostream>
#include <thread>
#include <future>
#include <string>
#include<map>
class PickModeHandler :public osgGA::GUIEventHandler
{
public:
	PickModeHandler() :_activeDrager(0) {}
	bool handle(const osgGA::GUIEventAdapter&ea, osgGA::GUIActionAdapter&aa, osg::Object*, osg::NodeVisitor*)
	{
		osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
		if (!view)
		{
			return false;
		}

		switch (ea.getEventType())
		{
		case osgGA::GUIEventAdapter::PUSH:
		{
			_pointer.reset();
			osgUtil::LineSegmentIntersector::Intersections hits;
			if (view->computeIntersections(ea.getX(), ea.getY(), hits))
			{
				_pointer.setCamera(view->getCamera());
				_pointer.setMousePosition(ea.getX(), ea.getY());

				for (auto&it : hits)
				{
					_pointer.addIntersection(it.nodePath, it.getLocalIntersectPoint());
				}
	
				auto hitLst = _pointer._hitList.front().first;
				for (auto&k : hitLst)
				{
					osgManipulator::Dragger*pDrager = dynamic_cast<osgManipulator::Dragger*>(k);
					if (pDrager)
					{
						pDrager->handle(_pointer, ea, aa);
						_activeDrager = pDrager;
						break;
					}
				}
			}
		}
		break;
		case osgGA::GUIEventAdapter::DRAG:
		case osgGA::GUIEventAdapter::RELEASE:
		{
			if (_activeDrager)
			{
				_pointer._hitIter = _pointer._hitList.begin();
				_pointer.setCamera(view->getCamera());
				_pointer.setMousePosition(ea.getX(), ea.getY());
				_activeDrager->handle(_pointer, ea, aa);
			}

			if (osgGA::GUIEventAdapter::RELEASE == ea.getEventType())
			{
				_activeDrager = nullptr;
				_pointer.reset();
			}
		}
		break;
		default:
			break;
		}
	}

protected:
	osgManipulator::PointerInfo	_pointer;
	osgManipulator::Dragger* _activeDrager;
};
int main()
{
	osg::Node*pNode = osgDB::readNodeFile("cow.osg");
	//osg::ref_ptr<osgFX::Scribe> sc = new osgFX::Scribe();//添加一个scribe节点,该节点下的模型会被加白描线高亮显示。  
	//sc->addChild(pNode);//将模型牛加入到scribe节点中,那么加入之后,该牛就会有白边高亮显示  
	
	osg::ref_ptr<osgManipulator::Selection> selection = new osgManipulator::Selection;
	selection->addChild(pNode);

	float scale = pNode->getBound().radius() * 6.0;
	osg::Vec3f v = pNode->getBound().center();
	osg::ref_ptr<osgManipulator::TranslateAxisDragger> spDrager = new osgManipulator::TranslateAxisDragger;
	spDrager->setupDefaultGeometry();
	spDrager->setMatrix(osg::Matrix::scale(scale, scale, scale)* osg::Matrix::translate(pNode->getBound().center()));
	
	osg::ref_ptr<osg::Group> spGroup = new osg::Group;
	//spGroup->addChild(sc.get());//将加白边的模型牛加入到默认的原点位置,默认为原点  
	spGroup->addChild(spDrager.get());
	spGroup->addChild(selection.get());
	spGroup->addChild(osgDB::readNodeFile("axes.osgt"));

	osg::ref_ptr<osgManipulator::CommandManager> spManager = new osgManipulator::CommandManager;
	spManager->connect(*spDrager.get(), *selection.get());

	osgViewer::Viewer viewer;
	viewer.addEventHandler(new PickModeHandler);
	viewer.setSceneData(spGroup.get());
	return viewer.run();
}

上述代码可以实现在三个坐标轴上按住,分别沿三个坐标轴拖动小奶牛

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值