如何在 OpenGL ES 1.1 中选择单个三角形?
我想要做的是在屏幕上选择一个点,沿着 z 轴直接穿过该点投射光线,然后返回光线相交的第一个三角形的 xyz 坐标和顶点法线。问题是,我对 OpenGL 不够熟悉,无法解决这个问题。我已经在后台缓冲区上使用不同的颜色在 Windows OpenGL 上进行了选择,但没有涉及单独的三角形。
What I want to do is to select a point on the screen, cast a ray through that point straight down the z-axis and return the x y z coordinates and vertex normals of the first triangle that my ray intersects. Problem is, I'm not familiar enough with OpenGL to figure this out. I've done picking on windows OpenGL using different colors on the back buffer, but nothing that involved individual triangles.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经在 iPhone 上完成了此操作,但正如前面提到的,我必须滚动自己的选择。不可避免地,它对我的特定应用程序具有独特的风味,但从高水平来看,它就是这样的。
您需要一个函数来将屏幕坐标取消投影到世界坐标中。在桌面上,有 gluUnProject(),但在 iPhone 上没有这样的实用程序。就我个人而言,我使用 Mesa 提供的功能完全相同的 iPhone 端口
您可以使用上述函数获取世界空间中的两个点,这两个点表示像素后面的无限线与近剪裁平面和远剪裁平面相交的位置。使用此向量,您必须迭代场景中的三角形,考虑到任何局部变换,并使用光线平面相交算法测试碰撞,这本身可能就是一个问题。
有很多方法可以使射线与三角形相交。我推荐访问维基百科。我会说我使用如下内容:首先,只需将射线与包含三角形的平面相交。然后,测试交点是否确实在相关三角形内。如果是的话,你就完成了。如果没有,请继续。
I have done this on the iPhone, but as mentioned, I had to roll my own picking. Inevitably, it has a flavor unique to my particular application, but at a high level, this is how it goes.
You'll need a function to un-project your screen coordinate into world coordinates. On the desktop, there is gluUnProject(), but on the iPhone there is no such utility. Personally, I use an iPhone port of the very same function available from Mesa here. You may need to tweak it.
You can use the above function to get two points in world space, which represent where the infinite line behind your pixel intersects the near and far clipping plane. Using this vector, you'll have to iterate the triangles in your scene, taking into account any local transformations, and test for collisions using a ray-plane intersection algorithm, which could be a question in and of itself.
There are plenty of ways to intersect a ray with a triangle. I recommend a trip to Wikipedia. I will say that I use something like the following: First, simply intersect the ray with the plane containing the triangle. Then, test to see if the point of intersection is actually within the triangle in question. If it is, you're done. If not, keep going.
OpenGL ES 1.1 没有专用的拾取功能。你必须构建自己的系统才能做到这一点。
OpenGL ES 1.1 has no dedicated picking functionality. You have to construct your own system to do it.
只是从我的头脑中思考,你需要开始,通过任何方法,获得你想要用于采摘的点。如果该点位于不同的坐标系中,您将需要对其进行转换,以确保了解场景中当前的任何变换。之后,您可以沿 z 轴投射一个数组,并使用任何三角形与射线相交算法来获取它经过的第一个三角形。您可以使用场景图或类似的东西来跟踪您的场景。希望这有帮助。
Just thinking off the top of my head you need to start, by whatever method, of getting the point you want to use for the picking. If this point is in a different coordinate system you will need to convert it making sure to be aware of any current transformations you have on your scene. After this you can cast an array down the z axis and using any triangle to ray intersection algorithm to get the first triangle it goes through. You could keep track of your scene using a scene graph or something like that. Hope this helps.