OSGEarth之座標轉換

// 屏幕座標轉世界座標
osg::Vec3d ScreenToWorld(const osg::Vec3d screen)
{
	osg::Camera* camera = _global->Viewer->getCamera();
	osg::Matrix VPW = camera->getViewMatrix() * camera->getProjectionMatrix() * camera->getViewport()->computeWindowMatrix();
	osg::Matrix inverseVPW = osg::Matrix::inverse(VPW);
	osg::Vec3d world = screen * inverseVPW;
	return world;
}

// 世界座標轉屏幕座標
osg::Vec3d WorldToScreen(const osg::Vec3d world)
{
	osg::Camera* camera = _global->Viewer->getCamera();
	osg::Matrix VPW = camera->getViewMatrix() * camera->getProjectionMatrix() * camera->getViewport()->computeWindowMatrix();
	osg::Vec3d screen = world * VPW;
	return screen;
}
// 世界座標轉經緯度
osg::Vec3d WorldToLonLatAlt(const osg::Vec3d world)
{
	osg::EllipsoidModel* em = new osg::EllipsoidModel();
	osg::Vec3d lonLatAlt;
	em->convertXYZToLatLongHeight(world.x(), world.y(), world.z(), lonLatAlt.y(), lonLatAlt.x(), lonLatAlt.z());
	lonLatAlt.x() = osg::RadiansToDegrees(lonLatAlt.x());
	lonLatAlt.y() = osg::RadiansToDegrees(lonLatAlt.y());
	return lonLatAlt;
}
// 經緯度轉世界座標
osg::Vec3d LonLatAltToWorld(const osg::Vec3d lonLatAlt)
{
	osg::Vec3d world;
	osg::EllipsoidModel* em = new osg::EllipsoidModel();
	em->convertLatLongHeightToXYZ(osg::DegreesToRadians(lonLatAlt.y()), osg::DegreesToRadians(lonLatAlt.x()), lonLatAlt.z(), world.x(), world.y(), world.z());
	return world;
}
// 屏幕座標轉經緯度
osg::Vec3d ScreenToLonLatAlt(const osg::Vec3d screen)
{
	return WorldToLonLatAlt(ScreenToWorld(screen));
}
// 經緯度轉屏幕座標
osg::Vec3d LonLatAltToScreen(const osg::Vec3d lonLatAlt)
{
	return WorldToScreen(LonLatAltToWorld(lonLatAlt));
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章