Tutorial - Picking

From Horde3D Wiki
Revision as of 17:08, 15 April 2008 by Volker (talk | contribs) (New page: == Picking the mesh under the mouse cursor == Picking an object within the scene is a common problem. Supposing you want to select the object using a mouse the following sample will show...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Picking the mesh under the mouse cursor

Picking an object within the scene is a common problem. Supposing you want to select the object using a mouse the following sample will show you how you get the NodeHandle for the object under the mouse cursor.

Based on the knight example in the Horde3D SDK we will first remove the mouseMoveListener. To enable the mouse cursor again we have to remove the
glfwDisable( GLFW_MOUSE_CURSOR ); and glfwEnable( GLFW_MOUSE_CURSOR ); statements, too.

Then we define a mouseButtonListener in the main.cpp:

Normalize mouse button for Horde3DUtils::pickNode.
void GLFWCALL mouseButtonListener( int button, int action )
{
   // If left mouse button has been pressed...
   if( button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
   {
       int x, y;
       int width, height;
       // get mouse position
       glfwGetMousePos(&x, &y);
       // get window size for normalization
       glfwGetWindowSize(&width, &height);
       // pick the node under the normalized buttons
       app->pick( x / float( width ), (height - y) / float( height ) );
   }
}

For the use of Horde3DUtils::pickNode we have to normalize the window coordinates received by glfwGetMousePos. That's why we need to call glfwGetWindowSize additionally.

Then in the app.cpp we implement a call to the pickNode method of Horde3DUtils.

Get the NodeHandle under the mouse cursor.
void Application::pick( float dX, float dY )
{
   NodeHandle node = Horde3DUtils::pickNode(_cam, dX, dY);
}