OSGEarth之坐标转换

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yangsen600/article/details/84564583
// 屏幕坐标转世界坐标
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));
}

猜你喜欢

转载自blog.csdn.net/yangsen600/article/details/84564583