问题描述
我正在使用 C++ 中的 GLUT 创建一个 3D 应用程序.
I'm creating a 3D application using GLUT in C++.
现在,我想实现一个类似这样的方法:
Now, I want to implement a method similar to this:
Vector3* MyClass::get3DObjectfromMouse(int mouseX, int mouseY);
如何实现这个方法?
推荐答案
正如 Andon M. Coleman,实现此目的的一种方法是使用未投影的屏幕坐标进行光线/对象相交测试.这种技术通常称为挑选.
As it was commented by Andon M. Coleman, one way you can achieve this is by doing a ray/object intersection test, with unprojected screen coordinates. This technique is commonly known as picking.
假设我们有一个 3D 对象类型/类:
Assume we have a 3D object type/class:
3D 拾取函数将返回所有对象的列表,这些对象与从近平面中的给定 2D 点到远平面中的同一点的直线相交.
A 3D picking function would return a list of all objects that are intersected by a line going from the given 2D point in the near plane to the same point in the far plane.
UnProject()
函数看起来像这样:
澄清一下,TestLineIntersection()
与 AABB 对比交叉测试.边界框应该转换到世界空间,因为它通常表示为局部模型空间中的一组点.
To clarify, TestLineIntersection()
does a line vs AABB intersection test. The bounding box should be transformed to world-space, since it is usually expressed as a set of points in local model-space.
这篇关于如何将屏幕上的鼠标坐标转换为 3D 坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!