本文主要是介绍鼠标单击点在世界坐标系中的射线 计算原理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
鼠标单击点在世界坐标系中的射线 计算原理
参考osgManipulator/Dragger.cpp:
求鼠标单击射线在世界坐标系中Znear、Zfar的交点:
bool PointerInfo::projectWindowXYIntoObject(const osg::Vec2d& windowCoord, osg::Vec3d& nearPoint, osg::Vec3d& farPoint) const
{ nearPoint = osg::Vec3d(windowCoord.x(),windowCoord.y(),0.0)*_inverseMVPW;
farPoint = osg::Vec3d(windowCoord.x(),windowCoord.y(),1.0)*_inverseMVPW;
return true;
}
窗口坐标值(winx,winy,深度值) =世界坐标点 * 视图矩阵 * 投影矩阵 * 视口矩阵=V世界 * VM * PM * WM。故V世界=(winx,winy,深度值)* (VM * PM * WM)的逆
Znear对应的深度值为0,Zfar对应的深度值为1,故:
Znear对应点坐标为(winx,winy,0)* (VM * PM * WM)的逆,
Zfar对应点坐标为(winx,winy,1)* (VM * PM * WM)的逆。
获取(VM * PM * WM)的逆的代码如下:
osg::ref_ptr<osg::Camera> cameraMaster = viewer->getCamera();
osg::Matrix mvpw = cameraMaster->getViewMatrix() * cameraMaster->getProjectionMatrix();
if ( cameraMaster->getViewport()) mvpw.postMult( cameraMaster->getViewport()->computeWindowMatrix());
osg::Matrix _inverseMVPW;
_inverseMVPW.invert( mvpw);
参考代码如下:
/*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 <iostream>
#include <sstream>
#pragma comment( lib, "osgd.lib"); //.在Debug版本下的库名都加d,如"osgd.lib"
#pragma comment( lib, "osgDBd.lib")
#pragma comment(
这篇关于鼠标单击点在世界坐标系中的射线 计算原理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!