本文介绍了如何从3D点获取屏幕坐标? Opengl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经看过很多屏幕坐标到世界坐标的帖子,但没有看到相反的。有没有一个简单的方法来获得屏幕坐标从一个3d点,从任何角度,我认为它?我使用C ++和opengl
I've seen alot of Screen-Coordinate to world coordinate posts, but have not seen the opposite. Is there an easy way of getting screen coordinates from a 3d point, from any angle I view it from? I'm using C++ and opengl
推荐答案
erjot的答案是不完整的。
erjot's answer is incomplete.
还有两件事需要考虑。
- c $ c> homogeneous = P。 M。 world ,
homogeneous
向量是均匀空间中的4-d向量。特别是,要把它带回到[-1:1] ^ 3立方体,你需要用homogeneous.w
除它的前3个坐标。cube.x = homogeneous.x / homogeneous.w
(有些人称之为投影鸿沟)。 - 然后,您需要通过对其应用视口变换将[-1:1] ^ 3立方体转换为窗口坐标。
window.x = viewport.x + viewport.width *(cube.x + 1)/ 2
- with
homogeneous = P . M . world
, thehomogeneous
vector is a 4-d vector in homogeneous space. In particular, to bring it back to the [-1:1]^3 cube, you need to divide its first 3 coordinates byhomogeneous.w
.cube.x = homogeneous.x / homogeneous.w
(what some people call the projection divide). - You then need to transform the [-1:1]^3 cube to window coordinates by applying the Viewport transformation to it.
window.x = viewport.x + viewport.width * (cube.x+1)/2
如果你关心最后的Z,转换不是通过视口,而是DepthRange。
In case you care about the final Z, the transformation is not going through viewport, but DepthRange instead.
这篇关于如何从3D点获取屏幕坐标? Opengl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!