Qt OpenGL-如何基于鼠标单击获取对象 [英] Qt OpenGL- How to get the object based on the mouse click

查看:601
本文介绍了Qt OpenGL-如何基于鼠标单击获取对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Qt-openGL中创建一个应用程序.在这里,我必须基于鼠标单击来了解被单击的对象.我的想法是存储点(在QWidget中对象的区域),并使鼠标单击与这些点匹配.有人可以说该怎么做吗?或者有人可以以其他方式表现出来吗?

I am trying to make an application in Qt-openGL. Here, I have to know the clicked object, based on the mouse click. My idea is storing the points(object's area in QWidget), and match the mouse click against these points. Can anybody say how to do this?, or Can any body show any other way?

推荐答案

此问题通常称为拾取". OpenGL本身只是画图,没有几何对象管理可言(OpenGL有对象,但是它们是您通常所说的资源).

This problem is usually known as "picking". OpenGL itself just draws things, there's no geometry object management to speak of (OpenGL has objects, but they are, what you'd normally call resources).

这几天实现OpenGL拾取的通常方法是检索单击位​​置(glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth))处的深度值,然后将其解投影到世界空间(gluUnProject(...))中.这会为您提供点击点的(x,y,z).

The usual way to implement OpenGL picking these days is retrieving the depth value at the click position (glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth)) and unproject it into world space (gluUnProject(...)). This gives you the (x,y,z) of the clicked point.

由于通常会通过将细分结构遍历到单击坐标来管理某些空间细分结构(BSP,Kd等)中的几何,因此可以通过这种方式检索对象.

Since you'll usually manage your geometry in some spatial subdivision structure (BSP, Kd, etc.) by traversing the subdivision structure to the click coordinates you can retrieve the object that way.

另一种方法是在单击之后将光线投射到场景中,并进行光线/边界体积相交测试.

Another method is projecting a ray that follows the click into the scene and do ray / bounding volume intersection tests.

我强烈不鼓励使用旧的OpenGL选择机制:使用起来既缓慢又麻烦.

I strongly discourage the use of the old OpenGL selection mechanism: It's slow and cumbersome to use.

这篇关于Qt OpenGL-如何基于鼠标单击获取对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆