예제 #1
0
//PICK动作
void CPickHandler::pick(osg::ref_ptr<osgViewer::Viewer> viewer, const osgGA::GUIEventAdapter& ea)
{
	//创建一个线段交集检测对象
	osgUtil::LineSegmentIntersector::Intersections intersections;

	std::string gdlist="";
	//申请一个流
	std::ostringstream os;
	//得到鼠标的位置
	float x = ea.getX();
	float y = ea.getY();
	//如果没有发生交集运算,及鼠标没有点中物体
	if (viewer->computeIntersections(x,y,intersections))
	{
		//得到相交交集的交点
		for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin();
			hitr != intersections.end();
			++hitr)
		{
			//输入流
			os<<"Mouse in World  X:"<< hitr->getWorldIntersectPoint().x()<<"     Y: "<<
				 hitr->getWorldIntersectPoint().y()<<"     Z: "<< hitr->getWorldIntersectPoint().z()<<std::endl ;
		}
	}
	//输入流
	os<<"Viewer Position X: "<<position[0]<<"     Y: "<<position[1]<<"     Z: "<<position[2]<<std::endl ;

	gdlist += os.str();
	
	//设置显示内容
	setLabel(gdlist);
}
예제 #2
0
//***********************************************************
//FUNCTION:
bool CPickNode::__pickNode(osg::ref_ptr<osgViewer::View> vView, float vX, float vY)
{
	osg::ref_ptr<osg::Node> SelectedNode;
	osg::ref_ptr<osg::Group> ParentNode;

	osgUtil::LineSegmentIntersector::Intersections intersections;
	if (vView->computeIntersections(vX, vY, intersections))
	{
		osgUtil::LineSegmentIntersector::Intersection intersection = *intersections.begin();
		osg::NodePath& nodePath = intersection.nodePath;

		SelectedNode = (nodePath.size() > 1) ? nodePath[nodePath.size()-1] : 0;
		ParentNode = (nodePath.size() >= 2) ? dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]) : nullptr;
	}

	if (SelectedNode.get() && ParentNode.get())
	{
		if (SelectedNode == m_PrevSelectedNode) return false;

		if (SelectedNode != m_PrevSelectedNode && m_PrevSelectedNode)
		{
			m_PrevParentGroup->replaceChild(m_PrevMatrixTransform.get(), m_PrevSelectedNode.get());
		}

		m_PrevSelectedNode = SelectedNode;
		m_PrevParentGroup = ParentNode;

		const osg::BoundingSphere& NodeBoundingSphere = SelectedNode->getBound();
		osg::ref_ptr<osg::Geode> LineNode = new osg::Geode;
		osg::ref_ptr<osg::Geode> MaskNode = new osg::Geode;

		__drawCoordinateSystem(NodeBoundingSphere.center(), NodeBoundingSphere.radius(), LineNode, MaskNode);
		osg::ref_ptr<osg::MatrixTransform> CurMatrixTransform = new osg::MatrixTransform;

		ParentNode->replaceChild(SelectedNode.get(), CurMatrixTransform.get());
		CurMatrixTransform->addChild(SelectedNode.get());
		CurMatrixTransform->addChild(LineNode.get());
		CurMatrixTransform->addChild(MaskNode.get());
 		m_PrevMatrixTransform = CurMatrixTransform;

		return true;
	}

	return false;
}