PDA

View Full Version : How to implement in OpenGL selected of 3D objects



ddguan
07-12-2015, 07:05 PM
For example:I have already created three 3D objects,and I want to use the mouse to select one of the objects,after we select one object,we can make it rotate through controling the mouse. I will be appreciate if you can help me solve this problem.

GClements
07-12-2015, 08:42 PM
OpenGL is a rendering library, not a modelling library.

However, it does have some features which can be used to implement picking.

Legacy OpenGL has feedback and selection modes (see glRenderMode (https://www.opengl.org/sdk/docs/man2/xhtml/glRenderMode.xml)), but that's deprecated in OpenGL 3+ (aside from being deprecated, it's not exactly the top of most hardware vendors' priorities, so don't be too surprised if it doesn't actually work on modern systems).

Modern OpenGL has transform-feedback mode (see e.g. [url=https://www.opengl.org/sdk/docs/man3/xhtml/glTransformFeedbackVaryings.xml]), which is similar to feedback mode in legacy OpenGL. Modern OpenGL doesn't have an equivalent of selection mode.

The other option is to render the scene into an "index buffer", i.e. a single-channel integer framebuffer where the value corresponds to the object's "ID". If you need to support older OpenGL versions which don't have integer textures (or don't even have framebuffer objects), you can get away with just rendering the scene with each object having a different colour. But you need to take care to ensure that distinct colours actually remain distinct (e.g. using the bytes forming the object ID directly will fail if you're rendering to a 16-bit display, as you'll lose the least-significant bits of each byte).

EmJayJay
07-13-2015, 03:59 PM
I would completely rely on my own code or other library which would handle this with ease.

But I would do it like this when implementing it.

1. You got the drawn objects sorted or placed so that the scene manager has knowledge of all of them.
2. Get screen coordinates of the mouse click event (I have used GLFW's mouse queries for my purposes)
3. Here comes the tricky part for me which is math related and I would like to see an answer for this also since I'm in doubt about its implementation when it comes to projected rendering (The way we humans see the world). Use the screen coordinates with the camera to grab the direction which the 2 values form and loop through your scene manager's drawable objects to test which of the objects is hit and which is the closest being hit. (Requires some sort of hit box usage) You can simplify this by only using your camera's direction where you get the direction directly.
4. With positive hit you keep that objects ID (pointer or ID, depends on your implementation) saved in your manual rotation handler and have the manual rotation handler add extra rotations to your selected objects rotation.
5. When you render your object it would grab the extra rotations and apply them to the rotation matrix.

Or do it as GClements said.