我用 OpenGL 单击了哪里?
我正在 Xcode / OpenGL 中编程,并在我的视图中显示一些 3D 对象。 有没有办法在 OpenGL 中检索我点击的 3D 位置/顶点/面?
I am programming in Xcode / OpenGL and have some 3d objects displayed in my view.
Is there a way I can retrieve in OpenGL the 3D location / vertex / face I clicked on ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常的方法是调用
gluUnProject
两次,在两种情况下使用 x 和 y 坐标,在一种情况下使用 znear,在另一种情况下使用 zfar。这给出了光线穿过的两个点。您的鼠标单击在那条射线上。
现在用该射线碰撞对象(首先是包围体,然后是每个三角形(如果需要的话,或者参数化的话))。最接近的命中就是您想要的。
另一种可能性是读回 z 缓冲区值 (
glReadPixels
)。这应该使用像素缓冲区对象来完成,并分布在多个帧上(否则这将是管道上非常令人讨厌的停顿)。这将为您提供一个 3D 坐标,您可以通过该坐标找到最近的对象。或者,您可以使用遮挡查询(在禁用颜色写入的情况下重绘 1x1 视口)来达到相同的效果。
最后,当然还有选择模式,但这是已弃用的功能,因此您可能不会想要使用它。
The usual way to do this is by calling
gluUnProject
twice, with your x and y coordinates in both cases, and znear in one, and zfar in the other case.That gives you two points through which a ray goes. Your mouse click is on that ray.
Now collide objects (first bounding volumes, then per triangle if you want, or parametrically if possible) with that ray. The closest hit is the one you want.
A different possibility would be to read back the z-buffer value (
glReadPixels
). This should be done with a pixel buffer object and spread over several frames (or it will be a very nasty stall on your pipeline). That will give you a 3D coordinate, for which you can find the closest object.Or, you could use occlusion queries (redrawing a 1x1 viewport with color writes disabled) for the same effect.
Lastly, there is of course selection mode, but this is deprecated functionality, so you will probably not want to use it.
如果您同意使用较新的 opengl 版本中已弃用的内容,另一种方法是使用拾取缓冲区/选择模式: http://www.lighthouse3d.com/opengl/picking/
Another way to do it if you are okay with using something that's deprecated in newer opengl versions is using the picking buffer/selection mode: http://www.lighthouse3d.com/opengl/picking/