Qt OpenGL- How to get the object based on the mouse click
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 thes开发者_C百科e points. Can anybody say how to do this?, or Can any body show any other way?
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).
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.
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.
I strongly discourage the use of the old OpenGL selection mechanism: It's slow and cumbersome to use.
精彩评论