简体   繁体   English

如何通过两次调用gluUnproject从场景坐标中获取深度值(不使用gluUnProject)

[英]How to get depth value from scene cordinates by calling gluUnproject twice(not using gluUnProject)

In this thread. 这个线程中。 The second answer suggest that : 第二个答案表明:

Solution 2 call the gluUnproject two time, one with clipZ = -1 and another one with clipZ = 1 , you get two point (in world space). 解决方案2调用gluUnproject两次,一次为clipZ = -1 ,另一次为clipZ = 1 ,则得到两个点(在世界空间中)。 With these two point you get a ray and you can use some raycast algorithm to compute the mouse coordinate. 通过这两点,您可以得到一条光线,并且可以使用某些光线投射算法来计算鼠标坐标。

But unfortunately it is not explained there after the suggestion. 但是,不幸的是,建议之后没有对此进行解释。 There is also this thread which explains a direction can be get by calling gluUnproject twice. 还有一个线程说明了通过两次调用gluUnproject可以获得的方向。 Getting a direction makes sense to me but my question is how can I get the depth info by calling gluUnProject twice? 确定方向对我来说很有意义,但我的问题是如何通过两次调用gluUnProject获得深度信息?

You don't get depth info. 您没有获得深度信息。 You only have a X and a Y, but to unproject you need a Z. And no magic is going to give it. 您只有一个X和一个Y,但要取消投影,则需要一个Z。 Instead try to deduce it. 而是尝试推断它。

After you undo the viewport transform you have X,Y in NDC. 撤消视口转换后,您在NDC中具有X,Y。 What Z? 什么Z? Use z1= 1 and X, Y. Unproject. 使用z1= 1和X,Y。取消投影。 Now you have a point P1 in world coordinates. 现在,您在世界坐标中有了一个点P1。 Repeat Unproject but this time with z2= -1 . 重复Unproject,但这一次z2= -1 You get P2. 您得到P2。 Likely you have to deal with forth 'w' coordinate. 您可能必须处理第四个“ w”坐标。 Remember you can extract 3D coordinates after dividing by w. 请记住,除以w后,您可以提取3D坐标。 In homogeneous coordinates w is a scale factor. 在齐次坐标中,w是比例因子。

With P1 and P2 perhaps you can find on your own the intersection between line P1P2 and your model. 使用P1和P2,也许您可​​以自己找到线P1P2与模型之间的交点。

z= -1, 1 are most separated coordinates in NDC, so more accurate the calculation results. z= -1, 1是NDC中最分开的坐标,因此计算结果更加准确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM