Difference between revisions of "Tutorial - Picking"

From Horde3D Wiki
Jump to: navigation, search
m
(Updated the code to work with Beta4 and SVN trunk.)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[category: Tutorials]]
 
 
{|  border="0"  
 
{|  border="0"  
 
| {{ContentBlock|width=800|color=white
 
| {{ContentBlock|width=800|color=white
Line 32: Line 31:
 
   }
 
   }
 
}
 
}
 +
</source>
 +
}}
 +
To activate that listener we have to add the following code to the '''setupWindow''' method:
 +
 +
{{CppSourceCode|description=Activate mouse button listener|code=<source lang="cpp" line="1">
 +
glfwSetMouseButtonCallback( mouseButtonListener );
 
</source>
 
</source>
 
}}
 
}}
 +
 +
  
 
For the use of [http://www.horde3d.org/docs/_utils.html#pickNode Horde3DUtils::pickNode] we have to normalize the window coordinates received by ''glfwGetMousePos''. That's why we need to call ''glfwGetWindowSize'' additionally. Another conversion has to be made, since GLFW has it's origin in the upper left corner while Horde3D requires the origin to be in the lower left corner.  
 
For the use of [http://www.horde3d.org/docs/_utils.html#pickNode Horde3DUtils::pickNode] we have to normalize the window coordinates received by ''glfwGetMousePos''. That's why we need to call ''glfwGetWindowSize'' additionally. Another conversion has to be made, since GLFW has it's origin in the upper left corner while Horde3D requires the origin to be in the lower left corner.  
  
Then in the '''app.cpp''' we implement a call to the pickNode method of Horde3DUtils.
+
Then in the '''app.cpp''' we implement a call to the h3dutPickNode utility function.
  
 
{{CppSourceCode|
 
{{CppSourceCode|
Line 45: Line 52:
 
void Application::pick( float dX, float dY )
 
void Application::pick( float dX, float dY )
 
{
 
{
   NodeHandle node = Horde3DUtils::pickNode(_cam, dX, dY);
+
   H3DNode node = h3dutPickNode(_cam, dX, dY);
 
}
 
}
 
</source>
 
</source>
Line 51: Line 58:
 
}}
 
}}
 
|  valign="top" | {{Extension_Summary|
 
|  valign="top" | {{Extension_Summary|
name = Hello World Tutorial|
+
name = Picking Tutorial|
 
screenshot = H3DPlaceHolder.png|
 
screenshot = H3DPlaceHolder.png|
description = This tutorial introduces the basic steps necessary to setup horde3d|
+
description = This tutorial present how to pick an object in a 3D scene.|
 
version = 1.0|
 
version = 1.0|
 
horde3dversion = 1.0 beta|
 
horde3dversion = 1.0 beta|
Line 60: Line 67:
 
}}
 
}}
 
|}
 
|}
 +
[[category: Tutorials]]

Latest revision as of 16:19, 28 June 2010

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 mouse position
       app->pick( x / float( width ), (height - y) / float( height ) );
   }
}

To activate that listener we have to add the following code to the setupWindow method:

Activate mouse button listener
glfwSetMouseButtonCallback( mouseButtonListener );


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. Another conversion has to be made, since GLFW has it's origin in the upper left corner while Horde3D requires the origin to be in the lower left corner.

Then in the app.cpp we implement a call to the h3dutPickNode utility function.

Get the NodeHandle under the mouse cursor.
void Application::pick( float dX, float dY )
{
   H3DNode node = h3dutPickNode(_cam, dX, dY);
}
Picking Tutorial
H3DPlaceHolder.png
This tutorial present how to pick an object in a 3D scene.
Version: 1.0
Compatible with Horde3D: 1.0 beta
Release date: 2008-04-15
Author(s): Volker Wiendl