本文主要是介绍OSG鼠标选择求交,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
OSG鼠标选择求交
求交方法一:(用WINDOW坐标值,在相机下求交)
//osg::ref_ptr< osgUtil::LineSegmentIntersector > picker = new osgUtil::LineSegmentIntersector(
// osgUtil::Intersector::WINDOW, ea.getX(), ea.getY());
//osgUtil::IntersectionVisitor iv( picker.get());
//cameraMaster->accept( iv);//(从相机往下遍历)
//求交方法二:(直接用view求交)
//view->computeIntersections( x, y, intersections);
//求交方法三:(用世界坐标值,既可在某个节点node下求交,也可在相机下求交。但需注意,在某个节点node下求交时,需要把该node的有矩阵变换的父节点都用上,比如根节点->MT节点->cow模型节点,则可用MT节点->accept( iv)或根节点->accept( iv)。求交时是根据执行accept( iv)的节点向下遍历,求出节点的真实世界坐标。而如果用cow->accept( iv) ,则会忽略父节点的MT节点,导致求不出真正的世界坐标值,这样求交会产生错误)
osg::ref_ptr< osgUtil::LineSegmentIntersector > picker =new osgUtil::LineSegmentIntersector(
nearPoint, farPoint);//线段(真实的世界坐标)
osgUtil::IntersectionVisitor iv( picker.get());
g_grpMouse->getParent( 0)->getChild( 0)->asGroup()->getChild( 0)->accept( iv);//模型求交
//求最前交点 方法一:
if (picker->containsIntersections())
{ //获取最前的交点。
osg::Vec3 ptWorldIntersectPointFirst= picker->getFirstIntersection().getWorldIntersectPoint();
cout<<"world coords vertex("<< ptWorldIntersectPointFirst.x()<<","
<< ptWorldIntersectPointFirst.y()<< ","<< ptWorldIntersectPointFirst.z()<<")"<< std::endl;
}
/*下面方法也可以计算求出最前的交点:*/
//求最前交点 方法二:
/*double dLen2Shortest= DBL_MAX, dLenTmp; osgUtil::LineSegmentIntersector::Intersections::iterator hitrShortest;
osgUtil::LineSegmentIntersector::Intersections intersections= picker->getIntersections();
for( osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin();
hitr != intersections.end();
++hitr)
{
//求离视点最近的点,即鼠标选择的最前面的点
dLenTmp= ( ptEye.x()- hitr->getWorldIntersectPoint().x())*
( ptEye.x()- hitr->getWorldIntersectPoint().x())+
( ptEye.y()- hitr->getWorldIntersectPoint().y())*
( ptEye.y()- hitr->getWorldIntersectPoint().y())+
( ptEye.z()- hitr->getWorldIntersectPoint().z())*
( ptEye.z()- hitr->getWorldIntersectPoint().z());
if ( dLen2Shortest> dLenTmp)
{
dLen2Shortest= dLenTmp;
hitrShortest= hitr;
}
}
其中ptEye为视点的世界坐标值:
osg::Matrix _inverseMV;
_inverseMV.invert( cameraMaster->getViewMatrix());
osg::Vec3 ptEye= osg::Vec3( 0, 0, 0) * _inverseMV;
*/
参考代码如下:
/*OSG中的HUD实时显示视点坐标*/
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osg/Geode>
#include <osg/Depth>
#include <osg/CameraNode>
#include <osgText/Text>
#include <osgGA/TrackballManipulator>
#include <osg/LineWidth>
#include <osg/Point>
#include <osg/ShapeDrawable>
#include <osg/MatrixTransform>
#include <iostream>
#include <sstream>
#pragma comment(
这篇关于OSG鼠标选择求交的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!