简体   繁体   English

如何检查点是否在透视投影的四边形内?

[英]How to check if a point is inside a quad in perspective projection?

I want to test if any given point in the world is on a quad/plane? 我想测试世界上是否有任何给定点在四边形/平面上? The quad/plane can be translated/rotated/scaled by any values but it still should be able to detect if the given point is on it. 可以通过任何值平移/旋转/缩放四边形/平面,但它仍应能够检测给定点是否在其上。 I also need to get the location where the point should have been, if the quad was not applied any rotation/scale/translation. 如果四边形未应用任何旋转/缩放/平移,我还需要获取该点应位于的位置。

For example, consider a quad at 0, 0, 0 with size 100x100, rotated at an angle of 45 degrees along z axis. 例如,考虑一个大小为100x100,位于0、0、0的四边形,它沿z轴旋转了45度。 If my mouse location in the world is at ( x, y, 0, ), I need to know if that point falls on that quad in its current transformation? 如果我的鼠标在世界上的位置位于(x,y,0,),我需要知道该点是否在当前变换中的那个四边形上? If yes, then I need to know if no transformations were applied to the quad, where that point would have been on it? 如果是,那么我需要知道是否没有对四边形应用变换,该点在哪里? Any code sample would be of great help 任何代码示例都会有很大帮助

A ray-casting approach is probably simplest: 光线投射方法可能是最简单的:

  1. Use gluUnProject() to get the world-space direction of the ray to cast into the scene. 使用gluUnProject()获取射线投射到场景中的世界空间方向。 The ray's origin is the camera position. 射线的原点是相机的位置。
  2. Put this ray into object space by transforming it by the inverse of your rectangle's transform. 通过与矩形的变换相反的变换将其放入对象空间。 Note that you need to transform both the ray's origin point and direction vector. 请注意,您需要同时变换射线的原点和方向向量。
  3. Compute the intersection point between this ray and the XY plane with a standard ray-plane intersection test. 使用标准射线平面相交测试计算此射线与XY平面之间的交点。
  4. Check that the intersection point's x and y values are within your rectangle's bounds, if they are then that's your desired result. 检查相交点的x和y值是否在矩形的边界内,如果这是您期望的结果。

A math library such as GLM will be very helpful if you aren't confident about some of the math involved here, it has corresponding functions such as glm::unProject() as well as functions to invert matrices and do all the other transformations you'd need. 如果您对此处涉及的某些数学不确定,那么诸如GLM之类的数学库将非常有帮助,它具有相应的函数,例如glm::unProject()以及用于求矩阵求逆和进行所有其他转换的函数需要。

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

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