|
我创建一个osgGA::GUIEventHandler,然后再handle里定义
osgViewer::Renderer *pRenderer = dynamic_cast<osgViewer::Renderer*>(pCamera->getRenderer());
osgUtil::SceneView *pSceneView = pRenderer->getSceneView(0);
osg::Vec3 ptNear, ptFar;
int xWin = pSceneView->getViewport()->width()*(ea.getX()+1)/2;
int yWin = pSceneView->getViewport()->height()*(ea.getY()+1)/2;
pSceneView->projectWindowXYIntoObject(xWin, yWin, ptNear, ptFar);//鼠标单击射线在世界坐标系中Znear、Zfar的交点
osg::Vec3 diff = ptFar - ptNear;
diff.normalize();
FPoint3 ptGround;
vtTerrain *pTerrain = m_pEnviro->GetCurrentTerrain();
vtHeightField3d *pHeightField = pTerrain->GetHeightField();
//这一步获取射线与地面的交点,为自定义函数
pHeightField->CastRayToSurface( FPoint3(ptNear.x(), ptNear.y(), ptNear.z()),
FPoint3(diff.x(), diff.y(), diff.z()), ptGround );
DPoint2 ptEarth;
g_Conv.ConvertToEarth(ptGround.x, ptGround.z, ptEarth);
这段代码用于获取鼠标当前拾取的点在场景中的坐标,然后把物体移动到鼠标点,发现物体并不是完全按照鼠标移动,如图:总是有些误差:
个人觉得
int xWin = pSceneView->getViewport()->width()*(ea.getX()+1)/2;
int yWin = pSceneView->getViewport()->height()*(ea.getY()+1)/2;
这里是不是有些问题,请各位老师帮帮看下,谢谢了
|
|