在MapGIS K9 TDE三维平台的二次开发中经常用到3D坐标转换为2D坐标的情况,这里分享一个实现的代码。
- void project(const Vector3& worldPos, Vector2 &proj)
- {
- HWND hWnd = TDE::Workspace::getSingletonPtr()->getActiveWindowHandle();
- Camera *pCam = TDE::Workspace::getSingletonPtr()->getCamera(hWnd);
- RenderWindow *pRW = TDE::Workspace::getSingletonPtr()->getRenderWindow(hWnd);
- Matrix4 viewMat = pCam->getViewMatrix(), sProj = pCam->getStandardProjectionMatrix();
- Vector3 p = sProj * viewMat * worldPos;
- //convert to screen point
- proj.x = (1 + p.x) * 0.5 * pRW->getWidth();
- proj.y = (1 - p.y) * 0.5 * pRW->getHeight();
- }