1. 程式人生 > >OSGEarth之座標轉換

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));
}