OSG鼠标选择求交

这篇博客介绍了在OSG(OpenSceneGraph)中使用鼠标选择与场景中的对象进行交点计算的三种方法。通过WINDOW坐标值、直接使用视图以及世界坐标值来实现。博主展示了如何获取最接近视点的交点,并提供了代码示例,包括创建HUD(Head-Up Display)实时显示视点坐标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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( lib, "osgd.lib"); //.在Debug版本下的库名都加d,如"osgd.lib"

#pragma comment( lib,

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值